code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, XLMRobertaTokenizer from diffusers import AltDiffusionPipeline, AutoencoderKL, DDIMScheduler, PNDMScheduler, UNetaDConditionModel from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) 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 PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class A__ ( _A , _A , _A , unittest.TestCase ): A__ = AltDiffusionPipeline A__ = TEXT_TO_IMAGE_PARAMS A__ = TEXT_TO_IMAGE_BATCH_PARAMS A__ = TEXT_TO_IMAGE_IMAGE_PARAMS A__ = TEXT_TO_IMAGE_IMAGE_PARAMS def A ( self : Dict ) -> Optional[Any]: '''simple docstring''' torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE =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 =DDIMScheduler( beta_start=0.0_00_85 , beta_end=0.0_12 , beta_schedule='scaled_linear' , clip_sample=UpperCamelCase__ , set_alpha_to_one=UpperCamelCase__ , ) torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE =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 , ) # TODO: address the non-deterministic text encoder (fails for save-load tests) # torch.manual_seed(0) # text_encoder_config = RobertaSeriesConfig( # hidden_size=32, # project_dim=32, # intermediate_size=37, # layer_norm_eps=1e-05, # num_attention_heads=4, # num_hidden_layers=5, # vocab_size=5002, # ) # text_encoder = RobertaSeriesModelWithTransformation(text_encoder_config) torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE =CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , projection_dim=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=5002 , ) _SCREAMING_SNAKE_CASE =CLIPTextModel(UpperCamelCase__ ) _SCREAMING_SNAKE_CASE =XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta' ) _SCREAMING_SNAKE_CASE =77 _SCREAMING_SNAKE_CASE ={ 'unet': unet, 'scheduler': scheduler, 'vae': vae, 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'safety_checker': None, 'feature_extractor': None, } return components def A ( self : Optional[int] , _a : int , _a : Optional[Any]=0 ) -> Any: '''simple docstring''' if str(UpperCamelCase__ ).startswith('mps' ): _SCREAMING_SNAKE_CASE =torch.manual_seed(UpperCamelCase__ ) else: _SCREAMING_SNAKE_CASE =torch.Generator(device=UpperCamelCase__ ).manual_seed(UpperCamelCase__ ) _SCREAMING_SNAKE_CASE ={ 'prompt': 'A painting of a squirrel eating a burger', 'generator': generator, 'num_inference_steps': 2, 'guidance_scale': 6.0, 'output_type': 'numpy', } return inputs def A ( self : Union[str, Any] ) -> int: '''simple docstring''' super().test_attention_slicing_forward_pass(expected_max_diff=3e-3 ) def A ( self : List[Any] ) -> Union[str, Any]: '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) def A ( self : Optional[Any] ) -> str: '''simple docstring''' _SCREAMING_SNAKE_CASE ='cpu' # ensure determinism for the device-dependent torch.Generator _SCREAMING_SNAKE_CASE =self.get_dummy_components() torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE =RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=5002 , ) # TODO: remove after fixing the non-deterministic text encoder _SCREAMING_SNAKE_CASE =RobertaSeriesModelWithTransformation(UpperCamelCase__ ) _SCREAMING_SNAKE_CASE =text_encoder _SCREAMING_SNAKE_CASE =AltDiffusionPipeline(**UpperCamelCase__ ) _SCREAMING_SNAKE_CASE =alt_pipe.to(UpperCamelCase__ ) alt_pipe.set_progress_bar_config(disable=UpperCamelCase__ ) _SCREAMING_SNAKE_CASE =self.get_dummy_inputs(UpperCamelCase__ ) _SCREAMING_SNAKE_CASE ='A photo of an astronaut' _SCREAMING_SNAKE_CASE =alt_pipe(**UpperCamelCase__ ) _SCREAMING_SNAKE_CASE =output.images _SCREAMING_SNAKE_CASE =image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) _SCREAMING_SNAKE_CASE =np.array( [0.5_74_81_62, 0.60_44_71_45, 0.48_82_12_17, 0.50_10_06_36, 0.5_43_11_85, 0.45_76_36_83, 0.49_65_76_96, 0.48_13_27_33, 0.47_57_30_93] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def A ( self : Any ) -> int: '''simple docstring''' _SCREAMING_SNAKE_CASE ='cpu' # ensure determinism for the device-dependent torch.Generator _SCREAMING_SNAKE_CASE =self.get_dummy_components() _SCREAMING_SNAKE_CASE =PNDMScheduler(skip_prk_steps=UpperCamelCase__ ) torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE =RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=5002 , ) # TODO: remove after fixing the non-deterministic text encoder _SCREAMING_SNAKE_CASE =RobertaSeriesModelWithTransformation(UpperCamelCase__ ) _SCREAMING_SNAKE_CASE =text_encoder _SCREAMING_SNAKE_CASE =AltDiffusionPipeline(**UpperCamelCase__ ) _SCREAMING_SNAKE_CASE =alt_pipe.to(UpperCamelCase__ ) alt_pipe.set_progress_bar_config(disable=UpperCamelCase__ ) _SCREAMING_SNAKE_CASE =self.get_dummy_inputs(UpperCamelCase__ ) _SCREAMING_SNAKE_CASE =alt_pipe(**UpperCamelCase__ ) _SCREAMING_SNAKE_CASE =output.images _SCREAMING_SNAKE_CASE =image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) _SCREAMING_SNAKE_CASE =np.array( [0.51_60_50_93, 0.5_70_72_41, 0.47_36_55_07, 0.50_57_88_86, 0.5_63_38_77, 0.4_64_25_03, 0.5_18_20_81, 0.48_76_34_84, 0.49_08_42_37] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch_gpu class A__ ( unittest.TestCase ): def A ( self : Union[str, Any] ) -> List[Any]: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def A ( self : Tuple ) -> Tuple: '''simple docstring''' _SCREAMING_SNAKE_CASE =AltDiffusionPipeline.from_pretrained('BAAI/AltDiffusion' , safety_checker=UpperCamelCase__ ) _SCREAMING_SNAKE_CASE =alt_pipe.to(UpperCamelCase__ ) alt_pipe.set_progress_bar_config(disable=UpperCamelCase__ ) _SCREAMING_SNAKE_CASE ='A painting of a squirrel eating a burger' _SCREAMING_SNAKE_CASE =torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE =alt_pipe([prompt] , generator=UpperCamelCase__ , guidance_scale=6.0 , num_inference_steps=20 , output_type='np' ) _SCREAMING_SNAKE_CASE =output.images _SCREAMING_SNAKE_CASE =image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) _SCREAMING_SNAKE_CASE =np.array([0.10_10, 0.08_00, 0.07_94, 0.08_85, 0.08_43, 0.07_62, 0.07_69, 0.07_29, 0.05_86] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def A ( self : str ) -> Dict: '''simple docstring''' _SCREAMING_SNAKE_CASE =DDIMScheduler.from_pretrained('BAAI/AltDiffusion' , subfolder='scheduler' ) _SCREAMING_SNAKE_CASE =AltDiffusionPipeline.from_pretrained('BAAI/AltDiffusion' , scheduler=UpperCamelCase__ , safety_checker=UpperCamelCase__ ) _SCREAMING_SNAKE_CASE =alt_pipe.to(UpperCamelCase__ ) alt_pipe.set_progress_bar_config(disable=UpperCamelCase__ ) _SCREAMING_SNAKE_CASE ='A painting of a squirrel eating a burger' _SCREAMING_SNAKE_CASE =torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE =alt_pipe([prompt] , generator=UpperCamelCase__ , num_inference_steps=2 , output_type='numpy' ) _SCREAMING_SNAKE_CASE =output.images _SCREAMING_SNAKE_CASE =image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) _SCREAMING_SNAKE_CASE =np.array([0.40_19, 0.40_52, 0.38_10, 0.41_19, 0.39_16, 0.39_82, 0.46_51, 0.41_95, 0.53_23] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
47
import importlib import sys from argparse import REMAINDER, ArgumentParser from pathlib import Path import torch_xla.distributed.xla_multiprocessing as xmp def a__ ( ): '''simple docstring''' __magic_name__ = ArgumentParser( description=( """PyTorch TPU distributed training launch helper utility that will spawn up multiple distributed processes""" ) ) # Optional arguments for the launch helper parser.add_argument("""--num_cores""", type=A_, default=1, help="""Number of TPU cores to use (1 or 8).""" ) # positional parser.add_argument( """training_script""", type=A_, help=( """The full path to the single TPU training """ """program/script to be launched in parallel, """ """followed by all the arguments for the """ """training script""" ), ) # rest from the training program parser.add_argument("""training_script_args""", nargs=A_ ) return parser.parse_args() def a__ ( ): '''simple docstring''' __magic_name__ = parse_args() # Import training_script as a module. __magic_name__ = Path(args.training_script ) sys.path.append(str(script_fpath.parent.resolve() ) ) __magic_name__ = script_fpath.stem __magic_name__ = importlib.import_module(A_ ) # Patch sys.argv __magic_name__ = [args.training_script] + args.training_script_args + ["""--tpu_num_cores""", str(args.num_cores )] xmp.spawn(mod._mp_fn, args=(), nprocs=args.num_cores ) if __name__ == "__main__": main()
88
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 ( lowerCAmelCase__ ): '''simple docstring''' def __init__( self : List[Any] , _a : List[Any] , _a : str=13 , _a : List[str]=7 , _a : Dict=True , _a : str=True , _a : int=False , _a : List[str]=True , _a : Union[str, Any]=99 , _a : str=32 , _a : List[Any]=5 , _a : int=4 , _a : str=37 , _a : str="gelu" , _a : Union[str, Any]=0.1 , _a : Any=0.1 , _a : List[str]=512 , _a : Optional[Any]=16 , _a : Optional[Any]=2 , _a : Optional[int]=0.02 , _a : List[Any]=3 , _a : int=4 , _a : List[Any]=None , ): 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 ): 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 A_ ( self : List[Any] ): 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 A_ ( self : Optional[int] , _a : Dict , _a : List[Any] , _a : List[Any] , _a : int , _a : Optional[int] , _a : int ): UpperCamelCase__ = DistilBertModel(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() UpperCamelCase__ = model(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCamelCase__ = model(_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def A_ ( self : List[Any] , _a : List[Any] , _a : Union[str, Any] , _a : Union[str, Any] , _a : int , _a : List[Any] , _a : Optional[int] ): UpperCamelCase__ = DistilBertForMaskedLM(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() UpperCamelCase__ = model(_SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def A_ ( self : Dict , _a : Optional[int] , _a : Tuple , _a : Union[str, Any] , _a : Any , _a : Tuple , _a : List[Any] ): UpperCamelCase__ = DistilBertForQuestionAnswering(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() UpperCamelCase__ = model( _SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , start_positions=_SCREAMING_SNAKE_CASE , end_positions=_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def A_ ( self : Union[str, Any] , _a : Union[str, Any] , _a : Tuple , _a : Dict , _a : List[str] , _a : Optional[int] , _a : Optional[Any] ): UpperCamelCase__ = self.num_labels UpperCamelCase__ = DistilBertForSequenceClassification(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() UpperCamelCase__ = model(_SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def A_ ( self : Tuple , _a : List[Any] , _a : Any , _a : Union[str, Any] , _a : List[str] , _a : Dict , _a : List[Any] ): UpperCamelCase__ = self.num_labels UpperCamelCase__ = DistilBertForTokenClassification(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() UpperCamelCase__ = model(_SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def A_ ( self : List[str] , _a : Any , _a : Optional[Any] , _a : Optional[Any] , _a : Union[str, Any] , _a : List[str] , _a : Optional[int] ): UpperCamelCase__ = self.num_choices UpperCamelCase__ = DistilBertForMultipleChoice(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) 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( _SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def A_ ( self : Optional[int] ): 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 ( lowerCAmelCase__, lowerCAmelCase__, unittest.TestCase ): '''simple docstring''' _A : Dict = ( ( DistilBertModel, DistilBertForMaskedLM, DistilBertForMultipleChoice, DistilBertForQuestionAnswering, DistilBertForSequenceClassification, DistilBertForTokenClassification, ) if is_torch_available() else None ) _A : Optional[Any] = ( { "feature-extraction": DistilBertModel, "fill-mask": DistilBertForMaskedLM, "question-answering": DistilBertForQuestionAnswering, "text-classification": DistilBertForSequenceClassification, "token-classification": DistilBertForTokenClassification, "zero-shot": DistilBertForSequenceClassification, } if is_torch_available() else {} ) _A : Tuple = True _A : List[Any] = True _A : Any = True _A : str = True def A_ ( self : Any ): UpperCamelCase__ = DistilBertModelTester(self ) UpperCamelCase__ = ConfigTester(self , config_class=_SCREAMING_SNAKE_CASE , dim=37 ) def A_ ( self : List[Any] ): self.config_tester.run_common_tests() def A_ ( self : Optional[Any] ): UpperCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_model(*_SCREAMING_SNAKE_CASE ) def A_ ( self : str ): UpperCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_masked_lm(*_SCREAMING_SNAKE_CASE ) def A_ ( self : str ): UpperCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_question_answering(*_SCREAMING_SNAKE_CASE ) def A_ ( self : Dict ): UpperCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_sequence_classification(*_SCREAMING_SNAKE_CASE ) def A_ ( self : Dict ): UpperCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_token_classification(*_SCREAMING_SNAKE_CASE ) def A_ ( self : List[Any] ): UpperCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_multiple_choice(*_SCREAMING_SNAKE_CASE ) @slow def A_ ( self : List[str] ): for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase__ = DistilBertModel.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) @slow @require_torch_gpu def A_ ( self : List[Any] ): 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=_SCREAMING_SNAKE_CASE ) UpperCamelCase__ = self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCamelCase__ = torch.jit.trace( _SCREAMING_SNAKE_CASE , (inputs_dict['''input_ids'''].to('''cpu''' ), inputs_dict['''attention_mask'''].to('''cpu''' )) ) with tempfile.TemporaryDirectory() as tmp: torch.jit.save(_SCREAMING_SNAKE_CASE , os.path.join(_SCREAMING_SNAKE_CASE , '''traced_model.pt''' ) ) UpperCamelCase__ = torch.jit.load(os.path.join(_SCREAMING_SNAKE_CASE , '''traced_model.pt''' ) , map_location=_SCREAMING_SNAKE_CASE ) loaded(inputs_dict['''input_ids'''].to(_SCREAMING_SNAKE_CASE ) , inputs_dict['''attention_mask'''].to(_SCREAMING_SNAKE_CASE ) ) @require_torch class __lowercase ( unittest.TestCase ): '''simple docstring''' @slow def A_ ( self : Dict ): UpperCamelCase__ = DistilBertModel.from_pretrained('''distilbert-base-uncased''' ) UpperCamelCase__ = torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]] ) UpperCamelCase__ = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): UpperCamelCase__ = model(_SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE )[0] UpperCamelCase__ = torch.Size((1, 11, 768) ) self.assertEqual(output.shape , _SCREAMING_SNAKE_CASE ) UpperCamelCase__ = torch.tensor( [[[-0.1639, 0.3299, 0.1648], [-0.1746, 0.3289, 0.1710], [-0.1884, 0.3357, 0.1810]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , _SCREAMING_SNAKE_CASE , atol=1E-4 ) )
354
import numpy as np from scipy.spatial.distance import cdist from sklearn.metrics import fa_score import datasets lowercase = """\ @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}, } """ lowercase = """\ 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. """ lowercase = """ 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_ ( UpperCamelCase__ : Union[str, Any], UpperCamelCase__ : Tuple ): '''simple docstring''' return float((preds == labels).mean() ) def lowerCamelCase_ ( UpperCamelCase__ : str, UpperCamelCase__ : Dict ): '''simple docstring''' UpperCamelCase__ = simple_accuracy(UpperCamelCase__, UpperCamelCase__ ) UpperCamelCase__ = float(fa_score(y_true=UpperCamelCase__, y_pred=UpperCamelCase__ ) ) return { "accuracy": acc, "f1": fa, } def lowerCamelCase_ ( UpperCamelCase__ : Union[str, Any], UpperCamelCase__ : str ): '''simple docstring''' UpperCamelCase__ = np.array(UpperCamelCase__ ) UpperCamelCase__ = np.array(UpperCamelCase__ ) UpperCamelCase__ = en_sentvecs.shape[0] # mean centering UpperCamelCase__ = en_sentvecs - np.mean(UpperCamelCase__, axis=0 ) UpperCamelCase__ = in_sentvecs - np.mean(UpperCamelCase__, axis=0 ) UpperCamelCase__ = cdist(UpperCamelCase__, UpperCamelCase__, '''cosine''' ) UpperCamelCase__ = np.array(range(UpperCamelCase__ ) ) UpperCamelCase__ = sim.argsort(axis=1 )[:, :10] UpperCamelCase__ = np.any(preds == actual[:, None], axis=1 ) return float(matches.mean() ) @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION ) class __lowercase ( datasets.Metric ): '''simple docstring''' def A_ ( self : Optional[Any] ): 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 A_ ( self : str , _a : Dict , _a : Tuple ): if self.config_name == "cvit-mkb-clsr": return {"precision@10": precision_at_aa(_a , _a )} elif self.config_name in ["wiki-ner"]: return acc_and_fa(_a , _a ) elif self.config_name in [ "wnli", "copa", "sna", "csqa", "wstp", "inltkh", "bbca", "iitp-mr", "iitp-pr", "actsa-sc", "md", ]: return {"accuracy": simple_accuracy(_a , _a )} 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"]''' )
35
0
import copy from typing import Any, Dict, List, Optional, Union import numpy as np import torch from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import TensorType, logging __A : Optional[Any] = logging.get_logger(__name__) class _SCREAMING_SNAKE_CASE ( lowerCAmelCase__): _UpperCamelCase:Tuple = ["input_features", "is_longer"] def __init__( self , _SCREAMING_SNAKE_CASE=64 , _SCREAMING_SNAKE_CASE=4_8000 , _SCREAMING_SNAKE_CASE=480 , _SCREAMING_SNAKE_CASE=10 , _SCREAMING_SNAKE_CASE=1024 , _SCREAMING_SNAKE_CASE=0.0 , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = 1_4000 , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = "fusion" , _SCREAMING_SNAKE_CASE = "repeatpad" , **_SCREAMING_SNAKE_CASE , )-> Optional[Any]: super().__init__( feature_size=_SCREAMING_SNAKE_CASE , sampling_rate=_SCREAMING_SNAKE_CASE , padding_value=_SCREAMING_SNAKE_CASE , return_attention_mask=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) lowerCamelCase_ =top_db lowerCamelCase_ =truncation lowerCamelCase_ =padding lowerCamelCase_ =fft_window_size lowerCamelCase_ =(fft_window_size >> 1) + 1 lowerCamelCase_ =hop_length lowerCamelCase_ =max_length_s lowerCamelCase_ =max_length_s * sampling_rate lowerCamelCase_ =sampling_rate lowerCamelCase_ =frequency_min lowerCamelCase_ =frequency_max lowerCamelCase_ =mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_SCREAMING_SNAKE_CASE , min_frequency=_SCREAMING_SNAKE_CASE , max_frequency=_SCREAMING_SNAKE_CASE , sampling_rate=_SCREAMING_SNAKE_CASE , norm=_SCREAMING_SNAKE_CASE , mel_scale="""htk""" , ) lowerCamelCase_ =mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_SCREAMING_SNAKE_CASE , min_frequency=_SCREAMING_SNAKE_CASE , max_frequency=_SCREAMING_SNAKE_CASE , sampling_rate=_SCREAMING_SNAKE_CASE , norm="""slaney""" , mel_scale="""slaney""" , ) def _snake_case ( self )-> Dict[str, Any]: lowerCamelCase_ =copy.deepcopy(self.__dict__ ) lowerCamelCase_ =self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] if "mel_filters_slaney" in output: del output["mel_filters_slaney"] return output def _snake_case ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None )-> np.ndarray: lowerCamelCase_ =spectrogram( _SCREAMING_SNAKE_CASE , window_function(self.fft_window_size , """hann""" ) , frame_length=self.fft_window_size , hop_length=self.hop_length , power=2.0 , mel_filters=_SCREAMING_SNAKE_CASE , log_mel="""dB""" , ) return log_mel_spectrogram.T def _snake_case ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE )-> Optional[Any]: lowerCamelCase_ =np.array_split(list(range(0 , total_frames - chunk_frames + 1 ) ) , 3 ) if len(ranges[1] ) == 0: # if the audio is too short, we just use the first chunk lowerCamelCase_ =[0] if len(ranges[2] ) == 0: # if the audio is too short, we just use the first chunk lowerCamelCase_ =[0] # randomly choose index for each part lowerCamelCase_ =np.random.choice(ranges[0] ) lowerCamelCase_ =np.random.choice(ranges[1] ) lowerCamelCase_ =np.random.choice(ranges[2] ) lowerCamelCase_ =mel[idx_front : idx_front + chunk_frames, :] lowerCamelCase_ =mel[idx_middle : idx_middle + chunk_frames, :] lowerCamelCase_ =mel[idx_back : idx_back + chunk_frames, :] lowerCamelCase_ =torch.tensor(mel[None, None, :] ) lowerCamelCase_ =torch.nn.functional.interpolate( _SCREAMING_SNAKE_CASE , size=[chunk_frames, 64] , mode="""bilinear""" , align_corners=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =mel_shrink[0][0].numpy() lowerCamelCase_ =np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back] , axis=0 ) return mel_fusion def _snake_case ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE )-> np.array: if waveform.shape[0] > max_length: if truncation == "rand_trunc": lowerCamelCase_ =True # random crop to max_length (for compatibility) -> this should be handled by self.pad lowerCamelCase_ =len(_SCREAMING_SNAKE_CASE ) - max_length lowerCamelCase_ =np.random.randint(0 , overflow + 1 ) lowerCamelCase_ =waveform[idx : idx + max_length] lowerCamelCase_ =self._np_extract_fbank_features(_SCREAMING_SNAKE_CASE , self.mel_filters_slaney )[None, :] elif truncation == "fusion": lowerCamelCase_ =self._np_extract_fbank_features(_SCREAMING_SNAKE_CASE , self.mel_filters ) lowerCamelCase_ =max_length // self.hop_length + 1 # the +1 related to how the spectrogram is computed lowerCamelCase_ =mel.shape[0] if chunk_frames == total_frames: # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length. # In this case, we just use the whole audio. lowerCamelCase_ =np.stack([mel, mel, mel, mel] , axis=0 ) lowerCamelCase_ =False else: lowerCamelCase_ =self._random_mel_fusion(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowerCamelCase_ =True else: raise NotImplementedError(f'data_truncating {truncation} not implemented' ) else: lowerCamelCase_ =False # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding if waveform.shape[0] < max_length: if padding == "repeat": lowerCamelCase_ =int(max_length / len(_SCREAMING_SNAKE_CASE ) ) lowerCamelCase_ =np.stack(np.tile(_SCREAMING_SNAKE_CASE , n_repeat + 1 ) )[:max_length] if padding == "repeatpad": lowerCamelCase_ =int(max_length / len(_SCREAMING_SNAKE_CASE ) ) lowerCamelCase_ =np.stack(np.tile(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) lowerCamelCase_ =np.pad(_SCREAMING_SNAKE_CASE , (0, max_length - waveform.shape[0]) , mode="""constant""" , constant_values=0 ) if truncation == "fusion": lowerCamelCase_ =self._np_extract_fbank_features(_SCREAMING_SNAKE_CASE , self.mel_filters ) lowerCamelCase_ =np.stack([input_mel, input_mel, input_mel, input_mel] , axis=0 ) else: lowerCamelCase_ =self._np_extract_fbank_features(_SCREAMING_SNAKE_CASE , self.mel_filters_slaney )[None, :] return input_mel, longer def __call__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , )-> BatchFeature: lowerCamelCase_ =truncation if truncation is not None else self.truncation lowerCamelCase_ =padding if padding else self.padding if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f'The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a' f' sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input' f' was sampled with {self.sampling_rate} and not {sampling_rate}.' ) else: logger.warning( """It is strongly recommended to pass the `sampling_rate` argument to this function. """ """Failing to do so can result in silent errors that might be hard to debug.""" ) lowerCamelCase_ =isinstance(_SCREAMING_SNAKE_CASE , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f'Only mono-channel audio is supported for input to {self}' ) lowerCamelCase_ =is_batched_numpy or ( isinstance(_SCREAMING_SNAKE_CASE , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: lowerCamelCase_ =[np.asarray(_SCREAMING_SNAKE_CASE , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(_SCREAMING_SNAKE_CASE , np.ndarray ): lowerCamelCase_ =np.asarray(_SCREAMING_SNAKE_CASE , dtype=np.floataa ) elif isinstance(_SCREAMING_SNAKE_CASE , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): lowerCamelCase_ =raw_speech.astype(np.floataa ) # always return batch if not is_batched: lowerCamelCase_ =[np.asarray(_SCREAMING_SNAKE_CASE )] # convert to mel spectrogram, truncate and pad if needed. lowerCamelCase_ =[ self._get_input_mel(_SCREAMING_SNAKE_CASE , max_length if max_length else self.nb_max_samples , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) for waveform in raw_speech ] lowerCamelCase_ =[] lowerCamelCase_ =[] for mel, longer in padded_inputs: input_mel.append(_SCREAMING_SNAKE_CASE ) is_longer.append(_SCREAMING_SNAKE_CASE ) if truncation == "fusion" and sum(_SCREAMING_SNAKE_CASE ) == 0: # if no audio is longer than 10s, then randomly select one audio to be longer lowerCamelCase_ =np.random.randint(0 , len(_SCREAMING_SNAKE_CASE ) ) lowerCamelCase_ =True if isinstance(input_mel[0] , _SCREAMING_SNAKE_CASE ): lowerCamelCase_ =[np.asarray(_SCREAMING_SNAKE_CASE , dtype=np.floataa ) for feature in input_mel] # is_longer is a list of bool lowerCamelCase_ =[[longer] for longer in is_longer] lowerCamelCase_ ={"""input_features""": input_mel, """is_longer""": is_longer} lowerCamelCase_ =BatchFeature(_SCREAMING_SNAKE_CASE ) if return_tensors is not None: lowerCamelCase_ =input_features.convert_to_tensors(_SCREAMING_SNAKE_CASE ) return input_features
154
def __UpperCamelCase ( _A : float , _A : int ) ->float: """simple docstring""" if digit_amount > 0: return round(number - int(_A ) , _A ) return number - int(_A ) if __name__ == "__main__": print(decimal_isolate(1.53, 0)) print(decimal_isolate(35.3_45, 1)) print(decimal_isolate(35.3_45, 2)) print(decimal_isolate(35.3_45, 3)) print(decimal_isolate(-14.7_89, 3)) print(decimal_isolate(0, 2)) print(decimal_isolate(-14.1_23, 1)) print(decimal_isolate(-14.1_23, 2)) print(decimal_isolate(-14.1_23, 3))
154
1
'''simple docstring''' import importlib import shutil import threading import warnings from typing import List import fsspec import fsspec.asyn from . import compression from .hffilesystem import HfFileSystem a_ : str = importlib.util.find_spec("s3fs") is not None if _has_safs: from .safilesystem import SaFileSystem # noqa: F401 a_ : List[compression.BaseCompressedFileFileSystem] = [ compression.BzaFileSystem, compression.GzipFileSystem, compression.LzaFileSystem, compression.XzFileSystem, compression.ZstdFileSystem, ] # Register custom filesystems for fs_class in COMPRESSION_FILESYSTEMS + [HfFileSystem]: if fs_class.protocol in fsspec.registry and fsspec.registry[fs_class.protocol] is not fs_class: warnings.warn(f'''A filesystem protocol was already set for {fs_class.protocol} and will be overwritten.''') fsspec.register_implementation(fs_class.protocol, fs_class, clobber=True) def _A (lowerCAmelCase__ :str ) -> str: '''simple docstring''' if "://" in dataset_path: _a = dataset_path.split('://' )[1] return dataset_path def _A (lowerCAmelCase__ :fsspec.AbstractFileSystem ) -> bool: '''simple docstring''' if fs is not None and fs.protocol != "file": return True else: return False def _A (lowerCAmelCase__ :fsspec.AbstractFileSystem , lowerCAmelCase__ :str , lowerCAmelCase__ :str ) -> List[str]: '''simple docstring''' _a = not is_remote_filesystem(lowerCAmelCase__ ) if is_local: # LocalFileSystem.mv does copy + rm, it is more efficient to simply move a local directory shutil.move(fs._strip_protocol(lowerCAmelCase__ ) , fs._strip_protocol(lowerCAmelCase__ ) ) else: fs.mv(lowerCAmelCase__ , lowerCAmelCase__ , recursive=lowerCAmelCase__ ) def _A () -> None: '''simple docstring''' if hasattr(fsspec.asyn , 'reset_lock' ): # for future fsspec>2022.05.0 fsspec.asyn.reset_lock() else: _a = None _a = None _a = threading.Lock()
104
'''simple docstring''' from __future__ import annotations def _A (lowerCAmelCase__ :int ) -> list[int]: '''simple docstring''' _a = 2 _a = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(lowerCAmelCase__ ) if n > 1: factors.append(lowerCAmelCase__ ) return factors if __name__ == "__main__": import doctest doctest.testmod()
104
1
'''simple docstring''' from ..utils import DummyObject, requires_backends class __a (metaclass=lowerCamelCase ): __a : List[Any] = ["speech"] def __init__( self : Optional[int] , *__magic_name__ : Optional[Any] , **__magic_name__ : List[Any] ) -> Any: """simple docstring""" requires_backends(self , ['''speech'''] ) class __a (metaclass=lowerCamelCase ): __a : int = ["speech"] def __init__( self : Any , *__magic_name__ : List[str] , **__magic_name__ : List[Any] ) -> Dict: """simple docstring""" requires_backends(self , ['''speech'''] )
125
import tempfile import unittest import numpy as np from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import BertConfig, is_flax_available from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax if is_flax_available(): import os from flax.core.frozen_dict import unfreeze from flax.traverse_util import flatten_dict from transformers import FlaxBertModel A : int = '0.12' # assumed parallelism: 8 @require_flax @is_staging_test class __A( unittest.TestCase ): @classmethod def SCREAMING_SNAKE_CASE_ ( cls ) -> Union[str, Any]: '''simple docstring''' __a = TOKEN HfFolder.save_token(_snake_case ) @classmethod def SCREAMING_SNAKE_CASE_ ( cls ) -> Union[str, Any]: '''simple docstring''' try: delete_repo(token=cls._token , repo_id='''test-model-flax''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-model-flax-org''' ) except HTTPError: pass def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' __a = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) __a = FlaxBertModel(_snake_case ) model.push_to_hub('''test-model-flax''' , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained(F"""{USER}/test-model-flax""" ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_snake_case , 1E-3 , msg=F"""{key} not identical""" ) # Reset repo delete_repo(token=self._token , repo_id='''test-model-flax''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(_snake_case , repo_id='''test-model-flax''' , push_to_hub=_snake_case , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained(F"""{USER}/test-model-flax""" ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_snake_case , 1E-3 , msg=F"""{key} not identical""" ) def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[int]: '''simple docstring''' __a = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) __a = FlaxBertModel(_snake_case ) model.push_to_hub('''valid_org/test-model-flax-org''' , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_snake_case , 1E-3 , msg=F"""{key} not identical""" ) # Reset repo delete_repo(token=self._token , repo_id='''valid_org/test-model-flax-org''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained( _snake_case , repo_id='''valid_org/test-model-flax-org''' , push_to_hub=_snake_case , use_auth_token=self._token ) __a = FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' ) __a = flatten_dict(unfreeze(model.params ) ) __a = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): __a = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_snake_case , 1E-3 , msg=F"""{key} not identical""" ) def __lowerCAmelCase ( a__ , a__ ) -> str: __a = True __a = flatten_dict(modela.params ) __a = flatten_dict(modela.params ) for key in flat_params_a.keys(): if np.sum(np.abs(flat_params_a[key] - flat_params_a[key] ) ) > 1e-4: __a = False return models_are_equal @require_flax class __A( unittest.TestCase ): def SCREAMING_SNAKE_CASE_ ( self ) -> Union[str, Any]: '''simple docstring''' __a = BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' ) __a = FlaxBertModel(_snake_case ) __a = '''bert''' with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(_snake_case , _snake_case ) ) with self.assertRaises(_snake_case ): __a = FlaxBertModel.from_pretrained(_snake_case ) __a = FlaxBertModel.from_pretrained(_snake_case , subfolder=_snake_case ) self.assertTrue(check_models_equal(_snake_case , _snake_case ) ) def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[Any]: '''simple docstring''' __a = BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' ) __a = FlaxBertModel(_snake_case ) __a = '''bert''' with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(_snake_case , _snake_case ) , max_shard_size='''10KB''' ) with self.assertRaises(_snake_case ): __a = FlaxBertModel.from_pretrained(_snake_case ) __a = FlaxBertModel.from_pretrained(_snake_case , subfolder=_snake_case ) self.assertTrue(check_models_equal(_snake_case , _snake_case ) ) def SCREAMING_SNAKE_CASE_ ( self ) -> int: '''simple docstring''' __a = '''bert''' __a = '''hf-internal-testing/tiny-random-bert-subfolder''' with self.assertRaises(_snake_case ): __a = FlaxBertModel.from_pretrained(_snake_case ) __a = FlaxBertModel.from_pretrained(_snake_case , subfolder=_snake_case ) self.assertIsNotNone(_snake_case ) def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[int]: '''simple docstring''' __a = '''bert''' __a = '''hf-internal-testing/tiny-random-bert-sharded-subfolder''' with self.assertRaises(_snake_case ): __a = FlaxBertModel.from_pretrained(_snake_case ) __a = FlaxBertModel.from_pretrained(_snake_case , subfolder=_snake_case ) self.assertIsNotNone(_snake_case )
6
0
'''simple docstring''' from __future__ import annotations from decimal import Decimal from math import * # noqa: F403 from sympy import diff def lowerCAmelCase__ ( lowerCamelCase : Tuple ,lowerCamelCase : str ,lowerCamelCase : Dict = 10**-10 ): _A : Union[str, Any] = a while True: _A : Optional[int] = Decimal(__SCREAMING_SNAKE_CASE ) - ( Decimal(eval(__SCREAMING_SNAKE_CASE ) ) / Decimal(eval(str(diff(__SCREAMING_SNAKE_CASE ) ) ) ) # noqa: S307 ) # This number dictates the accuracy of the answer if abs(eval(__SCREAMING_SNAKE_CASE ) ) < precision: # noqa: S307 return float(__SCREAMING_SNAKE_CASE ) # Let's Execute if __name__ == "__main__": # Find root of trigonometric function # Find value of pi print(f"""The root of sin(x) = 0 is {newton_raphson("sin(x)", 2)}""") # Find root of polynomial print(f"""The root of x**2 - 5*x + 2 = 0 is {newton_raphson("x**2 - 5*x + 2", 0.4)}""") # Find Square Root of 5 print(f"""The root of log(x) - 1 = 0 is {newton_raphson("log(x) - 1", 2)}""") # Exponential Roots print(f"""The root of exp(x) - 1 = 0 is {newton_raphson("exp(x) - 1", 0)}""")
370
'''simple docstring''' def lowerCAmelCase__ ( lowerCamelCase : int = 10 ): if not isinstance(lowerCamelCase ,lowerCamelCase ) or n < 0: raise ValueError('Invalid input' ) _A : Optional[Any] = 10**n _A : List[str] = 28433 * (pow(2 ,7830457 ,lowerCamelCase )) + 1 return str(number % modulus ) if __name__ == "__main__": from doctest import testmod testmod() print(f"""{solution(10) = }""")
227
0
import darl # noqa import gym import tqdm from diffusers.experimental import ValueGuidedRLPipeline snake_case_ = { '''n_samples''': 64, '''horizon''': 32, '''num_inference_steps''': 20, '''n_guide_steps''': 2, # can set to 0 for faster sampling, does not use value network '''scale_grad_by_std''': True, '''scale''': 0.1, '''eta''': 0.0, '''t_grad_cutoff''': 2, '''device''': '''cpu''', } if __name__ == "__main__": snake_case_ = '''hopper-medium-v2''' snake_case_ = gym.make(env_name) snake_case_ = ValueGuidedRLPipeline.from_pretrained( '''bglick13/hopper-medium-v2-value-function-hor32''', env=env, ) env.seed(0) snake_case_ = env.reset() snake_case_ = 0 snake_case_ = 0 snake_case_ = 1_000 snake_case_ = [obs.copy()] try: for t in tqdm.tqdm(range(T)): # call the policy snake_case_ = pipeline(obs, planning_horizon=32) # execute action in environment snake_case_ , snake_case_ , snake_case_ , snake_case_ = env.step(denorm_actions) snake_case_ = env.get_normalized_score(total_reward) # update return total_reward += reward total_score += score print( F'''Step: {t}, Reward: {reward}, Total Reward: {total_reward}, Score: {score}, Total Score:''' F''' {total_score}''' ) # save observations for rendering rollout.append(next_observation.copy()) snake_case_ = next_observation except KeyboardInterrupt: pass print(F'''Total reward: {total_reward}''')
214
def snake_case__ ( SCREAMING_SNAKE_CASE_ : str = "The quick brown fox jumps over the lazy dog" , ): '''simple docstring''' lowercase__ : str = set() # Replace all the whitespace in our sentence lowercase__ : Tuple = input_str.replace(' ' , '' ) for alpha in input_str: if "a" <= alpha.lower() <= "z": frequency.add(alpha.lower() ) return len(SCREAMING_SNAKE_CASE_ ) == 26 def snake_case__ ( SCREAMING_SNAKE_CASE_ : str = "The quick brown fox jumps over the lazy dog" , ): '''simple docstring''' lowercase__ : Dict = [False] * 26 for char in input_str: if char.islower(): lowercase__ : List[Any] = True elif char.isupper(): lowercase__ : Optional[Any] = True return all(SCREAMING_SNAKE_CASE_ ) def snake_case__ ( SCREAMING_SNAKE_CASE_ : str = "The quick brown fox jumps over the lazy dog" , ): '''simple docstring''' return len({char for char in input_str.lower() if char.isalpha()} ) == 26 def snake_case__ ( ): '''simple docstring''' from timeit import timeit lowercase__ : Union[str, Any] = 'from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest' print(timeit('is_pangram()' , setup=SCREAMING_SNAKE_CASE_ ) ) print(timeit('is_pangram_faster()' , setup=SCREAMING_SNAKE_CASE_ ) ) print(timeit('is_pangram_fastest()' , setup=SCREAMING_SNAKE_CASE_ ) ) # 5.348480500048026, 2.6477354579837993, 1.8470395830227062 # 5.036091582966037, 2.644472333951853, 1.8869528750656173 if __name__ == "__main__": import doctest doctest.testmod() benchmark()
214
1
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging A_ :Dict = logging.get_logger(__name__) A_ :Optional[Any] = {'vocab_file': 'spiece.model'} A_ :Tuple = { 'vocab_file': { 'bert_for_seq_generation': ( 'https://huggingface.co/google/bert_for_seq_generation_L-24_bbc_encoder/resolve/main/spiece.model' ), } } A_ :List[Any] = {'bert_for_seq_generation': 512} class __A ( _lowerCamelCase ): """simple docstring""" UpperCamelCase__ : Any =VOCAB_FILES_NAMES UpperCamelCase__ : Tuple =PRETRAINED_VOCAB_FILES_MAP UpperCamelCase__ : List[str] =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase__ : str =[] UpperCamelCase__ : Any =["""input_ids""", """attention_mask"""] def __init__( self , lowerCamelCase__ , lowerCamelCase__="<s>" , lowerCamelCase__="</s>" , lowerCamelCase__="<unk>" , lowerCamelCase__="<pad>" , lowerCamelCase__="<::::>" , lowerCamelCase__ = None , **lowerCamelCase__ , ): """simple docstring""" __UpperCamelCase : List[str] ={} if sp_model_kwargs is None else sp_model_kwargs # Add extra_ids to the special token list super().__init__( bos_token=lowerCamelCase__ , eos_token=lowerCamelCase__ , unk_token=lowerCamelCase__ , pad_token=lowerCamelCase__ , sep_token=lowerCamelCase__ , sp_model_kwargs=self.sp_model_kwargs , **lowerCamelCase__ , ) __UpperCamelCase : Tuple =vocab_file __UpperCamelCase : Tuple =spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(lowerCamelCase__ ) @property def __lowercase ( self ): """simple docstring""" return self.sp_model.get_piece_size() def __lowercase ( self ): """simple docstring""" __UpperCamelCase : str ={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 ): """simple docstring""" __UpperCamelCase : Optional[Any] =self.__dict__.copy() __UpperCamelCase : Optional[Any] =None return state def __setstate__( self , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase : Tuple =d # for backward compatibility if not hasattr(self , 'sp_model_kwargs' ): __UpperCamelCase : Optional[int] ={} __UpperCamelCase : Optional[Any] =spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __lowercase ( self , lowerCamelCase__ ): """simple docstring""" return self.sp_model.encode(lowerCamelCase__ , out_type=lowerCamelCase__ ) def __lowercase ( self , lowerCamelCase__ ): """simple docstring""" return self.sp_model.piece_to_id(lowerCamelCase__ ) def __lowercase ( self , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase : List[str] =self.sp_model.IdToPiece(lowerCamelCase__ ) return token def __lowercase ( self , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase : str =[] __UpperCamelCase : Union[str, Any] ='' for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: out_string += self.sp_model.decode(lowerCamelCase__ ) + token __UpperCamelCase : Dict =[] else: current_sub_tokens.append(lowerCamelCase__ ) out_string += self.sp_model.decode(lowerCamelCase__ ) return out_string.strip() def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__ = None ): """simple docstring""" if not os.path.isdir(lowerCamelCase__ ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return __UpperCamelCase : str =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__ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , lowerCamelCase__ ) elif not os.path.isfile(self.vocab_file ): with open(lowerCamelCase__ , 'wb' ) as fi: __UpperCamelCase : Optional[Any] =self.sp_model.serialized_model_proto() fi.write(lowerCamelCase__ ) return (out_vocab_file,)
353
import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, XLMRobertaTokenizer from diffusers import AltDiffusionPipeline, AutoencoderKL, DDIMScheduler, PNDMScheduler, UNetaDConditionModel from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) 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 PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __A ( a , a , a , unittest.TestCase ): """simple docstring""" UpperCamelCase__ : List[Any] =AltDiffusionPipeline UpperCamelCase__ : Optional[Any] =TEXT_TO_IMAGE_PARAMS UpperCamelCase__ : Any =TEXT_TO_IMAGE_BATCH_PARAMS UpperCamelCase__ : Any =TEXT_TO_IMAGE_IMAGE_PARAMS UpperCamelCase__ : Dict =TEXT_TO_IMAGE_IMAGE_PARAMS def __lowercase ( self ): """simple docstring""" torch.manual_seed(0 ) __UpperCamelCase : Union[str, Any] =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 , ) __UpperCamelCase : Tuple =DDIMScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule='scaled_linear' , clip_sample=lowerCamelCase__ , set_alpha_to_one=lowerCamelCase__ , ) torch.manual_seed(0 ) __UpperCamelCase : Dict =AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] , up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] , latent_channels=4 , ) # TODO: address the non-deterministic text encoder (fails for save-load tests) # torch.manual_seed(0) # text_encoder_config = RobertaSeriesConfig( # hidden_size=32, # project_dim=32, # intermediate_size=37, # layer_norm_eps=1e-05, # num_attention_heads=4, # num_hidden_layers=5, # vocab_size=5002, # ) # text_encoder = RobertaSeriesModelWithTransformation(text_encoder_config) torch.manual_seed(0 ) __UpperCamelCase : Union[str, Any] =CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , projection_dim=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=5002 , ) __UpperCamelCase : Optional[int] =CLIPTextModel(lowerCamelCase__ ) __UpperCamelCase : int =XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta' ) __UpperCamelCase : Dict =77 __UpperCamelCase : List[str] ={ 'unet': unet, 'scheduler': scheduler, 'vae': vae, 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'safety_checker': None, 'feature_extractor': None, } return components def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__=0 ): """simple docstring""" if str(lowerCamelCase__ ).startswith('mps' ): __UpperCamelCase : Optional[Any] =torch.manual_seed(lowerCamelCase__ ) else: __UpperCamelCase : List[Any] =torch.Generator(device=lowerCamelCase__ ).manual_seed(lowerCamelCase__ ) __UpperCamelCase : Tuple ={ 'prompt': 'A painting of a squirrel eating a burger', 'generator': generator, 'num_inference_steps': 2, 'guidance_scale': 6.0, 'output_type': 'numpy', } return inputs def __lowercase ( self ): """simple docstring""" super().test_attention_slicing_forward_pass(expected_max_diff=3E-3 ) def __lowercase ( self ): """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[Any] ='cpu' # ensure determinism for the device-dependent torch.Generator __UpperCamelCase : List[str] =self.get_dummy_components() torch.manual_seed(0 ) __UpperCamelCase : List[str] =RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=5002 , ) # TODO: remove after fixing the non-deterministic text encoder __UpperCamelCase : List[str] =RobertaSeriesModelWithTransformation(lowerCamelCase__ ) __UpperCamelCase : Any =text_encoder __UpperCamelCase : int =AltDiffusionPipeline(**lowerCamelCase__ ) __UpperCamelCase : Any =alt_pipe.to(lowerCamelCase__ ) alt_pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Optional[Any] =self.get_dummy_inputs(lowerCamelCase__ ) __UpperCamelCase : Tuple ='A photo of an astronaut' __UpperCamelCase : str =alt_pipe(**lowerCamelCase__ ) __UpperCamelCase : Any =output.images __UpperCamelCase : Any =image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __UpperCamelCase : Dict =np.array( [0.5_748_162, 0.60_447_145, 0.48_821_217, 0.50_100_636, 0.5_431_185, 0.45_763_683, 0.49_657_696, 0.48_132_733, 0.47_573_093] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[int] ='cpu' # ensure determinism for the device-dependent torch.Generator __UpperCamelCase : Optional[int] =self.get_dummy_components() __UpperCamelCase : int =PNDMScheduler(skip_prk_steps=lowerCamelCase__ ) torch.manual_seed(0 ) __UpperCamelCase : Optional[Any] =RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=5002 , ) # TODO: remove after fixing the non-deterministic text encoder __UpperCamelCase : Tuple =RobertaSeriesModelWithTransformation(lowerCamelCase__ ) __UpperCamelCase : List[str] =text_encoder __UpperCamelCase : List[str] =AltDiffusionPipeline(**lowerCamelCase__ ) __UpperCamelCase : Dict =alt_pipe.to(lowerCamelCase__ ) alt_pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Dict =self.get_dummy_inputs(lowerCamelCase__ ) __UpperCamelCase : Optional[Any] =alt_pipe(**lowerCamelCase__ ) __UpperCamelCase : str =output.images __UpperCamelCase : Optional[int] =image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __UpperCamelCase : Dict =np.array( [0.51_605_093, 0.5_707_241, 0.47_365_507, 0.50_578_886, 0.5_633_877, 0.4_642_503, 0.5_182_081, 0.48_763_484, 0.49_084_237] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @slow @require_torch_gpu class __A ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Dict =AltDiffusionPipeline.from_pretrained('BAAI/AltDiffusion' , safety_checker=lowerCamelCase__ ) __UpperCamelCase : List[str] =alt_pipe.to(lowerCamelCase__ ) alt_pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Any ='A painting of a squirrel eating a burger' __UpperCamelCase : Optional[int] =torch.manual_seed(0 ) __UpperCamelCase : Dict =alt_pipe([prompt] , generator=lowerCamelCase__ , guidance_scale=6.0 , num_inference_steps=20 , output_type='np' ) __UpperCamelCase : List[str] =output.images __UpperCamelCase : int =image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) __UpperCamelCase : Optional[int] =np.array([0.1_010, 0.0_800, 0.0_794, 0.0_885, 0.0_843, 0.0_762, 0.0_769, 0.0_729, 0.0_586] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =DDIMScheduler.from_pretrained('BAAI/AltDiffusion' , subfolder='scheduler' ) __UpperCamelCase : Tuple =AltDiffusionPipeline.from_pretrained('BAAI/AltDiffusion' , scheduler=lowerCamelCase__ , safety_checker=lowerCamelCase__ ) __UpperCamelCase : Any =alt_pipe.to(lowerCamelCase__ ) alt_pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __UpperCamelCase : Optional[Any] ='A painting of a squirrel eating a burger' __UpperCamelCase : Optional[int] =torch.manual_seed(0 ) __UpperCamelCase : Tuple =alt_pipe([prompt] , generator=lowerCamelCase__ , num_inference_steps=2 , output_type='numpy' ) __UpperCamelCase : List[str] =output.images __UpperCamelCase : Optional[Any] =image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) __UpperCamelCase : List[str] =np.array([0.4_019, 0.4_052, 0.3_810, 0.4_119, 0.3_916, 0.3_982, 0.4_651, 0.4_195, 0.5_323] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
245
0
"""simple docstring""" from __future__ import annotations from typing import Any def lowercase_ ( _snake_case ): if not postfix_notation: return 0 SCREAMING_SNAKE_CASE__ : Optional[Any] = {"""+""", """-""", """*""", """/"""} SCREAMING_SNAKE_CASE__ : list[Any] = [] for token in postfix_notation: if token in operations: SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Optional[Any] = stack.pop(), stack.pop() if token == "+": stack.append(a + b ) elif token == "-": stack.append(a - b ) elif token == "*": stack.append(a * b ) else: if a * b < 0 and a % b != 0: stack.append(a // b + 1 ) else: stack.append(a // b ) else: stack.append(int(_snake_case ) ) return stack.pop() if __name__ == "__main__": import doctest doctest.testmod()
25
"""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 snake_case ( SCREAMING_SNAKE_CASE_ ): a_ : List[str] = """""" a_ : Dict = """hf-legacy""" # "hf://"" is reserved for hffs def __init__( self , __UpperCAmelCase = None , __UpperCAmelCase = None , **__UpperCAmelCase , ) ->Optional[int]: super().__init__(self , **__UpperCAmelCase) a_ = repo_info a_ = token a_ = None def UpperCAmelCase__ ( self) ->Tuple: if self.dir_cache is None: a_ = {} for hf_file in self.repo_info.siblings: # TODO(QL): add sizes a_ = { "name": hf_file.rfilename, "size": None, "type": "file", } self.dir_cache.update( { str(__UpperCAmelCase): {"name": str(__UpperCAmelCase), "size": None, "type": "directory"} for d in list(PurePosixPath(hf_file.rfilename).parents)[:-1] }) def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase = "rb" , **__UpperCAmelCase , ) ->List[Any]: if not isinstance(self.repo_info , __UpperCAmelCase): raise NotImplementedError(F'''Open is only implemented for dataset repositories, but got {self.repo_info}''') a_ = hf_hub_url(self.repo_info.id , __UpperCAmelCase , revision=self.repo_info.sha) return fsspec.open( __UpperCAmelCase , mode=__UpperCAmelCase , headers=get_authentication_headers_for_url(__UpperCAmelCase , use_auth_token=self.token) , client_kwargs={"trust_env": True} , ).open() def UpperCAmelCase__ ( self , __UpperCAmelCase , **__UpperCAmelCase) ->int: self._get_dirs() a_ = self._strip_protocol(__UpperCAmelCase) if path in self.dir_cache: return self.dir_cache[path] else: raise FileNotFoundError(__UpperCAmelCase) def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase=False , **__UpperCAmelCase) ->List[Any]: self._get_dirs() a_ = PurePosixPath(path.strip("/")) a_ = {} for p, f in self.dir_cache.items(): a_ = PurePosixPath(p.strip("/")) a_ = p.parent if root == path: a_ = f a_ = list(paths.values()) if detail: return out else: return sorted(f["name"] for f in out)
243
0
import functools import gc import inspect import torch from .imports import is_npu_available, is_xpu_available def __lowerCamelCase ( *snake_case__ ) -> List[Any]: """simple docstring""" if not isinstance(snake_case__ ,snake_case__ ): _SCREAMING_SNAKE_CASE = list(snake_case__ ) for i in range(len(snake_case__ ) ): _SCREAMING_SNAKE_CASE = None gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() return objects def __lowerCamelCase ( snake_case__ ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE = [ """CUDA out of memory.""", # CUDA OOM """cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.""", # CUDNN SNAFU """DefaultCPUAllocator: can\'t allocate memory""", # CPU OOM ] if isinstance(snake_case__ ,snake_case__ ) and len(exception.args ) == 1: return any(err in exception.args[0] for err in _statements ) return False def __lowerCamelCase ( snake_case__ = None ,snake_case__ = 1_28 ) -> Any: """simple docstring""" if function is None: return functools.partial(snake_case__ ,starting_batch_size=snake_case__ ) _SCREAMING_SNAKE_CASE = starting_batch_size def decorator(*snake_case__ ,**snake_case__ ): nonlocal batch_size gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() _SCREAMING_SNAKE_CASE = list(inspect.signature(snake_case__ ).parameters.keys() ) # Guard against user error if len(snake_case__ ) < (len(snake_case__ ) + 1): _SCREAMING_SNAKE_CASE = """, """.join([F'{arg}={value}' for arg, value in zip(params[1:] ,args[1:] )] ) raise TypeError( F'Batch size was passed into `{function.__name__}` as the first argument when called.' F'Remove this as the decorator already does so: `{function.__name__}({arg_str})`' ) while True: if batch_size == 0: raise RuntimeError("""No executable batch size found, reached zero.""" ) try: return function(snake_case__ ,*snake_case__ ,**snake_case__ ) except Exception as e: if should_reduce_batch_size(snake_case__ ): gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() batch_size //= 2 else: raise return decorator
359
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 UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { '''google/mobilenet_v1_1.0_224''': '''https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json''', '''google/mobilenet_v1_0.75_192''': '''https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json''', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 } class __UpperCAmelCase (_UpperCAmelCase ): __snake_case : Any = "mobilenet_v1" def __init__( self: List[str] , UpperCAmelCase_: List[Any]=3 , UpperCAmelCase_: List[str]=224 , UpperCAmelCase_: Optional[Any]=1.0 , UpperCAmelCase_: Dict=8 , UpperCAmelCase_: List[str]="relu6" , UpperCAmelCase_: List[Any]=True , UpperCAmelCase_: Dict=0.9_99 , UpperCAmelCase_: Union[str, Any]=0.02 , UpperCAmelCase_: Tuple=0.0_01 , **UpperCAmelCase_: List[str] , ): '''simple docstring''' super().__init__(**UpperCAmelCase_ ) if depth_multiplier <= 0: raise ValueError("""depth_multiplier must be greater than zero.""" ) _SCREAMING_SNAKE_CASE = num_channels _SCREAMING_SNAKE_CASE = image_size _SCREAMING_SNAKE_CASE = depth_multiplier _SCREAMING_SNAKE_CASE = min_depth _SCREAMING_SNAKE_CASE = hidden_act _SCREAMING_SNAKE_CASE = tf_padding _SCREAMING_SNAKE_CASE = classifier_dropout_prob _SCREAMING_SNAKE_CASE = initializer_range _SCREAMING_SNAKE_CASE = layer_norm_eps class __UpperCAmelCase (_UpperCAmelCase ): __snake_case : List[Any] = version.parse("1.11" ) @property def UpperCamelCase ( self: Tuple ): '''simple docstring''' return OrderedDict([("""pixel_values""", {0: """batch"""})] ) @property def UpperCamelCase ( self: Dict ): '''simple docstring''' if self.task == "image-classification": return OrderedDict([("""logits""", {0: """batch"""})] ) else: return OrderedDict([("""last_hidden_state""", {0: """batch"""}), ("""pooler_output""", {0: """batch"""})] ) @property def UpperCamelCase ( self: int ): '''simple docstring''' return 1E-4
125
0
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> int: if len(SCREAMING_SNAKE_CASE_ ) != len(SCREAMING_SNAKE_CASE_ ): raise ValueError('String lengths must match!' ) lowerCAmelCase__ : Tuple = 0 for chara, chara in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): if chara != chara: count += 1 return count if __name__ == "__main__": import doctest doctest.testmod()
212
import argparse import re from typing import Dict import torch from datasets import Audio, Dataset, load_dataset, load_metric from transformers import AutoFeatureExtractor, pipeline def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[str]: lowerCAmelCase__ : Optional[Any] = args.log_outputs lowerCAmelCase__ : Union[str, Any] = '_'.join(args.dataset.split('/' ) + [args.config, args.split] ) # load metric lowerCAmelCase__ : Dict = load_metric('wer' ) lowerCAmelCase__ : Tuple = load_metric('cer' ) # compute metrics lowerCAmelCase__ : Dict = wer.compute(references=result['target'] , predictions=result['prediction'] ) lowerCAmelCase__ : int = cer.compute(references=result['target'] , predictions=result['prediction'] ) # print & log results lowerCAmelCase__ : Optional[int] = F'''WER: {wer_result}\nCER: {cer_result}''' print(SCREAMING_SNAKE_CASE_ ) with open(F'''{dataset_id}_eval_results.txt''' , 'w' ) as f: f.write(SCREAMING_SNAKE_CASE_ ) # log all results in text file. Possibly interesting for analysis if log_outputs is not None: lowerCAmelCase__ : List[str] = F'''log_{dataset_id}_predictions.txt''' lowerCAmelCase__ : Union[str, Any] = F'''log_{dataset_id}_targets.txt''' with open(SCREAMING_SNAKE_CASE_ , 'w' ) as p, open(SCREAMING_SNAKE_CASE_ , 'w' ) as t: # mapping function to write output def write_to_file(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): p.write(F'''{i}''' + '\n' ) p.write(batch['prediction'] + '\n' ) t.write(F'''{i}''' + '\n' ) t.write(batch['target'] + '\n' ) result.map(SCREAMING_SNAKE_CASE_ , with_indices=SCREAMING_SNAKE_CASE_ ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> str: lowerCAmelCase__ : List[str] = '[,?.!\-\;\:"“%‘”�—’…–]' # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training lowerCAmelCase__ : Union[str, Any] = re.sub(SCREAMING_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! lowerCAmelCase__ : List[Any] = ['\n\n', '\n', ' ', ' '] for t in token_sequences_to_ignore: lowerCAmelCase__ : Optional[int] = ' '.join(text.split(SCREAMING_SNAKE_CASE_ ) ) return text def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> List[Any]: # load dataset lowerCAmelCase__ : Tuple = load_dataset(args.dataset , args.config , split=args.split , use_auth_token=SCREAMING_SNAKE_CASE_ ) # for testing: only process the first two examples as a test # dataset = dataset.select(range(10)) # load processor lowerCAmelCase__ : List[Any] = AutoFeatureExtractor.from_pretrained(args.model_id ) lowerCAmelCase__ : Union[str, Any] = feature_extractor.sampling_rate # resample audio lowerCAmelCase__ : Union[str, Any] = dataset.cast_column('audio' , Audio(sampling_rate=SCREAMING_SNAKE_CASE_ ) ) # load eval pipeline if args.device is None: lowerCAmelCase__ : List[Any] = 0 if torch.cuda.is_available() else -1 lowerCAmelCase__ : List[Any] = pipeline('automatic-speech-recognition' , model=args.model_id , device=args.device ) # map function to decode audio def map_to_pred(SCREAMING_SNAKE_CASE_ ): lowerCAmelCase__ : Optional[int] = asr( batch['audio']['array'] , chunk_length_s=args.chunk_length_s , stride_length_s=args.stride_length_s ) lowerCAmelCase__ : int = prediction['text'] lowerCAmelCase__ : Optional[int] = normalize_text(batch['sentence'] ) return batch # run inference on all examples lowerCAmelCase__ : Dict = dataset.map(SCREAMING_SNAKE_CASE_ , remove_columns=dataset.column_names ) # compute and log_results # do not change function below log_results(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": lowerCamelCase__ = 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__ = parser.parse_args() main(args)
212
1
"""simple docstring""" from __future__ import annotations from fractions import Fraction def A ( snake_case :int , snake_case :int ) -> bool: return ( num != den and num % 1_0 == den // 1_0 and (num // 1_0) / (den % 1_0) == num / den ) def A ( snake_case :int ) -> list[str]: __UpperCamelCase = [] __UpperCamelCase = 1_1 __UpperCamelCase = int('1' + '0' * digit_len ) for num in range(snake_case , snake_case ): while den <= 9_9: if (num != den) and (num % 1_0 == den // 1_0) and (den % 1_0 != 0): if is_digit_cancelling(snake_case , snake_case ): solutions.append(f'{num}/{den}' ) den += 1 num += 1 __UpperCamelCase = 1_0 return solutions def A ( snake_case :int = 2 ) -> int: __UpperCamelCase = 1.0 for fraction in fraction_list(snake_case ): __UpperCamelCase = Fraction(snake_case ) result *= frac.denominator / frac.numerator return int(snake_case ) if __name__ == "__main__": print(solution())
355
"""simple docstring""" def A ( snake_case :int ) -> int: __UpperCamelCase = abs(snake_case ) __UpperCamelCase = 0 while n > 0: res += n % 1_0 n //= 1_0 return res def A ( snake_case :int ) -> int: __UpperCamelCase = abs(snake_case ) return n if n < 1_0 else n % 1_0 + sum_of_digits(n // 1_0 ) def A ( snake_case :int ) -> int: return sum(int(snake_case ) for c in str(abs(snake_case ) ) ) def A ( ) -> None: from collections.abc import Callable from timeit import timeit def benchmark_a_function(snake_case :Callable , snake_case :int ) -> None: __UpperCamelCase = f'{func.__name__}({value})' __UpperCamelCase = timeit(f'__main__.{call}' , setup='import __main__' ) print(f'{call:56} = {func(snake_case )} -- {timing:.4f} seconds' ) for value in (2_6_2_1_4_4, 1_1_2_5_8_9_9_9_0_6_8_4_2_6_2_4, 1_2_6_7_6_5_0_6_0_0_2_2_8_2_2_9_4_0_1_4_9_6_7_0_3_2_0_5_3_7_6): for func in (sum_of_digits, sum_of_digits_recursion, sum_of_digits_compact): benchmark_a_function(snake_case , snake_case ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
263
0
'''simple docstring''' import operator as op _A : Optional[Any] ='''scaler.pt''' _A : Optional[Any] ='''pytorch_model''' _A : int ='''random_states''' _A : List[Any] ='''optimizer''' _A : Dict ='''scheduler''' _A : Dict ='''pytorch_model.bin''' _A : Optional[Any] ='''pytorch_model.bin.index.json''' _A : List[str] ='''model.safetensors''' _A : List[Any] ='''model.safetensors.index.json''' _A : str ='''1.10.2''' _A : List[Any] ='''py38''' _A : int ='''4.17.0''' _A : List[str] =['''ml.p3.16xlarge''', '''ml.p3dn.24xlarge''', '''ml.p4dn.24xlarge'''] _A : Tuple =['''FULL_SHARD''', '''SHARD_GRAD_OP''', '''NO_SHARD''', '''HYBRID_SHARD''', '''HYBRID_SHARD_ZERO2'''] _A : Tuple =['''TRANSFORMER_BASED_WRAP''', '''SIZE_BASED_WRAP''', '''NO_WRAP'''] _A : Optional[int] =['''BACKWARD_PRE''', '''BACKWARD_POST''', '''NO_PREFETCH'''] _A : Optional[int] =['''FULL_STATE_DICT''', '''LOCAL_STATE_DICT''', '''SHARDED_STATE_DICT'''] _A : List[Any] ='''2.0.1''' _A : str =['''pdsh''', '''standard''', '''openmpi''', '''mvapich'''] _A : List[str] =['''default''', '''reduce-overhead''', '''max-autotune'''] _A : str ={'''>''': op.gt, '''>=''': op.ge, '''==''': op.eq, '''!=''': op.ne, '''<=''': op.le, '''<''': op.lt} # These are the args for `torch.distributed.launch` for pytorch < 1.9 _A : Tuple =[ '''nnodes''', '''nproc_per_node''', '''rdzv_backend''', '''rdzv_endpoint''', '''rdzv_id''', '''rdzv_conf''', '''standalone''', '''max_restarts''', '''monitor_interval''', '''start_method''', '''role''', '''module''', '''m''', '''no_python''', '''run_path''', '''log_dir''', '''r''', '''redirects''', '''t''', '''tee''', '''node_rank''', '''master_addr''', '''master_port''', ] _A : Any =['''DEEPSPEED''', '''MULTI_GPU''', '''FSDP''', '''MEGATRON_LM'''] _A : Union[str, Any] =['''DEEPSPEED''', '''MULTI_XPU''', '''FSDP''']
41
'''simple docstring''' import os import unittest from transformers import LayoutLMTokenizer, LayoutLMTokenizerFast from transformers.models.layoutlm.tokenization_layoutlm import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __magic_name__ ( _UpperCamelCase , unittest.TestCase ): lowerCAmelCase : str = LayoutLMTokenizer lowerCAmelCase : Tuple = LayoutLMTokenizerFast lowerCAmelCase : List[Any] = True lowerCAmelCase : int = True def __lowercase ( self : Dict ): super().setUp() _a : int = [ '[UNK]', '[CLS]', '[SEP]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] _a : List[str] = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file ,'w' ,encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) def __lowercase ( self : Dict ,**_UpperCAmelCase : List[str] ): return LayoutLMTokenizer.from_pretrained(self.tmpdirname ,**_UpperCAmelCase ) def __lowercase ( self : Optional[Any] ,_UpperCAmelCase : Tuple ): _a : Optional[int] = 'UNwant\u00E9d,running' _a : List[Any] = 'unwanted, running' return input_text, output_text def __lowercase ( self : Optional[int] ): _a : Optional[Any] = self.tokenizer_class(self.vocab_file ) _a : Optional[Any] = tokenizer.tokenize('UNwant\u00E9d,running' ) self.assertListEqual(_UpperCAmelCase ,['un', '##want', '##ed', ',', 'runn', '##ing'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_UpperCAmelCase ) ,[7, 4, 5, 10, 8, 9] ) def __lowercase ( self : Optional[int] ): pass
89
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase : Dict = logging.get_logger(__name__) UpperCamelCase : str = { "alibaba-damo/mgp-str-base": "https://huggingface.co/alibaba-damo/mgp-str-base/resolve/main/config.json", } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): lowercase = "mgp-str" def __init__( self , __UpperCAmelCase=[32, 128] , __UpperCAmelCase=4 , __UpperCAmelCase=3 , __UpperCAmelCase=27 , __UpperCAmelCase=38 , __UpperCAmelCase=5_0257 , __UpperCAmelCase=3_0522 , __UpperCAmelCase=768 , __UpperCAmelCase=12 , __UpperCAmelCase=12 , __UpperCAmelCase=4.0 , __UpperCAmelCase=True , __UpperCAmelCase=False , __UpperCAmelCase=1E-5 , __UpperCAmelCase=0.0 , __UpperCAmelCase=0.0 , __UpperCAmelCase=0.0 , __UpperCAmelCase=False , __UpperCAmelCase=0.0_2 , **__UpperCAmelCase , ): '''simple docstring''' super().__init__(**__UpperCAmelCase ) __UpperCamelCase = image_size __UpperCamelCase = patch_size __UpperCamelCase = num_channels __UpperCamelCase = max_token_length __UpperCamelCase = num_character_labels __UpperCamelCase = num_bpe_labels __UpperCamelCase = num_wordpiece_labels __UpperCamelCase = hidden_size __UpperCamelCase = num_hidden_layers __UpperCamelCase = num_attention_heads __UpperCamelCase = mlp_ratio __UpperCamelCase = distilled __UpperCamelCase = layer_norm_eps __UpperCamelCase = drop_rate __UpperCamelCase = qkv_bias __UpperCamelCase = attn_drop_rate __UpperCamelCase = drop_path_rate __UpperCamelCase = output_aa_attentions __UpperCamelCase = initializer_range
263
"""simple docstring""" from unittest import TestCase from datasets import Dataset from minhash_deduplication import deduplicate_dataset, make_duplicate_clusters def A ( ) -> Any: __UpperCamelCase = { 'repo_name': ['test_repo1', 'test_repo2', 'test_repo3'], 'path': ['test_1.py', 'test_2.py', 'unit_test.py'], 'content': ['a ' * 2_0, 'a ' * 3_0, 'b ' * 7], } __UpperCamelCase = Dataset.from_dict(snake_case ) return dataset class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): def UpperCAmelCase ( self ): '''simple docstring''' __UpperCamelCase = get_dataset() __UpperCamelCase = make_duplicate_clusters(__UpperCAmelCase , 0.8_5 ) self.assertEqual(len(duplicate_clusters[0] ) , 2 ) def UpperCAmelCase ( self ): '''simple docstring''' __UpperCamelCase = get_dataset() __UpperCamelCase , __UpperCamelCase = deduplicate_dataset(__UpperCAmelCase ) self.assertEqual(len(__UpperCAmelCase ) , 2 ) print(__UpperCAmelCase ) self.assertEqual(duplicate_clusters[0][0]['copies'] , 2 ) self.assertEqual(duplicate_clusters[0][0]['is_extreme'] , __UpperCAmelCase )
263
1
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _A : str =logging.get_logger(__name__) _A : int ={ '''YituTech/conv-bert-base''': '''https://huggingface.co/YituTech/conv-bert-base/resolve/main/config.json''', '''YituTech/conv-bert-medium-small''': ( '''https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/config.json''' ), '''YituTech/conv-bert-small''': '''https://huggingface.co/YituTech/conv-bert-small/resolve/main/config.json''', # See all ConvBERT models at https://huggingface.co/models?filter=convbert } class _lowercase ( _lowercase ): a = """convbert""" def __init__( self: Tuple , UpperCamelCase__: List[str]=30_522 , UpperCamelCase__: List[Any]=768 , UpperCamelCase__: Union[str, Any]=12 , UpperCamelCase__: Optional[int]=12 , UpperCamelCase__: Optional[int]=3_072 , UpperCamelCase__: Tuple="gelu" , UpperCamelCase__: Any=0.1 , UpperCamelCase__: Tuple=0.1 , UpperCamelCase__: Dict=512 , UpperCamelCase__: Optional[int]=2 , UpperCamelCase__: Dict=0.02 , UpperCamelCase__: Tuple=1e-12 , UpperCamelCase__: Tuple=1 , UpperCamelCase__: Optional[Any]=0 , UpperCamelCase__: Optional[Any]=2 , UpperCamelCase__: str=768 , UpperCamelCase__: Union[str, Any]=2 , UpperCamelCase__: Any=9 , UpperCamelCase__: Optional[Any]=1 , UpperCamelCase__: Any=None , **UpperCamelCase__: List[Any] , ): super().__init__( pad_token_id=UpperCamelCase__ , bos_token_id=UpperCamelCase__ , eos_token_id=UpperCamelCase__ , **UpperCamelCase__ , ) lowerCamelCase__ : Any = vocab_size lowerCamelCase__ : List[Any] = hidden_size lowerCamelCase__ : Union[str, Any] = num_hidden_layers lowerCamelCase__ : List[Any] = num_attention_heads lowerCamelCase__ : Tuple = intermediate_size lowerCamelCase__ : Optional[Any] = hidden_act lowerCamelCase__ : Optional[int] = hidden_dropout_prob lowerCamelCase__ : str = attention_probs_dropout_prob lowerCamelCase__ : Optional[Any] = max_position_embeddings lowerCamelCase__ : Optional[Any] = type_vocab_size lowerCamelCase__ : List[Any] = initializer_range lowerCamelCase__ : List[str] = layer_norm_eps lowerCamelCase__ : List[str] = embedding_size lowerCamelCase__ : Optional[Any] = head_ratio lowerCamelCase__ : int = conv_kernel_size lowerCamelCase__ : Optional[int] = num_groups lowerCamelCase__ : Optional[int] = classifier_dropout class _lowercase ( _lowercase ): @property def lowerCamelCase_ ( self: Dict ): if self.task == "multiple-choice": lowerCamelCase__ : Tuple = {0: """batch""", 1: """choice""", 2: """sequence"""} else: lowerCamelCase__ : Any = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ("""token_type_ids""", dynamic_axis), ] )
41
'''simple docstring''' # This script creates a super tiny model that is useful inside tests, when we just want to test that # the machinery works, without needing to the check the quality of the outcomes. # # This version creates a tiny vocab first, and then a tiny model - so the outcome is truly tiny - # all files ~60KB. As compared to taking a full-size model, reducing to the minimum its layers and # emb dimensions, but keeping the full vocab + merges files, leading to ~3MB in total for all files. # The latter is done by `fsmt-make-super-tiny-model.py`. # # It will be used then as "stas/tiny-wmt19-en-ru" from pathlib import Path import json import tempfile from transformers import FSMTTokenizer, FSMTConfig, FSMTForConditionalGeneration from transformers.models.fsmt.tokenization_fsmt import VOCAB_FILES_NAMES _A : Dict ='''tiny-wmt19-en-ru''' # Build # borrowed from a test _A : List[str] =[ '''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 : str =dict(zip(vocab, range(len(vocab)))) _A : List[str] =['''l o 123''', '''lo w 1456''', '''e r</w> 1789''', ''''''] with tempfile.TemporaryDirectory() as tmpdirname: _A : Union[str, Any] =Path(tmpdirname) _A : str =build_dir / VOCAB_FILES_NAMES['''src_vocab_file'''] _A : int =build_dir / VOCAB_FILES_NAMES['''tgt_vocab_file'''] _A : List[Any] =build_dir / VOCAB_FILES_NAMES['''merges_file'''] with open(src_vocab_file, '''w''') as fp: fp.write(json.dumps(vocab_tokens)) with open(tgt_vocab_file, '''w''') as fp: fp.write(json.dumps(vocab_tokens)) with open(merges_file, '''w''') as fp: fp.write('''\n'''.join(merges)) _A : int =FSMTTokenizer( langs=['''en''', '''ru'''], src_vocab_size=len(vocab), tgt_vocab_size=len(vocab), src_vocab_file=src_vocab_file, tgt_vocab_file=tgt_vocab_file, merges_file=merges_file, ) _A : List[str] =FSMTConfig( langs=['''ru''', '''en'''], src_vocab_size=1_000, tgt_vocab_size=1_000, d_model=4, encoder_layers=1, decoder_layers=1, encoder_ffn_dim=4, decoder_ffn_dim=4, encoder_attention_heads=1, decoder_attention_heads=1, ) _A : Union[str, Any] =FSMTForConditionalGeneration(config) print(F'num of params {tiny_model.num_parameters()}') # Test _A : List[str] =tokenizer(['''Making tiny model'''], return_tensors='''pt''') _A : Tuple =tiny_model(**batch) print('''test output:''', len(outputs.logits[0])) # Save 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-ru
41
1
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 _SCREAMING_SNAKE_CASE : Optional[int] = logging.getLogger(__name__) _SCREAMING_SNAKE_CASE : str = list(MODEL_WITH_LM_HEAD_MAPPING.keys()) _SCREAMING_SNAKE_CASE : Tuple = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class UpperCAmelCase__ : """simple docstring""" a = field( default=A__ , metadata={ "help": ( "The model checkpoint for weights initialization. Leave None if you want to train a model from" " scratch." ) } , ) a = field( default=A__ , metadata={"help": "If training from scratch, pass a model type from the list: " + ", ".join(A__ )} , ) a = field( default=A__ , metadata={"help": "Pretrained config name or path if not the same as model_name"} ) a = field( default=A__ , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"} ) a = field( default=A__ , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) @dataclass class UpperCAmelCase__ : """simple docstring""" a = field( default=A__ , metadata={"help": "The input training data file (a text file)."} ) a = field( default=A__ , metadata={ "help": ( "The input training data files (multiple files in glob format). " "Very often splitting large files to smaller files can prevent tokenizer going out of memory" ) } , ) a = field( default=A__ , metadata={"help": "An optional input evaluation data file to evaluate the perplexity on (a text file)."} , ) a = field( default=A__ , metadata={"help": "An optional input train ref data file for whole word mask in Chinese."} , ) a = field( default=A__ , metadata={"help": "An optional input eval ref data file for whole word mask in Chinese."} , ) a = field( default=A__ , metadata={"help": "Whether distinct lines of text in the dataset are to be handled as distinct sequences."} , ) a = field( default=A__ , metadata={"help": "Train with masked-language modeling loss instead of language modeling."} ) a = field(default=A__ , metadata={"help": "Whether ot not to use whole word mask."} ) a = field( default=0.1_5 , 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=A__ , metadata={"help": "Overwrite the cached training and evaluation sets"} ) def UpperCAmelCase_ ( _A , _A , _A = False , _A = None , ): '''simple docstring''' def _dataset(_A , _A=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_ ( ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__ = 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: SCREAMING_SNAKE_CASE__ = AutoConfig.from_pretrained(model_args.config_name , cache_dir=model_args.cache_dir ) elif model_args.model_name_or_path: SCREAMING_SNAKE_CASE__ = AutoConfig.from_pretrained(model_args.model_name_or_path , cache_dir=model_args.cache_dir ) else: SCREAMING_SNAKE_CASE__ = CONFIG_MAPPING[model_args.model_type]() logger.warning('''You are instantiating a new config instance from scratch.''' ) if model_args.tokenizer_name: SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained(model_args.tokenizer_name , cache_dir=model_args.cache_dir ) elif model_args.model_name_or_path: SCREAMING_SNAKE_CASE__ = 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: SCREAMING_SNAKE_CASE__ = 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''' ) SCREAMING_SNAKE_CASE__ = 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: SCREAMING_SNAKE_CASE__ = tokenizer.max_len # Our input block size will be the max possible for the model else: SCREAMING_SNAKE_CASE__ = min(data_args.block_size , tokenizer.max_len ) # Get datasets SCREAMING_SNAKE_CASE__ = ( get_dataset(_A , tokenizer=_A , cache_dir=model_args.cache_dir ) if training_args.do_train else None ) SCREAMING_SNAKE_CASE__ = ( 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": SCREAMING_SNAKE_CASE__ = 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: SCREAMING_SNAKE_CASE__ = DataCollatorForWholeWordMask( tokenizer=_A , mlm_probability=data_args.mlm_probability ) else: SCREAMING_SNAKE_CASE__ = DataCollatorForLanguageModeling( tokenizer=_A , mlm=data_args.mlm , mlm_probability=data_args.mlm_probability ) # Initialize our Trainer SCREAMING_SNAKE_CASE__ = Trainer( model=_A , args=_A , data_collator=_A , train_dataset=_A , eval_dataset=_A , prediction_loss_only=_A , ) # Training if training_args.do_train: SCREAMING_SNAKE_CASE__ = ( 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 SCREAMING_SNAKE_CASE__ = {} if training_args.do_eval: logger.info('''*** Evaluate ***''' ) SCREAMING_SNAKE_CASE__ = trainer.evaluate() SCREAMING_SNAKE_CASE__ = math.exp(eval_output['''eval_loss'''] ) SCREAMING_SNAKE_CASE__ = {'''perplexity''': perplexity} SCREAMING_SNAKE_CASE__ = 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 ): '''simple docstring''' main() if __name__ == "__main__": main()
218
from argparse import ArgumentParser from .env import EnvironmentCommand def UpperCAmelCase_ ( ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = ArgumentParser('''Diffusers CLI tool''' , usage='''diffusers-cli <command> [<args>]''' ) SCREAMING_SNAKE_CASE__ = parser.add_subparsers(help='''diffusers-cli command helpers''' ) # Register commands EnvironmentCommand.register_subcommand(_A ) # Let's go SCREAMING_SNAKE_CASE__ = parser.parse_args() if not hasattr(_A , '''func''' ): parser.print_help() exit(1 ) # Run SCREAMING_SNAKE_CASE__ = args.func(_A ) service.run() if __name__ == "__main__": main()
218
1
import multiprocessing from typing import TYPE_CHECKING, Optional, Union from .. import Dataset, Features, config from ..formatting import query_table from ..packaged_modules.sql.sql import Sql from ..utils import logging from .abc import AbstractDatasetInputStream if TYPE_CHECKING: import sqlitea import sqlalchemy class lowercase__( __lowerCamelCase ): """simple docstring""" def __init__( self : List[str] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int = None , SCREAMING_SNAKE_CASE_ : Optional[Any] = None , SCREAMING_SNAKE_CASE_ : List[Any] = False , **SCREAMING_SNAKE_CASE_ : List[Any] , ) -> Tuple: super().__init__(features=SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ , keep_in_memory=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) lowercase_ = Sql( cache_dir=SCREAMING_SNAKE_CASE_ , features=SCREAMING_SNAKE_CASE_ , sql=SCREAMING_SNAKE_CASE_ , con=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) def _lowercase ( self : Union[str, Any] ) -> str: lowercase_ = None lowercase_ = None lowercase_ = None lowercase_ = 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_ , ) # Build dataset for splits lowercase_ = self.builder.as_dataset( split='''train''' , verification_mode=SCREAMING_SNAKE_CASE_ , in_memory=self.keep_in_memory ) return dataset class lowercase__: """simple docstring""" def __init__( self : List[Any] , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : Tuple = None , SCREAMING_SNAKE_CASE_ : List[Any] = None , **SCREAMING_SNAKE_CASE_ : str , ) -> Optional[int]: if num_proc is not None and num_proc <= 0: raise ValueError(f'''num_proc {num_proc} must be an integer > 0.''' ) lowercase_ = dataset lowercase_ = name lowercase_ = con lowercase_ = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE lowercase_ = num_proc lowercase_ = to_sql_kwargs def _lowercase ( self : List[str] ) -> Union[str, Any]: lowercase_ = self.to_sql_kwargs.pop('''sql''' , SCREAMING_SNAKE_CASE_ ) lowercase_ = self.to_sql_kwargs.pop('''con''' , SCREAMING_SNAKE_CASE_ ) lowercase_ = self.to_sql_kwargs.pop('''index''' , SCREAMING_SNAKE_CASE_ ) lowercase_ = self._write(index=SCREAMING_SNAKE_CASE_ , **self.to_sql_kwargs ) return written def _lowercase ( self : int , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> List[Any]: lowercase_ , lowercase_ , lowercase_ = args lowercase_ = {**to_sql_kwargs, '''if_exists''': '''append'''} if offset > 0 else to_sql_kwargs lowercase_ = query_table( table=self.dataset.data , key=slice(SCREAMING_SNAKE_CASE_ , offset + self.batch_size ) , indices=self.dataset._indices , ) lowercase_ = batch.to_pandas() lowercase_ = df.to_sql(self.name , self.con , index=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) return num_rows or len(SCREAMING_SNAKE_CASE_ ) def _lowercase ( self : Dict , SCREAMING_SNAKE_CASE_ : Optional[Any] , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> Optional[Any]: lowercase_ = 0 if self.num_proc is None or self.num_proc == 1: for offset in logging.tqdm( range(0 , len(self.dataset ) , self.batch_size ) , unit='''ba''' , disable=not logging.is_progress_bar_enabled() , desc='''Creating SQL from Arrow format''' , ): written += self._batch_sql((offset, index, to_sql_kwargs) ) else: lowercase_ , lowercase_ = len(self.dataset ), self.batch_size with multiprocessing.Pool(self.num_proc ) as pool: for num_rows in logging.tqdm( pool.imap( self._batch_sql , [(offset, index, to_sql_kwargs) for offset in range(0 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )] , ) , total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size , unit='''ba''' , disable=not logging.is_progress_bar_enabled() , desc='''Creating SQL from Arrow format''' , ): written += num_rows return written
30
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCamelCase_ = {'''configuration_vit_msn''': ['''VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ViTMSNConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase_ = [ '''VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ViTMSNModel''', '''ViTMSNForImageClassification''', '''ViTMSNPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit_msn import ( VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST, ViTMSNForImageClassification, ViTMSNModel, ViTMSNPreTrainedModel, ) else: import sys lowerCamelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
244
0
from pathlib import Path import cva import numpy as np from matplotlib import pyplot as plt def lowerCAmelCase__ ( a__ , a__ , a__ , a__ , a__ ) ->np.ndarray: '''simple docstring''' _UpperCamelCase = cva.getAffineTransform(a__ , a__ ) return cva.warpAffine(a__ , a__ , (rows, cols) ) if __name__ == "__main__": # read original image lowerCamelCase__ = cva.imread( str(Path(__file__).resolve().parent.parent / '''image_data''' / '''lena.jpg''') ) # turn image in gray scale value lowerCamelCase__ = cva.cvtColor(image, cva.COLOR_BGR2GRAY) # get image shape lowerCamelCase__,lowerCamelCase__ = gray_img.shape # set different points to rotate image lowerCamelCase__ = np.array([[50, 50], [200, 50], [50, 200]], np.floataa) lowerCamelCase__ = np.array([[10, 100], [200, 50], [100, 250]], np.floataa) lowerCamelCase__ = np.array([[50, 50], [150, 50], [120, 200]], np.floataa) lowerCamelCase__ = np.array([[10, 100], [80, 50], [180, 250]], np.floataa) # add all rotated images in a list lowerCamelCase__ = [ gray_img, get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols), get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols), get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols), ] # plot different image rotations lowerCamelCase__ = plt.figure(1) lowerCamelCase__ = ['''Original''', '''Rotation 1''', '''Rotation 2''', '''Rotation 3'''] for i, image in enumerate(images): plt.subplot(2, 2, i + 1), plt.imshow(image, '''gray''') plt.title(titles[i]) plt.axis('''off''') plt.subplots_adjust(left=0.0, bottom=0.05, right=1.0, top=0.95) plt.show()
359
# This code is adapted from OpenAI's release # https://github.com/openai/human-eval/blob/master/human_eval/execution.py import contextlib import faulthandler import io import multiprocessing import os import platform import signal import tempfile def lowerCAmelCase__ ( a__ , a__ , a__ , a__ ) ->Optional[Any]: '''simple docstring''' _UpperCamelCase = multiprocessing.Manager() _UpperCamelCase = manager.list() _UpperCamelCase = multiprocessing.Process(target=a__ , args=(check_program, result, timeout) ) p.start() p.join(timeout=timeout + 1 ) if p.is_alive(): p.kill() if not result: result.append("timed out" ) return { "task_id": task_id, "passed": result[0] == "passed", "result": result[0], "completion_id": completion_id, } def lowerCAmelCase__ ( a__ , a__ , a__ ) ->int: '''simple docstring''' with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil _UpperCamelCase = shutil.rmtree _UpperCamelCase = os.rmdir _UpperCamelCase = os.chdir # Disable functionalities that can make destructive changes to the test. reliability_guard() # Run program. try: _UpperCamelCase = {} with swallow_io(): with time_limit(a__ ): exec(a__ , a__ ) result.append("passed" ) except TimeoutException: result.append("timed out" ) except BaseException as e: result.append(f'failed: {e}' ) # Needed for cleaning up. _UpperCamelCase = rmtree _UpperCamelCase = rmdir _UpperCamelCase = chdir @contextlib.contextmanager def lowerCAmelCase__ ( a__ ) ->List[Any]: '''simple docstring''' def signal_handler(a__ , a__ ): raise TimeoutException("Timed out!" ) signal.setitimer(signal.ITIMER_REAL , a__ ) signal.signal(signal.SIGALRM , a__ ) try: yield finally: signal.setitimer(signal.ITIMER_REAL , 0 ) @contextlib.contextmanager def lowerCAmelCase__ ( ) ->Tuple: '''simple docstring''' _UpperCamelCase = WriteOnlyStringIO() with contextlib.redirect_stdout(a__ ): with contextlib.redirect_stderr(a__ ): with redirect_stdin(a__ ): yield @contextlib.contextmanager def lowerCAmelCase__ ( ) ->Optional[Any]: '''simple docstring''' with tempfile.TemporaryDirectory() as dirname: with chdir(a__ ): yield dirname class _UpperCAmelCase ( lowerCAmelCase ): '''simple docstring''' pass class _UpperCAmelCase ( io.StringIO ): '''simple docstring''' def __UpperCAmelCase ( self : Optional[int] , *lowercase_ : List[Any] , **lowercase_ : Dict) -> Optional[int]: """simple docstring""" raise OSError def __UpperCAmelCase ( self : str , *lowercase_ : Any , **lowercase_ : Optional[Any]) -> str: """simple docstring""" raise OSError def __UpperCAmelCase ( self : Union[str, Any] , *lowercase_ : Optional[Any] , **lowercase_ : Optional[Any]) -> str: """simple docstring""" raise OSError def __UpperCAmelCase ( self : Optional[Any] , *lowercase_ : str , **lowercase_ : List[Any]) -> Union[str, Any]: """simple docstring""" return False class _UpperCAmelCase ( contextlib._RedirectStream ): # type: ignore '''simple docstring''' __A = '''stdin''' @contextlib.contextmanager def lowerCAmelCase__ ( a__ ) ->Union[str, Any]: '''simple docstring''' if root == ".": yield return _UpperCamelCase = os.getcwd() os.chdir(a__ ) try: yield except BaseException as exc: raise exc finally: os.chdir(a__ ) def lowerCAmelCase__ ( a__=None ) ->Tuple: '''simple docstring''' if maximum_memory_bytes is not None: import resource resource.setrlimit(resource.RLIMIT_AS , (maximum_memory_bytes, maximum_memory_bytes) ) resource.setrlimit(resource.RLIMIT_DATA , (maximum_memory_bytes, maximum_memory_bytes) ) if not platform.uname().system == "Darwin": resource.setrlimit(resource.RLIMIT_STACK , (maximum_memory_bytes, maximum_memory_bytes) ) faulthandler.disable() import builtins _UpperCamelCase = None _UpperCamelCase = None import os _UpperCamelCase = "1" _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None import shutil _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None import subprocess _UpperCamelCase = None # type: ignore _UpperCamelCase = None import sys _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None
63
0
from collections.abc import Generator from math import sin def lowerCAmelCase_ ( __lowerCamelCase ): if len(SCREAMING_SNAKE_CASE_ ) != 3_2: raise ValueError("Input must be of length 32" ) __snake_case : Optional[int] = b"" for i in [3, 2, 1, 0]: little_endian += string_aa[8 * i : 8 * i + 8] return little_endian def lowerCAmelCase_ ( __lowerCamelCase ): if i < 0: raise ValueError("Input must be non-negative" ) __snake_case : Union[str, Any] = format(SCREAMING_SNAKE_CASE_ , "08x" )[-8:] __snake_case : Tuple = b"" for i in [3, 2, 1, 0]: little_endian_hex += hex_rep[2 * i : 2 * i + 2].encode("utf-8" ) return little_endian_hex def lowerCAmelCase_ ( __lowerCamelCase ): __snake_case : Union[str, Any] = b"" for char in message: bit_string += format(SCREAMING_SNAKE_CASE_ , "08b" ).encode("utf-8" ) __snake_case : Tuple = format(len(SCREAMING_SNAKE_CASE_ ) , "064b" ).encode("utf-8" ) # Pad bit_string to a multiple of 512 chars bit_string += b"1" while len(SCREAMING_SNAKE_CASE_ ) % 5_1_2 != 4_4_8: bit_string += b"0" bit_string += to_little_endian(start_len[3_2:] ) + to_little_endian(start_len[:3_2] ) return bit_string def lowerCAmelCase_ ( __lowerCamelCase ): if len(SCREAMING_SNAKE_CASE_ ) % 5_1_2 != 0: raise ValueError("Input must have length that's a multiple of 512" ) for pos in range(0 , len(SCREAMING_SNAKE_CASE_ ) , 5_1_2 ): __snake_case : Union[str, Any] = bit_string[pos : pos + 5_1_2] __snake_case : Union[str, Any] = [] for i in range(0 , 5_1_2 , 3_2 ): block_words.append(int(to_little_endian(block[i : i + 3_2] ) , 2 ) ) yield block_words def lowerCAmelCase_ ( __lowerCamelCase ): if i < 0: raise ValueError("Input must be non-negative" ) __snake_case : Optional[int] = format(SCREAMING_SNAKE_CASE_ , "032b" ) __snake_case : List[str] = "" for c in i_str: new_str += "1" if c == "0" else "0" return int(SCREAMING_SNAKE_CASE_ , 2 ) def lowerCAmelCase_ ( __lowerCamelCase , __lowerCamelCase ): return (a + b) % 2**3_2 def lowerCAmelCase_ ( __lowerCamelCase , __lowerCamelCase ): if i < 0: raise ValueError("Input must be non-negative" ) if shift < 0: raise ValueError("Shift must be non-negative" ) return ((i << shift) ^ (i >> (3_2 - shift))) % 2**3_2 def lowerCAmelCase_ ( __lowerCamelCase ): __snake_case : Dict = preprocess(SCREAMING_SNAKE_CASE_ ) __snake_case : Optional[int] = [int(2**3_2 * abs(sin(i + 1 ) ) ) for i in range(6_4 )] # Starting states __snake_case : Union[str, Any] = 0X67_45_23_01 __snake_case : Optional[Any] = 0Xef_cd_ab_89 __snake_case : Union[str, Any] = 0X98_ba_dc_fe __snake_case : Optional[Any] = 0X10_32_54_76 __snake_case : List[str] = [ 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, ] # Process bit string in chunks, each with 16 32-char words for block_words in get_block_words(SCREAMING_SNAKE_CASE_ ): __snake_case : int = aa __snake_case : Dict = ba __snake_case : Any = ca __snake_case : str = da # Hash current chunk for i in range(6_4 ): if i <= 1_5: # f = (b & c) | (not_32(b) & d) # Alternate definition for f __snake_case : List[str] = d ^ (b & (c ^ d)) __snake_case : Optional[Any] = i elif i <= 3_1: # f = (d & b) | (not_32(d) & c) # Alternate definition for f __snake_case : List[Any] = c ^ (d & (b ^ c)) __snake_case : Tuple = (5 * i + 1) % 1_6 elif i <= 4_7: __snake_case : List[str] = b ^ c ^ d __snake_case : List[str] = (3 * i + 5) % 1_6 else: __snake_case : int = c ^ (b | not_aa(SCREAMING_SNAKE_CASE_ )) __snake_case : List[str] = (7 * i) % 1_6 __snake_case : Tuple = (f + a + added_consts[i] + block_words[g]) % 2**3_2 __snake_case : int = d __snake_case : int = c __snake_case : int = b __snake_case : Optional[Any] = sum_aa(SCREAMING_SNAKE_CASE_ , left_rotate_aa(SCREAMING_SNAKE_CASE_ , shift_amounts[i] ) ) # Add hashed chunk to running total __snake_case : Optional[int] = sum_aa(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) __snake_case : Optional[Any] = sum_aa(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) __snake_case : Dict = sum_aa(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) __snake_case : Optional[Any] = sum_aa(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) __snake_case : List[Any] = reformat_hex(SCREAMING_SNAKE_CASE_ ) + reformat_hex(SCREAMING_SNAKE_CASE_ ) + reformat_hex(SCREAMING_SNAKE_CASE_ ) + reformat_hex(SCREAMING_SNAKE_CASE_ ) return digest if __name__ == "__main__": import doctest doctest.testmod()
123
'''simple docstring''' from __future__ import annotations from collections import deque class lowerCAmelCase_ : def __init__( self , _lowerCAmelCase ) -> Optional[int]: _lowerCAmelCase = [] self.adlist.append( {"value": "", "next_states": [], "fail_state": 0, "output": []} ) for keyword in keywords: self.add_keyword(_lowerCAmelCase ) self.set_fail_transitions() def _snake_case ( self , _lowerCAmelCase , _lowerCAmelCase ) -> int | None: for state in self.adlist[current_state]["next_states"]: if char == self.adlist[state]["value"]: return state return None def _snake_case ( self , _lowerCAmelCase ) -> None: _lowerCAmelCase = 0 for character in keyword: _lowerCAmelCase = self.find_next_state(_lowerCAmelCase , _lowerCAmelCase ) if next_state is None: self.adlist.append( { "value": character, "next_states": [], "fail_state": 0, "output": [], } ) self.adlist[current_state]["next_states"].append(len(self.adlist ) - 1 ) _lowerCAmelCase = len(self.adlist ) - 1 else: _lowerCAmelCase = next_state self.adlist[current_state]["output"].append(_lowerCAmelCase ) def _snake_case ( self ) -> None: _lowerCAmelCase = deque() for node in self.adlist[0]["next_states"]: q.append(_lowerCAmelCase ) _lowerCAmelCase = 0 while q: _lowerCAmelCase = q.popleft() for child in self.adlist[r]["next_states"]: q.append(_lowerCAmelCase ) _lowerCAmelCase = self.adlist[r]["fail_state"] while ( self.find_next_state(_lowerCAmelCase , self.adlist[child]["value"] ) is None and state != 0 ): _lowerCAmelCase = self.adlist[state]["fail_state"] _lowerCAmelCase = self.find_next_state( _lowerCAmelCase , self.adlist[child]["value"] ) if self.adlist[child]["fail_state"] is None: _lowerCAmelCase = 0 _lowerCAmelCase = ( self.adlist[child]["output"] + self.adlist[self.adlist[child]["fail_state"]]["output"] ) def _snake_case ( self , _lowerCAmelCase ) -> dict[str, list[int]]: _lowerCAmelCase = {} # returns a dict with keywords and list of its occurrences _lowerCAmelCase = 0 for i in range(len(_lowerCAmelCase ) ): while ( self.find_next_state(_lowerCAmelCase , string[i] ) is None and current_state != 0 ): _lowerCAmelCase = self.adlist[current_state]["fail_state"] _lowerCAmelCase = self.find_next_state(_lowerCAmelCase , string[i] ) if next_state is None: _lowerCAmelCase = 0 else: _lowerCAmelCase = next_state for key in self.adlist[current_state]["output"]: if key not in result: _lowerCAmelCase = [] result[key].append(i - len(_lowerCAmelCase ) + 1 ) return result if __name__ == "__main__": import doctest doctest.testmod()
158
0
"""simple docstring""" from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available() and is_transformers_version('>=', '4.25.0')): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ( VersatileDiffusionDualGuidedPipeline, VersatileDiffusionImageVariationPipeline, VersatileDiffusionPipeline, VersatileDiffusionTextToImagePipeline, ) else: from .modeling_text_unet import UNetFlatConditionModel from .pipeline_versatile_diffusion import VersatileDiffusionPipeline from .pipeline_versatile_diffusion_dual_guided import VersatileDiffusionDualGuidedPipeline from .pipeline_versatile_diffusion_image_variation import VersatileDiffusionImageVariationPipeline from .pipeline_versatile_diffusion_text_to_image import VersatileDiffusionTextToImagePipeline
363
"""simple docstring""" from __future__ import annotations def lowercase__ ( snake_case_ :float , snake_case_ :float , snake_case_ :float ): if days_between_payments <= 0: raise ValueError('''days_between_payments must be > 0''' ) if daily_interest_rate < 0: raise ValueError('''daily_interest_rate must be >= 0''' ) if principal <= 0: raise ValueError('''principal must be > 0''' ) return principal * daily_interest_rate * days_between_payments def lowercase__ ( snake_case_ :float , snake_case_ :float , snake_case_ :float , ): if number_of_compounding_periods <= 0: raise ValueError('''number_of_compounding_periods must be > 0''' ) if nominal_annual_interest_rate_percentage < 0: raise ValueError('''nominal_annual_interest_rate_percentage must be >= 0''' ) if principal <= 0: raise ValueError('''principal must be > 0''' ) return principal * ( (1 + nominal_annual_interest_rate_percentage) ** number_of_compounding_periods - 1 ) def lowercase__ ( snake_case_ :float , snake_case_ :float , snake_case_ :float , ): if number_of_years <= 0: raise ValueError('''number_of_years must be > 0''' ) if nominal_annual_percentage_rate < 0: raise ValueError('''nominal_annual_percentage_rate must be >= 0''' ) if principal <= 0: raise ValueError('''principal must be > 0''' ) return compound_interest( snake_case_ , nominal_annual_percentage_rate / 365 , number_of_years * 365 ) if __name__ == "__main__": import doctest doctest.testmod()
86
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE__ = { 'configuration_xlm_roberta_xl': [ 'XLM_ROBERTA_XL_PRETRAINED_CONFIG_ARCHIVE_MAP', 'XLMRobertaXLConfig', 'XLMRobertaXLOnnxConfig', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ 'XLM_ROBERTA_XL_PRETRAINED_MODEL_ARCHIVE_LIST', 'XLMRobertaXLForCausalLM', 'XLMRobertaXLForMaskedLM', 'XLMRobertaXLForMultipleChoice', 'XLMRobertaXLForQuestionAnswering', 'XLMRobertaXLForSequenceClassification', 'XLMRobertaXLForTokenClassification', 'XLMRobertaXLModel', 'XLMRobertaXLPreTrainedModel', ] if TYPE_CHECKING: from .configuration_xlm_roberta_xl import ( XLM_ROBERTA_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMRobertaXLConfig, XLMRobertaXLOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm_roberta_xl import ( XLM_ROBERTA_XL_PRETRAINED_MODEL_ARCHIVE_LIST, XLMRobertaXLForCausalLM, XLMRobertaXLForMaskedLM, XLMRobertaXLForMultipleChoice, XLMRobertaXLForQuestionAnswering, XLMRobertaXLForSequenceClassification, XLMRobertaXLForTokenClassification, XLMRobertaXLModel, XLMRobertaXLPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure)
325
"""simple docstring""" import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class A__ ( unittest.TestCase ): '''simple docstring''' @slow def _SCREAMING_SNAKE_CASE ( self: Union[str, Any]) -> str: """simple docstring""" __lowerCAmelCase : Optional[Any] = FlaxMTaForConditionalGeneration.from_pretrained("google/mt5-small") __lowerCAmelCase : Optional[Any] = AutoTokenizer.from_pretrained("google/mt5-small") __lowerCAmelCase : Tuple = tokenizer("Hello there" , return_tensors="np").input_ids __lowerCAmelCase : Dict = tokenizer("Hi I am" , return_tensors="np").input_ids __lowerCAmelCase : str = shift_tokens_right(_SCREAMING_SNAKE_CASE , model.config.pad_token_id , model.config.decoder_start_token_id) __lowerCAmelCase : Optional[int] = model(_SCREAMING_SNAKE_CASE , decoder_input_ids=_SCREAMING_SNAKE_CASE).logits __lowerCAmelCase : int = optax.softmax_cross_entropy(_SCREAMING_SNAKE_CASE , onehot(_SCREAMING_SNAKE_CASE , logits.shape[-1])).mean() __lowerCAmelCase : List[str] = -(labels.shape[-1] * loss.item()) __lowerCAmelCase : str = -84.9127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE) < 1e-4)
269
0
import json import os from typing import Optional, Tuple import regex as re from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _lowerCamelCase : Optional[int] = logging.get_logger(__name__) _lowerCamelCase : List[Any] = { '''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', } _lowerCamelCase : Optional[Any] = { '''vocab_file''': {'''ctrl''': '''https://raw.githubusercontent.com/salesforce/ctrl/master/ctrl-vocab.json'''}, '''merges_file''': {'''ctrl''': '''https://raw.githubusercontent.com/salesforce/ctrl/master/ctrl-merges.txt'''}, } _lowerCamelCase : Optional[Any] = { '''ctrl''': 2_56, } _lowerCamelCase : List[str] = { '''Pregnancy''': 16_86_29, '''Christianity''': 76_75, '''Explain''': 10_64_23, '''Fitness''': 6_34_40, '''Saving''': 6_31_63, '''Ask''': 2_71_71, '''Ass''': 9_59_85, '''Joke''': 16_35_09, '''Questions''': 4_56_22, '''Thoughts''': 4_96_05, '''Retail''': 5_23_42, '''Feminism''': 16_43_38, '''Writing''': 1_19_92, '''Atheism''': 19_22_63, '''Netflix''': 4_86_16, '''Computing''': 3_96_39, '''Opinion''': 4_32_13, '''Alone''': 4_49_67, '''Funny''': 5_89_17, '''Gaming''': 4_03_58, '''Human''': 40_88, '''India''': 13_31, '''Joker''': 7_71_38, '''Diet''': 3_62_06, '''Legal''': 1_18_59, '''Norman''': 49_39, '''Tip''': 7_26_89, '''Weight''': 5_23_43, '''Movies''': 4_62_73, '''Running''': 2_34_25, '''Science''': 20_90, '''Horror''': 3_77_93, '''Confession''': 6_05_72, '''Finance''': 1_22_50, '''Politics''': 1_63_60, '''Scary''': 19_19_85, '''Support''': 1_26_54, '''Technologies''': 3_25_16, '''Teenage''': 6_61_60, '''Event''': 3_27_69, '''Learned''': 6_74_60, '''Notion''': 18_27_70, '''Wikipedia''': 3_75_83, '''Books''': 66_65, '''Extract''': 7_60_50, '''Confessions''': 10_27_01, '''Conspiracy''': 7_59_32, '''Links''': 6_36_74, '''Narcissus''': 15_04_25, '''Relationship''': 5_47_66, '''Relationships''': 13_47_96, '''Reviews''': 4_16_71, '''News''': 42_56, '''Translation''': 2_68_20, '''multilingual''': 12_84_06, } def __lowerCamelCase (UpperCAmelCase__ : int ): SCREAMING_SNAKE_CASE = set() SCREAMING_SNAKE_CASE = word[0] for char in word[1:]: pairs.add((prev_char, char) ) SCREAMING_SNAKE_CASE = char SCREAMING_SNAKE_CASE = set(UpperCAmelCase__ ) return pairs class lowercase ( a ): lowercase__ : int = VOCAB_FILES_NAMES lowercase__ : List[Any] = PRETRAINED_VOCAB_FILES_MAP lowercase__ : Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase__ : List[str] = CONTROL_CODES def __init__( self : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Optional[int]="<unk>" , **_UpperCamelCase : str ) -> List[str]: '''simple docstring''' super().__init__(unk_token=_UpperCamelCase , **_UpperCamelCase ) with open(_UpperCamelCase , encoding="utf-8" ) as vocab_handle: SCREAMING_SNAKE_CASE = json.load(_UpperCamelCase ) SCREAMING_SNAKE_CASE = {v: k for k, v in self.encoder.items()} with open(_UpperCamelCase , encoding="utf-8" ) as merges_handle: SCREAMING_SNAKE_CASE = merges_handle.read().split("\n" )[1:-1] SCREAMING_SNAKE_CASE = [tuple(merge.split() ) for merge in merges] SCREAMING_SNAKE_CASE = dict(zip(_UpperCamelCase , range(len(_UpperCamelCase ) ) ) ) SCREAMING_SNAKE_CASE = {} @property def __snake_case( self : List[str] ) -> List[Any]: '''simple docstring''' return len(self.encoder ) def __snake_case( self : int ) -> int: '''simple docstring''' return dict(self.encoder , **self.added_tokens_encoder ) def __snake_case( self : List[str] , _UpperCamelCase : Any ) -> Optional[int]: '''simple docstring''' if token in self.cache: return self.cache[token] SCREAMING_SNAKE_CASE = tuple(_UpperCamelCase ) SCREAMING_SNAKE_CASE = tuple(list(word[:-1] ) + [word[-1] + "</w>"] ) SCREAMING_SNAKE_CASE = get_pairs(_UpperCamelCase ) if not pairs: return token while True: SCREAMING_SNAKE_CASE = min(_UpperCamelCase , key=lambda _UpperCamelCase : self.bpe_ranks.get(_UpperCamelCase , float("inf" ) ) ) if bigram not in self.bpe_ranks: break SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = bigram SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = 0 while i < len(_UpperCamelCase ): try: SCREAMING_SNAKE_CASE = word.index(_UpperCamelCase , _UpperCamelCase ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) SCREAMING_SNAKE_CASE = j if word[i] == first and i < len(_UpperCamelCase ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 SCREAMING_SNAKE_CASE = tuple(_UpperCamelCase ) SCREAMING_SNAKE_CASE = new_word if len(_UpperCamelCase ) == 1: break else: SCREAMING_SNAKE_CASE = get_pairs(_UpperCamelCase ) SCREAMING_SNAKE_CASE = "@@ ".join(_UpperCamelCase ) SCREAMING_SNAKE_CASE = word[:-4] SCREAMING_SNAKE_CASE = word return word def __snake_case( self : str , _UpperCamelCase : int ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = re.findall(R"\S+\n?" , _UpperCamelCase ) for token in words: split_tokens.extend(list(self.bpe(_UpperCamelCase ).split(" " ) ) ) return split_tokens def __snake_case( self : Optional[int] , _UpperCamelCase : Optional[Any] ) -> Dict: '''simple docstring''' return self.encoder.get(_UpperCamelCase , self.encoder.get(self.unk_token ) ) def __snake_case( self : List[Any] , _UpperCamelCase : List[Any] ) -> Optional[Any]: '''simple docstring''' return self.decoder.get(_UpperCamelCase , self.unk_token ) def __snake_case( self : Any , _UpperCamelCase : Optional[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = " ".join(_UpperCamelCase ).replace("@@ " , "" ).strip() return out_string def __snake_case( self : int , _UpperCamelCase : str , _UpperCamelCase : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(_UpperCamelCase ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE = os.path.join( _UpperCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) SCREAMING_SNAKE_CASE = os.path.join( _UpperCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"] ) with open(_UpperCamelCase , "w" , encoding="utf-8" ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=_UpperCamelCase , ensure_ascii=_UpperCamelCase ) + "\n" ) SCREAMING_SNAKE_CASE = 0 with open(_UpperCamelCase , "w" , encoding="utf-8" ) as writer: writer.write("#version: 0.2\n" ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda _UpperCamelCase : kv[1] ): if index != token_index: logger.warning( F"Saving vocabulary to {merge_file}: BPE merge indices are not consecutive." " Please check that the tokenizer is not corrupted!" ) SCREAMING_SNAKE_CASE = token_index writer.write(" ".join(_UpperCamelCase ) + "\n" ) index += 1 return vocab_file, merge_file # def decode(self, token_ids, skip_special_tokens=False, clean_up_tokenization_spaces=True): # filtered_tokens = ' '.join(self.convert_ids_to_tokens(token_ids, skip_special_tokens=skip_special_tokens)) # tokens_generated_so_far = re.sub('(@@ )', '', string=filtered_tokens) # tokens_generated_so_far = re.sub('(@@ ?$)', '', string=tokens_generated_so_far) # return ''.join(tokens_generated_so_far)
206
import unittest from transformers import AutoTokenizer, is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, slow if is_flax_available(): import jax.numpy as jnp from transformers import FlaxXLMRobertaModel @require_sentencepiece @require_tokenizers @require_flax class lowercase ( unittest.TestCase ): @slow def __snake_case( self : Optional[int] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = FlaxXLMRobertaModel.from_pretrained("xlm-roberta-base" ) SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("xlm-roberta-base" ) SCREAMING_SNAKE_CASE = "The dog is cute and lives in the garden house" SCREAMING_SNAKE_CASE = jnp.array([tokenizer.encode(_UpperCamelCase )] ) SCREAMING_SNAKE_CASE = (1, 12, 768) # batch_size, sequence_length, embedding_vector_dim SCREAMING_SNAKE_CASE = jnp.array( [[-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]] ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase )["last_hidden_state"] self.assertEqual(output.shape , _UpperCamelCase ) # compare the actual values for a slice of last dim self.assertTrue(jnp.allclose(output[:, :, -1] , _UpperCamelCase , atol=1e-3 ) )
206
1
def __lowercase ( __lowerCAmelCase : list , __lowerCAmelCase : list ): _validate_point(_UpperCAmelCase ) _validate_point(_UpperCAmelCase ) if len(_UpperCAmelCase ) != len(_UpperCAmelCase ): raise ValueError('Both points must be in the same n-dimensional space' ) return float(sum(abs(a - b ) for a, b in zip(_UpperCAmelCase , _UpperCAmelCase ) ) ) def __lowercase ( __lowerCAmelCase : list[float] ): if point: if isinstance(_UpperCAmelCase , _UpperCAmelCase ): for item in point: if not isinstance(_UpperCAmelCase , (int, float) ): a__ = ( "Expected a list of numbers as input, found " F'{type(_UpperCAmelCase ).__name__}' ) raise TypeError(_UpperCAmelCase ) else: a__ = F'Expected a list of numbers as input, found {type(_UpperCAmelCase ).__name__}' raise TypeError(_UpperCAmelCase ) else: raise ValueError('Missing an input' ) def __lowercase ( __lowerCAmelCase : list , __lowerCAmelCase : list ): _validate_point(_UpperCAmelCase ) _validate_point(_UpperCAmelCase ) if len(_UpperCAmelCase ) != len(_UpperCAmelCase ): raise ValueError('Both points must be in the same n-dimensional space' ) return float(sum(abs(x - y ) for x, y in zip(_UpperCAmelCase , _UpperCAmelCase ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
240
'''simple docstring''' def UpperCamelCase_ ( _UpperCAmelCase : int , _UpperCAmelCase : int ) -> int: """simple docstring""" _UpperCAmelCase : List[str] = 1 # To kept the Calculated Value # Since C(n, k) = C(n, n-k) if k > (n - k): _UpperCAmelCase : Any = n - k # Calculate C(n,k) for i in range(_UpperCAmelCase ): result *= n - i result //= i + 1 return result def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" return binomial_coefficient(2 * node_count , _UpperCAmelCase ) // (node_count + 1) def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" if n < 0: raise ValueError("factorial() not defined for negative values" ) _UpperCAmelCase : List[str] = 1 for i in range(1 , n + 1 ): result *= i return result def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" return catalan_number(_UpperCAmelCase ) * factorial(_UpperCAmelCase ) if __name__ == "__main__": __SCREAMING_SNAKE_CASE : Dict = int(input("""Enter the number of nodes: """).strip() or 0) if node_count <= 0: raise ValueError("""We need some nodes to work with.""") print( F'Given {node_count} nodes, there are {binary_tree_count(node_count)} ' F'binary trees and {catalan_number(node_count)} binary search trees.' )
31
0
"""simple docstring""" import argparse import json import os import torch from torch import nn from transformers import NllbMoeConfig, NllbMoeModel from transformers.modeling_utils import dtype_byte_size from transformers.utils import WEIGHTS_INDEX_NAME, WEIGHTS_NAME def __snake_case ( SCREAMING_SNAKE_CASE__ : Union[str, Any] ) -> List[str]: '''simple docstring''' _UpperCAmelCase : str = [ "encoder.version", "decoder.version", "model.encoder.version", "model.decoder.version", "decoder.output_projection.weight", "_float_tensor", "encoder.embed_positions._float_tensor", "decoder.embed_positions._float_tensor", ] for k in ignore_keys: state_dict.pop(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def __snake_case ( SCREAMING_SNAKE_CASE__ : List[str] ) -> str: '''simple docstring''' _UpperCAmelCase , _UpperCAmelCase : Optional[int] = emb.weight.shape _UpperCAmelCase : List[Any] = nn.Linear(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , bias=SCREAMING_SNAKE_CASE__ ) _UpperCAmelCase : Optional[int] = emb.weight.data return lin_layer def __snake_case ( SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : Any=None ) -> str: '''simple docstring''' _UpperCAmelCase : Optional[int] = {} for old_key in state_dict.keys(): _UpperCAmelCase : Tuple = old_key if "moe_layer.experts." in key: if expert_idx is not None: _UpperCAmelCase : Any = key.replace("moe_layer.experts.0" , f'ffn.experts.expert_{expert_idx}' ) else: _UpperCAmelCase : Dict = key.replace("moe_layer.experts." , "ffn.experts.expert_" ) if "gate" in key: _UpperCAmelCase : Union[str, Any] = key.replace(".moe_layer.gate.wg" , ".ffn.router.classifier" ) if "fc2" and "experts" not in key: _UpperCAmelCase : List[str] = key.replace(".fc2." , ".ffn.fc2." ) if "fc1" and "experts" not in key: _UpperCAmelCase : Optional[int] = key.replace(".fc1." , ".ffn.fc1." ) if ".encoder_attn." in key: _UpperCAmelCase : Optional[int] = key.replace(".encoder_attn." , ".cross_attention." ) if "encoder_attn_layer_norm" in key: _UpperCAmelCase : Dict = key.replace("encoder_attn_layer_norm" , "cross_attention_layer_norm" ) if "final_layer_norm" in key: _UpperCAmelCase : Optional[int] = key.replace("final_layer_norm" , "ff_layer_norm" ) _UpperCAmelCase : Union[str, Any] = state_dict[old_key] return new_dict def __snake_case ( SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : str = WEIGHTS_NAME ) -> Dict: '''simple docstring''' _UpperCAmelCase : List[Any] = [] _UpperCAmelCase : Union[str, Any] = 0 os.makedirs(SCREAMING_SNAKE_CASE__ , exist_ok=SCREAMING_SNAKE_CASE__ ) for expert in range(SCREAMING_SNAKE_CASE__ ): _UpperCAmelCase : str = switch_checkpoint_path + f'-rank-{expert}.pt' if os.path.isfile(SCREAMING_SNAKE_CASE__ ): _UpperCAmelCase : List[str] = torch.load(SCREAMING_SNAKE_CASE__ )["model"] remove_ignore_keys_(SCREAMING_SNAKE_CASE__ ) _UpperCAmelCase : List[str] = rename_fairseq_keys(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) _UpperCAmelCase : List[str] = os.path.join( SCREAMING_SNAKE_CASE__ , weights_name.replace(".bin" , f'-{len(SCREAMING_SNAKE_CASE__ )+1:05d}-of-???.bin' ) ) torch.save(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) sharded_state_dicts.append(expert_state.keys() ) total_size += sum([value.numel() for key, value in expert_state.items()] ) * dtype_byte_size( expert_state[list(SCREAMING_SNAKE_CASE__ )[0]].dtype ) # Add the last block _UpperCAmelCase : Dict = os.path.join(SCREAMING_SNAKE_CASE__ , weights_name.replace(".bin" , f'-{len(SCREAMING_SNAKE_CASE__ )+1:05d}-of-???.bin' ) ) _UpperCAmelCase : str = torch.load(switch_checkpoint_path + "-shared.pt" )["model"] remove_ignore_keys_(SCREAMING_SNAKE_CASE__ ) _UpperCAmelCase : Union[str, Any] = rename_fairseq_keys(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) _UpperCAmelCase : Dict = shared_weights["decoder.embed_tokens.weight"] sharded_state_dicts.append(shared_weights.keys() ) # If we only have the shared weights (dummy model/experts saved on the same file) if len(SCREAMING_SNAKE_CASE__ ) == 1: _UpperCAmelCase : Any = os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) torch.save(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return {weights_name: sharded_state_dicts[0]}, None else: torch.save(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # Otherwise, let's build the index _UpperCAmelCase : List[Any] = {} for idx, shard in enumerate(SCREAMING_SNAKE_CASE__ ): _UpperCAmelCase : Optional[int] = weights_name.replace(".bin" , f'-{idx+1:05d}-of-{len(SCREAMING_SNAKE_CASE__ ):05d}.bin' ) _UpperCAmelCase : int = os.path.join(SCREAMING_SNAKE_CASE__ , weights_name.replace(".bin" , f'-{idx+1:05d}-of-???.bin' ) ) os.rename(SCREAMING_SNAKE_CASE__ , os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) for key in shard: _UpperCAmelCase : Tuple = shard_file # Add the metadata _UpperCAmelCase : Optional[Any] = {"total_size": total_size} _UpperCAmelCase : str = {"metadata": metadata, "weight_map": weight_map} with open(os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) , "w" , encoding="utf-8" ) as f: _UpperCAmelCase : str = json.dumps(SCREAMING_SNAKE_CASE__ , indent=2 , sort_keys=SCREAMING_SNAKE_CASE__ ) + "\n" f.write(SCREAMING_SNAKE_CASE__ ) return metadata, index if __name__ == "__main__": _lowerCAmelCase : List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--nllb_moe_checkpoint_path", default="/home/arthur_huggingface_co/fairseq/weights/checkpoints/model_moe_54b/checkpoint_2_300000", type=str, required=False, help="Path to a directory containing a folder per layer. Follows the original Google format.", ) parser.add_argument("--dtype", default="float32", type=str, required=False, help="dtype of the saved model") parser.add_argument( "--pytorch_dump_folder_path", default="/home/arthur_huggingface_co/fairseq/weights/checkpoints/hf-converted-moe-54b", type=str, required=False, help="Path to the output pytorch model.", ) _lowerCAmelCase : Tuple = parser.parse_args() _lowerCAmelCase, _lowerCAmelCase : Any = shard_on_the_fly( args.nllb_moe_checkpoint_path, args.pytorch_dump_folder_path, 1_28, args.dtype, ) _lowerCAmelCase : str = NllbMoeConfig.from_pretrained( "facebook/nllb-200-3.3B", encoder_sparse_step=4, decoder_sparse_step=4, num_experts=1_28 ) config.save_pretrained(args.pytorch_dump_folder_path) _lowerCAmelCase : Dict = NllbMoeModel.from_pretrained(args.pytorch_dump_folder_path) print("Done") model.save_pretrained(args.pytorch_dump_folder_path)
202
"""simple docstring""" import json import os from functools import lru_cache from typing import List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging _lowerCAmelCase : Optional[int] = logging.get_logger(__name__) _lowerCAmelCase : Dict = {"vocab_file": "vocab.json", "merges_file": "merges.txt"} _lowerCAmelCase : Tuple = { "vocab_file": { "allenai/longformer-base-4096": "https://huggingface.co/allenai/longformer-base-4096/resolve/main/vocab.json", "allenai/longformer-large-4096": ( "https://huggingface.co/allenai/longformer-large-4096/resolve/main/vocab.json" ), "allenai/longformer-large-4096-finetuned-triviaqa": ( "https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/vocab.json" ), "allenai/longformer-base-4096-extra.pos.embd.only": ( "https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/vocab.json" ), "allenai/longformer-large-4096-extra.pos.embd.only": ( "https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/vocab.json" ), }, "merges_file": { "allenai/longformer-base-4096": "https://huggingface.co/allenai/longformer-base-4096/resolve/main/merges.txt", "allenai/longformer-large-4096": ( "https://huggingface.co/allenai/longformer-large-4096/resolve/main/merges.txt" ), "allenai/longformer-large-4096-finetuned-triviaqa": ( "https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/merges.txt" ), "allenai/longformer-base-4096-extra.pos.embd.only": ( "https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/merges.txt" ), "allenai/longformer-large-4096-extra.pos.embd.only": ( "https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/merges.txt" ), }, } _lowerCAmelCase : str = { "allenai/longformer-base-4096": 40_96, "allenai/longformer-large-4096": 40_96, "allenai/longformer-large-4096-finetuned-triviaqa": 40_96, "allenai/longformer-base-4096-extra.pos.embd.only": 40_96, "allenai/longformer-large-4096-extra.pos.embd.only": 40_96, } @lru_cache() # Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode def __snake_case ( ) -> List[Any]: '''simple docstring''' _UpperCAmelCase : str = ( list(range(ord("!" ) , ord("~" ) + 1 ) ) + list(range(ord("¡" ) , ord("¬" ) + 1 ) ) + list(range(ord("®" ) , ord("ÿ" ) + 1 ) ) ) _UpperCAmelCase : Any = bs[:] _UpperCAmelCase : Tuple = 0 for b in range(2**8 ): if b not in bs: bs.append(SCREAMING_SNAKE_CASE__ ) cs.append(2**8 + n ) n += 1 _UpperCAmelCase : Union[str, Any] = [chr(SCREAMING_SNAKE_CASE__ ) for n in cs] return dict(zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) def __snake_case ( SCREAMING_SNAKE_CASE__ : Optional[Any] ) -> int: '''simple docstring''' _UpperCAmelCase : int = set() _UpperCAmelCase : int = word[0] for char in word[1:]: pairs.add((prev_char, char) ) _UpperCAmelCase : Optional[int] = char return pairs class UpperCAmelCase_ ( _UpperCamelCase ): __SCREAMING_SNAKE_CASE : List[str] = VOCAB_FILES_NAMES __SCREAMING_SNAKE_CASE : List[str] = PRETRAINED_VOCAB_FILES_MAP __SCREAMING_SNAKE_CASE : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __SCREAMING_SNAKE_CASE : Union[str, Any] = ['input_ids', 'attention_mask'] def __init__( self : Optional[Any] , A : int , A : Any , A : List[str]="replace" , A : List[Any]="<s>" , A : int="</s>" , A : Union[str, Any]="</s>" , A : Tuple="<s>" , A : str="<unk>" , A : Dict="<pad>" , A : Optional[Any]="<mask>" , A : Tuple=False , **A : Dict , ): _UpperCAmelCase : Tuple = AddedToken(A , lstrip=A , rstrip=A ) if isinstance(A , A ) else bos_token _UpperCAmelCase : int = AddedToken(A , lstrip=A , rstrip=A ) if isinstance(A , A ) else eos_token _UpperCAmelCase : int = AddedToken(A , lstrip=A , rstrip=A ) if isinstance(A , A ) else sep_token _UpperCAmelCase : Dict = AddedToken(A , lstrip=A , rstrip=A ) if isinstance(A , A ) else cls_token _UpperCAmelCase : Optional[int] = AddedToken(A , lstrip=A , rstrip=A ) if isinstance(A , A ) else unk_token _UpperCAmelCase : Dict = AddedToken(A , lstrip=A , rstrip=A ) if isinstance(A , A ) else pad_token # Mask token behave like a normal word, i.e. include the space before it _UpperCAmelCase : Dict = AddedToken(A , lstrip=A , rstrip=A ) if isinstance(A , A ) else mask_token super().__init__( errors=A , bos_token=A , eos_token=A , unk_token=A , sep_token=A , cls_token=A , pad_token=A , mask_token=A , add_prefix_space=A , **A , ) with open(A , encoding="utf-8" ) as vocab_handle: _UpperCAmelCase : Union[str, Any] = json.load(A ) _UpperCAmelCase : List[str] = {v: k for k, v in self.encoder.items()} _UpperCAmelCase : Dict = errors # how to handle errors in decoding _UpperCAmelCase : List[str] = bytes_to_unicode() _UpperCAmelCase : Dict = {v: k for k, v in self.byte_encoder.items()} with open(A , encoding="utf-8" ) as merges_handle: _UpperCAmelCase : Union[str, Any] = merges_handle.read().split("\n" )[1:-1] _UpperCAmelCase : Union[str, Any] = [tuple(merge.split() ) for merge in bpe_merges] _UpperCAmelCase : List[str] = dict(zip(A , range(len(A ) ) ) ) _UpperCAmelCase : Tuple = {} _UpperCAmelCase : Optional[Any] = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions _UpperCAmelCase : Optional[int] = re.compile(R"'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+" ) @property def snake_case_ ( self : Optional[Any] ): return len(self.encoder ) def snake_case_ ( self : List[str] ): return dict(self.encoder , **self.added_tokens_encoder ) def snake_case_ ( self : Tuple , A : Union[str, Any] ): if token in self.cache: return self.cache[token] _UpperCAmelCase : Optional[int] = tuple(A ) _UpperCAmelCase : Optional[Any] = get_pairs(A ) if not pairs: return token while True: _UpperCAmelCase : Optional[int] = min(A , key=lambda A : self.bpe_ranks.get(A , float("inf" ) ) ) if bigram not in self.bpe_ranks: break _UpperCAmelCase , _UpperCAmelCase : str = bigram _UpperCAmelCase : Dict = [] _UpperCAmelCase : Union[str, Any] = 0 while i < len(A ): try: _UpperCAmelCase : int = word.index(A , A ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) _UpperCAmelCase : Dict = j if word[i] == first and i < len(A ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 _UpperCAmelCase : Optional[Any] = tuple(A ) _UpperCAmelCase : Dict = new_word if len(A ) == 1: break else: _UpperCAmelCase : Optional[int] = get_pairs(A ) _UpperCAmelCase : Any = " ".join(A ) _UpperCAmelCase : int = word return word def snake_case_ ( self : Optional[int] , A : List[str] ): _UpperCAmelCase : str = [] for token in re.findall(self.pat , A ): _UpperCAmelCase : Optional[Any] = "".join( self.byte_encoder[b] for b in token.encode("utf-8" ) ) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(A ).split(" " ) ) return bpe_tokens def snake_case_ ( self : Optional[int] , A : Union[str, Any] ): return self.encoder.get(A , self.encoder.get(self.unk_token ) ) def snake_case_ ( self : Union[str, Any] , A : List[str] ): return self.decoder.get(A ) def snake_case_ ( self : Dict , A : int ): _UpperCAmelCase : Tuple = "".join(A ) _UpperCAmelCase : Dict = bytearray([self.byte_decoder[c] for c in text] ).decode("utf-8" , errors=self.errors ) return text def snake_case_ ( self : int , A : str , A : Optional[str] = None ): if not os.path.isdir(A ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return _UpperCAmelCase : List[Any] = os.path.join( A , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) _UpperCAmelCase : Any = os.path.join( A , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"] ) with open(A , "w" , encoding="utf-8" ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=A , ensure_ascii=A ) + "\n" ) _UpperCAmelCase : Optional[Any] = 0 with open(A , "w" , encoding="utf-8" ) as writer: writer.write("#version: 0.2\n" ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda A : kv[1] ): if index != token_index: logger.warning( f'Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.' " Please check that the tokenizer is not corrupted!" ) _UpperCAmelCase : Any = token_index writer.write(" ".join(A ) + "\n" ) index += 1 return vocab_file, merge_file def snake_case_ ( self : int , A : List[int] , A : Optional[List[int]] = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] _UpperCAmelCase : List[Any] = [self.cls_token_id] _UpperCAmelCase : List[Any] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def snake_case_ ( self : Optional[Any] , A : List[int] , A : Optional[List[int]] = None , A : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=A , token_ids_a=A , already_has_special_tokens=A ) if token_ids_a is None: return [1] + ([0] * len(A )) + [1] return [1] + ([0] * len(A )) + [1, 1] + ([0] * len(A )) + [1] def snake_case_ ( self : str , A : List[int] , A : Optional[List[int]] = None ): _UpperCAmelCase : Optional[Any] = [self.sep_token_id] _UpperCAmelCase : 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 snake_case_ ( self : str , A : List[str] , A : List[str]=False , **A : List[str] ): _UpperCAmelCase : Optional[Any] = kwargs.pop("add_prefix_space" , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(A ) > 0 and not text[0].isspace()): _UpperCAmelCase : List[Any] = " " + text return (text, kwargs)
202
1
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() SCREAMING_SNAKE_CASE_ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE_ = { """b0""": efficientnet.EfficientNetBa, """b1""": efficientnet.EfficientNetBa, """b2""": efficientnet.EfficientNetBa, """b3""": efficientnet.EfficientNetBa, """b4""": efficientnet.EfficientNetBa, """b5""": efficientnet.EfficientNetBa, """b6""": efficientnet.EfficientNetBa, """b7""": efficientnet.EfficientNetBa, } SCREAMING_SNAKE_CASE_ = { """b0""": { """hidden_dim""": 1_2_8_0, """width_coef""": 1.0, """depth_coef""": 1.0, """image_size""": 2_2_4, """dropout_rate""": 0.2, """dw_padding""": [], }, """b1""": { """hidden_dim""": 1_2_8_0, """width_coef""": 1.0, """depth_coef""": 1.1, """image_size""": 2_4_0, """dropout_rate""": 0.2, """dw_padding""": [1_6], }, """b2""": { """hidden_dim""": 1_4_0_8, """width_coef""": 1.1, """depth_coef""": 1.2, """image_size""": 2_6_0, """dropout_rate""": 0.3, """dw_padding""": [5, 8, 1_6], }, """b3""": { """hidden_dim""": 1_5_3_6, """width_coef""": 1.2, """depth_coef""": 1.4, """image_size""": 3_0_0, """dropout_rate""": 0.3, """dw_padding""": [5, 1_8], }, """b4""": { """hidden_dim""": 1_7_9_2, """width_coef""": 1.4, """depth_coef""": 1.8, """image_size""": 3_8_0, """dropout_rate""": 0.4, """dw_padding""": [6], }, """b5""": { """hidden_dim""": 2_0_4_8, """width_coef""": 1.6, """depth_coef""": 2.2, """image_size""": 4_5_6, """dropout_rate""": 0.4, """dw_padding""": [1_3, 2_7], }, """b6""": { """hidden_dim""": 2_3_0_4, """width_coef""": 1.8, """depth_coef""": 2.6, """image_size""": 5_2_8, """dropout_rate""": 0.5, """dw_padding""": [3_1], }, """b7""": { """hidden_dim""": 2_5_6_0, """width_coef""": 2.0, """depth_coef""": 3.1, """image_size""": 6_0_0, """dropout_rate""": 0.5, """dw_padding""": [1_8], }, } def __lowercase ( _SCREAMING_SNAKE_CASE ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = EfficientNetConfig() SCREAMING_SNAKE_CASE = CONFIG_MAP[model_name]["""hidden_dim"""] SCREAMING_SNAKE_CASE = CONFIG_MAP[model_name]["""width_coef"""] SCREAMING_SNAKE_CASE = CONFIG_MAP[model_name]["""depth_coef"""] SCREAMING_SNAKE_CASE = CONFIG_MAP[model_name]["""image_size"""] SCREAMING_SNAKE_CASE = CONFIG_MAP[model_name]["""dropout_rate"""] SCREAMING_SNAKE_CASE = CONFIG_MAP[model_name]["""dw_padding"""] SCREAMING_SNAKE_CASE = """huggingface/label-files""" SCREAMING_SNAKE_CASE = """imagenet-1k-id2label.json""" SCREAMING_SNAKE_CASE = 10_00 SCREAMING_SNAKE_CASE = json.load(open(hf_hub_download(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , repo_type="""dataset""" ) , """r""" ) ) SCREAMING_SNAKE_CASE = {int(_SCREAMING_SNAKE_CASE ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE = idalabel SCREAMING_SNAKE_CASE = {v: k for k, v in idalabel.items()} return config def __lowercase ( ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = """http://images.cocodataset.org/val2017/000000039769.jpg""" SCREAMING_SNAKE_CASE = Image.open(requests.get(_SCREAMING_SNAKE_CASE , stream=_SCREAMING_SNAKE_CASE ).raw ) return im def __lowercase ( _SCREAMING_SNAKE_CASE ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = CONFIG_MAP[model_name]["""image_size"""] SCREAMING_SNAKE_CASE = 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=_SCREAMING_SNAKE_CASE , ) return preprocessor def __lowercase ( _SCREAMING_SNAKE_CASE ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = [v.split("""_""" )[0].split("""block""" )[1] for v in original_param_names if v.startswith("""block""" )] SCREAMING_SNAKE_CASE = sorted(set(_SCREAMING_SNAKE_CASE ) ) SCREAMING_SNAKE_CASE = len(_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE = {b: str(_SCREAMING_SNAKE_CASE ) for b, i in zip(_SCREAMING_SNAKE_CASE , range(_SCREAMING_SNAKE_CASE ) )} SCREAMING_SNAKE_CASE = [] 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: SCREAMING_SNAKE_CASE = 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""") ) SCREAMING_SNAKE_CASE = {} for item in rename_keys: if item[0] in original_param_names: SCREAMING_SNAKE_CASE = """efficientnet.""" + item[1] SCREAMING_SNAKE_CASE = """classifier.weight""" SCREAMING_SNAKE_CASE = """classifier.bias""" return key_mapping def __lowercase ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: '''simple docstring''' for key, value in tf_params.items(): if "normalization" in key: continue SCREAMING_SNAKE_CASE = key_mapping[key] if "_conv" in key and "kernel" in key: SCREAMING_SNAKE_CASE = torch.from_numpy(_SCREAMING_SNAKE_CASE ).permute(3 , 2 , 0 , 1 ) elif "depthwise_kernel" in key: SCREAMING_SNAKE_CASE = torch.from_numpy(_SCREAMING_SNAKE_CASE ).permute(2 , 3 , 0 , 1 ) elif "kernel" in key: SCREAMING_SNAKE_CASE = torch.from_numpy(np.transpose(_SCREAMING_SNAKE_CASE ) ) else: SCREAMING_SNAKE_CASE = torch.from_numpy(_SCREAMING_SNAKE_CASE ) # Replace HF parameters with original TF model parameters assert hf_params[hf_key].shape == new_hf_value.shape hf_params[hf_key].copy_(_SCREAMING_SNAKE_CASE ) @torch.no_grad() def __lowercase ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = model_classes[model_name]( include_top=_SCREAMING_SNAKE_CASE , weights="""imagenet""" , input_tensor=_SCREAMING_SNAKE_CASE , input_shape=_SCREAMING_SNAKE_CASE , pooling=_SCREAMING_SNAKE_CASE , classes=10_00 , classifier_activation="""softmax""" , ) SCREAMING_SNAKE_CASE = original_model.trainable_variables SCREAMING_SNAKE_CASE = original_model.non_trainable_variables SCREAMING_SNAKE_CASE = {param.name: param.numpy() for param in tf_params} for param in tf_non_train_params: SCREAMING_SNAKE_CASE = param.numpy() SCREAMING_SNAKE_CASE = list(tf_params.keys() ) # Load HuggingFace model SCREAMING_SNAKE_CASE = get_efficientnet_config(_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE = EfficientNetForImageClassification(_SCREAMING_SNAKE_CASE ).eval() SCREAMING_SNAKE_CASE = hf_model.state_dict() # Create src-to-dst parameter name mapping dictionary print("""Converting parameters...""" ) SCREAMING_SNAKE_CASE = rename_keys(_SCREAMING_SNAKE_CASE ) replace_params(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Initialize preprocessor and preprocess input image SCREAMING_SNAKE_CASE = convert_image_processor(_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE = preprocessor(images=prepare_img() , return_tensors="""pt""" ) # HF model inference hf_model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE = hf_model(**_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE = outputs.logits.detach().numpy() # Original model inference SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = CONFIG_MAP[model_name]["""image_size"""] SCREAMING_SNAKE_CASE = prepare_img().resize((image_size, image_size) , resample=PIL.Image.NEAREST ) SCREAMING_SNAKE_CASE = image.img_to_array(_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE = np.expand_dims(_SCREAMING_SNAKE_CASE , axis=0 ) SCREAMING_SNAKE_CASE = original_model.predict(_SCREAMING_SNAKE_CASE ) # Check whether original and HF model outputs match -> np.allclose assert np.allclose(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , 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(_SCREAMING_SNAKE_CASE ): os.mkdir(_SCREAMING_SNAKE_CASE ) # Save converted model and image processor hf_model.save_pretrained(_SCREAMING_SNAKE_CASE ) preprocessor.save_pretrained(_SCREAMING_SNAKE_CASE ) if push_to_hub: # Push model and image processor to hub print(F"""Pushing converted {model_name} to the hub...""" ) SCREAMING_SNAKE_CASE = F"""efficientnet-{model_name}""" preprocessor.push_to_hub(_SCREAMING_SNAKE_CASE ) hf_model.push_to_hub(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": SCREAMING_SNAKE_CASE_ = 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""") SCREAMING_SNAKE_CASE_ = parser.parse_args() convert_efficientnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.save_model, args.push_to_hub)
296
from collections import OrderedDict from ...utils import logging from .auto_factory import _BaseAutoModelClass, _LazyAutoMapping, auto_class_update from .configuration_auto import CONFIG_MAPPING_NAMES SCREAMING_SNAKE_CASE_ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE_ = OrderedDict( [ # Base model mapping ("""albert""", """FlaxAlbertModel"""), ("""bart""", """FlaxBartModel"""), ("""beit""", """FlaxBeitModel"""), ("""bert""", """FlaxBertModel"""), ("""big_bird""", """FlaxBigBirdModel"""), ("""blenderbot""", """FlaxBlenderbotModel"""), ("""blenderbot-small""", """FlaxBlenderbotSmallModel"""), ("""clip""", """FlaxCLIPModel"""), ("""distilbert""", """FlaxDistilBertModel"""), ("""electra""", """FlaxElectraModel"""), ("""gpt-sw3""", """FlaxGPT2Model"""), ("""gpt2""", """FlaxGPT2Model"""), ("""gpt_neo""", """FlaxGPTNeoModel"""), ("""gptj""", """FlaxGPTJModel"""), ("""longt5""", """FlaxLongT5Model"""), ("""marian""", """FlaxMarianModel"""), ("""mbart""", """FlaxMBartModel"""), ("""mt5""", """FlaxMT5Model"""), ("""opt""", """FlaxOPTModel"""), ("""pegasus""", """FlaxPegasusModel"""), ("""regnet""", """FlaxRegNetModel"""), ("""resnet""", """FlaxResNetModel"""), ("""roberta""", """FlaxRobertaModel"""), ("""roberta-prelayernorm""", """FlaxRobertaPreLayerNormModel"""), ("""roformer""", """FlaxRoFormerModel"""), ("""t5""", """FlaxT5Model"""), ("""vision-text-dual-encoder""", """FlaxVisionTextDualEncoderModel"""), ("""vit""", """FlaxViTModel"""), ("""wav2vec2""", """FlaxWav2Vec2Model"""), ("""whisper""", """FlaxWhisperModel"""), ("""xglm""", """FlaxXGLMModel"""), ("""xlm-roberta""", """FlaxXLMRobertaModel"""), ] ) SCREAMING_SNAKE_CASE_ = OrderedDict( [ # Model for pre-training mapping ("""albert""", """FlaxAlbertForPreTraining"""), ("""bart""", """FlaxBartForConditionalGeneration"""), ("""bert""", """FlaxBertForPreTraining"""), ("""big_bird""", """FlaxBigBirdForPreTraining"""), ("""electra""", """FlaxElectraForPreTraining"""), ("""longt5""", """FlaxLongT5ForConditionalGeneration"""), ("""mbart""", """FlaxMBartForConditionalGeneration"""), ("""mt5""", """FlaxMT5ForConditionalGeneration"""), ("""roberta""", """FlaxRobertaForMaskedLM"""), ("""roberta-prelayernorm""", """FlaxRobertaPreLayerNormForMaskedLM"""), ("""roformer""", """FlaxRoFormerForMaskedLM"""), ("""t5""", """FlaxT5ForConditionalGeneration"""), ("""wav2vec2""", """FlaxWav2Vec2ForPreTraining"""), ("""whisper""", """FlaxWhisperForConditionalGeneration"""), ("""xlm-roberta""", """FlaxXLMRobertaForMaskedLM"""), ] ) SCREAMING_SNAKE_CASE_ = OrderedDict( [ # Model for Masked LM mapping ("""albert""", """FlaxAlbertForMaskedLM"""), ("""bart""", """FlaxBartForConditionalGeneration"""), ("""bert""", """FlaxBertForMaskedLM"""), ("""big_bird""", """FlaxBigBirdForMaskedLM"""), ("""distilbert""", """FlaxDistilBertForMaskedLM"""), ("""electra""", """FlaxElectraForMaskedLM"""), ("""mbart""", """FlaxMBartForConditionalGeneration"""), ("""roberta""", """FlaxRobertaForMaskedLM"""), ("""roberta-prelayernorm""", """FlaxRobertaPreLayerNormForMaskedLM"""), ("""roformer""", """FlaxRoFormerForMaskedLM"""), ("""xlm-roberta""", """FlaxXLMRobertaForMaskedLM"""), ] ) SCREAMING_SNAKE_CASE_ = OrderedDict( [ # Model for Seq2Seq Causal LM mapping ("""bart""", """FlaxBartForConditionalGeneration"""), ("""blenderbot""", """FlaxBlenderbotForConditionalGeneration"""), ("""blenderbot-small""", """FlaxBlenderbotSmallForConditionalGeneration"""), ("""encoder-decoder""", """FlaxEncoderDecoderModel"""), ("""longt5""", """FlaxLongT5ForConditionalGeneration"""), ("""marian""", """FlaxMarianMTModel"""), ("""mbart""", """FlaxMBartForConditionalGeneration"""), ("""mt5""", """FlaxMT5ForConditionalGeneration"""), ("""pegasus""", """FlaxPegasusForConditionalGeneration"""), ("""t5""", """FlaxT5ForConditionalGeneration"""), ] ) SCREAMING_SNAKE_CASE_ = OrderedDict( [ # Model for Image-classsification ("""beit""", """FlaxBeitForImageClassification"""), ("""regnet""", """FlaxRegNetForImageClassification"""), ("""resnet""", """FlaxResNetForImageClassification"""), ("""vit""", """FlaxViTForImageClassification"""), ] ) SCREAMING_SNAKE_CASE_ = OrderedDict( [ ("""vision-encoder-decoder""", """FlaxVisionEncoderDecoderModel"""), ] ) SCREAMING_SNAKE_CASE_ = OrderedDict( [ # Model for Causal LM mapping ("""bart""", """FlaxBartForCausalLM"""), ("""bert""", """FlaxBertForCausalLM"""), ("""big_bird""", """FlaxBigBirdForCausalLM"""), ("""electra""", """FlaxElectraForCausalLM"""), ("""gpt-sw3""", """FlaxGPT2LMHeadModel"""), ("""gpt2""", """FlaxGPT2LMHeadModel"""), ("""gpt_neo""", """FlaxGPTNeoForCausalLM"""), ("""gptj""", """FlaxGPTJForCausalLM"""), ("""opt""", """FlaxOPTForCausalLM"""), ("""roberta""", """FlaxRobertaForCausalLM"""), ("""roberta-prelayernorm""", """FlaxRobertaPreLayerNormForCausalLM"""), ("""xglm""", """FlaxXGLMForCausalLM"""), ("""xlm-roberta""", """FlaxXLMRobertaForCausalLM"""), ] ) SCREAMING_SNAKE_CASE_ = OrderedDict( [ # Model for Sequence Classification mapping ("""albert""", """FlaxAlbertForSequenceClassification"""), ("""bart""", """FlaxBartForSequenceClassification"""), ("""bert""", """FlaxBertForSequenceClassification"""), ("""big_bird""", """FlaxBigBirdForSequenceClassification"""), ("""distilbert""", """FlaxDistilBertForSequenceClassification"""), ("""electra""", """FlaxElectraForSequenceClassification"""), ("""mbart""", """FlaxMBartForSequenceClassification"""), ("""roberta""", """FlaxRobertaForSequenceClassification"""), ("""roberta-prelayernorm""", """FlaxRobertaPreLayerNormForSequenceClassification"""), ("""roformer""", """FlaxRoFormerForSequenceClassification"""), ("""xlm-roberta""", """FlaxXLMRobertaForSequenceClassification"""), ] ) SCREAMING_SNAKE_CASE_ = OrderedDict( [ # Model for Question Answering mapping ("""albert""", """FlaxAlbertForQuestionAnswering"""), ("""bart""", """FlaxBartForQuestionAnswering"""), ("""bert""", """FlaxBertForQuestionAnswering"""), ("""big_bird""", """FlaxBigBirdForQuestionAnswering"""), ("""distilbert""", """FlaxDistilBertForQuestionAnswering"""), ("""electra""", """FlaxElectraForQuestionAnswering"""), ("""mbart""", """FlaxMBartForQuestionAnswering"""), ("""roberta""", """FlaxRobertaForQuestionAnswering"""), ("""roberta-prelayernorm""", """FlaxRobertaPreLayerNormForQuestionAnswering"""), ("""roformer""", """FlaxRoFormerForQuestionAnswering"""), ("""xlm-roberta""", """FlaxXLMRobertaForQuestionAnswering"""), ] ) SCREAMING_SNAKE_CASE_ = OrderedDict( [ # Model for Token Classification mapping ("""albert""", """FlaxAlbertForTokenClassification"""), ("""bert""", """FlaxBertForTokenClassification"""), ("""big_bird""", """FlaxBigBirdForTokenClassification"""), ("""distilbert""", """FlaxDistilBertForTokenClassification"""), ("""electra""", """FlaxElectraForTokenClassification"""), ("""roberta""", """FlaxRobertaForTokenClassification"""), ("""roberta-prelayernorm""", """FlaxRobertaPreLayerNormForTokenClassification"""), ("""roformer""", """FlaxRoFormerForTokenClassification"""), ("""xlm-roberta""", """FlaxXLMRobertaForTokenClassification"""), ] ) SCREAMING_SNAKE_CASE_ = OrderedDict( [ # Model for Multiple Choice mapping ("""albert""", """FlaxAlbertForMultipleChoice"""), ("""bert""", """FlaxBertForMultipleChoice"""), ("""big_bird""", """FlaxBigBirdForMultipleChoice"""), ("""distilbert""", """FlaxDistilBertForMultipleChoice"""), ("""electra""", """FlaxElectraForMultipleChoice"""), ("""roberta""", """FlaxRobertaForMultipleChoice"""), ("""roberta-prelayernorm""", """FlaxRobertaPreLayerNormForMultipleChoice"""), ("""roformer""", """FlaxRoFormerForMultipleChoice"""), ("""xlm-roberta""", """FlaxXLMRobertaForMultipleChoice"""), ] ) SCREAMING_SNAKE_CASE_ = OrderedDict( [ ("""bert""", """FlaxBertForNextSentencePrediction"""), ] ) SCREAMING_SNAKE_CASE_ = OrderedDict( [ ("""speech-encoder-decoder""", """FlaxSpeechEncoderDecoderModel"""), ("""whisper""", """FlaxWhisperForConditionalGeneration"""), ] ) SCREAMING_SNAKE_CASE_ = OrderedDict( [ ("""whisper""", """FlaxWhisperForAudioClassification"""), ] ) SCREAMING_SNAKE_CASE_ = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_MAPPING_NAMES) SCREAMING_SNAKE_CASE_ = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_PRETRAINING_MAPPING_NAMES) SCREAMING_SNAKE_CASE_ = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_MASKED_LM_MAPPING_NAMES) SCREAMING_SNAKE_CASE_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES ) SCREAMING_SNAKE_CASE_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES ) SCREAMING_SNAKE_CASE_ = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES) SCREAMING_SNAKE_CASE_ = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_CAUSAL_LM_MAPPING_NAMES) SCREAMING_SNAKE_CASE_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES ) SCREAMING_SNAKE_CASE_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES ) SCREAMING_SNAKE_CASE_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES ) SCREAMING_SNAKE_CASE_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES ) SCREAMING_SNAKE_CASE_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING_NAMES ) SCREAMING_SNAKE_CASE_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES ) SCREAMING_SNAKE_CASE_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES ) class UpperCamelCase__ ( _BaseAutoModelClass ): '''simple docstring''' __snake_case : List[str] = FLAX_MODEL_MAPPING SCREAMING_SNAKE_CASE_ = auto_class_update(FlaxAutoModel) class UpperCamelCase__ ( _BaseAutoModelClass ): '''simple docstring''' __snake_case : Dict = FLAX_MODEL_FOR_PRETRAINING_MAPPING SCREAMING_SNAKE_CASE_ = auto_class_update(FlaxAutoModelForPreTraining, head_doc="""pretraining""") class UpperCamelCase__ ( _BaseAutoModelClass ): '''simple docstring''' __snake_case : Optional[Any] = FLAX_MODEL_FOR_CAUSAL_LM_MAPPING SCREAMING_SNAKE_CASE_ = auto_class_update(FlaxAutoModelForCausalLM, head_doc="""causal language modeling""") class UpperCamelCase__ ( _BaseAutoModelClass ): '''simple docstring''' __snake_case : Any = FLAX_MODEL_FOR_MASKED_LM_MAPPING SCREAMING_SNAKE_CASE_ = auto_class_update(FlaxAutoModelForMaskedLM, head_doc="""masked language modeling""") class UpperCamelCase__ ( _BaseAutoModelClass ): '''simple docstring''' __snake_case : int = FLAX_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING SCREAMING_SNAKE_CASE_ = auto_class_update( FlaxAutoModelForSeqaSeqLM, head_doc="""sequence-to-sequence language modeling""", checkpoint_for_example="""t5-base""" ) class UpperCamelCase__ ( _BaseAutoModelClass ): '''simple docstring''' __snake_case : Optional[int] = FLAX_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING SCREAMING_SNAKE_CASE_ = auto_class_update( FlaxAutoModelForSequenceClassification, head_doc="""sequence classification""" ) class UpperCamelCase__ ( _BaseAutoModelClass ): '''simple docstring''' __snake_case : List[Any] = FLAX_MODEL_FOR_QUESTION_ANSWERING_MAPPING SCREAMING_SNAKE_CASE_ = auto_class_update(FlaxAutoModelForQuestionAnswering, head_doc="""question answering""") class UpperCamelCase__ ( _BaseAutoModelClass ): '''simple docstring''' __snake_case : Tuple = FLAX_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING SCREAMING_SNAKE_CASE_ = auto_class_update( FlaxAutoModelForTokenClassification, head_doc="""token classification""" ) class UpperCamelCase__ ( _BaseAutoModelClass ): '''simple docstring''' __snake_case : Union[str, Any] = FLAX_MODEL_FOR_MULTIPLE_CHOICE_MAPPING SCREAMING_SNAKE_CASE_ = auto_class_update(FlaxAutoModelForMultipleChoice, head_doc="""multiple choice""") class UpperCamelCase__ ( _BaseAutoModelClass ): '''simple docstring''' __snake_case : List[str] = FLAX_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING SCREAMING_SNAKE_CASE_ = auto_class_update( FlaxAutoModelForNextSentencePrediction, head_doc="""next sentence prediction""" ) class UpperCamelCase__ ( _BaseAutoModelClass ): '''simple docstring''' __snake_case : Dict = FLAX_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING SCREAMING_SNAKE_CASE_ = auto_class_update( FlaxAutoModelForImageClassification, head_doc="""image classification""" ) class UpperCamelCase__ ( _BaseAutoModelClass ): '''simple docstring''' __snake_case : Dict = FLAX_MODEL_FOR_VISION_2_SEQ_MAPPING SCREAMING_SNAKE_CASE_ = auto_class_update(FlaxAutoModelForVisionaSeq, head_doc="""vision-to-text modeling""") class UpperCamelCase__ ( _BaseAutoModelClass ): '''simple docstring''' __snake_case : Optional[Any] = FLAX_MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING SCREAMING_SNAKE_CASE_ = auto_class_update( FlaxAutoModelForSpeechSeqaSeq, head_doc="""sequence-to-sequence speech-to-text modeling""" )
296
1
import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging _UpperCAmelCase = logging.get_logger(__name__) _UpperCAmelCase = {'vocab_file': 'spiece.model'} _UpperCAmelCase = { 'vocab_file': { 'albert-base-v1': 'https://huggingface.co/albert-base-v1/resolve/main/spiece.model', 'albert-large-v1': 'https://huggingface.co/albert-large-v1/resolve/main/spiece.model', 'albert-xlarge-v1': 'https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model', 'albert-xxlarge-v1': 'https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model', 'albert-base-v2': 'https://huggingface.co/albert-base-v2/resolve/main/spiece.model', 'albert-large-v2': 'https://huggingface.co/albert-large-v2/resolve/main/spiece.model', 'albert-xlarge-v2': 'https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model', 'albert-xxlarge-v2': 'https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model', } } _UpperCAmelCase = { 'albert-base-v1': 5_1_2, 'albert-large-v1': 5_1_2, 'albert-xlarge-v1': 5_1_2, 'albert-xxlarge-v1': 5_1_2, 'albert-base-v2': 5_1_2, 'albert-large-v2': 5_1_2, 'albert-xlarge-v2': 5_1_2, 'albert-xxlarge-v2': 5_1_2, } _UpperCAmelCase = '▁' class _UpperCamelCase ( lowerCAmelCase_ ): _UpperCamelCase : Optional[int] = VOCAB_FILES_NAMES _UpperCamelCase : int = PRETRAINED_VOCAB_FILES_MAP _UpperCamelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self: str , _SCREAMING_SNAKE_CASE: Optional[Any] , _SCREAMING_SNAKE_CASE: Union[str, Any]=True , _SCREAMING_SNAKE_CASE: Union[str, Any]=True , _SCREAMING_SNAKE_CASE: Any=False , _SCREAMING_SNAKE_CASE: List[Any]="[CLS]" , _SCREAMING_SNAKE_CASE: Tuple="[SEP]" , _SCREAMING_SNAKE_CASE: List[Any]="<unk>" , _SCREAMING_SNAKE_CASE: str="[SEP]" , _SCREAMING_SNAKE_CASE: Any="<pad>" , _SCREAMING_SNAKE_CASE: Dict="[CLS]" , _SCREAMING_SNAKE_CASE: Any="[MASK]" , _SCREAMING_SNAKE_CASE: Optional[Dict[str, Any]] = None , **_SCREAMING_SNAKE_CASE: List[str] , ) -> None: """simple docstring""" UpperCamelCase_ = ( AddedToken(_SCREAMING_SNAKE_CASE , lstrip=_SCREAMING_SNAKE_CASE , rstrip=_SCREAMING_SNAKE_CASE , normalized=_SCREAMING_SNAKE_CASE ) if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else mask_token ) UpperCamelCase_ = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=_SCREAMING_SNAKE_CASE , remove_space=_SCREAMING_SNAKE_CASE , keep_accents=_SCREAMING_SNAKE_CASE , bos_token=_SCREAMING_SNAKE_CASE , eos_token=_SCREAMING_SNAKE_CASE , unk_token=_SCREAMING_SNAKE_CASE , sep_token=_SCREAMING_SNAKE_CASE , pad_token=_SCREAMING_SNAKE_CASE , cls_token=_SCREAMING_SNAKE_CASE , mask_token=_SCREAMING_SNAKE_CASE , sp_model_kwargs=self.sp_model_kwargs , **_SCREAMING_SNAKE_CASE , ) UpperCamelCase_ = do_lower_case UpperCamelCase_ = remove_space UpperCamelCase_ = keep_accents UpperCamelCase_ = vocab_file UpperCamelCase_ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(_SCREAMING_SNAKE_CASE ) @property def lowercase ( self: int ) -> List[str]: """simple docstring""" return len(self.sp_model ) def lowercase ( self: Dict ) -> Optional[Any]: """simple docstring""" UpperCamelCase_ = {self.convert_ids_to_tokens(_SCREAMING_SNAKE_CASE ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self: Any ) -> Optional[Any]: """simple docstring""" UpperCamelCase_ = self.__dict__.copy() UpperCamelCase_ = None return state def __setstate__( self: Union[str, Any] , _SCREAMING_SNAKE_CASE: Optional[Any] ) -> Optional[int]: """simple docstring""" UpperCamelCase_ = d # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): UpperCamelCase_ = {} UpperCamelCase_ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def lowercase ( self: int , _SCREAMING_SNAKE_CASE: Union[str, Any] ) -> str: """simple docstring""" if self.remove_space: UpperCamelCase_ = " ".join(inputs.strip().split() ) else: UpperCamelCase_ = inputs UpperCamelCase_ = outputs.replace("``" , "\"" ).replace("''" , "\"" ) if not self.keep_accents: UpperCamelCase_ = unicodedata.normalize("NFKD" , _SCREAMING_SNAKE_CASE ) UpperCamelCase_ = "".join([c for c in outputs if not unicodedata.combining(_SCREAMING_SNAKE_CASE )] ) if self.do_lower_case: UpperCamelCase_ = outputs.lower() return outputs def lowercase ( self: Union[str, Any] , _SCREAMING_SNAKE_CASE: str ) -> List[str]: """simple docstring""" UpperCamelCase_ = self.preprocess_text(_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = self.sp_model.encode(_SCREAMING_SNAKE_CASE , out_type=_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = [] for piece in pieces: if len(_SCREAMING_SNAKE_CASE ) > 1 and piece[-1] == str("," ) and piece[-2].isdigit(): UpperCamelCase_ = self.sp_model.EncodeAsPieces(piece[:-1].replace(_SCREAMING_SNAKE_CASE , "" ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: UpperCamelCase_ = cur_pieces[1:] else: UpperCamelCase_ = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(_SCREAMING_SNAKE_CASE ) else: new_pieces.append(_SCREAMING_SNAKE_CASE ) return new_pieces def lowercase ( self: List[str] , _SCREAMING_SNAKE_CASE: List[Any] ) -> Union[str, Any]: """simple docstring""" return self.sp_model.PieceToId(_SCREAMING_SNAKE_CASE ) def lowercase ( self: Optional[int] , _SCREAMING_SNAKE_CASE: List[Any] ) -> List[Any]: """simple docstring""" return self.sp_model.IdToPiece(_SCREAMING_SNAKE_CASE ) def lowercase ( self: Any , _SCREAMING_SNAKE_CASE: Dict ) -> List[str]: """simple docstring""" UpperCamelCase_ = [] UpperCamelCase_ = "" UpperCamelCase_ = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(_SCREAMING_SNAKE_CASE ) + token UpperCamelCase_ = True UpperCamelCase_ = [] else: current_sub_tokens.append(_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = False out_string += self.sp_model.decode(_SCREAMING_SNAKE_CASE ) return out_string.strip() def lowercase ( self: List[str] , _SCREAMING_SNAKE_CASE: List[int] , _SCREAMING_SNAKE_CASE: Optional[List[int]] = None ) -> List[int]: """simple docstring""" UpperCamelCase_ = [self.sep_token_id] UpperCamelCase_ = [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: Union[str, Any] , _SCREAMING_SNAKE_CASE: List[int] , _SCREAMING_SNAKE_CASE: Optional[List[int]] = None , _SCREAMING_SNAKE_CASE: bool = False ) -> List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_SCREAMING_SNAKE_CASE , token_ids_a=_SCREAMING_SNAKE_CASE , already_has_special_tokens=_SCREAMING_SNAKE_CASE ) if token_ids_a is not None: return [1] + ([0] * len(_SCREAMING_SNAKE_CASE )) + [1] + ([0] * len(_SCREAMING_SNAKE_CASE )) + [1] return [1] + ([0] * len(_SCREAMING_SNAKE_CASE )) + [1] def lowercase ( self: List[Any] , _SCREAMING_SNAKE_CASE: List[int] , _SCREAMING_SNAKE_CASE: Optional[List[int]] = None ) -> List[int]: """simple docstring""" UpperCamelCase_ = [self.sep_token_id] UpperCamelCase_ = [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: List[Any] , _SCREAMING_SNAKE_CASE: str , _SCREAMING_SNAKE_CASE: Optional[str] = None ) -> Tuple[str]: """simple docstring""" if not os.path.isdir(_SCREAMING_SNAKE_CASE ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return UpperCamelCase_ = os.path.join( _SCREAMING_SNAKE_CASE , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_SCREAMING_SNAKE_CASE ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , _SCREAMING_SNAKE_CASE ) elif not os.path.isfile(self.vocab_file ): with open(_SCREAMING_SNAKE_CASE , "wb" ) as fi: UpperCamelCase_ = self.sp_model.serialized_model_proto() fi.write(_SCREAMING_SNAKE_CASE ) return (out_vocab_file,)
328
from ...configuration_utils import PretrainedConfig from ...utils import logging _UpperCAmelCase = logging.get_logger(__name__) _UpperCAmelCase = { 'alibaba-damo/mgp-str-base': 'https://huggingface.co/alibaba-damo/mgp-str-base/resolve/main/config.json', } class _UpperCamelCase ( lowerCAmelCase_ ): _UpperCamelCase : str = '''mgp-str''' def __init__( self: Optional[Any] , _SCREAMING_SNAKE_CASE: Optional[int]=[32, 128] , _SCREAMING_SNAKE_CASE: Tuple=4 , _SCREAMING_SNAKE_CASE: Optional[Any]=3 , _SCREAMING_SNAKE_CASE: Optional[int]=27 , _SCREAMING_SNAKE_CASE: Tuple=38 , _SCREAMING_SNAKE_CASE: Tuple=50257 , _SCREAMING_SNAKE_CASE: List[Any]=30522 , _SCREAMING_SNAKE_CASE: Optional[Any]=768 , _SCREAMING_SNAKE_CASE: Dict=12 , _SCREAMING_SNAKE_CASE: List[str]=12 , _SCREAMING_SNAKE_CASE: Dict=4.0 , _SCREAMING_SNAKE_CASE: int=True , _SCREAMING_SNAKE_CASE: Tuple=False , _SCREAMING_SNAKE_CASE: Tuple=1e-5 , _SCREAMING_SNAKE_CASE: Optional[Any]=0.0 , _SCREAMING_SNAKE_CASE: Tuple=0.0 , _SCREAMING_SNAKE_CASE: List[Any]=0.0 , _SCREAMING_SNAKE_CASE: List[str]=False , _SCREAMING_SNAKE_CASE: int=0.02 , **_SCREAMING_SNAKE_CASE: Any , ) -> str: """simple docstring""" super().__init__(**_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = image_size UpperCamelCase_ = patch_size UpperCamelCase_ = num_channels UpperCamelCase_ = max_token_length UpperCamelCase_ = num_character_labels UpperCamelCase_ = num_bpe_labels UpperCamelCase_ = num_wordpiece_labels UpperCamelCase_ = hidden_size UpperCamelCase_ = num_hidden_layers UpperCamelCase_ = num_attention_heads UpperCamelCase_ = mlp_ratio UpperCamelCase_ = distilled UpperCamelCase_ = layer_norm_eps UpperCamelCase_ = drop_rate UpperCamelCase_ = qkv_bias UpperCamelCase_ = attn_drop_rate UpperCamelCase_ = drop_path_rate UpperCamelCase_ = output_aa_attentions UpperCamelCase_ = initializer_range
328
1
'''simple docstring''' import argparse import os import re import packaging.version __UpperCAmelCase ="examples/" __UpperCAmelCase ={ "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"), } __UpperCAmelCase ={ "init": "src/transformers/__init__.py", "setup": "setup.py", } __UpperCAmelCase ="README.md" def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> int: with open(SCREAMING_SNAKE_CASE_ , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: __lowerCamelCase = f.read() __lowerCamelCase , __lowerCamelCase = REPLACE_PATTERNS[pattern] __lowerCamelCase = replace.replace('''VERSION''' , SCREAMING_SNAKE_CASE_ ) __lowerCamelCase = re_pattern.sub(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) with open(SCREAMING_SNAKE_CASE_ , '''w''' , encoding='''utf-8''' , newline='''\n''' ) as f: f.write(SCREAMING_SNAKE_CASE_ ) def __lowerCAmelCase ( UpperCamelCase__ ) -> Optional[Any]: for folder, directories, fnames in os.walk(SCREAMING_SNAKE_CASE_ ): # 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(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ , pattern='''examples''' ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__=False ) -> Optional[Any]: for pattern, fname in REPLACE_FILES.items(): update_version_in_file(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if not patch: update_version_in_examples(SCREAMING_SNAKE_CASE_ ) def __lowerCAmelCase ( ) -> List[str]: __lowerCamelCase = '''🤗 Transformers currently provides the following architectures''' __lowerCamelCase = '''1. Want to contribute a new model?''' with open(SCREAMING_SNAKE_CASE_ , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: __lowerCamelCase = f.readlines() # Find the start of the list. __lowerCamelCase = 0 while not lines[start_index].startswith(_start_prompt ): start_index += 1 start_index += 1 __lowerCamelCase = start_index # Update the lines in the model list. while not lines[index].startswith(_end_prompt ): if lines[index].startswith('''1.''' ): __lowerCamelCase = lines[index].replace( '''https://huggingface.co/docs/transformers/main/model_doc''' , '''https://huggingface.co/docs/transformers/model_doc''' , ) index += 1 with open(SCREAMING_SNAKE_CASE_ , '''w''' , encoding='''utf-8''' , newline='''\n''' ) as f: f.writelines(SCREAMING_SNAKE_CASE_ ) def __lowerCAmelCase ( ) -> Optional[Any]: with open(REPLACE_FILES['''init'''] , '''r''' ) as f: __lowerCamelCase = f.read() __lowerCamelCase = REPLACE_PATTERNS['''init'''][0].search(SCREAMING_SNAKE_CASE_ ).groups()[0] return packaging.version.parse(SCREAMING_SNAKE_CASE_ ) def __lowerCAmelCase ( UpperCamelCase__=False ) -> Optional[Any]: __lowerCamelCase = 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: __lowerCamelCase = default_version.base_version elif patch: __lowerCamelCase = f"""{default_version.major}.{default_version.minor}.{default_version.micro + 1}""" else: __lowerCamelCase = f"""{default_version.major}.{default_version.minor + 1}.0""" # Now let's ask nicely if that's the right one. __lowerCamelCase = input(f"""Which version are you releasing? [{default_version}]""" ) if len(SCREAMING_SNAKE_CASE_ ) == 0: __lowerCamelCase = default_version print(f"""Updating version to {version}.""" ) global_version_update(SCREAMING_SNAKE_CASE_ , patch=SCREAMING_SNAKE_CASE_ ) if not patch: print('''Cleaning main README, don\'t forget to run `make fix-copies`.''' ) clean_main_ref_in_model_list() def __lowerCAmelCase ( ) -> Optional[Any]: __lowerCamelCase = get_version() __lowerCamelCase = f"""{current_version.major}.{current_version.minor + 1}.0.dev0""" __lowerCamelCase = current_version.base_version # Check with the user we got that right. __lowerCamelCase = input(f"""Which version are we developing now? [{dev_version}]""" ) if len(SCREAMING_SNAKE_CASE_ ) == 0: __lowerCamelCase = dev_version print(f"""Updating version to {version}.""" ) global_version_update(SCREAMING_SNAKE_CASE_ ) print('''Cleaning main README, don\'t forget to run `make fix-copies`.''' ) clean_main_ref_in_model_list() if __name__ == "__main__": __UpperCAmelCase =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.") __UpperCAmelCase =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()
67
from __future__ import annotations import unittest from transformers import BlenderbotSmallConfig, BlenderbotSmallTokenizer, is_tf_available from transformers.testing_utils import require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotSmallForConditionalGeneration, TFBlenderbotSmallModel @require_tf class a__ : """simple docstring""" __lowerCamelCase = BlenderbotSmallConfig __lowerCamelCase = {} __lowerCamelCase = 'gelu' def __init__( self , lowercase , lowercase=13 , lowercase=7 , lowercase=True , lowercase=False , lowercase=99 , lowercase=32 , lowercase=2 , lowercase=4 , lowercase=37 , lowercase=0.1 , lowercase=0.1 , lowercase=20 , lowercase=2 , lowercase=1 , lowercase=0 , ) -> Any: '''simple docstring''' A__ = parent A__ = batch_size A__ = seq_length A__ = is_training A__ = use_labels A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = eos_token_id A__ = pad_token_id A__ = bos_token_id def UpperCamelCase ( self ) -> Tuple: '''simple docstring''' A__ = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) A__ = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) A__ = tf.concat([input_ids, eos_tensor] , axis=1 ) A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) A__ = prepare_blenderbot_small_inputs_dict(lowercase , lowercase , lowercase ) return config, inputs_dict def UpperCamelCase ( self , lowercase , lowercase ) -> str: '''simple docstring''' A__ = TFBlenderbotSmallModel(config=lowercase ).get_decoder() A__ = inputs_dict["input_ids"] A__ = input_ids[:1, :] A__ = inputs_dict["attention_mask"][:1, :] A__ = inputs_dict["head_mask"] A__ = 1 # first forward pass A__ = model(lowercase , attention_mask=lowercase , head_mask=lowercase , use_cache=lowercase ) A__ , A__ = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids A__ = ids_tensor((self.batch_size, 3) , config.vocab_size ) A__ = tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and A__ = tf.concat([input_ids, next_tokens] , axis=-1 ) A__ = tf.concat([attention_mask, next_attn_mask] , axis=-1 ) A__ = model(lowercase , attention_mask=lowercase )[0] A__ = model(lowercase , attention_mask=lowercase , past_key_values=lowercase )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice A__ = int(ids_tensor((1,) , output_from_past.shape[-1] ) ) A__ = output_from_no_past[:, -3:, random_slice_idx] A__ = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(lowercase , lowercase , rtol=1e-3 ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_: int , SCREAMING_SNAKE_CASE_: List[Any] , SCREAMING_SNAKE_CASE_: Optional[Any] , SCREAMING_SNAKE_CASE_: Optional[Any]=None , SCREAMING_SNAKE_CASE_: Optional[int]=None , SCREAMING_SNAKE_CASE_: Optional[int]=None , SCREAMING_SNAKE_CASE_: Dict=None , SCREAMING_SNAKE_CASE_: List[str]=None , ) -> List[Any]: '''simple docstring''' if attention_mask is None: A__ = tf.cast(tf.math.not_equal(SCREAMING_SNAKE_CASE_ , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: A__ = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: A__ = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: A__ = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: A__ = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class a__ ( snake_case , snake_case , unittest.TestCase ): """simple docstring""" __lowerCamelCase = ( (TFBlenderbotSmallForConditionalGeneration, TFBlenderbotSmallModel) if is_tf_available() else () ) __lowerCamelCase = (TFBlenderbotSmallForConditionalGeneration,) if is_tf_available() else () __lowerCamelCase = ( { 'conversational': TFBlenderbotSmallForConditionalGeneration, 'feature-extraction': TFBlenderbotSmallModel, 'summarization': TFBlenderbotSmallForConditionalGeneration, 'text2text-generation': TFBlenderbotSmallForConditionalGeneration, 'translation': TFBlenderbotSmallForConditionalGeneration, } if is_tf_available() else {} ) __lowerCamelCase = True __lowerCamelCase = False __lowerCamelCase = False def UpperCamelCase ( self ) -> Tuple: '''simple docstring''' A__ = TFBlenderbotSmallModelTester(self ) A__ = ConfigTester(self , config_class=lowercase ) def UpperCamelCase ( self ) -> Tuple: '''simple docstring''' self.config_tester.run_common_tests() def UpperCamelCase ( self ) -> Tuple: '''simple docstring''' A__ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*lowercase ) @require_tokenizers @require_tf class a__ ( unittest.TestCase ): """simple docstring""" __lowerCamelCase = [ 'Social anxiety\nWow, I am never shy. Do you have anxiety?\nYes. I end up sweating and blushing and feel like ' ' i\'m going to throw up.\nand why is that?' ] __lowerCamelCase = 'facebook/blenderbot_small-90M' @cached_property def UpperCamelCase ( self ) -> Tuple: '''simple docstring''' return BlenderbotSmallTokenizer.from_pretrained("facebook/blenderbot-90M" ) @cached_property def UpperCamelCase ( self ) -> Any: '''simple docstring''' A__ = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model @slow def UpperCamelCase ( self ) -> int: '''simple docstring''' A__ = self.tokenizer(self.src_text , return_tensors="tf" ) A__ = self.model.generate( model_inputs.input_ids , attention_mask=model_inputs.attention_mask , num_beams=2 , use_cache=lowercase , ) A__ = self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=lowercase )[0] assert generated_words in ( "i don't know. i just feel like i'm going to throw up. it's not fun.", "i'm not sure. i just feel like i've been feeling like i have to be in a certain place", "i'm not sure. i just feel like i've been in a bad situation.", )
68
0
"""simple docstring""" def _lowerCamelCase ( _UpperCamelCase , _UpperCamelCase = False ): '''simple docstring''' if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __lowerCAmelCase = f"Expected string as input, found {type(lowerCAmelCase__ )}" raise ValueError(lowerCAmelCase__ ) if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __lowerCAmelCase = f"Expected boolean as use_pascal parameter, found {type(lowerCAmelCase__ )}" raise ValueError(lowerCAmelCase__ ) __lowerCAmelCase = input_str.split("_" ) __lowerCAmelCase = 0 if use_pascal else 1 __lowerCAmelCase = words[start_index:] __lowerCAmelCase = [word[0].upper() + word[1:] for word in words_to_capitalize] __lowerCAmelCase = "" if use_pascal else words[0] return "".join([initial_word, *capitalized_words] ) if __name__ == "__main__": from doctest import testmod testmod()
361
"""simple docstring""" import warnings from ...configuration_utils import PretrainedConfig from ...utils import logging A : Dict = logging.get_logger(__name__) A : Optional[int] = { "xlnet-base-cased": "https://huggingface.co/xlnet-base-cased/resolve/main/config.json", "xlnet-large-cased": "https://huggingface.co/xlnet-large-cased/resolve/main/config.json", } class _UpperCamelCase ( lowerCAmelCase__ ): '''simple docstring''' __UpperCAmelCase : Any ="""xlnet""" __UpperCAmelCase : Tuple =["""mems"""] __UpperCAmelCase : List[str] ={ """n_token""": """vocab_size""", # Backward compatibility """hidden_size""": """d_model""", """num_attention_heads""": """n_head""", """num_hidden_layers""": """n_layer""", } def __init__( self , __a=3_20_00 , __a=10_24 , __a=24 , __a=16 , __a=40_96 , __a="gelu" , __a=True , __a="bi" , __a=0.0_2 , __a=1e-1_2 , __a=0.1 , __a=5_12 , __a=None , __a=True , __a=False , __a=False , __a=-1 , __a=False , __a="last" , __a=True , __a="tanh" , __a=0.1 , __a=5 , __a=5 , __a=5 , __a=1 , __a=2 , **__a , ): __lowerCAmelCase = vocab_size __lowerCAmelCase = d_model __lowerCAmelCase = n_layer __lowerCAmelCase = n_head if d_model % n_head != 0: raise ValueError(f"'d_model % n_head' ({d_model % n_head}) should be equal to 0" ) if "d_head" in kwargs: if kwargs["d_head"] != d_model // n_head: raise ValueError( f"`d_head` ({kwargs['d_head']}) should be equal to `d_model // n_head` ({d_model // n_head})" ) __lowerCAmelCase = d_model // n_head __lowerCAmelCase = ff_activation __lowerCAmelCase = d_inner __lowerCAmelCase = untie_r __lowerCAmelCase = attn_type __lowerCAmelCase = initializer_range __lowerCAmelCase = layer_norm_eps __lowerCAmelCase = dropout __lowerCAmelCase = mem_len __lowerCAmelCase = reuse_len __lowerCAmelCase = bi_data __lowerCAmelCase = clamp_len __lowerCAmelCase = same_length __lowerCAmelCase = summary_type __lowerCAmelCase = summary_use_proj __lowerCAmelCase = summary_activation __lowerCAmelCase = summary_last_dropout __lowerCAmelCase = start_n_top __lowerCAmelCase = end_n_top __lowerCAmelCase = bos_token_id __lowerCAmelCase = pad_token_id __lowerCAmelCase = eos_token_id if "use_cache" in kwargs: warnings.warn( "The `use_cache` argument is deprecated and will be removed in a future version, use `use_mems_eval`" " instead." , __a , ) __lowerCAmelCase = kwargs["use_cache"] __lowerCAmelCase = use_mems_eval __lowerCAmelCase = use_mems_train super().__init__(pad_token_id=__a , bos_token_id=__a , eos_token_id=__a , **__a ) @property def snake_case ( self ): logger.info(f"The model {self.model_type} is one of the few models that has no sequence length limit." ) return -1 @max_position_embeddings.setter def snake_case ( self , __a ): # Message copied from Transformer-XL documentation raise NotImplementedError( f"The model {self.model_type} is one of the few models that has no sequence length limit." )
259
0
from ..utils import is_flax_available, is_torch_available if is_torch_available(): from .autoencoder_kl import AutoencoderKL from .controlnet import ControlNetModel from .dual_transformer_ad import DualTransformeraDModel from .modeling_utils import ModelMixin from .prior_transformer import PriorTransformer from .ta_film_transformer import TaFilmDecoder from .transformer_ad import TransformeraDModel from .unet_ad import UNetaDModel from .unet_ad import UNetaDModel from .unet_ad_condition import UNetaDConditionModel from .unet_ad_condition import UNetaDConditionModel from .vq_model import VQModel if is_flax_available(): from .controlnet_flax import FlaxControlNetModel from .unet_ad_condition_flax import FlaxUNetaDConditionModel from .vae_flax import FlaxAutoencoderKL
51
'''simple docstring''' import re from filelock import FileLock try: import nltk a : Union[str, Any] = True except (ImportError, ModuleNotFoundError): a : Any = False if NLTK_AVAILABLE: with FileLock('.lock') as lock: nltk.download('punkt', quiet=True) def __magic_name__ ( __UpperCAmelCase ) -> str: '''simple docstring''' re.sub('''<n>''', '''''', __UpperCAmelCase ) # remove pegasus newline char assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)" return "\n".join(nltk.sent_tokenize(__UpperCAmelCase ) )
56
0
"""simple docstring""" from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel from ...schedulers import ScoreSdeVeScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class lowerCamelCase__ ( snake_case ): SCREAMING_SNAKE_CASE = 42 SCREAMING_SNAKE_CASE = 42 def __init__( self ,A ,A ): super().__init__() self.register_modules(unet=A ,scheduler=A ) @torch.no_grad() def __call__( self ,A = 1 ,A = 2_000 ,A = None ,A = "pil" ,A = True ,**A ,): UpperCAmelCase = self.unet.config.sample_size UpperCAmelCase = (batch_size, 3, img_size, img_size) UpperCAmelCase = self.unet UpperCAmelCase = randn_tensor(A ,generator=A ) * self.scheduler.init_noise_sigma UpperCAmelCase = sample.to(self.device ) self.scheduler.set_timesteps(A ) self.scheduler.set_sigmas(A ) for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): UpperCAmelCase = self.scheduler.sigmas[i] * torch.ones(shape[0] ,device=self.device ) # correction step for _ in range(self.scheduler.config.correct_steps ): UpperCAmelCase = self.unet(A ,A ).sample UpperCAmelCase = self.scheduler.step_correct(A ,A ,generator=A ).prev_sample # prediction step UpperCAmelCase = model(A ,A ).sample UpperCAmelCase = self.scheduler.step_pred(A ,A ,A ,generator=A ) UpperCAmelCase , UpperCAmelCase = output.prev_sample, output.prev_sample_mean UpperCAmelCase = sample_mean.clamp(0 ,1 ) UpperCAmelCase = sample.cpu().permute(0 ,2 ,3 ,1 ).numpy() if output_type == "pil": UpperCAmelCase = self.numpy_to_pil(A ) if not return_dict: return (sample,) return ImagePipelineOutput(images=A )
234
"""simple docstring""" import argparse import torch from transformers import ( WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaForAudioFrameClassification, WavaVecaForSequenceClassification, WavaVecaForXVector, logging, ) logging.set_verbosity_info() _UpperCamelCase = logging.get_logger(__name__) def _a ( _snake_case , _snake_case , _snake_case ): """simple docstring""" UpperCAmelCase = WavaVecaForSequenceClassification.from_pretrained(_snake_case , config=_snake_case ) UpperCAmelCase = downstream_dict["""projector.weight"""] UpperCAmelCase = downstream_dict["""projector.bias"""] UpperCAmelCase = downstream_dict["""model.post_net.linear.weight"""] UpperCAmelCase = downstream_dict["""model.post_net.linear.bias"""] return model def _a ( _snake_case , _snake_case , _snake_case ): """simple docstring""" UpperCAmelCase = WavaVecaForAudioFrameClassification.from_pretrained(_snake_case , config=_snake_case ) UpperCAmelCase = downstream_dict["""model.linear.weight"""] UpperCAmelCase = downstream_dict["""model.linear.bias"""] return model def _a ( _snake_case , _snake_case , _snake_case ): """simple docstring""" UpperCAmelCase = WavaVecaForXVector.from_pretrained(_snake_case , config=_snake_case ) UpperCAmelCase = downstream_dict["""connector.weight"""] UpperCAmelCase = downstream_dict["""connector.bias"""] for i, kernel_size in enumerate(hf_config.tdnn_kernel ): UpperCAmelCase = downstream_dict[ F'''model.framelevel_feature_extractor.module.{i}.kernel.weight''' ] UpperCAmelCase = downstream_dict[F'''model.framelevel_feature_extractor.module.{i}.kernel.bias'''] UpperCAmelCase = downstream_dict["""model.utterancelevel_feature_extractor.linear1.weight"""] UpperCAmelCase = downstream_dict["""model.utterancelevel_feature_extractor.linear1.bias"""] UpperCAmelCase = downstream_dict["""model.utterancelevel_feature_extractor.linear2.weight"""] UpperCAmelCase = downstream_dict["""model.utterancelevel_feature_extractor.linear2.bias"""] UpperCAmelCase = downstream_dict["""objective.W"""] return model @torch.no_grad() def _a ( _snake_case , _snake_case , _snake_case , _snake_case ): """simple docstring""" UpperCAmelCase = torch.load(_snake_case , map_location="""cpu""" ) UpperCAmelCase = checkpoint["""Downstream"""] UpperCAmelCase = WavaVecaConfig.from_pretrained(_snake_case ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained( _snake_case , return_attention_mask=_snake_case , do_normalize=_snake_case ) UpperCAmelCase = hf_config.architectures[0] if arch.endswith("""ForSequenceClassification""" ): UpperCAmelCase = convert_classification(_snake_case , _snake_case , _snake_case ) elif arch.endswith("""ForAudioFrameClassification""" ): UpperCAmelCase = convert_diarization(_snake_case , _snake_case , _snake_case ) elif arch.endswith("""ForXVector""" ): UpperCAmelCase = convert_xvector(_snake_case , _snake_case , _snake_case ) else: raise NotImplementedError(F'''S3PRL weights conversion is not supported for {arch}''' ) if hf_config.use_weighted_layer_sum: UpperCAmelCase = checkpoint["""Featurizer"""]["""weights"""] hf_feature_extractor.save_pretrained(_snake_case ) hf_model.save_pretrained(_snake_case ) if __name__ == "__main__": _UpperCamelCase = argparse.ArgumentParser() parser.add_argument( """--base_model_name""", default=None, type=str, help="""Name of the huggingface pretrained base model.""" ) parser.add_argument("""--config_path""", default=None, type=str, help="""Path to the huggingface classifier config.""") parser.add_argument("""--checkpoint_path""", default=None, type=str, help="""Path to the s3prl checkpoint.""") parser.add_argument("""--model_dump_path""", default=None, type=str, help="""Path to the final converted model.""") _UpperCamelCase = parser.parse_args() convert_saprl_checkpoint(args.base_model_name, args.config_path, args.checkpoint_path, args.model_dump_path)
234
1
'''simple docstring''' import copy import random from transformers import CLIPTokenizer class a__ ( snake_case_ ): def __init__( self : str , *a : int , **a : Any ): """simple docstring""" super().__init__(*a , **a ) __lowerCamelCase = {} def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : Optional[Any] , *a : Union[str, Any] , **a : Tuple ): """simple docstring""" __lowerCamelCase = super().add_tokens(a , *a , **a ) if num_added_tokens == 0: raise ValueError( f"""The tokenizer already contains the token {placeholder_token}. Please pass a different""" ''' `placeholder_token` that is not already in the tokenizer.''' ) def SCREAMING_SNAKE_CASE__ ( self : Any , a : List[str] , *a : List[Any] , a : List[str]=1 , **a : Tuple ): """simple docstring""" __lowerCamelCase = [] if num_vec_per_token == 1: self.try_adding_tokens(a , *a , **a ) output.append(a ) else: __lowerCamelCase = [] for i in range(a ): __lowerCamelCase = placeholder_token + f"""_{i}""" self.try_adding_tokens(a , *a , **a ) output.append(a ) # handle cases where there is a new placeholder token that contains the current placeholder token but is larger for token in self.token_map: if token in placeholder_token: raise ValueError( f"""The tokenizer already has placeholder token {token} that can get confused with""" f""" {placeholder_token}keep placeholder tokens independent""" ) __lowerCamelCase = output def SCREAMING_SNAKE_CASE__ ( self : Any , a : Optional[int] , a : Optional[int]=False , a : List[Any]=1.0 ): """simple docstring""" if isinstance(a , a ): __lowerCamelCase = [] for i in range(len(a ) ): output.append(self.replace_placeholder_tokens_in_text(text[i] , vector_shuffle=a ) ) return output for placeholder_token in self.token_map: if placeholder_token in text: __lowerCamelCase = self.token_map[placeholder_token] __lowerCamelCase = tokens[: 1 + int(len(a ) * prop_tokens_to_load )] if vector_shuffle: __lowerCamelCase = copy.copy(a ) random.shuffle(a ) __lowerCamelCase = text.replace(a , ''' '''.join(a ) ) return text def __call__( self : str , a : List[Any] , *a : Union[str, Any] , a : Tuple=False , a : Optional[Any]=1.0 , **a : str ): """simple docstring""" return super().__call__( self.replace_placeholder_tokens_in_text( a , vector_shuffle=a , prop_tokens_to_load=a ) , *a , **a , ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : List[Any] , *a : List[str] , a : Optional[int]=False , a : List[str]=1.0 , **a : Tuple ): """simple docstring""" return super().encode( self.replace_placeholder_tokens_in_text( a , vector_shuffle=a , prop_tokens_to_load=a ) , *a , **a , )
67
import math def lowerCamelCase_ ( lowerCamelCase__ , lowerCamelCase__ = 0 , lowerCamelCase__ = 0 ): lowerCamelCase_ = end or len(lowerCamelCase__ ) for i in range(lowerCamelCase__ , lowerCamelCase__ ): lowerCamelCase_ = i lowerCamelCase_ = array[i] while temp_index != start and temp_index_value < array[temp_index - 1]: lowerCamelCase_ = array[temp_index - 1] temp_index -= 1 lowerCamelCase_ = temp_index_value return array def lowerCamelCase_ ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ): # Max Heap lowerCamelCase_ = index lowerCamelCase_ = 2 * index + 1 # Left Node lowerCamelCase_ = 2 * index + 2 # Right Node if left_index < heap_size and array[largest] < array[left_index]: lowerCamelCase_ = left_index if right_index < heap_size and array[largest] < array[right_index]: lowerCamelCase_ = right_index if largest != index: lowerCamelCase_ , lowerCamelCase_ = array[largest], array[index] heapify(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def lowerCamelCase_ ( lowerCamelCase__ ): lowerCamelCase_ = len(lowerCamelCase__ ) for i in range(n // 2 , -1 , -1 ): heapify(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) for i in range(n - 1 , 0 , -1 ): lowerCamelCase_ , lowerCamelCase_ = array[0], array[i] heapify(lowerCamelCase__ , 0 , lowerCamelCase__ ) return array def lowerCamelCase_ ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ): 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 lowerCamelCase_ ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ): lowerCamelCase_ = low lowerCamelCase_ = high while True: while array[i] < pivot: i += 1 j -= 1 while pivot < array[j]: j -= 1 if i >= j: return i lowerCamelCase_ , lowerCamelCase_ = array[j], array[i] i += 1 def lowerCamelCase_ ( lowerCamelCase__ ): if len(lowerCamelCase__ ) == 0: return array lowerCamelCase_ = 2 * math.ceil(math.loga(len(lowerCamelCase__ ) ) ) lowerCamelCase_ = 1_6 return intro_sort(lowerCamelCase__ , 0 , len(lowerCamelCase__ ) , lowerCamelCase__ , lowerCamelCase__ ) def lowerCamelCase_ ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ): while end - start > size_threshold: if max_depth == 0: return heap_sort(lowerCamelCase__ ) max_depth -= 1 lowerCamelCase_ = median_of_a(lowerCamelCase__ , lowerCamelCase__ , start + ((end - start) // 2) + 1 , end - 1 ) lowerCamelCase_ = partition(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) intro_sort(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) lowerCamelCase_ = p return insertion_sort(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) if __name__ == "__main__": import doctest doctest.testmod() __A =input('''Enter numbers separated by a comma : ''').strip() __A =[float(item) for item in user_input.split(''',''')] print(sort(unsorted))
19
0
from math import factorial def UpperCAmelCase_ ( _A , _A ): '''simple docstring''' if n < k or k < 0: raise ValueError('''Please enter positive integers for n and k where n >= k''' ) return factorial(_A ) // (factorial(_A ) * factorial(n - k )) if __name__ == "__main__": print( '''The number of five-card hands possible from a standard''', F"fifty-two card deck is: {combinations(52, 5)}\n", ) print( '''If a class of 40 students must be arranged into groups of''', F"4 for group projects, there are {combinations(40, 4)} ways", '''to arrange them.\n''', ) print( '''If 10 teams are competing in a Formula One race, there''', F"are {combinations(10, 3)} ways that first, second and", '''third place can be awarded.''', )
358
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 UpperCAmelCase_ ( _A , _A ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = args.log_outputs SCREAMING_SNAKE_CASE__ = '''_'''.join(args.dataset.split('''/''' ) + [args.config, args.split] ) # load metric SCREAMING_SNAKE_CASE__ = load_metric('''wer''' ) SCREAMING_SNAKE_CASE__ = load_metric('''cer''' ) # compute metrics SCREAMING_SNAKE_CASE__ = wer.compute(references=result['''target'''] , predictions=result['''prediction'''] ) SCREAMING_SNAKE_CASE__ = cer.compute(references=result['''target'''] , predictions=result['''prediction'''] ) # print & log results SCREAMING_SNAKE_CASE__ = F'''WER: {wer_result}\nCER: {cer_result}''' print(_A ) with open(F'''{dataset_id}_eval_results.txt''' , '''w''' ) as f: f.write(_A ) # log all results in text file. Possibly interesting for analysis if log_outputs is not None: SCREAMING_SNAKE_CASE__ = F'''log_{dataset_id}_predictions.txt''' SCREAMING_SNAKE_CASE__ = F'''log_{dataset_id}_targets.txt''' with open(_A , '''w''' ) as p, open(_A , '''w''' ) as t: # mapping function to write output def write_to_file(_A , _A ): p.write(F'''{i}''' + '''\n''' ) p.write(batch['''prediction'''] + '''\n''' ) t.write(F'''{i}''' + '''\n''' ) t.write(batch['''target'''] + '''\n''' ) result.map(_A , with_indices=_A ) def UpperCAmelCase_ ( _A ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = '''[,?.!\-\;\:"“%‘”�—’…–]''' # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training SCREAMING_SNAKE_CASE__ = re.sub(_A , '''''' , text.lower() ) # In addition, we can normalize the target text, e.g. removing new lines characters etc... # note that order is important here! SCREAMING_SNAKE_CASE__ = ['''\n\n''', '''\n''', ''' ''', ''' '''] for t in token_sequences_to_ignore: SCREAMING_SNAKE_CASE__ = ''' '''.join(text.split(_A ) ) return text def UpperCAmelCase_ ( _A ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = load_dataset(args.dataset , args.config , split=args.split , use_auth_token=_A ) # for testing: only process the first two examples as a test # dataset = dataset.select(range(10)) # load processor SCREAMING_SNAKE_CASE__ = AutoFeatureExtractor.from_pretrained(args.model_id ) SCREAMING_SNAKE_CASE__ = feature_extractor.sampling_rate # resample audio SCREAMING_SNAKE_CASE__ = dataset.cast_column('''audio''' , Audio(sampling_rate=_A ) ) # load eval pipeline if args.device is None: SCREAMING_SNAKE_CASE__ = 0 if torch.cuda.is_available() else -1 SCREAMING_SNAKE_CASE__ = pipeline('''automatic-speech-recognition''' , model=args.model_id , device=args.device ) # map function to decode audio def map_to_pred(_A ): SCREAMING_SNAKE_CASE__ = asr( batch['''audio''']['''array'''] , chunk_length_s=args.chunk_length_s , stride_length_s=args.stride_length_s ) SCREAMING_SNAKE_CASE__ = prediction['''text'''] SCREAMING_SNAKE_CASE__ = normalize_text(batch['''sentence'''] ) return batch # run inference on all examples SCREAMING_SNAKE_CASE__ = dataset.map(_A , remove_columns=dataset.column_names ) # compute and log_results # do not change function below log_results(_A , _A ) if __name__ == "__main__": _SCREAMING_SNAKE_CASE : List[str] = 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.''', ) _SCREAMING_SNAKE_CASE : List[Any] = parser.parse_args() main(args)
218
0
def __A ( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) -> Dict: def count_of_possible_combinations(__lowerCamelCase ) -> int: if target < 0: return 0 if target == 0: return 1 return sum(count_of_possible_combinations(target - item ) for item in array ) return count_of_possible_combinations(UpperCamelCase_ ) def __A ( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) -> int: def count_of_possible_combinations_with_dp_array( __lowerCamelCase , __lowerCamelCase ) -> int: if target < 0: return 0 if target == 0: return 1 if dp_array[target] != -1: return dp_array[target] a = sum( count_of_possible_combinations_with_dp_array(target - item , UpperCamelCase_ ) for item in array ) a = answer return answer a = [-1] * (target + 1) return count_of_possible_combinations_with_dp_array(UpperCamelCase_ , UpperCamelCase_ ) def __A ( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) -> Optional[int]: a = [0] * (target + 1) a = 1 for i in range(1 , target + 1 ): for j in range(UpperCamelCase_ ): if i - array[j] >= 0: dp_array[i] += dp_array[i - array[j]] return dp_array[target] if __name__ == "__main__": import doctest doctest.testmod() __UpperCamelCase : Tuple = 3 __UpperCamelCase : Optional[Any] = 5 __UpperCamelCase : Optional[int] = [1, 2, 5] print(combination_sum_iv(n, array, target))
228
"""simple docstring""" from math import isqrt, loga def _lowerCAmelCase ( UpperCamelCase_ ): __SCREAMING_SNAKE_CASE = [True] * max_number for i in range(2 , isqrt(max_number - 1 ) + 1 ): if is_prime[i]: for j in range(i**2 , UpperCamelCase_ , UpperCamelCase_ ): __SCREAMING_SNAKE_CASE = False return [i for i in range(2 , UpperCamelCase_ ) if is_prime[i]] def _lowerCAmelCase ( UpperCamelCase_ = 80_0800 , UpperCamelCase_ = 80_0800 ): __SCREAMING_SNAKE_CASE = degree * loga(UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = int(UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = calculate_prime_numbers(UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = len(UpperCamelCase_ ) - 1 while left < right: while ( prime_numbers[right] * loga(prime_numbers[left] ) + prime_numbers[left] * loga(prime_numbers[right] ) > upper_bound ): right -= 1 hybrid_integers_count += right - left left += 1 return hybrid_integers_count if __name__ == "__main__": print(F"""{solution() = }""")
100
0
"""simple docstring""" import unittest import numpy as np import torch from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class _lowerCamelCase ( unittest.TestCase ): @property def snake_case_ (self ) -> Optional[int]: torch.manual_seed(0 ) UpperCamelCase = UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=("DownBlock2D", "AttnDownBlock2D") , up_block_types=("AttnUpBlock2D", "UpBlock2D") , ) return model def snake_case_ (self ) -> Any: UpperCamelCase = self.dummy_uncond_unet UpperCamelCase = KarrasVeScheduler() UpperCamelCase = KarrasVePipeline(unet=__a , scheduler=__a ) pipe.to(__a ) pipe.set_progress_bar_config(disable=__a ) UpperCamelCase = torch.manual_seed(0 ) UpperCamelCase = pipe(num_inference_steps=2 , generator=__a , output_type="numpy" ).images UpperCamelCase = torch.manual_seed(0 ) UpperCamelCase = pipe(num_inference_steps=2 , generator=__a , output_type="numpy" , return_dict=__a )[0] UpperCamelCase = image[0, -3:, -3:, -1] UpperCamelCase = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) UpperCamelCase = np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch class _lowerCamelCase ( unittest.TestCase ): def snake_case_ (self ) -> Union[str, Any]: UpperCamelCase = "google/ncsnpp-celebahq-256" UpperCamelCase = UNetaDModel.from_pretrained(__a ) UpperCamelCase = KarrasVeScheduler() UpperCamelCase = KarrasVePipeline(unet=__a , scheduler=__a ) pipe.to(__a ) pipe.set_progress_bar_config(disable=__a ) UpperCamelCase = torch.manual_seed(0 ) UpperCamelCase = pipe(num_inference_steps=20 , generator=__a , output_type="numpy" ).images UpperCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 2_56, 2_56, 3) UpperCamelCase = np.array([0.578, 0.5811, 0.5924, 0.5809, 0.587, 0.5886, 0.5861, 0.5802, 0.586] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
244
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { '''uclanlp/visualbert-vqa''': '''https://huggingface.co/uclanlp/visualbert-vqa/resolve/main/config.json''', '''uclanlp/visualbert-vqa-pre''': '''https://huggingface.co/uclanlp/visualbert-vqa-pre/resolve/main/config.json''', '''uclanlp/visualbert-vqa-coco-pre''': ( '''https://huggingface.co/uclanlp/visualbert-vqa-coco-pre/resolve/main/config.json''' ), '''uclanlp/visualbert-vcr''': '''https://huggingface.co/uclanlp/visualbert-vcr/resolve/main/config.json''', '''uclanlp/visualbert-vcr-pre''': '''https://huggingface.co/uclanlp/visualbert-vcr-pre/resolve/main/config.json''', '''uclanlp/visualbert-vcr-coco-pre''': ( '''https://huggingface.co/uclanlp/visualbert-vcr-coco-pre/resolve/main/config.json''' ), '''uclanlp/visualbert-nlvr2''': '''https://huggingface.co/uclanlp/visualbert-nlvr2/resolve/main/config.json''', '''uclanlp/visualbert-nlvr2-pre''': '''https://huggingface.co/uclanlp/visualbert-nlvr2-pre/resolve/main/config.json''', '''uclanlp/visualbert-nlvr2-coco-pre''': ( '''https://huggingface.co/uclanlp/visualbert-nlvr2-coco-pre/resolve/main/config.json''' ) # See all VisualBERT models at https://huggingface.co/models?filter=visual_bert } class _lowerCamelCase ( _lowercase ): UpperCAmelCase_ = "visual_bert" def __init__(self , __a=3_05_22 , __a=7_68 , __a=5_12 , __a=12 , __a=12 , __a=30_72 , __a="gelu" , __a=0.1 , __a=0.1 , __a=5_12 , __a=2 , __a=0.02 , __a=1e-1_2 , __a=False , __a=True , __a=1 , __a=0 , __a=2 , **__a , ) -> int: super().__init__(pad_token_id=__a , bos_token_id=__a , eos_token_id=__a , **__a ) UpperCamelCase = vocab_size UpperCamelCase = max_position_embeddings UpperCamelCase = hidden_size UpperCamelCase = visual_embedding_dim UpperCamelCase = num_hidden_layers UpperCamelCase = num_attention_heads UpperCamelCase = intermediate_size UpperCamelCase = hidden_act UpperCamelCase = hidden_dropout_prob UpperCamelCase = attention_probs_dropout_prob UpperCamelCase = initializer_range UpperCamelCase = type_vocab_size UpperCamelCase = layer_norm_eps UpperCamelCase = bypass_transformer UpperCamelCase = special_visual_initialize
244
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging SCREAMING_SNAKE_CASE_ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE_ = { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/config.json''' ), '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/config.json''' ), '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/config.json''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/config.json''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/config.json''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/config.json''' ), } class lowerCAmelCase_ ( A__ ): '''simple docstring''' _snake_case = '''dpr''' def __init__( self , snake_case_=30_522 , snake_case_=768 , snake_case_=12 , snake_case_=12 , snake_case_=3_072 , snake_case_="gelu" , snake_case_=0.1 , snake_case_=0.1 , snake_case_=512 , snake_case_=2 , snake_case_=0.02 , snake_case_=1e-1_2 , snake_case_=0 , snake_case_="absolute" , snake_case_ = 0 , **snake_case_ , ) -> Tuple: super().__init__(pad_token_id=snake_case_ , **snake_case_ ) __lowerCAmelCase = vocab_size __lowerCAmelCase = hidden_size __lowerCAmelCase = num_hidden_layers __lowerCAmelCase = num_attention_heads __lowerCAmelCase = hidden_act __lowerCAmelCase = intermediate_size __lowerCAmelCase = hidden_dropout_prob __lowerCAmelCase = attention_probs_dropout_prob __lowerCAmelCase = max_position_embeddings __lowerCAmelCase = type_vocab_size __lowerCAmelCase = initializer_range __lowerCAmelCase = layer_norm_eps __lowerCAmelCase = projection_dim __lowerCAmelCase = position_embedding_type
301
"""simple docstring""" def lowercase (_lowerCAmelCase = 100_0000 ): __lowerCAmelCase = 1 __lowerCAmelCase = 1 __lowerCAmelCase = {1: 1} for inputa in range(2 , _lowerCAmelCase ): __lowerCAmelCase = 0 __lowerCAmelCase = inputa while True: if number in counters: counter += counters[number] break if number % 2 == 0: number //= 2 counter += 1 else: __lowerCAmelCase = (3 * number) + 1 counter += 1 if inputa not in counters: __lowerCAmelCase = counter if counter > pre_counter: __lowerCAmelCase = inputa __lowerCAmelCase = counter return largest_number if __name__ == "__main__": print(solution(int(input().strip())))
301
1
import json import os import unittest from transformers import BatchEncoding, MvpTokenizer, MvpTokenizerFast from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, require_torch from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin, filter_roberta_detectors @require_tokenizers class SCREAMING_SNAKE_CASE_ ( snake_case_ , unittest.TestCase ): __magic_name__: str = MvpTokenizer __magic_name__: str = MvpTokenizerFast __magic_name__: str = True __magic_name__: Any = filter_roberta_detectors def UpperCAmelCase_ ( self : Optional[Any] ) -> List[str]: """simple docstring""" super().setUp() snake_case_ : str = [ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', '\u0120', '\u0120l', '\u0120n', '\u0120lo', '\u0120low', 'er', '\u0120lowest', '\u0120newer', '\u0120wider', '<unk>', ] snake_case_ : Optional[int] = dict(zip(_A , range(len(_A ) ) ) ) snake_case_ : Optional[int] = ['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', ''] snake_case_ : Optional[Any] = {'unk_token': '<unk>'} snake_case_ : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) snake_case_ : Tuple = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(_A ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(_A ) ) def UpperCAmelCase_ ( self : List[Any] , **_A : Any ) -> Union[str, Any]: """simple docstring""" kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **_A ) def UpperCAmelCase_ ( self : List[Any] , **_A : List[str] ) -> Any: """simple docstring""" kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **_A ) def UpperCAmelCase_ ( self : List[str] , _A : Optional[int] ) -> Any: """simple docstring""" return "lower newer", "lower newer" @cached_property def UpperCAmelCase_ ( self : List[str] ) -> List[str]: """simple docstring""" return MvpTokenizer.from_pretrained('RUCAIBox/mvp' ) @cached_property def UpperCAmelCase_ ( self : List[Any] ) -> str: """simple docstring""" return MvpTokenizerFast.from_pretrained('RUCAIBox/mvp' ) @require_torch def UpperCAmelCase_ ( self : List[str] ) -> Dict: """simple docstring""" snake_case_ : Any = ['A long paragraph for summarization.', 'Another paragraph for summarization.'] snake_case_ : List[Any] = [0, 250, 251, 17818, 13, 39186, 1938, 4, 2] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: snake_case_ : Optional[int] = tokenizer(_A , max_length=len(_A ) , padding=_A , return_tensors='pt' ) self.assertIsInstance(_A , _A ) self.assertEqual((2, 9) , batch.input_ids.shape ) self.assertEqual((2, 9) , batch.attention_mask.shape ) snake_case_ : List[str] = batch.input_ids.tolist()[0] self.assertListEqual(_A , _A ) # Test that special tokens are reset @require_torch def UpperCAmelCase_ ( self : str ) -> int: """simple docstring""" snake_case_ : Optional[Any] = ['A long paragraph for summarization.', 'Another paragraph for summarization.'] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: snake_case_ : Optional[Any] = tokenizer(_A , padding=_A , return_tensors='pt' ) # check if input_ids are returned and no labels self.assertIn('input_ids' , _A ) self.assertIn('attention_mask' , _A ) self.assertNotIn('labels' , _A ) self.assertNotIn('decoder_attention_mask' , _A ) @require_torch def UpperCAmelCase_ ( self : Optional[int] ) -> Optional[Any]: """simple docstring""" snake_case_ : Optional[int] = [ 'Summary of the text.', 'Another summary.', ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: snake_case_ : str = tokenizer(text_target=_A , max_length=32 , padding='max_length' , return_tensors='pt' ) self.assertEqual(32 , targets['input_ids'].shape[1] ) @require_torch def UpperCAmelCase_ ( self : List[str] ) -> Union[str, Any]: """simple docstring""" for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: snake_case_ : Optional[int] = tokenizer( ['I am a small frog' * 1024, 'I am a small frog'] , padding=_A , truncation=_A , return_tensors='pt' ) self.assertIsInstance(_A , _A ) self.assertEqual(batch.input_ids.shape , (2, 1024) ) @require_torch def UpperCAmelCase_ ( self : List[Any] ) -> List[Any]: """simple docstring""" snake_case_ : List[Any] = ['A long paragraph for summarization.'] snake_case_ : Tuple = [ 'Summary of the text.', ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: snake_case_ : Any = tokenizer(_A , text_target=_A , return_tensors='pt' ) snake_case_ : Union[str, Any] = inputs['input_ids'] snake_case_ : Dict = inputs['labels'] self.assertTrue((input_ids[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((labels[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((input_ids[:, -1] == tokenizer.eos_token_id).all().item() ) self.assertTrue((labels[:, -1] == tokenizer.eos_token_id).all().item() ) def UpperCAmelCase_ ( self : int ) -> Optional[Any]: """simple docstring""" pass def UpperCAmelCase_ ( self : Any ) -> List[str]: """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): snake_case_ : Tuple = self.rust_tokenizer_class.from_pretrained(_A , **_A ) snake_case_ : Tuple = self.tokenizer_class.from_pretrained(_A , **_A ) snake_case_ : Optional[int] = 'A, <mask> AllenNLP sentence.' snake_case_ : Optional[Any] = tokenizer_r.encode_plus(_A , add_special_tokens=_A , return_token_type_ids=_A ) snake_case_ : Dict = tokenizer_p.encode_plus(_A , add_special_tokens=_A , return_token_type_ids=_A ) # token_type_ids should put 0 everywhere self.assertEqual(sum(tokens_r['token_type_ids'] ) , sum(tokens_p['token_type_ids'] ) ) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEqual( sum(tokens_r['attention_mask'] ) / len(tokens_r['attention_mask'] ) , sum(tokens_p['attention_mask'] ) / len(tokens_p['attention_mask'] ) , ) snake_case_ : Any = tokenizer_r.convert_ids_to_tokens(tokens_r['input_ids'] ) snake_case_ : List[str] = tokenizer_p.convert_ids_to_tokens(tokens_p['input_ids'] ) # Rust correctly handles the space before the mask while python doesnt self.assertSequenceEqual(tokens_p['input_ids'] , [0, 250, 6, 50264, 3823, 487, 21992, 3645, 4, 2] ) self.assertSequenceEqual(tokens_r['input_ids'] , [0, 250, 6, 50264, 3823, 487, 21992, 3645, 4, 2] ) self.assertSequenceEqual( _A , ['<s>', 'A', ',', '<mask>', 'ĠAllen', 'N', 'LP', 'Ġsentence', '.', '</s>'] ) self.assertSequenceEqual( _A , ['<s>', 'A', ',', '<mask>', 'ĠAllen', 'N', 'LP', 'Ġsentence', '.', '</s>'] )
88
import torch from torch import nn from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin class SCREAMING_SNAKE_CASE_ ( snake_case_ , snake_case_ ): @register_to_config def __init__( self : Optional[Any] , *, _A : int = 4 , _A : int = 768 , _A : int , _A : Tuple , ) -> Dict: """simple docstring""" super().__init__() snake_case_ : int = nn.Parameter(torch.zeros(_A ) ) # parameters for additional clip time embeddings snake_case_ : Tuple = nn.Linear(_A , _A ) snake_case_ : List[Any] = nn.Linear(_A , _A ) # parameters for encoder hidden states snake_case_ : Union[str, Any] = clip_extra_context_tokens snake_case_ : str = nn.Linear( _A , self.clip_extra_context_tokens * cross_attention_dim ) snake_case_ : Any = nn.Linear(_A , _A ) snake_case_ : Tuple = nn.LayerNorm(_A ) def UpperCAmelCase_ ( self : List[str] , *, _A : Tuple , _A : List[Any] , _A : str , _A : Optional[Any] ) -> List[Any]: """simple docstring""" if do_classifier_free_guidance: # Add the classifier free guidance embeddings to the image embeddings snake_case_ : Optional[int] = image_embeddings.shape[0] snake_case_ : Optional[Any] = self.learned_classifier_free_guidance_embeddings.unsqueeze(0 ) snake_case_ : Optional[Any] = classifier_free_guidance_embeddings.expand( _A , -1 ) snake_case_ : Any = torch.cat([classifier_free_guidance_embeddings, image_embeddings] , dim=0 ) # The image embeddings batch size and the text embeddings batch size are equal assert image_embeddings.shape[0] == prompt_embeds.shape[0] snake_case_ : str = prompt_embeds.shape[0] # "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and # adding CLIP embeddings to the existing timestep embedding, ... snake_case_ : str = self.embedding_proj(_A ) snake_case_ : Dict = self.clip_image_embeddings_project_to_time_embeddings(_A ) snake_case_ : Tuple = time_projected_image_embeddings + time_projected_prompt_embeds # ... and by projecting CLIP embeddings into four # extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder" snake_case_ : List[str] = self.clip_extra_context_tokens_proj(_A ) snake_case_ : Optional[Any] = clip_extra_context_tokens.reshape(_A , -1 , self.clip_extra_context_tokens ) snake_case_ : int = clip_extra_context_tokens.permute(0 , 2 , 1 ) snake_case_ : Optional[int] = self.encoder_hidden_states_proj(_A ) snake_case_ : Any = self.text_encoder_hidden_states_norm(_A ) snake_case_ : Dict = torch.cat([clip_extra_context_tokens, text_encoder_hidden_states] , dim=1 ) return text_encoder_hidden_states, additive_clip_time_embeddings
88
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __UpperCAmelCase = { '''configuration_blenderbot''': [ '''BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BlenderbotConfig''', '''BlenderbotOnnxConfig''', ], '''tokenization_blenderbot''': ['''BlenderbotTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase = ['''BlenderbotTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase = [ '''BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BlenderbotForCausalLM''', '''BlenderbotForConditionalGeneration''', '''BlenderbotModel''', '''BlenderbotPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase = [ '''TFBlenderbotForConditionalGeneration''', '''TFBlenderbotModel''', '''TFBlenderbotPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase = [ '''FlaxBlenderbotForConditionalGeneration''', '''FlaxBlenderbotModel''', '''FlaxBlenderbotPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_blenderbot import ( BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotConfig, BlenderbotOnnxConfig, ) from .tokenization_blenderbot import BlenderbotTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_blenderbot_fast import BlenderbotTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blenderbot import ( BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST, BlenderbotForCausalLM, BlenderbotForConditionalGeneration, BlenderbotModel, BlenderbotPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blenderbot import ( TFBlenderbotForConditionalGeneration, TFBlenderbotModel, TFBlenderbotPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, FlaxBlenderbotPreTrainedModel, ) else: import sys __UpperCAmelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
119
import argparse import json from tqdm import tqdm def UpperCamelCase ( ) -> Optional[int]: UpperCamelCase : List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--src_path' , type=snake_case__ , default='biencoder-nq-dev.json' , help='Path to raw DPR training data' , ) parser.add_argument( '--evaluation_set' , type=snake_case__ , help='where to store parsed evaluation_set file' , ) parser.add_argument( '--gold_data_path' , type=snake_case__ , help='where to store parsed gold_data_path file' , ) UpperCamelCase : int = parser.parse_args() with open(args.src_path , 'r' ) as src_file, open(args.evaluation_set , 'w' ) as eval_file, open( args.gold_data_path , 'w' ) as gold_file: UpperCamelCase : int = json.load(snake_case__ ) for dpr_record in tqdm(snake_case__ ): UpperCamelCase : Union[str, Any] = dpr_record['question'] UpperCamelCase : Dict = [context['title'] for context in dpr_record['positive_ctxs']] eval_file.write(question + '\n' ) gold_file.write('\t'.join(snake_case__ ) + '\n' ) if __name__ == "__main__": main()
119
1
from ...configuration_utils import PretrainedConfig from ...utils import logging snake_case : Dict = logging.get_logger(__name__) snake_case : Optional[int] = { "naver-clova-ix/donut-base": "https://huggingface.co/naver-clova-ix/donut-base/resolve/main/config.json", # See all Donut models at https://huggingface.co/models?filter=donut-swin } class _snake_case ( snake_case ): UpperCamelCase__ = 'donut-swin' UpperCamelCase__ = { 'num_attention_heads': 'num_heads', 'num_hidden_layers': 'num_layers', } def __init__( self , _a=224 , _a=4 , _a=3 , _a=96 , _a=[2, 2, 6, 2] , _a=[3, 6, 12, 24] , _a=7 , _a=4.0 , _a=True , _a=0.0 , _a=0.0 , _a=0.1 , _a="gelu" , _a=False , _a=0.02 , _a=1e-5 , **_a , ): super().__init__(**_a ) __magic_name__ : Optional[int] = image_size __magic_name__ : Any = patch_size __magic_name__ : Tuple = num_channels __magic_name__ : Dict = embed_dim __magic_name__ : Dict = depths __magic_name__ : int = len(_a ) __magic_name__ : str = num_heads __magic_name__ : Tuple = window_size __magic_name__ : Dict = mlp_ratio __magic_name__ : List[str] = qkv_bias __magic_name__ : Any = hidden_dropout_prob __magic_name__ : str = attention_probs_dropout_prob __magic_name__ : Union[str, Any] = drop_path_rate __magic_name__ : List[Any] = hidden_act __magic_name__ : List[Any] = use_absolute_embeddings __magic_name__ : Union[str, Any] = layer_norm_eps __magic_name__ : Optional[Any] = initializer_range # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model __magic_name__ : Tuple = int(embed_dim * 2 ** (len(_a ) - 1) )
41
import numpy class _snake_case : def __init__( self , _a , _a ): __magic_name__ : Optional[Any] = input_array # Random initial weights are assigned where first argument is the # number of nodes in previous layer and second argument is the # number of nodes in the next layer. # Random initial weights are assigned. # self.input_array.shape[1] is used to represent number of nodes in input layer. # First hidden layer consists of 4 nodes. __magic_name__ : Any = numpy.random.rand( self.input_array.shape[1] , 4 ) # Random initial values for the first hidden layer. # First hidden layer has 4 nodes. # Second hidden layer has 3 nodes. __magic_name__ : List[str] = numpy.random.rand( 4 , 3 ) # Random initial values for the second hidden layer. # Second hidden layer has 3 nodes. # Output layer has 1 node. __magic_name__ : Any = numpy.random.rand(3 , 1 ) # Real output values provided. __magic_name__ : Dict = output_array # Predicted output values by the neural network. # Predicted_output array initially consists of zeroes. __magic_name__ : Tuple = numpy.zeros(output_array.shape ) def SCREAMING_SNAKE_CASE ( self ): __magic_name__ : List[str] = sigmoid( numpy.dot(self.input_array , self.input_layer_and_first_hidden_layer_weights ) ) # layer_between_first_hidden_layer_and_second_hidden_layer is the layer # connecting the first hidden set of nodes with the second hidden set of nodes. __magic_name__ : int = sigmoid( numpy.dot( self.layer_between_input_and_first_hidden_layer , self.first_hidden_layer_and_second_hidden_layer_weights , ) ) # layer_between_second_hidden_layer_and_output is the layer connecting # second hidden layer with the output node. __magic_name__ : Tuple = sigmoid( numpy.dot( self.layer_between_first_hidden_layer_and_second_hidden_layer , self.second_hidden_layer_and_output_layer_weights , ) ) return self.layer_between_second_hidden_layer_and_output def SCREAMING_SNAKE_CASE ( self ): __magic_name__ : Optional[int] = numpy.dot( self.layer_between_first_hidden_layer_and_second_hidden_layer.T , 2 * (self.output_array - self.predicted_output) * sigmoid_derivative(self.predicted_output ) , ) __magic_name__ : Optional[int] = numpy.dot( self.layer_between_input_and_first_hidden_layer.T , numpy.dot( 2 * (self.output_array - self.predicted_output) * sigmoid_derivative(self.predicted_output ) , self.second_hidden_layer_and_output_layer_weights.T , ) * sigmoid_derivative( self.layer_between_first_hidden_layer_and_second_hidden_layer ) , ) __magic_name__ : int = numpy.dot( self.input_array.T , numpy.dot( numpy.dot( 2 * (self.output_array - self.predicted_output) * sigmoid_derivative(self.predicted_output ) , self.second_hidden_layer_and_output_layer_weights.T , ) * sigmoid_derivative( self.layer_between_first_hidden_layer_and_second_hidden_layer ) , self.first_hidden_layer_and_second_hidden_layer_weights.T , ) * sigmoid_derivative(self.layer_between_input_and_first_hidden_layer ) , ) self.input_layer_and_first_hidden_layer_weights += ( updated_input_layer_and_first_hidden_layer_weights ) self.first_hidden_layer_and_second_hidden_layer_weights += ( updated_first_hidden_layer_and_second_hidden_layer_weights ) self.second_hidden_layer_and_output_layer_weights += ( updated_second_hidden_layer_and_output_layer_weights ) def SCREAMING_SNAKE_CASE ( self , _a , _a , _a ): for iteration in range(1 , iterations + 1 ): __magic_name__ : Any = self.feedforward() self.back_propagation() if give_loss: __magic_name__ : int = numpy.mean(numpy.square(output - self.feedforward() ) ) print(f'''Iteration {iteration} Loss: {loss}''' ) def SCREAMING_SNAKE_CASE ( self , _a ): __magic_name__ : str = input_arr __magic_name__ : Optional[Any] = sigmoid( numpy.dot(self.array , self.input_layer_and_first_hidden_layer_weights ) ) __magic_name__ : Union[str, Any] = sigmoid( numpy.dot( self.layer_between_input_and_first_hidden_layer , self.first_hidden_layer_and_second_hidden_layer_weights , ) ) __magic_name__ : Optional[Any] = sigmoid( numpy.dot( self.layer_between_first_hidden_layer_and_second_hidden_layer , self.second_hidden_layer_and_output_layer_weights , ) ) return int(self.layer_between_second_hidden_layer_and_output > 0.6 ) def lowerCAmelCase_ ( _snake_case : numpy.ndarray ) -> numpy.ndarray: '''simple docstring''' return 1 / (1 + numpy.exp(-value )) def lowerCAmelCase_ ( _snake_case : numpy.ndarray ) -> numpy.ndarray: '''simple docstring''' return (value) * (1 - (value)) def lowerCAmelCase_ ( ) -> int: '''simple docstring''' __magic_name__ : Tuple = numpy.array( ( [0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1], ) , dtype=numpy.floataa , ) # True output values for the given input values. __magic_name__ : List[str] = numpy.array(([0], [1], [1], [0], [1], [0], [0], [1]) , dtype=numpy.floataa ) # Calling neural network class. __magic_name__ : List[Any] = TwoHiddenLayerNeuralNetwork( input_array=_snake_case , output_array=_snake_case ) # Calling training function. # Set give_loss to True if you want to see loss in every iteration. neural_network.train(output=_snake_case , iterations=10 , give_loss=_snake_case ) return neural_network.predict(numpy.array(([1, 1, 1]) , dtype=numpy.floataa ) ) if __name__ == "__main__": example()
41
1
'''simple docstring''' import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case : int = logging.get_logger(__name__) __snake_case : Dict = { 'BridgeTower/bridgetower-base': 'https://huggingface.co/BridgeTower/bridgetower-base/blob/main/config.json', 'BridgeTower/bridgetower-base-itm-mlm': ( 'https://huggingface.co/BridgeTower/bridgetower-base-itm-mlm/blob/main/config.json' ), } class lowerCamelCase ( _a ): '''simple docstring''' __snake_case = """bridgetower_vision_model""" def __init__( self : str , lowerCAmelCase_ : Tuple=7_68 , lowerCAmelCase_ : str=12 , lowerCAmelCase_ : Dict=3 , lowerCAmelCase_ : Tuple=16 , lowerCAmelCase_ : int=2_88 , lowerCAmelCase_ : Optional[Any]=1 , lowerCAmelCase_ : str=1e-05 , lowerCAmelCase_ : Dict=False , lowerCAmelCase_ : Dict=True , lowerCAmelCase_ : Any=False , **lowerCAmelCase_ : int , ) -> Optional[Any]: '''simple docstring''' super().__init__(**snake_case_ ) A__ : Optional[int] =hidden_size A__ : Optional[Any] =num_hidden_layers A__ : Dict =num_channels A__ : Union[str, Any] =patch_size A__ : Tuple =image_size A__ : Optional[Any] =initializer_factor A__ : str =layer_norm_eps A__ : List[str] =stop_gradient A__ : Dict =share_layernorm A__ : str =remove_last_layer @classmethod def lowercase__ ( cls : Optional[Any] , lowerCAmelCase_ : List[Any] , **lowerCAmelCase_ : Tuple ) -> Dict: '''simple docstring''' A__ : List[str] =cls.get_config_dict(snake_case_ , **snake_case_ ) if config_dict.get("""model_type""" ) == "bridgetower": A__ : Tuple =config_dict["""text_config"""] if "model_type" in config_dict and hasattr(cls , """model_type""" ) and config_dict["model_type"] != cls.model_type: logger.warning( f"You are using a model of type {config_dict['model_type']} to instantiate a model of type " f"{cls.model_type}. This is not supported for all configurations of models and can yield errors." ) return cls.from_dict(snake_case_ , **snake_case_ ) class lowerCamelCase ( _a ): '''simple docstring''' __snake_case = """bridgetower_text_model""" def __init__( self : Optional[Any] , lowerCAmelCase_ : Dict=5_02_65 , lowerCAmelCase_ : str=7_68 , lowerCAmelCase_ : Any=12 , lowerCAmelCase_ : Union[str, Any]=12 , lowerCAmelCase_ : List[str]=1 , lowerCAmelCase_ : str=30_72 , lowerCAmelCase_ : Optional[Any]="gelu" , lowerCAmelCase_ : Union[str, Any]=0.1 , lowerCAmelCase_ : Optional[Any]=0.1 , lowerCAmelCase_ : Dict=5_14 , lowerCAmelCase_ : Dict=1 , lowerCAmelCase_ : str=1e-05 , lowerCAmelCase_ : Any=1 , lowerCAmelCase_ : List[Any]=0 , lowerCAmelCase_ : Tuple=2 , lowerCAmelCase_ : Any="absolute" , lowerCAmelCase_ : Optional[int]=True , **lowerCAmelCase_ : Any , ) -> Tuple: '''simple docstring''' super().__init__(**snake_case_ ) A__ : Any =vocab_size A__ : int =hidden_size A__ : str =num_hidden_layers A__ : str =num_attention_heads A__ : Optional[int] =hidden_act A__ : Optional[Any] =initializer_factor A__ : List[str] =intermediate_size A__ : Any =hidden_dropout_prob A__ : List[Any] =attention_probs_dropout_prob A__ : Dict =max_position_embeddings A__ : Tuple =type_vocab_size A__ : Optional[Any] =layer_norm_eps A__ : Any =position_embedding_type A__ : Any =use_cache A__ : int =pad_token_id A__ : str =bos_token_id A__ : Union[str, Any] =eos_token_id @classmethod def lowercase__ ( cls : List[Any] , lowerCAmelCase_ : Optional[Any] , **lowerCAmelCase_ : str ) -> Optional[int]: '''simple docstring''' A__ : Union[str, Any] =cls.get_config_dict(snake_case_ , **snake_case_ ) if config_dict.get("""model_type""" ) == "bridgetower": A__ : Dict =config_dict["""text_config"""] if "model_type" in config_dict and hasattr(cls , """model_type""" ) and config_dict["model_type"] != cls.model_type: logger.warning( f"You are using a model of type {config_dict['model_type']} to instantiate a model of type " f"{cls.model_type}. This is not supported for all configurations of models and can yield errors." ) return cls.from_dict(snake_case_ , **snake_case_ ) class lowerCamelCase ( _a ): '''simple docstring''' __snake_case = """bridgetower""" def __init__( self : Tuple , lowerCAmelCase_ : List[Any]=True , lowerCAmelCase_ : Tuple="gelu" , lowerCAmelCase_ : Optional[int]=7_68 , lowerCAmelCase_ : Optional[int]=1 , lowerCAmelCase_ : List[str]=1e-05 , lowerCAmelCase_ : Optional[int]=False , lowerCAmelCase_ : int="add" , lowerCAmelCase_ : List[Any]=12 , lowerCAmelCase_ : Optional[int]=6 , lowerCAmelCase_ : Tuple=False , lowerCAmelCase_ : Any=False , lowerCAmelCase_ : Tuple=None , lowerCAmelCase_ : Union[str, Any]=None , **lowerCAmelCase_ : List[str] , ) -> Optional[Any]: '''simple docstring''' # TODO: remove this once the Hub files are updated. A__ : List[str] =kwargs.pop("""text_config_dict""" , snake_case_ ) A__ : Any =kwargs.pop("""vision_config_dict""" , snake_case_ ) super().__init__(**snake_case_ ) A__ : str =share_cross_modal_transformer_layers A__ : List[Any] =hidden_act A__ : Union[str, Any] =hidden_size A__ : Dict =initializer_factor A__ : Optional[Any] =layer_norm_eps A__ : Any =share_link_tower_layers A__ : List[str] =link_tower_type A__ : int =num_attention_heads A__ : Any =num_hidden_layers A__ : List[str] =tie_word_embeddings A__ : List[Any] =init_layernorm_from_vision_encoder if text_config is None: A__ : Any ={} logger.info("""`text_config` is `None`. Initializing the `BridgeTowerTextConfig` with default values.""" ) if vision_config is None: A__ : Optional[int] ={} logger.info("""`vision_config` is `None`. Initializing the `BridgeTowerVisionConfig` with default values.""" ) A__ : str =BridgeTowerTextConfig(**snake_case_ ) A__ : List[str] =BridgeTowerVisionConfig(**snake_case_ ) @classmethod def lowercase__ ( cls : Union[str, Any] , lowerCAmelCase_ : Union[str, Any] , lowerCAmelCase_ : Union[str, Any] , **lowerCAmelCase_ : Dict ) -> int: '''simple docstring''' return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **snake_case_ ) def lowercase__ ( self : str ) -> int: '''simple docstring''' A__ : Tuple =copy.deepcopy(self.__dict__ ) A__ : Union[str, Any] =self.text_config.to_dict() A__ : int =self.vision_config.to_dict() A__ : Union[str, Any] =self.__class__.model_type return output
134
'''simple docstring''' import argparse import os import re UpperCamelCase_ = """src/diffusers""" # Pattern that looks at the indentation in a line. UpperCamelCase_ = re.compile(r"""^(\s*)\S""") # Pattern that matches `"key":" and puts `key` in group 0. UpperCamelCase_ = re.compile(r"""^\s*\"([^\"]+)\":""") # Pattern that matches `_import_structure["key"]` and puts `key` in group 0. UpperCamelCase_ = re.compile(r"""^\s*_import_structure\[\"([^\"]+)\"\]""") # Pattern that matches `"key",` and puts `key` in group 0. UpperCamelCase_ = re.compile(r"""^\s*\"([^\"]+)\",\s*$""") # Pattern that matches any `[stuff]` and puts `stuff` in group 0. UpperCamelCase_ = re.compile(r"""\[([^\]]+)\]""") def _UpperCAmelCase ( _lowerCamelCase : List[Any] ) -> str: _lowerCAmelCase : Dict = _re_indent.search(_lowerCamelCase ) return "" if search is None else search.groups()[0] def _UpperCAmelCase ( _lowerCamelCase : Optional[Any] , _lowerCamelCase : List[str]="" , _lowerCamelCase : str=None , _lowerCamelCase : List[Any]=None ) -> str: _lowerCAmelCase : Union[str, Any] = 0 _lowerCAmelCase : Tuple = code.split("""\n""" ) if start_prompt is not None: while not lines[index].startswith(_lowerCamelCase ): index += 1 _lowerCAmelCase : List[Any] = ["""\n""".join(lines[:index] )] else: _lowerCAmelCase : List[str] = [] # We split into blocks until we get to the `end_prompt` (or the end of the block). _lowerCAmelCase : Union[str, Any] = [lines[index]] index += 1 while index < len(_lowerCamelCase ) and (end_prompt is None or not lines[index].startswith(_lowerCamelCase )): if len(lines[index] ) > 0 and get_indent(lines[index] ) == indent_level: if len(_lowerCamelCase ) > 0 and get_indent(current_block[-1] ).startswith(indent_level + """ """ ): current_block.append(lines[index] ) blocks.append("""\n""".join(_lowerCamelCase ) ) if index < len(_lowerCamelCase ) - 1: _lowerCAmelCase : Union[str, Any] = [lines[index + 1]] index += 1 else: _lowerCAmelCase : Dict = [] else: blocks.append("""\n""".join(_lowerCamelCase ) ) _lowerCAmelCase : Tuple = [lines[index]] else: current_block.append(lines[index] ) index += 1 # Adds current block if it's nonempty. if len(_lowerCamelCase ) > 0: blocks.append("""\n""".join(_lowerCamelCase ) ) # Add final block after end_prompt if provided. if end_prompt is not None and index < len(_lowerCamelCase ): blocks.append("""\n""".join(lines[index:] ) ) return blocks def _UpperCAmelCase ( _lowerCamelCase : Optional[Any] ) -> Any: def _inner(_lowerCamelCase : Any ): return key(_lowerCamelCase ).lower().replace("""_""" , """""" ) return _inner def _UpperCAmelCase ( _lowerCamelCase : Union[str, Any] , _lowerCamelCase : Tuple=None ) -> Union[str, Any]: # If no key is provided, we use a noop. def noop(_lowerCamelCase : List[Any] ): return x if key is None: _lowerCAmelCase : Union[str, Any] = noop # Constants are all uppercase, they go first. _lowerCAmelCase : Any = [obj for obj in objects if key(_lowerCamelCase ).isupper()] # Classes are not all uppercase but start with a capital, they go second. _lowerCAmelCase : Union[str, Any] = [obj for obj in objects if key(_lowerCamelCase )[0].isupper() and not key(_lowerCamelCase ).isupper()] # Functions begin with a lowercase, they go last. _lowerCAmelCase : Optional[Any] = [obj for obj in objects if not key(_lowerCamelCase )[0].isupper()] _lowerCAmelCase : List[str] = ignore_underscore(_lowerCamelCase ) return sorted(_lowerCamelCase , key=_lowerCamelCase ) + sorted(_lowerCamelCase , key=_lowerCamelCase ) + sorted(_lowerCamelCase , key=_lowerCamelCase ) def _UpperCAmelCase ( _lowerCamelCase : str ) -> str: # This inner function sort imports between [ ]. def _replace(_lowerCamelCase : Union[str, Any] ): _lowerCAmelCase : Optional[Any] = match.groups()[0] if "," not in imports: return f'[{imports}]' _lowerCAmelCase : List[str] = [part.strip().replace("""\"""" , """""" ) for part in imports.split(""",""" )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: _lowerCAmelCase : int = keys[:-1] return "[" + ", ".join([f'"{k}"' for k in sort_objects(_lowerCamelCase )] ) + "]" _lowerCAmelCase : Optional[int] = import_statement.split("""\n""" ) if len(_lowerCamelCase ) > 3: # Here we have to sort internal imports that are on several lines (one per name): # key: [ # "object1", # "object2", # ... # ] # We may have to ignore one or two lines on each side. _lowerCAmelCase : Dict = 2 if lines[1].strip() == """[""" else 1 _lowerCAmelCase : Tuple = [(i, _re_strip_line.search(_lowerCamelCase ).groups()[0]) for i, line in enumerate(lines[idx:-idx] )] _lowerCAmelCase : Tuple = sort_objects(_lowerCamelCase , key=lambda _lowerCamelCase : x[1] ) _lowerCAmelCase : Optional[Any] = [lines[x[0] + idx] for x in sorted_indices] return "\n".join(lines[:idx] + sorted_lines + lines[-idx:] ) elif len(_lowerCamelCase ) == 3: # Here we have to sort internal imports that are on one separate line: # key: [ # "object1", "object2", ... # ] if _re_bracket_content.search(lines[1] ) is not None: _lowerCAmelCase : str = _re_bracket_content.sub(_replace , lines[1] ) else: _lowerCAmelCase : Tuple = [part.strip().replace("""\"""" , """""" ) for part in lines[1].split(""",""" )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: _lowerCAmelCase : Dict = keys[:-1] _lowerCAmelCase : Optional[Any] = get_indent(lines[1] ) + """, """.join([f'"{k}"' for k in sort_objects(_lowerCamelCase )] ) return "\n".join(_lowerCamelCase ) else: # Finally we have to deal with imports fitting on one line _lowerCAmelCase : Dict = _re_bracket_content.sub(_replace , _lowerCamelCase ) return import_statement def _UpperCAmelCase ( _lowerCamelCase : int , _lowerCamelCase : Union[str, Any]=True ) -> List[str]: with open(_lowerCamelCase , """r""" ) as f: _lowerCAmelCase : Optional[Any] = f.read() if "_import_structure" not in code: return # Blocks of indent level 0 _lowerCAmelCase : Optional[Any] = split_code_in_indented_blocks( _lowerCamelCase , start_prompt="""_import_structure = {""" , end_prompt="""if TYPE_CHECKING:""" ) # We ignore block 0 (everything until start_prompt) and the last block (everything after end_prompt). for block_idx in range(1 , len(_lowerCamelCase ) - 1 ): # Check if the block contains some `_import_structure`s thingy to sort. _lowerCAmelCase : List[str] = main_blocks[block_idx] _lowerCAmelCase : int = block.split("""\n""" ) # Get to the start of the imports. _lowerCAmelCase : Any = 0 while line_idx < len(_lowerCamelCase ) and "_import_structure" not in block_lines[line_idx]: # Skip dummy import blocks if "import dummy" in block_lines[line_idx]: _lowerCAmelCase : Optional[int] = len(_lowerCamelCase ) else: line_idx += 1 if line_idx >= len(_lowerCamelCase ): continue # Ignore beginning and last line: they don't contain anything. _lowerCAmelCase : Any = """\n""".join(block_lines[line_idx:-1] ) _lowerCAmelCase : Tuple = get_indent(block_lines[1] ) # Slit the internal block into blocks of indent level 1. _lowerCAmelCase : Optional[Any] = split_code_in_indented_blocks(_lowerCamelCase , indent_level=_lowerCamelCase ) # We have two categories of import key: list or _import_structure[key].append/extend _lowerCAmelCase : List[Any] = _re_direct_key if """_import_structure""" in block_lines[0] else _re_indirect_key # Grab the keys, but there is a trap: some lines are empty or just comments. _lowerCAmelCase : Tuple = [(pattern.search(_lowerCamelCase ).groups()[0] if pattern.search(_lowerCamelCase ) is not None else None) for b in internal_blocks] # We only sort the lines with a key. _lowerCAmelCase : List[str] = [(i, key) for i, key in enumerate(_lowerCamelCase ) if key is not None] _lowerCAmelCase : List[str] = [x[0] for x in sorted(_lowerCamelCase , key=lambda _lowerCamelCase : x[1] )] # We reorder the blocks by leaving empty lines/comments as they were and reorder the rest. _lowerCAmelCase : List[Any] = 0 _lowerCAmelCase : List[str] = [] for i in range(len(_lowerCamelCase ) ): if keys[i] is None: reordered_blocks.append(internal_blocks[i] ) else: _lowerCAmelCase : Any = sort_objects_in_import(internal_blocks[sorted_indices[count]] ) reordered_blocks.append(_lowerCamelCase ) count += 1 # And we put our main block back together with its first and last line. _lowerCAmelCase : str = """\n""".join(block_lines[:line_idx] + reordered_blocks + [block_lines[-1]] ) if code != "\n".join(_lowerCamelCase ): if check_only: return True else: print(f'Overwriting {file}.' ) with open(_lowerCamelCase , """w""" ) as f: f.write("""\n""".join(_lowerCamelCase ) ) def _UpperCAmelCase ( _lowerCamelCase : Optional[Any]=True ) -> Any: _lowerCAmelCase : List[Any] = [] for root, _, files in os.walk(_lowerCamelCase ): if "__init__.py" in files: _lowerCAmelCase : List[Any] = sort_imports(os.path.join(_lowerCamelCase , """__init__.py""" ) , check_only=_lowerCamelCase ) if result: _lowerCAmelCase : str = [os.path.join(_lowerCamelCase , """__init__.py""" )] if len(_lowerCamelCase ) > 0: raise ValueError(f'Would overwrite {len(_lowerCamelCase )} files, run `make style`.' ) if __name__ == "__main__": UpperCamelCase_ = argparse.ArgumentParser() parser.add_argument("""--check_only""", action="""store_true""", help="""Whether to only check or fix style.""") UpperCamelCase_ = parser.parse_args() sort_imports_in_all_inits(check_only=args.check_only)
309
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowercase : Dict = logging.get_logger(__name__) lowercase : Tuple = { 'facebook/convnextv2-tiny-1k-224': 'https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json', } class A ( __snake_case , __snake_case ): __magic_name__ = '''convnextv2''' def __init__( self , SCREAMING_SNAKE_CASE=3 , SCREAMING_SNAKE_CASE=4 , SCREAMING_SNAKE_CASE=4 , SCREAMING_SNAKE_CASE=None , SCREAMING_SNAKE_CASE=None , SCREAMING_SNAKE_CASE="gelu" , SCREAMING_SNAKE_CASE=0.02 , SCREAMING_SNAKE_CASE=1e-12 , SCREAMING_SNAKE_CASE=0.0 , SCREAMING_SNAKE_CASE=224 , SCREAMING_SNAKE_CASE=None , SCREAMING_SNAKE_CASE=None , **SCREAMING_SNAKE_CASE , ) -> Optional[int]: """simple docstring""" super().__init__(**SCREAMING_SNAKE_CASE ) A : Union[str, Any] = num_channels A : List[str] = patch_size A : str = num_stages A : Optional[Any] = [96, 192, 384, 768] if hidden_sizes is None else hidden_sizes A : Dict = [3, 3, 9, 3] if depths is None else depths A : Union[str, Any] = hidden_act A : List[Any] = initializer_range A : Optional[int] = layer_norm_eps A : List[Any] = drop_path_rate A : List[Any] = image_size A : Any = ['''stem'''] + [F'stage{idx}' for idx in range(1 , len(self.depths ) + 1 )] A, A : Optional[Any] = get_aligned_output_features_output_indices( out_features=SCREAMING_SNAKE_CASE , out_indices=SCREAMING_SNAKE_CASE , stage_names=self.stage_names )
311
'''simple docstring''' # Function to print upper half of diamond (pyramid) def lowerCAmelCase_ ( snake_case__ ): '''simple docstring''' for i in range(0 , snake_case__ ): for _ in range(0 , n - i - 1 ): # printing spaces print(''' ''' , end='''''' ) for _ in range(0 , i + 1 ): # printing stars print('''* ''' , end='''''' ) print() def lowerCAmelCase_ ( snake_case__ ): '''simple docstring''' for i in range(snake_case__ , 0 , -1 ): for _ in range(snake_case__ , 0 , -1 ): # printing stars print('''* ''' , end='''''' ) print() for _ in range(n - i + 1 , 0 , -1 ): # printing spaces print(''' ''' , end='''''' ) def lowerCAmelCase_ ( snake_case__ ): '''simple docstring''' if n <= 0: print(''' ... .... nothing printing :(''' ) return floyd(snake_case__ ) # upper half reverse_floyd(snake_case__ ) # lower half if __name__ == "__main__": print(R'| /\ | |- | |- |--| |\ /| |-') print(R'|/ \| |- |_ |_ |__| | \/ | |_') lowercase : List[str] = 1 while K: lowercase : List[Any] = int(input('enter the number and , and see the magic : ')) print() pretty_print(user_number) lowercase : Any = int(input('press 0 to exit... and 1 to continue...')) print('Good Bye...')
311
1
'''simple docstring''' import os import string import sys UpperCAmelCase = 1 << 8 UpperCAmelCase = { '''tab''': ord('''\t'''), '''newline''': ord('''\r'''), '''esc''': 27, '''up''': 65 + ARROW_KEY_FLAG, '''down''': 66 + ARROW_KEY_FLAG, '''right''': 67 + ARROW_KEY_FLAG, '''left''': 68 + ARROW_KEY_FLAG, '''mod_int''': 91, '''undefined''': sys.maxsize, '''interrupt''': 3, '''insert''': 50, '''delete''': 51, '''pg_up''': 53, '''pg_down''': 54, } UpperCAmelCase = KEYMAP['''up'''] UpperCAmelCase = KEYMAP['''left'''] if sys.platform == "win32": UpperCAmelCase = [] UpperCAmelCase = { B'''\xe0H''': KEYMAP['''up'''] - ARROW_KEY_FLAG, B'''\x00H''': KEYMAP['''up'''] - ARROW_KEY_FLAG, B'''\xe0P''': KEYMAP['''down'''] - ARROW_KEY_FLAG, B'''\x00P''': KEYMAP['''down'''] - ARROW_KEY_FLAG, B'''\xe0M''': KEYMAP['''right'''] - ARROW_KEY_FLAG, B'''\x00M''': KEYMAP['''right'''] - ARROW_KEY_FLAG, B'''\xe0K''': KEYMAP['''left'''] - ARROW_KEY_FLAG, B'''\x00K''': KEYMAP['''left'''] - ARROW_KEY_FLAG, } for i in range(10): UpperCAmelCase = ord(str(i)) def __UpperCamelCase ( ): '''simple docstring''' if os.name == "nt": import msvcrt __lowercase ='mbcs' # Flush the keyboard buffer while msvcrt.kbhit(): msvcrt.getch() if len(lowercase__ ) == 0: # Read the keystroke __lowercase =msvcrt.getch() # If it is a prefix char, get second part if ch in (b"\x00", b"\xe0"): __lowercase =ch + msvcrt.getch() # Translate actual Win chars to bullet char types try: __lowercase =chr(WIN_KEYMAP[cha] ) WIN_CH_BUFFER.append(chr(KEYMAP['mod_int'] ) ) WIN_CH_BUFFER.append(lowercase__ ) if ord(lowercase__ ) in ( KEYMAP["insert"] - 1 << 9, KEYMAP["delete"] - 1 << 9, KEYMAP["pg_up"] - 1 << 9, KEYMAP["pg_down"] - 1 << 9, ): WIN_CH_BUFFER.append(chr(1_26 ) ) __lowercase =chr(KEYMAP['esc'] ) except KeyError: __lowercase =cha[1] else: __lowercase =ch.decode(lowercase__ ) else: __lowercase =WIN_CH_BUFFER.pop(0 ) elif os.name == "posix": import termios import tty __lowercase =sys.stdin.fileno() __lowercase =termios.tcgetattr(lowercase__ ) try: tty.setraw(lowercase__ ) __lowercase =sys.stdin.read(1 ) finally: termios.tcsetattr(lowercase__, termios.TCSADRAIN, lowercase__ ) return ch def __UpperCamelCase ( ): '''simple docstring''' __lowercase =get_raw_chars() if ord(lowercase__ ) in [KEYMAP["interrupt"], KEYMAP["newline"]]: return char elif ord(lowercase__ ) == KEYMAP["esc"]: __lowercase =get_raw_chars() if ord(lowercase__ ) == KEYMAP["mod_int"]: __lowercase =get_raw_chars() if ord(lowercase__ ) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(lowercase__ ) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG: return chr(ord(lowercase__ ) + ARROW_KEY_FLAG ) else: return KEYMAP["undefined"] else: return get_raw_chars() else: if char in string.printable: return char else: return KEYMAP["undefined"]
141
'''simple docstring''' UpperCAmelCase = ''' # 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 ''' UpperCAmelCase = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] UpperCAmelCase = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
141
1
"""simple docstring""" def __SCREAMING_SNAKE_CASE ( __UpperCAmelCase ): return number & 1 == 0 if __name__ == "__main__": import doctest doctest.testmod()
336
"""simple docstring""" import os import posixpath import uuid from dataclasses import dataclass from typing import TYPE_CHECKING, Iterable, List, Optional, Tuple, Union import numpy as np import pyarrow as pa import datasets from datasets.arrow_writer import ArrowWriter, ParquetWriter from datasets.config import MAX_SHARD_SIZE from datasets.filesystems import ( is_remote_filesystem, rename, ) from datasets.iterable_dataset import _BaseExamplesIterable from datasets.utils.py_utils import convert_file_size_to_int UpperCAmelCase: str = datasets.utils.logging.get_logger(__name__) if TYPE_CHECKING: import pyspark @dataclass class UpperCamelCase ( datasets.BuilderConfig ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[datasets.Features] = None def __SCREAMING_SNAKE_CASE ( __UpperCAmelCase , __UpperCAmelCase , ): import pyspark def generate_fn(): _lowercase : List[Any] = df.select("""*""" , pyspark.sql.functions.spark_partition_id().alias("""part_id""" ) ) for partition_id in partition_order: _lowercase : Optional[int] = df_with_partition_id.select("""*""" ).where(F"""part_id = {partition_id}""" ).drop("""part_id""" ) _lowercase : int = partition_df.collect() _lowercase : Dict = 0 for row in rows: yield F"""{partition_id}_{row_id}""", row.asDict() row_id += 1 return generate_fn class UpperCamelCase ( _BaseExamplesIterable ): """simple docstring""" def __init__( self ,UpperCAmelCase_ ,UpperCAmelCase_=None ,): _lowercase : Union[str, Any] = df _lowercase : List[str] = partition_order or range(self.df.rdd.getNumPartitions() ) _lowercase : Tuple = _generate_iterable_examples(self.df ,self.partition_order ) def __iter__( self ): yield from self.generate_examples_fn() def lowerCamelCase__ ( self ,UpperCAmelCase_ ): _lowercase : List[str] = list(range(self.df.rdd.getNumPartitions() ) ) generator.shuffle(UpperCAmelCase_ ) return SparkExamplesIterable(self.df ,partition_order=UpperCAmelCase_ ) def lowerCamelCase__ ( self ,UpperCAmelCase_ ,UpperCAmelCase_ ): _lowercase : Union[str, Any] = self.split_shard_indices_by_worker(UpperCAmelCase_ ,UpperCAmelCase_ ) return SparkExamplesIterable(self.df ,partition_order=UpperCAmelCase_ ) @property def lowerCamelCase__ ( self ): return len(self.partition_order ) class UpperCamelCase ( datasets.DatasetBuilder ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Any = SparkConfig def __init__( self ,UpperCAmelCase_ ,UpperCAmelCase_ = None ,UpperCAmelCase_ = None ,**UpperCAmelCase_ ,): import pyspark _lowercase : List[Any] = pyspark.sql.SparkSession.builder.getOrCreate() _lowercase : List[Any] = df _lowercase : int = working_dir super().__init__( cache_dir=UpperCAmelCase_ ,config_name=str(self.df.semanticHash() ) ,**UpperCAmelCase_ ,) def lowerCamelCase__ ( self ): # Returns the path of the created file. def create_cache_and_write_probe(UpperCAmelCase_ ): # makedirs with exist_ok will recursively create the directory. It will not throw an error if directories # already exist. os.makedirs(self._cache_dir ,exist_ok=UpperCAmelCase_ ) _lowercase : Union[str, Any] = os.path.join(self._cache_dir ,"""fs_test""" + uuid.uuida().hex ) # Opening the file in append mode will create a new file unless it already exists, in which case it will not # change the file contents. open(UpperCAmelCase_ ,"""a""" ) return [probe_file] if self._spark.conf.get("""spark.master""" ,"""""" ).startswith("""local""" ): return # If the cluster is multi-node, make sure that the user provided a cache_dir and that it is on an NFS # accessible to the driver. # TODO: Stream batches to the driver using ArrowCollectSerializer instead of throwing an error. if self._cache_dir: _lowercase : List[str] = ( self._spark.sparkContext.parallelize(range(1 ) ,1 ).mapPartitions(UpperCAmelCase_ ).collect() ) if os.path.isfile(probe[0] ): return raise ValueError( """When using Dataset.from_spark on a multi-node cluster, the driver and all workers should be able to access cache_dir""" ) def lowerCamelCase__ ( self ): return datasets.DatasetInfo(features=self.config.features ) def lowerCamelCase__ ( self ,UpperCAmelCase_ ): return [datasets.SplitGenerator(name=datasets.Split.TRAIN )] def lowerCamelCase__ ( self ,UpperCAmelCase_ ): import pyspark def get_arrow_batch_size(UpperCAmelCase_ ): for batch in it: yield pa.RecordBatch.from_pydict({"""batch_bytes""": [batch.nbytes]} ) _lowercase : List[str] = self.df.count() _lowercase : List[str] = df_num_rows if df_num_rows <= 1_00 else 1_00 # Approximate the size of each row (in Arrow format) by averaging over a max-100-row sample. _lowercase : Union[str, Any] = ( self.df.limit(UpperCAmelCase_ ) .repartition(1 ) .mapInArrow(UpperCAmelCase_ ,"""batch_bytes: long""" ) .agg(pyspark.sql.functions.sum("""batch_bytes""" ).alias("""sample_bytes""" ) ) .collect()[0] .sample_bytes / sample_num_rows ) _lowercase : List[Any] = approx_bytes_per_row * df_num_rows if approx_total_size > max_shard_size: # Make sure there is at least one row per partition. _lowercase : int = min(UpperCAmelCase_ ,int(approx_total_size / max_shard_size ) ) _lowercase : List[Any] = self.df.repartition(UpperCAmelCase_ ) def lowerCamelCase__ ( self ,UpperCAmelCase_ ,UpperCAmelCase_ ,UpperCAmelCase_ ,): import pyspark _lowercase : Union[str, Any] = ParquetWriter if file_format == """parquet""" else ArrowWriter _lowercase : List[Any] = os.path.join(self._working_dir ,os.path.basename(UpperCAmelCase_ ) ) if self._working_dir else fpath _lowercase : Any = file_format == """parquet""" # Define these so that we don't reference self in write_arrow, which will result in a pickling error due to # pickling the SparkContext. _lowercase : Union[str, Any] = self.config.features _lowercase : Optional[int] = self._writer_batch_size _lowercase : Optional[Any] = self._fs.storage_options def write_arrow(UpperCAmelCase_ ): # Within the same SparkContext, no two task attempts will share the same attempt ID. _lowercase : Any = pyspark.TaskContext().taskAttemptId() _lowercase : List[str] = next(UpperCAmelCase_ ,UpperCAmelCase_ ) if first_batch is None: # Some partitions might not receive any data. return pa.RecordBatch.from_arrays( [[task_id], [0], [0]] ,names=["""task_id""", """num_examples""", """num_bytes"""] ,) _lowercase : List[Any] = 0 _lowercase : int = writer_class( features=UpperCAmelCase_ ,path=working_fpath.replace("""SSSSS""" ,f"""{shard_id:05d}""" ).replace("""TTTTT""" ,f"""{task_id:05d}""" ) ,writer_batch_size=UpperCAmelCase_ ,storage_options=UpperCAmelCase_ ,embed_local_files=UpperCAmelCase_ ,) _lowercase : Optional[int] = pa.Table.from_batches([first_batch] ) writer.write_table(UpperCAmelCase_ ) for batch in it: if max_shard_size is not None and writer._num_bytes >= max_shard_size: _lowercase , _lowercase : Optional[Any] = writer.finalize() writer.close() yield pa.RecordBatch.from_arrays( [[task_id], [num_examples], [num_bytes]] ,names=["""task_id""", """num_examples""", """num_bytes"""] ,) shard_id += 1 _lowercase : Union[str, Any] = writer_class( features=writer._features ,path=working_fpath.replace("""SSSSS""" ,f"""{shard_id:05d}""" ).replace("""TTTTT""" ,f"""{task_id:05d}""" ) ,writer_batch_size=UpperCAmelCase_ ,storage_options=UpperCAmelCase_ ,embed_local_files=UpperCAmelCase_ ,) _lowercase : Dict = pa.Table.from_batches([batch] ) writer.write_table(UpperCAmelCase_ ) if writer._num_bytes > 0: _lowercase , _lowercase : Dict = writer.finalize() writer.close() yield pa.RecordBatch.from_arrays( [[task_id], [num_examples], [num_bytes]] ,names=["""task_id""", """num_examples""", """num_bytes"""] ,) if working_fpath != fpath: for file in os.listdir(os.path.dirname(UpperCAmelCase_ ) ): _lowercase : Dict = os.path.join(os.path.dirname(UpperCAmelCase_ ) ,os.path.basename(UpperCAmelCase_ ) ) shutil.move(UpperCAmelCase_ ,UpperCAmelCase_ ) _lowercase : List[str] = ( self.df.mapInArrow(UpperCAmelCase_ ,"""task_id: long, num_examples: long, num_bytes: long""" ) .groupBy("""task_id""" ) .agg( pyspark.sql.functions.sum("""num_examples""" ).alias("""total_num_examples""" ) ,pyspark.sql.functions.sum("""num_bytes""" ).alias("""total_num_bytes""" ) ,pyspark.sql.functions.count("""num_bytes""" ).alias("""num_shards""" ) ,pyspark.sql.functions.collect_list("""num_examples""" ).alias("""shard_lengths""" ) ,) .collect() ) for row in stats: yield row.task_id, (row.total_num_examples, row.total_num_bytes, row.num_shards, row.shard_lengths) def lowerCamelCase__ ( self ,UpperCAmelCase_ ,UpperCAmelCase_ = "arrow" ,UpperCAmelCase_ = None ,UpperCAmelCase_ = None ,**UpperCAmelCase_ ,): self._validate_cache_dir() _lowercase : Tuple = convert_file_size_to_int(max_shard_size or MAX_SHARD_SIZE ) self._repartition_df_if_needed(UpperCAmelCase_ ) _lowercase : Optional[int] = not is_remote_filesystem(self._fs ) _lowercase : Dict = os.path.join if is_local else posixpath.join _lowercase : int = """-TTTTT-SSSSS-of-NNNNN""" _lowercase : Optional[Any] = f"""{self.name}-{split_generator.name}{SUFFIX}.{file_format}""" _lowercase : Dict = path_join(self._output_dir ,UpperCAmelCase_ ) _lowercase : List[Any] = 0 _lowercase : Optional[Any] = 0 _lowercase : int = 0 _lowercase : Any = [] _lowercase : Any = [] for task_id, content in self._prepare_split_single(UpperCAmelCase_ ,UpperCAmelCase_ ,UpperCAmelCase_ ): ( ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ) : Tuple = content if num_bytes > 0: total_num_examples += num_examples total_num_bytes += num_bytes total_shards += num_shards task_id_and_num_shards.append((task_id, num_shards) ) all_shard_lengths.extend(UpperCAmelCase_ ) _lowercase : Optional[int] = total_num_examples _lowercase : List[Any] = total_num_bytes # should rename everything at the end logger.debug(f"""Renaming {total_shards} shards.""" ) if total_shards > 1: _lowercase : List[Any] = all_shard_lengths # Define fs outside of _rename_shard so that we don't reference self in the function, which will result in a # pickling error due to pickling the SparkContext. _lowercase : Union[str, Any] = self._fs # use the -SSSSS-of-NNNNN pattern def _rename_shard( UpperCAmelCase_ ,UpperCAmelCase_ ,UpperCAmelCase_ ,): rename( UpperCAmelCase_ ,fpath.replace("""SSSSS""" ,f"""{shard_id:05d}""" ).replace("""TTTTT""" ,f"""{task_id:05d}""" ) ,fpath.replace("""TTTTT-SSSSS""" ,f"""{global_shard_id:05d}""" ).replace("""NNNNN""" ,f"""{total_shards:05d}""" ) ,) _lowercase : Optional[Any] = [] _lowercase : List[str] = 0 for i in range(len(UpperCAmelCase_ ) ): _lowercase , _lowercase : List[str] = task_id_and_num_shards[i] for shard_id in range(UpperCAmelCase_ ): args.append([task_id, shard_id, global_shard_id] ) global_shard_id += 1 self._spark.sparkContext.parallelize(UpperCAmelCase_ ,len(UpperCAmelCase_ ) ).map(lambda UpperCAmelCase_ : _rename_shard(*UpperCAmelCase_ ) ).collect() else: # don't use any pattern _lowercase : Tuple = 0 _lowercase : Optional[Any] = task_id_and_num_shards[0][0] self._rename( fpath.replace("""SSSSS""" ,f"""{shard_id:05d}""" ).replace("""TTTTT""" ,f"""{task_id:05d}""" ) ,fpath.replace(UpperCAmelCase_ ,"""""" ) ,) def lowerCamelCase__ ( self ,UpperCAmelCase_ ,): return SparkExamplesIterable(self.df )
336
1
import itertools import random import unittest import numpy as np from transformers import BatchFeature, SpeechTaFeatureExtractor from transformers.testing_utils import require_torch from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch UpperCAmelCase : str = random.Random() def _SCREAMING_SNAKE_CASE ( a , a=1.0 , a=None , a=None ) -> Optional[int]: if rng is None: __A : Dict = global_rng __A : Union[str, Any] = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch class _A( unittest.TestCase ): """simple docstring""" def __init__( self , _A , _A=7 , _A=400 , _A=2000 , _A=1 , _A=0.0 , _A=16000 , _A=True , _A=80 , _A=16 , _A=64 , _A="hann_window" , _A=80 , _A=7600 , _A=1e-1_0 , _A=True , ): __A : Optional[Any] = parent __A : Any = batch_size __A : Tuple = min_seq_length __A : str = max_seq_length __A : Union[str, Any] = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) __A : List[str] = feature_size __A : int = padding_value __A : Optional[Any] = sampling_rate __A : Any = do_normalize __A : List[str] = num_mel_bins __A : int = hop_length __A : Tuple = win_length __A : Union[str, Any] = win_function __A : Union[str, Any] = fmin __A : int = fmax __A : Union[str, Any] = mel_floor __A : Optional[int] = return_attention_mask def UpperCAmelCase_ ( self ): return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "do_normalize": self.do_normalize, "num_mel_bins": self.num_mel_bins, "hop_length": self.hop_length, "win_length": self.win_length, "win_function": self.win_function, "fmin": self.fmin, "fmax": self.fmax, "mel_floor": self.mel_floor, "return_attention_mask": self.return_attention_mask, } def UpperCAmelCase_ ( self , _A=False , _A=False ): def _flatten(_A ): return list(itertools.chain(*_A ) ) if equal_length: __A : int = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size __A : int = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: __A : Optional[int] = [np.asarray(_A ) for x in speech_inputs] return speech_inputs def UpperCAmelCase_ ( self , _A=False , _A=False ): if equal_length: __A : List[Any] = [floats_list((self.max_seq_length, self.num_mel_bins) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size __A : Dict = [ floats_list((x, self.num_mel_bins) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: __A : Optional[Any] = [np.asarray(_A ) for x in speech_inputs] return speech_inputs @require_torch class _A( snake_case__ , unittest.TestCase ): """simple docstring""" UpperCamelCase : Optional[Any] = SpeechTaFeatureExtractor def UpperCAmelCase_ ( self ): __A : Tuple = SpeechTaFeatureExtractionTester(self ) def UpperCAmelCase_ ( self , _A ): self.assertTrue(np.all(np.mean(_A , axis=0 ) < 1e-3 ) ) self.assertTrue(np.all(np.abs(np.var(_A , axis=0 ) - 1 ) < 1e-3 ) ) def UpperCAmelCase_ ( self ): # Tests that all call wrap to encode_plus and batch_encode_plus __A : Any = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 __A : Dict = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] __A : Optional[Any] = [np.asarray(_A ) for speech_input in speech_inputs] # Test not batched input __A : List[str] = feat_extract(speech_inputs[0] , return_tensors='np' ).input_values __A : Any = feat_extract(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(_A , _A , atol=1e-3 ) ) # Test batched __A : List[str] = feat_extract(_A , return_tensors='np' ).input_values __A : int = feat_extract(_A , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_A , _A ): self.assertTrue(np.allclose(_A , _A , atol=1e-3 ) ) def UpperCAmelCase_ ( self ): __A : int = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) __A : Union[str, Any] = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] __A : Union[str, Any] = ['longest', 'max_length', 'do_not_pad'] __A : List[str] = [None, 1600, None] for max_length, padding in zip(_A , _A ): __A : Tuple = feat_extract(_A , padding=_A , max_length=_A , return_tensors='np' ) __A : Dict = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self.assertTrue(input_values[0][800:].sum() < 1e-6 ) self._check_zero_mean_unit_variance(input_values[1][:1000] ) self.assertTrue(input_values[0][1000:].sum() < 1e-6 ) self._check_zero_mean_unit_variance(input_values[2][:1200] ) def UpperCAmelCase_ ( self ): __A : Any = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) __A : int = range(800 , 1400 , 200 ) __A : Optional[Any] = [floats_list((1, x) )[0] for x in lengths] __A : Any = ['longest', 'max_length', 'do_not_pad'] __A : int = [None, 1600, None] for max_length, padding in zip(_A , _A ): __A : List[str] = feat_extract(_A , max_length=_A , padding=_A ) __A : Optional[int] = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self._check_zero_mean_unit_variance(input_values[1][:1000] ) self._check_zero_mean_unit_variance(input_values[2][:1200] ) def UpperCAmelCase_ ( self ): __A : List[str] = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) __A : int = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] __A : Dict = feat_extract( _A , truncation=_A , max_length=1000 , padding='max_length' , return_tensors='np' ) __A : Union[str, Any] = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1] ) self._check_zero_mean_unit_variance(input_values[2] ) def UpperCAmelCase_ ( self ): __A : Any = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) __A : int = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] __A : Tuple = feat_extract( _A , truncation=_A , max_length=1000 , padding='longest' , return_tensors='np' ) __A : Tuple = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertTrue(input_values.shape == (3, 1000) ) __A : List[str] = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] __A : str = feat_extract( _A , truncation=_A , max_length=2000 , padding='longest' , return_tensors='np' ) __A : List[Any] = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length > longest -> then pad to longest self.assertTrue(input_values.shape == (3, 1200) ) def UpperCAmelCase_ ( self ): __A : Any = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) __A : List[str] = np.random.rand(100 ).astype(np.floataa ) __A : Optional[int] = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: __A : Optional[int] = feature_extractor.pad([{'input_values': inputs}] , return_tensors='np' ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) __A : List[str] = feature_extractor.pad([{'input_values': inputs}] , return_tensors='pt' ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) def UpperCAmelCase_ ( self ): # Tests that all call wrap to encode_plus and batch_encode_plus __A : Optional[int] = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 __A : Union[str, Any] = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] __A : List[str] = [np.asarray(_A ) for speech_input in speech_inputs] # Test feature size __A : Dict = feature_extractor(audio_target=_A , padding=_A , return_tensors='np' ).input_values self.assertTrue(input_values.ndim == 3 ) self.assertTrue(input_values.shape[-1] == feature_extractor.num_mel_bins ) # Test not batched input __A : Dict = feature_extractor(speech_inputs[0] , return_tensors='np' ).input_values __A : List[str] = feature_extractor(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(_A , _A , atol=1e-3 ) ) # Test batched __A : Union[str, Any] = feature_extractor(_A , return_tensors='np' ).input_values __A : Any = feature_extractor(_A , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_A , _A ): self.assertTrue(np.allclose(_A , _A , atol=1e-3 ) ) # Test 2-D numpy arrays are batched. __A : Union[str, Any] = [floats_list((1, x) )[0] for x in (800, 800, 800)] __A : Union[str, Any] = np.asarray(_A ) __A : Optional[int] = feature_extractor(_A , return_tensors='np' ).input_values __A : List[Any] = feature_extractor(_A , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_A , _A ): self.assertTrue(np.allclose(_A , _A , atol=1e-3 ) ) def UpperCAmelCase_ ( self ): __A : Dict = self.feat_extract_tester.prepare_inputs_for_target() __A : str = self.feature_extraction_class(**self.feat_extract_dict ) __A : Any = feat_extract.model_input_names[0] __A : Any = BatchFeature({input_name: speech_inputs} ) self.assertTrue(all(len(_A ) == len(_A ) for x, y in zip(_A , processed_features[input_name] ) ) ) __A : int = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_A ) __A : int = BatchFeature({input_name: speech_inputs} , tensor_type='np' ) __A : Dict = processed_features[input_name] if len(batch_features_input.shape ) < 3: __A : Tuple = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def UpperCAmelCase_ ( self ): __A : Tuple = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_A ) __A : Optional[int] = self.feature_extraction_class(**self.feat_extract_dict ) __A : Union[str, Any] = feat_extract.model_input_names[0] __A : Dict = BatchFeature({input_name: speech_inputs} , tensor_type='pt' ) __A : str = processed_features[input_name] if len(batch_features_input.shape ) < 3: __A : Tuple = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def UpperCAmelCase_ ( self ): __A : int = self.feature_extraction_class(**self.feat_extract_dict ) __A : Optional[Any] = self.feat_extract_tester.prepare_inputs_for_target() __A : int = feat_extract.model_input_names[0] __A : Optional[Any] = BatchFeature({input_name: speech_inputs} ) __A : Union[str, Any] = feat_extract.num_mel_bins # hack! __A : Dict = feat_extract.pad(_A , padding='longest' , return_tensors='np' )[input_name] __A : str = feat_extract.pad(_A , padding='longest' , return_tensors='pt' )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_pt.numpy().astype(np.floataa ).sum() ) < 1e-2 ) def UpperCAmelCase_ ( self ): __A : Dict = self.feat_extract_dict __A : str = True __A : Union[str, Any] = self.feature_extraction_class(**_A ) __A : str = self.feat_extract_tester.prepare_inputs_for_target() __A : Optional[int] = [len(_A ) for x in speech_inputs] __A : Any = feat_extract.model_input_names[0] __A : int = BatchFeature({input_name: speech_inputs} ) __A : Union[str, Any] = feat_extract.num_mel_bins # hack! __A : Dict = feat_extract.pad(_A , padding='longest' , return_tensors='np' ) self.assertIn('attention_mask' , _A ) self.assertListEqual(list(processed.attention_mask.shape ) , list(processed[input_name].shape[:2] ) ) self.assertListEqual(processed.attention_mask.sum(-1 ).tolist() , _A ) def UpperCAmelCase_ ( self ): __A : Union[str, Any] = self.feat_extract_dict __A : List[Any] = True __A : Optional[int] = self.feature_extraction_class(**_A ) __A : int = self.feat_extract_tester.prepare_inputs_for_target() __A : Union[str, Any] = [len(_A ) for x in speech_inputs] __A : Union[str, Any] = feat_extract.model_input_names[0] __A : str = BatchFeature({input_name: speech_inputs} ) __A : Dict = min(_A ) __A : List[Any] = feat_extract.num_mel_bins # hack! __A : Optional[Any] = feat_extract.pad( _A , padding='max_length' , max_length=_A , truncation=_A , return_tensors='np' ) self.assertIn('attention_mask' , _A ) self.assertListEqual( list(processed_pad.attention_mask.shape ) , [processed_pad[input_name].shape[0], max_length] ) self.assertListEqual( processed_pad.attention_mask[:, :max_length].sum(-1 ).tolist() , [max_length for x in speech_inputs] ) def UpperCAmelCase_ ( self , _A ): from datasets import load_dataset __A : Optional[int] = load_dataset('hf-internal-testing/librispeech_asr_dummy' , 'clean' , split='validation' ) # automatic decoding with librispeech __A : Optional[Any] = ds.sort('id' ).select(range(_A ) )[:num_samples]['audio'] return [x["array"] for x in speech_samples] def UpperCAmelCase_ ( self ): # fmt: off __A : Optional[Any] = torch.tensor( [2.3_8_0_4e-0_3, 2.0_7_5_2e-0_3, 1.9_8_3_6e-0_3, 2.1_0_5_7e-0_3, 1.6_1_7_4e-0_3, 3.0_5_1_8e-0_4, 9.1_5_5_3e-0_5, 3.3_5_6_9e-0_4, 9.7_6_5_6e-0_4, 1.8_3_1_1e-0_3, 2.0_1_4_2e-0_3, 2.1_0_5_7e-0_3, 1.7_3_9_5e-0_3, 4.5_7_7_6e-0_4, -3.9_6_7_3e-0_4, 4.5_7_7_6e-0_4, 1.0_0_7_1e-0_3, 9.1_5_5_3e-0_5, 4.8_8_2_8e-0_4, 1.1_5_9_7e-0_3, 7.3_2_4_2e-0_4, 9.4_6_0_4e-0_4, 1.8_0_0_5e-0_3, 1.8_3_1_1e-0_3, 8.8_5_0_1e-0_4, 4.2_7_2_5e-0_4, 4.8_8_2_8e-0_4, 7.3_2_4_2e-0_4, 1.0_9_8_6e-0_3, 2.1_0_5_7e-0_3] ) # fmt: on __A : Any = self._load_datasamples(1 ) __A : Tuple = SpeechTaFeatureExtractor() __A : str = feature_extractor(_A , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 93680) ) self.assertTrue(torch.allclose(input_values[0, :30] , _A , atol=1e-6 ) ) def UpperCAmelCase_ ( self ): # fmt: off __A : Optional[int] = torch.tensor( [-2.6_8_7_0, -3.0_1_0_4, -3.1_3_5_6, -3.5_3_5_2, -3.0_0_4_4, -3.0_3_5_3, -3.4_7_1_9, -3.6_7_7_7, -3.1_5_2_0, -2.9_4_3_5, -2.6_5_5_3, -2.8_7_9_5, -2.9_9_4_4, -2.5_9_2_1, -3.0_2_7_9, -3.0_3_8_6, -3.0_8_6_4, -3.1_2_9_1, -3.2_3_5_3, -2.7_4_4_4, -2.6_8_3_1, -2.7_2_8_7, -3.1_7_6_1, -3.1_5_7_1, -3.2_7_2_6, -3.0_5_8_2, -3.1_0_0_7, -3.4_5_3_3, -3.4_6_9_5, -3.0_9_9_8] ) # fmt: on __A : List[str] = self._load_datasamples(1 ) __A : Optional[Any] = SpeechTaFeatureExtractor() __A : Optional[int] = feature_extractor(audio_target=_A , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 366, 80) ) self.assertTrue(torch.allclose(input_values[0, 0, :30] , _A , atol=1e-4 ) )
280
import unittest from transformers import BertGenerationTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin UpperCAmelCase : List[str] = '''▁''' UpperCAmelCase : Optional[Any] = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece class _A( snake_case__ , unittest.TestCase ): """simple docstring""" UpperCamelCase : Optional[int] = BertGenerationTokenizer UpperCamelCase : str = False UpperCamelCase : Tuple = True def UpperCAmelCase_ ( self ): super().setUp() __A : Tuple = BertGenerationTokenizer(_A , keep_accents=_A ) tokenizer.save_pretrained(self.tmpdirname ) def UpperCAmelCase_ ( self ): __A : str = '<s>' __A : str = 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 ): __A : int = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '<unk>' ) self.assertEqual(vocab_keys[1] , '<s>' ) self.assertEqual(vocab_keys[-1] , '<pad>' ) self.assertEqual(len(_A ) , 1002 ) def UpperCAmelCase_ ( self ): self.assertEqual(self.get_tokenizer().vocab_size , 1000 ) def UpperCAmelCase_ ( self ): __A : str = BertGenerationTokenizer(_A , keep_accents=_A ) __A : Dict = tokenizer.tokenize('This is a test' ) self.assertListEqual(_A , ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_A ) , [285, 46, 10, 170, 382] , ) __A : int = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( _A , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '9', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', 'é', '.', ] , ) __A : Dict = tokenizer.convert_tokens_to_ids(_A ) self.assertListEqual( _A , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) __A : Optional[int] = tokenizer.convert_ids_to_tokens(_A ) self.assertListEqual( _A , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '<unk>', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', '<unk>', '.', ] , ) @cached_property def UpperCAmelCase_ ( self ): return BertGenerationTokenizer.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder' ) @slow def UpperCAmelCase_ ( self ): __A : List[Any] = 'Hello World!' __A : Optional[Any] = [18536, 2260, 101] self.assertListEqual(_A , self.big_tokenizer.encode(_A ) ) @slow def UpperCAmelCase_ ( self ): __A : 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' ) __A : int = [ 871, 419, 358, 946, 991, 2521, 452, 358, 1357, 387, 7751, 3536, 112, 985, 456, 126, 865, 938, 5400, 5734, 458, 1368, 467, 786, 2462, 5246, 1159, 633, 865, 4519, 457, 582, 852, 2557, 427, 916, 508, 405, 34324, 497, 391, 408, 11342, 1244, 385, 100, 938, 985, 456, 574, 362, 12597, 3200, 3129, 1172, ] self.assertListEqual(_A , self.big_tokenizer.encode(_A ) ) @require_torch @slow def UpperCAmelCase_ ( self ): import torch from transformers import BertGenerationConfig, BertGenerationEncoder # Build sequence __A : Tuple = list(self.big_tokenizer.get_vocab().keys() )[:10] __A : List[Any] = ' '.join(_A ) __A : Union[str, Any] = self.big_tokenizer.encode_plus(_A , return_tensors='pt' , return_token_type_ids=_A ) __A : Optional[Any] = self.big_tokenizer.batch_encode_plus( [sequence + ' ' + sequence] , return_tensors='pt' , return_token_type_ids=_A ) __A : int = BertGenerationConfig() __A : List[str] = BertGenerationEncoder(_A ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**_A ) model(**_A ) @slow def UpperCAmelCase_ ( self ): # fmt: off __A : str = {'input_ids': [[39286, 458, 36335, 2001, 456, 13073, 13266, 455, 113, 7746, 1741, 11157, 391, 13073, 13266, 455, 113, 3967, 35412, 113, 4936, 109, 3870, 2377, 113, 30084, 45720, 458, 134, 17496, 112, 503, 11672, 113, 118, 112, 5665, 13347, 38687, 112, 1496, 31389, 112, 3268, 47264, 134, 962, 112, 16377, 8035, 23130, 430, 12169, 15518, 28592, 458, 146, 41697, 109, 391, 12169, 15518, 16689, 458, 146, 41358, 109, 452, 726, 4034, 111, 763, 35412, 5082, 388, 1903, 111, 9051, 391, 2870, 48918, 1900, 1123, 550, 998, 112, 9586, 15985, 455, 391, 410, 22955, 37636, 114], [448, 17496, 419, 3663, 385, 763, 113, 27533, 2870, 3283, 13043, 1639, 24713, 523, 656, 24013, 18550, 2521, 517, 27014, 21244, 420, 1212, 1465, 391, 927, 4833, 388, 578, 11786, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [484, 2169, 7687, 21932, 18146, 726, 363, 17032, 3391, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_A , model_name='google/bert_for_seq_generation_L-24_bbc_encoder' , revision='c817d1fd1be2ffa69431227a1fe320544943d4db' , )
280
1
from typing import Callable, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { '''microsoft/xprophetnet-large-wiki100-cased''': ( '''https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/config.json''' ), } class snake_case__(_UpperCamelCase ): """simple docstring""" lowercase_ = """xlm-prophetnet""" lowercase_ = ["""past_key_values"""] lowercase_ = { """num_attention_heads""": """num_encoder_attention_heads""", } def __init__( self : Any , SCREAMING_SNAKE_CASE : Optional[float] = 0.1 , SCREAMING_SNAKE_CASE : Optional[Union[str, Callable]] = "gelu" , SCREAMING_SNAKE_CASE : Optional[int] = 30_522 , SCREAMING_SNAKE_CASE : Optional[int] = 1_024 , SCREAMING_SNAKE_CASE : Optional[int] = 4_096 , SCREAMING_SNAKE_CASE : Optional[int] = 12 , SCREAMING_SNAKE_CASE : Optional[int] = 16 , SCREAMING_SNAKE_CASE : Optional[int] = 4_096 , SCREAMING_SNAKE_CASE : Optional[int] = 12 , SCREAMING_SNAKE_CASE : Optional[int] = 16 , SCREAMING_SNAKE_CASE : Optional[float] = 0.1 , SCREAMING_SNAKE_CASE : Optional[float] = 0.1 , SCREAMING_SNAKE_CASE : Optional[int] = 512 , SCREAMING_SNAKE_CASE : Optional[float] = 0.02 , SCREAMING_SNAKE_CASE : Optional[bool] = True , SCREAMING_SNAKE_CASE : Optional[bool] = True , SCREAMING_SNAKE_CASE : Optional[int] = 0 , SCREAMING_SNAKE_CASE : Optional[int] = 2 , SCREAMING_SNAKE_CASE : Optional[int] = 32 , SCREAMING_SNAKE_CASE : Optional[int] = 128 , SCREAMING_SNAKE_CASE : Optional[bool] = False , SCREAMING_SNAKE_CASE : Optional[float] = 0.0 , SCREAMING_SNAKE_CASE : Optional[bool] = True , SCREAMING_SNAKE_CASE : Optional[int] = 0 , SCREAMING_SNAKE_CASE : Optional[int] = 1 , SCREAMING_SNAKE_CASE : Optional[int] = 2 , **SCREAMING_SNAKE_CASE : Optional[int] , ): lowercase__ : Any = vocab_size lowercase__ : Dict = hidden_size lowercase__ : List[Any] = encoder_ffn_dim lowercase__ : int = num_encoder_layers lowercase__ : str = num_encoder_attention_heads lowercase__ : Optional[int] = decoder_ffn_dim lowercase__ : Optional[int] = num_decoder_layers lowercase__ : int = num_decoder_attention_heads lowercase__ : Tuple = max_position_embeddings lowercase__ : Dict = init_std # Normal(0, this parameter) lowercase__ : int = activation_function # parameters for xlmprophetnet lowercase__ : int = ngram lowercase__ : int = num_buckets lowercase__ : Optional[int] = relative_max_distance lowercase__ : int = disable_ngram_loss lowercase__ : List[Any] = eps # 3 Types of Dropout lowercase__ : Union[str, Any] = attention_dropout lowercase__ : Tuple = activation_dropout lowercase__ : Tuple = dropout lowercase__ : Any = use_cache super().__init__( pad_token_id=SCREAMING_SNAKE_CASE , bos_token_id=SCREAMING_SNAKE_CASE , eos_token_id=SCREAMING_SNAKE_CASE , is_encoder_decoder=SCREAMING_SNAKE_CASE , add_cross_attention=SCREAMING_SNAKE_CASE , decoder_start_token_id=SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE , ) @property def snake_case ( self : Union[str, Any] ): return self.num_encoder_layers + self.num_decoder_layers @num_hidden_layers.setter def snake_case ( self : Dict , SCREAMING_SNAKE_CASE : Optional[int] ): raise NotImplementedError( "This model does not support the setting of `num_hidden_layers`. Please set `num_encoder_layers` and" " `num_decoder_layers`." )
121
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase__ = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ['''NllbTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ['''NllbTokenizerFast'''] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_nllb import NllbTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_nllb_fast import NllbTokenizerFast else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
121
1
'''simple docstring''' import os import sys import unittest UpperCamelCase__: int = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, "utils")) import check_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 UpperCamelCase__: Optional[Any] = os.path.join(git_repo_path, "src", "transformers") UpperCamelCase__: List[Any] = "\n{0} = None\n" UpperCamelCase__: Optional[Any] = "\nclass {0}(metaclass=DummyObject):\n _backends = {1}\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, {1})\n" UpperCamelCase__: List[str] = "\ndef {0}(*args, **kwargs):\n requires_backends({0}, {1})\n" class SCREAMING_SNAKE_CASE( unittest.TestCase ): """simple docstring""" def A ( self : List[str] ) -> Optional[int]: UpperCAmelCase : int = find_backend(''' _import_structure["models.albert"].append("AlbertTokenizerFast")''' ) self.assertIsNone(__snake_case ) UpperCAmelCase : List[Any] = find_backend(''' if not is_tokenizers_available():''' ) self.assertEqual(__snake_case , '''tokenizers''' ) UpperCAmelCase : Any = find_backend(''' if not is_tensorflow_text_available():''' ) self.assertEqual(__snake_case , '''tensorflow_text''' ) UpperCAmelCase : Union[str, Any] = find_backend(''' if not (is_sentencepiece_available() and is_tokenizers_available()):''' ) self.assertEqual(__snake_case , '''sentencepiece_and_tokenizers''' ) UpperCAmelCase : Dict = find_backend( ''' if not (is_sentencepiece_available() and is_tensorflow_text_available()):''' ) self.assertEqual(__snake_case , '''sentencepiece_and_tensorflow_text''' ) UpperCAmelCase : int = find_backend( ''' if not (is_sentencepiece_available() and is_tokenizers_available() and is_vision_available()):''' ) self.assertEqual(__snake_case , '''sentencepiece_and_tokenizers_and_vision''' ) def A ( self : List[Any] ) -> Optional[int]: UpperCAmelCase : Optional[int] = read_init() # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects self.assertIn('''torch''' , __snake_case ) self.assertIn('''tensorflow_text''' , __snake_case ) self.assertIn('''sentencepiece_and_tokenizers''' , __snake_case ) # 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 : Union[str, Any] ) -> str: UpperCAmelCase : List[Any] = create_dummy_object('''CONSTANT''' , '''\'torch\'''' ) self.assertEqual(__snake_case , '''\nCONSTANT = None\n''' ) UpperCAmelCase : Any = create_dummy_object('''function''' , '''\'torch\'''' ) self.assertEqual( __snake_case , '''\ndef function(*args, **kwargs):\n requires_backends(function, \'torch\')\n''' ) UpperCAmelCase : List[Any] = ''' class FakeClass(metaclass=DummyObject): _backends = \'torch\' def __init__(self, *args, **kwargs): requires_backends(self, \'torch\') ''' UpperCAmelCase : Dict = create_dummy_object('''FakeClass''' , '''\'torch\'''' ) self.assertEqual(__snake_case , __snake_case ) def A ( self : List[Any] ) -> Tuple: UpperCAmelCase : List[str] = '''# This file is autogenerated by the command `make fix-copies`, do not edit. from ..utils import DummyObject, requires_backends CONSTANT = None def function(*args, **kwargs): requires_backends(function, ["torch"]) class FakeClass(metaclass=DummyObject): _backends = ["torch"] def __init__(self, *args, **kwargs): requires_backends(self, ["torch"]) ''' UpperCAmelCase : List[Any] = create_dummy_files({'''torch''': ['''CONSTANT''', '''function''', '''FakeClass''']} ) self.assertEqual(dummy_files['''torch'''] , __snake_case )
23
from __future__ import annotations def UpperCamelCase ( _A ): # This function is recursive """simple docstring""" __magic_name__ : str = len(_A ) # If the array contains only one element, we return it (it's the stop condition of # recursion) if array_length <= 1: return array # Else __magic_name__ : Dict = array[0] __magic_name__ : Optional[Any] = False __magic_name__ : Tuple = 1 __magic_name__ : list[int] = [] while not is_found and i < array_length: if array[i] < pivot: __magic_name__ : Union[str, Any] = True __magic_name__ : List[Any] = [element for element in array[i:] if element >= array[i]] __magic_name__ : Dict = longest_subsequence(_A ) if len(_A ) > len(_A ): __magic_name__ : Tuple = temp_array else: i += 1 __magic_name__ : Any = [element for element in array[1:] if element >= pivot] __magic_name__ : Dict = [pivot, *longest_subsequence(_A )] if len(_A ) > len(_A ): return temp_array else: return longest_subseq if __name__ == "__main__": import doctest doctest.testmod()
342
0
# coding=utf-8 # Copyright 2020 The HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # this script dumps information about the environment import os import sys import transformers __a = '3' print('Python version:', sys.version) print('transformers version:', transformers.__version__) try: import torch print('Torch version:', torch.__version__) print('Cuda available:', torch.cuda.is_available()) print('Cuda version:', torch.version.cuda) print('CuDNN version:', torch.backends.cudnn.version()) print('Number of GPUs available:', torch.cuda.device_count()) print('NCCL version:', torch.cuda.nccl.version()) except ImportError: print('Torch version:', None) try: import deepspeed print('DeepSpeed version:', deepspeed.__version__) except ImportError: print('DeepSpeed version:', None) try: import tensorflow as tf print('TensorFlow version:', tf.__version__) print('TF GPUs available:', bool(tf.config.list_physical_devices('GPU'))) print('Number of TF GPUs available:', len(tf.config.list_physical_devices('GPU'))) except ImportError: print('TensorFlow version:', None)
235
import argparse import logging import sys from unittest.mock import patch import run_glue_deebert from transformers.testing_utils import TestCasePlus, get_gpu_count, require_torch_non_multi_gpu, slow logging.basicConfig(level=logging.DEBUG) __a = logging.getLogger() def lowerCamelCase__ ( ): '''simple docstring''' UpperCAmelCase_ : List[str] = argparse.ArgumentParser() parser.add_argument('''-f''' ) UpperCAmelCase_ : Dict = parser.parse_args() return args.f class __a( _a ): """simple docstring""" def a__ ( self ) -> None: UpperCAmelCase_ : int = logging.StreamHandler(sys.stdout ) logger.addHandler(_SCREAMING_SNAKE_CASE ) def a__ ( self ,_SCREAMING_SNAKE_CASE ) -> str: UpperCAmelCase_ : int = get_gpu_count() if n_gpu > 1: pass # XXX: doesn't quite work with n_gpu > 1 https://github.com/huggingface/transformers/issues/10560 # script = f"{self.examples_dir_str}/research_projects/deebert/run_glue_deebert.py" # distributed_args = f"-m torch.distributed.launch --nproc_per_node={n_gpu} {script}".split() # cmd = [sys.executable] + distributed_args + args # execute_subprocess_async(cmd, env=self.get_env()) # XXX: test the results - need to save them first into .json file else: args.insert(0 ,'''run_glue_deebert.py''' ) with patch.object(_SCREAMING_SNAKE_CASE ,'''argv''' ,_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : List[str] = run_glue_deebert.main() for value in result.values(): self.assertGreaterEqual(_SCREAMING_SNAKE_CASE ,0.6_66 ) @slow @require_torch_non_multi_gpu def a__ ( self ) -> List[str]: UpperCAmelCase_ : List[Any] = ''' --model_type roberta --model_name_or_path roberta-base --task_name MRPC --do_train --do_eval --do_lower_case --data_dir ./tests/fixtures/tests_samples/MRPC/ --max_seq_length 128 --per_gpu_eval_batch_size=1 --per_gpu_train_batch_size=8 --learning_rate 2e-4 --num_train_epochs 3 --overwrite_output_dir --seed 42 --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --plot_data_dir ./examples/deebert/results/ --save_steps 0 --overwrite_cache --eval_after_first_stage '''.split() self.run_and_check(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Union[str, Any] = ''' --model_type roberta --model_name_or_path ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --task_name MRPC --do_eval --do_lower_case --data_dir ./tests/fixtures/tests_samples/MRPC/ --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --plot_data_dir ./examples/deebert/results/ --max_seq_length 128 --eval_each_highway --eval_highway --overwrite_cache --per_gpu_eval_batch_size=1 '''.split() self.run_and_check(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Any = ''' --model_type roberta --model_name_or_path ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --task_name MRPC --do_eval --do_lower_case --data_dir ./tests/fixtures/tests_samples/MRPC/ --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --plot_data_dir ./examples/deebert/results/ --max_seq_length 128 --early_exit_entropy 0.1 --eval_highway --overwrite_cache --per_gpu_eval_batch_size=1 '''.split() self.run_and_check(_SCREAMING_SNAKE_CASE )
235
1
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowercase : List[str] = logging.get_logger(__name__) lowercase : Union[str, Any] = { "facebook/xlm-roberta-xl": "https://huggingface.co/facebook/xlm-roberta-xl/resolve/main/config.json", "facebook/xlm-roberta-xxl": "https://huggingface.co/facebook/xlm-roberta-xxl/resolve/main/config.json", # See all XLM-RoBERTa-XL models at https://huggingface.co/models?filter=xlm-roberta-xl } class __UpperCAmelCase ( _lowerCamelCase ): __lowercase = """xlm-roberta-xl""" def __init__( self , lowerCAmelCase_=25_08_80 , lowerCAmelCase_=25_60 , lowerCAmelCase_=36 , lowerCAmelCase_=32 , lowerCAmelCase_=1_02_40 , lowerCAmelCase_="gelu" , lowerCAmelCase_=0.1 , lowerCAmelCase_=0.1 , lowerCAmelCase_=5_14 , lowerCAmelCase_=1 , lowerCAmelCase_=0.02 , lowerCAmelCase_=1E-05 , lowerCAmelCase_=1 , lowerCAmelCase_=0 , lowerCAmelCase_=2 , lowerCAmelCase_="absolute" , lowerCAmelCase_=True , lowerCAmelCase_=None , **lowerCAmelCase_ , ): """simple docstring""" super().__init__(pad_token_id=lowerCAmelCase_ , bos_token_id=lowerCAmelCase_ , eos_token_id=lowerCAmelCase_ , **lowerCAmelCase_ ) _snake_case = vocab_size _snake_case = hidden_size _snake_case = num_hidden_layers _snake_case = num_attention_heads _snake_case = hidden_act _snake_case = intermediate_size _snake_case = hidden_dropout_prob _snake_case = attention_probs_dropout_prob _snake_case = max_position_embeddings _snake_case = type_vocab_size _snake_case = initializer_range _snake_case = layer_norm_eps _snake_case = position_embedding_type _snake_case = use_cache _snake_case = classifier_dropout class __UpperCAmelCase ( _lowerCamelCase ): @property def lowerCamelCase ( self ): """simple docstring""" if self.task == "multiple-choice": _snake_case = {0: 'batch', 1: 'choice', 2: 'sequence'} else: _snake_case = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ] )
42
"""simple docstring""" import unittest from transformers import ( MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING, TextGenerationPipeline, logging, pipeline, ) from transformers.testing_utils import ( CaptureLogger, is_pipeline_test, require_accelerate, require_tf, require_torch, require_torch_gpu, require_torch_or_tf, ) from .test_pipelines_common import ANY @is_pipeline_test @require_torch_or_tf class lowerCamelCase ( unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE = MODEL_FOR_CAUSAL_LM_MAPPING SCREAMING_SNAKE_CASE = TF_MODEL_FOR_CAUSAL_LM_MAPPING @require_torch def _a (self ): """simple docstring""" UpperCAmelCase__ : List[str] = pipeline(task="""text-generation""" , model="""sshleifer/tiny-ctrl""" , framework="""pt""" ) # Using `do_sample=False` to force deterministic output UpperCAmelCase__ : List[str] = text_generator("""This is a test""" , do_sample=_lowerCamelCase ) self.assertEqual( _lowerCamelCase , [ { """generated_text""": ( """This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope.""" """ oscope. FiliFili@@""" ) } ] , ) UpperCAmelCase__ : List[Any] = text_generator(["""This is a test""", """This is a second test"""] ) self.assertEqual( _lowerCamelCase , [ [ { """generated_text""": ( """This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope.""" """ oscope. FiliFili@@""" ) } ], [ { """generated_text""": ( """This is a second test ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy""" """ oscope. oscope. FiliFili@@""" ) } ], ] , ) UpperCAmelCase__ : int = text_generator("""This is a test""" , do_sample=_lowerCamelCase , num_return_sequences=2 , return_tensors=_lowerCamelCase ) self.assertEqual( _lowerCamelCase , [ {"""generated_token_ids""": ANY(_lowerCamelCase )}, {"""generated_token_ids""": ANY(_lowerCamelCase )}, ] , ) UpperCAmelCase__ : Optional[int] = text_generator.model.config.eos_token_id UpperCAmelCase__ : Any = """<pad>""" UpperCAmelCase__ : Any = text_generator( ["""This is a test""", """This is a second test"""] , do_sample=_lowerCamelCase , num_return_sequences=2 , batch_size=2 , return_tensors=_lowerCamelCase , ) self.assertEqual( _lowerCamelCase , [ [ {"""generated_token_ids""": ANY(_lowerCamelCase )}, {"""generated_token_ids""": ANY(_lowerCamelCase )}, ], [ {"""generated_token_ids""": ANY(_lowerCamelCase )}, {"""generated_token_ids""": ANY(_lowerCamelCase )}, ], ] , ) @require_tf def _a (self ): """simple docstring""" UpperCAmelCase__ : str = pipeline(task="""text-generation""" , model="""sshleifer/tiny-ctrl""" , framework="""tf""" ) # Using `do_sample=False` to force deterministic output UpperCAmelCase__ : List[str] = text_generator("""This is a test""" , do_sample=_lowerCamelCase ) self.assertEqual( _lowerCamelCase , [ { """generated_text""": ( """This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵""" """ please,""" ) } ] , ) UpperCAmelCase__ : Dict = text_generator(["""This is a test""", """This is a second test"""] , do_sample=_lowerCamelCase ) self.assertEqual( _lowerCamelCase , [ [ { """generated_text""": ( """This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵""" """ please,""" ) } ], [ { """generated_text""": ( """This is a second test Chieftain Chieftain prefecture prefecture prefecture Cannes Cannes""" """ Cannes 閲閲Cannes Cannes Cannes 攵 please,""" ) } ], ] , ) def _a (self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): """simple docstring""" UpperCAmelCase__ : int = TextGenerationPipeline(model=_lowerCamelCase , tokenizer=_lowerCamelCase ) return text_generator, ["This is a test", "Another test"] def _a (self ): """simple docstring""" UpperCAmelCase__ : Tuple = """Hello I believe in""" UpperCAmelCase__ : Optional[int] = pipeline("""text-generation""" , model="""hf-internal-testing/tiny-random-gpt2""" ) UpperCAmelCase__ : Any = text_generator(_lowerCamelCase ) self.assertEqual( _lowerCamelCase , [{"""generated_text""": """Hello I believe in fe fe fe fe fe fe fe fe fe fe fe fe"""}] , ) UpperCAmelCase__ : int = text_generator(_lowerCamelCase , stop_sequence=""" fe""" ) self.assertEqual(_lowerCamelCase , [{"""generated_text""": """Hello I believe in fe"""}] ) def _a (self , _lowerCamelCase , _lowerCamelCase ): """simple docstring""" UpperCAmelCase__ : Optional[int] = text_generator.model UpperCAmelCase__ : Union[str, Any] = text_generator.tokenizer UpperCAmelCase__ : Any = text_generator("""This is a test""" ) self.assertEqual(_lowerCamelCase , [{"""generated_text""": ANY(_lowerCamelCase )}] ) self.assertTrue(outputs[0]["""generated_text"""].startswith("""This is a test""" ) ) UpperCAmelCase__ : List[Any] = text_generator("""This is a test""" , return_full_text=_lowerCamelCase ) self.assertEqual(_lowerCamelCase , [{"""generated_text""": ANY(_lowerCamelCase )}] ) self.assertNotIn("""This is a test""" , outputs[0]["""generated_text"""] ) UpperCAmelCase__ : int = pipeline(task="""text-generation""" , model=_lowerCamelCase , tokenizer=_lowerCamelCase , return_full_text=_lowerCamelCase ) UpperCAmelCase__ : Dict = text_generator("""This is a test""" ) self.assertEqual(_lowerCamelCase , [{"""generated_text""": ANY(_lowerCamelCase )}] ) self.assertNotIn("""This is a test""" , outputs[0]["""generated_text"""] ) UpperCAmelCase__ : Optional[Any] = text_generator("""This is a test""" , return_full_text=_lowerCamelCase ) self.assertEqual(_lowerCamelCase , [{"""generated_text""": ANY(_lowerCamelCase )}] ) self.assertTrue(outputs[0]["""generated_text"""].startswith("""This is a test""" ) ) UpperCAmelCase__ : Union[str, Any] = text_generator(["""This is great !""", """Something else"""] , num_return_sequences=2 , do_sample=_lowerCamelCase ) self.assertEqual( _lowerCamelCase , [ [{"""generated_text""": ANY(_lowerCamelCase )}, {"""generated_text""": ANY(_lowerCamelCase )}], [{"""generated_text""": ANY(_lowerCamelCase )}, {"""generated_text""": ANY(_lowerCamelCase )}], ] , ) if text_generator.tokenizer.pad_token is not None: UpperCAmelCase__ : Union[str, Any] = text_generator( ["""This is great !""", """Something else"""] , num_return_sequences=2 , batch_size=2 , do_sample=_lowerCamelCase ) self.assertEqual( _lowerCamelCase , [ [{"""generated_text""": ANY(_lowerCamelCase )}, {"""generated_text""": ANY(_lowerCamelCase )}], [{"""generated_text""": ANY(_lowerCamelCase )}, {"""generated_text""": ANY(_lowerCamelCase )}], ] , ) with self.assertRaises(_lowerCamelCase ): UpperCAmelCase__ : List[Any] = text_generator("""test""" , return_full_text=_lowerCamelCase , return_text=_lowerCamelCase ) with self.assertRaises(_lowerCamelCase ): UpperCAmelCase__ : Optional[Any] = text_generator("""test""" , return_full_text=_lowerCamelCase , return_tensors=_lowerCamelCase ) with self.assertRaises(_lowerCamelCase ): UpperCAmelCase__ : Any = text_generator("""test""" , return_text=_lowerCamelCase , return_tensors=_lowerCamelCase ) # Empty prompt is slighly special # it requires BOS token to exist. # Special case for Pegasus which will always append EOS so will # work even without BOS. if ( text_generator.tokenizer.bos_token_id is not None or "Pegasus" in tokenizer.__class__.__name__ or "Git" in model.__class__.__name__ ): UpperCAmelCase__ : Dict = text_generator("""""" ) self.assertEqual(_lowerCamelCase , [{"""generated_text""": ANY(_lowerCamelCase )}] ) else: with self.assertRaises((ValueError, AssertionError) ): UpperCAmelCase__ : str = text_generator("""""" ) if text_generator.framework == "tf": # TF generation does not support max_new_tokens, and it's impossible # to control long generation with only max_length without # fancy calculation, dismissing tests for now. return # We don't care about infinite range models. # They already work. # Skip this test for XGLM, since it uses sinusoidal positional embeddings which are resized on-the-fly. UpperCAmelCase__ : Tuple = ["""RwkvForCausalLM""", """XGLMForCausalLM""", """GPTNeoXForCausalLM"""] if ( tokenizer.model_max_length < 10000 and text_generator.model.__class__.__name__ not in EXTRA_MODELS_CAN_HANDLE_LONG_INPUTS ): # Handling of large generations with self.assertRaises((RuntimeError, IndexError, ValueError, AssertionError) ): text_generator("""This is a test""" * 500 , max_new_tokens=20 ) UpperCAmelCase__ : str = text_generator("""This is a test""" * 500 , handle_long_generation="""hole""" , max_new_tokens=20 ) # Hole strategy cannot work with self.assertRaises(_lowerCamelCase ): text_generator( """This is a test""" * 500 , handle_long_generation="""hole""" , max_new_tokens=tokenizer.model_max_length + 10 , ) @require_torch @require_accelerate @require_torch_gpu def _a (self ): """simple docstring""" import torch # Classic `model_kwargs` UpperCAmelCase__ : str = pipeline( model="""hf-internal-testing/tiny-random-bloom""" , model_kwargs={"""device_map""": """auto""", """torch_dtype""": torch.bfloataa} , ) self.assertEqual(pipe.model.device , torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype , torch.bfloataa ) UpperCAmelCase__ : List[str] = pipe("""This is a test""" ) self.assertEqual( _lowerCamelCase , [ { """generated_text""": ( """This is a test test test test test test test test test test test test test test test test""" """ test""" ) } ] , ) # Upgraded those two to real pipeline arguments (they just get sent for the model as they're unlikely to mean anything else.) UpperCAmelCase__ : int = pipeline(model="""hf-internal-testing/tiny-random-bloom""" , device_map="""auto""" , torch_dtype=torch.bfloataa ) self.assertEqual(pipe.model.device , torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype , torch.bfloataa ) UpperCAmelCase__ : Any = pipe("""This is a test""" ) self.assertEqual( _lowerCamelCase , [ { """generated_text""": ( """This is a test test test test test test test test test test test test test test test test""" """ test""" ) } ] , ) # torch_dtype will be automatically set to float32 if not provided - check: https://github.com/huggingface/transformers/pull/20602 UpperCAmelCase__ : Optional[Any] = pipeline(model="""hf-internal-testing/tiny-random-bloom""" , device_map="""auto""" ) self.assertEqual(pipe.model.device , torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype , torch.floataa ) UpperCAmelCase__ : Optional[int] = pipe("""This is a test""" ) self.assertEqual( _lowerCamelCase , [ { """generated_text""": ( """This is a test test test test test test test test test test test test test test test test""" """ test""" ) } ] , ) @require_torch @require_torch_gpu def _a (self ): """simple docstring""" import torch UpperCAmelCase__ : Any = pipeline(model="""hf-internal-testing/tiny-random-bloom""" , device=0 , torch_dtype=torch.floataa ) pipe("""This is a test""" ) @require_torch @require_accelerate @require_torch_gpu def _a (self ): """simple docstring""" import torch UpperCAmelCase__ : Any = pipeline(model="""hf-internal-testing/tiny-random-bloom""" , device_map="""auto""" , torch_dtype=torch.floataa ) pipe("""This is a test""" , do_sample=_lowerCamelCase , top_p=0.5 ) def _a (self ): """simple docstring""" UpperCAmelCase__ : Optional[Any] = """Hello world""" UpperCAmelCase__ : str = pipeline("""text-generation""" , model="""hf-internal-testing/tiny-random-gpt2""" ) if text_generator.model.framework == "tf": UpperCAmelCase__ : Any = logging.get_logger("""transformers.generation.tf_utils""" ) else: UpperCAmelCase__ : Union[str, Any] = logging.get_logger("""transformers.generation.utils""" ) UpperCAmelCase__ : Optional[int] = """Both `max_new_tokens`""" # The beggining of the message to be checked in this test # Both are set by the user -> log warning with CaptureLogger(_lowerCamelCase ) as cl: UpperCAmelCase__ : List[str] = text_generator(_lowerCamelCase , max_length=10 , max_new_tokens=1 ) self.assertIn(_lowerCamelCase , cl.out ) # The user only sets one -> no warning with CaptureLogger(_lowerCamelCase ) as cl: UpperCAmelCase__ : Any = text_generator(_lowerCamelCase , max_new_tokens=1 ) self.assertNotIn(_lowerCamelCase , cl.out ) with CaptureLogger(_lowerCamelCase ) as cl: UpperCAmelCase__ : Optional[Any] = text_generator(_lowerCamelCase , max_length=10 ) self.assertNotIn(_lowerCamelCase , cl.out )
171
0
"""simple docstring""" def lowercase (SCREAMING_SNAKE_CASE_ : str ) -> str: SCREAMING_SNAKE_CASE = 0 # if input_string is "aba" than new_input_string become "a|b|a" SCREAMING_SNAKE_CASE = '' SCREAMING_SNAKE_CASE = '' # append each character + "|" in new_string for range(0, length-1) for i in input_string[: len(SCREAMING_SNAKE_CASE_ ) - 1]: new_input_string += i + "|" # append last character new_input_string += input_string[-1] # we will store the starting and ending of previous furthest ending palindromic # substring SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = 0, 0 # length[i] shows the length of palindromic substring with center i SCREAMING_SNAKE_CASE = [1 for i in range(len(SCREAMING_SNAKE_CASE_ ) )] # for each character in new_string find corresponding palindromic string SCREAMING_SNAKE_CASE = 0 for j in range(len(SCREAMING_SNAKE_CASE_ ) ): SCREAMING_SNAKE_CASE = 1 if j > r else min(length[l + r - j] // 2 , r - j + 1 ) while ( j - k >= 0 and j + k < len(SCREAMING_SNAKE_CASE_ ) and new_input_string[k + j] == new_input_string[j - k] ): k += 1 SCREAMING_SNAKE_CASE = 2 * k - 1 # does this string is ending after the previously explored end (that is r) ? # if yes the update the new r to the last index of this if j + k - 1 > r: SCREAMING_SNAKE_CASE = j - k + 1 # noqa: E741 SCREAMING_SNAKE_CASE = j + k - 1 # update max_length and start position if max_length < length[j]: SCREAMING_SNAKE_CASE = length[j] SCREAMING_SNAKE_CASE = j # create that string SCREAMING_SNAKE_CASE = new_input_string[start - max_length // 2 : start + max_length // 2 + 1] for i in s: if i != "|": output_string += i return output_string if __name__ == "__main__": import doctest doctest.testmod()
353
"""simple docstring""" import argparse import json import os from collections import OrderedDict import torch from transformers import LukeConfig, LukeForMaskedLM, MLukeTokenizer, XLMRobertaTokenizer from transformers.tokenization_utils_base import AddedToken @torch.no_grad() def lowercase (SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : str ) -> Any: # Load configuration defined in the metadata file with open(SCREAMING_SNAKE_CASE_ ) as metadata_file: SCREAMING_SNAKE_CASE = json.load(SCREAMING_SNAKE_CASE_ ) SCREAMING_SNAKE_CASE = LukeConfig(use_entity_aware_attention=SCREAMING_SNAKE_CASE_ , **metadata['model_config'] ) # Load in the weights from the checkpoint_path SCREAMING_SNAKE_CASE = torch.load(SCREAMING_SNAKE_CASE_ , map_location='cpu' )['module'] # Load the entity vocab file SCREAMING_SNAKE_CASE = load_original_entity_vocab(SCREAMING_SNAKE_CASE_ ) # add an entry for [MASK2] SCREAMING_SNAKE_CASE = max(entity_vocab.values() ) + 1 config.entity_vocab_size += 1 SCREAMING_SNAKE_CASE = XLMRobertaTokenizer.from_pretrained(metadata['model_config']['bert_model_name'] ) # Add special tokens to the token vocabulary for downstream tasks SCREAMING_SNAKE_CASE = AddedToken('<ent>' , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) SCREAMING_SNAKE_CASE = AddedToken('<ent2>' , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) tokenizer.add_special_tokens({'additional_special_tokens': [entity_token_a, entity_token_a]} ) config.vocab_size += 2 print(F'Saving tokenizer to {pytorch_dump_folder_path}' ) tokenizer.save_pretrained(SCREAMING_SNAKE_CASE_ ) with open(os.path.join(SCREAMING_SNAKE_CASE_ , 'tokenizer_config.json' ) , 'r' ) as f: SCREAMING_SNAKE_CASE = json.load(SCREAMING_SNAKE_CASE_ ) SCREAMING_SNAKE_CASE = 'MLukeTokenizer' with open(os.path.join(SCREAMING_SNAKE_CASE_ , 'tokenizer_config.json' ) , 'w' ) as f: json.dump(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) with open(os.path.join(SCREAMING_SNAKE_CASE_ , MLukeTokenizer.vocab_files_names['entity_vocab_file'] ) , 'w' ) as f: json.dump(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) SCREAMING_SNAKE_CASE = MLukeTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_ ) # Initialize the embeddings of the special tokens SCREAMING_SNAKE_CASE = tokenizer.convert_tokens_to_ids(['@'] )[0] SCREAMING_SNAKE_CASE = tokenizer.convert_tokens_to_ids(['#'] )[0] SCREAMING_SNAKE_CASE = state_dict['embeddings.word_embeddings.weight'] SCREAMING_SNAKE_CASE = word_emb[ent_init_index].unsqueeze(0 ) SCREAMING_SNAKE_CASE = word_emb[enta_init_index].unsqueeze(0 ) SCREAMING_SNAKE_CASE = torch.cat([word_emb, ent_emb, enta_emb] ) # add special tokens for 'entity_predictions.bias' for bias_name in ["lm_head.decoder.bias", "lm_head.bias"]: SCREAMING_SNAKE_CASE = state_dict[bias_name] SCREAMING_SNAKE_CASE = decoder_bias[ent_init_index].unsqueeze(0 ) SCREAMING_SNAKE_CASE = decoder_bias[enta_init_index].unsqueeze(0 ) SCREAMING_SNAKE_CASE = torch.cat([decoder_bias, ent_decoder_bias, enta_decoder_bias] ) # Initialize the query layers of the entity-aware self-attention mechanism for layer_index in range(config.num_hidden_layers ): for matrix_name in ["query.weight", "query.bias"]: SCREAMING_SNAKE_CASE = F'encoder.layer.{layer_index}.attention.self.' SCREAMING_SNAKE_CASE = state_dict[prefix + matrix_name] SCREAMING_SNAKE_CASE = state_dict[prefix + matrix_name] SCREAMING_SNAKE_CASE = state_dict[prefix + matrix_name] # Initialize the embedding of the [MASK2] entity using that of the [MASK] entity for downstream tasks SCREAMING_SNAKE_CASE = state_dict['entity_embeddings.entity_embeddings.weight'] SCREAMING_SNAKE_CASE = entity_emb[entity_vocab['[MASK]']].unsqueeze(0 ) SCREAMING_SNAKE_CASE = torch.cat([entity_emb, entity_mask_emb] ) # add [MASK2] for 'entity_predictions.bias' SCREAMING_SNAKE_CASE = state_dict['entity_predictions.bias'] SCREAMING_SNAKE_CASE = entity_prediction_bias[entity_vocab['[MASK]']].unsqueeze(0 ) SCREAMING_SNAKE_CASE = torch.cat([entity_prediction_bias, entity_mask_bias] ) SCREAMING_SNAKE_CASE = LukeForMaskedLM(config=SCREAMING_SNAKE_CASE_ ).eval() state_dict.pop('entity_predictions.decoder.weight' ) state_dict.pop('lm_head.decoder.weight' ) state_dict.pop('lm_head.decoder.bias' ) SCREAMING_SNAKE_CASE = OrderedDict() for key, value in state_dict.items(): if not (key.startswith('lm_head' ) or key.startswith('entity_predictions' )): SCREAMING_SNAKE_CASE = state_dict[key] else: SCREAMING_SNAKE_CASE = state_dict[key] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = model.load_state_dict(SCREAMING_SNAKE_CASE_ , strict=SCREAMING_SNAKE_CASE_ ) if set(SCREAMING_SNAKE_CASE_ ) != {"luke.embeddings.position_ids"}: raise ValueError(F'Unexpected unexpected_keys: {unexpected_keys}' ) if set(SCREAMING_SNAKE_CASE_ ) != { "lm_head.decoder.weight", "lm_head.decoder.bias", "entity_predictions.decoder.weight", }: raise ValueError(F'Unexpected missing_keys: {missing_keys}' ) model.tie_weights() assert (model.luke.embeddings.word_embeddings.weight == model.lm_head.decoder.weight).all() assert (model.luke.entity_embeddings.entity_embeddings.weight == model.entity_predictions.decoder.weight).all() # Check outputs SCREAMING_SNAKE_CASE = MLukeTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_ , task='entity_classification' ) SCREAMING_SNAKE_CASE = 'ISO 639-3 uses the code fas for the dialects spoken across Iran and アフガニスタン (Afghanistan).' SCREAMING_SNAKE_CASE = (0, 9) SCREAMING_SNAKE_CASE = tokenizer(SCREAMING_SNAKE_CASE_ , entity_spans=[span] , return_tensors='pt' ) SCREAMING_SNAKE_CASE = model(**SCREAMING_SNAKE_CASE_ ) # Verify word hidden states if model_size == "large": raise NotImplementedError else: # base SCREAMING_SNAKE_CASE = torch.Size((1, 33, 7_68) ) SCREAMING_SNAKE_CASE = torch.tensor([[0.08_92, 0.05_96, -0.28_19], [0.01_34, 0.11_99, 0.05_73], [-0.01_69, 0.09_27, 0.06_44]] ) if not (outputs.last_hidden_state.shape == expected_shape): raise ValueError( F'Outputs.last_hidden_state.shape is {outputs.last_hidden_state.shape}, Expected shape is {expected_shape}' ) if not torch.allclose(outputs.last_hidden_state[0, :3, :3] , SCREAMING_SNAKE_CASE_ , atol=1E-4 ): raise ValueError # Verify entity hidden states if model_size == "large": raise NotImplementedError else: # base SCREAMING_SNAKE_CASE = torch.Size((1, 1, 7_68) ) SCREAMING_SNAKE_CASE = torch.tensor([[-0.14_82, 0.06_09, 0.03_22]] ) if not (outputs.entity_last_hidden_state.shape == expected_shape): raise ValueError( F'Outputs.entity_last_hidden_state.shape is {outputs.entity_last_hidden_state.shape}, Expected shape is' F' {expected_shape}' ) if not torch.allclose(outputs.entity_last_hidden_state[0, :3, :3] , SCREAMING_SNAKE_CASE_ , atol=1E-4 ): raise ValueError # Verify masked word/entity prediction SCREAMING_SNAKE_CASE = MLukeTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_ ) SCREAMING_SNAKE_CASE = 'Tokyo is the capital of <mask>.' SCREAMING_SNAKE_CASE = (24, 30) SCREAMING_SNAKE_CASE = tokenizer(SCREAMING_SNAKE_CASE_ , entity_spans=[span] , return_tensors='pt' ) SCREAMING_SNAKE_CASE = model(**SCREAMING_SNAKE_CASE_ ) SCREAMING_SNAKE_CASE = encoding['input_ids'][0].tolist() SCREAMING_SNAKE_CASE = input_ids.index(tokenizer.convert_tokens_to_ids('<mask>' ) ) SCREAMING_SNAKE_CASE = outputs.logits[0][mask_position_id].argmax(dim=-1 ) assert "Japan" == tokenizer.decode(SCREAMING_SNAKE_CASE_ ) SCREAMING_SNAKE_CASE = outputs.entity_logits[0][0].argmax().item() SCREAMING_SNAKE_CASE = [ entity for entity, entity_id in tokenizer.entity_vocab.items() if entity_id == predicted_entity_id ] assert [e for e in multilingual_predicted_entities if e.startswith('en:' )][0] == "en:Japan" # Finally, save our PyTorch model and tokenizer print('Saving PyTorch model to {}'.format(SCREAMING_SNAKE_CASE_ ) ) model.save_pretrained(SCREAMING_SNAKE_CASE_ ) def lowercase (SCREAMING_SNAKE_CASE_ : int ) -> int: SCREAMING_SNAKE_CASE = ['[MASK]', '[PAD]', '[UNK]'] SCREAMING_SNAKE_CASE = [json.loads(SCREAMING_SNAKE_CASE_ ) for line in open(SCREAMING_SNAKE_CASE_ )] SCREAMING_SNAKE_CASE = {} for entry in data: SCREAMING_SNAKE_CASE = entry['id'] for entity_name, language in entry["entities"]: if entity_name in SPECIAL_TOKENS: SCREAMING_SNAKE_CASE = entity_id break SCREAMING_SNAKE_CASE = F'{language}:{entity_name}' SCREAMING_SNAKE_CASE = entity_id return new_mapping if __name__ == "__main__": __UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument('''--checkpoint_path''', type=str, help='''Path to a pytorch_model.bin file.''') parser.add_argument( '''--metadata_path''', default=None, type=str, help='''Path to a metadata.json file, defining the configuration.''' ) parser.add_argument( '''--entity_vocab_path''', default=None, type=str, help='''Path to an entity_vocab.tsv file, containing the entity vocabulary.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to where to dump the output PyTorch model.''' ) parser.add_argument( '''--model_size''', default='''base''', type=str, choices=['''base''', '''large'''], help='''Size of the model to be converted.''' ) __UpperCamelCase = parser.parse_args() convert_luke_checkpoint( args.checkpoint_path, args.metadata_path, args.entity_vocab_path, args.pytorch_dump_folder_path, args.model_size, )
38
0
import unittest import numpy as np from transformers import AlbertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.albert.modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, ) class __lowerCAmelCase ( unittest.TestCase ): def __init__( self , _snake_case , _snake_case=13 , _snake_case=7 , _snake_case=True , _snake_case=True , _snake_case=True , _snake_case=True , _snake_case=99 , _snake_case=32 , _snake_case=5 , _snake_case=4 , _snake_case=37 , _snake_case="gelu" , _snake_case=0.1 , _snake_case=0.1 , _snake_case=512 , _snake_case=16 , _snake_case=2 , _snake_case=0.02 , _snake_case=4 , ): """simple docstring""" _lowerCAmelCase = parent _lowerCAmelCase = batch_size _lowerCAmelCase = seq_length _lowerCAmelCase = is_training _lowerCAmelCase = use_attention_mask _lowerCAmelCase = use_token_type_ids _lowerCAmelCase = use_labels _lowerCAmelCase = vocab_size _lowerCAmelCase = hidden_size _lowerCAmelCase = num_hidden_layers _lowerCAmelCase = num_attention_heads _lowerCAmelCase = intermediate_size _lowerCAmelCase = hidden_act _lowerCAmelCase = hidden_dropout_prob _lowerCAmelCase = attention_probs_dropout_prob _lowerCAmelCase = max_position_embeddings _lowerCAmelCase = type_vocab_size _lowerCAmelCase = type_sequence_label_size _lowerCAmelCase = initializer_range _lowerCAmelCase = num_choices def snake_case ( self ): """simple docstring""" _lowerCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _lowerCAmelCase = None if self.use_attention_mask: _lowerCAmelCase = random_attention_mask([self.batch_size, self.seq_length] ) _lowerCAmelCase = None if self.use_token_type_ids: _lowerCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _lowerCAmelCase = AlbertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=SCREAMING_SNAKE_CASE_ , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def snake_case ( self ): """simple docstring""" _lowerCAmelCase = self.prepare_config_and_inputs() _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase = config_and_inputs _lowerCAmelCase = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict @require_flax class __lowerCAmelCase ( lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def snake_case ( self ): """simple docstring""" _lowerCAmelCase = FlaxAlbertModelTester(self ) @slow def snake_case ( self ): """simple docstring""" for model_class_name in self.all_model_classes: _lowerCAmelCase = model_class_name.from_pretrained("""albert-base-v2""" ) _lowerCAmelCase = model(np.ones((1, 1) ) ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) @require_flax class __lowerCAmelCase ( unittest.TestCase ): @slow def snake_case ( self ): """simple docstring""" _lowerCAmelCase = FlaxAlbertModel.from_pretrained("""albert-base-v2""" ) _lowerCAmelCase = np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]] ) _lowerCAmelCase = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) _lowerCAmelCase = model(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ )[0] _lowerCAmelCase = (1, 11, 768) self.assertEqual(output.shape , SCREAMING_SNAKE_CASE_ ) _lowerCAmelCase = np.array( [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]] ) self.assertTrue(jnp.allclose(output[:, 1:4, 1:4] , SCREAMING_SNAKE_CASE_ , atol=1e-4 ) )
82
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_torch_available, ) __a = { 'configuration_trocr': ['TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP', 'TrOCRConfig'], 'processing_trocr': ['TrOCRProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ 'TROCR_PRETRAINED_MODEL_ARCHIVE_LIST', 'TrOCRForCausalLM', 'TrOCRPreTrainedModel', ] if TYPE_CHECKING: from .configuration_trocr import TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP, TrOCRConfig from .processing_trocr import TrOCRProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_trocr import TROCR_PRETRAINED_MODEL_ARCHIVE_LIST, TrOCRForCausalLM, TrOCRPreTrainedModel else: import sys __a = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
30
0
"""simple docstring""" import functools def lowercase__ ( lowercase_ ,lowercase_ ) -> Optional[int]: """simple docstring""" _UpperCamelCase : List[str] = len(lowerCamelCase_ ) _UpperCamelCase : Dict = len(lowerCamelCase_ ) @functools.cache def min_distance(lowercase_ ,lowercase_ ) -> int: # if first word index is overflow - delete all from the second word if indexa >= len_worda: return len_worda - indexa # if second word index is overflow - delete all from the first word if indexa >= len_worda: return len_worda - indexa _UpperCamelCase : Union[str, Any] = int(worda[indexa] != worda[indexa] ) # current letters not identical return min( 1 + min_distance(indexa + 1 ,lowerCamelCase_ ) ,1 + min_distance(lowerCamelCase_ ,indexa + 1 ) ,diff + min_distance(indexa + 1 ,indexa + 1 ) ,) return min_distance(0 ,0 ) if __name__ == "__main__": import doctest doctest.testmod()
358
"""simple docstring""" from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = { "facebook/xlm-roberta-xl": "https://huggingface.co/facebook/xlm-roberta-xl/resolve/main/config.json", "facebook/xlm-roberta-xxl": "https://huggingface.co/facebook/xlm-roberta-xxl/resolve/main/config.json", # See all XLM-RoBERTa-XL models at https://huggingface.co/models?filter=xlm-roberta-xl } class __SCREAMING_SNAKE_CASE ( _UpperCamelCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ :List[Any] = "xlm-roberta-xl" def __init__( self : Any , __a : Tuple=25_0880 , __a : Optional[Any]=2560 , __a : List[str]=36 , __a : Any=32 , __a : Dict=1_0240 , __a : Optional[Any]="gelu" , __a : int=0.1 , __a : Tuple=0.1 , __a : str=514 , __a : Any=1 , __a : List[Any]=0.02 , __a : List[str]=1e-0_5 , __a : Optional[Any]=1 , __a : List[Any]=0 , __a : Tuple=2 , __a : int="absolute" , __a : Dict=True , __a : Dict=None , **__a : Tuple , ) -> str: super().__init__(pad_token_id=__a , bos_token_id=__a , eos_token_id=__a , **__a ) _UpperCamelCase : Any = vocab_size _UpperCamelCase : Optional[int] = hidden_size _UpperCamelCase : str = num_hidden_layers _UpperCamelCase : Optional[int] = num_attention_heads _UpperCamelCase : List[str] = hidden_act _UpperCamelCase : Union[str, Any] = intermediate_size _UpperCamelCase : str = hidden_dropout_prob _UpperCamelCase : str = attention_probs_dropout_prob _UpperCamelCase : Dict = max_position_embeddings _UpperCamelCase : Optional[Any] = type_vocab_size _UpperCamelCase : str = initializer_range _UpperCamelCase : Any = layer_norm_eps _UpperCamelCase : Any = position_embedding_type _UpperCamelCase : Union[str, Any] = use_cache _UpperCamelCase : Optional[Any] = classifier_dropout class __SCREAMING_SNAKE_CASE ( _UpperCamelCase ): '''simple docstring''' @property def __SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": _UpperCamelCase : Any = {0: "batch", 1: "choice", 2: "sequence"} else: _UpperCamelCase : Dict = {0: "batch", 1: "sequence"} return OrderedDict( [ ("input_ids", dynamic_axis), ("attention_mask", dynamic_axis), ] )
310
0
'''simple docstring''' 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''': 6_5_0, '''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''': 6_0_0, '''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''': 6_0_0, '''eval_accuracy''': 0.6, '''eval_loss''': 0.7}, }, ] ) class lowerCAmelCase_( unittest.TestCase ): '''simple docstring''' def UpperCAmelCase_ ( self ) -> Optional[Any]: 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=__UpperCAmelCase ,) assert hasattr(self ,"""env""" ) def UpperCAmelCase_ ( self ,__UpperCAmelCase ) -> Optional[Any]: lowerCAmelCase__ : Optional[int] = F"""{self.env.base_job_name}-{instance_count}-{'ddp' if 'ddp' in self.script else 'smd'}""" # distributed data settings lowerCAmelCase__ : 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=__UpperCAmelCase ,instance_count=__UpperCAmelCase ,instance_type=self.instance_type ,debugger_hook_config=__UpperCAmelCase ,hyperparameters={**self.env.distributed_hyperparameters, """model_name_or_path""": self.model_name_or_path} ,metric_definitions=self.env.metric_definitions ,distribution=__UpperCAmelCase ,py_version="""py36""" ,) def UpperCAmelCase_ ( self ,__UpperCAmelCase ) -> Optional[Any]: TrainingJobAnalytics(__UpperCAmelCase ).export_csv(F"""{self.env.test_path}/{job_name}_metrics.csv""" ) @parameterized.expand([(2,)] ) def UpperCAmelCase_ ( self ,__UpperCAmelCase ) -> Any: # create estimator lowerCAmelCase__ : List[Any] = self.create_estimator(__UpperCAmelCase ) # run training estimator.fit() # result dataframe lowerCAmelCase__ : Any = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis lowerCAmelCase__ : int = list(result_metrics_df[result_metrics_df.metric_name == """eval_accuracy"""]["""value"""] ) lowerCAmelCase__ : List[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__ : List[str] = ( Session().describe_training_job(estimator.latest_training_job.name ).get("""TrainingTimeInSeconds""" ,99_9999 ) ) # 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} ,__UpperCAmelCase )
37
'''simple docstring''' 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''': 6_5_0, '''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''': 6_0_0, '''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''': 6_0_0, '''eval_accuracy''': 0.6, '''eval_loss''': 0.7}, }, ] ) class lowerCAmelCase_( unittest.TestCase ): '''simple docstring''' def UpperCAmelCase_ ( self ) -> Optional[Any]: 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=__UpperCAmelCase ,) assert hasattr(self ,"""env""" ) def UpperCAmelCase_ ( self ,__UpperCAmelCase ) -> Optional[Any]: lowerCAmelCase__ : Optional[int] = F"""{self.env.base_job_name}-{instance_count}-{'ddp' if 'ddp' in self.script else 'smd'}""" # distributed data settings lowerCAmelCase__ : 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=__UpperCAmelCase ,instance_count=__UpperCAmelCase ,instance_type=self.instance_type ,debugger_hook_config=__UpperCAmelCase ,hyperparameters={**self.env.distributed_hyperparameters, """model_name_or_path""": self.model_name_or_path} ,metric_definitions=self.env.metric_definitions ,distribution=__UpperCAmelCase ,py_version="""py36""" ,) def UpperCAmelCase_ ( self ,__UpperCAmelCase ) -> Optional[Any]: TrainingJobAnalytics(__UpperCAmelCase ).export_csv(F"""{self.env.test_path}/{job_name}_metrics.csv""" ) @parameterized.expand([(2,)] ) def UpperCAmelCase_ ( self ,__UpperCAmelCase ) -> Any: # create estimator lowerCAmelCase__ : List[Any] = self.create_estimator(__UpperCAmelCase ) # run training estimator.fit() # result dataframe lowerCAmelCase__ : Any = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis lowerCAmelCase__ : int = list(result_metrics_df[result_metrics_df.metric_name == """eval_accuracy"""]["""value"""] ) lowerCAmelCase__ : List[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__ : List[str] = ( Session().describe_training_job(estimator.latest_training_job.name ).get("""TrainingTimeInSeconds""" ,99_9999 ) ) # 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} ,__UpperCAmelCase )
37
1
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { "andreasmadsen/efficient_mlm_m0.40": ( "https://huggingface.co/andreasmadsen/efficient_mlm_m0.40/resolve/main/config.json" ), } class __lowerCAmelCase ( A ): UpperCamelCase = '''roberta-prelayernorm''' def __init__( self : Union[str, Any] , A : List[Any]=5_02_65 , A : Optional[int]=7_68 , A : List[str]=12 , A : List[str]=12 , A : Tuple=30_72 , A : int="gelu" , A : Optional[int]=0.1 , A : Optional[Any]=0.1 , A : Dict=5_12 , A : List[Any]=2 , A : Any=0.0_2 , A : Tuple=1E-12 , A : Tuple=1 , A : Union[str, Any]=0 , A : List[str]=2 , A : Optional[int]="absolute" , A : Tuple=True , A : List[Any]=None , **A : Any , ) -> Union[str, Any]: """simple docstring""" super().__init__(pad_token_id=A , bos_token_id=A , eos_token_id=A , **A) _UpperCAmelCase = vocab_size _UpperCAmelCase = hidden_size _UpperCAmelCase = num_hidden_layers _UpperCAmelCase = num_attention_heads _UpperCAmelCase = hidden_act _UpperCAmelCase = intermediate_size _UpperCAmelCase = hidden_dropout_prob _UpperCAmelCase = attention_probs_dropout_prob _UpperCAmelCase = max_position_embeddings _UpperCAmelCase = type_vocab_size _UpperCAmelCase = initializer_range _UpperCAmelCase = layer_norm_eps _UpperCAmelCase = position_embedding_type _UpperCAmelCase = use_cache _UpperCAmelCase = classifier_dropout class __lowerCAmelCase ( A ): @property def _lowerCamelCase ( self : int) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task == "multiple-choice": _UpperCAmelCase = {0: 'batch', 1: 'choice', 2: 'sequence'} else: _UpperCAmelCase = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ])
365
import json import os import subprocess import unittest from ast import literal_eval import pytest from parameterized import 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.g4dn.xlarge''', '''results''': {'''train_runtime''': 6_5_0, '''eval_accuracy''': 0.6, '''eval_loss''': 0.9}, }, { '''framework''': '''tensorflow''', '''script''': '''run_tf.py''', '''model_name_or_path''': '''distilbert-base-cased''', '''instance_type''': '''ml.g4dn.xlarge''', '''results''': {'''train_runtime''': 6_0_0, '''eval_accuracy''': 0.3, '''eval_loss''': 0.9}, }, ] ) class __lowerCAmelCase ( unittest.TestCase ): def _lowerCamelCase ( self : List[Any]) -> Any: """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=A , ) assert hasattr(self , 'env') def _lowerCamelCase ( self : Any , A : Tuple=1) -> List[str]: """simple docstring""" 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=F"{self.env.base_job_name}-single" , instance_count=A , instance_type=self.instance_type , debugger_hook_config=A , hyperparameters={**self.env.hyperparameters, 'model_name_or_path': self.model_name_or_path} , metric_definitions=self.env.metric_definitions , py_version='py36' , ) def _lowerCamelCase ( self : Dict , A : int) -> str: """simple docstring""" TrainingJobAnalytics(A).export_csv(F"{self.env.test_path}/{job_name}_metrics.csv") def _lowerCamelCase ( self : Union[str, Any]) -> Dict: """simple docstring""" _UpperCAmelCase = self.create_estimator() # run training estimator.fit() # result dataframe _UpperCAmelCase = TrainingJobAnalytics(estimator.latest_training_job.name).dataframe() # extract kpis _UpperCAmelCase = list(result_metrics_df[result_metrics_df.metric_name == 'eval_accuracy']['value']) _UpperCAmelCase = list(result_metrics_df[result_metrics_df.metric_name == 'eval_loss']['value']) # get train time from SageMaker job, this includes starting, preprocessing, stopping _UpperCAmelCase = ( 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} , A)
290
0
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 = { '''google/bit-50''': '''https://huggingface.co/google/bit-50/resolve/main/config.json''', } class __a ( __UpperCamelCase , __UpperCamelCase ): __lowercase : str = 'bit' __lowercase : List[str] = ['preactivation', 'bottleneck'] __lowercase : List[Any] = ['SAME', 'VALID'] def __init__( self , lowerCAmelCase__=3 , lowerCAmelCase__=64 , lowerCAmelCase__=[256, 512, 1_024, 2_048] , lowerCAmelCase__=[3, 4, 6, 3] , lowerCAmelCase__="preactivation" , lowerCAmelCase__="relu" , lowerCAmelCase__=None , lowerCAmelCase__=32 , lowerCAmelCase__=0.0 , lowerCAmelCase__=False , lowerCAmelCase__=32 , lowerCAmelCase__=1 , lowerCAmelCase__=None , lowerCAmelCase__=None , **lowerCAmelCase__ , ) -> List[str]: '''simple docstring''' super().__init__(**lowerCAmelCase__ ) if layer_type not in self.layer_types: raise ValueError(F'layer_type={layer_type} is not one of {",".join(self.layer_types )}' ) if global_padding is not None: if global_padding.upper() in self.supported_padding: lowercase__: Any = global_padding.upper() else: raise ValueError(F'Padding strategy {global_padding} not supported' ) lowercase__: Optional[int] = num_channels lowercase__: str = embedding_size lowercase__: Optional[Any] = hidden_sizes lowercase__: Dict = depths lowercase__: Optional[int] = layer_type lowercase__: str = hidden_act lowercase__: str = global_padding lowercase__: str = num_groups lowercase__: int = drop_path_rate lowercase__: Tuple = embedding_dynamic_padding lowercase__: Tuple = output_stride lowercase__: Dict = width_factor lowercase__: Optional[int] = ['stem'] + [F'stage{idx}' for idx in range(1 , len(lowerCAmelCase__ ) + 1 )] lowercase__ , lowercase__: Tuple = get_aligned_output_features_output_indices( out_features=lowerCAmelCase__ , out_indices=lowerCAmelCase__ , stage_names=self.stage_names )
196
import numpy as np import torch from torch.utils.data import Dataset from utils import logger class __a ( __UpperCamelCase ): def __init__( self , lowerCAmelCase__ , lowerCAmelCase__ ) -> Any: '''simple docstring''' lowercase__: Any = params lowercase__: List[Any] = np.array(lowerCAmelCase__ ) lowercase__: Optional[Any] = np.array([len(lowerCAmelCase__ ) for t in data] ) self.check() self.remove_long_sequences() self.remove_empty_sequences() self.remove_unknown_sequences() self.check() self.print_statistics() def __getitem__( self , lowerCAmelCase__ ) -> List[Any]: '''simple docstring''' return (self.token_ids[index], self.lengths[index]) def __len__( self ) -> List[Any]: '''simple docstring''' return len(self.lengths ) def SCREAMING_SNAKE_CASE__ ( self ) -> Tuple: '''simple docstring''' assert len(self.token_ids ) == len(self.lengths ) assert all(self.lengths[i] == len(self.token_ids[i] ) for i in range(len(self.lengths ) ) ) def SCREAMING_SNAKE_CASE__ ( self ) -> Union[str, Any]: '''simple docstring''' lowercase__: Any = self.params.max_model_input_size lowercase__: Dict = self.lengths > max_len logger.info(F'Splitting {sum(lowerCAmelCase__ )} too long sequences.' ) def divide_chunks(lowerCAmelCase__ , lowerCAmelCase__ ): return [l[i : i + n] for i in range(0 , len(lowerCAmelCase__ ) , lowerCAmelCase__ )] lowercase__: str = [] lowercase__: List[str] = [] if self.params.mlm: lowercase__ , lowercase__: str = self.params.special_tok_ids['cls_token'], self.params.special_tok_ids['sep_token'] else: lowercase__ , lowercase__: int = self.params.special_tok_ids['bos_token'], self.params.special_tok_ids['eos_token'] for seq_, len_ in zip(self.token_ids , self.lengths ): assert (seq_[0] == cls_id) and (seq_[-1] == sep_id), seq_ if len_ <= max_len: new_tok_ids.append(seq_ ) new_lengths.append(len_ ) else: lowercase__: Optional[Any] = [] for sub_s in divide_chunks(seq_ , max_len - 2 ): if sub_s[0] != cls_id: lowercase__: int = np.insert(lowerCAmelCase__ , 0 , lowerCAmelCase__ ) if sub_s[-1] != sep_id: lowercase__: Union[str, Any] = np.insert(lowerCAmelCase__ , len(lowerCAmelCase__ ) , lowerCAmelCase__ ) assert len(lowerCAmelCase__ ) <= max_len assert (sub_s[0] == cls_id) and (sub_s[-1] == sep_id), sub_s sub_seqs.append(lowerCAmelCase__ ) new_tok_ids.extend(lowerCAmelCase__ ) new_lengths.extend([len(lowerCAmelCase__ ) for l in sub_seqs] ) lowercase__: Union[str, Any] = np.array(lowerCAmelCase__ ) lowercase__: Union[str, Any] = np.array(lowerCAmelCase__ ) def SCREAMING_SNAKE_CASE__ ( self ) -> Tuple: '''simple docstring''' lowercase__: Dict = len(self ) lowercase__: Union[str, Any] = self.lengths > 11 lowercase__: List[Any] = self.token_ids[indices] lowercase__: Optional[Any] = self.lengths[indices] lowercase__: List[str] = len(self ) logger.info(F'Remove {init_size - new_size} too short (<=11 tokens) sequences.' ) def SCREAMING_SNAKE_CASE__ ( self ) -> int: '''simple docstring''' if "unk_token" not in self.params.special_tok_ids: return else: lowercase__: List[str] = self.params.special_tok_ids['unk_token'] lowercase__: str = len(self ) lowercase__: Tuple = np.array([np.count_nonzero(a == unk_token_id ) for a in self.token_ids] ) lowercase__: Any = (unk_occs / self.lengths) < 0.5 lowercase__: Tuple = self.token_ids[indices] lowercase__: str = self.lengths[indices] lowercase__: Dict = len(self ) logger.info(F'Remove {init_size - new_size} sequences with a high level of unknown tokens (50%).' ) def SCREAMING_SNAKE_CASE__ ( self ) -> str: '''simple docstring''' if not self.params.is_master: return logger.info(F'{len(self )} sequences' ) # data_len = sum(self.lengths) # nb_unique_tokens = len(Counter(list(chain(*self.token_ids)))) # logger.info(f'{data_len} tokens ({nb_unique_tokens} unique)') # unk_idx = self.params.special_tok_ids['unk_token'] # nb_unknown = sum([(t==unk_idx).sum() for t in self.token_ids]) # logger.info(f'{nb_unknown} unknown tokens (covering {100*nb_unknown/data_len:.2f}% of the data)') def SCREAMING_SNAKE_CASE__ ( self , lowerCAmelCase__ ) -> int: '''simple docstring''' lowercase__: Union[str, Any] = [t[0] for t in batch] lowercase__: Dict = [t[1] for t in batch] assert len(lowerCAmelCase__ ) == len(lowerCAmelCase__ ) # Max for paddings lowercase__: List[Any] = max(lowerCAmelCase__ ) # Pad token ids if self.params.mlm: lowercase__: Dict = self.params.special_tok_ids['pad_token'] else: lowercase__: Optional[Any] = self.params.special_tok_ids['unk_token'] lowercase__: int = [list(t.astype(lowerCAmelCase__ ) ) + [pad_idx] * (max_seq_len_ - len(lowerCAmelCase__ )) for t in token_ids] assert len(tk_ ) == len(lowerCAmelCase__ ) assert all(len(lowerCAmelCase__ ) == max_seq_len_ for t in tk_ ) lowercase__: Tuple = torch.tensor(tk_ ) # (bs, max_seq_len_) lowercase__: Optional[Any] = torch.tensor(lowerCAmelCase__ ) # (bs) return tk_t, lg_t
196
1
from pathlib import Path import numpy as np from PIL import Image def __UpperCamelCase ( _A ): lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2] return 0.2_9_8_9 * r + 0.5_8_7_0 * g + 0.1_1_4_0 * b def __UpperCamelCase ( _A ): return (gray > 127) & (gray <= 255) def __UpperCamelCase ( _A , _A ): lowerCAmelCase_ = np.zeros_like(_A ) lowerCAmelCase_ = np.zeros( (image.shape[0] + kernel.shape[0] - 1, image.shape[1] + kernel.shape[1] - 1) ) # Copy image to padded image lowerCAmelCase_ = image # Iterate over image & apply kernel for x in range(image.shape[1] ): for y in range(image.shape[0] ): lowerCAmelCase_ = ( kernel * image_padded[y : y + kernel.shape[0], x : x + kernel.shape[1]] ).sum() lowerCAmelCase_ = int(summation > 0 ) return output if __name__ == "__main__": # read original image _A : int = Path(__file__).resolve().parent / '''image_data''' / '''lena.jpg''' _A : Any = np.array(Image.open(lena_path)) # kernel to be applied _A : Optional[int] = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]]) _A : Any = dilation(gray_to_binary(rgb_to_gray(lena)), structuring_element) # Save the output image _A : str = Image.fromarray(output).convert('''RGB''') pil_img.save('''result_dilation.png''')
354
def __UpperCamelCase ( _A = 4000000 ): lowerCAmelCase_ = [0, 1] lowerCAmelCase_ = 0 while fib[i] <= n: fib.append(fib[i] + fib[i + 1] ) if fib[i + 2] > n: break i += 1 lowerCAmelCase_ = 0 for j in range(len(_A ) - 1 ): if fib[j] % 2 == 0: total += fib[j] return total if __name__ == "__main__": print(f"{solution() = }")
167
0
from pathlib import Path from typing import List from transformers import is_torch_available, is_vision_available from transformers.testing_utils import get_tests_dir, is_tool_test from transformers.tools.agent_types import AGENT_TYPE_MAPPING, AgentAudio, AgentImage, AgentText if is_torch_available(): import torch if is_vision_available(): from PIL import Image _a = ['''text''', '''image''', '''audio'''] def __A ( __lowerCAmelCase )-> Optional[int]: """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(3_000 ) ) elif isinstance(__lowerCAmelCase , __lowerCAmelCase ): inputs.append(create_inputs(__lowerCAmelCase ) ) else: raise ValueError(F"""Invalid type requested: {input_type}""" ) return inputs def __A ( __lowerCAmelCase )-> Optional[Any]: """simple docstring""" _UpperCAmelCase = [] for output in outputs: if isinstance(__lowerCAmelCase , (str, AgentText) ): output_types.append('text' ) elif isinstance(__lowerCAmelCase , (Image.Image, AgentImage) ): output_types.append('image' ) elif isinstance(__lowerCAmelCase , (torch.Tensor, AgentAudio) ): output_types.append('audio' ) else: raise ValueError(F"""Invalid output: {output}""" ) return output_types @is_tool_test class __lowerCamelCase : """simple docstring""" def UpperCamelCase ( self ): """simple docstring""" self.assertTrue(hasattr(self.tool , 'inputs' ) ) self.assertTrue(hasattr(self.tool , 'outputs' ) ) _UpperCAmelCase = self.tool.inputs for _input in inputs: if isinstance(_input , UpperCAmelCase ): 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 ): """simple docstring""" _UpperCAmelCase = create_inputs(self.tool.inputs ) _UpperCAmelCase = self.tool(*UpperCAmelCase ) # There is a single output if len(self.tool.outputs ) == 1: _UpperCAmelCase = [outputs] self.assertListEqual(output_types(UpperCAmelCase ) , self.tool.outputs ) def UpperCamelCase ( self ): """simple docstring""" self.assertTrue(hasattr(self.tool , 'description' ) ) self.assertTrue(hasattr(self.tool , 'default_checkpoint' ) ) self.assertTrue(self.tool.description.startswith('This is a tool that' ) ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = create_inputs(self.tool.inputs ) _UpperCAmelCase = self.tool(*UpperCAmelCase ) if not isinstance(UpperCAmelCase , UpperCAmelCase ): _UpperCAmelCase = [outputs] self.assertEqual(len(UpperCAmelCase ) , len(self.tool.outputs ) ) for output, output_type in zip(UpperCAmelCase , self.tool.outputs ): _UpperCAmelCase = AGENT_TYPE_MAPPING[output_type] self.assertTrue(isinstance(UpperCAmelCase , UpperCAmelCase ) ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = create_inputs(self.tool.inputs ) _UpperCAmelCase = [] for _input, input_type in zip(UpperCAmelCase , self.tool.inputs ): if isinstance(UpperCAmelCase , UpperCAmelCase ): _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(*UpperCAmelCase ) if not isinstance(UpperCAmelCase , UpperCAmelCase ): _UpperCAmelCase = [outputs] self.assertEqual(len(UpperCAmelCase ) , len(self.tool.outputs ) )
39
from __future__ import annotations def __A ( __lowerCAmelCase )-> list[int]: """simple docstring""" _UpperCAmelCase = 2 _UpperCAmelCase = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(__lowerCAmelCase ) if n > 1: factors.append(__lowerCAmelCase ) return factors if __name__ == "__main__": import doctest doctest.testmod()
39
1
"""simple docstring""" 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 , lowerCAmelCase , lowerCAmelCase=sys.maxsize ): """simple docstring""" snake_case = 'bilinear' snake_case = max_size snake_case = short_edge_length def __call__( self , lowerCAmelCase ): """simple docstring""" snake_case = [] for img in imgs: snake_case ,snake_case = img.shape[:2] # later: provide list and randomly choose index for resize snake_case = np.random.randint(self.short_edge_length[0] , self.short_edge_length[1] + 1 ) if size == 0: return img snake_case = size * 1.0 / min(lowerCAmelCase , lowerCAmelCase ) if h < w: snake_case ,snake_case = size, scale * w else: snake_case ,snake_case = scale * h, size if max(lowerCAmelCase , lowerCAmelCase ) > self.max_size: snake_case = self.max_size * 1.0 / max(lowerCAmelCase , lowerCAmelCase ) snake_case = newh * scale snake_case = neww * scale snake_case = int(neww + 0.5 ) snake_case = int(newh + 0.5 ) if img.dtype == np.uinta: snake_case = Image.fromarray(lowerCAmelCase ) snake_case = pil_image.resize((neww, newh) , PILImageResampling.BILINEAR ) snake_case = np.asarray(lowerCAmelCase ) else: snake_case = img.permute(2 , 0 , 1 ).unsqueeze(0 ) # 3, 0, 1) # hw(c) -> nchw snake_case = nn.functional.interpolate( lowerCAmelCase , (newh, neww) , mode=self.interp_method , align_corners=lowerCAmelCase ).squeeze(0 ) img_augs.append(lowerCAmelCase ) return img_augs class lowerCAmelCase_ : """simple docstring""" def __init__( self , lowerCAmelCase ): """simple docstring""" snake_case = ResizeShortestEdge([cfg.INPUT.MIN_SIZE_TEST, cfg.INPUT.MIN_SIZE_TEST] , cfg.INPUT.MAX_SIZE_TEST ) snake_case = cfg.INPUT.FORMAT snake_case = cfg.SIZE_DIVISIBILITY snake_case = cfg.PAD_VALUE snake_case = cfg.INPUT.MAX_SIZE_TEST snake_case = cfg.MODEL.DEVICE snake_case = torch.tensor(cfg.MODEL.PIXEL_STD ).to(self.device ).view(len(cfg.MODEL.PIXEL_STD ) , 1 , 1 ) snake_case = torch.tensor(cfg.MODEL.PIXEL_MEAN ).to(self.device ).view(len(cfg.MODEL.PIXEL_STD ) , 1 , 1 ) snake_case = lambda lowerCAmelCase : (x - self.pixel_mean) / self.pixel_std def snake_case ( self , lowerCAmelCase ): """simple docstring""" snake_case = tuple(max(lowerCAmelCase ) for s in zip(*[img.shape for img in images] ) ) snake_case = [im.shape[-2:] for im in images] snake_case = [ nn.functional.pad( lowerCAmelCase , [0, max_size[-1] - size[1], 0, max_size[-2] - size[0]] , value=self.pad_value , ) for size, im in zip(lowerCAmelCase , lowerCAmelCase ) ] return torch.stack(lowerCAmelCase ), torch.tensor(lowerCAmelCase ) def __call__( self , lowerCAmelCase , lowerCAmelCase=False ): """simple docstring""" with torch.no_grad(): if not isinstance(lowerCAmelCase , lowerCAmelCase ): snake_case = [images] if single_image: assert len(lowerCAmelCase ) == 1 for i in range(len(lowerCAmelCase ) ): if isinstance(images[i] , torch.Tensor ): images.insert(lowerCAmelCase , images.pop(lowerCAmelCase ).to(self.device ).float() ) elif not isinstance(images[i] , torch.Tensor ): images.insert( lowerCAmelCase , torch.as_tensor(img_tensorize(images.pop(lowerCAmelCase ) , input_format=self.input_format ) ) .to(self.device ) .float() , ) # resize smallest edge snake_case = torch.tensor([im.shape[:2] for im in images] ) snake_case = self.aug(lowerCAmelCase ) # 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 snake_case = [self.normalizer(lowerCAmelCase ) for x in images] # now pad them to do the following operations snake_case ,snake_case = self.pad(lowerCAmelCase ) # Normalize if self.size_divisibility > 0: raise NotImplementedError() # pad snake_case = torch.true_divide(lowerCAmelCase , lowerCAmelCase ) if single_image: return images[0], sizes[0], scales_yx[0] else: return images, sizes, scales_yx def lowerCAmelCase__ ( _UpperCamelCase : Any , _UpperCamelCase : List[str] ) -> Optional[int]: """simple docstring""" boxes[:, 0::2] *= scale_yx[:, 1] boxes[:, 1::2] *= scale_yx[:, 0] return boxes def lowerCAmelCase__ ( _UpperCamelCase : List[str] , _UpperCamelCase : Tuple[int, int] ) -> Dict: """simple docstring""" assert torch.isfinite(_UpperCamelCase ).all(), "Box tensor contains infinite or NaN!" snake_case ,snake_case = box_size tensor[:, 0].clamp_(min=0 , max=_UpperCamelCase ) tensor[:, 1].clamp_(min=0 , max=_UpperCamelCase ) tensor[:, 2].clamp_(min=0 , max=_UpperCamelCase ) tensor[:, 3].clamp_(min=0 , max=_UpperCamelCase )
351
"""simple docstring""" import os import sys import unittest SCREAMING_SNAKE_CASE__ = 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 SCREAMING_SNAKE_CASE__ = os.path.join(git_repo_path, "src", "transformers") SCREAMING_SNAKE_CASE__ = "\n{0} = None\n" SCREAMING_SNAKE_CASE__ = "\nclass {0}(metaclass=DummyObject):\n _backends = {1}\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, {1})\n" SCREAMING_SNAKE_CASE__ = "\ndef {0}(*args, **kwargs):\n requires_backends({0}, {1})\n" class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" def snake_case ( self ): """simple docstring""" snake_case = find_backend(' _import_structure["models.albert"].append("AlbertTokenizerFast")' ) self.assertIsNone(lowerCAmelCase ) snake_case = find_backend(' if not is_tokenizers_available():' ) self.assertEqual(lowerCAmelCase , 'tokenizers' ) snake_case = find_backend(' if not is_tensorflow_text_available():' ) self.assertEqual(lowerCAmelCase , 'tensorflow_text' ) snake_case = find_backend(' if not (is_sentencepiece_available() and is_tokenizers_available()):' ) self.assertEqual(lowerCAmelCase , 'sentencepiece_and_tokenizers' ) snake_case = find_backend( ' if not (is_sentencepiece_available() and is_tensorflow_text_available()):' ) self.assertEqual(lowerCAmelCase , 'sentencepiece_and_tensorflow_text' ) snake_case = find_backend( ' if not (is_sentencepiece_available() and is_tokenizers_available() and is_vision_available()):' ) self.assertEqual(lowerCAmelCase , 'sentencepiece_and_tokenizers_and_vision' ) def snake_case ( self ): """simple docstring""" 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' , lowerCAmelCase ) self.assertIn('tensorflow_text' , lowerCAmelCase ) self.assertIn('sentencepiece_and_tokenizers' , lowerCAmelCase ) # 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 snake_case ( self ): """simple docstring""" snake_case = create_dummy_object('CONSTANT' , '\'torch\'' ) self.assertEqual(lowerCAmelCase , '\nCONSTANT = None\n' ) snake_case = create_dummy_object('function' , '\'torch\'' ) self.assertEqual( lowerCAmelCase , '\ndef function(*args, **kwargs):\n requires_backends(function, \'torch\')\n' ) snake_case = '\nclass FakeClass(metaclass=DummyObject):\n _backends = \'torch\'\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, \'torch\')\n' snake_case = create_dummy_object('FakeClass' , '\'torch\'' ) self.assertEqual(lowerCAmelCase , lowerCAmelCase ) def snake_case ( self ): """simple docstring""" 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' snake_case = create_dummy_files({'torch': ['CONSTANT', 'function', 'FakeClass']} ) self.assertEqual(dummy_files['torch'] , lowerCAmelCase )
149
0
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = { """edbeeching/decision-transformer-gym-hopper-medium""": ( """https://huggingface.co/edbeeching/decision-transformer-gym-hopper-medium/resolve/main/config.json""" ), # See all DecisionTransformer models at https://huggingface.co/models?filter=decision_transformer } class A__ ( __magic_name__ ): lowercase = 'decision_transformer' lowercase = ['past_key_values'] lowercase = { 'max_position_embeddings': 'n_positions', 'num_attention_heads': 'n_head', 'num_hidden_layers': 'n_layer', } def __init__( self : str , a : Optional[int]=17 , a : str=4 , a : str=128 , a : Any=4_096 , a : Union[str, Any]=True , a : Tuple=1 , a : Tuple=1_024 , a : List[Any]=3 , a : str=1 , a : Optional[Any]=None , a : List[Any]="relu" , a : Optional[int]=0.1 , a : Tuple=0.1 , a : int=0.1 , a : Tuple=1E-5 , a : Union[str, Any]=0.0_2 , a : List[Any]=True , a : Tuple=True , a : Optional[int]=50_256 , a : Optional[Any]=50_256 , a : Optional[Any]=False , a : Optional[int]=False , **a : List[str] , ): '''simple docstring''' lowerCAmelCase__ : Any = state_dim lowerCAmelCase__ : List[Any] = act_dim lowerCAmelCase__ : List[Any] = hidden_size lowerCAmelCase__ : List[str] = max_ep_len lowerCAmelCase__ : List[Any] = action_tanh lowerCAmelCase__ : List[str] = vocab_size lowerCAmelCase__ : int = n_positions lowerCAmelCase__ : int = n_layer lowerCAmelCase__ : Dict = n_head lowerCAmelCase__ : List[Any] = n_inner lowerCAmelCase__ : List[str] = activation_function lowerCAmelCase__ : Tuple = resid_pdrop lowerCAmelCase__ : Dict = embd_pdrop lowerCAmelCase__ : Dict = attn_pdrop lowerCAmelCase__ : Tuple = layer_norm_epsilon lowerCAmelCase__ : List[Any] = initializer_range lowerCAmelCase__ : List[Any] = scale_attn_weights lowerCAmelCase__ : Any = use_cache lowerCAmelCase__ : Optional[int] = scale_attn_by_inverse_layer_idx lowerCAmelCase__ : Dict = reorder_and_upcast_attn lowerCAmelCase__ : Dict = bos_token_id lowerCAmelCase__ : List[Any] = eos_token_id super().__init__(bos_token_id=a , eos_token_id=a , **a )
212
from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel from ...schedulers import ScoreSdeVeScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class A__ ( __magic_name__ ): lowercase = 42 lowercase = 42 def __init__( self : Any , a : UNetaDModel , a : ScoreSdeVeScheduler ): '''simple docstring''' super().__init__() self.register_modules(unet=a , scheduler=a ) @torch.no_grad() def __call__( self : List[str] , a : int = 1 , a : int = 2_000 , a : Optional[Union[torch.Generator, List[torch.Generator]]] = None , a : Optional[str] = "pil" , a : bool = True , **a : int , ): '''simple docstring''' lowerCAmelCase__ : List[str] = self.unet.config.sample_size lowerCAmelCase__ : Union[str, Any] = (batch_size, 3, img_size, img_size) lowerCAmelCase__ : Tuple = self.unet lowerCAmelCase__ : Optional[Any] = randn_tensor(a , generator=a ) * self.scheduler.init_noise_sigma lowerCAmelCase__ : int = sample.to(self.device ) self.scheduler.set_timesteps(a ) self.scheduler.set_sigmas(a ) for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): lowerCAmelCase__ : Dict = self.scheduler.sigmas[i] * torch.ones(shape[0] , device=self.device ) # correction step for _ in range(self.scheduler.config.correct_steps ): lowerCAmelCase__ : Optional[Any] = self.unet(a , a ).sample lowerCAmelCase__ : Dict = self.scheduler.step_correct(a , a , generator=a ).prev_sample # prediction step lowerCAmelCase__ : Optional[int] = model(a , a ).sample lowerCAmelCase__ : Optional[Any] = self.scheduler.step_pred(a , a , a , generator=a ) lowerCAmelCase__ , lowerCAmelCase__ : str = output.prev_sample, output.prev_sample_mean lowerCAmelCase__ : Any = sample_mean.clamp(0 , 1 ) lowerCAmelCase__ : List[str] = sample.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": lowerCAmelCase__ : int = self.numpy_to_pil(a ) if not return_dict: return (sample,) return ImagePipelineOutput(images=a )
212
1
'''simple docstring''' import copy import inspect import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers import TimesformerConfig 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, TimesformerForVideoClassification, TimesformerModel, ) from transformers.models.timesformer.modeling_timesformer import TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from transformers import VideoMAEImageProcessor class UpperCamelCase : """simple docstring""" def __init__( self : Any , UpperCAmelCase_ : Any , UpperCAmelCase_ : List[Any]=1_3 , UpperCAmelCase_ : Dict=1_0 , UpperCAmelCase_ : Optional[int]=3 , UpperCAmelCase_ : Tuple=2 , UpperCAmelCase_ : Union[str, Any]=2 , UpperCAmelCase_ : Any=True , UpperCAmelCase_ : Union[str, Any]=True , UpperCAmelCase_ : Optional[int]=3_2 , UpperCAmelCase_ : Optional[int]=5 , UpperCAmelCase_ : List[str]=4 , UpperCAmelCase_ : Tuple=3_7 , UpperCAmelCase_ : str="gelu" , UpperCAmelCase_ : Optional[int]=0.1 , UpperCAmelCase_ : Union[str, Any]=0.1 , UpperCAmelCase_ : int=1_0 , UpperCAmelCase_ : Any=0.02 , UpperCAmelCase_ : int="divided_space_time" , UpperCAmelCase_ : Optional[int]=None , ): """simple docstring""" a : str = parent a : Optional[Any] = batch_size a : Dict = image_size a : Optional[int] = num_channels a : List[Any] = patch_size a : List[Any] = num_frames a : Optional[int] = is_training a : Any = use_labels a : Tuple = hidden_size a : Tuple = num_hidden_layers a : str = num_attention_heads a : List[Any] = intermediate_size a : Tuple = hidden_act a : Optional[Any] = hidden_dropout_prob a : Any = attention_probs_dropout_prob a : Union[str, Any] = attention_type a : Optional[Any] = initializer_range a : str = scope a : Dict = num_labels # in TimeSformer, the number of spatial tokens equals num_frames * num_patches per frame + 1 CLS token a : Optional[Any] = (image_size // patch_size) ** 2 a : List[Any] = (num_frames) * self.num_patches_per_frame + 1 def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : Dict = floats_tensor( [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]) a : int = None if self.use_labels: a : Tuple = ids_tensor([self.batch_size] , self.num_labels) a : Optional[int] = self.get_config() return config, pixel_values, labels def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : Union[str, Any] = TimesformerConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , num_frames=self.num_frames , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , initializer_range=self.initializer_range , attention_type=self.attention_type , ) a : List[Any] = self.num_labels return config def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[Any]): """simple docstring""" a : List[Any] = TimesformerModel(config=UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Optional[Any] = model(UpperCAmelCase_) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def SCREAMING_SNAKE_CASE_ ( self : List[Any] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : Any): """simple docstring""" a : Union[str, Any] = TimesformerForVideoClassification(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : List[str] = model(UpperCAmelCase_) # verify the logits shape a : Optional[int] = torch.Size((self.batch_size, self.num_labels)) self.parent.assertEqual(result.logits.shape , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : Optional[Any] = self.prepare_config_and_inputs() a , a , a : str = config_and_inputs a : Tuple = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class UpperCamelCase ( a_ , a_ , unittest.TestCase ): """simple docstring""" A : Dict = (TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else () A : Optional[Any] = ( {"feature-extraction": TimesformerModel, "video-classification": TimesformerForVideoClassification} if is_torch_available() else {} ) A : Optional[int] = False A : Union[str, Any] = False A : Tuple = False A : Optional[int] = False def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" a : Optional[Any] = TimesformerModelTester(self) a : List[Any] = ConfigTester( self , config_class=UpperCAmelCase_ , has_text_modality=UpperCAmelCase_ , hidden_size=3_7) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Any , UpperCAmelCase_ : List[Any]=False): """simple docstring""" a : List[str] = copy.deepcopy(UpperCAmelCase_) if return_labels: if model_class in get_values(UpperCAmelCase_): a : Dict = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=UpperCAmelCase_) return inputs_dict def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='TimeSformer does not use inputs_embeds') def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" pass def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" a , a : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a : Tuple = model_class(UpperCAmelCase_) self.assertIsInstance(model.get_input_embeddings() , (nn.Module)) a : List[str] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(UpperCAmelCase_ , nn.Linear)) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a , a : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a : List[Any] = model_class(UpperCAmelCase_) a : str = inspect.signature(model.forward) # signature.parameters is an OrderedDict => so arg_names order is deterministic a : Any = [*signature.parameters.keys()] a : Optional[int] = ['pixel_values'] self.assertListEqual(arg_names[:1] , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" a : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_video_classification(*UpperCAmelCase_) @slow def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a : Dict = TimesformerModel.from_pretrained(UpperCAmelCase_) self.assertIsNotNone(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" if not self.has_attentions: pass else: a , a : int = self.model_tester.prepare_config_and_inputs_for_common() a : Any = True for model_class in self.all_model_classes: a : Optional[Any] = self.model_tester.seq_length a : List[str] = self.model_tester.num_frames a : Dict = True a : Optional[Any] = False a : List[str] = True a : Optional[int] = model_class(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() with torch.no_grad(): a : Dict = model(**self._prepare_for_class(UpperCAmelCase_ , UpperCAmelCase_)) a : Union[str, Any] = 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"] a : Dict = True a : List[Any] = model_class(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() with torch.no_grad(): a : List[str] = model(**self._prepare_for_class(UpperCAmelCase_ , UpperCAmelCase_)) a : List[str] = outputs.attentions self.assertEqual(len(UpperCAmelCase_) , self.model_tester.num_hidden_layers) # attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1) self.assertListEqual( list(attentions[0].shape[-3:]) , [self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1] , ) a : List[Any] = len(UpperCAmelCase_) # Check attention is always last and order is fine a : Union[str, Any] = True a : Any = True a : Optional[int] = model_class(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() with torch.no_grad(): a : Union[str, Any] = model(**self._prepare_for_class(UpperCAmelCase_ , UpperCAmelCase_)) self.assertEqual(out_len + 1 , len(UpperCAmelCase_)) a : Any = outputs.attentions self.assertEqual(len(UpperCAmelCase_) , self.model_tester.num_hidden_layers) # attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1) self.assertListEqual( list(self_attentions[0].shape[-3:]) , [self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1] , ) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" def check_hidden_states_output(UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Optional[int]): a : Tuple = model_class(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() with torch.no_grad(): a : List[str] = model(**self._prepare_for_class(UpperCAmelCase_ , UpperCAmelCase_)) a : List[Any] = outputs.hidden_states a : str = self.model_tester.num_hidden_layers + 1 self.assertEqual(len(UpperCAmelCase_) , UpperCAmelCase_) a : Any = self.model_tester.seq_length self.assertListEqual( list(hidden_states[0].shape[-2:]) , [seq_length, self.model_tester.hidden_size] , ) a , a : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a : str = True check_hidden_states_output(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] a : Optional[int] = True check_hidden_states_output(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_) def SCREAMING_SNAKE_CASE__ ( ) -> Tuple: """simple docstring""" a : Dict = hf_hub_download( repo_id='hf-internal-testing/spaghetti-video' , filename='eating_spaghetti.npy' , repo_type='dataset' ) a : Tuple = np.load(snake_case ) return list(snake_case ) @require_torch @require_vision class UpperCamelCase ( unittest.TestCase ): """simple docstring""" @cached_property def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """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 SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : int = TimesformerForVideoClassification.from_pretrained('facebook/timesformer-base-finetuned-k400').to( UpperCAmelCase_) a : Any = self.default_image_processor a : Optional[Any] = prepare_video() a : Any = image_processor(video[:8] , return_tensors='pt').to(UpperCAmelCase_) # forward pass with torch.no_grad(): a : List[str] = model(**UpperCAmelCase_) # verify the logits a : str = torch.Size((1, 4_0_0)) self.assertEqual(outputs.logits.shape , UpperCAmelCase_) a : Tuple = torch.tensor([-0.30_16, -0.77_13, -0.42_05]).to(UpperCAmelCase_) self.assertTrue(torch.allclose(outputs.logits[0, :3] , UpperCAmelCase_ , atol=1e-4))
345
'''simple docstring''' import baseaa import io import json import os from copy import deepcopy from ..optimizer import AcceleratedOptimizer from ..scheduler import AcceleratedScheduler class UpperCamelCase : """simple docstring""" def __init__( self : List[str] , UpperCAmelCase_ : Tuple): """simple docstring""" if isinstance(UpperCAmelCase_ , UpperCAmelCase_): # Don't modify user's data should they want to reuse it (e.g. in tests), because once we # modified it, it will not be accepted here again, since `auto` values would have been overridden a : Dict = deepcopy(UpperCAmelCase_) elif os.path.exists(UpperCAmelCase_): with io.open(UpperCAmelCase_ , 'r' , encoding='utf-8') as f: a : Union[str, Any] = json.load(UpperCAmelCase_) else: try: a : Union[str, Any] = baseaa.urlsafe_baadecode(UpperCAmelCase_).decode('utf-8') a : List[str] = json.loads(UpperCAmelCase_) except (UnicodeDecodeError, AttributeError, ValueError): raise ValueError( f"""Expected a string path to an existing deepspeed config, or a dictionary, or a base64 encoded string. Received: {config_file_or_dict}""") a : Optional[int] = config self.set_stage_and_offload() def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : str = self.get_value('zero_optimization.stage' , -1) # offload a : Any = False if self.is_zeroa() or self.is_zeroa(): a : Tuple = set(['cpu', 'nvme']) a : int = set( [ self.get_value('zero_optimization.offload_optimizer.device'), self.get_value('zero_optimization.offload_param.device'), ]) if len(offload_devices & offload_devices_valid) > 0: a : List[str] = True def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : Dict): """simple docstring""" a : List[str] = self.config # find the config node of interest if it exists a : int = ds_key_long.split('.') a : Union[str, Any] = nodes.pop() for node in nodes: a : Union[str, Any] = config.get(UpperCAmelCase_) if config is None: return None, ds_key return config, ds_key def SCREAMING_SNAKE_CASE_ ( self : Any , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : int=None): """simple docstring""" a , a : int = self.find_config_node(UpperCAmelCase_) if config is None: return default return config.get(UpperCAmelCase_ , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : int , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Any=False): """simple docstring""" a : Any = self.config # find the config node of interest if it exists a : Optional[Any] = ds_key_long.split('.') for node in nodes: a : List[str] = config a : int = config.get(UpperCAmelCase_) if config is None: if must_exist: raise ValueError(f"""Can't find {ds_key_long} entry in the config: {self.config}""") else: return # if found remove it if parent_config is not None: parent_config.pop(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : str): """simple docstring""" a : List[str] = self.get_value(UpperCAmelCase_) return False if value is None else bool(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : Union[str, Any]): """simple docstring""" a : List[Any] = self.get_value(UpperCAmelCase_) return False if value is None else not bool(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" return self._stage == 2 def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" return self._stage == 3 def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" return self._offload class UpperCamelCase : """simple docstring""" def __init__( self : str , UpperCAmelCase_ : int): """simple docstring""" a : Union[str, Any] = engine def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : Any , **UpperCAmelCase_ : List[Any]): """simple docstring""" self.engine.backward(UpperCAmelCase_ , **UpperCAmelCase_) # Deepspeed's `engine.step` performs the following operations: # - gradient accumulation check # - gradient clipping # - optimizer step # - zero grad # - checking overflow # - lr_scheduler step (only if engine.lr_scheduler is not None) self.engine.step() # and this plugin overrides the above calls with no-ops when Accelerate runs under # Deepspeed, but allows normal functionality for non-Deepspeed cases thus enabling a simple # training loop that works transparently under many training regimes. class UpperCamelCase ( a_ ): """simple docstring""" def __init__( self : Union[str, Any] , UpperCAmelCase_ : Any): """simple docstring""" super().__init__(UpperCAmelCase_ , device_placement=UpperCAmelCase_ , scaler=UpperCAmelCase_) a : List[str] = hasattr(self.optimizer , 'overflow') def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : Dict=None): """simple docstring""" pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed @property def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" if self.__has_overflow__: return self.optimizer.overflow return False class UpperCamelCase ( a_ ): """simple docstring""" def __init__( self : str , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Optional[Any]): """simple docstring""" super().__init__(UpperCAmelCase_ , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed class UpperCamelCase : """simple docstring""" def __init__( self : Union[str, Any] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Optional[int]=0.0_01 , UpperCAmelCase_ : List[Any]=0 , **UpperCAmelCase_ : Union[str, Any]): """simple docstring""" a : int = params a : str = lr a : Tuple = weight_decay a : Dict = kwargs class UpperCamelCase : """simple docstring""" def __init__( self : int , UpperCAmelCase_ : int , UpperCAmelCase_ : Any=None , UpperCAmelCase_ : Union[str, Any]=0 , **UpperCAmelCase_ : List[Any]): """simple docstring""" a : str = optimizer a : Tuple = total_num_steps a : Optional[Any] = warmup_num_steps a : List[str] = kwargs
345
1
'''simple docstring''' import unittest from transformers import DebertaConfig, is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device 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 ( DebertaForMaskedLM, DebertaForQuestionAnswering, DebertaForSequenceClassification, DebertaForTokenClassification, DebertaModel, ) from transformers.models.deberta.modeling_deberta import DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST class SCREAMING_SNAKE_CASE( A__ ): """simple docstring""" def __init__( self : Optional[Any] , __snake_case : List[str] , __snake_case : Tuple=13 , __snake_case : Optional[Any]=7 , __snake_case : Optional[int]=True , __snake_case : int=True , __snake_case : Optional[int]=True , __snake_case : str=True , __snake_case : str=99 , __snake_case : Any=32 , __snake_case : Union[str, Any]=5 , __snake_case : Optional[int]=4 , __snake_case : List[str]=37 , __snake_case : Any="gelu" , __snake_case : Optional[int]=0.1 , __snake_case : Dict=0.1 , __snake_case : Dict=512 , __snake_case : List[str]=16 , __snake_case : Any=2 , __snake_case : List[str]=0.02 , __snake_case : str=False , __snake_case : str=True , __snake_case : List[str]="None" , __snake_case : List[Any]=3 , __snake_case : Optional[Any]=4 , __snake_case : Tuple=None , ) -> List[str]: UpperCAmelCase : int = parent UpperCAmelCase : List[Any] = batch_size UpperCAmelCase : Tuple = seq_length UpperCAmelCase : int = is_training UpperCAmelCase : Union[str, Any] = use_input_mask UpperCAmelCase : List[str] = use_token_type_ids UpperCAmelCase : List[str] = use_labels UpperCAmelCase : Union[str, Any] = vocab_size UpperCAmelCase : int = hidden_size UpperCAmelCase : int = num_hidden_layers UpperCAmelCase : str = num_attention_heads UpperCAmelCase : Union[str, Any] = intermediate_size UpperCAmelCase : Optional[Any] = hidden_act UpperCAmelCase : Dict = hidden_dropout_prob UpperCAmelCase : int = attention_probs_dropout_prob UpperCAmelCase : Union[str, Any] = max_position_embeddings UpperCAmelCase : Union[str, Any] = type_vocab_size UpperCAmelCase : int = type_sequence_label_size UpperCAmelCase : List[Any] = initializer_range UpperCAmelCase : Optional[Any] = num_labels UpperCAmelCase : int = num_choices UpperCAmelCase : List[Any] = relative_attention UpperCAmelCase : Any = position_biased_input UpperCAmelCase : Any = pos_att_type UpperCAmelCase : Optional[int] = scope def A ( self : Optional[int] ) -> List[str]: UpperCAmelCase : str = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : Any = None if self.use_input_mask: UpperCAmelCase : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) UpperCAmelCase : Optional[Any] = None if self.use_token_type_ids: UpperCAmelCase : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) UpperCAmelCase : Union[str, Any] = None UpperCAmelCase : Union[str, Any] = None UpperCAmelCase : Any = None if self.use_labels: UpperCAmelCase : int = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCAmelCase : Any = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCAmelCase : Optional[int] = ids_tensor([self.batch_size] , self.num_choices ) UpperCAmelCase : Optional[int] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def A ( self : List[Any] ) -> Union[str, Any]: return DebertaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , relative_attention=self.relative_attention , position_biased_input=self.position_biased_input , pos_att_type=self.pos_att_type , ) def A ( self : int ) -> List[str]: UpperCAmelCase : Dict = self.get_config() UpperCAmelCase : int = 300 return config def A ( self : Optional[int] , __snake_case : str ) -> List[str]: self.parent.assertListEqual(list(result.loss.size() ) , [] ) def A ( self : Optional[int] , __snake_case : Dict , __snake_case : Tuple , __snake_case : Optional[Any] , __snake_case : int , __snake_case : List[str] , __snake_case : Dict , __snake_case : str ) -> List[Any]: UpperCAmelCase : Optional[int] = DebertaModel(config=__snake_case ) model.to(__snake_case ) model.eval() UpperCAmelCase : List[Any] = model(__snake_case , attention_mask=__snake_case , token_type_ids=__snake_case )[0] UpperCAmelCase : Union[str, Any] = model(__snake_case , token_type_ids=__snake_case )[0] UpperCAmelCase : List[Any] = model(__snake_case )[0] self.parent.assertListEqual(list(sequence_output.size() ) , [self.batch_size, self.seq_length, self.hidden_size] ) def A ( self : Dict , __snake_case : Dict , __snake_case : str , __snake_case : List[Any] , __snake_case : str , __snake_case : Optional[Any] , __snake_case : Tuple , __snake_case : List[Any] ) -> str: UpperCAmelCase : Tuple = DebertaForMaskedLM(config=__snake_case ) model.to(__snake_case ) model.eval() UpperCAmelCase : Dict = model(__snake_case , attention_mask=__snake_case , token_type_ids=__snake_case , labels=__snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def A ( self : Tuple , __snake_case : Optional[int] , __snake_case : List[str] , __snake_case : Dict , __snake_case : Optional[int] , __snake_case : int , __snake_case : Tuple , __snake_case : Optional[int] ) -> List[Any]: UpperCAmelCase : Any = self.num_labels UpperCAmelCase : Optional[int] = DebertaForSequenceClassification(__snake_case ) model.to(__snake_case ) model.eval() UpperCAmelCase : Optional[Any] = model(__snake_case , attention_mask=__snake_case , token_type_ids=__snake_case , labels=__snake_case ) self.parent.assertListEqual(list(result.logits.size() ) , [self.batch_size, self.num_labels] ) self.check_loss_output(__snake_case ) def A ( self : Tuple , __snake_case : Tuple , __snake_case : List[str] , __snake_case : int , __snake_case : int , __snake_case : Optional[int] , __snake_case : Optional[Any] , __snake_case : Any ) -> Tuple: UpperCAmelCase : int = self.num_labels UpperCAmelCase : Tuple = DebertaForTokenClassification(config=__snake_case ) model.to(__snake_case ) model.eval() UpperCAmelCase : Dict = model(__snake_case , attention_mask=__snake_case , token_type_ids=__snake_case , labels=__snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def A ( self : Union[str, Any] , __snake_case : Optional[int] , __snake_case : List[str] , __snake_case : List[str] , __snake_case : List[Any] , __snake_case : List[Any] , __snake_case : Union[str, Any] , __snake_case : Optional[int] ) -> List[Any]: UpperCAmelCase : int = DebertaForQuestionAnswering(config=__snake_case ) model.to(__snake_case ) model.eval() UpperCAmelCase : List[str] = model( __snake_case , attention_mask=__snake_case , token_type_ids=__snake_case , start_positions=__snake_case , end_positions=__snake_case , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def A ( self : Any ) -> Union[str, Any]: UpperCAmelCase : str = self.prepare_config_and_inputs() ( ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ) : Union[str, Any] = config_and_inputs UpperCAmelCase : Union[str, Any] = {'''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__ , A__ , unittest.TestCase ): """simple docstring""" lowerCamelCase__ = ( ( DebertaModel, DebertaForMaskedLM, DebertaForSequenceClassification, DebertaForTokenClassification, DebertaForQuestionAnswering, ) if is_torch_available() else () ) lowerCamelCase__ = ( { """feature-extraction""": DebertaModel, """fill-mask""": DebertaForMaskedLM, """question-answering""": DebertaForQuestionAnswering, """text-classification""": DebertaForSequenceClassification, """token-classification""": DebertaForTokenClassification, """zero-shot""": DebertaForSequenceClassification, } if is_torch_available() else {} ) lowerCamelCase__ = True lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False def A ( self : Optional[int] ) -> int: UpperCAmelCase : Optional[Any] = DebertaModelTester(self ) UpperCAmelCase : Optional[Any] = ConfigTester(self , config_class=__snake_case , hidden_size=37 ) def A ( self : Dict ) -> Any: self.config_tester.run_common_tests() def A ( self : Optional[int] ) -> Tuple: UpperCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_model(*__snake_case ) def A ( self : Union[str, Any] ) -> Dict: UpperCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_sequence_classification(*__snake_case ) def A ( self : Tuple ) -> int: UpperCAmelCase : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_masked_lm(*__snake_case ) def A ( self : List[Any] ) -> Dict: UpperCAmelCase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_question_answering(*__snake_case ) def A ( self : List[str] ) -> List[str]: UpperCAmelCase : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_token_classification(*__snake_case ) @slow def A ( self : str ) -> Optional[Any]: for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase : int = DebertaModel.from_pretrained(__snake_case ) self.assertIsNotNone(__snake_case ) @require_torch @require_sentencepiece @require_tokenizers class SCREAMING_SNAKE_CASE( unittest.TestCase ): """simple docstring""" @unittest.skip(reason='''Model not available yet''' ) def A ( self : Tuple ) -> str: pass @slow def A ( self : Any ) -> str: UpperCAmelCase : Tuple = DebertaModel.from_pretrained('''microsoft/deberta-base''' ) UpperCAmelCase : Optional[Any] = torch.tensor([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]] ) UpperCAmelCase : Any = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): UpperCAmelCase : Any = model(__snake_case , attention_mask=__snake_case )[0] # compare the actual values for a slice. UpperCAmelCase : Optional[int] = torch.tensor( [[[-0.59_86, -0.80_55, -0.84_62], [1.44_84, -0.93_48, -0.80_59], [0.31_23, 0.00_32, -1.41_31]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , __snake_case , atol=1E-4 ) , F"""{output[:, 1:4, 1:4]}""" )
23
'''simple docstring''' import os import tempfile import unittest import numpy as np from diffusers.utils import is_flax_available from diffusers.utils.testing_utils import require_flax, slow if is_flax_available(): import jax import jax.numpy as jnp from flax.jax_utils import replicate from flax.training.common_utils import shard from diffusers import FlaxDDIMScheduler, FlaxDiffusionPipeline, FlaxStableDiffusionPipeline @require_flax class SCREAMING_SNAKE_CASE( unittest.TestCase ): """simple docstring""" def A ( self : str ) -> int: with tempfile.TemporaryDirectory() as tmpdirname: # pipeline has Flax weights UpperCAmelCase : Optional[Any] = FlaxDiffusionPipeline.from_pretrained( '''hf-internal-testing/tiny-stable-diffusion-pipe''' , safety_checker=__snake_case , cache_dir=__snake_case ) UpperCAmelCase : str = [t[-1] for t in os.walk(os.path.join(__snake_case , os.listdir(__snake_case )[0] , '''snapshots''' ) )] UpperCAmelCase : str = [item for sublist in all_root_files for item in sublist] # None of the downloaded files should be a PyTorch file even if we have some here: # https://huggingface.co/hf-internal-testing/tiny-stable-diffusion-pipe/blob/main/unet/diffusion_pytorch_model.bin assert not any(f.endswith('''.bin''' ) for f in files ) @slow @require_flax class SCREAMING_SNAKE_CASE( unittest.TestCase ): """simple docstring""" def A ( self : List[str] ) -> Dict: UpperCAmelCase , UpperCAmelCase : str = FlaxStableDiffusionPipeline.from_pretrained( '''hf-internal-testing/tiny-stable-diffusion-pipe''' , safety_checker=__snake_case ) UpperCAmelCase : List[Any] = ( '''A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of''' ''' field, close up, split lighting, cinematic''' ) UpperCAmelCase : List[str] = jax.random.PRNGKey(0 ) UpperCAmelCase : Optional[Any] = 4 UpperCAmelCase : Optional[Any] = jax.device_count() UpperCAmelCase : Tuple = num_samples * [prompt] UpperCAmelCase : int = pipeline.prepare_inputs(__snake_case ) # shard inputs and rng UpperCAmelCase : Tuple = replicate(__snake_case ) UpperCAmelCase : Any = jax.random.split(__snake_case , __snake_case ) UpperCAmelCase : Optional[Any] = shard(__snake_case ) UpperCAmelCase : Optional[int] = pipeline(__snake_case , __snake_case , __snake_case , __snake_case , jit=__snake_case ).images assert images.shape == (num_samples, 1, 64, 64, 3) if jax.device_count() == 8: assert np.abs(np.abs(images[0, 0, :2, :2, -2:] , dtype=np.floataa ).sum() - 4.1_51_47_45 ) < 1E-3 assert np.abs(np.abs(__snake_case , dtype=np.floataa ).sum() - 4_99_47.8_75 ) < 5E-1 UpperCAmelCase : Union[str, Any] = pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:] ) ) ) assert len(__snake_case ) == num_samples def A ( self : List[Any] ) -> List[str]: UpperCAmelCase , UpperCAmelCase : Union[str, Any] = FlaxStableDiffusionPipeline.from_pretrained( '''CompVis/stable-diffusion-v1-4''' , revision='''flax''' , safety_checker=__snake_case ) UpperCAmelCase : Dict = ( '''A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of''' ''' field, close up, split lighting, cinematic''' ) UpperCAmelCase : Optional[Any] = jax.random.PRNGKey(0 ) UpperCAmelCase : Any = 50 UpperCAmelCase : Union[str, Any] = jax.device_count() UpperCAmelCase : int = num_samples * [prompt] UpperCAmelCase : Union[str, Any] = pipeline.prepare_inputs(__snake_case ) # shard inputs and rng UpperCAmelCase : Dict = replicate(__snake_case ) UpperCAmelCase : int = jax.random.split(__snake_case , __snake_case ) UpperCAmelCase : Tuple = shard(__snake_case ) UpperCAmelCase : Tuple = pipeline(__snake_case , __snake_case , __snake_case , __snake_case , jit=__snake_case ).images assert images.shape == (num_samples, 1, 512, 512, 3) if jax.device_count() == 8: assert np.abs((np.abs(images[0, 0, :2, :2, -2:] , dtype=np.floataa ).sum() - 0.05_65_24_01) ) < 1E-3 assert np.abs((np.abs(__snake_case , dtype=np.floataa ).sum() - 2_38_38_08.2) ) < 5E-1 def A ( self : int ) -> Dict: UpperCAmelCase , UpperCAmelCase : List[str] = FlaxStableDiffusionPipeline.from_pretrained( '''CompVis/stable-diffusion-v1-4''' , revision='''bf16''' , dtype=jnp.bfloataa , safety_checker=__snake_case ) UpperCAmelCase : Dict = ( '''A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of''' ''' field, close up, split lighting, cinematic''' ) UpperCAmelCase : Union[str, Any] = jax.random.PRNGKey(0 ) UpperCAmelCase : List[str] = 50 UpperCAmelCase : Union[str, Any] = jax.device_count() UpperCAmelCase : List[Any] = num_samples * [prompt] UpperCAmelCase : int = pipeline.prepare_inputs(__snake_case ) # shard inputs and rng UpperCAmelCase : Tuple = replicate(__snake_case ) UpperCAmelCase : List[Any] = jax.random.split(__snake_case , __snake_case ) UpperCAmelCase : Optional[int] = shard(__snake_case ) UpperCAmelCase : Optional[Any] = pipeline(__snake_case , __snake_case , __snake_case , __snake_case , jit=__snake_case ).images assert images.shape == (num_samples, 1, 512, 512, 3) if jax.device_count() == 8: assert np.abs((np.abs(images[0, 0, :2, :2, -2:] , dtype=np.floataa ).sum() - 0.04_00_39_06) ) < 1E-3 assert np.abs((np.abs(__snake_case , dtype=np.floataa ).sum() - 2_37_35_16.75) ) < 5E-1 def A ( self : int ) -> Any: UpperCAmelCase , UpperCAmelCase : Dict = FlaxStableDiffusionPipeline.from_pretrained( '''CompVis/stable-diffusion-v1-4''' , revision='''bf16''' , dtype=jnp.bfloataa ) UpperCAmelCase : List[str] = ( '''A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of''' ''' field, close up, split lighting, cinematic''' ) UpperCAmelCase : List[str] = jax.random.PRNGKey(0 ) UpperCAmelCase : Union[str, Any] = 50 UpperCAmelCase : Optional[int] = jax.device_count() UpperCAmelCase : List[str] = num_samples * [prompt] UpperCAmelCase : Dict = pipeline.prepare_inputs(__snake_case ) # shard inputs and rng UpperCAmelCase : Tuple = replicate(__snake_case ) UpperCAmelCase : Any = jax.random.split(__snake_case , __snake_case ) UpperCAmelCase : str = shard(__snake_case ) UpperCAmelCase : Optional[int] = pipeline(__snake_case , __snake_case , __snake_case , __snake_case , jit=__snake_case ).images assert images.shape == (num_samples, 1, 512, 512, 3) if jax.device_count() == 8: assert np.abs((np.abs(images[0, 0, :2, :2, -2:] , dtype=np.floataa ).sum() - 0.04_00_39_06) ) < 1E-3 assert np.abs((np.abs(__snake_case , dtype=np.floataa ).sum() - 2_37_35_16.75) ) < 5E-1 def A ( self : Tuple ) -> Optional[Any]: UpperCAmelCase : int = FlaxDDIMScheduler( beta_start=0.0_00_85 , beta_end=0.0_12 , beta_schedule='''scaled_linear''' , set_alpha_to_one=__snake_case , steps_offset=1 , ) UpperCAmelCase , UpperCAmelCase : Tuple = FlaxStableDiffusionPipeline.from_pretrained( '''CompVis/stable-diffusion-v1-4''' , revision='''bf16''' , dtype=jnp.bfloataa , scheduler=__snake_case , safety_checker=__snake_case , ) UpperCAmelCase : Tuple = scheduler.create_state() UpperCAmelCase : Dict = scheduler_state UpperCAmelCase : str = ( '''A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of''' ''' field, close up, split lighting, cinematic''' ) UpperCAmelCase : int = jax.random.PRNGKey(0 ) UpperCAmelCase : Union[str, Any] = 50 UpperCAmelCase : Optional[Any] = jax.device_count() UpperCAmelCase : Any = num_samples * [prompt] UpperCAmelCase : Dict = pipeline.prepare_inputs(__snake_case ) # shard inputs and rng UpperCAmelCase : str = replicate(__snake_case ) UpperCAmelCase : List[str] = jax.random.split(__snake_case , __snake_case ) UpperCAmelCase : Optional[int] = shard(__snake_case ) UpperCAmelCase : Dict = pipeline(__snake_case , __snake_case , __snake_case , __snake_case , jit=__snake_case ).images assert images.shape == (num_samples, 1, 512, 512, 3) if jax.device_count() == 8: assert np.abs((np.abs(images[0, 0, :2, :2, -2:] , dtype=np.floataa ).sum() - 0.0_45_04_39_45) ) < 1E-3 assert np.abs((np.abs(__snake_case , dtype=np.floataa ).sum() - 2_34_76_93.5) ) < 5E-1 def A ( self : Any ) -> Tuple: UpperCAmelCase : List[Any] = ( '''A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of''' ''' field, close up, split lighting, cinematic''' ) UpperCAmelCase : Union[str, Any] = jax.device_count() UpperCAmelCase : List[Any] = num_samples * [prompt] UpperCAmelCase : str = jax.random.split(jax.random.PRNGKey(0 ) , __snake_case ) UpperCAmelCase , UpperCAmelCase : Tuple = FlaxStableDiffusionPipeline.from_pretrained( '''CompVis/stable-diffusion-v1-4''' , revision='''bf16''' , dtype=jnp.bfloataa , safety_checker=__snake_case , ) UpperCAmelCase : Dict = replicate(__snake_case ) UpperCAmelCase : Optional[Any] = pipeline.prepare_inputs(__snake_case ) UpperCAmelCase : List[str] = shard(__snake_case ) UpperCAmelCase : Any = pipeline(__snake_case , __snake_case , __snake_case , jit=__snake_case ).images assert images.shape == (num_samples, 1, 512, 512, 3) UpperCAmelCase : Optional[int] = images[2, 0, 256, 10:17, 1] # With memory efficient attention UpperCAmelCase , UpperCAmelCase : Any = FlaxStableDiffusionPipeline.from_pretrained( '''CompVis/stable-diffusion-v1-4''' , revision='''bf16''' , dtype=jnp.bfloataa , safety_checker=__snake_case , use_memory_efficient_attention=__snake_case , ) UpperCAmelCase : int = replicate(__snake_case ) UpperCAmelCase : int = pipeline.prepare_inputs(__snake_case ) UpperCAmelCase : List[Any] = shard(__snake_case ) UpperCAmelCase : Optional[Any] = pipeline(__snake_case , __snake_case , __snake_case , jit=__snake_case ).images assert images_eff.shape == (num_samples, 1, 512, 512, 3) UpperCAmelCase : int = images[2, 0, 256, 10:17, 1] # I checked the results visually and they are very similar. However, I saw that the max diff is `1` and the `sum` # over the 8 images is exactly `256`, which is very suspicious. Testing a random slice for now. assert abs(slice_eff - slice ).max() < 1E-2
23
1
"""simple docstring""" import argparse import gc import json import os import shutil import warnings import torch from transformers import LlamaConfig, LlamaForCausalLM, LlamaTokenizer try: from transformers import LlamaTokenizerFast except ImportError as e: warnings.warn(e) warnings.warn( '''The converted tokenizer will be the `slow` tokenizer. To use the fast, update your `tokenizers` library and re-run the tokenizer conversion''' ) A_ = None A_ = { '''7B''': 1_10_08, '''13B''': 1_38_24, '''30B''': 1_79_20, '''65B''': 2_20_16, '''70B''': 2_86_72, } A_ = { '''7B''': 1, '''7Bf''': 1, '''13B''': 2, '''13Bf''': 2, '''30B''': 4, '''65B''': 8, '''70B''': 8, '''70Bf''': 8, } def UpperCAmelCase__ (snake_case__ : int , snake_case__ : List[Any]=1 , snake_case__ : List[Any]=2_56 ): """simple docstring""" return multiple_of * ((int(ffn_dim_multiplier * int(8 * n / 3 ) ) + multiple_of - 1) // multiple_of) def UpperCAmelCase__ (snake_case__ : str ): """simple docstring""" with open(a_ , """r""" ) as f: return json.load(a_ ) def UpperCAmelCase__ (snake_case__ : Dict , snake_case__ : Tuple ): """simple docstring""" with open(a_ , """w""" ) as f: json.dump(a_ , a_ ) def UpperCAmelCase__ (snake_case__ : List[str] , snake_case__ : Union[str, Any] , snake_case__ : Union[str, Any] , snake_case__ : Optional[int]=True ): """simple docstring""" os.makedirs(a_ , exist_ok=a_ ) _snake_case : Optional[Any] = os.path.join(a_ , """tmp""" ) os.makedirs(a_ , exist_ok=a_ ) _snake_case : int = read_json(os.path.join(a_ , """params.json""" ) ) _snake_case : Optional[Any] = NUM_SHARDS[model_size] _snake_case : str = params["""n_layers"""] _snake_case : List[Any] = params["""n_heads"""] _snake_case : str = n_heads // num_shards _snake_case : List[str] = params["""dim"""] _snake_case : Tuple = dim // n_heads _snake_case : Optional[Any] = 1_00_00.0 _snake_case : Dict = 1.0 / (base ** (torch.arange(0 , a_ , 2 ).float() / dims_per_head)) if "n_kv_heads" in params: _snake_case : Any = params["""n_kv_heads"""] # for GQA / MQA _snake_case : str = n_heads_per_shard // num_key_value_heads _snake_case : Union[str, Any] = dim // num_key_value_heads else: # compatibility with other checkpoints _snake_case : Tuple = n_heads _snake_case : List[str] = n_heads_per_shard _snake_case : Tuple = dim # permute for sliced rotary def permute(snake_case__ : Union[str, Any] , snake_case__ : Union[str, Any]=n_heads , snake_case__ : Union[str, Any]=dim , snake_case__ : int=dim ): return w.view(a_ , dima // n_heads // 2 , 2 , a_ ).transpose(1 , 2 ).reshape(a_ , a_ ) print(F"Fetching all parameters from the checkpoint at {input_base_path}." ) # Load weights if model_size == "7B": # Not sharded # (The sharded implementation would also work, but this is simpler.) _snake_case : Dict = torch.load(os.path.join(a_ , """consolidated.00.pth""" ) , map_location="""cpu""" ) else: # Sharded _snake_case : List[Any] = [ torch.load(os.path.join(a_ , F"consolidated.{i:02d}.pth" ) , map_location="""cpu""" ) for i in range(a_ ) ] _snake_case : Optional[int] = 0 _snake_case : Optional[Any] = {"""weight_map""": {}} for layer_i in range(a_ ): _snake_case : Dict = F"pytorch_model-{layer_i + 1}-of-{n_layers + 1}.bin" if model_size == "7B": # Unsharded _snake_case : Union[str, Any] = { F"model.layers.{layer_i}.self_attn.q_proj.weight": permute( loaded[F"layers.{layer_i}.attention.wq.weight"] ), F"model.layers.{layer_i}.self_attn.k_proj.weight": permute( loaded[F"layers.{layer_i}.attention.wk.weight"] ), F"model.layers.{layer_i}.self_attn.v_proj.weight": loaded[F"layers.{layer_i}.attention.wv.weight"], F"model.layers.{layer_i}.self_attn.o_proj.weight": loaded[F"layers.{layer_i}.attention.wo.weight"], F"model.layers.{layer_i}.mlp.gate_proj.weight": loaded[F"layers.{layer_i}.feed_forward.w1.weight"], F"model.layers.{layer_i}.mlp.down_proj.weight": loaded[F"layers.{layer_i}.feed_forward.w2.weight"], F"model.layers.{layer_i}.mlp.up_proj.weight": loaded[F"layers.{layer_i}.feed_forward.w3.weight"], F"model.layers.{layer_i}.input_layernorm.weight": loaded[F"layers.{layer_i}.attention_norm.weight"], F"model.layers.{layer_i}.post_attention_layernorm.weight": loaded[F"layers.{layer_i}.ffn_norm.weight"], } else: # Sharded # Note that attention.w{q,k,v,o}, feed_fordward.w[1,2,3], attention_norm.weight and ffn_norm.weight share # the same storage object, saving attention_norm and ffn_norm will save other weights too, which is # redundant as other weights will be stitched from multiple shards. To avoid that, they are cloned. _snake_case : Optional[Any] = { F"model.layers.{layer_i}.input_layernorm.weight": loaded[0][ F"layers.{layer_i}.attention_norm.weight" ].clone(), F"model.layers.{layer_i}.post_attention_layernorm.weight": loaded[0][ F"layers.{layer_i}.ffn_norm.weight" ].clone(), } _snake_case : Dict = permute( torch.cat( [ loaded[i][F"layers.{layer_i}.attention.wq.weight"].view(a_ , a_ , a_ ) for i in range(a_ ) ] , dim=0 , ).reshape(a_ , a_ ) ) _snake_case : str = permute( torch.cat( [ loaded[i][F"layers.{layer_i}.attention.wk.weight"].view( a_ , a_ , a_ ) for i in range(a_ ) ] , dim=0 , ).reshape(a_ , a_ ) , a_ , a_ , a_ , ) _snake_case : Optional[Any] = torch.cat( [ loaded[i][F"layers.{layer_i}.attention.wv.weight"].view( a_ , a_ , a_ ) for i in range(a_ ) ] , dim=0 , ).reshape(a_ , a_ ) _snake_case : Union[str, Any] = torch.cat( [loaded[i][F"layers.{layer_i}.attention.wo.weight"] for i in range(a_ )] , dim=1 ) _snake_case : Any = torch.cat( [loaded[i][F"layers.{layer_i}.feed_forward.w1.weight"] for i in range(a_ )] , dim=0 ) _snake_case : int = torch.cat( [loaded[i][F"layers.{layer_i}.feed_forward.w2.weight"] for i in range(a_ )] , dim=1 ) _snake_case : str = torch.cat( [loaded[i][F"layers.{layer_i}.feed_forward.w3.weight"] for i in range(a_ )] , dim=0 ) _snake_case : Tuple = inv_freq for k, v in state_dict.items(): _snake_case : Optional[Any] = filename param_count += v.numel() torch.save(a_ , os.path.join(a_ , a_ ) ) _snake_case : Union[str, Any] = F"pytorch_model-{n_layers + 1}-of-{n_layers + 1}.bin" if model_size == "7B": # Unsharded _snake_case : int = { """model.embed_tokens.weight""": loaded["""tok_embeddings.weight"""], """model.norm.weight""": loaded["""norm.weight"""], """lm_head.weight""": loaded["""output.weight"""], } else: _snake_case : Optional[Any] = { """model.norm.weight""": loaded[0]["""norm.weight"""], """model.embed_tokens.weight""": torch.cat( [loaded[i]["""tok_embeddings.weight"""] for i in range(a_ )] , dim=1 ), """lm_head.weight""": torch.cat([loaded[i]["""output.weight"""] for i in range(a_ )] , dim=0 ), } for k, v in state_dict.items(): _snake_case : int = filename param_count += v.numel() torch.save(a_ , os.path.join(a_ , a_ ) ) # Write configs _snake_case : Union[str, Any] = {"""total_size""": param_count * 2} write_json(a_ , os.path.join(a_ , """pytorch_model.bin.index.json""" ) ) _snake_case : Tuple = params["""ffn_dim_multiplier"""] if """ffn_dim_multiplier""" in params else 1 _snake_case : int = params["""multiple_of"""] if """multiple_of""" in params else 2_56 _snake_case : str = LlamaConfig( hidden_size=a_ , intermediate_size=compute_intermediate_size(a_ , a_ , a_ ) , num_attention_heads=params["""n_heads"""] , num_hidden_layers=params["""n_layers"""] , rms_norm_eps=params["""norm_eps"""] , num_key_value_heads=a_ , ) config.save_pretrained(a_ ) # Make space so we can load the model properly now. del state_dict del loaded gc.collect() print("""Loading the checkpoint in a Llama model.""" ) _snake_case : str = LlamaForCausalLM.from_pretrained(a_ , torch_dtype=torch.floataa , low_cpu_mem_usage=a_ ) # Avoid saving this as part of the config. del model.config._name_or_path print("""Saving in the Transformers format.""" ) model.save_pretrained(a_ , safe_serialization=a_ ) shutil.rmtree(a_ ) def UpperCAmelCase__ (snake_case__ : Tuple , snake_case__ : Optional[Any] ): """simple docstring""" _snake_case : int = LlamaTokenizer if LlamaTokenizerFast is None else LlamaTokenizerFast print(F"Saving a {tokenizer_class.__name__} to {tokenizer_path}." ) _snake_case : List[Any] = tokenizer_class(a_ ) tokenizer.save_pretrained(a_ ) def UpperCAmelCase__ (): """simple docstring""" _snake_case : Tuple = argparse.ArgumentParser() parser.add_argument( """--input_dir""" , help="""Location of LLaMA weights, which contains tokenizer.model and model folders""" , ) parser.add_argument( """--model_size""" , choices=["""7B""", """7Bf""", """13B""", """13Bf""", """30B""", """65B""", """70B""", """70Bf""", """tokenizer_only"""] , ) parser.add_argument( """--output_dir""" , help="""Location to write HF model and tokenizer""" , ) parser.add_argument("""--safe_serialization""" , type=a_ , help="""Whether or not to save using `safetensors`.""" ) _snake_case : Union[str, Any] = parser.parse_args() if args.model_size != "tokenizer_only": write_model( model_path=args.output_dir , input_base_path=os.path.join(args.input_dir , args.model_size ) , model_size=args.model_size , safe_serialization=args.safe_serialization , ) _snake_case : Optional[int] = os.path.join(args.input_dir , """tokenizer.model""" ) write_tokenizer(args.output_dir , a_ ) if __name__ == "__main__": main()
364
"""simple docstring""" import os import re import shutil from argparse import ArgumentParser, Namespace from datasets.commands import BaseDatasetsCLICommand from datasets.utils.logging import get_logger A_ = '''<<<<<<< This should probably be modified because it mentions: ''' A_ = '''======= >>>>>>> ''' A_ = [ '''TextEncoderConfig''', '''ByteTextEncoder''', '''SubwordTextEncoder''', '''encoder_config''', '''maybe_build_from_corpus''', '''manual_dir''', ] A_ = [ # (pattern, replacement) # Order is important here for some replacements (r'''tfds\.core''', r'''datasets'''), (r'''tf\.io\.gfile\.GFile''', r'''open'''), (r'''tf\.([\w\d]+)''', r'''datasets.Value(\'\1\')'''), (r'''tfds\.features\.Text\(\)''', r'''datasets.Value(\'string\')'''), (r'''tfds\.features\.Text\(''', r'''datasets.Value(\'string\'),'''), (r'''features\s*=\s*tfds.features.FeaturesDict\(''', r'''features=datasets.Features('''), (r'''tfds\.features\.FeaturesDict\(''', r'''dict('''), (r'''The TensorFlow Datasets Authors''', r'''The TensorFlow Datasets Authors and the HuggingFace Datasets Authors'''), (r'''tfds\.''', r'''datasets.'''), (r'''dl_manager\.manual_dir''', r'''self.config.data_dir'''), (r'''self\.builder_config''', r'''self.config'''), ] def UpperCAmelCase__ (snake_case__ : Namespace ): """simple docstring""" return ConvertCommand(args.tfds_path , args.datasets_directory ) class lowercase( __a ): '''simple docstring''' @staticmethod def UpperCamelCase_ ( a_: ArgumentParser ): '''simple docstring''' _snake_case : Tuple = parser.add_parser( """convert""", help="""Convert a TensorFlow Datasets dataset to a HuggingFace Datasets dataset.""", ) train_parser.add_argument( """--tfds_path""", type=a_, required=a_, help="""Path to a TensorFlow Datasets folder to convert or a single tfds file to convert.""", ) train_parser.add_argument( """--datasets_directory""", type=a_, required=a_, help="""Path to the HuggingFace Datasets folder.""" ) train_parser.set_defaults(func=a_ ) def __init__( self: List[str], a_: str, a_: str, *a_: str ): '''simple docstring''' _snake_case : Optional[Any] = get_logger("""datasets-cli/converting""" ) _snake_case : Any = tfds_path _snake_case : Optional[Any] = datasets_directory def UpperCamelCase_ ( self: Optional[int] ): '''simple docstring''' if os.path.isdir(self._tfds_path ): _snake_case : int = os.path.abspath(self._tfds_path ) elif os.path.isfile(self._tfds_path ): _snake_case : Any = os.path.dirname(self._tfds_path ) else: raise ValueError("""--tfds_path is neither a directory nor a file. Please check path.""" ) _snake_case : Union[str, Any] = os.path.abspath(self._datasets_directory ) self._logger.info(f"Converting datasets from {abs_tfds_path} to {abs_datasets_path}" ) _snake_case : Tuple = [] _snake_case : Dict = [] _snake_case : Optional[Any] = {} if os.path.isdir(self._tfds_path ): _snake_case : List[str] = os.listdir(a_ ) else: _snake_case : int = [os.path.basename(self._tfds_path )] for f_name in file_names: self._logger.info(f"Looking at file {f_name}" ) _snake_case : Dict = os.path.join(a_, a_ ) _snake_case : Union[str, Any] = os.path.join(a_, a_ ) if not os.path.isfile(a_ ) or "__init__" in f_name or "_test" in f_name or ".py" not in f_name: self._logger.info("""Skipping file""" ) continue with open(a_, encoding="""utf-8""" ) as f: _snake_case : str = f.readlines() _snake_case : List[str] = [] _snake_case : Any = False _snake_case : Union[str, Any] = False _snake_case : Optional[Any] = [] for line in lines: _snake_case : Optional[int] = line # Convert imports if "import tensorflow.compat.v2 as tf" in out_line: continue elif "@tfds.core" in out_line: continue elif "builder=self" in out_line: continue elif "import tensorflow_datasets.public_api as tfds" in out_line: _snake_case : Optional[Any] = """import datasets\n""" elif "import tensorflow" in out_line: # order is important here _snake_case : Optional[int] = """""" continue elif "from absl import logging" in out_line: _snake_case : int = """from datasets import logging\n""" elif "getLogger" in out_line: _snake_case : Any = out_line.replace("""getLogger""", """get_logger""" ) elif any(expression in out_line for expression in TO_HIGHLIGHT ): _snake_case : Union[str, Any] = True _snake_case : Optional[Any] = list(filter(lambda a_ : e in out_line, a_ ) ) out_lines.append(HIGHLIGHT_MESSAGE_PRE + str(a_ ) + """\n""" ) out_lines.append(a_ ) out_lines.append(a_ ) continue else: for pattern, replacement in TO_CONVERT: _snake_case : List[str] = re.sub(a_, a_, a_ ) # Take care of saving utilities (to later move them together with main script) if "tensorflow_datasets" in out_line: _snake_case : Dict = re.match(r"""from\stensorflow_datasets.*import\s([^\.\r\n]+)""", a_ ) tfds_imports.extend(imp.strip() for imp in match.group(1 ).split(""",""" ) ) _snake_case : Optional[Any] = """from . import """ + match.group(1 ) # Check we have not forget anything if "tf." in out_line or "tfds." in out_line or "tensorflow_datasets" in out_line: raise ValueError(f"Error converting {out_line.strip()}" ) if "GeneratorBasedBuilder" in out_line or "BeamBasedBuilder" in out_line: _snake_case : Tuple = True out_lines.append(a_ ) if is_builder or "wmt" in f_name: # We create a new directory for each dataset _snake_case : List[str] = f_name.replace(""".py""", """""" ) _snake_case : str = os.path.join(a_, a_ ) _snake_case : str = os.path.join(a_, a_ ) os.makedirs(a_, exist_ok=a_ ) self._logger.info(f"Adding directory {output_dir}" ) imports_to_builder_map.update({imp: output_dir for imp in tfds_imports} ) else: # Utilities will be moved at the end utils_files.append(a_ ) if needs_manual_update: with_manual_update.append(a_ ) with open(a_, """w""", encoding="""utf-8""" ) as f: f.writelines(a_ ) self._logger.info(f"Converted in {output_file}" ) for utils_file in utils_files: try: _snake_case : Optional[int] = os.path.basename(a_ ) _snake_case : Optional[Any] = imports_to_builder_map[f_name.replace(""".py""", """""" )] self._logger.info(f"Moving {dest_folder} to {utils_file}" ) shutil.copy(a_, a_ ) except KeyError: self._logger.error(f"Cannot find destination folder for {utils_file}. Please copy manually." ) if with_manual_update: for file_path in with_manual_update: self._logger.warning( f"You need to manually update file {file_path} to remove configurations using 'TextEncoderConfig'." )
132
0
from __future__ import annotations from itertools import permutations from random import randint from timeit import repeat def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE : List[Any] = [randint(-1000 , 1000 ) for i in range(10 )] __SCREAMING_SNAKE_CASE : Tuple = randint(-5000 , 5000 ) return (arr, r) __lowerCAmelCase : List[Any] =make_dataset() def _UpperCamelCase ( lowercase__ , lowercase__ ): for triplet in permutations(lowercase__ , 3 ): if sum(lowercase__ ) == target: return tuple(sorted(lowercase__ ) ) return (0, 0, 0) def _UpperCamelCase ( lowercase__ , lowercase__ ): arr.sort() __SCREAMING_SNAKE_CASE : Any = len(lowercase__ ) for i in range(n - 1 ): __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Tuple = i + 1, n - 1 while left < right: if arr[i] + arr[left] + arr[right] == target: return (arr[i], arr[left], arr[right]) elif arr[i] + arr[left] + arr[right] < target: left += 1 elif arr[i] + arr[left] + arr[right] > target: right -= 1 return (0, 0, 0) def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE : Optional[int] = ''' from __main__ import dataset, triplet_sum1, triplet_sum2 ''' __SCREAMING_SNAKE_CASE : List[str] = ''' triplet_sum1(*dataset) ''' __SCREAMING_SNAKE_CASE : Any = ''' triplet_sum2(*dataset) ''' __SCREAMING_SNAKE_CASE : Any = repeat(setup=lowercase__ , stmt=lowercase__ , repeat=5 , number=10000 ) __SCREAMING_SNAKE_CASE : List[str] = repeat(setup=lowercase__ , stmt=lowercase__ , repeat=5 , number=10000 ) return (min(lowercase__ ), min(lowercase__ )) if __name__ == "__main__": from doctest import testmod testmod() __lowerCAmelCase : List[Any] =solution_times() print(f"""The time for naive implementation is {times[0]}.""") print(f"""The time for optimized implementation is {times[1]}.""")
9
import logging import os import sys from dataclasses import dataclass, field from typing import Optional from seqaseq_trainer import SeqaSeqTrainer from seqaseq_training_args import SeqaSeqTrainingArguments import transformers from transformers import ( AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer, HfArgumentParser, MBartTokenizer, MBartTokenizerFast, set_seed, ) from transformers.trainer_utils import EvaluationStrategy, is_main_process from transformers.training_args import ParallelMode from utils import ( SeqaSeqDataCollator, SeqaSeqDataset, assert_all_frozen, build_compute_metrics_fn, check_output_dir, freeze_embeds, freeze_params, lmap, save_json, use_task_specific_params, write_txt_file, ) snake_case_ = logging.getLogger(__name__) @dataclass class SCREAMING_SNAKE_CASE__ : A_ : str = field( metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} ) A_ : Optional[str] = field( default=_UpperCAmelCase , metadata={'help': 'Pretrained config name or path if not the same as model_name'} ) A_ : Optional[str] = field( default=_UpperCAmelCase , metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'} ) A_ : Optional[str] = field( default=_UpperCAmelCase , metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'} , ) A_ : bool = field(default=_UpperCAmelCase , metadata={'help': 'Whether tp freeze the encoder.'} ) A_ : bool = field(default=_UpperCAmelCase , metadata={'help': 'Whether to freeze the embeddings.'} ) @dataclass class SCREAMING_SNAKE_CASE__ : A_ : str = field( metadata={'help': 'The input data dir. Should contain the .tsv files (or other data files) for the task.'} ) A_ : Optional[str] = field( default='summarization' , metadata={'help': 'Task name, summarization (or summarization_{dataset} for pegasus) or translation'} , ) A_ : Optional[int] = field( default=1_024 , metadata={ 'help': ( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) } , ) A_ : Optional[int] = field( default=128 , metadata={ 'help': ( 'The maximum total sequence length for target text after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) } , ) A_ : Optional[int] = field( default=142 , metadata={ 'help': ( 'The maximum total sequence length for validation target text after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded. ' 'This argument is also used to override the ``max_length`` param of ``model.generate``, which is used ' 'during ``evaluate`` and ``predict``.' ) } , ) A_ : Optional[int] = field( default=142 , metadata={ 'help': ( 'The maximum total sequence length for test target text after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) } , ) A_ : Optional[int] = field(default=-1 , metadata={'help': '# training examples. -1 means use all.'} ) A_ : Optional[int] = field(default=-1 , metadata={'help': '# validation examples. -1 means use all.'} ) A_ : Optional[int] = field(default=-1 , metadata={'help': '# test examples. -1 means use all.'} ) A_ : Optional[str] = field(default=_UpperCAmelCase , metadata={'help': 'Source language id for translation.'} ) A_ : Optional[str] = field(default=_UpperCAmelCase , metadata={'help': 'Target language id for translation.'} ) A_ : Optional[int] = field(default=_UpperCAmelCase , metadata={'help': '# num_beams to use for evaluation.'} ) A_ : bool = field( default=_UpperCAmelCase , metadata={'help': 'If only pad tokens should be ignored. This assumes that `config.pad_token_id` is defined.'} , ) def lowerCamelCase__ ( snake_case_ : List[Any] , snake_case_ : List[str] , snake_case_ : Dict ) -> str: logger.info(f"""***** {split} metrics *****""" ) for key in sorted(metrics.keys() ): logger.info(f""" {key} = {metrics[key]}""" ) save_json(snake_case_ , os.path.join(snake_case_ , f"""{split}_results.json""" ) ) def lowerCamelCase__ ( ) -> 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. __snake_case = HfArgumentParser((ModelArguments, DataTrainingArguments, SeqaSeqTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('''.json''' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. __snake_case , __snake_case , __snake_case = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: __snake_case , __snake_case , __snake_case = parser.parse_args_into_dataclasses() check_output_dir(snake_case_ ) # 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.parallel_mode == ParallelMode.DISTRIBUTED ) , training_args.fpaa , ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() logger.info('''Training/evaluation parameters %s''' , snake_case_ ) # Set seed 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. __snake_case = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) __snake_case = ('''encoder_layerdrop''', '''decoder_layerdrop''', '''dropout''', '''attention_dropout''') for p in extra_model_params: if getattr(snake_case_ , snake_case_ , snake_case_ ): assert hasattr(snake_case_ , snake_case_ ), f"""({config.__class__.__name__}) doesn't have a `{p}` attribute""" setattr(snake_case_ , snake_case_ , getattr(snake_case_ , snake_case_ ) ) __snake_case = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) __snake_case = AutoModelForSeqaSeqLM.from_pretrained( model_args.model_name_or_path , from_tf='''.ckpt''' in model_args.model_name_or_path , config=snake_case_ , cache_dir=model_args.cache_dir , ) # use task specific params use_task_specific_params(snake_case_ , data_args.task ) # set num_beams for evaluation if data_args.eval_beams is None: __snake_case = model.config.num_beams # set decoder_start_token_id for MBart if model.config.decoder_start_token_id is None and isinstance(snake_case_ , (MBartTokenizer, MBartTokenizerFast) ): assert ( data_args.tgt_lang is not None and data_args.src_lang is not None ), "mBart requires --tgt_lang and --src_lang" if isinstance(snake_case_ , snake_case_ ): __snake_case = tokenizer.lang_code_to_id[data_args.tgt_lang] else: __snake_case = tokenizer.convert_tokens_to_ids(data_args.tgt_lang ) if model_args.freeze_embeds: freeze_embeds(snake_case_ ) if model_args.freeze_encoder: freeze_params(model.get_encoder() ) assert_all_frozen(model.get_encoder() ) __snake_case = SeqaSeqDataset # Get datasets __snake_case = ( dataset_class( snake_case_ , type_path='''train''' , data_dir=data_args.data_dir , n_obs=data_args.n_train , max_target_length=data_args.max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or '''''' , ) if training_args.do_train else None ) __snake_case = ( dataset_class( snake_case_ , type_path='''val''' , data_dir=data_args.data_dir , n_obs=data_args.n_val , max_target_length=data_args.val_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or '''''' , ) if training_args.do_eval or training_args.evaluation_strategy != EvaluationStrategy.NO else None ) __snake_case = ( dataset_class( snake_case_ , type_path='''test''' , data_dir=data_args.data_dir , n_obs=data_args.n_test , max_target_length=data_args.test_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or '''''' , ) if training_args.do_predict else None ) # Initialize our Trainer __snake_case = ( build_compute_metrics_fn(data_args.task , snake_case_ ) if training_args.predict_with_generate else None ) __snake_case = SeqaSeqTrainer( model=snake_case_ , args=snake_case_ , data_args=snake_case_ , train_dataset=snake_case_ , eval_dataset=snake_case_ , data_collator=SeqaSeqDataCollator( snake_case_ , snake_case_ , model.config.decoder_start_token_id , training_args.tpu_num_cores ) , compute_metrics=snake_case_ , tokenizer=snake_case_ , ) __snake_case = {} # Training if training_args.do_train: logger.info('''*** Train ***''' ) __snake_case = trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) __snake_case = train_result.metrics __snake_case = data_args.n_train trainer.save_model() # this also saves the tokenizer if trainer.is_world_process_zero(): handle_metrics('''train''' , snake_case_ , training_args.output_dir ) all_metrics.update(snake_case_ ) # Need to save the state, since Trainer.save_model saves only the tokenizer with the model trainer.state.save_to_json(os.path.join(training_args.output_dir , '''trainer_state.json''' ) ) # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) tokenizer.save_pretrained(training_args.output_dir ) # Evaluation if training_args.do_eval: logger.info('''*** Evaluate ***''' ) __snake_case = trainer.evaluate(metric_key_prefix='''val''' ) __snake_case = data_args.n_val __snake_case = round(metrics['''val_loss'''] , 4 ) if trainer.is_world_process_zero(): handle_metrics('''val''' , snake_case_ , training_args.output_dir ) all_metrics.update(snake_case_ ) if training_args.do_predict: logger.info('''*** Predict ***''' ) __snake_case = trainer.predict(test_dataset=snake_case_ , metric_key_prefix='''test''' ) __snake_case = test_output.metrics __snake_case = data_args.n_test if trainer.is_world_process_zero(): __snake_case = round(metrics['''test_loss'''] , 4 ) handle_metrics('''test''' , snake_case_ , training_args.output_dir ) all_metrics.update(snake_case_ ) if training_args.predict_with_generate: __snake_case = tokenizer.batch_decode( test_output.predictions , skip_special_tokens=snake_case_ , clean_up_tokenization_spaces=snake_case_ ) __snake_case = lmap(str.strip , snake_case_ ) write_txt_file(snake_case_ , os.path.join(training_args.output_dir , '''test_generations.txt''' ) ) if trainer.is_world_process_zero(): save_json(snake_case_ , os.path.join(training_args.output_dir , '''all_results.json''' ) ) return all_metrics def lowerCamelCase__ ( snake_case_ : Optional[Any] ) -> Tuple: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
24
0
"""simple docstring""" import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_funnel import FunnelTokenizer UpperCAmelCase : Tuple = logging.get_logger(__name__) UpperCAmelCase : List[Any] = {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'} UpperCAmelCase : Dict = [ 'small', 'small-base', 'medium', 'medium-base', 'intermediate', 'intermediate-base', 'large', 'large-base', 'xlarge', 'xlarge-base', ] UpperCAmelCase : Optional[int] = { 'vocab_file': { 'funnel-transformer/small': 'https://huggingface.co/funnel-transformer/small/resolve/main/vocab.txt', 'funnel-transformer/small-base': 'https://huggingface.co/funnel-transformer/small-base/resolve/main/vocab.txt', 'funnel-transformer/medium': 'https://huggingface.co/funnel-transformer/medium/resolve/main/vocab.txt', 'funnel-transformer/medium-base': ( 'https://huggingface.co/funnel-transformer/medium-base/resolve/main/vocab.txt' ), 'funnel-transformer/intermediate': ( 'https://huggingface.co/funnel-transformer/intermediate/resolve/main/vocab.txt' ), 'funnel-transformer/intermediate-base': ( 'https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/vocab.txt' ), 'funnel-transformer/large': 'https://huggingface.co/funnel-transformer/large/resolve/main/vocab.txt', 'funnel-transformer/large-base': 'https://huggingface.co/funnel-transformer/large-base/resolve/main/vocab.txt', 'funnel-transformer/xlarge': 'https://huggingface.co/funnel-transformer/xlarge/resolve/main/vocab.txt', 'funnel-transformer/xlarge-base': ( 'https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'funnel-transformer/small': 'https://huggingface.co/funnel-transformer/small/resolve/main/tokenizer.json', 'funnel-transformer/small-base': ( 'https://huggingface.co/funnel-transformer/small-base/resolve/main/tokenizer.json' ), 'funnel-transformer/medium': 'https://huggingface.co/funnel-transformer/medium/resolve/main/tokenizer.json', 'funnel-transformer/medium-base': ( 'https://huggingface.co/funnel-transformer/medium-base/resolve/main/tokenizer.json' ), 'funnel-transformer/intermediate': ( 'https://huggingface.co/funnel-transformer/intermediate/resolve/main/tokenizer.json' ), 'funnel-transformer/intermediate-base': ( 'https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/tokenizer.json' ), 'funnel-transformer/large': 'https://huggingface.co/funnel-transformer/large/resolve/main/tokenizer.json', 'funnel-transformer/large-base': ( 'https://huggingface.co/funnel-transformer/large-base/resolve/main/tokenizer.json' ), 'funnel-transformer/xlarge': 'https://huggingface.co/funnel-transformer/xlarge/resolve/main/tokenizer.json', 'funnel-transformer/xlarge-base': ( 'https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/tokenizer.json' ), }, } UpperCAmelCase : Optional[int] = {F"funnel-transformer/{name}": 512 for name in _model_names} UpperCAmelCase : Dict = {F"funnel-transformer/{name}": {'do_lower_case': True} for name in _model_names} class lowerCamelCase__ ( A ): """simple docstring""" __a = VOCAB_FILES_NAMES __a = PRETRAINED_VOCAB_FILES_MAP __a = PRETRAINED_INIT_CONFIGURATION __a = FunnelTokenizer __a = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __a = 2 def __init__( self : Dict , UpperCamelCase : Optional[int]=None , UpperCamelCase : Optional[int]=None , UpperCamelCase : Optional[Any]=True , UpperCamelCase : Union[str, Any]="<unk>" , UpperCamelCase : Any="<sep>" , UpperCamelCase : Union[str, Any]="<pad>" , UpperCamelCase : str="<cls>" , UpperCamelCase : Optional[Any]="<mask>" , UpperCamelCase : int="<s>" , UpperCamelCase : List[Any]="</s>" , UpperCamelCase : List[str]=True , UpperCamelCase : Union[str, Any]=True , UpperCamelCase : Tuple=None , UpperCamelCase : Dict="##" , **UpperCamelCase : str , ): '''simple docstring''' 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 , bos_token=UpperCamelCase , eos_token=UpperCamelCase , clean_text=UpperCamelCase , tokenize_chinese_chars=UpperCamelCase , strip_accents=UpperCamelCase , wordpieces_prefix=UpperCamelCase , **UpperCamelCase , ) __UpperCAmelCase : Optional[Any] = 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 ): __UpperCAmelCase : List[str] = getattr(UpperCamelCase , normalizer_state.pop("""type""" ) ) __UpperCAmelCase : Dict = do_lower_case __UpperCAmelCase : int = strip_accents __UpperCAmelCase : Optional[int] = tokenize_chinese_chars __UpperCAmelCase : Optional[int] = normalizer_class(**UpperCamelCase ) __UpperCAmelCase : Optional[int] = do_lower_case def lowerCamelCase__ ( self : List[str] , UpperCamelCase : str , UpperCamelCase : List[str]=None ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def lowerCamelCase__ ( self : Optional[Any] , UpperCamelCase : List[int] , UpperCamelCase : Optional[List[int]] = None ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = [self.sep_token_id] __UpperCAmelCase : Optional[Any] = [self.cls_token_id] if token_ids_a is None: return len(cls ) * [self.cls_token_type_id] + len(token_ids_a + sep ) * [0] return len(cls ) * [self.cls_token_type_id] + len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def lowerCamelCase__ ( self : str , UpperCamelCase : str , UpperCamelCase : Optional[str] = None ): '''simple docstring''' __UpperCAmelCase : List[str] = self._tokenizer.model.save(UpperCamelCase , name=UpperCamelCase ) return tuple(UpperCamelCase )
320
"""simple docstring""" from __future__ import annotations import unittest from transformers import FunnelConfig, is_tf_available from transformers.testing_utils import require_tf from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFFunnelBaseModel, TFFunnelForMaskedLM, TFFunnelForMultipleChoice, TFFunnelForPreTraining, TFFunnelForQuestionAnswering, TFFunnelForSequenceClassification, TFFunnelForTokenClassification, TFFunnelModel, ) class lowerCamelCase__ : """simple docstring""" def __init__( self : List[str] , UpperCamelCase : int , UpperCamelCase : List[Any]=13 , UpperCamelCase : Tuple=7 , UpperCamelCase : Optional[int]=True , UpperCamelCase : Optional[int]=True , UpperCamelCase : Dict=True , UpperCamelCase : List[Any]=True , UpperCamelCase : int=99 , UpperCamelCase : Any=[1, 1, 2] , UpperCamelCase : Optional[Any]=1 , UpperCamelCase : Optional[Any]=32 , UpperCamelCase : Optional[int]=4 , UpperCamelCase : Union[str, Any]=8 , UpperCamelCase : int=37 , UpperCamelCase : Optional[Any]="gelu_new" , UpperCamelCase : Any=0.1 , UpperCamelCase : int=0.1 , UpperCamelCase : int=0.0 , UpperCamelCase : Union[str, Any]=512 , UpperCamelCase : Any=3 , UpperCamelCase : Optional[int]=0.02 , UpperCamelCase : Union[str, Any]=3 , UpperCamelCase : Union[str, Any]=4 , UpperCamelCase : str=None , UpperCamelCase : Tuple=False , ): '''simple docstring''' __UpperCAmelCase : int = parent __UpperCAmelCase : int = batch_size __UpperCAmelCase : str = seq_length __UpperCAmelCase : Optional[Any] = is_training __UpperCAmelCase : Optional[Any] = use_input_mask __UpperCAmelCase : Tuple = use_token_type_ids __UpperCAmelCase : List[str] = use_labels __UpperCAmelCase : Tuple = vocab_size __UpperCAmelCase : Optional[int] = block_sizes __UpperCAmelCase : Optional[Any] = num_decoder_layers __UpperCAmelCase : Union[str, Any] = d_model __UpperCAmelCase : Dict = n_head __UpperCAmelCase : Optional[Any] = d_head __UpperCAmelCase : Dict = d_inner __UpperCAmelCase : Any = hidden_act __UpperCAmelCase : Optional[Any] = hidden_dropout __UpperCAmelCase : List[Any] = attention_dropout __UpperCAmelCase : str = activation_dropout __UpperCAmelCase : Union[str, Any] = max_position_embeddings __UpperCAmelCase : List[Any] = type_vocab_size __UpperCAmelCase : str = 2 __UpperCAmelCase : Optional[Any] = num_labels __UpperCAmelCase : List[Any] = num_choices __UpperCAmelCase : Any = scope __UpperCAmelCase : Dict = initializer_std # Used in the tests to check the size of the first attention layer __UpperCAmelCase : Dict = n_head # Used in the tests to check the size of the first hidden state __UpperCAmelCase : Dict = self.d_model # Used in the tests to check the number of output hidden states/attentions __UpperCAmelCase : Dict = sum(self.block_sizes ) + (0 if base else self.num_decoder_layers) # FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with # the last hidden state of the first block (which is the first hidden state of the decoder). if not base: __UpperCAmelCase : List[Any] = self.num_hidden_layers + 2 def lowerCamelCase__ ( self : Any ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __UpperCAmelCase : List[str] = None if self.use_input_mask: __UpperCAmelCase : Optional[int] = random_attention_mask([self.batch_size, self.seq_length] ) __UpperCAmelCase : int = None if self.use_token_type_ids: __UpperCAmelCase : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __UpperCAmelCase : List[Any] = None __UpperCAmelCase : Dict = None __UpperCAmelCase : Optional[Any] = None if self.use_labels: __UpperCAmelCase : Tuple = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __UpperCAmelCase : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __UpperCAmelCase : List[Any] = ids_tensor([self.batch_size] , self.num_choices ) __UpperCAmelCase : str = FunnelConfig( vocab_size=self.vocab_size , block_sizes=self.block_sizes , num_decoder_layers=self.num_decoder_layers , d_model=self.d_model , n_head=self.n_head , d_head=self.d_head , d_inner=self.d_inner , hidden_act=self.hidden_act , hidden_dropout=self.hidden_dropout , attention_dropout=self.attention_dropout , activation_dropout=self.activation_dropout , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_std=self.initializer_std , ) return ( config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels, ) def lowerCamelCase__ ( self : Any , UpperCamelCase : Any , UpperCamelCase : Tuple , UpperCamelCase : List[Any] , UpperCamelCase : Any , UpperCamelCase : str , UpperCamelCase : List[Any] , UpperCamelCase : Optional[int] , ): '''simple docstring''' __UpperCAmelCase : List[Any] = TFFunnelModel(config=UpperCamelCase ) __UpperCAmelCase : List[str] = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} __UpperCAmelCase : List[str] = model(UpperCamelCase ) __UpperCAmelCase : List[Any] = [input_ids, input_mask] __UpperCAmelCase : Dict = model(UpperCamelCase ) __UpperCAmelCase : Tuple = model(UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.d_model) ) __UpperCAmelCase : int = False __UpperCAmelCase : Optional[int] = TFFunnelModel(config=UpperCamelCase ) __UpperCAmelCase : Union[str, Any] = model(UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.d_model) ) __UpperCAmelCase : Any = False __UpperCAmelCase : Optional[int] = TFFunnelModel(config=UpperCamelCase ) __UpperCAmelCase : List[str] = model(UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.d_model) ) def lowerCamelCase__ ( self : Optional[int] , UpperCamelCase : Optional[int] , UpperCamelCase : Any , UpperCamelCase : Optional[int] , UpperCamelCase : List[Any] , UpperCamelCase : str , UpperCamelCase : List[Any] , UpperCamelCase : Any , ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = TFFunnelBaseModel(config=UpperCamelCase ) __UpperCAmelCase : List[Any] = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} __UpperCAmelCase : Optional[Any] = model(UpperCamelCase ) __UpperCAmelCase : int = [input_ids, input_mask] __UpperCAmelCase : int = model(UpperCamelCase ) __UpperCAmelCase : List[Any] = model(UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, 2, self.d_model) ) __UpperCAmelCase : List[Any] = False __UpperCAmelCase : str = TFFunnelBaseModel(config=UpperCamelCase ) __UpperCAmelCase : Union[str, Any] = model(UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, 3, self.d_model) ) __UpperCAmelCase : int = False __UpperCAmelCase : str = TFFunnelBaseModel(config=UpperCamelCase ) __UpperCAmelCase : str = model(UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, 2, self.d_model) ) def lowerCamelCase__ ( self : Union[str, Any] , UpperCamelCase : Any , UpperCamelCase : Optional[int] , UpperCamelCase : Tuple , UpperCamelCase : int , UpperCamelCase : str , UpperCamelCase : Optional[Any] , UpperCamelCase : Optional[Any] , ): '''simple docstring''' __UpperCAmelCase : Tuple = TFFunnelForPreTraining(config=UpperCamelCase ) __UpperCAmelCase : List[str] = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} __UpperCAmelCase : int = model(UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length) ) def lowerCamelCase__ ( self : str , UpperCamelCase : Union[str, Any] , UpperCamelCase : int , UpperCamelCase : Dict , UpperCamelCase : Dict , UpperCamelCase : Tuple , UpperCamelCase : Tuple , UpperCamelCase : int , ): '''simple docstring''' __UpperCAmelCase : int = TFFunnelForMaskedLM(config=UpperCamelCase ) __UpperCAmelCase : str = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} __UpperCAmelCase : Optional[Any] = model(UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowerCamelCase__ ( self : Optional[Any] , UpperCamelCase : List[str] , UpperCamelCase : Optional[int] , UpperCamelCase : Optional[int] , UpperCamelCase : str , UpperCamelCase : Optional[int] , UpperCamelCase : Union[str, Any] , UpperCamelCase : Optional[int] , ): '''simple docstring''' __UpperCAmelCase : Dict = self.num_labels __UpperCAmelCase : Optional[Any] = TFFunnelForSequenceClassification(config=UpperCamelCase ) __UpperCAmelCase : Optional[Any] = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} __UpperCAmelCase : Tuple = model(UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowerCamelCase__ ( self : List[Any] , UpperCamelCase : Optional[Any] , UpperCamelCase : str , UpperCamelCase : str , UpperCamelCase : List[Any] , UpperCamelCase : List[Any] , UpperCamelCase : int , UpperCamelCase : int , ): '''simple docstring''' __UpperCAmelCase : Dict = self.num_choices __UpperCAmelCase : str = TFFunnelForMultipleChoice(config=UpperCamelCase ) __UpperCAmelCase : Optional[Any] = tf.tile(tf.expand_dims(UpperCamelCase , 1 ) , (1, self.num_choices, 1) ) __UpperCAmelCase : str = tf.tile(tf.expand_dims(UpperCamelCase , 1 ) , (1, self.num_choices, 1) ) __UpperCAmelCase : int = tf.tile(tf.expand_dims(UpperCamelCase , 1 ) , (1, self.num_choices, 1) ) __UpperCAmelCase : List[str] = { """input_ids""": multiple_choice_inputs_ids, """attention_mask""": multiple_choice_input_mask, """token_type_ids""": multiple_choice_token_type_ids, } __UpperCAmelCase : int = model(UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def lowerCamelCase__ ( self : List[str] , UpperCamelCase : str , UpperCamelCase : Union[str, Any] , UpperCamelCase : Tuple , UpperCamelCase : Any , UpperCamelCase : List[Any] , UpperCamelCase : int , UpperCamelCase : Any , ): '''simple docstring''' __UpperCAmelCase : int = self.num_labels __UpperCAmelCase : str = TFFunnelForTokenClassification(config=UpperCamelCase ) __UpperCAmelCase : Dict = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} __UpperCAmelCase : int = model(UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowerCamelCase__ ( self : str , UpperCamelCase : int , UpperCamelCase : Any , UpperCamelCase : List[str] , UpperCamelCase : str , UpperCamelCase : Union[str, Any] , UpperCamelCase : Optional[Any] , UpperCamelCase : List[Any] , ): '''simple docstring''' __UpperCAmelCase : Any = TFFunnelForQuestionAnswering(config=UpperCamelCase ) __UpperCAmelCase : List[str] = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} __UpperCAmelCase : Any = model(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 : Optional[int] ): '''simple docstring''' __UpperCAmelCase : List[Any] = self.prepare_config_and_inputs() ( ( __UpperCAmelCase ) ,( __UpperCAmelCase ) ,( __UpperCAmelCase ) ,( __UpperCAmelCase ) ,( __UpperCAmelCase ) ,( __UpperCAmelCase ) ,( __UpperCAmelCase ) , ) : Dict = config_and_inputs __UpperCAmelCase : int = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask} return config, inputs_dict @require_tf class lowerCamelCase__ ( A , A , unittest.TestCase ): """simple docstring""" __a = ( ( TFFunnelModel, TFFunnelForMaskedLM, TFFunnelForPreTraining, TFFunnelForQuestionAnswering, TFFunnelForTokenClassification, ) if is_tf_available() else () ) __a = ( { """feature-extraction""": (TFFunnelBaseModel, TFFunnelModel), """fill-mask""": TFFunnelForMaskedLM, """question-answering""": TFFunnelForQuestionAnswering, """text-classification""": TFFunnelForSequenceClassification, """token-classification""": TFFunnelForTokenClassification, """zero-shot""": TFFunnelForSequenceClassification, } if is_tf_available() else {} ) __a = False __a = False def lowerCamelCase__ ( self : Dict ): '''simple docstring''' __UpperCAmelCase : List[Any] = TFFunnelModelTester(self ) __UpperCAmelCase : Optional[Any] = ConfigTester(self , config_class=UpperCamelCase ) def lowerCamelCase__ ( self : Tuple ): '''simple docstring''' self.config_tester.run_common_tests() def lowerCamelCase__ ( self : int ): '''simple docstring''' __UpperCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCamelCase ) def lowerCamelCase__ ( self : int ): '''simple docstring''' __UpperCAmelCase : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*UpperCamelCase ) def lowerCamelCase__ ( self : Tuple ): '''simple docstring''' __UpperCAmelCase : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase ) def lowerCamelCase__ ( self : Union[str, Any] ): '''simple docstring''' __UpperCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*UpperCamelCase ) def lowerCamelCase__ ( self : str ): '''simple docstring''' __UpperCAmelCase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*UpperCamelCase ) @require_tf class lowerCamelCase__ ( A , unittest.TestCase ): """simple docstring""" __a = ( (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else () ) __a = False __a = False def lowerCamelCase__ ( self : str ): '''simple docstring''' __UpperCAmelCase : List[str] = TFFunnelModelTester(self , base=UpperCamelCase ) __UpperCAmelCase : List[Any] = ConfigTester(self , config_class=UpperCamelCase ) def lowerCamelCase__ ( self : List[Any] ): '''simple docstring''' self.config_tester.run_common_tests() def lowerCamelCase__ ( self : Union[str, Any] ): '''simple docstring''' __UpperCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_base_model(*UpperCamelCase ) def lowerCamelCase__ ( self : str ): '''simple docstring''' __UpperCAmelCase : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase ) def lowerCamelCase__ ( self : Optional[Any] ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase )
320
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging _A : int = logging.get_logger(__name__) _A : Any = {'''ctrl''': '''https://huggingface.co/ctrl/resolve/main/config.json'''} class _lowercase ( UpperCAmelCase__ ): '''simple docstring''' _SCREAMING_SNAKE_CASE : Any = """ctrl""" _SCREAMING_SNAKE_CASE : Optional[Any] = ["""past_key_values"""] _SCREAMING_SNAKE_CASE : Union[str, Any] = { """max_position_embeddings""": """n_positions""", """hidden_size""": """n_embd""", """num_attention_heads""": """n_head""", """num_hidden_layers""": """n_layer""", } def __init__( self : Any , SCREAMING_SNAKE_CASE__ : Optional[Any]=24_65_34 , SCREAMING_SNAKE_CASE__ : Optional[int]=2_56 , SCREAMING_SNAKE_CASE__ : Optional[Any]=12_80 , SCREAMING_SNAKE_CASE__ : str=81_92 , SCREAMING_SNAKE_CASE__ : List[str]=48 , SCREAMING_SNAKE_CASE__ : Tuple=16 , SCREAMING_SNAKE_CASE__ : str=0.1 , SCREAMING_SNAKE_CASE__ : Dict=0.1 , SCREAMING_SNAKE_CASE__ : Dict=1e-6 , SCREAMING_SNAKE_CASE__ : Any=0.0_2 , SCREAMING_SNAKE_CASE__ : Tuple=True , **SCREAMING_SNAKE_CASE__ : Optional[int] , ) -> Tuple: __lowerCAmelCase = vocab_size __lowerCAmelCase = n_positions __lowerCAmelCase = n_embd __lowerCAmelCase = n_layer __lowerCAmelCase = n_head __lowerCAmelCase = dff __lowerCAmelCase = resid_pdrop __lowerCAmelCase = embd_pdrop __lowerCAmelCase = layer_norm_epsilon __lowerCAmelCase = initializer_range __lowerCAmelCase = use_cache super().__init__(**SCREAMING_SNAKE_CASE__ )
229
'''simple docstring''' import importlib import inspect import json import os import re import shutil import sys from pathlib import Path from typing import Dict, Optional, Union from urllib import request from huggingface_hub import HfFolder, cached_download, hf_hub_download, model_info from packaging import version from .. import __version__ from . import DIFFUSERS_DYNAMIC_MODULE_NAME, HF_MODULES_CACHE, logging _A : List[Any] = ( '''https://raw.githubusercontent.com/huggingface/diffusers/{revision}/examples/community/{pipeline}.py''' ) _A : List[Any] = logging.get_logger(__name__) # pylint: disable=invalid-name def UpperCamelCase_ ( ) -> Dict: '''simple docstring''' __lowerCAmelCase = """https://pypi.org/pypi/diffusers/json""" __lowerCAmelCase = json.loads(request.urlopen(snake_case_ ).read() )["""releases"""].keys() return sorted(snake_case_ , key=lambda snake_case_ : version.Version(snake_case_ ) ) def UpperCamelCase_ ( ) -> int: '''simple docstring''' if HF_MODULES_CACHE in sys.path: return sys.path.append(snake_case_ ) os.makedirs(snake_case_ , exist_ok=snake_case_ ) __lowerCAmelCase = Path(snake_case_ ) / """__init__.py""" if not init_path.exists(): init_path.touch() def UpperCamelCase_ ( snake_case_ : Union[str, os.PathLike] ) -> Dict: '''simple docstring''' init_hf_modules() __lowerCAmelCase = Path(snake_case_ ) / name # If the parent module does not exist yet, recursively create it. if not dynamic_module_path.parent.exists(): create_dynamic_module(dynamic_module_path.parent ) os.makedirs(snake_case_ , exist_ok=snake_case_ ) __lowerCAmelCase = dynamic_module_path / """__init__.py""" if not init_path.exists(): init_path.touch() def UpperCamelCase_ ( snake_case_ : int ) -> str: '''simple docstring''' with open(snake_case_ , """r""" , encoding="""utf-8""" ) as f: __lowerCAmelCase = f.read() # Imports of the form `import .xxx` __lowerCAmelCase = re.findall("""^\s*import\s+\.(\S+)\s*$""" , snake_case_ , flags=re.MULTILINE ) # Imports of the form `from .xxx import yyy` relative_imports += re.findall("""^\s*from\s+\.(\S+)\s+import""" , snake_case_ , flags=re.MULTILINE ) # Unique-ify return list(set(snake_case_ ) ) def UpperCamelCase_ ( snake_case_ : int ) -> Dict: '''simple docstring''' __lowerCAmelCase = False __lowerCAmelCase = [module_file] __lowerCAmelCase = [] # Let's recurse through all relative imports while not no_change: __lowerCAmelCase = [] for f in files_to_check: new_imports.extend(get_relative_imports(snake_case_ ) ) __lowerCAmelCase = Path(snake_case_ ).parent __lowerCAmelCase = [str(module_path / m ) for m in new_imports] __lowerCAmelCase = [f for f in new_import_files if f not in all_relative_imports] __lowerCAmelCase = [f"""{f}.py""" for f in new_import_files] __lowerCAmelCase = len(snake_case_ ) == 0 all_relative_imports.extend(snake_case_ ) return all_relative_imports def UpperCamelCase_ ( snake_case_ : List[Any] ) -> Optional[Any]: '''simple docstring''' with open(snake_case_ , """r""" , encoding="""utf-8""" ) as f: __lowerCAmelCase = f.read() # Imports of the form `import xxx` __lowerCAmelCase = re.findall("""^\s*import\s+(\S+)\s*$""" , snake_case_ , flags=re.MULTILINE ) # Imports of the form `from xxx import yyy` imports += re.findall("""^\s*from\s+(\S+)\s+import""" , snake_case_ , flags=re.MULTILINE ) # Only keep the top-level module __lowerCAmelCase = [imp.split(""".""" )[0] for imp in imports if not imp.startswith(""".""" )] # Unique-ify and test we got them all __lowerCAmelCase = list(set(snake_case_ ) ) __lowerCAmelCase = [] for imp in imports: try: importlib.import_module(snake_case_ ) except ImportError: missing_packages.append(snake_case_ ) if len(snake_case_ ) > 0: raise ImportError( """This modeling file requires the following packages that were not found in your environment: """ f"""{", ".join(snake_case_ )}. Run `pip install {" ".join(snake_case_ )}`""" ) return get_relative_imports(snake_case_ ) def UpperCamelCase_ ( snake_case_ : Dict , snake_case_ : Optional[int] ) -> List[str]: '''simple docstring''' __lowerCAmelCase = module_path.replace(os.path.sep , """.""" ) __lowerCAmelCase = importlib.import_module(snake_case_ ) if class_name is None: return find_pipeline_class(snake_case_ ) return getattr(snake_case_ , snake_case_ ) def UpperCamelCase_ ( snake_case_ : str ) -> Optional[Any]: '''simple docstring''' from ..pipelines import DiffusionPipeline __lowerCAmelCase = dict(inspect.getmembers(snake_case_ , inspect.isclass ) ) __lowerCAmelCase = None for cls_name, cls in cls_members.items(): if ( cls_name != DiffusionPipeline.__name__ and issubclass(cls , snake_case_ ) and cls.__module__.split(""".""" )[0] != "diffusers" ): if pipeline_class is not None: raise ValueError( f"""Multiple classes that inherit from {DiffusionPipeline.__name__} have been found:""" f""" {pipeline_class.__name__}, and {cls_name}. Please make sure to define only one in""" f""" {loaded_module}.""" ) __lowerCAmelCase = cls return pipeline_class def UpperCamelCase_ ( snake_case_ : Union[str, os.PathLike] , snake_case_ : str , snake_case_ : Optional[Union[str, os.PathLike]] = None , snake_case_ : bool = False , snake_case_ : bool = False , snake_case_ : Optional[Dict[str, str]] = None , snake_case_ : Optional[Union[bool, str]] = None , snake_case_ : Optional[str] = None , snake_case_ : bool = False , ) -> Any: '''simple docstring''' __lowerCAmelCase = str(snake_case_ ) __lowerCAmelCase = os.path.join(snake_case_ , snake_case_ ) if os.path.isfile(snake_case_ ): __lowerCAmelCase = module_file_or_url __lowerCAmelCase = """local""" elif pretrained_model_name_or_path.count("""/""" ) == 0: __lowerCAmelCase = get_diffusers_versions() # cut ".dev0" __lowerCAmelCase = """v""" + """.""".join(__version__.split(""".""" )[:3] ) # retrieve github version that matches if revision is None: __lowerCAmelCase = latest_version if latest_version[1:] in available_versions else """main""" logger.info(f"""Defaulting to latest_version: {revision}.""" ) elif revision in available_versions: __lowerCAmelCase = f"""v{revision}""" elif revision == "main": __lowerCAmelCase = revision else: raise ValueError( f"""`custom_revision`: {revision} does not exist. Please make sure to choose one of""" f""" {", ".join(available_versions + ["main"] )}.""" ) # community pipeline on GitHub __lowerCAmelCase = COMMUNITY_PIPELINES_URL.format(revision=snake_case_ , pipeline=snake_case_ ) try: __lowerCAmelCase = cached_download( snake_case_ , cache_dir=snake_case_ , force_download=snake_case_ , proxies=snake_case_ , resume_download=snake_case_ , local_files_only=snake_case_ , use_auth_token=snake_case_ , ) __lowerCAmelCase = """git""" __lowerCAmelCase = pretrained_model_name_or_path + """.py""" except EnvironmentError: logger.error(f"""Could not locate the {module_file} inside {pretrained_model_name_or_path}.""" ) raise else: try: # Load from URL or cache if already cached __lowerCAmelCase = hf_hub_download( snake_case_ , snake_case_ , cache_dir=snake_case_ , force_download=snake_case_ , proxies=snake_case_ , resume_download=snake_case_ , local_files_only=snake_case_ , use_auth_token=snake_case_ , ) __lowerCAmelCase = os.path.join("""local""" , """--""".join(pretrained_model_name_or_path.split("""/""" ) ) ) except EnvironmentError: logger.error(f"""Could not locate the {module_file} inside {pretrained_model_name_or_path}.""" ) raise # Check we have all the requirements in our environment __lowerCAmelCase = check_imports(snake_case_ ) # Now we move the module inside our cached dynamic modules. __lowerCAmelCase = DIFFUSERS_DYNAMIC_MODULE_NAME + os.path.sep + submodule create_dynamic_module(snake_case_ ) __lowerCAmelCase = Path(snake_case_ ) / full_submodule if submodule == "local" or submodule == "git": # We always copy local files (we could hash the file to see if there was a change, and give them the name of # that hash, to only copy when there is a modification but it seems overkill for now). # The only reason we do the copy is to avoid putting too many folders in sys.path. shutil.copy(snake_case_ , submodule_path / module_file ) for module_needed in modules_needed: __lowerCAmelCase = f"""{module_needed}.py""" shutil.copy(os.path.join(snake_case_ , snake_case_ ) , submodule_path / module_needed ) else: # Get the commit hash # TODO: we will get this info in the etag soon, so retrieve it from there and not here. if isinstance(snake_case_ , snake_case_ ): __lowerCAmelCase = use_auth_token elif use_auth_token is True: __lowerCAmelCase = HfFolder.get_token() else: __lowerCAmelCase = None __lowerCAmelCase = model_info(snake_case_ , revision=snake_case_ , token=snake_case_ ).sha # The module file will end up being placed in a subfolder with the git hash of the repo. This way we get the # benefit of versioning. __lowerCAmelCase = submodule_path / commit_hash __lowerCAmelCase = full_submodule + os.path.sep + commit_hash create_dynamic_module(snake_case_ ) if not (submodule_path / module_file).exists(): shutil.copy(snake_case_ , submodule_path / module_file ) # Make sure we also have every file with relative for module_needed in modules_needed: if not (submodule_path / module_needed).exists(): get_cached_module_file( snake_case_ , f"""{module_needed}.py""" , cache_dir=snake_case_ , force_download=snake_case_ , resume_download=snake_case_ , proxies=snake_case_ , use_auth_token=snake_case_ , revision=snake_case_ , local_files_only=snake_case_ , ) return os.path.join(snake_case_ , snake_case_ ) def UpperCamelCase_ ( snake_case_ : Union[str, os.PathLike] , snake_case_ : str , snake_case_ : Optional[str] = None , snake_case_ : Optional[Union[str, os.PathLike]] = None , snake_case_ : bool = False , snake_case_ : bool = False , snake_case_ : Optional[Dict[str, str]] = None , snake_case_ : Optional[Union[bool, str]] = None , snake_case_ : Optional[str] = None , snake_case_ : bool = False , **snake_case_ : Dict , ) -> Tuple: '''simple docstring''' __lowerCAmelCase = get_cached_module_file( snake_case_ , snake_case_ , cache_dir=snake_case_ , force_download=snake_case_ , resume_download=snake_case_ , proxies=snake_case_ , use_auth_token=snake_case_ , revision=snake_case_ , local_files_only=snake_case_ , ) return get_class_in_module(snake_case_ , final_module.replace(""".py""" , """""" ) )
229
1
"""simple docstring""" import json import os import unittest from typing import Tuple from transformers import WavaVecaPhonemeCTCTokenizer from transformers.models.wavaveca.tokenization_wavaveca import VOCAB_FILES_NAMES from transformers.models.wavaveca_phoneme.tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizerOutput from transformers.testing_utils import require_phonemizer from ...test_tokenization_common import TokenizerTesterMixin @require_phonemizer class A_ ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = WavaVecaPhonemeCTCTokenizer __UpperCamelCase = False def UpperCAmelCase__ ( self :Optional[int] ) -> int: super().setUp() UpperCAmelCase = ( '<s> <pad> </s> <unk> n s t ə l a i k d m ɛ ɾ e ɪ p o ɐ z ð f j v b ɹ ʁ ʊ iː r w ʌ u ɡ æ aɪ ʃ h ɔ ɑː ' 'ŋ ɚ eɪ β uː y ɑ̃ oʊ ᵻ eː θ aʊ ts oː ɔ̃ ɣ ɜ ɑ dʒ əl x ɜː ç ʒ tʃ ɔː ɑːɹ ɛ̃ ʎ ɔːɹ ʋ aː ɕ œ ø oːɹ ɲ yː ' 'ʔ iə i5 s. tɕ ?? nʲ ɛː œ̃ ɭ ɔø ʑ tʲ ɨ ɛɹ ts. rʲ ɪɹ ɭʲ i.5 ɔɪ q sʲ u5 ʊɹ iɜ a5 iɛ5 øː ʕ ja əɜ th ɑ5 ' 'oɪ dʲ ə5 tɕh ts.h mʲ ɯ dʑ vʲ e̞ tʃʲ ei5 o5 onɡ5 ɑu5 iɑ5 ai5 aɪɚ kh ə1 ʐ i2 ʉ ħ t[ aɪə ʲ ju ə2 u2 oɜ ' 'pː iɛɜ ou5 y5 uɜ tː uo5 d[ uoɜ tsh ɑɜ ɵ i̪5 uei5 ɟ aɜ ɑɨ i.ɜ eʊ o2 ɐ̃ ä pʲ kʲ n̩ ɒ ph ɑu2 uɨ əɪ ɫ ɬ ' 'yɜ bʲ ɑ2 s̪ aiɜ χ ɐ̃ʊ̃ 1 ə4 yæɜ a2 ɨː t̪ iouɜ ũ onɡɜ aɨ iɛ2 ɔɨ ɑuɜ o̞ ei2 iou2 c kː y2 ɖ oe dˤ yɛɜ ' 'əʊ S ɡʲ onɡ2 u" eiɜ ʈ ɯᵝ iou5 dZ r̝̊ i.2 tS s^ ʝ yə5 iɑɜ uə5 pf ɨu iɑ2 ou2 ər2 fʲ ai2 r̝ uəɜ ɳ əɨ ' 'ua5 uɪ ɽ bː yu5 uo2 yɛ5 l̩ ɻ ərɜ ʂ i̪2 ouɜ uaɜ a. a.ː yæ5 dː r̩ ee ɪu ər5 i̪ ɜ æi u: i.ː t^ o1 ɪ^ ' 'ai ueiɜ æː ɛɪ eə i. ɴ ie ua2 ɑ1 o4 tʃː o: ɑ: u1 N i̪1 au yæ2 u. qː yəɜ y: kʰ tʃʰ iʊ sx õ uo tʰ ' 'uai5 bʰ u.ː uə2 ʊə d^ s̪ː yiɜ dʰ r. oe: i1 ɟː yu2 nʲʲ i̪4 uei2 tsʲ ɸ ĩ ɑ4 t̪ː eɑ u4 e: tsː ʈʰ ɡʰ ' 'ɯɯ dʒʲ ʂʲ X ɵː uaiɜ tɕʲ ã t^ː ẽː yɛ2 cː i.1 ɛʊ dˤdˤ dʒː i4 ɡː yi ɕʲ ɟʰ pʰ dʑʲ yuɜ ua1 ua4 æiː ɐɐ ' 'ui iou1 ʊː a1 iou4 cʰ iɛ1 yə2 ɖʰ ẽ ʒʲ ää ər4 iːː ɪː iɑ1 ər1 œː øi ɪuː cʰcʰ əː1 iː1 ũ kʰː o̞o̞ xʲ ' 'ou1 iɛ4 e̞e̞ y1 dzː dʲʲ dʰː ɯᵝɯᵝ lː uo1 i.4 i: yɛ5ʲ a4' ).split(' ' ) UpperCAmelCase = dict(zip(lowercase_ , range(len(lowercase_ ) ) ) ) UpperCAmelCase = {'pad_token': '<pad>', 'unk_token': '<unk>', 'bos_token': '<s>', 'eos_token': '</s>'} UpperCAmelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(lowercase_ ) + '\n' ) def UpperCAmelCase__ ( self :Dict , lowercase_ :Any , lowercase_ :Union[str, Any]=False , lowercase_ :int=20 , lowercase_ :Dict=5 ) -> Tuple[str, list]: UpperCAmelCase = [(i, tokenizer.decode([i] , clean_up_tokenization_spaces=lowercase_ )) for i in range(len(lowercase_ ) )] UpperCAmelCase = list(filter(lambda lowercase_ : [t[0]] == tokenizer.encode(t[1] , do_phonemize=lowercase_ ) , lowercase_ ) ) if max_length is not None and len(lowercase_ ) > max_length: UpperCAmelCase = toks[:max_length] if min_length is not None and len(lowercase_ ) < min_length and len(lowercase_ ) > 0: while len(lowercase_ ) < min_length: UpperCAmelCase = toks + toks # toks_str = [t[1] for t in toks] UpperCAmelCase = [t[0] for t in toks] # Ensure consistency UpperCAmelCase = tokenizer.decode(lowercase_ , clean_up_tokenization_spaces=lowercase_ ) if " " not in output_txt and len(lowercase_ ) > 1: UpperCAmelCase = ( tokenizer.decode([toks_ids[0]] , clean_up_tokenization_spaces=lowercase_ ) + ' ' + tokenizer.decode(toks_ids[1:] , clean_up_tokenization_spaces=lowercase_ ) ) if with_prefix_space: UpperCAmelCase = ' ' + output_txt UpperCAmelCase = tokenizer.encode(lowercase_ , add_special_tokens=lowercase_ ) return output_txt, output_ids def UpperCAmelCase__ ( self :Union[str, Any] , **lowercase_ :Union[str, Any] ) -> Optional[Any]: kwargs.update(self.special_tokens_map ) return WavaVecaPhonemeCTCTokenizer.from_pretrained(self.tmpdirname , **lowercase_ ) def UpperCAmelCase__ ( self :int ) -> str: UpperCAmelCase = self.tokenizer_class.from_pretrained('facebook/wav2vec2-lv-60-espeak-cv-ft' ) # check adding a single token tokenizer.add_tokens('xxx' ) UpperCAmelCase = tokenizer('m xxx ɪ' , do_phonemize=lowercase_ ).input_ids self.assertEqual(lowercase_ , [13, 3_92, 17] ) # xxx should be last token tokenizer.add_tokens(['aaa', 'bbb', 'ccc'] ) UpperCAmelCase = tokenizer('m aaa ɪ ccc' , do_phonemize=lowercase_ ).input_ids self.assertEqual(lowercase_ , [13, 3_93, 17, 3_95] ) # aaa and ccc should be after xxx and 2 after aaa UpperCAmelCase = tokenizer('maɪ c' , do_phonemize=lowercase_ ).input_ids self.assertEqual(lowercase_ , [3, 2_00] ) # mai should be <unk> (=3) def UpperCAmelCase__ ( self :Tuple ) -> Union[str, Any]: UpperCAmelCase = self.tokenizer_class.from_pretrained('facebook/wav2vec2-lv-60-espeak-cv-ft' ) UpperCAmelCase = 'Hello how are you' UpperCAmelCase = tokenizer.phonemize(lowercase_ , phonemizer_lang='en-us' ) self.assertEqual(lowercase_ , 'h ə l oʊ h aʊ ɑːɹ j uː' ) def UpperCAmelCase__ ( self :Dict ) -> int: UpperCAmelCase = self.tokenizer_class.from_pretrained('facebook/wav2vec2-lv-60-espeak-cv-ft' ) UpperCAmelCase = 'Hello how are you' UpperCAmelCase = tokenizer.phonemize(lowercase_ , phonemizer_lang='en-us' ) self.assertEqual(tokenizer(lowercase_ ).input_ids , tokenizer(lowercase_ , do_phonemize=lowercase_ ).input_ids ) def UpperCAmelCase__ ( self :Optional[Any] ) -> Dict: UpperCAmelCase = self.tokenizer_class.from_pretrained('facebook/wav2vec2-lv-60-espeak-cv-ft' ) UpperCAmelCase = 'Hello how are you' UpperCAmelCase = tokenizer.phonemize(lowercase_ , phonemizer_lang='en-us' ) UpperCAmelCase = tokenizer.decode(tokenizer(lowercase_ ).input_ids ) self.assertEqual(lowercase_ , lowercase_ ) def UpperCAmelCase__ ( self :Optional[int] ) -> str: UpperCAmelCase = self.tokenizer_class.from_pretrained('facebook/wav2vec2-lv-60-espeak-cv-ft' ) UpperCAmelCase = [ [11, 5, 15, tokenizer.pad_token_id, 15, 8, 98], [24, 22, 5, 24, 22, 5, 77], ] UpperCAmelCase = tokenizer.decode(sample_ids[0] ) UpperCAmelCase = tokenizer.batch_decode(lowercase_ ) self.assertEqual(lowercase_ , batch_tokens[0] ) self.assertEqual(lowercase_ , ['k s ɾ ɾ l ɭʲ', 'j ð s j ð s oːɹ'] ) def UpperCAmelCase__ ( self :Any ) -> str: UpperCAmelCase = self.tokenizer_class.from_pretrained( 'facebook/wav2vec2-lv-60-espeak-cv-ft' , word_delimiter_token='|' ) tokenizer.add_tokens('|' ) UpperCAmelCase = 'Hello how are you' UpperCAmelCase = tokenizer.phonemize(lowercase_ , phonemizer_lang='en-us' ) self.assertEqual(lowercase_ , 'h ə l oʊ | h aʊ | ɑːɹ | j uː |' ) def UpperCAmelCase__ ( self :Any ) -> Any: UpperCAmelCase = self.tokenizer_class.from_pretrained( 'facebook/wav2vec2-lv-60-espeak-cv-ft' , word_delimiter_token='|' ) tokenizer.add_tokens('|' ) UpperCAmelCase = 'Hello how are you' UpperCAmelCase = tokenizer.phonemize(lowercase_ , phonemizer_lang='en-us' ) self.assertEqual(tokenizer(lowercase_ ).input_ids , tokenizer(lowercase_ , do_phonemize=lowercase_ ).input_ids ) def UpperCAmelCase__ ( self :Dict ) -> Union[str, Any]: UpperCAmelCase = self.tokenizer_class.from_pretrained( 'facebook/wav2vec2-lv-60-espeak-cv-ft' , word_delimiter_token='|' ) tokenizer.add_tokens('|' ) # fmt: off UpperCAmelCase = [ [11, 5, 15, tokenizer.pad_token_id, tokenizer.word_delimiter_token_id, 15, 8, tokenizer.word_delimiter_token_id, 98], [tokenizer.word_delimiter_token_id, 24, 22, tokenizer.word_delimiter_token_id, 5, 24, 22, 5, 77], ] # fmt: on # decode with word_del_token filter UpperCAmelCase = tokenizer.decode(sample_ids[0] ) UpperCAmelCase = tokenizer.batch_decode(lowercase_ ) self.assertEqual(lowercase_ , batch_tokens[0] ) self.assertEqual(lowercase_ , ['k s ɾ ɾ l ɭʲ', 'j ð s j ð s oːɹ'] ) # decode with no word_del_token filter UpperCAmelCase = tokenizer.decode(sample_ids[0] , filter_word_delimiter_token=lowercase_ ) UpperCAmelCase = tokenizer.batch_decode(lowercase_ , filter_word_delimiter_token=lowercase_ ) self.assertEqual(lowercase_ , batch_tokens[0] ) self.assertEqual(lowercase_ , ['k s ɾ | ɾ l | ɭʲ', '| j ð | s j ð s oːɹ'] ) def UpperCAmelCase__ ( self :int ) -> int: UpperCAmelCase = self.tokenizer_class.from_pretrained( 'facebook/wav2vec2-lv-60-espeak-cv-ft' , word_delimiter_token='|' ) tokenizer.add_tokens('|' ) UpperCAmelCase = 'Hello how are you' UpperCAmelCase = tokenizer.phonemize(lowercase_ , phonemizer_lang='en-us' ) UpperCAmelCase = tokenizer.decode(tokenizer(lowercase_ ).input_ids , filter_word_delimiter_token=lowercase_ ) self.assertEqual(lowercase_ , lowercase_ ) def UpperCAmelCase__ ( self :Union[str, Any] ) -> Optional[Any]: UpperCAmelCase = self.tokenizer_class.from_pretrained( 'facebook/wav2vec2-lv-60-espeak-cv-ft' , word_delimiter_token='|' ) tokenizer.add_tokens('|' ) UpperCAmelCase = 'Hello how are you' UpperCAmelCase = tokenizer.phonemize(lowercase_ , phonemizer_lang='en-us' ) UpperCAmelCase = tokenizer.decode(tokenizer(lowercase_ ).input_ids , filter_word_delimiter_token=lowercase_ ) self.assertEqual(' '.join([p.strip() for p in phonemes.split(' |' )] ).strip() , lowercase_ ) def UpperCAmelCase__ ( self :int ) -> Optional[Any]: UpperCAmelCase = self.tokenizer_class.from_pretrained( 'facebook/wav2vec2-lv-60-espeak-cv-ft' , word_delimiter_token=lowercase_ ) UpperCAmelCase = 'Hello how are you' UpperCAmelCase = tokenizer(lowercase_ , phonemizer_lang='en-us' ).input_ids UpperCAmelCase = tokenizer(lowercase_ , phonemizer_lang='fr-fr' ).input_ids self.assertNotEqual(lowercase_ , lowercase_ ) UpperCAmelCase = tokenizer.decode(lowercase_ ) UpperCAmelCase = tokenizer.decode(lowercase_ ) self.assertEqual(lowercase_ , 'h ə l oʊ h aʊ ɑːɹ j uː' ) self.assertEqual(lowercase_ , 'ɛ l o h aʊ a ʁ j u' ) def UpperCAmelCase__ ( self :int ) -> List[Any]: UpperCAmelCase = self.tokenizer_class.from_pretrained('facebook/wav2vec2-lv-60-espeak-cv-ft' ) UpperCAmelCase = 'Hello how Are you' UpperCAmelCase = 'hello how are you' UpperCAmelCase = tokenizer(lowercase_ ).input_ids UpperCAmelCase = tokenizer(lowercase_ ).input_ids self.assertEqual(lowercase_ , lowercase_ ) def UpperCAmelCase__ ( self :Optional[Any] ) -> int: UpperCAmelCase = self.tokenizer_class.from_pretrained('facebook/wav2vec2-lv-60-espeak-cv-ft' ) tokenizer.add_tokens(['!', '?'] ) tokenizer.add_special_tokens({'cls_token': '$$$'} ) # fmt: off UpperCAmelCase = [ [11, 5, 15, tokenizer.pad_token_id, 15, 8, 98, 3_92, 3_92, 3_93, 3_92, 3_92, 3_93, 3_94, 3_94], [24, 22, 5, 24, 22, 5, 77, tokenizer.pad_token_id, 3_94, 3_94], ] # fmt: on UpperCAmelCase = tokenizer.batch_decode(lowercase_ ) self.assertEqual(lowercase_ , ['k s ɾ ɾ l ɭʲ!?!? $$$', 'j ð s j ð s oːɹ $$$'] ) @staticmethod def UpperCAmelCase__ ( lowercase_ :List[str] , lowercase_ :List[str] ) -> List[str]: UpperCAmelCase = [d[key] for d in offsets] return retrieved_list def UpperCAmelCase__ ( self :str ) -> Optional[int]: UpperCAmelCase = self.get_tokenizer(word_delimiter_token='|' ) tokenizer.add_tokens('|' ) # fmt: off # ksssɾɾ|ɾɾ<pad>ɾɾ|<pad>ɾlll|ɭʲ -> k s ɾ ɾ | ɾ l | ɭʲ" UpperCAmelCase = [11, 5, 5, 5, 15, 15, tokenizer.pad_token_id, 15, 15, tokenizer.word_delimiter_token_id, tokenizer.pad_token_id, 15, 8, 8, 8, tokenizer.word_delimiter_token_id, 98] # fmt: on UpperCAmelCase = tokenizer.decode(lowercase_ , output_char_offsets=lowercase_ , filter_word_delimiter_token=lowercase_ ) # check Wav2Vec2CTCTokenizerOutput keys for char self.assertEqual(len(outputs.keys() ) , 2 ) self.assertTrue('text' in outputs ) self.assertTrue('char_offsets' in outputs ) self.assertTrue(isinstance(lowercase_ , lowercase_ ) ) # check that order of chars is correct and identical for both outputs self.assertEqual(' '.join(self.get_from_offsets(outputs['char_offsets'] , 'char' ) ) , outputs.text ) self.assertListEqual( self.get_from_offsets(outputs['char_offsets'] , 'char' ) , ['k', 's', 'ɾ', 'ɾ', '|', 'ɾ', 'l', '|', 'ɭʲ'] ) # check that offsets are actually correct for char # 0-1 is 11, 1-4 is 5, 4-6 is first 15, 6-7 is <pad> (thus not shown), 7-9 is second 15, 9-10 is word_delimiter_token, # 10-11 is <pad> (thus not shown), 11-12 is third 15, 12-15 is 8, 15-16 is word_delimiter_token, 16-17 is 98 self.assertListEqual( self.get_from_offsets(outputs['char_offsets'] , 'start_offset' ) , [0, 1, 4, 7, 9, 11, 12, 15, 16] ) self.assertListEqual( self.get_from_offsets(outputs['char_offsets'] , 'end_offset' ) , [1, 4, 6, 9, 10, 12, 15, 16, 17] ) def UpperCAmelCase__ ( self :Optional[int] ) -> Optional[int]: UpperCAmelCase = self.get_tokenizer(word_delimiter_token='|' ) def check_list_tuples_equal(lowercase_ :List[Any] , lowercase_ :str ): self.assertTrue(isinstance(lowercase_ , lowercase_ ) ) self.assertTrue(isinstance(outputs_list[0] , lowercase_ ) ) # transform list to ModelOutput UpperCAmelCase = WavaVecaPhonemeCTCTokenizerOutput( {k: [d[k] for d in outputs_list] for k in outputs_list[0]} ) self.assertListEqual(outputs_batch['text'] , outputs_batch_a['text'] ) def recursive_check(lowercase_ :Any , lowercase_ :str ): if isinstance(lowercase_ , lowercase_ ): [recursive_check(lowercase_ , lowercase_ ) for la, la in zip(lowercase_ , lowercase_ )] self.assertEqual(lowercase_ , lowercase_ ) if "char_offsets" in outputs_batch: recursive_check(outputs_batch['char_offsets'] , outputs_batch_a['char_offsets'] ) # fmt: off UpperCAmelCase = [ [11, 5, 15, tokenizer.pad_token_id, 15, 4, 8, 98, 32, 32, 32, 32, 4, 33, tokenizer.word_delimiter_token_id, 32, 32, 33, 34, 34], [24, 22, 5, tokenizer.word_delimiter_token_id, tokenizer.word_delimiter_token_id, 24, 22, 22, 22, 4, 5, 77, tokenizer.pad_token_id, 22, 22, 4, 34, 34, 34, 34], ] # fmt: on # We assume that `decode` works as expected. All we will check now is # the output type is correct and the output is identical to `decode` # char UpperCAmelCase = tokenizer.batch_decode(lowercase_ , output_char_offsets=lowercase_ ) UpperCAmelCase = [tokenizer.decode(lowercase_ , output_char_offsets=lowercase_ ) for ids in sample_ids] check_list_tuples_equal(lowercase_ , lowercase_ ) @unittest.skip('Wav2Vec2PhonemeTokenizer always lower cases letters to correctly map to phonemes' ) def UpperCAmelCase__ ( self :Any ) -> str: pass @unittest.skip('Wav2Vec2PhonemeTokenizer always puts spaces between phonemes' ) def UpperCAmelCase__ ( self :str ) -> List[str]: pass @unittest.skip('encodes to text to ids, but decodes ids to phonemes -> not possible to have internal consistency' ) def UpperCAmelCase__ ( self :List[str] ) -> int: pass @unittest.skip('Wav2Vec2PhonemeModel has no max model length => no testing' ) def UpperCAmelCase__ ( self :List[Any] ) -> Optional[int]: pass def UpperCAmelCase__ ( self :int ) -> Optional[Any]: UpperCAmelCase = self.get_tokenizers(do_lower_case=lowercase_ ) for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}""" ): UpperCAmelCase = tokenizer.vocab_size UpperCAmelCase = len(lowercase_ ) self.assertNotEqual(lowercase_ , 0 ) # We usually have added tokens from the start in tests because our vocab fixtures are # smaller than the original vocabs - let's not assert this # self.assertEqual(vocab_size, all_size) UpperCAmelCase = ['aaaaa bbbbbb', 'cccccccccdddddddd'] UpperCAmelCase = tokenizer.add_tokens(lowercase_ ) UpperCAmelCase = tokenizer.vocab_size UpperCAmelCase = len(lowercase_ ) self.assertNotEqual(lowercase_ , 0 ) self.assertEqual(lowercase_ , lowercase_ ) self.assertEqual(lowercase_ , len(lowercase_ ) ) self.assertEqual(lowercase_ , all_size + len(lowercase_ ) ) UpperCAmelCase = tokenizer.encode('aaaaa bbbbbb low cccccccccdddddddd l' , add_special_tokens=lowercase_ ) self.assertGreaterEqual(len(lowercase_ ) , 4 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) UpperCAmelCase = {'eos_token': '>>>>|||<||<<|<<', 'pad_token': '<<<<<|||>|>>>>|>'} UpperCAmelCase = tokenizer.add_special_tokens(lowercase_ ) UpperCAmelCase = tokenizer.vocab_size UpperCAmelCase = len(lowercase_ ) self.assertNotEqual(lowercase_ , 0 ) self.assertEqual(lowercase_ , lowercase_ ) self.assertEqual(lowercase_ , len(lowercase_ ) ) self.assertEqual(lowercase_ , all_size_a + len(lowercase_ ) ) UpperCAmelCase = tokenizer.encode( '>>>>|||<||<<|<< aaaaabbbbbb low cccccccccdddddddd <<<<<|||>|>>>>|> l' , add_special_tokens=lowercase_ ) self.assertGreaterEqual(len(lowercase_ ) , 6 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[0] , tokens[1] ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokens[-4] ) self.assertEqual(tokens[0] , tokenizer.eos_token_id ) self.assertEqual(tokens[-3] , tokenizer.pad_token_id ) @unittest.skip('The tokenizer shouldn\'t be used to encode input IDs (except for labels), only to decode.' ) def UpperCAmelCase__ ( self :Tuple ) -> Optional[Any]: pass @unittest.skip('The tokenizer shouldn\'t be used to encode input IDs (except for labels), only to decode.' ) def UpperCAmelCase__ ( self :int ) -> Any: pass def UpperCAmelCase__ ( self :Tuple ) -> Dict: # The default common tokenizer tests assumes that the output of `convert_tokens_to_string` is a string which # is not the case for Wav2Vec2PhonemeCTCTokenizer. UpperCAmelCase = self.get_tokenizers(fast=lowercase_ , do_lower_case=lowercase_ ) for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}""" ): UpperCAmelCase = ['ð', 'ɪ', 's', 'ɪ', 'z', 'ɐ', 't', 'ɛ', 'k', 's', 't'] UpperCAmelCase = tokenizer.convert_tokens_to_string(lowercase_ ) self.assertIsInstance(output['text'] , lowercase_ )
181
"""simple docstring""" import tempfile import torch from diffusers import ( DEISMultistepScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) from .test_schedulers import SchedulerCommonTest class A_ ( SCREAMING_SNAKE_CASE_ ): """simple docstring""" __UpperCamelCase = (UniPCMultistepScheduler,) __UpperCamelCase = (("""num_inference_steps""", 25),) def UpperCAmelCase__ ( self :Any , **lowercase_ :Optional[Any] ) -> Dict: UpperCAmelCase = { 'num_train_timesteps': 10_00, 'beta_start': 0.0001, 'beta_end': 0.02, 'beta_schedule': 'linear', 'solver_order': 2, 'solver_type': 'bh2', } config.update(**lowercase_ ) return config def UpperCAmelCase__ ( self :Optional[Any] , lowercase_ :Optional[int]=0 , **lowercase_ :Optional[Any] ) -> List[str]: 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 UpperCAmelCase__ ( self :List[Any] , lowercase_ :Dict=0 , **lowercase_ :Optional[int] ) -> List[str]: 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 UpperCAmelCase__ ( self :List[Any] , lowercase_ :Optional[int]=None , **lowercase_ :str ) -> Dict: 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 UpperCAmelCase__ ( self :Union[str, Any] ) -> Tuple: UpperCAmelCase = dict(self.forward_default_kwargs ) UpperCAmelCase = kwargs.pop('num_inference_steps' , lowercase_ ) for scheduler_class in self.scheduler_classes: UpperCAmelCase = self.get_scheduler_config() UpperCAmelCase = scheduler_class(**lowercase_ ) UpperCAmelCase = self.dummy_sample UpperCAmelCase = 0.1 * sample if num_inference_steps is not None and hasattr(lowercase_ , 'set_timesteps' ): scheduler.set_timesteps(lowercase_ ) elif num_inference_steps is not None and not hasattr(lowercase_ , 'set_timesteps' ): UpperCAmelCase = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) UpperCAmelCase = [residual + 0.2, residual + 0.15, residual + 0.10] UpperCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] UpperCAmelCase = scheduler.timesteps[5] UpperCAmelCase = scheduler.timesteps[6] UpperCAmelCase = scheduler.step(lowercase_ , lowercase_ , lowercase_ , **lowercase_ ).prev_sample UpperCAmelCase = scheduler.step(lowercase_ , lowercase_ , lowercase_ , **lowercase_ ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def UpperCAmelCase__ ( self :Optional[int] ) -> Optional[Any]: # make sure that iterating over schedulers with same config names gives same results # for defaults UpperCAmelCase = UniPCMultistepScheduler(**self.get_scheduler_config() ) UpperCAmelCase = self.full_loop(scheduler=lowercase_ ) UpperCAmelCase = torch.mean(torch.abs(lowercase_ ) ) assert abs(result_mean.item() - 0.2464 ) < 1E-3 UpperCAmelCase = DPMSolverSinglestepScheduler.from_config(scheduler.config ) UpperCAmelCase = DEISMultistepScheduler.from_config(scheduler.config ) UpperCAmelCase = DPMSolverMultistepScheduler.from_config(scheduler.config ) UpperCAmelCase = UniPCMultistepScheduler.from_config(scheduler.config ) UpperCAmelCase = self.full_loop(scheduler=lowercase_ ) UpperCAmelCase = torch.mean(torch.abs(lowercase_ ) ) assert abs(result_mean.item() - 0.2464 ) < 1E-3 def UpperCAmelCase__ ( self :Optional[int] ) -> List[str]: for timesteps in [25, 50, 1_00, 9_99, 10_00]: self.check_over_configs(num_train_timesteps=lowercase_ ) def UpperCAmelCase__ ( self :int ) -> str: self.check_over_configs(thresholding=lowercase_ ) for order in [1, 2, 3]: for solver_type in ["bh1", "bh2"]: 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_ , solver_order=lowercase_ , solver_type=lowercase_ , ) def UpperCAmelCase__ ( self :Any ) -> str: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=lowercase_ ) def UpperCAmelCase__ ( self :str ) -> Optional[Any]: for solver_type in ["bh1", "bh2"]: 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_ , ) UpperCAmelCase = self.full_loop( solver_order=lowercase_ , solver_type=lowercase_ , prediction_type=lowercase_ , ) assert not torch.isnan(lowercase_ ).any(), "Samples have nan numbers" def UpperCAmelCase__ ( self :Dict ) -> List[Any]: self.check_over_configs(lower_order_final=lowercase_ ) self.check_over_configs(lower_order_final=lowercase_ ) def UpperCAmelCase__ ( self :Any ) -> List[Any]: for num_inference_steps in [1, 2, 3, 5, 10, 50, 1_00, 9_99, 10_00]: self.check_over_forward(num_inference_steps=lowercase_ , time_step=0 ) def UpperCAmelCase__ ( self :int ) -> Union[str, Any]: UpperCAmelCase = self.full_loop() UpperCAmelCase = torch.mean(torch.abs(lowercase_ ) ) assert abs(result_mean.item() - 0.2464 ) < 1E-3 def UpperCAmelCase__ ( self :Optional[int] ) -> str: UpperCAmelCase = self.full_loop(prediction_type='v_prediction' ) UpperCAmelCase = torch.mean(torch.abs(lowercase_ ) ) assert abs(result_mean.item() - 0.1014 ) < 1E-3 def UpperCAmelCase__ ( self :Tuple ) -> Tuple: 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 def UpperCAmelCase__ ( self :List[Any] , **lowercase_ :Optional[Any] ) -> Dict: for scheduler_class in self.scheduler_classes: UpperCAmelCase = self.get_scheduler_config(**lowercase_ ) UpperCAmelCase = scheduler_class(**lowercase_ ) scheduler.set_timesteps(scheduler.config.num_train_timesteps ) assert len(scheduler.timesteps.unique() ) == scheduler.num_inference_steps
181
1
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import BertTokenizer, BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import FEATURE_EXTRACTOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import ChineseCLIPImageProcessor, ChineseCLIPProcessor @require_vision class a (unittest.TestCase ): """simple docstring""" def __snake_case ( self : Union[str, Any] ) -> Dict: __snake_case : List[Any] = tempfile.mkdtemp() __snake_case : Dict = [ """[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """的""", """价""", """格""", """是""", """15""", """便""", """alex""", """##andra""", """,""", """。""", """-""", """t""", """shirt""", ] __snake_case : 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] ) ) __snake_case : Any = { """do_resize""": True, """size""": {"""height""": 224, """width""": 224}, """do_center_crop""": True, """crop_size""": {"""height""": 18, """width""": 18}, """do_normalize""": True, """image_mean""": [0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73], """image_std""": [0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11], """do_convert_rgb""": True, } __snake_case : Tuple = os.path.join(self.tmpdirname , lowerCamelCase ) with open(self.image_processor_file , "w" , encoding="utf-8" ) as fp: json.dump(lowerCamelCase , lowerCamelCase ) def __snake_case ( self : Optional[int] , **lowerCamelCase : Dict ) -> str: return BertTokenizer.from_pretrained(self.tmpdirname , **lowerCamelCase ) def __snake_case ( self : List[str] , **lowerCamelCase : str ) -> Union[str, Any]: return BertTokenizerFast.from_pretrained(self.tmpdirname , **lowerCamelCase ) def __snake_case ( self : str , **lowerCamelCase : Any ) -> Optional[int]: return ChineseCLIPImageProcessor.from_pretrained(self.tmpdirname , **lowerCamelCase ) def __snake_case ( self : List[Any] ) -> Dict: shutil.rmtree(self.tmpdirname ) def __snake_case ( self : Any ) -> Optional[Any]: __snake_case : int = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] __snake_case : List[str] = [Image.fromarray(np.moveaxis(lowerCamelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def __snake_case ( self : Union[str, Any] ) -> int: __snake_case : List[str] = self.get_tokenizer() __snake_case : Union[str, Any] = self.get_rust_tokenizer() __snake_case : int = self.get_image_processor() __snake_case : Tuple = ChineseCLIPProcessor(tokenizer=lowerCamelCase , image_processor=lowerCamelCase ) processor_slow.save_pretrained(self.tmpdirname ) __snake_case : Optional[int] = ChineseCLIPProcessor.from_pretrained(self.tmpdirname , use_fast=lowerCamelCase ) __snake_case : List[str] = ChineseCLIPProcessor(tokenizer=lowerCamelCase , image_processor=lowerCamelCase ) processor_fast.save_pretrained(self.tmpdirname ) __snake_case : List[Any] = ChineseCLIPProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , lowerCamelCase ) self.assertIsInstance(processor_fast.tokenizer , lowerCamelCase ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , lowerCamelCase ) self.assertIsInstance(processor_fast.image_processor , lowerCamelCase ) def __snake_case ( self : List[str] ) -> List[str]: __snake_case : str = ChineseCLIPProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) __snake_case : str = self.get_tokenizer(cls_token="(CLS)" , sep_token="(SEP)" ) __snake_case : Tuple = self.get_image_processor(do_normalize=lowerCamelCase ) __snake_case : Union[str, Any] = ChineseCLIPProcessor.from_pretrained( self.tmpdirname , cls_token="(CLS)" , sep_token="(SEP)" , do_normalize=lowerCamelCase ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , lowerCamelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , lowerCamelCase ) def __snake_case ( self : Any ) -> Dict: __snake_case : Dict = self.get_image_processor() __snake_case : Tuple = self.get_tokenizer() __snake_case : Optional[int] = ChineseCLIPProcessor(tokenizer=lowerCamelCase , image_processor=lowerCamelCase ) __snake_case : Dict = self.prepare_image_inputs() __snake_case : Tuple = image_processor(lowerCamelCase , return_tensors="np" ) __snake_case : int = processor(images=lowerCamelCase , 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 __snake_case ( self : Optional[int] ) -> Dict: __snake_case : Dict = self.get_image_processor() __snake_case : Union[str, Any] = self.get_tokenizer() __snake_case : Dict = ChineseCLIPProcessor(tokenizer=lowerCamelCase , image_processor=lowerCamelCase ) __snake_case : Union[str, Any] = """Alexandra,T-shirt的价格是15便士。""" __snake_case : Dict = processor(text=lowerCamelCase ) __snake_case : Dict = tokenizer(lowerCamelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def __snake_case ( self : int ) -> List[Any]: __snake_case : str = self.get_image_processor() __snake_case : Tuple = self.get_tokenizer() __snake_case : List[Any] = ChineseCLIPProcessor(tokenizer=lowerCamelCase , image_processor=lowerCamelCase ) __snake_case : Optional[int] = """Alexandra,T-shirt的价格是15便士。""" __snake_case : List[Any] = self.prepare_image_inputs() __snake_case : Optional[int] = processor(text=lowerCamelCase , images=lowerCamelCase ) self.assertListEqual(list(inputs.keys() ) , ["input_ids", "token_type_ids", "attention_mask", "pixel_values"] ) # test if it raises when no input is passed with pytest.raises(lowerCamelCase ): processor() def __snake_case ( self : str ) -> Any: __snake_case : Any = self.get_image_processor() __snake_case : int = self.get_tokenizer() __snake_case : Optional[Any] = ChineseCLIPProcessor(tokenizer=lowerCamelCase , image_processor=lowerCamelCase ) __snake_case : List[Any] = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] __snake_case : Union[str, Any] = processor.batch_decode(lowerCamelCase ) __snake_case : List[Any] = tokenizer.batch_decode(lowerCamelCase ) self.assertListEqual(lowerCamelCase , lowerCamelCase ) def __snake_case ( self : Tuple ) -> Union[str, Any]: __snake_case : Optional[int] = self.get_image_processor() __snake_case : int = self.get_tokenizer() __snake_case : Tuple = ChineseCLIPProcessor(tokenizer=lowerCamelCase , image_processor=lowerCamelCase ) __snake_case : List[Any] = """Alexandra,T-shirt的价格是15便士。""" __snake_case : Any = self.prepare_image_inputs() __snake_case : str = processor(text=lowerCamelCase , images=lowerCamelCase ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
123
'''simple docstring''' from string import ascii_lowercase, ascii_uppercase def a ( lowerCamelCase__ ): '''simple docstring''' if not sentence: return "" A_ : Optional[int] = dict(zip(lowerCamelCase__ , lowerCamelCase__ ) ) return lower_to_upper.get(sentence[0] , sentence[0] ) + sentence[1:] if __name__ == "__main__": from doctest import testmod testmod()
206
0
"""simple docstring""" import argparse import torch from datasets import load_dataset from donut import DonutModel from transformers import ( DonutImageProcessor, DonutProcessor, DonutSwinConfig, DonutSwinModel, MBartConfig, MBartForCausalLM, VisionEncoderDecoderModel, XLMRobertaTokenizerFast, ) def __UpperCAmelCase ( snake_case_ : str ) -> List[str]: """simple docstring""" _lowerCAmelCase = model.config _lowerCAmelCase = DonutSwinConfig( image_size=original_config.input_size , patch_size=4 , depths=original_config.encoder_layer , num_heads=[4, 8, 16, 32] , window_size=original_config.window_size , embed_dim=128 , ) _lowerCAmelCase = MBartConfig( is_decoder=__lowerCamelCase , is_encoder_decoder=__lowerCamelCase , add_cross_attention=__lowerCamelCase , decoder_layers=original_config.decoder_layer , max_position_embeddings=original_config.max_position_embeddings , vocab_size=len( model.decoder.tokenizer ) , scale_embedding=__lowerCamelCase , add_final_layer_norm=__lowerCamelCase , ) return encoder_config, decoder_config def __UpperCAmelCase ( snake_case_ : Optional[Any] ) -> Tuple: """simple docstring""" if "encoder.model" in name: _lowerCAmelCase = name.replace("""encoder.model""" , """encoder""" ) if "decoder.model" in name: _lowerCAmelCase = name.replace("""decoder.model""" , """decoder""" ) if "patch_embed.proj" in name: _lowerCAmelCase = name.replace("""patch_embed.proj""" , """embeddings.patch_embeddings.projection""" ) if "patch_embed.norm" in name: _lowerCAmelCase = name.replace("""patch_embed.norm""" , """embeddings.norm""" ) if name.startswith("""encoder""" ): if "layers" in name: _lowerCAmelCase = """encoder.""" + name if "attn.proj" in name: _lowerCAmelCase = name.replace("""attn.proj""" , """attention.output.dense""" ) if "attn" in name and "mask" not in name: _lowerCAmelCase = name.replace("""attn""" , """attention.self""" ) if "norm1" in name: _lowerCAmelCase = name.replace("""norm1""" , """layernorm_before""" ) if "norm2" in name: _lowerCAmelCase = name.replace("""norm2""" , """layernorm_after""" ) if "mlp.fc1" in name: _lowerCAmelCase = name.replace("""mlp.fc1""" , """intermediate.dense""" ) if "mlp.fc2" in name: _lowerCAmelCase = name.replace("""mlp.fc2""" , """output.dense""" ) if name == "encoder.norm.weight": _lowerCAmelCase = """encoder.layernorm.weight""" if name == "encoder.norm.bias": _lowerCAmelCase = """encoder.layernorm.bias""" return name def __UpperCAmelCase ( snake_case_ : int , snake_case_ : Dict ) -> Dict: """simple docstring""" for key in orig_state_dict.copy().keys(): _lowerCAmelCase = orig_state_dict.pop(__lowerCamelCase ) if "qkv" in key: _lowerCAmelCase = key.split(""".""" ) _lowerCAmelCase = int(key_split[3] ) _lowerCAmelCase = int(key_split[5] ) _lowerCAmelCase = model.encoder.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: _lowerCAmelCase = val[:dim, :] _lowerCAmelCase = val[dim : dim * 2, :] _lowerCAmelCase = val[-dim:, :] else: _lowerCAmelCase = val[:dim] _lowerCAmelCase = val[dim : dim * 2] _lowerCAmelCase = val[-dim:] elif "attn_mask" in key or key in ["encoder.model.norm.weight", "encoder.model.norm.bias"]: # HuggingFace implementation doesn't use attn_mask buffer # and model doesn't use final LayerNorms for the encoder pass else: _lowerCAmelCase = val return orig_state_dict def __UpperCAmelCase ( snake_case_ : int , snake_case_ : str=None , snake_case_ : Tuple=False ) -> Any: """simple docstring""" _lowerCAmelCase = DonutModel.from_pretrained(__lowerCamelCase ).eval() # load HuggingFace model _lowerCAmelCase , _lowerCAmelCase = get_configs(__lowerCamelCase ) _lowerCAmelCase = DonutSwinModel(__lowerCamelCase ) _lowerCAmelCase = MBartForCausalLM(__lowerCamelCase ) _lowerCAmelCase = VisionEncoderDecoderModel(encoder=__lowerCamelCase , decoder=__lowerCamelCase ) model.eval() _lowerCAmelCase = original_model.state_dict() _lowerCAmelCase = convert_state_dict(__lowerCamelCase , __lowerCamelCase ) model.load_state_dict(__lowerCamelCase ) # verify results on scanned document _lowerCAmelCase = load_dataset("""hf-internal-testing/example-documents""" ) _lowerCAmelCase = dataset["""test"""][0]["""image"""].convert("""RGB""" ) _lowerCAmelCase = XLMRobertaTokenizerFast.from_pretrained(__lowerCamelCase , from_slow=__lowerCamelCase ) _lowerCAmelCase = DonutImageProcessor( do_align_long_axis=original_model.config.align_long_axis , size=original_model.config.input_size[::-1] ) _lowerCAmelCase = DonutProcessor(__lowerCamelCase , __lowerCamelCase ) _lowerCAmelCase = processor(__lowerCamelCase , return_tensors="""pt""" ).pixel_values if model_name == "naver-clova-ix/donut-base-finetuned-docvqa": _lowerCAmelCase = """<s_docvqa><s_question>{user_input}</s_question><s_answer>""" _lowerCAmelCase = """When is the coffee break?""" _lowerCAmelCase = task_prompt.replace("""{user_input}""" , __lowerCamelCase ) elif model_name == "naver-clova-ix/donut-base-finetuned-rvlcdip": _lowerCAmelCase = """<s_rvlcdip>""" elif model_name in [ "naver-clova-ix/donut-base-finetuned-cord-v1", "naver-clova-ix/donut-base-finetuned-cord-v1-2560", ]: _lowerCAmelCase = """<s_cord>""" elif model_name == "naver-clova-ix/donut-base-finetuned-cord-v2": _lowerCAmelCase = """s_cord-v2>""" elif model_name == "naver-clova-ix/donut-base-finetuned-zhtrainticket": _lowerCAmelCase = """<s_zhtrainticket>""" elif model_name in ["naver-clova-ix/donut-proto", "naver-clova-ix/donut-base"]: # use a random prompt _lowerCAmelCase = """hello world""" else: raise ValueError("""Model name not supported""" ) _lowerCAmelCase = original_model.decoder.tokenizer(__lowerCamelCase , add_special_tokens=__lowerCamelCase , return_tensors="""pt""" )[ """input_ids""" ] _lowerCAmelCase = original_model.encoder.model.patch_embed(__lowerCamelCase ) _lowerCAmelCase , _lowerCAmelCase = model.encoder.embeddings(__lowerCamelCase ) assert torch.allclose(__lowerCamelCase , __lowerCamelCase , atol=1e-3 ) # verify encoder hidden states _lowerCAmelCase = original_model.encoder(__lowerCamelCase ) _lowerCAmelCase = model.encoder(__lowerCamelCase ).last_hidden_state assert torch.allclose(__lowerCamelCase , __lowerCamelCase , atol=1e-2 ) # verify decoder hidden states _lowerCAmelCase = original_model(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ).logits _lowerCAmelCase = model(__lowerCamelCase , decoder_input_ids=__lowerCamelCase ).logits assert torch.allclose(__lowerCamelCase , __lowerCamelCase , atol=1e-3 ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: print(F"""Saving model and processor to {pytorch_dump_folder_path}""" ) model.save_pretrained(__lowerCamelCase ) processor.save_pretrained(__lowerCamelCase ) if push_to_hub: model.push_to_hub("""nielsr/""" + model_name.split("""/""" )[-1] , commit_message="""Update model""" ) processor.push_to_hub("""nielsr/""" + model_name.split("""/""" )[-1] , commit_message="""Update model""" ) if __name__ == "__main__": SCREAMING_SNAKE_CASE : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''naver-clova-ix/donut-base-finetuned-docvqa''', required=False, type=str, help='''Name of the original model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, required=False, 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 and processor to the 🤗 hub.''', ) SCREAMING_SNAKE_CASE : List[str] = parser.parse_args() convert_donut_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
368
"""simple docstring""" from __future__ import annotations class __lowerCamelCase : def __init__(self , lowerCamelCase , lowerCamelCase ): '''simple docstring''' _lowerCAmelCase , _lowerCAmelCase = text, pattern _lowerCAmelCase , _lowerCAmelCase = len(lowerCamelCase ), len(lowerCamelCase ) def A__ (self , lowerCamelCase ): '''simple docstring''' for i in range(self.patLen - 1 , -1 , -1 ): if char == self.pattern[i]: return i return -1 def A__ (self , lowerCamelCase ): '''simple docstring''' for i in range(self.patLen - 1 , -1 , -1 ): if self.pattern[i] != self.text[current_pos + i]: return current_pos + i return -1 def A__ (self ): '''simple docstring''' _lowerCAmelCase = [] for i in range(self.textLen - self.patLen + 1 ): _lowerCAmelCase = self.mismatch_in_text(lowerCamelCase ) if mismatch_index == -1: positions.append(lowerCamelCase ) else: _lowerCAmelCase = self.match_in_pattern(self.text[mismatch_index] ) _lowerCAmelCase = ( mismatch_index - match_index ) # shifting index lgtm [py/multiple-definition] return positions SCREAMING_SNAKE_CASE : Any = '''ABAABA''' SCREAMING_SNAKE_CASE : Optional[int] = '''AB''' SCREAMING_SNAKE_CASE : str = BoyerMooreSearch(text, pattern) SCREAMING_SNAKE_CASE : Tuple = bms.bad_character_heuristic() if len(positions) == 0: print('''No match found''') else: print('''Pattern found in following positions: ''') print(positions)
317
0
'''simple docstring''' def snake_case_ ( lowerCAmelCase_ = 100 )-> int: '''simple docstring''' _UpperCAmelCase : Dict = set() _UpperCAmelCase : int = 0 _UpperCAmelCase : List[str] = n + 1 # maximum limit for a in range(2 , lowerCAmelCase_ ): for b in range(2 , lowerCAmelCase_ ): _UpperCAmelCase : List[Any] = a**b # calculates the current power collect_powers.add(lowerCAmelCase_ ) # adds the result to the set return len(lowerCAmelCase_ ) if __name__ == "__main__": print("""Number of terms """, solution(int(str(input()).strip())))
215
'''simple docstring''' from torch import nn class lowercase ( nn.Module ): """simple docstring""" def __init__( self ,a_ ,a_ ) -> List[Any]: super().__init__() _UpperCAmelCase : Dict = class_size _UpperCAmelCase : Union[str, Any] = embed_size # self.mlp1 = nn.Linear(embed_size, embed_size) # self.mlp2 = (nn.Linear(embed_size, class_size)) _UpperCAmelCase : List[Any] = nn.Linear(a_ ,a_ ) def _snake_case ( self ,a_ ) -> Tuple: # hidden_state = nn.functional.relu(self.mlp1(hidden_state)) # hidden_state = self.mlp2(hidden_state) _UpperCAmelCase : Optional[int] = self.mlp(a_ ) return logits
215
1
"""simple docstring""" import os import sys import unittest SCREAMING_SNAKE_CASE_ = 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 SCREAMING_SNAKE_CASE_ = os.path.join(git_repo_path, """src""", """diffusers""") class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def SCREAMING_SNAKE_CASE__ ( self : List[str] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = find_backend(""" if not is_torch_available():""" ) self.assertEqual(UpperCamelCase__ ,"""torch""" ) # backend_with_underscore = find_backend(" if not is_tensorflow_text_available():") # self.assertEqual(backend_with_underscore, "tensorflow_text") SCREAMING_SNAKE_CASE = find_backend(""" if not (is_torch_available() and is_transformers_available()):""" ) self.assertEqual(UpperCamelCase__ ,"""torch_and_transformers""" ) # double_backend_with_underscore = find_backend( # " if not (is_sentencepiece_available() and is_tensorflow_text_available()):" # ) # self.assertEqual(double_backend_with_underscore, "sentencepiece_and_tensorflow_text") SCREAMING_SNAKE_CASE = find_backend( """ if not (is_torch_available() and is_transformers_available() and is_onnx_available()):""" ) self.assertEqual(UpperCamelCase__ ,"""torch_and_transformers_and_onnx""" ) def SCREAMING_SNAKE_CASE__ ( self : 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""" ,UpperCamelCase__ ) self.assertIn("""torch_and_transformers""" ,UpperCamelCase__ ) self.assertIn("""flax_and_transformers""" ,UpperCamelCase__ ) self.assertIn("""torch_and_transformers_and_onnx""" ,UpperCamelCase__ ) # Likewise, we can't assert on the exact content of a key self.assertIn("""UNet2DModel""" ,objects["""torch"""] ) self.assertIn("""FlaxUNet2DConditionModel""" ,objects["""flax"""] ) self.assertIn("""StableDiffusionPipeline""" ,objects["""torch_and_transformers"""] ) self.assertIn("""FlaxStableDiffusionPipeline""" ,objects["""flax_and_transformers"""] ) self.assertIn("""LMSDiscreteScheduler""" ,objects["""torch_and_scipy"""] ) self.assertIn("""OnnxStableDiffusionPipeline""" ,objects["""torch_and_transformers_and_onnx"""] ) def SCREAMING_SNAKE_CASE__ ( self : Any ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = create_dummy_object("""CONSTANT""" ,"""\'torch\'""" ) self.assertEqual(UpperCamelCase__ ,"""\nCONSTANT = None\n""" ) SCREAMING_SNAKE_CASE = create_dummy_object("""function""" ,"""\'torch\'""" ) self.assertEqual( UpperCamelCase__ ,"""\ndef function(*args, **kwargs):\n requires_backends(function, \'torch\')\n""" ) SCREAMING_SNAKE_CASE = ''' class FakeClass(metaclass=DummyObject): _backends = \'torch\' def __init__(self, *args, **kwargs): requires_backends(self, \'torch\') @classmethod def from_config(cls, *args, **kwargs): requires_backends(cls, \'torch\') @classmethod def from_pretrained(cls, *args, **kwargs): requires_backends(cls, \'torch\') ''' SCREAMING_SNAKE_CASE = create_dummy_object("""FakeClass""" ,"""\'torch\'""" ) self.assertEqual(UpperCamelCase__ ,UpperCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( self : Tuple ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = '''# This file is autogenerated by the command `make fix-copies`, do not edit. from ..utils import DummyObject, requires_backends CONSTANT = None def function(*args, **kwargs): requires_backends(function, ["torch"]) class FakeClass(metaclass=DummyObject): _backends = ["torch"] def __init__(self, *args, **kwargs): requires_backends(self, ["torch"]) @classmethod def from_config(cls, *args, **kwargs): requires_backends(cls, ["torch"]) @classmethod def from_pretrained(cls, *args, **kwargs): requires_backends(cls, ["torch"]) ''' SCREAMING_SNAKE_CASE = create_dummy_files({"""torch""": ["""CONSTANT""", """function""", """FakeClass"""]} ) self.assertEqual(dummy_files["""torch"""] ,UpperCamelCase__ )
367
def __lowercase ( _SCREAMING_SNAKE_CASE = 50 ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = [1] * (length + 1) for row_length in range(3 , length + 1 ): for block_length in range(3 , row_length + 1 ): for block_start in range(row_length - block_length ): ways_number[row_length] += ways_number[ row_length - block_start - block_length - 1 ] ways_number[row_length] += 1 return ways_number[length] if __name__ == "__main__": print(F'''{solution() = }''')
193
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available _lowercase = { '''configuration_biogpt''': ['''BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BioGptConfig'''], '''tokenization_biogpt''': ['''BioGptTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowercase = [ '''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 _lowercase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
74
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import re from ..utils import cached_file # docstyle-ignore _lowerCAmelCase : Optional[int] = ''' Human: <<task>> Assistant: ''' _lowerCAmelCase : int = '''huggingface-tools/default-prompts''' _lowerCAmelCase : Any = {'''chat''': '''chat_prompt_template.txt''', '''run''': '''run_prompt_template.txt'''} def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Dict="run" ) -> List[Any]: if prompt_or_repo_id is None: A_ : Optional[int] = DEFAULT_PROMPTS_REPO # prompt is considered a repo ID when it does not contain any kind of space if re.search("\\s" , _lowerCAmelCase ) is not None: return prompt_or_repo_id A_ : Optional[Any] = cached_file( _lowerCAmelCase , PROMPT_FILES[mode] , repo_type="dataset" , user_agent={"agent": agent_name} ) with open(_lowerCAmelCase , "r" , encoding="utf-8" ) as f: return f.read()
300
0
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 __lowerCAmelCase : def __init__(self , __magic_name__ , __magic_name__=13 , __magic_name__=7 , __magic_name__=True , __magic_name__=True , __magic_name__=True , __magic_name__=True , __magic_name__=99 , __magic_name__=64 , __magic_name__=5 , __magic_name__=4 , __magic_name__=37 , __magic_name__="gelu" , __magic_name__=0.1 , __magic_name__=0.1 , __magic_name__=512 , __magic_name__=16 , __magic_name__=2 , __magic_name__=0.02 , __magic_name__=3 , __magic_name__=4 , __magic_name__=None , ) -> str: '''simple docstring''' snake_case_ : List[str] = parent snake_case_ : str = batch_size snake_case_ : Dict = seq_length snake_case_ : List[Any] = is_training snake_case_ : Union[str, Any] = use_input_mask snake_case_ : List[Any] = use_token_type_ids snake_case_ : Dict = use_labels snake_case_ : Dict = vocab_size snake_case_ : Optional[int] = hidden_size snake_case_ : int = num_hidden_layers snake_case_ : Union[str, Any] = num_attention_heads snake_case_ : Any = intermediate_size snake_case_ : str = hidden_act snake_case_ : Union[str, Any] = hidden_dropout_prob snake_case_ : Dict = attention_probs_dropout_prob snake_case_ : Optional[int] = max_position_embeddings snake_case_ : List[Any] = type_vocab_size snake_case_ : Any = type_sequence_label_size snake_case_ : Dict = initializer_range snake_case_ : Dict = num_labels snake_case_ : Union[str, Any] = num_choices snake_case_ : Union[str, Any] = scope snake_case_ : str = vocab_size - 1 def lowerCamelCase (self ) -> Optional[int]: '''simple docstring''' snake_case_ : Dict = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) snake_case_ : Tuple = None if self.use_input_mask: snake_case_ : List[Any] = random_attention_mask([self.batch_size, self.seq_length] ) snake_case_ : Optional[int] = None if self.use_labels: snake_case_ : Dict = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) snake_case_ : Union[str, Any] = self.get_config() return config, input_ids, input_mask, token_labels def lowerCamelCase (self ) -> Any: '''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=__magic_name__ , initializer_range=self.initializer_range , pad_token_id=self.pad_token_id , ) def lowerCamelCase (self ) -> Optional[int]: '''simple docstring''' snake_case_ : List[Any] = self.prepare_config_and_inputs() snake_case_ : Dict = True return config, input_ids, input_mask, token_labels def lowerCamelCase (self , __magic_name__ , __magic_name__ , __magic_name__ ) -> str: '''simple docstring''' snake_case_ : List[str] = GPTNeoXModel(config=__magic_name__ ) model.to(__magic_name__ ) model.eval() snake_case_ : List[Any] = model(__magic_name__ , attention_mask=__magic_name__ ) snake_case_ : Optional[int] = model(__magic_name__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowerCamelCase (self , __magic_name__ , __magic_name__ , __magic_name__ ) -> Any: '''simple docstring''' snake_case_ : Union[str, Any] = True snake_case_ : Optional[Any] = GPTNeoXModel(__magic_name__ ) model.to(__magic_name__ ) model.eval() snake_case_ : str = model(__magic_name__ , attention_mask=__magic_name__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowerCamelCase (self , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) -> Tuple: '''simple docstring''' snake_case_ : List[str] = GPTNeoXForCausalLM(config=__magic_name__ ) model.to(__magic_name__ ) model.eval() snake_case_ : Optional[Any] = 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 lowerCamelCase (self , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) -> Optional[int]: '''simple docstring''' snake_case_ : Any = self.num_labels snake_case_ : Any = GPTNeoXForQuestionAnswering(__magic_name__ ) model.to(__magic_name__ ) model.eval() snake_case_ : Any = model(__magic_name__ , attention_mask=__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 lowerCamelCase (self , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) -> List[Any]: '''simple docstring''' snake_case_ : str = self.num_labels snake_case_ : Optional[int] = GPTNeoXForSequenceClassification(__magic_name__ ) model.to(__magic_name__ ) model.eval() snake_case_ : List[str] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) snake_case_ : Tuple = model(__magic_name__ , attention_mask=__magic_name__ , labels=__magic_name__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowerCamelCase (self , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) -> Tuple: '''simple docstring''' snake_case_ : Tuple = self.num_labels snake_case_ : Optional[int] = GPTNeoXForTokenClassification(__magic_name__ ) model.to(__magic_name__ ) model.eval() snake_case_ : Union[str, Any] = 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 lowerCamelCase (self , __magic_name__ , __magic_name__ , __magic_name__ ) -> Union[str, Any]: '''simple docstring''' snake_case_ : Any = True snake_case_ : Optional[int] = GPTNeoXForCausalLM(config=__magic_name__ ) model.to(__magic_name__ ) model.eval() # first forward pass snake_case_ : Optional[Any] = model(__magic_name__ , attention_mask=__magic_name__ , use_cache=__magic_name__ ) snake_case_ : Union[str, Any] = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids snake_case_ : Dict = ids_tensor((self.batch_size, 3) , config.vocab_size ) snake_case_ : Dict = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and snake_case_ : str = torch.cat([input_ids, next_tokens] , dim=-1 ) snake_case_ : Any = torch.cat([input_mask, next_mask] , dim=-1 ) snake_case_ : Optional[Any] = model(__magic_name__ , attention_mask=__magic_name__ , output_hidden_states=__magic_name__ ) snake_case_ : str = output_from_no_past['''hidden_states'''][0] snake_case_ : Any = model( __magic_name__ , attention_mask=__magic_name__ , past_key_values=__magic_name__ , output_hidden_states=__magic_name__ , )['''hidden_states'''][0] # select random slice snake_case_ : Optional[int] = ids_tensor((1,) , output_from_past.shape[-1] ).item() snake_case_ : Optional[Any] = output_from_no_past[:, -3:, random_slice_idx].detach() snake_case_ : 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(__magic_name__ , __magic_name__ , atol=1e-3 ) ) def lowerCamelCase (self ) -> Optional[Any]: '''simple docstring''' snake_case_ : str = self.prepare_config_and_inputs() snake_case_ : Optional[int] = config_and_inputs snake_case_ : List[Any] = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class __lowerCAmelCase ( _a, _a, _a, unittest.TestCase ): lowerCamelCase_ : Optional[Any] = ( ( GPTNeoXModel, GPTNeoXForCausalLM, GPTNeoXForQuestionAnswering, GPTNeoXForSequenceClassification, GPTNeoXForTokenClassification, ) if is_torch_available() else () ) lowerCamelCase_ : Any = (GPTNeoXForCausalLM,) if is_torch_available() else () lowerCamelCase_ : Union[str, Any] = ( { '''feature-extraction''': GPTNeoXModel, '''question-answering''': GPTNeoXForQuestionAnswering, '''text-classification''': GPTNeoXForSequenceClassification, '''text-generation''': GPTNeoXForCausalLM, '''token-classification''': GPTNeoXForTokenClassification, '''zero-shot''': GPTNeoXForSequenceClassification, } if is_torch_available() else {} ) lowerCamelCase_ : Tuple = False lowerCamelCase_ : List[Any] = False lowerCamelCase_ : int = False lowerCamelCase_ : Optional[int] = False def lowerCamelCase (self ) -> Union[str, Any]: '''simple docstring''' snake_case_ : List[Any] = GPTNeoXModelTester(self ) snake_case_ : str = ConfigTester(self , config_class=__magic_name__ , hidden_size=64 , num_attention_heads=8 ) def lowerCamelCase (self ) -> Dict: '''simple docstring''' self.config_tester.run_common_tests() def lowerCamelCase (self ) -> Any: '''simple docstring''' snake_case_ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(__magic_name__ , __magic_name__ , __magic_name__ ) def lowerCamelCase (self ) -> Optional[Any]: '''simple docstring''' snake_case_ : Any = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(__magic_name__ , __magic_name__ , __magic_name__ ) def lowerCamelCase (self ) -> Any: '''simple docstring''' snake_case_ : List[Any] = self.model_tester.prepare_config_and_inputs_for_decoder() snake_case_ : List[Any] = None self.model_tester.create_and_check_model_as_decoder(__magic_name__ , __magic_name__ , __magic_name__ ) def lowerCamelCase (self ) -> int: '''simple docstring''' snake_case_ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(__magic_name__ , __magic_name__ , __magic_name__ ) def lowerCamelCase (self ) -> Optional[int]: '''simple docstring''' snake_case_ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_causal_lm(*__magic_name__ ) def lowerCamelCase (self ) -> str: '''simple docstring''' snake_case_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*__magic_name__ ) def lowerCamelCase (self ) -> List[str]: '''simple docstring''' snake_case_ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*__magic_name__ ) def lowerCamelCase (self ) -> Optional[int]: '''simple docstring''' snake_case_ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*__magic_name__ ) @unittest.skip(reason='''Feed forward chunking is not implemented''' ) def lowerCamelCase (self ) -> str: '''simple docstring''' pass @parameterized.expand([('''linear''',), ('''dynamic''',)] ) def lowerCamelCase (self , __magic_name__ ) -> int: '''simple docstring''' snake_case_ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ : List[str] = ids_tensor([1, 10] , config.vocab_size ) snake_case_ : Optional[int] = 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 snake_case_ : List[str] = GPTNeoXModel(__magic_name__ ) original_model.to(__magic_name__ ) original_model.eval() snake_case_ : List[Any] = original_model(__magic_name__ ).last_hidden_state snake_case_ : Any = original_model(__magic_name__ ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights snake_case_ : Any = {'''type''': scaling_type, '''factor''': 10.0} snake_case_ : Optional[int] = GPTNeoXModel(__magic_name__ ) scaled_model.to(__magic_name__ ) scaled_model.eval() snake_case_ : Optional[Any] = scaled_model(__magic_name__ ).last_hidden_state snake_case_ : str = scaled_model(__magic_name__ ).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(__magic_name__ , __magic_name__ , atol=1e-5 ) ) else: self.assertFalse(torch.allclose(__magic_name__ , __magic_name__ , atol=1e-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(__magic_name__ , __magic_name__ , atol=1e-5 ) ) @require_torch class __lowerCAmelCase ( unittest.TestCase ): @slow def lowerCamelCase (self ) -> str: '''simple docstring''' snake_case_ : Tuple = AutoTokenizer.from_pretrained('''EleutherAI/pythia-410m-deduped''' ) for checkpointing in [True, False]: snake_case_ : Dict = GPTNeoXForCausalLM.from_pretrained('''EleutherAI/pythia-410m-deduped''' ) if checkpointing: model.gradient_checkpointing_enable() else: model.gradient_checkpointing_disable() model.to(__magic_name__ ) snake_case_ : Tuple = tokenizer('''My favorite food is''' , return_tensors='''pt''' ).to(__magic_name__ ) # The hub repo. is updated on 2023-04-04, resulting in poor outputs. # See: https://github.com/huggingface/transformers/pull/24193 snake_case_ : Optional[int] = '''My favorite food is a good old-fashioned, old-fashioned, old-fashioned.\n\nI\'m not sure''' snake_case_ : Optional[int] = model.generate(**__magic_name__ , do_sample=__magic_name__ , max_new_tokens=20 ) snake_case_ : Tuple = tokenizer.batch_decode(__magic_name__ )[0] self.assertEqual(__magic_name__ , __magic_name__ )
361
import unittest from transformers import load_tool from transformers.utils import is_torch_available if is_torch_available(): import torch from transformers.testing_utils import require_torch from .test_tools_common import ToolTesterMixin @require_torch class __lowerCAmelCase ( unittest.TestCase, _a ): def lowerCamelCase (self ) -> int: '''simple docstring''' snake_case_ : Dict = load_tool('''text-to-speech''' ) self.tool.setup() def lowerCamelCase (self ) -> List[str]: '''simple docstring''' torch.manual_seed(0 ) snake_case_ : Optional[int] = self.tool('''hey''' ) snake_case_ : Union[str, Any] = result.to_raw() self.assertTrue( torch.allclose( resulting_tensor[:3] , torch.tensor([-0.0_005_966_668_832_115_829, -0.0_003_657_640_190_795_064, -0.00_013_439_502_799_883_485] ) , ) ) def lowerCamelCase (self ) -> Optional[Any]: '''simple docstring''' torch.manual_seed(0 ) snake_case_ : Any = self.tool('''hey''' ) snake_case_ : str = result.to_raw() self.assertTrue( torch.allclose( resulting_tensor[:3] , torch.tensor([-0.0_005_966_668_832_115_829, -0.0_003_657_640_190_795_064, -0.00_013_439_502_799_883_485] ) , ) )
279
0
from dataclasses import dataclass from typing import Dict, Optional, Union import torch import torch.nn.functional as F from torch import nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput from .attention import BasicTransformerBlock from .attention_processor import AttentionProcessor, AttnProcessor from .embeddings import TimestepEmbedding, Timesteps from .modeling_utils import ModelMixin @dataclass class lowercase_ ( A__ ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = 42 class lowercase_ ( A__ , A__ ): """simple docstring""" @register_to_config def __init__( self , __SCREAMING_SNAKE_CASE = 32 , __SCREAMING_SNAKE_CASE = 64 , __SCREAMING_SNAKE_CASE = 20 , __SCREAMING_SNAKE_CASE = 768 , __SCREAMING_SNAKE_CASE=77 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE = 0.0 , __SCREAMING_SNAKE_CASE = "silu" , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = "linear" , __SCREAMING_SNAKE_CASE = "prd" , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , ) ->Optional[Any]: super().__init__() lowerCAmelCase = num_attention_heads lowerCAmelCase = attention_head_dim lowerCAmelCase = num_attention_heads * attention_head_dim lowerCAmelCase = additional_embeddings lowerCAmelCase = time_embed_dim or inner_dim lowerCAmelCase = embedding_proj_dim or embedding_dim lowerCAmelCase = clip_embed_dim or embedding_dim lowerCAmelCase = Timesteps(__lowerCamelCase , __lowerCamelCase , 0 ) lowerCAmelCase = TimestepEmbedding(__lowerCamelCase , __lowerCamelCase , out_dim=__lowerCamelCase , act_fn=__lowerCamelCase ) lowerCAmelCase = nn.Linear(__lowerCamelCase , __lowerCamelCase ) if embedding_proj_norm_type is None: lowerCAmelCase = None elif embedding_proj_norm_type == "layer": lowerCAmelCase = nn.LayerNorm(__lowerCamelCase ) else: raise ValueError(F"unsupported embedding_proj_norm_type: {embedding_proj_norm_type}" ) lowerCAmelCase = nn.Linear(__lowerCamelCase , __lowerCamelCase ) if encoder_hid_proj_type is None: lowerCAmelCase = None elif encoder_hid_proj_type == "linear": lowerCAmelCase = nn.Linear(__lowerCamelCase , __lowerCamelCase ) else: raise ValueError(F"unsupported encoder_hid_proj_type: {encoder_hid_proj_type}" ) lowerCAmelCase = nn.Parameter(torch.zeros(1 , num_embeddings + additional_embeddings , __lowerCamelCase ) ) if added_emb_type == "prd": lowerCAmelCase = nn.Parameter(torch.zeros(1 , 1 , __lowerCamelCase ) ) elif added_emb_type is None: lowerCAmelCase = None else: raise ValueError( F"`added_emb_type`: {added_emb_type} is not supported. Make sure to choose one of `\'prd\'` or `None`." ) lowerCAmelCase = nn.ModuleList( [ BasicTransformerBlock( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , dropout=__lowerCamelCase , activation_fn='''gelu''' , attention_bias=__lowerCamelCase , ) for d in range(__lowerCamelCase ) ] ) if norm_in_type == "layer": lowerCAmelCase = nn.LayerNorm(__lowerCamelCase ) elif norm_in_type is None: lowerCAmelCase = None else: raise ValueError(F"Unsupported norm_in_type: {norm_in_type}." ) lowerCAmelCase = nn.LayerNorm(__lowerCamelCase ) lowerCAmelCase = nn.Linear(__lowerCamelCase , __lowerCamelCase ) lowerCAmelCase = torch.full( [num_embeddings + additional_embeddings, num_embeddings + additional_embeddings] , -1_0_0_0_0.0 ) causal_attention_mask.triu_(1 ) lowerCAmelCase = causal_attention_mask[None, ...] self.register_buffer('''causal_attention_mask''' , __lowerCamelCase , persistent=__lowerCamelCase ) lowerCAmelCase = nn.Parameter(torch.zeros(1 , __lowerCamelCase ) ) lowerCAmelCase = nn.Parameter(torch.zeros(1 , __lowerCamelCase ) ) @property # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors def SCREAMING_SNAKE_CASE_ ( self ) ->Dict[str, AttentionProcessor]: lowerCAmelCase = {} def fn_recursive_add_processors(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): if hasattr(__lowerCamelCase , '''set_processor''' ): lowerCAmelCase = module.processor for sub_name, child in module.named_children(): fn_recursive_add_processors(F"{name}.{sub_name}" , __lowerCamelCase , __lowerCamelCase ) return processors for name, module in self.named_children(): fn_recursive_add_processors(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) return processors def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->List[Any]: lowerCAmelCase = len(self.attn_processors.keys() ) if isinstance(__lowerCamelCase , __lowerCamelCase ) and len(__lowerCamelCase ) != count: raise ValueError( F"A dict of processors was passed, but the number of processors {len(__lowerCamelCase )} does not match the" F" number of attention layers: {count}. Please make sure to pass {count} processor classes." ) def fn_recursive_attn_processor(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): if hasattr(__lowerCamelCase , '''set_processor''' ): if not isinstance(__lowerCamelCase , __lowerCamelCase ): module.set_processor(__lowerCamelCase ) else: module.set_processor(processor.pop(F"{name}.processor" ) ) for sub_name, child in module.named_children(): fn_recursive_attn_processor(F"{name}.{sub_name}" , __lowerCamelCase , __lowerCamelCase ) for name, module in self.named_children(): fn_recursive_attn_processor(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: self.set_attn_processor(AttnProcessor() ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = True , ) ->List[Any]: lowerCAmelCase = hidden_states.shape[0] lowerCAmelCase = timestep if not torch.is_tensor(__lowerCamelCase ): lowerCAmelCase = torch.tensor([timesteps] , dtype=torch.long , device=hidden_states.device ) elif torch.is_tensor(__lowerCamelCase ) and len(timesteps.shape ) == 0: lowerCAmelCase = timesteps[None].to(hidden_states.device ) # broadcast to batch dimension in a way that's compatible with ONNX/Core ML lowerCAmelCase = timesteps * torch.ones(__lowerCamelCase , dtype=timesteps.dtype , device=timesteps.device ) lowerCAmelCase = self.time_proj(__lowerCamelCase ) # timesteps does not contain any weights and will always return f32 tensors # but time_embedding might be fp16, so we need to cast here. lowerCAmelCase = timesteps_projected.to(dtype=self.dtype ) lowerCAmelCase = self.time_embedding(__lowerCamelCase ) if self.embedding_proj_norm is not None: lowerCAmelCase = self.embedding_proj_norm(__lowerCamelCase ) lowerCAmelCase = self.embedding_proj(__lowerCamelCase ) if self.encoder_hidden_states_proj is not None and encoder_hidden_states is not None: lowerCAmelCase = self.encoder_hidden_states_proj(__lowerCamelCase ) elif self.encoder_hidden_states_proj is not None and encoder_hidden_states is None: raise ValueError('''`encoder_hidden_states_proj` requires `encoder_hidden_states` to be set''' ) lowerCAmelCase = self.proj_in(__lowerCamelCase ) lowerCAmelCase = self.positional_embedding.to(hidden_states.dtype ) lowerCAmelCase = [] lowerCAmelCase = 0 if encoder_hidden_states is not None: additional_embeds.append(__lowerCamelCase ) additional_embeddings_len += encoder_hidden_states.shape[1] if len(proj_embeddings.shape ) == 2: lowerCAmelCase = proj_embeddings[:, None, :] if len(hidden_states.shape ) == 2: lowerCAmelCase = hidden_states[:, None, :] lowerCAmelCase = additional_embeds + [ proj_embeddings, time_embeddings[:, None, :], hidden_states, ] if self.prd_embedding is not None: lowerCAmelCase = self.prd_embedding.to(hidden_states.dtype ).expand(__lowerCamelCase , -1 , -1 ) additional_embeds.append(__lowerCamelCase ) lowerCAmelCase = torch.cat( __lowerCamelCase , dim=1 , ) # Allow positional_embedding to not include the `addtional_embeddings` and instead pad it with zeros for these additional tokens lowerCAmelCase = additional_embeddings_len + proj_embeddings.shape[1] + 1 if positional_embeddings.shape[1] < hidden_states.shape[1]: lowerCAmelCase = F.pad( __lowerCamelCase , ( 0, 0, additional_embeddings_len, self.prd_embedding.shape[1] if self.prd_embedding is not None else 0, ) , value=0.0 , ) lowerCAmelCase = hidden_states + positional_embeddings if attention_mask is not None: lowerCAmelCase = (1 - attention_mask.to(hidden_states.dtype )) * -1_0_0_0_0.0 lowerCAmelCase = F.pad(__lowerCamelCase , (0, self.additional_embeddings) , value=0.0 ) lowerCAmelCase = (attention_mask[:, None, :] + self.causal_attention_mask).to(hidden_states.dtype ) lowerCAmelCase = attention_mask.repeat_interleave(self.config.num_attention_heads , dim=0 ) if self.norm_in is not None: lowerCAmelCase = self.norm_in(__lowerCamelCase ) for block in self.transformer_blocks: lowerCAmelCase = block(__lowerCamelCase , attention_mask=__lowerCamelCase ) lowerCAmelCase = self.norm_out(__lowerCamelCase ) if self.prd_embedding is not None: lowerCAmelCase = hidden_states[:, -1] else: lowerCAmelCase = hidden_states[:, additional_embeddings_len:] lowerCAmelCase = self.proj_to_clip_embeddings(__lowerCamelCase ) if not return_dict: return (predicted_image_embedding,) return PriorTransformerOutput(predicted_image_embedding=__lowerCamelCase ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->List[Any]: lowerCAmelCase = (prior_latents * self.clip_std) + self.clip_mean return prior_latents
338
import os import re from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _SCREAMING_SNAKE_CASE : Any = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE : str = { '''vocab_file''': '''vocab.txt''', '''merges_file''': '''bpe.codes''', } _SCREAMING_SNAKE_CASE : Dict = { '''vocab_file''': { '''vinai/phobert-base''': '''https://huggingface.co/vinai/phobert-base/resolve/main/vocab.txt''', '''vinai/phobert-large''': '''https://huggingface.co/vinai/phobert-large/resolve/main/vocab.txt''', }, '''merges_file''': { '''vinai/phobert-base''': '''https://huggingface.co/vinai/phobert-base/resolve/main/bpe.codes''', '''vinai/phobert-large''': '''https://huggingface.co/vinai/phobert-large/resolve/main/bpe.codes''', }, } _SCREAMING_SNAKE_CASE : Optional[int] = { '''vinai/phobert-base''': 256, '''vinai/phobert-large''': 256, } def UpperCAmelCase_ ( _A ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = set() SCREAMING_SNAKE_CASE__ = word[0] for char in word[1:]: pairs.add((prev_char, char) ) SCREAMING_SNAKE_CASE__ = char SCREAMING_SNAKE_CASE__ = set(_A ) return pairs class UpperCAmelCase__ ( A__ ): """simple docstring""" a = VOCAB_FILES_NAMES a = PRETRAINED_VOCAB_FILES_MAP a = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self : Union[str, Any] , __lowerCamelCase : Dict , __lowerCamelCase : str , __lowerCamelCase : Optional[Any]="<s>" , __lowerCamelCase : List[str]="</s>" , __lowerCamelCase : Dict="</s>" , __lowerCamelCase : Dict="<s>" , __lowerCamelCase : List[str]="<unk>" , __lowerCamelCase : Optional[Any]="<pad>" , __lowerCamelCase : Union[str, Any]="<mask>" , **__lowerCamelCase : Optional[int] , ) -> Union[str, Any]: super().__init__( bos_token=__lowerCamelCase , eos_token=__lowerCamelCase , unk_token=__lowerCamelCase , sep_token=__lowerCamelCase , cls_token=__lowerCamelCase , pad_token=__lowerCamelCase , mask_token=__lowerCamelCase , **__lowerCamelCase , ) SCREAMING_SNAKE_CASE__ = vocab_file SCREAMING_SNAKE_CASE__ = merges_file SCREAMING_SNAKE_CASE__ = {} SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = 1 SCREAMING_SNAKE_CASE__ = 2 SCREAMING_SNAKE_CASE__ = 3 self.add_from_file(__lowerCamelCase ) SCREAMING_SNAKE_CASE__ = {v: k for k, v in self.encoder.items()} with open(__lowerCamelCase , encoding='''utf-8''' ) as merges_handle: SCREAMING_SNAKE_CASE__ = merges_handle.read().split('''\n''' )[:-1] SCREAMING_SNAKE_CASE__ = [tuple(merge.split()[:-1] ) for merge in merges] SCREAMING_SNAKE_CASE__ = dict(zip(__lowerCamelCase , range(len(__lowerCamelCase ) ) ) ) SCREAMING_SNAKE_CASE__ = {} def lowercase_ ( self : Dict , __lowerCamelCase : List[int] , __lowerCamelCase : Optional[List[int]] = None ) -> List[int]: if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] SCREAMING_SNAKE_CASE__ = [self.cls_token_id] SCREAMING_SNAKE_CASE__ = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def lowercase_ ( self : Union[str, Any] , __lowerCamelCase : List[int] , __lowerCamelCase : Optional[List[int]] = None , __lowerCamelCase : bool = False ) -> List[int]: 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, 1] + ([0] * len(__lowerCamelCase )) + [1] def lowercase_ ( self : List[Any] , __lowerCamelCase : List[int] , __lowerCamelCase : Optional[List[int]] = None ) -> List[int]: SCREAMING_SNAKE_CASE__ = [self.sep_token_id] SCREAMING_SNAKE_CASE__ = [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] @property def lowercase_ ( self : Dict ) -> str: return len(self.encoder ) def lowercase_ ( self : List[Any] ) -> str: return dict(self.encoder , **self.added_tokens_encoder ) def lowercase_ ( self : Any , __lowerCamelCase : Any ) -> Any: if token in self.cache: return self.cache[token] SCREAMING_SNAKE_CASE__ = tuple(__lowerCamelCase ) SCREAMING_SNAKE_CASE__ = tuple(list(word[:-1] ) + [word[-1] + '''</w>'''] ) SCREAMING_SNAKE_CASE__ = get_pairs(__lowerCamelCase ) if not pairs: return token while True: SCREAMING_SNAKE_CASE__ = min(__lowerCamelCase , key=lambda __lowerCamelCase : self.bpe_ranks.get(__lowerCamelCase , float('''inf''' ) ) ) if bigram not in self.bpe_ranks: break SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__ = bigram SCREAMING_SNAKE_CASE__ = [] SCREAMING_SNAKE_CASE__ = 0 while i < len(__lowerCamelCase ): try: SCREAMING_SNAKE_CASE__ = word.index(__lowerCamelCase , __lowerCamelCase ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) SCREAMING_SNAKE_CASE__ = j if word[i] == first and i < len(__lowerCamelCase ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 SCREAMING_SNAKE_CASE__ = tuple(__lowerCamelCase ) SCREAMING_SNAKE_CASE__ = new_word if len(__lowerCamelCase ) == 1: break else: SCREAMING_SNAKE_CASE__ = get_pairs(__lowerCamelCase ) SCREAMING_SNAKE_CASE__ = '''@@ '''.join(__lowerCamelCase ) SCREAMING_SNAKE_CASE__ = word[:-4] SCREAMING_SNAKE_CASE__ = word return word def lowercase_ ( self : Optional[Any] , __lowerCamelCase : List[Any] ) -> List[Any]: SCREAMING_SNAKE_CASE__ = [] SCREAMING_SNAKE_CASE__ = re.findall(r'''\S+\n?''' , __lowerCamelCase ) for token in words: split_tokens.extend(list(self.bpe(__lowerCamelCase ).split(''' ''' ) ) ) return split_tokens def lowercase_ ( self : str , __lowerCamelCase : Optional[int] ) -> Optional[int]: return self.encoder.get(__lowerCamelCase , self.encoder.get(self.unk_token ) ) def lowercase_ ( self : List[Any] , __lowerCamelCase : List[str] ) -> Dict: return self.decoder.get(__lowerCamelCase , self.unk_token ) def lowercase_ ( self : Union[str, Any] , __lowerCamelCase : str ) -> Optional[int]: SCREAMING_SNAKE_CASE__ = ''' '''.join(__lowerCamelCase ).replace('''@@ ''' , '''''' ).strip() return out_string def lowercase_ ( self : Dict , __lowerCamelCase : str , __lowerCamelCase : Optional[str] = None ) -> Tuple[str]: if not os.path.isdir(__lowerCamelCase ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return SCREAMING_SNAKE_CASE__ = os.path.join( __lowerCamelCase , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) SCREAMING_SNAKE_CASE__ = os.path.join( __lowerCamelCase , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''merges_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__lowerCamelCase ): copyfile(self.vocab_file , __lowerCamelCase ) if os.path.abspath(self.merges_file ) != os.path.abspath(__lowerCamelCase ): copyfile(self.merges_file , __lowerCamelCase ) return out_vocab_file, out_merge_file def lowercase_ ( self : int , __lowerCamelCase : Tuple ) -> Optional[Any]: if isinstance(__lowerCamelCase , __lowerCamelCase ): try: with open(__lowerCamelCase , '''r''' , encoding='''utf-8''' ) as fd: self.add_from_file(__lowerCamelCase ) except FileNotFoundError as fnfe: raise fnfe except UnicodeError: raise Exception(f'''Incorrect encoding detected in {f}, please rebuild the dataset''' ) return SCREAMING_SNAKE_CASE__ = f.readlines() for lineTmp in lines: SCREAMING_SNAKE_CASE__ = lineTmp.strip() SCREAMING_SNAKE_CASE__ = line.rfind(''' ''' ) if idx == -1: raise ValueError('''Incorrect dictionary format, expected \'<token> <cnt>\'''' ) SCREAMING_SNAKE_CASE__ = line[:idx] SCREAMING_SNAKE_CASE__ = len(self.encoder )
314
0
"""simple docstring""" import importlib.metadata import operator import re import sys from typing import Optional from packaging import version UpperCAmelCase_ : Dict = { '<': operator.lt, '<=': operator.le, '==': operator.eq, '!=': operator.ne, '>=': operator.ge, '>': operator.gt, } def _A (__a , __a , __a , __a , __a , __a ) -> str: """simple docstring""" if got_ver is None or want_ver is None: raise ValueError( f'Unable to compare versions for {requirement}: need={want_ver} found={got_ver}. This is unusual. Consider' f' reinstalling {pkg}.' ) if not ops[op](version.parse(UpperCamelCase__ ) , version.parse(UpperCamelCase__ ) ): raise ImportError( f'{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}' ) def _A (__a , __a = None ) -> None: """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = f'\n{hint}' if hint is not None else '''''' # non-versioned check if re.match(R'''^[\w_\-\d]+$''' , UpperCamelCase__ ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Dict = requirement, None, None else: SCREAMING_SNAKE_CASE_ : int = re.findall(R'''^([^!=<>\s]+)([\s!=<>]{1,2}.+)''' , UpperCamelCase__ ) if not match: raise ValueError( '''requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23, but''' f' got {requirement}' ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = match[0] SCREAMING_SNAKE_CASE_ : List[Any] = want_full.split(''',''' ) # there could be multiple requirements SCREAMING_SNAKE_CASE_ : Dict = {} for w in want_range: SCREAMING_SNAKE_CASE_ : Optional[Any] = re.findall(R'''^([\s!=<>]{1,2})(.+)''' , UpperCamelCase__ ) if not match: raise ValueError( '''requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23,''' f' but got {requirement}' ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : List[Any] = match[0] SCREAMING_SNAKE_CASE_ : Optional[int] = want_ver if op not in ops: raise ValueError(f'{requirement}: need one of {list(ops.keys() )}, but got {op}' ) # special case if pkg == "python": SCREAMING_SNAKE_CASE_ : str = '''.'''.join([str(UpperCamelCase__ ) for x in sys.version_info[:3]] ) for op, want_ver in wanted.items(): _compare_versions(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) return # check if any version is installed try: SCREAMING_SNAKE_CASE_ : List[Any] = importlib.metadata.version(UpperCamelCase__ ) except importlib.metadata.PackageNotFoundError: raise importlib.metadata.PackageNotFoundError( f'The \'{requirement}\' distribution was not found and is required by this application. {hint}' ) # check that the right version is installed if version number or a range was provided if want_ver is not None: for op, want_ver in wanted.items(): _compare_versions(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) def _A (__a ) -> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE_ : int = '''Try: pip install transformers -U or pip install -e \'.[dev]\' if you\'re working with git main''' return require_version(UpperCamelCase__ , UpperCamelCase__ )
367
"""simple docstring""" # tests directory-specific settings - this file is run automatically # by pytest before any tests are run import sys import warnings from os.path import abspath, dirname, join # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. UpperCAmelCase_ : Union[str, Any] = abspath(join(dirname(dirname(dirname(__file__))), """src""")) sys.path.insert(1, git_repo_path) # silence FutureWarning warnings in tests since often we can't act on them until # they become normal warnings - i.e. the tests still need to test the current functionality warnings.simplefilter(action="""ignore""", category=FutureWarning) def _A (__a ) -> Union[str, Any]: """simple docstring""" from transformers.testing_utils import pytest_addoption_shared pytest_addoption_shared(__a ) def _A (__a ) -> Any: """simple docstring""" from transformers.testing_utils import pytest_terminal_summary_main SCREAMING_SNAKE_CASE_ : Optional[Any] = terminalreporter.config.getoption('''--make-reports''' ) if make_reports: pytest_terminal_summary_main(__a , id=__a )
318
0
import json import multiprocessing as mp import re from collections import defaultdict from functools import partial from typing import Dict, List, Optional, Set, Tuple, Type from datasets import Dataset from datasketch import MinHash, MinHashLSH from dpu_utils.utils.iterators import ThreadedIterator from tqdm import tqdm lowerCamelCase_ = re.compile('''[^A-Za-z_0-9]''') # parameters used in DuplicationIndex lowerCamelCase_ = 10 lowerCamelCase_ = 2_56 def __magic_name__ ( __a : List[str] ): '''simple docstring''' if len(__a ) < MIN_NUM_TOKENS: return None UpperCamelCase__ = MinHash(num_perm=__a ) for token in set(__a ): min_hash.update(token.encode() ) return min_hash def __magic_name__ ( __a : str ): '''simple docstring''' return {t for t in NON_ALPHA.split(__a ) if len(t.strip() ) > 0} class __A: """simple docstring""" def __init__(self , *, SCREAMING_SNAKE_CASE_ = 0.85 , ): UpperCamelCase__ = duplication_jaccard_threshold UpperCamelCase__ = NUM_PERM UpperCamelCase__ = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) UpperCamelCase__ = defaultdict(SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): UpperCamelCase__ = self._index.query(SCREAMING_SNAKE_CASE_ ) if code_key in self._index.keys: print(F"Duplicate key {code_key}" ) return self._index.insert(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if len(SCREAMING_SNAKE_CASE_ ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(SCREAMING_SNAKE_CASE_ ) break else: self._duplicate_clusters[close_duplicates[0]].add(SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase_ (self ): UpperCamelCase__ = [] for base, duplicates in self._duplicate_clusters.items(): UpperCamelCase__ = [base] + list(SCREAMING_SNAKE_CASE_ ) # reformat the cluster to be a list of dict UpperCamelCase__ = [{"""base_index""": el[0], """repo_name""": el[1], """path""": el[2]} for el in cluster] duplicate_clusters.append(SCREAMING_SNAKE_CASE_ ) return duplicate_clusters def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ ): UpperCamelCase__ = self.get_duplicate_clusters() with open(SCREAMING_SNAKE_CASE_ , """w""" ) as f: json.dump(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def __magic_name__ ( __a : Dict ): '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ = element UpperCamelCase__ = get_min_hash([t for t in NON_ALPHA.split(data["""content"""] ) if len(t.strip() ) > 0] ) if min_hash is not None: return (index, data["repo_name"], data["path"]), min_hash def __magic_name__ ( __a : Type[Dataset] ): '''simple docstring''' with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(__a , max_queue_size=10_000 ) , chunksize=100 , ): if data is not None: yield data def __magic_name__ ( __a : Type[Dataset] , __a : float ): '''simple docstring''' UpperCamelCase__ = DuplicationIndex(duplication_jaccard_threshold=__a ) for filename, min_hash in tqdm(ThreadedIterator(minhash_iter(enumerate(__a ) ) , max_queue_size=100 ) ): di.add(__a , __a ) # Returns a List[Cluster] where Cluster is List[str] with the filenames. return di.get_duplicate_clusters() def __magic_name__ ( __a : str , __a : str ): '''simple docstring''' UpperCamelCase__ = get_tokens(__a ) UpperCamelCase__ = get_tokens(__a ) return len(tokensa & tokensa ) / len(tokensa | tokensa ) lowerCamelCase_ = None def __magic_name__ ( __a : Optional[Any] , __a : List[Any] ): '''simple docstring''' UpperCamelCase__ = [] for elementa in cluster: UpperCamelCase__ = _shared_dataset[elementa["""base_index"""]]["""content"""] for elementa in extremes: UpperCamelCase__ = _shared_dataset[elementa["""base_index"""]]["""content"""] if jaccard_similarity(__a , __a ) >= jaccard_threshold: elementa["copies"] += 1 break else: UpperCamelCase__ = 1 extremes.append(__a ) return extremes def __magic_name__ ( __a : int , __a : int , __a : Any ): '''simple docstring''' global _shared_dataset UpperCamelCase__ = dataset UpperCamelCase__ = [] UpperCamelCase__ = partial(_find_cluster_extremes_shared , jaccard_threshold=__a ) with mp.Pool() as pool: for extremes in tqdm( pool.imap_unordered( __a , __a , ) , total=len(__a ) , ): extremes_list.append(__a ) return extremes_list def __magic_name__ ( __a : Type[Dataset] , __a : float = 0.85 ): '''simple docstring''' UpperCamelCase__ = make_duplicate_clusters(__a , __a ) UpperCamelCase__ = {x["""base_index"""] for cluster in duplicate_clusters for x in cluster} UpperCamelCase__ = {} UpperCamelCase__ = find_extremes(__a , __a , __a ) for extremes in extremes_clusters: for element in extremes: UpperCamelCase__ = element UpperCamelCase__ = duplicate_indices - set(extreme_dict.keys() ) UpperCamelCase__ = dataset.filter(lambda __a , __a : idx not in remove_indices , with_indices=__a ) # update duplicate_clusters for cluster in duplicate_clusters: for element in cluster: UpperCamelCase__ = element["""base_index"""] in extreme_dict if element["is_extreme"]: UpperCamelCase__ = extreme_dict[element["""base_index"""]]["""copies"""] print(f"Original dataset size: {len(__a )}" ) print(f"Number of duplicate clusters: {len(__a )}" ) print(f"Files in duplicate cluster: {len(__a )}" ) print(f"Unique files in duplicate cluster: {len(__a )}" ) print(f"Filtered dataset size: {len(__a )}" ) return ds_filter, duplicate_clusters
244
from __future__ import annotations import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFViTForImageClassification, TFViTModel if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __A: """simple docstring""" def __init__(self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=13 , SCREAMING_SNAKE_CASE_=30 , SCREAMING_SNAKE_CASE_=2 , SCREAMING_SNAKE_CASE_=3 , SCREAMING_SNAKE_CASE_=True , SCREAMING_SNAKE_CASE_=True , SCREAMING_SNAKE_CASE_=32 , SCREAMING_SNAKE_CASE_=2 , SCREAMING_SNAKE_CASE_=4 , SCREAMING_SNAKE_CASE_=37 , SCREAMING_SNAKE_CASE_="gelu" , SCREAMING_SNAKE_CASE_=0.1 , SCREAMING_SNAKE_CASE_=0.1 , SCREAMING_SNAKE_CASE_=10 , SCREAMING_SNAKE_CASE_=0.02 , SCREAMING_SNAKE_CASE_=3 , SCREAMING_SNAKE_CASE_=None , ): UpperCamelCase__ = parent UpperCamelCase__ = batch_size UpperCamelCase__ = image_size UpperCamelCase__ = patch_size UpperCamelCase__ = num_channels UpperCamelCase__ = is_training UpperCamelCase__ = use_labels 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__ = type_sequence_label_size UpperCamelCase__ = initializer_range UpperCamelCase__ = scope # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) UpperCamelCase__ = (image_size // patch_size) ** 2 UpperCamelCase__ = num_patches + 1 def UpperCAmelCase_ (self ): UpperCamelCase__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCamelCase__ = None if self.use_labels: UpperCamelCase__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase__ = self.get_config() return config, pixel_values, labels def UpperCAmelCase_ (self ): return 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=SCREAMING_SNAKE_CASE_ , initializer_range=self.initializer_range , ) def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): UpperCamelCase__ = TFViTModel(config=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = model(SCREAMING_SNAKE_CASE_ , training=SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # Test with an image with different size than the one specified in config. UpperCamelCase__ = self.image_size // 2 UpperCamelCase__ = pixel_values[:, :, :image_size, :image_size] UpperCamelCase__ = model(SCREAMING_SNAKE_CASE_ , interpolate_pos_encoding=SCREAMING_SNAKE_CASE_ , training=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = (image_size // self.patch_size) ** 2 + 1 self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, seq_length, self.hidden_size) ) def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): UpperCamelCase__ = self.type_sequence_label_size UpperCamelCase__ = TFViTForImageClassification(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = model(SCREAMING_SNAKE_CASE_ , labels=SCREAMING_SNAKE_CASE_ , training=SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # Test with an image with different size than the one specified in config. UpperCamelCase__ = self.image_size // 2 UpperCamelCase__ = pixel_values[:, :, :image_size, :image_size] UpperCamelCase__ = model(SCREAMING_SNAKE_CASE_ , interpolate_pos_encoding=SCREAMING_SNAKE_CASE_ , training=SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images UpperCamelCase__ = 1 UpperCamelCase__ = TFViTForImageClassification(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) UpperCamelCase__ = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def UpperCAmelCase_ (self ): UpperCamelCase__ = self.prepare_config_and_inputs() UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = config_and_inputs UpperCamelCase__ = {"""pixel_values""": pixel_values} return config, inputs_dict @require_tf class __A( __lowerCamelCase , __lowerCamelCase , unittest.TestCase ): """simple docstring""" SCREAMING_SNAKE_CASE__ = (TFViTModel, TFViTForImageClassification) if is_tf_available() else () SCREAMING_SNAKE_CASE__ = ( {"""feature-extraction""": TFViTModel, """image-classification""": TFViTForImageClassification} if is_tf_available() else {} ) SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False def UpperCAmelCase_ (self ): UpperCamelCase__ = TFViTModelTester(self ) UpperCamelCase__ = ConfigTester(self , config_class=SCREAMING_SNAKE_CASE_ , has_text_modality=SCREAMING_SNAKE_CASE_ , hidden_size=37 ) def UpperCAmelCase_ (self ): self.config_tester.run_common_tests() @unittest.skip(reason="""ViT does not use inputs_embeds""" ) def UpperCAmelCase_ (self ): pass @unittest.skip(reason="""ViT does not use inputs_embeds""" ) def UpperCAmelCase_ (self ): pass def UpperCAmelCase_ (self ): UpperCamelCase__ , UpperCamelCase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase__ = model_class(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(model.get_input_embeddings() , (tf.keras.layers.Layer) ) UpperCamelCase__ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE_ , tf.keras.layers.Layer ) ) def UpperCAmelCase_ (self ): UpperCamelCase__ , UpperCamelCase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase__ = model_class(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCamelCase__ = [*signature.parameters.keys()] UpperCamelCase__ = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase_ (self ): UpperCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase_ (self ): UpperCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_ ) @slow def UpperCAmelCase_ (self ): UpperCamelCase__ = TFViTModel.from_pretrained("""google/vit-base-patch16-224""" ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) def __magic_name__ ( ): '''simple docstring''' UpperCamelCase__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_tf @require_vision class __A( unittest.TestCase ): """simple docstring""" @cached_property def UpperCAmelCase_ (self ): return ViTImageProcessor.from_pretrained("""google/vit-base-patch16-224""" ) if is_vision_available() else None @slow def UpperCAmelCase_ (self ): UpperCamelCase__ = TFViTForImageClassification.from_pretrained("""google/vit-base-patch16-224""" ) UpperCamelCase__ = self.default_image_processor UpperCamelCase__ = prepare_img() UpperCamelCase__ = image_processor(images=SCREAMING_SNAKE_CASE_ , return_tensors="""tf""" ) # forward pass UpperCamelCase__ = model(**SCREAMING_SNAKE_CASE_ ) # verify the logits UpperCamelCase__ = tf.TensorShape((1, 10_00) ) self.assertEqual(outputs.logits.shape , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = tf.constant([-0.2744, 0.8215, -0.0836] ) tf.debugging.assert_near(outputs.logits[0, :3] , SCREAMING_SNAKE_CASE_ , atol=1E-4 )
244
1
"""simple docstring""" def a__ ( lowerCAmelCase ) -> int: if not isinstance(lowerCAmelCase , lowerCAmelCase ): raise ValueError("""Input must be an integer""" ) if input_num <= 0: raise ValueError("""Input must be positive""" ) return sum( divisor for divisor in range(1 , input_num // 2 + 1 ) if input_num % divisor == 0 ) if __name__ == "__main__": import doctest doctest.testmod()
166
"""simple docstring""" from dataclasses import dataclass from typing import Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput from .embeddings import GaussianFourierProjection, TimestepEmbedding, Timesteps from .modeling_utils import ModelMixin from .unet_ad_blocks import get_down_block, get_mid_block, get_out_block, get_up_block @dataclass class lowerCamelCase ( lowerCAmelCase__ ): '''simple docstring''' SCREAMING_SNAKE_CASE = 42 class lowerCamelCase ( lowerCAmelCase__ , lowerCAmelCase__ ): '''simple docstring''' @register_to_config def __init__(self , _lowerCamelCase = 65536 , _lowerCamelCase = None , _lowerCamelCase = 2 , _lowerCamelCase = 2 , _lowerCamelCase = 0 , _lowerCamelCase = "fourier" , _lowerCamelCase = True , _lowerCamelCase = False , _lowerCamelCase = 0.0 , _lowerCamelCase = ("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D") , _lowerCamelCase = ("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip") , _lowerCamelCase = "UNetMidBlock1D" , _lowerCamelCase = None , _lowerCamelCase = (32, 32, 64) , _lowerCamelCase = None , _lowerCamelCase = 8 , _lowerCamelCase = 1 , _lowerCamelCase = False , ): """simple docstring""" super().__init__() UpperCAmelCase__ : str = sample_size # time if time_embedding_type == "fourier": UpperCAmelCase__ : Any = GaussianFourierProjection( embedding_size=8 , set_W_to_weight=_lowerCamelCase , log=_lowerCamelCase , flip_sin_to_cos=_lowerCamelCase ) UpperCAmelCase__ : Tuple = 2 * block_out_channels[0] elif time_embedding_type == "positional": UpperCAmelCase__ : List[str] = Timesteps( block_out_channels[0] , flip_sin_to_cos=_lowerCamelCase , downscale_freq_shift=_lowerCamelCase ) UpperCAmelCase__ : Optional[Any] = block_out_channels[0] if use_timestep_embedding: UpperCAmelCase__ : Optional[Any] = block_out_channels[0] * 4 UpperCAmelCase__ : Any = TimestepEmbedding( in_channels=_lowerCamelCase , time_embed_dim=_lowerCamelCase , act_fn=_lowerCamelCase , out_dim=block_out_channels[0] , ) UpperCAmelCase__ : Optional[Any] = nn.ModuleList([] ) UpperCAmelCase__ : int = None UpperCAmelCase__ : str = nn.ModuleList([] ) UpperCAmelCase__ : Optional[int] = None # down UpperCAmelCase__ : List[str] = in_channels for i, down_block_type in enumerate(_lowerCamelCase ): UpperCAmelCase__ : Optional[Any] = output_channel UpperCAmelCase__ : List[str] = block_out_channels[i] if i == 0: input_channel += extra_in_channels UpperCAmelCase__ : Any = i == len(_lowerCamelCase ) - 1 UpperCAmelCase__ : Dict = get_down_block( _lowerCamelCase , num_layers=_lowerCamelCase , in_channels=_lowerCamelCase , out_channels=_lowerCamelCase , temb_channels=block_out_channels[0] , add_downsample=not is_final_block or downsample_each_block , ) self.down_blocks.append(_lowerCamelCase ) # mid UpperCAmelCase__ : Optional[Any] = get_mid_block( _lowerCamelCase , in_channels=block_out_channels[-1] , mid_channels=block_out_channels[-1] , out_channels=block_out_channels[-1] , embed_dim=block_out_channels[0] , num_layers=_lowerCamelCase , add_downsample=_lowerCamelCase , ) # up UpperCAmelCase__ : Tuple = list(reversed(_lowerCamelCase ) ) UpperCAmelCase__ : List[str] = reversed_block_out_channels[0] if out_block_type is None: UpperCAmelCase__ : int = out_channels else: UpperCAmelCase__ : List[Any] = block_out_channels[0] for i, up_block_type in enumerate(_lowerCamelCase ): UpperCAmelCase__ : Any = output_channel UpperCAmelCase__ : Dict = ( reversed_block_out_channels[i + 1] if i < len(_lowerCamelCase ) - 1 else final_upsample_channels ) UpperCAmelCase__ : Union[str, Any] = i == len(_lowerCamelCase ) - 1 UpperCAmelCase__ : Optional[int] = get_up_block( _lowerCamelCase , num_layers=_lowerCamelCase , in_channels=_lowerCamelCase , out_channels=_lowerCamelCase , temb_channels=block_out_channels[0] , add_upsample=not is_final_block , ) self.up_blocks.append(_lowerCamelCase ) UpperCAmelCase__ : Dict = output_channel # out UpperCAmelCase__ : Optional[int] = norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4 , 32 ) UpperCAmelCase__ : int = get_out_block( out_block_type=_lowerCamelCase , num_groups_out=_lowerCamelCase , embed_dim=block_out_channels[0] , out_channels=_lowerCamelCase , act_fn=_lowerCamelCase , fc_dim=block_out_channels[-1] // 4 , ) def _a (self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase = True , ): """simple docstring""" UpperCAmelCase__ : Union[str, Any] = timestep if not torch.is_tensor(_lowerCamelCase ): UpperCAmelCase__ : Dict = torch.tensor([timesteps] , dtype=torch.long , device=sample.device ) elif torch.is_tensor(_lowerCamelCase ) and len(timesteps.shape ) == 0: UpperCAmelCase__ : List[str] = timesteps[None].to(sample.device ) UpperCAmelCase__ : Optional[Any] = self.time_proj(_lowerCamelCase ) if self.config.use_timestep_embedding: UpperCAmelCase__ : Dict = self.time_mlp(_lowerCamelCase ) else: UpperCAmelCase__ : int = timestep_embed[..., None] UpperCAmelCase__ : List[str] = timestep_embed.repeat([1, 1, sample.shape[2]] ).to(sample.dtype ) UpperCAmelCase__ : Dict = timestep_embed.broadcast_to((sample.shape[:1] + timestep_embed.shape[1:]) ) # 2. down UpperCAmelCase__ : Optional[Any] = () for downsample_block in self.down_blocks: UpperCAmelCase__ , UpperCAmelCase__ : Dict = downsample_block(hidden_states=_lowerCamelCase , temb=_lowerCamelCase ) down_block_res_samples += res_samples # 3. mid if self.mid_block: UpperCAmelCase__ : Optional[Any] = self.mid_block(_lowerCamelCase , _lowerCamelCase ) # 4. up for i, upsample_block in enumerate(self.up_blocks ): UpperCAmelCase__ : int = down_block_res_samples[-1:] UpperCAmelCase__ : Dict = down_block_res_samples[:-1] UpperCAmelCase__ : str = upsample_block(_lowerCamelCase , res_hidden_states_tuple=_lowerCamelCase , temb=_lowerCamelCase ) # 5. post-process if self.out_block: UpperCAmelCase__ : str = self.out_block(_lowerCamelCase , _lowerCamelCase ) if not return_dict: return (sample,) return UNetaDOutput(sample=_lowerCamelCase )
166
1
lowerCamelCase : Dict = 2_56 # Modulus to hash a string lowerCamelCase : List[str] = 1_00_00_03 def snake_case_ ( lowerCAmelCase_ : str , lowerCAmelCase_ : str ): __lowercase : Union[str, Any] = len(lowerCAmelCase_ ) __lowercase : Optional[Any] = len(lowerCAmelCase_ ) if p_len > t_len: return False __lowercase : Union[str, Any] = 0 __lowercase : Optional[int] = 0 __lowercase : str = 1 # Calculating the hash of pattern and substring of text for i in range(lowerCAmelCase_ ): __lowercase : Optional[int] = (ord(pattern[i] ) + p_hash * alphabet_size) % modulus __lowercase : Tuple = (ord(text[i] ) + text_hash * alphabet_size) % modulus if i == p_len - 1: continue __lowercase : Optional[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 __lowercase : Tuple = ( (text_hash - ord(text[i] ) * modulus_power) * alphabet_size + ord(text[i + p_len] ) ) % modulus return False def snake_case_ ( ): __lowercase : List[Any] = """abc1abc12""" __lowercase : List[Any] = """alskfjaldsabc1abc1abc12k23adsfabcabc""" __lowercase : Tuple = """alskfjaldsk23adsfabcabc""" assert rabin_karp(lowerCAmelCase_ , lowerCAmelCase_ ) and not rabin_karp(lowerCAmelCase_ , lowerCAmelCase_ ) # Test 2) __lowercase : str = """ABABX""" __lowercase : Dict = """ABABZABABYABABX""" assert rabin_karp(lowerCAmelCase_ , lowerCAmelCase_ ) # Test 3) __lowercase : Tuple = """AAAB""" __lowercase : Tuple = """ABAAAAAB""" assert rabin_karp(lowerCAmelCase_ , lowerCAmelCase_ ) # Test 4) __lowercase : str = """abcdabcy""" __lowercase : Dict = """abcxabcdabxabcdabcdabcy""" assert rabin_karp(lowerCAmelCase_ , lowerCAmelCase_ ) # Test 5) __lowercase : Optional[int] = """Lü""" __lowercase : Any = """Lüsai""" assert rabin_karp(lowerCAmelCase_ , lowerCAmelCase_ ) __lowercase : Union[str, Any] = """Lue""" assert not rabin_karp(lowerCAmelCase_ , lowerCAmelCase_ ) print("""Success.""" ) if __name__ == "__main__": test_rabin_karp()
233
from __future__ import annotations import math lowerCamelCase : List[Any] = '''2020.9.26''' lowerCamelCase : str = '''xcodz-dot, cclaus, dhruvmanila''' def snake_case_ ( lowerCAmelCase_ : float , lowerCAmelCase_ : float , lowerCAmelCase_ : float , lowerCAmelCase_ : float , lowerCAmelCase_ : float ): if not all(isinstance(lowerCAmelCase_ , (float, int) ) for val in locals().values() ): __lowercase : str = F"Input values must either be float or int: {list(locals().values() )}" raise TypeError(lowerCAmelCase_ ) __lowercase : List[Any] = ((x * distance) / (z + distance)) * scale __lowercase : Tuple = ((y * distance) / (z + distance)) * scale return projected_x, projected_y def snake_case_ ( lowerCAmelCase_ : float , lowerCAmelCase_ : float , lowerCAmelCase_ : float , lowerCAmelCase_ : str , lowerCAmelCase_ : float ): if not isinstance(lowerCAmelCase_ , lowerCAmelCase_ ): raise TypeError("""Axis must be a str""" ) __lowercase : Optional[int] = locals() del input_variables["axis"] if not all(isinstance(lowerCAmelCase_ , (float, int) ) for val in input_variables.values() ): __lowercase : List[str] = ( """Input values except axis must either be float or int: """ F"{list(input_variables.values() )}" ) raise TypeError(lowerCAmelCase_ ) __lowercase : Tuple = (angle % 360) / 450 * 180 / math.pi if axis == "z": __lowercase : int = x * math.cos(lowerCAmelCase_ ) - y * math.sin(lowerCAmelCase_ ) __lowercase : Tuple = y * math.cos(lowerCAmelCase_ ) + x * math.sin(lowerCAmelCase_ ) __lowercase : Union[str, Any] = z elif axis == "x": __lowercase : str = y * math.cos(lowerCAmelCase_ ) - z * math.sin(lowerCAmelCase_ ) __lowercase : Dict = z * math.cos(lowerCAmelCase_ ) + y * math.sin(lowerCAmelCase_ ) __lowercase : List[str] = x elif axis == "y": __lowercase : List[str] = x * math.cos(lowerCAmelCase_ ) - z * math.sin(lowerCAmelCase_ ) __lowercase : List[str] = z * math.cos(lowerCAmelCase_ ) + x * math.sin(lowerCAmelCase_ ) __lowercase : List[Any] = y else: raise ValueError("""not a valid axis, choose one of 'x', 'y', 'z'""" ) return new_x, new_y, new_z if __name__ == "__main__": import doctest doctest.testmod() print(f'''{convert_to_ad(1.0, 2.0, 3.0, 10.0, 10.0) = }''') print(f'''{rotate(1.0, 2.0, 3.0, "y", 90.0) = }''')
233
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __a: Optional[int] = { 'configuration_upernet': ['UperNetConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a: List[str] = [ 'UperNetForSemanticSegmentation', 'UperNetPreTrainedModel', ] if TYPE_CHECKING: from .configuration_upernet import UperNetConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_upernet import UperNetForSemanticSegmentation, UperNetPreTrainedModel else: import sys __a: int = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
356
'''simple docstring''' import json import os import unittest from transformers import OpenAIGPTTokenizer, OpenAIGPTTokenizerFast from transformers.models.openai.tokenization_openai import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_spacy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class UpperCAmelCase ( a__ , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE = OpenAIGPTTokenizer SCREAMING_SNAKE_CASE = OpenAIGPTTokenizerFast SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = False def _lowerCAmelCase( self ) -> Optional[int]: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt lowercase__ : Optional[Any] = [ '''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>''', ] lowercase__ : Optional[int] = dict(zip(__lowerCAmelCase , range(len(__lowerCAmelCase ) ) ) ) lowercase__ : Union[str, Any] = ['''#version: 0.2''', '''l o''', '''lo w''', '''e r</w>''', ''''''] lowercase__ : Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) lowercase__ : Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' ) as fp: fp.write(json.dumps(__lowerCAmelCase ) ) with open(self.merges_file , '''w''' ) as fp: fp.write('''\n'''.join(__lowerCAmelCase ) ) def _lowerCAmelCase( self , __lowerCAmelCase ) -> Dict: return "lower newer", "lower newer" def _lowerCAmelCase( self ) -> List[Any]: lowercase__ : int = OpenAIGPTTokenizer(self.vocab_file , self.merges_file ) lowercase__ : List[Any] = '''lower''' lowercase__ : Any = ['''low''', '''er</w>'''] lowercase__ : Union[str, Any] = tokenizer.tokenize(__lowerCAmelCase ) self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase ) lowercase__ : Tuple = tokens + ['''<unk>'''] lowercase__ : Optional[Any] = [14, 15, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(__lowerCAmelCase ) , __lowerCAmelCase ) def _lowerCAmelCase( self , __lowerCAmelCase=15 ) -> Optional[int]: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): lowercase__ : List[str] = self.rust_tokenizer_class.from_pretrained(__lowerCAmelCase , **__lowerCAmelCase ) # Simple input lowercase__ : List[str] = '''This is a simple input''' lowercase__ : Any = ['''This is a simple input 1''', '''This is a simple input 2'''] lowercase__ : str = ('''This is a simple input''', '''This is a pair''') lowercase__ : str = [ ('''This is a simple input 1''', '''This is a simple input 2'''), ('''This is a simple pair 1''', '''This is a simple pair 2'''), ] # Simple input tests self.assertRaises(__lowerCAmelCase , tokenizer_r.encode , __lowerCAmelCase , max_length=__lowerCAmelCase , padding='''max_length''' ) # Simple input self.assertRaises(__lowerCAmelCase , tokenizer_r.encode_plus , __lowerCAmelCase , max_length=__lowerCAmelCase , padding='''max_length''' ) # Simple input self.assertRaises( __lowerCAmelCase , tokenizer_r.batch_encode_plus , __lowerCAmelCase , max_length=__lowerCAmelCase , padding='''max_length''' , ) # Pair input self.assertRaises(__lowerCAmelCase , tokenizer_r.encode , __lowerCAmelCase , max_length=__lowerCAmelCase , padding='''max_length''' ) # Pair input self.assertRaises(__lowerCAmelCase , tokenizer_r.encode_plus , __lowerCAmelCase , max_length=__lowerCAmelCase , padding='''max_length''' ) # Pair input self.assertRaises( __lowerCAmelCase , tokenizer_r.batch_encode_plus , __lowerCAmelCase , max_length=__lowerCAmelCase , padding='''max_length''' , ) def _lowerCAmelCase( self ) -> Tuple: pass @require_ftfy @require_spacy @require_tokenizers class UpperCAmelCase ( a__ ): '''simple docstring''' pass
214
0
import logging import numpy as np import pytest from scipy.linalg import eigh logging.basicConfig(level=logging.INFO, format='''%(message)s''') def lowerCAmelCase__( lowercase : np.ndarray ) -> List[str]: return input_array.reshape((input_array.size, 1) ) def lowerCAmelCase__( lowercase : np.ndarray , lowercase : np.ndarray , lowercase : int ) -> int: __snake_case : List[Any] = np.nan for i in range(_A ): __snake_case : List[Any] = features[:, labels == i] __snake_case : str = data.mean(1 ) # Centralize the data of class i __snake_case : List[str] = data - column_reshape(_A ) if i > 0: # If covariance_sum is not None covariance_sum += np.dot(_A , centered_data.T ) else: # If covariance_sum is np.nan (i.e. first loop) __snake_case : Tuple = np.dot(_A , centered_data.T ) return covariance_sum / features.shape[1] def lowerCAmelCase__( lowercase : np.ndarray , lowercase : np.ndarray , lowercase : int ) -> str: __snake_case : Optional[int] = features.mean(1 ) __snake_case : Optional[int] = np.nan for i in range(_A ): __snake_case : str = features[:, labels == i] __snake_case : List[str] = data.shape[1] __snake_case : Any = data.mean(1 ) if i > 0: # If covariance_sum is not None covariance_sum += device_data * np.dot( column_reshape(_A ) - column_reshape(_A ) , (column_reshape(_A ) - column_reshape(_A )).T , ) else: # If covariance_sum is np.nan (i.e. first loop) __snake_case : Optional[int] = device_data * np.dot( column_reshape(_A ) - column_reshape(_A ) , (column_reshape(_A ) - column_reshape(_A )).T , ) return covariance_sum / features.shape[1] def lowerCAmelCase__( lowercase : np.ndarray , lowercase : int ) -> List[Any]: if features.any(): __snake_case : List[Any] = features.mean(1 ) # Center the dataset __snake_case : str = features - np.reshape(_A , (data_mean.size, 1) ) __snake_case : Dict = np.dot(_A , centered_data.T ) / features.shape[1] __snake_case : List[Any] = np.linalg.eigh(_A ) # Take all the columns in the reverse order (-1), and then takes only the first __snake_case : List[Any] = eigenvectors[:, ::-1][:, 0:dimensions] # Project the database on the new space __snake_case : Dict = np.dot(filtered_eigenvectors.T , _A ) logging.info("Principal Component Analysis computed" ) return projected_data else: logging.basicConfig(level=logging.ERROR , format="%(message)s" , force=_A ) logging.error("Dataset empty" ) raise AssertionError def lowerCAmelCase__( lowercase : np.ndarray , lowercase : np.ndarray , lowercase : int , lowercase : int ) -> List[Any]: assert classes > dimensions # Check if features have been already loaded if features.any: __snake_case : Dict = eigh( covariance_between_classes(_A , _A , _A ) , covariance_within_classes(_A , _A , _A ) , ) __snake_case : int = eigenvectors[:, ::-1][:, :dimensions] __snake_case : Union[str, Any] = np.linalg.svd(_A ) __snake_case : Optional[int] = svd_matrix[:, 0:dimensions] __snake_case : Dict = np.dot(filtered_svd_matrix.T , _A ) logging.info("Linear Discriminant Analysis computed" ) return projected_data else: logging.basicConfig(level=logging.ERROR , format="%(message)s" , force=_A ) logging.error("Dataset empty" ) raise AssertionError def lowerCAmelCase__( ) -> int: __snake_case : int = np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]] ) __snake_case : List[str] = np.array([0, 0, 0, 1, 1] ) __snake_case : str = 2 __snake_case : Dict = 2 # Assert that the function raises an AssertionError if dimensions > classes with pytest.raises(_A ) as error_info: __snake_case : Tuple = linear_discriminant_analysis( _A , _A , _A , _A ) if isinstance(_A , np.ndarray ): raise AssertionError( "Did not raise AssertionError for dimensions > classes" ) assert error_info.type is AssertionError def lowerCAmelCase__( ) -> Tuple: __snake_case : Tuple = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]] ) __snake_case : str = 2 __snake_case : int = np.array([[6.9_2_8_2_0_3_2_3, 8.6_6_0_2_5_4_0_4, 1_0.3_9_2_3_0_4_8_5], [3.0, 3.0, 3.0]] ) with pytest.raises(_A ) as error_info: __snake_case : Optional[int] = principal_component_analysis(_A , _A ) if not np.allclose(_A , _A ): raise AssertionError assert error_info.type is AssertionError if __name__ == "__main__": import doctest doctest.testmod()
326
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, Features, Value from .base import TaskTemplate @dataclass(frozen=lowercase__) class _lowercase ( lowercase__): """simple docstring""" A__ = field(default="automatic-speech-recognition" , metadata={"include_in_asdict_even_if_is_default": True}) A__ = Features({"audio": Audio()}) A__ = Features({"transcription": Value("string")}) A__ = "audio" A__ = "transcription" def lowerCAmelCase ( self : Any , __lowerCamelCase : int ): '''simple docstring''' if self.audio_column not in features: raise ValueError(f"Column {self.audio_column} is not present in features." ) if not isinstance(features[self.audio_column] , __lowerCamelCase ): raise ValueError(f"Column {self.audio_column} is not an Audio type." ) lowerCamelCase__ : Tuple = copy.deepcopy(self ) lowerCamelCase__ : Tuple = self.input_schema.copy() lowerCamelCase__ : Optional[int] = features[self.audio_column] lowerCamelCase__ : int = input_schema return task_template @property def lowerCAmelCase ( self : int ): '''simple docstring''' return {self.audio_column: "audio", self.transcription_column: "transcription"}
184
0
import gc import unittest from transformers import CTRLConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, CTRLForSequenceClassification, CTRLLMHeadModel, CTRLModel, ) class __lowercase : """simple docstring""" def __init__( self : List[str] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Union[str, Any]=14 , lowerCAmelCase__ : Optional[Any]=7 , lowerCAmelCase__ : List[Any]=True , lowerCAmelCase__ : str=True , lowerCAmelCase__ : str=True , lowerCAmelCase__ : Any=True , lowerCAmelCase__ : Any=True , lowerCAmelCase__ : Optional[int]=99 , lowerCAmelCase__ : Union[str, Any]=32 , lowerCAmelCase__ : Optional[Any]=5 , lowerCAmelCase__ : List[Any]=4 , lowerCAmelCase__ : Tuple=37 , lowerCAmelCase__ : List[Any]="gelu" , lowerCAmelCase__ : Tuple=0.1 , lowerCAmelCase__ : List[str]=0.1 , lowerCAmelCase__ : Optional[Any]=512 , lowerCAmelCase__ : int=16 , lowerCAmelCase__ : Optional[int]=2 , lowerCAmelCase__ : List[Any]=0.02 , lowerCAmelCase__ : Optional[int]=3 , lowerCAmelCase__ : List[str]=4 , lowerCAmelCase__ : Optional[int]=None , ): SCREAMING_SNAKE_CASE_: List[Any] = parent SCREAMING_SNAKE_CASE_: Union[str, Any] = batch_size SCREAMING_SNAKE_CASE_: Optional[Any] = seq_length SCREAMING_SNAKE_CASE_: str = is_training SCREAMING_SNAKE_CASE_: int = use_token_type_ids SCREAMING_SNAKE_CASE_: Any = use_input_mask SCREAMING_SNAKE_CASE_: List[Any] = use_labels SCREAMING_SNAKE_CASE_: List[str] = use_mc_token_ids SCREAMING_SNAKE_CASE_: Dict = vocab_size SCREAMING_SNAKE_CASE_: Optional[Any] = hidden_size SCREAMING_SNAKE_CASE_: List[str] = num_hidden_layers SCREAMING_SNAKE_CASE_: Any = num_attention_heads SCREAMING_SNAKE_CASE_: Optional[Any] = intermediate_size SCREAMING_SNAKE_CASE_: str = hidden_act SCREAMING_SNAKE_CASE_: int = hidden_dropout_prob SCREAMING_SNAKE_CASE_: Dict = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_: str = max_position_embeddings SCREAMING_SNAKE_CASE_: Tuple = type_vocab_size SCREAMING_SNAKE_CASE_: str = type_sequence_label_size SCREAMING_SNAKE_CASE_: Any = initializer_range SCREAMING_SNAKE_CASE_: Any = num_labels SCREAMING_SNAKE_CASE_: str = num_choices SCREAMING_SNAKE_CASE_: Union[str, Any] = scope SCREAMING_SNAKE_CASE_: int = self.vocab_size - 1 def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Dict = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size) SCREAMING_SNAKE_CASE_: Union[str, Any] = None if self.use_input_mask: SCREAMING_SNAKE_CASE_: int = random_attention_mask([self.batch_size, self.seq_length]) SCREAMING_SNAKE_CASE_: Optional[Any] = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE_: List[str] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size) SCREAMING_SNAKE_CASE_: Optional[int] = None if self.use_mc_token_ids: SCREAMING_SNAKE_CASE_: int = ids_tensor([self.batch_size, self.num_choices] , self.seq_length) SCREAMING_SNAKE_CASE_: List[Any] = None SCREAMING_SNAKE_CASE_: List[str] = None SCREAMING_SNAKE_CASE_: List[Any] = None if self.use_labels: SCREAMING_SNAKE_CASE_: List[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size) SCREAMING_SNAKE_CASE_: str = ids_tensor([self.batch_size, self.seq_length] , self.num_labels) SCREAMING_SNAKE_CASE_: Union[str, Any] = ids_tensor([self.batch_size] , self.num_choices) SCREAMING_SNAKE_CASE_: Union[str, Any] = self.get_config() SCREAMING_SNAKE_CASE_: Dict = ids_tensor([self.num_hidden_layers, self.num_attention_heads] , 2) return ( config, input_ids, input_mask, head_mask, token_type_ids, mc_token_ids, sequence_labels, token_labels, choice_labels, ) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): return CTRLConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , pad_token_id=self.pad_token_id , ) def _SCREAMING_SNAKE_CASE ( self : int , lowerCAmelCase__ : Dict , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Optional[Any] , *lowerCAmelCase__ : Any): SCREAMING_SNAKE_CASE_: Tuple = CTRLModel(config=lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() model(lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , head_mask=lowerCAmelCase__) model(lowerCAmelCase__ , token_type_ids=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = model(lowerCAmelCase__) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) self.parent.assertEqual(len(result.past_key_values) , config.n_layer) def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : str , lowerCAmelCase__ : str , lowerCAmelCase__ : Any , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Optional[Any] , *lowerCAmelCase__ : Optional[int]): SCREAMING_SNAKE_CASE_: Dict = CTRLLMHeadModel(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: Optional[Any] = model(lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , labels=lowerCAmelCase__) self.parent.assertEqual(result.loss.shape , ()) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size)) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Optional[int] = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ): Optional[Any] = config_and_inputs SCREAMING_SNAKE_CASE_: Optional[int] = {"input_ids": input_ids, "token_type_ids": token_type_ids, "head_mask": head_mask} return config, inputs_dict def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Dict , lowerCAmelCase__ : int , lowerCAmelCase__ : Any , *lowerCAmelCase__ : Optional[int]): SCREAMING_SNAKE_CASE_: Any = self.num_labels SCREAMING_SNAKE_CASE_: Optional[int] = CTRLForSequenceClassification(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: Optional[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size) SCREAMING_SNAKE_CASE_: Optional[Any] = model(lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , labels=lowerCAmelCase__) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels)) @require_torch class __lowercase ( UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : Any = (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else () _UpperCAmelCase : Tuple = (CTRLLMHeadModel,) if is_torch_available() else () _UpperCAmelCase : Any = ( { '''feature-extraction''': CTRLModel, '''text-classification''': CTRLForSequenceClassification, '''text-generation''': CTRLLMHeadModel, '''zero-shot''': CTRLForSequenceClassification, } if is_torch_available() else {} ) _UpperCAmelCase : Optional[Any] = True _UpperCAmelCase : Union[str, Any] = False _UpperCAmelCase : Dict = False def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : str , lowerCAmelCase__ : Dict): if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests": # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers. # `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny # config could not be created. return True return False def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Optional[Any] = CTRLModelTester(self) SCREAMING_SNAKE_CASE_: Dict = ConfigTester(self , config_class=lowerCAmelCase__ , n_embd=37) def _SCREAMING_SNAKE_CASE ( self : Tuple): super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() torch.cuda.empty_cache() def _SCREAMING_SNAKE_CASE ( self : Optional[int]): self.config_tester.run_common_tests() def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_ctrl_model(*lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head_model(*lowerCAmelCase__) @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests.") def _SCREAMING_SNAKE_CASE ( self : Tuple): pass @slow def _SCREAMING_SNAKE_CASE ( self : str): for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_: List[str] = CTRLModel.from_pretrained(lowerCAmelCase__) self.assertIsNotNone(lowerCAmelCase__) @unittest.skip("The model doesn't support left padding") # and it's not used enough to be worth fixing :) def _SCREAMING_SNAKE_CASE ( self : int): pass @require_torch class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : List[str]): super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() torch.cuda.empty_cache() @slow def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Any = CTRLLMHeadModel.from_pretrained("ctrl") model.to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = torch.tensor( [[1_1859, 0, 1611, 8]] , dtype=torch.long , device=lowerCAmelCase__) # Legal the president is SCREAMING_SNAKE_CASE_: List[str] = [ 1_1859, 0, 1611, 8, 5, 150, 2_6449, 2, 19, 348, 469, 3, 2595, 48, 2_0740, 24_6533, 24_6533, 19, 30, 5, ] # Legal the president is a good guy and I don't want to lose my job. \n \n I have a SCREAMING_SNAKE_CASE_: Dict = model.generate(lowerCAmelCase__ , do_sample=lowerCAmelCase__) self.assertListEqual(output_ids[0].tolist() , lowerCAmelCase__)
127
import darl # noqa import gym import tqdm from diffusers.experimental import ValueGuidedRLPipeline lowerCAmelCase : Tuple = { """n_samples""": 64, """horizon""": 32, """num_inference_steps""": 20, """n_guide_steps""": 2, # can set to 0 for faster sampling, does not use value network """scale_grad_by_std""": True, """scale""": 0.1, """eta""": 0.0, """t_grad_cutoff""": 2, """device""": """cpu""", } if __name__ == "__main__": lowerCAmelCase : Optional[int] = """hopper-medium-v2""" lowerCAmelCase : Optional[int] = gym.make(env_name) lowerCAmelCase : Optional[int] = ValueGuidedRLPipeline.from_pretrained( """bglick13/hopper-medium-v2-value-function-hor32""", env=env, ) env.seed(0) lowerCAmelCase : Optional[Any] = env.reset() lowerCAmelCase : Union[str, Any] = 0 lowerCAmelCase : Optional[int] = 0 lowerCAmelCase : Optional[int] = 1000 lowerCAmelCase : Dict = [obs.copy()] try: for t in tqdm.tqdm(range(T)): # call the policy lowerCAmelCase : Union[str, Any] = pipeline(obs, planning_horizon=32) # execute action in environment lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : Dict = env.step(denorm_actions) lowerCAmelCase : Tuple = env.get_normalized_score(total_reward) # update return total_reward += reward total_score += score print( f'''Step: {t}, Reward: {reward}, Total Reward: {total_reward}, Score: {score}, Total Score:''' f''' {total_score}''' ) # save observations for rendering rollout.append(next_observation.copy()) lowerCAmelCase : Tuple = next_observation except KeyboardInterrupt: pass print(f'''Total reward: {total_reward}''')
127
1
'''simple docstring''' from __future__ import annotations class UpperCamelCase__ : """simple docstring""" def __init__( self , snake_case ): '''simple docstring''' UpperCAmelCase : str = data UpperCAmelCase : Node | None = None UpperCAmelCase : Node | None = None def lowercase ( __magic_name__ ): # In Order traversal of the tree '''simple docstring''' if tree: display(tree.left ) print(tree.data ) display(tree.right ) def lowercase ( __magic_name__ ): '''simple docstring''' return 1 + max(depth_of_tree(tree.left ) , depth_of_tree(tree.right ) ) if tree else 0 def lowercase ( __magic_name__ ): '''simple docstring''' if not tree: return True if tree.left and tree.right: return is_full_binary_tree(tree.left ) and is_full_binary_tree(tree.right ) else: return not tree.left and not tree.right def lowercase ( ): # Main function for testing. '''simple docstring''' UpperCAmelCase : Any = Node(1 ) UpperCAmelCase : List[str] = Node(2 ) UpperCAmelCase : Union[str, Any] = Node(3 ) UpperCAmelCase : List[str] = Node(4 ) UpperCAmelCase : Any = Node(5 ) UpperCAmelCase : Any = Node(6 ) UpperCAmelCase : Optional[int] = Node(7 ) UpperCAmelCase : Any = Node(8 ) UpperCAmelCase : int = Node(9 ) print(is_full_binary_tree(__magic_name__ ) ) print(depth_of_tree(__magic_name__ ) ) print("Tree is: " ) display(__magic_name__ ) if __name__ == "__main__": main()
311
'''simple docstring''' import unittest from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class UpperCamelCase__ : """simple docstring""" @staticmethod def A_ ( *snake_case , **snake_case ): '''simple docstring''' pass @is_pipeline_test @require_vision @require_torch class UpperCamelCase__ ( unittest.TestCase ): """simple docstring""" SCREAMING_SNAKE_CASE__ : Dict = MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING def A_ ( self , snake_case , snake_case , snake_case ): '''simple docstring''' UpperCAmelCase : str = pipeline( "zero-shot-object-detection" , model="hf-internal-testing/tiny-random-owlvit-object-detection" ) UpperCAmelCase : Union[str, Any] = [ { "image": "./tests/fixtures/tests_samples/COCO/000000039769.png", "candidate_labels": ["cat", "remote", "couch"], } ] return object_detector, examples def A_ ( self , snake_case , snake_case ): '''simple docstring''' UpperCAmelCase : List[Any] = object_detector(examples[0] , threshold=0.0 ) UpperCAmelCase : Dict = len(snake_case ) self.assertGreater(snake_case , 0 ) self.assertEqual( snake_case , [ { "score": ANY(snake_case ), "label": ANY(snake_case ), "box": {"xmin": ANY(snake_case ), "ymin": ANY(snake_case ), "xmax": ANY(snake_case ), "ymax": ANY(snake_case )}, } for i in range(snake_case ) ] , ) @require_tf @unittest.skip("Zero Shot Object Detection not implemented in TF" ) def A_ ( self ): '''simple docstring''' pass @require_torch def A_ ( self ): '''simple docstring''' UpperCAmelCase : Optional[Any] = pipeline( "zero-shot-object-detection" , model="hf-internal-testing/tiny-random-owlvit-object-detection" ) UpperCAmelCase : Optional[Any] = object_detector( "./tests/fixtures/tests_samples/COCO/000000039769.png" , candidate_labels=["cat", "remote", "couch"] , threshold=0.64 , ) self.assertEqual( nested_simplify(snake_case , decimals=4 ) , [ {"score": 0.7235, "label": "cat", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}}, {"score": 0.7218, "label": "remote", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}}, {"score": 0.7184, "label": "couch", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}}, {"score": 0.6748, "label": "remote", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}}, {"score": 0.6656, "label": "cat", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}}, {"score": 0.6614, "label": "couch", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}}, {"score": 0.6456, "label": "remote", "box": {"xmin": 4_9_4, "ymin": 1_0_5, "xmax": 5_2_1, "ymax": 1_2_7}}, {"score": 0.642, "label": "remote", "box": {"xmin": 6_7, "ymin": 2_7_4, "xmax": 9_3, "ymax": 2_9_7}}, {"score": 0.6419, "label": "cat", "box": {"xmin": 4_9_4, "ymin": 1_0_5, "xmax": 5_2_1, "ymax": 1_2_7}}, ] , ) UpperCAmelCase : Tuple = object_detector( [ { "image": "./tests/fixtures/tests_samples/COCO/000000039769.png", "candidate_labels": ["cat", "remote", "couch"], } ] , threshold=0.64 , ) self.assertEqual( nested_simplify(snake_case , decimals=4 ) , [ [ {"score": 0.7235, "label": "cat", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}}, {"score": 0.7218, "label": "remote", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}}, {"score": 0.7184, "label": "couch", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}}, {"score": 0.6748, "label": "remote", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}}, {"score": 0.6656, "label": "cat", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}}, {"score": 0.6614, "label": "couch", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}}, {"score": 0.6456, "label": "remote", "box": {"xmin": 4_9_4, "ymin": 1_0_5, "xmax": 5_2_1, "ymax": 1_2_7}}, {"score": 0.642, "label": "remote", "box": {"xmin": 6_7, "ymin": 2_7_4, "xmax": 9_3, "ymax": 2_9_7}}, {"score": 0.6419, "label": "cat", "box": {"xmin": 4_9_4, "ymin": 1_0_5, "xmax": 5_2_1, "ymax": 1_2_7}}, ] ] , ) @require_torch @slow def A_ ( self ): '''simple docstring''' UpperCAmelCase : Tuple = pipeline("zero-shot-object-detection" ) UpperCAmelCase : Optional[int] = object_detector( "http://images.cocodataset.org/val2017/000000039769.jpg" , candidate_labels=["cat", "remote", "couch"] , ) self.assertEqual( nested_simplify(snake_case , decimals=4 ) , [ {"score": 0.2868, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}}, {"score": 0.277, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}}, {"score": 0.2537, "label": "cat", "box": {"xmin": 1, "ymin": 5_5, "xmax": 3_1_5, "ymax": 4_7_2}}, {"score": 0.1474, "label": "remote", "box": {"xmin": 3_3_5, "ymin": 7_4, "xmax": 3_7_1, "ymax": 1_8_7}}, {"score": 0.1208, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_4_2, "ymax": 4_7_6}}, ] , ) UpperCAmelCase : Union[str, Any] = object_detector( [ { "image": "http://images.cocodataset.org/val2017/000000039769.jpg", "candidate_labels": ["cat", "remote", "couch"], }, { "image": "http://images.cocodataset.org/val2017/000000039769.jpg", "candidate_labels": ["cat", "remote", "couch"], }, ] , ) self.assertEqual( nested_simplify(snake_case , decimals=4 ) , [ [ {"score": 0.2868, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}}, {"score": 0.277, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}}, {"score": 0.2537, "label": "cat", "box": {"xmin": 1, "ymin": 5_5, "xmax": 3_1_5, "ymax": 4_7_2}}, {"score": 0.1474, "label": "remote", "box": {"xmin": 3_3_5, "ymin": 7_4, "xmax": 3_7_1, "ymax": 1_8_7}}, {"score": 0.1208, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_4_2, "ymax": 4_7_6}}, ], [ {"score": 0.2868, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}}, {"score": 0.277, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}}, {"score": 0.2537, "label": "cat", "box": {"xmin": 1, "ymin": 5_5, "xmax": 3_1_5, "ymax": 4_7_2}}, {"score": 0.1474, "label": "remote", "box": {"xmin": 3_3_5, "ymin": 7_4, "xmax": 3_7_1, "ymax": 1_8_7}}, {"score": 0.1208, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_4_2, "ymax": 4_7_6}}, ], ] , ) @require_tf @unittest.skip("Zero Shot Object Detection not implemented in TF" ) def A_ ( self ): '''simple docstring''' pass @require_torch @slow def A_ ( self ): '''simple docstring''' UpperCAmelCase : Any = 0.2 UpperCAmelCase : Union[str, Any] = pipeline("zero-shot-object-detection" ) UpperCAmelCase : str = object_detector( "http://images.cocodataset.org/val2017/000000039769.jpg" , candidate_labels=["cat", "remote", "couch"] , threshold=snake_case , ) self.assertEqual( nested_simplify(snake_case , decimals=4 ) , [ {"score": 0.2868, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}}, {"score": 0.277, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}}, {"score": 0.2537, "label": "cat", "box": {"xmin": 1, "ymin": 5_5, "xmax": 3_1_5, "ymax": 4_7_2}}, ] , ) @require_torch @slow def A_ ( self ): '''simple docstring''' UpperCAmelCase : Dict = 2 UpperCAmelCase : Optional[Any] = pipeline("zero-shot-object-detection" ) UpperCAmelCase : List[str] = object_detector( "http://images.cocodataset.org/val2017/000000039769.jpg" , candidate_labels=["cat", "remote", "couch"] , top_k=snake_case , ) self.assertEqual( nested_simplify(snake_case , decimals=4 ) , [ {"score": 0.2868, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}}, {"score": 0.277, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}}, ] , )
311
1
"""simple docstring""" from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available a_ = { """configuration_trajectory_transformer""": [ """TRAJECTORY_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """TrajectoryTransformerConfig""", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = [ """TRAJECTORY_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""", """TrajectoryTransformerModel""", """TrajectoryTransformerPreTrainedModel""", """load_tf_weights_in_trajectory_transformer""", ] if TYPE_CHECKING: from .configuration_trajectory_transformer import ( TRAJECTORY_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TrajectoryTransformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_trajectory_transformer import ( TRAJECTORY_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TrajectoryTransformerModel, TrajectoryTransformerPreTrainedModel, load_tf_weights_in_trajectory_transformer, ) else: import sys a_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
291
"""simple docstring""" from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available from ...utils import OptionalDependencyNotAvailable a_ = {"""configuration_gpt_neox""": ["""GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GPTNeoXConfig"""]} try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = ["""GPTNeoXTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = [ """GPT_NEOX_PRETRAINED_MODEL_ARCHIVE_LIST""", """GPTNeoXForCausalLM""", """GPTNeoXForQuestionAnswering""", """GPTNeoXForSequenceClassification""", """GPTNeoXForTokenClassification""", """GPTNeoXLayer""", """GPTNeoXModel""", """GPTNeoXPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_gpt_neox import GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoXConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_gpt_neox_fast import GPTNeoXTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_neox import ( GPT_NEOX_PRETRAINED_MODEL_ARCHIVE_LIST, GPTNeoXForCausalLM, GPTNeoXForQuestionAnswering, GPTNeoXForSequenceClassification, GPTNeoXForTokenClassification, GPTNeoXLayer, GPTNeoXModel, GPTNeoXPreTrainedModel, ) else: import sys a_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
291
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) if is_sentencepiece_available(): from ..ta.tokenization_ta import TaTokenizer else: from ...utils.dummy_sentencepiece_objects import TaTokenizer __lowerCamelCase = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast __lowerCamelCase = TaTokenizerFast __lowerCamelCase = {'''configuration_mt5''': ['''MT5Config''', '''MT5OnnxConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = [ '''MT5EncoderModel''', '''MT5ForConditionalGeneration''', '''MT5ForQuestionAnswering''', '''MT5Model''', '''MT5PreTrainedModel''', '''MT5Stack''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = ['''TFMT5EncoderModel''', '''TFMT5ForConditionalGeneration''', '''TFMT5Model'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = ['''FlaxMT5EncoderModel''', '''FlaxMT5ForConditionalGeneration''', '''FlaxMT5Model'''] if TYPE_CHECKING: from .configuration_mta import MTaConfig, MTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mta import ( MTaEncoderModel, MTaForConditionalGeneration, MTaForQuestionAnswering, MTaModel, MTaPreTrainedModel, MTaStack, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel else: import sys __lowerCamelCase = _LazyModule( __name__, globals()['''__file__'''], _import_structure, extra_objects={'''MT5Tokenizer''': MTaTokenizer, '''MT5TokenizerFast''': MTaTokenizerFast}, module_spec=__spec__, )
162
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST, OpenAIGPTConfig, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification, OpenAIGPTLMHeadModel, OpenAIGPTModel, ) class A__ : def __init__( self , UpperCamelCase__ , UpperCamelCase__=13 , UpperCamelCase__=7 , UpperCamelCase__=True , UpperCamelCase__=True , UpperCamelCase__=True , UpperCamelCase__=99 , UpperCamelCase__=32 , UpperCamelCase__=5 , UpperCamelCase__=4 , UpperCamelCase__=37 , UpperCamelCase__="gelu" , UpperCamelCase__=0.1 , UpperCamelCase__=0.1 , UpperCamelCase__=512 , UpperCamelCase__=16 , UpperCamelCase__=2 , UpperCamelCase__=0.02 , UpperCamelCase__=3 , UpperCamelCase__=4 , UpperCamelCase__=None , ) -> Union[str, Any]: '''simple docstring''' A_ = parent A_ = batch_size A_ = seq_length A_ = is_training 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_ = self.vocab_size - 1 def snake_case_ ( self ) -> Dict: '''simple docstring''' A_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A_ = None if self.use_token_type_ids: A_ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A_ = None A_ = None A_ = None if self.use_labels: A_ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A_ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) A_ = ids_tensor([self.batch_size] , self.num_choices ) A_ = OpenAIGPTConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , pad_token_id=self.pad_token_id , ) A_ = ids_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, head_mask, token_type_ids, sequence_labels, token_labels, choice_labels, ) def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , *UpperCamelCase__ ) -> Any: '''simple docstring''' A_ = OpenAIGPTModel(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A_ = model(UpperCamelCase__ , token_type_ids=UpperCamelCase__ , head_mask=UpperCamelCase__ ) A_ = model(UpperCamelCase__ , token_type_ids=UpperCamelCase__ ) A_ = model(UpperCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , *UpperCamelCase__ ) -> List[Any]: '''simple docstring''' A_ = OpenAIGPTLMHeadModel(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A_ = model(UpperCamelCase__ , token_type_ids=UpperCamelCase__ , labels=UpperCamelCase__ ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , *UpperCamelCase__ ) -> int: '''simple docstring''' A_ = OpenAIGPTDoubleHeadsModel(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A_ = model(UpperCamelCase__ , token_type_ids=UpperCamelCase__ , labels=UpperCamelCase__ ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , *UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' A_ = self.num_labels A_ = OpenAIGPTForSequenceClassification(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A_ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A_ = model(UpperCamelCase__ , token_type_ids=UpperCamelCase__ , labels=UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def snake_case_ ( self ) -> Optional[Any]: '''simple docstring''' A_ = self.prepare_config_and_inputs() ( ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ) = config_and_inputs A_ = { """input_ids""": input_ids, """token_type_ids""": token_type_ids, """head_mask""": head_mask, } return config, inputs_dict @require_torch class A__ ( _snake_case , _snake_case , _snake_case , unittest.TestCase ): lowercase = ( (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification) if is_torch_available() else () ) lowercase = ( (OpenAIGPTLMHeadModel,) if is_torch_available() else () ) # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly lowercase = ( { "feature-extraction": OpenAIGPTModel, "text-classification": OpenAIGPTForSequenceClassification, "text-generation": OpenAIGPTLMHeadModel, "zero-shot": OpenAIGPTForSequenceClassification, } if is_torch_available() else {} ) def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> Optional[Any]: '''simple docstring''' if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests": # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers. # `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a # tiny config could not be created. return True return False def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=False ) -> Union[str, Any]: '''simple docstring''' A_ = super()._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ , return_labels=UpperCamelCase__ ) if return_labels: if model_class.__name__ == "OpenAIGPTDoubleHeadsModel": A_ = torch.zeros( (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length) , dtype=torch.long , device=UpperCamelCase__ , ) A_ = inputs_dict["""labels"""] A_ = inputs_dict["""labels"""] A_ = torch.zeros( (self.model_tester.batch_size, self.model_tester.num_choices) , dtype=torch.long , device=UpperCamelCase__ , ) A_ = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=UpperCamelCase__ ) return inputs_dict def snake_case_ ( self ) -> Union[str, Any]: '''simple docstring''' A_ = OpenAIGPTModelTester(self ) A_ = ConfigTester(self , config_class=UpperCamelCase__ , n_embd=37 ) def snake_case_ ( self ) -> List[str]: '''simple docstring''' self.config_tester.run_common_tests() def snake_case_ ( self ) -> Dict: '''simple docstring''' A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_openai_gpt_model(*UpperCamelCase__ ) def snake_case_ ( self ) -> Optional[int]: '''simple docstring''' A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head_model(*UpperCamelCase__ ) def snake_case_ ( self ) -> Optional[Any]: '''simple docstring''' A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_double_lm_head_model(*UpperCamelCase__ ) def snake_case_ ( self ) -> List[str]: '''simple docstring''' A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCamelCase__ ) @slow def snake_case_ ( self ) -> List[Any]: '''simple docstring''' for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ = OpenAIGPTModel.from_pretrained(UpperCamelCase__ ) self.assertIsNotNone(UpperCamelCase__ ) @require_torch class A__ ( unittest.TestCase ): @slow def snake_case_ ( self ) -> Tuple: '''simple docstring''' A_ = OpenAIGPTLMHeadModel.from_pretrained("""openai-gpt""" ) model.to(UpperCamelCase__ ) A_ = torch.tensor([[481, 4735, 544]] , dtype=torch.long , device=UpperCamelCase__ ) # the president is A_ = [ 481, 4735, 544, 246, 963, 870, 762, 239, 244, 40477, 244, 249, 719, 881, 487, 544, 240, 244, 603, 481, ] # the president is a very good man. " \n " i\'m sure he is, " said the A_ = model.generate(UpperCamelCase__ , do_sample=UpperCamelCase__ ) self.assertListEqual(output_ids[0].tolist() , UpperCamelCase__ )
162
1
from __future__ import annotations import unittest from transformers import RoFormerConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForMultipleChoice, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerModel, ) from transformers.models.roformer.modeling_tf_roformer import ( TFRoFormerSelfAttention, TFRoFormerSinusoidalPositionalEmbedding, ) class A__ : """simple docstring""" def __init__( self , __snake_case , __snake_case=1_3 , __snake_case=7 , __snake_case=True , __snake_case=True , __snake_case=True , __snake_case=True , __snake_case=9_9 , __snake_case=3_2 , __snake_case=2 , __snake_case=4 , __snake_case=3_7 , __snake_case="gelu" , __snake_case=0.1 , __snake_case=0.1 , __snake_case=5_1_2 , __snake_case=1_6 , __snake_case=2 , __snake_case=0.02 , __snake_case=3 , __snake_case=4 , __snake_case=None , ): snake_case = parent snake_case = 1_3 snake_case = 7 snake_case = True snake_case = True snake_case = True snake_case = True snake_case = 9_9 snake_case = 3_2 snake_case = 2 snake_case = 4 snake_case = 3_7 snake_case = '''gelu''' snake_case = 0.1 snake_case = 0.1 snake_case = 5_1_2 snake_case = 1_6 snake_case = 2 snake_case = 0.02 snake_case = 3 snake_case = 4 snake_case = None def a_ ( self ): snake_case = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) snake_case = None if self.use_input_mask: snake_case = random_attention_mask([self.batch_size, self.seq_length] ) snake_case = None if self.use_token_type_ids: snake_case = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) snake_case = None snake_case = None snake_case = None if self.use_labels: snake_case = ids_tensor([self.batch_size] , self.type_sequence_label_size ) snake_case = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) snake_case = ids_tensor([self.batch_size] , self.num_choices ) snake_case = RoFormerConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , return_dict=__snake_case , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def a_ ( self , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ): snake_case = TFRoFormerModel(config=__snake_case ) snake_case = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids} snake_case = [input_ids, input_mask] snake_case = model(__snake_case ) snake_case = model(__snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a_ ( self , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ): snake_case = True snake_case = TFRoFormerForCausalLM(config=__snake_case ) snake_case = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } snake_case = model(__snake_case )['''logits'''] self.parent.assertListEqual( list(prediction_scores.numpy().shape ) , [self.batch_size, self.seq_length, self.vocab_size] ) def a_ ( self , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ): snake_case = TFRoFormerForMaskedLM(config=__snake_case ) snake_case = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } snake_case = model(__snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def a_ ( self , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ): snake_case = self.num_labels snake_case = TFRoFormerForSequenceClassification(config=__snake_case ) snake_case = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } snake_case = model(__snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def a_ ( self , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ): snake_case = self.num_choices snake_case = TFRoFormerForMultipleChoice(config=__snake_case ) snake_case = tf.tile(tf.expand_dims(__snake_case , 1 ) , (1, self.num_choices, 1) ) snake_case = tf.tile(tf.expand_dims(__snake_case , 1 ) , (1, self.num_choices, 1) ) snake_case = tf.tile(tf.expand_dims(__snake_case , 1 ) , (1, self.num_choices, 1) ) snake_case = { '''input_ids''': multiple_choice_inputs_ids, '''attention_mask''': multiple_choice_input_mask, '''token_type_ids''': multiple_choice_token_type_ids, } snake_case = model(__snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def a_ ( self , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ): snake_case = self.num_labels snake_case = TFRoFormerForTokenClassification(config=__snake_case ) snake_case = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } snake_case = model(__snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def a_ ( self , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ): snake_case = TFRoFormerForQuestionAnswering(config=__snake_case ) snake_case = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } snake_case = model(__snake_case ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def a_ ( self ): snake_case = self.prepare_config_and_inputs() ( ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ) = config_and_inputs snake_case = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class A__ ( snake_case__ , snake_case__ , unittest.TestCase ): """simple docstring""" __magic_name__ = ( ( TFRoFormerModel, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerForMultipleChoice, ) if is_tf_available() else () ) __magic_name__ = ( { 'feature-extraction': TFRoFormerModel, 'fill-mask': TFRoFormerForMaskedLM, 'question-answering': TFRoFormerForQuestionAnswering, 'text-classification': TFRoFormerForSequenceClassification, 'text-generation': TFRoFormerForCausalLM, 'token-classification': TFRoFormerForTokenClassification, 'zero-shot': TFRoFormerForSequenceClassification, } if is_tf_available() else {} ) __magic_name__ = False __magic_name__ = False def a_ ( self , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ): if pipeline_test_casse_name == "TextGenerationPipelineTests": return True return False def a_ ( self ): snake_case = TFRoFormerModelTester(self ) snake_case = ConfigTester(self , config_class=__snake_case , hidden_size=3_7 ) def a_ ( self ): self.config_tester.run_common_tests() def a_ ( self ): snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__snake_case ) def a_ ( self ): snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*__snake_case ) def a_ ( self ): snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head(*__snake_case ) def a_ ( self ): snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*__snake_case ) def a_ ( self ): snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*__snake_case ) def a_ ( self ): snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*__snake_case ) def a_ ( self ): snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*__snake_case ) @slow def a_ ( self ): snake_case = TFRoFormerModel.from_pretrained('''junnyu/roformer_chinese_base''' ) self.assertIsNotNone(__snake_case ) @require_tf class A__ ( unittest.TestCase ): """simple docstring""" @slow def a_ ( self ): snake_case = TFRoFormerForMaskedLM.from_pretrained('''junnyu/roformer_chinese_base''' ) snake_case = tf.constant([[0, 1, 2, 3, 4, 5]] ) snake_case = model(__snake_case )[0] # TODO Replace vocab size snake_case = 5_0_0_0_0 snake_case = [1, 6, vocab_size] self.assertEqual(output.shape , __snake_case ) print(output[:, :3, :3] ) # TODO Replace values below with what was printed above. snake_case = tf.constant( [ [ [-0.1205_3341, -1.026_4901, 0.2922_1946], [-1.513_3783, 0.19_7433, 0.1519_0607], [-5.013_5403, -3.90_0256, -0.8403_8764], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , __snake_case , atol=1E-4 ) @require_tf class A__ ( unittest.TestCase ): """simple docstring""" __magic_name__ = 1e-4 def a_ ( self ): snake_case = tf.constant([[4, 1_0]] ) snake_case = TFRoFormerSinusoidalPositionalEmbedding(num_positions=6 , embedding_dim=6 ) snake_case = emba(input_ids.shape ) snake_case = tf.constant( [[0.0000, 0.0000, 0.0000, 1.0000, 1.0000, 1.0000], [0.8415, 0.0464, 0.0022, 0.5403, 0.9989, 1.0000]] ) tf.debugging.assert_near(__snake_case , __snake_case , atol=self.tolerance ) def a_ ( self ): snake_case = tf.constant( [ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000], [0.8415, 0.8219, 0.8020, 0.7819, 0.7617], [0.9093, 0.9364, 0.9581, 0.9749, 0.9870], ] ) snake_case = TFRoFormerSinusoidalPositionalEmbedding(num_positions=5_1_2 , embedding_dim=5_1_2 ) emba([2, 1_6, 5_1_2] ) snake_case = emba.weight[:3, :5] tf.debugging.assert_near(__snake_case , __snake_case , atol=self.tolerance ) @require_tf class A__ ( unittest.TestCase ): """simple docstring""" __magic_name__ = 1e-4 def a_ ( self ): # 2,12,16,64 snake_case = tf.reshape(tf.range(2 * 1_2 * 1_6 * 6_4 , dtype=tf.floataa ) , shape=(2, 1_2, 1_6, 6_4) ) / 1_0_0 snake_case = -tf.reshape(tf.range(2 * 1_2 * 1_6 * 6_4 , dtype=tf.floataa ) , shape=(2, 1_2, 1_6, 6_4) ) / 1_0_0 snake_case = TFRoFormerSinusoidalPositionalEmbedding(num_positions=3_2 , embedding_dim=6_4 ) snake_case = embed_positions([2, 1_6, 7_6_8] )[None, None, :, :] snake_case , snake_case = TFRoFormerSelfAttention.apply_rotary_position_embeddings( __snake_case , __snake_case , __snake_case ) snake_case = tf.constant( [ [0.0000, 0.0100, 0.0200, 0.0300, 0.0400, 0.0500, 0.0600, 0.0700], [-0.2012, 0.8897, 0.0263, 0.9401, 0.2074, 0.9463, 0.3481, 0.9343], [-1.7057, 0.6271, -1.2145, 1.3897, -0.6303, 1.7647, -0.1173, 1.8985], [-2.1731, -1.6397, -2.7358, 0.2854, -2.1840, 1.7183, -1.3018, 2.4871], [0.2717, -3.6173, -2.9206, -2.1988, -3.6638, 0.3858, -2.9155, 2.2980], [3.9859, -2.1580, -0.7984, -4.4904, -4.1181, -2.0252, -4.4782, 1.1253], ] ) snake_case = tf.constant( [ [0.0000, -0.0100, -0.0200, -0.0300, -0.0400, -0.0500, -0.0600, -0.0700], [0.2012, -0.8897, -0.0263, -0.9401, -0.2074, -0.9463, -0.3481, -0.9343], [1.7057, -0.6271, 1.2145, -1.3897, 0.6303, -1.7647, 0.1173, -1.8985], [2.1731, 1.6397, 2.7358, -0.2854, 2.1840, -1.7183, 1.3018, -2.4871], [-0.2717, 3.6173, 2.9206, 2.1988, 3.6638, -0.3858, 2.9155, -2.2980], [-3.9859, 2.1580, 0.7984, 4.4904, 4.1181, 2.0252, 4.4782, -1.1253], ] ) tf.debugging.assert_near(query_layer[0, 0, :6, :8] , __snake_case , atol=self.tolerance ) tf.debugging.assert_near(key_layer[0, 0, :6, :8] , __snake_case , atol=self.tolerance )
350
import argparse import copy def UpperCAmelCase__ (UpperCamelCase_ ): """simple docstring""" snake_case = {} with open(UpperCamelCase_ ) as f: for line in f: if line.split()[0] not in dict_of_neighbours: snake_case = [] _list.append([line.split()[1], line.split()[2]] ) snake_case = _list else: dict_of_neighbours[line.split()[0]].append( [line.split()[1], line.split()[2]] ) if line.split()[1] not in dict_of_neighbours: snake_case = [] _list.append([line.split()[0], line.split()[2]] ) snake_case = _list else: dict_of_neighbours[line.split()[1]].append( [line.split()[0], line.split()[2]] ) return dict_of_neighbours def UpperCAmelCase__ (UpperCamelCase_ ,UpperCamelCase_ ): """simple docstring""" with open(UpperCamelCase_ ) as f: snake_case = f.read(1 ) snake_case = start_node snake_case = [] snake_case = start_node snake_case = 0 while visiting not in first_solution: snake_case = 1_00_00 for k in dict_of_neighbours[visiting]: if int(k[1] ) < int(UpperCamelCase_ ) and k[0] not in first_solution: snake_case = k[1] snake_case = k[0] first_solution.append(UpperCamelCase_ ) snake_case = distance_of_first_solution + int(UpperCamelCase_ ) snake_case = best_node first_solution.append(UpperCamelCase_ ) snake_case = 0 for k in dict_of_neighbours[first_solution[-2]]: if k[0] == start_node: break position += 1 snake_case = ( distance_of_first_solution + int(dict_of_neighbours[first_solution[-2]][position][1] ) - 1_00_00 ) return first_solution, distance_of_first_solution def UpperCAmelCase__ (UpperCamelCase_ ,UpperCamelCase_ ): """simple docstring""" snake_case = [] for n in solution[1:-1]: snake_case = solution.index(UpperCamelCase_ ) for kn in solution[1:-1]: snake_case = solution.index(UpperCamelCase_ ) if n == kn: continue snake_case = copy.deepcopy(UpperCamelCase_ ) snake_case = kn snake_case = n snake_case = 0 for k in _tmp[:-1]: snake_case = _tmp[_tmp.index(UpperCamelCase_ ) + 1] for i in dict_of_neighbours[k]: if i[0] == next_node: snake_case = distance + int(i[1] ) _tmp.append(UpperCamelCase_ ) if _tmp not in neighborhood_of_solution: neighborhood_of_solution.append(_tmp ) snake_case = len(neighborhood_of_solution[0] ) - 1 neighborhood_of_solution.sort(key=lambda UpperCamelCase_ : x[index_of_last_item_in_the_list] ) return neighborhood_of_solution def UpperCAmelCase__ (UpperCamelCase_ ,UpperCamelCase_ ,UpperCamelCase_ ,UpperCamelCase_ ,UpperCamelCase_ ): """simple docstring""" snake_case = 1 snake_case = first_solution snake_case = [] snake_case = distance_of_first_solution snake_case = solution while count <= iters: snake_case = find_neighborhood(UpperCamelCase_ ,UpperCamelCase_ ) snake_case = 0 snake_case = neighborhood[index_of_best_solution] snake_case = len(UpperCamelCase_ ) - 1 snake_case = False while not found: snake_case = 0 while i < len(UpperCamelCase_ ): if best_solution[i] != solution[i]: snake_case = best_solution[i] snake_case = solution[i] break snake_case = i + 1 if [first_exchange_node, second_exchange_node] not in tabu_list and [ second_exchange_node, first_exchange_node, ] not in tabu_list: tabu_list.append([first_exchange_node, second_exchange_node] ) snake_case = True snake_case = best_solution[:-1] snake_case = neighborhood[index_of_best_solution][best_cost_index] if cost < best_cost: snake_case = cost snake_case = solution else: snake_case = index_of_best_solution + 1 snake_case = neighborhood[index_of_best_solution] if len(UpperCamelCase_ ) >= size: tabu_list.pop(0 ) snake_case = count + 1 return best_solution_ever, best_cost def UpperCAmelCase__ (UpperCamelCase_=None ): """simple docstring""" snake_case = generate_neighbours(args.File ) snake_case , snake_case = generate_first_solution( args.File ,UpperCamelCase_ ) snake_case , snake_case = tabu_search( UpperCamelCase_ ,UpperCamelCase_ ,UpperCamelCase_ ,args.Iterations ,args.Size ,) print(F'''Best solution: {best_sol}, with total distance: {best_cost}.''' ) if __name__ == "__main__": _SCREAMING_SNAKE_CASE : Optional[Any] = argparse.ArgumentParser(description="Tabu Search") parser.add_argument( "-f", "--File", type=str, help="Path to the file containing the data", required=True, ) parser.add_argument( "-i", "--Iterations", type=int, help="How many iterations the algorithm should perform", required=True, ) parser.add_argument( "-s", "--Size", type=int, help="Size of the tabu list", required=True ) # Pass the arguments to main method main(parser.parse_args())
213
0
"""simple docstring""" from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType _lowercase : List[str] = logging.get_logger(__name__) _lowercase : int = { "microsoft/deberta-v2-xlarge": "https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json", "microsoft/deberta-v2-xxlarge": "https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json", "microsoft/deberta-v2-xlarge-mnli": ( "https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json" ), "microsoft/deberta-v2-xxlarge-mnli": ( "https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json" ), } class __SCREAMING_SNAKE_CASE ( lowerCAmelCase_ ): '''simple docstring''' _a = 'deberta-v2' def __init__( self : Optional[Any], lowerCamelCase : Optional[int]=12_8100, lowerCamelCase : List[Any]=1536, lowerCamelCase : Dict=24, lowerCamelCase : Any=24, lowerCamelCase : Union[str, Any]=6144, lowerCamelCase : List[Any]="gelu", lowerCamelCase : int=0.1, lowerCamelCase : Union[str, Any]=0.1, lowerCamelCase : Union[str, Any]=512, lowerCamelCase : Optional[Any]=0, lowerCamelCase : Any=0.02, lowerCamelCase : int=1E-7, lowerCamelCase : Union[str, Any]=False, lowerCamelCase : Union[str, Any]=-1, lowerCamelCase : Tuple=0, lowerCamelCase : Union[str, Any]=True, lowerCamelCase : int=None, lowerCamelCase : Dict=0, lowerCamelCase : Tuple="gelu", **lowerCamelCase : Optional[int], )-> Union[str, Any]: super().__init__(**lowerCamelCase ) lowerCamelCase__ : str =hidden_size lowerCamelCase__ : Optional[int] =num_hidden_layers lowerCamelCase__ : Optional[Any] =num_attention_heads lowerCamelCase__ : List[Any] =intermediate_size lowerCamelCase__ : int =hidden_act lowerCamelCase__ : Tuple =hidden_dropout_prob lowerCamelCase__ : Union[str, Any] =attention_probs_dropout_prob lowerCamelCase__ : Optional[Any] =max_position_embeddings lowerCamelCase__ : int =type_vocab_size lowerCamelCase__ : Tuple =initializer_range lowerCamelCase__ : Tuple =relative_attention lowerCamelCase__ : Optional[Any] =max_relative_positions lowerCamelCase__ : List[Any] =pad_token_id lowerCamelCase__ : int =position_biased_input # Backwards compatibility if type(lowerCamelCase ) == str: lowerCamelCase__ : Union[str, Any] =[x.strip() for x in pos_att_type.lower().split('''|''' )] lowerCamelCase__ : Tuple =pos_att_type lowerCamelCase__ : Union[str, Any] =vocab_size lowerCamelCase__ : Optional[int] =layer_norm_eps lowerCamelCase__ : Dict =kwargs.get('''pooler_hidden_size''', lowerCamelCase ) lowerCamelCase__ : Tuple =pooler_dropout lowerCamelCase__ : List[Any] =pooler_hidden_act class __SCREAMING_SNAKE_CASE ( lowerCAmelCase_ ): '''simple docstring''' @property def snake_case ( self : List[str] )-> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": lowerCamelCase__ : Union[str, Any] ={0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: lowerCamelCase__ : Any ={0: '''batch''', 1: '''sequence'''} if self._config.type_vocab_size > 0: return OrderedDict( [('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis)] ) else: return OrderedDict([('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis)] ) @property def snake_case ( self : List[str] )-> int: return 12 def snake_case ( self : str, lowerCamelCase : Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"], lowerCamelCase : int = -1, lowerCamelCase : int = -1, lowerCamelCase : int = -1, lowerCamelCase : bool = False, lowerCamelCase : Optional["TensorType"] = None, lowerCamelCase : int = 3, lowerCamelCase : int = 40, lowerCamelCase : int = 40, lowerCamelCase : "PreTrainedTokenizerBase" = None, )-> Mapping[str, Any]: lowerCamelCase__ : List[Any] =super().generate_dummy_inputs(preprocessor=lowerCamelCase, framework=lowerCamelCase ) if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs: del dummy_inputs["token_type_ids"] return dummy_inputs
238
"""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 _lowercase : Union[str, Any] = ["text", "image", "audio"] def snake_case__ ( __lowerCamelCase : List[str] ): """simple docstring""" lowerCamelCase__ : Union[str, Any] =[] for input_type in input_types: if input_type == "text": inputs.append('''Text input''' ) elif input_type == "image": inputs.append( Image.open(Path(get_tests_dir('''fixtures/tests_samples/COCO''' ) ) / '''000000039769.png''' ).resize((512, 512) ) ) elif input_type == "audio": inputs.append(torch.ones(3000 ) ) elif isinstance(__lowerCamelCase , __lowerCamelCase ): inputs.append(create_inputs(__lowerCamelCase ) ) else: raise ValueError(f'''Invalid type requested: {input_type}''' ) return inputs def snake_case__ ( __lowerCamelCase : List ): """simple docstring""" lowerCamelCase__ : Tuple =[] for output in outputs: if isinstance(__lowerCamelCase , (str, AgentText) ): output_types.append('''text''' ) elif isinstance(__lowerCamelCase , (Image.Image, AgentImage) ): output_types.append('''image''' ) elif isinstance(__lowerCamelCase , (torch.Tensor, AgentAudio) ): output_types.append('''audio''' ) else: raise ValueError(f'''Invalid output: {output}''' ) return output_types @is_tool_test class __SCREAMING_SNAKE_CASE : '''simple docstring''' def snake_case ( self : Any )-> Optional[Any]: self.assertTrue(hasattr(self.tool, '''inputs''' ) ) self.assertTrue(hasattr(self.tool, '''outputs''' ) ) lowerCamelCase__ : Tuple =self.tool.inputs for _input in inputs: if isinstance(_input, lowerCamelCase ): for __input in _input: self.assertTrue(__input in authorized_types ) else: self.assertTrue(_input in authorized_types ) lowerCamelCase__ : Optional[Any] =self.tool.outputs for _output in outputs: self.assertTrue(_output in authorized_types ) def snake_case ( self : Optional[int] )-> Union[str, Any]: lowerCamelCase__ : Optional[int] =create_inputs(self.tool.inputs ) lowerCamelCase__ : List[Any] =self.tool(*lowerCamelCase ) # There is a single output if len(self.tool.outputs ) == 1: lowerCamelCase__ : Optional[int] =[outputs] self.assertListEqual(output_types(lowerCamelCase ), self.tool.outputs ) def snake_case ( self : Union[str, Any] )-> List[str]: self.assertTrue(hasattr(self.tool, '''description''' ) ) self.assertTrue(hasattr(self.tool, '''default_checkpoint''' ) ) self.assertTrue(self.tool.description.startswith('''This is a tool that''' ) ) def snake_case ( self : Union[str, Any] )-> str: lowerCamelCase__ : List[str] =create_inputs(self.tool.inputs ) lowerCamelCase__ : Optional[Any] =self.tool(*lowerCamelCase ) if not isinstance(lowerCamelCase, lowerCamelCase ): lowerCamelCase__ : Any =[outputs] self.assertEqual(len(lowerCamelCase ), len(self.tool.outputs ) ) for output, output_type in zip(lowerCamelCase, self.tool.outputs ): lowerCamelCase__ : List[Any] =AGENT_TYPE_MAPPING[output_type] self.assertTrue(isinstance(lowerCamelCase, lowerCamelCase ) ) def snake_case ( self : Optional[Any] )-> List[Any]: lowerCamelCase__ : Optional[Any] =create_inputs(self.tool.inputs ) lowerCamelCase__ : List[str] =[] for _input, input_type in zip(lowerCamelCase, self.tool.inputs ): if isinstance(lowerCamelCase, lowerCamelCase ): _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 lowerCamelCase__ : Any =self.tool(*lowerCamelCase ) if not isinstance(lowerCamelCase, lowerCamelCase ): lowerCamelCase__ : Optional[int] =[outputs] self.assertEqual(len(lowerCamelCase ), len(self.tool.outputs ) )
238
1
"""simple docstring""" def __lowerCamelCase ( UpperCAmelCase_ : Union[str, Any] ): """simple docstring""" if isinstance(__snake_case , __snake_case ): raise TypeError('''\'float\' object cannot be interpreted as an integer''' ) if isinstance(__snake_case , __snake_case ): raise TypeError('''\'str\' object cannot be interpreted as an integer''' ) if num == 0: return "0b0" a :Any = False if num < 0: a :str = True a :Union[str, Any] = -num a :str = [] while num > 0: binary.insert(0 , num % 2 ) num >>= 1 if negative: return "-0b" + "".join(str(__snake_case ) for e in binary ) return "0b" + "".join(str(__snake_case ) for e in binary ) if __name__ == "__main__": import doctest doctest.testmod()
362
import gc import random import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, CycleDiffusionPipeline, DDIMScheduler, UNetaDConditionModel from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps from ..pipeline_params import ( IMAGE_TO_IMAGE_IMAGE_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class _snake_case ( _snake_case , _snake_case , unittest.TestCase ): SCREAMING_SNAKE_CASE__ = CycleDiffusionPipeline SCREAMING_SNAKE_CASE__ = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - { 'negative_prompt', 'height', 'width', 'negative_prompt_embeds', } SCREAMING_SNAKE_CASE__ = PipelineTesterMixin.required_optional_params - {'latents'} SCREAMING_SNAKE_CASE__ = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({'source_prompt'} ) SCREAMING_SNAKE_CASE__ = IMAGE_TO_IMAGE_IMAGE_PARAMS SCREAMING_SNAKE_CASE__ = IMAGE_TO_IMAGE_IMAGE_PARAMS def SCREAMING_SNAKE_CASE__ ( self ): torch.manual_seed(0 ) a :Optional[Any] = 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 , ) a :List[str] = DDIMScheduler( beta_start=0.0_0085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , num_train_timesteps=1000 , clip_sample=_lowerCamelCase , set_alpha_to_one=_lowerCamelCase , ) torch.manual_seed(0 ) a :List[str] = 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 ) a :Dict = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) a :str = CLIPTextModel(_lowerCamelCase ) a :List[str] = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) a :Union[str, Any] = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def SCREAMING_SNAKE_CASE__ ( self , _lowerCamelCase , _lowerCamelCase=0 ): a :Tuple = floats_tensor((1, 3, 32, 32) , rng=random.Random(_lowerCamelCase ) ).to(_lowerCamelCase ) a :Tuple = image / 2 + 0.5 if str(_lowerCamelCase ).startswith('''mps''' ): a :List[str] = torch.manual_seed(_lowerCamelCase ) else: a :Any = torch.Generator(device=_lowerCamelCase ).manual_seed(_lowerCamelCase ) a :int = { '''prompt''': '''An astronaut riding an elephant''', '''source_prompt''': '''An astronaut riding a horse''', '''image''': image, '''generator''': generator, '''num_inference_steps''': 2, '''eta''': 0.1, '''strength''': 0.8, '''guidance_scale''': 3, '''source_guidance_scale''': 1, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE__ ( self ): a :Union[str, Any] = '''cpu''' # ensure determinism for the device-dependent torch.Generator a :Optional[Any] = self.get_dummy_components() a :Dict = CycleDiffusionPipeline(**_lowerCamelCase ) a :Optional[Any] = pipe.to(_lowerCamelCase ) pipe.set_progress_bar_config(disable=_lowerCamelCase ) a :List[str] = self.get_dummy_inputs(_lowerCamelCase ) a :Any = pipe(**_lowerCamelCase ) a :List[Any] = output.images a :str = images[0, -3:, -3:, -1] assert images.shape == (1, 32, 32, 3) a :List[Any] = np.array([0.4459, 0.4943, 0.4544, 0.6643, 0.5474, 0.4327, 0.5701, 0.5959, 0.5179] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 @unittest.skipIf(torch_device != '''cuda''' , '''This test requires a GPU''' ) def SCREAMING_SNAKE_CASE__ ( self ): a :Optional[int] = self.get_dummy_components() for name, module in components.items(): if hasattr(_lowerCamelCase , '''half''' ): a :Union[str, Any] = module.half() a :List[Any] = CycleDiffusionPipeline(**_lowerCamelCase ) a :Dict = pipe.to(_lowerCamelCase ) pipe.set_progress_bar_config(disable=_lowerCamelCase ) a :Tuple = self.get_dummy_inputs(_lowerCamelCase ) a :Optional[int] = pipe(**_lowerCamelCase ) a :Optional[Any] = output.images a :List[Any] = images[0, -3:, -3:, -1] assert images.shape == (1, 32, 32, 3) a :str = np.array([0.3506, 0.4543, 0.446, 0.4575, 0.5195, 0.4155, 0.5273, 0.518, 0.4116] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 @skip_mps def SCREAMING_SNAKE_CASE__ ( self ): return super().test_save_load_local() @unittest.skip('''non-deterministic pipeline''' ) def SCREAMING_SNAKE_CASE__ ( self ): return super().test_inference_batch_single_identical() @skip_mps def SCREAMING_SNAKE_CASE__ ( self ): return super().test_dict_tuple_outputs_equivalent() @skip_mps def SCREAMING_SNAKE_CASE__ ( self ): return super().test_save_load_optional_components() @skip_mps def SCREAMING_SNAKE_CASE__ ( self ): return super().test_attention_slicing_forward_pass() @slow @require_torch_gpu class _snake_case ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE__ ( self ): a :str = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/cycle-diffusion/black_colored_car.png''' ) a :Optional[int] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/cycle-diffusion/blue_colored_car_fp16.npy''' ) a :Optional[Any] = init_image.resize((512, 512) ) a :List[str] = '''CompVis/stable-diffusion-v1-4''' a :List[str] = DDIMScheduler.from_pretrained(_lowerCamelCase , subfolder='''scheduler''' ) a :Tuple = CycleDiffusionPipeline.from_pretrained( _lowerCamelCase , scheduler=_lowerCamelCase , safety_checker=_lowerCamelCase , torch_dtype=torch.floataa , revision='''fp16''' ) pipe.to(_lowerCamelCase ) pipe.set_progress_bar_config(disable=_lowerCamelCase ) pipe.enable_attention_slicing() a :Optional[Any] = '''A black colored car''' a :Any = '''A blue colored car''' a :str = torch.manual_seed(0 ) a :List[Any] = pipe( prompt=_lowerCamelCase , source_prompt=_lowerCamelCase , image=_lowerCamelCase , num_inference_steps=100 , eta=0.1 , strength=0.85 , guidance_scale=3 , source_guidance_scale=1 , generator=_lowerCamelCase , output_type='''np''' , ) a :int = output.images # the values aren't exactly equal, but the images look the same visually assert np.abs(image - expected_image ).max() < 5e-1 def SCREAMING_SNAKE_CASE__ ( self ): a :str = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/cycle-diffusion/black_colored_car.png''' ) a :Optional[int] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/cycle-diffusion/blue_colored_car.npy''' ) a :List[str] = init_image.resize((512, 512) ) a :List[str] = '''CompVis/stable-diffusion-v1-4''' a :Any = DDIMScheduler.from_pretrained(_lowerCamelCase , subfolder='''scheduler''' ) a :int = CycleDiffusionPipeline.from_pretrained(_lowerCamelCase , scheduler=_lowerCamelCase , safety_checker=_lowerCamelCase ) pipe.to(_lowerCamelCase ) pipe.set_progress_bar_config(disable=_lowerCamelCase ) pipe.enable_attention_slicing() a :Optional[int] = '''A black colored car''' a :Any = '''A blue colored car''' a :Optional[int] = torch.manual_seed(0 ) a :Union[str, Any] = pipe( prompt=_lowerCamelCase , source_prompt=_lowerCamelCase , image=_lowerCamelCase , num_inference_steps=100 , eta=0.1 , strength=0.85 , guidance_scale=3 , source_guidance_scale=1 , generator=_lowerCamelCase , output_type='''np''' , ) a :Optional[int] = output.images assert np.abs(image - expected_image ).max() < 2e-2
281
0
"""simple docstring""" from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import KandinskyPipeline, KandinskyPriorPipeline else: from .pipeline_kandinsky import KandinskyPipeline from .pipeline_kandinsky_imgaimg import KandinskyImgaImgPipeline from .pipeline_kandinsky_inpaint import KandinskyInpaintPipeline from .pipeline_kandinsky_prior import KandinskyPriorPipeline, KandinskyPriorPipelineOutput from .text_encoder import MultilingualCLIP
109
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_convbert import ConvBertTokenizer UpperCAmelCase__ : Optional[Any] = logging.get_logger(__name__) UpperCAmelCase__ : Dict = {'vocab_file': 'vocab.txt'} UpperCAmelCase__ : List[Any] = { 'vocab_file': { 'YituTech/conv-bert-base': 'https://huggingface.co/YituTech/conv-bert-base/resolve/main/vocab.txt', 'YituTech/conv-bert-medium-small': ( 'https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/vocab.txt' ), 'YituTech/conv-bert-small': 'https://huggingface.co/YituTech/conv-bert-small/resolve/main/vocab.txt', } } UpperCAmelCase__ : Union[str, Any] = { 'YituTech/conv-bert-base': 512, 'YituTech/conv-bert-medium-small': 512, 'YituTech/conv-bert-small': 512, } UpperCAmelCase__ : Dict = { 'YituTech/conv-bert-base': {'do_lower_case': True}, 'YituTech/conv-bert-medium-small': {'do_lower_case': True}, 'YituTech/conv-bert-small': {'do_lower_case': True}, } class UpperCAmelCase ( SCREAMING_SNAKE_CASE__ ): '''simple docstring''' __UpperCamelCase : Any = VOCAB_FILES_NAMES __UpperCamelCase : Tuple = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : str = PRETRAINED_INIT_CONFIGURATION __UpperCamelCase : int = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : int = ConvBertTokenizer def __init__( self : Tuple , lowerCAmelCase_ : int=None , lowerCAmelCase_ : List[Any]=None , lowerCAmelCase_ : Optional[Any]=True , lowerCAmelCase_ : List[Any]="[UNK]" , lowerCAmelCase_ : Optional[Any]="[SEP]" , lowerCAmelCase_ : Optional[Any]="[PAD]" , lowerCAmelCase_ : List[Any]="[CLS]" , lowerCAmelCase_ : Optional[int]="[MASK]" , lowerCAmelCase_ : Any=True , lowerCAmelCase_ : List[str]=None , **lowerCAmelCase_ : List[Any] , ): """simple docstring""" super().__init__( lowerCAmelCase_ , tokenizer_file=lowerCAmelCase_ , do_lower_case=lowerCAmelCase_ , unk_token=lowerCAmelCase_ , sep_token=lowerCAmelCase_ , pad_token=lowerCAmelCase_ , cls_token=lowerCAmelCase_ , mask_token=lowerCAmelCase_ , tokenize_chinese_chars=lowerCAmelCase_ , strip_accents=lowerCAmelCase_ , **lowerCAmelCase_ , ) _A: List[Any] = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('''lowercase''' , lowerCAmelCase_ ) != do_lower_case or normalizer_state.get('''strip_accents''' , lowerCAmelCase_ ) != strip_accents or normalizer_state.get('''handle_chinese_chars''' , lowerCAmelCase_ ) != tokenize_chinese_chars ): _A: List[str] = getattr(lowerCAmelCase_ , normalizer_state.pop('''type''' ) ) _A: List[Any] = do_lower_case _A: Optional[Any] = strip_accents _A: Union[str, Any] = tokenize_chinese_chars _A: Optional[int] = normalizer_class(**lowerCAmelCase_ ) _A: Optional[Any] = do_lower_case def __magic_name__ ( self : str , lowerCAmelCase_ : Tuple , lowerCAmelCase_ : Union[str, Any]=None ): """simple docstring""" _A: Dict = [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 __magic_name__ ( self : Union[str, Any] , lowerCAmelCase_ : List[int] , lowerCAmelCase_ : Optional[List[int]] = None ): """simple docstring""" _A: Any = [self.sep_token_id] _A: int = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __magic_name__ ( self : int , lowerCAmelCase_ : str , lowerCAmelCase_ : Optional[str] = None ): """simple docstring""" _A: str = self._tokenizer.model.save(lowerCAmelCase_ , name=lowerCAmelCase_ ) return tuple(lowerCAmelCase_ )
121
0
"""simple docstring""" import functools def lowerCamelCase__ ( lowercase , lowercase ): """simple docstring""" if not isinstance(lowercase , lowercase ) or not all(isinstance(lowercase , lowercase ) for day in days ): raise ValueError("The parameter days should be a list of integers" ) if len(lowercase ) != 3 or not all(isinstance(lowercase , lowercase ) for cost in costs ): raise ValueError("The parameter costs should be a list of three integers" ) if len(lowercase ) == 0: return 0 if min(lowercase ) <= 0: raise ValueError("All days elements should be greater than 0" ) if max(lowercase ) >= 366: raise ValueError("All days elements should be less than 366" ) SCREAMING_SNAKE_CASE : Dict = set(lowercase ) @functools.cache def dynamic_programming(lowercase ) -> int: if index > 365: return 0 if index not in days_set: return dynamic_programming(index + 1 ) return min( costs[0] + dynamic_programming(index + 1 ) , costs[1] + dynamic_programming(index + 7 ) , costs[2] + dynamic_programming(index + 30 ) , ) return dynamic_programming(1 ) if __name__ == "__main__": import doctest doctest.testmod()
352
def lowerCamelCase__ ( lowercase ): """simple docstring""" SCREAMING_SNAKE_CASE : Dict = n ** (1 / 3) return (val * val * val) == n if __name__ == "__main__": print(perfect_cube(27)) print(perfect_cube(4))
319
0
'''simple docstring''' def _UpperCamelCase ( SCREAMING_SNAKE_CASE__ : int = 1000000 ): '''simple docstring''' UpperCAmelCase__ = [i - 1 for i in range(limit + 1 )] for i in range(2 , limit + 1 ): if phi[i] == i - 1: for j in range(2 * i , limit + 1 , SCREAMING_SNAKE_CASE__ ): phi[j] -= phi[j] // i return sum(phi[2 : limit + 1] ) if __name__ == "__main__": print(solution())
346
'''simple docstring''' from math import factorial def _UpperCamelCase ( SCREAMING_SNAKE_CASE__ : int = 20 ): '''simple docstring''' UpperCAmelCase__ = 2 * n # middle entry of odd rows starting at row 3 is the solution for n = 1, # 2, 3,... UpperCAmelCase__ = n // 2 return int(factorial(SCREAMING_SNAKE_CASE__ ) / (factorial(SCREAMING_SNAKE_CASE__ ) * factorial(n - k )) ) if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution(2_0)) else: try: UpperCAmelCase_ = int(sys.argv[1]) print(solution(n)) except ValueError: print('Invalid entry - please enter a number.')
346
1
'''simple docstring''' from __future__ import annotations from collections import namedtuple def SCREAMING_SNAKE_CASE__ ( __A , __A , __A ) -> tuple: _snake_case = namedtuple('result' , 'name value' ) if (voltage, current, power).count(0 ) != 1: raise ValueError('Only one argument must be 0' ) elif power < 0: raise ValueError( 'Power cannot be negative in any electrical/electronics system' ) elif voltage == 0: return result('voltage' , power / current ) elif current == 0: return result('current' , power / voltage ) elif power == 0: return result('power' , float(round(abs(voltage * current ) , 2 ) ) ) else: raise ValueError('Exactly one argument must be 0' ) if __name__ == "__main__": import doctest doctest.testmod()
160
'''simple docstring''' import os import sys import unittest lowercase : List[str] = 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 lowercase : List[Any] = os.path.join(git_repo_path, "src", "diffusers") class __UpperCAmelCase ( unittest.TestCase ): def lowerCamelCase ( self ): """simple docstring""" _snake_case = find_backend(' if not is_torch_available():' ) self.assertEqual(lowerCAmelCase_ , 'torch' ) # backend_with_underscore = find_backend(" if not is_tensorflow_text_available():") # self.assertEqual(backend_with_underscore, "tensorflow_text") _snake_case = find_backend(' if not (is_torch_available() and is_transformers_available()):' ) self.assertEqual(lowerCAmelCase_ , 'torch_and_transformers' ) # double_backend_with_underscore = find_backend( # " if not (is_sentencepiece_available() and is_tensorflow_text_available()):" # ) # self.assertEqual(double_backend_with_underscore, "sentencepiece_and_tensorflow_text") _snake_case = find_backend( ' if not (is_torch_available() and is_transformers_available() and is_onnx_available()):' ) self.assertEqual(lowerCAmelCase_ , 'torch_and_transformers_and_onnx' ) def lowerCamelCase ( self ): """simple docstring""" _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' , lowerCAmelCase_ ) self.assertIn('torch_and_transformers' , lowerCAmelCase_ ) self.assertIn('flax_and_transformers' , lowerCAmelCase_ ) self.assertIn('torch_and_transformers_and_onnx' , lowerCAmelCase_ ) # Likewise, we can't assert on the exact content of a key self.assertIn('UNet2DModel' , objects['torch'] ) self.assertIn('FlaxUNet2DConditionModel' , objects['flax'] ) self.assertIn('StableDiffusionPipeline' , objects['torch_and_transformers'] ) self.assertIn('FlaxStableDiffusionPipeline' , objects['flax_and_transformers'] ) self.assertIn('LMSDiscreteScheduler' , objects['torch_and_scipy'] ) self.assertIn('OnnxStableDiffusionPipeline' , objects['torch_and_transformers_and_onnx'] ) def lowerCamelCase ( self ): """simple docstring""" _snake_case = create_dummy_object('CONSTANT' , '\'torch\'' ) self.assertEqual(lowerCAmelCase_ , '\nCONSTANT = None\n' ) _snake_case = create_dummy_object('function' , '\'torch\'' ) self.assertEqual( lowerCAmelCase_ , '\ndef function(*args, **kwargs):\n requires_backends(function, \'torch\')\n' ) _snake_case = '\nclass FakeClass(metaclass=DummyObject):\n _backends = \'torch\'\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, \'torch\')\n\n @classmethod\n def from_config(cls, *args, **kwargs):\n requires_backends(cls, \'torch\')\n\n @classmethod\n def from_pretrained(cls, *args, **kwargs):\n requires_backends(cls, \'torch\')\n' _snake_case = create_dummy_object('FakeClass' , '\'torch\'' ) self.assertEqual(lowerCAmelCase_ , lowerCAmelCase_ ) def lowerCamelCase ( self ): """simple docstring""" _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\n @classmethod\n def from_config(cls, *args, **kwargs):\n requires_backends(cls, ["torch"])\n\n @classmethod\n def from_pretrained(cls, *args, **kwargs):\n requires_backends(cls, ["torch"])\n' _snake_case = create_dummy_files({'torch': ['CONSTANT', 'function', 'FakeClass']} ) self.assertEqual(dummy_files['torch'] , lowerCAmelCase_ )
160
1
from __future__ import annotations import os import tempfile import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers import is_tensorflow_text_available, is_tf_available from transformers.testing_utils import require_tensorflow_text, require_tf, slow from ..test_modeling_tf_common import floats_tensor from .test_framework_agnostic import GenerationIntegrationTestsMixin if is_tf_available(): import tensorflow as tf from transformers import ( AutoTokenizer, TFAutoModelForCausalLM, TFAutoModelForSeqaSeqLM, TFAutoModelForSpeechSeqaSeq, TFAutoModelForVisionaSeq, TFBartForConditionalGeneration, TFLogitsProcessorList, TFMinLengthLogitsProcessor, tf_top_k_top_p_filtering, ) if is_tensorflow_text_available(): import tensorflow_text as text @require_tf class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = tf.convert_to_tensor( [ [ 8.222_0991, # 3rd highest value; idx. 0 -0.562_0044, 5.2322_9752, 4.038_6393, -6.879_8378, -0.5478_5802, -3.201_2153, 2.9277_7176, 1.8817_1953, 7.3534_1276, # 5th highest value; idx. 9 8.4320_7833, # 2nd highest value; idx. 10 -9.8571_1836, -5.9620_9236, -1.1303_9161, -7.111_5294, -0.836_9633, -5.318_6408, 7.0642_7407, 0.8136_9344, -0.8202_3817, -5.917_9796, 0.5881_3443, -6.9977_8438, 4.7155_1189, -0.1877_1637, 7.4402_0759, # 4th highest value; idx. 25 9.3845_0987, # 1st highest value; idx. 26 2.1266_2941, -9.3256_2038, 2.3565_2522, ], # cummulative prob of 5 highest values <= 0.6 [ 0.5842_5518, 4.5313_9238, -5.5751_0464, -6.2803_0699, -7.1952_9503, -4.0212_2551, 1.3933_7037, -6.0670_7057, 1.5948_0517, -9.64_3119, 0.0390_7799, 0.6723_1762, -8.8820_6726, 6.2711_5922, # 4th highest value; idx. 13 2.2852_0723, 4.8276_7506, 4.3042_1368, 8.827_5313, # 2nd highest value; idx. 17 5.4402_9958, # 5th highest value; idx. 18 -4.473_5794, 7.3857_9536, # 3rd highest value; idx. 20 -2.9105_1663, 2.6194_6077, -2.567_4762, -9.4895_9302, -4.0292_2645, -1.3541_6918, 9.6770_2323, # 1st highest value; idx. 27 -5.8947_8553, 1.8537_0467, ], # cummulative prob of 5 highest values <= 0.6 ] , dtype=tf.floataa , ) SCREAMING_SNAKE_CASE_ = tf.convert_to_tensor( [[0, 0], [0, 9], [0, 10], [0, 25], [0, 26], [1, 13], [1, 17], [1, 18], [1, 20], [1, 27]] , dtype=tf.intaa , ) # expected non filtered idx as noted above SCREAMING_SNAKE_CASE_ = tf.convert_to_tensor( [8.22_2099, 7.353_4126, 8.43_2078, 7.440_2075, 9.3_8451, 6.27_1159, 8.82_7531, 5.440_2995, 7.385_7956, 9.67_7023] , dtype=tf.floataa , ) # expected non filtered values as noted above SCREAMING_SNAKE_CASE_ = tf_top_k_top_p_filtering(_lowerCAmelCase , top_k=10 , top_p=0.6 , min_tokens_to_keep=4 ) SCREAMING_SNAKE_CASE_ = output[output != -float('inf' )] SCREAMING_SNAKE_CASE_ = tf.cast( tf.where(tf.not_equal(_lowerCAmelCase , tf.constant(-float('inf' ) , dtype=tf.floataa ) ) ) , dtype=tf.intaa , ) tf.debugging.assert_near(_lowerCAmelCase , _lowerCAmelCase , rtol=1E-12 ) tf.debugging.assert_equal(_lowerCAmelCase , _lowerCAmelCase ) @require_tf class lowerCamelCase_ ( unittest.TestCase , _SCREAMING_SNAKE_CASE ): '''simple docstring''' if is_tf_available(): lowercase_ = { "AutoModelForCausalLM": TFAutoModelForCausalLM, "AutoModelForSpeechSeq2Seq": TFAutoModelForSpeechSeqaSeq, "AutoModelForSeq2SeqLM": TFAutoModelForSeqaSeqLM, "AutoModelForVision2Seq": TFAutoModelForVisionaSeq, "LogitsProcessorList": TFLogitsProcessorList, "MinLengthLogitsProcessor": TFMinLengthLogitsProcessor, "create_tensor_fn": tf.convert_to_tensor, "floats_tensor": floats_tensor, "return_tensors": "tf", } @slow def lowerCAmelCase_ ( self : Union[str, Any] ): # TF-only test: tf.saved_model export SCREAMING_SNAKE_CASE_ = TFAutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2' ) SCREAMING_SNAKE_CASE_ = 2 SCREAMING_SNAKE_CASE_ = 2 class lowerCamelCase_ ( tf.Module ): '''simple docstring''' def __init__( self : str , _lowerCAmelCase : Union[str, Any] ): super(_lowerCAmelCase , self ).__init__() SCREAMING_SNAKE_CASE_ = model @tf.function( input_signature=( tf.TensorSpec((None, input_length) , tf.intaa , name='input_ids' ), tf.TensorSpec((None, input_length) , tf.intaa , name='attention_mask' ), ) , jit_compile=_lowerCAmelCase , ) def lowerCAmelCase_ ( self : str , _lowerCAmelCase : int , _lowerCAmelCase : List[Any] ): SCREAMING_SNAKE_CASE_ = self.model.generate( input_ids=_lowerCAmelCase , attention_mask=_lowerCAmelCase , max_new_tokens=_lowerCAmelCase , return_dict_in_generate=_lowerCAmelCase , ) return {"sequences": outputs["sequences"]} SCREAMING_SNAKE_CASE_ = [[2, 0], [102, 103]] SCREAMING_SNAKE_CASE_ = [[1, 0], [1, 1]] SCREAMING_SNAKE_CASE_ = DummyModel(model=_lowerCAmelCase ) with tempfile.TemporaryDirectory() as tmp_dir: tf.saved_model.save(_lowerCAmelCase , _lowerCAmelCase , signatures={'serving_default': dummy_model.serving} ) SCREAMING_SNAKE_CASE_ = tf.saved_model.load(_lowerCAmelCase ).signatures['serving_default'] for batch_size in range(1 , len(_lowerCAmelCase ) + 1 ): SCREAMING_SNAKE_CASE_ = { 'input_ids': tf.constant(dummy_input_ids[:batch_size] ), 'attention_mask': tf.constant(dummy_attention_masks[:batch_size] ), } SCREAMING_SNAKE_CASE_ = serving_func(**_lowerCAmelCase )['sequences'] SCREAMING_SNAKE_CASE_ = test_model.generate(**_lowerCAmelCase , max_new_tokens=_lowerCAmelCase ) tf.debugging.assert_equal(_lowerCAmelCase , _lowerCAmelCase ) @slow def lowerCAmelCase_ ( self : List[Any] ): # TF-only test: tf.saved_model export SCREAMING_SNAKE_CASE_ = TFAutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2' ) SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = 2 class lowerCamelCase_ ( tf.Module ): '''simple docstring''' def __init__( self : Any , _lowerCAmelCase : List[str] ): super(_lowerCAmelCase , self ).__init__() SCREAMING_SNAKE_CASE_ = model @tf.function( input_signature=( tf.TensorSpec((batch_size, None) , tf.intaa , name='input_ids' ), tf.TensorSpec((batch_size, None) , tf.intaa , name='attention_mask' ), ) , jit_compile=_lowerCAmelCase , ) def lowerCAmelCase_ ( self : int , _lowerCAmelCase : str , _lowerCAmelCase : Optional[Any] ): SCREAMING_SNAKE_CASE_ = self.model.generate( input_ids=_lowerCAmelCase , attention_mask=_lowerCAmelCase , max_new_tokens=_lowerCAmelCase , return_dict_in_generate=_lowerCAmelCase , ) return {"sequences": outputs["sequences"]} SCREAMING_SNAKE_CASE_ = [[2], [102, 103]] SCREAMING_SNAKE_CASE_ = [[1], [1, 1]] SCREAMING_SNAKE_CASE_ = DummyModel(model=_lowerCAmelCase ) with tempfile.TemporaryDirectory() as tmp_dir: tf.saved_model.save(_lowerCAmelCase , _lowerCAmelCase , signatures={'serving_default': dummy_model.serving} ) SCREAMING_SNAKE_CASE_ = tf.saved_model.load(_lowerCAmelCase ).signatures['serving_default'] for input_row in range(len(_lowerCAmelCase ) ): SCREAMING_SNAKE_CASE_ = { 'input_ids': tf.constant([dummy_input_ids[input_row]] ), 'attention_mask': tf.constant([dummy_attention_masks[input_row]] ), } SCREAMING_SNAKE_CASE_ = serving_func(**_lowerCAmelCase )['sequences'] SCREAMING_SNAKE_CASE_ = test_model.generate(**_lowerCAmelCase , max_new_tokens=_lowerCAmelCase ) tf.debugging.assert_equal(_lowerCAmelCase , _lowerCAmelCase ) @slow @require_tensorflow_text def lowerCAmelCase_ ( self : Optional[Any] ): # TF-only test: tf.saved_model export with tempfile.TemporaryDirectory() as tmp_dir: # file needed to load the TF tokenizer hf_hub_download(repo_id='google/flan-t5-small' , filename='spiece.model' , local_dir=_lowerCAmelCase ) class lowerCamelCase_ ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Optional[Any] ): super().__init__() SCREAMING_SNAKE_CASE_ = text.SentencepieceTokenizer( model=tf.io.gfile.GFile(os.path.join(_lowerCAmelCase , 'spiece.model' ) , 'rb' ).read() ) SCREAMING_SNAKE_CASE_ = TFAutoModelForSeqaSeqLM.from_pretrained('hf-internal-testing/tiny-random-t5' ) def lowerCAmelCase_ ( self : str , _lowerCAmelCase : int , *_lowerCAmelCase : Dict , **_lowerCAmelCase : Any ): SCREAMING_SNAKE_CASE_ = self.tokenizer.tokenize(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = text.pad_model_inputs( _lowerCAmelCase , max_seq_length=64 , pad_value=self.model.config.pad_token_id ) SCREAMING_SNAKE_CASE_ = self.model.generate(input_ids=_lowerCAmelCase , attention_mask=_lowerCAmelCase ) return self.tokenizer.detokenize(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = CompleteSentenceTransformer() SCREAMING_SNAKE_CASE_ = tf.keras.layers.Input(shape=(1,) , dtype=tf.string , name='inputs' ) SCREAMING_SNAKE_CASE_ = complete_model(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tf.keras.Model(_lowerCAmelCase , _lowerCAmelCase ) keras_model.save(_lowerCAmelCase ) def lowerCAmelCase_ ( self : List[Any] ): # Has PT equivalent: this test relies on random sampling SCREAMING_SNAKE_CASE_ = { 'do_sample': True, 'num_beams': 1, 'top_p': 0.7, 'top_k': 10, 'temperature': 0.7, } SCREAMING_SNAKE_CASE_ = 14 SCREAMING_SNAKE_CASE_ = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-gpt2' ) SCREAMING_SNAKE_CASE_ = 'Hello, my dog is cute and' SCREAMING_SNAKE_CASE_ = tokenizer(_lowerCAmelCase , return_tensors='tf' ) SCREAMING_SNAKE_CASE_ = TFAutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2' ) SCREAMING_SNAKE_CASE_ = 638 # forces the generation to happen on CPU, to avoid GPU-related quirks with tf.device(':/CPU:0' ): tf.random.set_seed(0 ) SCREAMING_SNAKE_CASE_ = model.generate(**_lowerCAmelCase , eos_token_id=_lowerCAmelCase , **_lowerCAmelCase ) self.assertTrue(expectation == len(generated_tokens[0] ) ) SCREAMING_SNAKE_CASE_ = [638, 198] with tf.device(':/CPU:0' ): tf.random.set_seed(0 ) SCREAMING_SNAKE_CASE_ = model.generate(**_lowerCAmelCase , eos_token_id=_lowerCAmelCase , **_lowerCAmelCase ) self.assertTrue(expectation == len(generated_tokens[0] ) ) def lowerCAmelCase_ ( self : Tuple ): # Has PT equivalent: ample use of framework-specific code SCREAMING_SNAKE_CASE_ = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-bart' ) SCREAMING_SNAKE_CASE_ = 'Hugging Face is a technology company based in New York and Paris.' SCREAMING_SNAKE_CASE_ = bart_tokenizer(_lowerCAmelCase , return_tensors='tf' ).input_ids SCREAMING_SNAKE_CASE_ = TFBartForConditionalGeneration.from_pretrained('hf-internal-testing/tiny-random-bart' ) SCREAMING_SNAKE_CASE_ = bart_model.generate(_lowerCAmelCase ).numpy() class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : Optional[int] , _lowerCAmelCase : Tuple=None , **_lowerCAmelCase : Tuple ): return super().call(_lowerCAmelCase , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = FakeBart.from_pretrained('hf-internal-testing/tiny-random-bart' ) SCREAMING_SNAKE_CASE_ = bart_model.generate(_lowerCAmelCase , foo='bar' ).numpy() self.assertTrue(np.array_equal(_lowerCAmelCase , _lowerCAmelCase ) ) class lowerCamelCase_ ( bart_model.model.encoder.__class__ ): '''simple docstring''' def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Tuple , **_lowerCAmelCase : int ): return super().call(_lowerCAmelCase , **_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = FakeEncoder(bart_model.config , bart_model.model.shared ) SCREAMING_SNAKE_CASE_ = fake_encoder # Normal generation still works (the output will be different because the encoder weights are different) SCREAMING_SNAKE_CASE_ = bart_model.generate(_lowerCAmelCase ).numpy() with self.assertRaises(_lowerCAmelCase ): # FakeEncoder.call() accepts **kwargs -> no filtering -> value error due to unexpected input "foo" bart_model.generate(_lowerCAmelCase , foo='bar' )
225
def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : int ) -> int: return number | (1 << position) def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : int ) -> int: return number & ~(1 << position) def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : int ) -> int: return number ^ (1 << position) def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : int ) -> bool: return ((number >> position) & 1) == 1 def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : int ) -> int: return int((number & (1 << position)) != 0 ) if __name__ == "__main__": import doctest doctest.testmod()
225
1
def lowerCAmelCase_ ( __A ) -> list[list]: '''simple docstring''' UpperCAmelCase__ = current_set.copy() for row_index, row in enumerate(__A ): UpperCAmelCase__ = row[0] for column_index, column in enumerate(__A ): if magnitude == 0: UpperCAmelCase__ = column continue UpperCAmelCase__ = column / magnitude # Subtract to cancel term UpperCAmelCase__ = current_set[0] UpperCAmelCase__ = [first_row] UpperCAmelCase__ = current_set[1::] for row in current_set: UpperCAmelCase__ = [] # If first term is 0, it is already in form we want, so we preserve it if row[0] == 0: final_set.append(__A ) continue for column_index in range(len(__A ) ): temp_row.append(first_row[column_index] - row[column_index] ) final_set.append(__A ) # Create next recursion iteration set if len(final_set[0] ) != 3: UpperCAmelCase__ = final_set[0] UpperCAmelCase__ = [] UpperCAmelCase__ = [] for row in final_set[1::]: current_first_column.append(row[0] ) next_iteration.append(row[1::] ) UpperCAmelCase__ = simplify(__A ) for i in range(len(__A ) ): resultant[i].insert(0, current_first_column[i] ) resultant.insert(0, __A ) UpperCAmelCase__ = resultant return final_set def lowerCAmelCase_ ( __A ) -> list: '''simple docstring''' if len(__A ) == 0: raise IndexError("solve_simultaneous() requires n lists of length n+1" ) UpperCAmelCase__ = len(__A ) + 1 if any(len(__A ) != _length for item in equations ): raise IndexError("solve_simultaneous() requires n lists of length n+1" ) for row in equations: if any(not isinstance(__A, (int, float) ) for column in row ): raise ValueError("solve_simultaneous() requires lists of integers" ) if len(__A ) == 1: return [equations[0][-1] / equations[0][0]] UpperCAmelCase__ = equations.copy() if any(0 in row for row in data_set ): UpperCAmelCase__ = data_set.copy() UpperCAmelCase__ = [] for row_index, row in enumerate(__A ): if 0 not in row: UpperCAmelCase__ = data_set.pop(__A ) break if not full_row: raise ValueError("solve_simultaneous() requires at least 1 full equation" ) data_set.insert(0, __A ) UpperCAmelCase__ = data_set.copy() UpperCAmelCase__ = simplify(__A ) UpperCAmelCase__ = simplified[::-1] UpperCAmelCase__ = [] for row in simplified: UpperCAmelCase__ = row[-1] if not solutions: if row[-2] == 0: solutions.append(0 ) continue solutions.append(current_solution / row[-2] ) continue UpperCAmelCase__ = row.copy()[: len(__A ) - 1 :] while temp_row[0] == 0: temp_row.pop(0 ) if len(__A ) == 0: solutions.append(0 ) continue UpperCAmelCase__ = temp_row[1::] UpperCAmelCase__ = temp_row[::-1] for column_index, column in enumerate(__A ): current_solution -= column * solutions[column_index] solutions.append(__A ) UpperCAmelCase__ = [] for item in solutions: final.append(float(round(__A, 5 ) ) ) return final[::-1] if __name__ == "__main__": import doctest doctest.testmod() UpperCamelCase__ = [ [2, 1, 1, 1, 1, 4], [1, 2, 1, 1, 1, 5], [1, 1, 2, 1, 1, 6], [1, 1, 1, 2, 1, 7], [1, 1, 1, 1, 2, 8], ] print(solve_simultaneous(eq)) print(solve_simultaneous([[4, 2]]))
143
# Lint as: python3 import itertools import os import re UpperCamelCase__ = re.compile(R'([A-Z]+)([A-Z][a-z])') UpperCamelCase__ = re.compile(R'([a-z\d])([A-Z])') UpperCamelCase__ = re.compile(R'(?<!_)_(?!_)') UpperCamelCase__ = re.compile(R'(_{2,})') UpperCamelCase__ = R'^\w+(\.\w+)*$' UpperCamelCase__ = R'<>:/\|?*' def lowerCAmelCase_ ( __A ) -> Dict: '''simple docstring''' UpperCAmelCase__ = _uppercase_uppercase_re.sub(r"\1_\2", __A ) UpperCAmelCase__ = _lowercase_uppercase_re.sub(r"\1_\2", __A ) return name.lower() def lowerCAmelCase_ ( __A ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase__ = _single_underscore_re.split(__A ) UpperCAmelCase__ = [_multiple_underscores_re.split(__A ) for n in name] return "".join(n.capitalize() for n in itertools.chain.from_iterable(__A ) if n != "" ) def lowerCAmelCase_ ( __A ) -> List[str]: '''simple docstring''' if os.path.basename(__A ) != name: raise ValueError(f"""Should be a dataset name, not a path: {name}""" ) return camelcase_to_snakecase(__A ) def lowerCAmelCase_ ( __A, __A ) -> Optional[int]: '''simple docstring''' if os.path.basename(__A ) != name: raise ValueError(f"""Should be a dataset name, not a path: {name}""" ) if not re.match(_split_re, __A ): raise ValueError(f"""Split name should match '{_split_re}'' but got '{split}'.""" ) return f"""{filename_prefix_for_name(__A )}-{split}""" def lowerCAmelCase_ ( __A, __A, __A, __A=None ) -> str: '''simple docstring''' UpperCAmelCase__ = filename_prefix_for_split(__A, __A ) if filetype_suffix: prefix += f""".{filetype_suffix}""" UpperCAmelCase__ = os.path.join(__A, __A ) return f"""{filepath}*""" def lowerCAmelCase_ ( __A, __A, __A, __A=None, __A=None ) -> Any: '''simple docstring''' UpperCAmelCase__ = filename_prefix_for_split(__A, __A ) UpperCAmelCase__ = os.path.join(__A, __A ) if shard_lengths: UpperCAmelCase__ = len(__A ) UpperCAmelCase__ = [f"""{prefix}-{shard_id:05d}-of-{num_shards:05d}""" for shard_id in range(__A )] if filetype_suffix: UpperCAmelCase__ = [filename + f""".{filetype_suffix}""" for filename in filenames] return filenames else: UpperCAmelCase__ = prefix if filetype_suffix: filename += f""".{filetype_suffix}""" return [filename]
143
1
'''simple docstring''' import unittest from .lib import ( Matrix, Vector, axpy, square_zero_matrix, unit_basis_vector, zero_vector, ) class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): def __magic_name__ ( self : Any ) -> None: SCREAMING_SNAKE_CASE__ : List[str] =Vector([1, 2, 3] ) self.assertEqual(x.component(0 ) , 1 ) self.assertEqual(x.component(2 ) , 3 ) SCREAMING_SNAKE_CASE__ : str =Vector() def __magic_name__ ( self : List[Any] ) -> None: SCREAMING_SNAKE_CASE__ : Union[str, Any] =Vector([0, 0, 0, 0, 0, 1] ) self.assertEqual(str(__lowercase ) , '''(0,0,0,0,0,1)''' ) def __magic_name__ ( self : Dict ) -> None: SCREAMING_SNAKE_CASE__ : Optional[Any] =Vector([1, 2, 3, 4] ) self.assertEqual(len(__lowercase ) , 4 ) def __magic_name__ ( self : int ) -> None: SCREAMING_SNAKE_CASE__ : Optional[Any] =Vector([1, 2] ) SCREAMING_SNAKE_CASE__ : Optional[Any] =Vector([1, 2, 3, 4, 5] ) SCREAMING_SNAKE_CASE__ : Optional[Any] =Vector([0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ) SCREAMING_SNAKE_CASE__ : int =Vector([1, -1, 1, -1, 2, -3, 4, -5] ) self.assertAlmostEqual(x.euclidean_length() , 2.236 , 3 ) self.assertAlmostEqual(y.euclidean_length() , 7.416 , 3 ) self.assertEqual(z.euclidean_length() , 0 ) self.assertAlmostEqual(w.euclidean_length() , 7.616 , 3 ) def __magic_name__ ( self : Union[str, Any] ) -> None: SCREAMING_SNAKE_CASE__ : Dict =Vector([1, 2, 3] ) SCREAMING_SNAKE_CASE__ : Tuple =Vector([1, 1, 1] ) self.assertEqual((x + y).component(0 ) , 2 ) self.assertEqual((x + y).component(1 ) , 3 ) self.assertEqual((x + y).component(2 ) , 4 ) def __magic_name__ ( self : Optional[Any] ) -> None: SCREAMING_SNAKE_CASE__ : Union[str, Any] =Vector([1, 2, 3] ) SCREAMING_SNAKE_CASE__ : Optional[int] =Vector([1, 1, 1] ) self.assertEqual((x - y).component(0 ) , 0 ) self.assertEqual((x - y).component(1 ) , 1 ) self.assertEqual((x - y).component(2 ) , 2 ) def __magic_name__ ( self : Tuple ) -> None: SCREAMING_SNAKE_CASE__ : Optional[Any] =Vector([1, 2, 3] ) SCREAMING_SNAKE_CASE__ : Dict =Vector([2, -1, 4] ) # for test of dot product SCREAMING_SNAKE_CASE__ : Tuple =Vector([1, -2, -1] ) self.assertEqual(str(x * 3.0 ) , '''(3.0,6.0,9.0)''' ) self.assertEqual((a * b) , 0 ) def __magic_name__ ( self : Optional[int] ) -> None: self.assertEqual(str(zero_vector(10 ) ).count('''0''' ) , 10 ) def __magic_name__ ( self : Union[str, Any] ) -> None: self.assertEqual(str(unit_basis_vector(3 , 1 ) ) , '''(0,1,0)''' ) def __magic_name__ ( self : Optional[Any] ) -> None: SCREAMING_SNAKE_CASE__ : Optional[int] =Vector([1, 2, 3] ) SCREAMING_SNAKE_CASE__ : List[str] =Vector([1, 0, 1] ) self.assertEqual(str(axpy(2 , __lowercase , __lowercase ) ) , '''(3,4,7)''' ) def __magic_name__ ( self : Tuple ) -> None: SCREAMING_SNAKE_CASE__ : List[Any] =Vector([1, 0, 0, 0, 0, 0] ) SCREAMING_SNAKE_CASE__ : List[str] =x.copy() self.assertEqual(str(__lowercase ) , str(__lowercase ) ) def __magic_name__ ( self : Tuple ) -> None: SCREAMING_SNAKE_CASE__ : str =Vector([1, 0, 0] ) x.change_component(0 , 0 ) x.change_component(1 , 1 ) self.assertEqual(str(__lowercase ) , '''(0,1,0)''' ) def __magic_name__ ( self : Union[str, Any] ) -> None: SCREAMING_SNAKE_CASE__ : Dict =Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) self.assertEqual('''|1,2,3|\n|2,4,5|\n|6,7,8|\n''' , str(__lowercase ) ) def __magic_name__ ( self : Optional[Any] ) -> None: SCREAMING_SNAKE_CASE__ : Optional[Any] =Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) SCREAMING_SNAKE_CASE__ : Tuple =[[-3, -14, -10], [-5, -10, -5], [-2, -1, 0]] for x in range(a.height() ): for y in range(a.width() ): self.assertEqual(minors[x][y] , a.minor(__lowercase , __lowercase ) ) def __magic_name__ ( self : Union[str, Any] ) -> None: SCREAMING_SNAKE_CASE__ : List[Any] =Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) SCREAMING_SNAKE_CASE__ : int =[[-3, 14, -10], [5, -10, 5], [-2, 1, 0]] for x in range(a.height() ): for y in range(a.width() ): self.assertEqual(cofactors[x][y] , a.cofactor(__lowercase , __lowercase ) ) def __magic_name__ ( self : Any ) -> None: SCREAMING_SNAKE_CASE__ : Union[str, Any] =Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) self.assertEqual(-5 , a.determinant() ) def __magic_name__ ( self : List[str] ) -> None: SCREAMING_SNAKE_CASE__ : List[Any] =Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]] , 3 , 3 ) SCREAMING_SNAKE_CASE__ : Optional[Any] =Vector([1, 2, 3] ) self.assertEqual('''(14,32,50)''' , str(a * x ) ) self.assertEqual('''|2,4,6|\n|8,10,12|\n|14,16,18|\n''' , str(a * 2 ) ) def __magic_name__ ( self : Dict ) -> None: SCREAMING_SNAKE_CASE__ : str =Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) a.change_component(0 , 2 , 5 ) self.assertEqual('''|1,2,5|\n|2,4,5|\n|6,7,8|\n''' , str(__lowercase ) ) def __magic_name__ ( self : Union[str, Any] ) -> None: SCREAMING_SNAKE_CASE__ : Optional[Any] =Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) self.assertEqual(7 , a.component(2 , 1 ) , 0.01 ) def __magic_name__ ( self : str ) -> None: SCREAMING_SNAKE_CASE__ : Optional[int] =Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) SCREAMING_SNAKE_CASE__ : str =Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]] , 3 , 3 ) self.assertEqual('''|2,4,10|\n|4,8,10|\n|12,14,18|\n''' , str(a + b ) ) def __magic_name__ ( self : int ) -> None: SCREAMING_SNAKE_CASE__ : int =Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) SCREAMING_SNAKE_CASE__ : int =Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]] , 3 , 3 ) self.assertEqual('''|0,0,-4|\n|0,0,0|\n|0,0,-2|\n''' , str(a - b ) ) def __magic_name__ ( self : Tuple ) -> None: self.assertEqual( '''|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n''' , str(square_zero_matrix(5 ) ) , ) if __name__ == "__main__": unittest.main()
152
'''simple docstring''' import warnings from pathlib import Path from typing import List, Tuple, Union import fire from torch import nn from transformers import AutoModelForSeqaSeqLM, AutoTokenizer, PreTrainedModel from transformers.utils import logging a_ = logging.get_logger(__name__) def _a( UpperCamelCase__ : nn.ModuleList, UpperCamelCase__ : nn.ModuleList, UpperCamelCase__ : List[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[int] =nn.ModuleList([src_layers[i] for i in layers_to_copy] ) assert len(UpperCamelCase__ ) == len(UpperCamelCase__ ), f"{len(UpperCamelCase__ )} != {len(UpperCamelCase__ )}" dest_layers.load_state_dict(layers_to_copy.state_dict() ) a_ = { # maps num layers in teacher -> num_layers in student -> which teacher layers to copy. # 12: bart, 16: pegasus, 6: marian/Helsinki-NLP 1_2: { 1: [0], # This says that if the teacher has 12 layers and the student has 1, copy layer 0 of the teacher 2: [0, 6], 3: [0, 6, 1_1], 4: [0, 4, 8, 1_1], 6: [0, 2, 4, 7, 9, 1_1], 9: [0, 1, 2, 4, 5, 7, 9, 1_0, 1_1], 1_2: list(range(1_2)), }, 1_6: { # maps num layers in student -> which teacher layers to copy 1: [0], 2: [0, 1_5], 3: [0, 8, 1_5], 4: [0, 5, 1_0, 1_5], 6: [0, 3, 6, 9, 1_2, 1_5], 8: [0, 2, 4, 6, 8, 1_0, 1_2, 1_5], 9: [0, 1, 3, 5, 7, 9, 1_1, 1_3, 1_5], 1_2: [0, 1, 2, 3, 4, 5, 6, 7, 9, 1_1, 1_3, 1_5], 1_6: list(range(1_6)), }, 6: {1: [0], 2: [0, 5], 3: [0, 2, 5], 4: [0, 1, 3, 5], 6: list(range(6))}, } a_ = { # maps num layers in student -> which teacher layers to copy. 6: {1: [5], 2: [3, 5], 3: [1, 4, 5], 4: [1, 2, 4, 5]}, 1_2: {1: [1_1], 2: [5, 1_1], 3: [3, 7, 1_1], 6: [1, 3, 5, 8, 1_0, 1_1]}, 1_6: {1: [1_5], 4: [4, 9, 1_2, 1_5], 8: [1, 3, 5, 7, 9, 1_1, 1_3, 1_5]}, } def _a( UpperCamelCase__ : Optional[int], UpperCamelCase__ : str ): '''simple docstring''' try: SCREAMING_SNAKE_CASE__ : int =LAYERS_TO_COPY[n_teacher][n_student] return val except KeyError: if n_student != n_teacher: warnings.warn( f"no hardcoded layers to copy for teacher {n_teacher} -> student {n_student}, defaulting to first" f" {n_student}" ) return list(range(UpperCamelCase__ ) ) def _a( UpperCamelCase__ : List[str], UpperCamelCase__ : Optional[int] ): '''simple docstring''' if n_student > n_teacher: raise ValueError(f"Cannot perform intermediate supervision for student {n_student} > teacher {n_teacher}" ) elif n_teacher == n_student: return list(range(UpperCamelCase__ ) ) elif n_student == 1: return [n_teacher - 1] else: return LAYERS_TO_SUPERVISE[n_teacher][n_student] def _a( UpperCamelCase__ : Union[str, PreTrainedModel], UpperCamelCase__ : Union[str, Path] = "student", UpperCamelCase__ : Union[int, None] = None, UpperCamelCase__ : Union[int, None] = None, UpperCamelCase__ : List[str]=False, UpperCamelCase__ : Any=None, UpperCamelCase__ : Dict=None, **UpperCamelCase__ : List[Any], ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : List[str] ='''encoder_layers and decoder_layers cannot be both None-- you would just have an identical teacher.''' assert (e is not None) or (d is not None), _msg if isinstance(UpperCamelCase__, UpperCamelCase__ ): AutoTokenizer.from_pretrained(UpperCamelCase__ ).save_pretrained(UpperCamelCase__ ) # purely for convenience SCREAMING_SNAKE_CASE__ : str =AutoModelForSeqaSeqLM.from_pretrained(UpperCamelCase__ ).eval() else: assert isinstance(UpperCamelCase__, UpperCamelCase__ ), f"teacher must be a model or string got type {type(UpperCamelCase__ )}" SCREAMING_SNAKE_CASE__ : Union[str, Any] =teacher.config.to_diff_dict() try: SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Optional[Any] =teacher.config.encoder_layers, teacher.config.decoder_layers if e is None: SCREAMING_SNAKE_CASE__ : Dict =teacher_e if d is None: SCREAMING_SNAKE_CASE__ : List[Any] =teacher_d init_kwargs.update({'''encoder_layers''': e, '''decoder_layers''': d} ) except AttributeError: # T5 if hasattr(teacher.config, '''num_encoder_layers''' ): SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Tuple =teacher.config.num_encoder_layers, teacher.config.num_decoder_layers else: SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : List[Any] =teacher.config.num_layers, teacher.config.num_decoder_layers if e is None: SCREAMING_SNAKE_CASE__ : Dict =teacher_e if d is None: SCREAMING_SNAKE_CASE__ : Optional[Any] =teacher_d if hasattr(teacher.config, '''num_encoder_layers''' ): init_kwargs.update({'''num_encoder_layers''': e, '''num_decoder_layers''': d} ) else: init_kwargs.update({'''num_layers''': e, '''num_decoder_layers''': d} ) # Kwargs to instantiate student: teacher kwargs with updated layer numbers + **extra_config_kwargs init_kwargs.update(UpperCamelCase__ ) # Copy weights SCREAMING_SNAKE_CASE__ : Optional[Any] =teacher.config_class(**UpperCamelCase__ ) SCREAMING_SNAKE_CASE__ : int =AutoModelForSeqaSeqLM.from_config(UpperCamelCase__ ) # Start by copying the full teacher state dict this will copy the first N teacher layers to the student. SCREAMING_SNAKE_CASE__ : Any =student.load_state_dict(teacher.state_dict(), strict=UpperCamelCase__ ) assert info.missing_keys == [], info.missing_keys # every student key should have a teacher keys. if copy_first_teacher_layers: # Our copying is done. We just log and save SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Tuple =list(range(UpperCamelCase__ ) ), list(range(UpperCamelCase__ ) ) logger.info( f"Copied encoder layers {e_layers_to_copy} and decoder layers {d_layers_to_copy}. Saving them to" f" {save_path}" ) student.save_pretrained(UpperCamelCase__ ) return student, e_layers_to_copy, d_layers_to_copy # Decide which layers of the teacher to copy. Not exactly alternating -- we try to keep first and last layer. if e_layers_to_copy is None: SCREAMING_SNAKE_CASE__ : List[int] =pick_layers_to_copy(UpperCamelCase__, UpperCamelCase__ ) if d_layers_to_copy is None: SCREAMING_SNAKE_CASE__ : List[int] =pick_layers_to_copy(UpperCamelCase__, UpperCamelCase__ ) try: if hasattr( UpperCamelCase__, '''prophetnet''' ): # For ProphetNet, student.model.encoder.layers is called student.prophetnet.encoder.layers copy_layers(teacher.prophetnet.encoder.layers, student.prophetnet.encoder.layers, UpperCamelCase__ ) copy_layers(teacher.prophetnet.decoder.layers, student.prophetnet.decoder.layers, UpperCamelCase__ ) else: copy_layers(teacher.model.encoder.layers, student.model.encoder.layers, UpperCamelCase__ ) copy_layers(teacher.model.decoder.layers, student.model.decoder.layers, UpperCamelCase__ ) except AttributeError: # For t5, student.model.encoder.layers is called student.encoder.block copy_layers(teacher.encoder.block, student.encoder.block, UpperCamelCase__ ) copy_layers(teacher.decoder.block, student.decoder.block, UpperCamelCase__ ) logger.info( f"Copied encoder layers {e_layers_to_copy} and decoder layers {d_layers_to_copy}. Saving them to {save_path}" ) SCREAMING_SNAKE_CASE__ : List[str] ={ '''teacher_type''': teacher.config.model_type, '''copied_encoder_layers''': e_layers_to_copy, '''copied_decoder_layers''': d_layers_to_copy, } student.save_pretrained(UpperCamelCase__ ) # Save information about copying for easier reproducibility return student, e_layers_to_copy, d_layers_to_copy if __name__ == "__main__": fire.Fire(create_student_by_copying_alternating_layers)
152
1
from pathlib import Path from typing import List from transformers import is_torch_available, is_vision_available from transformers.testing_utils import get_tests_dir, is_tool_test from transformers.tools.agent_types import AGENT_TYPE_MAPPING, AgentAudio, AgentImage, AgentText if is_torch_available(): import torch if is_vision_available(): from PIL import Image a_ : Optional[int] = ['text', 'image', 'audio'] def lowerCamelCase__ (_UpperCAmelCase): SCREAMING_SNAKE_CASE = [] 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(_UpperCAmelCase , _UpperCAmelCase): inputs.append(create_inputs(_UpperCAmelCase)) else: raise ValueError(F'''Invalid type requested: {input_type}''') return inputs def lowerCamelCase__ (_UpperCAmelCase): SCREAMING_SNAKE_CASE = [] for output in outputs: if isinstance(_UpperCAmelCase , (str, AgentText)): output_types.append('text') elif isinstance(_UpperCAmelCase , (Image.Image, AgentImage)): output_types.append('image') elif isinstance(_UpperCAmelCase , (torch.Tensor, AgentAudio)): output_types.append('audio') else: raise ValueError(F'''Invalid output: {output}''') return output_types @is_tool_test class _snake_case : def SCREAMING_SNAKE_CASE__ ( self) -> int: self.assertTrue(hasattr(self.tool , 'inputs')) self.assertTrue(hasattr(self.tool , 'outputs')) SCREAMING_SNAKE_CASE = self.tool.inputs for _input in inputs: if isinstance(_input , a): for __input in _input: self.assertTrue(__input in authorized_types) else: self.assertTrue(_input in authorized_types) SCREAMING_SNAKE_CASE = self.tool.outputs for _output in outputs: self.assertTrue(_output in authorized_types) def SCREAMING_SNAKE_CASE__ ( self) -> int: SCREAMING_SNAKE_CASE = create_inputs(self.tool.inputs) SCREAMING_SNAKE_CASE = self.tool(*a) # There is a single output if len(self.tool.outputs) == 1: SCREAMING_SNAKE_CASE = [outputs] self.assertListEqual(output_types(a) , self.tool.outputs) def SCREAMING_SNAKE_CASE__ ( self) -> Union[str, Any]: 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 SCREAMING_SNAKE_CASE__ ( self) -> Optional[Any]: SCREAMING_SNAKE_CASE = create_inputs(self.tool.inputs) SCREAMING_SNAKE_CASE = self.tool(*a) if not isinstance(a , a): SCREAMING_SNAKE_CASE = [outputs] self.assertEqual(len(a) , len(self.tool.outputs)) for output, output_type in zip(a , self.tool.outputs): SCREAMING_SNAKE_CASE = AGENT_TYPE_MAPPING[output_type] self.assertTrue(isinstance(a , a)) def SCREAMING_SNAKE_CASE__ ( self) -> List[Any]: SCREAMING_SNAKE_CASE = create_inputs(self.tool.inputs) SCREAMING_SNAKE_CASE = [] for _input, input_type in zip(a , self.tool.inputs): if isinstance(a , a): _inputs.append([AGENT_TYPE_MAPPING[_input_type](_input) for _input_type in input_type]) else: _inputs.append(AGENT_TYPE_MAPPING[input_type](_input)) # Should not raise an error SCREAMING_SNAKE_CASE = self.tool(*a) if not isinstance(a , a): SCREAMING_SNAKE_CASE = [outputs] self.assertEqual(len(a) , len(self.tool.outputs))
327
import argparse import gc import json import os import re import torch from huggingface_hub import hf_hub_download from transformers import AutoModelForCausalLM, AutoTokenizer, PreTrainedTokenizerFast, RwkvConfig from transformers.modeling_utils import WEIGHTS_INDEX_NAME, shard_checkpoint a_ : Optional[int] = { '169M': 12, '430M': 24, '1B5': 24, '3B': 32, '7B': 32, '14B': 40, } a_ : Optional[int] = { '169M': 7_68, '430M': 10_24, '1B5': 20_48, '3B': 25_60, '7B': 40_96, '14B': 51_20, } def lowerCamelCase__ (_UpperCAmelCase): SCREAMING_SNAKE_CASE = list(state_dict.keys()) for name in state_dict_keys: SCREAMING_SNAKE_CASE = state_dict.pop(_UpperCAmelCase) # emb -> embedding if name.startswith('emb.'): SCREAMING_SNAKE_CASE = name.replace('emb.' , 'embeddings.') # ln_0 -> pre_ln (only present at block 0) if name.startswith('blocks.0.ln0'): SCREAMING_SNAKE_CASE = name.replace('blocks.0.ln0' , 'blocks.0.pre_ln') # att -> attention SCREAMING_SNAKE_CASE = re.sub(R'blocks\.(\d+)\.att' , R'blocks.\1.attention' , _UpperCAmelCase) # ffn -> feed_forward SCREAMING_SNAKE_CASE = re.sub(R'blocks\.(\d+)\.ffn' , R'blocks.\1.feed_forward' , _UpperCAmelCase) # time_mix_k -> time_mix_key and reshape if name.endswith('.time_mix_k'): SCREAMING_SNAKE_CASE = name.replace('.time_mix_k' , '.time_mix_key') # time_mix_v -> time_mix_value and reshape if name.endswith('.time_mix_v'): SCREAMING_SNAKE_CASE = name.replace('.time_mix_v' , '.time_mix_value') # time_mix_r -> time_mix_key and reshape if name.endswith('.time_mix_r'): SCREAMING_SNAKE_CASE = name.replace('.time_mix_r' , '.time_mix_receptance') if name != "head.weight": SCREAMING_SNAKE_CASE = 'rwkv.' + name SCREAMING_SNAKE_CASE = weight return state_dict def lowerCamelCase__ (_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=False , _UpperCAmelCase=None): # 1. If possible, build the tokenizer. if tokenizer_file is None: print('No `--tokenizer_file` provided, we will use the default tokenizer.') SCREAMING_SNAKE_CASE = 5_0277 SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained('EleutherAI/gpt-neox-20b') else: SCREAMING_SNAKE_CASE = PreTrainedTokenizerFast(tokenizer_file=_UpperCAmelCase) SCREAMING_SNAKE_CASE = len(_UpperCAmelCase) tokenizer.save_pretrained(_UpperCAmelCase) # 2. Build the config SCREAMING_SNAKE_CASE = list(NUM_HIDDEN_LAYERS_MAPPING.keys()) if size is None: # Try to infer size from the checkpoint name for candidate in possible_sizes: if candidate in checkpoint_file: SCREAMING_SNAKE_CASE = candidate break if size is None: raise ValueError('Could not infer the size, please provide it with the `--size` argument.') if size not in possible_sizes: raise ValueError(F'''`size` should be one of {possible_sizes}, got {size}.''') SCREAMING_SNAKE_CASE = RwkvConfig( vocab_size=_UpperCAmelCase , num_hidden_layers=NUM_HIDDEN_LAYERS_MAPPING[size] , hidden_size=HIDEN_SIZE_MAPPING[size] , ) config.save_pretrained(_UpperCAmelCase) # 3. Download model file then convert state_dict SCREAMING_SNAKE_CASE = hf_hub_download(_UpperCAmelCase , _UpperCAmelCase) SCREAMING_SNAKE_CASE = torch.load(_UpperCAmelCase , map_location='cpu') SCREAMING_SNAKE_CASE = convert_state_dict(_UpperCAmelCase) # 4. Split in shards and save SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = shard_checkpoint(_UpperCAmelCase) for shard_file, shard in shards.items(): torch.save(_UpperCAmelCase , os.path.join(_UpperCAmelCase , _UpperCAmelCase)) if index is not None: SCREAMING_SNAKE_CASE = os.path.join(_UpperCAmelCase , _UpperCAmelCase) # Save the index as well with open(_UpperCAmelCase , 'w' , encoding='utf-8') as f: SCREAMING_SNAKE_CASE = json.dumps(_UpperCAmelCase , indent=2 , sort_keys=_UpperCAmelCase) + '\n' f.write(_UpperCAmelCase) # 5. Clean up shards (for some reason the file PyTorch saves take the same space as the whole state_dict print( 'Cleaning up shards. This may error with an OOM error, it this is the case don\'t worry you still have converted the model.') SCREAMING_SNAKE_CASE = list(shards.keys()) del state_dict del shards gc.collect() for shard_file in shard_files: SCREAMING_SNAKE_CASE = torch.load(os.path.join(_UpperCAmelCase , _UpperCAmelCase)) torch.save({k: v.cpu().clone() for k, v in state_dict.items()} , os.path.join(_UpperCAmelCase , _UpperCAmelCase)) del state_dict gc.collect() if push_to_hub: if model_name is None: raise ValueError('Please provide a `model_name` to push the model to the Hub.') SCREAMING_SNAKE_CASE = AutoModelForCausalLM.from_pretrained(_UpperCAmelCase) model.push_to_hub(_UpperCAmelCase , max_shard_size='2GB') tokenizer.push_to_hub(_UpperCAmelCase) if __name__ == "__main__": a_ : int = argparse.ArgumentParser() # Required parameters parser.add_argument( '--repo_id', default=None, type=str, required=True, help='Repo ID from which to pull the checkpoint.' ) parser.add_argument( '--checkpoint_file', default=None, type=str, required=True, help='Name of the checkpoint file in the repo.' ) parser.add_argument( '--output_dir', default=None, type=str, required=True, help='Where to save the converted model.' ) parser.add_argument( '--tokenizer_file', default=None, type=str, help='Path to the tokenizer file to use (if not provided, only the model is converted).', ) parser.add_argument( '--size', default=None, type=str, help='Size of the model. Will be inferred from the `checkpoint_file` if not passed.', ) parser.add_argument( '--push_to_hub', action='store_true', help='Push to the Hub the converted model.', ) parser.add_argument( '--model_name', default=None, type=str, help='Name of the pushed model on the Hub, including the username / organization.', ) a_ : Tuple = parser.parse_args() convert_rmkv_checkpoint_to_hf_format( args.repo_id, args.checkpoint_file, args.output_dir, size=args.size, tokenizer_file=args.tokenizer_file, push_to_hub=args.push_to_hub, model_name=args.model_name, )
327
1
"""simple docstring""" import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation __lowercase = logging.get_logger(__name__) __lowercase = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''} __lowercase = { '''tokenizer_file''': { '''EleutherAI/gpt-neox-20b''': '''https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/tokenizer.json''', }, } __lowercase = { '''gpt-neox-20b''': 2048, } class _A ( _a ): """simple docstring""" UpperCAmelCase : List[Any] = VOCAB_FILES_NAMES UpperCAmelCase : Dict = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase : Union[str, Any] = ["input_ids", "attention_mask"] def __init__( self : Union[str, Any] , __UpperCAmelCase : int=None , __UpperCAmelCase : List[str]=None , __UpperCAmelCase : str=None , __UpperCAmelCase : Union[str, Any]="<|endoftext|>" , __UpperCAmelCase : Optional[Any]="<|endoftext|>" , __UpperCAmelCase : List[str]="<|endoftext|>" , __UpperCAmelCase : List[Any]=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__ , ) a : Union[str, Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()) if pre_tok_state.get("add_prefix_space" , UpperCAmelCase__) != add_prefix_space: a : List[str] = getattr(UpperCAmelCase__ , pre_tok_state.pop("type")) a : List[str] = add_prefix_space a : Optional[int] = pre_tok_class(**UpperCAmelCase__) a : List[Any] = add_prefix_space def __snake_case ( self : Optional[int] , __UpperCAmelCase : str , __UpperCAmelCase : Optional[str] = None): a : Union[str, Any] = self._tokenizer.model.save(UpperCAmelCase__ , name=UpperCAmelCase__) return tuple(UpperCAmelCase__) def __snake_case ( self : Optional[int] , __UpperCAmelCase : "Conversation"): a : Dict = [] 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: a : Tuple = input_ids[-self.model_max_length :] return input_ids
40
"""simple docstring""" from __future__ import annotations import pandas as pd def UpperCAmelCase__ (lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ): '''simple docstring''' __SCREAMING_SNAKE_CASE = [0] * no_of_processes __SCREAMING_SNAKE_CASE = [0] * no_of_processes # Copy the burst time into remaining_time[] for i in range(lowerCAmelCase_ ): __SCREAMING_SNAKE_CASE = burst_time[i] __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 9_9999_9999 __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = False # Process until all processes are completed while complete != no_of_processes: for j in range(lowerCAmelCase_ ): if arrival_time[j] <= increment_time and remaining_time[j] > 0: if remaining_time[j] < minm: __SCREAMING_SNAKE_CASE = remaining_time[j] __SCREAMING_SNAKE_CASE = j __SCREAMING_SNAKE_CASE = True if not check: increment_time += 1 continue remaining_time[short] -= 1 __SCREAMING_SNAKE_CASE = remaining_time[short] if minm == 0: __SCREAMING_SNAKE_CASE = 9_9999_9999 if remaining_time[short] == 0: complete += 1 __SCREAMING_SNAKE_CASE = False # Find finish time of current process __SCREAMING_SNAKE_CASE = increment_time + 1 # Calculate waiting time __SCREAMING_SNAKE_CASE = finish_time - arrival_time[short] __SCREAMING_SNAKE_CASE = finar - burst_time[short] if waiting_time[short] < 0: __SCREAMING_SNAKE_CASE = 0 # Increment time increment_time += 1 return waiting_time def UpperCAmelCase__ (lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ): '''simple docstring''' __SCREAMING_SNAKE_CASE = [0] * no_of_processes for i in range(lowerCAmelCase_ ): __SCREAMING_SNAKE_CASE = burst_time[i] + waiting_time[i] return turn_around_time def UpperCAmelCase__ (lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ): '''simple docstring''' __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 0 for i in range(lowerCAmelCase_ ): __SCREAMING_SNAKE_CASE = total_waiting_time + waiting_time[i] __SCREAMING_SNAKE_CASE = total_turn_around_time + turn_around_time[i] print(f"""Average waiting time = {total_waiting_time / no_of_processes:.5f}""" ) print("Average turn around time =" , total_turn_around_time / no_of_processes ) if __name__ == "__main__": print('''Enter how many process you want to analyze''') a__ : Optional[Any] = int(input()) a__ : Optional[int] = [0] * no_of_processes a__ : int = [0] * no_of_processes a__ : List[Any] = list(range(1, no_of_processes + 1)) for i in range(no_of_processes): print('''Enter the arrival time and burst time for process:--''' + str(i + 1)) a__ , a__ : Tuple = map(int, input().split()) a__ : int = calculate_waitingtime(arrival_time, burst_time, no_of_processes) a__ : Dict = burst_time a__ : Any = no_of_processes a__ : Optional[int] = waiting_time a__ : Union[str, Any] = calculate_turnaroundtime(bt, n, wt) calculate_average_times(waiting_time, turn_around_time, no_of_processes) a__ : str = pd.DataFrame( list(zip(processes, burst_time, arrival_time, waiting_time, turn_around_time)), columns=[ '''Process''', '''BurstTime''', '''ArrivalTime''', '''WaitingTime''', '''TurnAroundTime''', ], ) # Printing the dataFrame pd.set_option('''display.max_rows''', fcfs.shape[0] + 1) print(fcfs)
54
0
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_pegasus import PegasusTokenizer else: __magic_name__: Dict = None __magic_name__: List[Any] = logging.get_logger(__name__) __magic_name__: Union[str, Any] = "▁" __magic_name__: Optional[int] = {"vocab_file": "spiece.model", "tokenizer_file": "tokenizer.json"} __magic_name__: Any = { "vocab_file": {"google/pegasus-xsum": "https://huggingface.co/google/pegasus-xsum/resolve/main/spiece.model"}, "tokenizer_file": { "google/pegasus-xsum": "https://huggingface.co/google/pegasus-xsum/resolve/main/tokenizer.json" }, } __magic_name__: int = { "google/pegasus-xsum": 512, } class snake_case__ ( _lowerCAmelCase ): lowercase__ : List[Any] = VOCAB_FILES_NAMES lowercase__ : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP lowercase__ : Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase__ : str = PegasusTokenizer lowercase__ : Optional[int] = ['''input_ids''', '''attention_mask'''] def __init__( self , lowerCAmelCase__=None , lowerCAmelCase__=None , lowerCAmelCase__="<pad>" , lowerCAmelCase__="</s>" , lowerCAmelCase__="<unk>" , lowerCAmelCase__="<mask_2>" , lowerCAmelCase__="<mask_1>" , lowerCAmelCase__=None , lowerCAmelCase__=1_03 , **lowerCAmelCase__ , ) -> Union[str, Any]: __magic_name__ : Optional[Any] = offset if additional_special_tokens is not None: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): raise TypeError( F'additional_special_tokens should be of type {type(lowerCAmelCase__ )}, but is' F' {type(lowerCAmelCase__ )}' ) __magic_name__ : Optional[Any] = ( ([mask_token_sent] + additional_special_tokens) if mask_token_sent not in additional_special_tokens and mask_token_sent is not None else additional_special_tokens ) # fill additional tokens with ..., <unk_token_102> in case not all additional tokens are already taken additional_special_tokens_extended += [ F'<unk_{i}>' for i in range(len(lowerCAmelCase__ ) , self.offset - 1 ) ] if len(set(lowerCAmelCase__ ) ) != len(lowerCAmelCase__ ): raise ValueError( """Please make sure that the provided additional_special_tokens do not contain an incorrectly""" F' shifted list of <unk_x> tokens. Found {additional_special_tokens_extended}.' ) __magic_name__ : Union[str, Any] = additional_special_tokens_extended else: __magic_name__ : Any = [mask_token_sent] if mask_token_sent is not None else [] additional_special_tokens += [F'<unk_{i}>' for i in range(2 , self.offset )] super().__init__( lowerCAmelCase__ , tokenizer_file=lowerCAmelCase__ , pad_token=lowerCAmelCase__ , eos_token=lowerCAmelCase__ , unk_token=lowerCAmelCase__ , mask_token=lowerCAmelCase__ , mask_token_sent=lowerCAmelCase__ , offset=lowerCAmelCase__ , additional_special_tokens=lowerCAmelCase__ , **lowerCAmelCase__ , ) __magic_name__ : Any = vocab_file __magic_name__ : Tuple = False if not self.vocab_file else True def __magic_name__ ( self , lowerCAmelCase__ ) -> List[str]: __magic_name__ : Union[str, Any] = set(self.all_special_ids ) # call it once instead of inside list comp all_special_ids.remove(self.unk_token_id ) # <unk> is only sometimes special if all_special_ids != set(range(len(self.additional_special_tokens ) + 3 ) ): raise ValueError( """There should be 3 special tokens: mask_token, pad_token, and eos_token +""" F' {len(self.additional_special_tokens )} additional_special_tokens, but got {all_special_ids}' ) return [1 if x in all_special_ids else 0 for x in seq] def __magic_name__ ( self , lowerCAmelCase__ , lowerCAmelCase__ = None , lowerCAmelCase__ = False ) -> List[int]: if already_has_special_tokens: return self._special_token_mask(lowerCAmelCase__ ) elif token_ids_a is None: return self._special_token_mask(lowerCAmelCase__ ) + [1] else: return self._special_token_mask(token_ids_a + token_ids_a ) + [1] def __magic_name__ ( self , lowerCAmelCase__ , lowerCAmelCase__=None ) -> List[int]: if token_ids_a is None: return token_ids_a + [self.eos_token_id] # We don't expect to process pairs, but leave the pair logic for API consistency return token_ids_a + token_ids_a + [self.eos_token_id] def __magic_name__ ( self , lowerCAmelCase__ , lowerCAmelCase__ = None ) -> Tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """ """tokenizer.""" ) if not os.path.isdir(lowerCAmelCase__ ): logger.error(F'Vocabulary path ({save_directory}) should be a directory' ) return __magic_name__ : Optional[int] = 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,)
363
import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class snake_case__ ( unittest.TestCase ): @slow def __magic_name__ ( self ) -> Optional[Any]: __magic_name__ : Any = FlaxMTaForConditionalGeneration.from_pretrained("""google/mt5-small""" ) __magic_name__ : Dict = AutoTokenizer.from_pretrained("""google/mt5-small""" ) __magic_name__ : Tuple = tokenizer("""Hello there""" , return_tensors="""np""" ).input_ids __magic_name__ : List[Any] = tokenizer("""Hi I am""" , return_tensors="""np""" ).input_ids __magic_name__ : Any = shift_tokens_right(lowerCAmelCase__ , model.config.pad_token_id , model.config.decoder_start_token_id ) __magic_name__ : List[Any] = model(lowerCAmelCase__ , decoder_input_ids=lowerCAmelCase__ ).logits __magic_name__ : Tuple = optax.softmax_cross_entropy(lowerCAmelCase__ , onehot(lowerCAmelCase__ , logits.shape[-1] ) ).mean() __magic_name__ : List[Any] = -(labels.shape[-1] * loss.item()) __magic_name__ : List[Any] = -8_4.9_1_2_7 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1e-4 )
138
0
def __lowerCamelCase ( UpperCamelCase__ ): '''simple docstring''' try: snake_case_ = float(_A ) except ValueError: raise ValueError('Please enter a valid number' ) snake_case_ = decimal - int(_A ) if fractional_part == 0: return int(_A ), 1 else: snake_case_ = len(str(_A ).split('.' )[1] ) snake_case_ = int(decimal * (10**number_of_frac_digits) ) snake_case_ = 10**number_of_frac_digits snake_case_ = denominator, numerator while True: snake_case_ = dividend % divisor if remainder == 0: break snake_case_ = divisor, remainder snake_case_ = numerator / divisor, denominator / divisor return int(_A ), int(_A ) if __name__ == "__main__": print(F'''{decimal_to_fraction(2) = }''') print(F'''{decimal_to_fraction(89.0) = }''') print(F'''{decimal_to_fraction("67") = }''') print(F'''{decimal_to_fraction("45.0") = }''') print(F'''{decimal_to_fraction(1.5) = }''') print(F'''{decimal_to_fraction("6.25") = }''') print(F'''{decimal_to_fraction("78td") = }''')
285
from ...configuration_utils import PretrainedConfig from ...utils import logging A : str = logging.get_logger(__name__) A : int = { "alibaba-damo/mgp-str-base": "https://huggingface.co/alibaba-damo/mgp-str-base/resolve/main/config.json", } class _lowercase ( lowercase__): """simple docstring""" A__ = "mgp-str" def __init__( self : List[str] , __lowerCamelCase : List[Any]=[32, 128] , __lowerCamelCase : Optional[int]=4 , __lowerCamelCase : str=3 , __lowerCamelCase : Optional[Any]=27 , __lowerCamelCase : List[str]=38 , __lowerCamelCase : Dict=50257 , __lowerCamelCase : List[Any]=30522 , __lowerCamelCase : Optional[Any]=768 , __lowerCamelCase : str=12 , __lowerCamelCase : Optional[int]=12 , __lowerCamelCase : List[str]=4.0 , __lowerCamelCase : Union[str, Any]=True , __lowerCamelCase : Any=False , __lowerCamelCase : Dict=1E-5 , __lowerCamelCase : int=0.0 , __lowerCamelCase : Optional[int]=0.0 , __lowerCamelCase : List[Any]=0.0 , __lowerCamelCase : Union[str, Any]=False , __lowerCamelCase : str=0.0_2 , **__lowerCamelCase : Dict , ): '''simple docstring''' super().__init__(**__lowerCamelCase ) lowerCamelCase__ : int = image_size lowerCamelCase__ : Union[str, Any] = patch_size lowerCamelCase__ : Dict = num_channels lowerCamelCase__ : Union[str, Any] = max_token_length lowerCamelCase__ : Optional[int] = num_character_labels lowerCamelCase__ : Union[str, Any] = num_bpe_labels lowerCamelCase__ : Optional[int] = num_wordpiece_labels lowerCamelCase__ : List[str] = hidden_size lowerCamelCase__ : Dict = num_hidden_layers lowerCamelCase__ : Any = num_attention_heads lowerCamelCase__ : Any = mlp_ratio lowerCamelCase__ : List[Any] = distilled lowerCamelCase__ : Optional[Any] = layer_norm_eps lowerCamelCase__ : Union[str, Any] = drop_rate lowerCamelCase__ : List[Any] = qkv_bias lowerCamelCase__ : int = attn_drop_rate lowerCamelCase__ : List[Any] = drop_path_rate lowerCamelCase__ : List[str] = output_aa_attentions lowerCamelCase__ : Dict = initializer_range
184
0
"""simple docstring""" from __future__ import annotations def SCREAMING_SNAKE_CASE__ ( snake_case : list[int] )-> list[int]: # This function is recursive '''simple docstring''' UpperCAmelCase__ : Tuple = len(snake_case ) # If the array contains only one element, we return it (it's the stop condition of # recursion) if array_length <= 1: return array # Else UpperCAmelCase__ : List[Any] = array[0] UpperCAmelCase__ : List[str] = False UpperCAmelCase__ : Optional[Any] = 1 UpperCAmelCase__ : list[int] = [] while not is_found and i < array_length: if array[i] < pivot: UpperCAmelCase__ : List[Any] = True UpperCAmelCase__ : List[Any] = [element for element in array[i:] if element >= array[i]] UpperCAmelCase__ : Any = longest_subsequence(snake_case ) if len(snake_case ) > len(snake_case ): UpperCAmelCase__ : Optional[Any] = temp_array else: i += 1 UpperCAmelCase__ : str = [element for element in array[1:] if element >= pivot] UpperCAmelCase__ : List[Any] = [pivot, *longest_subsequence(snake_case )] if len(snake_case ) > len(snake_case ): return temp_array else: return longest_subseq if __name__ == "__main__": import doctest doctest.testmod()
357
"""simple docstring""" # DISCLAIMER: This file is strongly influenced by https://github.com/ermongroup/ddim from dataclasses import dataclass from typing import Optional, Tuple, Union import flax import jax import jax.numpy as jnp from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils_flax import ( CommonSchedulerState, FlaxKarrasDiffusionSchedulers, FlaxSchedulerMixin, FlaxSchedulerOutput, add_noise_common, get_velocity_common, ) @flax.struct.dataclass class lowerCAmelCase__ : SCREAMING_SNAKE_CASE_ =42 # setable values SCREAMING_SNAKE_CASE_ =42 SCREAMING_SNAKE_CASE_ =42 SCREAMING_SNAKE_CASE_ =None @classmethod def __a ( cls : Optional[int] , snake_case__ : CommonSchedulerState , snake_case__ : jnp.ndarray , snake_case__ : jnp.ndarray ): '''simple docstring''' return cls(common=snake_case__ , init_noise_sigma=snake_case__ , timesteps=snake_case__ ) @dataclass class lowerCAmelCase__ ( __magic_name__ ): SCREAMING_SNAKE_CASE_ =42 class lowerCAmelCase__ ( __magic_name__ , __magic_name__ ): SCREAMING_SNAKE_CASE_ =[e.name for e in FlaxKarrasDiffusionSchedulers] SCREAMING_SNAKE_CASE_ =42 @property def __a ( self : Union[str, Any] ): '''simple docstring''' return True @register_to_config def __init__( self : Tuple , snake_case__ : int = 1_0_0_0 , snake_case__ : float = 0.0001 , snake_case__ : float = 0.02 , snake_case__ : str = "linear" , snake_case__ : Optional[jnp.ndarray] = None , snake_case__ : str = "fixed_small" , snake_case__ : bool = True , snake_case__ : str = "epsilon" , snake_case__ : jnp.dtype = jnp.floataa , ): '''simple docstring''' UpperCAmelCase__ : Tuple = dtype def __a ( self : Any , snake_case__ : Optional[CommonSchedulerState] = None ): '''simple docstring''' if common is None: UpperCAmelCase__ : Any = CommonSchedulerState.create(self ) # standard deviation of the initial noise distribution UpperCAmelCase__ : Tuple = jnp.array(1.0 , dtype=self.dtype ) UpperCAmelCase__ : Optional[Any] = jnp.arange(0 , self.config.num_train_timesteps ).round()[::-1] return DDPMSchedulerState.create( common=snake_case__ , init_noise_sigma=snake_case__ , timesteps=snake_case__ , ) def __a ( self : int , snake_case__ : DDPMSchedulerState , snake_case__ : jnp.ndarray , snake_case__ : Optional[int] = None ): '''simple docstring''' return sample def __a ( self : Dict , snake_case__ : DDPMSchedulerState , snake_case__ : int , snake_case__ : Tuple = () ): '''simple docstring''' UpperCAmelCase__ : Union[str, Any] = self.config.num_train_timesteps // num_inference_steps # creates integer timesteps by multiplying by ratio # rounding to avoid issues when num_inference_step is power of 3 UpperCAmelCase__ : Tuple = (jnp.arange(0 , snake_case__ ) * step_ratio).round()[::-1] return state.replace( num_inference_steps=snake_case__ , timesteps=snake_case__ , ) def __a ( self : List[str] , snake_case__ : DDPMSchedulerState , snake_case__ : int , snake_case__ : Any=None , snake_case__ : Union[str, Any]=None ): '''simple docstring''' UpperCAmelCase__ : int = state.common.alphas_cumprod[t] UpperCAmelCase__ : Optional[int] = jnp.where(t > 0 , state.common.alphas_cumprod[t - 1] , jnp.array(1.0 , dtype=self.dtype ) ) # For t > 0, compute predicted variance βt (see formula (6) and (7) from https://arxiv.org/pdf/2006.11239.pdf) # and sample from it to get previous sample # x_{t-1} ~ N(pred_prev_sample, variance) == add variance to pred_sample UpperCAmelCase__ : int = (1 - alpha_prod_t_prev) / (1 - alpha_prod_t) * state.common.betas[t] if variance_type is None: UpperCAmelCase__ : Union[str, Any] = self.config.variance_type # hacks - were probably added for training stability if variance_type == "fixed_small": UpperCAmelCase__ : int = jnp.clip(snake_case__ , a_min=1e-20 ) # for rl-diffuser https://arxiv.org/abs/2205.09991 elif variance_type == "fixed_small_log": UpperCAmelCase__ : Union[str, Any] = jnp.log(jnp.clip(snake_case__ , a_min=1e-20 ) ) elif variance_type == "fixed_large": UpperCAmelCase__ : List[Any] = state.common.betas[t] elif variance_type == "fixed_large_log": # Glide max_log UpperCAmelCase__ : Optional[int] = jnp.log(state.common.betas[t] ) elif variance_type == "learned": return predicted_variance elif variance_type == "learned_range": UpperCAmelCase__ : List[str] = variance UpperCAmelCase__ : Optional[Any] = state.common.betas[t] UpperCAmelCase__ : Any = (predicted_variance + 1) / 2 UpperCAmelCase__ : Dict = frac * max_log + (1 - frac) * min_log return variance def __a ( self : Dict , snake_case__ : DDPMSchedulerState , snake_case__ : jnp.ndarray , snake_case__ : int , snake_case__ : jnp.ndarray , snake_case__ : Optional[jax.random.KeyArray] = None , snake_case__ : bool = True , ): '''simple docstring''' UpperCAmelCase__ : Optional[int] = timestep if key is None: UpperCAmelCase__ : Optional[int] = jax.random.PRNGKey(0 ) if model_output.shape[1] == sample.shape[1] * 2 and self.config.variance_type in ["learned", "learned_range"]: UpperCAmelCase__ , UpperCAmelCase__ : Union[str, Any] = jnp.split(snake_case__ , sample.shape[1] , axis=1 ) else: UpperCAmelCase__ : int = None # 1. compute alphas, betas UpperCAmelCase__ : Union[str, Any] = state.common.alphas_cumprod[t] UpperCAmelCase__ : Optional[int] = jnp.where(t > 0 , state.common.alphas_cumprod[t - 1] , jnp.array(1.0 , dtype=self.dtype ) ) UpperCAmelCase__ : List[str] = 1 - alpha_prod_t UpperCAmelCase__ : List[str] = 1 - alpha_prod_t_prev # 2. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (15) from https://arxiv.org/pdf/2006.11239.pdf if self.config.prediction_type == "epsilon": UpperCAmelCase__ : Optional[int] = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 elif self.config.prediction_type == "sample": UpperCAmelCase__ : List[Any] = model_output elif self.config.prediction_type == "v_prediction": UpperCAmelCase__ : int = (alpha_prod_t**0.5) * sample - (beta_prod_t**0.5) * model_output else: raise ValueError( f'prediction_type given as {self.config.prediction_type} must be one of `epsilon`, `sample` ' " for the FlaxDDPMScheduler." ) # 3. Clip "predicted x_0" if self.config.clip_sample: UpperCAmelCase__ : Optional[Any] = jnp.clip(snake_case__ , -1 , 1 ) # 4. Compute coefficients for pred_original_sample x_0 and current sample x_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf UpperCAmelCase__ : Union[str, Any] = (alpha_prod_t_prev ** 0.5 * state.common.betas[t]) / beta_prod_t UpperCAmelCase__ : Tuple = state.common.alphas[t] ** 0.5 * beta_prod_t_prev / beta_prod_t # 5. Compute predicted previous sample µ_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf UpperCAmelCase__ : Union[str, Any] = pred_original_sample_coeff * pred_original_sample + current_sample_coeff * sample # 6. Add noise def random_variance(): UpperCAmelCase__ : List[str] = jax.random.split(snake_case__ , num=1 ) UpperCAmelCase__ : List[str] = jax.random.normal(snake_case__ , shape=model_output.shape , dtype=self.dtype ) return (self._get_variance(snake_case__ , snake_case__ , predicted_variance=snake_case__ ) ** 0.5) * noise UpperCAmelCase__ : Optional[int] = jnp.where(t > 0 , random_variance() , jnp.zeros(model_output.shape , dtype=self.dtype ) ) UpperCAmelCase__ : Optional[Any] = pred_prev_sample + variance if not return_dict: return (pred_prev_sample, state) return FlaxDDPMSchedulerOutput(prev_sample=snake_case__ , state=snake_case__ ) def __a ( self : List[Any] , snake_case__ : DDPMSchedulerState , snake_case__ : jnp.ndarray , snake_case__ : jnp.ndarray , snake_case__ : jnp.ndarray , ): '''simple docstring''' return add_noise_common(state.common , snake_case__ , snake_case__ , snake_case__ ) def __a ( self : Optional[int] , snake_case__ : DDPMSchedulerState , snake_case__ : jnp.ndarray , snake_case__ : jnp.ndarray , snake_case__ : jnp.ndarray , ): '''simple docstring''' return get_velocity_common(state.common , snake_case__ , snake_case__ , snake_case__ ) def __len__( self : Union[str, Any] ): '''simple docstring''' return self.config.num_train_timesteps
298
0
import argparse import requests import torch from PIL import Image from torchvision.transforms import Compose, Normalize, Resize, ToTensor from transformers import SwinaSRConfig, SwinaSRForImageSuperResolution, SwinaSRImageProcessor def a ( lowerCamelCase_ ): '''simple docstring''' lowercase__ = SwinaSRConfig() if "Swin2SR_ClassicalSR_X4_64" in checkpoint_url: lowercase__ = 4 elif "Swin2SR_CompressedSR_X4_48" in checkpoint_url: lowercase__ = 4 lowercase__ = 48 lowercase__ = '''pixelshuffle_aux''' elif "Swin2SR_Lightweight_X2_64" in checkpoint_url: lowercase__ = [6, 6, 6, 6] lowercase__ = 60 lowercase__ = [6, 6, 6, 6] lowercase__ = '''pixelshuffledirect''' elif "Swin2SR_RealworldSR_X4_64_BSRGAN_PSNR" in checkpoint_url: lowercase__ = 4 lowercase__ = '''nearest+conv''' elif "Swin2SR_Jpeg_dynamic" in checkpoint_url: lowercase__ = 1 lowercase__ = 1 lowercase__ = 126 lowercase__ = 7 lowercase__ = 2_55.0 lowercase__ = '''''' return config def a ( lowerCamelCase_ , lowerCamelCase_ ): '''simple docstring''' if "patch_embed.proj" in name and "layers" not in name: lowercase__ = name.replace('''patch_embed.proj''' , '''embeddings.patch_embeddings.projection''' ) if "patch_embed.norm" in name: lowercase__ = name.replace('''patch_embed.norm''' , '''embeddings.patch_embeddings.layernorm''' ) if "layers" in name: lowercase__ = name.replace('''layers''' , '''encoder.stages''' ) if "residual_group.blocks" in name: lowercase__ = name.replace('''residual_group.blocks''' , '''layers''' ) if "attn.proj" in name: lowercase__ = name.replace('''attn.proj''' , '''attention.output.dense''' ) if "attn" in name: lowercase__ = name.replace('''attn''' , '''attention.self''' ) if "norm1" in name: lowercase__ = name.replace('''norm1''' , '''layernorm_before''' ) if "norm2" in name: lowercase__ = name.replace('''norm2''' , '''layernorm_after''' ) if "mlp.fc1" in name: lowercase__ = name.replace('''mlp.fc1''' , '''intermediate.dense''' ) if "mlp.fc2" in name: lowercase__ = name.replace('''mlp.fc2''' , '''output.dense''' ) if "q_bias" in name: lowercase__ = name.replace('''q_bias''' , '''query.bias''' ) if "k_bias" in name: lowercase__ = name.replace('''k_bias''' , '''key.bias''' ) if "v_bias" in name: lowercase__ = name.replace('''v_bias''' , '''value.bias''' ) if "cpb_mlp" in name: lowercase__ = name.replace('''cpb_mlp''' , '''continuous_position_bias_mlp''' ) if "patch_embed.proj" in name: lowercase__ = name.replace('''patch_embed.proj''' , '''patch_embed.projection''' ) if name == "norm.weight": lowercase__ = '''layernorm.weight''' if name == "norm.bias": lowercase__ = '''layernorm.bias''' if "conv_first" in name: lowercase__ = name.replace('''conv_first''' , '''first_convolution''' ) if ( "upsample" in name or "conv_before_upsample" in name or "conv_bicubic" in name or "conv_up" in name or "conv_hr" in name or "conv_last" in name or "aux" in name ): # heads if "conv_last" in name: lowercase__ = name.replace('''conv_last''' , '''final_convolution''' ) if config.upsampler in ["pixelshuffle", "pixelshuffle_aux", "nearest+conv"]: if "conv_before_upsample.0" in name: lowercase__ = name.replace('''conv_before_upsample.0''' , '''conv_before_upsample''' ) if "upsample.0" in name: lowercase__ = name.replace('''upsample.0''' , '''upsample.convolution_0''' ) if "upsample.2" in name: lowercase__ = name.replace('''upsample.2''' , '''upsample.convolution_1''' ) lowercase__ = '''upsample.''' + name elif config.upsampler == "pixelshuffledirect": lowercase__ = name.replace('''upsample.0.weight''' , '''upsample.conv.weight''' ) lowercase__ = name.replace('''upsample.0.bias''' , '''upsample.conv.bias''' ) else: pass else: lowercase__ = '''swin2sr.''' + name return name def a ( lowerCamelCase_ , lowerCamelCase_ ): '''simple docstring''' for key in orig_state_dict.copy().keys(): lowercase__ = orig_state_dict.pop(lowerCamelCase_ ) if "qkv" in key: lowercase__ = key.split('''.''' ) lowercase__ = int(key_split[1] ) lowercase__ = int(key_split[4] ) lowercase__ = config.embed_dim if "weight" in key: lowercase__ = val[:dim, :] lowercase__ = val[dim : dim * 2, :] lowercase__ = val[-dim:, :] else: lowercase__ = val[:dim] lowercase__ = val[dim : dim * 2] lowercase__ = val[-dim:] pass else: lowercase__ = val return orig_state_dict def a ( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ): '''simple docstring''' lowercase__ = get_config(lowerCamelCase_ ) lowercase__ = SwinaSRForImageSuperResolution(lowerCamelCase_ ) model.eval() lowercase__ = torch.hub.load_state_dict_from_url(lowerCamelCase_ , map_location='''cpu''' ) lowercase__ = convert_state_dict(lowerCamelCase_ , lowerCamelCase_ ) lowercase__ , lowercase__ = model.load_state_dict(lowerCamelCase_ , strict=lowerCamelCase_ ) if len(lowerCamelCase_ ) > 0: raise ValueError('''Missing keys when converting: {}'''.format(lowerCamelCase_ ) ) for key in unexpected_keys: if not ("relative_position_index" in key or "relative_coords_table" in key or "self_mask" in key): raise ValueError(F"""Unexpected key {key} in state_dict""" ) # verify values lowercase__ = '''https://github.com/mv-lab/swin2sr/blob/main/testsets/real-inputs/shanghai.jpg?raw=true''' lowercase__ = Image.open(requests.get(lowerCamelCase_ , stream=lowerCamelCase_ ).raw ).convert('''RGB''' ) lowercase__ = SwinaSRImageProcessor() # pixel_values = processor(image, return_tensors="pt").pixel_values lowercase__ = 126 if '''Jpeg''' in checkpoint_url else 256 lowercase__ = Compose( [ Resize((image_size, image_size) ), ToTensor(), Normalize(mean=[0.4_85, 0.4_56, 0.4_06] , std=[0.2_29, 0.2_24, 0.2_25] ), ] ) lowercase__ = transforms(lowerCamelCase_ ).unsqueeze(0 ) if config.num_channels == 1: lowercase__ = pixel_values[:, 0, :, :].unsqueeze(1 ) lowercase__ = model(lowerCamelCase_ ) # assert values if "Swin2SR_ClassicalSR_X2_64" in checkpoint_url: lowercase__ = torch.Size([1, 3, 512, 512] ) lowercase__ = torch.tensor( [[-0.70_87, -0.71_38, -0.67_21], [-0.83_40, -0.80_95, -0.72_98], [-0.91_49, -0.84_14, -0.79_40]] ) elif "Swin2SR_ClassicalSR_X4_64" in checkpoint_url: lowercase__ = torch.Size([1, 3, 1024, 1024] ) lowercase__ = torch.tensor( [[-0.77_75, -0.81_05, -0.89_33], [-0.77_64, -0.83_56, -0.92_25], [-0.79_76, -0.86_86, -0.95_79]] ) elif "Swin2SR_CompressedSR_X4_48" in checkpoint_url: # TODO values didn't match exactly here lowercase__ = torch.Size([1, 3, 1024, 1024] ) lowercase__ = torch.tensor( [[-0.80_35, -0.75_04, -0.74_91], [-0.85_38, -0.81_24, -0.77_82], [-0.88_04, -0.86_51, -0.84_93]] ) elif "Swin2SR_Lightweight_X2_64" in checkpoint_url: lowercase__ = torch.Size([1, 3, 512, 512] ) lowercase__ = torch.tensor( [[-0.76_69, -0.86_62, -0.87_67], [-0.88_10, -0.99_62, -0.98_20], [-0.93_40, -1.03_22, -1.11_49]] ) elif "Swin2SR_RealworldSR_X4_64_BSRGAN_PSNR" in checkpoint_url: lowercase__ = torch.Size([1, 3, 1024, 1024] ) lowercase__ = torch.tensor( [[-0.52_38, -0.55_57, -0.63_21], [-0.60_16, -0.59_03, -0.63_91], [-0.62_44, -0.63_34, -0.68_89]] ) assert ( outputs.reconstruction.shape == expected_shape ), F"""Shape of reconstruction should be {expected_shape}, but is {outputs.reconstruction.shape}""" assert torch.allclose(outputs.reconstruction[0, 0, :3, :3] , lowerCamelCase_ , atol=1e-3 ) print('''Looks ok!''' ) lowercase__ = { '''https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_ClassicalSR_X2_64.pth''': ( '''swin2SR-classical-sr-x2-64''' ), '''https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_ClassicalSR_X4_64.pth''': ( '''swin2SR-classical-sr-x4-64''' ), '''https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_CompressedSR_X4_48.pth''': ( '''swin2SR-compressed-sr-x4-48''' ), '''https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_Lightweight_X2_64.pth''': ( '''swin2SR-lightweight-x2-64''' ), '''https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_RealworldSR_X4_64_BSRGAN_PSNR.pth''': ( '''swin2SR-realworld-sr-x4-64-bsrgan-psnr''' ), } lowercase__ = url_to_name[checkpoint_url] if pytorch_dump_folder_path is not None: print(F"""Saving model {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(lowerCamelCase_ ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) processor.save_pretrained(lowerCamelCase_ ) if push_to_hub: model.push_to_hub(F"""caidas/{model_name}""" ) processor.push_to_hub(F"""caidas/{model_name}""" ) if __name__ == "__main__": A__ : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--checkpoint_url', default='https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_ClassicalSR_X2_64.pth', type=str, help='URL of the original Swin2SR checkpoint you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.' ) parser.add_argument('--push_to_hub', action='store_true', help='Whether to push the converted model to the hub.') A__ : Any = parser.parse_args() convert_swinasr_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub)
207
from math import asin, atan, cos, radians, sin, sqrt, tan A__ : Optional[int] = 637_8137.0 A__ : List[str] = 635_6752.31_4245 A__ : Union[str, Any] = 6_37_81_37 def a ( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ): '''simple docstring''' lowercase__ = (AXIS_A - AXIS_B) / AXIS_A lowercase__ = atan((1 - flattening) * tan(radians(lowerCamelCase_ ) ) ) lowercase__ = atan((1 - flattening) * tan(radians(lowerCamelCase_ ) ) ) lowercase__ = radians(lowerCamelCase_ ) lowercase__ = radians(lowerCamelCase_ ) # Equation lowercase__ = sin((phi_a - phi_a) / 2 ) lowercase__ = sin((lambda_a - lambda_a) / 2 ) # Square both values sin_sq_phi *= sin_sq_phi sin_sq_lambda *= sin_sq_lambda lowercase__ = sqrt(sin_sq_phi + (cos(lowerCamelCase_ ) * cos(lowerCamelCase_ ) * sin_sq_lambda) ) return 2 * RADIUS * asin(lowerCamelCase_ ) if __name__ == "__main__": import doctest doctest.testmod()
207
1
import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, BatchEncoding, MBartaaTokenizer, MBartaaTokenizerFast, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, slow, ) from ...test_tokenization_common import TokenizerTesterMixin lowerCamelCase : Tuple = get_tests_dir("fixtures/test_sentencepiece.model") if is_torch_available(): from transformers.models.mbart.modeling_mbart import shift_tokens_right lowerCamelCase : Any = 250_004 lowerCamelCase : str = 250_020 @require_sentencepiece @require_tokenizers class A( UpperCamelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase = MBartaaTokenizer UpperCamelCase = MBartaaTokenizerFast UpperCamelCase = True UpperCamelCase = True def a__ ( self : Dict ) -> Optional[Any]: """simple docstring""" super().setUp() # We have a SentencePiece fixture for testing lowerCamelCase_ = MBartaaTokenizer(A_ , src_lang='en_XX' , tgt_lang='ro_RO' , keep_accents=A_ ) tokenizer.save_pretrained(self.tmpdirname ) def a__ ( self : int ) -> Any: """simple docstring""" lowerCamelCase_ = '<s>' lowerCamelCase_ = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(A_ ) , A_ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(A_ ) , A_ ) def a__ ( self : Optional[int] ) -> int: """simple docstring""" lowerCamelCase_ = 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_ ) , 1054 ) def a__ ( self : List[str] ) -> Dict: """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 1054 ) def a__ ( self : List[str] ) -> List[str]: """simple docstring""" lowerCamelCase_ = MBartaaTokenizer(A_ , src_lang='en_XX' , tgt_lang='ro_RO' , keep_accents=A_ ) lowerCamelCase_ = 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]] , ) lowerCamelCase_ = 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', 'é', '.'] , ) lowerCamelCase_ = 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] ] , ) lowerCamelCase_ = 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>', '.'] , ) @slow def a__ ( self : int ) -> Union[str, Any]: """simple docstring""" lowerCamelCase_ = {'input_ids': [[250004, 11062, 82772, 7, 15, 82772, 538, 51529, 237, 17198, 1290, 206, 9, 215175, 1314, 136, 17198, 1290, 206, 9, 56359, 42, 122009, 9, 16466, 16, 87344, 4537, 9, 4717, 78381, 6, 159958, 7, 15, 24480, 618, 4, 527, 22693, 5428, 4, 2777, 24480, 9874, 4, 43523, 594, 4, 803, 18392, 33189, 18, 4, 43523, 24447, 12399, 100, 24955, 83658, 9626, 144057, 15, 839, 22335, 16, 136, 24955, 83658, 83479, 15, 39102, 724, 16, 678, 645, 2789, 1328, 4589, 42, 122009, 115774, 23, 805, 1328, 46876, 7, 136, 53894, 1940, 42227, 41159, 17721, 823, 425, 4, 27512, 98722, 206, 136, 5531, 4970, 919, 17336, 5, 2], [250004, 20080, 618, 83, 82775, 47, 479, 9, 1517, 73, 53894, 333, 80581, 110117, 18811, 5256, 1295, 51, 152526, 297, 7986, 390, 124416, 538, 35431, 214, 98, 15044, 25737, 136, 7108, 43701, 23, 756, 135355, 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], [250004, 581, 63773, 119455, 6, 147797, 88203, 7, 645, 70, 21, 3285, 10269, 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='facebook/mbart-large-50' , revision='d3913889c59cd5c9e456b269c376325eabad57e2' , ) def a__ ( self : Union[str, Any] ) -> Dict: """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_ = (self.rust_tokenizer_class, 'hf-internal-testing/tiny-random-mbart50', {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): lowerCamelCase_ = self.rust_tokenizer_class.from_pretrained(A_ , **A_ ) lowerCamelCase_ = self.tokenizer_class.from_pretrained(A_ , **A_ ) lowerCamelCase_ = tempfile.mkdtemp() lowerCamelCase_ = tokenizer_r.save_pretrained(A_ ) lowerCamelCase_ = 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 ) ) lowerCamelCase_ = 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 lowerCamelCase_ = tokenizer_r.from_pretrained(A_ ) lowerCamelCase_ = 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 lowerCamelCase_ = tempfile.mkdtemp() lowerCamelCase_ = tokenizer_r.save_pretrained(A_ , legacy_format=A_ ) lowerCamelCase_ = tokenizer_p.save_pretrained(A_ ) # Checks it save with the same files self.assertSequenceEqual(A_ , A_ ) # Checks everything loads correctly in the same way lowerCamelCase_ = tokenizer_r.from_pretrained(A_ ) lowerCamelCase_ = 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 lowerCamelCase_ = tempfile.mkdtemp() lowerCamelCase_ = tokenizer_r.save_pretrained(A_ , legacy_format=A_ ) lowerCamelCase_ = 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 lowerCamelCase_ = tokenizer_r.from_pretrained(A_ ) lowerCamelCase_ = 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_ ) @require_torch @require_sentencepiece @require_tokenizers class A( unittest.TestCase ): '''simple docstring''' UpperCamelCase = '''facebook/mbart-large-50-one-to-many-mmt''' UpperCamelCase = [ ''' UN Chief Says There Is No Military Solution in Syria''', ''' Secretary-General Ban Ki-moon says his response to Russia\'s stepped up military support for Syria is that "there is no military solution" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.''', ] UpperCamelCase = [ '''Şeful ONU declară că nu există o soluţie militară în Siria''', '''Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei''' ''' pentru Siria este că "nu există o soluţie militară" la conflictul de aproape cinci ani şi că noi arme nu vor''' ''' face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.''', ] UpperCamelCase = [EN_CODE, 8274, 12_7873, 2_5916, 7, 8622, 2071, 438, 6_7485, 53, 18_7895, 23, 5_1712, 2] @classmethod def a__ ( cls : Tuple ) -> str: """simple docstring""" lowerCamelCase_ = MBartaaTokenizer.from_pretrained( cls.checkpoint_name , src_lang='en_XX' , tgt_lang='ro_RO' ) lowerCamelCase_ = 1 return cls def a__ ( self : Union[str, Any] ) -> Dict: """simple docstring""" self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['ar_AR'] , 250001 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['en_EN'] , 250004 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['ro_RO'] , 250020 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['mr_IN'] , 250038 ) def a__ ( self : int ) -> Dict: """simple docstring""" lowerCamelCase_ = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , A_ ) def a__ ( self : Dict ) -> Optional[int]: """simple docstring""" self.assertIn(A_ , self.tokenizer.all_special_ids ) lowerCamelCase_ = [RO_CODE, 884, 9019, 96, 9, 916, 86792, 36, 18743, 15596, 5, 2] lowerCamelCase_ = self.tokenizer.decode(A_ , skip_special_tokens=A_ ) lowerCamelCase_ = 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 ) -> str: """simple docstring""" lowerCamelCase_ = ['this is gunna be a long sentence ' * 20] assert isinstance(src_text[0] , A_ ) lowerCamelCase_ = 10 lowerCamelCase_ = self.tokenizer(A_ , max_length=A_ , truncation=A_ ).input_ids[0] self.assertEqual(ids[0] , A_ ) self.assertEqual(ids[-1] , 2 ) self.assertEqual(len(A_ ) , A_ ) def a__ ( self : Any ) -> str: """simple docstring""" self.assertListEqual(self.tokenizer.convert_tokens_to_ids(['<mask>', 'ar_AR'] ) , [250053, 250001] ) def a__ ( self : Any ) -> Union[str, Any]: """simple docstring""" lowerCamelCase_ = tempfile.mkdtemp() lowerCamelCase_ = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(A_ ) lowerCamelCase_ = MBartaaTokenizer.from_pretrained(A_ ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , A_ ) @require_torch def a__ ( self : Tuple ) -> Optional[int]: """simple docstring""" lowerCamelCase_ = self.tokenizer(self.src_text , text_target=self.tgt_text , padding=A_ , return_tensors='pt' ) lowerCamelCase_ = shift_tokens_right(batch['labels'] , self.tokenizer.pad_token_id ) # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 assert batch.input_ids[1][0] == EN_CODE assert batch.input_ids[1][-1] == 2 assert batch.labels[1][0] == RO_CODE assert batch.labels[1][-1] == 2 assert batch.decoder_input_ids[1][:2].tolist() == [2, RO_CODE] @require_torch def a__ ( self : Optional[int] ) -> Union[str, Any]: """simple docstring""" lowerCamelCase_ = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=A_ , truncation=A_ , max_length=len(self.expected_src_tokens ) , return_tensors='pt' , ) lowerCamelCase_ = shift_tokens_right(batch['labels'] , self.tokenizer.pad_token_id ) self.assertIsInstance(A_ , A_ ) self.assertEqual((2, 14) , batch.input_ids.shape ) self.assertEqual((2, 14) , batch.attention_mask.shape ) lowerCamelCase_ = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , A_ ) self.assertEqual(2 , batch.decoder_input_ids[0, 0] ) # decoder_start_token_id # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [EN_CODE] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) def a__ ( self : Optional[Any] ) -> Any: """simple docstring""" lowerCamelCase_ = self.tokenizer(self.src_text , padding=A_ , truncation=A_ , max_length=3 , return_tensors='pt' ) lowerCamelCase_ = self.tokenizer( text_target=self.tgt_text , padding=A_ , truncation=A_ , max_length=10 , return_tensors='pt' ) lowerCamelCase_ = targets['input_ids'] lowerCamelCase_ = 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 : List[str] ) -> Union[str, Any]: """simple docstring""" lowerCamelCase_ = self.tokenizer._build_translation_inputs( 'A test' , return_tensors='pt' , src_lang='en_XX' , tgt_lang='ar_AR' ) self.assertEqual( nested_simplify(A_ ) , { # en_XX, A, test, EOS 'input_ids': [[250004, 62, 3034, 2]], 'attention_mask': [[1, 1, 1, 1]], # ar_AR 'forced_bos_token_id': 250001, } , )
367
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: lowerCamelCase : Optional[Any] = False lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) lowerCamelCase : Any = "ybelkada/fonts" def _SCREAMING_SNAKE_CASE ( ): '''simple docstring''' 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 _SCREAMING_SNAKE_CASE ( lowercase : Dict , lowercase : List[str] , lowercase : List[Any] ): '''simple docstring''' requires_backends(lowercase , ['torch'] ) _check_torch_version() lowerCamelCase_ = image_tensor.unsqueeze(0 ) lowerCamelCase_ = torch.nn.functional.unfold(lowercase , (patch_height, patch_width) , stride=(patch_height, patch_width) ) lowerCamelCase_ = patches.reshape(image_tensor.size(0 ) , image_tensor.size(1 ) , lowercase , lowercase , -1 ) lowerCamelCase_ = 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 _SCREAMING_SNAKE_CASE ( lowercase : str , lowercase : int = 36 , lowercase : str = "black" , lowercase : str = "white" , lowercase : int = 5 , lowercase : int = 5 , lowercase : int = 5 , lowercase : int = 5 , lowercase : Optional[bytes] = None , lowercase : Optional[str] = None , ): '''simple docstring''' requires_backends(lowercase , 'vision' ) # Add new lines so that each line is no more than 80 characters. lowerCamelCase_ = textwrap.TextWrapper(width=80 ) lowerCamelCase_ = wrapper.wrap(text=lowercase ) lowerCamelCase_ = '\n'.join(lowercase ) if font_bytes is not None and font_path is None: lowerCamelCase_ = io.BytesIO(lowercase ) elif font_path is not None: lowerCamelCase_ = font_path else: lowerCamelCase_ = hf_hub_download(lowercase , 'Arial.TTF' ) lowerCamelCase_ = ImageFont.truetype(lowercase , encoding='UTF-8' , size=lowercase ) # Use a temporary canvas to determine the width and height in pixels when # rendering the text. lowerCamelCase_ = ImageDraw.Draw(Image.new('RGB' , (1, 1) , lowercase ) ) lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ = temp_draw.textbbox((0, 0) , lowercase , lowercase ) # Create the actual image with a bit of padding around the text. lowerCamelCase_ = text_width + left_padding + right_padding lowerCamelCase_ = text_height + top_padding + bottom_padding lowerCamelCase_ = Image.new('RGB' , (image_width, image_height) , lowercase ) lowerCamelCase_ = ImageDraw.Draw(lowercase ) draw.text(xy=(left_padding, top_padding) , text=lowercase , fill=lowercase , font=lowercase ) return image def _SCREAMING_SNAKE_CASE ( lowercase : np.ndarray , lowercase : str , **lowercase : List[Any] ): '''simple docstring''' requires_backends(lowercase , 'vision' ) # Convert to PIL image if necessary lowerCamelCase_ = to_pil_image(lowercase ) lowerCamelCase_ = render_text(lowercase , **lowercase ) lowerCamelCase_ = max(header_image.width , image.width ) lowerCamelCase_ = int(image.height * (new_width / image.width) ) lowerCamelCase_ = int(header_image.height * (new_width / header_image.width) ) lowerCamelCase_ = 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 lowerCamelCase_ = to_numpy_array(lowercase ) if infer_channel_dimension_format(lowercase ) == ChannelDimension.LAST: lowerCamelCase_ = to_channel_dimension_format(lowercase , ChannelDimension.LAST ) return new_image class A( UpperCamelCase ): '''simple docstring''' UpperCamelCase = ['''flattened_patches'''] def __init__( self : Dict , A_ : bool = True , A_ : bool = True , A_ : Dict[str, int] = None , A_ : int = 2048 , A_ : bool = False , **A_ : str , ) -> None: """simple docstring""" super().__init__(**A_ ) lowerCamelCase_ = patch_size if patch_size is not None else {'height': 16, 'width': 16} lowerCamelCase_ = do_normalize lowerCamelCase_ = do_convert_rgb lowerCamelCase_ = max_patches lowerCamelCase_ = is_vqa def a__ ( self : Union[str, Any] , A_ : np.ndarray , A_ : int , A_ : dict , **A_ : Any ) -> np.ndarray: """simple docstring""" requires_backends(self.extract_flattened_patches , 'torch' ) _check_torch_version() # convert to torch lowerCamelCase_ = to_channel_dimension_format(A_ , ChannelDimension.FIRST ) lowerCamelCase_ = torch.from_numpy(A_ ) lowerCamelCase_ , lowerCamelCase_ = patch_size['height'], patch_size['width'] lowerCamelCase_ , lowerCamelCase_ = get_image_size(A_ ) # maximize scale s.t. lowerCamelCase_ = math.sqrt(max_patches * (patch_height / image_height) * (patch_width / image_width) ) lowerCamelCase_ = max(min(math.floor(scale * image_height / patch_height ) , A_ ) , 1 ) lowerCamelCase_ = max(min(math.floor(scale * image_width / patch_width ) , A_ ) , 1 ) lowerCamelCase_ = max(num_feasible_rows * patch_height , 1 ) lowerCamelCase_ = max(num_feasible_cols * patch_width , 1 ) lowerCamelCase_ = torch.nn.functional.interpolate( image.unsqueeze(0 ) , size=(resized_height, resized_width) , mode='bilinear' , align_corners=A_ , antialias=A_ , ).squeeze(0 ) # [1, rows, columns, patch_height * patch_width * image_channels] lowerCamelCase_ = torch_extract_patches(A_ , A_ , A_ ) lowerCamelCase_ = patches.shape lowerCamelCase_ = patches_shape[1] lowerCamelCase_ = patches_shape[2] lowerCamelCase_ = patches_shape[3] # [rows * columns, patch_height * patch_width * image_channels] lowerCamelCase_ = patches.reshape([rows * columns, depth] ) # [rows * columns, 1] lowerCamelCase_ = torch.arange(A_ ).reshape([rows, 1] ).repeat(1 , A_ ).reshape([rows * columns, 1] ) lowerCamelCase_ = torch.arange(A_ ).reshape([1, columns] ).repeat(A_ , 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] lowerCamelCase_ = row_ids.to(torch.floataa ) lowerCamelCase_ = col_ids.to(torch.floataa ) # [rows * columns, 2 + patch_height * patch_width * image_channels] lowerCamelCase_ = torch.cat([row_ids, col_ids, patches] , -1 ) # [max_patches, 2 + patch_height * patch_width * image_channels] lowerCamelCase_ = torch.nn.functional.pad(A_ , [0, 0, 0, max_patches - (rows * columns)] ).float() lowerCamelCase_ = to_numpy_array(A_ ) return result def a__ ( self : Optional[Any] , A_ : np.ndarray , A_ : Optional[Union[str, ChannelDimension]] = None , **A_ : str ) -> np.ndarray: """simple docstring""" if image.dtype == np.uinta: lowerCamelCase_ = image.astype(np.floataa ) # take mean across the whole `image` lowerCamelCase_ = np.mean(A_ ) lowerCamelCase_ = np.std(A_ ) lowerCamelCase_ = max(A_ , 1.0 / math.sqrt(np.prod(image.shape ) ) ) return normalize(A_ , mean=A_ , std=A_ , **A_ ) def a__ ( self : Optional[Any] , A_ : ImageInput , A_ : Optional[str] = None , A_ : bool = None , A_ : Optional[bool] = None , A_ : Optional[int] = None , A_ : Optional[Dict[str, int]] = None , A_ : Optional[Union[str, TensorType]] = None , A_ : ChannelDimension = ChannelDimension.FIRST , **A_ : Optional[int] , ) -> ImageInput: """simple docstring""" lowerCamelCase_ = do_normalize if do_normalize is not None else self.do_normalize lowerCamelCase_ = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb lowerCamelCase_ = patch_size if patch_size is not None else self.patch_size lowerCamelCase_ = max_patches if max_patches is not None else self.max_patches lowerCamelCase_ = self.is_vqa if kwargs.get('data_format' , A_ ) is not None: raise ValueError('data_format is not an accepted input as the outputs are ' ) lowerCamelCase_ = make_list_of_images(A_ ) if not valid_images(A_ ): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.' ) # PIL RGBA images are converted to RGB if do_convert_rgb: lowerCamelCase_ = [convert_to_rgb(A_ ) for image in images] # All transformations expect numpy arrays. lowerCamelCase_ = [to_numpy_array(A_ ) for image in images] if is_vqa: if header_text is None: raise ValueError('A header text must be provided for VQA models.' ) lowerCamelCase_ = kwargs.pop('font_bytes' , A_ ) lowerCamelCase_ = kwargs.pop('font_path' , A_ ) if isinstance(A_ , A_ ): lowerCamelCase_ = [header_text] * len(A_ ) lowerCamelCase_ = [ render_header(A_ , header_text[i] , font_bytes=A_ , font_path=A_ ) for i, image in enumerate(A_ ) ] if do_normalize: lowerCamelCase_ = [self.normalize(image=A_ ) for image in images] # convert to torch tensor and permute lowerCamelCase_ = [ self.extract_flattened_patches(image=A_ , max_patches=A_ , patch_size=A_ ) for image in images ] # create attention mask in numpy lowerCamelCase_ = [(image.sum(axis=-1 ) != 0).astype(np.floataa ) for image in images] lowerCamelCase_ = BatchFeature( data={'flattened_patches': images, 'attention_mask': attention_masks} , tensor_type=A_ ) return encoded_outputs
208
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase__ = { """configuration_instructblip""": [ """INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP""", """InstructBlipConfig""", """InstructBlipQFormerConfig""", """InstructBlipVisionConfig""", ], """processing_instructblip""": ["""InstructBlipProcessor"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ """INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST""", """InstructBlipQFormerModel""", """InstructBlipPreTrainedModel""", """InstructBlipForConditionalGeneration""", """InstructBlipVisionModel""", ] if TYPE_CHECKING: from .configuration_instructblip import ( INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, InstructBlipConfig, InstructBlipQFormerConfig, InstructBlipVisionConfig, ) from .processing_instructblip import InstructBlipProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_instructblip import ( INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST, InstructBlipForConditionalGeneration, InstructBlipPreTrainedModel, InstructBlipQFormerModel, InstructBlipVisionModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
68
'''simple docstring''' def __lowercase ( __lowercase = 100 ) -> int: '''simple docstring''' _A = n * (n + 1) * (2 * n + 1) / 6 _A = (n * (n + 1) / 2) ** 2 return int(square_of_sum - sum_of_squares ) if __name__ == "__main__": print(F"""{solution() = }""")
79
0
'''simple docstring''' # tests directory-specific settings - this file is run automatically # by pytest before any tests are run import sys import warnings from os.path import abspath, dirname, join # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. __snake_case = abspath(join(dirname(dirname(__file__)), '''src''')) sys.path.insert(1, git_repo_path) # silence FutureWarning warnings in tests since often we can't act on them until # they become normal warnings - i.e. the tests still need to test the current functionality warnings.simplefilter(action='''ignore''', category=FutureWarning) def a ( __a ) -> Optional[int]: '''simple docstring''' from diffusers.utils.testing_utils import pytest_addoption_shared pytest_addoption_shared(__a ) def a ( __a ) -> str: '''simple docstring''' from diffusers.utils.testing_utils import pytest_terminal_summary_main UpperCamelCase__ :Union[str, Any] = terminalreporter.config.getoption('''--make-reports''' ) if make_reports: pytest_terminal_summary_main(__a , id=__a )
219
'''simple docstring''' from argparse import ArgumentParser from .env import EnvironmentCommand def a ( ) -> Union[str, Any]: '''simple docstring''' UpperCamelCase__ :Union[str, Any] = ArgumentParser('''Diffusers CLI tool''' , usage='''diffusers-cli <command> [<args>]''' ) UpperCamelCase__ :Union[str, Any] = parser.add_subparsers(help='''diffusers-cli command helpers''' ) # Register commands EnvironmentCommand.register_subcommand(__a ) # Let's go UpperCamelCase__ :Optional[int] = parser.parse_args() if not hasattr(__a , '''func''' ): parser.print_help() exit(1 ) # Run UpperCamelCase__ :Optional[int] = args.func(__a ) service.run() if __name__ == "__main__": main()
219
1