code
stringlengths
81
54k
code_codestyle
int64
0
721
style_context
stringlengths
91
41.9k
style_context_codestyle
int64
0
699
label
int64
0
1
'''simple docstring''' from __future__ import annotations import inspect import unittest import numpy as np from transformers import ResNetConfig 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 TFResNetForImageClassification, TFResNetModel from transformers.models.resnet.modeling_tf_resnet import TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class _a : """simple docstring""" def __init__( self , A__ , A__=3 , A__=32 , A__=3 , A__=10 , A__=[10, 20, 30, 40] , A__=[1, 1, 2, 1] , A__=True , A__=True , A__="relu" , A__=3 , A__=None , ) -> List[str]: _SCREAMING_SNAKE_CASE = parent _SCREAMING_SNAKE_CASE = batch_size _SCREAMING_SNAKE_CASE = image_size _SCREAMING_SNAKE_CASE = num_channels _SCREAMING_SNAKE_CASE = embeddings_size _SCREAMING_SNAKE_CASE = hidden_sizes _SCREAMING_SNAKE_CASE = depths _SCREAMING_SNAKE_CASE = is_training _SCREAMING_SNAKE_CASE = use_labels _SCREAMING_SNAKE_CASE = hidden_act _SCREAMING_SNAKE_CASE = num_labels _SCREAMING_SNAKE_CASE = scope _SCREAMING_SNAKE_CASE = len(A__ ) def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _SCREAMING_SNAKE_CASE = None if self.use_labels: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.num_labels ) _SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, labels def UpperCamelCase ( self ) -> Optional[int]: return ResNetConfig( num_channels=self.num_channels , embeddings_size=self.embeddings_size , hidden_sizes=self.hidden_sizes , depths=self.depths , hidden_act=self.hidden_act , num_labels=self.num_labels , image_size=self.image_size , ) def UpperCamelCase ( self , A__ , A__ , A__ ) -> int: _SCREAMING_SNAKE_CASE = TFResNetModel(config=A__ ) _SCREAMING_SNAKE_CASE = model(A__ ) # expected last hidden states: B, C, H // 32, W // 32 self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def UpperCamelCase ( self , A__ , A__ , A__ ) -> Tuple: _SCREAMING_SNAKE_CASE = self.num_labels _SCREAMING_SNAKE_CASE = TFResNetForImageClassification(A__ ) _SCREAMING_SNAKE_CASE = model(A__ , labels=A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = config_and_inputs _SCREAMING_SNAKE_CASE = {"""pixel_values""": pixel_values} return config, inputs_dict @require_tf class _a (_lowerCamelCase , _lowerCamelCase , unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = (TFResNetModel, TFResNetForImageClassification) if is_tf_available() else () SCREAMING_SNAKE_CASE = ( {'feature-extraction': TFResNetModel, 'image-classification': TFResNetForImageClassification} if is_tf_available() else {} ) SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = False def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = TFResNetModelTester(self ) _SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=A__ , has_text_modality=A__ ) def UpperCamelCase ( self ) -> int: self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def UpperCamelCase ( self ) -> str: return @unittest.skip(reason="""ResNet does not use inputs_embeds""" ) def UpperCamelCase ( self ) -> Tuple: pass @unittest.skip(reason="""ResNet does not support input and output embeddings""" ) def UpperCamelCase ( self ) -> Union[str, Any]: pass def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _SCREAMING_SNAKE_CASE = model_class(A__ ) _SCREAMING_SNAKE_CASE = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] _SCREAMING_SNAKE_CASE = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , A__ ) def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A__ ) def UpperCamelCase ( self ) -> Optional[Any]: def check_hidden_states_output(A__ , A__ , A__ ): _SCREAMING_SNAKE_CASE = model_class(A__ ) _SCREAMING_SNAKE_CASE = model(**self._prepare_for_class(A__ , A__ ) ) _SCREAMING_SNAKE_CASE = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states _SCREAMING_SNAKE_CASE = self.model_tester.num_stages self.assertEqual(len(A__ ) , expected_num_stages + 1 ) # ResNet's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() _SCREAMING_SNAKE_CASE = ["""basic""", """bottleneck"""] for model_class in self.all_model_classes: for layer_type in layers_type: _SCREAMING_SNAKE_CASE = layer_type _SCREAMING_SNAKE_CASE = True check_hidden_states_output(A__ , A__ , A__ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _SCREAMING_SNAKE_CASE = True check_hidden_states_output(A__ , A__ , A__ ) def UpperCamelCase ( self ) -> int: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*A__ ) @slow def UpperCamelCase ( self ) -> List[Any]: for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _SCREAMING_SNAKE_CASE = TFResNetModel.from_pretrained(A__ ) self.assertIsNotNone(A__ ) def lowerCAmelCase_ ( ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE = 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 ) -> Any: return ( AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) _SCREAMING_SNAKE_CASE = self.default_image_processor _SCREAMING_SNAKE_CASE = prepare_img() _SCREAMING_SNAKE_CASE = image_processor(images=A__ , return_tensors="""tf""" ) # forward pass _SCREAMING_SNAKE_CASE = model(**A__ ) # verify the logits _SCREAMING_SNAKE_CASE = tf.TensorShape((1, 10_00) ) self.assertEqual(outputs.logits.shape , A__ ) _SCREAMING_SNAKE_CASE = tf.constant([-11.1069, -9.7877, -8.3777] ) self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() , A__ , atol=1E-4 ) )
0
'''simple docstring''' import argparse import torch from torch import nn from transformers import MaMaaaConfig, MaMaaaForConditionalGeneration def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE = [ """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 lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = emb.weight.shape _SCREAMING_SNAKE_CASE = nn.Linear(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , bias=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = emb.weight.data return lin_layer def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = torch.load(SCREAMING_SNAKE_CASE_ , map_location="""cpu""" ) _SCREAMING_SNAKE_CASE = mam_aaa["""args"""] or mam_aaa["""cfg"""]["""model"""] _SCREAMING_SNAKE_CASE = mam_aaa["""model"""] remove_ignore_keys_(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = state_dict["""encoder.embed_tokens.weight"""].shape[0] _SCREAMING_SNAKE_CASE = MaMaaaConfig( vocab_size=SCREAMING_SNAKE_CASE_ , max_position_embeddings=10_24 , encoder_layers=args.encoder_layers , decoder_layers=args.decoder_layers , encoder_attention_heads=args.encoder_attention_heads , decoder_attention_heads=args.decoder_attention_heads , encoder_ffn_dim=args.encoder_ffn_embed_dim , decoder_ffn_dim=args.decoder_ffn_embed_dim , d_model=args.encoder_embed_dim , encoder_layerdrop=args.encoder_layerdrop , decoder_layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function="""relu""" , ) _SCREAMING_SNAKE_CASE = state_dict["""decoder.embed_tokens.weight"""] _SCREAMING_SNAKE_CASE = MaMaaaForConditionalGeneration(SCREAMING_SNAKE_CASE_ ) model.model.load_state_dict(SCREAMING_SNAKE_CASE_ , strict=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = make_linear_from_emb(model.model.shared ) return model if __name__ == "__main__": UpperCamelCase__ : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument("fairseq_path", type=str, help="path to a model.pt on local filesystem.") parser.add_argument("pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") UpperCamelCase__ : List[str] = parser.parse_args() UpperCamelCase__ : Any = convert_fairseq_mamaaa_checkpoint_from_disk(args.fairseq_pathß) model.save_pretrained(args.pytorch_dump_folder_path)
0
1
'''simple docstring''' import copy import inspect import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers import VideoMAEConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VideoMAEForPreTraining, VideoMAEForVideoClassification, VideoMAEModel, ) from transformers.models.videomae.modeling_videomae import VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from transformers import VideoMAEImageProcessor class _a : """simple docstring""" def __init__( self , A__ , A__=13 , A__=10 , A__=3 , A__=2 , A__=2 , A__=2 , A__=True , A__=True , A__=32 , A__=5 , A__=4 , A__=37 , A__="gelu" , A__=0.1 , A__=0.1 , A__=10 , A__=0.02 , A__=0.9 , A__=None , ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = parent _SCREAMING_SNAKE_CASE = batch_size _SCREAMING_SNAKE_CASE = image_size _SCREAMING_SNAKE_CASE = num_channels _SCREAMING_SNAKE_CASE = patch_size _SCREAMING_SNAKE_CASE = tubelet_size _SCREAMING_SNAKE_CASE = num_frames _SCREAMING_SNAKE_CASE = is_training _SCREAMING_SNAKE_CASE = use_labels _SCREAMING_SNAKE_CASE = hidden_size _SCREAMING_SNAKE_CASE = num_hidden_layers _SCREAMING_SNAKE_CASE = num_attention_heads _SCREAMING_SNAKE_CASE = intermediate_size _SCREAMING_SNAKE_CASE = hidden_act _SCREAMING_SNAKE_CASE = hidden_dropout_prob _SCREAMING_SNAKE_CASE = attention_probs_dropout_prob _SCREAMING_SNAKE_CASE = type_sequence_label_size _SCREAMING_SNAKE_CASE = initializer_range _SCREAMING_SNAKE_CASE = mask_ratio _SCREAMING_SNAKE_CASE = scope # in VideoMAE, the number of tokens equals num_frames/tubelet_size * num_patches per frame _SCREAMING_SNAKE_CASE = (image_size // patch_size) ** 2 _SCREAMING_SNAKE_CASE = (num_frames // tubelet_size) * self.num_patches_per_frame # use this variable to define bool_masked_pos _SCREAMING_SNAKE_CASE = int(mask_ratio * self.seq_length ) def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = floats_tensor( [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size] ) _SCREAMING_SNAKE_CASE = None if self.use_labels: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, labels def UpperCamelCase ( self ) -> List[Any]: return VideoMAEConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , num_frames=self.num_frames , tubelet_size=self.tubelet_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=A__ , initializer_range=self.initializer_range , ) def UpperCamelCase ( self , A__ , A__ , A__ ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = VideoMAEModel(config=A__ ) model.to(A__ ) model.eval() _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCamelCase ( self , A__ , A__ , A__ ) -> int: _SCREAMING_SNAKE_CASE = VideoMAEForPreTraining(A__ ) model.to(A__ ) model.eval() # important: each video needs to have the same number of masked patches # hence we define a single mask, which we then repeat for each example in the batch _SCREAMING_SNAKE_CASE = torch.ones((self.num_masks,) ) _SCREAMING_SNAKE_CASE = torch.cat([mask, torch.zeros(self.seq_length - mask.size(0 ) )] ) _SCREAMING_SNAKE_CASE = mask.expand(self.batch_size , -1 ).bool() _SCREAMING_SNAKE_CASE = model(A__ , A__ ) # model only returns predictions for masked patches _SCREAMING_SNAKE_CASE = mask.sum().item() _SCREAMING_SNAKE_CASE = 3 * self.tubelet_size * self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_masked_patches, decoder_num_labels) ) def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = config_and_inputs _SCREAMING_SNAKE_CASE = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class _a (_lowerCamelCase , _lowerCamelCase , unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = ( (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else () ) SCREAMING_SNAKE_CASE = ( {'feature-extraction': VideoMAEModel, 'video-classification': VideoMAEForVideoClassification} if is_torch_available() else {} ) SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = False def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = VideoMAEModelTester(self ) _SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=A__ , has_text_modality=A__ , hidden_size=37 ) def UpperCamelCase ( self , A__ , A__ , A__=False ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = copy.deepcopy(A__ ) if model_class == VideoMAEForPreTraining: # important: each video needs to have the same number of masked patches # hence we define a single mask, which we then repeat for each example in the batch _SCREAMING_SNAKE_CASE = torch.ones((self.model_tester.num_masks,) ) _SCREAMING_SNAKE_CASE = torch.cat([mask, torch.zeros(self.model_tester.seq_length - mask.size(0 ) )] ) _SCREAMING_SNAKE_CASE = mask.expand(self.model_tester.batch_size , -1 ).bool() _SCREAMING_SNAKE_CASE = bool_masked_pos.to(A__ ) if return_labels: if model_class in [ *get_values(A__ ), ]: _SCREAMING_SNAKE_CASE = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=A__ ) return inputs_dict def UpperCamelCase ( self ) -> int: self.config_tester.run_common_tests() @unittest.skip(reason="""VideoMAE does not use inputs_embeds""" ) def UpperCamelCase ( self ) -> int: pass def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _SCREAMING_SNAKE_CASE = model_class(A__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) _SCREAMING_SNAKE_CASE = model.get_output_embeddings() self.assertTrue(x is None or isinstance(A__ , nn.Linear ) ) def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _SCREAMING_SNAKE_CASE = model_class(A__ ) _SCREAMING_SNAKE_CASE = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] _SCREAMING_SNAKE_CASE = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , A__ ) def UpperCamelCase ( self ) -> Dict: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A__ ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*A__ ) @slow def UpperCamelCase ( self ) -> Optional[int]: for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _SCREAMING_SNAKE_CASE = VideoMAEModel.from_pretrained(A__ ) self.assertIsNotNone(A__ ) def UpperCamelCase ( self ) -> int: if not self.has_attentions: pass else: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() _SCREAMING_SNAKE_CASE = True for model_class in self.all_model_classes: _SCREAMING_SNAKE_CASE = self.model_tester.seq_length - self.model_tester.num_masks _SCREAMING_SNAKE_CASE = ( num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length ) _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = model_class(A__ ) model.to(A__ ) model.eval() with torch.no_grad(): _SCREAMING_SNAKE_CASE = model(**self._prepare_for_class(A__ , A__ ) ) _SCREAMING_SNAKE_CASE = outputs.attentions self.assertEqual(len(A__ ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = model_class(A__ ) model.to(A__ ) model.eval() with torch.no_grad(): _SCREAMING_SNAKE_CASE = model(**self._prepare_for_class(A__ , A__ ) ) _SCREAMING_SNAKE_CASE = outputs.attentions self.assertEqual(len(A__ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) _SCREAMING_SNAKE_CASE = len(A__ ) # Check attention is always last and order is fine _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = model_class(A__ ) model.to(A__ ) model.eval() with torch.no_grad(): _SCREAMING_SNAKE_CASE = model(**self._prepare_for_class(A__ , A__ ) ) self.assertEqual(out_len + 1 , len(A__ ) ) _SCREAMING_SNAKE_CASE = outputs.attentions self.assertEqual(len(A__ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) def UpperCamelCase ( self ) -> Dict: def check_hidden_states_output(A__ , A__ , A__ ): _SCREAMING_SNAKE_CASE = model_class(A__ ) model.to(A__ ) model.eval() with torch.no_grad(): _SCREAMING_SNAKE_CASE = model(**self._prepare_for_class(A__ , A__ ) ) _SCREAMING_SNAKE_CASE = outputs.hidden_states _SCREAMING_SNAKE_CASE = self.model_tester.num_hidden_layers + 1 self.assertEqual(len(A__ ) , A__ ) _SCREAMING_SNAKE_CASE = self.model_tester.seq_length - self.model_tester.num_masks _SCREAMING_SNAKE_CASE = num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [seq_length, self.model_tester.hidden_size] , ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _SCREAMING_SNAKE_CASE = True check_hidden_states_output(A__ , A__ , A__ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _SCREAMING_SNAKE_CASE = True check_hidden_states_output(A__ , A__ , A__ ) @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" ) def UpperCamelCase ( self ) -> List[Any]: pass def lowerCAmelCase_ ( ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = hf_hub_download( repo_id="""hf-internal-testing/spaghetti-video""" , filename="""eating_spaghetti.npy""" , repo_type="""dataset""" ) _SCREAMING_SNAKE_CASE = np.load(SCREAMING_SNAKE_CASE_ ) return list(SCREAMING_SNAKE_CASE_ ) @require_torch @require_vision class _a (unittest.TestCase): """simple docstring""" @cached_property def UpperCamelCase ( self ) -> str: # logits were tested with a different mean and std, so we use the same here 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 UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = VideoMAEForVideoClassification.from_pretrained("""MCG-NJU/videomae-base-finetuned-kinetics""" ).to( A__ ) _SCREAMING_SNAKE_CASE = self.default_image_processor _SCREAMING_SNAKE_CASE = prepare_video() _SCREAMING_SNAKE_CASE = image_processor(A__ , return_tensors="""pt""" ).to(A__ ) # forward pass with torch.no_grad(): _SCREAMING_SNAKE_CASE = model(**A__ ) # verify the logits _SCREAMING_SNAKE_CASE = torch.Size((1, 4_00) ) self.assertEqual(outputs.logits.shape , A__ ) _SCREAMING_SNAKE_CASE = torch.tensor([0.3669, -0.0688, -0.2421] ).to(A__ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , A__ , atol=1E-4 ) ) @slow def UpperCamelCase ( self ) -> int: _SCREAMING_SNAKE_CASE = VideoMAEForPreTraining.from_pretrained("""MCG-NJU/videomae-base-short""" ).to(A__ ) _SCREAMING_SNAKE_CASE = self.default_image_processor _SCREAMING_SNAKE_CASE = prepare_video() _SCREAMING_SNAKE_CASE = image_processor(A__ , return_tensors="""pt""" ).to(A__ ) # add boolean mask, indicating which patches to mask _SCREAMING_SNAKE_CASE = hf_hub_download(repo_id="""hf-internal-testing/bool-masked-pos""" , filename="""bool_masked_pos.pt""" ) _SCREAMING_SNAKE_CASE = torch.load(A__ ) # forward pass with torch.no_grad(): _SCREAMING_SNAKE_CASE = model(**A__ ) # verify the logits _SCREAMING_SNAKE_CASE = torch.Size([1, 14_08, 15_36] ) _SCREAMING_SNAKE_CASE = torch.tensor( [[0.7994, 0.9612, 0.8508], [0.7401, 0.8958, 0.8302], [0.5862, 0.7468, 0.7325]] , device=A__ ) self.assertEqual(outputs.logits.shape , A__ ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , A__ , atol=1E-4 ) ) # verify the loss (`config.norm_pix_loss` = `True`) _SCREAMING_SNAKE_CASE = torch.tensor([0.5142] , device=A__ ) self.assertTrue(torch.allclose(outputs.loss , A__ , atol=1E-4 ) ) # verify the loss (`config.norm_pix_loss` = `False`) _SCREAMING_SNAKE_CASE = VideoMAEForPreTraining.from_pretrained("""MCG-NJU/videomae-base-short""" , norm_pix_loss=A__ ).to( A__ ) with torch.no_grad(): _SCREAMING_SNAKE_CASE = model(**A__ ) _SCREAMING_SNAKE_CASE = torch.tensor(torch.tensor([0.6469] ) , device=A__ ) self.assertTrue(torch.allclose(outputs.loss , A__ , atol=1E-4 ) )
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available UpperCamelCase__ : str = { "configuration_canine": ["CANINE_PRETRAINED_CONFIG_ARCHIVE_MAP", "CanineConfig"], "tokenization_canine": ["CanineTokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : List[Any] = [ "CANINE_PRETRAINED_MODEL_ARCHIVE_LIST", "CanineForMultipleChoice", "CanineForQuestionAnswering", "CanineForSequenceClassification", "CanineForTokenClassification", "CanineLayer", "CanineModel", "CaninePreTrainedModel", "load_tf_weights_in_canine", ] if TYPE_CHECKING: from .configuration_canine import CANINE_PRETRAINED_CONFIG_ARCHIVE_MAP, CanineConfig from .tokenization_canine import CanineTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_canine import ( CANINE_PRETRAINED_MODEL_ARCHIVE_LIST, CanineForMultipleChoice, CanineForQuestionAnswering, CanineForSequenceClassification, CanineForTokenClassification, CanineLayer, CanineModel, CaninePreTrainedModel, load_tf_weights_in_canine, ) else: import sys UpperCamelCase__ : str = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
0
1
'''simple docstring''' import argparse import datetime def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE = { """0""": """Sunday""", """1""": """Monday""", """2""": """Tuesday""", """3""": """Wednesday""", """4""": """Thursday""", """5""": """Friday""", """6""": """Saturday""", } _SCREAMING_SNAKE_CASE = {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 0} # Validate if not 0 < len(SCREAMING_SNAKE_CASE_ ) < 11: raise ValueError("""Must be 10 characters long""" ) # Get month _SCREAMING_SNAKE_CASE = int(date_input[0] + date_input[1] ) # Validate if not 0 < m < 13: raise ValueError("""Month must be between 1 - 12""" ) _SCREAMING_SNAKE_CASE = date_input[2] # Validate if sep_a not in ["-", "/"]: raise ValueError("""Date separator must be '-' or '/'""" ) # Get day _SCREAMING_SNAKE_CASE = int(date_input[3] + date_input[4] ) # Validate if not 0 < d < 32: raise ValueError("""Date must be between 1 - 31""" ) # Get second separator _SCREAMING_SNAKE_CASE = date_input[5] # Validate if sep_a not in ["-", "/"]: raise ValueError("""Date separator must be '-' or '/'""" ) # Get year _SCREAMING_SNAKE_CASE = int(date_input[6] + date_input[7] + date_input[8] + date_input[9] ) # Arbitrary year range if not 45 < y < 85_00: raise ValueError( """Year out of range. There has to be some sort of limit...right?""" ) # Get datetime obj for validation _SCREAMING_SNAKE_CASE = datetime.date(int(SCREAMING_SNAKE_CASE_ ) , int(SCREAMING_SNAKE_CASE_ ) , int(SCREAMING_SNAKE_CASE_ ) ) # Start math if m <= 2: _SCREAMING_SNAKE_CASE = y - 1 _SCREAMING_SNAKE_CASE = m + 12 # maths var _SCREAMING_SNAKE_CASE = int(str(SCREAMING_SNAKE_CASE_ )[:2] ) _SCREAMING_SNAKE_CASE = int(str(SCREAMING_SNAKE_CASE_ )[2:] ) _SCREAMING_SNAKE_CASE = int(2.6 * m - 5.39 ) _SCREAMING_SNAKE_CASE = int(c / 4 ) _SCREAMING_SNAKE_CASE = int(k / 4 ) _SCREAMING_SNAKE_CASE = int(d + k ) _SCREAMING_SNAKE_CASE = int(t + u + v + x ) _SCREAMING_SNAKE_CASE = int(z - (2 * c) ) _SCREAMING_SNAKE_CASE = round(w % 7 ) # End math # Validate math if f != convert_datetime_days[dt_ck.weekday()]: raise AssertionError("""The date was evaluated incorrectly. Contact developer.""" ) # Response _SCREAMING_SNAKE_CASE = F"Your date {date_input}, is a {days[str(SCREAMING_SNAKE_CASE_ )]}!" return response if __name__ == "__main__": import doctest doctest.testmod() UpperCamelCase__ : Dict = argparse.ArgumentParser( description=( "Find out what day of the week nearly any date is or was. Enter " "date as a string in the mm-dd-yyyy or mm/dd/yyyy format" ) ) parser.add_argument( "date_input", type=str, help="Date as a string (mm-dd-yyyy or mm/dd/yyyy)" ) UpperCamelCase__ : int = parser.parse_args() zeller(args.date_input)
0
'''simple docstring''' import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class _a (_lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = ['image_processor', 'tokenizer'] SCREAMING_SNAKE_CASE = 'ChineseCLIPImageProcessor' SCREAMING_SNAKE_CASE = ('BertTokenizer', 'BertTokenizerFast') def __init__( self , A__=None , A__=None , **A__ ) -> int: _SCREAMING_SNAKE_CASE = None if "feature_extractor" in kwargs: warnings.warn( """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`""" """ instead.""" , A__ , ) _SCREAMING_SNAKE_CASE = kwargs.pop("""feature_extractor""" ) _SCREAMING_SNAKE_CASE = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("""You need to specify an `image_processor`.""" ) if tokenizer is None: raise ValueError("""You need to specify a `tokenizer`.""" ) super().__init__(A__ , A__ ) _SCREAMING_SNAKE_CASE = self.image_processor def __call__( self , A__=None , A__=None , A__=None , **A__ ) -> Optional[int]: if text is None and images is None: raise ValueError("""You have to specify either text or images. Both cannot be none.""" ) if text is not None: _SCREAMING_SNAKE_CASE = self.tokenizer(A__ , return_tensors=A__ , **A__ ) if images is not None: _SCREAMING_SNAKE_CASE = self.image_processor(A__ , return_tensors=A__ , **A__ ) if text is not None and images is not None: _SCREAMING_SNAKE_CASE = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**A__ ) , tensor_type=A__ ) def UpperCamelCase ( self , *A__ , **A__ ) -> Dict: return self.tokenizer.batch_decode(*A__ , **A__ ) def UpperCamelCase ( self , *A__ , **A__ ) -> Optional[Any]: return self.tokenizer.decode(*A__ , **A__ ) @property def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.tokenizer.model_input_names _SCREAMING_SNAKE_CASE = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def UpperCamelCase ( self ) -> Optional[int]: warnings.warn( """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.""" , A__ , ) return self.image_processor_class
0
1
'''simple docstring''' from typing import Dict from .base import GenericTensor, Pipeline class _a (_lowerCamelCase): """simple docstring""" def UpperCamelCase ( self , A__=None , A__=None , A__=None , **A__ ) -> Optional[int]: if tokenize_kwargs is None: _SCREAMING_SNAKE_CASE = {} if truncation is not None: if "truncation" in tokenize_kwargs: raise ValueError( """truncation parameter defined twice (given as keyword argument as well as in tokenize_kwargs)""" ) _SCREAMING_SNAKE_CASE = truncation _SCREAMING_SNAKE_CASE = tokenize_kwargs _SCREAMING_SNAKE_CASE = {} if return_tensors is not None: _SCREAMING_SNAKE_CASE = return_tensors return preprocess_params, {}, postprocess_params def UpperCamelCase ( self , A__ , **A__ ) -> Dict[str, GenericTensor]: _SCREAMING_SNAKE_CASE = self.framework _SCREAMING_SNAKE_CASE = self.tokenizer(A__ , return_tensors=A__ , **A__ ) return model_inputs def UpperCamelCase ( self , A__ ) -> Optional[int]: _SCREAMING_SNAKE_CASE = self.model(**A__ ) return model_outputs def UpperCamelCase ( self , A__ , A__=False ) -> Optional[Any]: # [0] is the first available tensor, logits or last_hidden_state. if return_tensors: return model_outputs[0] if self.framework == "pt": return model_outputs[0].tolist() elif self.framework == "tf": return model_outputs[0].numpy().tolist() def __call__( self , *A__ , **A__ ) -> List[str]: return super().__call__(*A__ , **A__ )
0
'''simple docstring''' from sklearn.metrics import matthews_corrcoef import datasets UpperCamelCase__ : List[str] = "\nCompute the Matthews correlation coefficient (MCC)\n\nThe Matthews correlation coefficient is used in machine learning as a\nmeasure of the quality of binary and multiclass classifications. It takes\ninto account true and false positives and negatives and is generally\nregarded as a balanced measure which can be used even if the classes are of\nvery different sizes. The MCC is in essence a correlation coefficient value\nbetween -1 and +1. A coefficient of +1 represents a perfect prediction, 0\nan average random prediction and -1 an inverse prediction. The statistic\nis also known as the phi coefficient. [source: Wikipedia]\n" UpperCamelCase__ : List[Any] = "\nArgs:\n predictions (list of int): Predicted labels, as returned by a model.\n references (list of int): Ground truth labels.\n sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`.\nReturns:\n matthews_correlation (dict containing float): Matthews correlation.\nExamples:\n Example 1, a basic example with only predictions and references as inputs:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3])\n >>> print(round(results['matthews_correlation'], 2))\n 0.54\n\n Example 2, the same example as above, but also including sample weights:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 3, 1, 1, 1, 2])\n >>> print(round(results['matthews_correlation'], 2))\n 0.1\n\n Example 3, the same example as above, but with sample weights that cause a negative correlation:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 1, 0, 0, 0, 1])\n >>> print(round(results['matthews_correlation'], 2))\n -0.25\n" UpperCamelCase__ : Any = "\\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class _a (datasets.Metric): """simple docstring""" def UpperCamelCase ( self ) -> Optional[Any]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=[ """https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html""" ] , ) def UpperCamelCase ( self , A__ , A__ , A__=None ) -> List[str]: return { "matthews_correlation": float(matthews_corrcoef(A__ , A__ , sample_weight=A__ ) ), }
0
1
'''simple docstring''' import math import os import re import sys import unittest from pathlib import Path from typing import Tuple from unittest.mock import patch from parameterized import parameterized from transformers.testing_utils import ( CaptureStderr, ExtendSysPath, TestCasePlus, execute_subprocess_async, get_gpu_count, get_torch_dist_unique_port, require_apex, require_bitsandbytes, require_fairscale, require_torch, require_torch_gpu, require_torch_multi_gpu, require_torch_non_multi_gpu, slow, ) from transformers.trainer_callback import TrainerState from transformers.trainer_utils import set_seed UpperCamelCase__ : Tuple = os.path.abspath(os.path.dirname(__file__)) with ExtendSysPath(f"""{bindir}/../../examples/pytorch/translation"""): from run_translation import main # noqa set_seed(42) UpperCamelCase__ : Union[str, Any] = "sshleifer/student_marian_en_ro_6_1" UpperCamelCase__ : str = "sshleifer/tiny-mbart" @require_torch class _a (_lowerCamelCase): """simple docstring""" def UpperCamelCase ( self , A__=False , A__=None , A__=True , A__=True , A__=True , A__=True , ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = self.run_trainer( eval_steps=1 , max_len=12 , model_name=A__ , num_train_epochs=1 , distributed=A__ , extra_args_str=A__ , predict_with_generate=A__ , do_train=A__ , do_eval=A__ , do_predict=A__ , ) _SCREAMING_SNAKE_CASE = TrainerState.load_from_json(os.path.join(A__ , """trainer_state.json""" ) ).log_history if not do_eval: return _SCREAMING_SNAKE_CASE = [log for log in logs if """eval_loss""" in log.keys()] _SCREAMING_SNAKE_CASE = eval_metrics[0] if predict_with_generate: assert "eval_bleu" in first_step_stats _SCREAMING_SNAKE_CASE = eval_metrics[-1] assert isinstance(last_step_stats["""eval_bleu"""] , A__ ) assert not math.isnan(float(last_step_stats["""eval_loss"""] ) ), "eval_loss must not be `nan`" @require_torch_non_multi_gpu def UpperCamelCase ( self ) -> Optional[int]: self.run_seqaseq_quick() @require_torch_multi_gpu def UpperCamelCase ( self ) -> Optional[Any]: self.run_seqaseq_quick(distributed=A__ ) @require_torch_multi_gpu def UpperCamelCase ( self ) -> Union[str, Any]: self.run_seqaseq_quick(distributed=A__ ) @unittest.skip("""Requires an update of the env running those tests""" ) @require_torch_multi_gpu @require_fairscale def UpperCamelCase ( self ) -> Any: self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--sharded_ddp simple""" ) @unittest.skip("""Requires an update of the env running those tests""" ) @require_torch_multi_gpu @require_fairscale def UpperCamelCase ( self ) -> Tuple: self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--sharded_ddp simple --fp16""" ) @unittest.skip("""Requires an update of the env running those tests""" ) @require_torch_multi_gpu @require_fairscale def UpperCamelCase ( self ) -> str: self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--sharded_ddp zero_dp_2""" , predict_with_generate=A__ ) @unittest.skip("""Requires an update of the env running those tests""" ) @require_torch_multi_gpu @require_fairscale def UpperCamelCase ( self ) -> List[str]: self.run_seqaseq_quick( distributed=A__ , extra_args_str="""--sharded_ddp zero_dp_2 --fp16""" , predict_with_generate=A__ ) @require_apex @require_torch_gpu def UpperCamelCase ( self ) -> Optional[Any]: # XXX: apex breaks the trainer if it's run twice e.g. run_seq2seq.main() from the same # program and it breaks other tests that run from the same pytest worker, therefore until this is # sorted out it must be run only in an external program, that is distributed=True in this # test and only under one or more gpus - if we want cpu will need to make a special test # # specifically to the problem traced it to self.optimizer.step() - if it's run 2nd time via # 2nd main() call it botches the future eval. # self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--fp16 --fp16_backend=apex""" ) # test 2nd time - was getting eval_loss': nan' # to reproduce the problem set distributed=False self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--fp16 --fp16_backend=apex""" ) @parameterized.expand(["""base""", """low""", """high""", """mixed"""] ) @require_torch_multi_gpu def UpperCamelCase ( self , A__ ) -> List[Any]: # as each sub-test is slow-ish split into multiple sub-tests to avoid CI timeout _SCREAMING_SNAKE_CASE = { # test with the default log_level - should be info and thus log info once """base""": {"""extra_args_str""": """""", """n_matches""": 1}, # test with low log_level and log_level_replica - should be noisy on all processes # now the info string should appear twice on 2 processes """low""": {"""extra_args_str""": """--log_level debug --log_level_replica debug""", """n_matches""": 2}, # test with high log_level and low log_level_replica # now the info string should appear once only on the replica """high""": {"""extra_args_str""": """--log_level error --log_level_replica debug""", """n_matches""": 1}, # test with high log_level and log_level_replica - should be quiet on all processes """mixed""": {"""extra_args_str""": """--log_level error --log_level_replica error""", """n_matches""": 0}, } _SCREAMING_SNAKE_CASE = experiments[experiment_id] _SCREAMING_SNAKE_CASE = {"""distributed""": True, """predict_with_generate""": False, """do_eval""": False, """do_predict""": False} _SCREAMING_SNAKE_CASE = """Running training""" with CaptureStderr() as cl: self.run_seqaseq_quick(**A__ , extra_args_str=data["""extra_args_str"""] ) _SCREAMING_SNAKE_CASE = len(re.findall(A__ , cl.err ) ) self.assertEqual(A__ , data["""n_matches"""] ) @slow def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = self.run_trainer( eval_steps=2 , max_len=1_28 , model_name=A__ , learning_rate=3E-4 , num_train_epochs=10 , distributed=A__ , ) # Check metrics _SCREAMING_SNAKE_CASE = TrainerState.load_from_json(os.path.join(A__ , """trainer_state.json""" ) ).log_history _SCREAMING_SNAKE_CASE = [log for log in logs if """eval_loss""" in log.keys()] _SCREAMING_SNAKE_CASE = eval_metrics[0] _SCREAMING_SNAKE_CASE = eval_metrics[-1] assert first_step_stats["eval_loss"] > last_step_stats["eval_loss"], "model learned nothing" assert isinstance(last_step_stats["""eval_bleu"""] , A__ ) # test if do_predict saves generations and metrics _SCREAMING_SNAKE_CASE = os.listdir(A__ ) _SCREAMING_SNAKE_CASE = {os.path.basename(A__ ) for p in contents} assert "generated_predictions.txt" in contents assert "predict_results.json" in contents @slow @require_bitsandbytes def UpperCamelCase ( self ) -> Dict: from transformers.training_args import OptimizerNames def train_and_return_metrics(A__ ) -> Tuple[int, float]: _SCREAMING_SNAKE_CASE = """--skip_memory_metrics 0""" _SCREAMING_SNAKE_CASE = self.run_trainer( max_len=1_28 , model_name=A__ , learning_rate=3E-4 , num_train_epochs=1 , optim=A__ , distributed=A__ , extra_args_str=A__ , do_eval=A__ , do_predict=A__ , n_gpus_to_use=1 , ) # Check metrics _SCREAMING_SNAKE_CASE = TrainerState.load_from_json(Path(A__ , """trainer_state.json""" ) ).log_history _SCREAMING_SNAKE_CASE = int(logs[0]["""train_mem_gpu_peaked_delta"""] / 2**20 ) _SCREAMING_SNAKE_CASE = int(logs[0]["""train_mem_gpu_alloc_delta"""] / 2**20 ) _SCREAMING_SNAKE_CASE = logs[0]["""train_loss"""] return gpu_peak_mem_mb, gpu_alloc_mem_mb, loss _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = train_and_return_metrics(OptimizerNames.ADAMW_TORCH.value ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = train_and_return_metrics(OptimizerNames.ADAMW_BNB.value ) _SCREAMING_SNAKE_CASE = gpu_alloc_mem_orig - gpu_alloc_mem_bnb _SCREAMING_SNAKE_CASE = gpu_peak_mem_orig + gpu_alloc_mem_orig _SCREAMING_SNAKE_CASE = gpu_peak_mem_bnb + gpu_alloc_mem_bnb _SCREAMING_SNAKE_CASE = gpu_total_mem_orig - gpu_total_mem_bnb # sshleifer/student_marian_en_ro_6_1 has 54M parameter, 29M of which is `nn.Embedding` which # doesn't get quantized and remains in fp32. Therefore we only have 25M parameters quantized # in 2 bytes and the diff in optim memory usage is derived as so: # # - normal 25*8=~200MB (8 bytes per param) # - bnb 25*2= ~50MB (2 bytes per param) # # Thus we should expect ~150MB total memory saved. # # Peak memory should be the same - the total should be different by about that same margin # # After leaving a small margin to accommodate for differences between gpus let's check # that we have at least 120MB in savings _SCREAMING_SNAKE_CASE = 1_20 # uncomment the following if this test starts failing - requires py38 for a new print feature # gpu_peak_mem_diff = gpu_peak_mem_orig - gpu_peak_mem_bnb # print(f"{gpu_alloc_mem_orig=}MB {gpu_peak_mem_orig=}MB {gpu_alloc_mem_orig+gpu_peak_mem_orig=}MB") # print(f" {gpu_alloc_mem_bnb=}MB {gpu_peak_mem_bnb=}MB {gpu_alloc_mem_bnb+gpu_peak_mem_bnb=}MB") # print(f"{gpu_alloc_mem_diff=}MB") # print(f"{gpu_peak_mem_diff=}MB") # print(f"{gpu_total_mem_orig=}MB, {gpu_total_mem_bnb=}MB") # print(f"{gpu_total_mem_diff=}MB, {gpu_total_mem_diff=}MB") self.assertGreater( A__ , A__ , """should use ~150MB less alloc gpu memory with BNB, compared to without it for this model but got""" F" a difference of {gpu_alloc_mem_diff}MB, with gpu_alloc_mem_orig={gpu_alloc_mem_orig}MB and" F" gpu_alloc_mem_bnb={gpu_alloc_mem_bnb}MB" , ) self.assertGreater( A__ , A__ , """should use ~150MB less total gpu memory with BNB, compared to without it for this model but got""" F" a difference of {gpu_total_mem_diff}MB, with gpu_total_mem_orig={gpu_total_mem_orig}MB and" F" gpu_total_mem_bnb={gpu_total_mem_bnb}MB" , ) self.assertEqual( A__ , A__ , F"loss should be the same, but got loss_orig={loss_orig}, loss_bnb={loss_bnb}" ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ = 3E-3 , A__ = "adafactor" , A__ = False , A__ = None , A__ = 0 , A__ = True , A__ = True , A__ = True , A__ = True , A__ = None , ) -> Dict: _SCREAMING_SNAKE_CASE = self.test_file_dir / """../fixtures/tests_samples/wmt_en_ro""" _SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() _SCREAMING_SNAKE_CASE = F"\n --model_name_or_path {model_name}\n --train_file {data_dir}/train.json\n --validation_file {data_dir}/val.json\n --test_file {data_dir}/test.json\n --output_dir {output_dir}\n --overwrite_output_dir\n --max_train_samples 8\n --max_source_length {max_len}\n --max_target_length {max_len}\n --do_train\n --num_train_epochs {str(A__ )}\n --per_device_train_batch_size 4\n --learning_rate {learning_rate}\n --warmup_steps 8\n --logging_steps 0\n --logging_strategy no\n --save_steps {str(A__ )}\n --group_by_length\n --label_smoothing_factor 0.1\n --target_lang ro_RO\n --source_lang en_XX\n ".split() _SCREAMING_SNAKE_CASE = F"\n --do_eval\n --per_device_eval_batch_size 4\n --max_eval_samples 8\n --val_max_target_length {max_len}\n --evaluation_strategy steps\n --eval_steps {str(A__ )}\n ".split() _SCREAMING_SNAKE_CASE = """ --do_predict """.split() _SCREAMING_SNAKE_CASE = [] if do_train: args += args_train if do_eval: args += args_eval if do_predict: args += args_predict if predict_with_generate: args += "--predict_with_generate".split() if do_train: if optim == "adafactor": args += "--adafactor".split() else: args += F"--optim {optim}".split() if extra_args_str is not None: args += extra_args_str.split() if distributed: if n_gpus_to_use is None: _SCREAMING_SNAKE_CASE = get_gpu_count() _SCREAMING_SNAKE_CASE = get_torch_dist_unique_port() _SCREAMING_SNAKE_CASE = F"\n -m torch.distributed.run\n --nproc_per_node={n_gpus_to_use}\n --master_port={master_port}\n {self.examples_dir_str}/pytorch/translation/run_translation.py\n ".split() _SCREAMING_SNAKE_CASE = [sys.executable] + distributed_args + args # keep for quick debug # print(" ".join([f"\nPYTHONPATH={self.src_dir_str}"] +cmd)); die execute_subprocess_async(A__ , env=self.get_env() ) else: _SCREAMING_SNAKE_CASE = ["""run_translation.py"""] + args with patch.object(A__ , """argv""" , A__ ): main() return output_dir
0
'''simple docstring''' from __future__ import annotations def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Tuple: """simple docstring""" print(F"Vertex\tShortest Distance from vertex {src}" ) for i, d in enumerate(SCREAMING_SNAKE_CASE_ ): print(F"{i}\t\t{d}" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" for j in range(SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = (graph[j][k] for k in ["""src""", """dst""", """weight"""]) if distance[u] != float("""inf""" ) and distance[u] + w < distance[v]: return True return False def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> list[float]: """simple docstring""" _SCREAMING_SNAKE_CASE = [float("""inf""" )] * vertex_count _SCREAMING_SNAKE_CASE = 0.0 for _ in range(vertex_count - 1 ): for j in range(SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = (graph[j][k] for k in ["""src""", """dst""", """weight"""]) if distance[u] != float("""inf""" ) and distance[u] + w < distance[v]: _SCREAMING_SNAKE_CASE = distance[u] + w _SCREAMING_SNAKE_CASE = check_negative_cycle(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if negative_cycle_exists: raise Exception("""Negative cycle found""" ) return distance if __name__ == "__main__": import doctest doctest.testmod() UpperCamelCase__ : int = int(input("Enter number of vertices: ").strip()) UpperCamelCase__ : int = int(input("Enter number of edges: ").strip()) UpperCamelCase__ : list[dict[str, int]] = [{} for _ in range(E)] for i in range(E): print("Edge ", i + 1) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ : Dict = ( int(x) for x in input("Enter source, destination, weight: ").strip().split(" ") ) UpperCamelCase__ : Optional[Any] = {"src": src, "dst": dest, "weight": weight} UpperCamelCase__ : Optional[Any] = int(input("\nEnter shortest path source:").strip()) UpperCamelCase__ : Any = bellman_ford(graph, V, E, source) print_distance(shortest_distance, 0)
0
1
'''simple docstring''' import argparse import torch from transformers import GPTaLMHeadModel, RobertaForMaskedLM if __name__ == "__main__": UpperCamelCase__ : Tuple = argparse.ArgumentParser( description=( "Extraction some layers of the full RobertaForMaskedLM or GPT2LMHeadModel for Transfer Learned" " Distillation" ) ) parser.add_argument("--model_type", default="roberta", choices=["roberta", "gpt2"]) parser.add_argument("--model_name", default="roberta-large", type=str) parser.add_argument("--dump_checkpoint", default="serialization_dir/tf_roberta_048131723.pth", type=str) parser.add_argument("--vocab_transform", action="store_true") UpperCamelCase__ : Optional[Any] = parser.parse_args() if args.model_type == "roberta": UpperCamelCase__ : Tuple = RobertaForMaskedLM.from_pretrained(args.model_name) UpperCamelCase__ : List[str] = "roberta" elif args.model_type == "gpt2": UpperCamelCase__ : Any = GPTaLMHeadModel.from_pretrained(args.model_name) UpperCamelCase__ : List[str] = "transformer" UpperCamelCase__ : Tuple = model.state_dict() UpperCamelCase__ : Tuple = {} # Embeddings # if args.model_type == "gpt2": for param_name in ["wte.weight", "wpe.weight"]: UpperCamelCase__ : Dict = state_dict[f"""{prefix}.{param_name}"""] else: for w in ["word_embeddings", "position_embeddings", "token_type_embeddings"]: UpperCamelCase__ : List[str] = f"""{prefix}.embeddings.{w}.weight""" UpperCamelCase__ : List[Any] = state_dict[param_name] for w in ["weight", "bias"]: UpperCamelCase__ : Optional[Any] = f"""{prefix}.embeddings.LayerNorm.{w}""" UpperCamelCase__ : Union[str, Any] = state_dict[param_name] # Transformer Blocks # UpperCamelCase__ : List[Any] = 0 for teacher_idx in [0, 2, 4, 7, 9, 11]: if args.model_type == "gpt2": for layer in ["ln_1", "attn.c_attn", "attn.c_proj", "ln_2", "mlp.c_fc", "mlp.c_proj"]: for w in ["weight", "bias"]: UpperCamelCase__ : Tuple = state_dict[ f"""{prefix}.h.{teacher_idx}.{layer}.{w}""" ] UpperCamelCase__ : List[str] = state_dict[f"""{prefix}.h.{teacher_idx}.attn.bias"""] else: for layer in [ "attention.self.query", "attention.self.key", "attention.self.value", "attention.output.dense", "attention.output.LayerNorm", "intermediate.dense", "output.dense", "output.LayerNorm", ]: for w in ["weight", "bias"]: UpperCamelCase__ : Optional[Any] = state_dict[ f"""{prefix}.encoder.layer.{teacher_idx}.{layer}.{w}""" ] std_idx += 1 # Language Modeling Head ###s if args.model_type == "roberta": for layer in ["lm_head.decoder.weight", "lm_head.bias"]: UpperCamelCase__ : Any = state_dict[f"""{layer}"""] if args.vocab_transform: for w in ["weight", "bias"]: UpperCamelCase__ : Dict = state_dict[f"""lm_head.dense.{w}"""] UpperCamelCase__ : int = state_dict[f"""lm_head.layer_norm.{w}"""] elif args.model_type == "gpt2": for w in ["weight", "bias"]: UpperCamelCase__ : Dict = state_dict[f"""{prefix}.ln_f.{w}"""] UpperCamelCase__ : Optional[Any] = state_dict["lm_head.weight"] print(f"""N layers selected for distillation: {std_idx}""") print(f"""Number of params transferred for distillation: {len(compressed_sd.keys())}""") print(f"""Save transferred checkpoint to {args.dump_checkpoint}.""") torch.save(compressed_sd, args.dump_checkpoint)
0
'''simple docstring''' 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 , A__ , A__=13 , A__=7 , A__=True , A__=True , A__=True , A__=True , A__=99 , A__=32 , A__=2 , A__=4 , A__=37 , A__="gelu" , A__=0.1 , A__=0.1 , A__=5_12 , A__=16 , A__=2 , A__=0.02 , A__=3 , A__=4 , A__=None , ) -> int: _SCREAMING_SNAKE_CASE = parent _SCREAMING_SNAKE_CASE = 13 _SCREAMING_SNAKE_CASE = 7 _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = 99 _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 = 5_12 _SCREAMING_SNAKE_CASE = 16 _SCREAMING_SNAKE_CASE = 2 _SCREAMING_SNAKE_CASE = 0.02 _SCREAMING_SNAKE_CASE = 3 _SCREAMING_SNAKE_CASE = 4 _SCREAMING_SNAKE_CASE = None def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _SCREAMING_SNAKE_CASE = None if self.use_input_mask: _SCREAMING_SNAKE_CASE = random_attention_mask([self.batch_size, self.seq_length] ) _SCREAMING_SNAKE_CASE = None if self.use_token_type_ids: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None if self.use_labels: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.num_choices ) _SCREAMING_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=A__ , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = TFRoFormerModel(config=A__ ) _SCREAMING_SNAKE_CASE = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} _SCREAMING_SNAKE_CASE = [input_ids, input_mask] _SCREAMING_SNAKE_CASE = model(A__ ) _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> str: _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = TFRoFormerForCausalLM(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ )["""logits"""] self.parent.assertListEqual( list(prediction_scores.numpy().shape ) , [self.batch_size, self.seq_length, self.vocab_size] ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> Dict: _SCREAMING_SNAKE_CASE = TFRoFormerForMaskedLM(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> List[str]: _SCREAMING_SNAKE_CASE = self.num_labels _SCREAMING_SNAKE_CASE = TFRoFormerForSequenceClassification(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> Any: _SCREAMING_SNAKE_CASE = self.num_choices _SCREAMING_SNAKE_CASE = TFRoFormerForMultipleChoice(config=A__ ) _SCREAMING_SNAKE_CASE = tf.tile(tf.expand_dims(A__ , 1 ) , (1, self.num_choices, 1) ) _SCREAMING_SNAKE_CASE = tf.tile(tf.expand_dims(A__ , 1 ) , (1, self.num_choices, 1) ) _SCREAMING_SNAKE_CASE = tf.tile(tf.expand_dims(A__ , 1 ) , (1, self.num_choices, 1) ) _SCREAMING_SNAKE_CASE = { """input_ids""": multiple_choice_inputs_ids, """attention_mask""": multiple_choice_input_mask, """token_type_ids""": multiple_choice_token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> List[str]: _SCREAMING_SNAKE_CASE = self.num_labels _SCREAMING_SNAKE_CASE = TFRoFormerForTokenClassification(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> Tuple: _SCREAMING_SNAKE_CASE = TFRoFormerForQuestionAnswering(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = 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 ) , ) = config_and_inputs _SCREAMING_SNAKE_CASE = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask} return config, inputs_dict @require_tf class _a (_lowerCamelCase , _lowerCamelCase , unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = ( ( TFRoFormerModel, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerForMultipleChoice, ) if is_tf_available() else () ) SCREAMING_SNAKE_CASE = ( { '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 {} ) SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = False def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ ) -> str: if pipeline_test_casse_name == "TextGenerationPipelineTests": return True return False def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = TFRoFormerModelTester(self ) _SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=A__ , hidden_size=37 ) def UpperCamelCase ( self ) -> Optional[Any]: self.config_tester.run_common_tests() def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A__ ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*A__ ) def UpperCamelCase ( self ) -> int: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head(*A__ ) def UpperCamelCase ( self ) -> Dict: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*A__ ) def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*A__ ) def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*A__ ) def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*A__ ) @slow def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = TFRoFormerModel.from_pretrained("""junnyu/roformer_chinese_base""" ) self.assertIsNotNone(A__ ) @require_tf class _a (unittest.TestCase): """simple docstring""" @slow def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = TFRoFormerForMaskedLM.from_pretrained("""junnyu/roformer_chinese_base""" ) _SCREAMING_SNAKE_CASE = tf.constant([[0, 1, 2, 3, 4, 5]] ) _SCREAMING_SNAKE_CASE = model(A__ )[0] # TODO Replace vocab size _SCREAMING_SNAKE_CASE = 5_00_00 _SCREAMING_SNAKE_CASE = [1, 6, vocab_size] self.assertEqual(output.shape , A__ ) print(output[:, :3, :3] ) # TODO Replace values below with what was printed above. _SCREAMING_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] , A__ , atol=1E-4 ) @require_tf class _a (unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = 1E-4 def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = tf.constant([[4, 10]] ) _SCREAMING_SNAKE_CASE = TFRoFormerSinusoidalPositionalEmbedding(num_positions=6 , embedding_dim=6 ) _SCREAMING_SNAKE_CASE = emba(input_ids.shape ) _SCREAMING_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(A__ , A__ , atol=self.tolerance ) def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_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], ] ) _SCREAMING_SNAKE_CASE = TFRoFormerSinusoidalPositionalEmbedding(num_positions=5_12 , embedding_dim=5_12 ) emba([2, 16, 5_12] ) _SCREAMING_SNAKE_CASE = emba.weight[:3, :5] tf.debugging.assert_near(A__ , A__ , atol=self.tolerance ) @require_tf class _a (unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = 1E-4 def UpperCamelCase ( self ) -> int: # 2,12,16,64 _SCREAMING_SNAKE_CASE = tf.reshape(tf.range(2 * 12 * 16 * 64 , dtype=tf.floataa ) , shape=(2, 12, 16, 64) ) / 1_00 _SCREAMING_SNAKE_CASE = -tf.reshape(tf.range(2 * 12 * 16 * 64 , dtype=tf.floataa ) , shape=(2, 12, 16, 64) ) / 1_00 _SCREAMING_SNAKE_CASE = TFRoFormerSinusoidalPositionalEmbedding(num_positions=32 , embedding_dim=64 ) _SCREAMING_SNAKE_CASE = embed_positions([2, 16, 7_68] )[None, None, :, :] _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = TFRoFormerSelfAttention.apply_rotary_position_embeddings( A__ , A__ , A__ ) _SCREAMING_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], ] ) _SCREAMING_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] , A__ , atol=self.tolerance ) tf.debugging.assert_near(key_layer[0, 0, :6, :8] , A__ , atol=self.tolerance )
0
1
'''simple docstring''' import math from collections.abc import Iterator from itertools import takewhile def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> bool: """simple docstring""" if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(SCREAMING_SNAKE_CASE_ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def lowerCAmelCase_ ( ) -> Iterator[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = 2 while True: if is_prime(SCREAMING_SNAKE_CASE_ ): yield num num += 1 def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ = 2_00_00_00 ) -> int: """simple docstring""" return sum(takewhile(lambda SCREAMING_SNAKE_CASE_ : x < n , prime_generator() ) ) if __name__ == "__main__": print(f"""{solution() = }""")
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available UpperCamelCase__ : int = {"tokenization_herbert": ["HerbertTokenizer"]} try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Tuple = ["HerbertTokenizerFast"] if TYPE_CHECKING: from .tokenization_herbert import HerbertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_herbert_fast import HerbertTokenizerFast else: import sys UpperCamelCase__ : Tuple = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
0
1
'''simple docstring''' from pathlib import Path from typing import List from transformers import is_torch_available, is_vision_available from transformers.testing_utils import get_tests_dir, is_tool_test from transformers.tools.agent_types import AGENT_TYPE_MAPPING, AgentAudio, AgentImage, AgentText if is_torch_available(): import torch if is_vision_available(): from PIL import Image UpperCamelCase__ : str = ["text", "image", "audio"] def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" _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((5_12, 5_12) ) ) elif input_type == "audio": inputs.append(torch.ones(30_00 ) ) elif isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): inputs.append(create_inputs(SCREAMING_SNAKE_CASE_ ) ) else: raise ValueError(F"Invalid type requested: {input_type}" ) return inputs def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = [] for output in outputs: if isinstance(SCREAMING_SNAKE_CASE_ , (str, AgentText) ): output_types.append("""text""" ) elif isinstance(SCREAMING_SNAKE_CASE_ , (Image.Image, AgentImage) ): output_types.append("""image""" ) elif isinstance(SCREAMING_SNAKE_CASE_ , (torch.Tensor, AgentAudio) ): output_types.append("""audio""" ) else: raise ValueError(F"Invalid output: {output}" ) return output_types @is_tool_test class _a : """simple docstring""" def UpperCamelCase ( self ) -> Optional[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 UpperCamelCase ( self ) -> Dict: _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 UpperCamelCase ( self ) -> Optional[int]: 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 ) -> 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 UpperCamelCase ( 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 ) )
0
'''simple docstring''' import argparse import gdown import numpy as np import torch from huggingface_hub import hf_hub_download from transformers import ( CLIPTokenizer, CLIPTokenizerFast, VideoMAEImageProcessor, XCLIPConfig, XCLIPModel, XCLIPProcessor, XCLIPTextConfig, XCLIPVisionConfig, ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE = XCLIPTextConfig() # derive patch size from model name _SCREAMING_SNAKE_CASE = model_name.find("""patch""" ) _SCREAMING_SNAKE_CASE = int(model_name[start_idx + len("""patch""" ) : start_idx + len("""patch""" ) + 2] ) _SCREAMING_SNAKE_CASE = XCLIPVisionConfig(patch_size=SCREAMING_SNAKE_CASE_ , num_frames=SCREAMING_SNAKE_CASE_ ) if "large" in model_name: _SCREAMING_SNAKE_CASE = 7_68 _SCREAMING_SNAKE_CASE = 30_72 _SCREAMING_SNAKE_CASE = 12 _SCREAMING_SNAKE_CASE = 10_24 _SCREAMING_SNAKE_CASE = 40_96 _SCREAMING_SNAKE_CASE = 16 _SCREAMING_SNAKE_CASE = 24 _SCREAMING_SNAKE_CASE = 7_68 _SCREAMING_SNAKE_CASE = 30_72 if model_name == "xclip-large-patch14-16-frames": _SCREAMING_SNAKE_CASE = 3_36 _SCREAMING_SNAKE_CASE = XCLIPConfig.from_text_vision_configs(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if "large" in model_name: _SCREAMING_SNAKE_CASE = 7_68 return config def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" # text encoder if name == "token_embedding.weight": _SCREAMING_SNAKE_CASE = name.replace("""token_embedding.weight""" , """text_model.embeddings.token_embedding.weight""" ) if name == "positional_embedding": _SCREAMING_SNAKE_CASE = name.replace("""positional_embedding""" , """text_model.embeddings.position_embedding.weight""" ) if "ln_1" in name: _SCREAMING_SNAKE_CASE = name.replace("""ln_1""" , """layer_norm1""" ) if "ln_2" in name: _SCREAMING_SNAKE_CASE = name.replace("""ln_2""" , """layer_norm2""" ) if "c_fc" in name: _SCREAMING_SNAKE_CASE = name.replace("""c_fc""" , """fc1""" ) if "c_proj" in name: _SCREAMING_SNAKE_CASE = name.replace("""c_proj""" , """fc2""" ) if name.startswith("""transformer.resblocks""" ): _SCREAMING_SNAKE_CASE = name.replace("""transformer.resblocks""" , """text_model.encoder.layers""" ) if "attn.out_proj" in name and "message" not in name: _SCREAMING_SNAKE_CASE = name.replace("""attn.out_proj""" , """self_attn.out_proj""" ) if "ln_final" in name: _SCREAMING_SNAKE_CASE = name.replace("""ln_final""" , """text_model.final_layer_norm""" ) # visual encoder if name == "visual.class_embedding": _SCREAMING_SNAKE_CASE = name.replace("""visual.class_embedding""" , """vision_model.embeddings.class_embedding""" ) if name == "visual.positional_embedding": _SCREAMING_SNAKE_CASE = name.replace("""visual.positional_embedding""" , """vision_model.embeddings.position_embedding.weight""" ) if name.startswith("""visual.transformer.resblocks""" ): _SCREAMING_SNAKE_CASE = name.replace("""visual.transformer.resblocks""" , """vision_model.encoder.layers""" ) if "visual.conv1" in name: _SCREAMING_SNAKE_CASE = name.replace("""visual.conv1""" , """vision_model.embeddings.patch_embedding""" ) if "visual.ln_pre" in name: _SCREAMING_SNAKE_CASE = name.replace("""visual.ln_pre""" , """vision_model.pre_layernorm""" ) if "visual.ln_post" in name: _SCREAMING_SNAKE_CASE = name.replace("""visual.ln_post""" , """vision_model.post_layernorm""" ) if "visual.proj" in name: _SCREAMING_SNAKE_CASE = name.replace("""visual.proj""" , """visual_projection.weight""" ) if "text_projection" in name: _SCREAMING_SNAKE_CASE = name.replace("""text_projection""" , """text_projection.weight""" ) # things on top if "prompts_visual_proj" in name: _SCREAMING_SNAKE_CASE = name.replace("""prompts_visual_proj""" , """prompts_visual_projection""" ) if "prompts_visual_ln" in name: _SCREAMING_SNAKE_CASE = name.replace("""prompts_visual_ln""" , """prompts_visual_layernorm""" ) # mit if name == "mit.positional_embedding": _SCREAMING_SNAKE_CASE = name.replace("""positional""" , """position""" ) if name.startswith("""mit.resblocks""" ): _SCREAMING_SNAKE_CASE = name.replace("""mit.resblocks""" , """mit.encoder.layers""" ) # prompts generator if name.startswith("""prompts_generator.norm""" ): _SCREAMING_SNAKE_CASE = name.replace("""prompts_generator.norm""" , """prompts_generator.layernorm""" ) return name def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" for key in orig_state_dict.copy().keys(): _SCREAMING_SNAKE_CASE = orig_state_dict.pop(SCREAMING_SNAKE_CASE_ ) if "attn.in_proj" in key: _SCREAMING_SNAKE_CASE = key.split(""".""" ) if key.startswith("""visual""" ): _SCREAMING_SNAKE_CASE = key_split[3] _SCREAMING_SNAKE_CASE = config.vision_config.hidden_size if "message_attn" in key: if "weight" in key: _SCREAMING_SNAKE_CASE = val[ :dim, : ] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2, : ] _SCREAMING_SNAKE_CASE = val[ -dim:, : ] else: _SCREAMING_SNAKE_CASE = val[ :dim ] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2 ] _SCREAMING_SNAKE_CASE = val[ -dim: ] else: if "weight" in key: _SCREAMING_SNAKE_CASE = val[ :dim, : ] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2, : ] _SCREAMING_SNAKE_CASE = val[ -dim:, : ] else: _SCREAMING_SNAKE_CASE = val[:dim] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2 ] _SCREAMING_SNAKE_CASE = val[-dim:] elif key.startswith("""mit""" ): _SCREAMING_SNAKE_CASE = key_split[2] _SCREAMING_SNAKE_CASE = config.vision_config.mit_hidden_size if "weight" in key: _SCREAMING_SNAKE_CASE = val[:dim, :] _SCREAMING_SNAKE_CASE = val[dim : dim * 2, :] _SCREAMING_SNAKE_CASE = val[-dim:, :] else: _SCREAMING_SNAKE_CASE = val[:dim] _SCREAMING_SNAKE_CASE = val[dim : dim * 2] _SCREAMING_SNAKE_CASE = val[-dim:] else: _SCREAMING_SNAKE_CASE = key_split[2] _SCREAMING_SNAKE_CASE = config.text_config.hidden_size if "weight" in key: _SCREAMING_SNAKE_CASE = val[:dim, :] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2, : ] _SCREAMING_SNAKE_CASE = val[-dim:, :] else: _SCREAMING_SNAKE_CASE = val[:dim] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2 ] _SCREAMING_SNAKE_CASE = val[-dim:] else: _SCREAMING_SNAKE_CASE = rename_key(SCREAMING_SNAKE_CASE_ ) if new_key_name in ["visual_projection.weight", "text_projection.weight"]: _SCREAMING_SNAKE_CASE = val.T _SCREAMING_SNAKE_CASE = val return orig_state_dict def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" if num_frames == 8: _SCREAMING_SNAKE_CASE = """eating_spaghetti_8_frames.npy""" elif num_frames == 16: _SCREAMING_SNAKE_CASE = """eating_spaghetti.npy""" elif num_frames == 32: _SCREAMING_SNAKE_CASE = """eating_spaghetti_32_frames.npy""" _SCREAMING_SNAKE_CASE = hf_hub_download( repo_id="""hf-internal-testing/spaghetti-video""" , filename=SCREAMING_SNAKE_CASE_ , repo_type="""dataset""" , ) _SCREAMING_SNAKE_CASE = np.load(SCREAMING_SNAKE_CASE_ ) return list(SCREAMING_SNAKE_CASE_ ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_=False ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = { # fully supervised kinetics-400 checkpoints """xclip-base-patch32""": """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_8.pth""", """xclip-base-patch32-16-frames""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_16.pth""" ), """xclip-base-patch16""": """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_8.pth""", """xclip-base-patch16-16-frames""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_16.pth""" ), """xclip-large-patch14""": """https://drive.google.com/u/0/uc?id=1NUOImq0o5DlQTST17iIP3vG7DgmHQuCx&amp;export=download&amp;confirm=t&amp;uuid=b26caedc-88e2-473e-830a-9d158b653cdb""", """xclip-large-patch14-16-frames""": """https://drive.google.com/u/0/uc?id=1FOYgnJc097OJ4lGwtRCCydQyVPJEOH7d&amp;export=download&amp;confirm=t&amp;uuid=538fa810-e671-4050-b385-9a623f89804f""", # fully supervised kinetics-600 checkpoints """xclip-base-patch16-kinetics-600""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_8.pth""" ), """xclip-base-patch16-kinetics-600-16-frames""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_16.pth""" ), """xclip-large-patch14-kinetics-600""": """https://drive.google.com/u/0/uc?id=1FV8C1INuM91sLAN4ImjzePLIlpMSihwV&amp;export=download&amp;confirm=t&amp;uuid=141d4977-4a65-44ae-864f-4b0c19f838be""", # few shot """xclip-base-patch16-hmdb-2-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_2.pth""" ), """xclip-base-patch16-hmdb-4-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_4.pth""" ), """xclip-base-patch16-hmdb-8-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_8.pth""" ), """xclip-base-patch16-hmdb-16-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_16.pth""" ), """xclip-base-patch16-ucf-2-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_2.pth""" ), """xclip-base-patch16-ucf-4-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_4.pth""" ), """xclip-base-patch16-ucf-8-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_8.pth""" ), """xclip-base-patch16-ucf-16-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_16.pth""" ), # zero shot """xclip-base-patch16-zero-shot""": """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/zero.pth""", } _SCREAMING_SNAKE_CASE = model_to_url[model_name] _SCREAMING_SNAKE_CASE = 8 if "16-frames" in model_name: _SCREAMING_SNAKE_CASE = 16 elif "shot" in model_name: _SCREAMING_SNAKE_CASE = 32 _SCREAMING_SNAKE_CASE = get_xclip_config(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = XCLIPModel(SCREAMING_SNAKE_CASE_ ) model.eval() if "drive" in checkpoint_url: _SCREAMING_SNAKE_CASE = """pytorch_model.bin""" gdown.cached_download(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , quiet=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = torch.load(SCREAMING_SNAKE_CASE_ , map_location="""cpu""" )["""model"""] else: _SCREAMING_SNAKE_CASE = torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE_ )["""model"""] _SCREAMING_SNAKE_CASE = convert_state_dict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = XCLIPModel(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = model.load_state_dict(SCREAMING_SNAKE_CASE_ , strict=SCREAMING_SNAKE_CASE_ ) assert missing_keys == ["text_model.embeddings.position_ids", "vision_model.embeddings.position_ids"] model.eval() _SCREAMING_SNAKE_CASE = 3_36 if model_name == """xclip-large-patch14-16-frames""" else 2_24 _SCREAMING_SNAKE_CASE = VideoMAEImageProcessor(size=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = CLIPTokenizer.from_pretrained("""openai/clip-vit-base-patch32""" ) _SCREAMING_SNAKE_CASE = CLIPTokenizerFast.from_pretrained("""openai/clip-vit-base-patch32""" ) _SCREAMING_SNAKE_CASE = XCLIPProcessor(image_processor=SCREAMING_SNAKE_CASE_ , tokenizer=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = prepare_video(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = processor( text=["""playing sports""", """eating spaghetti""", """go shopping"""] , videos=SCREAMING_SNAKE_CASE_ , return_tensors="""pt""" , padding=SCREAMING_SNAKE_CASE_ ) print("""Shape of pixel values:""" , inputs.pixel_values.shape ) with torch.no_grad(): _SCREAMING_SNAKE_CASE = model(**SCREAMING_SNAKE_CASE_ ) # Verify outputs _SCREAMING_SNAKE_CASE = outputs.logits_per_video _SCREAMING_SNAKE_CASE = logits_per_video.softmax(dim=1 ) print("""Probs:""" , SCREAMING_SNAKE_CASE_ ) # kinetics-400 if model_name == "xclip-base-patch32": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0019, 0.9951, 0.0030]] ) elif model_name == "xclip-base-patch32-16-frames": _SCREAMING_SNAKE_CASE = torch.tensor([[7.0999e-04, 9.9883e-01, 4.5580e-04]] ) elif model_name == "xclip-base-patch16": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0083, 0.9681, 0.0236]] ) elif model_name == "xclip-base-patch16-16-frames": _SCREAMING_SNAKE_CASE = torch.tensor([[7.6937e-04, 9.9728e-01, 1.9473e-03]] ) elif model_name == "xclip-large-patch14": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0062, 0.9864, 0.0075]] ) elif model_name == "xclip-large-patch14-16-frames": _SCREAMING_SNAKE_CASE = torch.tensor([[3.3877e-04, 9.9937e-01, 2.8888e-04]] ) # kinetics-600 elif model_name == "xclip-base-patch16-kinetics-600": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0555, 0.8914, 0.0531]] ) elif model_name == "xclip-base-patch16-kinetics-600-16-frames": _SCREAMING_SNAKE_CASE = torch.tensor([[3.8554e-04, 9.9929e-01, 3.2754e-04]] ) elif model_name == "xclip-large-patch14-kinetics-600": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0036, 0.9920, 0.0045]] ) # few shot elif model_name == "xclip-base-patch16-hmdb-2-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[7.1890e-06, 9.9994e-01, 5.6559e-05]] ) elif model_name == "xclip-base-patch16-hmdb-4-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[1.0320e-05, 9.9993e-01, 6.2435e-05]] ) elif model_name == "xclip-base-patch16-hmdb-8-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[4.1377e-06, 9.9990e-01, 9.8386e-05]] ) elif model_name == "xclip-base-patch16-hmdb-16-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[4.1347e-05, 9.9962e-01, 3.3411e-04]] ) elif model_name == "xclip-base-patch16-ucf-2-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[8.5857e-05, 9.9928e-01, 6.3291e-04]] ) elif model_name == "xclip-base-patch16-ucf-4-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[8.5857e-05, 9.9928e-01, 6.3291e-04]] ) elif model_name == "xclip-base-patch16-ucf-8-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0027, 0.9904, 0.0070]] ) elif model_name == "xclip-base-patch16-ucf-16-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[9.8219e-04, 9.9593e-01, 3.0863e-03]] ) # zero shot elif model_name == "xclip-base-patch16-zero-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[3.5082e-04, 9.9785e-01, 1.7966e-03]] ) else: raise ValueError(F"Model name {model_name} not supported" ) assert torch.allclose(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , atol=1e-3 ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: print(F"Saving model {model_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(SCREAMING_SNAKE_CASE_ ) if push_to_hub: print("""Pushing model, processor and slow tokenizer files to the hub...""" ) model.push_to_hub(SCREAMING_SNAKE_CASE_ , organization="""nielsr""" ) processor.push_to_hub(SCREAMING_SNAKE_CASE_ , organization="""nielsr""" ) slow_tokenizer.push_to_hub(SCREAMING_SNAKE_CASE_ , organization="""nielsr""" ) if __name__ == "__main__": UpperCamelCase__ : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default="xclip-base-patch32", type=str, help="Name of the model.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether or not to push the converted model to the 🤗 hub." ) UpperCamelCase__ : str = parser.parse_args() convert_xclip_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
0
1
'''simple docstring''' import gzip import hashlib import json import multiprocessing import os import re import shutil import time from pathlib import Path import numpy as np from arguments import PreprocessingArguments from datasets import load_dataset from minhash_deduplication import deduplicate_dataset from transformers import AutoTokenizer, HfArgumentParser UpperCamelCase__ : Optional[Any] = re.compile(R"\s+") def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" return {"hash": hashlib.mda(re.sub(SCREAMING_SNAKE_CASE_ , """""" , example["""content"""] ).encode("""utf-8""" ) ).hexdigest()} def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = [len(SCREAMING_SNAKE_CASE_ ) for line in example["""content"""].splitlines()] return {"line_mean": np.mean(SCREAMING_SNAKE_CASE_ ), "line_max": max(SCREAMING_SNAKE_CASE_ )} def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = np.mean([c.isalnum() for c in example["""content"""]] ) return {"alpha_frac": alpha_frac} def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: """simple docstring""" if example["hash"] in uniques: uniques.remove(example["""hash"""] ) return True else: return False def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=5 ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE = ["""auto-generated""", """autogenerated""", """automatically generated"""] _SCREAMING_SNAKE_CASE = example["""content"""].splitlines() for _, line in zip(range(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ): for keyword in keywords: if keyword in line.lower(): return {"autogenerated": True} else: return {"autogenerated": False} def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=5 , SCREAMING_SNAKE_CASE_=0.05 ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = ["""unit tests""", """test file""", """configuration file"""] _SCREAMING_SNAKE_CASE = example["""content"""].splitlines() _SCREAMING_SNAKE_CASE = 0 _SCREAMING_SNAKE_CASE = 0 # first test for _, line in zip(range(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ): for keyword in keywords: if keyword in line.lower(): return {"config_or_test": True} # second test _SCREAMING_SNAKE_CASE = example["""content"""].count("""\n""" ) _SCREAMING_SNAKE_CASE = int(coeff * nlines ) for line in lines: count_config += line.lower().count("""config""" ) count_test += line.lower().count("""test""" ) if count_config > threshold or count_test > threshold: return {"config_or_test": True} return {"config_or_test": False} def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = ["""def """, """class """, """for """, """while """] _SCREAMING_SNAKE_CASE = example["""content"""].splitlines() for line in lines: for keyword in keywords: if keyword in line.lower(): return {"has_no_keywords": False} return {"has_no_keywords": True} def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=4 ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE = example["""content"""].splitlines() _SCREAMING_SNAKE_CASE = 0 for line in lines: counter += line.lower().count("""=""" ) if counter > minimum: return {"has_few_assignments": False} return {"has_few_assignments": True} def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE = tokenizer(example["""content"""] , truncation=SCREAMING_SNAKE_CASE_ )["""input_ids"""] _SCREAMING_SNAKE_CASE = len(example["""content"""] ) / len(SCREAMING_SNAKE_CASE_ ) return {"ratio": ratio} def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE = {} results.update(get_hash(SCREAMING_SNAKE_CASE_ ) ) results.update(line_stats(SCREAMING_SNAKE_CASE_ ) ) results.update(alpha_stats(SCREAMING_SNAKE_CASE_ ) ) results.update(char_token_ratio(SCREAMING_SNAKE_CASE_ ) ) results.update(is_autogenerated(SCREAMING_SNAKE_CASE_ ) ) results.update(is_config_or_test(SCREAMING_SNAKE_CASE_ ) ) results.update(has_no_keywords(SCREAMING_SNAKE_CASE_ ) ) results.update(has_few_assignments(SCREAMING_SNAKE_CASE_ ) ) return results def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" if not check_uniques(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): return False elif example["autogenerated"]: return False elif example["line_max"] > args.line_max: return False elif example["line_mean"] > args.line_mean: return False elif example["alpha_frac"] < args.alpha_frac: return False elif example["ratio"] < args.min_token_ratio: return False elif example["config_or_test"] and np.random.rand() <= args.filter_proba: return False elif example["has_no_keywords"] and np.random.rand() <= args.filter_proba: return False elif example["has_few_assignments"]: return False else: return True def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" with open(SCREAMING_SNAKE_CASE_ , """rb""" ) as f_in: with gzip.open(str(SCREAMING_SNAKE_CASE_ ) + """.gz""" , """wb""" , compresslevel=6 ) as f_out: shutil.copyfileobj(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) os.unlink(SCREAMING_SNAKE_CASE_ ) # Settings UpperCamelCase__ : List[str] = HfArgumentParser(PreprocessingArguments) UpperCamelCase__ : List[Any] = parser.parse_args() if args.num_workers is None: UpperCamelCase__ : Optional[int] = multiprocessing.cpu_count() UpperCamelCase__ : List[Any] = AutoTokenizer.from_pretrained(args.tokenizer_dir) # Load dataset UpperCamelCase__ : Union[str, Any] = time.time() UpperCamelCase__ : Optional[Any] = load_dataset(args.dataset_name, split="train") print(f"""Time to load dataset: {time.time()-t_start:.2f}""") # Run preprocessing UpperCamelCase__ : Any = time.time() UpperCamelCase__ : Any = ds.map(preprocess, num_proc=args.num_workers) print(f"""Time to preprocess dataset: {time.time()-t_start:.2f}""") # Deduplicate hashes UpperCamelCase__ : int = set(ds.unique("hash")) UpperCamelCase__ : Optional[int] = len(uniques) / len(ds) print(f"""Fraction of duplicates: {1-frac:.2%}""") # Deduplicate data and apply heuristics UpperCamelCase__ : Optional[Any] = time.time() UpperCamelCase__ : Tuple = ds.filter(filter, fn_kwargs={"uniques": uniques, "args": args}) print(f"""Time to filter dataset: {time.time()-t_start:.2f}""") print(f"""Size of filtered dataset: {len(ds_filter)}""") # Deduplicate with minhash and jaccard similarity if args.near_deduplication: UpperCamelCase__ : Optional[int] = time.time() UpperCamelCase__ , UpperCamelCase__ : List[Any] = deduplicate_dataset(ds_filter, args.jaccard_threshold) print(f"""Time to deduplicate dataset: {time.time()-t_start:.2f}""") print(f"""Size of deduplicate dataset: {len(ds_filter)}""") # Save data in batches of samples_per_file UpperCamelCase__ : str = Path(args.output_dir) output_dir.mkdir(exist_ok=True) # save duplicate_clusters in the output_dir as artifacts # not sure it is the right place the save it if args.near_deduplication: with open(output_dir / "duplicate_clusters.json", "w") as f: json.dump(duplicate_clusters, f) UpperCamelCase__ : Tuple = output_dir / "data" data_dir.mkdir(exist_ok=True) UpperCamelCase__ : Optional[Any] = time.time() for file_number, index in enumerate(range(0, len(ds_filter), args.samples_per_file)): UpperCamelCase__ : List[str] = str(data_dir / f"""file-{file_number+1:012}.json""") UpperCamelCase__ : Optional[int] = min(len(ds_filter), index + args.samples_per_file) ds_filter.select(list(range(index, end_index))).to_json(file_path) compress_file(file_path) print(f"""Time to save dataset: {time.time()-t_start:.2f}""")
0
'''simple docstring''' import numpy as np import torch from torch.utils.data import Dataset from utils import logger class _a (_lowerCamelCase): """simple docstring""" def __init__( self , A__ , A__ ) -> Any: _SCREAMING_SNAKE_CASE = params _SCREAMING_SNAKE_CASE = np.array(A__ ) _SCREAMING_SNAKE_CASE = np.array([len(A__ ) for t in data] ) self.check() self.remove_long_sequences() self.remove_empty_sequences() self.remove_unknown_sequences() self.check() self.print_statistics() def __getitem__( self , A__ ) -> Dict: return (self.token_ids[index], self.lengths[index]) def __len__( self ) -> Tuple: return len(self.lengths ) def UpperCamelCase ( self ) -> Dict: assert len(self.token_ids ) == len(self.lengths ) assert all(self.lengths[i] == len(self.token_ids[i] ) for i in range(len(self.lengths ) ) ) def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = self.params.max_model_input_size _SCREAMING_SNAKE_CASE = self.lengths > max_len logger.info(F"Splitting {sum(A__ )} too long sequences." ) def divide_chunks(A__ , A__ ): return [l[i : i + n] for i in range(0 , len(A__ ) , A__ )] _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = [] if self.params.mlm: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self.params.special_tok_ids["""cls_token"""], self.params.special_tok_ids["""sep_token"""] else: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 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: _SCREAMING_SNAKE_CASE = [] for sub_s in divide_chunks(seq_ , max_len - 2 ): if sub_s[0] != cls_id: _SCREAMING_SNAKE_CASE = np.insert(A__ , 0 , A__ ) if sub_s[-1] != sep_id: _SCREAMING_SNAKE_CASE = np.insert(A__ , len(A__ ) , A__ ) assert len(A__ ) <= max_len assert (sub_s[0] == cls_id) and (sub_s[-1] == sep_id), sub_s sub_seqs.append(A__ ) new_tok_ids.extend(A__ ) new_lengths.extend([len(A__ ) for l in sub_seqs] ) _SCREAMING_SNAKE_CASE = np.array(A__ ) _SCREAMING_SNAKE_CASE = np.array(A__ ) def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = len(self ) _SCREAMING_SNAKE_CASE = self.lengths > 11 _SCREAMING_SNAKE_CASE = self.token_ids[indices] _SCREAMING_SNAKE_CASE = self.lengths[indices] _SCREAMING_SNAKE_CASE = len(self ) logger.info(F"Remove {init_size - new_size} too short (<=11 tokens) sequences." ) def UpperCamelCase ( self ) -> int: if "unk_token" not in self.params.special_tok_ids: return else: _SCREAMING_SNAKE_CASE = self.params.special_tok_ids["""unk_token"""] _SCREAMING_SNAKE_CASE = len(self ) _SCREAMING_SNAKE_CASE = np.array([np.count_nonzero(a == unk_token_id ) for a in self.token_ids] ) _SCREAMING_SNAKE_CASE = (unk_occs / self.lengths) < 0.5 _SCREAMING_SNAKE_CASE = self.token_ids[indices] _SCREAMING_SNAKE_CASE = self.lengths[indices] _SCREAMING_SNAKE_CASE = len(self ) logger.info(F"Remove {init_size - new_size} sequences with a high level of unknown tokens (50%)." ) def UpperCamelCase ( self ) -> Optional[Any]: if not self.params.is_master: return logger.info(F"{len(self )} sequences" ) # data_len = sum(self.lengths) # nb_unique_tokens = len(Counter(list(chain(*self.token_ids)))) # logger.info(f'{data_len} tokens ({nb_unique_tokens} unique)') # unk_idx = self.params.special_tok_ids['unk_token'] # nb_unknown = sum([(t==unk_idx).sum() for t in self.token_ids]) # logger.info(f'{nb_unknown} unknown tokens (covering {100*nb_unknown/data_len:.2f}% of the data)') def UpperCamelCase ( self , A__ ) -> Any: _SCREAMING_SNAKE_CASE = [t[0] for t in batch] _SCREAMING_SNAKE_CASE = [t[1] for t in batch] assert len(A__ ) == len(A__ ) # Max for paddings _SCREAMING_SNAKE_CASE = max(A__ ) # Pad token ids if self.params.mlm: _SCREAMING_SNAKE_CASE = self.params.special_tok_ids["""pad_token"""] else: _SCREAMING_SNAKE_CASE = self.params.special_tok_ids["""unk_token"""] _SCREAMING_SNAKE_CASE = [list(t.astype(A__ ) ) + [pad_idx] * (max_seq_len_ - len(A__ )) for t in token_ids] assert len(tk_ ) == len(A__ ) assert all(len(A__ ) == max_seq_len_ for t in tk_ ) _SCREAMING_SNAKE_CASE = torch.tensor(tk_ ) # (bs, max_seq_len_) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) # (bs) return tk_t, lg_t
0
1
'''simple docstring''' # limitations under the License. from typing import Optional, Tuple, Union import torch from diffusers import DiffusionPipeline, ImagePipelineOutput class _a (_lowerCamelCase): """simple docstring""" def __init__( self , A__ , A__ ) -> Union[str, Any]: super().__init__() self.register_modules(unet=A__ , scheduler=A__ ) @torch.no_grad() def __call__( self , A__ = 1 , A__ = None , A__ = 50 , A__ = "pil" , A__ = True , **A__ , ) -> Union[ImagePipelineOutput, Tuple]: _SCREAMING_SNAKE_CASE = torch.randn( (batch_size, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size) , generator=A__ , ) _SCREAMING_SNAKE_CASE = image.to(self.device ) # set step values self.scheduler.set_timesteps(A__ ) for t in self.progress_bar(self.scheduler.timesteps ): # 1. predict noise model_output _SCREAMING_SNAKE_CASE = self.unet(A__ , A__ ).sample # 2. predict previous mean of image x_t-1 and add variance depending on eta # eta corresponds to η in paper and should be between [0, 1] # do x_t -> x_t-1 _SCREAMING_SNAKE_CASE = self.scheduler.step(A__ , A__ , A__ ).prev_sample _SCREAMING_SNAKE_CASE = (image / 2 + 0.5).clamp(0 , 1 ) _SCREAMING_SNAKE_CASE = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": _SCREAMING_SNAKE_CASE = self.numpy_to_pil(A__ ) if not return_dict: return (image,), "This is a local test" return ImagePipelineOutput(images=A__ ), "This is a local test"
0
'''simple docstring''' 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 UpperCamelCase__ : List[Any] = logging.get_logger(__name__) UpperCamelCase__ : Any = "▁" UpperCamelCase__ : Any = {"vocab_file": "spiece.model"} UpperCamelCase__ : int = { "vocab_file": { "google/reformer-crime-and-punishment": ( "https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/spiece.model" ) } } UpperCamelCase__ : Optional[int] = { "google/reformer-crime-and-punishment": 524_288, } class _a (_lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE = ['input_ids', 'attention_mask'] def __init__( self , A__ , A__="</s>" , A__="<unk>" , A__=[] , A__ = None , **A__ , ) -> None: _SCREAMING_SNAKE_CASE = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( eos_token=A__ , unk_token=A__ , additional_special_tokens=A__ , sp_model_kwargs=self.sp_model_kwargs , **A__ , ) _SCREAMING_SNAKE_CASE = vocab_file _SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(A__ ) @property def UpperCamelCase ( self ) -> Any: return self.sp_model.get_piece_size() def UpperCamelCase ( self ) -> Dict[str, int]: _SCREAMING_SNAKE_CASE = {self.convert_ids_to_tokens(A__ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ) -> int: _SCREAMING_SNAKE_CASE = self.__dict__.copy() _SCREAMING_SNAKE_CASE = None return state def __setstate__( self , A__ ) -> str: _SCREAMING_SNAKE_CASE = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): _SCREAMING_SNAKE_CASE = {} _SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCamelCase ( self , A__ ) -> List[str]: return self.sp_model.encode(A__ , out_type=A__ ) def UpperCamelCase ( self , A__ ) -> Union[str, Any]: return self.sp_model.piece_to_id(A__ ) def UpperCamelCase ( self , A__ ) -> List[Any]: if index < self.sp_model.get_piece_size(): _SCREAMING_SNAKE_CASE = self.sp_model.IdToPiece(A__ ) return token def UpperCamelCase ( self , A__ ) -> str: _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = """""" 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(A__ ) + token _SCREAMING_SNAKE_CASE = [] else: current_sub_tokens.append(A__ ) out_string += self.sp_model.decode(A__ ) return out_string.strip() def UpperCamelCase ( self , A__ , A__ = None ) -> Tuple[str]: if not os.path.isdir(A__ ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return _SCREAMING_SNAKE_CASE = os.path.join( A__ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(A__ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , A__ ) elif not os.path.isfile(self.vocab_file ): with open(A__ , """wb""" ) as fi: _SCREAMING_SNAKE_CASE = self.sp_model.serialized_model_proto() fi.write(A__ ) return (out_vocab_file,)
0
1
'''simple docstring''' def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): raise ValueError("""multiplicative_persistence() only accepts integral values""" ) if num < 0: raise ValueError("""multiplicative_persistence() does not accept negative values""" ) _SCREAMING_SNAKE_CASE = 0 _SCREAMING_SNAKE_CASE = str(SCREAMING_SNAKE_CASE_ ) while len(SCREAMING_SNAKE_CASE_ ) != 1: _SCREAMING_SNAKE_CASE = [int(SCREAMING_SNAKE_CASE_ ) for i in num_string] _SCREAMING_SNAKE_CASE = 1 for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) ): total *= numbers[i] _SCREAMING_SNAKE_CASE = str(SCREAMING_SNAKE_CASE_ ) steps += 1 return steps def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): raise ValueError("""additive_persistence() only accepts integral values""" ) if num < 0: raise ValueError("""additive_persistence() does not accept negative values""" ) _SCREAMING_SNAKE_CASE = 0 _SCREAMING_SNAKE_CASE = str(SCREAMING_SNAKE_CASE_ ) while len(SCREAMING_SNAKE_CASE_ ) != 1: _SCREAMING_SNAKE_CASE = [int(SCREAMING_SNAKE_CASE_ ) for i in num_string] _SCREAMING_SNAKE_CASE = 0 for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) ): total += numbers[i] _SCREAMING_SNAKE_CASE = str(SCREAMING_SNAKE_CASE_ ) steps += 1 return steps if __name__ == "__main__": import doctest doctest.testmod()
0
'''simple docstring''' import os import unittest from transformers import MobileBertTokenizer, MobileBertTokenizerFast from transformers.models.bert.tokenization_bert import ( VOCAB_FILES_NAMES, BasicTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin, filter_non_english @require_tokenizers class _a (_lowerCamelCase , unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = MobileBertTokenizer SCREAMING_SNAKE_CASE = MobileBertTokenizerFast SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = filter_non_english SCREAMING_SNAKE_CASE = 'google/mobilebert-uncased' def UpperCamelCase ( self ) -> Any: super().setUp() _SCREAMING_SNAKE_CASE = [ """[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest""", ] _SCREAMING_SNAKE_CASE = 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] ) ) _SCREAMING_SNAKE_CASE = [ (tokenizer_def[0], self.pre_trained_model_path, tokenizer_def[2]) # else the 'google/' prefix is stripped for tokenizer_def in self.tokenizers_list ] def UpperCamelCase ( self , A__ ) -> List[str]: _SCREAMING_SNAKE_CASE = """UNwant\u00E9d,running""" _SCREAMING_SNAKE_CASE = """unwanted, running""" return input_text, output_text def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = self.tokenizer_class(self.vocab_file ) _SCREAMING_SNAKE_CASE = tokenizer.tokenize("""UNwant\u00E9d,running""" ) self.assertListEqual(A__ , ["""un""", """##want""", """##ed""", """,""", """runn""", """##ing"""] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(A__ ) , [9, 6, 7, 12, 10, 11] ) def UpperCamelCase ( self ) -> Optional[int]: if not self.test_rust_tokenizer: return _SCREAMING_SNAKE_CASE = self.get_tokenizer() _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() _SCREAMING_SNAKE_CASE = """UNwant\u00E9d,running""" _SCREAMING_SNAKE_CASE = tokenizer.tokenize(A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.tokenize(A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = tokenizer.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.encode(A__ , add_special_tokens=A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() _SCREAMING_SNAKE_CASE = tokenizer.encode(A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.encode(A__ ) self.assertListEqual(A__ , A__ ) # With lower casing _SCREAMING_SNAKE_CASE = self.get_tokenizer(do_lower_case=A__ ) _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer(do_lower_case=A__ ) _SCREAMING_SNAKE_CASE = """UNwant\u00E9d,running""" _SCREAMING_SNAKE_CASE = tokenizer.tokenize(A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.tokenize(A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = tokenizer.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.encode(A__ , add_special_tokens=A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() _SCREAMING_SNAKE_CASE = tokenizer.encode(A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.encode(A__ ) self.assertListEqual(A__ , A__ ) def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = BasicTokenizer() self.assertListEqual(tokenizer.tokenize("""ah\u535A\u63A8zz""" ) , ["""ah""", """\u535A""", """\u63A8""", """zz"""] ) def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHeLLo!how \n Are yoU? """ ) , ["""hello""", """!""", """how""", """are""", """you""", """?"""] ) self.assertListEqual(tokenizer.tokenize("""H\u00E9llo""" ) , ["""hello"""] ) def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , strip_accents=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""hällo""", """!""", """how""", """are""", """you""", """?"""] ) self.assertListEqual(tokenizer.tokenize("""H\u00E9llo""" ) , ["""h\u00E9llo"""] ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , strip_accents=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""hallo""", """!""", """how""", """are""", """you""", """?"""] ) self.assertListEqual(tokenizer.tokenize("""H\u00E9llo""" ) , ["""hello"""] ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""hallo""", """!""", """how""", """are""", """you""", """?"""] ) self.assertListEqual(tokenizer.tokenize("""H\u00E9llo""" ) , ["""hello"""] ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHeLLo!how \n Are yoU? """ ) , ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?"""] ) def UpperCamelCase ( self ) -> Dict: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , strip_accents=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""HäLLo""", """!""", """how""", """Are""", """yoU""", """?"""] ) def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , strip_accents=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""HaLLo""", """!""", """how""", """Are""", """yoU""", """?"""] ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , never_split=["""[UNK]"""] ) self.assertListEqual( tokenizer.tokenize(""" \tHeLLo!how \n Are yoU? [UNK]""" ) , ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?""", """[UNK]"""] ) def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = ["""[UNK]""", """[CLS]""", """[SEP]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing"""] _SCREAMING_SNAKE_CASE = {} for i, token in enumerate(A__ ): _SCREAMING_SNAKE_CASE = i _SCREAMING_SNAKE_CASE = WordpieceTokenizer(vocab=A__ , unk_token="""[UNK]""" ) self.assertListEqual(tokenizer.tokenize("""""" ) , [] ) self.assertListEqual(tokenizer.tokenize("""unwanted running""" ) , ["""un""", """##want""", """##ed""", """runn""", """##ing"""] ) self.assertListEqual(tokenizer.tokenize("""unwantedX running""" ) , ["""[UNK]""", """runn""", """##ing"""] ) def UpperCamelCase ( self ) -> str: self.assertTrue(_is_whitespace(""" """ ) ) self.assertTrue(_is_whitespace("""\t""" ) ) self.assertTrue(_is_whitespace("""\r""" ) ) self.assertTrue(_is_whitespace("""\n""" ) ) self.assertTrue(_is_whitespace("""\u00A0""" ) ) self.assertFalse(_is_whitespace("""A""" ) ) self.assertFalse(_is_whitespace("""-""" ) ) def UpperCamelCase ( self ) -> Union[str, Any]: self.assertTrue(_is_control("""\u0005""" ) ) self.assertFalse(_is_control("""A""" ) ) self.assertFalse(_is_control(""" """ ) ) self.assertFalse(_is_control("""\t""" ) ) self.assertFalse(_is_control("""\r""" ) ) def UpperCamelCase ( self ) -> Dict: self.assertTrue(_is_punctuation("""-""" ) ) self.assertTrue(_is_punctuation("""$""" ) ) self.assertTrue(_is_punctuation("""`""" ) ) self.assertTrue(_is_punctuation(""".""" ) ) self.assertFalse(_is_punctuation("""A""" ) ) self.assertFalse(_is_punctuation(""" """ ) ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = self.get_tokenizer() _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() # Example taken from the issue https://github.com/huggingface/tokenizers/issues/340 self.assertListEqual([tokenizer.tokenize(A__ ) for t in ["""Test""", """\xad""", """test"""]] , [["""[UNK]"""], [], ["""[UNK]"""]] ) self.assertListEqual( [rust_tokenizer.tokenize(A__ ) for t in ["""Test""", """\xad""", """test"""]] , [["""[UNK]"""], [], ["""[UNK]"""]] ) @slow def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained("""google/mobilebert-uncased""" ) _SCREAMING_SNAKE_CASE = tokenizer.encode("""sequence builders""" , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer.encode("""multi-sequence build""" , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer.build_inputs_with_special_tokens(A__ ) _SCREAMING_SNAKE_CASE = tokenizer.build_inputs_with_special_tokens(A__ , A__ ) assert encoded_sentence == [1_01] + text + [1_02] assert encoded_pair == [1_01] + text + [1_02] + text_a + [1_02] def UpperCamelCase ( self ) -> List[str]: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})" ): _SCREAMING_SNAKE_CASE = self.rust_tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = F"A, naïve {tokenizer_r.mask_token} AllenNLP sentence." _SCREAMING_SNAKE_CASE = tokenizer_r.encode_plus( A__ , return_attention_mask=A__ , return_token_type_ids=A__ , return_offsets_mapping=A__ , add_special_tokens=A__ , ) _SCREAMING_SNAKE_CASE = tokenizer_r.do_lower_case if hasattr(A__ , """do_lower_case""" ) else False _SCREAMING_SNAKE_CASE = ( [ ((0, 0), tokenizer_r.cls_token), ((0, 1), """A"""), ((1, 2), ""","""), ((3, 5), """na"""), ((5, 6), """##ï"""), ((6, 8), """##ve"""), ((9, 15), tokenizer_r.mask_token), ((16, 21), """Allen"""), ((21, 23), """##NL"""), ((23, 24), """##P"""), ((25, 33), """sentence"""), ((33, 34), """."""), ((0, 0), tokenizer_r.sep_token), ] if not do_lower_case else [ ((0, 0), tokenizer_r.cls_token), ((0, 1), """a"""), ((1, 2), ""","""), ((3, 8), """naive"""), ((9, 15), tokenizer_r.mask_token), ((16, 21), """allen"""), ((21, 23), """##nl"""), ((23, 24), """##p"""), ((25, 33), """sentence"""), ((33, 34), """."""), ((0, 0), tokenizer_r.sep_token), ] ) self.assertEqual( [e[1] for e in expected_results] , tokenizer_r.convert_ids_to_tokens(tokens["""input_ids"""] ) ) self.assertEqual([e[0] for e in expected_results] , tokens["""offset_mapping"""] ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = ["""的""", """人""", """有"""] _SCREAMING_SNAKE_CASE = """""".join(A__ ) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})" ): _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = self.rust_tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = tokenizer_p.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer_r.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer_r.convert_ids_to_tokens(A__ ) _SCREAMING_SNAKE_CASE = tokenizer_p.convert_ids_to_tokens(A__ ) # it is expected that each Chinese character is not preceded by "##" self.assertListEqual(A__ , A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = self.rust_tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = tokenizer_r.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer_p.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer_r.convert_ids_to_tokens(A__ ) _SCREAMING_SNAKE_CASE = tokenizer_p.convert_ids_to_tokens(A__ ) # it is expected that only the first Chinese character is not preceded by "##". _SCREAMING_SNAKE_CASE = [ F"##{token}" if idx != 0 else token for idx, token in enumerate(A__ ) ] self.assertListEqual(A__ , A__ ) self.assertListEqual(A__ , A__ )
0
1
'''simple docstring''' import math_equivalence # From: git+https://github.com/hendrycks/math.git import datasets UpperCamelCase__ : int = "\\n@article{hendrycksmath2021,\n title={Measuring Mathematical Problem Solving With the MATH Dataset},\n author={Dan Hendrycks\n and Collin Burns\n and Saurav Kadavath\n and Akul Arora\n and Steven Basart\n and Eric Tang\n and Dawn Song\n and Jacob Steinhardt},\n journal={arXiv preprint arXiv:2103.03874},\n year={2021}\n}\n" UpperCamelCase__ : Optional[Any] = "\\nThis metric is used to assess performance on the Mathematics Aptitude Test of Heuristics (MATH) dataset.\nIt first canonicalizes the inputs (e.g., converting \"1/2\" to \"\\frac{1}{2}\") and then computes accuracy.\n" UpperCamelCase__ : int = R"\nCalculates accuracy after canonicalizing inputs.\n\nArgs:\n predictions: list of predictions to score. Each prediction\n is a string that contains natural language and LaTex.\n references: list of reference for each prediction. Each\n reference is a string that contains natural language\n and LaTex.\nReturns:\n accuracy: accuracy after canonicalizing inputs\n (e.g., converting \"1/2\" to \"\\frac{1}{2}\")\n\nExamples:\n >>> metric = datasets.load_metric(\"competition_math\")\n >>> results = metric.compute(references=[\"\\frac{1}{2}\"], predictions=[\"1/2\"])\n >>> print(results)\n {'accuracy': 1.0}\n" @datasets.utils.file_utils.add_end_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class _a (datasets.Metric): """simple docstring""" def UpperCamelCase ( self ) -> List[str]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""string""" ), """references""": datasets.Value("""string""" ), } ) , homepage="""https://github.com/hendrycks/math""" , codebase_urls=["""https://github.com/hendrycks/math"""] , ) def UpperCamelCase ( self , A__ , A__ ) -> Tuple: _SCREAMING_SNAKE_CASE = 0.0 for i, j in zip(A__ , A__ ): n_correct += 1.0 if math_equivalence.is_equiv(A__ , A__ ) else 0.0 _SCREAMING_SNAKE_CASE = n_correct / len(A__ ) return { "accuracy": accuracy, }
0
'''simple docstring''' import logging import os import quant_trainer import torch from torch.utils.data import DataLoader from transformers import Trainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput UpperCamelCase__ : Tuple = logging.getLogger(__name__) if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class _a (_lowerCamelCase): """simple docstring""" def __init__( self , *A__ , A__=None , A__=None , A__=None , **A__ ) -> Optional[int]: super().__init__(*A__ , **A__ ) _SCREAMING_SNAKE_CASE = eval_examples _SCREAMING_SNAKE_CASE = post_process_function _SCREAMING_SNAKE_CASE = quant_trainer_args _SCREAMING_SNAKE_CASE = 1_28 # default number of calibration samples def UpperCamelCase ( self , A__=None ) -> Union[str, Any]: if calib_dataset is None and self.calib_dataset is None: raise ValueError("""Trainer: calibration requires an calib_dataset.""" ) _SCREAMING_SNAKE_CASE = calib_dataset if calib_dataset is not None else self.calib_dataset _SCREAMING_SNAKE_CASE = self._remove_unused_columns(A__ , description="""Calibration""" ) return DataLoader( A__ , batch_size=self.args.eval_batch_size , collate_fn=self.data_collator , drop_last=self.args.dataloader_drop_last , num_workers=self.args.dataloader_num_workers , pin_memory=self.args.dataloader_pin_memory , shuffle=A__ , ) def UpperCamelCase ( self , A__=None ) -> str: _SCREAMING_SNAKE_CASE = self.train_dataset if calib_dataset is None else calib_dataset _SCREAMING_SNAKE_CASE = self.get_calib_dataloader(A__ ) _SCREAMING_SNAKE_CASE = self.model quant_trainer.configure_model(A__ , self.quant_trainer_args , calib=A__ ) model.eval() quant_trainer.enable_calibration(A__ ) logger.info("""***** Running calibration *****""" ) logger.info(F" Num examples = {self.calib_num}" ) logger.info(F" Batch size = {calib_dataloader.batch_size}" ) for step, inputs in enumerate(A__ ): # Prediction step _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self.prediction_step(A__ , A__ , prediction_loss_only=A__ ) if (step + 1) * calib_dataloader.batch_size >= self.calib_num: break quant_trainer.finish_calibration(A__ , self.quant_trainer_args ) _SCREAMING_SNAKE_CASE = model def UpperCamelCase ( self , A__=None , A__=None , A__=None , A__ = "eval" ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.eval_dataset if eval_dataset is None else eval_dataset _SCREAMING_SNAKE_CASE = self.get_eval_dataloader(A__ ) _SCREAMING_SNAKE_CASE = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. _SCREAMING_SNAKE_CASE = self.compute_metrics _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: _SCREAMING_SNAKE_CASE = eval_loop( A__ , description="""Evaluation""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=A__ , ) finally: _SCREAMING_SNAKE_CASE = compute_metrics if self.post_process_function is not None and self.compute_metrics is not None: _SCREAMING_SNAKE_CASE = self.post_process_function(A__ , A__ , output.predictions ) _SCREAMING_SNAKE_CASE = self.compute_metrics(A__ ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(F"{metric_key_prefix}_" ): _SCREAMING_SNAKE_CASE = metrics.pop(A__ ) self.log(A__ ) else: _SCREAMING_SNAKE_CASE = {} if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report() ) _SCREAMING_SNAKE_CASE = self.callback_handler.on_evaluate(self.args , self.state , self.control , A__ ) return metrics def UpperCamelCase ( self , A__ , A__ , A__=None , A__ = "test" ) -> List[str]: _SCREAMING_SNAKE_CASE = self.get_test_dataloader(A__ ) # Temporarily disable metric computation, we will do it in the loop here. _SCREAMING_SNAKE_CASE = self.compute_metrics _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: _SCREAMING_SNAKE_CASE = eval_loop( A__ , description="""Prediction""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=A__ , ) finally: _SCREAMING_SNAKE_CASE = compute_metrics if self.post_process_function is None or self.compute_metrics is None: return output _SCREAMING_SNAKE_CASE = self.post_process_function(A__ , A__ , output.predictions , """predict""" ) _SCREAMING_SNAKE_CASE = self.compute_metrics(A__ ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(F"{metric_key_prefix}_" ): _SCREAMING_SNAKE_CASE = metrics.pop(A__ ) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=A__ ) def UpperCamelCase ( self , A__="./" ) -> Tuple: _SCREAMING_SNAKE_CASE = self.eval_dataset _SCREAMING_SNAKE_CASE = self.get_eval_dataloader(A__ ) _SCREAMING_SNAKE_CASE = next(iter(A__ ) ) # saving device - to make it consistent _SCREAMING_SNAKE_CASE = torch.device("""cuda""" if torch.cuda.is_available() else """cpu""" ) # convert to tuple _SCREAMING_SNAKE_CASE = tuple(v.to(A__ ) for k, v in batch.items() ) logger.info("""Converting model to be onnx compatible""" ) from pytorch_quantization.nn import TensorQuantizer _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = self.model.to(A__ ) model.eval() model.float() _SCREAMING_SNAKE_CASE = model.module if hasattr(A__ , """module""" ) else model quant_trainer.configure_model(A__ , self.quant_trainer_args ) _SCREAMING_SNAKE_CASE = os.path.join(A__ , """model.onnx""" ) logger.info(F"exporting model to {output_model_file}" ) _SCREAMING_SNAKE_CASE = {0: """batch_size""", 1: """seq_len"""} torch.onnx.export( A__ , A__ , A__ , export_params=A__ , opset_version=13 , do_constant_folding=A__ , input_names=["""input_ids""", """attention_mask""", """token_type_ids"""] , output_names=["""output_start_logits""", """output_end_logits"""] , dynamic_axes={ """input_ids""": axes, """attention_mask""": axes, """token_type_ids""": axes, """output_start_logits""": axes, """output_end_logits""": axes, } , verbose=A__ , ) logger.info("""onnx export finished""" )
0
1
'''simple docstring''' import argparse import re from typing import Dict import torch from datasets import Audio, Dataset, load_dataset, load_metric from transformers import AutoFeatureExtractor, pipeline def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE = args.log_outputs _SCREAMING_SNAKE_CASE = """_""".join(args.dataset.split("""/""" ) + [args.config, args.split] ) # load metric _SCREAMING_SNAKE_CASE = load_metric("""wer""" ) _SCREAMING_SNAKE_CASE = load_metric("""cer""" ) # compute metrics _SCREAMING_SNAKE_CASE = wer.compute(references=result["""target"""] , predictions=result["""prediction"""] ) _SCREAMING_SNAKE_CASE = cer.compute(references=result["""target"""] , predictions=result["""prediction"""] ) # print & log results _SCREAMING_SNAKE_CASE = F"WER: {wer_result}\nCER: {cer_result}" print(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: _SCREAMING_SNAKE_CASE = F"log_{dataset_id}_predictions.txt" _SCREAMING_SNAKE_CASE = 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: """simple docstring""" _SCREAMING_SNAKE_CASE = """[,?.!\-\;\:\"“%‘”�—’…–]""" # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training _SCREAMING_SNAKE_CASE = 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! _SCREAMING_SNAKE_CASE = ["""\n\n""", """\n""", """ """, """ """] for t in token_sequences_to_ignore: _SCREAMING_SNAKE_CASE = """ """.join(text.split(SCREAMING_SNAKE_CASE_ ) ) return text def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" # load dataset _SCREAMING_SNAKE_CASE = 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 _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=SCREAMING_SNAKE_CASE_ ) ) # load eval pipeline if args.device is None: _SCREAMING_SNAKE_CASE = 0 if torch.cuda.is_available() else -1 _SCREAMING_SNAKE_CASE = pipeline("""automatic-speech-recognition""" , model=args.model_id , device=args.device ) # map function to decode audio def map_to_pred(SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE = asr( batch["""audio"""]["""array"""] , chunk_length_s=args.chunk_length_s , stride_length_s=args.stride_length_s ) _SCREAMING_SNAKE_CASE = prediction["""text"""] _SCREAMING_SNAKE_CASE = normalize_text(batch["""sentence"""] ) return batch # run inference on all examples _SCREAMING_SNAKE_CASE = dataset.map(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__": UpperCamelCase__ : 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.", ) UpperCamelCase__ : List[str] = parser.parse_args() main(args)
0
'''simple docstring''' def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" return "".join([hex(SCREAMING_SNAKE_CASE_ )[2:].zfill(2 ).upper() for byte in list(SCREAMING_SNAKE_CASE_ )] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> bytes: """simple docstring""" # Check data validity, following RFC3548 # https://www.ietf.org/rfc/rfc3548.txt if (len(SCREAMING_SNAKE_CASE_ ) % 2) != 0: raise ValueError( """Base16 encoded data is invalid: Data does not have an even number of hex digits.""" ) # Check the character set - the standard base16 alphabet # is uppercase according to RFC3548 section 6 if not set(SCREAMING_SNAKE_CASE_ ) <= set("""0123456789ABCDEF""" ): raise ValueError( """Base16 encoded data is invalid: Data is not uppercase hex or it contains invalid characters.""" ) # For every two hexadecimal digits (= a byte), turn it into an integer. # Then, string the result together into bytes, and return it. return bytes(int(data[i] + data[i + 1] , 16 ) for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) , 2 ) ) if __name__ == "__main__": import doctest doctest.testmod()
0
1
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging UpperCamelCase__ : List[Any] = logging.get_logger(__name__) UpperCamelCase__ : Optional[Any] = { "google/bigbird-roberta-base": "https://huggingface.co/google/bigbird-roberta-base/resolve/main/config.json", "google/bigbird-roberta-large": "https://huggingface.co/google/bigbird-roberta-large/resolve/main/config.json", "google/bigbird-base-trivia-itc": "https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/config.json", # See all BigBird models at https://huggingface.co/models?filter=big_bird } class _a (_lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = 'big_bird' def __init__( self , A__=5_03_58 , A__=7_68 , A__=12 , A__=12 , A__=30_72 , A__="gelu_new" , A__=0.1 , A__=0.1 , A__=40_96 , A__=2 , A__=0.02 , A__=1E-12 , A__=True , A__=0 , A__=1 , A__=2 , A__=66 , A__="block_sparse" , A__=True , A__=False , A__=64 , A__=3 , A__=None , **A__ , ) -> Dict: super().__init__( pad_token_id=A__ , bos_token_id=A__ , eos_token_id=A__ , sep_token_id=A__ , **A__ , ) _SCREAMING_SNAKE_CASE = vocab_size _SCREAMING_SNAKE_CASE = max_position_embeddings _SCREAMING_SNAKE_CASE = hidden_size _SCREAMING_SNAKE_CASE = num_hidden_layers _SCREAMING_SNAKE_CASE = num_attention_heads _SCREAMING_SNAKE_CASE = intermediate_size _SCREAMING_SNAKE_CASE = hidden_act _SCREAMING_SNAKE_CASE = hidden_dropout_prob _SCREAMING_SNAKE_CASE = attention_probs_dropout_prob _SCREAMING_SNAKE_CASE = initializer_range _SCREAMING_SNAKE_CASE = type_vocab_size _SCREAMING_SNAKE_CASE = layer_norm_eps _SCREAMING_SNAKE_CASE = use_cache _SCREAMING_SNAKE_CASE = rescale_embeddings _SCREAMING_SNAKE_CASE = attention_type _SCREAMING_SNAKE_CASE = use_bias _SCREAMING_SNAKE_CASE = block_size _SCREAMING_SNAKE_CASE = num_random_blocks _SCREAMING_SNAKE_CASE = classifier_dropout class _a (_lowerCamelCase): """simple docstring""" @property def UpperCamelCase ( self ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": _SCREAMING_SNAKE_CASE = {0: """batch""", 1: """choice""", 2: """sequence"""} else: _SCREAMING_SNAKE_CASE = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
0
'''simple docstring''' import pytest import requests from datasets.utils.file_utils import http_head from .utils import OfflineSimulationMode, RequestWouldHangIndefinitelyError, offline @pytest.mark.integration def lowerCAmelCase_ ( ) -> List[Any]: """simple docstring""" with offline(OfflineSimulationMode.CONNECTION_TIMES_OUT ): with pytest.raises(SCREAMING_SNAKE_CASE_ ): requests.request("""GET""" , """https://huggingface.co""" ) with pytest.raises(requests.exceptions.ConnectTimeout ): requests.request("""GET""" , """https://huggingface.co""" , timeout=1.0 ) @pytest.mark.integration def lowerCAmelCase_ ( ) -> int: """simple docstring""" with offline(OfflineSimulationMode.CONNECTION_FAILS ): with pytest.raises(requests.exceptions.ConnectionError ): requests.request("""GET""" , """https://huggingface.co""" ) def lowerCAmelCase_ ( ) -> Optional[Any]: """simple docstring""" with offline(OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1 ): with pytest.raises(SCREAMING_SNAKE_CASE_ ): http_head("""https://huggingface.co""" )
0
1
'''simple docstring''' from scipy.stats import pearsonr import datasets UpperCamelCase__ : str = "\nPearson correlation coefficient and p-value for testing non-correlation.\nThe Pearson correlation coefficient measures the linear relationship between two datasets. The calculation of the p-value relies on the assumption that each dataset is normally distributed. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Correlations of -1 or +1 imply an exact linear relationship. Positive correlations imply that as x increases, so does y. Negative correlations imply that as x increases, y decreases.\nThe p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets.\n" UpperCamelCase__ : str = "\nArgs:\n predictions (`list` of `int`): Predicted class labels, as returned by a model.\n references (`list` of `int`): Ground truth labels.\n return_pvalue (`boolean`): If `True`, returns the p-value, along with the correlation coefficient. If `False`, returns only the correlation coefficient. Defaults to `False`.\n\nReturns:\n pearsonr (`float`): Pearson correlation coefficient. Minimum possible value is -1. Maximum possible value is 1. Values of 1 and -1 indicate exact linear positive and negative relationships, respectively. A value of 0 implies no correlation.\n p-value (`float`): P-value, which roughly indicates the probability of an The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets. Minimum possible value is 0. Maximum possible value is 1. Higher values indicate higher probabilities.\n\nExamples:\n\n Example 1-A simple example using only predictions and references.\n >>> pearsonr_metric = datasets.load_metric(\"pearsonr\")\n >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5])\n >>> print(round(results['pearsonr'], 2))\n -0.74\n\n Example 2-The same as Example 1, but that also returns the `p-value`.\n >>> pearsonr_metric = datasets.load_metric(\"pearsonr\")\n >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5], return_pvalue=True)\n >>> print(sorted(list(results.keys())))\n ['p-value', 'pearsonr']\n >>> print(round(results['pearsonr'], 2))\n -0.74\n >>> print(round(results['p-value'], 2))\n 0.15\n" UpperCamelCase__ : Dict = "\n@article{2020SciPy-NMeth,\nauthor = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and\n Haberland, Matt and Reddy, Tyler and Cournapeau, David and\n Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and\n Bright, Jonathan and {van der Walt}, St{\'e}fan J. and\n Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and\n Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and\n Kern, Robert and Larson, Eric and Carey, C J and\n Polat, Ilhan and Feng, Yu and Moore, Eric W. and\n {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and\n Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and\n Harris, Charles R. and Archibald, Anne M. and\n Ribeiro, Antonio H. and Pedregosa, Fabian and\n {van Mulbregt}, Paul and {SciPy 1.0 Contributors}},\ntitle = {{{SciPy} 1.0: Fundamental Algorithms for Scientific\n Computing in Python}},\njournal = {Nature Methods},\nyear = {2020},\nvolume = {17},\npages = {261--272},\nadsurl = {https://rdcu.be/b08Wh},\ndoi = {10.1038/s41592-019-0686-2},\n}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class _a (datasets.Metric): """simple docstring""" def UpperCamelCase ( self ) -> Union[str, Any]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""float""" ), """references""": datasets.Value("""float""" ), } ) , reference_urls=["""https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.pearsonr.html"""] , ) def UpperCamelCase ( self , A__ , A__ , A__=False ) -> Tuple: if return_pvalue: _SCREAMING_SNAKE_CASE = pearsonr(A__ , A__ ) return {"pearsonr": results[0], "p-value": results[1]} else: return {"pearsonr": float(pearsonr(A__ , A__ )[0] )}
0
'''simple docstring''' import math from collections.abc import Iterator from itertools import takewhile def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> bool: """simple docstring""" if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(SCREAMING_SNAKE_CASE_ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def lowerCAmelCase_ ( ) -> Iterator[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = 2 while True: if is_prime(SCREAMING_SNAKE_CASE_ ): yield num num += 1 def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ = 2_00_00_00 ) -> int: """simple docstring""" return sum(takewhile(lambda SCREAMING_SNAKE_CASE_ : x < n , prime_generator() ) ) if __name__ == "__main__": print(f"""{solution() = }""")
0
1
'''simple docstring''' import os from shutil import copyfile from typing import List, Optional, Tuple from tokenizers import processors from ...tokenization_utils import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_mbart import MBartTokenizer else: UpperCamelCase__ : List[Any] = None UpperCamelCase__ : List[str] = logging.get_logger(__name__) UpperCamelCase__ : Optional[Any] = {"vocab_file": "sentencepiece.bpe.model", "tokenizer_file": "tokenizer.json"} UpperCamelCase__ : Tuple = { "vocab_file": { "facebook/mbart-large-en-ro": ( "https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/sentencepiece.bpe.model" ), "facebook/mbart-large-cc25": ( "https://huggingface.co/facebook/mbart-large-cc25/resolve/main/sentencepiece.bpe.model" ), }, "tokenizer_file": { "facebook/mbart-large-en-ro": "https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/tokenizer.json", "facebook/mbart-large-cc25": "https://huggingface.co/facebook/mbart-large-cc25/resolve/main/tokenizer.json", }, } UpperCamelCase__ : Optional[int] = { "facebook/mbart-large-en-ro": 1_024, "facebook/mbart-large-cc25": 1_024, } # fmt: off UpperCamelCase__ : Optional[int] = ["ar_AR", "cs_CZ", "de_DE", "en_XX", "es_XX", "et_EE", "fi_FI", "fr_XX", "gu_IN", "hi_IN", "it_IT", "ja_XX", "kk_KZ", "ko_KR", "lt_LT", "lv_LV", "my_MM", "ne_NP", "nl_XX", "ro_RO", "ru_RU", "si_LK", "tr_TR", "vi_VN", "zh_CN"] class _a (_lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE = ['input_ids', 'attention_mask'] SCREAMING_SNAKE_CASE = MBartTokenizer SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] def __init__( self , A__=None , A__=None , A__="<s>" , A__="</s>" , A__="</s>" , A__="<s>" , A__="<unk>" , A__="<pad>" , A__="<mask>" , A__=None , A__=None , A__=None , **A__ , ) -> str: # Mask token behave like a normal word, i.e. include the space before it _SCREAMING_SNAKE_CASE = AddedToken(A__ , lstrip=A__ , rstrip=A__ ) if isinstance(A__ , A__ ) else mask_token super().__init__( vocab_file=A__ , tokenizer_file=A__ , bos_token=A__ , eos_token=A__ , sep_token=A__ , cls_token=A__ , unk_token=A__ , pad_token=A__ , mask_token=A__ , src_lang=A__ , tgt_lang=A__ , additional_special_tokens=A__ , **A__ , ) _SCREAMING_SNAKE_CASE = vocab_file _SCREAMING_SNAKE_CASE = False if not self.vocab_file else True _SCREAMING_SNAKE_CASE = FAIRSEQ_LANGUAGE_CODES.copy() if additional_special_tokens is not None: # Only add those special tokens if they are not already there. _additional_special_tokens.extend( [t for t in additional_special_tokens if t not in _additional_special_tokens] ) self.add_special_tokens({"""additional_special_tokens""": _additional_special_tokens} ) _SCREAMING_SNAKE_CASE = { lang_code: self.convert_tokens_to_ids(A__ ) for lang_code in FAIRSEQ_LANGUAGE_CODES } _SCREAMING_SNAKE_CASE = src_lang if src_lang is not None else """en_XX""" _SCREAMING_SNAKE_CASE = self.convert_tokens_to_ids(self._src_lang ) _SCREAMING_SNAKE_CASE = tgt_lang self.set_src_lang_special_tokens(self._src_lang ) @property def UpperCamelCase ( self ) -> str: return self._src_lang @src_lang.setter def UpperCamelCase ( self , A__ ) -> None: _SCREAMING_SNAKE_CASE = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def UpperCamelCase ( self , A__ , A__ = None ) -> List[int]: if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def UpperCamelCase ( self , A__ , A__ = 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] def UpperCamelCase ( self , A__ , A__ , A__ , A__ , **A__ ) -> Tuple: if src_lang is None or tgt_lang is None: raise ValueError("""Translation requires a `src_lang` and a `tgt_lang` for this model""" ) _SCREAMING_SNAKE_CASE = src_lang _SCREAMING_SNAKE_CASE = self(A__ , add_special_tokens=A__ , return_tensors=A__ , **A__ ) _SCREAMING_SNAKE_CASE = self.convert_tokens_to_ids(A__ ) _SCREAMING_SNAKE_CASE = tgt_lang_id return inputs def UpperCamelCase ( self , A__ , A__ = "en_XX" , A__ = None , A__ = "ro_RO" , **A__ , ) -> BatchEncoding: _SCREAMING_SNAKE_CASE = src_lang _SCREAMING_SNAKE_CASE = tgt_lang return super().prepare_seqaseq_batch(A__ , A__ , **A__ ) def UpperCamelCase ( self ) -> Any: return self.set_src_lang_special_tokens(self.src_lang ) def UpperCamelCase ( self ) -> Tuple: return self.set_tgt_lang_special_tokens(self.tgt_lang ) def UpperCamelCase ( self , A__ ) -> None: _SCREAMING_SNAKE_CASE = self.convert_tokens_to_ids(A__ ) _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = [self.eos_token_id, self.cur_lang_code] _SCREAMING_SNAKE_CASE = self.convert_ids_to_tokens(self.prefix_tokens ) _SCREAMING_SNAKE_CASE = self.convert_ids_to_tokens(self.suffix_tokens ) _SCREAMING_SNAKE_CASE = processors.TemplateProcessing( single=prefix_tokens_str + ["""$A"""] + suffix_tokens_str , pair=prefix_tokens_str + ["""$A""", """$B"""] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def UpperCamelCase ( self , A__ ) -> None: _SCREAMING_SNAKE_CASE = self.convert_tokens_to_ids(A__ ) _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = [self.eos_token_id, self.cur_lang_code] _SCREAMING_SNAKE_CASE = self.convert_ids_to_tokens(self.prefix_tokens ) _SCREAMING_SNAKE_CASE = self.convert_ids_to_tokens(self.suffix_tokens ) _SCREAMING_SNAKE_CASE = processors.TemplateProcessing( single=prefix_tokens_str + ["""$A"""] + suffix_tokens_str , pair=prefix_tokens_str + ["""$A""", """$B"""] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def UpperCamelCase ( self , A__ , A__ = 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(A__ ): logger.error(F"Vocabulary path ({save_directory}) should be a directory." ) return _SCREAMING_SNAKE_CASE = os.path.join( A__ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(A__ ): copyfile(self.vocab_file , A__ ) return (out_vocab_file,)
0
'''simple docstring''' import unittest import numpy as np from transformers.testing_utils import require_flax, require_tf, require_torch from transformers.utils import ( expand_dims, flatten_dict, is_flax_available, is_tf_available, is_torch_available, reshape, squeeze, transpose, ) if is_flax_available(): import jax.numpy as jnp if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch class _a (unittest.TestCase): """simple docstring""" def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = { """task_specific_params""": { """summarization""": {"""length_penalty""": 1.0, """max_length""": 1_28, """min_length""": 12, """num_beams""": 4}, """summarization_cnn""": {"""length_penalty""": 2.0, """max_length""": 1_42, """min_length""": 56, """num_beams""": 4}, """summarization_xsum""": {"""length_penalty""": 1.0, """max_length""": 62, """min_length""": 11, """num_beams""": 6}, } } _SCREAMING_SNAKE_CASE = { """task_specific_params.summarization.length_penalty""": 1.0, """task_specific_params.summarization.max_length""": 1_28, """task_specific_params.summarization.min_length""": 12, """task_specific_params.summarization.num_beams""": 4, """task_specific_params.summarization_cnn.length_penalty""": 2.0, """task_specific_params.summarization_cnn.max_length""": 1_42, """task_specific_params.summarization_cnn.min_length""": 56, """task_specific_params.summarization_cnn.num_beams""": 4, """task_specific_params.summarization_xsum.length_penalty""": 1.0, """task_specific_params.summarization_xsum.max_length""": 62, """task_specific_params.summarization_xsum.min_length""": 11, """task_specific_params.summarization_xsum.num_beams""": 6, } self.assertEqual(flatten_dict(A__ ) , A__ ) def UpperCamelCase ( self ) -> int: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(transpose(A__ ) , x.transpose() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(transpose(A__ , axes=(1, 2, 0) ) , x.transpose((1, 2, 0) ) ) ) @require_torch def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(transpose(A__ ) , transpose(A__ ).numpy() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(transpose(A__ , axes=(1, 2, 0) ) , transpose(A__ , axes=(1, 2, 0) ).numpy() ) ) @require_tf def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(transpose(A__ ) , transpose(A__ ).numpy() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(transpose(A__ , axes=(1, 2, 0) ) , transpose(A__ , axes=(1, 2, 0) ).numpy() ) ) @require_flax def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(transpose(A__ ) , np.asarray(transpose(A__ ) ) ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(transpose(A__ , axes=(1, 2, 0) ) , np.asarray(transpose(A__ , axes=(1, 2, 0) ) ) ) ) def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(reshape(A__ , (4, 3) ) , np.reshape(A__ , (4, 3) ) ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(reshape(A__ , (12, 5) ) , np.reshape(A__ , (12, 5) ) ) ) @require_torch def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(reshape(A__ , (4, 3) ) , reshape(A__ , (4, 3) ).numpy() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(reshape(A__ , (12, 5) ) , reshape(A__ , (12, 5) ).numpy() ) ) @require_tf def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(reshape(A__ , (4, 3) ) , reshape(A__ , (4, 3) ).numpy() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(reshape(A__ , (12, 5) ) , reshape(A__ , (12, 5) ).numpy() ) ) @require_flax def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(reshape(A__ , (4, 3) ) , np.asarray(reshape(A__ , (4, 3) ) ) ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(reshape(A__ , (12, 5) ) , np.asarray(reshape(A__ , (12, 5) ) ) ) ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = np.random.randn(1 , 3 , 4 ) self.assertTrue(np.allclose(squeeze(A__ ) , np.squeeze(A__ ) ) ) _SCREAMING_SNAKE_CASE = np.random.randn(1 , 4 , 1 , 5 ) self.assertTrue(np.allclose(squeeze(A__ , axis=2 ) , np.squeeze(A__ , axis=2 ) ) ) @require_torch def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = np.random.randn(1 , 3 , 4 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(squeeze(A__ ) , squeeze(A__ ).numpy() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(1 , 4 , 1 , 5 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(squeeze(A__ , axis=2 ) , squeeze(A__ , axis=2 ).numpy() ) ) @require_tf def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = np.random.randn(1 , 3 , 4 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(squeeze(A__ ) , squeeze(A__ ).numpy() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(1 , 4 , 1 , 5 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(squeeze(A__ , axis=2 ) , squeeze(A__ , axis=2 ).numpy() ) ) @require_flax def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = np.random.randn(1 , 3 , 4 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(squeeze(A__ ) , np.asarray(squeeze(A__ ) ) ) ) _SCREAMING_SNAKE_CASE = np.random.randn(1 , 4 , 1 , 5 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(squeeze(A__ , axis=2 ) , np.asarray(squeeze(A__ , axis=2 ) ) ) ) def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(expand_dims(A__ , axis=1 ) , np.expand_dims(A__ , axis=1 ) ) ) @require_torch def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(expand_dims(A__ , axis=1 ) , expand_dims(A__ , axis=1 ).numpy() ) ) @require_tf def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(expand_dims(A__ , axis=1 ) , expand_dims(A__ , axis=1 ).numpy() ) ) @require_flax def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(expand_dims(A__ , axis=1 ) , np.asarray(expand_dims(A__ , axis=1 ) ) ) )
0
1
'''simple docstring''' import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..bit import BitConfig UpperCamelCase__ : Optional[Any] = logging.get_logger(__name__) UpperCamelCase__ : List[str] = { "Intel/dpt-large": "https://huggingface.co/Intel/dpt-large/resolve/main/config.json", # See all DPT models at https://huggingface.co/models?filter=dpt } class _a (_lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = 'dpt' def __init__( self , A__=7_68 , A__=12 , A__=12 , A__=30_72 , A__="gelu" , A__=0.0 , A__=0.0 , A__=0.02 , A__=1E-12 , A__=3_84 , A__=16 , A__=3 , A__=False , A__=True , A__=[2, 5, 8, 11] , A__="project" , A__=[4, 2, 1, 0.5] , A__=[96, 1_92, 3_84, 7_68] , A__=2_56 , A__=-1 , A__=False , A__=True , A__=0.4 , A__=2_55 , A__=0.1 , A__=[1, 10_24, 24, 24] , A__=[0, 1] , A__=None , **A__ , ) -> str: super().__init__(**A__ ) _SCREAMING_SNAKE_CASE = hidden_size _SCREAMING_SNAKE_CASE = is_hybrid if self.is_hybrid: if backbone_config is None: logger.info("""Initializing the config with a `BiT` backbone.""" ) _SCREAMING_SNAKE_CASE = { """global_padding""": """same""", """layer_type""": """bottleneck""", """depths""": [3, 4, 9], """out_features""": ["""stage1""", """stage2""", """stage3"""], """embedding_dynamic_padding""": True, } _SCREAMING_SNAKE_CASE = BitConfig(**A__ ) elif isinstance(A__ , A__ ): logger.info("""Initializing the config with a `BiT` backbone.""" ) _SCREAMING_SNAKE_CASE = BitConfig(**A__ ) elif isinstance(A__ , A__ ): _SCREAMING_SNAKE_CASE = backbone_config else: raise ValueError( F"backbone_config must be a dictionary or a `PretrainedConfig`, got {backbone_config.__class__}." ) _SCREAMING_SNAKE_CASE = backbone_featmap_shape _SCREAMING_SNAKE_CASE = neck_ignore_stages if readout_type != "project": raise ValueError("""Readout type must be 'project' when using `DPT-hybrid` mode.""" ) else: _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = num_hidden_layers _SCREAMING_SNAKE_CASE = num_attention_heads _SCREAMING_SNAKE_CASE = intermediate_size _SCREAMING_SNAKE_CASE = hidden_act _SCREAMING_SNAKE_CASE = hidden_dropout_prob _SCREAMING_SNAKE_CASE = attention_probs_dropout_prob _SCREAMING_SNAKE_CASE = initializer_range _SCREAMING_SNAKE_CASE = layer_norm_eps _SCREAMING_SNAKE_CASE = image_size _SCREAMING_SNAKE_CASE = patch_size _SCREAMING_SNAKE_CASE = num_channels _SCREAMING_SNAKE_CASE = qkv_bias _SCREAMING_SNAKE_CASE = backbone_out_indices if readout_type not in ["ignore", "add", "project"]: raise ValueError("""Readout_type must be one of ['ignore', 'add', 'project']""" ) _SCREAMING_SNAKE_CASE = readout_type _SCREAMING_SNAKE_CASE = reassemble_factors _SCREAMING_SNAKE_CASE = neck_hidden_sizes _SCREAMING_SNAKE_CASE = fusion_hidden_size _SCREAMING_SNAKE_CASE = head_in_index _SCREAMING_SNAKE_CASE = use_batch_norm_in_fusion_residual # auxiliary head attributes (semantic segmentation) _SCREAMING_SNAKE_CASE = use_auxiliary_head _SCREAMING_SNAKE_CASE = auxiliary_loss_weight _SCREAMING_SNAKE_CASE = semantic_loss_ignore_index _SCREAMING_SNAKE_CASE = semantic_classifier_dropout def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = copy.deepcopy(self.__dict__ ) if output["backbone_config"] is not None: _SCREAMING_SNAKE_CASE = self.backbone_config.to_dict() _SCREAMING_SNAKE_CASE = self.__class__.model_type return output
0
'''simple docstring''' from pathlib import PurePosixPath from typing import Optional import fsspec from fsspec import AbstractFileSystem from huggingface_hub.hf_api import DatasetInfo from ..utils.file_utils import get_authentication_headers_for_url from ..utils.hub import hf_hub_url class _a (_lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = '' SCREAMING_SNAKE_CASE = 'hf-legacy' # "hf://"" is reserved for hffs def __init__( self , A__ = None , A__ = None , **A__ , ) -> Optional[int]: super().__init__(self , **A__ ) _SCREAMING_SNAKE_CASE = repo_info _SCREAMING_SNAKE_CASE = token _SCREAMING_SNAKE_CASE = None def UpperCamelCase ( self ) -> Tuple: if self.dir_cache is None: _SCREAMING_SNAKE_CASE = {} for hf_file in self.repo_info.siblings: # TODO(QL): add sizes _SCREAMING_SNAKE_CASE = { """name""": hf_file.rfilename, """size""": None, """type""": """file""", } self.dir_cache.update( { str(A__ ): {"""name""": str(A__ ), """size""": None, """type""": """directory"""} for d in list(PurePosixPath(hf_file.rfilename ).parents )[:-1] } ) def UpperCamelCase ( self , A__ , A__ = "rb" , **A__ , ) -> Optional[int]: if not isinstance(self.repo_info , A__ ): raise NotImplementedError(F"Open is only implemented for dataset repositories, but got {self.repo_info}" ) _SCREAMING_SNAKE_CASE = hf_hub_url(self.repo_info.id , A__ , revision=self.repo_info.sha ) return fsspec.open( A__ , mode=A__ , headers=get_authentication_headers_for_url(A__ , use_auth_token=self.token ) , client_kwargs={"""trust_env""": True} , ).open() def UpperCamelCase ( self , A__ , **A__ ) -> str: self._get_dirs() _SCREAMING_SNAKE_CASE = self._strip_protocol(A__ ) if path in self.dir_cache: return self.dir_cache[path] else: raise FileNotFoundError(A__ ) def UpperCamelCase ( self , A__ , A__=False , **A__ ) -> List[Any]: self._get_dirs() _SCREAMING_SNAKE_CASE = PurePosixPath(path.strip("""/""" ) ) _SCREAMING_SNAKE_CASE = {} for p, f in self.dir_cache.items(): _SCREAMING_SNAKE_CASE = PurePosixPath(p.strip("""/""" ) ) _SCREAMING_SNAKE_CASE = p.parent if root == path: _SCREAMING_SNAKE_CASE = f _SCREAMING_SNAKE_CASE = list(paths.values() ) if detail: return out else: return sorted(f["""name"""] for f in out )
0
1
'''simple docstring''' 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, ) UpperCamelCase__ : str = logging.getLogger(__name__) @dataclass class _a : """simple docstring""" SCREAMING_SNAKE_CASE = field( metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'}) SCREAMING_SNAKE_CASE = field( default=_lowerCamelCase , metadata={'help': 'Pretrained config name or path if not the same as model_name'}) SCREAMING_SNAKE_CASE = field( default=_lowerCamelCase , metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'}) SCREAMING_SNAKE_CASE = field( default=_lowerCamelCase , metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'} , ) SCREAMING_SNAKE_CASE = field(default=_lowerCamelCase , metadata={'help': 'Whether tp freeze the encoder.'}) SCREAMING_SNAKE_CASE = field(default=_lowerCamelCase , metadata={'help': 'Whether to freeze the embeddings.'}) @dataclass class _a : """simple docstring""" SCREAMING_SNAKE_CASE = field( metadata={'help': 'The input data dir. Should contain the .tsv files (or other data files) for the task.'}) SCREAMING_SNAKE_CASE = field( default='summarization' , metadata={'help': 'Task name, summarization (or summarization_{dataset} for pegasus) or translation'} , ) SCREAMING_SNAKE_CASE = field( default=10_24 , metadata={ 'help': ( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) } , ) SCREAMING_SNAKE_CASE = field( default=1_28 , metadata={ 'help': ( 'The maximum total sequence length for target text after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) } , ) SCREAMING_SNAKE_CASE = field( default=1_42 , 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``.' ) } , ) SCREAMING_SNAKE_CASE = field( default=1_42 , 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.' ) } , ) SCREAMING_SNAKE_CASE = field(default=-1 , metadata={'help': '# training examples. -1 means use all.'}) SCREAMING_SNAKE_CASE = field(default=-1 , metadata={'help': '# validation examples. -1 means use all.'}) SCREAMING_SNAKE_CASE = field(default=-1 , metadata={'help': '# test examples. -1 means use all.'}) SCREAMING_SNAKE_CASE = field(default=_lowerCamelCase , metadata={'help': 'Source language id for translation.'}) SCREAMING_SNAKE_CASE = field(default=_lowerCamelCase , metadata={'help': 'Target language id for translation.'}) SCREAMING_SNAKE_CASE = field(default=_lowerCamelCase , metadata={'help': '# num_beams to use for evaluation.'}) SCREAMING_SNAKE_CASE = field( default=_lowerCamelCase , metadata={'help': 'If only pad tokens should be ignored. This assumes that `config.pad_token_id` is defined.'} , ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: """simple docstring""" logger.info(F"***** {split} metrics *****" ) for key in sorted(metrics.keys() ): logger.info(F" {key} = {metrics[key]}" ) save_json(SCREAMING_SNAKE_CASE_ , os.path.join(SCREAMING_SNAKE_CASE_ , F"{split}_results.json" ) ) def lowerCAmelCase_ ( ) -> Optional[int]: """simple docstring""" # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. _SCREAMING_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. _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = parser.parse_args_into_dataclasses() check_output_dir(SCREAMING_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""" , SCREAMING_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. _SCREAMING_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 , ) _SCREAMING_SNAKE_CASE = ("""encoder_layerdrop""", """decoder_layerdrop""", """dropout""", """attention_dropout""") for p in extra_model_params: if getattr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): assert hasattr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ), F"({config.__class__.__name__}) doesn't have a `{p}` attribute" setattr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , getattr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) _SCREAMING_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 , ) _SCREAMING_SNAKE_CASE = AutoModelForSeqaSeqLM.from_pretrained( model_args.model_name_or_path , from_tf=""".ckpt""" in model_args.model_name_or_path , config=SCREAMING_SNAKE_CASE_ , cache_dir=model_args.cache_dir , ) # use task specific params use_task_specific_params(SCREAMING_SNAKE_CASE_ , data_args.task ) # set num_beams for evaluation if data_args.eval_beams is None: _SCREAMING_SNAKE_CASE = model.config.num_beams # set decoder_start_token_id for MBart if model.config.decoder_start_token_id is None and isinstance(SCREAMING_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(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE = tokenizer.lang_code_to_id[data_args.tgt_lang] else: _SCREAMING_SNAKE_CASE = tokenizer.convert_tokens_to_ids(data_args.tgt_lang ) if model_args.freeze_embeds: freeze_embeds(SCREAMING_SNAKE_CASE_ ) if model_args.freeze_encoder: freeze_params(model.get_encoder() ) assert_all_frozen(model.get_encoder() ) _SCREAMING_SNAKE_CASE = SeqaSeqDataset # Get datasets _SCREAMING_SNAKE_CASE = ( dataset_class( SCREAMING_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 ) _SCREAMING_SNAKE_CASE = ( dataset_class( SCREAMING_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 ) _SCREAMING_SNAKE_CASE = ( dataset_class( SCREAMING_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 _SCREAMING_SNAKE_CASE = ( build_compute_metrics_fn(data_args.task , SCREAMING_SNAKE_CASE_ ) if training_args.predict_with_generate else None ) _SCREAMING_SNAKE_CASE = SeqaSeqTrainer( model=SCREAMING_SNAKE_CASE_ , args=SCREAMING_SNAKE_CASE_ , data_args=SCREAMING_SNAKE_CASE_ , train_dataset=SCREAMING_SNAKE_CASE_ , eval_dataset=SCREAMING_SNAKE_CASE_ , data_collator=SeqaSeqDataCollator( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , model.config.decoder_start_token_id , training_args.tpu_num_cores ) , compute_metrics=SCREAMING_SNAKE_CASE_ , tokenizer=SCREAMING_SNAKE_CASE_ , ) _SCREAMING_SNAKE_CASE = {} # Training if training_args.do_train: logger.info("""*** Train ***""" ) _SCREAMING_SNAKE_CASE = trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) _SCREAMING_SNAKE_CASE = train_result.metrics _SCREAMING_SNAKE_CASE = data_args.n_train trainer.save_model() # this also saves the tokenizer if trainer.is_world_process_zero(): handle_metrics("""train""" , SCREAMING_SNAKE_CASE_ , training_args.output_dir ) all_metrics.update(SCREAMING_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 ***""" ) _SCREAMING_SNAKE_CASE = trainer.evaluate(metric_key_prefix="""val""" ) _SCREAMING_SNAKE_CASE = data_args.n_val _SCREAMING_SNAKE_CASE = round(metrics["""val_loss"""] , 4 ) if trainer.is_world_process_zero(): handle_metrics("""val""" , SCREAMING_SNAKE_CASE_ , training_args.output_dir ) all_metrics.update(SCREAMING_SNAKE_CASE_ ) if training_args.do_predict: logger.info("""*** Predict ***""" ) _SCREAMING_SNAKE_CASE = trainer.predict(test_dataset=SCREAMING_SNAKE_CASE_ , metric_key_prefix="""test""" ) _SCREAMING_SNAKE_CASE = test_output.metrics _SCREAMING_SNAKE_CASE = data_args.n_test if trainer.is_world_process_zero(): _SCREAMING_SNAKE_CASE = round(metrics["""test_loss"""] , 4 ) handle_metrics("""test""" , SCREAMING_SNAKE_CASE_ , training_args.output_dir ) all_metrics.update(SCREAMING_SNAKE_CASE_ ) if training_args.predict_with_generate: _SCREAMING_SNAKE_CASE = tokenizer.batch_decode( test_output.predictions , skip_special_tokens=SCREAMING_SNAKE_CASE_ , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = lmap(str.strip , SCREAMING_SNAKE_CASE_ ) write_txt_file(SCREAMING_SNAKE_CASE_ , os.path.join(training_args.output_dir , """test_generations.txt""" ) ) if trainer.is_world_process_zero(): save_json(SCREAMING_SNAKE_CASE_ , os.path.join(training_args.output_dir , """all_results.json""" ) ) return all_metrics def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> List[str]: """simple docstring""" # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
0
'''simple docstring''' import pyarrow.parquet as pq import pytest from datasets import Audio, Dataset, DatasetDict, Features, NamedSplit, Sequence, Value, config from datasets.features.image import Image from datasets.io.parquet import ParquetDatasetReader, ParquetDatasetWriter, get_writer_batch_size from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" assert isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("""keep_in_memory""" , [False, True] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): _SCREAMING_SNAKE_CASE = ParquetDatasetReader(SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ , keep_in_memory=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_dataset(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @pytest.mark.parametrize( """features""" , [ None, {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}, {"""col_1""": """string""", """col_2""": """string""", """col_3""": """string"""}, {"""col_1""": """int32""", """col_2""": """int32""", """col_3""": """int32"""}, {"""col_1""": """float32""", """col_2""": """float32""", """col_3""": """float32"""}, ] , ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} _SCREAMING_SNAKE_CASE = features.copy() if features else default_expected_features _SCREAMING_SNAKE_CASE = ( Features({feature: Value(SCREAMING_SNAKE_CASE_ ) for feature, dtype in features.items()} ) if features is not None else None ) _SCREAMING_SNAKE_CASE = ParquetDatasetReader(SCREAMING_SNAKE_CASE_ , features=SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_dataset(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @pytest.mark.parametrize("""split""" , [None, NamedSplit("""train""" ), """train""", """test"""] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} _SCREAMING_SNAKE_CASE = ParquetDatasetReader(SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ , split=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_dataset(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert dataset.split == split if split else "train" @pytest.mark.parametrize("""path_type""" , [str, list] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" if issubclass(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE = parquet_path elif issubclass(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE = [parquet_path] _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} _SCREAMING_SNAKE_CASE = ParquetDatasetReader(SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_dataset(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=("train",) ) -> List[str]: """simple docstring""" assert isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) for split in splits: _SCREAMING_SNAKE_CASE = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("""keep_in_memory""" , [False, True] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): _SCREAMING_SNAKE_CASE = ParquetDatasetReader( {"""train""": parquet_path} , cache_dir=SCREAMING_SNAKE_CASE_ , keep_in_memory=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_datasetdict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @pytest.mark.parametrize( """features""" , [ None, {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}, {"""col_1""": """string""", """col_2""": """string""", """col_3""": """string"""}, {"""col_1""": """int32""", """col_2""": """int32""", """col_3""": """int32"""}, {"""col_1""": """float32""", """col_2""": """float32""", """col_3""": """float32"""}, ] , ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} _SCREAMING_SNAKE_CASE = features.copy() if features else default_expected_features _SCREAMING_SNAKE_CASE = ( Features({feature: Value(SCREAMING_SNAKE_CASE_ ) for feature, dtype in features.items()} ) if features is not None else None ) _SCREAMING_SNAKE_CASE = ParquetDatasetReader({"""train""": parquet_path} , features=SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_datasetdict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @pytest.mark.parametrize("""split""" , [None, NamedSplit("""train""" ), """train""", """test"""] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" if split: _SCREAMING_SNAKE_CASE = {split: parquet_path} else: _SCREAMING_SNAKE_CASE = """train""" _SCREAMING_SNAKE_CASE = {"""train""": parquet_path, """test""": parquet_path} _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} _SCREAMING_SNAKE_CASE = ParquetDatasetReader(SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_datasetdict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE = ParquetDatasetWriter(SCREAMING_SNAKE_CASE_ , tmp_path / """foo.parquet""" ) assert writer.write() > 0 _SCREAMING_SNAKE_CASE = pq.ParquetFile(tmp_path / """foo.parquet""" ) _SCREAMING_SNAKE_CASE = pf.read() assert dataset.data.table == output_table def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = str(shared_datadir / """test_image_rgb.jpg""" ) _SCREAMING_SNAKE_CASE = {"""image""": [image_path]} _SCREAMING_SNAKE_CASE = Features({"""image""": Image()} ) _SCREAMING_SNAKE_CASE = Dataset.from_dict(SCREAMING_SNAKE_CASE_ , features=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = ParquetDatasetWriter(SCREAMING_SNAKE_CASE_ , tmp_path / """foo.parquet""" ) assert writer.write() > 0 _SCREAMING_SNAKE_CASE = Dataset.from_parquet(str(tmp_path / """foo.parquet""" ) ) assert dataset.features == reloaded_dataset.features _SCREAMING_SNAKE_CASE = ParquetDatasetReader(str(tmp_path / """foo.parquet""" ) , streaming=SCREAMING_SNAKE_CASE_ ).read() assert dataset.features == reloaded_iterable_dataset.features @pytest.mark.parametrize( """feature, expected""" , [ (Features({"""foo""": Value("""int32""" )} ), None), (Features({"""image""": Image(), """foo""": Value("""int32""" )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS), (Features({"""nested""": Sequence(Audio() )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS), ] , ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" assert get_writer_batch_size(SCREAMING_SNAKE_CASE_ ) == expected
0
1
'''simple docstring''' import unittest from transformers import SqueezeBertConfig, 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, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, SqueezeBertModel, ) class _a (_lowerCamelCase): """simple docstring""" def __init__( self , A__ , A__=13 , A__=7 , A__=True , A__=True , A__=False , A__=True , A__=99 , A__=32 , A__=5 , A__=4 , A__=64 , A__="gelu" , A__=0.1 , A__=0.1 , A__=5_12 , A__=16 , A__=2 , A__=0.02 , A__=3 , A__=4 , A__=None , A__=2 , A__=2 , A__=2 , A__=2 , A__=4 , A__=1 , ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = parent _SCREAMING_SNAKE_CASE = batch_size _SCREAMING_SNAKE_CASE = seq_length _SCREAMING_SNAKE_CASE = is_training _SCREAMING_SNAKE_CASE = use_input_mask _SCREAMING_SNAKE_CASE = use_token_type_ids _SCREAMING_SNAKE_CASE = use_labels _SCREAMING_SNAKE_CASE = vocab_size _SCREAMING_SNAKE_CASE = hidden_size _SCREAMING_SNAKE_CASE = num_hidden_layers _SCREAMING_SNAKE_CASE = num_attention_heads _SCREAMING_SNAKE_CASE = intermediate_size _SCREAMING_SNAKE_CASE = hidden_act _SCREAMING_SNAKE_CASE = hidden_dropout_prob _SCREAMING_SNAKE_CASE = attention_probs_dropout_prob _SCREAMING_SNAKE_CASE = max_position_embeddings _SCREAMING_SNAKE_CASE = type_vocab_size _SCREAMING_SNAKE_CASE = type_sequence_label_size _SCREAMING_SNAKE_CASE = initializer_range _SCREAMING_SNAKE_CASE = num_labels _SCREAMING_SNAKE_CASE = num_choices _SCREAMING_SNAKE_CASE = scope _SCREAMING_SNAKE_CASE = q_groups _SCREAMING_SNAKE_CASE = k_groups _SCREAMING_SNAKE_CASE = v_groups _SCREAMING_SNAKE_CASE = post_attention_groups _SCREAMING_SNAKE_CASE = intermediate_groups _SCREAMING_SNAKE_CASE = output_groups def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _SCREAMING_SNAKE_CASE = None if self.use_input_mask: _SCREAMING_SNAKE_CASE = random_attention_mask([self.batch_size, self.seq_length] ) _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None if self.use_labels: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.num_choices ) _SCREAMING_SNAKE_CASE = self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCamelCase ( self ) -> Tuple: return SqueezeBertConfig( embedding_size=self.hidden_size , 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 , attention_probs_dropout_prob=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , q_groups=self.q_groups , k_groups=self.k_groups , v_groups=self.v_groups , post_attention_groups=self.post_attention_groups , intermediate_groups=self.intermediate_groups , output_groups=self.output_groups , ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ ) -> Any: _SCREAMING_SNAKE_CASE = SqueezeBertModel(config=A__ ) model.to(A__ ) model.eval() _SCREAMING_SNAKE_CASE = model(A__ , A__ ) _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ ) -> List[str]: _SCREAMING_SNAKE_CASE = SqueezeBertForMaskedLM(config=A__ ) model.to(A__ ) model.eval() _SCREAMING_SNAKE_CASE = model(A__ , attention_mask=A__ , labels=A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ ) -> int: _SCREAMING_SNAKE_CASE = SqueezeBertForQuestionAnswering(config=A__ ) model.to(A__ ) model.eval() _SCREAMING_SNAKE_CASE = model( A__ , attention_mask=A__ , start_positions=A__ , end_positions=A__ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ ) -> Optional[int]: _SCREAMING_SNAKE_CASE = self.num_labels _SCREAMING_SNAKE_CASE = SqueezeBertForSequenceClassification(A__ ) model.to(A__ ) model.eval() _SCREAMING_SNAKE_CASE = model(A__ , attention_mask=A__ , labels=A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ ) -> Optional[int]: _SCREAMING_SNAKE_CASE = self.num_labels _SCREAMING_SNAKE_CASE = SqueezeBertForTokenClassification(config=A__ ) model.to(A__ ) model.eval() _SCREAMING_SNAKE_CASE = model(A__ , attention_mask=A__ , labels=A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.num_choices _SCREAMING_SNAKE_CASE = SqueezeBertForMultipleChoice(config=A__ ) model.to(A__ ) model.eval() _SCREAMING_SNAKE_CASE = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _SCREAMING_SNAKE_CASE = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _SCREAMING_SNAKE_CASE = model( A__ , attention_mask=A__ , labels=A__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def UpperCamelCase ( self ) -> int: _SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() ((_SCREAMING_SNAKE_CASE) , (_SCREAMING_SNAKE_CASE) , (_SCREAMING_SNAKE_CASE) , (_SCREAMING_SNAKE_CASE) , (_SCREAMING_SNAKE_CASE) , (_SCREAMING_SNAKE_CASE)) = config_and_inputs _SCREAMING_SNAKE_CASE = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_torch class _a (_lowerCamelCase , _lowerCamelCase , unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = ( ( SqueezeBertModel, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, ) if is_torch_available() else None ) SCREAMING_SNAKE_CASE = ( { 'feature-extraction': SqueezeBertModel, 'fill-mask': SqueezeBertForMaskedLM, 'question-answering': SqueezeBertForQuestionAnswering, 'text-classification': SqueezeBertForSequenceClassification, 'token-classification': SqueezeBertForTokenClassification, 'zero-shot': SqueezeBertForSequenceClassification, } if is_torch_available() else {} ) SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = False def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = SqueezeBertModelTester(self ) _SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=A__ , dim=37 ) def UpperCamelCase ( self ) -> Optional[Any]: self.config_tester.run_common_tests() def UpperCamelCase ( self ) -> Dict: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_model(*A__ ) def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_masked_lm(*A__ ) def UpperCamelCase ( self ) -> Dict: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_question_answering(*A__ ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_sequence_classification(*A__ ) def UpperCamelCase ( self ) -> Dict: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_token_classification(*A__ ) def UpperCamelCase ( self ) -> Dict: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_multiple_choice(*A__ ) @slow def UpperCamelCase ( self ) -> List[Any]: for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _SCREAMING_SNAKE_CASE = SqueezeBertModel.from_pretrained(A__ ) self.assertIsNotNone(A__ ) @require_sentencepiece @require_tokenizers @require_torch class _a (unittest.TestCase): """simple docstring""" @slow def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = SqueezeBertForSequenceClassification.from_pretrained("""squeezebert/squeezebert-mnli""" ) _SCREAMING_SNAKE_CASE = torch.tensor([[1, 2_94_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 13, 15_88, 2]] ) _SCREAMING_SNAKE_CASE = model(A__ )[0] _SCREAMING_SNAKE_CASE = torch.Size((1, 3) ) self.assertEqual(output.shape , A__ ) _SCREAMING_SNAKE_CASE = torch.tensor([[0.6401, -0.0349, -0.6041]] ) self.assertTrue(torch.allclose(A__ , A__ , atol=1E-4 ) )
0
'''simple docstring''' def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): raise ValueError("""multiplicative_persistence() only accepts integral values""" ) if num < 0: raise ValueError("""multiplicative_persistence() does not accept negative values""" ) _SCREAMING_SNAKE_CASE = 0 _SCREAMING_SNAKE_CASE = str(SCREAMING_SNAKE_CASE_ ) while len(SCREAMING_SNAKE_CASE_ ) != 1: _SCREAMING_SNAKE_CASE = [int(SCREAMING_SNAKE_CASE_ ) for i in num_string] _SCREAMING_SNAKE_CASE = 1 for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) ): total *= numbers[i] _SCREAMING_SNAKE_CASE = str(SCREAMING_SNAKE_CASE_ ) steps += 1 return steps def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): raise ValueError("""additive_persistence() only accepts integral values""" ) if num < 0: raise ValueError("""additive_persistence() does not accept negative values""" ) _SCREAMING_SNAKE_CASE = 0 _SCREAMING_SNAKE_CASE = str(SCREAMING_SNAKE_CASE_ ) while len(SCREAMING_SNAKE_CASE_ ) != 1: _SCREAMING_SNAKE_CASE = [int(SCREAMING_SNAKE_CASE_ ) for i in num_string] _SCREAMING_SNAKE_CASE = 0 for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) ): total += numbers[i] _SCREAMING_SNAKE_CASE = str(SCREAMING_SNAKE_CASE_ ) steps += 1 return steps if __name__ == "__main__": import doctest doctest.testmod()
0
1
'''simple docstring''' import fire from utils import calculate_rouge, save_json def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None , **SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = [x.strip() for x in open(SCREAMING_SNAKE_CASE_ ).readlines()] _SCREAMING_SNAKE_CASE = [x.strip() for x in open(SCREAMING_SNAKE_CASE_ ).readlines()][: len(SCREAMING_SNAKE_CASE_ )] _SCREAMING_SNAKE_CASE = calculate_rouge(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) if save_path is not None: save_json(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , indent=SCREAMING_SNAKE_CASE_ ) return metrics # these print nicely if __name__ == "__main__": fire.Fire(calculate_rouge_path)
0
'''simple docstring''' import math import os import re import sys import unittest from pathlib import Path from typing import Tuple from unittest.mock import patch from parameterized import parameterized from transformers.testing_utils import ( CaptureStderr, ExtendSysPath, TestCasePlus, execute_subprocess_async, get_gpu_count, get_torch_dist_unique_port, require_apex, require_bitsandbytes, require_fairscale, require_torch, require_torch_gpu, require_torch_multi_gpu, require_torch_non_multi_gpu, slow, ) from transformers.trainer_callback import TrainerState from transformers.trainer_utils import set_seed UpperCamelCase__ : Tuple = os.path.abspath(os.path.dirname(__file__)) with ExtendSysPath(f"""{bindir}/../../examples/pytorch/translation"""): from run_translation import main # noqa set_seed(42) UpperCamelCase__ : Union[str, Any] = "sshleifer/student_marian_en_ro_6_1" UpperCamelCase__ : str = "sshleifer/tiny-mbart" @require_torch class _a (_lowerCamelCase): """simple docstring""" def UpperCamelCase ( self , A__=False , A__=None , A__=True , A__=True , A__=True , A__=True , ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = self.run_trainer( eval_steps=1 , max_len=12 , model_name=A__ , num_train_epochs=1 , distributed=A__ , extra_args_str=A__ , predict_with_generate=A__ , do_train=A__ , do_eval=A__ , do_predict=A__ , ) _SCREAMING_SNAKE_CASE = TrainerState.load_from_json(os.path.join(A__ , """trainer_state.json""" ) ).log_history if not do_eval: return _SCREAMING_SNAKE_CASE = [log for log in logs if """eval_loss""" in log.keys()] _SCREAMING_SNAKE_CASE = eval_metrics[0] if predict_with_generate: assert "eval_bleu" in first_step_stats _SCREAMING_SNAKE_CASE = eval_metrics[-1] assert isinstance(last_step_stats["""eval_bleu"""] , A__ ) assert not math.isnan(float(last_step_stats["""eval_loss"""] ) ), "eval_loss must not be `nan`" @require_torch_non_multi_gpu def UpperCamelCase ( self ) -> Optional[int]: self.run_seqaseq_quick() @require_torch_multi_gpu def UpperCamelCase ( self ) -> Optional[Any]: self.run_seqaseq_quick(distributed=A__ ) @require_torch_multi_gpu def UpperCamelCase ( self ) -> Union[str, Any]: self.run_seqaseq_quick(distributed=A__ ) @unittest.skip("""Requires an update of the env running those tests""" ) @require_torch_multi_gpu @require_fairscale def UpperCamelCase ( self ) -> Any: self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--sharded_ddp simple""" ) @unittest.skip("""Requires an update of the env running those tests""" ) @require_torch_multi_gpu @require_fairscale def UpperCamelCase ( self ) -> Tuple: self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--sharded_ddp simple --fp16""" ) @unittest.skip("""Requires an update of the env running those tests""" ) @require_torch_multi_gpu @require_fairscale def UpperCamelCase ( self ) -> str: self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--sharded_ddp zero_dp_2""" , predict_with_generate=A__ ) @unittest.skip("""Requires an update of the env running those tests""" ) @require_torch_multi_gpu @require_fairscale def UpperCamelCase ( self ) -> List[str]: self.run_seqaseq_quick( distributed=A__ , extra_args_str="""--sharded_ddp zero_dp_2 --fp16""" , predict_with_generate=A__ ) @require_apex @require_torch_gpu def UpperCamelCase ( self ) -> Optional[Any]: # XXX: apex breaks the trainer if it's run twice e.g. run_seq2seq.main() from the same # program and it breaks other tests that run from the same pytest worker, therefore until this is # sorted out it must be run only in an external program, that is distributed=True in this # test and only under one or more gpus - if we want cpu will need to make a special test # # specifically to the problem traced it to self.optimizer.step() - if it's run 2nd time via # 2nd main() call it botches the future eval. # self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--fp16 --fp16_backend=apex""" ) # test 2nd time - was getting eval_loss': nan' # to reproduce the problem set distributed=False self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--fp16 --fp16_backend=apex""" ) @parameterized.expand(["""base""", """low""", """high""", """mixed"""] ) @require_torch_multi_gpu def UpperCamelCase ( self , A__ ) -> List[Any]: # as each sub-test is slow-ish split into multiple sub-tests to avoid CI timeout _SCREAMING_SNAKE_CASE = { # test with the default log_level - should be info and thus log info once """base""": {"""extra_args_str""": """""", """n_matches""": 1}, # test with low log_level and log_level_replica - should be noisy on all processes # now the info string should appear twice on 2 processes """low""": {"""extra_args_str""": """--log_level debug --log_level_replica debug""", """n_matches""": 2}, # test with high log_level and low log_level_replica # now the info string should appear once only on the replica """high""": {"""extra_args_str""": """--log_level error --log_level_replica debug""", """n_matches""": 1}, # test with high log_level and log_level_replica - should be quiet on all processes """mixed""": {"""extra_args_str""": """--log_level error --log_level_replica error""", """n_matches""": 0}, } _SCREAMING_SNAKE_CASE = experiments[experiment_id] _SCREAMING_SNAKE_CASE = {"""distributed""": True, """predict_with_generate""": False, """do_eval""": False, """do_predict""": False} _SCREAMING_SNAKE_CASE = """Running training""" with CaptureStderr() as cl: self.run_seqaseq_quick(**A__ , extra_args_str=data["""extra_args_str"""] ) _SCREAMING_SNAKE_CASE = len(re.findall(A__ , cl.err ) ) self.assertEqual(A__ , data["""n_matches"""] ) @slow def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = self.run_trainer( eval_steps=2 , max_len=1_28 , model_name=A__ , learning_rate=3E-4 , num_train_epochs=10 , distributed=A__ , ) # Check metrics _SCREAMING_SNAKE_CASE = TrainerState.load_from_json(os.path.join(A__ , """trainer_state.json""" ) ).log_history _SCREAMING_SNAKE_CASE = [log for log in logs if """eval_loss""" in log.keys()] _SCREAMING_SNAKE_CASE = eval_metrics[0] _SCREAMING_SNAKE_CASE = eval_metrics[-1] assert first_step_stats["eval_loss"] > last_step_stats["eval_loss"], "model learned nothing" assert isinstance(last_step_stats["""eval_bleu"""] , A__ ) # test if do_predict saves generations and metrics _SCREAMING_SNAKE_CASE = os.listdir(A__ ) _SCREAMING_SNAKE_CASE = {os.path.basename(A__ ) for p in contents} assert "generated_predictions.txt" in contents assert "predict_results.json" in contents @slow @require_bitsandbytes def UpperCamelCase ( self ) -> Dict: from transformers.training_args import OptimizerNames def train_and_return_metrics(A__ ) -> Tuple[int, float]: _SCREAMING_SNAKE_CASE = """--skip_memory_metrics 0""" _SCREAMING_SNAKE_CASE = self.run_trainer( max_len=1_28 , model_name=A__ , learning_rate=3E-4 , num_train_epochs=1 , optim=A__ , distributed=A__ , extra_args_str=A__ , do_eval=A__ , do_predict=A__ , n_gpus_to_use=1 , ) # Check metrics _SCREAMING_SNAKE_CASE = TrainerState.load_from_json(Path(A__ , """trainer_state.json""" ) ).log_history _SCREAMING_SNAKE_CASE = int(logs[0]["""train_mem_gpu_peaked_delta"""] / 2**20 ) _SCREAMING_SNAKE_CASE = int(logs[0]["""train_mem_gpu_alloc_delta"""] / 2**20 ) _SCREAMING_SNAKE_CASE = logs[0]["""train_loss"""] return gpu_peak_mem_mb, gpu_alloc_mem_mb, loss _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = train_and_return_metrics(OptimizerNames.ADAMW_TORCH.value ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = train_and_return_metrics(OptimizerNames.ADAMW_BNB.value ) _SCREAMING_SNAKE_CASE = gpu_alloc_mem_orig - gpu_alloc_mem_bnb _SCREAMING_SNAKE_CASE = gpu_peak_mem_orig + gpu_alloc_mem_orig _SCREAMING_SNAKE_CASE = gpu_peak_mem_bnb + gpu_alloc_mem_bnb _SCREAMING_SNAKE_CASE = gpu_total_mem_orig - gpu_total_mem_bnb # sshleifer/student_marian_en_ro_6_1 has 54M parameter, 29M of which is `nn.Embedding` which # doesn't get quantized and remains in fp32. Therefore we only have 25M parameters quantized # in 2 bytes and the diff in optim memory usage is derived as so: # # - normal 25*8=~200MB (8 bytes per param) # - bnb 25*2= ~50MB (2 bytes per param) # # Thus we should expect ~150MB total memory saved. # # Peak memory should be the same - the total should be different by about that same margin # # After leaving a small margin to accommodate for differences between gpus let's check # that we have at least 120MB in savings _SCREAMING_SNAKE_CASE = 1_20 # uncomment the following if this test starts failing - requires py38 for a new print feature # gpu_peak_mem_diff = gpu_peak_mem_orig - gpu_peak_mem_bnb # print(f"{gpu_alloc_mem_orig=}MB {gpu_peak_mem_orig=}MB {gpu_alloc_mem_orig+gpu_peak_mem_orig=}MB") # print(f" {gpu_alloc_mem_bnb=}MB {gpu_peak_mem_bnb=}MB {gpu_alloc_mem_bnb+gpu_peak_mem_bnb=}MB") # print(f"{gpu_alloc_mem_diff=}MB") # print(f"{gpu_peak_mem_diff=}MB") # print(f"{gpu_total_mem_orig=}MB, {gpu_total_mem_bnb=}MB") # print(f"{gpu_total_mem_diff=}MB, {gpu_total_mem_diff=}MB") self.assertGreater( A__ , A__ , """should use ~150MB less alloc gpu memory with BNB, compared to without it for this model but got""" F" a difference of {gpu_alloc_mem_diff}MB, with gpu_alloc_mem_orig={gpu_alloc_mem_orig}MB and" F" gpu_alloc_mem_bnb={gpu_alloc_mem_bnb}MB" , ) self.assertGreater( A__ , A__ , """should use ~150MB less total gpu memory with BNB, compared to without it for this model but got""" F" a difference of {gpu_total_mem_diff}MB, with gpu_total_mem_orig={gpu_total_mem_orig}MB and" F" gpu_total_mem_bnb={gpu_total_mem_bnb}MB" , ) self.assertEqual( A__ , A__ , F"loss should be the same, but got loss_orig={loss_orig}, loss_bnb={loss_bnb}" ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ = 3E-3 , A__ = "adafactor" , A__ = False , A__ = None , A__ = 0 , A__ = True , A__ = True , A__ = True , A__ = True , A__ = None , ) -> Dict: _SCREAMING_SNAKE_CASE = self.test_file_dir / """../fixtures/tests_samples/wmt_en_ro""" _SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() _SCREAMING_SNAKE_CASE = F"\n --model_name_or_path {model_name}\n --train_file {data_dir}/train.json\n --validation_file {data_dir}/val.json\n --test_file {data_dir}/test.json\n --output_dir {output_dir}\n --overwrite_output_dir\n --max_train_samples 8\n --max_source_length {max_len}\n --max_target_length {max_len}\n --do_train\n --num_train_epochs {str(A__ )}\n --per_device_train_batch_size 4\n --learning_rate {learning_rate}\n --warmup_steps 8\n --logging_steps 0\n --logging_strategy no\n --save_steps {str(A__ )}\n --group_by_length\n --label_smoothing_factor 0.1\n --target_lang ro_RO\n --source_lang en_XX\n ".split() _SCREAMING_SNAKE_CASE = F"\n --do_eval\n --per_device_eval_batch_size 4\n --max_eval_samples 8\n --val_max_target_length {max_len}\n --evaluation_strategy steps\n --eval_steps {str(A__ )}\n ".split() _SCREAMING_SNAKE_CASE = """ --do_predict """.split() _SCREAMING_SNAKE_CASE = [] if do_train: args += args_train if do_eval: args += args_eval if do_predict: args += args_predict if predict_with_generate: args += "--predict_with_generate".split() if do_train: if optim == "adafactor": args += "--adafactor".split() else: args += F"--optim {optim}".split() if extra_args_str is not None: args += extra_args_str.split() if distributed: if n_gpus_to_use is None: _SCREAMING_SNAKE_CASE = get_gpu_count() _SCREAMING_SNAKE_CASE = get_torch_dist_unique_port() _SCREAMING_SNAKE_CASE = F"\n -m torch.distributed.run\n --nproc_per_node={n_gpus_to_use}\n --master_port={master_port}\n {self.examples_dir_str}/pytorch/translation/run_translation.py\n ".split() _SCREAMING_SNAKE_CASE = [sys.executable] + distributed_args + args # keep for quick debug # print(" ".join([f"\nPYTHONPATH={self.src_dir_str}"] +cmd)); die execute_subprocess_async(A__ , env=self.get_env() ) else: _SCREAMING_SNAKE_CASE = ["""run_translation.py"""] + args with patch.object(A__ , """argv""" , A__ ): main() return output_dir
0
1
'''simple docstring''' import argparse import torch from transformers import YosoConfig, YosoForMaskedLM def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" if "model" in orig_key: _SCREAMING_SNAKE_CASE = orig_key.replace("""model.""" , """""" ) if "norm1" in orig_key: _SCREAMING_SNAKE_CASE = orig_key.replace("""norm1""" , """attention.output.LayerNorm""" ) if "norm2" in orig_key: _SCREAMING_SNAKE_CASE = orig_key.replace("""norm2""" , """output.LayerNorm""" ) if "norm" in orig_key: _SCREAMING_SNAKE_CASE = orig_key.replace("""norm""" , """LayerNorm""" ) if "transformer" in orig_key: _SCREAMING_SNAKE_CASE = orig_key.split(""".""" )[0].split("""_""" )[-1] _SCREAMING_SNAKE_CASE = orig_key.replace(F"transformer_{layer_num}" , F"encoder.layer.{layer_num}" ) if "mha.attn" in orig_key: _SCREAMING_SNAKE_CASE = orig_key.replace("""mha.attn""" , """attention.self""" ) if "mha" in orig_key: _SCREAMING_SNAKE_CASE = orig_key.replace("""mha""" , """attention""" ) if "W_q" in orig_key: _SCREAMING_SNAKE_CASE = orig_key.replace("""W_q""" , """self.query""" ) if "W_k" in orig_key: _SCREAMING_SNAKE_CASE = orig_key.replace("""W_k""" , """self.key""" ) if "W_v" in orig_key: _SCREAMING_SNAKE_CASE = orig_key.replace("""W_v""" , """self.value""" ) if "ff1" in orig_key: _SCREAMING_SNAKE_CASE = orig_key.replace("""ff1""" , """intermediate.dense""" ) if "ff2" in orig_key: _SCREAMING_SNAKE_CASE = orig_key.replace("""ff2""" , """output.dense""" ) if "ff" in orig_key: _SCREAMING_SNAKE_CASE = orig_key.replace("""ff""" , """output.dense""" ) if "mlm_class" in orig_key: _SCREAMING_SNAKE_CASE = orig_key.replace("""mlm.mlm_class""" , """cls.predictions.decoder""" ) if "mlm" in orig_key: _SCREAMING_SNAKE_CASE = orig_key.replace("""mlm""" , """cls.predictions.transform""" ) if "cls" not in orig_key: _SCREAMING_SNAKE_CASE = """yoso.""" + orig_key return orig_key def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" for key in orig_state_dict.copy().keys(): _SCREAMING_SNAKE_CASE = orig_state_dict.pop(__A ) if ("pooler" in key) or ("sen_class" in key): continue else: _SCREAMING_SNAKE_CASE = val _SCREAMING_SNAKE_CASE = orig_state_dict["""cls.predictions.decoder.bias"""] _SCREAMING_SNAKE_CASE = torch.arange(__A ).expand((1, -1) ) + 2 return orig_state_dict def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE = torch.load(__A , map_location="""cpu""" )["""model_state_dict"""] _SCREAMING_SNAKE_CASE = YosoConfig.from_json_file(__A ) _SCREAMING_SNAKE_CASE = YosoForMaskedLM(__A ) _SCREAMING_SNAKE_CASE = convert_checkpoint_helper(config.max_position_embeddings , __A ) print(model.load_state_dict(__A ) ) model.eval() model.save_pretrained(__A ) print(F"Checkpoint successfuly converted. Model saved at {pytorch_dump_path}" ) if __name__ == "__main__": UpperCamelCase__ : List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--pytorch_model_path", default=None, type=str, required=True, help="Path to YOSO pytorch checkpoint." ) parser.add_argument( "--config_file", default=None, type=str, required=True, help="The json file for YOSO model config.", ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) UpperCamelCase__ : List[Any] = parser.parse_args() convert_yoso_checkpoint(args.pytorch_model_path, args.config_file, args.pytorch_dump_path)
700
'''simple docstring''' import sys UpperCamelCase__ : int = ( "73167176531330624919225119674426574742355349194934" "96983520312774506326239578318016984801869478851843" "85861560789112949495459501737958331952853208805511" "12540698747158523863050715693290963295227443043557" "66896648950445244523161731856403098711121722383113" "62229893423380308135336276614282806444486645238749" "30358907296290491560440772390713810515859307960866" "70172427121883998797908792274921901699720888093776" "65727333001053367881220235421809751254540594752243" "52584907711670556013604839586446706324415722155397" "53697817977846174064955149290862569321978468622482" "83972241375657056057490261407972968652414535100474" "82166370484403199890008895243450658541227588666881" "16427171479924442928230863465674813919123162824586" "17866458359124566529476545682848912883142607690042" "24219022671055626321111109370544217506941658960408" "07198403850962455444362981230987879927244284909188" "84580156166097919133875499200524063689912560717606" "05886116467109405077541002256983155200055935729725" "71636269561882670428252483600823257530420752963450" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ = N ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE = -sys.maxsize - 1 for i in range(len(SCREAMING_SNAKE_CASE_ ) - 12 ): _SCREAMING_SNAKE_CASE = 1 for j in range(13 ): product *= int(n[i + j] ) if product > largest_product: _SCREAMING_SNAKE_CASE = product return largest_product if __name__ == "__main__": print(f"""{solution() = }""")
0
0
'''simple docstring''' import datasets UpperCamelCase__ : Dict = '\\n@InProceedings{conneau2018xnli,\n author = "Conneau, Alexis\n and Rinott, Ruty\n and Lample, Guillaume\n and Williams, Adina\n and Bowman, Samuel R.\n and Schwenk, Holger\n and Stoyanov, Veselin",\n title = "XNLI: Evaluating Cross-lingual Sentence Representations",\n booktitle = "Proceedings of the 2018 Conference on Empirical Methods\n in Natural Language Processing",\n year = "2018",\n publisher = "Association for Computational Linguistics",\n location = "Brussels, Belgium",\n}\n' UpperCamelCase__ : str = '\\nXNLI is a subset of a few thousand examples from MNLI which has been translated\ninto a 14 different languages (some low-ish resource). As with MNLI, the goal is\nto predict textual entailment (does sentence A imply/contradict/neither sentence\nB) and is a classification task (given two sentences, predict one of three\nlabels).\n' UpperCamelCase__ : List[Any] = '\nComputes XNLI score which is just simple accuracy.\nArgs:\n predictions: Predicted labels.\n references: Ground truth labels.\nReturns:\n \'accuracy\': accuracy\nExamples:\n\n >>> predictions = [0, 1]\n >>> references = [0, 1]\n >>> xnli_metric = datasets.load_metric("xnli")\n >>> results = xnli_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'accuracy\': 1.0}\n' def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Tuple: """simple docstring""" return (preds == labels).mean() @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class _a (datasets.Metric): """simple docstring""" def UpperCamelCase ( self ) -> int: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""int64""" if self.config_name != """sts-b""" else """float32""" ), """references""": datasets.Value("""int64""" if self.config_name != """sts-b""" else """float32""" ), } ) , codebase_urls=[] , reference_urls=[] , format="""numpy""" , ) def UpperCamelCase ( self , A__ , A__ ) -> List[str]: return {"accuracy": simple_accuracy(UpperCAmelCase_ , UpperCAmelCase_ )}
701
'''simple docstring''' UpperCamelCase__ : Dict = { "a": "AAAAA", "b": "AAAAB", "c": "AAABA", "d": "AAABB", "e": "AABAA", "f": "AABAB", "g": "AABBA", "h": "AABBB", "i": "ABAAA", "j": "BBBAA", "k": "ABAAB", "l": "ABABA", "m": "ABABB", "n": "ABBAA", "o": "ABBAB", "p": "ABBBA", "q": "ABBBB", "r": "BAAAA", "s": "BAAAB", "t": "BAABA", "u": "BAABB", "v": "BBBAB", "w": "BABAA", "x": "BABAB", "y": "BABBA", "z": "BABBB", " ": " ", } UpperCamelCase__ : str = {value: key for key, value in encode_dict.items()} def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE = """""" for letter in word.lower(): if letter.isalpha() or letter == " ": encoded += encode_dict[letter] else: raise Exception("""encode() accepts only letters of the alphabet and spaces""" ) return encoded def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" if set(SCREAMING_SNAKE_CASE_ ) - {"A", "B", " "} != set(): raise Exception("""decode() accepts only 'A', 'B' and spaces""" ) _SCREAMING_SNAKE_CASE = """""" for word in coded.split(): while len(SCREAMING_SNAKE_CASE_ ) != 0: decoded += decode_dict[word[:5]] _SCREAMING_SNAKE_CASE = word[5:] decoded += " " return decoded.strip() if __name__ == "__main__": from doctest import testmod testmod()
0
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) UpperCamelCase__ : Optional[Any] = { "configuration_efficientformer": [ "EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "EfficientFormerConfig", ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Optional[Any] = ["EfficientFormerImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : List[str] = [ "EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "EfficientFormerForImageClassification", "EfficientFormerForImageClassificationWithTeacher", "EfficientFormerModel", "EfficientFormerPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : int = [ "TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TFEfficientFormerForImageClassification", "TFEfficientFormerForImageClassificationWithTeacher", "TFEfficientFormerModel", "TFEfficientFormerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_efficientformer import EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, EfficientFormerConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_efficientformer import EfficientFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_efficientformer import ( EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, EfficientFormerForImageClassification, EfficientFormerForImageClassificationWithTeacher, EfficientFormerModel, EfficientFormerPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_efficientformer import ( TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFEfficientFormerForImageClassification, TFEfficientFormerForImageClassificationWithTeacher, TFEfficientFormerModel, TFEfficientFormerPreTrainedModel, ) else: import sys UpperCamelCase__ : Union[str, Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
702
'''simple docstring''' import argparse import torch from torch import nn from transformers import MaMaaaConfig, MaMaaaForConditionalGeneration def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE = [ """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 lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = emb.weight.shape _SCREAMING_SNAKE_CASE = nn.Linear(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , bias=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = emb.weight.data return lin_layer def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = torch.load(SCREAMING_SNAKE_CASE_ , map_location="""cpu""" ) _SCREAMING_SNAKE_CASE = mam_aaa["""args"""] or mam_aaa["""cfg"""]["""model"""] _SCREAMING_SNAKE_CASE = mam_aaa["""model"""] remove_ignore_keys_(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = state_dict["""encoder.embed_tokens.weight"""].shape[0] _SCREAMING_SNAKE_CASE = MaMaaaConfig( vocab_size=SCREAMING_SNAKE_CASE_ , max_position_embeddings=10_24 , encoder_layers=args.encoder_layers , decoder_layers=args.decoder_layers , encoder_attention_heads=args.encoder_attention_heads , decoder_attention_heads=args.decoder_attention_heads , encoder_ffn_dim=args.encoder_ffn_embed_dim , decoder_ffn_dim=args.decoder_ffn_embed_dim , d_model=args.encoder_embed_dim , encoder_layerdrop=args.encoder_layerdrop , decoder_layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function="""relu""" , ) _SCREAMING_SNAKE_CASE = state_dict["""decoder.embed_tokens.weight"""] _SCREAMING_SNAKE_CASE = MaMaaaForConditionalGeneration(SCREAMING_SNAKE_CASE_ ) model.model.load_state_dict(SCREAMING_SNAKE_CASE_ , strict=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = make_linear_from_emb(model.model.shared ) return model if __name__ == "__main__": UpperCamelCase__ : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument("fairseq_path", type=str, help="path to a model.pt on local filesystem.") parser.add_argument("pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") UpperCamelCase__ : List[str] = parser.parse_args() UpperCamelCase__ : Any = convert_fairseq_mamaaa_checkpoint_from_disk(args.fairseq_pathß) model.save_pretrained(args.pytorch_dump_folder_path)
0
0
'''simple docstring''' import dataclasses import re from dataclasses import dataclass from functools import total_ordering from typing import Optional, Union UpperCamelCase__ : Any = re.compile(R"^(?P<major>\d+)" R"\.(?P<minor>\d+)" R"\.(?P<patch>\d+)$") @total_ordering @dataclass class _a : """simple docstring""" SCREAMING_SNAKE_CASE = 42 SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None def UpperCamelCase ( self ) -> Dict: _SCREAMING_SNAKE_CASE = _str_to_version_tuple(self.version_str ) def __repr__( self ) -> int: return F"{self.tuple[0]}.{self.tuple[1]}.{self.tuple[2]}" @property def UpperCamelCase ( self ) -> Union[str, Any]: return self.major, self.minor, self.patch def UpperCamelCase ( self , A__ ) -> List[Any]: if isinstance(UpperCamelCase_ , UpperCamelCase_ ): return Version(UpperCamelCase_ ) elif isinstance(UpperCamelCase_ , UpperCamelCase_ ): return other raise TypeError(F"{other} (type {type(UpperCamelCase_ )}) cannot be compared to version." ) def __eq__( self , A__ ) -> Optional[int]: try: _SCREAMING_SNAKE_CASE = self._validate_operand(UpperCamelCase_ ) except (TypeError, ValueError): return False else: return self.tuple == other.tuple def __lt__( self , A__ ) -> Tuple: _SCREAMING_SNAKE_CASE = self._validate_operand(UpperCamelCase_ ) return self.tuple < other.tuple def __hash__( self ) -> List[str]: return hash(_version_tuple_to_str(self.tuple ) ) @classmethod def UpperCamelCase ( cls , A__ ) -> List[Any]: _SCREAMING_SNAKE_CASE = {f.name for f in dataclasses.fields(cls )} return cls(**{k: v for k, v in dic.items() if k in field_names} ) def UpperCamelCase ( self ) -> str: return self.version_str def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE = _VERSION_REG.match(_lowercase ) if not res: raise ValueError(F"Invalid version \'{version_str}\'. Format should be x.y.z with {{x,y,z}} being digits." ) return tuple(int(_lowercase ) for v in [res.group("""major""" ), res.group("""minor""" ), res.group("""patch""" )] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" return ".".join(str(_lowercase ) for v in version_tuple )
703
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available UpperCamelCase__ : str = { "configuration_canine": ["CANINE_PRETRAINED_CONFIG_ARCHIVE_MAP", "CanineConfig"], "tokenization_canine": ["CanineTokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : List[Any] = [ "CANINE_PRETRAINED_MODEL_ARCHIVE_LIST", "CanineForMultipleChoice", "CanineForQuestionAnswering", "CanineForSequenceClassification", "CanineForTokenClassification", "CanineLayer", "CanineModel", "CaninePreTrainedModel", "load_tf_weights_in_canine", ] if TYPE_CHECKING: from .configuration_canine import CANINE_PRETRAINED_CONFIG_ARCHIVE_MAP, CanineConfig from .tokenization_canine import CanineTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_canine import ( CANINE_PRETRAINED_MODEL_ARCHIVE_LIST, CanineForMultipleChoice, CanineForQuestionAnswering, CanineForSequenceClassification, CanineForTokenClassification, CanineLayer, CanineModel, CaninePreTrainedModel, load_tf_weights_in_canine, ) else: import sys UpperCamelCase__ : str = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
0
0
import os import tempfile import unittest from pathlib import Path from transformers import AutoConfig, is_torch_available from transformers.testing_utils import require_torch, torch_device if is_torch_available(): from transformers import PyTorchBenchmark, PyTorchBenchmarkArguments @require_torch class _a (unittest.TestCase): """simple docstring""" def UpperCamelCase ( self , A__ ) -> Tuple: for model_result in results.values(): for batch_size, sequence_length in zip(model_result["""bs"""] , model_result["""ss"""] ): _SCREAMING_SNAKE_CASE = model_result["result"][batch_size][sequence_length] self.assertIsNotNone(__lowerCamelCase ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = "sshleifer/tiny-gpt2" _SCREAMING_SNAKE_CASE = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__lowerCamelCase , inference=__lowerCamelCase , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__lowerCamelCase , ) _SCREAMING_SNAKE_CASE = PyTorchBenchmark(__lowerCamelCase ) _SCREAMING_SNAKE_CASE = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = "sgugger/tiny-distilbert-classification" _SCREAMING_SNAKE_CASE = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__lowerCamelCase , inference=__lowerCamelCase , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__lowerCamelCase , only_pretrain_model=__lowerCamelCase , ) _SCREAMING_SNAKE_CASE = PyTorchBenchmark(__lowerCamelCase ) _SCREAMING_SNAKE_CASE = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = "sshleifer/tiny-gpt2" _SCREAMING_SNAKE_CASE = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__lowerCamelCase , inference=__lowerCamelCase , torchscript=__lowerCamelCase , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__lowerCamelCase , ) _SCREAMING_SNAKE_CASE = PyTorchBenchmark(__lowerCamelCase ) _SCREAMING_SNAKE_CASE = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) @unittest.skipIf(torch_device == """cpu""" , """Cant do half precision""" ) def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = "sshleifer/tiny-gpt2" _SCREAMING_SNAKE_CASE = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__lowerCamelCase , inference=__lowerCamelCase , fpaa=__lowerCamelCase , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__lowerCamelCase , ) _SCREAMING_SNAKE_CASE = PyTorchBenchmark(__lowerCamelCase ) _SCREAMING_SNAKE_CASE = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = "sshleifer/tiny-gpt2" _SCREAMING_SNAKE_CASE = AutoConfig.from_pretrained(__lowerCamelCase ) # set architectures equal to `None` _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__lowerCamelCase , inference=__lowerCamelCase , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__lowerCamelCase , ) _SCREAMING_SNAKE_CASE = PyTorchBenchmark(__lowerCamelCase , configs=[config] ) _SCREAMING_SNAKE_CASE = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = "sshleifer/tiny-gpt2" _SCREAMING_SNAKE_CASE = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__lowerCamelCase , inference=__lowerCamelCase , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__lowerCamelCase , ) _SCREAMING_SNAKE_CASE = PyTorchBenchmark(__lowerCamelCase ) _SCREAMING_SNAKE_CASE = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) @unittest.skipIf(torch_device == """cpu""" , """Can't do half precision""" ) def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = "sshleifer/tiny-gpt2" _SCREAMING_SNAKE_CASE = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__lowerCamelCase , inference=__lowerCamelCase , sequence_lengths=[8] , batch_sizes=[1] , fpaa=__lowerCamelCase , multi_process=__lowerCamelCase , ) _SCREAMING_SNAKE_CASE = PyTorchBenchmark(__lowerCamelCase ) _SCREAMING_SNAKE_CASE = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = "sshleifer/tiny-gpt2" _SCREAMING_SNAKE_CASE = AutoConfig.from_pretrained(__lowerCamelCase ) _SCREAMING_SNAKE_CASE = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__lowerCamelCase , inference=__lowerCamelCase , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__lowerCamelCase , ) _SCREAMING_SNAKE_CASE = PyTorchBenchmark(__lowerCamelCase , configs=[config] ) _SCREAMING_SNAKE_CASE = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = "sshleifer/tinier_bart" _SCREAMING_SNAKE_CASE = AutoConfig.from_pretrained(__lowerCamelCase ) _SCREAMING_SNAKE_CASE = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__lowerCamelCase , inference=__lowerCamelCase , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__lowerCamelCase , ) _SCREAMING_SNAKE_CASE = PyTorchBenchmark(__lowerCamelCase , configs=[config] ) _SCREAMING_SNAKE_CASE = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = "sshleifer/tiny-gpt2" _SCREAMING_SNAKE_CASE = AutoConfig.from_pretrained(__lowerCamelCase ) _SCREAMING_SNAKE_CASE = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__lowerCamelCase , inference=__lowerCamelCase , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__lowerCamelCase , ) _SCREAMING_SNAKE_CASE = PyTorchBenchmark(__lowerCamelCase , configs=[config] ) _SCREAMING_SNAKE_CASE = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = "sshleifer/tinier_bart" _SCREAMING_SNAKE_CASE = AutoConfig.from_pretrained(__lowerCamelCase ) _SCREAMING_SNAKE_CASE = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__lowerCamelCase , inference=__lowerCamelCase , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__lowerCamelCase , ) _SCREAMING_SNAKE_CASE = PyTorchBenchmark(__lowerCamelCase , configs=[config] ) _SCREAMING_SNAKE_CASE = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = "sshleifer/tiny-gpt2" with tempfile.TemporaryDirectory() as tmp_dir: _SCREAMING_SNAKE_CASE = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__lowerCamelCase , inference=__lowerCamelCase , save_to_csv=__lowerCamelCase , sequence_lengths=[8] , batch_sizes=[1] , inference_time_csv_file=os.path.join(__lowerCamelCase , """inf_time.csv""" ) , train_memory_csv_file=os.path.join(__lowerCamelCase , """train_mem.csv""" ) , inference_memory_csv_file=os.path.join(__lowerCamelCase , """inf_mem.csv""" ) , train_time_csv_file=os.path.join(__lowerCamelCase , """train_time.csv""" ) , env_info_csv_file=os.path.join(__lowerCamelCase , """env.csv""" ) , multi_process=__lowerCamelCase , ) _SCREAMING_SNAKE_CASE = PyTorchBenchmark(__lowerCamelCase ) benchmark.run() self.assertTrue(Path(os.path.join(__lowerCamelCase , """inf_time.csv""" ) ).exists() ) self.assertTrue(Path(os.path.join(__lowerCamelCase , """train_time.csv""" ) ).exists() ) self.assertTrue(Path(os.path.join(__lowerCamelCase , """inf_mem.csv""" ) ).exists() ) self.assertTrue(Path(os.path.join(__lowerCamelCase , """train_mem.csv""" ) ).exists() ) self.assertTrue(Path(os.path.join(__lowerCamelCase , """env.csv""" ) ).exists() ) def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = "sshleifer/tiny-gpt2" def _check_summary_is_not_empty(A__ ): self.assertTrue(hasattr(__lowerCamelCase , """sequential""" ) ) self.assertTrue(hasattr(__lowerCamelCase , """cumulative""" ) ) self.assertTrue(hasattr(__lowerCamelCase , """current""" ) ) self.assertTrue(hasattr(__lowerCamelCase , """total""" ) ) with tempfile.TemporaryDirectory() as tmp_dir: _SCREAMING_SNAKE_CASE = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__lowerCamelCase , inference=__lowerCamelCase , sequence_lengths=[8] , batch_sizes=[1] , log_filename=os.path.join(__lowerCamelCase , """log.txt""" ) , log_print=__lowerCamelCase , trace_memory_line_by_line=__lowerCamelCase , multi_process=__lowerCamelCase , ) _SCREAMING_SNAKE_CASE = PyTorchBenchmark(__lowerCamelCase ) _SCREAMING_SNAKE_CASE = benchmark.run() _check_summary_is_not_empty(result.inference_summary ) _check_summary_is_not_empty(result.train_summary ) self.assertTrue(Path(os.path.join(__lowerCamelCase , """log.txt""" ) ).exists() )
704
'''simple docstring''' import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class _a (_lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = ['image_processor', 'tokenizer'] SCREAMING_SNAKE_CASE = 'ChineseCLIPImageProcessor' SCREAMING_SNAKE_CASE = ('BertTokenizer', 'BertTokenizerFast') def __init__( self , A__=None , A__=None , **A__ ) -> int: _SCREAMING_SNAKE_CASE = None if "feature_extractor" in kwargs: warnings.warn( """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`""" """ instead.""" , A__ , ) _SCREAMING_SNAKE_CASE = kwargs.pop("""feature_extractor""" ) _SCREAMING_SNAKE_CASE = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("""You need to specify an `image_processor`.""" ) if tokenizer is None: raise ValueError("""You need to specify a `tokenizer`.""" ) super().__init__(A__ , A__ ) _SCREAMING_SNAKE_CASE = self.image_processor def __call__( self , A__=None , A__=None , A__=None , **A__ ) -> Optional[int]: if text is None and images is None: raise ValueError("""You have to specify either text or images. Both cannot be none.""" ) if text is not None: _SCREAMING_SNAKE_CASE = self.tokenizer(A__ , return_tensors=A__ , **A__ ) if images is not None: _SCREAMING_SNAKE_CASE = self.image_processor(A__ , return_tensors=A__ , **A__ ) if text is not None and images is not None: _SCREAMING_SNAKE_CASE = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**A__ ) , tensor_type=A__ ) def UpperCamelCase ( self , *A__ , **A__ ) -> Dict: return self.tokenizer.batch_decode(*A__ , **A__ ) def UpperCamelCase ( self , *A__ , **A__ ) -> Optional[Any]: return self.tokenizer.decode(*A__ , **A__ ) @property def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.tokenizer.model_input_names _SCREAMING_SNAKE_CASE = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def UpperCamelCase ( self ) -> Optional[int]: warnings.warn( """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.""" , A__ , ) return self.image_processor_class
0
0
from dataclasses import dataclass from typing import Optional import numpy as np import torch import torch.nn as nn from ..utils import BaseOutput, is_torch_version, randn_tensor from .attention_processor import SpatialNorm from .unet_ad_blocks import UNetMidBlockaD, get_down_block, get_up_block @dataclass class _a (__lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = 42 class _a (nn.Module): """simple docstring""" def __init__( self , A__=3 , A__=3 , A__=("DownEncoderBlock2D",) , A__=(64,) , A__=2 , A__=32 , A__="silu" , A__=True , ) -> Any: super().__init__() _SCREAMING_SNAKE_CASE = layers_per_block _SCREAMING_SNAKE_CASE = torch.nn.Convad( UpperCAmelCase_ , block_out_channels[0] , kernel_size=3 , stride=1 , padding=1 , ) _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = nn.ModuleList([] ) # down _SCREAMING_SNAKE_CASE = block_out_channels[0] for i, down_block_type in enumerate(UpperCAmelCase_ ): _SCREAMING_SNAKE_CASE = output_channel _SCREAMING_SNAKE_CASE = block_out_channels[i] _SCREAMING_SNAKE_CASE = i == len(UpperCAmelCase_ ) - 1 _SCREAMING_SNAKE_CASE = get_down_block( UpperCAmelCase_ , num_layers=self.layers_per_block , in_channels=UpperCAmelCase_ , out_channels=UpperCAmelCase_ , add_downsample=not is_final_block , resnet_eps=1E-6 , downsample_padding=0 , resnet_act_fn=UpperCAmelCase_ , resnet_groups=UpperCAmelCase_ , attention_head_dim=UpperCAmelCase_ , temb_channels=UpperCAmelCase_ , ) self.down_blocks.append(UpperCAmelCase_ ) # mid _SCREAMING_SNAKE_CASE = UNetMidBlockaD( in_channels=block_out_channels[-1] , resnet_eps=1E-6 , resnet_act_fn=UpperCAmelCase_ , output_scale_factor=1 , resnet_time_scale_shift="""default""" , attention_head_dim=block_out_channels[-1] , resnet_groups=UpperCAmelCase_ , temb_channels=UpperCAmelCase_ , ) # out _SCREAMING_SNAKE_CASE = nn.GroupNorm(num_channels=block_out_channels[-1] , num_groups=UpperCAmelCase_ , eps=1E-6 ) _SCREAMING_SNAKE_CASE = nn.SiLU() _SCREAMING_SNAKE_CASE = 2 * out_channels if double_z else out_channels _SCREAMING_SNAKE_CASE = nn.Convad(block_out_channels[-1] , UpperCAmelCase_ , 3 , padding=1 ) _SCREAMING_SNAKE_CASE = False def UpperCamelCase ( self , A__ ) -> Any: _SCREAMING_SNAKE_CASE = x _SCREAMING_SNAKE_CASE = self.conv_in(UpperCAmelCase_ ) if self.training and self.gradient_checkpointing: def create_custom_forward(A__ ): def custom_forward(*A__ ): return module(*UpperCAmelCase_ ) return custom_forward # down if is_torch_version(""">=""" , """1.11.0""" ): for down_block in self.down_blocks: _SCREAMING_SNAKE_CASE = torch.utils.checkpoint.checkpoint( create_custom_forward(UpperCAmelCase_ ) , UpperCAmelCase_ , use_reentrant=UpperCAmelCase_ ) # middle _SCREAMING_SNAKE_CASE = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , UpperCAmelCase_ , use_reentrant=UpperCAmelCase_ ) else: for down_block in self.down_blocks: _SCREAMING_SNAKE_CASE = torch.utils.checkpoint.checkpoint(create_custom_forward(UpperCAmelCase_ ) , UpperCAmelCase_ ) # middle _SCREAMING_SNAKE_CASE = torch.utils.checkpoint.checkpoint(create_custom_forward(self.mid_block ) , UpperCAmelCase_ ) else: # down for down_block in self.down_blocks: _SCREAMING_SNAKE_CASE = down_block(UpperCAmelCase_ ) # middle _SCREAMING_SNAKE_CASE = self.mid_block(UpperCAmelCase_ ) # post-process _SCREAMING_SNAKE_CASE = self.conv_norm_out(UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = self.conv_act(UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = self.conv_out(UpperCAmelCase_ ) return sample class _a (nn.Module): """simple docstring""" def __init__( self , A__=3 , A__=3 , A__=("UpDecoderBlock2D",) , A__=(64,) , A__=2 , A__=32 , A__="silu" , A__="group" , ) -> int: super().__init__() _SCREAMING_SNAKE_CASE = layers_per_block _SCREAMING_SNAKE_CASE = nn.Convad( UpperCAmelCase_ , block_out_channels[-1] , kernel_size=3 , stride=1 , padding=1 , ) _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = nn.ModuleList([] ) _SCREAMING_SNAKE_CASE = in_channels if norm_type == 'spatial' else None # mid _SCREAMING_SNAKE_CASE = UNetMidBlockaD( in_channels=block_out_channels[-1] , resnet_eps=1E-6 , resnet_act_fn=UpperCAmelCase_ , output_scale_factor=1 , resnet_time_scale_shift="""default""" if norm_type == """group""" else norm_type , attention_head_dim=block_out_channels[-1] , resnet_groups=UpperCAmelCase_ , temb_channels=UpperCAmelCase_ , ) # up _SCREAMING_SNAKE_CASE = list(reversed(UpperCAmelCase_ ) ) _SCREAMING_SNAKE_CASE = reversed_block_out_channels[0] for i, up_block_type in enumerate(UpperCAmelCase_ ): _SCREAMING_SNAKE_CASE = output_channel _SCREAMING_SNAKE_CASE = reversed_block_out_channels[i] _SCREAMING_SNAKE_CASE = i == len(UpperCAmelCase_ ) - 1 _SCREAMING_SNAKE_CASE = get_up_block( UpperCAmelCase_ , num_layers=self.layers_per_block + 1 , in_channels=UpperCAmelCase_ , out_channels=UpperCAmelCase_ , prev_output_channel=UpperCAmelCase_ , add_upsample=not is_final_block , resnet_eps=1E-6 , resnet_act_fn=UpperCAmelCase_ , resnet_groups=UpperCAmelCase_ , attention_head_dim=UpperCAmelCase_ , temb_channels=UpperCAmelCase_ , resnet_time_scale_shift=UpperCAmelCase_ , ) self.up_blocks.append(UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = output_channel # out if norm_type == "spatial": _SCREAMING_SNAKE_CASE = SpatialNorm(block_out_channels[0] , UpperCAmelCase_ ) else: _SCREAMING_SNAKE_CASE = nn.GroupNorm(num_channels=block_out_channels[0] , num_groups=UpperCAmelCase_ , eps=1E-6 ) _SCREAMING_SNAKE_CASE = nn.SiLU() _SCREAMING_SNAKE_CASE = nn.Convad(block_out_channels[0] , UpperCAmelCase_ , 3 , padding=1 ) _SCREAMING_SNAKE_CASE = False def UpperCamelCase ( self , A__ , A__=None ) -> Dict: _SCREAMING_SNAKE_CASE = z _SCREAMING_SNAKE_CASE = self.conv_in(UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = next(iter(self.up_blocks.parameters() ) ).dtype if self.training and self.gradient_checkpointing: def create_custom_forward(A__ ): def custom_forward(*A__ ): return module(*UpperCAmelCase_ ) return custom_forward if is_torch_version(""">=""" , """1.11.0""" ): # middle _SCREAMING_SNAKE_CASE = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , UpperCAmelCase_ , UpperCAmelCase_ , use_reentrant=UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = sample.to(UpperCAmelCase_ ) # up for up_block in self.up_blocks: _SCREAMING_SNAKE_CASE = torch.utils.checkpoint.checkpoint( create_custom_forward(UpperCAmelCase_ ) , UpperCAmelCase_ , UpperCAmelCase_ , use_reentrant=UpperCAmelCase_ ) else: # middle _SCREAMING_SNAKE_CASE = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , UpperCAmelCase_ , UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = sample.to(UpperCAmelCase_ ) # up for up_block in self.up_blocks: _SCREAMING_SNAKE_CASE = torch.utils.checkpoint.checkpoint(create_custom_forward(UpperCAmelCase_ ) , UpperCAmelCase_ , UpperCAmelCase_ ) else: # middle _SCREAMING_SNAKE_CASE = self.mid_block(UpperCAmelCase_ , UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = sample.to(UpperCAmelCase_ ) # up for up_block in self.up_blocks: _SCREAMING_SNAKE_CASE = up_block(UpperCAmelCase_ , UpperCAmelCase_ ) # post-process if latent_embeds is None: _SCREAMING_SNAKE_CASE = self.conv_norm_out(UpperCAmelCase_ ) else: _SCREAMING_SNAKE_CASE = self.conv_norm_out(UpperCAmelCase_ , UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = self.conv_act(UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = self.conv_out(UpperCAmelCase_ ) return sample class _a (nn.Module): """simple docstring""" def __init__( self , A__ , A__ , A__ , A__=None , A__="random" , A__=False , A__=True ) -> Optional[Any]: super().__init__() _SCREAMING_SNAKE_CASE = n_e _SCREAMING_SNAKE_CASE = vq_embed_dim _SCREAMING_SNAKE_CASE = beta _SCREAMING_SNAKE_CASE = legacy _SCREAMING_SNAKE_CASE = nn.Embedding(self.n_e , self.vq_embed_dim ) self.embedding.weight.data.uniform_(-1.0 / self.n_e , 1.0 / self.n_e ) _SCREAMING_SNAKE_CASE = remap if self.remap is not None: self.register_buffer("""used""" , torch.tensor(np.load(self.remap ) ) ) _SCREAMING_SNAKE_CASE = self.used.shape[0] _SCREAMING_SNAKE_CASE = unknown_index # "random" or "extra" or integer if self.unknown_index == "extra": _SCREAMING_SNAKE_CASE = self.re_embed _SCREAMING_SNAKE_CASE = self.re_embed + 1 print( F"Remapping {self.n_e} indices to {self.re_embed} indices. " F"Using {self.unknown_index} for unknown indices." ) else: _SCREAMING_SNAKE_CASE = n_e _SCREAMING_SNAKE_CASE = sane_index_shape def UpperCamelCase ( self , A__ ) -> List[Any]: _SCREAMING_SNAKE_CASE = inds.shape assert len(UpperCAmelCase_ ) > 1 _SCREAMING_SNAKE_CASE = inds.reshape(ishape[0] , -1 ) _SCREAMING_SNAKE_CASE = self.used.to(UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = (inds[:, :, None] == used[None, None, ...]).long() _SCREAMING_SNAKE_CASE = match.argmax(-1 ) _SCREAMING_SNAKE_CASE = match.sum(2 ) < 1 if self.unknown_index == "random": _SCREAMING_SNAKE_CASE = torch.randint(0 , self.re_embed , size=new[unknown].shape ).to(device=new.device ) else: _SCREAMING_SNAKE_CASE = self.unknown_index return new.reshape(UpperCAmelCase_ ) def UpperCamelCase ( self , A__ ) -> Optional[int]: _SCREAMING_SNAKE_CASE = inds.shape assert len(UpperCAmelCase_ ) > 1 _SCREAMING_SNAKE_CASE = inds.reshape(ishape[0] , -1 ) _SCREAMING_SNAKE_CASE = self.used.to(UpperCAmelCase_ ) if self.re_embed > self.used.shape[0]: # extra token _SCREAMING_SNAKE_CASE = 0 # simply set to zero _SCREAMING_SNAKE_CASE = torch.gather(used[None, :][inds.shape[0] * [0], :] , 1 , UpperCAmelCase_ ) return back.reshape(UpperCAmelCase_ ) def UpperCamelCase ( self , A__ ) -> List[Any]: # reshape z -> (batch, height, width, channel) and flatten _SCREAMING_SNAKE_CASE = z.permute(0 , 2 , 3 , 1 ).contiguous() _SCREAMING_SNAKE_CASE = z.view(-1 , self.vq_embed_dim ) # distances from z to embeddings e_j (z - e)^2 = z^2 + e^2 - 2 e * z _SCREAMING_SNAKE_CASE = torch.argmin(torch.cdist(UpperCAmelCase_ , self.embedding.weight ) , dim=1 ) _SCREAMING_SNAKE_CASE = self.embedding(UpperCAmelCase_ ).view(z.shape ) _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None # compute loss for embedding if not self.legacy: _SCREAMING_SNAKE_CASE = self.beta * torch.mean((z_q.detach() - z) ** 2 ) + torch.mean((z_q - z.detach()) ** 2 ) else: _SCREAMING_SNAKE_CASE = torch.mean((z_q.detach() - z) ** 2 ) + self.beta * torch.mean((z_q - z.detach()) ** 2 ) # preserve gradients _SCREAMING_SNAKE_CASE = z + (z_q - z).detach() # reshape back to match original input shape _SCREAMING_SNAKE_CASE = z_q.permute(0 , 3 , 1 , 2 ).contiguous() if self.remap is not None: _SCREAMING_SNAKE_CASE = min_encoding_indices.reshape(z.shape[0] , -1 ) # add batch axis _SCREAMING_SNAKE_CASE = self.remap_to_used(UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = min_encoding_indices.reshape(-1 , 1 ) # flatten if self.sane_index_shape: _SCREAMING_SNAKE_CASE = min_encoding_indices.reshape(z_q.shape[0] , z_q.shape[2] , z_q.shape[3] ) return z_q, loss, (perplexity, min_encodings, min_encoding_indices) def UpperCamelCase ( self , A__ , A__ ) -> Optional[Any]: # shape specifying (batch, height, width, channel) if self.remap is not None: _SCREAMING_SNAKE_CASE = indices.reshape(shape[0] , -1 ) # add batch axis _SCREAMING_SNAKE_CASE = self.unmap_to_all(UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = indices.reshape(-1 ) # flatten again # get quantized latent vectors _SCREAMING_SNAKE_CASE = self.embedding(UpperCAmelCase_ ) if shape is not None: _SCREAMING_SNAKE_CASE = z_q.view(UpperCAmelCase_ ) # reshape back to match original input shape _SCREAMING_SNAKE_CASE = z_q.permute(0 , 3 , 1 , 2 ).contiguous() return z_q class _a (__lowerCamelCase): """simple docstring""" def __init__( self , A__ , A__=False ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = parameters _SCREAMING_SNAKE_CASE = torch.chunk(UpperCAmelCase_ , 2 , dim=1 ) _SCREAMING_SNAKE_CASE = torch.clamp(self.logvar , -30.0 , 20.0 ) _SCREAMING_SNAKE_CASE = deterministic _SCREAMING_SNAKE_CASE = torch.exp(0.5 * self.logvar ) _SCREAMING_SNAKE_CASE = torch.exp(self.logvar ) if self.deterministic: _SCREAMING_SNAKE_CASE = torch.zeros_like( self.mean , device=self.parameters.device , dtype=self.parameters.dtype ) def UpperCamelCase ( self , A__ = None ) -> str: # make sure sample is on the same device as the parameters and has same dtype _SCREAMING_SNAKE_CASE = randn_tensor( self.mean.shape , generator=UpperCAmelCase_ , device=self.parameters.device , dtype=self.parameters.dtype ) _SCREAMING_SNAKE_CASE = self.mean + self.std * sample return x def UpperCamelCase ( self , A__=None ) -> Optional[int]: if self.deterministic: return torch.Tensor([0.0] ) else: if other is None: return 0.5 * torch.sum(torch.pow(self.mean , 2 ) + self.var - 1.0 - self.logvar , dim=[1, 2, 3] ) else: return 0.5 * torch.sum( torch.pow(self.mean - other.mean , 2 ) / other.var + self.var / other.var - 1.0 - self.logvar + other.logvar , dim=[1, 2, 3] , ) def UpperCamelCase ( self , A__ , A__=[1, 2, 3] ) -> Union[str, Any]: if self.deterministic: return torch.Tensor([0.0] ) _SCREAMING_SNAKE_CASE = np.log(2.0 * np.pi ) return 0.5 * torch.sum(logtwopi + self.logvar + torch.pow(sample - self.mean , 2 ) / self.var , dim=UpperCAmelCase_ ) def UpperCamelCase ( self ) -> Tuple: return self.mean
705
'''simple docstring''' from sklearn.metrics import matthews_corrcoef import datasets UpperCamelCase__ : List[str] = "\nCompute the Matthews correlation coefficient (MCC)\n\nThe Matthews correlation coefficient is used in machine learning as a\nmeasure of the quality of binary and multiclass classifications. It takes\ninto account true and false positives and negatives and is generally\nregarded as a balanced measure which can be used even if the classes are of\nvery different sizes. The MCC is in essence a correlation coefficient value\nbetween -1 and +1. A coefficient of +1 represents a perfect prediction, 0\nan average random prediction and -1 an inverse prediction. The statistic\nis also known as the phi coefficient. [source: Wikipedia]\n" UpperCamelCase__ : List[Any] = "\nArgs:\n predictions (list of int): Predicted labels, as returned by a model.\n references (list of int): Ground truth labels.\n sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`.\nReturns:\n matthews_correlation (dict containing float): Matthews correlation.\nExamples:\n Example 1, a basic example with only predictions and references as inputs:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3])\n >>> print(round(results['matthews_correlation'], 2))\n 0.54\n\n Example 2, the same example as above, but also including sample weights:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 3, 1, 1, 1, 2])\n >>> print(round(results['matthews_correlation'], 2))\n 0.1\n\n Example 3, the same example as above, but with sample weights that cause a negative correlation:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 1, 0, 0, 0, 1])\n >>> print(round(results['matthews_correlation'], 2))\n -0.25\n" UpperCamelCase__ : Any = "\\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class _a (datasets.Metric): """simple docstring""" def UpperCamelCase ( self ) -> Optional[Any]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=[ """https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html""" ] , ) def UpperCamelCase ( self , A__ , A__ , A__=None ) -> List[str]: return { "matthews_correlation": float(matthews_corrcoef(A__ , A__ , sample_weight=A__ ) ), }
0
0
import requests UpperCamelCase__ : Tuple = "YOUR API KEY" def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = giphy_api_key ) -> list: """simple docstring""" _SCREAMING_SNAKE_CASE = '+'.join(query.split() ) _SCREAMING_SNAKE_CASE = F"https://api.giphy.com/v1/gifs/search?q={formatted_query}&api_key={api_key}" _SCREAMING_SNAKE_CASE = requests.get(SCREAMING_SNAKE_CASE_ ).json()['data'] return [gif["url"] for gif in gifs] if __name__ == "__main__": print("\n".join(get_gifs("space ship")))
706
'''simple docstring''' from __future__ import annotations def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Tuple: """simple docstring""" print(F"Vertex\tShortest Distance from vertex {src}" ) for i, d in enumerate(SCREAMING_SNAKE_CASE_ ): print(F"{i}\t\t{d}" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" for j in range(SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = (graph[j][k] for k in ["""src""", """dst""", """weight"""]) if distance[u] != float("""inf""" ) and distance[u] + w < distance[v]: return True return False def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> list[float]: """simple docstring""" _SCREAMING_SNAKE_CASE = [float("""inf""" )] * vertex_count _SCREAMING_SNAKE_CASE = 0.0 for _ in range(vertex_count - 1 ): for j in range(SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = (graph[j][k] for k in ["""src""", """dst""", """weight"""]) if distance[u] != float("""inf""" ) and distance[u] + w < distance[v]: _SCREAMING_SNAKE_CASE = distance[u] + w _SCREAMING_SNAKE_CASE = check_negative_cycle(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if negative_cycle_exists: raise Exception("""Negative cycle found""" ) return distance if __name__ == "__main__": import doctest doctest.testmod() UpperCamelCase__ : int = int(input("Enter number of vertices: ").strip()) UpperCamelCase__ : int = int(input("Enter number of edges: ").strip()) UpperCamelCase__ : list[dict[str, int]] = [{} for _ in range(E)] for i in range(E): print("Edge ", i + 1) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ : Dict = ( int(x) for x in input("Enter source, destination, weight: ").strip().split(" ") ) UpperCamelCase__ : Optional[Any] = {"src": src, "dst": dest, "weight": weight} UpperCamelCase__ : Optional[Any] = int(input("\nEnter shortest path source:").strip()) UpperCamelCase__ : Any = bellman_ford(graph, V, E, source) print_distance(shortest_distance, 0)
0
0
'''simple docstring''' from typing import List, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase__ : Union[str, Any] = logging.get_logger(__name__) UpperCamelCase__ : List[Any] = { "huggingface/informer-tourism-monthly": ( "https://huggingface.co/huggingface/informer-tourism-monthly/resolve/main/config.json" ), # See all Informer models at https://huggingface.co/models?filter=informer } class _a (__A): """simple docstring""" SCREAMING_SNAKE_CASE = 'informer' SCREAMING_SNAKE_CASE = { 'hidden_size': 'd_model', 'num_attention_heads': 'encoder_attention_heads', 'num_hidden_layers': 'encoder_layers', } def __init__( self , A__ = None , A__ = None , A__ = "student_t" , A__ = "nll" , A__ = 1 , A__ = None , A__ = "mean" , A__ = 0 , A__ = 0 , A__ = 0 , A__ = 0 , A__ = None , A__ = None , A__ = 64 , A__ = 32 , A__ = 32 , A__ = 2 , A__ = 2 , A__ = 2 , A__ = 2 , A__ = True , A__ = "gelu" , A__ = 0.05 , A__ = 0.1 , A__ = 0.1 , A__ = 0.1 , A__ = 0.1 , A__ = 1_00 , A__ = 0.02 , A__=True , A__ = "prob" , A__ = 5 , A__ = True , **A__ , ) -> Dict: _SCREAMING_SNAKE_CASE = prediction_length _SCREAMING_SNAKE_CASE = context_length or prediction_length _SCREAMING_SNAKE_CASE = distribution_output _SCREAMING_SNAKE_CASE = loss _SCREAMING_SNAKE_CASE = input_size _SCREAMING_SNAKE_CASE = num_time_features _SCREAMING_SNAKE_CASE = lags_sequence if lags_sequence is not None else [1, 2, 3, 4, 5, 6, 7] _SCREAMING_SNAKE_CASE = scaling _SCREAMING_SNAKE_CASE = num_dynamic_real_features _SCREAMING_SNAKE_CASE = num_static_real_features _SCREAMING_SNAKE_CASE = num_static_categorical_features # set cardinality if cardinality and num_static_categorical_features > 0: if len(A__ ) != num_static_categorical_features: raise ValueError( """The cardinality should be a list of the same length as `num_static_categorical_features`""" ) _SCREAMING_SNAKE_CASE = cardinality else: _SCREAMING_SNAKE_CASE = [0] # set embedding_dimension if embedding_dimension and num_static_categorical_features > 0: if len(A__ ) != num_static_categorical_features: raise ValueError( """The embedding dimension should be a list of the same length as `num_static_categorical_features`""" ) _SCREAMING_SNAKE_CASE = embedding_dimension else: _SCREAMING_SNAKE_CASE = [min(50 , (cat + 1) // 2 ) for cat in self.cardinality] _SCREAMING_SNAKE_CASE = num_parallel_samples # Transformer architecture configuration _SCREAMING_SNAKE_CASE = input_size * len(self.lags_sequence ) + self._number_of_features _SCREAMING_SNAKE_CASE = d_model _SCREAMING_SNAKE_CASE = encoder_attention_heads _SCREAMING_SNAKE_CASE = decoder_attention_heads _SCREAMING_SNAKE_CASE = encoder_ffn_dim _SCREAMING_SNAKE_CASE = decoder_ffn_dim _SCREAMING_SNAKE_CASE = encoder_layers _SCREAMING_SNAKE_CASE = decoder_layers _SCREAMING_SNAKE_CASE = dropout _SCREAMING_SNAKE_CASE = attention_dropout _SCREAMING_SNAKE_CASE = activation_dropout _SCREAMING_SNAKE_CASE = encoder_layerdrop _SCREAMING_SNAKE_CASE = decoder_layerdrop _SCREAMING_SNAKE_CASE = activation_function _SCREAMING_SNAKE_CASE = init_std _SCREAMING_SNAKE_CASE = use_cache # Informer _SCREAMING_SNAKE_CASE = attention_type _SCREAMING_SNAKE_CASE = sampling_factor _SCREAMING_SNAKE_CASE = distil super().__init__(is_encoder_decoder=A__ , **A__ ) @property def UpperCamelCase ( self ) -> int: return ( sum(self.embedding_dimension ) + self.num_dynamic_real_features + self.num_time_features + self.num_static_real_features + self.input_size * 2 # the log1p(abs(loc)) and log(scale) features )
707
'''simple docstring''' 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 , A__ , A__=13 , A__=7 , A__=True , A__=True , A__=True , A__=True , A__=99 , A__=32 , A__=2 , A__=4 , A__=37 , A__="gelu" , A__=0.1 , A__=0.1 , A__=5_12 , A__=16 , A__=2 , A__=0.02 , A__=3 , A__=4 , A__=None , ) -> int: _SCREAMING_SNAKE_CASE = parent _SCREAMING_SNAKE_CASE = 13 _SCREAMING_SNAKE_CASE = 7 _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = 99 _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 = 5_12 _SCREAMING_SNAKE_CASE = 16 _SCREAMING_SNAKE_CASE = 2 _SCREAMING_SNAKE_CASE = 0.02 _SCREAMING_SNAKE_CASE = 3 _SCREAMING_SNAKE_CASE = 4 _SCREAMING_SNAKE_CASE = None def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _SCREAMING_SNAKE_CASE = None if self.use_input_mask: _SCREAMING_SNAKE_CASE = random_attention_mask([self.batch_size, self.seq_length] ) _SCREAMING_SNAKE_CASE = None if self.use_token_type_ids: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None if self.use_labels: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.num_choices ) _SCREAMING_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=A__ , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = TFRoFormerModel(config=A__ ) _SCREAMING_SNAKE_CASE = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} _SCREAMING_SNAKE_CASE = [input_ids, input_mask] _SCREAMING_SNAKE_CASE = model(A__ ) _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> str: _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = TFRoFormerForCausalLM(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ )["""logits"""] self.parent.assertListEqual( list(prediction_scores.numpy().shape ) , [self.batch_size, self.seq_length, self.vocab_size] ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> Dict: _SCREAMING_SNAKE_CASE = TFRoFormerForMaskedLM(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> List[str]: _SCREAMING_SNAKE_CASE = self.num_labels _SCREAMING_SNAKE_CASE = TFRoFormerForSequenceClassification(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> Any: _SCREAMING_SNAKE_CASE = self.num_choices _SCREAMING_SNAKE_CASE = TFRoFormerForMultipleChoice(config=A__ ) _SCREAMING_SNAKE_CASE = tf.tile(tf.expand_dims(A__ , 1 ) , (1, self.num_choices, 1) ) _SCREAMING_SNAKE_CASE = tf.tile(tf.expand_dims(A__ , 1 ) , (1, self.num_choices, 1) ) _SCREAMING_SNAKE_CASE = tf.tile(tf.expand_dims(A__ , 1 ) , (1, self.num_choices, 1) ) _SCREAMING_SNAKE_CASE = { """input_ids""": multiple_choice_inputs_ids, """attention_mask""": multiple_choice_input_mask, """token_type_ids""": multiple_choice_token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> List[str]: _SCREAMING_SNAKE_CASE = self.num_labels _SCREAMING_SNAKE_CASE = TFRoFormerForTokenClassification(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> Tuple: _SCREAMING_SNAKE_CASE = TFRoFormerForQuestionAnswering(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = 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 ) , ) = config_and_inputs _SCREAMING_SNAKE_CASE = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask} return config, inputs_dict @require_tf class _a (_lowerCamelCase , _lowerCamelCase , unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = ( ( TFRoFormerModel, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerForMultipleChoice, ) if is_tf_available() else () ) SCREAMING_SNAKE_CASE = ( { '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 {} ) SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = False def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ ) -> str: if pipeline_test_casse_name == "TextGenerationPipelineTests": return True return False def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = TFRoFormerModelTester(self ) _SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=A__ , hidden_size=37 ) def UpperCamelCase ( self ) -> Optional[Any]: self.config_tester.run_common_tests() def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A__ ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*A__ ) def UpperCamelCase ( self ) -> int: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head(*A__ ) def UpperCamelCase ( self ) -> Dict: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*A__ ) def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*A__ ) def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*A__ ) def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*A__ ) @slow def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = TFRoFormerModel.from_pretrained("""junnyu/roformer_chinese_base""" ) self.assertIsNotNone(A__ ) @require_tf class _a (unittest.TestCase): """simple docstring""" @slow def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = TFRoFormerForMaskedLM.from_pretrained("""junnyu/roformer_chinese_base""" ) _SCREAMING_SNAKE_CASE = tf.constant([[0, 1, 2, 3, 4, 5]] ) _SCREAMING_SNAKE_CASE = model(A__ )[0] # TODO Replace vocab size _SCREAMING_SNAKE_CASE = 5_00_00 _SCREAMING_SNAKE_CASE = [1, 6, vocab_size] self.assertEqual(output.shape , A__ ) print(output[:, :3, :3] ) # TODO Replace values below with what was printed above. _SCREAMING_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] , A__ , atol=1E-4 ) @require_tf class _a (unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = 1E-4 def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = tf.constant([[4, 10]] ) _SCREAMING_SNAKE_CASE = TFRoFormerSinusoidalPositionalEmbedding(num_positions=6 , embedding_dim=6 ) _SCREAMING_SNAKE_CASE = emba(input_ids.shape ) _SCREAMING_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(A__ , A__ , atol=self.tolerance ) def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_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], ] ) _SCREAMING_SNAKE_CASE = TFRoFormerSinusoidalPositionalEmbedding(num_positions=5_12 , embedding_dim=5_12 ) emba([2, 16, 5_12] ) _SCREAMING_SNAKE_CASE = emba.weight[:3, :5] tf.debugging.assert_near(A__ , A__ , atol=self.tolerance ) @require_tf class _a (unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = 1E-4 def UpperCamelCase ( self ) -> int: # 2,12,16,64 _SCREAMING_SNAKE_CASE = tf.reshape(tf.range(2 * 12 * 16 * 64 , dtype=tf.floataa ) , shape=(2, 12, 16, 64) ) / 1_00 _SCREAMING_SNAKE_CASE = -tf.reshape(tf.range(2 * 12 * 16 * 64 , dtype=tf.floataa ) , shape=(2, 12, 16, 64) ) / 1_00 _SCREAMING_SNAKE_CASE = TFRoFormerSinusoidalPositionalEmbedding(num_positions=32 , embedding_dim=64 ) _SCREAMING_SNAKE_CASE = embed_positions([2, 16, 7_68] )[None, None, :, :] _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = TFRoFormerSelfAttention.apply_rotary_position_embeddings( A__ , A__ , A__ ) _SCREAMING_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], ] ) _SCREAMING_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] , A__ , atol=self.tolerance ) tf.debugging.assert_near(key_layer[0, 0, :6, :8] , A__ , atol=self.tolerance )
0
0
'''simple docstring''' import cva import numpy as np class _a : """simple docstring""" def __init__( self , A__ , A__ ) -> List[Any]: if k in (0.04, 0.06): _SCREAMING_SNAKE_CASE = k _SCREAMING_SNAKE_CASE = window_size else: raise ValueError("""invalid k value""" ) def __str__( self ) -> str: return str(self.k ) def UpperCamelCase ( self , A__ ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = cva.imread(_lowerCAmelCase , 0 ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = img.shape _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = img.copy() _SCREAMING_SNAKE_CASE = cva.cvtColor(_lowerCAmelCase , cva.COLOR_GRAY2RGB ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = np.gradient(_lowerCAmelCase ) _SCREAMING_SNAKE_CASE = dx**2 _SCREAMING_SNAKE_CASE = dy**2 _SCREAMING_SNAKE_CASE = dx * dy _SCREAMING_SNAKE_CASE = 0.04 _SCREAMING_SNAKE_CASE = self.window_size // 2 for y in range(_lowerCAmelCase , h - offset ): for x in range(_lowerCAmelCase , w - offset ): _SCREAMING_SNAKE_CASE = ixx[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() _SCREAMING_SNAKE_CASE = iyy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() _SCREAMING_SNAKE_CASE = ixy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() _SCREAMING_SNAKE_CASE = (wxx * wyy) - (wxy**2) _SCREAMING_SNAKE_CASE = wxx + wyy _SCREAMING_SNAKE_CASE = det - k * (trace**2) # Can change the value if r > 0.5: corner_list.append([x, y, r] ) color_img.itemset((y, x, 0) , 0 ) color_img.itemset((y, x, 1) , 0 ) color_img.itemset((y, x, 2) , 2_55 ) return color_img, corner_list if __name__ == "__main__": UpperCamelCase__ : Optional[Any] = HarrisCorner(0.04, 3) UpperCamelCase__ : Tuple = edge_detect.detect("path_to_image") cva.imwrite("detect.png", color_img)
708
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available UpperCamelCase__ : int = {"tokenization_herbert": ["HerbertTokenizer"]} try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Tuple = ["HerbertTokenizerFast"] if TYPE_CHECKING: from .tokenization_herbert import HerbertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_herbert_fast import HerbertTokenizerFast else: import sys UpperCamelCase__ : Tuple = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
0
0
'''simple docstring''' from __future__ import annotations from collections import Counter from random import random class _a : """simple docstring""" def __init__( self ) -> Any: _SCREAMING_SNAKE_CASE = {} def UpperCamelCase ( self , A__ ) -> List[str]: _SCREAMING_SNAKE_CASE = {} def UpperCamelCase ( self , A__ , A__ , A__ ) -> Any: if nodea not in self.connections: self.add_node(UpperCamelCase_ ) if nodea not in self.connections: self.add_node(UpperCamelCase_ ) _SCREAMING_SNAKE_CASE = probability def UpperCamelCase ( self ) -> Optional[int]: return list(self.connections ) def UpperCamelCase ( self , A__ ) -> str: _SCREAMING_SNAKE_CASE = 0 _SCREAMING_SNAKE_CASE = random() for dest in self.connections[node]: current_probability += self.connections[node][dest] if current_probability > random_value: return dest return "" def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> dict[str, int]: """simple docstring""" _SCREAMING_SNAKE_CASE = MarkovChainGraphUndirectedUnweighted() for nodea, nodea, probability in transitions: graph.add_transition_probability(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) _SCREAMING_SNAKE_CASE = Counter(graph.get_nodes() ) _SCREAMING_SNAKE_CASE = start for _ in range(lowerCamelCase__ ): _SCREAMING_SNAKE_CASE = graph.transition(lowerCamelCase__ ) visited[node] += 1 return visited if __name__ == "__main__": import doctest doctest.testmod()
709
'''simple docstring''' import argparse import gdown import numpy as np import torch from huggingface_hub import hf_hub_download from transformers import ( CLIPTokenizer, CLIPTokenizerFast, VideoMAEImageProcessor, XCLIPConfig, XCLIPModel, XCLIPProcessor, XCLIPTextConfig, XCLIPVisionConfig, ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE = XCLIPTextConfig() # derive patch size from model name _SCREAMING_SNAKE_CASE = model_name.find("""patch""" ) _SCREAMING_SNAKE_CASE = int(model_name[start_idx + len("""patch""" ) : start_idx + len("""patch""" ) + 2] ) _SCREAMING_SNAKE_CASE = XCLIPVisionConfig(patch_size=SCREAMING_SNAKE_CASE_ , num_frames=SCREAMING_SNAKE_CASE_ ) if "large" in model_name: _SCREAMING_SNAKE_CASE = 7_68 _SCREAMING_SNAKE_CASE = 30_72 _SCREAMING_SNAKE_CASE = 12 _SCREAMING_SNAKE_CASE = 10_24 _SCREAMING_SNAKE_CASE = 40_96 _SCREAMING_SNAKE_CASE = 16 _SCREAMING_SNAKE_CASE = 24 _SCREAMING_SNAKE_CASE = 7_68 _SCREAMING_SNAKE_CASE = 30_72 if model_name == "xclip-large-patch14-16-frames": _SCREAMING_SNAKE_CASE = 3_36 _SCREAMING_SNAKE_CASE = XCLIPConfig.from_text_vision_configs(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if "large" in model_name: _SCREAMING_SNAKE_CASE = 7_68 return config def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" # text encoder if name == "token_embedding.weight": _SCREAMING_SNAKE_CASE = name.replace("""token_embedding.weight""" , """text_model.embeddings.token_embedding.weight""" ) if name == "positional_embedding": _SCREAMING_SNAKE_CASE = name.replace("""positional_embedding""" , """text_model.embeddings.position_embedding.weight""" ) if "ln_1" in name: _SCREAMING_SNAKE_CASE = name.replace("""ln_1""" , """layer_norm1""" ) if "ln_2" in name: _SCREAMING_SNAKE_CASE = name.replace("""ln_2""" , """layer_norm2""" ) if "c_fc" in name: _SCREAMING_SNAKE_CASE = name.replace("""c_fc""" , """fc1""" ) if "c_proj" in name: _SCREAMING_SNAKE_CASE = name.replace("""c_proj""" , """fc2""" ) if name.startswith("""transformer.resblocks""" ): _SCREAMING_SNAKE_CASE = name.replace("""transformer.resblocks""" , """text_model.encoder.layers""" ) if "attn.out_proj" in name and "message" not in name: _SCREAMING_SNAKE_CASE = name.replace("""attn.out_proj""" , """self_attn.out_proj""" ) if "ln_final" in name: _SCREAMING_SNAKE_CASE = name.replace("""ln_final""" , """text_model.final_layer_norm""" ) # visual encoder if name == "visual.class_embedding": _SCREAMING_SNAKE_CASE = name.replace("""visual.class_embedding""" , """vision_model.embeddings.class_embedding""" ) if name == "visual.positional_embedding": _SCREAMING_SNAKE_CASE = name.replace("""visual.positional_embedding""" , """vision_model.embeddings.position_embedding.weight""" ) if name.startswith("""visual.transformer.resblocks""" ): _SCREAMING_SNAKE_CASE = name.replace("""visual.transformer.resblocks""" , """vision_model.encoder.layers""" ) if "visual.conv1" in name: _SCREAMING_SNAKE_CASE = name.replace("""visual.conv1""" , """vision_model.embeddings.patch_embedding""" ) if "visual.ln_pre" in name: _SCREAMING_SNAKE_CASE = name.replace("""visual.ln_pre""" , """vision_model.pre_layernorm""" ) if "visual.ln_post" in name: _SCREAMING_SNAKE_CASE = name.replace("""visual.ln_post""" , """vision_model.post_layernorm""" ) if "visual.proj" in name: _SCREAMING_SNAKE_CASE = name.replace("""visual.proj""" , """visual_projection.weight""" ) if "text_projection" in name: _SCREAMING_SNAKE_CASE = name.replace("""text_projection""" , """text_projection.weight""" ) # things on top if "prompts_visual_proj" in name: _SCREAMING_SNAKE_CASE = name.replace("""prompts_visual_proj""" , """prompts_visual_projection""" ) if "prompts_visual_ln" in name: _SCREAMING_SNAKE_CASE = name.replace("""prompts_visual_ln""" , """prompts_visual_layernorm""" ) # mit if name == "mit.positional_embedding": _SCREAMING_SNAKE_CASE = name.replace("""positional""" , """position""" ) if name.startswith("""mit.resblocks""" ): _SCREAMING_SNAKE_CASE = name.replace("""mit.resblocks""" , """mit.encoder.layers""" ) # prompts generator if name.startswith("""prompts_generator.norm""" ): _SCREAMING_SNAKE_CASE = name.replace("""prompts_generator.norm""" , """prompts_generator.layernorm""" ) return name def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" for key in orig_state_dict.copy().keys(): _SCREAMING_SNAKE_CASE = orig_state_dict.pop(SCREAMING_SNAKE_CASE_ ) if "attn.in_proj" in key: _SCREAMING_SNAKE_CASE = key.split(""".""" ) if key.startswith("""visual""" ): _SCREAMING_SNAKE_CASE = key_split[3] _SCREAMING_SNAKE_CASE = config.vision_config.hidden_size if "message_attn" in key: if "weight" in key: _SCREAMING_SNAKE_CASE = val[ :dim, : ] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2, : ] _SCREAMING_SNAKE_CASE = val[ -dim:, : ] else: _SCREAMING_SNAKE_CASE = val[ :dim ] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2 ] _SCREAMING_SNAKE_CASE = val[ -dim: ] else: if "weight" in key: _SCREAMING_SNAKE_CASE = val[ :dim, : ] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2, : ] _SCREAMING_SNAKE_CASE = val[ -dim:, : ] else: _SCREAMING_SNAKE_CASE = val[:dim] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2 ] _SCREAMING_SNAKE_CASE = val[-dim:] elif key.startswith("""mit""" ): _SCREAMING_SNAKE_CASE = key_split[2] _SCREAMING_SNAKE_CASE = config.vision_config.mit_hidden_size if "weight" in key: _SCREAMING_SNAKE_CASE = val[:dim, :] _SCREAMING_SNAKE_CASE = val[dim : dim * 2, :] _SCREAMING_SNAKE_CASE = val[-dim:, :] else: _SCREAMING_SNAKE_CASE = val[:dim] _SCREAMING_SNAKE_CASE = val[dim : dim * 2] _SCREAMING_SNAKE_CASE = val[-dim:] else: _SCREAMING_SNAKE_CASE = key_split[2] _SCREAMING_SNAKE_CASE = config.text_config.hidden_size if "weight" in key: _SCREAMING_SNAKE_CASE = val[:dim, :] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2, : ] _SCREAMING_SNAKE_CASE = val[-dim:, :] else: _SCREAMING_SNAKE_CASE = val[:dim] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2 ] _SCREAMING_SNAKE_CASE = val[-dim:] else: _SCREAMING_SNAKE_CASE = rename_key(SCREAMING_SNAKE_CASE_ ) if new_key_name in ["visual_projection.weight", "text_projection.weight"]: _SCREAMING_SNAKE_CASE = val.T _SCREAMING_SNAKE_CASE = val return orig_state_dict def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" if num_frames == 8: _SCREAMING_SNAKE_CASE = """eating_spaghetti_8_frames.npy""" elif num_frames == 16: _SCREAMING_SNAKE_CASE = """eating_spaghetti.npy""" elif num_frames == 32: _SCREAMING_SNAKE_CASE = """eating_spaghetti_32_frames.npy""" _SCREAMING_SNAKE_CASE = hf_hub_download( repo_id="""hf-internal-testing/spaghetti-video""" , filename=SCREAMING_SNAKE_CASE_ , repo_type="""dataset""" , ) _SCREAMING_SNAKE_CASE = np.load(SCREAMING_SNAKE_CASE_ ) return list(SCREAMING_SNAKE_CASE_ ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_=False ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = { # fully supervised kinetics-400 checkpoints """xclip-base-patch32""": """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_8.pth""", """xclip-base-patch32-16-frames""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_16.pth""" ), """xclip-base-patch16""": """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_8.pth""", """xclip-base-patch16-16-frames""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_16.pth""" ), """xclip-large-patch14""": """https://drive.google.com/u/0/uc?id=1NUOImq0o5DlQTST17iIP3vG7DgmHQuCx&amp;export=download&amp;confirm=t&amp;uuid=b26caedc-88e2-473e-830a-9d158b653cdb""", """xclip-large-patch14-16-frames""": """https://drive.google.com/u/0/uc?id=1FOYgnJc097OJ4lGwtRCCydQyVPJEOH7d&amp;export=download&amp;confirm=t&amp;uuid=538fa810-e671-4050-b385-9a623f89804f""", # fully supervised kinetics-600 checkpoints """xclip-base-patch16-kinetics-600""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_8.pth""" ), """xclip-base-patch16-kinetics-600-16-frames""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_16.pth""" ), """xclip-large-patch14-kinetics-600""": """https://drive.google.com/u/0/uc?id=1FV8C1INuM91sLAN4ImjzePLIlpMSihwV&amp;export=download&amp;confirm=t&amp;uuid=141d4977-4a65-44ae-864f-4b0c19f838be""", # few shot """xclip-base-patch16-hmdb-2-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_2.pth""" ), """xclip-base-patch16-hmdb-4-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_4.pth""" ), """xclip-base-patch16-hmdb-8-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_8.pth""" ), """xclip-base-patch16-hmdb-16-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_16.pth""" ), """xclip-base-patch16-ucf-2-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_2.pth""" ), """xclip-base-patch16-ucf-4-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_4.pth""" ), """xclip-base-patch16-ucf-8-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_8.pth""" ), """xclip-base-patch16-ucf-16-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_16.pth""" ), # zero shot """xclip-base-patch16-zero-shot""": """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/zero.pth""", } _SCREAMING_SNAKE_CASE = model_to_url[model_name] _SCREAMING_SNAKE_CASE = 8 if "16-frames" in model_name: _SCREAMING_SNAKE_CASE = 16 elif "shot" in model_name: _SCREAMING_SNAKE_CASE = 32 _SCREAMING_SNAKE_CASE = get_xclip_config(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = XCLIPModel(SCREAMING_SNAKE_CASE_ ) model.eval() if "drive" in checkpoint_url: _SCREAMING_SNAKE_CASE = """pytorch_model.bin""" gdown.cached_download(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , quiet=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = torch.load(SCREAMING_SNAKE_CASE_ , map_location="""cpu""" )["""model"""] else: _SCREAMING_SNAKE_CASE = torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE_ )["""model"""] _SCREAMING_SNAKE_CASE = convert_state_dict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = XCLIPModel(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = model.load_state_dict(SCREAMING_SNAKE_CASE_ , strict=SCREAMING_SNAKE_CASE_ ) assert missing_keys == ["text_model.embeddings.position_ids", "vision_model.embeddings.position_ids"] model.eval() _SCREAMING_SNAKE_CASE = 3_36 if model_name == """xclip-large-patch14-16-frames""" else 2_24 _SCREAMING_SNAKE_CASE = VideoMAEImageProcessor(size=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = CLIPTokenizer.from_pretrained("""openai/clip-vit-base-patch32""" ) _SCREAMING_SNAKE_CASE = CLIPTokenizerFast.from_pretrained("""openai/clip-vit-base-patch32""" ) _SCREAMING_SNAKE_CASE = XCLIPProcessor(image_processor=SCREAMING_SNAKE_CASE_ , tokenizer=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = prepare_video(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = processor( text=["""playing sports""", """eating spaghetti""", """go shopping"""] , videos=SCREAMING_SNAKE_CASE_ , return_tensors="""pt""" , padding=SCREAMING_SNAKE_CASE_ ) print("""Shape of pixel values:""" , inputs.pixel_values.shape ) with torch.no_grad(): _SCREAMING_SNAKE_CASE = model(**SCREAMING_SNAKE_CASE_ ) # Verify outputs _SCREAMING_SNAKE_CASE = outputs.logits_per_video _SCREAMING_SNAKE_CASE = logits_per_video.softmax(dim=1 ) print("""Probs:""" , SCREAMING_SNAKE_CASE_ ) # kinetics-400 if model_name == "xclip-base-patch32": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0019, 0.9951, 0.0030]] ) elif model_name == "xclip-base-patch32-16-frames": _SCREAMING_SNAKE_CASE = torch.tensor([[7.0999e-04, 9.9883e-01, 4.5580e-04]] ) elif model_name == "xclip-base-patch16": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0083, 0.9681, 0.0236]] ) elif model_name == "xclip-base-patch16-16-frames": _SCREAMING_SNAKE_CASE = torch.tensor([[7.6937e-04, 9.9728e-01, 1.9473e-03]] ) elif model_name == "xclip-large-patch14": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0062, 0.9864, 0.0075]] ) elif model_name == "xclip-large-patch14-16-frames": _SCREAMING_SNAKE_CASE = torch.tensor([[3.3877e-04, 9.9937e-01, 2.8888e-04]] ) # kinetics-600 elif model_name == "xclip-base-patch16-kinetics-600": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0555, 0.8914, 0.0531]] ) elif model_name == "xclip-base-patch16-kinetics-600-16-frames": _SCREAMING_SNAKE_CASE = torch.tensor([[3.8554e-04, 9.9929e-01, 3.2754e-04]] ) elif model_name == "xclip-large-patch14-kinetics-600": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0036, 0.9920, 0.0045]] ) # few shot elif model_name == "xclip-base-patch16-hmdb-2-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[7.1890e-06, 9.9994e-01, 5.6559e-05]] ) elif model_name == "xclip-base-patch16-hmdb-4-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[1.0320e-05, 9.9993e-01, 6.2435e-05]] ) elif model_name == "xclip-base-patch16-hmdb-8-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[4.1377e-06, 9.9990e-01, 9.8386e-05]] ) elif model_name == "xclip-base-patch16-hmdb-16-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[4.1347e-05, 9.9962e-01, 3.3411e-04]] ) elif model_name == "xclip-base-patch16-ucf-2-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[8.5857e-05, 9.9928e-01, 6.3291e-04]] ) elif model_name == "xclip-base-patch16-ucf-4-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[8.5857e-05, 9.9928e-01, 6.3291e-04]] ) elif model_name == "xclip-base-patch16-ucf-8-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0027, 0.9904, 0.0070]] ) elif model_name == "xclip-base-patch16-ucf-16-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[9.8219e-04, 9.9593e-01, 3.0863e-03]] ) # zero shot elif model_name == "xclip-base-patch16-zero-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[3.5082e-04, 9.9785e-01, 1.7966e-03]] ) else: raise ValueError(F"Model name {model_name} not supported" ) assert torch.allclose(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , atol=1e-3 ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: print(F"Saving model {model_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(SCREAMING_SNAKE_CASE_ ) if push_to_hub: print("""Pushing model, processor and slow tokenizer files to the hub...""" ) model.push_to_hub(SCREAMING_SNAKE_CASE_ , organization="""nielsr""" ) processor.push_to_hub(SCREAMING_SNAKE_CASE_ , organization="""nielsr""" ) slow_tokenizer.push_to_hub(SCREAMING_SNAKE_CASE_ , organization="""nielsr""" ) if __name__ == "__main__": UpperCamelCase__ : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default="xclip-base-patch32", type=str, help="Name of the model.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether or not to push the converted model to the 🤗 hub." ) UpperCamelCase__ : str = parser.parse_args() convert_xclip_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
0
0
'''simple docstring''' def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Tuple: """simple docstring""" if any(not isinstance(_lowerCAmelCase , _lowerCAmelCase ) or x < 0 for x in sequence ): raise TypeError("""Sequence must be list of non-negative integers""" ) for _ in range(len(_lowerCAmelCase ) ): for i, (rod_upper, rod_lower) in enumerate(zip(_lowerCAmelCase , sequence[1:] ) ): if rod_upper > rod_lower: sequence[i] -= rod_upper - rod_lower sequence[i + 1] += rod_upper - rod_lower return sequence if __name__ == "__main__": assert bead_sort([5, 4, 3, 2, 1]) == [1, 2, 3, 4, 5] assert bead_sort([7, 9, 4, 3, 5]) == [3, 4, 5, 7, 9]
710
'''simple docstring''' import numpy as np import torch from torch.utils.data import Dataset from utils import logger class _a (_lowerCamelCase): """simple docstring""" def __init__( self , A__ , A__ ) -> Any: _SCREAMING_SNAKE_CASE = params _SCREAMING_SNAKE_CASE = np.array(A__ ) _SCREAMING_SNAKE_CASE = np.array([len(A__ ) for t in data] ) self.check() self.remove_long_sequences() self.remove_empty_sequences() self.remove_unknown_sequences() self.check() self.print_statistics() def __getitem__( self , A__ ) -> Dict: return (self.token_ids[index], self.lengths[index]) def __len__( self ) -> Tuple: return len(self.lengths ) def UpperCamelCase ( self ) -> Dict: assert len(self.token_ids ) == len(self.lengths ) assert all(self.lengths[i] == len(self.token_ids[i] ) for i in range(len(self.lengths ) ) ) def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = self.params.max_model_input_size _SCREAMING_SNAKE_CASE = self.lengths > max_len logger.info(F"Splitting {sum(A__ )} too long sequences." ) def divide_chunks(A__ , A__ ): return [l[i : i + n] for i in range(0 , len(A__ ) , A__ )] _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = [] if self.params.mlm: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self.params.special_tok_ids["""cls_token"""], self.params.special_tok_ids["""sep_token"""] else: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 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: _SCREAMING_SNAKE_CASE = [] for sub_s in divide_chunks(seq_ , max_len - 2 ): if sub_s[0] != cls_id: _SCREAMING_SNAKE_CASE = np.insert(A__ , 0 , A__ ) if sub_s[-1] != sep_id: _SCREAMING_SNAKE_CASE = np.insert(A__ , len(A__ ) , A__ ) assert len(A__ ) <= max_len assert (sub_s[0] == cls_id) and (sub_s[-1] == sep_id), sub_s sub_seqs.append(A__ ) new_tok_ids.extend(A__ ) new_lengths.extend([len(A__ ) for l in sub_seqs] ) _SCREAMING_SNAKE_CASE = np.array(A__ ) _SCREAMING_SNAKE_CASE = np.array(A__ ) def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = len(self ) _SCREAMING_SNAKE_CASE = self.lengths > 11 _SCREAMING_SNAKE_CASE = self.token_ids[indices] _SCREAMING_SNAKE_CASE = self.lengths[indices] _SCREAMING_SNAKE_CASE = len(self ) logger.info(F"Remove {init_size - new_size} too short (<=11 tokens) sequences." ) def UpperCamelCase ( self ) -> int: if "unk_token" not in self.params.special_tok_ids: return else: _SCREAMING_SNAKE_CASE = self.params.special_tok_ids["""unk_token"""] _SCREAMING_SNAKE_CASE = len(self ) _SCREAMING_SNAKE_CASE = np.array([np.count_nonzero(a == unk_token_id ) for a in self.token_ids] ) _SCREAMING_SNAKE_CASE = (unk_occs / self.lengths) < 0.5 _SCREAMING_SNAKE_CASE = self.token_ids[indices] _SCREAMING_SNAKE_CASE = self.lengths[indices] _SCREAMING_SNAKE_CASE = len(self ) logger.info(F"Remove {init_size - new_size} sequences with a high level of unknown tokens (50%)." ) def UpperCamelCase ( self ) -> Optional[Any]: if not self.params.is_master: return logger.info(F"{len(self )} sequences" ) # data_len = sum(self.lengths) # nb_unique_tokens = len(Counter(list(chain(*self.token_ids)))) # logger.info(f'{data_len} tokens ({nb_unique_tokens} unique)') # unk_idx = self.params.special_tok_ids['unk_token'] # nb_unknown = sum([(t==unk_idx).sum() for t in self.token_ids]) # logger.info(f'{nb_unknown} unknown tokens (covering {100*nb_unknown/data_len:.2f}% of the data)') def UpperCamelCase ( self , A__ ) -> Any: _SCREAMING_SNAKE_CASE = [t[0] for t in batch] _SCREAMING_SNAKE_CASE = [t[1] for t in batch] assert len(A__ ) == len(A__ ) # Max for paddings _SCREAMING_SNAKE_CASE = max(A__ ) # Pad token ids if self.params.mlm: _SCREAMING_SNAKE_CASE = self.params.special_tok_ids["""pad_token"""] else: _SCREAMING_SNAKE_CASE = self.params.special_tok_ids["""unk_token"""] _SCREAMING_SNAKE_CASE = [list(t.astype(A__ ) ) + [pad_idx] * (max_seq_len_ - len(A__ )) for t in token_ids] assert len(tk_ ) == len(A__ ) assert all(len(A__ ) == max_seq_len_ for t in tk_ ) _SCREAMING_SNAKE_CASE = torch.tensor(tk_ ) # (bs, max_seq_len_) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) # (bs) return tk_t, lg_t
0
0
'''simple docstring''' import unittest from transformers import SPIECE_UNDERLINE, ReformerTokenizer, ReformerTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin UpperCamelCase__ : List[str] = get_tests_dir("fixtures/test_sentencepiece.model") @require_sentencepiece @require_tokenizers class _a (UpperCamelCase_ , unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = ReformerTokenizer SCREAMING_SNAKE_CASE = ReformerTokenizerFast SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = True def UpperCamelCase ( self ) -> List[str]: super().setUp() _SCREAMING_SNAKE_CASE = ReformerTokenizer(_a , keep_accents=_a ) tokenizer.save_pretrained(self.tmpdirname ) def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = """<s>""" _SCREAMING_SNAKE_CASE = 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 ) -> List[str]: _SCREAMING_SNAKE_CASE = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , """<unk>""" ) self.assertEqual(vocab_keys[1] , """<s>""" ) self.assertEqual(vocab_keys[-1] , """j""" ) self.assertEqual(len(_a ) , 10_00 ) def UpperCamelCase ( self ) -> Any: self.assertEqual(self.get_tokenizer().vocab_size , 10_00 ) def UpperCamelCase ( self ) -> Optional[int]: if not self.test_rust_tokenizer: return _SCREAMING_SNAKE_CASE = self.get_tokenizer() _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() _SCREAMING_SNAKE_CASE = """I was born in 92000, and this is falsé.""" _SCREAMING_SNAKE_CASE = tokenizer.tokenize(_a ) _SCREAMING_SNAKE_CASE = rust_tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) _SCREAMING_SNAKE_CASE = tokenizer.encode(_a , add_special_tokens=_a ) _SCREAMING_SNAKE_CASE = rust_tokenizer.encode(_a , add_special_tokens=_a ) self.assertListEqual(_a , _a ) _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() _SCREAMING_SNAKE_CASE = tokenizer.encode(_a ) _SCREAMING_SNAKE_CASE = rust_tokenizer.encode(_a ) self.assertListEqual(_a , _a ) def UpperCamelCase ( self , A__=15 ) -> Union[str, Any]: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})" ): _SCREAMING_SNAKE_CASE = self.rust_tokenizer_class.from_pretrained(_a , **_a ) # Simple input _SCREAMING_SNAKE_CASE = """This is a simple input""" _SCREAMING_SNAKE_CASE = ["""This is a simple input 1""", """This is a simple input 2"""] _SCREAMING_SNAKE_CASE = ("""This is a simple input""", """This is a pair""") _SCREAMING_SNAKE_CASE = [ ("""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(_a , tokenizer_r.encode , _a , max_length=_a , padding="""max_length""" ) # Simple input self.assertRaises(_a , tokenizer_r.encode_plus , _a , max_length=_a , padding="""max_length""" ) # Simple input self.assertRaises( _a , tokenizer_r.batch_encode_plus , _a , max_length=_a , padding="""max_length""" , ) # Pair input self.assertRaises(_a , tokenizer_r.encode , _a , max_length=_a , padding="""max_length""" ) # Pair input self.assertRaises(_a , tokenizer_r.encode_plus , _a , max_length=_a , padding="""max_length""" ) # Pair input self.assertRaises( _a , tokenizer_r.batch_encode_plus , _a , max_length=_a , padding="""max_length""" , ) def UpperCamelCase ( self ) -> Optional[Any]: pass def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = ReformerTokenizer(_a , keep_accents=_a ) _SCREAMING_SNAKE_CASE = tokenizer.tokenize("""This is a test""" ) self.assertListEqual(_a , ["""▁This""", """▁is""", """▁a""", """▁t""", """est"""] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_a ) , [2_85, 46, 10, 1_70, 3_82] , ) _SCREAMING_SNAKE_CASE = tokenizer.tokenize("""I was born in 92000, and this is falsé.""" ) self.assertListEqual( _a , [ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """9""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """é""", """.""", ] , ) _SCREAMING_SNAKE_CASE = tokenizer.convert_tokens_to_ids(_a ) self.assertListEqual( _a , [8, 21, 84, 55, 24, 19, 7, 0, 6_02, 3_47, 3_47, 3_47, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) _SCREAMING_SNAKE_CASE = 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 ) -> Optional[Any]: return ReformerTokenizer.from_pretrained("""google/reformer-crime-and-punishment""" ) @slow def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = """Hello World!""" _SCREAMING_SNAKE_CASE = [1_26, 32, 2_62, 1_52, 38, 72, 2_87] self.assertListEqual(_a , self.big_tokenizer.encode(_a ) ) @slow def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = ( """This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will""" """ add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth""" ) _SCREAMING_SNAKE_CASE = [ 1_08, 2_65, 24, 1_11, 4, 2_58, 1_56, 35, 28, 2_75, 3, 2_59, 2_97, 2_60, 84, 4, 35, 1_10, 44, 8, 2_59, 91, 2_68, 21, 11, 2_09, 2_74, 1_09, 2_66, 2_77, 1_17, 86, 93, 3_15, 2_58, 2_78, 2_58, 2_77, 2_58, 0, 2_58, 2_88, 2_58, 3_19, 2_58, 0, 2_58, 0, 2_58, 0, 2_58, 0, 2_58, 2_87, 2_58, 3_15, 2_58, 2_89, 2_58, 2_78, 99, 2_69, 2_66, 2_62, 8, 2_59, 2_41, 4, 2_17, 2_30, 2_68, 2_66, 55, 1_68, 1_06, 75, 1_93, 2_66, 2_23, 27, 49, 26, 2_82, 25, 2_64, 2_99, 19, 26, 0, 2_58, 2_77, 1_17, 86, 93, 1_76, 1_83, 2_70, 11, 2_62, 42, 61, 2_65, ] self.assertListEqual(_a , self.big_tokenizer.encode(_a ) ) @require_torch @slow def UpperCamelCase ( self ) -> Tuple: import torch from transformers import ReformerConfig, ReformerModel # Build sequence _SCREAMING_SNAKE_CASE = list(self.big_tokenizer.get_vocab().keys() )[:10] _SCREAMING_SNAKE_CASE = """ """.join(_a ) _SCREAMING_SNAKE_CASE = self.big_tokenizer.encode_plus(_a , return_tensors="""pt""" ) _SCREAMING_SNAKE_CASE = self.big_tokenizer.batch_encode_plus([sequence, sequence] , return_tensors="""pt""" ) _SCREAMING_SNAKE_CASE = ReformerConfig() # The input gets padded during training so adjust the axial position encodings from the pretrained model value of (512, 1024) _SCREAMING_SNAKE_CASE = encoded_sequence["""input_ids"""].shape _SCREAMING_SNAKE_CASE = ReformerModel(_a ) # Reformer has config.vocab_size == tokenizer.vocab_size == len(tokenizer) - 1 = 320; len(tokenizer) is 321 (including a pad token with id 320) 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 ) -> List[str]: _SCREAMING_SNAKE_CASE = {"""input_ids""": [[1_08, 2_65, 24, 1_11, 4, 2_58, 1_56, 7, 51, 2_79, 58, 7, 76, 25, 69, 2_78], [1_40, 2_43, 2_64, 1_34, 17, 2_67, 77, 2_63, 22, 2_62, 2_97, 2_58, 3_04, 1_77, 2_79, 2_66, 14, 89, 13, 35, 2_61, 2_99, 2_72, 1_37, 2_75, 2_78]], """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]]} # noqa: E501 # fmt: on # This tokenizer does not know some characters like ")". # That is the reason why we use very simple texts here. # Also see https://github.com/huggingface/transformers/pull/11737#issuecomment-850769064 _SCREAMING_SNAKE_CASE = [ """This is a very simple sentence.""", """The quick brown fox jumps over the lazy dog.""", ] self.tokenizer_integration_test_util( expected_encoding=_a , model_name="""google/reformer-crime-and-punishment""" , revision="""0e6c3decb8211d49bf881013425dc8b0448b3f5a""" , padding=_a , sequences=_a , )
711
'''simple docstring''' 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 UpperCamelCase__ : List[Any] = logging.get_logger(__name__) UpperCamelCase__ : Any = "▁" UpperCamelCase__ : Any = {"vocab_file": "spiece.model"} UpperCamelCase__ : int = { "vocab_file": { "google/reformer-crime-and-punishment": ( "https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/spiece.model" ) } } UpperCamelCase__ : Optional[int] = { "google/reformer-crime-and-punishment": 524_288, } class _a (_lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE = ['input_ids', 'attention_mask'] def __init__( self , A__ , A__="</s>" , A__="<unk>" , A__=[] , A__ = None , **A__ , ) -> None: _SCREAMING_SNAKE_CASE = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( eos_token=A__ , unk_token=A__ , additional_special_tokens=A__ , sp_model_kwargs=self.sp_model_kwargs , **A__ , ) _SCREAMING_SNAKE_CASE = vocab_file _SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(A__ ) @property def UpperCamelCase ( self ) -> Any: return self.sp_model.get_piece_size() def UpperCamelCase ( self ) -> Dict[str, int]: _SCREAMING_SNAKE_CASE = {self.convert_ids_to_tokens(A__ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ) -> int: _SCREAMING_SNAKE_CASE = self.__dict__.copy() _SCREAMING_SNAKE_CASE = None return state def __setstate__( self , A__ ) -> str: _SCREAMING_SNAKE_CASE = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): _SCREAMING_SNAKE_CASE = {} _SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCamelCase ( self , A__ ) -> List[str]: return self.sp_model.encode(A__ , out_type=A__ ) def UpperCamelCase ( self , A__ ) -> Union[str, Any]: return self.sp_model.piece_to_id(A__ ) def UpperCamelCase ( self , A__ ) -> List[Any]: if index < self.sp_model.get_piece_size(): _SCREAMING_SNAKE_CASE = self.sp_model.IdToPiece(A__ ) return token def UpperCamelCase ( self , A__ ) -> str: _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = """""" 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(A__ ) + token _SCREAMING_SNAKE_CASE = [] else: current_sub_tokens.append(A__ ) out_string += self.sp_model.decode(A__ ) return out_string.strip() def UpperCamelCase ( self , A__ , A__ = None ) -> Tuple[str]: if not os.path.isdir(A__ ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return _SCREAMING_SNAKE_CASE = os.path.join( A__ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(A__ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , A__ ) elif not os.path.isfile(self.vocab_file ): with open(A__ , """wb""" ) as fi: _SCREAMING_SNAKE_CASE = self.sp_model.serialized_model_proto() fi.write(A__ ) return (out_vocab_file,)
0
0
'''simple docstring''' from __future__ import annotations from collections.abc import Callable UpperCamelCase__ : List[Any] = list[list[float | int]] def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Matrix: """simple docstring""" _SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) _SCREAMING_SNAKE_CASE = [[0 for _ in range(size + 1 )] for _ in range(__UpperCAmelCase )] _SCREAMING_SNAKE_CASE = 42 _SCREAMING_SNAKE_CASE = 42 _SCREAMING_SNAKE_CASE = 42 _SCREAMING_SNAKE_CASE = 42 _SCREAMING_SNAKE_CASE = 42 _SCREAMING_SNAKE_CASE = 42 for row in range(__UpperCAmelCase ): for col in range(__UpperCAmelCase ): _SCREAMING_SNAKE_CASE = matrix[row][col] _SCREAMING_SNAKE_CASE = vector[row][0] _SCREAMING_SNAKE_CASE = 0 _SCREAMING_SNAKE_CASE = 0 while row < size and col < size: # pivoting _SCREAMING_SNAKE_CASE = max((abs(augmented[rowa][col] ), rowa) for rowa in range(__UpperCAmelCase , __UpperCAmelCase ) )[ 1 ] if augmented[pivot_row][col] == 0: col += 1 continue else: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = augmented[pivot_row], augmented[row] for rowa in range(row + 1 , __UpperCAmelCase ): _SCREAMING_SNAKE_CASE = augmented[rowa][col] / augmented[row][col] _SCREAMING_SNAKE_CASE = 0 for cola in range(col + 1 , size + 1 ): augmented[rowa][cola] -= augmented[row][cola] * ratio row += 1 col += 1 # back substitution for col in range(1 , __UpperCAmelCase ): for row in range(__UpperCAmelCase ): _SCREAMING_SNAKE_CASE = augmented[row][col] / augmented[col][col] for cola in range(__UpperCAmelCase , size + 1 ): augmented[row][cola] -= augmented[col][cola] * ratio # round to get rid of numbers like 2.000000000000004 return [ [round(augmented[row][size] / augmented[row][row] , 10 )] for row in range(__UpperCAmelCase ) ] def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Callable[[int], int]: """simple docstring""" _SCREAMING_SNAKE_CASE = len(__UpperCAmelCase ) _SCREAMING_SNAKE_CASE = [[0 for _ in range(__UpperCAmelCase )] for _ in range(__UpperCAmelCase )] _SCREAMING_SNAKE_CASE = [[0] for _ in range(__UpperCAmelCase )] _SCREAMING_SNAKE_CASE = 42 _SCREAMING_SNAKE_CASE = 42 _SCREAMING_SNAKE_CASE = 42 _SCREAMING_SNAKE_CASE = 42 for x_val, y_val in enumerate(__UpperCAmelCase ): for col in range(__UpperCAmelCase ): _SCREAMING_SNAKE_CASE = (x_val + 1) ** (size - col - 1) _SCREAMING_SNAKE_CASE = y_val _SCREAMING_SNAKE_CASE = solve(__UpperCAmelCase , __UpperCAmelCase ) def interpolated_func(SCREAMING_SNAKE_CASE_ ) -> int: return sum( round(coeffs[x_val][0] ) * (var ** (size - x_val - 1)) for x_val in range(__UpperCAmelCase ) ) return interpolated_func def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" return ( 1 - variable + variable**2 - variable**3 + variable**4 - variable**5 + variable**6 - variable**7 + variable**8 - variable**9 + variable**10 ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ = question_function , SCREAMING_SNAKE_CASE_ = 10 ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE = [func(__UpperCAmelCase ) for x_val in range(1 , order + 1 )] _SCREAMING_SNAKE_CASE = [ interpolate(data_points[:max_coeff] ) for max_coeff in range(1 , order + 1 ) ] _SCREAMING_SNAKE_CASE = 0 _SCREAMING_SNAKE_CASE = 42 _SCREAMING_SNAKE_CASE = 42 for poly in polynomials: _SCREAMING_SNAKE_CASE = 1 while func(__UpperCAmelCase ) == poly(__UpperCAmelCase ): x_val += 1 ret += poly(__UpperCAmelCase ) return ret if __name__ == "__main__": print(f"""{solution() = }""")
712
'''simple docstring''' import os import unittest from transformers import MobileBertTokenizer, MobileBertTokenizerFast from transformers.models.bert.tokenization_bert import ( VOCAB_FILES_NAMES, BasicTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin, filter_non_english @require_tokenizers class _a (_lowerCamelCase , unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = MobileBertTokenizer SCREAMING_SNAKE_CASE = MobileBertTokenizerFast SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = filter_non_english SCREAMING_SNAKE_CASE = 'google/mobilebert-uncased' def UpperCamelCase ( self ) -> Any: super().setUp() _SCREAMING_SNAKE_CASE = [ """[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest""", ] _SCREAMING_SNAKE_CASE = 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] ) ) _SCREAMING_SNAKE_CASE = [ (tokenizer_def[0], self.pre_trained_model_path, tokenizer_def[2]) # else the 'google/' prefix is stripped for tokenizer_def in self.tokenizers_list ] def UpperCamelCase ( self , A__ ) -> List[str]: _SCREAMING_SNAKE_CASE = """UNwant\u00E9d,running""" _SCREAMING_SNAKE_CASE = """unwanted, running""" return input_text, output_text def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = self.tokenizer_class(self.vocab_file ) _SCREAMING_SNAKE_CASE = tokenizer.tokenize("""UNwant\u00E9d,running""" ) self.assertListEqual(A__ , ["""un""", """##want""", """##ed""", """,""", """runn""", """##ing"""] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(A__ ) , [9, 6, 7, 12, 10, 11] ) def UpperCamelCase ( self ) -> Optional[int]: if not self.test_rust_tokenizer: return _SCREAMING_SNAKE_CASE = self.get_tokenizer() _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() _SCREAMING_SNAKE_CASE = """UNwant\u00E9d,running""" _SCREAMING_SNAKE_CASE = tokenizer.tokenize(A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.tokenize(A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = tokenizer.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.encode(A__ , add_special_tokens=A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() _SCREAMING_SNAKE_CASE = tokenizer.encode(A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.encode(A__ ) self.assertListEqual(A__ , A__ ) # With lower casing _SCREAMING_SNAKE_CASE = self.get_tokenizer(do_lower_case=A__ ) _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer(do_lower_case=A__ ) _SCREAMING_SNAKE_CASE = """UNwant\u00E9d,running""" _SCREAMING_SNAKE_CASE = tokenizer.tokenize(A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.tokenize(A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = tokenizer.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.encode(A__ , add_special_tokens=A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() _SCREAMING_SNAKE_CASE = tokenizer.encode(A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.encode(A__ ) self.assertListEqual(A__ , A__ ) def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = BasicTokenizer() self.assertListEqual(tokenizer.tokenize("""ah\u535A\u63A8zz""" ) , ["""ah""", """\u535A""", """\u63A8""", """zz"""] ) def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHeLLo!how \n Are yoU? """ ) , ["""hello""", """!""", """how""", """are""", """you""", """?"""] ) self.assertListEqual(tokenizer.tokenize("""H\u00E9llo""" ) , ["""hello"""] ) def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , strip_accents=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""hällo""", """!""", """how""", """are""", """you""", """?"""] ) self.assertListEqual(tokenizer.tokenize("""H\u00E9llo""" ) , ["""h\u00E9llo"""] ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , strip_accents=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""hallo""", """!""", """how""", """are""", """you""", """?"""] ) self.assertListEqual(tokenizer.tokenize("""H\u00E9llo""" ) , ["""hello"""] ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""hallo""", """!""", """how""", """are""", """you""", """?"""] ) self.assertListEqual(tokenizer.tokenize("""H\u00E9llo""" ) , ["""hello"""] ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHeLLo!how \n Are yoU? """ ) , ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?"""] ) def UpperCamelCase ( self ) -> Dict: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , strip_accents=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""HäLLo""", """!""", """how""", """Are""", """yoU""", """?"""] ) def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , strip_accents=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""HaLLo""", """!""", """how""", """Are""", """yoU""", """?"""] ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , never_split=["""[UNK]"""] ) self.assertListEqual( tokenizer.tokenize(""" \tHeLLo!how \n Are yoU? [UNK]""" ) , ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?""", """[UNK]"""] ) def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = ["""[UNK]""", """[CLS]""", """[SEP]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing"""] _SCREAMING_SNAKE_CASE = {} for i, token in enumerate(A__ ): _SCREAMING_SNAKE_CASE = i _SCREAMING_SNAKE_CASE = WordpieceTokenizer(vocab=A__ , unk_token="""[UNK]""" ) self.assertListEqual(tokenizer.tokenize("""""" ) , [] ) self.assertListEqual(tokenizer.tokenize("""unwanted running""" ) , ["""un""", """##want""", """##ed""", """runn""", """##ing"""] ) self.assertListEqual(tokenizer.tokenize("""unwantedX running""" ) , ["""[UNK]""", """runn""", """##ing"""] ) def UpperCamelCase ( self ) -> str: self.assertTrue(_is_whitespace(""" """ ) ) self.assertTrue(_is_whitespace("""\t""" ) ) self.assertTrue(_is_whitespace("""\r""" ) ) self.assertTrue(_is_whitespace("""\n""" ) ) self.assertTrue(_is_whitespace("""\u00A0""" ) ) self.assertFalse(_is_whitespace("""A""" ) ) self.assertFalse(_is_whitespace("""-""" ) ) def UpperCamelCase ( self ) -> Union[str, Any]: self.assertTrue(_is_control("""\u0005""" ) ) self.assertFalse(_is_control("""A""" ) ) self.assertFalse(_is_control(""" """ ) ) self.assertFalse(_is_control("""\t""" ) ) self.assertFalse(_is_control("""\r""" ) ) def UpperCamelCase ( self ) -> Dict: self.assertTrue(_is_punctuation("""-""" ) ) self.assertTrue(_is_punctuation("""$""" ) ) self.assertTrue(_is_punctuation("""`""" ) ) self.assertTrue(_is_punctuation(""".""" ) ) self.assertFalse(_is_punctuation("""A""" ) ) self.assertFalse(_is_punctuation(""" """ ) ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = self.get_tokenizer() _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() # Example taken from the issue https://github.com/huggingface/tokenizers/issues/340 self.assertListEqual([tokenizer.tokenize(A__ ) for t in ["""Test""", """\xad""", """test"""]] , [["""[UNK]"""], [], ["""[UNK]"""]] ) self.assertListEqual( [rust_tokenizer.tokenize(A__ ) for t in ["""Test""", """\xad""", """test"""]] , [["""[UNK]"""], [], ["""[UNK]"""]] ) @slow def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained("""google/mobilebert-uncased""" ) _SCREAMING_SNAKE_CASE = tokenizer.encode("""sequence builders""" , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer.encode("""multi-sequence build""" , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer.build_inputs_with_special_tokens(A__ ) _SCREAMING_SNAKE_CASE = tokenizer.build_inputs_with_special_tokens(A__ , A__ ) assert encoded_sentence == [1_01] + text + [1_02] assert encoded_pair == [1_01] + text + [1_02] + text_a + [1_02] def UpperCamelCase ( self ) -> List[str]: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})" ): _SCREAMING_SNAKE_CASE = self.rust_tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = F"A, naïve {tokenizer_r.mask_token} AllenNLP sentence." _SCREAMING_SNAKE_CASE = tokenizer_r.encode_plus( A__ , return_attention_mask=A__ , return_token_type_ids=A__ , return_offsets_mapping=A__ , add_special_tokens=A__ , ) _SCREAMING_SNAKE_CASE = tokenizer_r.do_lower_case if hasattr(A__ , """do_lower_case""" ) else False _SCREAMING_SNAKE_CASE = ( [ ((0, 0), tokenizer_r.cls_token), ((0, 1), """A"""), ((1, 2), ""","""), ((3, 5), """na"""), ((5, 6), """##ï"""), ((6, 8), """##ve"""), ((9, 15), tokenizer_r.mask_token), ((16, 21), """Allen"""), ((21, 23), """##NL"""), ((23, 24), """##P"""), ((25, 33), """sentence"""), ((33, 34), """."""), ((0, 0), tokenizer_r.sep_token), ] if not do_lower_case else [ ((0, 0), tokenizer_r.cls_token), ((0, 1), """a"""), ((1, 2), ""","""), ((3, 8), """naive"""), ((9, 15), tokenizer_r.mask_token), ((16, 21), """allen"""), ((21, 23), """##nl"""), ((23, 24), """##p"""), ((25, 33), """sentence"""), ((33, 34), """."""), ((0, 0), tokenizer_r.sep_token), ] ) self.assertEqual( [e[1] for e in expected_results] , tokenizer_r.convert_ids_to_tokens(tokens["""input_ids"""] ) ) self.assertEqual([e[0] for e in expected_results] , tokens["""offset_mapping"""] ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = ["""的""", """人""", """有"""] _SCREAMING_SNAKE_CASE = """""".join(A__ ) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})" ): _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = self.rust_tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = tokenizer_p.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer_r.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer_r.convert_ids_to_tokens(A__ ) _SCREAMING_SNAKE_CASE = tokenizer_p.convert_ids_to_tokens(A__ ) # it is expected that each Chinese character is not preceded by "##" self.assertListEqual(A__ , A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = self.rust_tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = tokenizer_r.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer_p.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer_r.convert_ids_to_tokens(A__ ) _SCREAMING_SNAKE_CASE = tokenizer_p.convert_ids_to_tokens(A__ ) # it is expected that only the first Chinese character is not preceded by "##". _SCREAMING_SNAKE_CASE = [ F"##{token}" if idx != 0 else token for idx, token in enumerate(A__ ) ] self.assertListEqual(A__ , A__ ) self.assertListEqual(A__ , A__ )
0
0
'''simple docstring''' from typing import Optional, Tuple, Union import torch from einops import rearrange, reduce from diffusers import DDIMScheduler, DDPMScheduler, DiffusionPipeline, ImagePipelineOutput, UNetaDConditionModel from diffusers.schedulers.scheduling_ddim import DDIMSchedulerOutput from diffusers.schedulers.scheduling_ddpm import DDPMSchedulerOutput UpperCamelCase__ : Dict = 8 def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=BITS ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = x.device _SCREAMING_SNAKE_CASE = (x * 2_55).int().clamp(0 , 2_55 ) _SCREAMING_SNAKE_CASE = 2 ** torch.arange(bits - 1 , -1 , -1 , device=lowerCAmelCase__ ) _SCREAMING_SNAKE_CASE = rearrange(lowerCAmelCase__ , """d -> d 1 1""" ) _SCREAMING_SNAKE_CASE = rearrange(lowerCAmelCase__ , """b c h w -> b c 1 h w""" ) _SCREAMING_SNAKE_CASE = ((x & mask) != 0).float() _SCREAMING_SNAKE_CASE = rearrange(lowerCAmelCase__ , """b c d h w -> b (c d) h w""" ) _SCREAMING_SNAKE_CASE = bits * 2 - 1 return bits def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=BITS ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE = x.device _SCREAMING_SNAKE_CASE = (x > 0).int() _SCREAMING_SNAKE_CASE = 2 ** torch.arange(bits - 1 , -1 , -1 , device=lowerCAmelCase__ , dtype=torch.intaa ) _SCREAMING_SNAKE_CASE = rearrange(lowerCAmelCase__ , """d -> d 1 1""" ) _SCREAMING_SNAKE_CASE = rearrange(lowerCAmelCase__ , """b (c d) h w -> b c d h w""" , d=8 ) _SCREAMING_SNAKE_CASE = reduce(x * mask , """b c d h w -> b c h w""" , """sum""" ) return (dec / 2_55).clamp(0.0 , 1.0 ) def lowerCAmelCase_ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = 0.0 , SCREAMING_SNAKE_CASE_ = True , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_ = True , ) -> Union[str, Any]: """simple docstring""" if self.num_inference_steps is None: raise ValueError( """Number of inference steps is 'None', you need to run 'set_timesteps' after creating the scheduler""" ) # See formulas (12) and (16) of DDIM paper https://arxiv.org/pdf/2010.02502.pdf # Ideally, read DDIM paper in-detail understanding # Notation (<variable name> -> <name in paper> # - pred_noise_t -> e_theta(x_t, t) # - pred_original_sample -> f_theta(x_t, t) or x_0 # - std_dev_t -> sigma_t # - eta -> η # - pred_sample_direction -> "direction pointing to x_t" # - pred_prev_sample -> "x_t-1" # 1. get previous step value (=t-1) _SCREAMING_SNAKE_CASE = timestep - self.config.num_train_timesteps // self.num_inference_steps # 2. compute alphas, betas _SCREAMING_SNAKE_CASE = self.alphas_cumprod[timestep] _SCREAMING_SNAKE_CASE = self.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.final_alpha_cumprod _SCREAMING_SNAKE_CASE = 1 - alpha_prod_t # 3. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf _SCREAMING_SNAKE_CASE = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 # 4. Clip "predicted x_0" _SCREAMING_SNAKE_CASE = self.bit_scale if self.config.clip_sample: _SCREAMING_SNAKE_CASE = torch.clamp(lowerCAmelCase__ , -scale , lowerCAmelCase__ ) # 5. compute variance: "sigma_t(η)" -> see formula (16) # σ_t = sqrt((1 − α_t−1)/(1 − α_t)) * sqrt(1 − α_t/α_t−1) _SCREAMING_SNAKE_CASE = self._get_variance(lowerCAmelCase__ , lowerCAmelCase__ ) _SCREAMING_SNAKE_CASE = eta * variance ** 0.5 if use_clipped_model_output: # the model_output is always re-derived from the clipped x_0 in Glide _SCREAMING_SNAKE_CASE = (sample - alpha_prod_t ** 0.5 * pred_original_sample) / beta_prod_t ** 0.5 # 6. compute "direction pointing to x_t" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf _SCREAMING_SNAKE_CASE = (1 - alpha_prod_t_prev - std_dev_t**2) ** 0.5 * model_output # 7. compute x_t without "random noise" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf _SCREAMING_SNAKE_CASE = alpha_prod_t_prev ** 0.5 * pred_original_sample + pred_sample_direction if eta > 0: # randn_like does not support generator https://github.com/pytorch/pytorch/issues/27072 _SCREAMING_SNAKE_CASE = model_output.device if torch.is_tensor(lowerCAmelCase__ ) else """cpu""" _SCREAMING_SNAKE_CASE = torch.randn(model_output.shape , dtype=model_output.dtype , generator=lowerCAmelCase__ ).to(lowerCAmelCase__ ) _SCREAMING_SNAKE_CASE = self._get_variance(lowerCAmelCase__ , lowerCAmelCase__ ) ** 0.5 * eta * noise _SCREAMING_SNAKE_CASE = prev_sample + variance if not return_dict: return (prev_sample,) return DDIMSchedulerOutput(prev_sample=lowerCAmelCase__ , pred_original_sample=lowerCAmelCase__ ) def lowerCAmelCase_ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_="epsilon" , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_ = True , ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = timestep if model_output.shape[1] == sample.shape[1] * 2 and self.variance_type in ["learned", "learned_range"]: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = torch.split(lowerCAmelCase__ , sample.shape[1] , dim=1 ) else: _SCREAMING_SNAKE_CASE = None # 1. compute alphas, betas _SCREAMING_SNAKE_CASE = self.alphas_cumprod[t] _SCREAMING_SNAKE_CASE = self.alphas_cumprod[t - 1] if t > 0 else self.one _SCREAMING_SNAKE_CASE = 1 - alpha_prod_t _SCREAMING_SNAKE_CASE = 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 prediction_type == "epsilon": _SCREAMING_SNAKE_CASE = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 elif prediction_type == "sample": _SCREAMING_SNAKE_CASE = model_output else: raise ValueError(F"Unsupported prediction_type {prediction_type}." ) # 3. Clip "predicted x_0" _SCREAMING_SNAKE_CASE = self.bit_scale if self.config.clip_sample: _SCREAMING_SNAKE_CASE = torch.clamp(lowerCAmelCase__ , -scale , lowerCAmelCase__ ) # 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 _SCREAMING_SNAKE_CASE = (alpha_prod_t_prev ** 0.5 * self.betas[t]) / beta_prod_t _SCREAMING_SNAKE_CASE = self.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 _SCREAMING_SNAKE_CASE = pred_original_sample_coeff * pred_original_sample + current_sample_coeff * sample # 6. Add noise _SCREAMING_SNAKE_CASE = 0 if t > 0: _SCREAMING_SNAKE_CASE = torch.randn( model_output.size() , dtype=model_output.dtype , layout=model_output.layout , generator=lowerCAmelCase__ ).to(model_output.device ) _SCREAMING_SNAKE_CASE = (self._get_variance(lowerCAmelCase__ , predicted_variance=lowerCAmelCase__ ) ** 0.5) * noise _SCREAMING_SNAKE_CASE = pred_prev_sample + variance if not return_dict: return (pred_prev_sample,) return DDPMSchedulerOutput(prev_sample=lowerCAmelCase__ , pred_original_sample=lowerCAmelCase__ ) class _a (_lowerCamelCase): """simple docstring""" def __init__( self , A__ , A__ , A__ = 1.0 , ) -> str: super().__init__() _SCREAMING_SNAKE_CASE = bit_scale _SCREAMING_SNAKE_CASE = ( ddim_bit_scheduler_step if isinstance(_UpperCAmelCase , _UpperCAmelCase ) else ddpm_bit_scheduler_step ) self.register_modules(unet=_UpperCAmelCase , scheduler=_UpperCAmelCase ) @torch.no_grad() def __call__( self , A__ = 2_56 , A__ = 2_56 , A__ = 50 , A__ = None , A__ = 1 , A__ = "pil" , A__ = True , **A__ , ) -> Union[Tuple, ImagePipelineOutput]: _SCREAMING_SNAKE_CASE = torch.randn( (batch_size, self.unet.config.in_channels, height, width) , generator=_UpperCAmelCase , ) _SCREAMING_SNAKE_CASE = decimal_to_bits(_UpperCAmelCase ) * self.bit_scale _SCREAMING_SNAKE_CASE = latents.to(self.device ) self.scheduler.set_timesteps(_UpperCAmelCase ) for t in self.progress_bar(self.scheduler.timesteps ): # predict the noise residual _SCREAMING_SNAKE_CASE = self.unet(_UpperCAmelCase , _UpperCAmelCase ).sample # compute the previous noisy sample x_t -> x_t-1 _SCREAMING_SNAKE_CASE = self.scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ).prev_sample _SCREAMING_SNAKE_CASE = bits_to_decimal(_UpperCAmelCase ) if output_type == "pil": _SCREAMING_SNAKE_CASE = self.numpy_to_pil(_UpperCAmelCase ) if not return_dict: return (image,) return ImagePipelineOutput(images=_UpperCAmelCase )
713
'''simple docstring''' import logging import os import quant_trainer import torch from torch.utils.data import DataLoader from transformers import Trainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput UpperCamelCase__ : Tuple = logging.getLogger(__name__) if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class _a (_lowerCamelCase): """simple docstring""" def __init__( self , *A__ , A__=None , A__=None , A__=None , **A__ ) -> Optional[int]: super().__init__(*A__ , **A__ ) _SCREAMING_SNAKE_CASE = eval_examples _SCREAMING_SNAKE_CASE = post_process_function _SCREAMING_SNAKE_CASE = quant_trainer_args _SCREAMING_SNAKE_CASE = 1_28 # default number of calibration samples def UpperCamelCase ( self , A__=None ) -> Union[str, Any]: if calib_dataset is None and self.calib_dataset is None: raise ValueError("""Trainer: calibration requires an calib_dataset.""" ) _SCREAMING_SNAKE_CASE = calib_dataset if calib_dataset is not None else self.calib_dataset _SCREAMING_SNAKE_CASE = self._remove_unused_columns(A__ , description="""Calibration""" ) return DataLoader( A__ , batch_size=self.args.eval_batch_size , collate_fn=self.data_collator , drop_last=self.args.dataloader_drop_last , num_workers=self.args.dataloader_num_workers , pin_memory=self.args.dataloader_pin_memory , shuffle=A__ , ) def UpperCamelCase ( self , A__=None ) -> str: _SCREAMING_SNAKE_CASE = self.train_dataset if calib_dataset is None else calib_dataset _SCREAMING_SNAKE_CASE = self.get_calib_dataloader(A__ ) _SCREAMING_SNAKE_CASE = self.model quant_trainer.configure_model(A__ , self.quant_trainer_args , calib=A__ ) model.eval() quant_trainer.enable_calibration(A__ ) logger.info("""***** Running calibration *****""" ) logger.info(F" Num examples = {self.calib_num}" ) logger.info(F" Batch size = {calib_dataloader.batch_size}" ) for step, inputs in enumerate(A__ ): # Prediction step _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self.prediction_step(A__ , A__ , prediction_loss_only=A__ ) if (step + 1) * calib_dataloader.batch_size >= self.calib_num: break quant_trainer.finish_calibration(A__ , self.quant_trainer_args ) _SCREAMING_SNAKE_CASE = model def UpperCamelCase ( self , A__=None , A__=None , A__=None , A__ = "eval" ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.eval_dataset if eval_dataset is None else eval_dataset _SCREAMING_SNAKE_CASE = self.get_eval_dataloader(A__ ) _SCREAMING_SNAKE_CASE = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. _SCREAMING_SNAKE_CASE = self.compute_metrics _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: _SCREAMING_SNAKE_CASE = eval_loop( A__ , description="""Evaluation""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=A__ , ) finally: _SCREAMING_SNAKE_CASE = compute_metrics if self.post_process_function is not None and self.compute_metrics is not None: _SCREAMING_SNAKE_CASE = self.post_process_function(A__ , A__ , output.predictions ) _SCREAMING_SNAKE_CASE = self.compute_metrics(A__ ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(F"{metric_key_prefix}_" ): _SCREAMING_SNAKE_CASE = metrics.pop(A__ ) self.log(A__ ) else: _SCREAMING_SNAKE_CASE = {} if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report() ) _SCREAMING_SNAKE_CASE = self.callback_handler.on_evaluate(self.args , self.state , self.control , A__ ) return metrics def UpperCamelCase ( self , A__ , A__ , A__=None , A__ = "test" ) -> List[str]: _SCREAMING_SNAKE_CASE = self.get_test_dataloader(A__ ) # Temporarily disable metric computation, we will do it in the loop here. _SCREAMING_SNAKE_CASE = self.compute_metrics _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: _SCREAMING_SNAKE_CASE = eval_loop( A__ , description="""Prediction""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=A__ , ) finally: _SCREAMING_SNAKE_CASE = compute_metrics if self.post_process_function is None or self.compute_metrics is None: return output _SCREAMING_SNAKE_CASE = self.post_process_function(A__ , A__ , output.predictions , """predict""" ) _SCREAMING_SNAKE_CASE = self.compute_metrics(A__ ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(F"{metric_key_prefix}_" ): _SCREAMING_SNAKE_CASE = metrics.pop(A__ ) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=A__ ) def UpperCamelCase ( self , A__="./" ) -> Tuple: _SCREAMING_SNAKE_CASE = self.eval_dataset _SCREAMING_SNAKE_CASE = self.get_eval_dataloader(A__ ) _SCREAMING_SNAKE_CASE = next(iter(A__ ) ) # saving device - to make it consistent _SCREAMING_SNAKE_CASE = torch.device("""cuda""" if torch.cuda.is_available() else """cpu""" ) # convert to tuple _SCREAMING_SNAKE_CASE = tuple(v.to(A__ ) for k, v in batch.items() ) logger.info("""Converting model to be onnx compatible""" ) from pytorch_quantization.nn import TensorQuantizer _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = self.model.to(A__ ) model.eval() model.float() _SCREAMING_SNAKE_CASE = model.module if hasattr(A__ , """module""" ) else model quant_trainer.configure_model(A__ , self.quant_trainer_args ) _SCREAMING_SNAKE_CASE = os.path.join(A__ , """model.onnx""" ) logger.info(F"exporting model to {output_model_file}" ) _SCREAMING_SNAKE_CASE = {0: """batch_size""", 1: """seq_len"""} torch.onnx.export( A__ , A__ , A__ , export_params=A__ , opset_version=13 , do_constant_folding=A__ , input_names=["""input_ids""", """attention_mask""", """token_type_ids"""] , output_names=["""output_start_logits""", """output_end_logits"""] , dynamic_axes={ """input_ids""": axes, """attention_mask""": axes, """token_type_ids""": axes, """output_start_logits""": axes, """output_end_logits""": axes, } , verbose=A__ , ) logger.info("""onnx export finished""" )
0
0
import math UpperCamelCase__ : List[str] = 10 UpperCamelCase__ : List[Any] = 7 UpperCamelCase__ : Dict = BALLS_PER_COLOUR * NUM_COLOURS def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ = 20 ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE = math.comb(__A , __A ) _SCREAMING_SNAKE_CASE = math.comb(NUM_BALLS - BALLS_PER_COLOUR , __A ) _SCREAMING_SNAKE_CASE = NUM_COLOURS * (1 - missing_colour / total) return F"{result:.9f}" if __name__ == "__main__": print(solution(20))
714
'''simple docstring''' def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" return "".join([hex(SCREAMING_SNAKE_CASE_ )[2:].zfill(2 ).upper() for byte in list(SCREAMING_SNAKE_CASE_ )] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> bytes: """simple docstring""" # Check data validity, following RFC3548 # https://www.ietf.org/rfc/rfc3548.txt if (len(SCREAMING_SNAKE_CASE_ ) % 2) != 0: raise ValueError( """Base16 encoded data is invalid: Data does not have an even number of hex digits.""" ) # Check the character set - the standard base16 alphabet # is uppercase according to RFC3548 section 6 if not set(SCREAMING_SNAKE_CASE_ ) <= set("""0123456789ABCDEF""" ): raise ValueError( """Base16 encoded data is invalid: Data is not uppercase hex or it contains invalid characters.""" ) # For every two hexadecimal digits (= a byte), turn it into an integer. # Then, string the result together into bytes, and return it. return bytes(int(data[i] + data[i + 1] , 16 ) for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) , 2 ) ) if __name__ == "__main__": import doctest doctest.testmod()
0
0
'''simple docstring''' from ....configuration_utils import PretrainedConfig from ....utils import logging UpperCamelCase__ : Optional[Any] = logging.get_logger(__name__) UpperCamelCase__ : str = { "speechbrain/m-ctc-t-large": "https://huggingface.co/speechbrain/m-ctc-t-large/resolve/main/config.json", # See all M-CTC-T models at https://huggingface.co/models?filter=mctct } class _a (a__): """simple docstring""" SCREAMING_SNAKE_CASE = 'mctct' def __init__( self , A__=80_65 , A__=15_36 , A__=36 , A__=61_44 , A__=4 , A__=3_84 , A__=9_20 , A__=1E-5 , A__=0.3 , A__="relu" , A__=0.02 , A__=0.3 , A__=0.3 , A__=1 , A__=0 , A__=2 , A__=1 , A__=0.3 , A__=1 , A__=(7,) , A__=(3,) , A__=80 , A__=1 , A__=None , A__="sum" , A__=False , **A__ , ) -> str: super().__init__(**_A , pad_token_id=_A , bos_token_id=_A , eos_token_id=_A ) _SCREAMING_SNAKE_CASE = vocab_size _SCREAMING_SNAKE_CASE = hidden_size _SCREAMING_SNAKE_CASE = num_hidden_layers _SCREAMING_SNAKE_CASE = intermediate_size _SCREAMING_SNAKE_CASE = num_attention_heads _SCREAMING_SNAKE_CASE = attention_head_dim _SCREAMING_SNAKE_CASE = max_position_embeddings _SCREAMING_SNAKE_CASE = layer_norm_eps _SCREAMING_SNAKE_CASE = layerdrop _SCREAMING_SNAKE_CASE = hidden_act _SCREAMING_SNAKE_CASE = initializer_range _SCREAMING_SNAKE_CASE = hidden_dropout_prob _SCREAMING_SNAKE_CASE = attention_probs_dropout_prob _SCREAMING_SNAKE_CASE = pad_token_id _SCREAMING_SNAKE_CASE = bos_token_id _SCREAMING_SNAKE_CASE = eos_token_id _SCREAMING_SNAKE_CASE = conv_glu_dim _SCREAMING_SNAKE_CASE = conv_dropout _SCREAMING_SNAKE_CASE = num_conv_layers _SCREAMING_SNAKE_CASE = input_feat_per_channel _SCREAMING_SNAKE_CASE = input_channels _SCREAMING_SNAKE_CASE = conv_channels _SCREAMING_SNAKE_CASE = ctc_loss_reduction _SCREAMING_SNAKE_CASE = ctc_zero_infinity # prevents config testing fail with exporting to json _SCREAMING_SNAKE_CASE = list(_A ) _SCREAMING_SNAKE_CASE = list(_A ) if len(self.conv_kernel ) != self.num_conv_layers: raise ValueError( """Configuration for convolutional module is incorrect. """ """It is required that `len(config.conv_kernel)` == `config.num_conv_layers` """ F"but is `len(config.conv_kernel) = {len(self.conv_kernel )}`, " F"`config.num_conv_layers = {self.num_conv_layers}`." )
715
'''simple docstring''' import pytest import requests from datasets.utils.file_utils import http_head from .utils import OfflineSimulationMode, RequestWouldHangIndefinitelyError, offline @pytest.mark.integration def lowerCAmelCase_ ( ) -> List[Any]: """simple docstring""" with offline(OfflineSimulationMode.CONNECTION_TIMES_OUT ): with pytest.raises(SCREAMING_SNAKE_CASE_ ): requests.request("""GET""" , """https://huggingface.co""" ) with pytest.raises(requests.exceptions.ConnectTimeout ): requests.request("""GET""" , """https://huggingface.co""" , timeout=1.0 ) @pytest.mark.integration def lowerCAmelCase_ ( ) -> int: """simple docstring""" with offline(OfflineSimulationMode.CONNECTION_FAILS ): with pytest.raises(requests.exceptions.ConnectionError ): requests.request("""GET""" , """https://huggingface.co""" ) def lowerCAmelCase_ ( ) -> Optional[Any]: """simple docstring""" with offline(OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1 ): with pytest.raises(SCREAMING_SNAKE_CASE_ ): http_head("""https://huggingface.co""" )
0
0
import warnings from contextlib import contextmanager from ...processing_utils import ProcessorMixin from .feature_extraction_wavaveca import WavaVecaFeatureExtractor from .tokenization_wavaveca import WavaVecaCTCTokenizer class _a (_A): """simple docstring""" SCREAMING_SNAKE_CASE = 'Wav2Vec2FeatureExtractor' SCREAMING_SNAKE_CASE = 'AutoTokenizer' def __init__( self , A__ , A__ ) -> Optional[int]: super().__init__(__lowerCamelCase , __lowerCamelCase ) _SCREAMING_SNAKE_CASE = self.feature_extractor _SCREAMING_SNAKE_CASE = False @classmethod def UpperCamelCase ( cls , A__ , **A__ ) -> str: try: return super().from_pretrained(__lowerCamelCase , **__lowerCamelCase ) except OSError: warnings.warn( F"Loading a tokenizer inside {cls.__name__} from a config that does not" """ include a `tokenizer_class` attribute is deprecated and will be """ """removed in v5. Please add `'tokenizer_class': 'Wav2Vec2CTCTokenizer'`""" """ attribute to either your `config.json` or `tokenizer_config.json` """ """file to suppress this warning: """ , __lowerCamelCase , ) _SCREAMING_SNAKE_CASE = WavaVecaFeatureExtractor.from_pretrained(__lowerCamelCase , **__lowerCamelCase ) _SCREAMING_SNAKE_CASE = WavaVecaCTCTokenizer.from_pretrained(__lowerCamelCase , **__lowerCamelCase ) return cls(feature_extractor=__lowerCamelCase , tokenizer=__lowerCamelCase ) def __call__( self , *A__ , **A__ ) -> List[Any]: # For backward compatibility if self._in_target_context_manager: return self.current_processor(*__lowerCamelCase , **__lowerCamelCase ) if "raw_speech" in kwargs: warnings.warn("""Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.""" ) _SCREAMING_SNAKE_CASE = kwargs.pop("""raw_speech""" ) else: _SCREAMING_SNAKE_CASE = kwargs.pop("""audio""" , __lowerCamelCase ) _SCREAMING_SNAKE_CASE = kwargs.pop("""sampling_rate""" , __lowerCamelCase ) _SCREAMING_SNAKE_CASE = kwargs.pop("""text""" , __lowerCamelCase ) if len(__lowerCamelCase ) > 0: _SCREAMING_SNAKE_CASE = args[0] _SCREAMING_SNAKE_CASE = args[1:] if audio is None and text is None: raise ValueError("""You need to specify either an `audio` or `text` input to process.""" ) if audio is not None: _SCREAMING_SNAKE_CASE = self.feature_extractor(__lowerCamelCase , *__lowerCamelCase , sampling_rate=__lowerCamelCase , **__lowerCamelCase ) if text is not None: _SCREAMING_SNAKE_CASE = self.tokenizer(__lowerCamelCase , **__lowerCamelCase ) if text is None: return inputs elif audio is None: return encodings else: _SCREAMING_SNAKE_CASE = encodings["input_ids"] return inputs def UpperCamelCase ( self , *A__ , **A__ ) -> str: # For backward compatibility if self._in_target_context_manager: return self.current_processor.pad(*__lowerCamelCase , **__lowerCamelCase ) _SCREAMING_SNAKE_CASE = kwargs.pop("""input_features""" , __lowerCamelCase ) _SCREAMING_SNAKE_CASE = kwargs.pop("""labels""" , __lowerCamelCase ) if len(__lowerCamelCase ) > 0: _SCREAMING_SNAKE_CASE = args[0] _SCREAMING_SNAKE_CASE = args[1:] if input_features is not None: _SCREAMING_SNAKE_CASE = self.feature_extractor.pad(__lowerCamelCase , *__lowerCamelCase , **__lowerCamelCase ) if labels is not None: _SCREAMING_SNAKE_CASE = self.tokenizer.pad(__lowerCamelCase , **__lowerCamelCase ) if labels is None: return input_features elif input_features is None: return labels else: _SCREAMING_SNAKE_CASE = labels["input_ids"] return input_features def UpperCamelCase ( self , *A__ , **A__ ) -> List[Any]: return self.tokenizer.batch_decode(*__lowerCamelCase , **__lowerCamelCase ) def UpperCamelCase ( self , *A__ , **A__ ) -> List[Any]: return self.tokenizer.decode(*__lowerCamelCase , **__lowerCamelCase ) @contextmanager def UpperCamelCase ( self ) -> Union[str, Any]: warnings.warn( """`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your """ """labels by using the argument `text` of the regular `__call__` method (either in the same call as """ """your audio inputs, or in a separate call.""" ) _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = self.tokenizer yield _SCREAMING_SNAKE_CASE = self.feature_extractor _SCREAMING_SNAKE_CASE = False
716
'''simple docstring''' import math from collections.abc import Iterator from itertools import takewhile def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> bool: """simple docstring""" if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(SCREAMING_SNAKE_CASE_ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def lowerCAmelCase_ ( ) -> Iterator[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = 2 while True: if is_prime(SCREAMING_SNAKE_CASE_ ): yield num num += 1 def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ = 2_00_00_00 ) -> int: """simple docstring""" return sum(takewhile(lambda SCREAMING_SNAKE_CASE_ : x < n , prime_generator() ) ) if __name__ == "__main__": print(f"""{solution() = }""")
0
0
'''simple docstring''' import argparse import json import os import re import torch from transformers import BloomConfig, BloomModel from transformers.file_utils import CONFIG_NAME, WEIGHTS_NAME from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase__ : Tuple = [ "word_embeddings_layernorm.weight", "word_embeddings_layernorm.bias", "input_layernorm.weight", "input_layernorm.bias", "post_attention_layernorm.weight", "post_attention_layernorm.bias", "self_attention.dense.bias", "mlp.dense_4h_to_h.bias", "ln_f.weight", "ln_f.bias", ] UpperCamelCase__ : Dict = [ "mlp.dense_4h_to_h.weight", "self_attention.dense.weight", ] def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = { '''word_embeddings.weight''': '''word_embeddings.weight''', '''word_embeddings.norm.weight''': '''word_embeddings_layernorm.weight''', '''word_embeddings.norm.bias''': '''word_embeddings_layernorm.bias''', '''weight''': '''ln_f.weight''', '''bias''': '''ln_f.bias''', } if key in layer_rename_map: return layer_rename_map[key] # Handle transformer blocks _SCREAMING_SNAKE_CASE = int(re.match(r""".*layer_(\d*).*""" , a_ )[1] ) layer_number -= 3 return F"h.{layer_number}." + key def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" if dtype == torch.bool: return 1 / 8 _SCREAMING_SNAKE_CASE = re.search(r"""[^\d](\d+)$""" , str(a_ ) ) if bit_search is None: raise ValueError(F"`dtype` is not a valid dtype: {dtype}." ) _SCREAMING_SNAKE_CASE = int(bit_search.groups()[0] ) return bit_size // 8 def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" if bloom_config_file == "": _SCREAMING_SNAKE_CASE = BloomConfig() else: _SCREAMING_SNAKE_CASE = BloomConfig.from_json_file(a_ ) if shard_model: _SCREAMING_SNAKE_CASE = os.listdir(a_ ) _SCREAMING_SNAKE_CASE = sorted(filter(lambda SCREAMING_SNAKE_CASE_ : s.startswith("""layer""" ) and "model_00" in s , a_ ) ) _SCREAMING_SNAKE_CASE = {'''weight_map''': {}, '''metadata''': {}} _SCREAMING_SNAKE_CASE = 0 _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = BloomConfig() for j, file in enumerate(a_ ): print("""Processing file: {}""".format(a_ ) ) _SCREAMING_SNAKE_CASE = None for i in range(a_ ): # load all TP files _SCREAMING_SNAKE_CASE = file.replace("""model_00""" , F"model_0{i}" ) _SCREAMING_SNAKE_CASE = torch.load(os.path.join(a_ , a_ ) , map_location="""cpu""" ) # Rename keys in the transformers names _SCREAMING_SNAKE_CASE = list(temp.keys() ) for key in keys: _SCREAMING_SNAKE_CASE = temp.pop(a_ ) if tensors is None: _SCREAMING_SNAKE_CASE = temp else: for key in tensors.keys(): if any(key.endswith(a_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): # We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425) tensors[key] += temp[key] else: # Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel _SCREAMING_SNAKE_CASE = 1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN ) else 0 # We concatenate these weights accross TP ranks _SCREAMING_SNAKE_CASE = torch.cat([tensors[key], temp[key]] , dim=a_ ) # Divide by the number of TP the weights we want to average for key in tensors.keys(): if any(key.endswith(a_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): _SCREAMING_SNAKE_CASE = tensors[key] / pretraining_tp torch.save( a_ , os.path.join( a_ , """pytorch_model_{}-of-{}.bin""".format(str(j + 1 ).zfill(5 ) , str(len(a_ ) ).zfill(5 ) ) , ) , ) for key in tensors.keys(): _SCREAMING_SNAKE_CASE = tensors[key] total_size += value.numel() * get_dtype_size(value.dtype ) if key not in index_dict["weight_map"]: _SCREAMING_SNAKE_CASE = '''pytorch_model_{}-of-{}.bin'''.format( str(j + 1 ).zfill(5 ) , str(len(a_ ) ).zfill(5 ) ) _SCREAMING_SNAKE_CASE = BloomConfig() _SCREAMING_SNAKE_CASE = pytorch_dump_folder_path + '''/''' + CONFIG_NAME _SCREAMING_SNAKE_CASE = total_size with open(a_ , """w""" , encoding="""utf-8""" ) as f: f.write(config.to_json_string() ) with open(os.path.join(a_ , WEIGHTS_NAME + """.index.json""" ) , """w""" , encoding="""utf-8""" ) as f: _SCREAMING_SNAKE_CASE = json.dumps(a_ , indent=2 , sort_keys=a_ ) + '''\n''' f.write(a_ ) else: _SCREAMING_SNAKE_CASE = BloomModel(a_ ) _SCREAMING_SNAKE_CASE = os.listdir(a_ ) _SCREAMING_SNAKE_CASE = sorted(filter(lambda SCREAMING_SNAKE_CASE_ : s.startswith("""layer""" ) and "model_00" in s , a_ ) ) _SCREAMING_SNAKE_CASE = None for i, file in enumerate(a_ ): _SCREAMING_SNAKE_CASE = None for i in range(a_ ): # load all TP files _SCREAMING_SNAKE_CASE = file.replace("""model_00""" , F"model_0{i}" ) _SCREAMING_SNAKE_CASE = torch.load(os.path.join(a_ , a_ ) , map_location="""cpu""" ) # Rename keys in the transformers names _SCREAMING_SNAKE_CASE = list(temp.keys() ) for key in keys: _SCREAMING_SNAKE_CASE = temp.pop(a_ ) if tensors is None: _SCREAMING_SNAKE_CASE = temp else: for key in tensors.keys(): # We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425) if any(key.endswith(a_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): tensors[key] += temp[key] else: # Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel _SCREAMING_SNAKE_CASE = 1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN ) else 0 # We concatenate these weights accross TP ranks _SCREAMING_SNAKE_CASE = torch.cat([tensors[key], temp[key]] , dim=a_ ) # Divide by the number of TP the weights we want to average for key in tensors.keys(): if any(key.endswith(a_ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): _SCREAMING_SNAKE_CASE = tensors[key] / pretraining_tp _SCREAMING_SNAKE_CASE = model.load_state_dict(a_ , strict=a_ ) assert not other_keys.unexpected_keys, F"The keys {other_keys.unexpected_keys} are unexpected" if missing_keys is None: _SCREAMING_SNAKE_CASE = set(other_keys.missing_keys ) else: _SCREAMING_SNAKE_CASE = missing_keys.intersection(set(other_keys.missing_keys ) ) assert not missing_keys, F"The keys {missing_keys} are missing" # Save pytorch-model os.makedirs(a_ , exist_ok=a_ ) _SCREAMING_SNAKE_CASE = pytorch_dump_folder_path + '''/''' + WEIGHTS_NAME _SCREAMING_SNAKE_CASE = pytorch_dump_folder_path + '''/''' + CONFIG_NAME print(F"Save PyTorch model to {pytorch_weights_dump_path} with dtype {config.torch_dtype}" ) if config.torch_dtype is not None: _SCREAMING_SNAKE_CASE = model.to(config.torch_dtype ) torch.save(model.state_dict() , a_ ) print(F"Save configuration file to {pytorch_config_dump_path}" ) with open(a_ , """w""" , encoding="""utf-8""" ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": UpperCamelCase__ : Any = argparse.ArgumentParser() # Required parameters parser.add_argument( "--bloom_checkpoint_path", default=None, type=str, required=True, help="Path to the Megatron-LM checkpoint path.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) parser.add_argument( "--bloom_config_file", default="", type=str, help=( "An optional config json file corresponding to the pre-trained model. \n" "This specifies the model architecture." ), ) parser.add_argument( "--shard_model", action="store_true", help="An optional setting to shard the output model \nThis enables sharding the converted checkpoint", ) parser.add_argument( "--pretraining_tp", default=4, type=int, help="Pretraining TP rank that has been used when training the model in Megatron-LM \n", ) UpperCamelCase__ : str = parser.parse_args() convert_bloom_checkpoint_to_pytorch( args.bloom_checkpoint_path, args.bloom_config_file, args.pytorch_dump_folder_path, args.shard_model, args.pretraining_tp, )
717
'''simple docstring''' import unittest import numpy as np from transformers.testing_utils import require_flax, require_tf, require_torch from transformers.utils import ( expand_dims, flatten_dict, is_flax_available, is_tf_available, is_torch_available, reshape, squeeze, transpose, ) if is_flax_available(): import jax.numpy as jnp if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch class _a (unittest.TestCase): """simple docstring""" def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = { """task_specific_params""": { """summarization""": {"""length_penalty""": 1.0, """max_length""": 1_28, """min_length""": 12, """num_beams""": 4}, """summarization_cnn""": {"""length_penalty""": 2.0, """max_length""": 1_42, """min_length""": 56, """num_beams""": 4}, """summarization_xsum""": {"""length_penalty""": 1.0, """max_length""": 62, """min_length""": 11, """num_beams""": 6}, } } _SCREAMING_SNAKE_CASE = { """task_specific_params.summarization.length_penalty""": 1.0, """task_specific_params.summarization.max_length""": 1_28, """task_specific_params.summarization.min_length""": 12, """task_specific_params.summarization.num_beams""": 4, """task_specific_params.summarization_cnn.length_penalty""": 2.0, """task_specific_params.summarization_cnn.max_length""": 1_42, """task_specific_params.summarization_cnn.min_length""": 56, """task_specific_params.summarization_cnn.num_beams""": 4, """task_specific_params.summarization_xsum.length_penalty""": 1.0, """task_specific_params.summarization_xsum.max_length""": 62, """task_specific_params.summarization_xsum.min_length""": 11, """task_specific_params.summarization_xsum.num_beams""": 6, } self.assertEqual(flatten_dict(A__ ) , A__ ) def UpperCamelCase ( self ) -> int: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(transpose(A__ ) , x.transpose() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(transpose(A__ , axes=(1, 2, 0) ) , x.transpose((1, 2, 0) ) ) ) @require_torch def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(transpose(A__ ) , transpose(A__ ).numpy() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(transpose(A__ , axes=(1, 2, 0) ) , transpose(A__ , axes=(1, 2, 0) ).numpy() ) ) @require_tf def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(transpose(A__ ) , transpose(A__ ).numpy() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(transpose(A__ , axes=(1, 2, 0) ) , transpose(A__ , axes=(1, 2, 0) ).numpy() ) ) @require_flax def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(transpose(A__ ) , np.asarray(transpose(A__ ) ) ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(transpose(A__ , axes=(1, 2, 0) ) , np.asarray(transpose(A__ , axes=(1, 2, 0) ) ) ) ) def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(reshape(A__ , (4, 3) ) , np.reshape(A__ , (4, 3) ) ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(reshape(A__ , (12, 5) ) , np.reshape(A__ , (12, 5) ) ) ) @require_torch def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(reshape(A__ , (4, 3) ) , reshape(A__ , (4, 3) ).numpy() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(reshape(A__ , (12, 5) ) , reshape(A__ , (12, 5) ).numpy() ) ) @require_tf def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(reshape(A__ , (4, 3) ) , reshape(A__ , (4, 3) ).numpy() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(reshape(A__ , (12, 5) ) , reshape(A__ , (12, 5) ).numpy() ) ) @require_flax def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(reshape(A__ , (4, 3) ) , np.asarray(reshape(A__ , (4, 3) ) ) ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(reshape(A__ , (12, 5) ) , np.asarray(reshape(A__ , (12, 5) ) ) ) ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = np.random.randn(1 , 3 , 4 ) self.assertTrue(np.allclose(squeeze(A__ ) , np.squeeze(A__ ) ) ) _SCREAMING_SNAKE_CASE = np.random.randn(1 , 4 , 1 , 5 ) self.assertTrue(np.allclose(squeeze(A__ , axis=2 ) , np.squeeze(A__ , axis=2 ) ) ) @require_torch def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = np.random.randn(1 , 3 , 4 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(squeeze(A__ ) , squeeze(A__ ).numpy() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(1 , 4 , 1 , 5 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(squeeze(A__ , axis=2 ) , squeeze(A__ , axis=2 ).numpy() ) ) @require_tf def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = np.random.randn(1 , 3 , 4 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(squeeze(A__ ) , squeeze(A__ ).numpy() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(1 , 4 , 1 , 5 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(squeeze(A__ , axis=2 ) , squeeze(A__ , axis=2 ).numpy() ) ) @require_flax def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = np.random.randn(1 , 3 , 4 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(squeeze(A__ ) , np.asarray(squeeze(A__ ) ) ) ) _SCREAMING_SNAKE_CASE = np.random.randn(1 , 4 , 1 , 5 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(squeeze(A__ , axis=2 ) , np.asarray(squeeze(A__ , axis=2 ) ) ) ) def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(expand_dims(A__ , axis=1 ) , np.expand_dims(A__ , axis=1 ) ) ) @require_torch def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(expand_dims(A__ , axis=1 ) , expand_dims(A__ , axis=1 ).numpy() ) ) @require_tf def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(expand_dims(A__ , axis=1 ) , expand_dims(A__ , axis=1 ).numpy() ) ) @require_flax def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(expand_dims(A__ , axis=1 ) , np.asarray(expand_dims(A__ , axis=1 ) ) ) )
0
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase__ : str = logging.get_logger(__name__) UpperCamelCase__ : Optional[int] = { '''google/vivit-b-16x2-kinetics400''': ( '''https://huggingface.co/google/vivit-b-16x2-kinetics400/resolve/main/config.json''' ), # See all Vivit models at https://huggingface.co/models?filter=vivit } class _a (_UpperCAmelCase): """simple docstring""" SCREAMING_SNAKE_CASE = 'vivit' def __init__( self , A__=2_24 , A__=32 , A__=[2, 16, 16] , A__=3 , A__=7_68 , A__=12 , A__=12 , A__=30_72 , A__="gelu_fast" , A__=0.0 , A__=0.0 , A__=0.02 , A__=1E-06 , A__=True , **A__ , ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = hidden_size _SCREAMING_SNAKE_CASE = num_hidden_layers _SCREAMING_SNAKE_CASE = num_attention_heads _SCREAMING_SNAKE_CASE = intermediate_size _SCREAMING_SNAKE_CASE = hidden_act _SCREAMING_SNAKE_CASE = hidden_dropout_prob _SCREAMING_SNAKE_CASE = attention_probs_dropout_prob _SCREAMING_SNAKE_CASE = initializer_range _SCREAMING_SNAKE_CASE = layer_norm_eps _SCREAMING_SNAKE_CASE = image_size _SCREAMING_SNAKE_CASE = num_frames _SCREAMING_SNAKE_CASE = tubelet_size _SCREAMING_SNAKE_CASE = num_channels _SCREAMING_SNAKE_CASE = qkv_bias super().__init__(**__UpperCamelCase )
718
'''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 _a (_lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = '' SCREAMING_SNAKE_CASE = 'hf-legacy' # "hf://"" is reserved for hffs def __init__( self , A__ = None , A__ = None , **A__ , ) -> Optional[int]: super().__init__(self , **A__ ) _SCREAMING_SNAKE_CASE = repo_info _SCREAMING_SNAKE_CASE = token _SCREAMING_SNAKE_CASE = None def UpperCamelCase ( self ) -> Tuple: if self.dir_cache is None: _SCREAMING_SNAKE_CASE = {} for hf_file in self.repo_info.siblings: # TODO(QL): add sizes _SCREAMING_SNAKE_CASE = { """name""": hf_file.rfilename, """size""": None, """type""": """file""", } self.dir_cache.update( { str(A__ ): {"""name""": str(A__ ), """size""": None, """type""": """directory"""} for d in list(PurePosixPath(hf_file.rfilename ).parents )[:-1] } ) def UpperCamelCase ( self , A__ , A__ = "rb" , **A__ , ) -> Optional[int]: if not isinstance(self.repo_info , A__ ): raise NotImplementedError(F"Open is only implemented for dataset repositories, but got {self.repo_info}" ) _SCREAMING_SNAKE_CASE = hf_hub_url(self.repo_info.id , A__ , revision=self.repo_info.sha ) return fsspec.open( A__ , mode=A__ , headers=get_authentication_headers_for_url(A__ , use_auth_token=self.token ) , client_kwargs={"""trust_env""": True} , ).open() def UpperCamelCase ( self , A__ , **A__ ) -> str: self._get_dirs() _SCREAMING_SNAKE_CASE = self._strip_protocol(A__ ) if path in self.dir_cache: return self.dir_cache[path] else: raise FileNotFoundError(A__ ) def UpperCamelCase ( self , A__ , A__=False , **A__ ) -> List[Any]: self._get_dirs() _SCREAMING_SNAKE_CASE = PurePosixPath(path.strip("""/""" ) ) _SCREAMING_SNAKE_CASE = {} for p, f in self.dir_cache.items(): _SCREAMING_SNAKE_CASE = PurePosixPath(p.strip("""/""" ) ) _SCREAMING_SNAKE_CASE = p.parent if root == path: _SCREAMING_SNAKE_CASE = f _SCREAMING_SNAKE_CASE = list(paths.values() ) if detail: return out else: return sorted(f["""name"""] for f in out )
0
0
import contextlib import csv import json import os import sqlitea import tarfile import textwrap import zipfile import pyarrow as pa import pyarrow.parquet as pq import pytest import datasets import datasets.config @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE = 10 _SCREAMING_SNAKE_CASE = datasets.Features( { """tokens""": datasets.Sequence(datasets.Value("""string""" ) ), """labels""": datasets.Sequence(datasets.ClassLabel(names=["""negative""", """positive"""] ) ), """answers""": datasets.Sequence( { """text""": datasets.Value("""string""" ), """answer_start""": datasets.Value("""int32""" ), } ), """id""": datasets.Value("""int64""" ), } ) _SCREAMING_SNAKE_CASE = datasets.Dataset.from_dict( { """tokens""": [["""foo"""] * 5] * n, """labels""": [[1] * 5] * n, """answers""": [{"""answer_start""": [97], """text""": ["""1976"""]}] * 10, """id""": list(range(SCREAMING_SNAKE_CASE_ ) ), } , features=SCREAMING_SNAKE_CASE_ , ) return dataset @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = str(tmp_path_factory.mktemp("""data""" ) / """file.arrow""" ) dataset.map(cache_file_name=SCREAMING_SNAKE_CASE_ ) return filename # FILE_CONTENT + files UpperCamelCase__ : Tuple = """\ Text data. Second line of data.""" @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("""data""" ) / """file.txt""" _SCREAMING_SNAKE_CASE = FILE_CONTENT with open(SCREAMING_SNAKE_CASE_ , """w""" ) as f: f.write(SCREAMING_SNAKE_CASE_ ) return filename @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" import bza _SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("""data""" ) / """file.txt.bz2""" _SCREAMING_SNAKE_CASE = bytes(SCREAMING_SNAKE_CASE_ , """utf-8""" ) with bza.open(SCREAMING_SNAKE_CASE_ , """wb""" ) as f: f.write(SCREAMING_SNAKE_CASE_ ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Tuple: """simple docstring""" import gzip _SCREAMING_SNAKE_CASE = str(tmp_path_factory.mktemp("""data""" ) / """file.txt.gz""" ) _SCREAMING_SNAKE_CASE = bytes(SCREAMING_SNAKE_CASE_ , """utf-8""" ) with gzip.open(SCREAMING_SNAKE_CASE_ , """wb""" ) as f: f.write(SCREAMING_SNAKE_CASE_ ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: """simple docstring""" if datasets.config.LZ4_AVAILABLE: import lza.frame _SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("""data""" ) / """file.txt.lz4""" _SCREAMING_SNAKE_CASE = bytes(SCREAMING_SNAKE_CASE_ , """utf-8""" ) with lza.frame.open(SCREAMING_SNAKE_CASE_ , """wb""" ) as f: f.write(SCREAMING_SNAKE_CASE_ ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Tuple: """simple docstring""" if datasets.config.PY7ZR_AVAILABLE: import pyazr _SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("""data""" ) / """file.txt.7z""" with pyazr.SevenZipFile(SCREAMING_SNAKE_CASE_ , """w""" ) as archive: archive.write(SCREAMING_SNAKE_CASE_ , arcname=os.path.basename(SCREAMING_SNAKE_CASE_ ) ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" import tarfile _SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("""data""" ) / """file.txt.tar""" with tarfile.TarFile(SCREAMING_SNAKE_CASE_ , """w""" ) as f: f.add(SCREAMING_SNAKE_CASE_ , arcname=os.path.basename(SCREAMING_SNAKE_CASE_ ) ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" import lzma _SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("""data""" ) / """file.txt.xz""" _SCREAMING_SNAKE_CASE = bytes(SCREAMING_SNAKE_CASE_ , """utf-8""" ) with lzma.open(SCREAMING_SNAKE_CASE_ , """wb""" ) as f: f.write(SCREAMING_SNAKE_CASE_ ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" import zipfile _SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("""data""" ) / """file.txt.zip""" with zipfile.ZipFile(SCREAMING_SNAKE_CASE_ , """w""" ) as f: f.write(SCREAMING_SNAKE_CASE_ , arcname=os.path.basename(SCREAMING_SNAKE_CASE_ ) ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" if datasets.config.ZSTANDARD_AVAILABLE: import zstandard as zstd _SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("""data""" ) / """file.txt.zst""" _SCREAMING_SNAKE_CASE = bytes(SCREAMING_SNAKE_CASE_ , """utf-8""" ) with zstd.open(SCREAMING_SNAKE_CASE_ , """wb""" ) as f: f.write(SCREAMING_SNAKE_CASE_ ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("""data""" ) / """file.xml""" _SCREAMING_SNAKE_CASE = textwrap.dedent( """\\n <?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n <tmx version=\"1.4\">\n <header segtype=\"sentence\" srclang=\"ca\" />\n <body>\n <tu>\n <tuv xml:lang=\"ca\"><seg>Contingut 1</seg></tuv>\n <tuv xml:lang=\"en\"><seg>Content 1</seg></tuv>\n </tu>\n <tu>\n <tuv xml:lang=\"ca\"><seg>Contingut 2</seg></tuv>\n <tuv xml:lang=\"en\"><seg>Content 2</seg></tuv>\n </tu>\n <tu>\n <tuv xml:lang=\"ca\"><seg>Contingut 3</seg></tuv>\n <tuv xml:lang=\"en\"><seg>Content 3</seg></tuv>\n </tu>\n <tu>\n <tuv xml:lang=\"ca\"><seg>Contingut 4</seg></tuv>\n <tuv xml:lang=\"en\"><seg>Content 4</seg></tuv>\n </tu>\n <tu>\n <tuv xml:lang=\"ca\"><seg>Contingut 5</seg></tuv>\n <tuv xml:lang=\"en\"><seg>Content 5</seg></tuv>\n </tu>\n </body>\n </tmx>""" ) with open(SCREAMING_SNAKE_CASE_ , """w""" ) as f: f.write(SCREAMING_SNAKE_CASE_ ) return filename UpperCamelCase__ : Tuple = [ {"""col_1""": """0""", """col_2""": 0, """col_3""": 0.0}, {"""col_1""": """1""", """col_2""": 1, """col_3""": 1.0}, {"""col_1""": """2""", """col_2""": 2, """col_3""": 2.0}, {"""col_1""": """3""", """col_2""": 3, """col_3""": 3.0}, ] UpperCamelCase__ : Optional[Any] = [ {"""col_1""": """4""", """col_2""": 4, """col_3""": 4.0}, {"""col_1""": """5""", """col_2""": 5, """col_3""": 5.0}, ] UpperCamelCase__ : Any = { """col_1""": ["""0""", """1""", """2""", """3"""], """col_2""": [0, 1, 2, 3], """col_3""": [0.0, 1.0, 2.0, 3.0], } UpperCamelCase__ : List[Any] = [ {"""col_3""": 0.0, """col_1""": """0""", """col_2""": 0}, {"""col_3""": 1.0, """col_1""": """1""", """col_2""": 1}, ] UpperCamelCase__ : List[str] = [ {"""col_1""": """s0""", """col_2""": 0, """col_3""": 0.0}, {"""col_1""": """s1""", """col_2""": 1, """col_3""": 1.0}, {"""col_1""": """s2""", """col_2""": 2, """col_3""": 2.0}, {"""col_1""": """s3""", """col_2""": 3, """col_3""": 3.0}, ] @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( ) -> int: """simple docstring""" return DATA_DICT_OF_LISTS @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = datasets.Dataset.from_dict(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = str(tmp_path_factory.mktemp("""data""" ) / """dataset.arrow""" ) dataset.map(cache_file_name=SCREAMING_SNAKE_CASE_ ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = str(tmp_path_factory.mktemp("""data""" ) / """dataset.sqlite""" ) with contextlib.closing(sqlitea.connect(SCREAMING_SNAKE_CASE_ ) ) as con: _SCREAMING_SNAKE_CASE = con.cursor() cur.execute("""CREATE TABLE dataset(col_1 text, col_2 int, col_3 real)""" ) for item in DATA: cur.execute("""INSERT INTO dataset(col_1, col_2, col_3) VALUES (?, ?, ?)""" , tuple(item.values() ) ) con.commit() return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE = str(tmp_path_factory.mktemp("""data""" ) / """dataset.csv""" ) with open(SCREAMING_SNAKE_CASE_ , """w""" , newline="""""" ) as f: _SCREAMING_SNAKE_CASE = csv.DictWriter(SCREAMING_SNAKE_CASE_ , fieldnames=["""col_1""", """col_2""", """col_3"""] ) writer.writeheader() for item in DATA: writer.writerow(SCREAMING_SNAKE_CASE_ ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE = str(tmp_path_factory.mktemp("""data""" ) / """dataset2.csv""" ) with open(SCREAMING_SNAKE_CASE_ , """w""" , newline="""""" ) as f: _SCREAMING_SNAKE_CASE = csv.DictWriter(SCREAMING_SNAKE_CASE_ , fieldnames=["""col_1""", """col_2""", """col_3"""] ) writer.writeheader() for item in DATA: writer.writerow(SCREAMING_SNAKE_CASE_ ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Tuple: """simple docstring""" import bza _SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("""data""" ) / """dataset.csv.bz2""" with open(SCREAMING_SNAKE_CASE_ , """rb""" ) as f: _SCREAMING_SNAKE_CASE = f.read() # data = bytes(FILE_CONTENT, "utf-8") with bza.open(SCREAMING_SNAKE_CASE_ , """wb""" ) as f: f.write(SCREAMING_SNAKE_CASE_ ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("""data""" ) / """dataset.csv.zip""" with zipfile.ZipFile(SCREAMING_SNAKE_CASE_ , """w""" ) as f: f.write(SCREAMING_SNAKE_CASE_ , arcname=os.path.basename(SCREAMING_SNAKE_CASE_ ) ) f.write(SCREAMING_SNAKE_CASE_ , arcname=os.path.basename(SCREAMING_SNAKE_CASE_ ) ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("""data""" ) / """dataset.csv.zip""" with zipfile.ZipFile(SCREAMING_SNAKE_CASE_ , """w""" ) as f: f.write(SCREAMING_SNAKE_CASE_ , arcname=os.path.basename(csv_path.replace(""".csv""" , """.CSV""" ) ) ) f.write(SCREAMING_SNAKE_CASE_ , arcname=os.path.basename(csva_path.replace(""".csv""" , """.CSV""" ) ) ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("""data""" ) / """dataset_with_dir.csv.zip""" with zipfile.ZipFile(SCREAMING_SNAKE_CASE_ , """w""" ) as f: f.write(SCREAMING_SNAKE_CASE_ , arcname=os.path.join("""main_dir""" , os.path.basename(SCREAMING_SNAKE_CASE_ ) ) ) f.write(SCREAMING_SNAKE_CASE_ , arcname=os.path.join("""main_dir""" , os.path.basename(SCREAMING_SNAKE_CASE_ ) ) ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE = str(tmp_path_factory.mktemp("""data""" ) / """dataset.parquet""" ) _SCREAMING_SNAKE_CASE = pa.schema( { """col_1""": pa.string(), """col_2""": pa.intaa(), """col_3""": pa.floataa(), } ) with open(SCREAMING_SNAKE_CASE_ , """wb""" ) as f: _SCREAMING_SNAKE_CASE = pq.ParquetWriter(SCREAMING_SNAKE_CASE_ , schema=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = pa.Table.from_pydict({k: [DATA[i][k] for i in range(len(SCREAMING_SNAKE_CASE_ ) )] for k in DATA[0]} , schema=SCREAMING_SNAKE_CASE_ ) writer.write_table(SCREAMING_SNAKE_CASE_ ) writer.close() return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = str(tmp_path_factory.mktemp("""data""" ) / """dataset.json""" ) _SCREAMING_SNAKE_CASE = {"""data""": DATA} with open(SCREAMING_SNAKE_CASE_ , """w""" ) as f: json.dump(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = str(tmp_path_factory.mktemp("""data""" ) / """dataset.json""" ) _SCREAMING_SNAKE_CASE = {"""data""": DATA_DICT_OF_LISTS} with open(SCREAMING_SNAKE_CASE_ , """w""" ) as f: json.dump(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE = str(tmp_path_factory.mktemp("""data""" ) / """dataset.jsonl""" ) with open(SCREAMING_SNAKE_CASE_ , """w""" ) as f: for item in DATA: f.write(json.dumps(SCREAMING_SNAKE_CASE_ ) + """\n""" ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = str(tmp_path_factory.mktemp("""data""" ) / """dataset2.jsonl""" ) with open(SCREAMING_SNAKE_CASE_ , """w""" ) as f: for item in DATA: f.write(json.dumps(SCREAMING_SNAKE_CASE_ ) + """\n""" ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE = str(tmp_path_factory.mktemp("""data""" ) / """dataset_312.jsonl""" ) with open(SCREAMING_SNAKE_CASE_ , """w""" ) as f: for item in DATA_312: f.write(json.dumps(SCREAMING_SNAKE_CASE_ ) + """\n""" ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE = str(tmp_path_factory.mktemp("""data""" ) / """dataset-str.jsonl""" ) with open(SCREAMING_SNAKE_CASE_ , """w""" ) as f: for item in DATA_STR: f.write(json.dumps(SCREAMING_SNAKE_CASE_ ) + """\n""" ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" import gzip _SCREAMING_SNAKE_CASE = str(tmp_path_factory.mktemp("""data""" ) / """dataset.txt.gz""" ) with open(SCREAMING_SNAKE_CASE_ , """rb""" ) as orig_file: with gzip.open(SCREAMING_SNAKE_CASE_ , """wb""" ) as zipped_file: zipped_file.writelines(SCREAMING_SNAKE_CASE_ ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" import gzip _SCREAMING_SNAKE_CASE = str(tmp_path_factory.mktemp("""data""" ) / """dataset.jsonl.gz""" ) with open(SCREAMING_SNAKE_CASE_ , """rb""" ) as orig_file: with gzip.open(SCREAMING_SNAKE_CASE_ , """wb""" ) as zipped_file: zipped_file.writelines(SCREAMING_SNAKE_CASE_ ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("""data""" ) / """dataset.jsonl.zip""" with zipfile.ZipFile(SCREAMING_SNAKE_CASE_ , """w""" ) as f: f.write(SCREAMING_SNAKE_CASE_ , arcname=os.path.basename(SCREAMING_SNAKE_CASE_ ) ) f.write(SCREAMING_SNAKE_CASE_ , arcname=os.path.basename(SCREAMING_SNAKE_CASE_ ) ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("""data""" ) / """dataset_nested.jsonl.zip""" with zipfile.ZipFile(SCREAMING_SNAKE_CASE_ , """w""" ) as f: f.write(SCREAMING_SNAKE_CASE_ , arcname=os.path.join("""nested""" , os.path.basename(SCREAMING_SNAKE_CASE_ ) ) ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("""data""" ) / """dataset_with_dir.jsonl.zip""" with zipfile.ZipFile(SCREAMING_SNAKE_CASE_ , """w""" ) as f: f.write(SCREAMING_SNAKE_CASE_ , arcname=os.path.join("""main_dir""" , os.path.basename(SCREAMING_SNAKE_CASE_ ) ) ) f.write(SCREAMING_SNAKE_CASE_ , arcname=os.path.join("""main_dir""" , os.path.basename(SCREAMING_SNAKE_CASE_ ) ) ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("""data""" ) / """dataset.jsonl.tar""" with tarfile.TarFile(SCREAMING_SNAKE_CASE_ , """w""" ) as f: f.add(SCREAMING_SNAKE_CASE_ , arcname=os.path.basename(SCREAMING_SNAKE_CASE_ ) ) f.add(SCREAMING_SNAKE_CASE_ , arcname=os.path.basename(SCREAMING_SNAKE_CASE_ ) ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("""data""" ) / """dataset_nested.jsonl.tar""" with tarfile.TarFile(SCREAMING_SNAKE_CASE_ , """w""" ) as f: f.add(SCREAMING_SNAKE_CASE_ , arcname=os.path.join("""nested""" , os.path.basename(SCREAMING_SNAKE_CASE_ ) ) ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE = ["""0""", """1""", """2""", """3"""] _SCREAMING_SNAKE_CASE = str(tmp_path_factory.mktemp("""data""" ) / """dataset.txt""" ) with open(SCREAMING_SNAKE_CASE_ , """w""" ) as f: for item in data: f.write(item + """\n""" ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE = ["""0""", """1""", """2""", """3"""] _SCREAMING_SNAKE_CASE = str(tmp_path_factory.mktemp("""data""" ) / """dataset2.txt""" ) with open(SCREAMING_SNAKE_CASE_ , """w""" ) as f: for item in data: f.write(item + """\n""" ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE = ["""0""", """1""", """2""", """3"""] _SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("""data""" ) / """dataset.abc""" with open(SCREAMING_SNAKE_CASE_ , """w""" ) as f: for item in data: f.write(item + """\n""" ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("""data""" ) / """dataset.text.zip""" with zipfile.ZipFile(SCREAMING_SNAKE_CASE_ , """w""" ) as f: f.write(SCREAMING_SNAKE_CASE_ , arcname=os.path.basename(SCREAMING_SNAKE_CASE_ ) ) f.write(SCREAMING_SNAKE_CASE_ , arcname=os.path.basename(SCREAMING_SNAKE_CASE_ ) ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("""data""" ) / """dataset_with_dir.text.zip""" with zipfile.ZipFile(SCREAMING_SNAKE_CASE_ , """w""" ) as f: f.write(SCREAMING_SNAKE_CASE_ , arcname=os.path.join("""main_dir""" , os.path.basename(SCREAMING_SNAKE_CASE_ ) ) ) f.write(SCREAMING_SNAKE_CASE_ , arcname=os.path.join("""main_dir""" , os.path.basename(SCREAMING_SNAKE_CASE_ ) ) ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("""data""" ) / """dataset.ext.zip""" with zipfile.ZipFile(SCREAMING_SNAKE_CASE_ , """w""" ) as f: f.write(SCREAMING_SNAKE_CASE_ , arcname=os.path.basename("""unsupported.ext""" ) ) f.write(SCREAMING_SNAKE_CASE_ , arcname=os.path.basename("""unsupported_2.ext""" ) ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = """\n""".join(["""First""", """Second\u2029with Unicode new line""", """Third"""] ) _SCREAMING_SNAKE_CASE = str(tmp_path_factory.mktemp("""data""" ) / """dataset_with_unicode_new_lines.txt""" ) with open(SCREAMING_SNAKE_CASE_ , """w""" , encoding="""utf-8""" ) as f: f.write(SCREAMING_SNAKE_CASE_ ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( ) -> Tuple: """simple docstring""" return os.path.join("""tests""" , """features""" , """data""" , """test_image_rgb.jpg""" ) @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( ) -> Any: """simple docstring""" return os.path.join("""tests""" , """features""" , """data""" , """test_audio_44100.wav""" ) @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("""data""" ) / """dataset.img.zip""" with zipfile.ZipFile(SCREAMING_SNAKE_CASE_ , """w""" ) as f: f.write(SCREAMING_SNAKE_CASE_ , arcname=os.path.basename(SCREAMING_SNAKE_CASE_ ) ) f.write(SCREAMING_SNAKE_CASE_ , arcname=os.path.basename(SCREAMING_SNAKE_CASE_ ).replace(""".jpg""" , """2.jpg""" ) ) return path @pytest.fixture(scope="""session""" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("""data_dir""" ) (data_dir / "subdir").mkdir() with open(data_dir / """subdir""" / """train.txt""" , """w""" ) as f: f.write("""foo\n""" * 10 ) with open(data_dir / """subdir""" / """test.txt""" , """w""" ) as f: f.write("""bar\n""" * 10 ) # hidden file with open(data_dir / """subdir""" / """.test.txt""" , """w""" ) as f: f.write("""bar\n""" * 10 ) # hidden directory (data_dir / ".subdir").mkdir() with open(data_dir / """.subdir""" / """train.txt""" , """w""" ) as f: f.write("""foo\n""" * 10 ) with open(data_dir / """.subdir""" / """test.txt""" , """w""" ) as f: f.write("""bar\n""" * 10 ) return data_dir
719
'''simple docstring''' import pyarrow.parquet as pq import pytest from datasets import Audio, Dataset, DatasetDict, Features, NamedSplit, Sequence, Value, config from datasets.features.image import Image from datasets.io.parquet import ParquetDatasetReader, ParquetDatasetWriter, get_writer_batch_size from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" assert isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("""keep_in_memory""" , [False, True] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): _SCREAMING_SNAKE_CASE = ParquetDatasetReader(SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ , keep_in_memory=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_dataset(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @pytest.mark.parametrize( """features""" , [ None, {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}, {"""col_1""": """string""", """col_2""": """string""", """col_3""": """string"""}, {"""col_1""": """int32""", """col_2""": """int32""", """col_3""": """int32"""}, {"""col_1""": """float32""", """col_2""": """float32""", """col_3""": """float32"""}, ] , ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} _SCREAMING_SNAKE_CASE = features.copy() if features else default_expected_features _SCREAMING_SNAKE_CASE = ( Features({feature: Value(SCREAMING_SNAKE_CASE_ ) for feature, dtype in features.items()} ) if features is not None else None ) _SCREAMING_SNAKE_CASE = ParquetDatasetReader(SCREAMING_SNAKE_CASE_ , features=SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_dataset(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @pytest.mark.parametrize("""split""" , [None, NamedSplit("""train""" ), """train""", """test"""] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} _SCREAMING_SNAKE_CASE = ParquetDatasetReader(SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ , split=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_dataset(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert dataset.split == split if split else "train" @pytest.mark.parametrize("""path_type""" , [str, list] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" if issubclass(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE = parquet_path elif issubclass(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE = [parquet_path] _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} _SCREAMING_SNAKE_CASE = ParquetDatasetReader(SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_dataset(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=("train",) ) -> List[str]: """simple docstring""" assert isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) for split in splits: _SCREAMING_SNAKE_CASE = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("""keep_in_memory""" , [False, True] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): _SCREAMING_SNAKE_CASE = ParquetDatasetReader( {"""train""": parquet_path} , cache_dir=SCREAMING_SNAKE_CASE_ , keep_in_memory=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_datasetdict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @pytest.mark.parametrize( """features""" , [ None, {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}, {"""col_1""": """string""", """col_2""": """string""", """col_3""": """string"""}, {"""col_1""": """int32""", """col_2""": """int32""", """col_3""": """int32"""}, {"""col_1""": """float32""", """col_2""": """float32""", """col_3""": """float32"""}, ] , ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} _SCREAMING_SNAKE_CASE = features.copy() if features else default_expected_features _SCREAMING_SNAKE_CASE = ( Features({feature: Value(SCREAMING_SNAKE_CASE_ ) for feature, dtype in features.items()} ) if features is not None else None ) _SCREAMING_SNAKE_CASE = ParquetDatasetReader({"""train""": parquet_path} , features=SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_datasetdict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @pytest.mark.parametrize("""split""" , [None, NamedSplit("""train""" ), """train""", """test"""] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" if split: _SCREAMING_SNAKE_CASE = {split: parquet_path} else: _SCREAMING_SNAKE_CASE = """train""" _SCREAMING_SNAKE_CASE = {"""train""": parquet_path, """test""": parquet_path} _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} _SCREAMING_SNAKE_CASE = ParquetDatasetReader(SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_datasetdict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE = ParquetDatasetWriter(SCREAMING_SNAKE_CASE_ , tmp_path / """foo.parquet""" ) assert writer.write() > 0 _SCREAMING_SNAKE_CASE = pq.ParquetFile(tmp_path / """foo.parquet""" ) _SCREAMING_SNAKE_CASE = pf.read() assert dataset.data.table == output_table def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = str(shared_datadir / """test_image_rgb.jpg""" ) _SCREAMING_SNAKE_CASE = {"""image""": [image_path]} _SCREAMING_SNAKE_CASE = Features({"""image""": Image()} ) _SCREAMING_SNAKE_CASE = Dataset.from_dict(SCREAMING_SNAKE_CASE_ , features=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = ParquetDatasetWriter(SCREAMING_SNAKE_CASE_ , tmp_path / """foo.parquet""" ) assert writer.write() > 0 _SCREAMING_SNAKE_CASE = Dataset.from_parquet(str(tmp_path / """foo.parquet""" ) ) assert dataset.features == reloaded_dataset.features _SCREAMING_SNAKE_CASE = ParquetDatasetReader(str(tmp_path / """foo.parquet""" ) , streaming=SCREAMING_SNAKE_CASE_ ).read() assert dataset.features == reloaded_iterable_dataset.features @pytest.mark.parametrize( """feature, expected""" , [ (Features({"""foo""": Value("""int32""" )} ), None), (Features({"""image""": Image(), """foo""": Value("""int32""" )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS), (Features({"""nested""": Sequence(Audio() )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS), ] , ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" assert get_writer_batch_size(SCREAMING_SNAKE_CASE_ ) == expected
0
0
'''simple docstring''' import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( MobileViTConfig, MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase__ : Dict = logging.get_logger(__name__) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE = MobileViTConfig() # size of the architecture if "mobilevit_s" in mobilevit_name: _SCREAMING_SNAKE_CASE = [1_44, 1_92, 2_40] _SCREAMING_SNAKE_CASE = [16, 32, 64, 96, 1_28, 1_60, 6_40] elif "mobilevit_xs" in mobilevit_name: _SCREAMING_SNAKE_CASE = [96, 1_20, 1_44] _SCREAMING_SNAKE_CASE = [16, 32, 48, 64, 80, 96, 3_84] elif "mobilevit_xxs" in mobilevit_name: _SCREAMING_SNAKE_CASE = [64, 80, 96] _SCREAMING_SNAKE_CASE = [16, 16, 24, 48, 64, 80, 3_20] _SCREAMING_SNAKE_CASE = 0.05 _SCREAMING_SNAKE_CASE = 2.0 if mobilevit_name.startswith("""deeplabv3_""" ): _SCREAMING_SNAKE_CASE = 5_12 _SCREAMING_SNAKE_CASE = 16 _SCREAMING_SNAKE_CASE = 21 _SCREAMING_SNAKE_CASE = """pascal-voc-id2label.json""" else: _SCREAMING_SNAKE_CASE = 10_00 _SCREAMING_SNAKE_CASE = """imagenet-1k-id2label.json""" _SCREAMING_SNAKE_CASE = """huggingface/label-files""" _SCREAMING_SNAKE_CASE = json.load(open(hf_hub_download(__UpperCamelCase , __UpperCamelCase , repo_type="""dataset""" ) , """r""" ) ) _SCREAMING_SNAKE_CASE = {int(__UpperCamelCase ): 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 lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=False ) -> Any: """simple docstring""" for i in range(1 , 6 ): if F"layer_{i}." in name: _SCREAMING_SNAKE_CASE = name.replace(F"layer_{i}." , F"encoder.layer.{i - 1}." ) if "conv_1." in name: _SCREAMING_SNAKE_CASE = name.replace("""conv_1.""" , """conv_stem.""" ) if ".block." in name: _SCREAMING_SNAKE_CASE = name.replace(""".block.""" , """.""" ) if "exp_1x1" in name: _SCREAMING_SNAKE_CASE = name.replace("""exp_1x1""" , """expand_1x1""" ) if "red_1x1" in name: _SCREAMING_SNAKE_CASE = name.replace("""red_1x1""" , """reduce_1x1""" ) if ".local_rep.conv_3x3." in name: _SCREAMING_SNAKE_CASE = name.replace(""".local_rep.conv_3x3.""" , """.conv_kxk.""" ) if ".local_rep.conv_1x1." in name: _SCREAMING_SNAKE_CASE = name.replace(""".local_rep.conv_1x1.""" , """.conv_1x1.""" ) if ".norm." in name: _SCREAMING_SNAKE_CASE = name.replace(""".norm.""" , """.normalization.""" ) if ".conv." in name: _SCREAMING_SNAKE_CASE = name.replace(""".conv.""" , """.convolution.""" ) if ".conv_proj." in name: _SCREAMING_SNAKE_CASE = name.replace(""".conv_proj.""" , """.conv_projection.""" ) for i in range(0 , 2 ): for j in range(0 , 4 ): if F".{i}.{j}." in name: _SCREAMING_SNAKE_CASE = name.replace(F".{i}.{j}." , F".{i}.layer.{j}." ) for i in range(2 , 6 ): for j in range(0 , 4 ): if F".{i}.{j}." in name: _SCREAMING_SNAKE_CASE = name.replace(F".{i}.{j}." , F".{i}." ) if "expand_1x1" in name: _SCREAMING_SNAKE_CASE = name.replace("""expand_1x1""" , """downsampling_layer.expand_1x1""" ) if "conv_3x3" in name: _SCREAMING_SNAKE_CASE = name.replace("""conv_3x3""" , """downsampling_layer.conv_3x3""" ) if "reduce_1x1" in name: _SCREAMING_SNAKE_CASE = name.replace("""reduce_1x1""" , """downsampling_layer.reduce_1x1""" ) for i in range(2 , 5 ): if F".global_rep.{i}.weight" in name: _SCREAMING_SNAKE_CASE = name.replace(F".global_rep.{i}.weight" , """.layernorm.weight""" ) if F".global_rep.{i}.bias" in name: _SCREAMING_SNAKE_CASE = name.replace(F".global_rep.{i}.bias" , """.layernorm.bias""" ) if ".global_rep." in name: _SCREAMING_SNAKE_CASE = name.replace(""".global_rep.""" , """.transformer.""" ) if ".pre_norm_mha.0." in name: _SCREAMING_SNAKE_CASE = name.replace(""".pre_norm_mha.0.""" , """.layernorm_before.""" ) if ".pre_norm_mha.1.out_proj." in name: _SCREAMING_SNAKE_CASE = name.replace(""".pre_norm_mha.1.out_proj.""" , """.attention.output.dense.""" ) if ".pre_norm_ffn.0." in name: _SCREAMING_SNAKE_CASE = name.replace(""".pre_norm_ffn.0.""" , """.layernorm_after.""" ) if ".pre_norm_ffn.1." in name: _SCREAMING_SNAKE_CASE = name.replace(""".pre_norm_ffn.1.""" , """.intermediate.dense.""" ) if ".pre_norm_ffn.4." in name: _SCREAMING_SNAKE_CASE = name.replace(""".pre_norm_ffn.4.""" , """.output.dense.""" ) if ".transformer." in name: _SCREAMING_SNAKE_CASE = name.replace(""".transformer.""" , """.transformer.layer.""" ) if ".aspp_layer." in name: _SCREAMING_SNAKE_CASE = name.replace(""".aspp_layer.""" , """.""" ) if ".aspp_pool." in name: _SCREAMING_SNAKE_CASE = name.replace(""".aspp_pool.""" , """.""" ) if "seg_head." in name: _SCREAMING_SNAKE_CASE = name.replace("""seg_head.""" , """segmentation_head.""" ) if "segmentation_head.classifier.classifier." in name: _SCREAMING_SNAKE_CASE = name.replace("""segmentation_head.classifier.classifier.""" , """segmentation_head.classifier.""" ) if "classifier.fc." in name: _SCREAMING_SNAKE_CASE = name.replace("""classifier.fc.""" , """classifier.""" ) elif (not base_model) and ("segmentation_head." not in name): _SCREAMING_SNAKE_CASE = """mobilevit.""" + name return name def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=False ) -> str: """simple docstring""" if base_model: _SCREAMING_SNAKE_CASE = """""" else: _SCREAMING_SNAKE_CASE = """mobilevit.""" for key in orig_state_dict.copy().keys(): _SCREAMING_SNAKE_CASE = orig_state_dict.pop(__UpperCamelCase ) if key[:8] == "encoder.": _SCREAMING_SNAKE_CASE = key[8:] if "qkv" in key: _SCREAMING_SNAKE_CASE = key.split(""".""" ) _SCREAMING_SNAKE_CASE = int(key_split[0][6:] ) - 1 _SCREAMING_SNAKE_CASE = int(key_split[3] ) _SCREAMING_SNAKE_CASE = model.get_submodule(F"{model_prefix}encoder.layer.{layer_num}" ) _SCREAMING_SNAKE_CASE = layer.transformer.layer[transformer_num].attention.attention.all_head_size _SCREAMING_SNAKE_CASE = ( F"{model_prefix}encoder.layer.{layer_num}.transformer.layer.{transformer_num}.attention.attention." ) if "weight" in key: _SCREAMING_SNAKE_CASE = val[:dim, :] _SCREAMING_SNAKE_CASE = val[dim : dim * 2, :] _SCREAMING_SNAKE_CASE = val[-dim:, :] else: _SCREAMING_SNAKE_CASE = val[:dim] _SCREAMING_SNAKE_CASE = val[dim : dim * 2] _SCREAMING_SNAKE_CASE = val[-dim:] else: _SCREAMING_SNAKE_CASE = val return orig_state_dict def lowerCAmelCase_ ( ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE = """http://images.cocodataset.org/val2017/000000039769.jpg""" _SCREAMING_SNAKE_CASE = Image.open(requests.get(__UpperCamelCase , stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=False ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE = get_mobilevit_config(__UpperCamelCase ) # load original state_dict _SCREAMING_SNAKE_CASE = torch.load(__UpperCamelCase , map_location="""cpu""" ) # load 🤗 model if mobilevit_name.startswith("""deeplabv3_""" ): _SCREAMING_SNAKE_CASE = MobileViTForSemanticSegmentation(__UpperCamelCase ).eval() else: _SCREAMING_SNAKE_CASE = MobileViTForImageClassification(__UpperCamelCase ).eval() _SCREAMING_SNAKE_CASE = convert_state_dict(__UpperCamelCase , __UpperCamelCase ) model.load_state_dict(__UpperCamelCase ) # Check outputs on an image, prepared by MobileViTImageProcessor _SCREAMING_SNAKE_CASE = MobileViTImageProcessor(crop_size=config.image_size , size=config.image_size + 32 ) _SCREAMING_SNAKE_CASE = image_processor(images=prepare_img() , return_tensors="""pt""" ) _SCREAMING_SNAKE_CASE = model(**__UpperCamelCase ) _SCREAMING_SNAKE_CASE = outputs.logits if mobilevit_name.startswith("""deeplabv3_""" ): assert logits.shape == (1, 21, 32, 32) if mobilevit_name == "deeplabv3_mobilevit_s": _SCREAMING_SNAKE_CASE = torch.tensor( [ [[6.2065, 6.1292, 6.2070], [6.1079, 6.1254, 6.1747], [6.0042, 6.1071, 6.1034]], [[-6.9253, -6.8653, -7.0398], [-7.3218, -7.3983, -7.3670], [-7.1961, -7.2482, -7.1569]], [[-4.4723, -4.4348, -4.3769], [-5.3629, -5.4632, -5.4598], [-5.1587, -5.3402, -5.5059]], ] ) elif mobilevit_name == "deeplabv3_mobilevit_xs": _SCREAMING_SNAKE_CASE = torch.tensor( [ [[5.4449, 5.5733, 5.6314], [5.1815, 5.3930, 5.5963], [5.1656, 5.4333, 5.4853]], [[-9.4423, -9.7766, -9.6714], [-9.1581, -9.5720, -9.5519], [-9.1006, -9.6458, -9.5703]], [[-7.7721, -7.3716, -7.1583], [-8.4599, -8.0624, -7.7944], [-8.4172, -7.8366, -7.5025]], ] ) elif mobilevit_name == "deeplabv3_mobilevit_xxs": _SCREAMING_SNAKE_CASE = torch.tensor( [ [[6.9811, 6.9743, 7.3123], [7.1777, 7.1931, 7.3938], [7.5633, 7.8050, 7.8901]], [[-10.5536, -10.2332, -10.2924], [-10.2336, -9.8624, -9.5964], [-10.8840, -10.8158, -10.6659]], [[-3.4938, -3.0631, -2.8620], [-3.4205, -2.8135, -2.6875], [-3.4179, -2.7945, -2.8750]], ] ) else: raise ValueError(F"Unknown mobilevit_name: {mobilevit_name}" ) assert torch.allclose(logits[0, :3, :3, :3] , __UpperCamelCase , atol=1e-4 ) else: assert logits.shape == (1, 10_00) if mobilevit_name == "mobilevit_s": _SCREAMING_SNAKE_CASE = torch.tensor([-0.9866, 0.2392, -1.1241] ) elif mobilevit_name == "mobilevit_xs": _SCREAMING_SNAKE_CASE = torch.tensor([-2.4761, -0.9399, -1.9587] ) elif mobilevit_name == "mobilevit_xxs": _SCREAMING_SNAKE_CASE = torch.tensor([-1.9364, -1.2327, -0.4653] ) else: raise ValueError(F"Unknown mobilevit_name: {mobilevit_name}" ) assert torch.allclose(logits[0, :3] , __UpperCamelCase , atol=1e-4 ) Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) print(F"Saving model {mobilevit_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(__UpperCamelCase ) print(F"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(__UpperCamelCase ) if push_to_hub: _SCREAMING_SNAKE_CASE = { """mobilevit_s""": """mobilevit-small""", """mobilevit_xs""": """mobilevit-x-small""", """mobilevit_xxs""": """mobilevit-xx-small""", """deeplabv3_mobilevit_s""": """deeplabv3-mobilevit-small""", """deeplabv3_mobilevit_xs""": """deeplabv3-mobilevit-x-small""", """deeplabv3_mobilevit_xxs""": """deeplabv3-mobilevit-xx-small""", } print("""Pushing to the hub...""" ) _SCREAMING_SNAKE_CASE = model_mapping[mobilevit_name] image_processor.push_to_hub(__UpperCamelCase , organization="""apple""" ) model.push_to_hub(__UpperCamelCase , organization="""apple""" ) if __name__ == "__main__": UpperCamelCase__ : Any = argparse.ArgumentParser() # Required parameters parser.add_argument( "--mobilevit_name", default="mobilevit_s", type=str, help=( "Name of the MobileViT model you\'d like to convert. Should be one of \'mobilevit_s\', \'mobilevit_xs\'," " \'mobilevit_xxs\', \'deeplabv3_mobilevit_s\', \'deeplabv3_mobilevit_xs\', \'deeplabv3_mobilevit_xxs\'." ), ) parser.add_argument( "--checkpoint_path", required=True, type=str, help="Path to the original state dict (.pt file)." ) parser.add_argument( "--pytorch_dump_folder_path", required=True, type=str, help="Path to the output PyTorch model directory." ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether or not to push the converted model to the 🤗 hub." ) UpperCamelCase__ : str = parser.parse_args() convert_movilevit_checkpoint( args.mobilevit_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
720
'''simple docstring''' def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): raise ValueError("""multiplicative_persistence() only accepts integral values""" ) if num < 0: raise ValueError("""multiplicative_persistence() does not accept negative values""" ) _SCREAMING_SNAKE_CASE = 0 _SCREAMING_SNAKE_CASE = str(SCREAMING_SNAKE_CASE_ ) while len(SCREAMING_SNAKE_CASE_ ) != 1: _SCREAMING_SNAKE_CASE = [int(SCREAMING_SNAKE_CASE_ ) for i in num_string] _SCREAMING_SNAKE_CASE = 1 for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) ): total *= numbers[i] _SCREAMING_SNAKE_CASE = str(SCREAMING_SNAKE_CASE_ ) steps += 1 return steps def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): raise ValueError("""additive_persistence() only accepts integral values""" ) if num < 0: raise ValueError("""additive_persistence() does not accept negative values""" ) _SCREAMING_SNAKE_CASE = 0 _SCREAMING_SNAKE_CASE = str(SCREAMING_SNAKE_CASE_ ) while len(SCREAMING_SNAKE_CASE_ ) != 1: _SCREAMING_SNAKE_CASE = [int(SCREAMING_SNAKE_CASE_ ) for i in num_string] _SCREAMING_SNAKE_CASE = 0 for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) ): total += numbers[i] _SCREAMING_SNAKE_CASE = str(SCREAMING_SNAKE_CASE_ ) steps += 1 return steps if __name__ == "__main__": import doctest doctest.testmod()
0
0
'''simple docstring''' from __future__ import annotations from typing import Any def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> List[str]: """simple docstring""" create_state_space_tree(snake_case__ , [] , 0 ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" if index == len(snake_case__ ): print(snake_case__ ) return create_state_space_tree(snake_case__ , snake_case__ , index + 1 ) current_subsequence.append(sequence[index] ) create_state_space_tree(snake_case__ , snake_case__ , index + 1 ) current_subsequence.pop() if __name__ == "__main__": UpperCamelCase__ : Any = [3, 1, 2, 4] generate_all_subsequences(seq) seq.clear() seq.extend(["A", "B", "C"]) generate_all_subsequences(seq)
721
'''simple docstring''' import math import os import re import sys import unittest from pathlib import Path from typing import Tuple from unittest.mock import patch from parameterized import parameterized from transformers.testing_utils import ( CaptureStderr, ExtendSysPath, TestCasePlus, execute_subprocess_async, get_gpu_count, get_torch_dist_unique_port, require_apex, require_bitsandbytes, require_fairscale, require_torch, require_torch_gpu, require_torch_multi_gpu, require_torch_non_multi_gpu, slow, ) from transformers.trainer_callback import TrainerState from transformers.trainer_utils import set_seed UpperCamelCase__ : Tuple = os.path.abspath(os.path.dirname(__file__)) with ExtendSysPath(f"""{bindir}/../../examples/pytorch/translation"""): from run_translation import main # noqa set_seed(42) UpperCamelCase__ : Union[str, Any] = "sshleifer/student_marian_en_ro_6_1" UpperCamelCase__ : str = "sshleifer/tiny-mbart" @require_torch class _a (_lowerCamelCase): """simple docstring""" def UpperCamelCase ( self , A__=False , A__=None , A__=True , A__=True , A__=True , A__=True , ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = self.run_trainer( eval_steps=1 , max_len=12 , model_name=A__ , num_train_epochs=1 , distributed=A__ , extra_args_str=A__ , predict_with_generate=A__ , do_train=A__ , do_eval=A__ , do_predict=A__ , ) _SCREAMING_SNAKE_CASE = TrainerState.load_from_json(os.path.join(A__ , """trainer_state.json""" ) ).log_history if not do_eval: return _SCREAMING_SNAKE_CASE = [log for log in logs if """eval_loss""" in log.keys()] _SCREAMING_SNAKE_CASE = eval_metrics[0] if predict_with_generate: assert "eval_bleu" in first_step_stats _SCREAMING_SNAKE_CASE = eval_metrics[-1] assert isinstance(last_step_stats["""eval_bleu"""] , A__ ) assert not math.isnan(float(last_step_stats["""eval_loss"""] ) ), "eval_loss must not be `nan`" @require_torch_non_multi_gpu def UpperCamelCase ( self ) -> Optional[int]: self.run_seqaseq_quick() @require_torch_multi_gpu def UpperCamelCase ( self ) -> Optional[Any]: self.run_seqaseq_quick(distributed=A__ ) @require_torch_multi_gpu def UpperCamelCase ( self ) -> Union[str, Any]: self.run_seqaseq_quick(distributed=A__ ) @unittest.skip("""Requires an update of the env running those tests""" ) @require_torch_multi_gpu @require_fairscale def UpperCamelCase ( self ) -> Any: self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--sharded_ddp simple""" ) @unittest.skip("""Requires an update of the env running those tests""" ) @require_torch_multi_gpu @require_fairscale def UpperCamelCase ( self ) -> Tuple: self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--sharded_ddp simple --fp16""" ) @unittest.skip("""Requires an update of the env running those tests""" ) @require_torch_multi_gpu @require_fairscale def UpperCamelCase ( self ) -> str: self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--sharded_ddp zero_dp_2""" , predict_with_generate=A__ ) @unittest.skip("""Requires an update of the env running those tests""" ) @require_torch_multi_gpu @require_fairscale def UpperCamelCase ( self ) -> List[str]: self.run_seqaseq_quick( distributed=A__ , extra_args_str="""--sharded_ddp zero_dp_2 --fp16""" , predict_with_generate=A__ ) @require_apex @require_torch_gpu def UpperCamelCase ( self ) -> Optional[Any]: # XXX: apex breaks the trainer if it's run twice e.g. run_seq2seq.main() from the same # program and it breaks other tests that run from the same pytest worker, therefore until this is # sorted out it must be run only in an external program, that is distributed=True in this # test and only under one or more gpus - if we want cpu will need to make a special test # # specifically to the problem traced it to self.optimizer.step() - if it's run 2nd time via # 2nd main() call it botches the future eval. # self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--fp16 --fp16_backend=apex""" ) # test 2nd time - was getting eval_loss': nan' # to reproduce the problem set distributed=False self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--fp16 --fp16_backend=apex""" ) @parameterized.expand(["""base""", """low""", """high""", """mixed"""] ) @require_torch_multi_gpu def UpperCamelCase ( self , A__ ) -> List[Any]: # as each sub-test is slow-ish split into multiple sub-tests to avoid CI timeout _SCREAMING_SNAKE_CASE = { # test with the default log_level - should be info and thus log info once """base""": {"""extra_args_str""": """""", """n_matches""": 1}, # test with low log_level and log_level_replica - should be noisy on all processes # now the info string should appear twice on 2 processes """low""": {"""extra_args_str""": """--log_level debug --log_level_replica debug""", """n_matches""": 2}, # test with high log_level and low log_level_replica # now the info string should appear once only on the replica """high""": {"""extra_args_str""": """--log_level error --log_level_replica debug""", """n_matches""": 1}, # test with high log_level and log_level_replica - should be quiet on all processes """mixed""": {"""extra_args_str""": """--log_level error --log_level_replica error""", """n_matches""": 0}, } _SCREAMING_SNAKE_CASE = experiments[experiment_id] _SCREAMING_SNAKE_CASE = {"""distributed""": True, """predict_with_generate""": False, """do_eval""": False, """do_predict""": False} _SCREAMING_SNAKE_CASE = """Running training""" with CaptureStderr() as cl: self.run_seqaseq_quick(**A__ , extra_args_str=data["""extra_args_str"""] ) _SCREAMING_SNAKE_CASE = len(re.findall(A__ , cl.err ) ) self.assertEqual(A__ , data["""n_matches"""] ) @slow def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = self.run_trainer( eval_steps=2 , max_len=1_28 , model_name=A__ , learning_rate=3E-4 , num_train_epochs=10 , distributed=A__ , ) # Check metrics _SCREAMING_SNAKE_CASE = TrainerState.load_from_json(os.path.join(A__ , """trainer_state.json""" ) ).log_history _SCREAMING_SNAKE_CASE = [log for log in logs if """eval_loss""" in log.keys()] _SCREAMING_SNAKE_CASE = eval_metrics[0] _SCREAMING_SNAKE_CASE = eval_metrics[-1] assert first_step_stats["eval_loss"] > last_step_stats["eval_loss"], "model learned nothing" assert isinstance(last_step_stats["""eval_bleu"""] , A__ ) # test if do_predict saves generations and metrics _SCREAMING_SNAKE_CASE = os.listdir(A__ ) _SCREAMING_SNAKE_CASE = {os.path.basename(A__ ) for p in contents} assert "generated_predictions.txt" in contents assert "predict_results.json" in contents @slow @require_bitsandbytes def UpperCamelCase ( self ) -> Dict: from transformers.training_args import OptimizerNames def train_and_return_metrics(A__ ) -> Tuple[int, float]: _SCREAMING_SNAKE_CASE = """--skip_memory_metrics 0""" _SCREAMING_SNAKE_CASE = self.run_trainer( max_len=1_28 , model_name=A__ , learning_rate=3E-4 , num_train_epochs=1 , optim=A__ , distributed=A__ , extra_args_str=A__ , do_eval=A__ , do_predict=A__ , n_gpus_to_use=1 , ) # Check metrics _SCREAMING_SNAKE_CASE = TrainerState.load_from_json(Path(A__ , """trainer_state.json""" ) ).log_history _SCREAMING_SNAKE_CASE = int(logs[0]["""train_mem_gpu_peaked_delta"""] / 2**20 ) _SCREAMING_SNAKE_CASE = int(logs[0]["""train_mem_gpu_alloc_delta"""] / 2**20 ) _SCREAMING_SNAKE_CASE = logs[0]["""train_loss"""] return gpu_peak_mem_mb, gpu_alloc_mem_mb, loss _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = train_and_return_metrics(OptimizerNames.ADAMW_TORCH.value ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = train_and_return_metrics(OptimizerNames.ADAMW_BNB.value ) _SCREAMING_SNAKE_CASE = gpu_alloc_mem_orig - gpu_alloc_mem_bnb _SCREAMING_SNAKE_CASE = gpu_peak_mem_orig + gpu_alloc_mem_orig _SCREAMING_SNAKE_CASE = gpu_peak_mem_bnb + gpu_alloc_mem_bnb _SCREAMING_SNAKE_CASE = gpu_total_mem_orig - gpu_total_mem_bnb # sshleifer/student_marian_en_ro_6_1 has 54M parameter, 29M of which is `nn.Embedding` which # doesn't get quantized and remains in fp32. Therefore we only have 25M parameters quantized # in 2 bytes and the diff in optim memory usage is derived as so: # # - normal 25*8=~200MB (8 bytes per param) # - bnb 25*2= ~50MB (2 bytes per param) # # Thus we should expect ~150MB total memory saved. # # Peak memory should be the same - the total should be different by about that same margin # # After leaving a small margin to accommodate for differences between gpus let's check # that we have at least 120MB in savings _SCREAMING_SNAKE_CASE = 1_20 # uncomment the following if this test starts failing - requires py38 for a new print feature # gpu_peak_mem_diff = gpu_peak_mem_orig - gpu_peak_mem_bnb # print(f"{gpu_alloc_mem_orig=}MB {gpu_peak_mem_orig=}MB {gpu_alloc_mem_orig+gpu_peak_mem_orig=}MB") # print(f" {gpu_alloc_mem_bnb=}MB {gpu_peak_mem_bnb=}MB {gpu_alloc_mem_bnb+gpu_peak_mem_bnb=}MB") # print(f"{gpu_alloc_mem_diff=}MB") # print(f"{gpu_peak_mem_diff=}MB") # print(f"{gpu_total_mem_orig=}MB, {gpu_total_mem_bnb=}MB") # print(f"{gpu_total_mem_diff=}MB, {gpu_total_mem_diff=}MB") self.assertGreater( A__ , A__ , """should use ~150MB less alloc gpu memory with BNB, compared to without it for this model but got""" F" a difference of {gpu_alloc_mem_diff}MB, with gpu_alloc_mem_orig={gpu_alloc_mem_orig}MB and" F" gpu_alloc_mem_bnb={gpu_alloc_mem_bnb}MB" , ) self.assertGreater( A__ , A__ , """should use ~150MB less total gpu memory with BNB, compared to without it for this model but got""" F" a difference of {gpu_total_mem_diff}MB, with gpu_total_mem_orig={gpu_total_mem_orig}MB and" F" gpu_total_mem_bnb={gpu_total_mem_bnb}MB" , ) self.assertEqual( A__ , A__ , F"loss should be the same, but got loss_orig={loss_orig}, loss_bnb={loss_bnb}" ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ = 3E-3 , A__ = "adafactor" , A__ = False , A__ = None , A__ = 0 , A__ = True , A__ = True , A__ = True , A__ = True , A__ = None , ) -> Dict: _SCREAMING_SNAKE_CASE = self.test_file_dir / """../fixtures/tests_samples/wmt_en_ro""" _SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() _SCREAMING_SNAKE_CASE = F"\n --model_name_or_path {model_name}\n --train_file {data_dir}/train.json\n --validation_file {data_dir}/val.json\n --test_file {data_dir}/test.json\n --output_dir {output_dir}\n --overwrite_output_dir\n --max_train_samples 8\n --max_source_length {max_len}\n --max_target_length {max_len}\n --do_train\n --num_train_epochs {str(A__ )}\n --per_device_train_batch_size 4\n --learning_rate {learning_rate}\n --warmup_steps 8\n --logging_steps 0\n --logging_strategy no\n --save_steps {str(A__ )}\n --group_by_length\n --label_smoothing_factor 0.1\n --target_lang ro_RO\n --source_lang en_XX\n ".split() _SCREAMING_SNAKE_CASE = F"\n --do_eval\n --per_device_eval_batch_size 4\n --max_eval_samples 8\n --val_max_target_length {max_len}\n --evaluation_strategy steps\n --eval_steps {str(A__ )}\n ".split() _SCREAMING_SNAKE_CASE = """ --do_predict """.split() _SCREAMING_SNAKE_CASE = [] if do_train: args += args_train if do_eval: args += args_eval if do_predict: args += args_predict if predict_with_generate: args += "--predict_with_generate".split() if do_train: if optim == "adafactor": args += "--adafactor".split() else: args += F"--optim {optim}".split() if extra_args_str is not None: args += extra_args_str.split() if distributed: if n_gpus_to_use is None: _SCREAMING_SNAKE_CASE = get_gpu_count() _SCREAMING_SNAKE_CASE = get_torch_dist_unique_port() _SCREAMING_SNAKE_CASE = F"\n -m torch.distributed.run\n --nproc_per_node={n_gpus_to_use}\n --master_port={master_port}\n {self.examples_dir_str}/pytorch/translation/run_translation.py\n ".split() _SCREAMING_SNAKE_CASE = [sys.executable] + distributed_args + args # keep for quick debug # print(" ".join([f"\nPYTHONPATH={self.src_dir_str}"] +cmd)); die execute_subprocess_async(A__ , env=self.get_env() ) else: _SCREAMING_SNAKE_CASE = ["""run_translation.py"""] + args with patch.object(A__ , """argv""" , A__ ): main() return output_dir
0
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available UpperCamelCase__ : int = {"tokenization_herbert": ["HerbertTokenizer"]} try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Union[str, Any] = ["HerbertTokenizerFast"] if TYPE_CHECKING: from .tokenization_herbert import HerbertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_herbert_fast import HerbertTokenizerFast else: import sys UpperCamelCase__ : Optional[int] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
700
'''simple docstring''' import sys UpperCamelCase__ : int = ( "73167176531330624919225119674426574742355349194934" "96983520312774506326239578318016984801869478851843" "85861560789112949495459501737958331952853208805511" "12540698747158523863050715693290963295227443043557" "66896648950445244523161731856403098711121722383113" "62229893423380308135336276614282806444486645238749" "30358907296290491560440772390713810515859307960866" "70172427121883998797908792274921901699720888093776" "65727333001053367881220235421809751254540594752243" "52584907711670556013604839586446706324415722155397" "53697817977846174064955149290862569321978468622482" "83972241375657056057490261407972968652414535100474" "82166370484403199890008895243450658541227588666881" "16427171479924442928230863465674813919123162824586" "17866458359124566529476545682848912883142607690042" "24219022671055626321111109370544217506941658960408" "07198403850962455444362981230987879927244284909188" "84580156166097919133875499200524063689912560717606" "05886116467109405077541002256983155200055935729725" "71636269561882670428252483600823257530420752963450" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ = N ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE = -sys.maxsize - 1 for i in range(len(SCREAMING_SNAKE_CASE_ ) - 12 ): _SCREAMING_SNAKE_CASE = 1 for j in range(13 ): product *= int(n[i + j] ) if product > largest_product: _SCREAMING_SNAKE_CASE = product return largest_product if __name__ == "__main__": print(f"""{solution() = }""")
0
0
'''simple docstring''' import os def lowerCAmelCase_ ( ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE = os.path.dirname(os.path.realpath(SCREAMING_SNAKE_CASE_ ) ) _SCREAMING_SNAKE_CASE = os.path.join(SCREAMING_SNAKE_CASE_ , """triangle.txt""" ) with open(SCREAMING_SNAKE_CASE_ ) as f: _SCREAMING_SNAKE_CASE = f.readlines() _SCREAMING_SNAKE_CASE = [] for line in triangle: _SCREAMING_SNAKE_CASE = [] for number in line.strip().split(""" """ ): numbers_from_line.append(int(SCREAMING_SNAKE_CASE_ ) ) a.append(SCREAMING_SNAKE_CASE_ ) for i in range(1 , len(SCREAMING_SNAKE_CASE_ ) ): for j in range(len(a[i] ) ): _SCREAMING_SNAKE_CASE = a[i - 1][j] if j != len(a[i - 1] ) else 0 _SCREAMING_SNAKE_CASE = a[i - 1][j - 1] if j > 0 else 0 a[i][j] += max(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return max(a[-1] ) if __name__ == "__main__": print(solution())
701
'''simple docstring''' UpperCamelCase__ : Dict = { "a": "AAAAA", "b": "AAAAB", "c": "AAABA", "d": "AAABB", "e": "AABAA", "f": "AABAB", "g": "AABBA", "h": "AABBB", "i": "ABAAA", "j": "BBBAA", "k": "ABAAB", "l": "ABABA", "m": "ABABB", "n": "ABBAA", "o": "ABBAB", "p": "ABBBA", "q": "ABBBB", "r": "BAAAA", "s": "BAAAB", "t": "BAABA", "u": "BAABB", "v": "BBBAB", "w": "BABAA", "x": "BABAB", "y": "BABBA", "z": "BABBB", " ": " ", } UpperCamelCase__ : str = {value: key for key, value in encode_dict.items()} def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE = """""" for letter in word.lower(): if letter.isalpha() or letter == " ": encoded += encode_dict[letter] else: raise Exception("""encode() accepts only letters of the alphabet and spaces""" ) return encoded def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" if set(SCREAMING_SNAKE_CASE_ ) - {"A", "B", " "} != set(): raise Exception("""decode() accepts only 'A', 'B' and spaces""" ) _SCREAMING_SNAKE_CASE = """""" for word in coded.split(): while len(SCREAMING_SNAKE_CASE_ ) != 0: decoded += decode_dict[word[:5]] _SCREAMING_SNAKE_CASE = word[5:] decoded += " " return decoded.strip() if __name__ == "__main__": from doctest import testmod testmod()
0
0
'''simple docstring''' import argparse import os import jax as jnp import numpy as onp import torch import torch.nn as nn from music_spectrogram_diffusion import inference from tax import checkpoints from diffusers import DDPMScheduler, OnnxRuntimeModel, SpectrogramDiffusionPipeline from diffusers.pipelines.spectrogram_diffusion import SpectrogramContEncoder, SpectrogramNotesEncoder, TaFilmDecoder UpperCamelCase__ : List[str] = """base_with_context""" def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(weights["""token_embedder"""]["""embedding"""] ) ) _SCREAMING_SNAKE_CASE = nn.Parameter( torch.FloatTensor(weights["""Embed_0"""]["""embedding"""] ) , requires_grad=snake_case_ ) for lyr_num, lyr in enumerate(model.encoders ): _SCREAMING_SNAKE_CASE = weights[F"layers_{lyr_num}"] _SCREAMING_SNAKE_CASE = nn.Parameter( torch.FloatTensor(ly_weight["""pre_attention_layer_norm"""]["""scale"""] ) ) _SCREAMING_SNAKE_CASE = ly_weight["attention"] _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(attention_weights["""query"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(attention_weights["""key"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(attention_weights["""value"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(attention_weights["""out"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(ly_weight["""pre_mlp_layer_norm"""]["""scale"""] ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(ly_weight["""mlp"""]["""wi_0"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(ly_weight["""mlp"""]["""wi_1"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(ly_weight["""mlp"""]["""wo"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(weights["""encoder_norm"""]["""scale"""] ) ) return model def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(weights["""input_proj"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter( torch.FloatTensor(weights["""Embed_0"""]["""embedding"""] ) , requires_grad=snake_case_ ) for lyr_num, lyr in enumerate(model.encoders ): _SCREAMING_SNAKE_CASE = weights[F"layers_{lyr_num}"] _SCREAMING_SNAKE_CASE = ly_weight["attention"] _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(attention_weights["""query"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(attention_weights["""key"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(attention_weights["""value"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(attention_weights["""out"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter( torch.FloatTensor(ly_weight["""pre_attention_layer_norm"""]["""scale"""] ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(ly_weight["""mlp"""]["""wi_0"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(ly_weight["""mlp"""]["""wi_1"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(ly_weight["""mlp"""]["""wo"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(ly_weight["""pre_mlp_layer_norm"""]["""scale"""] ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(weights["""encoder_norm"""]["""scale"""] ) ) return model def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(weights["""time_emb_dense0"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(weights["""time_emb_dense1"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter( torch.FloatTensor(weights["""Embed_0"""]["""embedding"""] ) , requires_grad=snake_case_ ) _SCREAMING_SNAKE_CASE = nn.Parameter( torch.FloatTensor(weights["""continuous_inputs_projection"""]["""kernel"""].T ) ) for lyr_num, lyr in enumerate(model.decoders ): _SCREAMING_SNAKE_CASE = weights[F"layers_{lyr_num}"] _SCREAMING_SNAKE_CASE = nn.Parameter( torch.FloatTensor(ly_weight["""pre_self_attention_layer_norm"""]["""scale"""] ) ) _SCREAMING_SNAKE_CASE = nn.Parameter( torch.FloatTensor(ly_weight["""FiLMLayer_0"""]["""DenseGeneral_0"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = ly_weight["self_attention"] _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(attention_weights["""query"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(attention_weights["""key"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(attention_weights["""value"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(attention_weights["""out"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = ly_weight["MultiHeadDotProductAttention_0"] _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(attention_weights["""query"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(attention_weights["""key"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(attention_weights["""value"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(attention_weights["""out"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter( torch.FloatTensor(ly_weight["""pre_cross_attention_layer_norm"""]["""scale"""] ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(ly_weight["""pre_mlp_layer_norm"""]["""scale"""] ) ) _SCREAMING_SNAKE_CASE = nn.Parameter( torch.FloatTensor(ly_weight["""FiLMLayer_1"""]["""DenseGeneral_0"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(ly_weight["""mlp"""]["""wi_0"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(ly_weight["""mlp"""]["""wi_1"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(ly_weight["""mlp"""]["""wo"""]["""kernel"""].T ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(weights["""decoder_norm"""]["""scale"""] ) ) _SCREAMING_SNAKE_CASE = nn.Parameter(torch.FloatTensor(weights["""spec_out_dense"""]["""kernel"""].T ) ) return model def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE = checkpoints.load_tax_checkpoint(args.checkpoint_path ) _SCREAMING_SNAKE_CASE = jnp.tree_util.tree_map(onp.array , snake_case_ ) _SCREAMING_SNAKE_CASE = [ "from __gin__ import dynamic_registration", "from music_spectrogram_diffusion.models.diffusion import diffusion_utils", "diffusion_utils.ClassifierFreeGuidanceConfig.eval_condition_weight = 2.0", "diffusion_utils.DiffusionConfig.classifier_free_guidance = @diffusion_utils.ClassifierFreeGuidanceConfig()", ] _SCREAMING_SNAKE_CASE = os.path.join(args.checkpoint_path , """..""" , """config.gin""" ) _SCREAMING_SNAKE_CASE = inference.parse_training_gin_file(snake_case_ , snake_case_ ) _SCREAMING_SNAKE_CASE = inference.InferenceModel(args.checkpoint_path , snake_case_ ) _SCREAMING_SNAKE_CASE = DDPMScheduler(beta_schedule="""squaredcos_cap_v2""" , variance_type="""fixed_large""" ) _SCREAMING_SNAKE_CASE = SpectrogramNotesEncoder( max_length=synth_model.sequence_length["""inputs"""] , vocab_size=synth_model.model.module.config.vocab_size , d_model=synth_model.model.module.config.emb_dim , dropout_rate=synth_model.model.module.config.dropout_rate , num_layers=synth_model.model.module.config.num_encoder_layers , num_heads=synth_model.model.module.config.num_heads , d_kv=synth_model.model.module.config.head_dim , d_ff=synth_model.model.module.config.mlp_dim , feed_forward_proj="""gated-gelu""" , ) _SCREAMING_SNAKE_CASE = SpectrogramContEncoder( input_dims=synth_model.audio_codec.n_dims , targets_context_length=synth_model.sequence_length["""targets_context"""] , d_model=synth_model.model.module.config.emb_dim , dropout_rate=synth_model.model.module.config.dropout_rate , num_layers=synth_model.model.module.config.num_encoder_layers , num_heads=synth_model.model.module.config.num_heads , d_kv=synth_model.model.module.config.head_dim , d_ff=synth_model.model.module.config.mlp_dim , feed_forward_proj="""gated-gelu""" , ) _SCREAMING_SNAKE_CASE = TaFilmDecoder( input_dims=synth_model.audio_codec.n_dims , targets_length=synth_model.sequence_length["""targets_context"""] , max_decoder_noise_time=synth_model.model.module.config.max_decoder_noise_time , d_model=synth_model.model.module.config.emb_dim , num_layers=synth_model.model.module.config.num_decoder_layers , num_heads=synth_model.model.module.config.num_heads , d_kv=synth_model.model.module.config.head_dim , d_ff=synth_model.model.module.config.mlp_dim , dropout_rate=synth_model.model.module.config.dropout_rate , ) _SCREAMING_SNAKE_CASE = load_notes_encoder(ta_checkpoint["""target"""]["""token_encoder"""] , snake_case_ ) _SCREAMING_SNAKE_CASE = load_continuous_encoder(ta_checkpoint["""target"""]["""continuous_encoder"""] , snake_case_ ) _SCREAMING_SNAKE_CASE = load_decoder(ta_checkpoint["""target"""]["""decoder"""] , snake_case_ ) _SCREAMING_SNAKE_CASE = OnnxRuntimeModel.from_pretrained("""kashif/soundstream_mel_decoder""" ) _SCREAMING_SNAKE_CASE = SpectrogramDiffusionPipeline( notes_encoder=snake_case_ , continuous_encoder=snake_case_ , decoder=snake_case_ , scheduler=snake_case_ , melgan=snake_case_ , ) if args.save: pipe.save_pretrained(args.output_path ) if __name__ == "__main__": UpperCamelCase__ : List[str] = argparse.ArgumentParser() parser.add_argument("--output_path", default=None, type=str, required=True, help="Path to the converted model.") parser.add_argument( "--save", default=True, type=bool, required=False, help="Whether to save the converted model or not." ) parser.add_argument( "--checkpoint_path", default=f"""{MODEL}/checkpoint_500000""", type=str, required=False, help="Path to the original jax model checkpoint.", ) UpperCamelCase__ : List[str] = parser.parse_args() main(args)
702
'''simple docstring''' import argparse import torch from torch import nn from transformers import MaMaaaConfig, MaMaaaForConditionalGeneration def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE = [ """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 lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = emb.weight.shape _SCREAMING_SNAKE_CASE = nn.Linear(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , bias=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = emb.weight.data return lin_layer def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = torch.load(SCREAMING_SNAKE_CASE_ , map_location="""cpu""" ) _SCREAMING_SNAKE_CASE = mam_aaa["""args"""] or mam_aaa["""cfg"""]["""model"""] _SCREAMING_SNAKE_CASE = mam_aaa["""model"""] remove_ignore_keys_(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = state_dict["""encoder.embed_tokens.weight"""].shape[0] _SCREAMING_SNAKE_CASE = MaMaaaConfig( vocab_size=SCREAMING_SNAKE_CASE_ , max_position_embeddings=10_24 , encoder_layers=args.encoder_layers , decoder_layers=args.decoder_layers , encoder_attention_heads=args.encoder_attention_heads , decoder_attention_heads=args.decoder_attention_heads , encoder_ffn_dim=args.encoder_ffn_embed_dim , decoder_ffn_dim=args.decoder_ffn_embed_dim , d_model=args.encoder_embed_dim , encoder_layerdrop=args.encoder_layerdrop , decoder_layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function="""relu""" , ) _SCREAMING_SNAKE_CASE = state_dict["""decoder.embed_tokens.weight"""] _SCREAMING_SNAKE_CASE = MaMaaaForConditionalGeneration(SCREAMING_SNAKE_CASE_ ) model.model.load_state_dict(SCREAMING_SNAKE_CASE_ , strict=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = make_linear_from_emb(model.model.shared ) return model if __name__ == "__main__": UpperCamelCase__ : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument("fairseq_path", type=str, help="path to a model.pt on local filesystem.") parser.add_argument("pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") UpperCamelCase__ : List[str] = parser.parse_args() UpperCamelCase__ : Any = convert_fairseq_mamaaa_checkpoint_from_disk(args.fairseq_pathß) model.save_pretrained(args.pytorch_dump_folder_path)
0
0
'''simple docstring''' def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE = int(_lowerCamelCase ) # Initialize Result _SCREAMING_SNAKE_CASE = [] # Traverse through all denomination for denomination in reversed(_lowerCamelCase ): # Find denominations while int(_lowerCamelCase ) >= int(_lowerCamelCase ): total_value -= int(_lowerCamelCase ) answer.append(_lowerCamelCase ) # Append the "answers" array return answer # Driver Code if __name__ == "__main__": UpperCamelCase__ : Dict = [] UpperCamelCase__ : Tuple = "0" if ( input("Do you want to enter your denominations ? (yY/n): ").strip().lower() == "y" ): UpperCamelCase__ : str = int(input("Enter the number of denominations you want to add: ").strip()) for i in range(0, n): denominations.append(int(input(f"""Denomination {i}: """).strip())) UpperCamelCase__ : str = input("Enter the change you want to make in Indian Currency: ").strip() else: # All denominations of Indian Currency if user does not enter UpperCamelCase__ : Dict = [1, 2, 5, 10, 20, 50, 100, 500, 2_000] UpperCamelCase__ : Tuple = input("Enter the change you want to make: ").strip() if int(value) == 0 or int(value) < 0: print("The total value cannot be zero or negative.") else: print(f"""Following is minimal change for {value}: """) UpperCamelCase__ : Dict = find_minimum_change(denominations, value) # Print result for i in range(len(answer)): print(answer[i], end=" ")
703
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available UpperCamelCase__ : str = { "configuration_canine": ["CANINE_PRETRAINED_CONFIG_ARCHIVE_MAP", "CanineConfig"], "tokenization_canine": ["CanineTokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : List[Any] = [ "CANINE_PRETRAINED_MODEL_ARCHIVE_LIST", "CanineForMultipleChoice", "CanineForQuestionAnswering", "CanineForSequenceClassification", "CanineForTokenClassification", "CanineLayer", "CanineModel", "CaninePreTrainedModel", "load_tf_weights_in_canine", ] if TYPE_CHECKING: from .configuration_canine import CANINE_PRETRAINED_CONFIG_ARCHIVE_MAP, CanineConfig from .tokenization_canine import CanineTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_canine import ( CANINE_PRETRAINED_MODEL_ARCHIVE_LIST, CanineForMultipleChoice, CanineForQuestionAnswering, CanineForSequenceClassification, CanineForTokenClassification, CanineLayer, CanineModel, CaninePreTrainedModel, load_tf_weights_in_canine, ) else: import sys UpperCamelCase__ : str = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
0
0
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ViTConfig, ViTForImageClassification, ViTImageProcessor, ViTModel from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase__ : Dict = logging.get_logger(__name__) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=False ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"blocks.{i}.norm1.weight", F"vit.encoder.layer.{i}.layernorm_before.weight") ) rename_keys.append((F"blocks.{i}.norm1.bias", F"vit.encoder.layer.{i}.layernorm_before.bias") ) rename_keys.append((F"blocks.{i}.attn.proj.weight", F"vit.encoder.layer.{i}.attention.output.dense.weight") ) rename_keys.append((F"blocks.{i}.attn.proj.bias", F"vit.encoder.layer.{i}.attention.output.dense.bias") ) rename_keys.append((F"blocks.{i}.norm2.weight", F"vit.encoder.layer.{i}.layernorm_after.weight") ) rename_keys.append((F"blocks.{i}.norm2.bias", F"vit.encoder.layer.{i}.layernorm_after.bias") ) rename_keys.append((F"blocks.{i}.mlp.fc1.weight", F"vit.encoder.layer.{i}.intermediate.dense.weight") ) rename_keys.append((F"blocks.{i}.mlp.fc1.bias", F"vit.encoder.layer.{i}.intermediate.dense.bias") ) rename_keys.append((F"blocks.{i}.mlp.fc2.weight", F"vit.encoder.layer.{i}.output.dense.weight") ) rename_keys.append((F"blocks.{i}.mlp.fc2.bias", F"vit.encoder.layer.{i}.output.dense.bias") ) # projection layer + position embeddings rename_keys.extend( [ ("""cls_token""", """vit.embeddings.cls_token"""), ("""patch_embed.proj.weight""", """vit.embeddings.patch_embeddings.projection.weight"""), ("""patch_embed.proj.bias""", """vit.embeddings.patch_embeddings.projection.bias"""), ("""pos_embed""", """vit.embeddings.position_embeddings"""), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ("""norm.weight""", """layernorm.weight"""), ("""norm.bias""", """layernorm.bias"""), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" _SCREAMING_SNAKE_CASE = [(pair[0], pair[1][4:]) if pair[1].startswith("""vit""" ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ("""norm.weight""", """vit.layernorm.weight"""), ("""norm.bias""", """vit.layernorm.bias"""), ("""head.weight""", """classifier.weight"""), ("""head.bias""", """classifier.bias"""), ] ) return rename_keys def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=False ) -> List[str]: """simple docstring""" for i in range(config.num_hidden_layers ): if base_model: _SCREAMING_SNAKE_CASE = """""" else: _SCREAMING_SNAKE_CASE = """vit.""" # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) _SCREAMING_SNAKE_CASE = state_dict.pop(F"blocks.{i}.attn.qkv.weight" ) _SCREAMING_SNAKE_CASE = state_dict.pop(F"blocks.{i}.attn.qkv.bias" ) # next, add query, keys and values (in that order) to the state dict _SCREAMING_SNAKE_CASE = in_proj_weight[ : config.hidden_size, : ] _SCREAMING_SNAKE_CASE = in_proj_bias[: config.hidden_size] _SCREAMING_SNAKE_CASE = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] _SCREAMING_SNAKE_CASE = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] _SCREAMING_SNAKE_CASE = in_proj_weight[ -config.hidden_size :, : ] _SCREAMING_SNAKE_CASE = in_proj_bias[-config.hidden_size :] def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE = ["""head.weight""", """head.bias"""] for k in ignore_keys: state_dict.pop(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = dct.pop(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = val def lowerCAmelCase_ ( ) -> Union[str, 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 @torch.no_grad() def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=True ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = ViTConfig() # patch_size if model_name[-1] == "8": _SCREAMING_SNAKE_CASE = 8 # set labels if required if not base_model: _SCREAMING_SNAKE_CASE = 10_00 _SCREAMING_SNAKE_CASE = """huggingface/label-files""" _SCREAMING_SNAKE_CASE = """imagenet-1k-id2label.json""" _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()} # size of the architecture if model_name in ["dino_vits8", "dino_vits16"]: _SCREAMING_SNAKE_CASE = 3_84 _SCREAMING_SNAKE_CASE = 15_36 _SCREAMING_SNAKE_CASE = 12 _SCREAMING_SNAKE_CASE = 6 # load original model from torch hub _SCREAMING_SNAKE_CASE = torch.hub.load("""facebookresearch/dino:main""" , SCREAMING_SNAKE_CASE_ ) original_model.eval() # load state_dict of original model, remove and rename some keys _SCREAMING_SNAKE_CASE = original_model.state_dict() if base_model: remove_classification_head_(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = create_rename_keys(SCREAMING_SNAKE_CASE_ , base_model=SCREAMING_SNAKE_CASE_ ) for src, dest in rename_keys: rename_key(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) read_in_q_k_v(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # load HuggingFace model if base_model: _SCREAMING_SNAKE_CASE = ViTModel(SCREAMING_SNAKE_CASE_ , add_pooling_layer=SCREAMING_SNAKE_CASE_ ).eval() else: _SCREAMING_SNAKE_CASE = ViTForImageClassification(SCREAMING_SNAKE_CASE_ ).eval() model.load_state_dict(SCREAMING_SNAKE_CASE_ ) # Check outputs on an image, prepared by ViTImageProcessor _SCREAMING_SNAKE_CASE = ViTImageProcessor() _SCREAMING_SNAKE_CASE = image_processor(images=prepare_img() , return_tensors="""pt""" ) _SCREAMING_SNAKE_CASE = encoding["""pixel_values"""] _SCREAMING_SNAKE_CASE = model(SCREAMING_SNAKE_CASE_ ) if base_model: _SCREAMING_SNAKE_CASE = original_model(SCREAMING_SNAKE_CASE_ ) assert torch.allclose(SCREAMING_SNAKE_CASE_ , outputs.last_hidden_state[:, 0, :] , atol=1e-1 ) else: _SCREAMING_SNAKE_CASE = original_model(SCREAMING_SNAKE_CASE_ ) assert logits.shape == outputs.logits.shape assert torch.allclose(SCREAMING_SNAKE_CASE_ , outputs.logits , atol=1e-3 ) Path(SCREAMING_SNAKE_CASE_ ).mkdir(exist_ok=SCREAMING_SNAKE_CASE_ ) print(F"Saving model {model_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(SCREAMING_SNAKE_CASE_ ) print(F"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": UpperCamelCase__ : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default="dino_vitb16", type=str, help="Name of the model trained with DINO you\'d like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) parser.add_argument( "--base_model", action="store_true", help="Whether to only convert the base model (no projection head weights).", ) parser.set_defaults(base_model=True) UpperCamelCase__ : Tuple = parser.parse_args() convert_vit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.base_model)
704
'''simple docstring''' import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class _a (_lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = ['image_processor', 'tokenizer'] SCREAMING_SNAKE_CASE = 'ChineseCLIPImageProcessor' SCREAMING_SNAKE_CASE = ('BertTokenizer', 'BertTokenizerFast') def __init__( self , A__=None , A__=None , **A__ ) -> int: _SCREAMING_SNAKE_CASE = None if "feature_extractor" in kwargs: warnings.warn( """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`""" """ instead.""" , A__ , ) _SCREAMING_SNAKE_CASE = kwargs.pop("""feature_extractor""" ) _SCREAMING_SNAKE_CASE = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("""You need to specify an `image_processor`.""" ) if tokenizer is None: raise ValueError("""You need to specify a `tokenizer`.""" ) super().__init__(A__ , A__ ) _SCREAMING_SNAKE_CASE = self.image_processor def __call__( self , A__=None , A__=None , A__=None , **A__ ) -> Optional[int]: if text is None and images is None: raise ValueError("""You have to specify either text or images. Both cannot be none.""" ) if text is not None: _SCREAMING_SNAKE_CASE = self.tokenizer(A__ , return_tensors=A__ , **A__ ) if images is not None: _SCREAMING_SNAKE_CASE = self.image_processor(A__ , return_tensors=A__ , **A__ ) if text is not None and images is not None: _SCREAMING_SNAKE_CASE = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**A__ ) , tensor_type=A__ ) def UpperCamelCase ( self , *A__ , **A__ ) -> Dict: return self.tokenizer.batch_decode(*A__ , **A__ ) def UpperCamelCase ( self , *A__ , **A__ ) -> Optional[Any]: return self.tokenizer.decode(*A__ , **A__ ) @property def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.tokenizer.model_input_names _SCREAMING_SNAKE_CASE = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def UpperCamelCase ( self ) -> Optional[int]: warnings.warn( """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.""" , A__ , ) return self.image_processor_class
0
0
from typing import List, Optional, TypeVar from .arrow_dataset import Dataset, _concatenate_map_style_datasets, _interleave_map_style_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .info import DatasetInfo from .iterable_dataset import IterableDataset, _concatenate_iterable_datasets, _interleave_iterable_datasets from .splits import NamedSplit from .utils import logging from .utils.py_utils import Literal UpperCamelCase__ : List[Any] = logging.get_logger(__name__) UpperCamelCase__ : str = TypeVar("DatasetType", Dataset, IterableDataset) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = "first_exhausted" , ) -> List[str]: """simple docstring""" from .arrow_dataset import Dataset from .iterable_dataset import IterableDataset if not datasets: raise ValueError("""Unable to interleave an empty list of datasets.""" ) for i, dataset in enumerate(snake_case__ ): if not isinstance(snake_case__ , (Dataset, IterableDataset) ): if isinstance(snake_case__ , (DatasetDict, IterableDatasetDict) ): if not dataset: raise ValueError( F"Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} " """is an empty dataset dictionary.""" ) raise ValueError( F"Dataset at position {i} has at least one split: {list(snake_case__ )}\n" F"Please pick one to interleave with the other datasets, for example: dataset['{next(iter(snake_case__ ) )}']" ) raise ValueError( F"Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(snake_case__ ).__name__}." ) if i == 0: _SCREAMING_SNAKE_CASE = ( (Dataset, IterableDataset) if isinstance(snake_case__ , snake_case__ ) else (IterableDataset, Dataset) ) elif not isinstance(snake_case__ , snake_case__ ): raise ValueError( F"Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects." ) if stopping_strategy not in ["first_exhausted", "all_exhausted"]: raise ValueError(F"{stopping_strategy} is not supported. Please enter a valid stopping_strategy." ) if dataset_type is Dataset: return _interleave_map_style_datasets( snake_case__ , snake_case__ , snake_case__ , info=snake_case__ , split=snake_case__ , stopping_strategy=snake_case__ ) else: return _interleave_iterable_datasets( snake_case__ , snake_case__ , snake_case__ , info=snake_case__ , split=snake_case__ , stopping_strategy=snake_case__ ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = 0 , ) -> int: """simple docstring""" if not dsets: raise ValueError("""Unable to concatenate an empty list of datasets.""" ) for i, dataset in enumerate(snake_case__ ): if not isinstance(snake_case__ , (Dataset, IterableDataset) ): if isinstance(snake_case__ , (DatasetDict, IterableDatasetDict) ): if not dataset: raise ValueError( F"Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} " """is an empty dataset dictionary.""" ) raise ValueError( F"Dataset at position {i} has at least one split: {list(snake_case__ )}\n" F"Please pick one to interleave with the other datasets, for example: dataset['{next(iter(snake_case__ ) )}']" ) raise ValueError( F"Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(snake_case__ ).__name__}." ) if i == 0: _SCREAMING_SNAKE_CASE = ( (Dataset, IterableDataset) if isinstance(snake_case__ , snake_case__ ) else (IterableDataset, Dataset) ) elif not isinstance(snake_case__ , snake_case__ ): raise ValueError( F"Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects." ) if dataset_type is Dataset: return _concatenate_map_style_datasets(snake_case__ , info=snake_case__ , split=snake_case__ , axis=snake_case__ ) else: return _concatenate_iterable_datasets(snake_case__ , info=snake_case__ , split=snake_case__ , axis=snake_case__ )
705
'''simple docstring''' from sklearn.metrics import matthews_corrcoef import datasets UpperCamelCase__ : List[str] = "\nCompute the Matthews correlation coefficient (MCC)\n\nThe Matthews correlation coefficient is used in machine learning as a\nmeasure of the quality of binary and multiclass classifications. It takes\ninto account true and false positives and negatives and is generally\nregarded as a balanced measure which can be used even if the classes are of\nvery different sizes. The MCC is in essence a correlation coefficient value\nbetween -1 and +1. A coefficient of +1 represents a perfect prediction, 0\nan average random prediction and -1 an inverse prediction. The statistic\nis also known as the phi coefficient. [source: Wikipedia]\n" UpperCamelCase__ : List[Any] = "\nArgs:\n predictions (list of int): Predicted labels, as returned by a model.\n references (list of int): Ground truth labels.\n sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`.\nReturns:\n matthews_correlation (dict containing float): Matthews correlation.\nExamples:\n Example 1, a basic example with only predictions and references as inputs:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3])\n >>> print(round(results['matthews_correlation'], 2))\n 0.54\n\n Example 2, the same example as above, but also including sample weights:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 3, 1, 1, 1, 2])\n >>> print(round(results['matthews_correlation'], 2))\n 0.1\n\n Example 3, the same example as above, but with sample weights that cause a negative correlation:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 1, 0, 0, 0, 1])\n >>> print(round(results['matthews_correlation'], 2))\n -0.25\n" UpperCamelCase__ : Any = "\\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class _a (datasets.Metric): """simple docstring""" def UpperCamelCase ( self ) -> Optional[Any]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=[ """https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html""" ] , ) def UpperCamelCase ( self , A__ , A__ , A__=None ) -> List[str]: return { "matthews_correlation": float(matthews_corrcoef(A__ , A__ , sample_weight=A__ ) ), }
0
0
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import BeitConfig, BeitForImageClassification, BeitForMaskedImageModeling, BeitImageProcessor from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase__ : List[Any] = logging.get_logger(__name__) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=False , SCREAMING_SNAKE_CASE_=False ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = """backbone.""" if is_semantic else """""" _SCREAMING_SNAKE_CASE = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"{prefix}blocks.{i}.norm1.weight", F"beit.encoder.layer.{i}.layernorm_before.weight") ) rename_keys.append((F"{prefix}blocks.{i}.norm1.bias", F"beit.encoder.layer.{i}.layernorm_before.bias") ) rename_keys.append( (F"{prefix}blocks.{i}.attn.proj.weight", F"beit.encoder.layer.{i}.attention.output.dense.weight") ) rename_keys.append( (F"{prefix}blocks.{i}.attn.proj.bias", F"beit.encoder.layer.{i}.attention.output.dense.bias") ) rename_keys.append((F"{prefix}blocks.{i}.norm2.weight", F"beit.encoder.layer.{i}.layernorm_after.weight") ) rename_keys.append((F"{prefix}blocks.{i}.norm2.bias", F"beit.encoder.layer.{i}.layernorm_after.bias") ) rename_keys.append((F"{prefix}blocks.{i}.mlp.fc1.weight", F"beit.encoder.layer.{i}.intermediate.dense.weight") ) rename_keys.append((F"{prefix}blocks.{i}.mlp.fc1.bias", F"beit.encoder.layer.{i}.intermediate.dense.bias") ) rename_keys.append((F"{prefix}blocks.{i}.mlp.fc2.weight", F"beit.encoder.layer.{i}.output.dense.weight") ) rename_keys.append((F"{prefix}blocks.{i}.mlp.fc2.bias", F"beit.encoder.layer.{i}.output.dense.bias") ) # projection layer + position embeddings rename_keys.extend( [ (F"{prefix}cls_token", """beit.embeddings.cls_token"""), (F"{prefix}patch_embed.proj.weight", """beit.embeddings.patch_embeddings.projection.weight"""), (F"{prefix}patch_embed.proj.bias", """beit.embeddings.patch_embeddings.projection.bias"""), (F"{prefix}pos_embed", """beit.embeddings.position_embeddings"""), ] ) if has_lm_head: # mask token + layernorm rename_keys.extend( [ ("""mask_token""", """beit.embeddings.mask_token"""), ("""norm.weight""", """layernorm.weight"""), ("""norm.bias""", """layernorm.bias"""), ] ) else: # layernorm + classification head rename_keys.extend( [ ("""fc_norm.weight""", """beit.pooler.layernorm.weight"""), ("""fc_norm.bias""", """beit.pooler.layernorm.bias"""), ("""head.weight""", """classifier.weight"""), ("""head.bias""", """classifier.bias"""), ] ) return rename_keys def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=False , SCREAMING_SNAKE_CASE_=False ) -> Optional[int]: """simple docstring""" for i in range(config.num_hidden_layers ): _SCREAMING_SNAKE_CASE = """backbone.""" if is_semantic else """""" # queries, keys and values _SCREAMING_SNAKE_CASE = state_dict.pop(F"{prefix}blocks.{i}.attn.qkv.weight" ) _SCREAMING_SNAKE_CASE = state_dict.pop(F"{prefix}blocks.{i}.attn.q_bias" ) _SCREAMING_SNAKE_CASE = state_dict.pop(F"{prefix}blocks.{i}.attn.v_bias" ) _SCREAMING_SNAKE_CASE = in_proj_weight[ : config.hidden_size, : ] _SCREAMING_SNAKE_CASE = q_bias _SCREAMING_SNAKE_CASE = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] _SCREAMING_SNAKE_CASE = in_proj_weight[ -config.hidden_size :, : ] _SCREAMING_SNAKE_CASE = v_bias # gamma_1 and gamma_2 # we call them lambda because otherwise they are renamed when using .from_pretrained _SCREAMING_SNAKE_CASE = state_dict.pop(F"{prefix}blocks.{i}.gamma_1" ) _SCREAMING_SNAKE_CASE = state_dict.pop(F"{prefix}blocks.{i}.gamma_2" ) _SCREAMING_SNAKE_CASE = gamma_a _SCREAMING_SNAKE_CASE = gamma_a def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE = dct.pop(__snake_case ) _SCREAMING_SNAKE_CASE = val def lowerCAmelCase_ ( ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = """http://images.cocodataset.org/val2017/000000039769.jpg""" _SCREAMING_SNAKE_CASE = Image.open(requests.get(__snake_case , stream=__snake_case ).raw ) return im @torch.no_grad() def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=False ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = False if """rvlcdip""" in checkpoint_url else True _SCREAMING_SNAKE_CASE = BeitConfig(use_absolute_position_embeddings=__snake_case , use_mask_token=__snake_case ) # size of the architecture if "large" in checkpoint_url or "dit-l" in checkpoint_url: _SCREAMING_SNAKE_CASE = 10_24 _SCREAMING_SNAKE_CASE = 40_96 _SCREAMING_SNAKE_CASE = 24 _SCREAMING_SNAKE_CASE = 16 # labels if "rvlcdip" in checkpoint_url: _SCREAMING_SNAKE_CASE = 16 _SCREAMING_SNAKE_CASE = """huggingface/label-files""" _SCREAMING_SNAKE_CASE = """rvlcdip-id2label.json""" _SCREAMING_SNAKE_CASE = json.load(open(hf_hub_download(__snake_case , __snake_case , repo_type="""dataset""" ) , """r""" ) ) _SCREAMING_SNAKE_CASE = {int(__snake_case ): v for k, v in idalabel.items()} _SCREAMING_SNAKE_CASE = idalabel _SCREAMING_SNAKE_CASE = {v: k for k, v in idalabel.items()} # load state_dict of original model, remove and rename some keys _SCREAMING_SNAKE_CASE = torch.hub.load_state_dict_from_url(__snake_case , map_location="""cpu""" )["""model"""] _SCREAMING_SNAKE_CASE = create_rename_keys(__snake_case , has_lm_head=__snake_case ) for src, dest in rename_keys: rename_key(__snake_case , __snake_case , __snake_case ) read_in_q_k_v(__snake_case , __snake_case , has_lm_head=__snake_case ) # load HuggingFace model _SCREAMING_SNAKE_CASE = BeitForMaskedImageModeling(__snake_case ) if has_lm_head else BeitForImageClassification(__snake_case ) model.eval() model.load_state_dict(__snake_case ) # Check outputs on an image _SCREAMING_SNAKE_CASE = BeitImageProcessor( size=config.image_size , resample=PILImageResampling.BILINEAR , do_center_crop=__snake_case ) _SCREAMING_SNAKE_CASE = prepare_img() _SCREAMING_SNAKE_CASE = image_processor(images=__snake_case , return_tensors="""pt""" ) _SCREAMING_SNAKE_CASE = encoding["""pixel_values"""] _SCREAMING_SNAKE_CASE = model(__snake_case ) _SCREAMING_SNAKE_CASE = outputs.logits # verify logits _SCREAMING_SNAKE_CASE = [1, 16] if """rvlcdip""" in checkpoint_url else [1, 1_96, 81_92] assert logits.shape == torch.Size(__snake_case ), "Shape of logits not as expected" Path(__snake_case ).mkdir(exist_ok=__snake_case ) print(F"Saving model to {pytorch_dump_folder_path}" ) model.save_pretrained(__snake_case ) print(F"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(__snake_case ) if push_to_hub: if has_lm_head: _SCREAMING_SNAKE_CASE = """dit-base""" if """base""" in checkpoint_url else """dit-large""" else: _SCREAMING_SNAKE_CASE = """dit-base-finetuned-rvlcdip""" if """dit-b""" in checkpoint_url else """dit-large-finetuned-rvlcdip""" image_processor.push_to_hub( repo_path_or_name=Path(__snake_case , __snake_case ) , organization="""nielsr""" , commit_message="""Add image processor""" , use_temp_dir=__snake_case , ) model.push_to_hub( repo_path_or_name=Path(__snake_case , __snake_case ) , organization="""nielsr""" , commit_message="""Add model""" , use_temp_dir=__snake_case , ) if __name__ == "__main__": UpperCamelCase__ : str = argparse.ArgumentParser() parser.add_argument( "--checkpoint_url", default="https://layoutlm.blob.core.windows.net/dit/dit-pts/dit-base-224-p16-500k-62d53a.pth", type=str, help="URL to the original PyTorch checkpoint (.pth file).", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the folder to output PyTorch model." ) parser.add_argument( "--push_to_hub", action="store_true", ) UpperCamelCase__ : List[str] = parser.parse_args() convert_dit_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub)
706
'''simple docstring''' from __future__ import annotations def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Tuple: """simple docstring""" print(F"Vertex\tShortest Distance from vertex {src}" ) for i, d in enumerate(SCREAMING_SNAKE_CASE_ ): print(F"{i}\t\t{d}" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" for j in range(SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = (graph[j][k] for k in ["""src""", """dst""", """weight"""]) if distance[u] != float("""inf""" ) and distance[u] + w < distance[v]: return True return False def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> list[float]: """simple docstring""" _SCREAMING_SNAKE_CASE = [float("""inf""" )] * vertex_count _SCREAMING_SNAKE_CASE = 0.0 for _ in range(vertex_count - 1 ): for j in range(SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = (graph[j][k] for k in ["""src""", """dst""", """weight"""]) if distance[u] != float("""inf""" ) and distance[u] + w < distance[v]: _SCREAMING_SNAKE_CASE = distance[u] + w _SCREAMING_SNAKE_CASE = check_negative_cycle(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if negative_cycle_exists: raise Exception("""Negative cycle found""" ) return distance if __name__ == "__main__": import doctest doctest.testmod() UpperCamelCase__ : int = int(input("Enter number of vertices: ").strip()) UpperCamelCase__ : int = int(input("Enter number of edges: ").strip()) UpperCamelCase__ : list[dict[str, int]] = [{} for _ in range(E)] for i in range(E): print("Edge ", i + 1) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ : Dict = ( int(x) for x in input("Enter source, destination, weight: ").strip().split(" ") ) UpperCamelCase__ : Optional[Any] = {"src": src, "dst": dest, "weight": weight} UpperCamelCase__ : Optional[Any] = int(input("\nEnter shortest path source:").strip()) UpperCamelCase__ : Any = bellman_ford(graph, V, E, source) print_distance(shortest_distance, 0)
0
0
'''simple docstring''' from __future__ import annotations UpperCamelCase__ : str = [-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0] UpperCamelCase__ : List[Any] = [-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1] def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> list[float]: """simple docstring""" _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = len(UpperCAmelCase__ ) for i in range(UpperCAmelCase__ ): _SCREAMING_SNAKE_CASE = -1 for j in range(i + 1 , UpperCAmelCase__ ): if arr[i] < arr[j]: _SCREAMING_SNAKE_CASE = arr[j] break result.append(UpperCAmelCase__ ) return result def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> list[float]: """simple docstring""" _SCREAMING_SNAKE_CASE = [] for i, outer in enumerate(UpperCAmelCase__ ): _SCREAMING_SNAKE_CASE = -1 for inner in arr[i + 1 :]: if outer < inner: _SCREAMING_SNAKE_CASE = inner break result.append(UpperCAmelCase__ ) return result def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> list[float]: """simple docstring""" _SCREAMING_SNAKE_CASE = len(UpperCAmelCase__ ) _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = [-1] * arr_size for index in reversed(range(UpperCAmelCase__ ) ): if stack: while stack[-1] <= arr[index]: stack.pop() if not stack: break if stack: _SCREAMING_SNAKE_CASE = stack[-1] stack.append(arr[index] ) return result if __name__ == "__main__": from doctest import testmod from timeit import timeit testmod() print(next_greatest_element_slow(arr)) print(next_greatest_element_fast(arr)) print(next_greatest_element(arr)) UpperCamelCase__ : List[Any] = ( "from __main__ import arr, next_greatest_element_slow, " "next_greatest_element_fast, next_greatest_element" ) print( "next_greatest_element_slow():", timeit("next_greatest_element_slow(arr)", setup=setup), ) print( "next_greatest_element_fast():", timeit("next_greatest_element_fast(arr)", setup=setup), ) print( " next_greatest_element():", timeit("next_greatest_element(arr)", setup=setup), )
707
'''simple docstring''' 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 , A__ , A__=13 , A__=7 , A__=True , A__=True , A__=True , A__=True , A__=99 , A__=32 , A__=2 , A__=4 , A__=37 , A__="gelu" , A__=0.1 , A__=0.1 , A__=5_12 , A__=16 , A__=2 , A__=0.02 , A__=3 , A__=4 , A__=None , ) -> int: _SCREAMING_SNAKE_CASE = parent _SCREAMING_SNAKE_CASE = 13 _SCREAMING_SNAKE_CASE = 7 _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = 99 _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 = 5_12 _SCREAMING_SNAKE_CASE = 16 _SCREAMING_SNAKE_CASE = 2 _SCREAMING_SNAKE_CASE = 0.02 _SCREAMING_SNAKE_CASE = 3 _SCREAMING_SNAKE_CASE = 4 _SCREAMING_SNAKE_CASE = None def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _SCREAMING_SNAKE_CASE = None if self.use_input_mask: _SCREAMING_SNAKE_CASE = random_attention_mask([self.batch_size, self.seq_length] ) _SCREAMING_SNAKE_CASE = None if self.use_token_type_ids: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None if self.use_labels: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.num_choices ) _SCREAMING_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=A__ , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = TFRoFormerModel(config=A__ ) _SCREAMING_SNAKE_CASE = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} _SCREAMING_SNAKE_CASE = [input_ids, input_mask] _SCREAMING_SNAKE_CASE = model(A__ ) _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> str: _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = TFRoFormerForCausalLM(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ )["""logits"""] self.parent.assertListEqual( list(prediction_scores.numpy().shape ) , [self.batch_size, self.seq_length, self.vocab_size] ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> Dict: _SCREAMING_SNAKE_CASE = TFRoFormerForMaskedLM(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> List[str]: _SCREAMING_SNAKE_CASE = self.num_labels _SCREAMING_SNAKE_CASE = TFRoFormerForSequenceClassification(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> Any: _SCREAMING_SNAKE_CASE = self.num_choices _SCREAMING_SNAKE_CASE = TFRoFormerForMultipleChoice(config=A__ ) _SCREAMING_SNAKE_CASE = tf.tile(tf.expand_dims(A__ , 1 ) , (1, self.num_choices, 1) ) _SCREAMING_SNAKE_CASE = tf.tile(tf.expand_dims(A__ , 1 ) , (1, self.num_choices, 1) ) _SCREAMING_SNAKE_CASE = tf.tile(tf.expand_dims(A__ , 1 ) , (1, self.num_choices, 1) ) _SCREAMING_SNAKE_CASE = { """input_ids""": multiple_choice_inputs_ids, """attention_mask""": multiple_choice_input_mask, """token_type_ids""": multiple_choice_token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> List[str]: _SCREAMING_SNAKE_CASE = self.num_labels _SCREAMING_SNAKE_CASE = TFRoFormerForTokenClassification(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> Tuple: _SCREAMING_SNAKE_CASE = TFRoFormerForQuestionAnswering(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = 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 ) , ) = config_and_inputs _SCREAMING_SNAKE_CASE = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask} return config, inputs_dict @require_tf class _a (_lowerCamelCase , _lowerCamelCase , unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = ( ( TFRoFormerModel, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerForMultipleChoice, ) if is_tf_available() else () ) SCREAMING_SNAKE_CASE = ( { '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 {} ) SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = False def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ ) -> str: if pipeline_test_casse_name == "TextGenerationPipelineTests": return True return False def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = TFRoFormerModelTester(self ) _SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=A__ , hidden_size=37 ) def UpperCamelCase ( self ) -> Optional[Any]: self.config_tester.run_common_tests() def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A__ ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*A__ ) def UpperCamelCase ( self ) -> int: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head(*A__ ) def UpperCamelCase ( self ) -> Dict: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*A__ ) def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*A__ ) def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*A__ ) def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*A__ ) @slow def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = TFRoFormerModel.from_pretrained("""junnyu/roformer_chinese_base""" ) self.assertIsNotNone(A__ ) @require_tf class _a (unittest.TestCase): """simple docstring""" @slow def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = TFRoFormerForMaskedLM.from_pretrained("""junnyu/roformer_chinese_base""" ) _SCREAMING_SNAKE_CASE = tf.constant([[0, 1, 2, 3, 4, 5]] ) _SCREAMING_SNAKE_CASE = model(A__ )[0] # TODO Replace vocab size _SCREAMING_SNAKE_CASE = 5_00_00 _SCREAMING_SNAKE_CASE = [1, 6, vocab_size] self.assertEqual(output.shape , A__ ) print(output[:, :3, :3] ) # TODO Replace values below with what was printed above. _SCREAMING_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] , A__ , atol=1E-4 ) @require_tf class _a (unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = 1E-4 def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = tf.constant([[4, 10]] ) _SCREAMING_SNAKE_CASE = TFRoFormerSinusoidalPositionalEmbedding(num_positions=6 , embedding_dim=6 ) _SCREAMING_SNAKE_CASE = emba(input_ids.shape ) _SCREAMING_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(A__ , A__ , atol=self.tolerance ) def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_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], ] ) _SCREAMING_SNAKE_CASE = TFRoFormerSinusoidalPositionalEmbedding(num_positions=5_12 , embedding_dim=5_12 ) emba([2, 16, 5_12] ) _SCREAMING_SNAKE_CASE = emba.weight[:3, :5] tf.debugging.assert_near(A__ , A__ , atol=self.tolerance ) @require_tf class _a (unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = 1E-4 def UpperCamelCase ( self ) -> int: # 2,12,16,64 _SCREAMING_SNAKE_CASE = tf.reshape(tf.range(2 * 12 * 16 * 64 , dtype=tf.floataa ) , shape=(2, 12, 16, 64) ) / 1_00 _SCREAMING_SNAKE_CASE = -tf.reshape(tf.range(2 * 12 * 16 * 64 , dtype=tf.floataa ) , shape=(2, 12, 16, 64) ) / 1_00 _SCREAMING_SNAKE_CASE = TFRoFormerSinusoidalPositionalEmbedding(num_positions=32 , embedding_dim=64 ) _SCREAMING_SNAKE_CASE = embed_positions([2, 16, 7_68] )[None, None, :, :] _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = TFRoFormerSelfAttention.apply_rotary_position_embeddings( A__ , A__ , A__ ) _SCREAMING_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], ] ) _SCREAMING_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] , A__ , atol=self.tolerance ) tf.debugging.assert_near(key_layer[0, 0, :6, :8] , A__ , atol=self.tolerance )
0
0
'''simple docstring''' def lowerCAmelCase_ ( ) -> Any: """simple docstring""" for n in range(1 , 1_00_00_00 ): yield n * (n + 1) // 2 def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE = 1 _SCREAMING_SNAKE_CASE = 2 while i * i <= n: _SCREAMING_SNAKE_CASE = 0 while n % i == 0: n //= i multiplicity += 1 divisors_count *= multiplicity + 1 i += 1 if n > 1: divisors_count *= 2 return divisors_count def lowerCAmelCase_ ( ) -> str: """simple docstring""" return next(i for i in triangle_number_generator() if count_divisors(_lowercase ) > 5_00 ) if __name__ == "__main__": print(solution())
708
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available UpperCamelCase__ : int = {"tokenization_herbert": ["HerbertTokenizer"]} try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Tuple = ["HerbertTokenizerFast"] if TYPE_CHECKING: from .tokenization_herbert import HerbertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_herbert_fast import HerbertTokenizerFast else: import sys UpperCamelCase__ : Tuple = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
0
0
'''simple docstring''' import time from dataclasses import dataclass from multiprocessing import Pool from unittest import TestCase from unittest.mock import patch import multiprocess import numpy as np import pytest from datasets.utils.py_utils import ( NestedDataStructure, asdict, iflatmap_unordered, map_nested, temp_seed, temporary_assignment, zip_dict, ) from .utils import require_tf, require_torch def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Tuple: # picklable for multiprocessing """simple docstring""" return x.sum() def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> List[Any]: # picklable for multiprocessing """simple docstring""" return i + 1 @dataclass class _a : """simple docstring""" SCREAMING_SNAKE_CASE = 42 SCREAMING_SNAKE_CASE = 42 class _a (UpperCAmelCase__): """simple docstring""" def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = {} _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = 1 _SCREAMING_SNAKE_CASE = [1, 2] _SCREAMING_SNAKE_CASE = {"""a""": 1, """b""": 2} _SCREAMING_SNAKE_CASE = {"""a""": [1, 2], """b""": [3, 4]} _SCREAMING_SNAKE_CASE = {"""a""": {"""1""": 1}, """b""": 2} _SCREAMING_SNAKE_CASE = {"""a""": 1, """b""": 2, """c""": 3, """d""": 4} _SCREAMING_SNAKE_CASE = {} _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = 2 _SCREAMING_SNAKE_CASE = [2, 3] _SCREAMING_SNAKE_CASE = {"""a""": 2, """b""": 3} _SCREAMING_SNAKE_CASE = {"""a""": [2, 3], """b""": [4, 5]} _SCREAMING_SNAKE_CASE = {"""a""": {"""1""": 2}, """b""": 3} _SCREAMING_SNAKE_CASE = {"""a""": 2, """b""": 3, """c""": 4, """d""": 5} self.assertEqual(map_nested(__lowerCAmelCase , __lowerCAmelCase ) , __lowerCAmelCase ) self.assertEqual(map_nested(__lowerCAmelCase , __lowerCAmelCase ) , __lowerCAmelCase ) self.assertEqual(map_nested(__lowerCAmelCase , __lowerCAmelCase ) , __lowerCAmelCase ) self.assertEqual(map_nested(__lowerCAmelCase , __lowerCAmelCase ) , __lowerCAmelCase ) self.assertEqual(map_nested(__lowerCAmelCase , __lowerCAmelCase ) , __lowerCAmelCase ) self.assertEqual(map_nested(__lowerCAmelCase , __lowerCAmelCase ) , __lowerCAmelCase ) self.assertEqual(map_nested(__lowerCAmelCase , __lowerCAmelCase ) , __lowerCAmelCase ) self.assertEqual(map_nested(__lowerCAmelCase , __lowerCAmelCase ) , __lowerCAmelCase ) _SCREAMING_SNAKE_CASE = 2 self.assertEqual(map_nested(__lowerCAmelCase , __lowerCAmelCase , num_proc=__lowerCAmelCase ) , __lowerCAmelCase ) self.assertEqual(map_nested(__lowerCAmelCase , __lowerCAmelCase , num_proc=__lowerCAmelCase ) , __lowerCAmelCase ) self.assertEqual(map_nested(__lowerCAmelCase , __lowerCAmelCase , num_proc=__lowerCAmelCase ) , __lowerCAmelCase ) self.assertEqual(map_nested(__lowerCAmelCase , __lowerCAmelCase , num_proc=__lowerCAmelCase ) , __lowerCAmelCase ) self.assertEqual(map_nested(__lowerCAmelCase , __lowerCAmelCase , num_proc=__lowerCAmelCase ) , __lowerCAmelCase ) self.assertEqual(map_nested(__lowerCAmelCase , __lowerCAmelCase , num_proc=__lowerCAmelCase ) , __lowerCAmelCase ) self.assertEqual(map_nested(__lowerCAmelCase , __lowerCAmelCase , num_proc=__lowerCAmelCase ) , __lowerCAmelCase ) self.assertEqual(map_nested(__lowerCAmelCase , __lowerCAmelCase , num_proc=__lowerCAmelCase ) , __lowerCAmelCase ) _SCREAMING_SNAKE_CASE = {"""a""": np.eye(2 ), """b""": np.zeros(3 ), """c""": np.ones(2 )} _SCREAMING_SNAKE_CASE = {"""a""": 2, """b""": 0, """c""": 2} _SCREAMING_SNAKE_CASE = { """a""": np.eye(2 ).astype(__lowerCAmelCase ), """b""": np.zeros(3 ).astype(__lowerCAmelCase ), """c""": np.ones(2 ).astype(__lowerCAmelCase ), } self.assertEqual(map_nested(__lowerCAmelCase , __lowerCAmelCase , map_numpy=__lowerCAmelCase ) , __lowerCAmelCase ) self.assertEqual( {k: v.tolist() for k, v in map_nested(__lowerCAmelCase , __lowerCAmelCase , map_numpy=__lowerCAmelCase ).items()} , {k: v.tolist() for k, v in expected_map_nested_sna_int.items()} , ) self.assertEqual(map_nested(__lowerCAmelCase , __lowerCAmelCase , map_numpy=__lowerCAmelCase , num_proc=__lowerCAmelCase ) , __lowerCAmelCase ) self.assertEqual( {k: v.tolist() for k, v in map_nested(__lowerCAmelCase , __lowerCAmelCase , map_numpy=__lowerCAmelCase , num_proc=__lowerCAmelCase ).items()} , {k: v.tolist() for k, v in expected_map_nested_sna_int.items()} , ) with self.assertRaises(__lowerCAmelCase ): # can't pickle a local lambda map_nested(lambda A__ : x + 1 , __lowerCAmelCase , num_proc=__lowerCAmelCase ) def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = {"""a""": 1, """b""": 2} _SCREAMING_SNAKE_CASE = {"""a""": 3, """b""": 4} _SCREAMING_SNAKE_CASE = {"""a""": 5, """b""": 6} _SCREAMING_SNAKE_CASE = sorted([("""a""", (1, 3, 5)), ("""b""", (2, 4, 6))] ) self.assertEqual(sorted(zip_dict(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) ) , __lowerCAmelCase ) def UpperCamelCase ( self ) -> Dict: class _a : """simple docstring""" SCREAMING_SNAKE_CASE = 'bar' _SCREAMING_SNAKE_CASE = Foo() self.assertEqual(foo.my_attr , """bar""" ) with temporary_assignment(__lowerCAmelCase , """my_attr""" , """BAR""" ): self.assertEqual(foo.my_attr , """BAR""" ) self.assertEqual(foo.my_attr , """bar""" ) @pytest.mark.parametrize( """iterable_length, num_proc, expected_num_proc""" , [ (1, None, 1), (1, 1, 1), (2, None, 1), (2, 1, 1), (2, 2, 1), (2, 3, 1), (3, 2, 1), (16, 16, 16), (16, 17, 16), (17, 16, 16), ] , ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" with patch("""datasets.utils.py_utils._single_map_nested""" ) as mock_single_map_nested, patch( """datasets.parallel.parallel.Pool""" ) as mock_multiprocessing_pool: _SCREAMING_SNAKE_CASE = {F"{i}": i for i in range(UpperCAmelCase__ )} _SCREAMING_SNAKE_CASE = map_nested(lambda SCREAMING_SNAKE_CASE_ : x + 10 , UpperCAmelCase__ , num_proc=UpperCAmelCase__ , parallel_min_length=16 ) if expected_num_proc == 1: assert mock_single_map_nested.called assert not mock_multiprocessing_pool.called else: assert not mock_single_map_nested.called assert mock_multiprocessing_pool.called assert mock_multiprocessing_pool.call_args[0][0] == expected_num_proc class _a (UpperCAmelCase__): """simple docstring""" @require_tf def UpperCamelCase ( self ) -> Optional[Any]: import tensorflow as tf from tensorflow.keras import layers _SCREAMING_SNAKE_CASE = layers.Dense(2 ) def gen_random_output(): _SCREAMING_SNAKE_CASE = tf.random.uniform((1, 3) ) return model(__lowerCAmelCase ).numpy() with temp_seed(42 , set_tensorflow=__lowerCAmelCase ): _SCREAMING_SNAKE_CASE = gen_random_output() with temp_seed(42 , set_tensorflow=__lowerCAmelCase ): _SCREAMING_SNAKE_CASE = gen_random_output() _SCREAMING_SNAKE_CASE = gen_random_output() np.testing.assert_equal(__lowerCAmelCase , __lowerCAmelCase ) self.assertGreater(np.abs(outa - outa ).sum() , 0 ) @require_torch def UpperCamelCase ( self ) -> Tuple: import torch def gen_random_output(): _SCREAMING_SNAKE_CASE = torch.nn.Linear(3 , 2 ) _SCREAMING_SNAKE_CASE = torch.rand(1 , 3 ) return model(__lowerCAmelCase ).detach().numpy() with temp_seed(42 , set_pytorch=__lowerCAmelCase ): _SCREAMING_SNAKE_CASE = gen_random_output() with temp_seed(42 , set_pytorch=__lowerCAmelCase ): _SCREAMING_SNAKE_CASE = gen_random_output() _SCREAMING_SNAKE_CASE = gen_random_output() np.testing.assert_equal(__lowerCAmelCase , __lowerCAmelCase ) self.assertGreater(np.abs(outa - outa ).sum() , 0 ) def UpperCamelCase ( self ) -> List[Any]: def gen_random_output(): return np.random.rand(1 , 3 ) with temp_seed(42 ): _SCREAMING_SNAKE_CASE = gen_random_output() with temp_seed(42 ): _SCREAMING_SNAKE_CASE = gen_random_output() _SCREAMING_SNAKE_CASE = gen_random_output() np.testing.assert_equal(__lowerCAmelCase , __lowerCAmelCase ) self.assertGreater(np.abs(outa - outa ).sum() , 0 ) @pytest.mark.parametrize("""input_data""" , [{}] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = NestedDataStructure(UpperCAmelCase__ ).data assert output_data == input_data @pytest.mark.parametrize( """data, expected_output""" , [ ({}, []), ([], []), ("""foo""", ["""foo"""]), (["""foo""", """bar"""], ["""foo""", """bar"""]), ([["""foo""", """bar"""]], ["""foo""", """bar"""]), ([[["""foo"""], ["""bar"""]]], ["""foo""", """bar"""]), ([[["""foo"""], """bar"""]], ["""foo""", """bar"""]), ({"""a""": 1, """b""": 2}, [1, 2]), ({"""a""": [1, 2], """b""": [3, 4]}, [1, 2, 3, 4]), ({"""a""": [[1, 2]], """b""": [[3, 4]]}, [1, 2, 3, 4]), ({"""a""": [[1, 2]], """b""": [3, 4]}, [1, 2, 3, 4]), ({"""a""": [[[1], [2]]], """b""": [[[3], [4]]]}, [1, 2, 3, 4]), ({"""a""": [[[1], [2]]], """b""": [[3, 4]]}, [1, 2, 3, 4]), ({"""a""": [[[1], [2]]], """b""": [3, 4]}, [1, 2, 3, 4]), ({"""a""": [[[1], [2]]], """b""": [3, [4]]}, [1, 2, 3, 4]), ({"""a""": {"""1""": 1}, """b""": 2}, [1, 2]), ({"""a""": {"""1""": [1]}, """b""": 2}, [1, 2]), ({"""a""": {"""1""": [1]}, """b""": [2]}, [1, 2]), ] , ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE = NestedDataStructure(UpperCAmelCase__ ).flatten() assert output == expected_output def lowerCAmelCase_ ( ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE = A(x=1 , y="""foobar""" ) _SCREAMING_SNAKE_CASE = {"""x""": 1, """y""": """foobar"""} assert asdict(UpperCAmelCase__ ) == expected_output _SCREAMING_SNAKE_CASE = {"""a""": {"""b""": A(x=10 , y="""foo""" )}, """c""": [A(x=20 , y="""bar""" )]} _SCREAMING_SNAKE_CASE = {"""a""": {"""b""": {"""x""": 10, """y""": """foo"""}}, """c""": [{"""x""": 20, """y""": """bar"""}]} assert asdict(UpperCAmelCase__ ) == expected_output with pytest.raises(UpperCAmelCase__ ): asdict([1, A(x=10 , y="""foo""" )] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" return text.split() def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Any: """simple docstring""" yield (time.time(), content) time.sleep(2 ) yield (time.time(), content) def lowerCAmelCase_ ( ) -> List[str]: """simple docstring""" with Pool(2 ) as pool: _SCREAMING_SNAKE_CASE = list(iflatmap_unordered(UpperCAmelCase__ , _split_text , kwargs_iterable=[{"""text""": """hello there"""}] * 10 ) ) assert out.count("""hello""" ) == 10 assert out.count("""there""" ) == 10 assert len(UpperCAmelCase__ ) == 20 # check multiprocess from pathos (uses dill for pickling) with multiprocess.Pool(2 ) as pool: _SCREAMING_SNAKE_CASE = list(iflatmap_unordered(UpperCAmelCase__ , _split_text , kwargs_iterable=[{"""text""": """hello there"""}] * 10 ) ) assert out.count("""hello""" ) == 10 assert out.count("""there""" ) == 10 assert len(UpperCAmelCase__ ) == 20 # check that we get items as fast as possible with Pool(2 ) as pool: _SCREAMING_SNAKE_CASE = [] for yield_time, content in iflatmap_unordered( UpperCAmelCase__ , _aseconds_generator_of_aitems_with_timing , kwargs_iterable=[{"""content""": """a"""}, {"""content""": """b"""}] ): assert yield_time < time.time() + 0.1, "we should each item directly after it was yielded" out.append(UpperCAmelCase__ ) assert out.count("""a""" ) == 2 assert out.count("""b""" ) == 2 assert len(UpperCAmelCase__ ) == 4
709
'''simple docstring''' import argparse import gdown import numpy as np import torch from huggingface_hub import hf_hub_download from transformers import ( CLIPTokenizer, CLIPTokenizerFast, VideoMAEImageProcessor, XCLIPConfig, XCLIPModel, XCLIPProcessor, XCLIPTextConfig, XCLIPVisionConfig, ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE = XCLIPTextConfig() # derive patch size from model name _SCREAMING_SNAKE_CASE = model_name.find("""patch""" ) _SCREAMING_SNAKE_CASE = int(model_name[start_idx + len("""patch""" ) : start_idx + len("""patch""" ) + 2] ) _SCREAMING_SNAKE_CASE = XCLIPVisionConfig(patch_size=SCREAMING_SNAKE_CASE_ , num_frames=SCREAMING_SNAKE_CASE_ ) if "large" in model_name: _SCREAMING_SNAKE_CASE = 7_68 _SCREAMING_SNAKE_CASE = 30_72 _SCREAMING_SNAKE_CASE = 12 _SCREAMING_SNAKE_CASE = 10_24 _SCREAMING_SNAKE_CASE = 40_96 _SCREAMING_SNAKE_CASE = 16 _SCREAMING_SNAKE_CASE = 24 _SCREAMING_SNAKE_CASE = 7_68 _SCREAMING_SNAKE_CASE = 30_72 if model_name == "xclip-large-patch14-16-frames": _SCREAMING_SNAKE_CASE = 3_36 _SCREAMING_SNAKE_CASE = XCLIPConfig.from_text_vision_configs(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if "large" in model_name: _SCREAMING_SNAKE_CASE = 7_68 return config def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" # text encoder if name == "token_embedding.weight": _SCREAMING_SNAKE_CASE = name.replace("""token_embedding.weight""" , """text_model.embeddings.token_embedding.weight""" ) if name == "positional_embedding": _SCREAMING_SNAKE_CASE = name.replace("""positional_embedding""" , """text_model.embeddings.position_embedding.weight""" ) if "ln_1" in name: _SCREAMING_SNAKE_CASE = name.replace("""ln_1""" , """layer_norm1""" ) if "ln_2" in name: _SCREAMING_SNAKE_CASE = name.replace("""ln_2""" , """layer_norm2""" ) if "c_fc" in name: _SCREAMING_SNAKE_CASE = name.replace("""c_fc""" , """fc1""" ) if "c_proj" in name: _SCREAMING_SNAKE_CASE = name.replace("""c_proj""" , """fc2""" ) if name.startswith("""transformer.resblocks""" ): _SCREAMING_SNAKE_CASE = name.replace("""transformer.resblocks""" , """text_model.encoder.layers""" ) if "attn.out_proj" in name and "message" not in name: _SCREAMING_SNAKE_CASE = name.replace("""attn.out_proj""" , """self_attn.out_proj""" ) if "ln_final" in name: _SCREAMING_SNAKE_CASE = name.replace("""ln_final""" , """text_model.final_layer_norm""" ) # visual encoder if name == "visual.class_embedding": _SCREAMING_SNAKE_CASE = name.replace("""visual.class_embedding""" , """vision_model.embeddings.class_embedding""" ) if name == "visual.positional_embedding": _SCREAMING_SNAKE_CASE = name.replace("""visual.positional_embedding""" , """vision_model.embeddings.position_embedding.weight""" ) if name.startswith("""visual.transformer.resblocks""" ): _SCREAMING_SNAKE_CASE = name.replace("""visual.transformer.resblocks""" , """vision_model.encoder.layers""" ) if "visual.conv1" in name: _SCREAMING_SNAKE_CASE = name.replace("""visual.conv1""" , """vision_model.embeddings.patch_embedding""" ) if "visual.ln_pre" in name: _SCREAMING_SNAKE_CASE = name.replace("""visual.ln_pre""" , """vision_model.pre_layernorm""" ) if "visual.ln_post" in name: _SCREAMING_SNAKE_CASE = name.replace("""visual.ln_post""" , """vision_model.post_layernorm""" ) if "visual.proj" in name: _SCREAMING_SNAKE_CASE = name.replace("""visual.proj""" , """visual_projection.weight""" ) if "text_projection" in name: _SCREAMING_SNAKE_CASE = name.replace("""text_projection""" , """text_projection.weight""" ) # things on top if "prompts_visual_proj" in name: _SCREAMING_SNAKE_CASE = name.replace("""prompts_visual_proj""" , """prompts_visual_projection""" ) if "prompts_visual_ln" in name: _SCREAMING_SNAKE_CASE = name.replace("""prompts_visual_ln""" , """prompts_visual_layernorm""" ) # mit if name == "mit.positional_embedding": _SCREAMING_SNAKE_CASE = name.replace("""positional""" , """position""" ) if name.startswith("""mit.resblocks""" ): _SCREAMING_SNAKE_CASE = name.replace("""mit.resblocks""" , """mit.encoder.layers""" ) # prompts generator if name.startswith("""prompts_generator.norm""" ): _SCREAMING_SNAKE_CASE = name.replace("""prompts_generator.norm""" , """prompts_generator.layernorm""" ) return name def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" for key in orig_state_dict.copy().keys(): _SCREAMING_SNAKE_CASE = orig_state_dict.pop(SCREAMING_SNAKE_CASE_ ) if "attn.in_proj" in key: _SCREAMING_SNAKE_CASE = key.split(""".""" ) if key.startswith("""visual""" ): _SCREAMING_SNAKE_CASE = key_split[3] _SCREAMING_SNAKE_CASE = config.vision_config.hidden_size if "message_attn" in key: if "weight" in key: _SCREAMING_SNAKE_CASE = val[ :dim, : ] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2, : ] _SCREAMING_SNAKE_CASE = val[ -dim:, : ] else: _SCREAMING_SNAKE_CASE = val[ :dim ] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2 ] _SCREAMING_SNAKE_CASE = val[ -dim: ] else: if "weight" in key: _SCREAMING_SNAKE_CASE = val[ :dim, : ] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2, : ] _SCREAMING_SNAKE_CASE = val[ -dim:, : ] else: _SCREAMING_SNAKE_CASE = val[:dim] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2 ] _SCREAMING_SNAKE_CASE = val[-dim:] elif key.startswith("""mit""" ): _SCREAMING_SNAKE_CASE = key_split[2] _SCREAMING_SNAKE_CASE = config.vision_config.mit_hidden_size if "weight" in key: _SCREAMING_SNAKE_CASE = val[:dim, :] _SCREAMING_SNAKE_CASE = val[dim : dim * 2, :] _SCREAMING_SNAKE_CASE = val[-dim:, :] else: _SCREAMING_SNAKE_CASE = val[:dim] _SCREAMING_SNAKE_CASE = val[dim : dim * 2] _SCREAMING_SNAKE_CASE = val[-dim:] else: _SCREAMING_SNAKE_CASE = key_split[2] _SCREAMING_SNAKE_CASE = config.text_config.hidden_size if "weight" in key: _SCREAMING_SNAKE_CASE = val[:dim, :] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2, : ] _SCREAMING_SNAKE_CASE = val[-dim:, :] else: _SCREAMING_SNAKE_CASE = val[:dim] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2 ] _SCREAMING_SNAKE_CASE = val[-dim:] else: _SCREAMING_SNAKE_CASE = rename_key(SCREAMING_SNAKE_CASE_ ) if new_key_name in ["visual_projection.weight", "text_projection.weight"]: _SCREAMING_SNAKE_CASE = val.T _SCREAMING_SNAKE_CASE = val return orig_state_dict def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" if num_frames == 8: _SCREAMING_SNAKE_CASE = """eating_spaghetti_8_frames.npy""" elif num_frames == 16: _SCREAMING_SNAKE_CASE = """eating_spaghetti.npy""" elif num_frames == 32: _SCREAMING_SNAKE_CASE = """eating_spaghetti_32_frames.npy""" _SCREAMING_SNAKE_CASE = hf_hub_download( repo_id="""hf-internal-testing/spaghetti-video""" , filename=SCREAMING_SNAKE_CASE_ , repo_type="""dataset""" , ) _SCREAMING_SNAKE_CASE = np.load(SCREAMING_SNAKE_CASE_ ) return list(SCREAMING_SNAKE_CASE_ ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_=False ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = { # fully supervised kinetics-400 checkpoints """xclip-base-patch32""": """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_8.pth""", """xclip-base-patch32-16-frames""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_16.pth""" ), """xclip-base-patch16""": """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_8.pth""", """xclip-base-patch16-16-frames""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_16.pth""" ), """xclip-large-patch14""": """https://drive.google.com/u/0/uc?id=1NUOImq0o5DlQTST17iIP3vG7DgmHQuCx&amp;export=download&amp;confirm=t&amp;uuid=b26caedc-88e2-473e-830a-9d158b653cdb""", """xclip-large-patch14-16-frames""": """https://drive.google.com/u/0/uc?id=1FOYgnJc097OJ4lGwtRCCydQyVPJEOH7d&amp;export=download&amp;confirm=t&amp;uuid=538fa810-e671-4050-b385-9a623f89804f""", # fully supervised kinetics-600 checkpoints """xclip-base-patch16-kinetics-600""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_8.pth""" ), """xclip-base-patch16-kinetics-600-16-frames""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_16.pth""" ), """xclip-large-patch14-kinetics-600""": """https://drive.google.com/u/0/uc?id=1FV8C1INuM91sLAN4ImjzePLIlpMSihwV&amp;export=download&amp;confirm=t&amp;uuid=141d4977-4a65-44ae-864f-4b0c19f838be""", # few shot """xclip-base-patch16-hmdb-2-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_2.pth""" ), """xclip-base-patch16-hmdb-4-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_4.pth""" ), """xclip-base-patch16-hmdb-8-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_8.pth""" ), """xclip-base-patch16-hmdb-16-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_16.pth""" ), """xclip-base-patch16-ucf-2-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_2.pth""" ), """xclip-base-patch16-ucf-4-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_4.pth""" ), """xclip-base-patch16-ucf-8-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_8.pth""" ), """xclip-base-patch16-ucf-16-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_16.pth""" ), # zero shot """xclip-base-patch16-zero-shot""": """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/zero.pth""", } _SCREAMING_SNAKE_CASE = model_to_url[model_name] _SCREAMING_SNAKE_CASE = 8 if "16-frames" in model_name: _SCREAMING_SNAKE_CASE = 16 elif "shot" in model_name: _SCREAMING_SNAKE_CASE = 32 _SCREAMING_SNAKE_CASE = get_xclip_config(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = XCLIPModel(SCREAMING_SNAKE_CASE_ ) model.eval() if "drive" in checkpoint_url: _SCREAMING_SNAKE_CASE = """pytorch_model.bin""" gdown.cached_download(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , quiet=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = torch.load(SCREAMING_SNAKE_CASE_ , map_location="""cpu""" )["""model"""] else: _SCREAMING_SNAKE_CASE = torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE_ )["""model"""] _SCREAMING_SNAKE_CASE = convert_state_dict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = XCLIPModel(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = model.load_state_dict(SCREAMING_SNAKE_CASE_ , strict=SCREAMING_SNAKE_CASE_ ) assert missing_keys == ["text_model.embeddings.position_ids", "vision_model.embeddings.position_ids"] model.eval() _SCREAMING_SNAKE_CASE = 3_36 if model_name == """xclip-large-patch14-16-frames""" else 2_24 _SCREAMING_SNAKE_CASE = VideoMAEImageProcessor(size=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = CLIPTokenizer.from_pretrained("""openai/clip-vit-base-patch32""" ) _SCREAMING_SNAKE_CASE = CLIPTokenizerFast.from_pretrained("""openai/clip-vit-base-patch32""" ) _SCREAMING_SNAKE_CASE = XCLIPProcessor(image_processor=SCREAMING_SNAKE_CASE_ , tokenizer=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = prepare_video(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = processor( text=["""playing sports""", """eating spaghetti""", """go shopping"""] , videos=SCREAMING_SNAKE_CASE_ , return_tensors="""pt""" , padding=SCREAMING_SNAKE_CASE_ ) print("""Shape of pixel values:""" , inputs.pixel_values.shape ) with torch.no_grad(): _SCREAMING_SNAKE_CASE = model(**SCREAMING_SNAKE_CASE_ ) # Verify outputs _SCREAMING_SNAKE_CASE = outputs.logits_per_video _SCREAMING_SNAKE_CASE = logits_per_video.softmax(dim=1 ) print("""Probs:""" , SCREAMING_SNAKE_CASE_ ) # kinetics-400 if model_name == "xclip-base-patch32": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0019, 0.9951, 0.0030]] ) elif model_name == "xclip-base-patch32-16-frames": _SCREAMING_SNAKE_CASE = torch.tensor([[7.0999e-04, 9.9883e-01, 4.5580e-04]] ) elif model_name == "xclip-base-patch16": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0083, 0.9681, 0.0236]] ) elif model_name == "xclip-base-patch16-16-frames": _SCREAMING_SNAKE_CASE = torch.tensor([[7.6937e-04, 9.9728e-01, 1.9473e-03]] ) elif model_name == "xclip-large-patch14": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0062, 0.9864, 0.0075]] ) elif model_name == "xclip-large-patch14-16-frames": _SCREAMING_SNAKE_CASE = torch.tensor([[3.3877e-04, 9.9937e-01, 2.8888e-04]] ) # kinetics-600 elif model_name == "xclip-base-patch16-kinetics-600": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0555, 0.8914, 0.0531]] ) elif model_name == "xclip-base-patch16-kinetics-600-16-frames": _SCREAMING_SNAKE_CASE = torch.tensor([[3.8554e-04, 9.9929e-01, 3.2754e-04]] ) elif model_name == "xclip-large-patch14-kinetics-600": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0036, 0.9920, 0.0045]] ) # few shot elif model_name == "xclip-base-patch16-hmdb-2-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[7.1890e-06, 9.9994e-01, 5.6559e-05]] ) elif model_name == "xclip-base-patch16-hmdb-4-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[1.0320e-05, 9.9993e-01, 6.2435e-05]] ) elif model_name == "xclip-base-patch16-hmdb-8-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[4.1377e-06, 9.9990e-01, 9.8386e-05]] ) elif model_name == "xclip-base-patch16-hmdb-16-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[4.1347e-05, 9.9962e-01, 3.3411e-04]] ) elif model_name == "xclip-base-patch16-ucf-2-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[8.5857e-05, 9.9928e-01, 6.3291e-04]] ) elif model_name == "xclip-base-patch16-ucf-4-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[8.5857e-05, 9.9928e-01, 6.3291e-04]] ) elif model_name == "xclip-base-patch16-ucf-8-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0027, 0.9904, 0.0070]] ) elif model_name == "xclip-base-patch16-ucf-16-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[9.8219e-04, 9.9593e-01, 3.0863e-03]] ) # zero shot elif model_name == "xclip-base-patch16-zero-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[3.5082e-04, 9.9785e-01, 1.7966e-03]] ) else: raise ValueError(F"Model name {model_name} not supported" ) assert torch.allclose(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , atol=1e-3 ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: print(F"Saving model {model_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(SCREAMING_SNAKE_CASE_ ) if push_to_hub: print("""Pushing model, processor and slow tokenizer files to the hub...""" ) model.push_to_hub(SCREAMING_SNAKE_CASE_ , organization="""nielsr""" ) processor.push_to_hub(SCREAMING_SNAKE_CASE_ , organization="""nielsr""" ) slow_tokenizer.push_to_hub(SCREAMING_SNAKE_CASE_ , organization="""nielsr""" ) if __name__ == "__main__": UpperCamelCase__ : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default="xclip-base-patch32", type=str, help="Name of the model.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether or not to push the converted model to the 🤗 hub." ) UpperCamelCase__ : str = parser.parse_args() convert_xclip_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
0
0
'''simple docstring''' def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[str]: """simple docstring""" if height >= 1: move_tower(height - 1 , _snake_case , _snake_case , _snake_case ) move_disk(_snake_case , _snake_case ) move_tower(height - 1 , _snake_case , _snake_case , _snake_case ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[str]: """simple docstring""" print("""moving disk from""" , _snake_case , """to""" , _snake_case ) def lowerCAmelCase_ ( ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE = int(input("""Height of hanoi: """ ).strip() ) move_tower(_snake_case , """A""" , """B""" , """C""" ) if __name__ == "__main__": main()
710
'''simple docstring''' import numpy as np import torch from torch.utils.data import Dataset from utils import logger class _a (_lowerCamelCase): """simple docstring""" def __init__( self , A__ , A__ ) -> Any: _SCREAMING_SNAKE_CASE = params _SCREAMING_SNAKE_CASE = np.array(A__ ) _SCREAMING_SNAKE_CASE = np.array([len(A__ ) for t in data] ) self.check() self.remove_long_sequences() self.remove_empty_sequences() self.remove_unknown_sequences() self.check() self.print_statistics() def __getitem__( self , A__ ) -> Dict: return (self.token_ids[index], self.lengths[index]) def __len__( self ) -> Tuple: return len(self.lengths ) def UpperCamelCase ( self ) -> Dict: assert len(self.token_ids ) == len(self.lengths ) assert all(self.lengths[i] == len(self.token_ids[i] ) for i in range(len(self.lengths ) ) ) def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = self.params.max_model_input_size _SCREAMING_SNAKE_CASE = self.lengths > max_len logger.info(F"Splitting {sum(A__ )} too long sequences." ) def divide_chunks(A__ , A__ ): return [l[i : i + n] for i in range(0 , len(A__ ) , A__ )] _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = [] if self.params.mlm: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self.params.special_tok_ids["""cls_token"""], self.params.special_tok_ids["""sep_token"""] else: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 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: _SCREAMING_SNAKE_CASE = [] for sub_s in divide_chunks(seq_ , max_len - 2 ): if sub_s[0] != cls_id: _SCREAMING_SNAKE_CASE = np.insert(A__ , 0 , A__ ) if sub_s[-1] != sep_id: _SCREAMING_SNAKE_CASE = np.insert(A__ , len(A__ ) , A__ ) assert len(A__ ) <= max_len assert (sub_s[0] == cls_id) and (sub_s[-1] == sep_id), sub_s sub_seqs.append(A__ ) new_tok_ids.extend(A__ ) new_lengths.extend([len(A__ ) for l in sub_seqs] ) _SCREAMING_SNAKE_CASE = np.array(A__ ) _SCREAMING_SNAKE_CASE = np.array(A__ ) def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = len(self ) _SCREAMING_SNAKE_CASE = self.lengths > 11 _SCREAMING_SNAKE_CASE = self.token_ids[indices] _SCREAMING_SNAKE_CASE = self.lengths[indices] _SCREAMING_SNAKE_CASE = len(self ) logger.info(F"Remove {init_size - new_size} too short (<=11 tokens) sequences." ) def UpperCamelCase ( self ) -> int: if "unk_token" not in self.params.special_tok_ids: return else: _SCREAMING_SNAKE_CASE = self.params.special_tok_ids["""unk_token"""] _SCREAMING_SNAKE_CASE = len(self ) _SCREAMING_SNAKE_CASE = np.array([np.count_nonzero(a == unk_token_id ) for a in self.token_ids] ) _SCREAMING_SNAKE_CASE = (unk_occs / self.lengths) < 0.5 _SCREAMING_SNAKE_CASE = self.token_ids[indices] _SCREAMING_SNAKE_CASE = self.lengths[indices] _SCREAMING_SNAKE_CASE = len(self ) logger.info(F"Remove {init_size - new_size} sequences with a high level of unknown tokens (50%)." ) def UpperCamelCase ( self ) -> Optional[Any]: if not self.params.is_master: return logger.info(F"{len(self )} sequences" ) # data_len = sum(self.lengths) # nb_unique_tokens = len(Counter(list(chain(*self.token_ids)))) # logger.info(f'{data_len} tokens ({nb_unique_tokens} unique)') # unk_idx = self.params.special_tok_ids['unk_token'] # nb_unknown = sum([(t==unk_idx).sum() for t in self.token_ids]) # logger.info(f'{nb_unknown} unknown tokens (covering {100*nb_unknown/data_len:.2f}% of the data)') def UpperCamelCase ( self , A__ ) -> Any: _SCREAMING_SNAKE_CASE = [t[0] for t in batch] _SCREAMING_SNAKE_CASE = [t[1] for t in batch] assert len(A__ ) == len(A__ ) # Max for paddings _SCREAMING_SNAKE_CASE = max(A__ ) # Pad token ids if self.params.mlm: _SCREAMING_SNAKE_CASE = self.params.special_tok_ids["""pad_token"""] else: _SCREAMING_SNAKE_CASE = self.params.special_tok_ids["""unk_token"""] _SCREAMING_SNAKE_CASE = [list(t.astype(A__ ) ) + [pad_idx] * (max_seq_len_ - len(A__ )) for t in token_ids] assert len(tk_ ) == len(A__ ) assert all(len(A__ ) == max_seq_len_ for t in tk_ ) _SCREAMING_SNAKE_CASE = torch.tensor(tk_ ) # (bs, max_seq_len_) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) # (bs) return tk_t, lg_t
0
0
'''simple docstring''' def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE = [0] * len(_lowercase ) for i in range(1 , len(_lowercase ) ): # use last results for better performance - dynamic programming _SCREAMING_SNAKE_CASE = prefix_result[i - 1] while j > 0 and input_string[i] != input_string[j]: _SCREAMING_SNAKE_CASE = prefix_result[j - 1] if input_string[i] == input_string[j]: j += 1 _SCREAMING_SNAKE_CASE = j return prefix_result def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Tuple: """simple docstring""" return max(prefix_function(_lowercase ) ) if __name__ == "__main__": import doctest doctest.testmod()
711
'''simple docstring''' 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 UpperCamelCase__ : List[Any] = logging.get_logger(__name__) UpperCamelCase__ : Any = "▁" UpperCamelCase__ : Any = {"vocab_file": "spiece.model"} UpperCamelCase__ : int = { "vocab_file": { "google/reformer-crime-and-punishment": ( "https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/spiece.model" ) } } UpperCamelCase__ : Optional[int] = { "google/reformer-crime-and-punishment": 524_288, } class _a (_lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE = ['input_ids', 'attention_mask'] def __init__( self , A__ , A__="</s>" , A__="<unk>" , A__=[] , A__ = None , **A__ , ) -> None: _SCREAMING_SNAKE_CASE = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( eos_token=A__ , unk_token=A__ , additional_special_tokens=A__ , sp_model_kwargs=self.sp_model_kwargs , **A__ , ) _SCREAMING_SNAKE_CASE = vocab_file _SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(A__ ) @property def UpperCamelCase ( self ) -> Any: return self.sp_model.get_piece_size() def UpperCamelCase ( self ) -> Dict[str, int]: _SCREAMING_SNAKE_CASE = {self.convert_ids_to_tokens(A__ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ) -> int: _SCREAMING_SNAKE_CASE = self.__dict__.copy() _SCREAMING_SNAKE_CASE = None return state def __setstate__( self , A__ ) -> str: _SCREAMING_SNAKE_CASE = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): _SCREAMING_SNAKE_CASE = {} _SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCamelCase ( self , A__ ) -> List[str]: return self.sp_model.encode(A__ , out_type=A__ ) def UpperCamelCase ( self , A__ ) -> Union[str, Any]: return self.sp_model.piece_to_id(A__ ) def UpperCamelCase ( self , A__ ) -> List[Any]: if index < self.sp_model.get_piece_size(): _SCREAMING_SNAKE_CASE = self.sp_model.IdToPiece(A__ ) return token def UpperCamelCase ( self , A__ ) -> str: _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = """""" 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(A__ ) + token _SCREAMING_SNAKE_CASE = [] else: current_sub_tokens.append(A__ ) out_string += self.sp_model.decode(A__ ) return out_string.strip() def UpperCamelCase ( self , A__ , A__ = None ) -> Tuple[str]: if not os.path.isdir(A__ ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return _SCREAMING_SNAKE_CASE = os.path.join( A__ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(A__ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , A__ ) elif not os.path.isfile(self.vocab_file ): with open(A__ , """wb""" ) as fi: _SCREAMING_SNAKE_CASE = self.sp_model.serialized_model_proto() fi.write(A__ ) return (out_vocab_file,)
0
0
'''simple docstring''' from ...utils import ( OptionalDependencyNotAvailable, is_flax_available, is_torch_available, is_transformers_available, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .multicontrolnet import MultiControlNetModel from .pipeline_controlnet import StableDiffusionControlNetPipeline from .pipeline_controlnet_imgaimg import StableDiffusionControlNetImgaImgPipeline from .pipeline_controlnet_inpaint import StableDiffusionControlNetInpaintPipeline if is_transformers_available() and is_flax_available(): from .pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline
712
'''simple docstring''' import os import unittest from transformers import MobileBertTokenizer, MobileBertTokenizerFast from transformers.models.bert.tokenization_bert import ( VOCAB_FILES_NAMES, BasicTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin, filter_non_english @require_tokenizers class _a (_lowerCamelCase , unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = MobileBertTokenizer SCREAMING_SNAKE_CASE = MobileBertTokenizerFast SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = filter_non_english SCREAMING_SNAKE_CASE = 'google/mobilebert-uncased' def UpperCamelCase ( self ) -> Any: super().setUp() _SCREAMING_SNAKE_CASE = [ """[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest""", ] _SCREAMING_SNAKE_CASE = 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] ) ) _SCREAMING_SNAKE_CASE = [ (tokenizer_def[0], self.pre_trained_model_path, tokenizer_def[2]) # else the 'google/' prefix is stripped for tokenizer_def in self.tokenizers_list ] def UpperCamelCase ( self , A__ ) -> List[str]: _SCREAMING_SNAKE_CASE = """UNwant\u00E9d,running""" _SCREAMING_SNAKE_CASE = """unwanted, running""" return input_text, output_text def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = self.tokenizer_class(self.vocab_file ) _SCREAMING_SNAKE_CASE = tokenizer.tokenize("""UNwant\u00E9d,running""" ) self.assertListEqual(A__ , ["""un""", """##want""", """##ed""", """,""", """runn""", """##ing"""] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(A__ ) , [9, 6, 7, 12, 10, 11] ) def UpperCamelCase ( self ) -> Optional[int]: if not self.test_rust_tokenizer: return _SCREAMING_SNAKE_CASE = self.get_tokenizer() _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() _SCREAMING_SNAKE_CASE = """UNwant\u00E9d,running""" _SCREAMING_SNAKE_CASE = tokenizer.tokenize(A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.tokenize(A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = tokenizer.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.encode(A__ , add_special_tokens=A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() _SCREAMING_SNAKE_CASE = tokenizer.encode(A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.encode(A__ ) self.assertListEqual(A__ , A__ ) # With lower casing _SCREAMING_SNAKE_CASE = self.get_tokenizer(do_lower_case=A__ ) _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer(do_lower_case=A__ ) _SCREAMING_SNAKE_CASE = """UNwant\u00E9d,running""" _SCREAMING_SNAKE_CASE = tokenizer.tokenize(A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.tokenize(A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = tokenizer.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.encode(A__ , add_special_tokens=A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() _SCREAMING_SNAKE_CASE = tokenizer.encode(A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.encode(A__ ) self.assertListEqual(A__ , A__ ) def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = BasicTokenizer() self.assertListEqual(tokenizer.tokenize("""ah\u535A\u63A8zz""" ) , ["""ah""", """\u535A""", """\u63A8""", """zz"""] ) def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHeLLo!how \n Are yoU? """ ) , ["""hello""", """!""", """how""", """are""", """you""", """?"""] ) self.assertListEqual(tokenizer.tokenize("""H\u00E9llo""" ) , ["""hello"""] ) def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , strip_accents=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""hällo""", """!""", """how""", """are""", """you""", """?"""] ) self.assertListEqual(tokenizer.tokenize("""H\u00E9llo""" ) , ["""h\u00E9llo"""] ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , strip_accents=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""hallo""", """!""", """how""", """are""", """you""", """?"""] ) self.assertListEqual(tokenizer.tokenize("""H\u00E9llo""" ) , ["""hello"""] ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""hallo""", """!""", """how""", """are""", """you""", """?"""] ) self.assertListEqual(tokenizer.tokenize("""H\u00E9llo""" ) , ["""hello"""] ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHeLLo!how \n Are yoU? """ ) , ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?"""] ) def UpperCamelCase ( self ) -> Dict: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , strip_accents=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""HäLLo""", """!""", """how""", """Are""", """yoU""", """?"""] ) def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , strip_accents=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""HaLLo""", """!""", """how""", """Are""", """yoU""", """?"""] ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , never_split=["""[UNK]"""] ) self.assertListEqual( tokenizer.tokenize(""" \tHeLLo!how \n Are yoU? [UNK]""" ) , ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?""", """[UNK]"""] ) def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = ["""[UNK]""", """[CLS]""", """[SEP]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing"""] _SCREAMING_SNAKE_CASE = {} for i, token in enumerate(A__ ): _SCREAMING_SNAKE_CASE = i _SCREAMING_SNAKE_CASE = WordpieceTokenizer(vocab=A__ , unk_token="""[UNK]""" ) self.assertListEqual(tokenizer.tokenize("""""" ) , [] ) self.assertListEqual(tokenizer.tokenize("""unwanted running""" ) , ["""un""", """##want""", """##ed""", """runn""", """##ing"""] ) self.assertListEqual(tokenizer.tokenize("""unwantedX running""" ) , ["""[UNK]""", """runn""", """##ing"""] ) def UpperCamelCase ( self ) -> str: self.assertTrue(_is_whitespace(""" """ ) ) self.assertTrue(_is_whitespace("""\t""" ) ) self.assertTrue(_is_whitespace("""\r""" ) ) self.assertTrue(_is_whitespace("""\n""" ) ) self.assertTrue(_is_whitespace("""\u00A0""" ) ) self.assertFalse(_is_whitespace("""A""" ) ) self.assertFalse(_is_whitespace("""-""" ) ) def UpperCamelCase ( self ) -> Union[str, Any]: self.assertTrue(_is_control("""\u0005""" ) ) self.assertFalse(_is_control("""A""" ) ) self.assertFalse(_is_control(""" """ ) ) self.assertFalse(_is_control("""\t""" ) ) self.assertFalse(_is_control("""\r""" ) ) def UpperCamelCase ( self ) -> Dict: self.assertTrue(_is_punctuation("""-""" ) ) self.assertTrue(_is_punctuation("""$""" ) ) self.assertTrue(_is_punctuation("""`""" ) ) self.assertTrue(_is_punctuation(""".""" ) ) self.assertFalse(_is_punctuation("""A""" ) ) self.assertFalse(_is_punctuation(""" """ ) ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = self.get_tokenizer() _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() # Example taken from the issue https://github.com/huggingface/tokenizers/issues/340 self.assertListEqual([tokenizer.tokenize(A__ ) for t in ["""Test""", """\xad""", """test"""]] , [["""[UNK]"""], [], ["""[UNK]"""]] ) self.assertListEqual( [rust_tokenizer.tokenize(A__ ) for t in ["""Test""", """\xad""", """test"""]] , [["""[UNK]"""], [], ["""[UNK]"""]] ) @slow def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained("""google/mobilebert-uncased""" ) _SCREAMING_SNAKE_CASE = tokenizer.encode("""sequence builders""" , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer.encode("""multi-sequence build""" , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer.build_inputs_with_special_tokens(A__ ) _SCREAMING_SNAKE_CASE = tokenizer.build_inputs_with_special_tokens(A__ , A__ ) assert encoded_sentence == [1_01] + text + [1_02] assert encoded_pair == [1_01] + text + [1_02] + text_a + [1_02] def UpperCamelCase ( self ) -> List[str]: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})" ): _SCREAMING_SNAKE_CASE = self.rust_tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = F"A, naïve {tokenizer_r.mask_token} AllenNLP sentence." _SCREAMING_SNAKE_CASE = tokenizer_r.encode_plus( A__ , return_attention_mask=A__ , return_token_type_ids=A__ , return_offsets_mapping=A__ , add_special_tokens=A__ , ) _SCREAMING_SNAKE_CASE = tokenizer_r.do_lower_case if hasattr(A__ , """do_lower_case""" ) else False _SCREAMING_SNAKE_CASE = ( [ ((0, 0), tokenizer_r.cls_token), ((0, 1), """A"""), ((1, 2), ""","""), ((3, 5), """na"""), ((5, 6), """##ï"""), ((6, 8), """##ve"""), ((9, 15), tokenizer_r.mask_token), ((16, 21), """Allen"""), ((21, 23), """##NL"""), ((23, 24), """##P"""), ((25, 33), """sentence"""), ((33, 34), """."""), ((0, 0), tokenizer_r.sep_token), ] if not do_lower_case else [ ((0, 0), tokenizer_r.cls_token), ((0, 1), """a"""), ((1, 2), ""","""), ((3, 8), """naive"""), ((9, 15), tokenizer_r.mask_token), ((16, 21), """allen"""), ((21, 23), """##nl"""), ((23, 24), """##p"""), ((25, 33), """sentence"""), ((33, 34), """."""), ((0, 0), tokenizer_r.sep_token), ] ) self.assertEqual( [e[1] for e in expected_results] , tokenizer_r.convert_ids_to_tokens(tokens["""input_ids"""] ) ) self.assertEqual([e[0] for e in expected_results] , tokens["""offset_mapping"""] ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = ["""的""", """人""", """有"""] _SCREAMING_SNAKE_CASE = """""".join(A__ ) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})" ): _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = self.rust_tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = tokenizer_p.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer_r.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer_r.convert_ids_to_tokens(A__ ) _SCREAMING_SNAKE_CASE = tokenizer_p.convert_ids_to_tokens(A__ ) # it is expected that each Chinese character is not preceded by "##" self.assertListEqual(A__ , A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = self.rust_tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = tokenizer_r.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer_p.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer_r.convert_ids_to_tokens(A__ ) _SCREAMING_SNAKE_CASE = tokenizer_p.convert_ids_to_tokens(A__ ) # it is expected that only the first Chinese character is not preceded by "##". _SCREAMING_SNAKE_CASE = [ F"##{token}" if idx != 0 else token for idx, token in enumerate(A__ ) ] self.assertListEqual(A__ , A__ ) self.assertListEqual(A__ , A__ )
0
0
'''simple docstring''' def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" if len(SCREAMING_SNAKE_CASE_ ) <= 1: return lst _SCREAMING_SNAKE_CASE = 1 while i < len(SCREAMING_SNAKE_CASE_ ): if lst[i - 1] <= lst[i]: i += 1 else: _SCREAMING_SNAKE_CASE = lst[i], lst[i - 1] i -= 1 if i == 0: _SCREAMING_SNAKE_CASE = 1 return lst if __name__ == "__main__": UpperCamelCase__ : Tuple = input("Enter numbers separated by a comma:\n").strip() UpperCamelCase__ : Union[str, Any] = [int(item) for item in user_input.split(",")] print(gnome_sort(unsorted))
713
'''simple docstring''' import logging import os import quant_trainer import torch from torch.utils.data import DataLoader from transformers import Trainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput UpperCamelCase__ : Tuple = logging.getLogger(__name__) if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class _a (_lowerCamelCase): """simple docstring""" def __init__( self , *A__ , A__=None , A__=None , A__=None , **A__ ) -> Optional[int]: super().__init__(*A__ , **A__ ) _SCREAMING_SNAKE_CASE = eval_examples _SCREAMING_SNAKE_CASE = post_process_function _SCREAMING_SNAKE_CASE = quant_trainer_args _SCREAMING_SNAKE_CASE = 1_28 # default number of calibration samples def UpperCamelCase ( self , A__=None ) -> Union[str, Any]: if calib_dataset is None and self.calib_dataset is None: raise ValueError("""Trainer: calibration requires an calib_dataset.""" ) _SCREAMING_SNAKE_CASE = calib_dataset if calib_dataset is not None else self.calib_dataset _SCREAMING_SNAKE_CASE = self._remove_unused_columns(A__ , description="""Calibration""" ) return DataLoader( A__ , batch_size=self.args.eval_batch_size , collate_fn=self.data_collator , drop_last=self.args.dataloader_drop_last , num_workers=self.args.dataloader_num_workers , pin_memory=self.args.dataloader_pin_memory , shuffle=A__ , ) def UpperCamelCase ( self , A__=None ) -> str: _SCREAMING_SNAKE_CASE = self.train_dataset if calib_dataset is None else calib_dataset _SCREAMING_SNAKE_CASE = self.get_calib_dataloader(A__ ) _SCREAMING_SNAKE_CASE = self.model quant_trainer.configure_model(A__ , self.quant_trainer_args , calib=A__ ) model.eval() quant_trainer.enable_calibration(A__ ) logger.info("""***** Running calibration *****""" ) logger.info(F" Num examples = {self.calib_num}" ) logger.info(F" Batch size = {calib_dataloader.batch_size}" ) for step, inputs in enumerate(A__ ): # Prediction step _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self.prediction_step(A__ , A__ , prediction_loss_only=A__ ) if (step + 1) * calib_dataloader.batch_size >= self.calib_num: break quant_trainer.finish_calibration(A__ , self.quant_trainer_args ) _SCREAMING_SNAKE_CASE = model def UpperCamelCase ( self , A__=None , A__=None , A__=None , A__ = "eval" ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.eval_dataset if eval_dataset is None else eval_dataset _SCREAMING_SNAKE_CASE = self.get_eval_dataloader(A__ ) _SCREAMING_SNAKE_CASE = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. _SCREAMING_SNAKE_CASE = self.compute_metrics _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: _SCREAMING_SNAKE_CASE = eval_loop( A__ , description="""Evaluation""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=A__ , ) finally: _SCREAMING_SNAKE_CASE = compute_metrics if self.post_process_function is not None and self.compute_metrics is not None: _SCREAMING_SNAKE_CASE = self.post_process_function(A__ , A__ , output.predictions ) _SCREAMING_SNAKE_CASE = self.compute_metrics(A__ ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(F"{metric_key_prefix}_" ): _SCREAMING_SNAKE_CASE = metrics.pop(A__ ) self.log(A__ ) else: _SCREAMING_SNAKE_CASE = {} if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report() ) _SCREAMING_SNAKE_CASE = self.callback_handler.on_evaluate(self.args , self.state , self.control , A__ ) return metrics def UpperCamelCase ( self , A__ , A__ , A__=None , A__ = "test" ) -> List[str]: _SCREAMING_SNAKE_CASE = self.get_test_dataloader(A__ ) # Temporarily disable metric computation, we will do it in the loop here. _SCREAMING_SNAKE_CASE = self.compute_metrics _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: _SCREAMING_SNAKE_CASE = eval_loop( A__ , description="""Prediction""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=A__ , ) finally: _SCREAMING_SNAKE_CASE = compute_metrics if self.post_process_function is None or self.compute_metrics is None: return output _SCREAMING_SNAKE_CASE = self.post_process_function(A__ , A__ , output.predictions , """predict""" ) _SCREAMING_SNAKE_CASE = self.compute_metrics(A__ ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(F"{metric_key_prefix}_" ): _SCREAMING_SNAKE_CASE = metrics.pop(A__ ) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=A__ ) def UpperCamelCase ( self , A__="./" ) -> Tuple: _SCREAMING_SNAKE_CASE = self.eval_dataset _SCREAMING_SNAKE_CASE = self.get_eval_dataloader(A__ ) _SCREAMING_SNAKE_CASE = next(iter(A__ ) ) # saving device - to make it consistent _SCREAMING_SNAKE_CASE = torch.device("""cuda""" if torch.cuda.is_available() else """cpu""" ) # convert to tuple _SCREAMING_SNAKE_CASE = tuple(v.to(A__ ) for k, v in batch.items() ) logger.info("""Converting model to be onnx compatible""" ) from pytorch_quantization.nn import TensorQuantizer _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = self.model.to(A__ ) model.eval() model.float() _SCREAMING_SNAKE_CASE = model.module if hasattr(A__ , """module""" ) else model quant_trainer.configure_model(A__ , self.quant_trainer_args ) _SCREAMING_SNAKE_CASE = os.path.join(A__ , """model.onnx""" ) logger.info(F"exporting model to {output_model_file}" ) _SCREAMING_SNAKE_CASE = {0: """batch_size""", 1: """seq_len"""} torch.onnx.export( A__ , A__ , A__ , export_params=A__ , opset_version=13 , do_constant_folding=A__ , input_names=["""input_ids""", """attention_mask""", """token_type_ids"""] , output_names=["""output_start_logits""", """output_end_logits"""] , dynamic_axes={ """input_ids""": axes, """attention_mask""": axes, """token_type_ids""": axes, """output_start_logits""": axes, """output_end_logits""": axes, } , verbose=A__ , ) logger.info("""onnx export finished""" )
0
0
def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: """simple docstring""" stooge(SCREAMING_SNAKE_CASE_ , 0 , len(SCREAMING_SNAKE_CASE_ ) - 1 ) return arr def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Tuple: """simple docstring""" if i >= h: return # If first element is smaller than the last then swap them if arr[i] > arr[h]: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = arr[h], arr[i] # If there are more than 2 elements in the array if h - i + 1 > 2: _SCREAMING_SNAKE_CASE = (int)((h - i + 1) / 3 ) # Recursively sort first 2/3 elements stooge(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , (h - t) ) # Recursively sort last 2/3 elements stooge(SCREAMING_SNAKE_CASE_ , i + t , (SCREAMING_SNAKE_CASE_) ) # Recursively sort first 2/3 elements stooge(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , (h - t) ) if __name__ == "__main__": UpperCamelCase__ : Union[str, Any] = input("Enter numbers separated by a comma:\n").strip() UpperCamelCase__ : List[Any] = [int(item) for item in user_input.split(",")] print(stooge_sort(unsorted))
714
'''simple docstring''' def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" return "".join([hex(SCREAMING_SNAKE_CASE_ )[2:].zfill(2 ).upper() for byte in list(SCREAMING_SNAKE_CASE_ )] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> bytes: """simple docstring""" # Check data validity, following RFC3548 # https://www.ietf.org/rfc/rfc3548.txt if (len(SCREAMING_SNAKE_CASE_ ) % 2) != 0: raise ValueError( """Base16 encoded data is invalid: Data does not have an even number of hex digits.""" ) # Check the character set - the standard base16 alphabet # is uppercase according to RFC3548 section 6 if not set(SCREAMING_SNAKE_CASE_ ) <= set("""0123456789ABCDEF""" ): raise ValueError( """Base16 encoded data is invalid: Data is not uppercase hex or it contains invalid characters.""" ) # For every two hexadecimal digits (= a byte), turn it into an integer. # Then, string the result together into bytes, and return it. return bytes(int(data[i] + data[i + 1] , 16 ) for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) , 2 ) ) if __name__ == "__main__": import doctest doctest.testmod()
0
0
'''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, ) UpperCamelCase__ : Any = {"""configuration_xglm""": ["""XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """XGLMConfig"""]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : int = ["""XGLMTokenizer"""] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Tuple = ["""XGLMTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : str = [ """XGLM_PRETRAINED_MODEL_ARCHIVE_LIST""", """XGLMForCausalLM""", """XGLMModel""", """XGLMPreTrainedModel""", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Union[str, Any] = [ """FlaxXGLMForCausalLM""", """FlaxXGLMModel""", """FlaxXGLMPreTrainedModel""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : int = [ """TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFXGLMForCausalLM""", """TFXGLMModel""", """TFXGLMPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm import XGLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm_fast import XGLMTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, TFXGLMPreTrainedModel, ) else: import sys UpperCamelCase__ : Dict = _LazyModule(__name__, globals()["__file__"], _import_structure)
715
'''simple docstring''' import pytest import requests from datasets.utils.file_utils import http_head from .utils import OfflineSimulationMode, RequestWouldHangIndefinitelyError, offline @pytest.mark.integration def lowerCAmelCase_ ( ) -> List[Any]: """simple docstring""" with offline(OfflineSimulationMode.CONNECTION_TIMES_OUT ): with pytest.raises(SCREAMING_SNAKE_CASE_ ): requests.request("""GET""" , """https://huggingface.co""" ) with pytest.raises(requests.exceptions.ConnectTimeout ): requests.request("""GET""" , """https://huggingface.co""" , timeout=1.0 ) @pytest.mark.integration def lowerCAmelCase_ ( ) -> int: """simple docstring""" with offline(OfflineSimulationMode.CONNECTION_FAILS ): with pytest.raises(requests.exceptions.ConnectionError ): requests.request("""GET""" , """https://huggingface.co""" ) def lowerCAmelCase_ ( ) -> Optional[Any]: """simple docstring""" with offline(OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1 ): with pytest.raises(SCREAMING_SNAKE_CASE_ ): http_head("""https://huggingface.co""" )
0
0
from __future__ import annotations import math def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" if num <= 0: _SCREAMING_SNAKE_CASE = F"{num}: Invalid input, please enter a positive integer." raise ValueError(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = [True] * (num + 1) _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = 2 _SCREAMING_SNAKE_CASE = int(math.sqrt(SCREAMING_SNAKE_CASE_ ) ) while start <= end: # If start is a prime if sieve[start] is True: prime.append(SCREAMING_SNAKE_CASE_ ) # Set multiples of start be False for i in range(start * start , num + 1 , SCREAMING_SNAKE_CASE_ ): if sieve[i] is True: _SCREAMING_SNAKE_CASE = False start += 1 for j in range(end + 1 , num + 1 ): if sieve[j] is True: prime.append(SCREAMING_SNAKE_CASE_ ) return prime if __name__ == "__main__": print(prime_sieve(int(input("Enter a positive integer: ").strip())))
716
'''simple docstring''' import math from collections.abc import Iterator from itertools import takewhile def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> bool: """simple docstring""" if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(SCREAMING_SNAKE_CASE_ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def lowerCAmelCase_ ( ) -> Iterator[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = 2 while True: if is_prime(SCREAMING_SNAKE_CASE_ ): yield num num += 1 def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ = 2_00_00_00 ) -> int: """simple docstring""" return sum(takewhile(lambda SCREAMING_SNAKE_CASE_ : x < n , prime_generator() ) ) if __name__ == "__main__": print(f"""{solution() = }""")
0
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) UpperCamelCase__ : int = { "configuration_deberta": ["DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP", "DebertaConfig", "DebertaOnnxConfig"], "tokenization_deberta": ["DebertaTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : List[Any] = ["DebertaTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : List[Any] = [ "DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "DebertaForMaskedLM", "DebertaForQuestionAnswering", "DebertaForSequenceClassification", "DebertaForTokenClassification", "DebertaModel", "DebertaPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Optional[int] = [ "TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "TFDebertaForMaskedLM", "TFDebertaForQuestionAnswering", "TFDebertaForSequenceClassification", "TFDebertaForTokenClassification", "TFDebertaModel", "TFDebertaPreTrainedModel", ] if TYPE_CHECKING: from .configuration_deberta import DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, DebertaConfig, DebertaOnnxConfig from .tokenization_deberta import DebertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_deberta_fast import DebertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_deberta import ( DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, DebertaForMaskedLM, DebertaForQuestionAnswering, DebertaForSequenceClassification, DebertaForTokenClassification, DebertaModel, DebertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_deberta import ( TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFDebertaForMaskedLM, TFDebertaForQuestionAnswering, TFDebertaForSequenceClassification, TFDebertaForTokenClassification, TFDebertaModel, TFDebertaPreTrainedModel, ) else: import sys UpperCamelCase__ : List[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
717
'''simple docstring''' import unittest import numpy as np from transformers.testing_utils import require_flax, require_tf, require_torch from transformers.utils import ( expand_dims, flatten_dict, is_flax_available, is_tf_available, is_torch_available, reshape, squeeze, transpose, ) if is_flax_available(): import jax.numpy as jnp if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch class _a (unittest.TestCase): """simple docstring""" def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = { """task_specific_params""": { """summarization""": {"""length_penalty""": 1.0, """max_length""": 1_28, """min_length""": 12, """num_beams""": 4}, """summarization_cnn""": {"""length_penalty""": 2.0, """max_length""": 1_42, """min_length""": 56, """num_beams""": 4}, """summarization_xsum""": {"""length_penalty""": 1.0, """max_length""": 62, """min_length""": 11, """num_beams""": 6}, } } _SCREAMING_SNAKE_CASE = { """task_specific_params.summarization.length_penalty""": 1.0, """task_specific_params.summarization.max_length""": 1_28, """task_specific_params.summarization.min_length""": 12, """task_specific_params.summarization.num_beams""": 4, """task_specific_params.summarization_cnn.length_penalty""": 2.0, """task_specific_params.summarization_cnn.max_length""": 1_42, """task_specific_params.summarization_cnn.min_length""": 56, """task_specific_params.summarization_cnn.num_beams""": 4, """task_specific_params.summarization_xsum.length_penalty""": 1.0, """task_specific_params.summarization_xsum.max_length""": 62, """task_specific_params.summarization_xsum.min_length""": 11, """task_specific_params.summarization_xsum.num_beams""": 6, } self.assertEqual(flatten_dict(A__ ) , A__ ) def UpperCamelCase ( self ) -> int: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(transpose(A__ ) , x.transpose() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(transpose(A__ , axes=(1, 2, 0) ) , x.transpose((1, 2, 0) ) ) ) @require_torch def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(transpose(A__ ) , transpose(A__ ).numpy() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(transpose(A__ , axes=(1, 2, 0) ) , transpose(A__ , axes=(1, 2, 0) ).numpy() ) ) @require_tf def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(transpose(A__ ) , transpose(A__ ).numpy() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(transpose(A__ , axes=(1, 2, 0) ) , transpose(A__ , axes=(1, 2, 0) ).numpy() ) ) @require_flax def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(transpose(A__ ) , np.asarray(transpose(A__ ) ) ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(transpose(A__ , axes=(1, 2, 0) ) , np.asarray(transpose(A__ , axes=(1, 2, 0) ) ) ) ) def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(reshape(A__ , (4, 3) ) , np.reshape(A__ , (4, 3) ) ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(reshape(A__ , (12, 5) ) , np.reshape(A__ , (12, 5) ) ) ) @require_torch def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(reshape(A__ , (4, 3) ) , reshape(A__ , (4, 3) ).numpy() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(reshape(A__ , (12, 5) ) , reshape(A__ , (12, 5) ).numpy() ) ) @require_tf def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(reshape(A__ , (4, 3) ) , reshape(A__ , (4, 3) ).numpy() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(reshape(A__ , (12, 5) ) , reshape(A__ , (12, 5) ).numpy() ) ) @require_flax def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(reshape(A__ , (4, 3) ) , np.asarray(reshape(A__ , (4, 3) ) ) ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(reshape(A__ , (12, 5) ) , np.asarray(reshape(A__ , (12, 5) ) ) ) ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = np.random.randn(1 , 3 , 4 ) self.assertTrue(np.allclose(squeeze(A__ ) , np.squeeze(A__ ) ) ) _SCREAMING_SNAKE_CASE = np.random.randn(1 , 4 , 1 , 5 ) self.assertTrue(np.allclose(squeeze(A__ , axis=2 ) , np.squeeze(A__ , axis=2 ) ) ) @require_torch def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = np.random.randn(1 , 3 , 4 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(squeeze(A__ ) , squeeze(A__ ).numpy() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(1 , 4 , 1 , 5 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(squeeze(A__ , axis=2 ) , squeeze(A__ , axis=2 ).numpy() ) ) @require_tf def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = np.random.randn(1 , 3 , 4 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(squeeze(A__ ) , squeeze(A__ ).numpy() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(1 , 4 , 1 , 5 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(squeeze(A__ , axis=2 ) , squeeze(A__ , axis=2 ).numpy() ) ) @require_flax def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = np.random.randn(1 , 3 , 4 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(squeeze(A__ ) , np.asarray(squeeze(A__ ) ) ) ) _SCREAMING_SNAKE_CASE = np.random.randn(1 , 4 , 1 , 5 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(squeeze(A__ , axis=2 ) , np.asarray(squeeze(A__ , axis=2 ) ) ) ) def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(expand_dims(A__ , axis=1 ) , np.expand_dims(A__ , axis=1 ) ) ) @require_torch def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(expand_dims(A__ , axis=1 ) , expand_dims(A__ , axis=1 ).numpy() ) ) @require_tf def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(expand_dims(A__ , axis=1 ) , expand_dims(A__ , axis=1 ).numpy() ) ) @require_flax def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(expand_dims(A__ , axis=1 ) , np.asarray(expand_dims(A__ , axis=1 ) ) ) )
0
0
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging UpperCamelCase__ : Union[str, Any] = logging.get_logger(__name__) UpperCamelCase__ : List[Any] = { "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/config.json", "distilbert-base-uncased-distilled-squad": ( "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json" ), "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/config.json", "distilbert-base-cased-distilled-squad": ( "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json" ), "distilbert-base-german-cased": "https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json", "distilbert-base-multilingual-cased": ( "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json" ), "distilbert-base-uncased-finetuned-sst-2-english": ( "https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json" ), } class _a (__a): """simple docstring""" SCREAMING_SNAKE_CASE = "distilbert" SCREAMING_SNAKE_CASE = { "hidden_size": "dim", "num_attention_heads": "n_heads", "num_hidden_layers": "n_layers", } def __init__( self , A__=3_05_22 , A__=5_12 , A__=False , A__=6 , A__=12 , A__=7_68 , A__=4 * 7_68 , A__=0.1 , A__=0.1 , A__="gelu" , A__=0.02 , A__=0.1 , A__=0.2 , A__=0 , **A__ , ) -> List[Any]: _SCREAMING_SNAKE_CASE = vocab_size _SCREAMING_SNAKE_CASE = max_position_embeddings _SCREAMING_SNAKE_CASE = sinusoidal_pos_embds _SCREAMING_SNAKE_CASE = n_layers _SCREAMING_SNAKE_CASE = n_heads _SCREAMING_SNAKE_CASE = dim _SCREAMING_SNAKE_CASE = hidden_dim _SCREAMING_SNAKE_CASE = dropout _SCREAMING_SNAKE_CASE = attention_dropout _SCREAMING_SNAKE_CASE = activation _SCREAMING_SNAKE_CASE = initializer_range _SCREAMING_SNAKE_CASE = qa_dropout _SCREAMING_SNAKE_CASE = seq_classif_dropout super().__init__(**lowerCAmelCase_ , pad_token_id=lowerCAmelCase_ ) class _a (__a): """simple docstring""" @property def UpperCamelCase ( self ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": _SCREAMING_SNAKE_CASE = {0: """batch""", 1: """choice""", 2: """sequence"""} else: _SCREAMING_SNAKE_CASE = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
718
'''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 _a (_lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = '' SCREAMING_SNAKE_CASE = 'hf-legacy' # "hf://"" is reserved for hffs def __init__( self , A__ = None , A__ = None , **A__ , ) -> Optional[int]: super().__init__(self , **A__ ) _SCREAMING_SNAKE_CASE = repo_info _SCREAMING_SNAKE_CASE = token _SCREAMING_SNAKE_CASE = None def UpperCamelCase ( self ) -> Tuple: if self.dir_cache is None: _SCREAMING_SNAKE_CASE = {} for hf_file in self.repo_info.siblings: # TODO(QL): add sizes _SCREAMING_SNAKE_CASE = { """name""": hf_file.rfilename, """size""": None, """type""": """file""", } self.dir_cache.update( { str(A__ ): {"""name""": str(A__ ), """size""": None, """type""": """directory"""} for d in list(PurePosixPath(hf_file.rfilename ).parents )[:-1] } ) def UpperCamelCase ( self , A__ , A__ = "rb" , **A__ , ) -> Optional[int]: if not isinstance(self.repo_info , A__ ): raise NotImplementedError(F"Open is only implemented for dataset repositories, but got {self.repo_info}" ) _SCREAMING_SNAKE_CASE = hf_hub_url(self.repo_info.id , A__ , revision=self.repo_info.sha ) return fsspec.open( A__ , mode=A__ , headers=get_authentication_headers_for_url(A__ , use_auth_token=self.token ) , client_kwargs={"""trust_env""": True} , ).open() def UpperCamelCase ( self , A__ , **A__ ) -> str: self._get_dirs() _SCREAMING_SNAKE_CASE = self._strip_protocol(A__ ) if path in self.dir_cache: return self.dir_cache[path] else: raise FileNotFoundError(A__ ) def UpperCamelCase ( self , A__ , A__=False , **A__ ) -> List[Any]: self._get_dirs() _SCREAMING_SNAKE_CASE = PurePosixPath(path.strip("""/""" ) ) _SCREAMING_SNAKE_CASE = {} for p, f in self.dir_cache.items(): _SCREAMING_SNAKE_CASE = PurePosixPath(p.strip("""/""" ) ) _SCREAMING_SNAKE_CASE = p.parent if root == path: _SCREAMING_SNAKE_CASE = f _SCREAMING_SNAKE_CASE = list(paths.values() ) if detail: return out else: return sorted(f["""name"""] for f in out )
0
0
import argparse import collections import json from pathlib import Path import requests import torch import yaml from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( MobileViTImageProcessor, MobileViTVaConfig, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation, ) from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase__ : Tuple = logging.get_logger(__name__) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Any: """simple docstring""" print("""Loading config file...""" ) def flatten_yaml_as_dict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_="" , SCREAMING_SNAKE_CASE_="." ): _SCREAMING_SNAKE_CASE = [] for k, v in d.items(): _SCREAMING_SNAKE_CASE = parent_key + sep + k if parent_key else k if isinstance(UpperCamelCase__ , collections.abc.MutableMapping ): items.extend(flatten_yaml_as_dict(UpperCamelCase__ , UpperCamelCase__ , sep=UpperCamelCase__ ).items() ) else: items.append((new_key, v) ) return dict(UpperCamelCase__ ) _SCREAMING_SNAKE_CASE = argparse.Namespace() with open(UpperCamelCase__ , """r""" ) as yaml_file: try: _SCREAMING_SNAKE_CASE = yaml.load(UpperCamelCase__ , Loader=yaml.FullLoader ) _SCREAMING_SNAKE_CASE = flatten_yaml_as_dict(UpperCamelCase__ ) for k, v in flat_cfg.items(): setattr(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) except yaml.YAMLError as exc: logger.error("""Error while loading config file: {}. Error message: {}""".format(UpperCamelCase__ , str(UpperCamelCase__ ) ) ) return config def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = MobileViTVaConfig() _SCREAMING_SNAKE_CASE = False # dataset if task_name.startswith("""imagenet1k_""" ): _SCREAMING_SNAKE_CASE = 10_00 if int(task_name.strip().split("""_""" )[-1] ) == 3_84: _SCREAMING_SNAKE_CASE = 3_84 else: _SCREAMING_SNAKE_CASE = 2_56 _SCREAMING_SNAKE_CASE = """imagenet-1k-id2label.json""" elif task_name.startswith("""imagenet21k_to_1k_""" ): _SCREAMING_SNAKE_CASE = 2_10_00 if int(task_name.strip().split("""_""" )[-1] ) == 3_84: _SCREAMING_SNAKE_CASE = 3_84 else: _SCREAMING_SNAKE_CASE = 2_56 _SCREAMING_SNAKE_CASE = """imagenet-22k-id2label.json""" elif task_name.startswith("""ade20k_""" ): _SCREAMING_SNAKE_CASE = 1_51 _SCREAMING_SNAKE_CASE = 5_12 _SCREAMING_SNAKE_CASE = """ade20k-id2label.json""" _SCREAMING_SNAKE_CASE = True elif task_name.startswith("""voc_""" ): _SCREAMING_SNAKE_CASE = 21 _SCREAMING_SNAKE_CASE = 5_12 _SCREAMING_SNAKE_CASE = """pascal-voc-id2label.json""" _SCREAMING_SNAKE_CASE = True # orig_config _SCREAMING_SNAKE_CASE = load_orig_config_file(UpperCamelCase__ ) assert getattr(UpperCamelCase__ , """model.classification.name""" , -1 ) == "mobilevit_v2", "Invalid model" _SCREAMING_SNAKE_CASE = getattr(UpperCamelCase__ , """model.classification.mitv2.width_multiplier""" , 1.0 ) assert ( getattr(UpperCamelCase__ , """model.classification.mitv2.attn_norm_layer""" , -1 ) == "layer_norm_2d" ), "Norm layers other than layer_norm_2d is not supported" _SCREAMING_SNAKE_CASE = getattr(UpperCamelCase__ , """model.classification.activation.name""" , """swish""" ) # config.image_size == getattr(orig_config, 'sampler.bs.crop_size_width', 256) if is_segmentation_model: _SCREAMING_SNAKE_CASE = getattr(UpperCamelCase__ , """model.segmentation.output_stride""" , 16 ) if "_deeplabv3" in task_name: _SCREAMING_SNAKE_CASE = getattr(UpperCamelCase__ , """model.segmentation.deeplabv3.aspp_rates""" , [12, 24, 36] ) _SCREAMING_SNAKE_CASE = getattr(UpperCamelCase__ , """model.segmentation.deeplabv3.aspp_out_channels""" , 5_12 ) _SCREAMING_SNAKE_CASE = getattr(UpperCamelCase__ , """model.segmentation.deeplabv3.aspp_dropout""" , 0.1 ) # id2label _SCREAMING_SNAKE_CASE = """huggingface/label-files""" _SCREAMING_SNAKE_CASE = json.load(open(hf_hub_download(UpperCamelCase__ , UpperCamelCase__ , repo_type="""dataset""" ) , """r""" ) ) _SCREAMING_SNAKE_CASE = {int(UpperCamelCase__ ): 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 lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE = dct.pop(UpperCamelCase__ ) _SCREAMING_SNAKE_CASE = val def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=False ) -> int: """simple docstring""" if base_model: _SCREAMING_SNAKE_CASE = """""" else: _SCREAMING_SNAKE_CASE = """mobilevitv2.""" _SCREAMING_SNAKE_CASE = [] for k in state_dict.keys(): if k[:8] == "encoder.": _SCREAMING_SNAKE_CASE = k[8:] else: _SCREAMING_SNAKE_CASE = k if ".block." in k: _SCREAMING_SNAKE_CASE = k_new.replace(""".block.""" , """.""" ) if ".conv." in k: _SCREAMING_SNAKE_CASE = k_new.replace(""".conv.""" , """.convolution.""" ) if ".norm." in k: _SCREAMING_SNAKE_CASE = k_new.replace(""".norm.""" , """.normalization.""" ) if "conv_1." in k: _SCREAMING_SNAKE_CASE = k_new.replace("""conv_1.""" , F"{model_prefix}conv_stem." ) for i in [1, 2]: if F"layer_{i}." in k: _SCREAMING_SNAKE_CASE = k_new.replace(F"layer_{i}." , F"{model_prefix}encoder.layer.{i-1}.layer." ) if ".exp_1x1." in k: _SCREAMING_SNAKE_CASE = k_new.replace(""".exp_1x1.""" , """.expand_1x1.""" ) if ".red_1x1." in k: _SCREAMING_SNAKE_CASE = k_new.replace(""".red_1x1.""" , """.reduce_1x1.""" ) for i in [3, 4, 5]: if F"layer_{i}.0." in k: _SCREAMING_SNAKE_CASE = k_new.replace(F"layer_{i}.0." , F"{model_prefix}encoder.layer.{i-1}.downsampling_layer." ) if F"layer_{i}.1.local_rep.0." in k: _SCREAMING_SNAKE_CASE = k_new.replace(F"layer_{i}.1.local_rep.0." , F"{model_prefix}encoder.layer.{i-1}.conv_kxk." ) if F"layer_{i}.1.local_rep.1." in k: _SCREAMING_SNAKE_CASE = k_new.replace(F"layer_{i}.1.local_rep.1." , F"{model_prefix}encoder.layer.{i-1}.conv_1x1." ) for i in [3, 4, 5]: if i == 3: _SCREAMING_SNAKE_CASE = [0, 1] elif i == 4: _SCREAMING_SNAKE_CASE = [0, 1, 2, 3] elif i == 5: _SCREAMING_SNAKE_CASE = [0, 1, 2] for j in j_in: if F"layer_{i}.1.global_rep.{j}." in k: _SCREAMING_SNAKE_CASE = k_new.replace( F"layer_{i}.1.global_rep.{j}." , F"{model_prefix}encoder.layer.{i-1}.transformer.layer.{j}." ) if F"layer_{i}.1.global_rep.{j+1}." in k: _SCREAMING_SNAKE_CASE = k_new.replace( F"layer_{i}.1.global_rep.{j+1}." , F"{model_prefix}encoder.layer.{i-1}.layernorm." ) if F"layer_{i}.1.conv_proj." in k: _SCREAMING_SNAKE_CASE = k_new.replace(F"layer_{i}.1.conv_proj." , F"{model_prefix}encoder.layer.{i-1}.conv_projection." ) if "pre_norm_attn.0." in k: _SCREAMING_SNAKE_CASE = k_new.replace("""pre_norm_attn.0.""" , """layernorm_before.""" ) if "pre_norm_attn.1." in k: _SCREAMING_SNAKE_CASE = k_new.replace("""pre_norm_attn.1.""" , """attention.""" ) if "pre_norm_ffn.0." in k: _SCREAMING_SNAKE_CASE = k_new.replace("""pre_norm_ffn.0.""" , """layernorm_after.""" ) if "pre_norm_ffn.1." in k: _SCREAMING_SNAKE_CASE = k_new.replace("""pre_norm_ffn.1.""" , """ffn.conv1.""" ) if "pre_norm_ffn.3." in k: _SCREAMING_SNAKE_CASE = k_new.replace("""pre_norm_ffn.3.""" , """ffn.conv2.""" ) if "classifier.1." in k: _SCREAMING_SNAKE_CASE = k_new.replace("""classifier.1.""" , """classifier.""" ) if "seg_head." in k: _SCREAMING_SNAKE_CASE = k_new.replace("""seg_head.""" , """segmentation_head.""" ) if ".aspp_layer." in k: _SCREAMING_SNAKE_CASE = k_new.replace(""".aspp_layer.""" , """.""" ) if ".aspp_pool." in k: _SCREAMING_SNAKE_CASE = k_new.replace(""".aspp_pool.""" , """.""" ) rename_keys.append((k, k_new) ) return rename_keys def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = [] for k in state_dict.keys(): if k.startswith("""seg_head.aux_head.""" ): keys_to_ignore.append(UpperCamelCase__ ) for k in keys_to_ignore: state_dict.pop(UpperCamelCase__ , UpperCamelCase__ ) def lowerCAmelCase_ ( ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE = """http://images.cocodataset.org/val2017/000000039769.jpg""" # url = "https://cdn.britannica.com/86/141086-050-9D7C75EE/Gulfstream-G450-business-jet-passengers.jpg" _SCREAMING_SNAKE_CASE = Image.open(requests.get(UpperCamelCase__ , stream=UpperCamelCase__ ).raw ) return im @torch.no_grad() def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE = get_mobilevitva_config(UpperCamelCase__ , UpperCamelCase__ ) # load original state_dict _SCREAMING_SNAKE_CASE = torch.load(UpperCamelCase__ , map_location="""cpu""" ) # load huggingface model if task_name.startswith("""ade20k_""" ) or task_name.startswith("""voc_""" ): _SCREAMING_SNAKE_CASE = MobileViTVaForSemanticSegmentation(UpperCamelCase__ ).eval() _SCREAMING_SNAKE_CASE = False else: _SCREAMING_SNAKE_CASE = MobileViTVaForImageClassification(UpperCamelCase__ ).eval() _SCREAMING_SNAKE_CASE = False # remove and rename some keys of load the original model _SCREAMING_SNAKE_CASE = checkpoint remove_unused_keys(UpperCamelCase__ ) _SCREAMING_SNAKE_CASE = create_rename_keys(UpperCamelCase__ , base_model=UpperCamelCase__ ) for rename_key_src, rename_key_dest in rename_keys: rename_key(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) # load modified state_dict model.load_state_dict(UpperCamelCase__ ) # Check outputs on an image, prepared by MobileViTImageProcessor _SCREAMING_SNAKE_CASE = MobileViTImageProcessor(crop_size=config.image_size , size=config.image_size + 32 ) _SCREAMING_SNAKE_CASE = image_processor(images=prepare_img() , return_tensors="""pt""" ) _SCREAMING_SNAKE_CASE = model(**UpperCamelCase__ ) # verify classification model if task_name.startswith("""imagenet""" ): _SCREAMING_SNAKE_CASE = outputs.logits _SCREAMING_SNAKE_CASE = logits.argmax(-1 ).item() print("""Predicted class:""" , model.config.idalabel[predicted_class_idx] ) if task_name.startswith("""imagenet1k_256""" ) and config.width_multiplier == 1.0: # expected_logits for base variant _SCREAMING_SNAKE_CASE = torch.tensor([-1.6336e00, -7.3204e-02, -5.1883e-01] ) assert torch.allclose(logits[0, :3] , UpperCamelCase__ , atol=1e-4 ) Path(UpperCamelCase__ ).mkdir(exist_ok=UpperCamelCase__ ) print(F"Saving model {task_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(UpperCamelCase__ ) print(F"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(UpperCamelCase__ ) if __name__ == "__main__": UpperCamelCase__ : List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--task", default="imagenet1k_256", type=str, help=( "Name of the task for which the MobileViTV2 model you\'d like to convert is trained on . " "\n Classification (ImageNet-1k)\n - MobileViTV2 (256x256) : imagenet1k_256\n - MobileViTV2 (Trained on 256x256 and Finetuned on 384x384) : imagenet1k_384\n - MobileViTV2 (Trained on ImageNet-21k and Finetuned on ImageNet-1k 256x256) :\n imagenet21k_to_1k_256\n - MobileViTV2 (Trained on ImageNet-21k, Finetuned on ImageNet-1k 256x256, and Finetuned on\n ImageNet-1k 384x384) : imagenet21k_to_1k_384\n Segmentation\n - ADE20K Dataset : ade20k_deeplabv3\n - Pascal VOC 2012 Dataset: voc_deeplabv3\n " ), choices=[ "imagenet1k_256", "imagenet1k_384", "imagenet21k_to_1k_256", "imagenet21k_to_1k_384", "ade20k_deeplabv3", "voc_deeplabv3", ], ) parser.add_argument( "--orig_checkpoint_path", required=True, type=str, help="Path to the original state dict (.pt file)." ) parser.add_argument("--orig_config_path", required=True, type=str, help="Path to the original config file.") parser.add_argument( "--pytorch_dump_folder_path", required=True, type=str, help="Path to the output PyTorch model directory." ) UpperCamelCase__ : Any = parser.parse_args() convert_mobilevitva_checkpoint( args.task, args.orig_checkpoint_path, args.orig_config_path, args.pytorch_dump_folder_path )
719
'''simple docstring''' import pyarrow.parquet as pq import pytest from datasets import Audio, Dataset, DatasetDict, Features, NamedSplit, Sequence, Value, config from datasets.features.image import Image from datasets.io.parquet import ParquetDatasetReader, ParquetDatasetWriter, get_writer_batch_size from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" assert isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("""keep_in_memory""" , [False, True] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): _SCREAMING_SNAKE_CASE = ParquetDatasetReader(SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ , keep_in_memory=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_dataset(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @pytest.mark.parametrize( """features""" , [ None, {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}, {"""col_1""": """string""", """col_2""": """string""", """col_3""": """string"""}, {"""col_1""": """int32""", """col_2""": """int32""", """col_3""": """int32"""}, {"""col_1""": """float32""", """col_2""": """float32""", """col_3""": """float32"""}, ] , ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} _SCREAMING_SNAKE_CASE = features.copy() if features else default_expected_features _SCREAMING_SNAKE_CASE = ( Features({feature: Value(SCREAMING_SNAKE_CASE_ ) for feature, dtype in features.items()} ) if features is not None else None ) _SCREAMING_SNAKE_CASE = ParquetDatasetReader(SCREAMING_SNAKE_CASE_ , features=SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_dataset(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @pytest.mark.parametrize("""split""" , [None, NamedSplit("""train""" ), """train""", """test"""] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} _SCREAMING_SNAKE_CASE = ParquetDatasetReader(SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ , split=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_dataset(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert dataset.split == split if split else "train" @pytest.mark.parametrize("""path_type""" , [str, list] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" if issubclass(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE = parquet_path elif issubclass(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE = [parquet_path] _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} _SCREAMING_SNAKE_CASE = ParquetDatasetReader(SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_dataset(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=("train",) ) -> List[str]: """simple docstring""" assert isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) for split in splits: _SCREAMING_SNAKE_CASE = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("""keep_in_memory""" , [False, True] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): _SCREAMING_SNAKE_CASE = ParquetDatasetReader( {"""train""": parquet_path} , cache_dir=SCREAMING_SNAKE_CASE_ , keep_in_memory=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_datasetdict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @pytest.mark.parametrize( """features""" , [ None, {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}, {"""col_1""": """string""", """col_2""": """string""", """col_3""": """string"""}, {"""col_1""": """int32""", """col_2""": """int32""", """col_3""": """int32"""}, {"""col_1""": """float32""", """col_2""": """float32""", """col_3""": """float32"""}, ] , ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} _SCREAMING_SNAKE_CASE = features.copy() if features else default_expected_features _SCREAMING_SNAKE_CASE = ( Features({feature: Value(SCREAMING_SNAKE_CASE_ ) for feature, dtype in features.items()} ) if features is not None else None ) _SCREAMING_SNAKE_CASE = ParquetDatasetReader({"""train""": parquet_path} , features=SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_datasetdict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @pytest.mark.parametrize("""split""" , [None, NamedSplit("""train""" ), """train""", """test"""] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" if split: _SCREAMING_SNAKE_CASE = {split: parquet_path} else: _SCREAMING_SNAKE_CASE = """train""" _SCREAMING_SNAKE_CASE = {"""train""": parquet_path, """test""": parquet_path} _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} _SCREAMING_SNAKE_CASE = ParquetDatasetReader(SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_datasetdict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE = ParquetDatasetWriter(SCREAMING_SNAKE_CASE_ , tmp_path / """foo.parquet""" ) assert writer.write() > 0 _SCREAMING_SNAKE_CASE = pq.ParquetFile(tmp_path / """foo.parquet""" ) _SCREAMING_SNAKE_CASE = pf.read() assert dataset.data.table == output_table def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = str(shared_datadir / """test_image_rgb.jpg""" ) _SCREAMING_SNAKE_CASE = {"""image""": [image_path]} _SCREAMING_SNAKE_CASE = Features({"""image""": Image()} ) _SCREAMING_SNAKE_CASE = Dataset.from_dict(SCREAMING_SNAKE_CASE_ , features=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = ParquetDatasetWriter(SCREAMING_SNAKE_CASE_ , tmp_path / """foo.parquet""" ) assert writer.write() > 0 _SCREAMING_SNAKE_CASE = Dataset.from_parquet(str(tmp_path / """foo.parquet""" ) ) assert dataset.features == reloaded_dataset.features _SCREAMING_SNAKE_CASE = ParquetDatasetReader(str(tmp_path / """foo.parquet""" ) , streaming=SCREAMING_SNAKE_CASE_ ).read() assert dataset.features == reloaded_iterable_dataset.features @pytest.mark.parametrize( """feature, expected""" , [ (Features({"""foo""": Value("""int32""" )} ), None), (Features({"""image""": Image(), """foo""": Value("""int32""" )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS), (Features({"""nested""": Sequence(Audio() )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS), ] , ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" assert get_writer_batch_size(SCREAMING_SNAKE_CASE_ ) == expected
0
0
'''simple docstring''' 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 , A__ , A__=13 , A__=30 , A__=2 , A__=3 , A__=True , A__=True , A__=32 , A__=2 , A__=4 , A__=37 , A__="gelu" , A__=0.1 , A__=0.1 , A__=10 , A__=0.02 , A__=3 , A__=None , ) -> Optional[int]: _SCREAMING_SNAKE_CASE = parent _SCREAMING_SNAKE_CASE = batch_size _SCREAMING_SNAKE_CASE = image_size _SCREAMING_SNAKE_CASE = patch_size _SCREAMING_SNAKE_CASE = num_channels _SCREAMING_SNAKE_CASE = is_training _SCREAMING_SNAKE_CASE = use_labels _SCREAMING_SNAKE_CASE = hidden_size _SCREAMING_SNAKE_CASE = num_hidden_layers _SCREAMING_SNAKE_CASE = num_attention_heads _SCREAMING_SNAKE_CASE = intermediate_size _SCREAMING_SNAKE_CASE = hidden_act _SCREAMING_SNAKE_CASE = hidden_dropout_prob _SCREAMING_SNAKE_CASE = attention_probs_dropout_prob _SCREAMING_SNAKE_CASE = type_sequence_label_size _SCREAMING_SNAKE_CASE = initializer_range _SCREAMING_SNAKE_CASE = scope # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) _SCREAMING_SNAKE_CASE = (image_size // patch_size) ** 2 _SCREAMING_SNAKE_CASE = num_patches + 1 def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _SCREAMING_SNAKE_CASE = None if self.use_labels: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, labels def UpperCamelCase ( self ) -> Any: 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=UpperCAmelCase__ , initializer_range=self.initializer_range , ) def UpperCamelCase ( self , A__ , A__ , A__ ) -> List[str]: _SCREAMING_SNAKE_CASE = TFViTModel(config=UpperCAmelCase__ ) _SCREAMING_SNAKE_CASE = model(UpperCAmelCase__ , training=UpperCAmelCase__ ) 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. _SCREAMING_SNAKE_CASE = self.image_size // 2 _SCREAMING_SNAKE_CASE = pixel_values[:, :, :image_size, :image_size] _SCREAMING_SNAKE_CASE = model(UpperCAmelCase__ , interpolate_pos_encoding=UpperCAmelCase__ , training=UpperCAmelCase__ ) _SCREAMING_SNAKE_CASE = (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 , A__ , A__ , A__ ) -> Tuple: _SCREAMING_SNAKE_CASE = self.type_sequence_label_size _SCREAMING_SNAKE_CASE = TFViTForImageClassification(UpperCAmelCase__ ) _SCREAMING_SNAKE_CASE = model(UpperCAmelCase__ , labels=UpperCAmelCase__ , training=UpperCAmelCase__ ) 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. _SCREAMING_SNAKE_CASE = self.image_size // 2 _SCREAMING_SNAKE_CASE = pixel_values[:, :, :image_size, :image_size] _SCREAMING_SNAKE_CASE = model(UpperCAmelCase__ , interpolate_pos_encoding=UpperCAmelCase__ , training=UpperCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images _SCREAMING_SNAKE_CASE = 1 _SCREAMING_SNAKE_CASE = TFViTForImageClassification(UpperCAmelCase__ ) _SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) _SCREAMING_SNAKE_CASE = model(UpperCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def UpperCamelCase ( self ) -> Dict: _SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = config_and_inputs _SCREAMING_SNAKE_CASE = {"""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 ) -> int: _SCREAMING_SNAKE_CASE = TFViTModelTester(self ) _SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=UpperCAmelCase__ , has_text_modality=UpperCAmelCase__ , hidden_size=37 ) def UpperCamelCase ( self ) -> Optional[Any]: self.config_tester.run_common_tests() @unittest.skip(reason="""ViT does not use inputs_embeds""" ) def UpperCamelCase ( self ) -> Tuple: pass @unittest.skip(reason="""ViT does not use inputs_embeds""" ) def UpperCamelCase ( self ) -> List[str]: pass def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _SCREAMING_SNAKE_CASE = model_class(UpperCAmelCase__ ) self.assertIsInstance(model.get_input_embeddings() , (tf.keras.layers.Layer) ) _SCREAMING_SNAKE_CASE = model.get_output_embeddings() self.assertTrue(x is None or isinstance(UpperCAmelCase__ , tf.keras.layers.Layer ) ) def UpperCamelCase ( self ) -> int: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _SCREAMING_SNAKE_CASE = model_class(UpperCAmelCase__ ) _SCREAMING_SNAKE_CASE = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] _SCREAMING_SNAKE_CASE = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , UpperCAmelCase__ ) def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCAmelCase__ ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase__ ) @slow def UpperCamelCase ( self ) -> int: _SCREAMING_SNAKE_CASE = TFViTModel.from_pretrained("""google/vit-base-patch16-224""" ) self.assertIsNotNone(UpperCAmelCase__ ) def lowerCAmelCase_ ( ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE = 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 ) -> str: return ViTImageProcessor.from_pretrained("""google/vit-base-patch16-224""" ) if is_vision_available() else None @slow def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = TFViTForImageClassification.from_pretrained("""google/vit-base-patch16-224""" ) _SCREAMING_SNAKE_CASE = self.default_image_processor _SCREAMING_SNAKE_CASE = prepare_img() _SCREAMING_SNAKE_CASE = image_processor(images=UpperCAmelCase__ , return_tensors="""tf""" ) # forward pass _SCREAMING_SNAKE_CASE = model(**UpperCAmelCase__ ) # verify the logits _SCREAMING_SNAKE_CASE = tf.TensorShape((1, 10_00) ) self.assertEqual(outputs.logits.shape , UpperCAmelCase__ ) _SCREAMING_SNAKE_CASE = tf.constant([-0.2744, 0.8215, -0.0836] ) tf.debugging.assert_near(outputs.logits[0, :3] , UpperCAmelCase__ , atol=1E-4 )
720
'''simple docstring''' def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): raise ValueError("""multiplicative_persistence() only accepts integral values""" ) if num < 0: raise ValueError("""multiplicative_persistence() does not accept negative values""" ) _SCREAMING_SNAKE_CASE = 0 _SCREAMING_SNAKE_CASE = str(SCREAMING_SNAKE_CASE_ ) while len(SCREAMING_SNAKE_CASE_ ) != 1: _SCREAMING_SNAKE_CASE = [int(SCREAMING_SNAKE_CASE_ ) for i in num_string] _SCREAMING_SNAKE_CASE = 1 for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) ): total *= numbers[i] _SCREAMING_SNAKE_CASE = str(SCREAMING_SNAKE_CASE_ ) steps += 1 return steps def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): raise ValueError("""additive_persistence() only accepts integral values""" ) if num < 0: raise ValueError("""additive_persistence() does not accept negative values""" ) _SCREAMING_SNAKE_CASE = 0 _SCREAMING_SNAKE_CASE = str(SCREAMING_SNAKE_CASE_ ) while len(SCREAMING_SNAKE_CASE_ ) != 1: _SCREAMING_SNAKE_CASE = [int(SCREAMING_SNAKE_CASE_ ) for i in num_string] _SCREAMING_SNAKE_CASE = 0 for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) ): total += numbers[i] _SCREAMING_SNAKE_CASE = str(SCREAMING_SNAKE_CASE_ ) steps += 1 return steps if __name__ == "__main__": import doctest doctest.testmod()
0
0
'''simple docstring''' from typing import Dict, List, Optional, Union import numpy as np from transformers.utils import is_vision_available from transformers.utils.generic import TensorType from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, is_valid_image, to_numpy_array, valid_images, ) from ...utils import logging if is_vision_available(): import PIL UpperCamelCase__ : Dict = logging.get_logger(__name__) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> List[List[ImageInput]]: """simple docstring""" if isinstance(SCREAMING_SNAKE_CASE_ , (list, tuple) ) and isinstance(videos[0] , (list, tuple) ) and is_valid_image(videos[0][0] ): return videos elif isinstance(SCREAMING_SNAKE_CASE_ , (list, tuple) ) and is_valid_image(videos[0] ): return [videos] elif is_valid_image(SCREAMING_SNAKE_CASE_ ): return [[videos]] raise ValueError(F"Could not make batched video from {videos}" ) class _a (__a): """simple docstring""" SCREAMING_SNAKE_CASE = ['pixel_values'] def __init__( self , A__ = True , A__ = None , A__ = PILImageResampling.BILINEAR , A__ = True , A__ = None , A__ = True , A__ = 1 / 2_55 , A__ = True , A__ = True , A__ = None , A__ = None , **A__ , ) -> None: super().__init__(**A__ ) _SCREAMING_SNAKE_CASE = size if size is not None else {"""shortest_edge""": 2_56} _SCREAMING_SNAKE_CASE = get_size_dict(A__ , default_to_square=A__ ) _SCREAMING_SNAKE_CASE = crop_size if crop_size is not None else {"""height""": 2_24, """width""": 2_24} _SCREAMING_SNAKE_CASE = get_size_dict(A__ , param_name="""crop_size""" ) _SCREAMING_SNAKE_CASE = do_resize _SCREAMING_SNAKE_CASE = size _SCREAMING_SNAKE_CASE = do_center_crop _SCREAMING_SNAKE_CASE = crop_size _SCREAMING_SNAKE_CASE = resample _SCREAMING_SNAKE_CASE = do_rescale _SCREAMING_SNAKE_CASE = rescale_factor _SCREAMING_SNAKE_CASE = offset _SCREAMING_SNAKE_CASE = do_normalize _SCREAMING_SNAKE_CASE = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN _SCREAMING_SNAKE_CASE = image_std if image_std is not None else IMAGENET_STANDARD_STD def UpperCamelCase ( self , A__ , A__ , A__ = PILImageResampling.BILINEAR , A__ = None , **A__ , ) -> np.ndarray: _SCREAMING_SNAKE_CASE = get_size_dict(A__ , default_to_square=A__ ) if "shortest_edge" in size: _SCREAMING_SNAKE_CASE = get_resize_output_image_size(A__ , size["""shortest_edge"""] , default_to_square=A__ ) elif "height" in size and "width" in size: _SCREAMING_SNAKE_CASE = (size["""height"""], size["""width"""]) else: raise ValueError(F"Size must have 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}" ) return resize(A__ , size=A__ , resample=A__ , data_format=A__ , **A__ ) def UpperCamelCase ( self , A__ , A__ , A__ = None , **A__ , ) -> np.ndarray: _SCREAMING_SNAKE_CASE = get_size_dict(A__ ) if "height" not in size or "width" not in size: raise ValueError(F"Size must have 'height' and 'width' as keys. Got {size.keys()}" ) return center_crop(A__ , size=(size["""height"""], size["""width"""]) , data_format=A__ , **A__ ) def UpperCamelCase ( self , A__ , A__ , A__ = True , A__ = None , **A__ , ) -> List[Any]: _SCREAMING_SNAKE_CASE = image.astype(np.floataa ) if offset: _SCREAMING_SNAKE_CASE = image - (scale / 2) return rescale(A__ , scale=A__ , data_format=A__ , **A__ ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ = None , **A__ , ) -> np.ndarray: return normalize(A__ , mean=A__ , std=A__ , data_format=A__ , **A__ ) def UpperCamelCase ( self , A__ , A__ = None , A__ = None , A__ = None , A__ = None , A__ = None , A__ = None , A__ = None , A__ = None , A__ = None , A__ = None , A__ = None , A__ = ChannelDimension.FIRST , ) -> np.ndarray: if do_resize and size is None or resample is None: raise ValueError("""Size and resample must be specified if do_resize is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("""Image mean and std must be specified if do_normalize is True.""" ) if offset and not do_rescale: raise ValueError("""For offset, do_rescale must also be set to True.""" ) # All transformations expect numpy arrays. _SCREAMING_SNAKE_CASE = to_numpy_array(A__ ) if do_resize: _SCREAMING_SNAKE_CASE = self.resize(image=A__ , size=A__ , resample=A__ ) if do_center_crop: _SCREAMING_SNAKE_CASE = self.center_crop(A__ , size=A__ ) if do_rescale: _SCREAMING_SNAKE_CASE = self.rescale(image=A__ , scale=A__ , offset=A__ ) if do_normalize: _SCREAMING_SNAKE_CASE = self.normalize(image=A__ , mean=A__ , std=A__ ) _SCREAMING_SNAKE_CASE = to_channel_dimension_format(A__ , A__ ) return image def UpperCamelCase ( self , A__ , A__ = None , A__ = None , A__ = None , A__ = None , A__ = None , A__ = None , A__ = None , A__ = None , A__ = None , A__ = None , A__ = None , A__ = None , A__ = ChannelDimension.FIRST , **A__ , ) -> PIL.Image.Image: _SCREAMING_SNAKE_CASE = do_resize if do_resize is not None else self.do_resize _SCREAMING_SNAKE_CASE = resample if resample is not None else self.resample _SCREAMING_SNAKE_CASE = do_center_crop if do_center_crop is not None else self.do_center_crop _SCREAMING_SNAKE_CASE = do_rescale if do_rescale is not None else self.do_rescale _SCREAMING_SNAKE_CASE = rescale_factor if rescale_factor is not None else self.rescale_factor _SCREAMING_SNAKE_CASE = offset if offset is not None else self.offset _SCREAMING_SNAKE_CASE = do_normalize if do_normalize is not None else self.do_normalize _SCREAMING_SNAKE_CASE = image_mean if image_mean is not None else self.image_mean _SCREAMING_SNAKE_CASE = image_std if image_std is not None else self.image_std _SCREAMING_SNAKE_CASE = size if size is not None else self.size _SCREAMING_SNAKE_CASE = get_size_dict(A__ , default_to_square=A__ ) _SCREAMING_SNAKE_CASE = crop_size if crop_size is not None else self.crop_size _SCREAMING_SNAKE_CASE = get_size_dict(A__ , param_name="""crop_size""" ) 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.""" ) _SCREAMING_SNAKE_CASE = make_batched(A__ ) _SCREAMING_SNAKE_CASE = [ [ self._preprocess_image( image=A__ , do_resize=A__ , size=A__ , resample=A__ , do_center_crop=A__ , crop_size=A__ , do_rescale=A__ , rescale_factor=A__ , offset=A__ , do_normalize=A__ , image_mean=A__ , image_std=A__ , data_format=A__ , ) for img in video ] for video in videos ] _SCREAMING_SNAKE_CASE = {"""pixel_values""": videos} return BatchFeature(data=A__ , tensor_type=A__ )
721
'''simple docstring''' import math import os import re import sys import unittest from pathlib import Path from typing import Tuple from unittest.mock import patch from parameterized import parameterized from transformers.testing_utils import ( CaptureStderr, ExtendSysPath, TestCasePlus, execute_subprocess_async, get_gpu_count, get_torch_dist_unique_port, require_apex, require_bitsandbytes, require_fairscale, require_torch, require_torch_gpu, require_torch_multi_gpu, require_torch_non_multi_gpu, slow, ) from transformers.trainer_callback import TrainerState from transformers.trainer_utils import set_seed UpperCamelCase__ : Tuple = os.path.abspath(os.path.dirname(__file__)) with ExtendSysPath(f"""{bindir}/../../examples/pytorch/translation"""): from run_translation import main # noqa set_seed(42) UpperCamelCase__ : Union[str, Any] = "sshleifer/student_marian_en_ro_6_1" UpperCamelCase__ : str = "sshleifer/tiny-mbart" @require_torch class _a (_lowerCamelCase): """simple docstring""" def UpperCamelCase ( self , A__=False , A__=None , A__=True , A__=True , A__=True , A__=True , ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = self.run_trainer( eval_steps=1 , max_len=12 , model_name=A__ , num_train_epochs=1 , distributed=A__ , extra_args_str=A__ , predict_with_generate=A__ , do_train=A__ , do_eval=A__ , do_predict=A__ , ) _SCREAMING_SNAKE_CASE = TrainerState.load_from_json(os.path.join(A__ , """trainer_state.json""" ) ).log_history if not do_eval: return _SCREAMING_SNAKE_CASE = [log for log in logs if """eval_loss""" in log.keys()] _SCREAMING_SNAKE_CASE = eval_metrics[0] if predict_with_generate: assert "eval_bleu" in first_step_stats _SCREAMING_SNAKE_CASE = eval_metrics[-1] assert isinstance(last_step_stats["""eval_bleu"""] , A__ ) assert not math.isnan(float(last_step_stats["""eval_loss"""] ) ), "eval_loss must not be `nan`" @require_torch_non_multi_gpu def UpperCamelCase ( self ) -> Optional[int]: self.run_seqaseq_quick() @require_torch_multi_gpu def UpperCamelCase ( self ) -> Optional[Any]: self.run_seqaseq_quick(distributed=A__ ) @require_torch_multi_gpu def UpperCamelCase ( self ) -> Union[str, Any]: self.run_seqaseq_quick(distributed=A__ ) @unittest.skip("""Requires an update of the env running those tests""" ) @require_torch_multi_gpu @require_fairscale def UpperCamelCase ( self ) -> Any: self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--sharded_ddp simple""" ) @unittest.skip("""Requires an update of the env running those tests""" ) @require_torch_multi_gpu @require_fairscale def UpperCamelCase ( self ) -> Tuple: self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--sharded_ddp simple --fp16""" ) @unittest.skip("""Requires an update of the env running those tests""" ) @require_torch_multi_gpu @require_fairscale def UpperCamelCase ( self ) -> str: self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--sharded_ddp zero_dp_2""" , predict_with_generate=A__ ) @unittest.skip("""Requires an update of the env running those tests""" ) @require_torch_multi_gpu @require_fairscale def UpperCamelCase ( self ) -> List[str]: self.run_seqaseq_quick( distributed=A__ , extra_args_str="""--sharded_ddp zero_dp_2 --fp16""" , predict_with_generate=A__ ) @require_apex @require_torch_gpu def UpperCamelCase ( self ) -> Optional[Any]: # XXX: apex breaks the trainer if it's run twice e.g. run_seq2seq.main() from the same # program and it breaks other tests that run from the same pytest worker, therefore until this is # sorted out it must be run only in an external program, that is distributed=True in this # test and only under one or more gpus - if we want cpu will need to make a special test # # specifically to the problem traced it to self.optimizer.step() - if it's run 2nd time via # 2nd main() call it botches the future eval. # self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--fp16 --fp16_backend=apex""" ) # test 2nd time - was getting eval_loss': nan' # to reproduce the problem set distributed=False self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--fp16 --fp16_backend=apex""" ) @parameterized.expand(["""base""", """low""", """high""", """mixed"""] ) @require_torch_multi_gpu def UpperCamelCase ( self , A__ ) -> List[Any]: # as each sub-test is slow-ish split into multiple sub-tests to avoid CI timeout _SCREAMING_SNAKE_CASE = { # test with the default log_level - should be info and thus log info once """base""": {"""extra_args_str""": """""", """n_matches""": 1}, # test with low log_level and log_level_replica - should be noisy on all processes # now the info string should appear twice on 2 processes """low""": {"""extra_args_str""": """--log_level debug --log_level_replica debug""", """n_matches""": 2}, # test with high log_level and low log_level_replica # now the info string should appear once only on the replica """high""": {"""extra_args_str""": """--log_level error --log_level_replica debug""", """n_matches""": 1}, # test with high log_level and log_level_replica - should be quiet on all processes """mixed""": {"""extra_args_str""": """--log_level error --log_level_replica error""", """n_matches""": 0}, } _SCREAMING_SNAKE_CASE = experiments[experiment_id] _SCREAMING_SNAKE_CASE = {"""distributed""": True, """predict_with_generate""": False, """do_eval""": False, """do_predict""": False} _SCREAMING_SNAKE_CASE = """Running training""" with CaptureStderr() as cl: self.run_seqaseq_quick(**A__ , extra_args_str=data["""extra_args_str"""] ) _SCREAMING_SNAKE_CASE = len(re.findall(A__ , cl.err ) ) self.assertEqual(A__ , data["""n_matches"""] ) @slow def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = self.run_trainer( eval_steps=2 , max_len=1_28 , model_name=A__ , learning_rate=3E-4 , num_train_epochs=10 , distributed=A__ , ) # Check metrics _SCREAMING_SNAKE_CASE = TrainerState.load_from_json(os.path.join(A__ , """trainer_state.json""" ) ).log_history _SCREAMING_SNAKE_CASE = [log for log in logs if """eval_loss""" in log.keys()] _SCREAMING_SNAKE_CASE = eval_metrics[0] _SCREAMING_SNAKE_CASE = eval_metrics[-1] assert first_step_stats["eval_loss"] > last_step_stats["eval_loss"], "model learned nothing" assert isinstance(last_step_stats["""eval_bleu"""] , A__ ) # test if do_predict saves generations and metrics _SCREAMING_SNAKE_CASE = os.listdir(A__ ) _SCREAMING_SNAKE_CASE = {os.path.basename(A__ ) for p in contents} assert "generated_predictions.txt" in contents assert "predict_results.json" in contents @slow @require_bitsandbytes def UpperCamelCase ( self ) -> Dict: from transformers.training_args import OptimizerNames def train_and_return_metrics(A__ ) -> Tuple[int, float]: _SCREAMING_SNAKE_CASE = """--skip_memory_metrics 0""" _SCREAMING_SNAKE_CASE = self.run_trainer( max_len=1_28 , model_name=A__ , learning_rate=3E-4 , num_train_epochs=1 , optim=A__ , distributed=A__ , extra_args_str=A__ , do_eval=A__ , do_predict=A__ , n_gpus_to_use=1 , ) # Check metrics _SCREAMING_SNAKE_CASE = TrainerState.load_from_json(Path(A__ , """trainer_state.json""" ) ).log_history _SCREAMING_SNAKE_CASE = int(logs[0]["""train_mem_gpu_peaked_delta"""] / 2**20 ) _SCREAMING_SNAKE_CASE = int(logs[0]["""train_mem_gpu_alloc_delta"""] / 2**20 ) _SCREAMING_SNAKE_CASE = logs[0]["""train_loss"""] return gpu_peak_mem_mb, gpu_alloc_mem_mb, loss _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = train_and_return_metrics(OptimizerNames.ADAMW_TORCH.value ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = train_and_return_metrics(OptimizerNames.ADAMW_BNB.value ) _SCREAMING_SNAKE_CASE = gpu_alloc_mem_orig - gpu_alloc_mem_bnb _SCREAMING_SNAKE_CASE = gpu_peak_mem_orig + gpu_alloc_mem_orig _SCREAMING_SNAKE_CASE = gpu_peak_mem_bnb + gpu_alloc_mem_bnb _SCREAMING_SNAKE_CASE = gpu_total_mem_orig - gpu_total_mem_bnb # sshleifer/student_marian_en_ro_6_1 has 54M parameter, 29M of which is `nn.Embedding` which # doesn't get quantized and remains in fp32. Therefore we only have 25M parameters quantized # in 2 bytes and the diff in optim memory usage is derived as so: # # - normal 25*8=~200MB (8 bytes per param) # - bnb 25*2= ~50MB (2 bytes per param) # # Thus we should expect ~150MB total memory saved. # # Peak memory should be the same - the total should be different by about that same margin # # After leaving a small margin to accommodate for differences between gpus let's check # that we have at least 120MB in savings _SCREAMING_SNAKE_CASE = 1_20 # uncomment the following if this test starts failing - requires py38 for a new print feature # gpu_peak_mem_diff = gpu_peak_mem_orig - gpu_peak_mem_bnb # print(f"{gpu_alloc_mem_orig=}MB {gpu_peak_mem_orig=}MB {gpu_alloc_mem_orig+gpu_peak_mem_orig=}MB") # print(f" {gpu_alloc_mem_bnb=}MB {gpu_peak_mem_bnb=}MB {gpu_alloc_mem_bnb+gpu_peak_mem_bnb=}MB") # print(f"{gpu_alloc_mem_diff=}MB") # print(f"{gpu_peak_mem_diff=}MB") # print(f"{gpu_total_mem_orig=}MB, {gpu_total_mem_bnb=}MB") # print(f"{gpu_total_mem_diff=}MB, {gpu_total_mem_diff=}MB") self.assertGreater( A__ , A__ , """should use ~150MB less alloc gpu memory with BNB, compared to without it for this model but got""" F" a difference of {gpu_alloc_mem_diff}MB, with gpu_alloc_mem_orig={gpu_alloc_mem_orig}MB and" F" gpu_alloc_mem_bnb={gpu_alloc_mem_bnb}MB" , ) self.assertGreater( A__ , A__ , """should use ~150MB less total gpu memory with BNB, compared to without it for this model but got""" F" a difference of {gpu_total_mem_diff}MB, with gpu_total_mem_orig={gpu_total_mem_orig}MB and" F" gpu_total_mem_bnb={gpu_total_mem_bnb}MB" , ) self.assertEqual( A__ , A__ , F"loss should be the same, but got loss_orig={loss_orig}, loss_bnb={loss_bnb}" ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ = 3E-3 , A__ = "adafactor" , A__ = False , A__ = None , A__ = 0 , A__ = True , A__ = True , A__ = True , A__ = True , A__ = None , ) -> Dict: _SCREAMING_SNAKE_CASE = self.test_file_dir / """../fixtures/tests_samples/wmt_en_ro""" _SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() _SCREAMING_SNAKE_CASE = F"\n --model_name_or_path {model_name}\n --train_file {data_dir}/train.json\n --validation_file {data_dir}/val.json\n --test_file {data_dir}/test.json\n --output_dir {output_dir}\n --overwrite_output_dir\n --max_train_samples 8\n --max_source_length {max_len}\n --max_target_length {max_len}\n --do_train\n --num_train_epochs {str(A__ )}\n --per_device_train_batch_size 4\n --learning_rate {learning_rate}\n --warmup_steps 8\n --logging_steps 0\n --logging_strategy no\n --save_steps {str(A__ )}\n --group_by_length\n --label_smoothing_factor 0.1\n --target_lang ro_RO\n --source_lang en_XX\n ".split() _SCREAMING_SNAKE_CASE = F"\n --do_eval\n --per_device_eval_batch_size 4\n --max_eval_samples 8\n --val_max_target_length {max_len}\n --evaluation_strategy steps\n --eval_steps {str(A__ )}\n ".split() _SCREAMING_SNAKE_CASE = """ --do_predict """.split() _SCREAMING_SNAKE_CASE = [] if do_train: args += args_train if do_eval: args += args_eval if do_predict: args += args_predict if predict_with_generate: args += "--predict_with_generate".split() if do_train: if optim == "adafactor": args += "--adafactor".split() else: args += F"--optim {optim}".split() if extra_args_str is not None: args += extra_args_str.split() if distributed: if n_gpus_to_use is None: _SCREAMING_SNAKE_CASE = get_gpu_count() _SCREAMING_SNAKE_CASE = get_torch_dist_unique_port() _SCREAMING_SNAKE_CASE = F"\n -m torch.distributed.run\n --nproc_per_node={n_gpus_to_use}\n --master_port={master_port}\n {self.examples_dir_str}/pytorch/translation/run_translation.py\n ".split() _SCREAMING_SNAKE_CASE = [sys.executable] + distributed_args + args # keep for quick debug # print(" ".join([f"\nPYTHONPATH={self.src_dir_str}"] +cmd)); die execute_subprocess_async(A__ , env=self.get_env() ) else: _SCREAMING_SNAKE_CASE = ["""run_translation.py"""] + args with patch.object(A__ , """argv""" , A__ ): main() return output_dir
0
0
'''simple docstring''' from __future__ import annotations import copy import inspect import unittest import numpy as np from transformers import is_tf_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST, TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, TFLayoutLMvaModel, ) if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class _a : def __init__( self , A__ , A__=2 , A__=3 , A__=4 , A__=2 , A__=7 , A__=True , A__=True , A__=True , A__=True , A__=99 , A__=36 , A__=2 , A__=4 , A__=37 , A__="gelu" , A__=0.1 , A__=0.1 , A__=5_12 , A__=16 , A__=2 , A__=0.02 , A__=6 , A__=6 , A__=3 , A__=4 , A__=None , A__=10_00 , ) -> List[str]: _SCREAMING_SNAKE_CASE = parent _SCREAMING_SNAKE_CASE = batch_size _SCREAMING_SNAKE_CASE = num_channels _SCREAMING_SNAKE_CASE = image_size _SCREAMING_SNAKE_CASE = patch_size _SCREAMING_SNAKE_CASE = is_training _SCREAMING_SNAKE_CASE = use_input_mask _SCREAMING_SNAKE_CASE = use_token_type_ids _SCREAMING_SNAKE_CASE = use_labels _SCREAMING_SNAKE_CASE = vocab_size _SCREAMING_SNAKE_CASE = hidden_size _SCREAMING_SNAKE_CASE = num_hidden_layers _SCREAMING_SNAKE_CASE = num_attention_heads _SCREAMING_SNAKE_CASE = intermediate_size _SCREAMING_SNAKE_CASE = hidden_act _SCREAMING_SNAKE_CASE = hidden_dropout_prob _SCREAMING_SNAKE_CASE = attention_probs_dropout_prob _SCREAMING_SNAKE_CASE = max_position_embeddings _SCREAMING_SNAKE_CASE = type_vocab_size _SCREAMING_SNAKE_CASE = type_sequence_label_size _SCREAMING_SNAKE_CASE = initializer_range _SCREAMING_SNAKE_CASE = coordinate_size _SCREAMING_SNAKE_CASE = shape_size _SCREAMING_SNAKE_CASE = num_labels _SCREAMING_SNAKE_CASE = num_choices _SCREAMING_SNAKE_CASE = scope _SCREAMING_SNAKE_CASE = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) _SCREAMING_SNAKE_CASE = text_seq_length _SCREAMING_SNAKE_CASE = (image_size // patch_size) ** 2 + 1 _SCREAMING_SNAKE_CASE = self.text_seq_length + self.image_seq_length def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size ) _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.text_seq_length, 4] , self.range_bbox ) _SCREAMING_SNAKE_CASE = bbox.numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: _SCREAMING_SNAKE_CASE = bbox[i, j, 3] _SCREAMING_SNAKE_CASE = bbox[i, j, 1] _SCREAMING_SNAKE_CASE = tmp_coordinate if bbox[i, j, 2] < bbox[i, j, 0]: _SCREAMING_SNAKE_CASE = bbox[i, j, 2] _SCREAMING_SNAKE_CASE = bbox[i, j, 0] _SCREAMING_SNAKE_CASE = tmp_coordinate _SCREAMING_SNAKE_CASE = tf.constant(A__ ) _SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _SCREAMING_SNAKE_CASE = None if self.use_input_mask: _SCREAMING_SNAKE_CASE = random_attention_mask([self.batch_size, self.text_seq_length] ) _SCREAMING_SNAKE_CASE = None if self.use_token_type_ids: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.text_seq_length] , self.type_vocab_size ) _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None if self.use_labels: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.text_seq_length] , self.num_labels ) _SCREAMING_SNAKE_CASE = LayoutLMvaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , coordinate_size=self.coordinate_size , shape_size=self.shape_size , input_size=self.image_size , patch_size=self.patch_size , ) return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ ) -> Tuple: _SCREAMING_SNAKE_CASE = TFLayoutLMvaModel(config=A__ ) # text + image _SCREAMING_SNAKE_CASE = model(A__ , pixel_values=A__ , training=A__ ) _SCREAMING_SNAKE_CASE = model( A__ , bbox=A__ , pixel_values=A__ , attention_mask=A__ , token_type_ids=A__ , training=A__ , ) _SCREAMING_SNAKE_CASE = model(A__ , bbox=A__ , pixel_values=A__ , training=A__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # text only _SCREAMING_SNAKE_CASE = model(A__ , training=A__ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size) ) # image only _SCREAMING_SNAKE_CASE = model({"""pixel_values""": pixel_values} , training=A__ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> Tuple: _SCREAMING_SNAKE_CASE = self.num_labels _SCREAMING_SNAKE_CASE = TFLayoutLMvaForSequenceClassification(config=A__ ) _SCREAMING_SNAKE_CASE = model( A__ , bbox=A__ , pixel_values=A__ , attention_mask=A__ , token_type_ids=A__ , labels=A__ , training=A__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> Tuple: _SCREAMING_SNAKE_CASE = self.num_labels _SCREAMING_SNAKE_CASE = TFLayoutLMvaForTokenClassification(config=A__ ) _SCREAMING_SNAKE_CASE = model( A__ , bbox=A__ , pixel_values=A__ , attention_mask=A__ , token_type_ids=A__ , labels=A__ , training=A__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = 2 _SCREAMING_SNAKE_CASE = TFLayoutLMvaForQuestionAnswering(config=A__ ) _SCREAMING_SNAKE_CASE = model( A__ , bbox=A__ , pixel_values=A__ , attention_mask=A__ , token_type_ids=A__ , start_positions=A__ , end_positions=A__ , training=A__ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = 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)) = config_and_inputs _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """bbox""": bbox, """pixel_values""": pixel_values, """token_type_ids""": token_type_ids, """attention_mask""": input_mask, } return config, inputs_dict @require_tf class _a (_lowerCamelCase , _lowerCamelCase , unittest.TestCase): SCREAMING_SNAKE_CASE = ( ( TFLayoutLMvaModel, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, ) if is_tf_available() else () ) SCREAMING_SNAKE_CASE = ( {'document-question-answering': TFLayoutLMvaForQuestionAnswering, 'feature-extraction': TFLayoutLMvaModel} if is_tf_available() else {} ) SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = False def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ ) -> List[Any]: return True def UpperCamelCase ( self , A__ , A__ , A__=False ) -> dict: _SCREAMING_SNAKE_CASE = copy.deepcopy(A__ ) if model_class in get_values(A__ ): _SCREAMING_SNAKE_CASE = { k: tf.tile(tf.expand_dims(A__ , 1 ) , (1, self.model_tester.num_choices) + (1,) * (v.ndim - 1) ) if isinstance(A__ , tf.Tensor ) and v.ndim > 0 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(A__ ): _SCREAMING_SNAKE_CASE = tf.ones(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(A__ ): _SCREAMING_SNAKE_CASE = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) _SCREAMING_SNAKE_CASE = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(A__ ): _SCREAMING_SNAKE_CASE = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(A__ ): _SCREAMING_SNAKE_CASE = tf.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=tf.intaa ) return inputs_dict def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = TFLayoutLMvaModelTester(self ) _SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=A__ , hidden_size=37 ) def UpperCamelCase ( self ) -> Union[str, Any]: self.config_tester.run_common_tests() def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _SCREAMING_SNAKE_CASE = model_class(A__ ) if getattr(A__ , """hf_compute_loss""" , A__ ): # The number of elements in the loss should be the same as the number of elements in the label _SCREAMING_SNAKE_CASE = self._prepare_for_class(inputs_dict.copy() , A__ , return_labels=A__ ) _SCREAMING_SNAKE_CASE = prepared_for_class[ sorted(prepared_for_class.keys() - inputs_dict.keys() , reverse=A__ )[0] ] _SCREAMING_SNAKE_CASE = added_label.shape.as_list()[:1] # Test that model correctly compute the loss with kwargs _SCREAMING_SNAKE_CASE = self._prepare_for_class(inputs_dict.copy() , A__ , return_labels=A__ ) _SCREAMING_SNAKE_CASE = prepared_for_class.pop("""input_ids""" ) _SCREAMING_SNAKE_CASE = model(A__ , **A__ )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss when we mask some positions _SCREAMING_SNAKE_CASE = self._prepare_for_class(inputs_dict.copy() , A__ , return_labels=A__ ) _SCREAMING_SNAKE_CASE = prepared_for_class.pop("""input_ids""" ) if "labels" in prepared_for_class: _SCREAMING_SNAKE_CASE = prepared_for_class["""labels"""].numpy() if len(labels.shape ) > 1 and labels.shape[1] != 1: _SCREAMING_SNAKE_CASE = -1_00 _SCREAMING_SNAKE_CASE = tf.convert_to_tensor(A__ ) _SCREAMING_SNAKE_CASE = model(A__ , **A__ )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) self.assertTrue(not np.any(np.isnan(loss.numpy() ) ) ) # Test that model correctly compute the loss with a dict _SCREAMING_SNAKE_CASE = self._prepare_for_class(inputs_dict.copy() , A__ , return_labels=A__ ) _SCREAMING_SNAKE_CASE = model(A__ )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss with a tuple _SCREAMING_SNAKE_CASE = self._prepare_for_class(inputs_dict.copy() , A__ , return_labels=A__ ) # Get keys that were added with the _prepare_for_class function _SCREAMING_SNAKE_CASE = prepared_for_class.keys() - inputs_dict.keys() _SCREAMING_SNAKE_CASE = inspect.signature(model.call ).parameters _SCREAMING_SNAKE_CASE = list(signature.keys() ) # Create a dictionary holding the location of the tensors in the tuple _SCREAMING_SNAKE_CASE = {0: """input_ids"""} for label_key in label_keys: _SCREAMING_SNAKE_CASE = signature_names.index(A__ ) _SCREAMING_SNAKE_CASE = label_key _SCREAMING_SNAKE_CASE = sorted(tuple_index_mapping.items() ) # Initialize a list with their default values, update the values and convert to a tuple _SCREAMING_SNAKE_CASE = [] for name in signature_names: if name != "kwargs": list_input.append(signature[name].default ) for index, value in sorted_tuple_index_mapping: _SCREAMING_SNAKE_CASE = prepared_for_class[value] _SCREAMING_SNAKE_CASE = tuple(A__ ) # Send to model _SCREAMING_SNAKE_CASE = model(tuple_input[:-1] )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) def UpperCamelCase ( self ) -> Tuple: ( ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ) = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(A__ , A__ , A__ , A__ , A__ , A__ ) def UpperCamelCase ( self ) -> Union[str, Any]: ( ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ) = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: _SCREAMING_SNAKE_CASE = type self.model_tester.create_and_check_model(A__ , A__ , A__ , A__ , A__ , A__ ) def UpperCamelCase ( self ) -> Any: ( ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ) = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification( A__ , A__ , A__ , A__ , A__ , A__ , A__ ) def UpperCamelCase ( self ) -> List[str]: ( ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ) = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification( A__ , A__ , A__ , A__ , A__ , A__ , A__ ) def UpperCamelCase ( self ) -> Optional[Any]: ( ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ( _SCREAMING_SNAKE_CASE ) , ) = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering( A__ , A__ , A__ , A__ , A__ , A__ , A__ ) @slow def UpperCamelCase ( self ) -> int: for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _SCREAMING_SNAKE_CASE = TFLayoutLMvaModel.from_pretrained(A__ ) self.assertIsNotNone(A__ ) def lowerCAmelCase_ ( ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_tf class _a (unittest.TestCase): @cached_property def UpperCamelCase ( self ) -> List[Any]: return LayoutLMvaImageProcessor(apply_ocr=A__ ) if is_vision_available() else None @slow def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = TFLayoutLMvaModel.from_pretrained("""microsoft/layoutlmv3-base""" ) _SCREAMING_SNAKE_CASE = self.default_image_processor _SCREAMING_SNAKE_CASE = prepare_img() _SCREAMING_SNAKE_CASE = image_processor(images=A__ , return_tensors="""tf""" ).pixel_values _SCREAMING_SNAKE_CASE = tf.constant([[1, 2]] ) _SCREAMING_SNAKE_CASE = tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]] ) , axis=0 ) # forward pass _SCREAMING_SNAKE_CASE = model(input_ids=A__ , bbox=A__ , pixel_values=A__ , training=A__ ) # verify the logits _SCREAMING_SNAKE_CASE = (1, 1_99, 7_68) self.assertEqual(outputs.last_hidden_state.shape , A__ ) _SCREAMING_SNAKE_CASE = tf.constant( [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]] ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] , A__ , atol=1E-4 ) )
700
'''simple docstring''' import sys UpperCamelCase__ : int = ( "73167176531330624919225119674426574742355349194934" "96983520312774506326239578318016984801869478851843" "85861560789112949495459501737958331952853208805511" "12540698747158523863050715693290963295227443043557" "66896648950445244523161731856403098711121722383113" "62229893423380308135336276614282806444486645238749" "30358907296290491560440772390713810515859307960866" "70172427121883998797908792274921901699720888093776" "65727333001053367881220235421809751254540594752243" "52584907711670556013604839586446706324415722155397" "53697817977846174064955149290862569321978468622482" "83972241375657056057490261407972968652414535100474" "82166370484403199890008895243450658541227588666881" "16427171479924442928230863465674813919123162824586" "17866458359124566529476545682848912883142607690042" "24219022671055626321111109370544217506941658960408" "07198403850962455444362981230987879927244284909188" "84580156166097919133875499200524063689912560717606" "05886116467109405077541002256983155200055935729725" "71636269561882670428252483600823257530420752963450" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ = N ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE = -sys.maxsize - 1 for i in range(len(SCREAMING_SNAKE_CASE_ ) - 12 ): _SCREAMING_SNAKE_CASE = 1 for j in range(13 ): product *= int(n[i + j] ) if product > largest_product: _SCREAMING_SNAKE_CASE = product return largest_product if __name__ == "__main__": print(f"""{solution() = }""")
0
0
'''simple docstring''' from ..utils import ( OptionalDependencyNotAvailable, is_flax_available, is_scipy_available, is_torch_available, is_torchsde_available, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_pt_objects import * # noqa F403 else: from .scheduling_consistency_models import CMStochasticIterativeScheduler from .scheduling_ddim import DDIMScheduler from .scheduling_ddim_inverse import DDIMInverseScheduler from .scheduling_ddim_parallel import DDIMParallelScheduler from .scheduling_ddpm import DDPMScheduler from .scheduling_ddpm_parallel import DDPMParallelScheduler from .scheduling_deis_multistep import DEISMultistepScheduler from .scheduling_dpmsolver_multistep import DPMSolverMultistepScheduler from .scheduling_dpmsolver_multistep_inverse import DPMSolverMultistepInverseScheduler from .scheduling_dpmsolver_singlestep import DPMSolverSinglestepScheduler from .scheduling_euler_ancestral_discrete import EulerAncestralDiscreteScheduler from .scheduling_euler_discrete import EulerDiscreteScheduler from .scheduling_heun_discrete import HeunDiscreteScheduler from .scheduling_ipndm import IPNDMScheduler from .scheduling_k_dpm_2_ancestral_discrete import KDPMaAncestralDiscreteScheduler from .scheduling_k_dpm_2_discrete import KDPMaDiscreteScheduler from .scheduling_karras_ve import KarrasVeScheduler from .scheduling_pndm import PNDMScheduler from .scheduling_repaint import RePaintScheduler from .scheduling_sde_ve import ScoreSdeVeScheduler from .scheduling_sde_vp import ScoreSdeVpScheduler from .scheduling_unclip import UnCLIPScheduler from .scheduling_unipc_multistep import UniPCMultistepScheduler from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin from .scheduling_vq_diffusion import VQDiffusionScheduler try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_flax_objects import * # noqa F403 else: from .scheduling_ddim_flax import FlaxDDIMScheduler from .scheduling_ddpm_flax import FlaxDDPMScheduler from .scheduling_dpmsolver_multistep_flax import FlaxDPMSolverMultistepScheduler from .scheduling_karras_ve_flax import FlaxKarrasVeScheduler from .scheduling_lms_discrete_flax import FlaxLMSDiscreteScheduler from .scheduling_pndm_flax import FlaxPNDMScheduler from .scheduling_sde_ve_flax import FlaxScoreSdeVeScheduler from .scheduling_utils_flax import ( FlaxKarrasDiffusionSchedulers, FlaxSchedulerMixin, FlaxSchedulerOutput, broadcast_to_shape_from_left, ) try: if not (is_torch_available() and is_scipy_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_torch_and_scipy_objects import * # noqa F403 else: from .scheduling_lms_discrete import LMSDiscreteScheduler try: if not (is_torch_available() and is_torchsde_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_torch_and_torchsde_objects import * # noqa F403 else: from .scheduling_dpmsolver_sde import DPMSolverSDEScheduler
701
'''simple docstring''' UpperCamelCase__ : Dict = { "a": "AAAAA", "b": "AAAAB", "c": "AAABA", "d": "AAABB", "e": "AABAA", "f": "AABAB", "g": "AABBA", "h": "AABBB", "i": "ABAAA", "j": "BBBAA", "k": "ABAAB", "l": "ABABA", "m": "ABABB", "n": "ABBAA", "o": "ABBAB", "p": "ABBBA", "q": "ABBBB", "r": "BAAAA", "s": "BAAAB", "t": "BAABA", "u": "BAABB", "v": "BBBAB", "w": "BABAA", "x": "BABAB", "y": "BABBA", "z": "BABBB", " ": " ", } UpperCamelCase__ : str = {value: key for key, value in encode_dict.items()} def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE = """""" for letter in word.lower(): if letter.isalpha() or letter == " ": encoded += encode_dict[letter] else: raise Exception("""encode() accepts only letters of the alphabet and spaces""" ) return encoded def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" if set(SCREAMING_SNAKE_CASE_ ) - {"A", "B", " "} != set(): raise Exception("""decode() accepts only 'A', 'B' and spaces""" ) _SCREAMING_SNAKE_CASE = """""" for word in coded.split(): while len(SCREAMING_SNAKE_CASE_ ) != 0: decoded += decode_dict[word[:5]] _SCREAMING_SNAKE_CASE = word[5:] decoded += " " return decoded.strip() if __name__ == "__main__": from doctest import testmod testmod()
0
0
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging UpperCamelCase__ : Union[str, Any] = logging.get_logger(__name__) UpperCamelCase__ : 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 _a (a__): """simple docstring""" SCREAMING_SNAKE_CASE = """xlm-roberta-xl""" def __init__( self , A__=25_08_80 , A__=25_60 , A__=36 , A__=32 , A__=1_02_40 , A__="gelu" , A__=0.1 , A__=0.1 , A__=5_14 , A__=1 , A__=0.02 , A__=1E-05 , A__=1 , A__=0 , A__=2 , A__="absolute" , A__=True , A__=None , **A__ , ) -> List[str]: super().__init__(pad_token_id=lowercase__ , bos_token_id=lowercase__ , eos_token_id=lowercase__ , **lowercase__ ) _SCREAMING_SNAKE_CASE = vocab_size _SCREAMING_SNAKE_CASE = hidden_size _SCREAMING_SNAKE_CASE = num_hidden_layers _SCREAMING_SNAKE_CASE = num_attention_heads _SCREAMING_SNAKE_CASE = hidden_act _SCREAMING_SNAKE_CASE = intermediate_size _SCREAMING_SNAKE_CASE = hidden_dropout_prob _SCREAMING_SNAKE_CASE = attention_probs_dropout_prob _SCREAMING_SNAKE_CASE = max_position_embeddings _SCREAMING_SNAKE_CASE = type_vocab_size _SCREAMING_SNAKE_CASE = initializer_range _SCREAMING_SNAKE_CASE = layer_norm_eps _SCREAMING_SNAKE_CASE = position_embedding_type _SCREAMING_SNAKE_CASE = use_cache _SCREAMING_SNAKE_CASE = classifier_dropout class _a (a__): """simple docstring""" @property def UpperCamelCase ( self ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": _SCREAMING_SNAKE_CASE = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: _SCREAMING_SNAKE_CASE = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
702
'''simple docstring''' import argparse import torch from torch import nn from transformers import MaMaaaConfig, MaMaaaForConditionalGeneration def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE = [ """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 lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = emb.weight.shape _SCREAMING_SNAKE_CASE = nn.Linear(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , bias=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = emb.weight.data return lin_layer def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = torch.load(SCREAMING_SNAKE_CASE_ , map_location="""cpu""" ) _SCREAMING_SNAKE_CASE = mam_aaa["""args"""] or mam_aaa["""cfg"""]["""model"""] _SCREAMING_SNAKE_CASE = mam_aaa["""model"""] remove_ignore_keys_(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = state_dict["""encoder.embed_tokens.weight"""].shape[0] _SCREAMING_SNAKE_CASE = MaMaaaConfig( vocab_size=SCREAMING_SNAKE_CASE_ , max_position_embeddings=10_24 , encoder_layers=args.encoder_layers , decoder_layers=args.decoder_layers , encoder_attention_heads=args.encoder_attention_heads , decoder_attention_heads=args.decoder_attention_heads , encoder_ffn_dim=args.encoder_ffn_embed_dim , decoder_ffn_dim=args.decoder_ffn_embed_dim , d_model=args.encoder_embed_dim , encoder_layerdrop=args.encoder_layerdrop , decoder_layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function="""relu""" , ) _SCREAMING_SNAKE_CASE = state_dict["""decoder.embed_tokens.weight"""] _SCREAMING_SNAKE_CASE = MaMaaaForConditionalGeneration(SCREAMING_SNAKE_CASE_ ) model.model.load_state_dict(SCREAMING_SNAKE_CASE_ , strict=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = make_linear_from_emb(model.model.shared ) return model if __name__ == "__main__": UpperCamelCase__ : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument("fairseq_path", type=str, help="path to a model.pt on local filesystem.") parser.add_argument("pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") UpperCamelCase__ : List[str] = parser.parse_args() UpperCamelCase__ : Any = convert_fairseq_mamaaa_checkpoint_from_disk(args.fairseq_pathß) model.save_pretrained(args.pytorch_dump_folder_path)
0
0
'''simple docstring''' import baseaa import io import json import os from copy import deepcopy from ..optimizer import AcceleratedOptimizer from ..scheduler import AcceleratedScheduler class _a : """simple docstring""" def __init__( self , A__ ) -> List[str]: if isinstance(lowercase__ , lowercase__ ): # 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 _SCREAMING_SNAKE_CASE = deepcopy(lowercase__ ) elif os.path.exists(lowercase__ ): with io.open(lowercase__ , """r""" , encoding="""utf-8""" ) as f: _SCREAMING_SNAKE_CASE = json.load(lowercase__ ) else: try: _SCREAMING_SNAKE_CASE = baseaa.urlsafe_baadecode(lowercase__ ).decode("""utf-8""" ) _SCREAMING_SNAKE_CASE = json.loads(lowercase__ ) 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}" ) _SCREAMING_SNAKE_CASE = config self.set_stage_and_offload() def UpperCamelCase ( self ) -> Optional[int]: # zero stage - this is done as early as possible, before model is created, to allow # ``is_deepspeed_zero3_enabled`` query and getting to the early deepspeed config object # during ``zero.Init()`` which needs to know the dtype, and some other hparams. _SCREAMING_SNAKE_CASE = self.get_value("""zero_optimization.stage""" , -1 ) # offload _SCREAMING_SNAKE_CASE = False if self.is_zeroa() or self.is_zeroa(): _SCREAMING_SNAKE_CASE = set(["""cpu""", """nvme"""] ) _SCREAMING_SNAKE_CASE = 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: _SCREAMING_SNAKE_CASE = True def UpperCamelCase ( self , A__ ) -> Optional[int]: _SCREAMING_SNAKE_CASE = self.config # find the config node of interest if it exists _SCREAMING_SNAKE_CASE = ds_key_long.split(""".""" ) _SCREAMING_SNAKE_CASE = nodes.pop() for node in nodes: _SCREAMING_SNAKE_CASE = config.get(lowercase__ ) if config is None: return None, ds_key return config, ds_key def UpperCamelCase ( self , A__ , A__=None ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.find_config_node(lowercase__ ) if config is None: return default return config.get(lowercase__ , lowercase__ ) def UpperCamelCase ( self , A__ , A__=False ) -> Any: _SCREAMING_SNAKE_CASE = self.config # find the config node of interest if it exists _SCREAMING_SNAKE_CASE = ds_key_long.split(""".""" ) for node in nodes: _SCREAMING_SNAKE_CASE = config _SCREAMING_SNAKE_CASE = config.get(lowercase__ ) 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(lowercase__ ) def UpperCamelCase ( self , A__ ) -> str: _SCREAMING_SNAKE_CASE = self.get_value(lowercase__ ) return False if value is None else bool(lowercase__ ) def UpperCamelCase ( self , A__ ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = self.get_value(lowercase__ ) return False if value is None else not bool(lowercase__ ) def UpperCamelCase ( self ) -> int: return self._stage == 2 def UpperCamelCase ( self ) -> Dict: return self._stage == 3 def UpperCamelCase ( self ) -> Any: return self._offload class _a : """simple docstring""" def __init__( self , A__ ) -> List[Any]: _SCREAMING_SNAKE_CASE = engine def UpperCamelCase ( self , A__ , **A__ ) -> List[str]: # runs backpropagation and handles mixed precision self.engine.backward(lowercase__ , **lowercase__ ) # 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 _a (_UpperCAmelCase): """simple docstring""" def __init__( self , A__ ) -> Union[str, Any]: super().__init__(lowercase__ , device_placement=lowercase__ , scaler=lowercase__ ) _SCREAMING_SNAKE_CASE = hasattr(self.optimizer , """overflow""" ) def UpperCamelCase ( self , A__=None ) -> Dict: pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed def UpperCamelCase ( self ) -> Any: pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed @property def UpperCamelCase ( self ) -> Optional[Any]: if self.__has_overflow__: return self.optimizer.overflow return False class _a (_UpperCAmelCase): """simple docstring""" def __init__( self , A__ , A__ ) -> Dict: super().__init__(lowercase__ , lowercase__ ) def UpperCamelCase ( self ) -> Optional[Any]: pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed class _a : """simple docstring""" def __init__( self , A__ , A__=0.001 , A__=0 , **A__ ) -> Optional[int]: _SCREAMING_SNAKE_CASE = params _SCREAMING_SNAKE_CASE = lr _SCREAMING_SNAKE_CASE = weight_decay _SCREAMING_SNAKE_CASE = kwargs class _a : """simple docstring""" def __init__( self , A__ , A__=None , A__=0 , **A__ ) -> Any: _SCREAMING_SNAKE_CASE = optimizer _SCREAMING_SNAKE_CASE = total_num_steps _SCREAMING_SNAKE_CASE = warmup_num_steps _SCREAMING_SNAKE_CASE = kwargs
703
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available UpperCamelCase__ : str = { "configuration_canine": ["CANINE_PRETRAINED_CONFIG_ARCHIVE_MAP", "CanineConfig"], "tokenization_canine": ["CanineTokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : List[Any] = [ "CANINE_PRETRAINED_MODEL_ARCHIVE_LIST", "CanineForMultipleChoice", "CanineForQuestionAnswering", "CanineForSequenceClassification", "CanineForTokenClassification", "CanineLayer", "CanineModel", "CaninePreTrainedModel", "load_tf_weights_in_canine", ] if TYPE_CHECKING: from .configuration_canine import CANINE_PRETRAINED_CONFIG_ARCHIVE_MAP, CanineConfig from .tokenization_canine import CanineTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_canine import ( CANINE_PRETRAINED_MODEL_ARCHIVE_LIST, CanineForMultipleChoice, CanineForQuestionAnswering, CanineForSequenceClassification, CanineForTokenClassification, CanineLayer, CanineModel, CaninePreTrainedModel, load_tf_weights_in_canine, ) else: import sys UpperCamelCase__ : str = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
0
0
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyImgaImgPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class _a (__SCREAMING_SNAKE_CASE , unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = KandinskyImgaImgPipeline SCREAMING_SNAKE_CASE = ['prompt', 'image_embeds', 'negative_image_embeds', 'image'] SCREAMING_SNAKE_CASE = [ 'prompt', 'negative_prompt', 'image_embeds', 'negative_image_embeds', 'image', ] SCREAMING_SNAKE_CASE = [ 'generator', 'height', 'width', 'strength', 'guidance_scale', 'negative_prompt', 'num_inference_steps', 'return_dict', 'guidance_scale', 'num_images_per_prompt', 'output_type', 'return_dict', ] SCREAMING_SNAKE_CASE = False @property def UpperCamelCase ( self ) -> Dict: return 32 @property def UpperCamelCase ( self ) -> Optional[Any]: return 32 @property def UpperCamelCase ( self ) -> Any: return self.time_input_dim @property def UpperCamelCase ( self ) -> Tuple: return self.time_input_dim * 4 @property def UpperCamelCase ( self ) -> Dict: return 1_00 @property def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = XLMRobertaTokenizerFast.from_pretrained("""YiYiXu/tiny-random-mclip-base""" ) return tokenizer @property def UpperCamelCase ( self ) -> Any: torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE = MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=37 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=10_05 , ) _SCREAMING_SNAKE_CASE = MultilingualCLIP(__snake_case ) _SCREAMING_SNAKE_CASE = text_encoder.eval() return text_encoder @property def UpperCamelCase ( self ) -> Dict: torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE = { '''in_channels''': 4, # Out channels is double in channels because predicts mean and variance '''out_channels''': 8, '''addition_embed_type''': '''text_image''', '''down_block_types''': ('''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D'''), '''up_block_types''': ('''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''), '''mid_block_type''': '''UNetMidBlock2DSimpleCrossAttn''', '''block_out_channels''': (self.block_out_channels_a, self.block_out_channels_a * 2), '''layers_per_block''': 1, '''encoder_hid_dim''': self.text_embedder_hidden_size, '''encoder_hid_dim_type''': '''text_image_proj''', '''cross_attention_dim''': self.cross_attention_dim, '''attention_head_dim''': 4, '''resnet_time_scale_shift''': '''scale_shift''', '''class_embed_type''': None, } _SCREAMING_SNAKE_CASE = UNetaDConditionModel(**__snake_case ) return model @property def UpperCamelCase ( self ) -> Dict: return { "block_out_channels": [32, 64], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def UpperCamelCase ( self ) -> Union[str, Any]: torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE = VQModel(**self.dummy_movq_kwargs ) return model def UpperCamelCase ( self ) -> int: _SCREAMING_SNAKE_CASE = self.dummy_text_encoder _SCREAMING_SNAKE_CASE = self.dummy_tokenizer _SCREAMING_SNAKE_CASE = self.dummy_unet _SCREAMING_SNAKE_CASE = self.dummy_movq _SCREAMING_SNAKE_CASE = { '''num_train_timesteps''': 10_00, '''beta_schedule''': '''linear''', '''beta_start''': 0.0_0085, '''beta_end''': 0.012, '''clip_sample''': False, '''set_alpha_to_one''': False, '''steps_offset''': 0, '''prediction_type''': '''epsilon''', '''thresholding''': False, } _SCREAMING_SNAKE_CASE = DDIMScheduler(**__snake_case ) _SCREAMING_SNAKE_CASE = { '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''unet''': unet, '''scheduler''': scheduler, '''movq''': movq, } return components def UpperCamelCase ( self , A__ , A__=0 ) -> Tuple: _SCREAMING_SNAKE_CASE = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(__snake_case ) ).to(__snake_case ) _SCREAMING_SNAKE_CASE = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(__snake_case ) # create init_image _SCREAMING_SNAKE_CASE = floats_tensor((1, 3, 64, 64) , rng=random.Random(__snake_case ) ).to(__snake_case ) _SCREAMING_SNAKE_CASE = image.cpu().permute(0 , 2 , 3 , 1 )[0] _SCREAMING_SNAKE_CASE = Image.fromarray(np.uinta(__snake_case ) ).convert("""RGB""" ).resize((2_56, 2_56) ) if str(__snake_case ).startswith("""mps""" ): _SCREAMING_SNAKE_CASE = torch.manual_seed(__snake_case ) else: _SCREAMING_SNAKE_CASE = torch.Generator(device=__snake_case ).manual_seed(__snake_case ) _SCREAMING_SNAKE_CASE = { '''prompt''': '''horse''', '''image''': init_image, '''image_embeds''': image_embeds, '''negative_image_embeds''': negative_image_embeds, '''generator''': generator, '''height''': 64, '''width''': 64, '''num_inference_steps''': 10, '''guidance_scale''': 7.0, '''strength''': 0.2, '''output_type''': '''np''', } return inputs def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = '''cpu''' _SCREAMING_SNAKE_CASE = self.get_dummy_components() _SCREAMING_SNAKE_CASE = self.pipeline_class(**__snake_case ) _SCREAMING_SNAKE_CASE = pipe.to(__snake_case ) pipe.set_progress_bar_config(disable=__snake_case ) _SCREAMING_SNAKE_CASE = pipe(**self.get_dummy_inputs(__snake_case ) ) _SCREAMING_SNAKE_CASE = output.images _SCREAMING_SNAKE_CASE = pipe( **self.get_dummy_inputs(__snake_case ) , return_dict=__snake_case , )[0] _SCREAMING_SNAKE_CASE = image[0, -3:, -3:, -1] _SCREAMING_SNAKE_CASE = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) _SCREAMING_SNAKE_CASE = np.array( [0.6147_4943, 0.607_3539, 0.4330_8544, 0.592_8269, 0.4749_3595, 0.4675_5973, 0.461_3838, 0.4536_8797, 0.5011_9233] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 ), F" expected_slice {expected_slice}, but got {image_slice.flatten()}" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 ), F" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}" @slow @require_torch_gpu class _a (unittest.TestCase): """simple docstring""" def UpperCamelCase ( self ) -> List[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinsky/kandinsky_img2img_frog.npy""" ) _SCREAMING_SNAKE_CASE = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinsky/cat.png""" ) _SCREAMING_SNAKE_CASE = '''A red cartoon frog, 4k''' _SCREAMING_SNAKE_CASE = KandinskyPriorPipeline.from_pretrained( """kandinsky-community/kandinsky-2-1-prior""" , torch_dtype=torch.floataa ) pipe_prior.to(__snake_case ) _SCREAMING_SNAKE_CASE = KandinskyImgaImgPipeline.from_pretrained( """kandinsky-community/kandinsky-2-1""" , torch_dtype=torch.floataa ) _SCREAMING_SNAKE_CASE = pipeline.to(__snake_case ) pipeline.set_progress_bar_config(disable=__snake_case ) _SCREAMING_SNAKE_CASE = torch.Generator(device="""cpu""" ).manual_seed(0 ) _SCREAMING_SNAKE_CASE = pipe_prior( __snake_case , generator=__snake_case , num_inference_steps=5 , negative_prompt="""""" , ).to_tuple() _SCREAMING_SNAKE_CASE = pipeline( __snake_case , image=__snake_case , image_embeds=__snake_case , negative_image_embeds=__snake_case , generator=__snake_case , num_inference_steps=1_00 , height=7_68 , width=7_68 , strength=0.2 , output_type="""np""" , ) _SCREAMING_SNAKE_CASE = output.images[0] assert image.shape == (7_68, 7_68, 3) assert_mean_pixel_difference(__snake_case , __snake_case )
704
'''simple docstring''' import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class _a (_lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = ['image_processor', 'tokenizer'] SCREAMING_SNAKE_CASE = 'ChineseCLIPImageProcessor' SCREAMING_SNAKE_CASE = ('BertTokenizer', 'BertTokenizerFast') def __init__( self , A__=None , A__=None , **A__ ) -> int: _SCREAMING_SNAKE_CASE = None if "feature_extractor" in kwargs: warnings.warn( """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`""" """ instead.""" , A__ , ) _SCREAMING_SNAKE_CASE = kwargs.pop("""feature_extractor""" ) _SCREAMING_SNAKE_CASE = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("""You need to specify an `image_processor`.""" ) if tokenizer is None: raise ValueError("""You need to specify a `tokenizer`.""" ) super().__init__(A__ , A__ ) _SCREAMING_SNAKE_CASE = self.image_processor def __call__( self , A__=None , A__=None , A__=None , **A__ ) -> Optional[int]: if text is None and images is None: raise ValueError("""You have to specify either text or images. Both cannot be none.""" ) if text is not None: _SCREAMING_SNAKE_CASE = self.tokenizer(A__ , return_tensors=A__ , **A__ ) if images is not None: _SCREAMING_SNAKE_CASE = self.image_processor(A__ , return_tensors=A__ , **A__ ) if text is not None and images is not None: _SCREAMING_SNAKE_CASE = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**A__ ) , tensor_type=A__ ) def UpperCamelCase ( self , *A__ , **A__ ) -> Dict: return self.tokenizer.batch_decode(*A__ , **A__ ) def UpperCamelCase ( self , *A__ , **A__ ) -> Optional[Any]: return self.tokenizer.decode(*A__ , **A__ ) @property def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.tokenizer.model_input_names _SCREAMING_SNAKE_CASE = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def UpperCamelCase ( self ) -> Optional[int]: warnings.warn( """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.""" , A__ , ) return self.image_processor_class
0
0
def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE = int(SCREAMING_SNAKE_CASE_ ) if decimal in (0, 1): # Exit cases for the recursion return str(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = divmod(SCREAMING_SNAKE_CASE_ , 2 ) return binary_recursive(SCREAMING_SNAKE_CASE_ ) + str(SCREAMING_SNAKE_CASE_ ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE = str(SCREAMING_SNAKE_CASE_ ).strip() if not number: raise ValueError("""No input value was provided""" ) _SCREAMING_SNAKE_CASE = """-""" if number.startswith("""-""" ) else """""" _SCREAMING_SNAKE_CASE = number.lstrip("""-""" ) if not number.isnumeric(): raise ValueError("""Input value is not an integer""" ) return F"{negative}0b{binary_recursive(int(SCREAMING_SNAKE_CASE_ ) )}" if __name__ == "__main__": from doctest import testmod testmod()
705
'''simple docstring''' from sklearn.metrics import matthews_corrcoef import datasets UpperCamelCase__ : List[str] = "\nCompute the Matthews correlation coefficient (MCC)\n\nThe Matthews correlation coefficient is used in machine learning as a\nmeasure of the quality of binary and multiclass classifications. It takes\ninto account true and false positives and negatives and is generally\nregarded as a balanced measure which can be used even if the classes are of\nvery different sizes. The MCC is in essence a correlation coefficient value\nbetween -1 and +1. A coefficient of +1 represents a perfect prediction, 0\nan average random prediction and -1 an inverse prediction. The statistic\nis also known as the phi coefficient. [source: Wikipedia]\n" UpperCamelCase__ : List[Any] = "\nArgs:\n predictions (list of int): Predicted labels, as returned by a model.\n references (list of int): Ground truth labels.\n sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`.\nReturns:\n matthews_correlation (dict containing float): Matthews correlation.\nExamples:\n Example 1, a basic example with only predictions and references as inputs:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3])\n >>> print(round(results['matthews_correlation'], 2))\n 0.54\n\n Example 2, the same example as above, but also including sample weights:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 3, 1, 1, 1, 2])\n >>> print(round(results['matthews_correlation'], 2))\n 0.1\n\n Example 3, the same example as above, but with sample weights that cause a negative correlation:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 1, 0, 0, 0, 1])\n >>> print(round(results['matthews_correlation'], 2))\n -0.25\n" UpperCamelCase__ : Any = "\\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class _a (datasets.Metric): """simple docstring""" def UpperCamelCase ( self ) -> Optional[Any]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=[ """https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html""" ] , ) def UpperCamelCase ( self , A__ , A__ , A__=None ) -> List[str]: return { "matthews_correlation": float(matthews_corrcoef(A__ , A__ , sample_weight=A__ ) ), }
0
0
import argparse import os from accelerate.test_utils import execute_subprocess_async def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_=None ) -> Any: """simple docstring""" if subparsers is not None: _SCREAMING_SNAKE_CASE = subparsers.add_parser("""test""" ) else: _SCREAMING_SNAKE_CASE = argparse.ArgumentParser("""Accelerate test command""" ) parser.add_argument( """--config_file""" , default=lowercase_ , help=( """The path to use to store the config file. Will default to a file named default_config.yaml in the cache """ """location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have """ """such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed """ """with 'huggingface'.""" ) , ) if subparsers is not None: parser.set_defaults(func=lowercase_ ) return parser def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = os.path.sep.join(__file__.split(os.path.sep )[:-2] + ["""test_utils""", """scripts""", """test_script.py"""] ) if args.config_file is None: _SCREAMING_SNAKE_CASE = script_name else: _SCREAMING_SNAKE_CASE = F"--config_file={args.config_file} {script_name}" _SCREAMING_SNAKE_CASE = ["accelerate-launch"] + test_args.split() _SCREAMING_SNAKE_CASE = execute_subprocess_async(lowercase_ , env=os.environ.copy() ) if result.returncode == 0: print("""Test is a success! You are ready for your distributed training!""" ) def lowerCAmelCase_ ( ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = test_command_parser() _SCREAMING_SNAKE_CASE = parser.parse_args() test_command(lowercase_ ) if __name__ == "__main__": main()
706
'''simple docstring''' from __future__ import annotations def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Tuple: """simple docstring""" print(F"Vertex\tShortest Distance from vertex {src}" ) for i, d in enumerate(SCREAMING_SNAKE_CASE_ ): print(F"{i}\t\t{d}" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" for j in range(SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = (graph[j][k] for k in ["""src""", """dst""", """weight"""]) if distance[u] != float("""inf""" ) and distance[u] + w < distance[v]: return True return False def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> list[float]: """simple docstring""" _SCREAMING_SNAKE_CASE = [float("""inf""" )] * vertex_count _SCREAMING_SNAKE_CASE = 0.0 for _ in range(vertex_count - 1 ): for j in range(SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = (graph[j][k] for k in ["""src""", """dst""", """weight"""]) if distance[u] != float("""inf""" ) and distance[u] + w < distance[v]: _SCREAMING_SNAKE_CASE = distance[u] + w _SCREAMING_SNAKE_CASE = check_negative_cycle(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if negative_cycle_exists: raise Exception("""Negative cycle found""" ) return distance if __name__ == "__main__": import doctest doctest.testmod() UpperCamelCase__ : int = int(input("Enter number of vertices: ").strip()) UpperCamelCase__ : int = int(input("Enter number of edges: ").strip()) UpperCamelCase__ : list[dict[str, int]] = [{} for _ in range(E)] for i in range(E): print("Edge ", i + 1) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ : Dict = ( int(x) for x in input("Enter source, destination, weight: ").strip().split(" ") ) UpperCamelCase__ : Optional[Any] = {"src": src, "dst": dest, "weight": weight} UpperCamelCase__ : Optional[Any] = int(input("\nEnter shortest path source:").strip()) UpperCamelCase__ : Any = bellman_ford(graph, V, E, source) print_distance(shortest_distance, 0)
0
0
'''simple docstring''' 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 _a (UpperCAmelCase_): """simple docstring""" def __init__( self , A__ , A__ , A__ = None , A__ = None , A__ = False , **A__ , ) -> str: super().__init__(features=_snake_case , cache_dir=_snake_case , keep_in_memory=_snake_case , **_snake_case ) _SCREAMING_SNAKE_CASE = Sql( cache_dir=_snake_case , features=_snake_case , sql=_snake_case , con=_snake_case , **_snake_case , ) def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None self.builder.download_and_prepare( download_config=_snake_case , download_mode=_snake_case , verification_mode=_snake_case , base_path=_snake_case , ) # Build dataset for splits _SCREAMING_SNAKE_CASE = self.builder.as_dataset( split="""train""" , verification_mode=_snake_case , in_memory=self.keep_in_memory ) return dataset class _a : """simple docstring""" def __init__( self , A__ , A__ , A__ , A__ = None , A__ = None , **A__ , ) -> Any: if num_proc is not None and num_proc <= 0: raise ValueError(F"num_proc {num_proc} must be an integer > 0." ) _SCREAMING_SNAKE_CASE = dataset _SCREAMING_SNAKE_CASE = name _SCREAMING_SNAKE_CASE = con _SCREAMING_SNAKE_CASE = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE _SCREAMING_SNAKE_CASE = num_proc _SCREAMING_SNAKE_CASE = to_sql_kwargs def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = self.to_sql_kwargs.pop("""sql""" , _snake_case ) _SCREAMING_SNAKE_CASE = self.to_sql_kwargs.pop("""con""" , _snake_case ) _SCREAMING_SNAKE_CASE = self.to_sql_kwargs.pop("""index""" , _snake_case ) _SCREAMING_SNAKE_CASE = self._write(index=_snake_case , **self.to_sql_kwargs ) return written def UpperCamelCase ( self , A__ ) -> str: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = args _SCREAMING_SNAKE_CASE = {**to_sql_kwargs, """if_exists""": """append"""} if offset > 0 else to_sql_kwargs _SCREAMING_SNAKE_CASE = query_table( table=self.dataset.data , key=slice(_snake_case , offset + self.batch_size ) , indices=self.dataset._indices , ) _SCREAMING_SNAKE_CASE = batch.to_pandas() _SCREAMING_SNAKE_CASE = df.to_sql(self.name , self.con , index=_snake_case , **_snake_case ) return num_rows or len(_snake_case ) def UpperCamelCase ( self , A__ , **A__ ) -> int: _SCREAMING_SNAKE_CASE = 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: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 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 , _snake_case , _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
707
'''simple docstring''' 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 , A__ , A__=13 , A__=7 , A__=True , A__=True , A__=True , A__=True , A__=99 , A__=32 , A__=2 , A__=4 , A__=37 , A__="gelu" , A__=0.1 , A__=0.1 , A__=5_12 , A__=16 , A__=2 , A__=0.02 , A__=3 , A__=4 , A__=None , ) -> int: _SCREAMING_SNAKE_CASE = parent _SCREAMING_SNAKE_CASE = 13 _SCREAMING_SNAKE_CASE = 7 _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = 99 _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 = 5_12 _SCREAMING_SNAKE_CASE = 16 _SCREAMING_SNAKE_CASE = 2 _SCREAMING_SNAKE_CASE = 0.02 _SCREAMING_SNAKE_CASE = 3 _SCREAMING_SNAKE_CASE = 4 _SCREAMING_SNAKE_CASE = None def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _SCREAMING_SNAKE_CASE = None if self.use_input_mask: _SCREAMING_SNAKE_CASE = random_attention_mask([self.batch_size, self.seq_length] ) _SCREAMING_SNAKE_CASE = None if self.use_token_type_ids: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None if self.use_labels: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.num_choices ) _SCREAMING_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=A__ , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = TFRoFormerModel(config=A__ ) _SCREAMING_SNAKE_CASE = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} _SCREAMING_SNAKE_CASE = [input_ids, input_mask] _SCREAMING_SNAKE_CASE = model(A__ ) _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> str: _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = TFRoFormerForCausalLM(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ )["""logits"""] self.parent.assertListEqual( list(prediction_scores.numpy().shape ) , [self.batch_size, self.seq_length, self.vocab_size] ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> Dict: _SCREAMING_SNAKE_CASE = TFRoFormerForMaskedLM(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> List[str]: _SCREAMING_SNAKE_CASE = self.num_labels _SCREAMING_SNAKE_CASE = TFRoFormerForSequenceClassification(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> Any: _SCREAMING_SNAKE_CASE = self.num_choices _SCREAMING_SNAKE_CASE = TFRoFormerForMultipleChoice(config=A__ ) _SCREAMING_SNAKE_CASE = tf.tile(tf.expand_dims(A__ , 1 ) , (1, self.num_choices, 1) ) _SCREAMING_SNAKE_CASE = tf.tile(tf.expand_dims(A__ , 1 ) , (1, self.num_choices, 1) ) _SCREAMING_SNAKE_CASE = tf.tile(tf.expand_dims(A__ , 1 ) , (1, self.num_choices, 1) ) _SCREAMING_SNAKE_CASE = { """input_ids""": multiple_choice_inputs_ids, """attention_mask""": multiple_choice_input_mask, """token_type_ids""": multiple_choice_token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> List[str]: _SCREAMING_SNAKE_CASE = self.num_labels _SCREAMING_SNAKE_CASE = TFRoFormerForTokenClassification(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> Tuple: _SCREAMING_SNAKE_CASE = TFRoFormerForQuestionAnswering(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = 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 ) , ) = config_and_inputs _SCREAMING_SNAKE_CASE = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask} return config, inputs_dict @require_tf class _a (_lowerCamelCase , _lowerCamelCase , unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = ( ( TFRoFormerModel, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerForMultipleChoice, ) if is_tf_available() else () ) SCREAMING_SNAKE_CASE = ( { '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 {} ) SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = False def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ ) -> str: if pipeline_test_casse_name == "TextGenerationPipelineTests": return True return False def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = TFRoFormerModelTester(self ) _SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=A__ , hidden_size=37 ) def UpperCamelCase ( self ) -> Optional[Any]: self.config_tester.run_common_tests() def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A__ ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*A__ ) def UpperCamelCase ( self ) -> int: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head(*A__ ) def UpperCamelCase ( self ) -> Dict: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*A__ ) def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*A__ ) def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*A__ ) def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*A__ ) @slow def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = TFRoFormerModel.from_pretrained("""junnyu/roformer_chinese_base""" ) self.assertIsNotNone(A__ ) @require_tf class _a (unittest.TestCase): """simple docstring""" @slow def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = TFRoFormerForMaskedLM.from_pretrained("""junnyu/roformer_chinese_base""" ) _SCREAMING_SNAKE_CASE = tf.constant([[0, 1, 2, 3, 4, 5]] ) _SCREAMING_SNAKE_CASE = model(A__ )[0] # TODO Replace vocab size _SCREAMING_SNAKE_CASE = 5_00_00 _SCREAMING_SNAKE_CASE = [1, 6, vocab_size] self.assertEqual(output.shape , A__ ) print(output[:, :3, :3] ) # TODO Replace values below with what was printed above. _SCREAMING_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] , A__ , atol=1E-4 ) @require_tf class _a (unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = 1E-4 def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = tf.constant([[4, 10]] ) _SCREAMING_SNAKE_CASE = TFRoFormerSinusoidalPositionalEmbedding(num_positions=6 , embedding_dim=6 ) _SCREAMING_SNAKE_CASE = emba(input_ids.shape ) _SCREAMING_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(A__ , A__ , atol=self.tolerance ) def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_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], ] ) _SCREAMING_SNAKE_CASE = TFRoFormerSinusoidalPositionalEmbedding(num_positions=5_12 , embedding_dim=5_12 ) emba([2, 16, 5_12] ) _SCREAMING_SNAKE_CASE = emba.weight[:3, :5] tf.debugging.assert_near(A__ , A__ , atol=self.tolerance ) @require_tf class _a (unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = 1E-4 def UpperCamelCase ( self ) -> int: # 2,12,16,64 _SCREAMING_SNAKE_CASE = tf.reshape(tf.range(2 * 12 * 16 * 64 , dtype=tf.floataa ) , shape=(2, 12, 16, 64) ) / 1_00 _SCREAMING_SNAKE_CASE = -tf.reshape(tf.range(2 * 12 * 16 * 64 , dtype=tf.floataa ) , shape=(2, 12, 16, 64) ) / 1_00 _SCREAMING_SNAKE_CASE = TFRoFormerSinusoidalPositionalEmbedding(num_positions=32 , embedding_dim=64 ) _SCREAMING_SNAKE_CASE = embed_positions([2, 16, 7_68] )[None, None, :, :] _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = TFRoFormerSelfAttention.apply_rotary_position_embeddings( A__ , A__ , A__ ) _SCREAMING_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], ] ) _SCREAMING_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] , A__ , atol=self.tolerance ) tf.debugging.assert_near(key_layer[0, 0, :6, :8] , A__ , atol=self.tolerance )
0
0
'''simple docstring''' from datetime import datetime import requests from bsa import BeautifulSoup if __name__ == "__main__": UpperCamelCase__ : Any = input("Enter image url: ").strip() print(f"""Downloading image from {url} ...""") UpperCamelCase__ : List[str] = BeautifulSoup(requests.get(url).content, "html.parser") # The image URL is in the content field of the first meta tag with property og:image UpperCamelCase__ : Union[str, Any] = soup.find("meta", {"property": "og:image"})["""content"""] UpperCamelCase__ : int = requests.get(image_url).content UpperCamelCase__ : Dict = f"""{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg""" with open(file_name, "wb") as fp: fp.write(image_data) print(f"""Done. Image saved to disk as {file_name}.""")
708
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available UpperCamelCase__ : int = {"tokenization_herbert": ["HerbertTokenizer"]} try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Tuple = ["HerbertTokenizerFast"] if TYPE_CHECKING: from .tokenization_herbert import HerbertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_herbert_fast import HerbertTokenizerFast else: import sys UpperCamelCase__ : Tuple = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
0
0
'''simple docstring''' from __future__ import annotations class _a : """simple docstring""" def __init__( self , A__ = 0 ) -> str: _SCREAMING_SNAKE_CASE = key def UpperCamelCase ( self , A__ , A__ ) -> list[str]: assert isinstance(_a , _a ) and isinstance(_a , _a ) _SCREAMING_SNAKE_CASE = key or self.__key or 1 # make sure key is an appropriate size key %= 2_55 return [chr(ord(_a ) ^ key ) for ch in content] def UpperCamelCase ( self , A__ , A__ ) -> list[str]: assert isinstance(_a , _a ) and isinstance(_a , _a ) _SCREAMING_SNAKE_CASE = key or self.__key or 1 # make sure key is an appropriate size key %= 2_55 return [chr(ord(_a ) ^ key ) for ch in content] def UpperCamelCase ( self , A__ , A__ = 0 ) -> str: assert isinstance(_a , _a ) and isinstance(_a , _a ) _SCREAMING_SNAKE_CASE = key or self.__key or 1 # make sure key can be any size while key > 2_55: key -= 2_55 # This will be returned _SCREAMING_SNAKE_CASE = """""" for ch in content: ans += chr(ord(_a ) ^ key ) return ans def UpperCamelCase ( self , A__ , A__ = 0 ) -> str: assert isinstance(_a , _a ) and isinstance(_a , _a ) _SCREAMING_SNAKE_CASE = key or self.__key or 1 # make sure key can be any size while key > 2_55: key -= 2_55 # This will be returned _SCREAMING_SNAKE_CASE = """""" for ch in content: ans += chr(ord(_a ) ^ key ) return ans def UpperCamelCase ( self , A__ , A__ = 0 ) -> bool: assert isinstance(_a , _a ) and isinstance(_a , _a ) try: with open(_a ) as fin, open("""encrypt.out""" , """w+""" ) as fout: # actual encrypt-process for line in fin: fout.write(self.encrypt_string(_a , _a ) ) except OSError: return False return True def UpperCamelCase ( self , A__ , A__ ) -> bool: assert isinstance(_a , _a ) and isinstance(_a , _a ) try: with open(_a ) as fin, open("""decrypt.out""" , """w+""" ) as fout: # actual encrypt-process for line in fin: fout.write(self.decrypt_string(_a , _a ) ) except OSError: return False return True # Tests # crypt = XORCipher() # key = 67 # # test encrypt # print(crypt.encrypt("hallo welt",key)) # # test decrypt # print(crypt.decrypt(crypt.encrypt("hallo welt",key), key)) # # test encrypt_string # print(crypt.encrypt_string("hallo welt",key)) # # test decrypt_string # print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key)) # if (crypt.encrypt_file("test.txt",key)): # print("encrypt successful") # else: # print("encrypt unsuccessful") # if (crypt.decrypt_file("encrypt.out",key)): # print("decrypt successful") # else: # print("decrypt unsuccessful")
709
'''simple docstring''' import argparse import gdown import numpy as np import torch from huggingface_hub import hf_hub_download from transformers import ( CLIPTokenizer, CLIPTokenizerFast, VideoMAEImageProcessor, XCLIPConfig, XCLIPModel, XCLIPProcessor, XCLIPTextConfig, XCLIPVisionConfig, ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE = XCLIPTextConfig() # derive patch size from model name _SCREAMING_SNAKE_CASE = model_name.find("""patch""" ) _SCREAMING_SNAKE_CASE = int(model_name[start_idx + len("""patch""" ) : start_idx + len("""patch""" ) + 2] ) _SCREAMING_SNAKE_CASE = XCLIPVisionConfig(patch_size=SCREAMING_SNAKE_CASE_ , num_frames=SCREAMING_SNAKE_CASE_ ) if "large" in model_name: _SCREAMING_SNAKE_CASE = 7_68 _SCREAMING_SNAKE_CASE = 30_72 _SCREAMING_SNAKE_CASE = 12 _SCREAMING_SNAKE_CASE = 10_24 _SCREAMING_SNAKE_CASE = 40_96 _SCREAMING_SNAKE_CASE = 16 _SCREAMING_SNAKE_CASE = 24 _SCREAMING_SNAKE_CASE = 7_68 _SCREAMING_SNAKE_CASE = 30_72 if model_name == "xclip-large-patch14-16-frames": _SCREAMING_SNAKE_CASE = 3_36 _SCREAMING_SNAKE_CASE = XCLIPConfig.from_text_vision_configs(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if "large" in model_name: _SCREAMING_SNAKE_CASE = 7_68 return config def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" # text encoder if name == "token_embedding.weight": _SCREAMING_SNAKE_CASE = name.replace("""token_embedding.weight""" , """text_model.embeddings.token_embedding.weight""" ) if name == "positional_embedding": _SCREAMING_SNAKE_CASE = name.replace("""positional_embedding""" , """text_model.embeddings.position_embedding.weight""" ) if "ln_1" in name: _SCREAMING_SNAKE_CASE = name.replace("""ln_1""" , """layer_norm1""" ) if "ln_2" in name: _SCREAMING_SNAKE_CASE = name.replace("""ln_2""" , """layer_norm2""" ) if "c_fc" in name: _SCREAMING_SNAKE_CASE = name.replace("""c_fc""" , """fc1""" ) if "c_proj" in name: _SCREAMING_SNAKE_CASE = name.replace("""c_proj""" , """fc2""" ) if name.startswith("""transformer.resblocks""" ): _SCREAMING_SNAKE_CASE = name.replace("""transformer.resblocks""" , """text_model.encoder.layers""" ) if "attn.out_proj" in name and "message" not in name: _SCREAMING_SNAKE_CASE = name.replace("""attn.out_proj""" , """self_attn.out_proj""" ) if "ln_final" in name: _SCREAMING_SNAKE_CASE = name.replace("""ln_final""" , """text_model.final_layer_norm""" ) # visual encoder if name == "visual.class_embedding": _SCREAMING_SNAKE_CASE = name.replace("""visual.class_embedding""" , """vision_model.embeddings.class_embedding""" ) if name == "visual.positional_embedding": _SCREAMING_SNAKE_CASE = name.replace("""visual.positional_embedding""" , """vision_model.embeddings.position_embedding.weight""" ) if name.startswith("""visual.transformer.resblocks""" ): _SCREAMING_SNAKE_CASE = name.replace("""visual.transformer.resblocks""" , """vision_model.encoder.layers""" ) if "visual.conv1" in name: _SCREAMING_SNAKE_CASE = name.replace("""visual.conv1""" , """vision_model.embeddings.patch_embedding""" ) if "visual.ln_pre" in name: _SCREAMING_SNAKE_CASE = name.replace("""visual.ln_pre""" , """vision_model.pre_layernorm""" ) if "visual.ln_post" in name: _SCREAMING_SNAKE_CASE = name.replace("""visual.ln_post""" , """vision_model.post_layernorm""" ) if "visual.proj" in name: _SCREAMING_SNAKE_CASE = name.replace("""visual.proj""" , """visual_projection.weight""" ) if "text_projection" in name: _SCREAMING_SNAKE_CASE = name.replace("""text_projection""" , """text_projection.weight""" ) # things on top if "prompts_visual_proj" in name: _SCREAMING_SNAKE_CASE = name.replace("""prompts_visual_proj""" , """prompts_visual_projection""" ) if "prompts_visual_ln" in name: _SCREAMING_SNAKE_CASE = name.replace("""prompts_visual_ln""" , """prompts_visual_layernorm""" ) # mit if name == "mit.positional_embedding": _SCREAMING_SNAKE_CASE = name.replace("""positional""" , """position""" ) if name.startswith("""mit.resblocks""" ): _SCREAMING_SNAKE_CASE = name.replace("""mit.resblocks""" , """mit.encoder.layers""" ) # prompts generator if name.startswith("""prompts_generator.norm""" ): _SCREAMING_SNAKE_CASE = name.replace("""prompts_generator.norm""" , """prompts_generator.layernorm""" ) return name def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" for key in orig_state_dict.copy().keys(): _SCREAMING_SNAKE_CASE = orig_state_dict.pop(SCREAMING_SNAKE_CASE_ ) if "attn.in_proj" in key: _SCREAMING_SNAKE_CASE = key.split(""".""" ) if key.startswith("""visual""" ): _SCREAMING_SNAKE_CASE = key_split[3] _SCREAMING_SNAKE_CASE = config.vision_config.hidden_size if "message_attn" in key: if "weight" in key: _SCREAMING_SNAKE_CASE = val[ :dim, : ] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2, : ] _SCREAMING_SNAKE_CASE = val[ -dim:, : ] else: _SCREAMING_SNAKE_CASE = val[ :dim ] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2 ] _SCREAMING_SNAKE_CASE = val[ -dim: ] else: if "weight" in key: _SCREAMING_SNAKE_CASE = val[ :dim, : ] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2, : ] _SCREAMING_SNAKE_CASE = val[ -dim:, : ] else: _SCREAMING_SNAKE_CASE = val[:dim] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2 ] _SCREAMING_SNAKE_CASE = val[-dim:] elif key.startswith("""mit""" ): _SCREAMING_SNAKE_CASE = key_split[2] _SCREAMING_SNAKE_CASE = config.vision_config.mit_hidden_size if "weight" in key: _SCREAMING_SNAKE_CASE = val[:dim, :] _SCREAMING_SNAKE_CASE = val[dim : dim * 2, :] _SCREAMING_SNAKE_CASE = val[-dim:, :] else: _SCREAMING_SNAKE_CASE = val[:dim] _SCREAMING_SNAKE_CASE = val[dim : dim * 2] _SCREAMING_SNAKE_CASE = val[-dim:] else: _SCREAMING_SNAKE_CASE = key_split[2] _SCREAMING_SNAKE_CASE = config.text_config.hidden_size if "weight" in key: _SCREAMING_SNAKE_CASE = val[:dim, :] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2, : ] _SCREAMING_SNAKE_CASE = val[-dim:, :] else: _SCREAMING_SNAKE_CASE = val[:dim] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2 ] _SCREAMING_SNAKE_CASE = val[-dim:] else: _SCREAMING_SNAKE_CASE = rename_key(SCREAMING_SNAKE_CASE_ ) if new_key_name in ["visual_projection.weight", "text_projection.weight"]: _SCREAMING_SNAKE_CASE = val.T _SCREAMING_SNAKE_CASE = val return orig_state_dict def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" if num_frames == 8: _SCREAMING_SNAKE_CASE = """eating_spaghetti_8_frames.npy""" elif num_frames == 16: _SCREAMING_SNAKE_CASE = """eating_spaghetti.npy""" elif num_frames == 32: _SCREAMING_SNAKE_CASE = """eating_spaghetti_32_frames.npy""" _SCREAMING_SNAKE_CASE = hf_hub_download( repo_id="""hf-internal-testing/spaghetti-video""" , filename=SCREAMING_SNAKE_CASE_ , repo_type="""dataset""" , ) _SCREAMING_SNAKE_CASE = np.load(SCREAMING_SNAKE_CASE_ ) return list(SCREAMING_SNAKE_CASE_ ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_=False ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = { # fully supervised kinetics-400 checkpoints """xclip-base-patch32""": """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_8.pth""", """xclip-base-patch32-16-frames""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_16.pth""" ), """xclip-base-patch16""": """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_8.pth""", """xclip-base-patch16-16-frames""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_16.pth""" ), """xclip-large-patch14""": """https://drive.google.com/u/0/uc?id=1NUOImq0o5DlQTST17iIP3vG7DgmHQuCx&amp;export=download&amp;confirm=t&amp;uuid=b26caedc-88e2-473e-830a-9d158b653cdb""", """xclip-large-patch14-16-frames""": """https://drive.google.com/u/0/uc?id=1FOYgnJc097OJ4lGwtRCCydQyVPJEOH7d&amp;export=download&amp;confirm=t&amp;uuid=538fa810-e671-4050-b385-9a623f89804f""", # fully supervised kinetics-600 checkpoints """xclip-base-patch16-kinetics-600""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_8.pth""" ), """xclip-base-patch16-kinetics-600-16-frames""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_16.pth""" ), """xclip-large-patch14-kinetics-600""": """https://drive.google.com/u/0/uc?id=1FV8C1INuM91sLAN4ImjzePLIlpMSihwV&amp;export=download&amp;confirm=t&amp;uuid=141d4977-4a65-44ae-864f-4b0c19f838be""", # few shot """xclip-base-patch16-hmdb-2-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_2.pth""" ), """xclip-base-patch16-hmdb-4-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_4.pth""" ), """xclip-base-patch16-hmdb-8-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_8.pth""" ), """xclip-base-patch16-hmdb-16-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_16.pth""" ), """xclip-base-patch16-ucf-2-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_2.pth""" ), """xclip-base-patch16-ucf-4-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_4.pth""" ), """xclip-base-patch16-ucf-8-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_8.pth""" ), """xclip-base-patch16-ucf-16-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_16.pth""" ), # zero shot """xclip-base-patch16-zero-shot""": """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/zero.pth""", } _SCREAMING_SNAKE_CASE = model_to_url[model_name] _SCREAMING_SNAKE_CASE = 8 if "16-frames" in model_name: _SCREAMING_SNAKE_CASE = 16 elif "shot" in model_name: _SCREAMING_SNAKE_CASE = 32 _SCREAMING_SNAKE_CASE = get_xclip_config(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = XCLIPModel(SCREAMING_SNAKE_CASE_ ) model.eval() if "drive" in checkpoint_url: _SCREAMING_SNAKE_CASE = """pytorch_model.bin""" gdown.cached_download(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , quiet=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = torch.load(SCREAMING_SNAKE_CASE_ , map_location="""cpu""" )["""model"""] else: _SCREAMING_SNAKE_CASE = torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE_ )["""model"""] _SCREAMING_SNAKE_CASE = convert_state_dict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = XCLIPModel(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = model.load_state_dict(SCREAMING_SNAKE_CASE_ , strict=SCREAMING_SNAKE_CASE_ ) assert missing_keys == ["text_model.embeddings.position_ids", "vision_model.embeddings.position_ids"] model.eval() _SCREAMING_SNAKE_CASE = 3_36 if model_name == """xclip-large-patch14-16-frames""" else 2_24 _SCREAMING_SNAKE_CASE = VideoMAEImageProcessor(size=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = CLIPTokenizer.from_pretrained("""openai/clip-vit-base-patch32""" ) _SCREAMING_SNAKE_CASE = CLIPTokenizerFast.from_pretrained("""openai/clip-vit-base-patch32""" ) _SCREAMING_SNAKE_CASE = XCLIPProcessor(image_processor=SCREAMING_SNAKE_CASE_ , tokenizer=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = prepare_video(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = processor( text=["""playing sports""", """eating spaghetti""", """go shopping"""] , videos=SCREAMING_SNAKE_CASE_ , return_tensors="""pt""" , padding=SCREAMING_SNAKE_CASE_ ) print("""Shape of pixel values:""" , inputs.pixel_values.shape ) with torch.no_grad(): _SCREAMING_SNAKE_CASE = model(**SCREAMING_SNAKE_CASE_ ) # Verify outputs _SCREAMING_SNAKE_CASE = outputs.logits_per_video _SCREAMING_SNAKE_CASE = logits_per_video.softmax(dim=1 ) print("""Probs:""" , SCREAMING_SNAKE_CASE_ ) # kinetics-400 if model_name == "xclip-base-patch32": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0019, 0.9951, 0.0030]] ) elif model_name == "xclip-base-patch32-16-frames": _SCREAMING_SNAKE_CASE = torch.tensor([[7.0999e-04, 9.9883e-01, 4.5580e-04]] ) elif model_name == "xclip-base-patch16": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0083, 0.9681, 0.0236]] ) elif model_name == "xclip-base-patch16-16-frames": _SCREAMING_SNAKE_CASE = torch.tensor([[7.6937e-04, 9.9728e-01, 1.9473e-03]] ) elif model_name == "xclip-large-patch14": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0062, 0.9864, 0.0075]] ) elif model_name == "xclip-large-patch14-16-frames": _SCREAMING_SNAKE_CASE = torch.tensor([[3.3877e-04, 9.9937e-01, 2.8888e-04]] ) # kinetics-600 elif model_name == "xclip-base-patch16-kinetics-600": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0555, 0.8914, 0.0531]] ) elif model_name == "xclip-base-patch16-kinetics-600-16-frames": _SCREAMING_SNAKE_CASE = torch.tensor([[3.8554e-04, 9.9929e-01, 3.2754e-04]] ) elif model_name == "xclip-large-patch14-kinetics-600": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0036, 0.9920, 0.0045]] ) # few shot elif model_name == "xclip-base-patch16-hmdb-2-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[7.1890e-06, 9.9994e-01, 5.6559e-05]] ) elif model_name == "xclip-base-patch16-hmdb-4-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[1.0320e-05, 9.9993e-01, 6.2435e-05]] ) elif model_name == "xclip-base-patch16-hmdb-8-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[4.1377e-06, 9.9990e-01, 9.8386e-05]] ) elif model_name == "xclip-base-patch16-hmdb-16-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[4.1347e-05, 9.9962e-01, 3.3411e-04]] ) elif model_name == "xclip-base-patch16-ucf-2-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[8.5857e-05, 9.9928e-01, 6.3291e-04]] ) elif model_name == "xclip-base-patch16-ucf-4-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[8.5857e-05, 9.9928e-01, 6.3291e-04]] ) elif model_name == "xclip-base-patch16-ucf-8-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0027, 0.9904, 0.0070]] ) elif model_name == "xclip-base-patch16-ucf-16-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[9.8219e-04, 9.9593e-01, 3.0863e-03]] ) # zero shot elif model_name == "xclip-base-patch16-zero-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[3.5082e-04, 9.9785e-01, 1.7966e-03]] ) else: raise ValueError(F"Model name {model_name} not supported" ) assert torch.allclose(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , atol=1e-3 ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: print(F"Saving model {model_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(SCREAMING_SNAKE_CASE_ ) if push_to_hub: print("""Pushing model, processor and slow tokenizer files to the hub...""" ) model.push_to_hub(SCREAMING_SNAKE_CASE_ , organization="""nielsr""" ) processor.push_to_hub(SCREAMING_SNAKE_CASE_ , organization="""nielsr""" ) slow_tokenizer.push_to_hub(SCREAMING_SNAKE_CASE_ , organization="""nielsr""" ) if __name__ == "__main__": UpperCamelCase__ : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default="xclip-base-patch32", type=str, help="Name of the model.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether or not to push the converted model to the 🤗 hub." ) UpperCamelCase__ : str = parser.parse_args() convert_xclip_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
0
0
'''simple docstring''' from typing import Dict import numpy as np from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline, PipelineException if is_tf_available(): import tensorflow as tf from ..tf_utils import stable_softmax if is_torch_available(): import torch UpperCamelCase__ = logging.get_logger(__name__) @add_end_docstrings( SCREAMING_SNAKE_CASE_ , r'\n top_k (`int`, defaults to 5):\n The number of predictions to return.\n targets (`str` or `List[str]`, *optional*):\n When passed, the model will limit the scores to the passed targets instead of looking up in the whole\n vocab. If the provided targets are not in the model vocab, they will be tokenized and the first resulting\n token will be used (with a warning, and that might be slower).\n\n ' , ) class _a (SCREAMING_SNAKE_CASE_): """simple docstring""" def UpperCamelCase ( self , A__ ) -> Dict: if self.framework == "tf": _SCREAMING_SNAKE_CASE = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy() elif self.framework == "pt": _SCREAMING_SNAKE_CASE = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=UpperCamelCase__ ) else: raise ValueError("""Unsupported framework""" ) return masked_index def UpperCamelCase ( self , A__ ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.get_masked_index(UpperCamelCase__ ) _SCREAMING_SNAKE_CASE = np.prod(masked_index.shape ) if numel < 1: raise PipelineException( """fill-mask""" , self.model.base_model_prefix , F"No mask_token ({self.tokenizer.mask_token}) found on the input" , ) def UpperCamelCase ( self , A__ ) -> Optional[int]: if isinstance(UpperCamelCase__ , UpperCamelCase__ ): for model_input in model_inputs: self._ensure_exactly_one_mask_token(model_input["""input_ids"""][0] ) else: for input_ids in model_inputs["input_ids"]: self._ensure_exactly_one_mask_token(UpperCamelCase__ ) def UpperCamelCase ( self , A__ , A__=None , **A__ ) -> int: if return_tensors is None: _SCREAMING_SNAKE_CASE = self.framework _SCREAMING_SNAKE_CASE = self.tokenizer(UpperCamelCase__ , return_tensors=UpperCamelCase__ ) self.ensure_exactly_one_mask_token(UpperCamelCase__ ) return model_inputs def UpperCamelCase ( self , A__ ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = self.model(**UpperCamelCase__ ) _SCREAMING_SNAKE_CASE = model_inputs['''input_ids'''] return model_outputs def UpperCamelCase ( self , A__ , A__=5 , A__=None ) -> Dict: # Cap top_k if there are targets if target_ids is not None and target_ids.shape[0] < top_k: _SCREAMING_SNAKE_CASE = target_ids.shape[0] _SCREAMING_SNAKE_CASE = model_outputs['''input_ids'''][0] _SCREAMING_SNAKE_CASE = model_outputs['''logits'''] if self.framework == "tf": _SCREAMING_SNAKE_CASE = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy()[:, 0] _SCREAMING_SNAKE_CASE = outputs.numpy() _SCREAMING_SNAKE_CASE = outputs[0, masked_index, :] _SCREAMING_SNAKE_CASE = stable_softmax(UpperCamelCase__ , axis=-1 ) if target_ids is not None: _SCREAMING_SNAKE_CASE = tf.gather_nd(tf.squeeze(UpperCamelCase__ , 0 ) , target_ids.reshape(-1 , 1 ) ) _SCREAMING_SNAKE_CASE = tf.expand_dims(UpperCamelCase__ , 0 ) _SCREAMING_SNAKE_CASE = tf.math.top_k(UpperCamelCase__ , k=UpperCamelCase__ ) _SCREAMING_SNAKE_CASE = topk.values.numpy(), topk.indices.numpy() else: _SCREAMING_SNAKE_CASE = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=UpperCamelCase__ ).squeeze(-1 ) # Fill mask pipeline supports only one ${mask_token} per sample _SCREAMING_SNAKE_CASE = outputs[0, masked_index, :] _SCREAMING_SNAKE_CASE = logits.softmax(dim=-1 ) if target_ids is not None: _SCREAMING_SNAKE_CASE = probs[..., target_ids] _SCREAMING_SNAKE_CASE = probs.topk(UpperCamelCase__ ) _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = values.shape[0] == 1 for i, (_values, _predictions) in enumerate(zip(values.tolist() , predictions.tolist() ) ): _SCREAMING_SNAKE_CASE = [] for v, p in zip(_values , _predictions ): # Copy is important since we're going to modify this array in place _SCREAMING_SNAKE_CASE = input_ids.numpy().copy() if target_ids is not None: _SCREAMING_SNAKE_CASE = target_ids[p].tolist() _SCREAMING_SNAKE_CASE = p # Filter padding out: _SCREAMING_SNAKE_CASE = tokens[np.where(tokens != self.tokenizer.pad_token_id )] # Originally we skip special tokens to give readable output. # For multi masks though, the other [MASK] would be removed otherwise # making the output look odd, so we add them back _SCREAMING_SNAKE_CASE = self.tokenizer.decode(UpperCamelCase__ , skip_special_tokens=UpperCamelCase__ ) _SCREAMING_SNAKE_CASE = {'''score''': v, '''token''': p, '''token_str''': self.tokenizer.decode([p] ), '''sequence''': sequence} row.append(UpperCamelCase__ ) result.append(UpperCamelCase__ ) if single_mask: return result[0] return result def UpperCamelCase ( self , A__ , A__=None ) -> int: if isinstance(UpperCamelCase__ , UpperCamelCase__ ): _SCREAMING_SNAKE_CASE = [targets] try: _SCREAMING_SNAKE_CASE = self.tokenizer.get_vocab() except Exception: _SCREAMING_SNAKE_CASE = {} _SCREAMING_SNAKE_CASE = [] for target in targets: _SCREAMING_SNAKE_CASE = vocab.get(UpperCamelCase__ , UpperCamelCase__ ) if id_ is None: _SCREAMING_SNAKE_CASE = self.tokenizer( UpperCamelCase__ , add_special_tokens=UpperCamelCase__ , return_attention_mask=UpperCamelCase__ , return_token_type_ids=UpperCamelCase__ , max_length=1 , truncation=UpperCamelCase__ , )['''input_ids'''] if len(UpperCamelCase__ ) == 0: logger.warning( F"The specified target token `{target}` does not exist in the model vocabulary. " """We cannot replace it with anything meaningful, ignoring it""" ) continue _SCREAMING_SNAKE_CASE = input_ids[0] # XXX: If users encounter this pass # it becomes pretty slow, so let's make sure # The warning enables them to fix the input to # get faster performance. logger.warning( F"The specified target token `{target}` does not exist in the model vocabulary. " F"Replacing with `{self.tokenizer.convert_ids_to_tokens(id_ )}`." ) target_ids.append(id_ ) _SCREAMING_SNAKE_CASE = list(set(UpperCamelCase__ ) ) if len(UpperCamelCase__ ) == 0: raise ValueError("""At least one target must be provided when passed.""" ) _SCREAMING_SNAKE_CASE = np.array(UpperCamelCase__ ) return target_ids def UpperCamelCase ( self , A__=None , A__=None ) -> Tuple: _SCREAMING_SNAKE_CASE = {} if targets is not None: _SCREAMING_SNAKE_CASE = self.get_target_ids(UpperCamelCase__ , UpperCamelCase__ ) _SCREAMING_SNAKE_CASE = target_ids if top_k is not None: _SCREAMING_SNAKE_CASE = top_k if self.tokenizer.mask_token_id is None: raise PipelineException( """fill-mask""" , self.model.base_model_prefix , """The tokenizer does not define a `mask_token`.""" ) return {}, {}, postprocess_params def __call__( self , A__ , *A__ , **A__ ) -> List[Any]: _SCREAMING_SNAKE_CASE = super().__call__(UpperCamelCase__ , **UpperCamelCase__ ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) and len(UpperCamelCase__ ) == 1: return outputs[0] return outputs
710
'''simple docstring''' import numpy as np import torch from torch.utils.data import Dataset from utils import logger class _a (_lowerCamelCase): """simple docstring""" def __init__( self , A__ , A__ ) -> Any: _SCREAMING_SNAKE_CASE = params _SCREAMING_SNAKE_CASE = np.array(A__ ) _SCREAMING_SNAKE_CASE = np.array([len(A__ ) for t in data] ) self.check() self.remove_long_sequences() self.remove_empty_sequences() self.remove_unknown_sequences() self.check() self.print_statistics() def __getitem__( self , A__ ) -> Dict: return (self.token_ids[index], self.lengths[index]) def __len__( self ) -> Tuple: return len(self.lengths ) def UpperCamelCase ( self ) -> Dict: assert len(self.token_ids ) == len(self.lengths ) assert all(self.lengths[i] == len(self.token_ids[i] ) for i in range(len(self.lengths ) ) ) def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = self.params.max_model_input_size _SCREAMING_SNAKE_CASE = self.lengths > max_len logger.info(F"Splitting {sum(A__ )} too long sequences." ) def divide_chunks(A__ , A__ ): return [l[i : i + n] for i in range(0 , len(A__ ) , A__ )] _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = [] if self.params.mlm: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self.params.special_tok_ids["""cls_token"""], self.params.special_tok_ids["""sep_token"""] else: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 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: _SCREAMING_SNAKE_CASE = [] for sub_s in divide_chunks(seq_ , max_len - 2 ): if sub_s[0] != cls_id: _SCREAMING_SNAKE_CASE = np.insert(A__ , 0 , A__ ) if sub_s[-1] != sep_id: _SCREAMING_SNAKE_CASE = np.insert(A__ , len(A__ ) , A__ ) assert len(A__ ) <= max_len assert (sub_s[0] == cls_id) and (sub_s[-1] == sep_id), sub_s sub_seqs.append(A__ ) new_tok_ids.extend(A__ ) new_lengths.extend([len(A__ ) for l in sub_seqs] ) _SCREAMING_SNAKE_CASE = np.array(A__ ) _SCREAMING_SNAKE_CASE = np.array(A__ ) def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = len(self ) _SCREAMING_SNAKE_CASE = self.lengths > 11 _SCREAMING_SNAKE_CASE = self.token_ids[indices] _SCREAMING_SNAKE_CASE = self.lengths[indices] _SCREAMING_SNAKE_CASE = len(self ) logger.info(F"Remove {init_size - new_size} too short (<=11 tokens) sequences." ) def UpperCamelCase ( self ) -> int: if "unk_token" not in self.params.special_tok_ids: return else: _SCREAMING_SNAKE_CASE = self.params.special_tok_ids["""unk_token"""] _SCREAMING_SNAKE_CASE = len(self ) _SCREAMING_SNAKE_CASE = np.array([np.count_nonzero(a == unk_token_id ) for a in self.token_ids] ) _SCREAMING_SNAKE_CASE = (unk_occs / self.lengths) < 0.5 _SCREAMING_SNAKE_CASE = self.token_ids[indices] _SCREAMING_SNAKE_CASE = self.lengths[indices] _SCREAMING_SNAKE_CASE = len(self ) logger.info(F"Remove {init_size - new_size} sequences with a high level of unknown tokens (50%)." ) def UpperCamelCase ( self ) -> Optional[Any]: if not self.params.is_master: return logger.info(F"{len(self )} sequences" ) # data_len = sum(self.lengths) # nb_unique_tokens = len(Counter(list(chain(*self.token_ids)))) # logger.info(f'{data_len} tokens ({nb_unique_tokens} unique)') # unk_idx = self.params.special_tok_ids['unk_token'] # nb_unknown = sum([(t==unk_idx).sum() for t in self.token_ids]) # logger.info(f'{nb_unknown} unknown tokens (covering {100*nb_unknown/data_len:.2f}% of the data)') def UpperCamelCase ( self , A__ ) -> Any: _SCREAMING_SNAKE_CASE = [t[0] for t in batch] _SCREAMING_SNAKE_CASE = [t[1] for t in batch] assert len(A__ ) == len(A__ ) # Max for paddings _SCREAMING_SNAKE_CASE = max(A__ ) # Pad token ids if self.params.mlm: _SCREAMING_SNAKE_CASE = self.params.special_tok_ids["""pad_token"""] else: _SCREAMING_SNAKE_CASE = self.params.special_tok_ids["""unk_token"""] _SCREAMING_SNAKE_CASE = [list(t.astype(A__ ) ) + [pad_idx] * (max_seq_len_ - len(A__ )) for t in token_ids] assert len(tk_ ) == len(A__ ) assert all(len(A__ ) == max_seq_len_ for t in tk_ ) _SCREAMING_SNAKE_CASE = torch.tensor(tk_ ) # (bs, max_seq_len_) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) # (bs) return tk_t, lg_t
0
0
'''simple docstring''' 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 UpperCamelCase__ : Optional[int] = logging.get_logger(__name__) UpperCamelCase__ : Dict = "▁" UpperCamelCase__ : str = {"vocab_file": "spiece.model"} UpperCamelCase__ : Tuple = { "vocab_file": { "google/reformer-crime-and-punishment": ( "https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/spiece.model" ) } } UpperCamelCase__ : Any = { "google/reformer-crime-and-punishment": 524_288, } class _a (lowerCAmelCase__): """simple docstring""" SCREAMING_SNAKE_CASE = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE = ["input_ids", "attention_mask"] def __init__( self , A__ , A__="</s>" , A__="<unk>" , A__=[] , A__ = None , **A__ , ) -> None: _SCREAMING_SNAKE_CASE = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( eos_token=_lowerCamelCase , unk_token=_lowerCamelCase , additional_special_tokens=_lowerCamelCase , sp_model_kwargs=self.sp_model_kwargs , **_lowerCamelCase , ) _SCREAMING_SNAKE_CASE = vocab_file _SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(_lowerCamelCase ) @property def UpperCamelCase ( self ) -> List[str]: return self.sp_model.get_piece_size() def UpperCamelCase ( self ) -> Dict[str, int]: _SCREAMING_SNAKE_CASE = {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 ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = self.__dict__.copy() _SCREAMING_SNAKE_CASE = None return state def __setstate__( self , A__ ) -> Tuple: _SCREAMING_SNAKE_CASE = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): _SCREAMING_SNAKE_CASE = {} _SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCamelCase ( self , A__ ) -> List[str]: return self.sp_model.encode(_lowerCamelCase , out_type=_lowerCamelCase ) def UpperCamelCase ( self , A__ ) -> List[Any]: return self.sp_model.piece_to_id(_lowerCamelCase ) def UpperCamelCase ( self , A__ ) -> Union[str, Any]: if index < self.sp_model.get_piece_size(): _SCREAMING_SNAKE_CASE = self.sp_model.IdToPiece(_lowerCamelCase ) return token def UpperCamelCase ( self , A__ ) -> Dict: _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = """""" 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 _SCREAMING_SNAKE_CASE = [] else: current_sub_tokens.append(_lowerCamelCase ) out_string += self.sp_model.decode(_lowerCamelCase ) return out_string.strip() def UpperCamelCase ( self , A__ , A__ = 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"""] ) 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: _SCREAMING_SNAKE_CASE = self.sp_model.serialized_model_proto() fi.write(_lowerCamelCase ) return (out_vocab_file,)
711
'''simple docstring''' 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 UpperCamelCase__ : List[Any] = logging.get_logger(__name__) UpperCamelCase__ : Any = "▁" UpperCamelCase__ : Any = {"vocab_file": "spiece.model"} UpperCamelCase__ : int = { "vocab_file": { "google/reformer-crime-and-punishment": ( "https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/spiece.model" ) } } UpperCamelCase__ : Optional[int] = { "google/reformer-crime-and-punishment": 524_288, } class _a (_lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE = ['input_ids', 'attention_mask'] def __init__( self , A__ , A__="</s>" , A__="<unk>" , A__=[] , A__ = None , **A__ , ) -> None: _SCREAMING_SNAKE_CASE = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( eos_token=A__ , unk_token=A__ , additional_special_tokens=A__ , sp_model_kwargs=self.sp_model_kwargs , **A__ , ) _SCREAMING_SNAKE_CASE = vocab_file _SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(A__ ) @property def UpperCamelCase ( self ) -> Any: return self.sp_model.get_piece_size() def UpperCamelCase ( self ) -> Dict[str, int]: _SCREAMING_SNAKE_CASE = {self.convert_ids_to_tokens(A__ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ) -> int: _SCREAMING_SNAKE_CASE = self.__dict__.copy() _SCREAMING_SNAKE_CASE = None return state def __setstate__( self , A__ ) -> str: _SCREAMING_SNAKE_CASE = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): _SCREAMING_SNAKE_CASE = {} _SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCamelCase ( self , A__ ) -> List[str]: return self.sp_model.encode(A__ , out_type=A__ ) def UpperCamelCase ( self , A__ ) -> Union[str, Any]: return self.sp_model.piece_to_id(A__ ) def UpperCamelCase ( self , A__ ) -> List[Any]: if index < self.sp_model.get_piece_size(): _SCREAMING_SNAKE_CASE = self.sp_model.IdToPiece(A__ ) return token def UpperCamelCase ( self , A__ ) -> str: _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = """""" 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(A__ ) + token _SCREAMING_SNAKE_CASE = [] else: current_sub_tokens.append(A__ ) out_string += self.sp_model.decode(A__ ) return out_string.strip() def UpperCamelCase ( self , A__ , A__ = None ) -> Tuple[str]: if not os.path.isdir(A__ ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return _SCREAMING_SNAKE_CASE = os.path.join( A__ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(A__ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , A__ ) elif not os.path.isfile(self.vocab_file ): with open(A__ , """wb""" ) as fi: _SCREAMING_SNAKE_CASE = self.sp_model.serialized_model_proto() fi.write(A__ ) return (out_vocab_file,)
0
0
'''simple docstring''' def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> list: """simple docstring""" if len(__snake_case ) < 2: return collection def circle_sort_util(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> bool: _SCREAMING_SNAKE_CASE = False if low == high: return swapped _SCREAMING_SNAKE_CASE = low _SCREAMING_SNAKE_CASE = high while left < right: if collection[left] > collection[right]: _SCREAMING_SNAKE_CASE = ( collection[right], collection[left], ) _SCREAMING_SNAKE_CASE = True left += 1 right -= 1 if left == right and collection[left] > collection[right + 1]: _SCREAMING_SNAKE_CASE = ( collection[right + 1], collection[left], ) _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = low + int((high - low) / 2 ) _SCREAMING_SNAKE_CASE = circle_sort_util(__snake_case , __snake_case , __snake_case ) _SCREAMING_SNAKE_CASE = circle_sort_util(__snake_case , mid + 1 , __snake_case ) return swapped or left_swap or right_swap _SCREAMING_SNAKE_CASE = True while is_not_sorted is True: _SCREAMING_SNAKE_CASE = circle_sort_util(__snake_case , 0 , len(__snake_case ) - 1 ) return collection if __name__ == "__main__": UpperCamelCase__ : Dict = input("Enter numbers separated by a comma:\n").strip() UpperCamelCase__ : Dict = [int(item) for item in user_input.split(",")] print(circle_sort(unsorted))
712
'''simple docstring''' import os import unittest from transformers import MobileBertTokenizer, MobileBertTokenizerFast from transformers.models.bert.tokenization_bert import ( VOCAB_FILES_NAMES, BasicTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin, filter_non_english @require_tokenizers class _a (_lowerCamelCase , unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = MobileBertTokenizer SCREAMING_SNAKE_CASE = MobileBertTokenizerFast SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = filter_non_english SCREAMING_SNAKE_CASE = 'google/mobilebert-uncased' def UpperCamelCase ( self ) -> Any: super().setUp() _SCREAMING_SNAKE_CASE = [ """[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest""", ] _SCREAMING_SNAKE_CASE = 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] ) ) _SCREAMING_SNAKE_CASE = [ (tokenizer_def[0], self.pre_trained_model_path, tokenizer_def[2]) # else the 'google/' prefix is stripped for tokenizer_def in self.tokenizers_list ] def UpperCamelCase ( self , A__ ) -> List[str]: _SCREAMING_SNAKE_CASE = """UNwant\u00E9d,running""" _SCREAMING_SNAKE_CASE = """unwanted, running""" return input_text, output_text def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = self.tokenizer_class(self.vocab_file ) _SCREAMING_SNAKE_CASE = tokenizer.tokenize("""UNwant\u00E9d,running""" ) self.assertListEqual(A__ , ["""un""", """##want""", """##ed""", """,""", """runn""", """##ing"""] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(A__ ) , [9, 6, 7, 12, 10, 11] ) def UpperCamelCase ( self ) -> Optional[int]: if not self.test_rust_tokenizer: return _SCREAMING_SNAKE_CASE = self.get_tokenizer() _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() _SCREAMING_SNAKE_CASE = """UNwant\u00E9d,running""" _SCREAMING_SNAKE_CASE = tokenizer.tokenize(A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.tokenize(A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = tokenizer.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.encode(A__ , add_special_tokens=A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() _SCREAMING_SNAKE_CASE = tokenizer.encode(A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.encode(A__ ) self.assertListEqual(A__ , A__ ) # With lower casing _SCREAMING_SNAKE_CASE = self.get_tokenizer(do_lower_case=A__ ) _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer(do_lower_case=A__ ) _SCREAMING_SNAKE_CASE = """UNwant\u00E9d,running""" _SCREAMING_SNAKE_CASE = tokenizer.tokenize(A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.tokenize(A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = tokenizer.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.encode(A__ , add_special_tokens=A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() _SCREAMING_SNAKE_CASE = tokenizer.encode(A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.encode(A__ ) self.assertListEqual(A__ , A__ ) def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = BasicTokenizer() self.assertListEqual(tokenizer.tokenize("""ah\u535A\u63A8zz""" ) , ["""ah""", """\u535A""", """\u63A8""", """zz"""] ) def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHeLLo!how \n Are yoU? """ ) , ["""hello""", """!""", """how""", """are""", """you""", """?"""] ) self.assertListEqual(tokenizer.tokenize("""H\u00E9llo""" ) , ["""hello"""] ) def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , strip_accents=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""hällo""", """!""", """how""", """are""", """you""", """?"""] ) self.assertListEqual(tokenizer.tokenize("""H\u00E9llo""" ) , ["""h\u00E9llo"""] ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , strip_accents=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""hallo""", """!""", """how""", """are""", """you""", """?"""] ) self.assertListEqual(tokenizer.tokenize("""H\u00E9llo""" ) , ["""hello"""] ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""hallo""", """!""", """how""", """are""", """you""", """?"""] ) self.assertListEqual(tokenizer.tokenize("""H\u00E9llo""" ) , ["""hello"""] ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHeLLo!how \n Are yoU? """ ) , ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?"""] ) def UpperCamelCase ( self ) -> Dict: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , strip_accents=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""HäLLo""", """!""", """how""", """Are""", """yoU""", """?"""] ) def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , strip_accents=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""HaLLo""", """!""", """how""", """Are""", """yoU""", """?"""] ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , never_split=["""[UNK]"""] ) self.assertListEqual( tokenizer.tokenize(""" \tHeLLo!how \n Are yoU? [UNK]""" ) , ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?""", """[UNK]"""] ) def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = ["""[UNK]""", """[CLS]""", """[SEP]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing"""] _SCREAMING_SNAKE_CASE = {} for i, token in enumerate(A__ ): _SCREAMING_SNAKE_CASE = i _SCREAMING_SNAKE_CASE = WordpieceTokenizer(vocab=A__ , unk_token="""[UNK]""" ) self.assertListEqual(tokenizer.tokenize("""""" ) , [] ) self.assertListEqual(tokenizer.tokenize("""unwanted running""" ) , ["""un""", """##want""", """##ed""", """runn""", """##ing"""] ) self.assertListEqual(tokenizer.tokenize("""unwantedX running""" ) , ["""[UNK]""", """runn""", """##ing"""] ) def UpperCamelCase ( self ) -> str: self.assertTrue(_is_whitespace(""" """ ) ) self.assertTrue(_is_whitespace("""\t""" ) ) self.assertTrue(_is_whitespace("""\r""" ) ) self.assertTrue(_is_whitespace("""\n""" ) ) self.assertTrue(_is_whitespace("""\u00A0""" ) ) self.assertFalse(_is_whitespace("""A""" ) ) self.assertFalse(_is_whitespace("""-""" ) ) def UpperCamelCase ( self ) -> Union[str, Any]: self.assertTrue(_is_control("""\u0005""" ) ) self.assertFalse(_is_control("""A""" ) ) self.assertFalse(_is_control(""" """ ) ) self.assertFalse(_is_control("""\t""" ) ) self.assertFalse(_is_control("""\r""" ) ) def UpperCamelCase ( self ) -> Dict: self.assertTrue(_is_punctuation("""-""" ) ) self.assertTrue(_is_punctuation("""$""" ) ) self.assertTrue(_is_punctuation("""`""" ) ) self.assertTrue(_is_punctuation(""".""" ) ) self.assertFalse(_is_punctuation("""A""" ) ) self.assertFalse(_is_punctuation(""" """ ) ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = self.get_tokenizer() _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() # Example taken from the issue https://github.com/huggingface/tokenizers/issues/340 self.assertListEqual([tokenizer.tokenize(A__ ) for t in ["""Test""", """\xad""", """test"""]] , [["""[UNK]"""], [], ["""[UNK]"""]] ) self.assertListEqual( [rust_tokenizer.tokenize(A__ ) for t in ["""Test""", """\xad""", """test"""]] , [["""[UNK]"""], [], ["""[UNK]"""]] ) @slow def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained("""google/mobilebert-uncased""" ) _SCREAMING_SNAKE_CASE = tokenizer.encode("""sequence builders""" , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer.encode("""multi-sequence build""" , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer.build_inputs_with_special_tokens(A__ ) _SCREAMING_SNAKE_CASE = tokenizer.build_inputs_with_special_tokens(A__ , A__ ) assert encoded_sentence == [1_01] + text + [1_02] assert encoded_pair == [1_01] + text + [1_02] + text_a + [1_02] def UpperCamelCase ( self ) -> List[str]: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})" ): _SCREAMING_SNAKE_CASE = self.rust_tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = F"A, naïve {tokenizer_r.mask_token} AllenNLP sentence." _SCREAMING_SNAKE_CASE = tokenizer_r.encode_plus( A__ , return_attention_mask=A__ , return_token_type_ids=A__ , return_offsets_mapping=A__ , add_special_tokens=A__ , ) _SCREAMING_SNAKE_CASE = tokenizer_r.do_lower_case if hasattr(A__ , """do_lower_case""" ) else False _SCREAMING_SNAKE_CASE = ( [ ((0, 0), tokenizer_r.cls_token), ((0, 1), """A"""), ((1, 2), ""","""), ((3, 5), """na"""), ((5, 6), """##ï"""), ((6, 8), """##ve"""), ((9, 15), tokenizer_r.mask_token), ((16, 21), """Allen"""), ((21, 23), """##NL"""), ((23, 24), """##P"""), ((25, 33), """sentence"""), ((33, 34), """."""), ((0, 0), tokenizer_r.sep_token), ] if not do_lower_case else [ ((0, 0), tokenizer_r.cls_token), ((0, 1), """a"""), ((1, 2), ""","""), ((3, 8), """naive"""), ((9, 15), tokenizer_r.mask_token), ((16, 21), """allen"""), ((21, 23), """##nl"""), ((23, 24), """##p"""), ((25, 33), """sentence"""), ((33, 34), """."""), ((0, 0), tokenizer_r.sep_token), ] ) self.assertEqual( [e[1] for e in expected_results] , tokenizer_r.convert_ids_to_tokens(tokens["""input_ids"""] ) ) self.assertEqual([e[0] for e in expected_results] , tokens["""offset_mapping"""] ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = ["""的""", """人""", """有"""] _SCREAMING_SNAKE_CASE = """""".join(A__ ) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})" ): _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = self.rust_tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = tokenizer_p.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer_r.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer_r.convert_ids_to_tokens(A__ ) _SCREAMING_SNAKE_CASE = tokenizer_p.convert_ids_to_tokens(A__ ) # it is expected that each Chinese character is not preceded by "##" self.assertListEqual(A__ , A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = self.rust_tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = tokenizer_r.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer_p.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer_r.convert_ids_to_tokens(A__ ) _SCREAMING_SNAKE_CASE = tokenizer_p.convert_ids_to_tokens(A__ ) # it is expected that only the first Chinese character is not preceded by "##". _SCREAMING_SNAKE_CASE = [ F"##{token}" if idx != 0 else token for idx, token in enumerate(A__ ) ] self.assertListEqual(A__ , A__ ) self.assertListEqual(A__ , A__ )
0
0
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging UpperCamelCase__ : List[str] = logging.get_logger(__name__) UpperCamelCase__ : Any = { "kssteven/ibert-roberta-base": "https://huggingface.co/kssteven/ibert-roberta-base/resolve/main/config.json", "kssteven/ibert-roberta-large": "https://huggingface.co/kssteven/ibert-roberta-large/resolve/main/config.json", "kssteven/ibert-roberta-large-mnli": ( "https://huggingface.co/kssteven/ibert-roberta-large-mnli/resolve/main/config.json" ), } class _a (__lowercase): """simple docstring""" SCREAMING_SNAKE_CASE = 'ibert' def __init__( self , A__=3_05_22 , A__=7_68 , 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-12 , A__=1 , A__=0 , A__=2 , A__="absolute" , A__=False , A__="none" , **A__ , ) -> Any: super().__init__(pad_token_id=__A , bos_token_id=__A , eos_token_id=__A , **__A ) _SCREAMING_SNAKE_CASE = vocab_size _SCREAMING_SNAKE_CASE = hidden_size _SCREAMING_SNAKE_CASE = num_hidden_layers _SCREAMING_SNAKE_CASE = num_attention_heads _SCREAMING_SNAKE_CASE = hidden_act _SCREAMING_SNAKE_CASE = intermediate_size _SCREAMING_SNAKE_CASE = hidden_dropout_prob _SCREAMING_SNAKE_CASE = attention_probs_dropout_prob _SCREAMING_SNAKE_CASE = max_position_embeddings _SCREAMING_SNAKE_CASE = type_vocab_size _SCREAMING_SNAKE_CASE = initializer_range _SCREAMING_SNAKE_CASE = layer_norm_eps _SCREAMING_SNAKE_CASE = position_embedding_type _SCREAMING_SNAKE_CASE = quant_mode _SCREAMING_SNAKE_CASE = force_dequant class _a (__lowercase): """simple docstring""" @property def UpperCamelCase ( self ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": _SCREAMING_SNAKE_CASE = {0: """batch""", 1: """choice""", 2: """sequence"""} else: _SCREAMING_SNAKE_CASE = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
713
'''simple docstring''' import logging import os import quant_trainer import torch from torch.utils.data import DataLoader from transformers import Trainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput UpperCamelCase__ : Tuple = logging.getLogger(__name__) if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class _a (_lowerCamelCase): """simple docstring""" def __init__( self , *A__ , A__=None , A__=None , A__=None , **A__ ) -> Optional[int]: super().__init__(*A__ , **A__ ) _SCREAMING_SNAKE_CASE = eval_examples _SCREAMING_SNAKE_CASE = post_process_function _SCREAMING_SNAKE_CASE = quant_trainer_args _SCREAMING_SNAKE_CASE = 1_28 # default number of calibration samples def UpperCamelCase ( self , A__=None ) -> Union[str, Any]: if calib_dataset is None and self.calib_dataset is None: raise ValueError("""Trainer: calibration requires an calib_dataset.""" ) _SCREAMING_SNAKE_CASE = calib_dataset if calib_dataset is not None else self.calib_dataset _SCREAMING_SNAKE_CASE = self._remove_unused_columns(A__ , description="""Calibration""" ) return DataLoader( A__ , batch_size=self.args.eval_batch_size , collate_fn=self.data_collator , drop_last=self.args.dataloader_drop_last , num_workers=self.args.dataloader_num_workers , pin_memory=self.args.dataloader_pin_memory , shuffle=A__ , ) def UpperCamelCase ( self , A__=None ) -> str: _SCREAMING_SNAKE_CASE = self.train_dataset if calib_dataset is None else calib_dataset _SCREAMING_SNAKE_CASE = self.get_calib_dataloader(A__ ) _SCREAMING_SNAKE_CASE = self.model quant_trainer.configure_model(A__ , self.quant_trainer_args , calib=A__ ) model.eval() quant_trainer.enable_calibration(A__ ) logger.info("""***** Running calibration *****""" ) logger.info(F" Num examples = {self.calib_num}" ) logger.info(F" Batch size = {calib_dataloader.batch_size}" ) for step, inputs in enumerate(A__ ): # Prediction step _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self.prediction_step(A__ , A__ , prediction_loss_only=A__ ) if (step + 1) * calib_dataloader.batch_size >= self.calib_num: break quant_trainer.finish_calibration(A__ , self.quant_trainer_args ) _SCREAMING_SNAKE_CASE = model def UpperCamelCase ( self , A__=None , A__=None , A__=None , A__ = "eval" ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.eval_dataset if eval_dataset is None else eval_dataset _SCREAMING_SNAKE_CASE = self.get_eval_dataloader(A__ ) _SCREAMING_SNAKE_CASE = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. _SCREAMING_SNAKE_CASE = self.compute_metrics _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: _SCREAMING_SNAKE_CASE = eval_loop( A__ , description="""Evaluation""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=A__ , ) finally: _SCREAMING_SNAKE_CASE = compute_metrics if self.post_process_function is not None and self.compute_metrics is not None: _SCREAMING_SNAKE_CASE = self.post_process_function(A__ , A__ , output.predictions ) _SCREAMING_SNAKE_CASE = self.compute_metrics(A__ ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(F"{metric_key_prefix}_" ): _SCREAMING_SNAKE_CASE = metrics.pop(A__ ) self.log(A__ ) else: _SCREAMING_SNAKE_CASE = {} if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report() ) _SCREAMING_SNAKE_CASE = self.callback_handler.on_evaluate(self.args , self.state , self.control , A__ ) return metrics def UpperCamelCase ( self , A__ , A__ , A__=None , A__ = "test" ) -> List[str]: _SCREAMING_SNAKE_CASE = self.get_test_dataloader(A__ ) # Temporarily disable metric computation, we will do it in the loop here. _SCREAMING_SNAKE_CASE = self.compute_metrics _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: _SCREAMING_SNAKE_CASE = eval_loop( A__ , description="""Prediction""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=A__ , ) finally: _SCREAMING_SNAKE_CASE = compute_metrics if self.post_process_function is None or self.compute_metrics is None: return output _SCREAMING_SNAKE_CASE = self.post_process_function(A__ , A__ , output.predictions , """predict""" ) _SCREAMING_SNAKE_CASE = self.compute_metrics(A__ ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(F"{metric_key_prefix}_" ): _SCREAMING_SNAKE_CASE = metrics.pop(A__ ) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=A__ ) def UpperCamelCase ( self , A__="./" ) -> Tuple: _SCREAMING_SNAKE_CASE = self.eval_dataset _SCREAMING_SNAKE_CASE = self.get_eval_dataloader(A__ ) _SCREAMING_SNAKE_CASE = next(iter(A__ ) ) # saving device - to make it consistent _SCREAMING_SNAKE_CASE = torch.device("""cuda""" if torch.cuda.is_available() else """cpu""" ) # convert to tuple _SCREAMING_SNAKE_CASE = tuple(v.to(A__ ) for k, v in batch.items() ) logger.info("""Converting model to be onnx compatible""" ) from pytorch_quantization.nn import TensorQuantizer _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = self.model.to(A__ ) model.eval() model.float() _SCREAMING_SNAKE_CASE = model.module if hasattr(A__ , """module""" ) else model quant_trainer.configure_model(A__ , self.quant_trainer_args ) _SCREAMING_SNAKE_CASE = os.path.join(A__ , """model.onnx""" ) logger.info(F"exporting model to {output_model_file}" ) _SCREAMING_SNAKE_CASE = {0: """batch_size""", 1: """seq_len"""} torch.onnx.export( A__ , A__ , A__ , export_params=A__ , opset_version=13 , do_constant_folding=A__ , input_names=["""input_ids""", """attention_mask""", """token_type_ids"""] , output_names=["""output_start_logits""", """output_end_logits"""] , dynamic_axes={ """input_ids""": axes, """attention_mask""": axes, """token_type_ids""": axes, """output_start_logits""": axes, """output_end_logits""": axes, } , verbose=A__ , ) logger.info("""onnx export finished""" )
0
0
import argparse import os import pickle import sys import torch from transformers import TransfoXLConfig, TransfoXLLMHeadModel, load_tf_weights_in_transfo_xl from transformers.models.transfo_xl import tokenization_transfo_xl as data_utils from transformers.models.transfo_xl.tokenization_transfo_xl import CORPUS_NAME, VOCAB_FILES_NAMES from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() # We do this to be able to load python 2 datasets pickles # See e.g. https://stackoverflow.com/questions/2121874/python-pickling-after-changing-a-modules-directory/2121918#2121918 UpperCamelCase__ : List[Any] = data_utils.TransfoXLTokenizer UpperCamelCase__ : List[Any] = data_utils.TransfoXLCorpus UpperCamelCase__ : str = data_utils UpperCamelCase__ : Optional[int] = data_utils def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Tuple: """simple docstring""" if transfo_xl_dataset_file: # Convert a pre-processed corpus (see original TensorFlow repo) with open(UpperCamelCase__ , """rb""" ) as fp: _SCREAMING_SNAKE_CASE = pickle.load(UpperCamelCase__ , encoding="""latin1""" ) # Save vocabulary and dataset cache as Dictionaries (should be better than pickles for the long-term) _SCREAMING_SNAKE_CASE = pytorch_dump_folder_path + """/""" + VOCAB_FILES_NAMES["""pretrained_vocab_file"""] print(F"Save vocabulary to {pytorch_vocab_dump_path}" ) _SCREAMING_SNAKE_CASE = corpus.vocab.__dict__ torch.save(UpperCamelCase__ , UpperCamelCase__ ) _SCREAMING_SNAKE_CASE = corpus.__dict__ corpus_dict_no_vocab.pop("""vocab""" , UpperCamelCase__ ) _SCREAMING_SNAKE_CASE = pytorch_dump_folder_path + """/""" + CORPUS_NAME print(F"Save dataset to {pytorch_dataset_dump_path}" ) torch.save(UpperCamelCase__ , UpperCamelCase__ ) if tf_checkpoint_path: # Convert a pre-trained TensorFlow model _SCREAMING_SNAKE_CASE = os.path.abspath(UpperCamelCase__ ) _SCREAMING_SNAKE_CASE = os.path.abspath(UpperCamelCase__ ) print(F"Converting Transformer XL checkpoint from {tf_path} with config at {config_path}." ) # Initialise PyTorch model if transfo_xl_config_file == "": _SCREAMING_SNAKE_CASE = TransfoXLConfig() else: _SCREAMING_SNAKE_CASE = TransfoXLConfig.from_json_file(UpperCamelCase__ ) print(F"Building PyTorch model from configuration: {config}" ) _SCREAMING_SNAKE_CASE = TransfoXLLMHeadModel(UpperCamelCase__ ) _SCREAMING_SNAKE_CASE = load_tf_weights_in_transfo_xl(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) # Save pytorch-model _SCREAMING_SNAKE_CASE = os.path.join(UpperCamelCase__ , UpperCamelCase__ ) _SCREAMING_SNAKE_CASE = os.path.join(UpperCamelCase__ , UpperCamelCase__ ) print(F"Save PyTorch model to {os.path.abspath(UpperCamelCase__ )}" ) torch.save(model.state_dict() , UpperCamelCase__ ) print(F"Save configuration file to {os.path.abspath(UpperCamelCase__ )}" ) with open(UpperCamelCase__ , """w""" , encoding="""utf-8""" ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": UpperCamelCase__ : Dict = argparse.ArgumentParser() parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the folder to store the PyTorch model or dataset/vocab.", ) parser.add_argument( "--tf_checkpoint_path", default="", type=str, help="An optional path to a TensorFlow checkpoint path to be converted.", ) parser.add_argument( "--transfo_xl_config_file", default="", type=str, help=( "An optional config json file corresponding to the pre-trained BERT model. \n" "This specifies the model architecture." ), ) parser.add_argument( "--transfo_xl_dataset_file", default="", type=str, help="An optional dataset file to be converted in a vocabulary.", ) UpperCamelCase__ : int = parser.parse_args() convert_transfo_xl_checkpoint_to_pytorch( args.tf_checkpoint_path, args.transfo_xl_config_file, args.pytorch_dump_folder_path, args.transfo_xl_dataset_file, )
714
'''simple docstring''' def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" return "".join([hex(SCREAMING_SNAKE_CASE_ )[2:].zfill(2 ).upper() for byte in list(SCREAMING_SNAKE_CASE_ )] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> bytes: """simple docstring""" # Check data validity, following RFC3548 # https://www.ietf.org/rfc/rfc3548.txt if (len(SCREAMING_SNAKE_CASE_ ) % 2) != 0: raise ValueError( """Base16 encoded data is invalid: Data does not have an even number of hex digits.""" ) # Check the character set - the standard base16 alphabet # is uppercase according to RFC3548 section 6 if not set(SCREAMING_SNAKE_CASE_ ) <= set("""0123456789ABCDEF""" ): raise ValueError( """Base16 encoded data is invalid: Data is not uppercase hex or it contains invalid characters.""" ) # For every two hexadecimal digits (= a byte), turn it into an integer. # Then, string the result together into bytes, and return it. return bytes(int(data[i] + data[i + 1] , 16 ) for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) , 2 ) ) if __name__ == "__main__": import doctest doctest.testmod()
0
0
'''simple docstring''' def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" if not grid or not grid[0]: raise TypeError("""The grid does not contain the appropriate information""" ) for cell_n in range(1 , len(grid[0] ) ): grid[0][cell_n] += grid[0][cell_n - 1] _SCREAMING_SNAKE_CASE = grid[0] for row_n in range(1 , len(_UpperCAmelCase ) ): _SCREAMING_SNAKE_CASE = grid[row_n] _SCREAMING_SNAKE_CASE = fill_row(_UpperCAmelCase , _UpperCAmelCase ) _SCREAMING_SNAKE_CASE = grid[row_n] return grid[-1][-1] def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" current_row[0] += row_above[0] for cell_n in range(1 , len(_UpperCAmelCase ) ): current_row[cell_n] += min(current_row[cell_n - 1] , row_above[cell_n] ) return current_row if __name__ == "__main__": import doctest doctest.testmod()
715
'''simple docstring''' import pytest import requests from datasets.utils.file_utils import http_head from .utils import OfflineSimulationMode, RequestWouldHangIndefinitelyError, offline @pytest.mark.integration def lowerCAmelCase_ ( ) -> List[Any]: """simple docstring""" with offline(OfflineSimulationMode.CONNECTION_TIMES_OUT ): with pytest.raises(SCREAMING_SNAKE_CASE_ ): requests.request("""GET""" , """https://huggingface.co""" ) with pytest.raises(requests.exceptions.ConnectTimeout ): requests.request("""GET""" , """https://huggingface.co""" , timeout=1.0 ) @pytest.mark.integration def lowerCAmelCase_ ( ) -> int: """simple docstring""" with offline(OfflineSimulationMode.CONNECTION_FAILS ): with pytest.raises(requests.exceptions.ConnectionError ): requests.request("""GET""" , """https://huggingface.co""" ) def lowerCAmelCase_ ( ) -> Optional[Any]: """simple docstring""" with offline(OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1 ): with pytest.raises(SCREAMING_SNAKE_CASE_ ): http_head("""https://huggingface.co""" )
0
0
import tempfile import torch from diffusers import ( DEISMultistepScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) from .test_schedulers import SchedulerCommonTest class _a (UpperCamelCase_): """simple docstring""" SCREAMING_SNAKE_CASE = (DEISMultistepScheduler,) SCREAMING_SNAKE_CASE = (('num_inference_steps', 25),) def UpperCamelCase ( self , **A__ ) -> List[Any]: _SCREAMING_SNAKE_CASE = { 'num_train_timesteps': 10_00, 'beta_start': 0.0001, 'beta_end': 0.02, 'beta_schedule': 'linear', 'solver_order': 2, } config.update(**__a ) return config def UpperCamelCase ( self , A__=0 , **A__ ) -> Optional[int]: _SCREAMING_SNAKE_CASE = dict(self.forward_default_kwargs ) _SCREAMING_SNAKE_CASE = kwargs.pop("""num_inference_steps""" , __a ) _SCREAMING_SNAKE_CASE = self.dummy_sample _SCREAMING_SNAKE_CASE = 0.1 * sample _SCREAMING_SNAKE_CASE = [residual + 0.2, residual + 0.15, residual + 0.10] for scheduler_class in self.scheduler_classes: _SCREAMING_SNAKE_CASE = self.get_scheduler_config(**__a ) _SCREAMING_SNAKE_CASE = scheduler_class(**__a ) scheduler.set_timesteps(__a ) # copy over dummy past residuals _SCREAMING_SNAKE_CASE = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__a ) _SCREAMING_SNAKE_CASE = scheduler_class.from_pretrained(__a ) new_scheduler.set_timesteps(__a ) # copy over dummy past residuals _SCREAMING_SNAKE_CASE = dummy_past_residuals[: new_scheduler.config.solver_order] _SCREAMING_SNAKE_CASE = sample, sample for t in range(__a , time_step + scheduler.config.solver_order + 1 ): _SCREAMING_SNAKE_CASE = scheduler.step(__a , __a , __a , **__a ).prev_sample _SCREAMING_SNAKE_CASE = new_scheduler.step(__a , __a , __a , **__a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def UpperCamelCase ( self ) -> Tuple: pass def UpperCamelCase ( self , A__=0 , **A__ ) -> Any: _SCREAMING_SNAKE_CASE = dict(self.forward_default_kwargs ) _SCREAMING_SNAKE_CASE = kwargs.pop("""num_inference_steps""" , __a ) _SCREAMING_SNAKE_CASE = self.dummy_sample _SCREAMING_SNAKE_CASE = 0.1 * sample _SCREAMING_SNAKE_CASE = [residual + 0.2, residual + 0.15, residual + 0.10] for scheduler_class in self.scheduler_classes: _SCREAMING_SNAKE_CASE = self.get_scheduler_config() _SCREAMING_SNAKE_CASE = scheduler_class(**__a ) scheduler.set_timesteps(__a ) # copy over dummy past residuals (must be after setting timesteps) _SCREAMING_SNAKE_CASE = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__a ) _SCREAMING_SNAKE_CASE = scheduler_class.from_pretrained(__a ) # copy over dummy past residuals new_scheduler.set_timesteps(__a ) # copy over dummy past residual (must be after setting timesteps) _SCREAMING_SNAKE_CASE = dummy_past_residuals[: new_scheduler.config.solver_order] _SCREAMING_SNAKE_CASE = scheduler.step(__a , __a , __a , **__a ).prev_sample _SCREAMING_SNAKE_CASE = new_scheduler.step(__a , __a , __a , **__a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def UpperCamelCase ( self , A__=None , **A__ ) -> Optional[Any]: if scheduler is None: _SCREAMING_SNAKE_CASE = self.scheduler_classes[0] _SCREAMING_SNAKE_CASE = self.get_scheduler_config(**__a ) _SCREAMING_SNAKE_CASE = scheduler_class(**__a ) _SCREAMING_SNAKE_CASE = self.scheduler_classes[0] _SCREAMING_SNAKE_CASE = self.get_scheduler_config(**__a ) _SCREAMING_SNAKE_CASE = scheduler_class(**__a ) _SCREAMING_SNAKE_CASE = 10 _SCREAMING_SNAKE_CASE = self.dummy_model() _SCREAMING_SNAKE_CASE = self.dummy_sample_deter scheduler.set_timesteps(__a ) for i, t in enumerate(scheduler.timesteps ): _SCREAMING_SNAKE_CASE = model(__a , __a ) _SCREAMING_SNAKE_CASE = scheduler.step(__a , __a , __a ).prev_sample return sample def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = dict(self.forward_default_kwargs ) _SCREAMING_SNAKE_CASE = kwargs.pop("""num_inference_steps""" , __a ) for scheduler_class in self.scheduler_classes: _SCREAMING_SNAKE_CASE = self.get_scheduler_config() _SCREAMING_SNAKE_CASE = scheduler_class(**__a ) _SCREAMING_SNAKE_CASE = self.dummy_sample _SCREAMING_SNAKE_CASE = 0.1 * sample if num_inference_steps is not None and hasattr(__a , """set_timesteps""" ): scheduler.set_timesteps(__a ) elif num_inference_steps is not None and not hasattr(__a , """set_timesteps""" ): _SCREAMING_SNAKE_CASE = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) _SCREAMING_SNAKE_CASE = [residual + 0.2, residual + 0.15, residual + 0.10] _SCREAMING_SNAKE_CASE = dummy_past_residuals[: scheduler.config.solver_order] _SCREAMING_SNAKE_CASE = scheduler.timesteps[5] _SCREAMING_SNAKE_CASE = scheduler.timesteps[6] _SCREAMING_SNAKE_CASE = scheduler.step(__a , __a , __a , **__a ).prev_sample _SCREAMING_SNAKE_CASE = scheduler.step(__a , __a , __a , **__a ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def UpperCamelCase ( self ) -> int: _SCREAMING_SNAKE_CASE = DEISMultistepScheduler(**self.get_scheduler_config() ) _SCREAMING_SNAKE_CASE = self.full_loop(scheduler=__a ) _SCREAMING_SNAKE_CASE = torch.mean(torch.abs(__a ) ) assert abs(result_mean.item() - 0.2_3916 ) < 1E-3 _SCREAMING_SNAKE_CASE = DPMSolverSinglestepScheduler.from_config(scheduler.config ) _SCREAMING_SNAKE_CASE = DPMSolverMultistepScheduler.from_config(scheduler.config ) _SCREAMING_SNAKE_CASE = UniPCMultistepScheduler.from_config(scheduler.config ) _SCREAMING_SNAKE_CASE = DEISMultistepScheduler.from_config(scheduler.config ) _SCREAMING_SNAKE_CASE = self.full_loop(scheduler=__a ) _SCREAMING_SNAKE_CASE = torch.mean(torch.abs(__a ) ) assert abs(result_mean.item() - 0.2_3916 ) < 1E-3 def UpperCamelCase ( self ) -> int: for timesteps in [25, 50, 1_00, 9_99, 10_00]: self.check_over_configs(num_train_timesteps=__a ) def UpperCamelCase ( self ) -> Tuple: self.check_over_configs(thresholding=__a ) for order in [1, 2, 3]: for solver_type in ["logrho"]: for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( thresholding=__a , prediction_type=__a , sample_max_value=__a , algorithm_type="""deis""" , solver_order=__a , solver_type=__a , ) def UpperCamelCase ( self ) -> int: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=__a ) def UpperCamelCase ( self ) -> Optional[Any]: for algorithm_type in ["deis"]: for solver_type in ["logrho"]: for order in [1, 2, 3]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( solver_order=__a , solver_type=__a , prediction_type=__a , algorithm_type=__a , ) _SCREAMING_SNAKE_CASE = self.full_loop( solver_order=__a , solver_type=__a , prediction_type=__a , algorithm_type=__a , ) assert not torch.isnan(__a ).any(), "Samples have nan numbers" def UpperCamelCase ( self ) -> Any: self.check_over_configs(lower_order_final=__a ) self.check_over_configs(lower_order_final=__a ) def UpperCamelCase ( self ) -> Optional[int]: for num_inference_steps in [1, 2, 3, 5, 10, 50, 1_00, 9_99, 10_00]: self.check_over_forward(num_inference_steps=__a , time_step=0 ) def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = self.full_loop() _SCREAMING_SNAKE_CASE = torch.mean(torch.abs(__a ) ) assert abs(result_mean.item() - 0.2_3916 ) < 1E-3 def UpperCamelCase ( self ) -> Dict: _SCREAMING_SNAKE_CASE = self.full_loop(prediction_type="""v_prediction""" ) _SCREAMING_SNAKE_CASE = torch.mean(torch.abs(__a ) ) assert abs(result_mean.item() - 0.091 ) < 1E-3 def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.scheduler_classes[0] _SCREAMING_SNAKE_CASE = self.get_scheduler_config(thresholding=__a , dynamic_thresholding_ratio=0 ) _SCREAMING_SNAKE_CASE = scheduler_class(**__a ) _SCREAMING_SNAKE_CASE = 10 _SCREAMING_SNAKE_CASE = self.dummy_model() _SCREAMING_SNAKE_CASE = self.dummy_sample_deter.half() scheduler.set_timesteps(__a ) for i, t in enumerate(scheduler.timesteps ): _SCREAMING_SNAKE_CASE = model(__a , __a ) _SCREAMING_SNAKE_CASE = scheduler.step(__a , __a , __a ).prev_sample assert sample.dtype == torch.floataa
716
'''simple docstring''' import math from collections.abc import Iterator from itertools import takewhile def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> bool: """simple docstring""" if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(SCREAMING_SNAKE_CASE_ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def lowerCAmelCase_ ( ) -> Iterator[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = 2 while True: if is_prime(SCREAMING_SNAKE_CASE_ ): yield num num += 1 def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ = 2_00_00_00 ) -> int: """simple docstring""" return sum(takewhile(lambda SCREAMING_SNAKE_CASE_ : x < n , prime_generator() ) ) if __name__ == "__main__": print(f"""{solution() = }""")
0
0
'''simple docstring''' from collections import Counter import numpy as np from sklearn import datasets from sklearn.model_selection import train_test_split UpperCamelCase__ : List[str] = datasets.load_iris() UpperCamelCase__ : List[Any] = np.array(data["data"]) UpperCamelCase__ : str = np.array(data["target"]) UpperCamelCase__ : Optional[int] = data["target_names"] UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ : str = train_test_split(X, y) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" return np.linalg.norm(np.array(_lowercase ) - np.array(_lowercase ) ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=5 ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE = zip(_lowercase , _lowercase ) # List of distances of all points from the point to be classified _SCREAMING_SNAKE_CASE = [] for data_point in data: _SCREAMING_SNAKE_CASE = euclidean_distance(data_point[0] , _lowercase ) distances.append((distance, data_point[1]) ) # Choosing 'k' points with the least distances. _SCREAMING_SNAKE_CASE = [i[1] for i in sorted(_lowercase )[:k]] # Most commonly occurring class among them # is the class into which the point is classified _SCREAMING_SNAKE_CASE = Counter(_lowercase ).most_common(1 )[0][0] return classes[result] if __name__ == "__main__": print(classifier(X_train, y_train, classes, [4.4, 3.1, 1.3, 1.4]))
717
'''simple docstring''' import unittest import numpy as np from transformers.testing_utils import require_flax, require_tf, require_torch from transformers.utils import ( expand_dims, flatten_dict, is_flax_available, is_tf_available, is_torch_available, reshape, squeeze, transpose, ) if is_flax_available(): import jax.numpy as jnp if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch class _a (unittest.TestCase): """simple docstring""" def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = { """task_specific_params""": { """summarization""": {"""length_penalty""": 1.0, """max_length""": 1_28, """min_length""": 12, """num_beams""": 4}, """summarization_cnn""": {"""length_penalty""": 2.0, """max_length""": 1_42, """min_length""": 56, """num_beams""": 4}, """summarization_xsum""": {"""length_penalty""": 1.0, """max_length""": 62, """min_length""": 11, """num_beams""": 6}, } } _SCREAMING_SNAKE_CASE = { """task_specific_params.summarization.length_penalty""": 1.0, """task_specific_params.summarization.max_length""": 1_28, """task_specific_params.summarization.min_length""": 12, """task_specific_params.summarization.num_beams""": 4, """task_specific_params.summarization_cnn.length_penalty""": 2.0, """task_specific_params.summarization_cnn.max_length""": 1_42, """task_specific_params.summarization_cnn.min_length""": 56, """task_specific_params.summarization_cnn.num_beams""": 4, """task_specific_params.summarization_xsum.length_penalty""": 1.0, """task_specific_params.summarization_xsum.max_length""": 62, """task_specific_params.summarization_xsum.min_length""": 11, """task_specific_params.summarization_xsum.num_beams""": 6, } self.assertEqual(flatten_dict(A__ ) , A__ ) def UpperCamelCase ( self ) -> int: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(transpose(A__ ) , x.transpose() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(transpose(A__ , axes=(1, 2, 0) ) , x.transpose((1, 2, 0) ) ) ) @require_torch def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(transpose(A__ ) , transpose(A__ ).numpy() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(transpose(A__ , axes=(1, 2, 0) ) , transpose(A__ , axes=(1, 2, 0) ).numpy() ) ) @require_tf def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(transpose(A__ ) , transpose(A__ ).numpy() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(transpose(A__ , axes=(1, 2, 0) ) , transpose(A__ , axes=(1, 2, 0) ).numpy() ) ) @require_flax def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(transpose(A__ ) , np.asarray(transpose(A__ ) ) ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(transpose(A__ , axes=(1, 2, 0) ) , np.asarray(transpose(A__ , axes=(1, 2, 0) ) ) ) ) def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(reshape(A__ , (4, 3) ) , np.reshape(A__ , (4, 3) ) ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(reshape(A__ , (12, 5) ) , np.reshape(A__ , (12, 5) ) ) ) @require_torch def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(reshape(A__ , (4, 3) ) , reshape(A__ , (4, 3) ).numpy() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(reshape(A__ , (12, 5) ) , reshape(A__ , (12, 5) ).numpy() ) ) @require_tf def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(reshape(A__ , (4, 3) ) , reshape(A__ , (4, 3) ).numpy() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(reshape(A__ , (12, 5) ) , reshape(A__ , (12, 5) ).numpy() ) ) @require_flax def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(reshape(A__ , (4, 3) ) , np.asarray(reshape(A__ , (4, 3) ) ) ) ) _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 , 5 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(reshape(A__ , (12, 5) ) , np.asarray(reshape(A__ , (12, 5) ) ) ) ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = np.random.randn(1 , 3 , 4 ) self.assertTrue(np.allclose(squeeze(A__ ) , np.squeeze(A__ ) ) ) _SCREAMING_SNAKE_CASE = np.random.randn(1 , 4 , 1 , 5 ) self.assertTrue(np.allclose(squeeze(A__ , axis=2 ) , np.squeeze(A__ , axis=2 ) ) ) @require_torch def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = np.random.randn(1 , 3 , 4 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(squeeze(A__ ) , squeeze(A__ ).numpy() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(1 , 4 , 1 , 5 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(squeeze(A__ , axis=2 ) , squeeze(A__ , axis=2 ).numpy() ) ) @require_tf def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = np.random.randn(1 , 3 , 4 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(squeeze(A__ ) , squeeze(A__ ).numpy() ) ) _SCREAMING_SNAKE_CASE = np.random.randn(1 , 4 , 1 , 5 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(squeeze(A__ , axis=2 ) , squeeze(A__ , axis=2 ).numpy() ) ) @require_flax def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = np.random.randn(1 , 3 , 4 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(squeeze(A__ ) , np.asarray(squeeze(A__ ) ) ) ) _SCREAMING_SNAKE_CASE = np.random.randn(1 , 4 , 1 , 5 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(squeeze(A__ , axis=2 ) , np.asarray(squeeze(A__ , axis=2 ) ) ) ) def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(expand_dims(A__ , axis=1 ) , np.expand_dims(A__ , axis=1 ) ) ) @require_torch def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) self.assertTrue(np.allclose(expand_dims(A__ , axis=1 ) , expand_dims(A__ , axis=1 ).numpy() ) ) @require_tf def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = tf.constant(A__ ) self.assertTrue(np.allclose(expand_dims(A__ , axis=1 ) , expand_dims(A__ , axis=1 ).numpy() ) ) @require_flax def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = np.random.randn(3 , 4 ) _SCREAMING_SNAKE_CASE = jnp.array(A__ ) self.assertTrue(np.allclose(expand_dims(A__ , axis=1 ) , np.asarray(expand_dims(A__ , axis=1 ) ) ) )
0
0
'''simple docstring''' UpperCamelCase__ : List[Any] = { "a": "AAAAA", "b": "AAAAB", "c": "AAABA", "d": "AAABB", "e": "AABAA", "f": "AABAB", "g": "AABBA", "h": "AABBB", "i": "ABAAA", "j": "BBBAA", "k": "ABAAB", "l": "ABABA", "m": "ABABB", "n": "ABBAA", "o": "ABBAB", "p": "ABBBA", "q": "ABBBB", "r": "BAAAA", "s": "BAAAB", "t": "BAABA", "u": "BAABB", "v": "BBBAB", "w": "BABAA", "x": "BABAB", "y": "BABBA", "z": "BABBB", " ": " ", } UpperCamelCase__ : Union[str, Any] = {value: key for key, value in encode_dict.items()} def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = """""" for letter in word.lower(): if letter.isalpha() or letter == " ": encoded += encode_dict[letter] else: raise Exception("""encode() accepts only letters of the alphabet and spaces""" ) return encoded def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" if set(_lowerCamelCase ) - {"A", "B", " "} != set(): raise Exception("""decode() accepts only 'A', 'B' and spaces""" ) _SCREAMING_SNAKE_CASE = """""" for word in coded.split(): while len(_lowerCamelCase ) != 0: decoded += decode_dict[word[:5]] _SCREAMING_SNAKE_CASE = word[5:] decoded += " " return decoded.strip() if __name__ == "__main__": from doctest import testmod testmod()
718
'''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 _a (_lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = '' SCREAMING_SNAKE_CASE = 'hf-legacy' # "hf://"" is reserved for hffs def __init__( self , A__ = None , A__ = None , **A__ , ) -> Optional[int]: super().__init__(self , **A__ ) _SCREAMING_SNAKE_CASE = repo_info _SCREAMING_SNAKE_CASE = token _SCREAMING_SNAKE_CASE = None def UpperCamelCase ( self ) -> Tuple: if self.dir_cache is None: _SCREAMING_SNAKE_CASE = {} for hf_file in self.repo_info.siblings: # TODO(QL): add sizes _SCREAMING_SNAKE_CASE = { """name""": hf_file.rfilename, """size""": None, """type""": """file""", } self.dir_cache.update( { str(A__ ): {"""name""": str(A__ ), """size""": None, """type""": """directory"""} for d in list(PurePosixPath(hf_file.rfilename ).parents )[:-1] } ) def UpperCamelCase ( self , A__ , A__ = "rb" , **A__ , ) -> Optional[int]: if not isinstance(self.repo_info , A__ ): raise NotImplementedError(F"Open is only implemented for dataset repositories, but got {self.repo_info}" ) _SCREAMING_SNAKE_CASE = hf_hub_url(self.repo_info.id , A__ , revision=self.repo_info.sha ) return fsspec.open( A__ , mode=A__ , headers=get_authentication_headers_for_url(A__ , use_auth_token=self.token ) , client_kwargs={"""trust_env""": True} , ).open() def UpperCamelCase ( self , A__ , **A__ ) -> str: self._get_dirs() _SCREAMING_SNAKE_CASE = self._strip_protocol(A__ ) if path in self.dir_cache: return self.dir_cache[path] else: raise FileNotFoundError(A__ ) def UpperCamelCase ( self , A__ , A__=False , **A__ ) -> List[Any]: self._get_dirs() _SCREAMING_SNAKE_CASE = PurePosixPath(path.strip("""/""" ) ) _SCREAMING_SNAKE_CASE = {} for p, f in self.dir_cache.items(): _SCREAMING_SNAKE_CASE = PurePosixPath(p.strip("""/""" ) ) _SCREAMING_SNAKE_CASE = p.parent if root == path: _SCREAMING_SNAKE_CASE = f _SCREAMING_SNAKE_CASE = list(paths.values() ) if detail: return out else: return sorted(f["""name"""] for f in out )
0
0
import numpy as np def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = int(np.ceil((x_end - xa) / h ) ) _SCREAMING_SNAKE_CASE = np.zeros((n + 1,) ) _SCREAMING_SNAKE_CASE = ya _SCREAMING_SNAKE_CASE = xa for k in range(lowercase_ ): _SCREAMING_SNAKE_CASE = f(lowercase_ , y[k] ) _SCREAMING_SNAKE_CASE = f(x + 0.5 * h , y[k] + 0.5 * h * ka ) _SCREAMING_SNAKE_CASE = f(x + 0.5 * h , y[k] + 0.5 * h * ka ) _SCREAMING_SNAKE_CASE = f(x + h , y[k] + h * ka ) _SCREAMING_SNAKE_CASE = y[k] + (1 / 6) * h * (ka + 2 * ka + 2 * ka + ka) x += h return y if __name__ == "__main__": import doctest doctest.testmod()
719
'''simple docstring''' import pyarrow.parquet as pq import pytest from datasets import Audio, Dataset, DatasetDict, Features, NamedSplit, Sequence, Value, config from datasets.features.image import Image from datasets.io.parquet import ParquetDatasetReader, ParquetDatasetWriter, get_writer_batch_size from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" assert isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("""keep_in_memory""" , [False, True] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): _SCREAMING_SNAKE_CASE = ParquetDatasetReader(SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ , keep_in_memory=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_dataset(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @pytest.mark.parametrize( """features""" , [ None, {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}, {"""col_1""": """string""", """col_2""": """string""", """col_3""": """string"""}, {"""col_1""": """int32""", """col_2""": """int32""", """col_3""": """int32"""}, {"""col_1""": """float32""", """col_2""": """float32""", """col_3""": """float32"""}, ] , ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} _SCREAMING_SNAKE_CASE = features.copy() if features else default_expected_features _SCREAMING_SNAKE_CASE = ( Features({feature: Value(SCREAMING_SNAKE_CASE_ ) for feature, dtype in features.items()} ) if features is not None else None ) _SCREAMING_SNAKE_CASE = ParquetDatasetReader(SCREAMING_SNAKE_CASE_ , features=SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_dataset(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @pytest.mark.parametrize("""split""" , [None, NamedSplit("""train""" ), """train""", """test"""] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} _SCREAMING_SNAKE_CASE = ParquetDatasetReader(SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ , split=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_dataset(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert dataset.split == split if split else "train" @pytest.mark.parametrize("""path_type""" , [str, list] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" if issubclass(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE = parquet_path elif issubclass(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE = [parquet_path] _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} _SCREAMING_SNAKE_CASE = ParquetDatasetReader(SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_dataset(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=("train",) ) -> List[str]: """simple docstring""" assert isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) for split in splits: _SCREAMING_SNAKE_CASE = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("""keep_in_memory""" , [False, True] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): _SCREAMING_SNAKE_CASE = ParquetDatasetReader( {"""train""": parquet_path} , cache_dir=SCREAMING_SNAKE_CASE_ , keep_in_memory=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_datasetdict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @pytest.mark.parametrize( """features""" , [ None, {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}, {"""col_1""": """string""", """col_2""": """string""", """col_3""": """string"""}, {"""col_1""": """int32""", """col_2""": """int32""", """col_3""": """int32"""}, {"""col_1""": """float32""", """col_2""": """float32""", """col_3""": """float32"""}, ] , ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} _SCREAMING_SNAKE_CASE = features.copy() if features else default_expected_features _SCREAMING_SNAKE_CASE = ( Features({feature: Value(SCREAMING_SNAKE_CASE_ ) for feature, dtype in features.items()} ) if features is not None else None ) _SCREAMING_SNAKE_CASE = ParquetDatasetReader({"""train""": parquet_path} , features=SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_datasetdict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @pytest.mark.parametrize("""split""" , [None, NamedSplit("""train""" ), """train""", """test"""] ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" if split: _SCREAMING_SNAKE_CASE = {split: parquet_path} else: _SCREAMING_SNAKE_CASE = """train""" _SCREAMING_SNAKE_CASE = {"""train""": parquet_path, """test""": parquet_path} _SCREAMING_SNAKE_CASE = tmp_path / """cache""" _SCREAMING_SNAKE_CASE = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} _SCREAMING_SNAKE_CASE = ParquetDatasetReader(SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ ).read() _check_parquet_datasetdict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE = ParquetDatasetWriter(SCREAMING_SNAKE_CASE_ , tmp_path / """foo.parquet""" ) assert writer.write() > 0 _SCREAMING_SNAKE_CASE = pq.ParquetFile(tmp_path / """foo.parquet""" ) _SCREAMING_SNAKE_CASE = pf.read() assert dataset.data.table == output_table def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = str(shared_datadir / """test_image_rgb.jpg""" ) _SCREAMING_SNAKE_CASE = {"""image""": [image_path]} _SCREAMING_SNAKE_CASE = Features({"""image""": Image()} ) _SCREAMING_SNAKE_CASE = Dataset.from_dict(SCREAMING_SNAKE_CASE_ , features=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = ParquetDatasetWriter(SCREAMING_SNAKE_CASE_ , tmp_path / """foo.parquet""" ) assert writer.write() > 0 _SCREAMING_SNAKE_CASE = Dataset.from_parquet(str(tmp_path / """foo.parquet""" ) ) assert dataset.features == reloaded_dataset.features _SCREAMING_SNAKE_CASE = ParquetDatasetReader(str(tmp_path / """foo.parquet""" ) , streaming=SCREAMING_SNAKE_CASE_ ).read() assert dataset.features == reloaded_iterable_dataset.features @pytest.mark.parametrize( """feature, expected""" , [ (Features({"""foo""": Value("""int32""" )} ), None), (Features({"""image""": Image(), """foo""": Value("""int32""" )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS), (Features({"""nested""": Sequence(Audio() )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS), ] , ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" assert get_writer_batch_size(SCREAMING_SNAKE_CASE_ ) == expected
0
0
'''simple docstring''' from __future__ import annotations import inspect import unittest from math import floor import numpy as np from transformers import CvtConfig 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 TFCvtForImageClassification, TFCvtModel from transformers.models.cvt.modeling_tf_cvt import TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class _a (__SCREAMING_SNAKE_CASE): """simple docstring""" def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(_a , """embed_dim""" ) ) self.parent.assertTrue(hasattr(_a , """num_heads""" ) ) class _a : """simple docstring""" def __init__( self , A__ , A__=13 , A__=64 , A__=3 , A__=[16, 48, 96] , A__=[1, 3, 6] , A__=[1, 2, 10] , A__=[7, 3, 3] , A__=[4, 2, 2] , A__=[2, 1, 1] , A__=[2, 2, 2] , A__=[False, False, True] , A__=[0.0, 0.0, 0.0] , A__=0.02 , A__=1E-12 , A__=True , A__=True , A__=2 , ) -> int: _SCREAMING_SNAKE_CASE = parent _SCREAMING_SNAKE_CASE = batch_size _SCREAMING_SNAKE_CASE = image_size _SCREAMING_SNAKE_CASE = patch_sizes _SCREAMING_SNAKE_CASE = patch_stride _SCREAMING_SNAKE_CASE = patch_padding _SCREAMING_SNAKE_CASE = is_training _SCREAMING_SNAKE_CASE = use_labels _SCREAMING_SNAKE_CASE = num_labels _SCREAMING_SNAKE_CASE = num_channels _SCREAMING_SNAKE_CASE = embed_dim _SCREAMING_SNAKE_CASE = num_heads _SCREAMING_SNAKE_CASE = stride_kv _SCREAMING_SNAKE_CASE = depth _SCREAMING_SNAKE_CASE = cls_token _SCREAMING_SNAKE_CASE = attention_drop_rate _SCREAMING_SNAKE_CASE = initializer_range _SCREAMING_SNAKE_CASE = layer_norm_eps def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _SCREAMING_SNAKE_CASE = None if self.use_labels: # create a random int32 tensor of given shape _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.num_labels ) _SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, labels def UpperCamelCase ( self ) -> Union[str, Any]: return CvtConfig( image_size=self.image_size , num_labels=self.num_labels , num_channels=self.num_channels , embed_dim=self.embed_dim , num_heads=self.num_heads , patch_sizes=self.patch_sizes , patch_padding=self.patch_padding , patch_stride=self.patch_stride , stride_kv=self.stride_kv , depth=self.depth , cls_token=self.cls_token , attention_drop_rate=self.attention_drop_rate , initializer_range=self.initializer_range , ) def UpperCamelCase ( self , A__ , A__ , A__ ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = TFCvtModel(config=_a ) _SCREAMING_SNAKE_CASE = model(_a , training=_a ) _SCREAMING_SNAKE_CASE = (self.image_size, self.image_size) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = image_size[0], image_size[1] for i in range(len(self.depth ) ): _SCREAMING_SNAKE_CASE = floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 ) _SCREAMING_SNAKE_CASE = floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.embed_dim[-1], height, width) ) def UpperCamelCase ( self , A__ , A__ , A__ ) -> str: _SCREAMING_SNAKE_CASE = self.num_labels _SCREAMING_SNAKE_CASE = TFCvtForImageClassification(_a ) _SCREAMING_SNAKE_CASE = model(_a , labels=_a , training=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = config_and_inputs _SCREAMING_SNAKE_CASE = {"""pixel_values""": pixel_values} return config, inputs_dict @require_tf class _a (__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = (TFCvtModel, TFCvtForImageClassification) if is_tf_available() else () SCREAMING_SNAKE_CASE = ( {'feature-extraction': TFCvtModel, 'image-classification': TFCvtForImageClassification} if is_tf_available() else {} ) SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = False def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = TFCvtModelTester(self ) _SCREAMING_SNAKE_CASE = TFCvtConfigTester(self , config_class=_a , has_text_modality=_a , hidden_size=37 ) def UpperCamelCase ( self ) -> List[str]: self.config_tester.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() @unittest.skip(reason="""Cvt does not output attentions""" ) def UpperCamelCase ( self ) -> List[Any]: pass @unittest.skip(reason="""Cvt does not use inputs_embeds""" ) def UpperCamelCase ( self ) -> Union[str, Any]: pass @unittest.skip(reason="""Cvt does not support input and output embeddings""" ) def UpperCamelCase ( self ) -> List[str]: pass @unittest.skipIf( not is_tf_available() or len(tf.config.list_physical_devices("""GPU""" ) ) == 0 , reason="""TF does not support backprop for grouped convolutions on CPU.""" , ) def UpperCamelCase ( self ) -> List[str]: super().test_dataset_conversion() @unittest.skipIf( not is_tf_available() or len(tf.config.list_physical_devices("""GPU""" ) ) == 0 , reason="""TF does not support backprop for grouped convolutions on CPU.""" , ) @slow def UpperCamelCase ( self ) -> List[str]: super().test_keras_fit() @unittest.skip(reason="""Get `Failed to determine best cudnn convolution algo.` error after using TF 2.12+cuda 11.8""" ) def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = tf.keras.mixed_precision.Policy("""mixed_float16""" ) tf.keras.mixed_precision.set_global_policy(_a ) super().test_keras_fit() tf.keras.mixed_precision.set_global_policy("""float32""" ) def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _SCREAMING_SNAKE_CASE = model_class(_a ) _SCREAMING_SNAKE_CASE = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] _SCREAMING_SNAKE_CASE = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , _a ) def UpperCamelCase ( self ) -> Any: def check_hidden_states_output(A__ , A__ , A__ ): _SCREAMING_SNAKE_CASE = model_class(_a ) _SCREAMING_SNAKE_CASE = model(**self._prepare_for_class(_a , _a ) ) _SCREAMING_SNAKE_CASE = outputs.hidden_states _SCREAMING_SNAKE_CASE = len(self.model_tester.depth ) self.assertEqual(len(_a ) , _a ) # verify the first hidden states (first block) self.assertListEqual( list(hidden_states[0].shape[-3:] ) , [ self.model_tester.embed_dim[0], self.model_tester.image_size // 4, self.model_tester.image_size // 4, ] , ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _SCREAMING_SNAKE_CASE = True check_hidden_states_output(_a , _a , _a ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _SCREAMING_SNAKE_CASE = True check_hidden_states_output(_a , _a , _a ) def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_a ) @slow def UpperCamelCase ( self ) -> List[str]: for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _SCREAMING_SNAKE_CASE = TFCvtModel.from_pretrained(_a ) self.assertIsNotNone(_a ) def lowerCAmelCase_ ( ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE = 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 ) -> Optional[int]: return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) @slow def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) _SCREAMING_SNAKE_CASE = self.default_image_processor _SCREAMING_SNAKE_CASE = prepare_img() _SCREAMING_SNAKE_CASE = image_processor(images=_a , return_tensors="""tf""" ) # forward pass _SCREAMING_SNAKE_CASE = model(**_a ) # verify the logits _SCREAMING_SNAKE_CASE = tf.TensorShape((1, 10_00) ) self.assertEqual(outputs.logits.shape , _a ) _SCREAMING_SNAKE_CASE = tf.constant([0.9285, 0.9015, -0.3150] ) self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() , _a , atol=1E-4 ) )
720
'''simple docstring''' def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): raise ValueError("""multiplicative_persistence() only accepts integral values""" ) if num < 0: raise ValueError("""multiplicative_persistence() does not accept negative values""" ) _SCREAMING_SNAKE_CASE = 0 _SCREAMING_SNAKE_CASE = str(SCREAMING_SNAKE_CASE_ ) while len(SCREAMING_SNAKE_CASE_ ) != 1: _SCREAMING_SNAKE_CASE = [int(SCREAMING_SNAKE_CASE_ ) for i in num_string] _SCREAMING_SNAKE_CASE = 1 for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) ): total *= numbers[i] _SCREAMING_SNAKE_CASE = str(SCREAMING_SNAKE_CASE_ ) steps += 1 return steps def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): raise ValueError("""additive_persistence() only accepts integral values""" ) if num < 0: raise ValueError("""additive_persistence() does not accept negative values""" ) _SCREAMING_SNAKE_CASE = 0 _SCREAMING_SNAKE_CASE = str(SCREAMING_SNAKE_CASE_ ) while len(SCREAMING_SNAKE_CASE_ ) != 1: _SCREAMING_SNAKE_CASE = [int(SCREAMING_SNAKE_CASE_ ) for i in num_string] _SCREAMING_SNAKE_CASE = 0 for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) ): total += numbers[i] _SCREAMING_SNAKE_CASE = str(SCREAMING_SNAKE_CASE_ ) steps += 1 return steps if __name__ == "__main__": import doctest doctest.testmod()
0
0
'''simple docstring''' def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" return price * (1 + tax_rate) if __name__ == "__main__": print(f"""{price_plus_tax(100, 0.25) = }""") print(f"""{price_plus_tax(125.50, 0.05) = }""")
721
'''simple docstring''' import math import os import re import sys import unittest from pathlib import Path from typing import Tuple from unittest.mock import patch from parameterized import parameterized from transformers.testing_utils import ( CaptureStderr, ExtendSysPath, TestCasePlus, execute_subprocess_async, get_gpu_count, get_torch_dist_unique_port, require_apex, require_bitsandbytes, require_fairscale, require_torch, require_torch_gpu, require_torch_multi_gpu, require_torch_non_multi_gpu, slow, ) from transformers.trainer_callback import TrainerState from transformers.trainer_utils import set_seed UpperCamelCase__ : Tuple = os.path.abspath(os.path.dirname(__file__)) with ExtendSysPath(f"""{bindir}/../../examples/pytorch/translation"""): from run_translation import main # noqa set_seed(42) UpperCamelCase__ : Union[str, Any] = "sshleifer/student_marian_en_ro_6_1" UpperCamelCase__ : str = "sshleifer/tiny-mbart" @require_torch class _a (_lowerCamelCase): """simple docstring""" def UpperCamelCase ( self , A__=False , A__=None , A__=True , A__=True , A__=True , A__=True , ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = self.run_trainer( eval_steps=1 , max_len=12 , model_name=A__ , num_train_epochs=1 , distributed=A__ , extra_args_str=A__ , predict_with_generate=A__ , do_train=A__ , do_eval=A__ , do_predict=A__ , ) _SCREAMING_SNAKE_CASE = TrainerState.load_from_json(os.path.join(A__ , """trainer_state.json""" ) ).log_history if not do_eval: return _SCREAMING_SNAKE_CASE = [log for log in logs if """eval_loss""" in log.keys()] _SCREAMING_SNAKE_CASE = eval_metrics[0] if predict_with_generate: assert "eval_bleu" in first_step_stats _SCREAMING_SNAKE_CASE = eval_metrics[-1] assert isinstance(last_step_stats["""eval_bleu"""] , A__ ) assert not math.isnan(float(last_step_stats["""eval_loss"""] ) ), "eval_loss must not be `nan`" @require_torch_non_multi_gpu def UpperCamelCase ( self ) -> Optional[int]: self.run_seqaseq_quick() @require_torch_multi_gpu def UpperCamelCase ( self ) -> Optional[Any]: self.run_seqaseq_quick(distributed=A__ ) @require_torch_multi_gpu def UpperCamelCase ( self ) -> Union[str, Any]: self.run_seqaseq_quick(distributed=A__ ) @unittest.skip("""Requires an update of the env running those tests""" ) @require_torch_multi_gpu @require_fairscale def UpperCamelCase ( self ) -> Any: self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--sharded_ddp simple""" ) @unittest.skip("""Requires an update of the env running those tests""" ) @require_torch_multi_gpu @require_fairscale def UpperCamelCase ( self ) -> Tuple: self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--sharded_ddp simple --fp16""" ) @unittest.skip("""Requires an update of the env running those tests""" ) @require_torch_multi_gpu @require_fairscale def UpperCamelCase ( self ) -> str: self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--sharded_ddp zero_dp_2""" , predict_with_generate=A__ ) @unittest.skip("""Requires an update of the env running those tests""" ) @require_torch_multi_gpu @require_fairscale def UpperCamelCase ( self ) -> List[str]: self.run_seqaseq_quick( distributed=A__ , extra_args_str="""--sharded_ddp zero_dp_2 --fp16""" , predict_with_generate=A__ ) @require_apex @require_torch_gpu def UpperCamelCase ( self ) -> Optional[Any]: # XXX: apex breaks the trainer if it's run twice e.g. run_seq2seq.main() from the same # program and it breaks other tests that run from the same pytest worker, therefore until this is # sorted out it must be run only in an external program, that is distributed=True in this # test and only under one or more gpus - if we want cpu will need to make a special test # # specifically to the problem traced it to self.optimizer.step() - if it's run 2nd time via # 2nd main() call it botches the future eval. # self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--fp16 --fp16_backend=apex""" ) # test 2nd time - was getting eval_loss': nan' # to reproduce the problem set distributed=False self.run_seqaseq_quick(distributed=A__ , extra_args_str="""--fp16 --fp16_backend=apex""" ) @parameterized.expand(["""base""", """low""", """high""", """mixed"""] ) @require_torch_multi_gpu def UpperCamelCase ( self , A__ ) -> List[Any]: # as each sub-test is slow-ish split into multiple sub-tests to avoid CI timeout _SCREAMING_SNAKE_CASE = { # test with the default log_level - should be info and thus log info once """base""": {"""extra_args_str""": """""", """n_matches""": 1}, # test with low log_level and log_level_replica - should be noisy on all processes # now the info string should appear twice on 2 processes """low""": {"""extra_args_str""": """--log_level debug --log_level_replica debug""", """n_matches""": 2}, # test with high log_level and low log_level_replica # now the info string should appear once only on the replica """high""": {"""extra_args_str""": """--log_level error --log_level_replica debug""", """n_matches""": 1}, # test with high log_level and log_level_replica - should be quiet on all processes """mixed""": {"""extra_args_str""": """--log_level error --log_level_replica error""", """n_matches""": 0}, } _SCREAMING_SNAKE_CASE = experiments[experiment_id] _SCREAMING_SNAKE_CASE = {"""distributed""": True, """predict_with_generate""": False, """do_eval""": False, """do_predict""": False} _SCREAMING_SNAKE_CASE = """Running training""" with CaptureStderr() as cl: self.run_seqaseq_quick(**A__ , extra_args_str=data["""extra_args_str"""] ) _SCREAMING_SNAKE_CASE = len(re.findall(A__ , cl.err ) ) self.assertEqual(A__ , data["""n_matches"""] ) @slow def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = self.run_trainer( eval_steps=2 , max_len=1_28 , model_name=A__ , learning_rate=3E-4 , num_train_epochs=10 , distributed=A__ , ) # Check metrics _SCREAMING_SNAKE_CASE = TrainerState.load_from_json(os.path.join(A__ , """trainer_state.json""" ) ).log_history _SCREAMING_SNAKE_CASE = [log for log in logs if """eval_loss""" in log.keys()] _SCREAMING_SNAKE_CASE = eval_metrics[0] _SCREAMING_SNAKE_CASE = eval_metrics[-1] assert first_step_stats["eval_loss"] > last_step_stats["eval_loss"], "model learned nothing" assert isinstance(last_step_stats["""eval_bleu"""] , A__ ) # test if do_predict saves generations and metrics _SCREAMING_SNAKE_CASE = os.listdir(A__ ) _SCREAMING_SNAKE_CASE = {os.path.basename(A__ ) for p in contents} assert "generated_predictions.txt" in contents assert "predict_results.json" in contents @slow @require_bitsandbytes def UpperCamelCase ( self ) -> Dict: from transformers.training_args import OptimizerNames def train_and_return_metrics(A__ ) -> Tuple[int, float]: _SCREAMING_SNAKE_CASE = """--skip_memory_metrics 0""" _SCREAMING_SNAKE_CASE = self.run_trainer( max_len=1_28 , model_name=A__ , learning_rate=3E-4 , num_train_epochs=1 , optim=A__ , distributed=A__ , extra_args_str=A__ , do_eval=A__ , do_predict=A__ , n_gpus_to_use=1 , ) # Check metrics _SCREAMING_SNAKE_CASE = TrainerState.load_from_json(Path(A__ , """trainer_state.json""" ) ).log_history _SCREAMING_SNAKE_CASE = int(logs[0]["""train_mem_gpu_peaked_delta"""] / 2**20 ) _SCREAMING_SNAKE_CASE = int(logs[0]["""train_mem_gpu_alloc_delta"""] / 2**20 ) _SCREAMING_SNAKE_CASE = logs[0]["""train_loss"""] return gpu_peak_mem_mb, gpu_alloc_mem_mb, loss _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = train_and_return_metrics(OptimizerNames.ADAMW_TORCH.value ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = train_and_return_metrics(OptimizerNames.ADAMW_BNB.value ) _SCREAMING_SNAKE_CASE = gpu_alloc_mem_orig - gpu_alloc_mem_bnb _SCREAMING_SNAKE_CASE = gpu_peak_mem_orig + gpu_alloc_mem_orig _SCREAMING_SNAKE_CASE = gpu_peak_mem_bnb + gpu_alloc_mem_bnb _SCREAMING_SNAKE_CASE = gpu_total_mem_orig - gpu_total_mem_bnb # sshleifer/student_marian_en_ro_6_1 has 54M parameter, 29M of which is `nn.Embedding` which # doesn't get quantized and remains in fp32. Therefore we only have 25M parameters quantized # in 2 bytes and the diff in optim memory usage is derived as so: # # - normal 25*8=~200MB (8 bytes per param) # - bnb 25*2= ~50MB (2 bytes per param) # # Thus we should expect ~150MB total memory saved. # # Peak memory should be the same - the total should be different by about that same margin # # After leaving a small margin to accommodate for differences between gpus let's check # that we have at least 120MB in savings _SCREAMING_SNAKE_CASE = 1_20 # uncomment the following if this test starts failing - requires py38 for a new print feature # gpu_peak_mem_diff = gpu_peak_mem_orig - gpu_peak_mem_bnb # print(f"{gpu_alloc_mem_orig=}MB {gpu_peak_mem_orig=}MB {gpu_alloc_mem_orig+gpu_peak_mem_orig=}MB") # print(f" {gpu_alloc_mem_bnb=}MB {gpu_peak_mem_bnb=}MB {gpu_alloc_mem_bnb+gpu_peak_mem_bnb=}MB") # print(f"{gpu_alloc_mem_diff=}MB") # print(f"{gpu_peak_mem_diff=}MB") # print(f"{gpu_total_mem_orig=}MB, {gpu_total_mem_bnb=}MB") # print(f"{gpu_total_mem_diff=}MB, {gpu_total_mem_diff=}MB") self.assertGreater( A__ , A__ , """should use ~150MB less alloc gpu memory with BNB, compared to without it for this model but got""" F" a difference of {gpu_alloc_mem_diff}MB, with gpu_alloc_mem_orig={gpu_alloc_mem_orig}MB and" F" gpu_alloc_mem_bnb={gpu_alloc_mem_bnb}MB" , ) self.assertGreater( A__ , A__ , """should use ~150MB less total gpu memory with BNB, compared to without it for this model but got""" F" a difference of {gpu_total_mem_diff}MB, with gpu_total_mem_orig={gpu_total_mem_orig}MB and" F" gpu_total_mem_bnb={gpu_total_mem_bnb}MB" , ) self.assertEqual( A__ , A__ , F"loss should be the same, but got loss_orig={loss_orig}, loss_bnb={loss_bnb}" ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ = 3E-3 , A__ = "adafactor" , A__ = False , A__ = None , A__ = 0 , A__ = True , A__ = True , A__ = True , A__ = True , A__ = None , ) -> Dict: _SCREAMING_SNAKE_CASE = self.test_file_dir / """../fixtures/tests_samples/wmt_en_ro""" _SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() _SCREAMING_SNAKE_CASE = F"\n --model_name_or_path {model_name}\n --train_file {data_dir}/train.json\n --validation_file {data_dir}/val.json\n --test_file {data_dir}/test.json\n --output_dir {output_dir}\n --overwrite_output_dir\n --max_train_samples 8\n --max_source_length {max_len}\n --max_target_length {max_len}\n --do_train\n --num_train_epochs {str(A__ )}\n --per_device_train_batch_size 4\n --learning_rate {learning_rate}\n --warmup_steps 8\n --logging_steps 0\n --logging_strategy no\n --save_steps {str(A__ )}\n --group_by_length\n --label_smoothing_factor 0.1\n --target_lang ro_RO\n --source_lang en_XX\n ".split() _SCREAMING_SNAKE_CASE = F"\n --do_eval\n --per_device_eval_batch_size 4\n --max_eval_samples 8\n --val_max_target_length {max_len}\n --evaluation_strategy steps\n --eval_steps {str(A__ )}\n ".split() _SCREAMING_SNAKE_CASE = """ --do_predict """.split() _SCREAMING_SNAKE_CASE = [] if do_train: args += args_train if do_eval: args += args_eval if do_predict: args += args_predict if predict_with_generate: args += "--predict_with_generate".split() if do_train: if optim == "adafactor": args += "--adafactor".split() else: args += F"--optim {optim}".split() if extra_args_str is not None: args += extra_args_str.split() if distributed: if n_gpus_to_use is None: _SCREAMING_SNAKE_CASE = get_gpu_count() _SCREAMING_SNAKE_CASE = get_torch_dist_unique_port() _SCREAMING_SNAKE_CASE = F"\n -m torch.distributed.run\n --nproc_per_node={n_gpus_to_use}\n --master_port={master_port}\n {self.examples_dir_str}/pytorch/translation/run_translation.py\n ".split() _SCREAMING_SNAKE_CASE = [sys.executable] + distributed_args + args # keep for quick debug # print(" ".join([f"\nPYTHONPATH={self.src_dir_str}"] +cmd)); die execute_subprocess_async(A__ , env=self.get_env() ) else: _SCREAMING_SNAKE_CASE = ["""run_translation.py"""] + args with patch.object(A__ , """argv""" , A__ ): main() return output_dir
0
0
'''simple docstring''' import importlib import sys from argparse import REMAINDER, ArgumentParser from pathlib import Path import torch_xla.distributed.xla_multiprocessing as xmp def lowerCAmelCase_ ( ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = 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=SCREAMING_SNAKE_CASE_ , default=1 , help="""Number of TPU cores to use (1 or 8).""" ) # positional parser.add_argument( """training_script""" , type=SCREAMING_SNAKE_CASE_ , 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=SCREAMING_SNAKE_CASE_ ) return parser.parse_args() def lowerCAmelCase_ ( ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE = parse_args() # Import training_script as a module. _SCREAMING_SNAKE_CASE = Path(args.training_script ) sys.path.append(str(script_fpath.parent.resolve() ) ) _SCREAMING_SNAKE_CASE = script_fpath.stem _SCREAMING_SNAKE_CASE = importlib.import_module(SCREAMING_SNAKE_CASE_ ) # Patch sys.argv _SCREAMING_SNAKE_CASE = [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()
700
'''simple docstring''' import sys UpperCamelCase__ : int = ( "73167176531330624919225119674426574742355349194934" "96983520312774506326239578318016984801869478851843" "85861560789112949495459501737958331952853208805511" "12540698747158523863050715693290963295227443043557" "66896648950445244523161731856403098711121722383113" "62229893423380308135336276614282806444486645238749" "30358907296290491560440772390713810515859307960866" "70172427121883998797908792274921901699720888093776" "65727333001053367881220235421809751254540594752243" "52584907711670556013604839586446706324415722155397" "53697817977846174064955149290862569321978468622482" "83972241375657056057490261407972968652414535100474" "82166370484403199890008895243450658541227588666881" "16427171479924442928230863465674813919123162824586" "17866458359124566529476545682848912883142607690042" "24219022671055626321111109370544217506941658960408" "07198403850962455444362981230987879927244284909188" "84580156166097919133875499200524063689912560717606" "05886116467109405077541002256983155200055935729725" "71636269561882670428252483600823257530420752963450" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ = N ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE = -sys.maxsize - 1 for i in range(len(SCREAMING_SNAKE_CASE_ ) - 12 ): _SCREAMING_SNAKE_CASE = 1 for j in range(13 ): product *= int(n[i + j] ) if product > largest_product: _SCREAMING_SNAKE_CASE = product return largest_product if __name__ == "__main__": print(f"""{solution() = }""")
0
0
'''simple docstring''' from __future__ import annotations import math def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> float: """simple docstring""" _SCREAMING_SNAKE_CASE = u for i in range(1 , SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE = temp * (u - i) return temp def lowerCAmelCase_ ( ) -> None: """simple docstring""" _SCREAMING_SNAKE_CASE = int(input("""enter the numbers of values: """ ) ) _SCREAMING_SNAKE_CASE = [] for _ in range(SCREAMING_SNAKE_CASE_ ): y.append([] ) for i in range(SCREAMING_SNAKE_CASE_ ): for j in range(SCREAMING_SNAKE_CASE_ ): y[i].append(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = 0 print("""enter the values of parameters in a list: """ ) _SCREAMING_SNAKE_CASE = list(map(SCREAMING_SNAKE_CASE_ , input().split() ) ) print("""enter the values of corresponding parameters: """ ) for i in range(SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE = float(input() ) _SCREAMING_SNAKE_CASE = int(input("""enter the value to interpolate: """ ) ) _SCREAMING_SNAKE_CASE = (value - x[0]) / (x[1] - x[0]) # for calculating forward difference table for i in range(1 , SCREAMING_SNAKE_CASE_ ): for j in range(n - i ): _SCREAMING_SNAKE_CASE = y[j + 1][i - 1] - y[j][i - 1] _SCREAMING_SNAKE_CASE = y[0][0] for i in range(1 , SCREAMING_SNAKE_CASE_ ): summ += (ucal(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) * y[0][i]) / math.factorial(SCREAMING_SNAKE_CASE_ ) print(F"the value at {value} is {summ}" ) if __name__ == "__main__": main()
701
'''simple docstring''' UpperCamelCase__ : Dict = { "a": "AAAAA", "b": "AAAAB", "c": "AAABA", "d": "AAABB", "e": "AABAA", "f": "AABAB", "g": "AABBA", "h": "AABBB", "i": "ABAAA", "j": "BBBAA", "k": "ABAAB", "l": "ABABA", "m": "ABABB", "n": "ABBAA", "o": "ABBAB", "p": "ABBBA", "q": "ABBBB", "r": "BAAAA", "s": "BAAAB", "t": "BAABA", "u": "BAABB", "v": "BBBAB", "w": "BABAA", "x": "BABAB", "y": "BABBA", "z": "BABBB", " ": " ", } UpperCamelCase__ : str = {value: key for key, value in encode_dict.items()} def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE = """""" for letter in word.lower(): if letter.isalpha() or letter == " ": encoded += encode_dict[letter] else: raise Exception("""encode() accepts only letters of the alphabet and spaces""" ) return encoded def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" if set(SCREAMING_SNAKE_CASE_ ) - {"A", "B", " "} != set(): raise Exception("""decode() accepts only 'A', 'B' and spaces""" ) _SCREAMING_SNAKE_CASE = """""" for word in coded.split(): while len(SCREAMING_SNAKE_CASE_ ) != 0: decoded += decode_dict[word[:5]] _SCREAMING_SNAKE_CASE = word[5:] decoded += " " return decoded.strip() if __name__ == "__main__": from doctest import testmod testmod()
0
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available UpperCamelCase__ : Any = { "configuration_roc_bert": ["ROC_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "RoCBertConfig"], "tokenization_roc_bert": ["RoCBertTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: pass try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Dict = [ "ROC_BERT_PRETRAINED_MODEL_ARCHIVE_LIST", "RoCBertForCausalLM", "RoCBertForMaskedLM", "RoCBertForMultipleChoice", "RoCBertForPreTraining", "RoCBertForQuestionAnswering", "RoCBertForSequenceClassification", "RoCBertForTokenClassification", "RoCBertLayer", "RoCBertModel", "RoCBertPreTrainedModel", "load_tf_weights_in_roc_bert", ] if TYPE_CHECKING: from .configuration_roc_bert import ROC_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RoCBertConfig from .tokenization_roc_bert import RoCBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: raise OptionalDependencyNotAvailable() try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roc_bert import ( ROC_BERT_PRETRAINED_MODEL_ARCHIVE_LIST, RoCBertForCausalLM, RoCBertForMaskedLM, RoCBertForMultipleChoice, RoCBertForPreTraining, RoCBertForQuestionAnswering, RoCBertForSequenceClassification, RoCBertForTokenClassification, RoCBertLayer, RoCBertModel, RoCBertPreTrainedModel, load_tf_weights_in_roc_bert, ) else: import sys UpperCamelCase__ : List[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
702
'''simple docstring''' import argparse import torch from torch import nn from transformers import MaMaaaConfig, MaMaaaForConditionalGeneration def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE = [ """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 lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = emb.weight.shape _SCREAMING_SNAKE_CASE = nn.Linear(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , bias=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = emb.weight.data return lin_layer def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = torch.load(SCREAMING_SNAKE_CASE_ , map_location="""cpu""" ) _SCREAMING_SNAKE_CASE = mam_aaa["""args"""] or mam_aaa["""cfg"""]["""model"""] _SCREAMING_SNAKE_CASE = mam_aaa["""model"""] remove_ignore_keys_(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = state_dict["""encoder.embed_tokens.weight"""].shape[0] _SCREAMING_SNAKE_CASE = MaMaaaConfig( vocab_size=SCREAMING_SNAKE_CASE_ , max_position_embeddings=10_24 , encoder_layers=args.encoder_layers , decoder_layers=args.decoder_layers , encoder_attention_heads=args.encoder_attention_heads , decoder_attention_heads=args.decoder_attention_heads , encoder_ffn_dim=args.encoder_ffn_embed_dim , decoder_ffn_dim=args.decoder_ffn_embed_dim , d_model=args.encoder_embed_dim , encoder_layerdrop=args.encoder_layerdrop , decoder_layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function="""relu""" , ) _SCREAMING_SNAKE_CASE = state_dict["""decoder.embed_tokens.weight"""] _SCREAMING_SNAKE_CASE = MaMaaaForConditionalGeneration(SCREAMING_SNAKE_CASE_ ) model.model.load_state_dict(SCREAMING_SNAKE_CASE_ , strict=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = make_linear_from_emb(model.model.shared ) return model if __name__ == "__main__": UpperCamelCase__ : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument("fairseq_path", type=str, help="path to a model.pt on local filesystem.") parser.add_argument("pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") UpperCamelCase__ : List[str] = parser.parse_args() UpperCamelCase__ : Any = convert_fairseq_mamaaa_checkpoint_from_disk(args.fairseq_pathß) model.save_pretrained(args.pytorch_dump_folder_path)
0
0
'''simple docstring''' from __future__ import annotations import numpy as np def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" return np.maximum(0 , __lowercase ) if __name__ == "__main__": print(np.array(relu([-1, 0, 5]))) # --> [0, 0, 5]
703
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available UpperCamelCase__ : str = { "configuration_canine": ["CANINE_PRETRAINED_CONFIG_ARCHIVE_MAP", "CanineConfig"], "tokenization_canine": ["CanineTokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : List[Any] = [ "CANINE_PRETRAINED_MODEL_ARCHIVE_LIST", "CanineForMultipleChoice", "CanineForQuestionAnswering", "CanineForSequenceClassification", "CanineForTokenClassification", "CanineLayer", "CanineModel", "CaninePreTrainedModel", "load_tf_weights_in_canine", ] if TYPE_CHECKING: from .configuration_canine import CANINE_PRETRAINED_CONFIG_ARCHIVE_MAP, CanineConfig from .tokenization_canine import CanineTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_canine import ( CANINE_PRETRAINED_MODEL_ARCHIVE_LIST, CanineForMultipleChoice, CanineForQuestionAnswering, CanineForSequenceClassification, CanineForTokenClassification, CanineLayer, CanineModel, CaninePreTrainedModel, load_tf_weights_in_canine, ) else: import sys UpperCamelCase__ : str = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
0
0
import argparse import os import re UpperCamelCase__ : Tuple = "src/diffusers" # Pattern that looks at the indentation in a line. UpperCamelCase__ : Optional[int] = re.compile(R"^(\s*)\S") # Pattern that matches `"key":" and puts `key` in group 0. UpperCamelCase__ : Union[str, Any] = re.compile(R"^\s*\"([^\"]+)\":") # Pattern that matches `_import_structure["key"]` and puts `key` in group 0. UpperCamelCase__ : str = re.compile(R"^\s*_import_structure\[\"([^\"]+)\"\]") # Pattern that matches `"key",` and puts `key` in group 0. UpperCamelCase__ : List[Any] = re.compile(R"^\s*\"([^\"]+)\",\s*$") # Pattern that matches any `[stuff]` and puts `stuff` in group 0. UpperCamelCase__ : Dict = re.compile(R"\[([^\]]+)\]") def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = _re_indent.search(__snake_case ) return "" if search is None else search.groups()[0] def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_="" , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_=None ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE = 0 _SCREAMING_SNAKE_CASE = code.split("""\n""" ) if start_prompt is not None: while not lines[index].startswith(__snake_case ): index += 1 _SCREAMING_SNAKE_CASE = ["""\n""".join(lines[:index] )] else: _SCREAMING_SNAKE_CASE = [] # We split into blocks until we get to the `end_prompt` (or the end of the block). _SCREAMING_SNAKE_CASE = [lines[index]] index += 1 while index < len(__snake_case ) and (end_prompt is None or not lines[index].startswith(__snake_case )): if len(lines[index] ) > 0 and get_indent(lines[index] ) == indent_level: if len(__snake_case ) > 0 and get_indent(current_block[-1] ).startswith(indent_level + """ """ ): current_block.append(lines[index] ) blocks.append("""\n""".join(__snake_case ) ) if index < len(__snake_case ) - 1: _SCREAMING_SNAKE_CASE = [lines[index + 1]] index += 1 else: _SCREAMING_SNAKE_CASE = [] else: blocks.append("""\n""".join(__snake_case ) ) _SCREAMING_SNAKE_CASE = [lines[index]] else: current_block.append(lines[index] ) index += 1 # Adds current block if it's nonempty. if len(__snake_case ) > 0: blocks.append("""\n""".join(__snake_case ) ) # Add final block after end_prompt if provided. if end_prompt is not None and index < len(__snake_case ): blocks.append("""\n""".join(lines[index:] ) ) return blocks def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> List[str]: """simple docstring""" def _inner(SCREAMING_SNAKE_CASE_ ): return key(__snake_case ).lower().replace("""_""" , """""" ) return _inner def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None ) -> Tuple: """simple docstring""" # If no key is provided, we use a noop. def noop(SCREAMING_SNAKE_CASE_ ): return x if key is None: _SCREAMING_SNAKE_CASE = noop # Constants are all uppercase, they go first. _SCREAMING_SNAKE_CASE = [obj for obj in objects if key(__snake_case ).isupper()] # Classes are not all uppercase but start with a capital, they go second. _SCREAMING_SNAKE_CASE = [obj for obj in objects if key(__snake_case )[0].isupper() and not key(__snake_case ).isupper()] # Functions begin with a lowercase, they go last. _SCREAMING_SNAKE_CASE = [obj for obj in objects if not key(__snake_case )[0].isupper()] _SCREAMING_SNAKE_CASE = ignore_underscore(__snake_case ) return sorted(__snake_case , key=__snake_case ) + sorted(__snake_case , key=__snake_case ) + sorted(__snake_case , key=__snake_case ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" # This inner function sort imports between [ ]. def _replace(SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE = match.groups()[0] if "," not in imports: return F"[{imports}]" _SCREAMING_SNAKE_CASE = [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: _SCREAMING_SNAKE_CASE = keys[:-1] return "[" + ", ".join([F"\"{k}\"" for k in sort_objects(__snake_case )] ) + "]" _SCREAMING_SNAKE_CASE = import_statement.split("""\n""" ) if len(__snake_case ) > 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. _SCREAMING_SNAKE_CASE = 2 if lines[1].strip() == """[""" else 1 _SCREAMING_SNAKE_CASE = [(i, _re_strip_line.search(__snake_case ).groups()[0]) for i, line in enumerate(lines[idx:-idx] )] _SCREAMING_SNAKE_CASE = sort_objects(__snake_case , key=lambda SCREAMING_SNAKE_CASE_ : x[1] ) _SCREAMING_SNAKE_CASE = [lines[x[0] + idx] for x in sorted_indices] return "\n".join(lines[:idx] + sorted_lines + lines[-idx:] ) elif len(__snake_case ) == 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: _SCREAMING_SNAKE_CASE = _re_bracket_content.sub(_replace , lines[1] ) else: _SCREAMING_SNAKE_CASE = [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: _SCREAMING_SNAKE_CASE = keys[:-1] _SCREAMING_SNAKE_CASE = get_indent(lines[1] ) + """, """.join([F"\"{k}\"" for k in sort_objects(__snake_case )] ) return "\n".join(__snake_case ) else: # Finally we have to deal with imports fitting on one line _SCREAMING_SNAKE_CASE = _re_bracket_content.sub(_replace , __snake_case ) return import_statement def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=True ) -> int: """simple docstring""" with open(__snake_case , """r""" ) as f: _SCREAMING_SNAKE_CASE = f.read() if "_import_structure" not in code: return # Blocks of indent level 0 _SCREAMING_SNAKE_CASE = split_code_in_indented_blocks( __snake_case , 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(__snake_case ) - 1 ): # Check if the block contains some `_import_structure`s thingy to sort. _SCREAMING_SNAKE_CASE = main_blocks[block_idx] _SCREAMING_SNAKE_CASE = block.split("""\n""" ) # Get to the start of the imports. _SCREAMING_SNAKE_CASE = 0 while line_idx < len(__snake_case ) and "_import_structure" not in block_lines[line_idx]: # Skip dummy import blocks if "import dummy" in block_lines[line_idx]: _SCREAMING_SNAKE_CASE = len(__snake_case ) else: line_idx += 1 if line_idx >= len(__snake_case ): continue # Ignore beginning and last line: they don't contain anything. _SCREAMING_SNAKE_CASE = """\n""".join(block_lines[line_idx:-1] ) _SCREAMING_SNAKE_CASE = get_indent(block_lines[1] ) # Slit the internal block into blocks of indent level 1. _SCREAMING_SNAKE_CASE = split_code_in_indented_blocks(__snake_case , indent_level=__snake_case ) # We have two categories of import key: list or _import_structure[key].append/extend _SCREAMING_SNAKE_CASE = _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. _SCREAMING_SNAKE_CASE = [(pattern.search(__snake_case ).groups()[0] if pattern.search(__snake_case ) is not None else None) for b in internal_blocks] # We only sort the lines with a key. _SCREAMING_SNAKE_CASE = [(i, key) for i, key in enumerate(__snake_case ) if key is not None] _SCREAMING_SNAKE_CASE = [x[0] for x in sorted(__snake_case , key=lambda SCREAMING_SNAKE_CASE_ : x[1] )] # We reorder the blocks by leaving empty lines/comments as they were and reorder the rest. _SCREAMING_SNAKE_CASE = 0 _SCREAMING_SNAKE_CASE = [] for i in range(len(__snake_case ) ): if keys[i] is None: reordered_blocks.append(internal_blocks[i] ) else: _SCREAMING_SNAKE_CASE = sort_objects_in_import(internal_blocks[sorted_indices[count]] ) reordered_blocks.append(__snake_case ) count += 1 # And we put our main block back together with its first and last line. _SCREAMING_SNAKE_CASE = """\n""".join(block_lines[:line_idx] + reordered_blocks + [block_lines[-1]] ) if code != "\n".join(__snake_case ): if check_only: return True else: print(F"Overwriting {file}." ) with open(__snake_case , """w""" ) as f: f.write("""\n""".join(__snake_case ) ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_=True ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = [] for root, _, files in os.walk(__snake_case ): if "__init__.py" in files: _SCREAMING_SNAKE_CASE = sort_imports(os.path.join(__snake_case , """__init__.py""" ) , check_only=__snake_case ) if result: _SCREAMING_SNAKE_CASE = [os.path.join(__snake_case , """__init__.py""" )] if len(__snake_case ) > 0: raise ValueError(F"Would overwrite {len(__snake_case )} files, run `make style`." ) if __name__ == "__main__": UpperCamelCase__ : List[Any] = argparse.ArgumentParser() parser.add_argument("--check_only", action="store_true", help="Whether to only check or fix style.") UpperCamelCase__ : Any = parser.parse_args() sort_imports_in_all_inits(check_only=args.check_only)
704
'''simple docstring''' import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class _a (_lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = ['image_processor', 'tokenizer'] SCREAMING_SNAKE_CASE = 'ChineseCLIPImageProcessor' SCREAMING_SNAKE_CASE = ('BertTokenizer', 'BertTokenizerFast') def __init__( self , A__=None , A__=None , **A__ ) -> int: _SCREAMING_SNAKE_CASE = None if "feature_extractor" in kwargs: warnings.warn( """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`""" """ instead.""" , A__ , ) _SCREAMING_SNAKE_CASE = kwargs.pop("""feature_extractor""" ) _SCREAMING_SNAKE_CASE = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("""You need to specify an `image_processor`.""" ) if tokenizer is None: raise ValueError("""You need to specify a `tokenizer`.""" ) super().__init__(A__ , A__ ) _SCREAMING_SNAKE_CASE = self.image_processor def __call__( self , A__=None , A__=None , A__=None , **A__ ) -> Optional[int]: if text is None and images is None: raise ValueError("""You have to specify either text or images. Both cannot be none.""" ) if text is not None: _SCREAMING_SNAKE_CASE = self.tokenizer(A__ , return_tensors=A__ , **A__ ) if images is not None: _SCREAMING_SNAKE_CASE = self.image_processor(A__ , return_tensors=A__ , **A__ ) if text is not None and images is not None: _SCREAMING_SNAKE_CASE = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**A__ ) , tensor_type=A__ ) def UpperCamelCase ( self , *A__ , **A__ ) -> Dict: return self.tokenizer.batch_decode(*A__ , **A__ ) def UpperCamelCase ( self , *A__ , **A__ ) -> Optional[Any]: return self.tokenizer.decode(*A__ , **A__ ) @property def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.tokenizer.model_input_names _SCREAMING_SNAKE_CASE = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def UpperCamelCase ( self ) -> Optional[int]: warnings.warn( """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.""" , A__ , ) return self.image_processor_class
0
0
from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class _a : """simple docstring""" def __init__( self , A__ , A__=12 , A__=7 , A__=True , A__=True , A__=True , A__=99 , A__=32 , A__=32 , A__=2 , A__=4 , A__=37 , A__=0.1 , A__=0.1 , A__=5_12 , A__=0.02 , A__=0 , A__=None , ) -> List[str]: _SCREAMING_SNAKE_CASE = parent _SCREAMING_SNAKE_CASE = batch_size _SCREAMING_SNAKE_CASE = seq_length _SCREAMING_SNAKE_CASE = is_training _SCREAMING_SNAKE_CASE = use_input_mask _SCREAMING_SNAKE_CASE = use_labels _SCREAMING_SNAKE_CASE = vocab_size _SCREAMING_SNAKE_CASE = hidden_size _SCREAMING_SNAKE_CASE = projection_dim _SCREAMING_SNAKE_CASE = num_hidden_layers _SCREAMING_SNAKE_CASE = num_attention_heads _SCREAMING_SNAKE_CASE = intermediate_size _SCREAMING_SNAKE_CASE = dropout _SCREAMING_SNAKE_CASE = attention_dropout _SCREAMING_SNAKE_CASE = max_position_embeddings _SCREAMING_SNAKE_CASE = initializer_range _SCREAMING_SNAKE_CASE = scope _SCREAMING_SNAKE_CASE = bos_token_id def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _SCREAMING_SNAKE_CASE = None if self.use_input_mask: _SCREAMING_SNAKE_CASE = random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: _SCREAMING_SNAKE_CASE = input_mask.numpy() _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = input_mask.shape _SCREAMING_SNAKE_CASE = np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(snake_case__ ): _SCREAMING_SNAKE_CASE = 1 _SCREAMING_SNAKE_CASE = 0 _SCREAMING_SNAKE_CASE = self.get_config() return config, input_ids, tf.convert_to_tensor(snake_case__ ) def UpperCamelCase ( self ) -> Optional[Any]: return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def UpperCamelCase ( self , A__ , A__ , A__ ) -> Dict: _SCREAMING_SNAKE_CASE = TFBlipTextModel(config=snake_case__ ) _SCREAMING_SNAKE_CASE = model(snake_case__ , attention_mask=snake_case__ , training=snake_case__ ) _SCREAMING_SNAKE_CASE = model(snake_case__ , training=snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = config_and_inputs _SCREAMING_SNAKE_CASE = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_tf class _a (__a , unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = (TFBlipTextModel,) if is_tf_available() else () SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = False def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = BlipTextModelTester(self ) _SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=snake_case__ , hidden_size=37 ) def UpperCamelCase ( self ) -> int: self.config_tester.run_common_tests() def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def UpperCamelCase ( self ) -> Tuple: pass def UpperCamelCase ( self ) -> int: pass @unittest.skip(reason="""Blip does not use inputs_embeds""" ) def UpperCamelCase ( self ) -> int: pass @unittest.skip(reason="""BlipTextModel has no base class and is not available in MODEL_MAPPING""" ) def UpperCamelCase ( self ) -> int: pass @unittest.skip(reason="""BlipTextModel has no base class and is not available in MODEL_MAPPING""" ) def UpperCamelCase ( self ) -> Union[str, Any]: pass @slow def UpperCamelCase ( self ) -> Optional[int]: for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _SCREAMING_SNAKE_CASE = TFBlipTextModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) def UpperCamelCase ( self , A__=True ) -> Optional[Any]: super().test_pt_tf_model_equivalence(allow_missing_keys=snake_case__ )
705
'''simple docstring''' from sklearn.metrics import matthews_corrcoef import datasets UpperCamelCase__ : List[str] = "\nCompute the Matthews correlation coefficient (MCC)\n\nThe Matthews correlation coefficient is used in machine learning as a\nmeasure of the quality of binary and multiclass classifications. It takes\ninto account true and false positives and negatives and is generally\nregarded as a balanced measure which can be used even if the classes are of\nvery different sizes. The MCC is in essence a correlation coefficient value\nbetween -1 and +1. A coefficient of +1 represents a perfect prediction, 0\nan average random prediction and -1 an inverse prediction. The statistic\nis also known as the phi coefficient. [source: Wikipedia]\n" UpperCamelCase__ : List[Any] = "\nArgs:\n predictions (list of int): Predicted labels, as returned by a model.\n references (list of int): Ground truth labels.\n sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`.\nReturns:\n matthews_correlation (dict containing float): Matthews correlation.\nExamples:\n Example 1, a basic example with only predictions and references as inputs:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3])\n >>> print(round(results['matthews_correlation'], 2))\n 0.54\n\n Example 2, the same example as above, but also including sample weights:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 3, 1, 1, 1, 2])\n >>> print(round(results['matthews_correlation'], 2))\n 0.1\n\n Example 3, the same example as above, but with sample weights that cause a negative correlation:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 1, 0, 0, 0, 1])\n >>> print(round(results['matthews_correlation'], 2))\n -0.25\n" UpperCamelCase__ : Any = "\\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class _a (datasets.Metric): """simple docstring""" def UpperCamelCase ( self ) -> Optional[Any]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=[ """https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html""" ] , ) def UpperCamelCase ( self , A__ , A__ , A__=None ) -> List[str]: return { "matthews_correlation": float(matthews_corrcoef(A__ , A__ , sample_weight=A__ ) ), }
0
0
from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase__ : Optional[Any] = logging.get_logger(__name__) UpperCamelCase__ : Union[str, Any] = { "unc-nlp/lxmert-base-uncased": "https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/config.json", } class _a (__lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = '''lxmert''' SCREAMING_SNAKE_CASE = {} def __init__( self , A__=3_05_22 , A__=7_68 , A__=12 , A__=95_00 , A__=16_00 , A__=4_00 , A__=30_72 , A__="gelu" , A__=0.1 , A__=0.1 , A__=5_12 , A__=2 , A__=0.02 , A__=1E-12 , A__=9 , A__=5 , A__=5 , A__=20_48 , A__=4 , A__=6.67 , A__=True , A__=True , A__=True , A__=True , A__=True , A__=True , A__=True , **A__ , ) -> List[Any]: _SCREAMING_SNAKE_CASE = vocab_size _SCREAMING_SNAKE_CASE = hidden_size _SCREAMING_SNAKE_CASE = num_attention_heads _SCREAMING_SNAKE_CASE = hidden_act _SCREAMING_SNAKE_CASE = intermediate_size _SCREAMING_SNAKE_CASE = hidden_dropout_prob _SCREAMING_SNAKE_CASE = attention_probs_dropout_prob _SCREAMING_SNAKE_CASE = max_position_embeddings _SCREAMING_SNAKE_CASE = type_vocab_size _SCREAMING_SNAKE_CASE = initializer_range _SCREAMING_SNAKE_CASE = layer_norm_eps _SCREAMING_SNAKE_CASE = num_qa_labels _SCREAMING_SNAKE_CASE = num_object_labels _SCREAMING_SNAKE_CASE = num_attr_labels _SCREAMING_SNAKE_CASE = l_layers _SCREAMING_SNAKE_CASE = x_layers _SCREAMING_SNAKE_CASE = r_layers _SCREAMING_SNAKE_CASE = visual_feat_dim _SCREAMING_SNAKE_CASE = visual_pos_dim _SCREAMING_SNAKE_CASE = visual_loss_normalizer _SCREAMING_SNAKE_CASE = task_matched _SCREAMING_SNAKE_CASE = task_mask_lm _SCREAMING_SNAKE_CASE = task_obj_predict _SCREAMING_SNAKE_CASE = task_qa _SCREAMING_SNAKE_CASE = visual_obj_loss _SCREAMING_SNAKE_CASE = visual_attr_loss _SCREAMING_SNAKE_CASE = visual_feat_loss _SCREAMING_SNAKE_CASE = {"vision": r_layers, "cross_encoder": x_layers, "language": l_layers} super().__init__(**a_ )
706
'''simple docstring''' from __future__ import annotations def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Tuple: """simple docstring""" print(F"Vertex\tShortest Distance from vertex {src}" ) for i, d in enumerate(SCREAMING_SNAKE_CASE_ ): print(F"{i}\t\t{d}" ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" for j in range(SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = (graph[j][k] for k in ["""src""", """dst""", """weight"""]) if distance[u] != float("""inf""" ) and distance[u] + w < distance[v]: return True return False def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> list[float]: """simple docstring""" _SCREAMING_SNAKE_CASE = [float("""inf""" )] * vertex_count _SCREAMING_SNAKE_CASE = 0.0 for _ in range(vertex_count - 1 ): for j in range(SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = (graph[j][k] for k in ["""src""", """dst""", """weight"""]) if distance[u] != float("""inf""" ) and distance[u] + w < distance[v]: _SCREAMING_SNAKE_CASE = distance[u] + w _SCREAMING_SNAKE_CASE = check_negative_cycle(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if negative_cycle_exists: raise Exception("""Negative cycle found""" ) return distance if __name__ == "__main__": import doctest doctest.testmod() UpperCamelCase__ : int = int(input("Enter number of vertices: ").strip()) UpperCamelCase__ : int = int(input("Enter number of edges: ").strip()) UpperCamelCase__ : list[dict[str, int]] = [{} for _ in range(E)] for i in range(E): print("Edge ", i + 1) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ : Dict = ( int(x) for x in input("Enter source, destination, weight: ").strip().split(" ") ) UpperCamelCase__ : Optional[Any] = {"src": src, "dst": dest, "weight": weight} UpperCamelCase__ : Optional[Any] = int(input("\nEnter shortest path source:").strip()) UpperCamelCase__ : Any = bellman_ford(graph, V, E, source) print_distance(shortest_distance, 0)
0
0
'''simple docstring''' from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import numpy as np import tensorflow as tf from transformers import ( TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST, FlaubertConfig, TFFlaubertForMultipleChoice, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForSequenceClassification, TFFlaubertForTokenClassification, TFFlaubertModel, TFFlaubertWithLMHeadModel, ) class _a : """simple docstring""" def __init__( self , A__ , ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = parent _SCREAMING_SNAKE_CASE = 13 _SCREAMING_SNAKE_CASE = 7 _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = 2 _SCREAMING_SNAKE_CASE = 99 _SCREAMING_SNAKE_CASE = 0 _SCREAMING_SNAKE_CASE = 32 _SCREAMING_SNAKE_CASE = 2 _SCREAMING_SNAKE_CASE = 4 _SCREAMING_SNAKE_CASE = 0.1 _SCREAMING_SNAKE_CASE = 0.1 _SCREAMING_SNAKE_CASE = 5_12 _SCREAMING_SNAKE_CASE = 16 _SCREAMING_SNAKE_CASE = 2 _SCREAMING_SNAKE_CASE = 0.02 _SCREAMING_SNAKE_CASE = 3 _SCREAMING_SNAKE_CASE = 4 _SCREAMING_SNAKE_CASE = """last""" _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = 0 def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _SCREAMING_SNAKE_CASE = random_attention_mask([self.batch_size, self.seq_length] , dtype=tf.floataa ) _SCREAMING_SNAKE_CASE = None if self.use_input_lengths: _SCREAMING_SNAKE_CASE = ( ids_tensor([self.batch_size] , vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length _SCREAMING_SNAKE_CASE = None if self.use_token_type_ids: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.n_langs ) _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None if self.use_labels: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , 2 , dtype=tf.floataa ) _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.num_choices ) _SCREAMING_SNAKE_CASE = FlaubertConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , bos_token_id=self.bos_token_id , ) return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ , A__ , A__ , ) -> List[Any]: _SCREAMING_SNAKE_CASE = TFFlaubertModel(config=A__ ) _SCREAMING_SNAKE_CASE = {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids} _SCREAMING_SNAKE_CASE = model(A__ ) _SCREAMING_SNAKE_CASE = [input_ids, input_mask] _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ , A__ , A__ , ) -> List[str]: _SCREAMING_SNAKE_CASE = TFFlaubertWithLMHeadModel(A__ ) _SCREAMING_SNAKE_CASE = {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids} _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ , A__ , A__ , ) -> List[Any]: _SCREAMING_SNAKE_CASE = TFFlaubertForQuestionAnsweringSimple(A__ ) _SCREAMING_SNAKE_CASE = {"""input_ids""": input_ids, """lengths""": input_lengths} _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ , A__ , A__ , ) -> Dict: _SCREAMING_SNAKE_CASE = TFFlaubertForSequenceClassification(A__ ) _SCREAMING_SNAKE_CASE = {"""input_ids""": input_ids, """lengths""": input_lengths} _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ , A__ , A__ , ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.num_labels _SCREAMING_SNAKE_CASE = TFFlaubertForTokenClassification(config=A__ ) _SCREAMING_SNAKE_CASE = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ , A__ , A__ , ) -> int: _SCREAMING_SNAKE_CASE = self.num_choices _SCREAMING_SNAKE_CASE = TFFlaubertForMultipleChoice(config=A__ ) _SCREAMING_SNAKE_CASE = tf.tile(tf.expand_dims(A__ , 1 ) , (1, self.num_choices, 1) ) _SCREAMING_SNAKE_CASE = tf.tile(tf.expand_dims(A__ , 1 ) , (1, self.num_choices, 1) ) _SCREAMING_SNAKE_CASE = tf.tile(tf.expand_dims(A__ , 1 ) , (1, self.num_choices, 1) ) _SCREAMING_SNAKE_CASE = { """input_ids""": multiple_choice_inputs_ids, """attention_mask""": multiple_choice_input_mask, """token_type_ids""": multiple_choice_token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = 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 ) , ) = config_and_inputs _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """token_type_ids""": token_type_ids, """langs""": token_type_ids, """lengths""": input_lengths, } return config, inputs_dict @require_tf class _a (_lowerCamelCase , _lowerCamelCase , unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = ( ( TFFlaubertModel, TFFlaubertWithLMHeadModel, TFFlaubertForSequenceClassification, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForTokenClassification, TFFlaubertForMultipleChoice, ) if is_tf_available() else () ) SCREAMING_SNAKE_CASE = ( (TFFlaubertWithLMHeadModel,) if is_tf_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable SCREAMING_SNAKE_CASE = ( { 'feature-extraction': TFFlaubertModel, 'fill-mask': TFFlaubertWithLMHeadModel, 'question-answering': TFFlaubertForQuestionAnsweringSimple, 'text-classification': TFFlaubertForSequenceClassification, 'token-classification': TFFlaubertForTokenClassification, 'zero-shot': TFFlaubertForSequenceClassification, } if is_tf_available() else {} ) SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = False def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ ) -> Tuple: if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith("""Fast""" ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def UpperCamelCase ( self ) -> int: _SCREAMING_SNAKE_CASE = TFFlaubertModelTester(self ) _SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=A__ , emb_dim=37 ) def UpperCamelCase ( self ) -> Dict: self.config_tester.run_common_tests() def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*A__ ) def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*A__ ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*A__ ) def UpperCamelCase ( self ) -> int: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*A__ ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_token_classification(*A__ ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_multiple_choice(*A__ ) @slow def UpperCamelCase ( self ) -> Union[str, Any]: for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _SCREAMING_SNAKE_CASE = TFFlaubertModel.from_pretrained(A__ ) self.assertIsNotNone(A__ ) @require_tf @require_sentencepiece @require_tokenizers class _a (unittest.TestCase): """simple docstring""" @slow def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = TFFlaubertModel.from_pretrained("""jplu/tf-flaubert-small-cased""" ) _SCREAMING_SNAKE_CASE = tf.convert_to_tensor( [[0, 1_58, 7_35, 25_92, 14_24, 67_27, 82, 1]] , dtype=tf.intaa , ) # "J'aime flaubert !" _SCREAMING_SNAKE_CASE = model(A__ )[0] _SCREAMING_SNAKE_CASE = tf.TensorShape((1, 8, 5_12) ) self.assertEqual(output.shape , A__ ) # compare the actual values for a slice. _SCREAMING_SNAKE_CASE = tf.convert_to_tensor( [ [ [-1.876_8773, -1.56_6555, 0.2707_2418], [-1.692_0038, -0.587_3505, 1.932_9599], [-2.956_3985, -1.699_3835, 1.797_2052], ] ] , dtype=tf.floataa , ) self.assertTrue(np.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1E-4 ) )
707
'''simple docstring''' 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 , A__ , A__=13 , A__=7 , A__=True , A__=True , A__=True , A__=True , A__=99 , A__=32 , A__=2 , A__=4 , A__=37 , A__="gelu" , A__=0.1 , A__=0.1 , A__=5_12 , A__=16 , A__=2 , A__=0.02 , A__=3 , A__=4 , A__=None , ) -> int: _SCREAMING_SNAKE_CASE = parent _SCREAMING_SNAKE_CASE = 13 _SCREAMING_SNAKE_CASE = 7 _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = 99 _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 = 5_12 _SCREAMING_SNAKE_CASE = 16 _SCREAMING_SNAKE_CASE = 2 _SCREAMING_SNAKE_CASE = 0.02 _SCREAMING_SNAKE_CASE = 3 _SCREAMING_SNAKE_CASE = 4 _SCREAMING_SNAKE_CASE = None def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _SCREAMING_SNAKE_CASE = None if self.use_input_mask: _SCREAMING_SNAKE_CASE = random_attention_mask([self.batch_size, self.seq_length] ) _SCREAMING_SNAKE_CASE = None if self.use_token_type_ids: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None if self.use_labels: _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.num_choices ) _SCREAMING_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=A__ , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = TFRoFormerModel(config=A__ ) _SCREAMING_SNAKE_CASE = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} _SCREAMING_SNAKE_CASE = [input_ids, input_mask] _SCREAMING_SNAKE_CASE = model(A__ ) _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> str: _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = TFRoFormerForCausalLM(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ )["""logits"""] self.parent.assertListEqual( list(prediction_scores.numpy().shape ) , [self.batch_size, self.seq_length, self.vocab_size] ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> Dict: _SCREAMING_SNAKE_CASE = TFRoFormerForMaskedLM(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> List[str]: _SCREAMING_SNAKE_CASE = self.num_labels _SCREAMING_SNAKE_CASE = TFRoFormerForSequenceClassification(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> Any: _SCREAMING_SNAKE_CASE = self.num_choices _SCREAMING_SNAKE_CASE = TFRoFormerForMultipleChoice(config=A__ ) _SCREAMING_SNAKE_CASE = tf.tile(tf.expand_dims(A__ , 1 ) , (1, self.num_choices, 1) ) _SCREAMING_SNAKE_CASE = tf.tile(tf.expand_dims(A__ , 1 ) , (1, self.num_choices, 1) ) _SCREAMING_SNAKE_CASE = tf.tile(tf.expand_dims(A__ , 1 ) , (1, self.num_choices, 1) ) _SCREAMING_SNAKE_CASE = { """input_ids""": multiple_choice_inputs_ids, """attention_mask""": multiple_choice_input_mask, """token_type_ids""": multiple_choice_token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> List[str]: _SCREAMING_SNAKE_CASE = self.num_labels _SCREAMING_SNAKE_CASE = TFRoFormerForTokenClassification(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ , A__ , A__ ) -> Tuple: _SCREAMING_SNAKE_CASE = TFRoFormerForQuestionAnswering(config=A__ ) _SCREAMING_SNAKE_CASE = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _SCREAMING_SNAKE_CASE = model(A__ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = 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 ) , ) = config_and_inputs _SCREAMING_SNAKE_CASE = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask} return config, inputs_dict @require_tf class _a (_lowerCamelCase , _lowerCamelCase , unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = ( ( TFRoFormerModel, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerForMultipleChoice, ) if is_tf_available() else () ) SCREAMING_SNAKE_CASE = ( { '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 {} ) SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = False def UpperCamelCase ( self , A__ , A__ , A__ , A__ , A__ ) -> str: if pipeline_test_casse_name == "TextGenerationPipelineTests": return True return False def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = TFRoFormerModelTester(self ) _SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=A__ , hidden_size=37 ) def UpperCamelCase ( self ) -> Optional[Any]: self.config_tester.run_common_tests() def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A__ ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*A__ ) def UpperCamelCase ( self ) -> int: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head(*A__ ) def UpperCamelCase ( self ) -> Dict: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*A__ ) def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*A__ ) def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*A__ ) def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*A__ ) @slow def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = TFRoFormerModel.from_pretrained("""junnyu/roformer_chinese_base""" ) self.assertIsNotNone(A__ ) @require_tf class _a (unittest.TestCase): """simple docstring""" @slow def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = TFRoFormerForMaskedLM.from_pretrained("""junnyu/roformer_chinese_base""" ) _SCREAMING_SNAKE_CASE = tf.constant([[0, 1, 2, 3, 4, 5]] ) _SCREAMING_SNAKE_CASE = model(A__ )[0] # TODO Replace vocab size _SCREAMING_SNAKE_CASE = 5_00_00 _SCREAMING_SNAKE_CASE = [1, 6, vocab_size] self.assertEqual(output.shape , A__ ) print(output[:, :3, :3] ) # TODO Replace values below with what was printed above. _SCREAMING_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] , A__ , atol=1E-4 ) @require_tf class _a (unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = 1E-4 def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = tf.constant([[4, 10]] ) _SCREAMING_SNAKE_CASE = TFRoFormerSinusoidalPositionalEmbedding(num_positions=6 , embedding_dim=6 ) _SCREAMING_SNAKE_CASE = emba(input_ids.shape ) _SCREAMING_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(A__ , A__ , atol=self.tolerance ) def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_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], ] ) _SCREAMING_SNAKE_CASE = TFRoFormerSinusoidalPositionalEmbedding(num_positions=5_12 , embedding_dim=5_12 ) emba([2, 16, 5_12] ) _SCREAMING_SNAKE_CASE = emba.weight[:3, :5] tf.debugging.assert_near(A__ , A__ , atol=self.tolerance ) @require_tf class _a (unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = 1E-4 def UpperCamelCase ( self ) -> int: # 2,12,16,64 _SCREAMING_SNAKE_CASE = tf.reshape(tf.range(2 * 12 * 16 * 64 , dtype=tf.floataa ) , shape=(2, 12, 16, 64) ) / 1_00 _SCREAMING_SNAKE_CASE = -tf.reshape(tf.range(2 * 12 * 16 * 64 , dtype=tf.floataa ) , shape=(2, 12, 16, 64) ) / 1_00 _SCREAMING_SNAKE_CASE = TFRoFormerSinusoidalPositionalEmbedding(num_positions=32 , embedding_dim=64 ) _SCREAMING_SNAKE_CASE = embed_positions([2, 16, 7_68] )[None, None, :, :] _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = TFRoFormerSelfAttention.apply_rotary_position_embeddings( A__ , A__ , A__ ) _SCREAMING_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], ] ) _SCREAMING_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] , A__ , atol=self.tolerance ) tf.debugging.assert_near(key_layer[0, 0, :6, :8] , A__ , atol=self.tolerance )
0
0
'''simple docstring''' import math def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> list[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = 2 _SCREAMING_SNAKE_CASE = int(math.sqrt(_A ) ) # Size of every segment _SCREAMING_SNAKE_CASE = [True] * (end + 1) _SCREAMING_SNAKE_CASE = [] while start <= end: if temp[start] is True: in_prime.append(_A ) for i in range(start * start , end + 1 , _A ): _SCREAMING_SNAKE_CASE = False start += 1 prime += in_prime _SCREAMING_SNAKE_CASE = end + 1 _SCREAMING_SNAKE_CASE = min(2 * end , _A ) while low <= n: _SCREAMING_SNAKE_CASE = [True] * (high - low + 1) for each in in_prime: _SCREAMING_SNAKE_CASE = math.floor(low / each ) * each if t < low: t += each for j in range(_A , high + 1 , _A ): _SCREAMING_SNAKE_CASE = False for j in range(len(_A ) ): if temp[j] is True: prime.append(j + low ) _SCREAMING_SNAKE_CASE = high + 1 _SCREAMING_SNAKE_CASE = min(high + end , _A ) return prime print(sieve(10**6))
708
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available UpperCamelCase__ : int = {"tokenization_herbert": ["HerbertTokenizer"]} try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Tuple = ["HerbertTokenizerFast"] if TYPE_CHECKING: from .tokenization_herbert import HerbertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_herbert_fast import HerbertTokenizerFast else: import sys UpperCamelCase__ : Tuple = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
0
0
'''simple docstring''' import collections import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging UpperCamelCase__ : Tuple = logging.get_logger(__name__) UpperCamelCase__ : Tuple = '▁' UpperCamelCase__ : Any = {'vocab_file': 'prophetnet.tokenizer'} UpperCamelCase__ : List[str] = { 'vocab_file': { 'microsoft/xprophetnet-large-wiki100-cased': ( 'https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/prophetnet.tokenizer' ), } } UpperCamelCase__ : str = { 'microsoft/xprophetnet-large-wiki100-cased': {'do_lower_case': False}, } UpperCamelCase__ : Any = { 'microsoft/xprophetnet-large-wiki100-cased': 512, } def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE = collections.OrderedDict() with open(A_ , """r""" , encoding="""utf-8""" ) as reader: _SCREAMING_SNAKE_CASE = reader.readlines() for index, token in enumerate(A_ ): _SCREAMING_SNAKE_CASE = token.rstrip("""\n""" ) _SCREAMING_SNAKE_CASE = index return vocab class _a (_A): """simple docstring""" SCREAMING_SNAKE_CASE = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE = ['input_ids', 'attention_mask'] def __init__( self , A__ , A__="[SEP]" , A__="[SEP]" , A__="[SEP]" , A__="[UNK]" , A__="[PAD]" , A__="[CLS]" , A__="[MASK]" , A__ = None , **A__ , ) -> None: _SCREAMING_SNAKE_CASE = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=UpperCamelCase__ , eos_token=UpperCamelCase__ , sep_token=UpperCamelCase__ , unk_token=UpperCamelCase__ , pad_token=UpperCamelCase__ , cls_token=UpperCamelCase__ , mask_token=UpperCamelCase__ , sp_model_kwargs=self.sp_model_kwargs , **UpperCamelCase__ , ) try: import sentencepiece as spm except ImportError: logger.warning( """You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece""" """ pip install sentencepiece""" ) raise _SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(UpperCamelCase__ ) ) _SCREAMING_SNAKE_CASE = vocab_file # Original fairseq vocab and spm vocab must be "aligned": # Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ---- # fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's' | '▁de' | '-' # spm | '<unk>' | '<s>' | '</s>' | ',' | '.' | '▁' | 's' | '▁de' | '-' | '▁a' # put special tokens and [unused] tokens into the vocab _SCREAMING_SNAKE_CASE = {"""[PAD]""": 0, """[CLS]""": 1, """[SEP]""": 2, """[UNK]""": 3, """[MASK]""": 4} for i in range(10 ): _SCREAMING_SNAKE_CASE = F"[unused{i}]" _SCREAMING_SNAKE_CASE = 5 + i # The first "real" token "," has position 15 in the embedding vocab and position 3 in the spm vocab _SCREAMING_SNAKE_CASE = 12 _SCREAMING_SNAKE_CASE = {v: k for k, v in self.fairseq_tokens_to_ids.items()} for k in self.fairseq_tokens_to_ids.keys(): self.unique_no_split_tokens.append(UpperCamelCase__ ) def __getstate__( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = self.__dict__.copy() _SCREAMING_SNAKE_CASE = None return state def __setstate__( self , A__ ) -> Optional[int]: _SCREAMING_SNAKE_CASE = d try: import sentencepiece as spm except ImportError: logger.warning( """You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece""" """ pip install sentencepiece""" ) raise # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): _SCREAMING_SNAKE_CASE = {} _SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCamelCase ( self , A__ , A__ = None , A__ = False ) -> List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=UpperCamelCase__ , token_ids_a=UpperCamelCase__ , already_has_special_tokens=UpperCamelCase__ ) if token_ids_a is None: return ([0] * len(UpperCamelCase__ )) + [1] return ([0] * len(UpperCamelCase__ )) + [1] + ([0] * len(UpperCamelCase__ )) + [1] def UpperCamelCase ( self , A__ , A__ = None ) -> List[int]: _SCREAMING_SNAKE_CASE = [self.sep_token_id] if token_ids_a is None: return len(token_ids_a + sep ) * [0] return len(token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def UpperCamelCase ( self ) -> str: return len(self.sp_model ) + self.fairseq_offset def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = {self.convert_ids_to_tokens(UpperCamelCase__ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def UpperCamelCase ( self , A__ ) -> str: return self.sp_model.encode(UpperCamelCase__ , out_type=UpperCamelCase__ ) def UpperCamelCase ( self , A__ ) -> Dict: if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] _SCREAMING_SNAKE_CASE = self.sp_model.PieceToId(UpperCamelCase__ ) # Need to return unknown token if the SP model returned 0 return spm_id + self.fairseq_offset if spm_id else self.unk_token_id def UpperCamelCase ( self , A__ ) -> List[Any]: if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def UpperCamelCase ( self , A__ ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = """""".join(UpperCamelCase__ ).replace(UpperCamelCase__ , """ """ ).strip() return out_string def UpperCamelCase ( self , A__ , A__ = None ) -> Tuple[str]: 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"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(UpperCamelCase__ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , UpperCamelCase__ ) elif not os.path.isfile(self.vocab_file ): with open(UpperCamelCase__ , """wb""" ) as fi: _SCREAMING_SNAKE_CASE = self.sp_model.serialized_model_proto() fi.write(UpperCamelCase__ ) return (out_vocab_file,) def UpperCamelCase ( self , A__ , A__ = None ) -> List[int]: if token_ids_a is None: return token_ids_a + [self.sep_token_id] _SCREAMING_SNAKE_CASE = [self.sep_token_id] return token_ids_a + sep + token_ids_a + sep
709
'''simple docstring''' import argparse import gdown import numpy as np import torch from huggingface_hub import hf_hub_download from transformers import ( CLIPTokenizer, CLIPTokenizerFast, VideoMAEImageProcessor, XCLIPConfig, XCLIPModel, XCLIPProcessor, XCLIPTextConfig, XCLIPVisionConfig, ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE = XCLIPTextConfig() # derive patch size from model name _SCREAMING_SNAKE_CASE = model_name.find("""patch""" ) _SCREAMING_SNAKE_CASE = int(model_name[start_idx + len("""patch""" ) : start_idx + len("""patch""" ) + 2] ) _SCREAMING_SNAKE_CASE = XCLIPVisionConfig(patch_size=SCREAMING_SNAKE_CASE_ , num_frames=SCREAMING_SNAKE_CASE_ ) if "large" in model_name: _SCREAMING_SNAKE_CASE = 7_68 _SCREAMING_SNAKE_CASE = 30_72 _SCREAMING_SNAKE_CASE = 12 _SCREAMING_SNAKE_CASE = 10_24 _SCREAMING_SNAKE_CASE = 40_96 _SCREAMING_SNAKE_CASE = 16 _SCREAMING_SNAKE_CASE = 24 _SCREAMING_SNAKE_CASE = 7_68 _SCREAMING_SNAKE_CASE = 30_72 if model_name == "xclip-large-patch14-16-frames": _SCREAMING_SNAKE_CASE = 3_36 _SCREAMING_SNAKE_CASE = XCLIPConfig.from_text_vision_configs(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if "large" in model_name: _SCREAMING_SNAKE_CASE = 7_68 return config def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Dict: """simple docstring""" # text encoder if name == "token_embedding.weight": _SCREAMING_SNAKE_CASE = name.replace("""token_embedding.weight""" , """text_model.embeddings.token_embedding.weight""" ) if name == "positional_embedding": _SCREAMING_SNAKE_CASE = name.replace("""positional_embedding""" , """text_model.embeddings.position_embedding.weight""" ) if "ln_1" in name: _SCREAMING_SNAKE_CASE = name.replace("""ln_1""" , """layer_norm1""" ) if "ln_2" in name: _SCREAMING_SNAKE_CASE = name.replace("""ln_2""" , """layer_norm2""" ) if "c_fc" in name: _SCREAMING_SNAKE_CASE = name.replace("""c_fc""" , """fc1""" ) if "c_proj" in name: _SCREAMING_SNAKE_CASE = name.replace("""c_proj""" , """fc2""" ) if name.startswith("""transformer.resblocks""" ): _SCREAMING_SNAKE_CASE = name.replace("""transformer.resblocks""" , """text_model.encoder.layers""" ) if "attn.out_proj" in name and "message" not in name: _SCREAMING_SNAKE_CASE = name.replace("""attn.out_proj""" , """self_attn.out_proj""" ) if "ln_final" in name: _SCREAMING_SNAKE_CASE = name.replace("""ln_final""" , """text_model.final_layer_norm""" ) # visual encoder if name == "visual.class_embedding": _SCREAMING_SNAKE_CASE = name.replace("""visual.class_embedding""" , """vision_model.embeddings.class_embedding""" ) if name == "visual.positional_embedding": _SCREAMING_SNAKE_CASE = name.replace("""visual.positional_embedding""" , """vision_model.embeddings.position_embedding.weight""" ) if name.startswith("""visual.transformer.resblocks""" ): _SCREAMING_SNAKE_CASE = name.replace("""visual.transformer.resblocks""" , """vision_model.encoder.layers""" ) if "visual.conv1" in name: _SCREAMING_SNAKE_CASE = name.replace("""visual.conv1""" , """vision_model.embeddings.patch_embedding""" ) if "visual.ln_pre" in name: _SCREAMING_SNAKE_CASE = name.replace("""visual.ln_pre""" , """vision_model.pre_layernorm""" ) if "visual.ln_post" in name: _SCREAMING_SNAKE_CASE = name.replace("""visual.ln_post""" , """vision_model.post_layernorm""" ) if "visual.proj" in name: _SCREAMING_SNAKE_CASE = name.replace("""visual.proj""" , """visual_projection.weight""" ) if "text_projection" in name: _SCREAMING_SNAKE_CASE = name.replace("""text_projection""" , """text_projection.weight""" ) # things on top if "prompts_visual_proj" in name: _SCREAMING_SNAKE_CASE = name.replace("""prompts_visual_proj""" , """prompts_visual_projection""" ) if "prompts_visual_ln" in name: _SCREAMING_SNAKE_CASE = name.replace("""prompts_visual_ln""" , """prompts_visual_layernorm""" ) # mit if name == "mit.positional_embedding": _SCREAMING_SNAKE_CASE = name.replace("""positional""" , """position""" ) if name.startswith("""mit.resblocks""" ): _SCREAMING_SNAKE_CASE = name.replace("""mit.resblocks""" , """mit.encoder.layers""" ) # prompts generator if name.startswith("""prompts_generator.norm""" ): _SCREAMING_SNAKE_CASE = name.replace("""prompts_generator.norm""" , """prompts_generator.layernorm""" ) return name def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Optional[int]: """simple docstring""" for key in orig_state_dict.copy().keys(): _SCREAMING_SNAKE_CASE = orig_state_dict.pop(SCREAMING_SNAKE_CASE_ ) if "attn.in_proj" in key: _SCREAMING_SNAKE_CASE = key.split(""".""" ) if key.startswith("""visual""" ): _SCREAMING_SNAKE_CASE = key_split[3] _SCREAMING_SNAKE_CASE = config.vision_config.hidden_size if "message_attn" in key: if "weight" in key: _SCREAMING_SNAKE_CASE = val[ :dim, : ] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2, : ] _SCREAMING_SNAKE_CASE = val[ -dim:, : ] else: _SCREAMING_SNAKE_CASE = val[ :dim ] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2 ] _SCREAMING_SNAKE_CASE = val[ -dim: ] else: if "weight" in key: _SCREAMING_SNAKE_CASE = val[ :dim, : ] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2, : ] _SCREAMING_SNAKE_CASE = val[ -dim:, : ] else: _SCREAMING_SNAKE_CASE = val[:dim] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2 ] _SCREAMING_SNAKE_CASE = val[-dim:] elif key.startswith("""mit""" ): _SCREAMING_SNAKE_CASE = key_split[2] _SCREAMING_SNAKE_CASE = config.vision_config.mit_hidden_size if "weight" in key: _SCREAMING_SNAKE_CASE = val[:dim, :] _SCREAMING_SNAKE_CASE = val[dim : dim * 2, :] _SCREAMING_SNAKE_CASE = val[-dim:, :] else: _SCREAMING_SNAKE_CASE = val[:dim] _SCREAMING_SNAKE_CASE = val[dim : dim * 2] _SCREAMING_SNAKE_CASE = val[-dim:] else: _SCREAMING_SNAKE_CASE = key_split[2] _SCREAMING_SNAKE_CASE = config.text_config.hidden_size if "weight" in key: _SCREAMING_SNAKE_CASE = val[:dim, :] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2, : ] _SCREAMING_SNAKE_CASE = val[-dim:, :] else: _SCREAMING_SNAKE_CASE = val[:dim] _SCREAMING_SNAKE_CASE = val[ dim : dim * 2 ] _SCREAMING_SNAKE_CASE = val[-dim:] else: _SCREAMING_SNAKE_CASE = rename_key(SCREAMING_SNAKE_CASE_ ) if new_key_name in ["visual_projection.weight", "text_projection.weight"]: _SCREAMING_SNAKE_CASE = val.T _SCREAMING_SNAKE_CASE = val return orig_state_dict def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: """simple docstring""" if num_frames == 8: _SCREAMING_SNAKE_CASE = """eating_spaghetti_8_frames.npy""" elif num_frames == 16: _SCREAMING_SNAKE_CASE = """eating_spaghetti.npy""" elif num_frames == 32: _SCREAMING_SNAKE_CASE = """eating_spaghetti_32_frames.npy""" _SCREAMING_SNAKE_CASE = hf_hub_download( repo_id="""hf-internal-testing/spaghetti-video""" , filename=SCREAMING_SNAKE_CASE_ , repo_type="""dataset""" , ) _SCREAMING_SNAKE_CASE = np.load(SCREAMING_SNAKE_CASE_ ) return list(SCREAMING_SNAKE_CASE_ ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_=False ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = { # fully supervised kinetics-400 checkpoints """xclip-base-patch32""": """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_8.pth""", """xclip-base-patch32-16-frames""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_16.pth""" ), """xclip-base-patch16""": """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_8.pth""", """xclip-base-patch16-16-frames""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_16.pth""" ), """xclip-large-patch14""": """https://drive.google.com/u/0/uc?id=1NUOImq0o5DlQTST17iIP3vG7DgmHQuCx&amp;export=download&amp;confirm=t&amp;uuid=b26caedc-88e2-473e-830a-9d158b653cdb""", """xclip-large-patch14-16-frames""": """https://drive.google.com/u/0/uc?id=1FOYgnJc097OJ4lGwtRCCydQyVPJEOH7d&amp;export=download&amp;confirm=t&amp;uuid=538fa810-e671-4050-b385-9a623f89804f""", # fully supervised kinetics-600 checkpoints """xclip-base-patch16-kinetics-600""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_8.pth""" ), """xclip-base-patch16-kinetics-600-16-frames""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_16.pth""" ), """xclip-large-patch14-kinetics-600""": """https://drive.google.com/u/0/uc?id=1FV8C1INuM91sLAN4ImjzePLIlpMSihwV&amp;export=download&amp;confirm=t&amp;uuid=141d4977-4a65-44ae-864f-4b0c19f838be""", # few shot """xclip-base-patch16-hmdb-2-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_2.pth""" ), """xclip-base-patch16-hmdb-4-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_4.pth""" ), """xclip-base-patch16-hmdb-8-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_8.pth""" ), """xclip-base-patch16-hmdb-16-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_16.pth""" ), """xclip-base-patch16-ucf-2-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_2.pth""" ), """xclip-base-patch16-ucf-4-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_4.pth""" ), """xclip-base-patch16-ucf-8-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_8.pth""" ), """xclip-base-patch16-ucf-16-shot""": ( """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_16.pth""" ), # zero shot """xclip-base-patch16-zero-shot""": """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/zero.pth""", } _SCREAMING_SNAKE_CASE = model_to_url[model_name] _SCREAMING_SNAKE_CASE = 8 if "16-frames" in model_name: _SCREAMING_SNAKE_CASE = 16 elif "shot" in model_name: _SCREAMING_SNAKE_CASE = 32 _SCREAMING_SNAKE_CASE = get_xclip_config(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = XCLIPModel(SCREAMING_SNAKE_CASE_ ) model.eval() if "drive" in checkpoint_url: _SCREAMING_SNAKE_CASE = """pytorch_model.bin""" gdown.cached_download(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , quiet=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = torch.load(SCREAMING_SNAKE_CASE_ , map_location="""cpu""" )["""model"""] else: _SCREAMING_SNAKE_CASE = torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE_ )["""model"""] _SCREAMING_SNAKE_CASE = convert_state_dict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = XCLIPModel(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = model.load_state_dict(SCREAMING_SNAKE_CASE_ , strict=SCREAMING_SNAKE_CASE_ ) assert missing_keys == ["text_model.embeddings.position_ids", "vision_model.embeddings.position_ids"] model.eval() _SCREAMING_SNAKE_CASE = 3_36 if model_name == """xclip-large-patch14-16-frames""" else 2_24 _SCREAMING_SNAKE_CASE = VideoMAEImageProcessor(size=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = CLIPTokenizer.from_pretrained("""openai/clip-vit-base-patch32""" ) _SCREAMING_SNAKE_CASE = CLIPTokenizerFast.from_pretrained("""openai/clip-vit-base-patch32""" ) _SCREAMING_SNAKE_CASE = XCLIPProcessor(image_processor=SCREAMING_SNAKE_CASE_ , tokenizer=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = prepare_video(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = processor( text=["""playing sports""", """eating spaghetti""", """go shopping"""] , videos=SCREAMING_SNAKE_CASE_ , return_tensors="""pt""" , padding=SCREAMING_SNAKE_CASE_ ) print("""Shape of pixel values:""" , inputs.pixel_values.shape ) with torch.no_grad(): _SCREAMING_SNAKE_CASE = model(**SCREAMING_SNAKE_CASE_ ) # Verify outputs _SCREAMING_SNAKE_CASE = outputs.logits_per_video _SCREAMING_SNAKE_CASE = logits_per_video.softmax(dim=1 ) print("""Probs:""" , SCREAMING_SNAKE_CASE_ ) # kinetics-400 if model_name == "xclip-base-patch32": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0019, 0.9951, 0.0030]] ) elif model_name == "xclip-base-patch32-16-frames": _SCREAMING_SNAKE_CASE = torch.tensor([[7.0999e-04, 9.9883e-01, 4.5580e-04]] ) elif model_name == "xclip-base-patch16": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0083, 0.9681, 0.0236]] ) elif model_name == "xclip-base-patch16-16-frames": _SCREAMING_SNAKE_CASE = torch.tensor([[7.6937e-04, 9.9728e-01, 1.9473e-03]] ) elif model_name == "xclip-large-patch14": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0062, 0.9864, 0.0075]] ) elif model_name == "xclip-large-patch14-16-frames": _SCREAMING_SNAKE_CASE = torch.tensor([[3.3877e-04, 9.9937e-01, 2.8888e-04]] ) # kinetics-600 elif model_name == "xclip-base-patch16-kinetics-600": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0555, 0.8914, 0.0531]] ) elif model_name == "xclip-base-patch16-kinetics-600-16-frames": _SCREAMING_SNAKE_CASE = torch.tensor([[3.8554e-04, 9.9929e-01, 3.2754e-04]] ) elif model_name == "xclip-large-patch14-kinetics-600": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0036, 0.9920, 0.0045]] ) # few shot elif model_name == "xclip-base-patch16-hmdb-2-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[7.1890e-06, 9.9994e-01, 5.6559e-05]] ) elif model_name == "xclip-base-patch16-hmdb-4-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[1.0320e-05, 9.9993e-01, 6.2435e-05]] ) elif model_name == "xclip-base-patch16-hmdb-8-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[4.1377e-06, 9.9990e-01, 9.8386e-05]] ) elif model_name == "xclip-base-patch16-hmdb-16-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[4.1347e-05, 9.9962e-01, 3.3411e-04]] ) elif model_name == "xclip-base-patch16-ucf-2-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[8.5857e-05, 9.9928e-01, 6.3291e-04]] ) elif model_name == "xclip-base-patch16-ucf-4-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[8.5857e-05, 9.9928e-01, 6.3291e-04]] ) elif model_name == "xclip-base-patch16-ucf-8-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[0.0027, 0.9904, 0.0070]] ) elif model_name == "xclip-base-patch16-ucf-16-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[9.8219e-04, 9.9593e-01, 3.0863e-03]] ) # zero shot elif model_name == "xclip-base-patch16-zero-shot": _SCREAMING_SNAKE_CASE = torch.tensor([[3.5082e-04, 9.9785e-01, 1.7966e-03]] ) else: raise ValueError(F"Model name {model_name} not supported" ) assert torch.allclose(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , atol=1e-3 ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: print(F"Saving model {model_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(SCREAMING_SNAKE_CASE_ ) if push_to_hub: print("""Pushing model, processor and slow tokenizer files to the hub...""" ) model.push_to_hub(SCREAMING_SNAKE_CASE_ , organization="""nielsr""" ) processor.push_to_hub(SCREAMING_SNAKE_CASE_ , organization="""nielsr""" ) slow_tokenizer.push_to_hub(SCREAMING_SNAKE_CASE_ , organization="""nielsr""" ) if __name__ == "__main__": UpperCamelCase__ : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default="xclip-base-patch32", type=str, help="Name of the model.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether or not to push the converted model to the 🤗 hub." ) UpperCamelCase__ : str = parser.parse_args() convert_xclip_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
0
0
'''simple docstring''' def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" return number | (1 << position) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" return number & ~(1 << position) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" return number ^ (1 << position) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> bool: """simple docstring""" return ((number >> position) & 1) == 1 def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" return int((number & (1 << position)) != 0 ) if __name__ == "__main__": import doctest doctest.testmod()
710
'''simple docstring''' import numpy as np import torch from torch.utils.data import Dataset from utils import logger class _a (_lowerCamelCase): """simple docstring""" def __init__( self , A__ , A__ ) -> Any: _SCREAMING_SNAKE_CASE = params _SCREAMING_SNAKE_CASE = np.array(A__ ) _SCREAMING_SNAKE_CASE = np.array([len(A__ ) for t in data] ) self.check() self.remove_long_sequences() self.remove_empty_sequences() self.remove_unknown_sequences() self.check() self.print_statistics() def __getitem__( self , A__ ) -> Dict: return (self.token_ids[index], self.lengths[index]) def __len__( self ) -> Tuple: return len(self.lengths ) def UpperCamelCase ( self ) -> Dict: assert len(self.token_ids ) == len(self.lengths ) assert all(self.lengths[i] == len(self.token_ids[i] ) for i in range(len(self.lengths ) ) ) def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = self.params.max_model_input_size _SCREAMING_SNAKE_CASE = self.lengths > max_len logger.info(F"Splitting {sum(A__ )} too long sequences." ) def divide_chunks(A__ , A__ ): return [l[i : i + n] for i in range(0 , len(A__ ) , A__ )] _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = [] if self.params.mlm: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self.params.special_tok_ids["""cls_token"""], self.params.special_tok_ids["""sep_token"""] else: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 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: _SCREAMING_SNAKE_CASE = [] for sub_s in divide_chunks(seq_ , max_len - 2 ): if sub_s[0] != cls_id: _SCREAMING_SNAKE_CASE = np.insert(A__ , 0 , A__ ) if sub_s[-1] != sep_id: _SCREAMING_SNAKE_CASE = np.insert(A__ , len(A__ ) , A__ ) assert len(A__ ) <= max_len assert (sub_s[0] == cls_id) and (sub_s[-1] == sep_id), sub_s sub_seqs.append(A__ ) new_tok_ids.extend(A__ ) new_lengths.extend([len(A__ ) for l in sub_seqs] ) _SCREAMING_SNAKE_CASE = np.array(A__ ) _SCREAMING_SNAKE_CASE = np.array(A__ ) def UpperCamelCase ( self ) -> List[str]: _SCREAMING_SNAKE_CASE = len(self ) _SCREAMING_SNAKE_CASE = self.lengths > 11 _SCREAMING_SNAKE_CASE = self.token_ids[indices] _SCREAMING_SNAKE_CASE = self.lengths[indices] _SCREAMING_SNAKE_CASE = len(self ) logger.info(F"Remove {init_size - new_size} too short (<=11 tokens) sequences." ) def UpperCamelCase ( self ) -> int: if "unk_token" not in self.params.special_tok_ids: return else: _SCREAMING_SNAKE_CASE = self.params.special_tok_ids["""unk_token"""] _SCREAMING_SNAKE_CASE = len(self ) _SCREAMING_SNAKE_CASE = np.array([np.count_nonzero(a == unk_token_id ) for a in self.token_ids] ) _SCREAMING_SNAKE_CASE = (unk_occs / self.lengths) < 0.5 _SCREAMING_SNAKE_CASE = self.token_ids[indices] _SCREAMING_SNAKE_CASE = self.lengths[indices] _SCREAMING_SNAKE_CASE = len(self ) logger.info(F"Remove {init_size - new_size} sequences with a high level of unknown tokens (50%)." ) def UpperCamelCase ( self ) -> Optional[Any]: if not self.params.is_master: return logger.info(F"{len(self )} sequences" ) # data_len = sum(self.lengths) # nb_unique_tokens = len(Counter(list(chain(*self.token_ids)))) # logger.info(f'{data_len} tokens ({nb_unique_tokens} unique)') # unk_idx = self.params.special_tok_ids['unk_token'] # nb_unknown = sum([(t==unk_idx).sum() for t in self.token_ids]) # logger.info(f'{nb_unknown} unknown tokens (covering {100*nb_unknown/data_len:.2f}% of the data)') def UpperCamelCase ( self , A__ ) -> Any: _SCREAMING_SNAKE_CASE = [t[0] for t in batch] _SCREAMING_SNAKE_CASE = [t[1] for t in batch] assert len(A__ ) == len(A__ ) # Max for paddings _SCREAMING_SNAKE_CASE = max(A__ ) # Pad token ids if self.params.mlm: _SCREAMING_SNAKE_CASE = self.params.special_tok_ids["""pad_token"""] else: _SCREAMING_SNAKE_CASE = self.params.special_tok_ids["""unk_token"""] _SCREAMING_SNAKE_CASE = [list(t.astype(A__ ) ) + [pad_idx] * (max_seq_len_ - len(A__ )) for t in token_ids] assert len(tk_ ) == len(A__ ) assert all(len(A__ ) == max_seq_len_ for t in tk_ ) _SCREAMING_SNAKE_CASE = torch.tensor(tk_ ) # (bs, max_seq_len_) _SCREAMING_SNAKE_CASE = torch.tensor(A__ ) # (bs) return tk_t, lg_t
0
0
'''simple docstring''' import os from shutil import copyfile from typing import List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging UpperCamelCase__ : Dict = logging.get_logger(__name__) UpperCamelCase__ : Optional[Any] = {"vocab_file": "sentencepiece.model"} UpperCamelCase__ : Any = { "vocab_file": { "google/rembert": "https://huggingface.co/google/rembert/resolve/main/sentencepiece.model", }, } UpperCamelCase__ : List[str] = { "google/rembert": 256, } class _a (_lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self , A__ , A__=False , A__=True , A__=True , A__="[CLS]" , A__="[SEP]" , A__="[UNK]" , A__="[SEP]" , A__="[PAD]" , A__="[CLS]" , A__="[MASK]" , **A__ , ) -> Optional[int]: super().__init__( do_lower_case=__a , remove_space=__a , keep_accents=__a , bos_token=__a , eos_token=__a , unk_token=__a , sep_token=__a , pad_token=__a , cls_token=__a , mask_token=__a , **__a , ) _SCREAMING_SNAKE_CASE = do_lower_case _SCREAMING_SNAKE_CASE = remove_space _SCREAMING_SNAKE_CASE = keep_accents _SCREAMING_SNAKE_CASE = vocab_file _SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor() self.sp_model.Load(__a ) @property def UpperCamelCase ( self ) -> Dict: return len(self.sp_model ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = {self.convert_ids_to_tokens(__a ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = self.__dict__.copy() _SCREAMING_SNAKE_CASE = None return state def __setstate__( self , A__ ) -> str: _SCREAMING_SNAKE_CASE = d _SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor() self.sp_model.Load(self.vocab_file ) def UpperCamelCase ( self , A__ , A__=False ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = self.sp_model.EncodeAsPieces(__a ) return pieces def UpperCamelCase ( self , A__ ) -> Union[str, Any]: return self.sp_model.PieceToId(__a ) def UpperCamelCase ( self , A__ ) -> Dict: return self.sp_model.IdToPiece(__a ) def UpperCamelCase ( self , A__ ) -> Tuple: _SCREAMING_SNAKE_CASE = self.sp_model.decode_pieces(__a ) return out_string def UpperCamelCase ( self , A__ , A__ = None ) -> List[int]: _SCREAMING_SNAKE_CASE = [self.sep_token_id] _SCREAMING_SNAKE_CASE = [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 UpperCamelCase ( self , A__ , A__ = None , A__ = False ) -> List[int]: if already_has_special_tokens: if token_ids_a is not None: raise ValueError( """You should not supply a second sequence if the provided sequence of """ """ids is already formatted with special tokens for the model.""" ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(__a )) + [1] + ([0] * len(__a )) + [1] return [1] + ([0] * len(__a )) + [1] def UpperCamelCase ( self , A__ , A__ = 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 ) * [0] + len(token_ids_a + sep ) * [1] def UpperCamelCase ( self , A__ , A__ = None ) -> Tuple[str]: if not os.path.isdir(__a ): logger.error("""Vocabulary path ({}) should be a directory""".format(__a ) ) return _SCREAMING_SNAKE_CASE = os.path.join( __a , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__a ): copyfile(self.vocab_file , __a ) return (out_vocab_file,)
711
'''simple docstring''' 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 UpperCamelCase__ : List[Any] = logging.get_logger(__name__) UpperCamelCase__ : Any = "▁" UpperCamelCase__ : Any = {"vocab_file": "spiece.model"} UpperCamelCase__ : int = { "vocab_file": { "google/reformer-crime-and-punishment": ( "https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/spiece.model" ) } } UpperCamelCase__ : Optional[int] = { "google/reformer-crime-and-punishment": 524_288, } class _a (_lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE = ['input_ids', 'attention_mask'] def __init__( self , A__ , A__="</s>" , A__="<unk>" , A__=[] , A__ = None , **A__ , ) -> None: _SCREAMING_SNAKE_CASE = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( eos_token=A__ , unk_token=A__ , additional_special_tokens=A__ , sp_model_kwargs=self.sp_model_kwargs , **A__ , ) _SCREAMING_SNAKE_CASE = vocab_file _SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(A__ ) @property def UpperCamelCase ( self ) -> Any: return self.sp_model.get_piece_size() def UpperCamelCase ( self ) -> Dict[str, int]: _SCREAMING_SNAKE_CASE = {self.convert_ids_to_tokens(A__ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ) -> int: _SCREAMING_SNAKE_CASE = self.__dict__.copy() _SCREAMING_SNAKE_CASE = None return state def __setstate__( self , A__ ) -> str: _SCREAMING_SNAKE_CASE = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): _SCREAMING_SNAKE_CASE = {} _SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCamelCase ( self , A__ ) -> List[str]: return self.sp_model.encode(A__ , out_type=A__ ) def UpperCamelCase ( self , A__ ) -> Union[str, Any]: return self.sp_model.piece_to_id(A__ ) def UpperCamelCase ( self , A__ ) -> List[Any]: if index < self.sp_model.get_piece_size(): _SCREAMING_SNAKE_CASE = self.sp_model.IdToPiece(A__ ) return token def UpperCamelCase ( self , A__ ) -> str: _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = """""" 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(A__ ) + token _SCREAMING_SNAKE_CASE = [] else: current_sub_tokens.append(A__ ) out_string += self.sp_model.decode(A__ ) return out_string.strip() def UpperCamelCase ( self , A__ , A__ = None ) -> Tuple[str]: if not os.path.isdir(A__ ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return _SCREAMING_SNAKE_CASE = os.path.join( A__ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(A__ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , A__ ) elif not os.path.isfile(self.vocab_file ): with open(A__ , """wb""" ) as fi: _SCREAMING_SNAKE_CASE = self.sp_model.serialized_model_proto() fi.write(A__ ) return (out_vocab_file,)
0
0
'''simple docstring''' import math def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" if ( not isinstance(lowerCamelCase_ , (int, float) ) or power_factor < -1 or power_factor > 1 ): raise ValueError("""power_factor must be a valid float value between -1 and 1.""" ) return apparent_power * power_factor def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[Any]: """simple docstring""" if ( not isinstance(lowerCamelCase_ , (int, float) ) or power_factor < -1 or power_factor > 1 ): raise ValueError("""power_factor must be a valid float value between -1 and 1.""" ) return apparent_power * math.sqrt(1 - power_factor**2 ) if __name__ == "__main__": import doctest doctest.testmod()
712
'''simple docstring''' import os import unittest from transformers import MobileBertTokenizer, MobileBertTokenizerFast from transformers.models.bert.tokenization_bert import ( VOCAB_FILES_NAMES, BasicTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin, filter_non_english @require_tokenizers class _a (_lowerCamelCase , unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = MobileBertTokenizer SCREAMING_SNAKE_CASE = MobileBertTokenizerFast SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = filter_non_english SCREAMING_SNAKE_CASE = 'google/mobilebert-uncased' def UpperCamelCase ( self ) -> Any: super().setUp() _SCREAMING_SNAKE_CASE = [ """[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest""", ] _SCREAMING_SNAKE_CASE = 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] ) ) _SCREAMING_SNAKE_CASE = [ (tokenizer_def[0], self.pre_trained_model_path, tokenizer_def[2]) # else the 'google/' prefix is stripped for tokenizer_def in self.tokenizers_list ] def UpperCamelCase ( self , A__ ) -> List[str]: _SCREAMING_SNAKE_CASE = """UNwant\u00E9d,running""" _SCREAMING_SNAKE_CASE = """unwanted, running""" return input_text, output_text def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = self.tokenizer_class(self.vocab_file ) _SCREAMING_SNAKE_CASE = tokenizer.tokenize("""UNwant\u00E9d,running""" ) self.assertListEqual(A__ , ["""un""", """##want""", """##ed""", """,""", """runn""", """##ing"""] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(A__ ) , [9, 6, 7, 12, 10, 11] ) def UpperCamelCase ( self ) -> Optional[int]: if not self.test_rust_tokenizer: return _SCREAMING_SNAKE_CASE = self.get_tokenizer() _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() _SCREAMING_SNAKE_CASE = """UNwant\u00E9d,running""" _SCREAMING_SNAKE_CASE = tokenizer.tokenize(A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.tokenize(A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = tokenizer.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.encode(A__ , add_special_tokens=A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() _SCREAMING_SNAKE_CASE = tokenizer.encode(A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.encode(A__ ) self.assertListEqual(A__ , A__ ) # With lower casing _SCREAMING_SNAKE_CASE = self.get_tokenizer(do_lower_case=A__ ) _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer(do_lower_case=A__ ) _SCREAMING_SNAKE_CASE = """UNwant\u00E9d,running""" _SCREAMING_SNAKE_CASE = tokenizer.tokenize(A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.tokenize(A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = tokenizer.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.encode(A__ , add_special_tokens=A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() _SCREAMING_SNAKE_CASE = tokenizer.encode(A__ ) _SCREAMING_SNAKE_CASE = rust_tokenizer.encode(A__ ) self.assertListEqual(A__ , A__ ) def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = BasicTokenizer() self.assertListEqual(tokenizer.tokenize("""ah\u535A\u63A8zz""" ) , ["""ah""", """\u535A""", """\u63A8""", """zz"""] ) def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHeLLo!how \n Are yoU? """ ) , ["""hello""", """!""", """how""", """are""", """you""", """?"""] ) self.assertListEqual(tokenizer.tokenize("""H\u00E9llo""" ) , ["""hello"""] ) def UpperCamelCase ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , strip_accents=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""hällo""", """!""", """how""", """are""", """you""", """?"""] ) self.assertListEqual(tokenizer.tokenize("""H\u00E9llo""" ) , ["""h\u00E9llo"""] ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , strip_accents=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""hallo""", """!""", """how""", """are""", """you""", """?"""] ) self.assertListEqual(tokenizer.tokenize("""H\u00E9llo""" ) , ["""hello"""] ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""hallo""", """!""", """how""", """are""", """you""", """?"""] ) self.assertListEqual(tokenizer.tokenize("""H\u00E9llo""" ) , ["""hello"""] ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHeLLo!how \n Are yoU? """ ) , ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?"""] ) def UpperCamelCase ( self ) -> Dict: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , strip_accents=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""HäLLo""", """!""", """how""", """Are""", """yoU""", """?"""] ) def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , strip_accents=A__ ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""HaLLo""", """!""", """how""", """Are""", """yoU""", """?"""] ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = BasicTokenizer(do_lower_case=A__ , never_split=["""[UNK]"""] ) self.assertListEqual( tokenizer.tokenize(""" \tHeLLo!how \n Are yoU? [UNK]""" ) , ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?""", """[UNK]"""] ) def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = ["""[UNK]""", """[CLS]""", """[SEP]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing"""] _SCREAMING_SNAKE_CASE = {} for i, token in enumerate(A__ ): _SCREAMING_SNAKE_CASE = i _SCREAMING_SNAKE_CASE = WordpieceTokenizer(vocab=A__ , unk_token="""[UNK]""" ) self.assertListEqual(tokenizer.tokenize("""""" ) , [] ) self.assertListEqual(tokenizer.tokenize("""unwanted running""" ) , ["""un""", """##want""", """##ed""", """runn""", """##ing"""] ) self.assertListEqual(tokenizer.tokenize("""unwantedX running""" ) , ["""[UNK]""", """runn""", """##ing"""] ) def UpperCamelCase ( self ) -> str: self.assertTrue(_is_whitespace(""" """ ) ) self.assertTrue(_is_whitespace("""\t""" ) ) self.assertTrue(_is_whitespace("""\r""" ) ) self.assertTrue(_is_whitespace("""\n""" ) ) self.assertTrue(_is_whitespace("""\u00A0""" ) ) self.assertFalse(_is_whitespace("""A""" ) ) self.assertFalse(_is_whitespace("""-""" ) ) def UpperCamelCase ( self ) -> Union[str, Any]: self.assertTrue(_is_control("""\u0005""" ) ) self.assertFalse(_is_control("""A""" ) ) self.assertFalse(_is_control(""" """ ) ) self.assertFalse(_is_control("""\t""" ) ) self.assertFalse(_is_control("""\r""" ) ) def UpperCamelCase ( self ) -> Dict: self.assertTrue(_is_punctuation("""-""" ) ) self.assertTrue(_is_punctuation("""$""" ) ) self.assertTrue(_is_punctuation("""`""" ) ) self.assertTrue(_is_punctuation(""".""" ) ) self.assertFalse(_is_punctuation("""A""" ) ) self.assertFalse(_is_punctuation(""" """ ) ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = self.get_tokenizer() _SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() # Example taken from the issue https://github.com/huggingface/tokenizers/issues/340 self.assertListEqual([tokenizer.tokenize(A__ ) for t in ["""Test""", """\xad""", """test"""]] , [["""[UNK]"""], [], ["""[UNK]"""]] ) self.assertListEqual( [rust_tokenizer.tokenize(A__ ) for t in ["""Test""", """\xad""", """test"""]] , [["""[UNK]"""], [], ["""[UNK]"""]] ) @slow def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained("""google/mobilebert-uncased""" ) _SCREAMING_SNAKE_CASE = tokenizer.encode("""sequence builders""" , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer.encode("""multi-sequence build""" , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer.build_inputs_with_special_tokens(A__ ) _SCREAMING_SNAKE_CASE = tokenizer.build_inputs_with_special_tokens(A__ , A__ ) assert encoded_sentence == [1_01] + text + [1_02] assert encoded_pair == [1_01] + text + [1_02] + text_a + [1_02] def UpperCamelCase ( self ) -> List[str]: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})" ): _SCREAMING_SNAKE_CASE = self.rust_tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = F"A, naïve {tokenizer_r.mask_token} AllenNLP sentence." _SCREAMING_SNAKE_CASE = tokenizer_r.encode_plus( A__ , return_attention_mask=A__ , return_token_type_ids=A__ , return_offsets_mapping=A__ , add_special_tokens=A__ , ) _SCREAMING_SNAKE_CASE = tokenizer_r.do_lower_case if hasattr(A__ , """do_lower_case""" ) else False _SCREAMING_SNAKE_CASE = ( [ ((0, 0), tokenizer_r.cls_token), ((0, 1), """A"""), ((1, 2), ""","""), ((3, 5), """na"""), ((5, 6), """##ï"""), ((6, 8), """##ve"""), ((9, 15), tokenizer_r.mask_token), ((16, 21), """Allen"""), ((21, 23), """##NL"""), ((23, 24), """##P"""), ((25, 33), """sentence"""), ((33, 34), """."""), ((0, 0), tokenizer_r.sep_token), ] if not do_lower_case else [ ((0, 0), tokenizer_r.cls_token), ((0, 1), """a"""), ((1, 2), ""","""), ((3, 8), """naive"""), ((9, 15), tokenizer_r.mask_token), ((16, 21), """allen"""), ((21, 23), """##nl"""), ((23, 24), """##p"""), ((25, 33), """sentence"""), ((33, 34), """."""), ((0, 0), tokenizer_r.sep_token), ] ) self.assertEqual( [e[1] for e in expected_results] , tokenizer_r.convert_ids_to_tokens(tokens["""input_ids"""] ) ) self.assertEqual([e[0] for e in expected_results] , tokens["""offset_mapping"""] ) def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = ["""的""", """人""", """有"""] _SCREAMING_SNAKE_CASE = """""".join(A__ ) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})" ): _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = self.rust_tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = tokenizer_p.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer_r.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer_r.convert_ids_to_tokens(A__ ) _SCREAMING_SNAKE_CASE = tokenizer_p.convert_ids_to_tokens(A__ ) # it is expected that each Chinese character is not preceded by "##" self.assertListEqual(A__ , A__ ) self.assertListEqual(A__ , A__ ) _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = self.rust_tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained(A__ , **A__ ) _SCREAMING_SNAKE_CASE = tokenizer_r.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer_p.encode(A__ , add_special_tokens=A__ ) _SCREAMING_SNAKE_CASE = tokenizer_r.convert_ids_to_tokens(A__ ) _SCREAMING_SNAKE_CASE = tokenizer_p.convert_ids_to_tokens(A__ ) # it is expected that only the first Chinese character is not preceded by "##". _SCREAMING_SNAKE_CASE = [ F"##{token}" if idx != 0 else token for idx, token in enumerate(A__ ) ] self.assertListEqual(A__ , A__ ) self.assertListEqual(A__ , A__ )
0
0
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEPipeline from diffusers.pipelines.shap_e import ShapERenderer from diffusers.utils import load_numpy, slow from diffusers.utils.testing_utils import require_torch_gpu, torch_device from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference class _a (__lowerCamelCase , unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = ShapEPipeline SCREAMING_SNAKE_CASE = ['prompt'] SCREAMING_SNAKE_CASE = ['prompt'] SCREAMING_SNAKE_CASE = [ 'num_images_per_prompt', 'num_inference_steps', 'generator', 'latents', 'guidance_scale', 'frame_size', 'output_type', 'return_dict', ] SCREAMING_SNAKE_CASE = False @property def UpperCamelCase ( self ) -> str: return 32 @property def UpperCamelCase ( self ) -> str: return 32 @property def UpperCamelCase ( self ) -> Union[str, Any]: return self.time_input_dim * 4 @property def UpperCamelCase ( self ) -> Dict: return 8 @property def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) return tokenizer @property def UpperCamelCase ( self ) -> Optional[Any]: torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=10_00 , ) return CLIPTextModelWithProjection(UpperCamelCase_ ) @property def UpperCamelCase ( self ) -> List[Any]: torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE = { """num_attention_heads""": 2, """attention_head_dim""": 16, """embedding_dim""": self.time_input_dim, """num_embeddings""": 32, """embedding_proj_dim""": self.text_embedder_hidden_size, """time_embed_dim""": self.time_embed_dim, """num_layers""": 1, """clip_embed_dim""": self.time_input_dim * 2, """additional_embeddings""": 0, """time_embed_act_fn""": """gelu""", """norm_in_type""": """layer""", """encoder_hid_proj_type""": None, """added_emb_type""": None, } _SCREAMING_SNAKE_CASE = PriorTransformer(**UpperCamelCase_ ) return model @property def UpperCamelCase ( self ) -> Optional[int]: torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE = { """param_shapes""": ( (self.renderer_dim, 93), (self.renderer_dim, 8), (self.renderer_dim, 8), (self.renderer_dim, 8), ), """d_latent""": self.time_input_dim, """d_hidden""": self.renderer_dim, """n_output""": 12, """background""": ( 0.1, 0.1, 0.1, ), } _SCREAMING_SNAKE_CASE = ShapERenderer(**UpperCamelCase_ ) return model def UpperCamelCase ( self ) -> Tuple: _SCREAMING_SNAKE_CASE = self.dummy_prior _SCREAMING_SNAKE_CASE = self.dummy_text_encoder _SCREAMING_SNAKE_CASE = self.dummy_tokenizer _SCREAMING_SNAKE_CASE = self.dummy_renderer _SCREAMING_SNAKE_CASE = HeunDiscreteScheduler( beta_schedule="""exp""" , num_train_timesteps=10_24 , prediction_type="""sample""" , use_karras_sigmas=UpperCamelCase_ , clip_sample=UpperCamelCase_ , clip_sample_range=1.0 , ) _SCREAMING_SNAKE_CASE = { """prior""": prior, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """renderer""": renderer, """scheduler""": scheduler, } return components def UpperCamelCase ( self , A__ , A__=0 ) -> Dict: 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""": """horse""", """generator""": generator, """num_inference_steps""": 1, """frame_size""": 32, """output_type""": """np""", } return inputs def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = """cpu""" _SCREAMING_SNAKE_CASE = self.get_dummy_components() _SCREAMING_SNAKE_CASE = self.pipeline_class(**UpperCamelCase_ ) _SCREAMING_SNAKE_CASE = pipe.to(UpperCamelCase_ ) pipe.set_progress_bar_config(disable=UpperCamelCase_ ) _SCREAMING_SNAKE_CASE = pipe(**self.get_dummy_inputs(UpperCamelCase_ ) ) _SCREAMING_SNAKE_CASE = output.images[0] _SCREAMING_SNAKE_CASE = image[0, -3:, -3:, -1] assert image.shape == (20, 32, 32, 3) _SCREAMING_SNAKE_CASE = np.array( [ 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def UpperCamelCase ( self ) -> Any: # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches self._test_inference_batch_consistent(batch_sizes=[1, 2] ) def UpperCamelCase ( self ) -> str: _SCREAMING_SNAKE_CASE = torch_device == """cpu""" _SCREAMING_SNAKE_CASE = True self._test_inference_batch_single_identical( batch_size=2 , test_max_difference=UpperCamelCase_ , relax_max_difference=UpperCamelCase_ , ) def UpperCamelCase ( self ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.get_dummy_components() _SCREAMING_SNAKE_CASE = self.pipeline_class(**UpperCamelCase_ ) _SCREAMING_SNAKE_CASE = pipe.to(UpperCamelCase_ ) pipe.set_progress_bar_config(disable=UpperCamelCase_ ) _SCREAMING_SNAKE_CASE = 1 _SCREAMING_SNAKE_CASE = 2 _SCREAMING_SNAKE_CASE = self.get_dummy_inputs(UpperCamelCase_ ) for key in inputs.keys(): if key in self.batch_params: _SCREAMING_SNAKE_CASE = batch_size * [inputs[key]] _SCREAMING_SNAKE_CASE = pipe(**UpperCamelCase_ , num_images_per_prompt=UpperCamelCase_ )[0] assert images.shape[0] == batch_size * num_images_per_prompt @slow @require_torch_gpu class _a (unittest.TestCase): """simple docstring""" def UpperCamelCase ( self ) -> Tuple: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase ( self ) -> Dict: _SCREAMING_SNAKE_CASE = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/shap_e/test_shap_e_np_out.npy""" ) _SCREAMING_SNAKE_CASE = ShapEPipeline.from_pretrained("""openai/shap-e""" ) _SCREAMING_SNAKE_CASE = pipe.to(UpperCamelCase_ ) pipe.set_progress_bar_config(disable=UpperCamelCase_ ) _SCREAMING_SNAKE_CASE = torch.Generator(device=UpperCamelCase_ ).manual_seed(0 ) _SCREAMING_SNAKE_CASE = pipe( """a shark""" , generator=UpperCamelCase_ , guidance_scale=15.0 , num_inference_steps=64 , frame_size=64 , output_type="""np""" , ).images[0] assert images.shape == (20, 64, 64, 3) assert_mean_pixel_difference(UpperCamelCase_ , UpperCamelCase_ )
713
'''simple docstring''' import logging import os import quant_trainer import torch from torch.utils.data import DataLoader from transformers import Trainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput UpperCamelCase__ : Tuple = logging.getLogger(__name__) if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class _a (_lowerCamelCase): """simple docstring""" def __init__( self , *A__ , A__=None , A__=None , A__=None , **A__ ) -> Optional[int]: super().__init__(*A__ , **A__ ) _SCREAMING_SNAKE_CASE = eval_examples _SCREAMING_SNAKE_CASE = post_process_function _SCREAMING_SNAKE_CASE = quant_trainer_args _SCREAMING_SNAKE_CASE = 1_28 # default number of calibration samples def UpperCamelCase ( self , A__=None ) -> Union[str, Any]: if calib_dataset is None and self.calib_dataset is None: raise ValueError("""Trainer: calibration requires an calib_dataset.""" ) _SCREAMING_SNAKE_CASE = calib_dataset if calib_dataset is not None else self.calib_dataset _SCREAMING_SNAKE_CASE = self._remove_unused_columns(A__ , description="""Calibration""" ) return DataLoader( A__ , batch_size=self.args.eval_batch_size , collate_fn=self.data_collator , drop_last=self.args.dataloader_drop_last , num_workers=self.args.dataloader_num_workers , pin_memory=self.args.dataloader_pin_memory , shuffle=A__ , ) def UpperCamelCase ( self , A__=None ) -> str: _SCREAMING_SNAKE_CASE = self.train_dataset if calib_dataset is None else calib_dataset _SCREAMING_SNAKE_CASE = self.get_calib_dataloader(A__ ) _SCREAMING_SNAKE_CASE = self.model quant_trainer.configure_model(A__ , self.quant_trainer_args , calib=A__ ) model.eval() quant_trainer.enable_calibration(A__ ) logger.info("""***** Running calibration *****""" ) logger.info(F" Num examples = {self.calib_num}" ) logger.info(F" Batch size = {calib_dataloader.batch_size}" ) for step, inputs in enumerate(A__ ): # Prediction step _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self.prediction_step(A__ , A__ , prediction_loss_only=A__ ) if (step + 1) * calib_dataloader.batch_size >= self.calib_num: break quant_trainer.finish_calibration(A__ , self.quant_trainer_args ) _SCREAMING_SNAKE_CASE = model def UpperCamelCase ( self , A__=None , A__=None , A__=None , A__ = "eval" ) -> List[Any]: _SCREAMING_SNAKE_CASE = self.eval_dataset if eval_dataset is None else eval_dataset _SCREAMING_SNAKE_CASE = self.get_eval_dataloader(A__ ) _SCREAMING_SNAKE_CASE = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. _SCREAMING_SNAKE_CASE = self.compute_metrics _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: _SCREAMING_SNAKE_CASE = eval_loop( A__ , description="""Evaluation""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=A__ , ) finally: _SCREAMING_SNAKE_CASE = compute_metrics if self.post_process_function is not None and self.compute_metrics is not None: _SCREAMING_SNAKE_CASE = self.post_process_function(A__ , A__ , output.predictions ) _SCREAMING_SNAKE_CASE = self.compute_metrics(A__ ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(F"{metric_key_prefix}_" ): _SCREAMING_SNAKE_CASE = metrics.pop(A__ ) self.log(A__ ) else: _SCREAMING_SNAKE_CASE = {} if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report() ) _SCREAMING_SNAKE_CASE = self.callback_handler.on_evaluate(self.args , self.state , self.control , A__ ) return metrics def UpperCamelCase ( self , A__ , A__ , A__=None , A__ = "test" ) -> List[str]: _SCREAMING_SNAKE_CASE = self.get_test_dataloader(A__ ) # Temporarily disable metric computation, we will do it in the loop here. _SCREAMING_SNAKE_CASE = self.compute_metrics _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: _SCREAMING_SNAKE_CASE = eval_loop( A__ , description="""Prediction""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=A__ , ) finally: _SCREAMING_SNAKE_CASE = compute_metrics if self.post_process_function is None or self.compute_metrics is None: return output _SCREAMING_SNAKE_CASE = self.post_process_function(A__ , A__ , output.predictions , """predict""" ) _SCREAMING_SNAKE_CASE = self.compute_metrics(A__ ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(F"{metric_key_prefix}_" ): _SCREAMING_SNAKE_CASE = metrics.pop(A__ ) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=A__ ) def UpperCamelCase ( self , A__="./" ) -> Tuple: _SCREAMING_SNAKE_CASE = self.eval_dataset _SCREAMING_SNAKE_CASE = self.get_eval_dataloader(A__ ) _SCREAMING_SNAKE_CASE = next(iter(A__ ) ) # saving device - to make it consistent _SCREAMING_SNAKE_CASE = torch.device("""cuda""" if torch.cuda.is_available() else """cpu""" ) # convert to tuple _SCREAMING_SNAKE_CASE = tuple(v.to(A__ ) for k, v in batch.items() ) logger.info("""Converting model to be onnx compatible""" ) from pytorch_quantization.nn import TensorQuantizer _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = self.model.to(A__ ) model.eval() model.float() _SCREAMING_SNAKE_CASE = model.module if hasattr(A__ , """module""" ) else model quant_trainer.configure_model(A__ , self.quant_trainer_args ) _SCREAMING_SNAKE_CASE = os.path.join(A__ , """model.onnx""" ) logger.info(F"exporting model to {output_model_file}" ) _SCREAMING_SNAKE_CASE = {0: """batch_size""", 1: """seq_len"""} torch.onnx.export( A__ , A__ , A__ , export_params=A__ , opset_version=13 , do_constant_folding=A__ , input_names=["""input_ids""", """attention_mask""", """token_type_ids"""] , output_names=["""output_start_logits""", """output_end_logits"""] , dynamic_axes={ """input_ids""": axes, """attention_mask""": axes, """token_type_ids""": axes, """output_start_logits""": axes, """output_end_logits""": axes, } , verbose=A__ , ) logger.info("""onnx export finished""" )
0
0