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
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class _a (unittest.TestCase): """simple docstring""" def __init__( self , A__ , A__=13 , A__=3 , A__=2_24 , A__=30 , A__=4_00 , A__=True , A__=None , A__=True , A__=[0.5, 0.5, 0.5] , A__=[0.5, 0.5, 0.5] , ) -> Optional[int]: _SCREAMING_SNAKE_CASE = size if size is not None else {"""height""": 18, """width""": 18} _SCREAMING_SNAKE_CASE = parent _SCREAMING_SNAKE_CASE = batch_size _SCREAMING_SNAKE_CASE = num_channels _SCREAMING_SNAKE_CASE = image_size _SCREAMING_SNAKE_CASE = min_resolution _SCREAMING_SNAKE_CASE = max_resolution _SCREAMING_SNAKE_CASE = do_resize _SCREAMING_SNAKE_CASE = size _SCREAMING_SNAKE_CASE = do_normalize _SCREAMING_SNAKE_CASE = image_mean _SCREAMING_SNAKE_CASE = image_std def UpperCamelCase ( self ) -> List[str]: return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class _a (_lowerCamelCase , unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = ViTImageProcessor if is_vision_available() else None def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = EfficientFormerImageProcessorTester(self ) @property def UpperCamelCase ( self ) -> List[str]: return self.image_proc_tester.prepare_image_processor_dict() def UpperCamelCase ( self ) -> Any: _SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A__ , """image_mean""" ) ) self.assertTrue(hasattr(A__ , """image_std""" ) ) self.assertTrue(hasattr(A__ , """do_normalize""" ) ) self.assertTrue(hasattr(A__ , """do_resize""" ) ) self.assertTrue(hasattr(A__ , """size""" ) ) def UpperCamelCase ( self ) -> Dict: pass def UpperCamelCase ( self ) -> int: # Initialize image_processor _SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_proc_tester , equal_resolution=A__ ) for image in image_inputs: self.assertIsInstance(A__ , Image.Image ) # Test not batched input _SCREAMING_SNAKE_CASE = image_processor(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_proc_tester.num_channels, self.image_proc_tester.size["""height"""], self.image_proc_tester.size["""width"""], ) , ) # Test batched _SCREAMING_SNAKE_CASE = image_processor(A__ , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_proc_tester.batch_size, self.image_proc_tester.num_channels, self.image_proc_tester.size["""height"""], self.image_proc_tester.size["""width"""], ) , ) def UpperCamelCase ( self ) -> Dict: # Initialize image_processor _SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_proc_tester , equal_resolution=A__ , numpify=A__ ) for image in image_inputs: self.assertIsInstance(A__ , np.ndarray ) # Test not batched input _SCREAMING_SNAKE_CASE = image_processor(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_proc_tester.num_channels, self.image_proc_tester.size["""height"""], self.image_proc_tester.size["""width"""], ) , ) # Test batched _SCREAMING_SNAKE_CASE = image_processor(A__ , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_proc_tester.batch_size, self.image_proc_tester.num_channels, self.image_proc_tester.size["""height"""], self.image_proc_tester.size["""width"""], ) , ) def UpperCamelCase ( self ) -> int: # Initialize image_processor _SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_proc_tester , equal_resolution=A__ , torchify=A__ ) for image in image_inputs: self.assertIsInstance(A__ , torch.Tensor ) # Test not batched input _SCREAMING_SNAKE_CASE = image_processor(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_proc_tester.num_channels, self.image_proc_tester.size["""height"""], self.image_proc_tester.size["""width"""], ) , ) # Test batched _SCREAMING_SNAKE_CASE = image_processor(A__ , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_proc_tester.batch_size, self.image_proc_tester.num_channels, self.image_proc_tester.size["""height"""], self.image_proc_tester.size["""width"""], ) , )
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_ ) -> str: """simple docstring""" if not all(char in """01""" for char in bin_string ): raise ValueError("""Non-binary value was passed to the function""" ) if not bin_string: raise ValueError("""Empty string was passed to the function""" ) _SCREAMING_SNAKE_CASE = """""" while len(SCREAMING_SNAKE_CASE_ ) % 3 != 0: _SCREAMING_SNAKE_CASE = """0""" + bin_string _SCREAMING_SNAKE_CASE = [ bin_string[index : index + 3] for index in range(len(SCREAMING_SNAKE_CASE_ ) ) if index % 3 == 0 ] for bin_group in bin_string_in_3_list: _SCREAMING_SNAKE_CASE = 0 for index, val in enumerate(SCREAMING_SNAKE_CASE_ ): oct_val += int(2 ** (2 - index) * int(SCREAMING_SNAKE_CASE_ ) ) oct_string += str(SCREAMING_SNAKE_CASE_ ) return oct_string 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
from ..utils import DummyObject, requires_backends class _a (metaclass=_lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = ['note_seq'] def __init__( self , *A__ , **A__ ) -> int: requires_backends(self , ["""note_seq"""] ) @classmethod def UpperCamelCase ( cls , *A__ , **A__ ) -> Union[str, Any]: requires_backends(cls , ["""note_seq"""] ) @classmethod def UpperCamelCase ( cls , *A__ , **A__ ) -> int: requires_backends(cls , ["""note_seq"""] )
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''' def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE = 1 for i in range(1 , num + 1 ): fact *= i return fact def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE = 0 while number > 0: _SCREAMING_SNAKE_CASE = number % 10 sum_of_digits += last_digit _SCREAMING_SNAKE_CASE = number // 10 # Removing the last_digit from the given number return sum_of_digits def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ = 1_00 ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE = factorial(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = split_and_add(SCREAMING_SNAKE_CASE_ ) return result if __name__ == "__main__": print(solution(int(input("Enter the Number: ").strip())))
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 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 )
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 def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , ) -> None: """simple docstring""" _SCREAMING_SNAKE_CASE = len(SCREAMING_SNAKE_CASE_ ) # If row is equal to the size of the board it means there are a queen in each row in # the current board (possible_board) if row == n: # We convert the variable possible_board that looks like this: [1, 3, 0, 2] to # this: ['. Q . . ', '. . . Q ', 'Q . . . ', '. . Q . '] boards.append([""". """ * i + """Q """ + """. """ * (n - 1 - i) for i in possible_board] ) return # We iterate each column in the row to find all possible results in each row for col in range(SCREAMING_SNAKE_CASE_ ): # We apply that we learned previously. First we check that in the current board # (possible_board) there are not other same value because if there is it means # that there are a collision in vertical. Then we apply the two formulas we # learned before: # # 45º: y - x = b or 45: row - col = b # 135º: y + x = b or row + col = b. # # And we verify if the results of this two formulas not exist in their variables # respectively. (diagonal_right_collisions, diagonal_left_collisions) # # If any or these are True it means there is a collision so we continue to the # next value in the for loop. if ( col in possible_board or row - col in diagonal_right_collisions or row + col in diagonal_left_collisions ): continue # If it is False we call dfs function again and we update the inputs depth_first_search( [*possible_board, col] , [*diagonal_right_collisions, row - col] , [*diagonal_left_collisions, row + col] , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> None: """simple docstring""" _SCREAMING_SNAKE_CASE = [] depth_first_search([] , [] , [] , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # Print all the boards for board in boards: for column in board: print(SCREAMING_SNAKE_CASE_ ) print("""""" ) print(len(SCREAMING_SNAKE_CASE_ ) , """solutions were found.""" ) if __name__ == "__main__": import doctest doctest.testmod() n_queens_solution(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''' import argparse import torch from transformers import GPTaLMHeadModel, RobertaForMaskedLM if __name__ == "__main__": UpperCamelCase__ = 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__ = parser.parse_args() if args.model_type == "roberta": UpperCamelCase__ = RobertaForMaskedLM.from_pretrained(args.model_name) UpperCamelCase__ = "roberta" elif args.model_type == "gpt2": UpperCamelCase__ = GPTaLMHeadModel.from_pretrained(args.model_name) UpperCamelCase__ = "transformer" UpperCamelCase__ = model.state_dict() UpperCamelCase__ = {} # Embeddings # if args.model_type == "gpt2": for param_name in ["wte.weight", "wpe.weight"]: UpperCamelCase__ = state_dict[f"""{prefix}.{param_name}"""] else: for w in ["word_embeddings", "position_embeddings", "token_type_embeddings"]: UpperCamelCase__ = f"""{prefix}.embeddings.{w}.weight""" UpperCamelCase__ = state_dict[param_name] for w in ["weight", "bias"]: UpperCamelCase__ = f"""{prefix}.embeddings.LayerNorm.{w}""" UpperCamelCase__ = state_dict[param_name] # Transformer Blocks # UpperCamelCase__ = 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__ = state_dict[ f"""{prefix}.h.{teacher_idx}.{layer}.{w}""" ] UpperCamelCase__ = 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__ = 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__ = state_dict[f"""{layer}"""] if args.vocab_transform: for w in ["weight", "bias"]: UpperCamelCase__ = state_dict[f"""lm_head.dense.{w}"""] UpperCamelCase__ = state_dict[f"""lm_head.layer_norm.{w}"""] elif args.model_type == "gpt2": for w in ["weight", "bias"]: UpperCamelCase__ = state_dict[f"""{prefix}.ln_f.{w}"""] UpperCamelCase__ = 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)
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''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCamelCase__ : int = { "configuration_x_clip": [ "XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP", "XCLIPConfig", "XCLIPTextConfig", "XCLIPVisionConfig", ], "processing_x_clip": ["XCLIPProcessor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : List[Any] = [ "XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "XCLIPModel", "XCLIPPreTrainedModel", "XCLIPTextModel", "XCLIPVisionModel", ] if TYPE_CHECKING: from .configuration_x_clip import ( XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, XCLIPConfig, XCLIPTextConfig, XCLIPVisionConfig, ) from .processing_x_clip import XCLIPProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_x_clip import ( XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST, XCLIPModel, XCLIPPreTrainedModel, XCLIPTextModel, XCLIPVisionModel, ) else: import sys UpperCamelCase__ : List[str] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
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 typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCamelCase__ : Optional[Any] = { "configuration_swinv2": ["SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP", "Swinv2Config"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : str = [ "SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST", "Swinv2ForImageClassification", "Swinv2ForMaskedImageModeling", "Swinv2Model", "Swinv2PreTrainedModel", ] if TYPE_CHECKING: from .configuration_swinva import SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinvaConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swinva import ( SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST, SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel, SwinvaPreTrainedModel, ) else: import sys UpperCamelCase__ : Union[str, Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
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 List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_herbert import HerbertTokenizer UpperCamelCase__ : int = logging.get_logger(__name__) UpperCamelCase__ : List[str] = {"vocab_file": "vocab.json", "merges_file": "merges.txt", "tokenizer_file": "tokenizer.json"} UpperCamelCase__ : str = { "vocab_file": { "allegro/herbert-base-cased": "https://huggingface.co/allegro/herbert-base-cased/resolve/main/vocab.json" }, "merges_file": { "allegro/herbert-base-cased": "https://huggingface.co/allegro/herbert-base-cased/resolve/main/merges.txt" }, } UpperCamelCase__ : Optional[Any] = {"allegro/herbert-base-cased": 514} UpperCamelCase__ : List[str] = {} class _a (_lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE = PRETRAINED_INIT_CONFIGURATION SCREAMING_SNAKE_CASE = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE = HerbertTokenizer def __init__( self , A__=None , A__=None , A__=None , A__="<s>" , A__="<unk>" , A__="<pad>" , A__="<mask>" , A__="</s>" , **A__ , ) -> List[str]: super().__init__( A__ , A__ , tokenizer_file=A__ , cls_token=A__ , unk_token=A__ , pad_token=A__ , mask_token=A__ , sep_token=A__ , **A__ , ) def UpperCamelCase ( self , A__ , A__ = None ) -> List[int]: _SCREAMING_SNAKE_CASE = [self.cls_token_id] _SCREAMING_SNAKE_CASE = [self.sep_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def UpperCamelCase ( self , A__ , A__ = None , A__ = False ) -> List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=A__ , token_ids_a=A__ , already_has_special_tokens=A__ ) if token_ids_a is None: return [1] + ([0] * len(A__ )) + [1] return [1] + ([0] * len(A__ )) + [1] + ([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]: _SCREAMING_SNAKE_CASE = self._tokenizer.model.save(A__ , name=A__ ) return tuple(A__ )
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 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
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''' 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)
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 typing import Any, Dict, List, Optional, Tuple, Union import torch from torch import nn from torch.utils.data import DistributedSampler, RandomSampler from transformers import PreTrainedModel, Trainer, logging from transformers.integrations import is_fairscale_available from transformers.models.fsmt.configuration_fsmt import FSMTConfig from transformers.optimization import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.trainer_pt_utils import get_tpu_sampler from transformers.training_args import ParallelMode from transformers.utils import is_torch_tpu_available if is_fairscale_available(): from fairscale.optim import OSS UpperCamelCase__ : Dict = logging.get_logger(__name__) UpperCamelCase__ : int = { "linear": get_linear_schedule_with_warmup, "cosine": get_cosine_schedule_with_warmup, "cosine_w_restarts": get_cosine_with_hard_restarts_schedule_with_warmup, "polynomial": get_polynomial_decay_schedule_with_warmup, "constant": get_constant_schedule, "constant_w_warmup": get_constant_schedule_with_warmup, } class _a (_lowerCamelCase): """simple docstring""" def __init__( self , A__=None , A__=None , *A__ , **A__ ) -> List[str]: super().__init__(*A__ , **A__ ) if config is None: assert isinstance(self.model , A__ ), ( "If no `config` is passed the model to be trained has to be of type `PreTrainedModel`, but is" F" {self.model.__class__}" ) _SCREAMING_SNAKE_CASE = self.model.config else: _SCREAMING_SNAKE_CASE = config _SCREAMING_SNAKE_CASE = data_args _SCREAMING_SNAKE_CASE = self.config.tgt_vocab_size if isinstance(self.config , A__ ) else self.config.vocab_size if self.args.label_smoothing != 0 or (self.data_args is not None and self.data_args.ignore_pad_token_for_loss): assert self.config.pad_token_id is not None, ( "Make sure that `config.pad_token_id` is correcly defined when ignoring `pad_token` for loss" " calculation or doing label smoothing." ) if self.config.pad_token_id is None and self.config.eos_token_id is not None: logger.warning( F"The `config.pad_token_id` is `None`. Using `config.eos_token_id` = {self.config.eos_token_id} for" """ padding..""" ) if self.args.label_smoothing == 0: _SCREAMING_SNAKE_CASE = torch.nn.CrossEntropyLoss(ignore_index=self.config.pad_token_id ) else: # dynamically import label_smoothed_nll_loss from utils import label_smoothed_nll_loss _SCREAMING_SNAKE_CASE = label_smoothed_nll_loss def UpperCamelCase ( self , A__ ) -> Any: if self.optimizer is None: _SCREAMING_SNAKE_CASE = ["""bias""", """LayerNorm.weight"""] _SCREAMING_SNAKE_CASE = [ { """params""": [p for n, p in self.model.named_parameters() if not any(nd in n for nd in no_decay )], """weight_decay""": self.args.weight_decay, }, { """params""": [p for n, p in self.model.named_parameters() if any(nd in n for nd in no_decay )], """weight_decay""": 0.0, }, ] _SCREAMING_SNAKE_CASE = Adafactor if self.args.adafactor else AdamW if self.args.adafactor: _SCREAMING_SNAKE_CASE = Adafactor _SCREAMING_SNAKE_CASE = {"""scale_parameter""": False, """relative_step""": False} else: _SCREAMING_SNAKE_CASE = AdamW _SCREAMING_SNAKE_CASE = { """betas""": (self.args.adam_betaa, self.args.adam_betaa), """eps""": self.args.adam_epsilon, } _SCREAMING_SNAKE_CASE = self.args.learning_rate if self.sharded_ddp: _SCREAMING_SNAKE_CASE = OSS( params=A__ , optim=A__ , **A__ , ) else: _SCREAMING_SNAKE_CASE = optimizer_cls(A__ , **A__ ) if self.lr_scheduler is None: _SCREAMING_SNAKE_CASE = self._get_lr_scheduler(A__ ) else: # ignoring --lr_scheduler logger.warning("""scheduler is passed to `Seq2SeqTrainer`, `--lr_scheduler` arg is ignored.""" ) def UpperCamelCase ( self , A__ ) -> int: _SCREAMING_SNAKE_CASE = arg_to_scheduler[self.args.lr_scheduler] if self.args.lr_scheduler == "constant": _SCREAMING_SNAKE_CASE = schedule_func(self.optimizer ) elif self.args.lr_scheduler == "constant_w_warmup": _SCREAMING_SNAKE_CASE = schedule_func(self.optimizer , num_warmup_steps=self.args.warmup_steps ) else: _SCREAMING_SNAKE_CASE = schedule_func( self.optimizer , num_warmup_steps=self.args.warmup_steps , num_training_steps=A__ ) return scheduler def UpperCamelCase ( self ) -> Optional[torch.utils.data.Sampler]: if isinstance(self.train_dataset , torch.utils.data.IterableDataset ): return None elif is_torch_tpu_available(): return get_tpu_sampler(self.train_dataset ) else: if self.args.sortish_sampler: self.train_dataset.make_sortish_sampler( self.args.per_device_train_batch_size , distributed=(self.args.parallel_mode == ParallelMode.DISTRIBUTED) , ) return ( RandomSampler(self.train_dataset ) if self.args.local_rank == -1 else DistributedSampler(self.train_dataset ) ) def UpperCamelCase ( self , A__ , A__ , A__ ) -> Optional[int]: if self.args.label_smoothing == 0: if self.data_args is not None and self.data_args.ignore_pad_token_for_loss: # force training to ignore pad token _SCREAMING_SNAKE_CASE = model(**A__ , use_cache=A__ )[0] _SCREAMING_SNAKE_CASE = self.loss_fn(logits.view(-1 , logits.shape[-1] ) , labels.view(-1 ) ) else: # compute usual loss via models _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = model(**A__ , labels=A__ , use_cache=A__ )[:2] else: # compute label smoothed loss _SCREAMING_SNAKE_CASE = model(**A__ , use_cache=A__ )[0] _SCREAMING_SNAKE_CASE = torch.nn.functional.log_softmax(A__ , dim=-1 ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self.loss_fn(A__ , A__ , self.args.label_smoothing , ignore_index=self.config.pad_token_id ) return loss, logits def UpperCamelCase ( self , A__ , A__ ) -> Optional[int]: _SCREAMING_SNAKE_CASE = inputs.pop("""labels""" ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self._compute_loss(A__ , A__ , A__ ) return loss def UpperCamelCase ( self , A__ , A__ , A__ , A__ = None , ) -> Tuple[Optional[float], Optional[torch.Tensor], Optional[torch.Tensor]]: _SCREAMING_SNAKE_CASE = self._prepare_inputs(A__ ) _SCREAMING_SNAKE_CASE = { """max_length""": self.data_args.val_max_target_length if self.data_args is not None else self.config.max_length, """num_beams""": self.data_args.eval_beams if self.data_args is not None else self.config.num_beams, } if self.args.predict_with_generate and not self.args.prediction_loss_only: _SCREAMING_SNAKE_CASE = self.model.generate( inputs["""input_ids"""] , attention_mask=inputs["""attention_mask"""] , **A__ , ) # in case the batch is shorter than max length, the output should be padded if generated_tokens.shape[-1] < gen_kwargs["max_length"]: _SCREAMING_SNAKE_CASE = self._pad_tensors_to_max_len(A__ , gen_kwargs["""max_length"""] ) _SCREAMING_SNAKE_CASE = inputs.pop("""labels""" ) with torch.no_grad(): # compute loss on predict data _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = self._compute_loss(A__ , A__ , A__ ) _SCREAMING_SNAKE_CASE = loss.mean().detach() if self.args.prediction_loss_only: return (loss, None, None) _SCREAMING_SNAKE_CASE = generated_tokens if self.args.predict_with_generate else logits if labels.shape[-1] < gen_kwargs["max_length"]: _SCREAMING_SNAKE_CASE = self._pad_tensors_to_max_len(A__ , gen_kwargs["""max_length"""] ) return (loss, logits, labels) def UpperCamelCase ( self , A__ , A__ ) -> Optional[Any]: # If PAD token is not defined at least EOS token has to be defined _SCREAMING_SNAKE_CASE = self.config.pad_token_id if self.config.pad_token_id is not None else self.config.eos_token_id if pad_token_id is None: raise ValueError( """Make sure that either `config.pad_token_id` or `config.eos_token_id` is defined if tensor has to be""" F" padded to `max_length`={max_length}" ) _SCREAMING_SNAKE_CASE = pad_token_id * torch.ones( (tensor.shape[0], max_length) , dtype=tensor.dtype , device=tensor.device ) _SCREAMING_SNAKE_CASE = tensor return padded_tensor
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 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 = 'CLIPImageProcessor' SCREAMING_SNAKE_CASE = ('XLMRobertaTokenizer', 'XLMRobertaTokenizerFast') def __init__( self , A__=None , A__=None , **A__ ) -> Tuple: _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__ ) def __call__( self , A__=None , A__=None , A__=None , **A__ ) -> List[str]: 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__ ) -> Optional[int]: return self.tokenizer.batch_decode(*A__ , **A__ ) def UpperCamelCase ( self , *A__ , **A__ ) -> Optional[Any]: return self.tokenizer.decode(*A__ , **A__ ) @property def UpperCamelCase ( self ) -> Dict: _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 ) )
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''' 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)
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 torch from transformers import BlenderbotConfig, BlenderbotForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase__ : List[str] = logging.get_logger(__name__) UpperCamelCase__ : Any = [ ["attention", "attn"], ["encoder_attention", "encoder_attn"], ["q_lin", "q_proj"], ["k_lin", "k_proj"], ["v_lin", "v_proj"], ["out_lin", "out_proj"], ["norm_embeddings", "layernorm_embedding"], ["position_embeddings", "embed_positions"], ["embeddings", "embed_tokens"], ["ffn.lin", "fc"], ] def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" if k == "embeddings.weight": return "shared.weight" for parlai_name, hf_name in PATTERNS: _SCREAMING_SNAKE_CASE = k.replace(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if k.startswith("""encoder""" ): _SCREAMING_SNAKE_CASE = k.replace(""".attn""" , """.self_attn""" ) _SCREAMING_SNAKE_CASE = k.replace("""norm1""" , """self_attn_layer_norm""" ) _SCREAMING_SNAKE_CASE = k.replace("""norm2""" , """final_layer_norm""" ) elif k.startswith("""decoder""" ): _SCREAMING_SNAKE_CASE = k.replace("""norm1""" , """self_attn_layer_norm""" ) _SCREAMING_SNAKE_CASE = k.replace("""norm2""" , """encoder_attn_layer_norm""" ) _SCREAMING_SNAKE_CASE = k.replace("""norm3""" , """final_layer_norm""" ) return k def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE = [ """model.encoder.layernorm_embedding.weight""", """model.encoder.layernorm_embedding.bias""", """model.decoder.layernorm_embedding.weight""", """model.decoder.layernorm_embedding.bias""", ] for k in keys: _SCREAMING_SNAKE_CASE = sd.pop(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = k.replace("""layernorm_embedding""" , """layer_norm""" ) assert new_k not in sd _SCREAMING_SNAKE_CASE = v UpperCamelCase__ : Any = ["START"] @torch.no_grad() def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE = torch.load(SCREAMING_SNAKE_CASE_ , map_location="""cpu""" ) _SCREAMING_SNAKE_CASE = model["""model"""] _SCREAMING_SNAKE_CASE = BlenderbotConfig.from_json_file(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = BlenderbotForConditionalGeneration(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = m.model.state_dict().keys() _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = {} for k, v in sd.items(): if k in IGNORE_KEYS: continue _SCREAMING_SNAKE_CASE = rename_state_dict_key(SCREAMING_SNAKE_CASE_ ) if new_k not in valid_keys: failures.append([k, new_k] ) else: _SCREAMING_SNAKE_CASE = v if cfg.normalize_before: # Blenderbot-3B checkpoints. Rename layernorm_embedding -> layer_norm rename_layernorm_keys(SCREAMING_SNAKE_CASE_ ) m.model.load_state_dict(SCREAMING_SNAKE_CASE_ , strict=SCREAMING_SNAKE_CASE_ ) m.half() m.save_pretrained(SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": UpperCamelCase__ : int = argparse.ArgumentParser() # Required parameters parser.add_argument("--src_path", type=str, help="like blenderbot-model.bin") parser.add_argument("--save_dir", default="hf_blenderbot", type=str, help="Where to save converted model.") parser.add_argument( "--hf_config_json", default="blenderbot-3b-config.json", type=str, help="Path to config to use" ) UpperCamelCase__ : Dict = parser.parse_args() convert_parlai_checkpoint(args.src_path, args.save_dir, args.hf_config_json)
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 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, }
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 math import pi, sqrt def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> tuple: """simple docstring""" if inductance <= 0: raise ValueError("""Inductance cannot be 0 or negative""" ) elif capacitance <= 0: raise ValueError("""Capacitance cannot be 0 or negative""" ) else: return ( "Resonant frequency", float(1 / (2 * pi * (sqrt(inductance * capacitance ))) ), ) if __name__ == "__main__": import doctest doctest.testmod()
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 def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> list[int]: """simple docstring""" return [ord(SCREAMING_SNAKE_CASE_ ) - 96 for elem in plain] def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" return "".join(chr(elem + 96 ) for elem in encoded ) def lowerCAmelCase_ ( ) -> None: """simple docstring""" _SCREAMING_SNAKE_CASE = encode(input("""-> """ ).strip().lower() ) print("""Encoded: """ , SCREAMING_SNAKE_CASE_ ) print("""Decoded:""" , decode(SCREAMING_SNAKE_CASE_ ) ) 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''' import datasets UpperCamelCase__ : int = "\\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__ : Tuple = "\\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__ : str = "\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_ ) -> Dict: """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__ ) -> Tuple: return {"accuracy": simple_accuracy(A__ , A__ )}
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 json import os import re import sys import urllib.request import requests from bsa import BeautifulSoup UpperCamelCase__ : Optional[Any] = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" " (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582" } def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ = "dhaka" , SCREAMING_SNAKE_CASE_ = 5 ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE = min(SCREAMING_SNAKE_CASE_ , 50 ) # Prevent abuse! _SCREAMING_SNAKE_CASE = { """q""": query, """tbm""": """isch""", """hl""": """en""", """ijn""": """0""", } _SCREAMING_SNAKE_CASE = requests.get("""https://www.google.com/search""" , params=SCREAMING_SNAKE_CASE_ , headers=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = BeautifulSoup(html.text , """html.parser""" ) _SCREAMING_SNAKE_CASE = """""".join( re.findall(r"""AF_initDataCallback\(([^<]+)\);""" , str(soup.select("""script""" ) ) ) ) _SCREAMING_SNAKE_CASE = json.dumps(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = json.loads(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = re.findall( r"""\[\"GRID_STATE0\",null,\[\[1,\[0,\".*?\",(.*),\"All\",""" , SCREAMING_SNAKE_CASE_ , ) if not matched_google_image_data: return 0 _SCREAMING_SNAKE_CASE = re.sub( r"""\[\"(https\:\/\/encrypted-tbn0\.gstatic\.com\/images\?.*?)\",\d+,\d+\]""" , """""" , str(SCREAMING_SNAKE_CASE_ ) , ) _SCREAMING_SNAKE_CASE = re.findall( r"""(?:'|,),\[\"(https:|http.*?)\",\d+,\d+\]""" , SCREAMING_SNAKE_CASE_ , ) for index, fixed_full_res_image in enumerate(SCREAMING_SNAKE_CASE_ ): if index >= max_images: return index _SCREAMING_SNAKE_CASE = bytes(SCREAMING_SNAKE_CASE_ , """ascii""" ).decode( """unicode-escape""" ) _SCREAMING_SNAKE_CASE = bytes(SCREAMING_SNAKE_CASE_ , """ascii""" ).decode( """unicode-escape""" ) _SCREAMING_SNAKE_CASE = urllib.request.build_opener() _SCREAMING_SNAKE_CASE = [ ( """User-Agent""", """Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36""" """ (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582""", ) ] urllib.request.install_opener(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = F"query_{query.replace(' ' , '_' )}" if not os.path.exists(SCREAMING_SNAKE_CASE_ ): os.makedirs(SCREAMING_SNAKE_CASE_ ) urllib.request.urlretrieve( # noqa: S310 SCREAMING_SNAKE_CASE_ , F"{path_name}/original_size_img_{index}.jpg" ) return index if __name__ == "__main__": try: UpperCamelCase__ : List[str] = download_images_from_google_query(sys.argv[1]) print(f"""{image_count} images were downloaded to disk.""") except IndexError: print("Please provide a search term.") raise
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 collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert import BertTokenizer UpperCamelCase__ : Optional[Any] = logging.get_logger(__name__) UpperCamelCase__ : Optional[int] = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} UpperCamelCase__ : Tuple = { "vocab_file": { "facebook/dpr-ctx_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt" ), "facebook/dpr-ctx_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "facebook/dpr-ctx_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json" ), "facebook/dpr-ctx_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json" ), }, } UpperCamelCase__ : int = { "vocab_file": { "facebook/dpr-question_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt" ), "facebook/dpr-question_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "facebook/dpr-question_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json" ), "facebook/dpr-question_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json" ), }, } UpperCamelCase__ : Optional[int] = { "vocab_file": { "facebook/dpr-reader-single-nq-base": ( "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt" ), "facebook/dpr-reader-multiset-base": ( "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "facebook/dpr-reader-single-nq-base": ( "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json" ), "facebook/dpr-reader-multiset-base": ( "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json" ), }, } UpperCamelCase__ : List[Any] = { "facebook/dpr-ctx_encoder-single-nq-base": 512, "facebook/dpr-ctx_encoder-multiset-base": 512, } UpperCamelCase__ : Optional[Any] = { "facebook/dpr-question_encoder-single-nq-base": 512, "facebook/dpr-question_encoder-multiset-base": 512, } UpperCamelCase__ : Optional[int] = { "facebook/dpr-reader-single-nq-base": 512, "facebook/dpr-reader-multiset-base": 512, } UpperCamelCase__ : Optional[Any] = { "facebook/dpr-ctx_encoder-single-nq-base": {"do_lower_case": True}, "facebook/dpr-ctx_encoder-multiset-base": {"do_lower_case": True}, } UpperCamelCase__ : Any = { "facebook/dpr-question_encoder-single-nq-base": {"do_lower_case": True}, "facebook/dpr-question_encoder-multiset-base": {"do_lower_case": True}, } UpperCamelCase__ : str = { "facebook/dpr-reader-single-nq-base": {"do_lower_case": True}, "facebook/dpr-reader-multiset-base": {"do_lower_case": True}, } class _a (_lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION class _a (_lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase__ : Optional[Any] = collections.namedtuple( "DPRSpanPrediction", ["span_score", "relevance_score", "doc_id", "start_index", "end_index", "text"] ) UpperCamelCase__ : Union[str, Any] = collections.namedtuple("DPRReaderOutput", ["start_logits", "end_logits", "relevance_logits"]) UpperCamelCase__ : Union[str, Any] = R"\n Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`.\n It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers),\n using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)`\n with the format:\n\n ```\n [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids>\n ```\n\n Args:\n questions (`str` or `List[str]`):\n The questions to be encoded. You can specify one question for many passages. In this case, the question\n will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in\n `titles` or `texts`.\n titles (`str` or `List[str]`):\n The passages titles to be encoded. This can be a string or a list of strings if there are several passages.\n texts (`str` or `List[str]`):\n The passages texts to be encoded. This can be a string or a list of strings if there are several passages.\n padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`):\n Activates and controls padding. Accepts the following values:\n\n - `True` or `'longest'`: Pad to the longest sequence in the batch (or no padding if only a single sequence\n if provided).\n - `'max_length'`: Pad to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided.\n - `False` or `'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of different\n lengths).\n truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`):\n Activates and controls truncation. Accepts the following values:\n\n - `True` or `'longest_first'`: Truncate to a maximum length specified with the argument `max_length` or to\n the maximum acceptable input length for the model if that argument is not provided. This will truncate\n token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch\n of pairs) is provided.\n - `'only_first'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the first\n sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `'only_second'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the\n second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `False` or `'do_not_truncate'` (default): No truncation (i.e., can output batch with sequence lengths\n greater than the model maximum admissible input size).\n max_length (`int`, *optional*):\n Controls the maximum length to use by one of the truncation/padding parameters.\n\n If left unset or set to `None`, this will use the predefined model maximum length if a maximum length\n is required by one of the truncation/padding parameters. If the model has no specific maximum input\n length (like XLNet) truncation/padding to a maximum length will be deactivated.\n return_tensors (`str` or [`~utils.TensorType`], *optional*):\n If set, will return tensors instead of list of python integers. Acceptable values are:\n\n - `'tf'`: Return TensorFlow `tf.constant` objects.\n - `'pt'`: Return PyTorch `torch.Tensor` objects.\n - `'np'`: Return Numpy `np.ndarray` objects.\n return_attention_mask (`bool`, *optional*):\n Whether or not to return the attention mask. If not set, will return the attention mask according to the\n specific tokenizer's default, defined by the `return_outputs` attribute.\n\n [What are attention masks?](../glossary#attention-mask)\n\n Returns:\n `Dict[str, List[List[int]]]`: A dictionary with the following keys:\n\n - `input_ids`: List of token ids to be fed to a model.\n - `attention_mask`: List of indices specifying which tokens should be attended to by the model.\n " @add_start_docstrings(_lowerCamelCase) class _a : """simple docstring""" def __call__( self , A__ , A__ = None , A__ = None , A__ = False , A__ = False , A__ = None , A__ = None , A__ = None , **A__ , ) -> BatchEncoding: if titles is None and texts is None: return super().__call__( A__ , padding=A__ , truncation=A__ , max_length=A__ , return_tensors=A__ , return_attention_mask=A__ , **A__ , ) elif titles is None or texts is None: _SCREAMING_SNAKE_CASE = titles if texts is None else texts return super().__call__( A__ , A__ , padding=A__ , truncation=A__ , max_length=A__ , return_tensors=A__ , return_attention_mask=A__ , **A__ , ) _SCREAMING_SNAKE_CASE = titles if not isinstance(A__ , A__ ) else [titles] _SCREAMING_SNAKE_CASE = texts if not isinstance(A__ , A__ ) else [texts] _SCREAMING_SNAKE_CASE = len(A__ ) _SCREAMING_SNAKE_CASE = questions if not isinstance(A__ , A__ ) else [questions] * n_passages if len(A__ ) != len(A__ ): raise ValueError( F"There should be as many titles than texts but got {len(A__ )} titles and {len(A__ )} texts." ) _SCREAMING_SNAKE_CASE = super().__call__(A__ , A__ , padding=A__ , truncation=A__ )["""input_ids"""] _SCREAMING_SNAKE_CASE = super().__call__(A__ , add_special_tokens=A__ , padding=A__ , truncation=A__ )["""input_ids"""] _SCREAMING_SNAKE_CASE = { """input_ids""": [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(A__ , A__ ) ] } if return_attention_mask is not False: _SCREAMING_SNAKE_CASE = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) _SCREAMING_SNAKE_CASE = attention_mask return self.pad(A__ , padding=A__ , max_length=A__ , return_tensors=A__ ) def UpperCamelCase ( self , A__ , A__ , A__ = 16 , A__ = 64 , A__ = 4 , ) -> List[DPRSpanPrediction]: _SCREAMING_SNAKE_CASE = reader_input["""input_ids"""] _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = reader_output[:3] _SCREAMING_SNAKE_CASE = len(A__ ) _SCREAMING_SNAKE_CASE = sorted(range(A__ ) , reverse=A__ , key=relevance_logits.__getitem__ ) _SCREAMING_SNAKE_CASE = [] for doc_id in sorted_docs: _SCREAMING_SNAKE_CASE = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence _SCREAMING_SNAKE_CASE = sequence_ids.index(self.sep_token_id , 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: _SCREAMING_SNAKE_CASE = sequence_ids.index(self.pad_token_id ) else: _SCREAMING_SNAKE_CASE = len(A__ ) _SCREAMING_SNAKE_CASE = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=A__ , top_spans=A__ , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=A__ , start_index=A__ , end_index=A__ , text=self.decode(sequence_ids[start_index : end_index + 1] ) , ) ) if len(A__ ) >= num_spans: break return nbest_spans_predictions[:num_spans] def UpperCamelCase ( self , A__ , A__ , A__ , A__ , ) -> List[DPRSpanPrediction]: _SCREAMING_SNAKE_CASE = [] for start_index, start_score in enumerate(A__ ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) _SCREAMING_SNAKE_CASE = sorted(A__ , key=lambda A__ : x[1] , reverse=A__ ) _SCREAMING_SNAKE_CASE = [] for (start_index, end_index), score in scores: if start_index > end_index: raise ValueError(F"Wrong span indices: [{start_index}:{end_index}]" ) _SCREAMING_SNAKE_CASE = end_index - start_index + 1 if length > max_answer_length: raise ValueError(F"Span is too long: {length} > {max_answer_length}" ) if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(A__ ) == top_spans: break return chosen_span_intervals @add_end_docstrings(_lowerCamelCase) class _a (_lowerCamelCase , _lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE = READER_PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE = READER_PRETRAINED_INIT_CONFIGURATION SCREAMING_SNAKE_CASE = ['input_ids', 'attention_mask']
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 from transformers import BigBirdConfig, BigBirdForPreTraining, BigBirdForQuestionAnswering, load_tf_weights_in_big_bird from transformers.utils import logging logging.set_verbosity_info() def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = BigBirdConfig.from_json_file(SCREAMING_SNAKE_CASE_ ) print(F"Building PyTorch model from configuration: {config}" ) if is_trivia_qa: _SCREAMING_SNAKE_CASE = BigBirdForQuestionAnswering(SCREAMING_SNAKE_CASE_ ) else: _SCREAMING_SNAKE_CASE = BigBirdForPreTraining(SCREAMING_SNAKE_CASE_ ) # Load weights from tf checkpoint load_tf_weights_in_big_bird(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , is_trivia_qa=SCREAMING_SNAKE_CASE_ ) # Save pytorch-model print(F"Save PyTorch model to {pytorch_dump_path}" ) model.save_pretrained(SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": UpperCamelCase__ : str = argparse.ArgumentParser() # Required parameters parser.add_argument( "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path." ) parser.add_argument( "--big_bird_config_file", default=None, type=str, required=True, help=( "The config json file corresponding to the pre-trained BERT model. \n" "This specifies the model architecture." ), ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) parser.add_argument( "--is_trivia_qa", action="store_true", help="Whether to convert a model with a trivia_qa head." ) UpperCamelCase__ : List[str] = parser.parse_args() convert_tf_checkpoint_to_pytorch( args.tf_checkpoint_path, args.big_bird_config_file, args.pytorch_dump_path, args.is_trivia_qa )
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 from fractions import Fraction from math import gcd, sqrt def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> bool: """simple docstring""" _SCREAMING_SNAKE_CASE = int(number**0.5 ) return number == sq * sq def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> tuple[int, int]: """simple docstring""" _SCREAMING_SNAKE_CASE = x_num * y_den * z_den + y_num * x_den * z_den + z_num * x_den * y_den _SCREAMING_SNAKE_CASE = x_den * y_den * z_den _SCREAMING_SNAKE_CASE = gcd(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) top //= hcf bottom //= hcf return top, bottom def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ = 35 ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE = set() _SCREAMING_SNAKE_CASE = 42 _SCREAMING_SNAKE_CASE = Fraction(0 ) _SCREAMING_SNAKE_CASE = 42 for x_num in range(1 , order + 1 ): for x_den in range(x_num + 1 , order + 1 ): for y_num in range(1 , order + 1 ): for y_den in range(y_num + 1 , order + 1 ): # n=1 _SCREAMING_SNAKE_CASE = x_num * y_den + x_den * y_num _SCREAMING_SNAKE_CASE = x_den * y_den _SCREAMING_SNAKE_CASE = gcd(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: _SCREAMING_SNAKE_CASE = add_three( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) unique_s.add(SCREAMING_SNAKE_CASE_ ) # n=2 _SCREAMING_SNAKE_CASE = ( x_num * x_num * y_den * y_den + x_den * x_den * y_num * y_num ) _SCREAMING_SNAKE_CASE = x_den * x_den * y_den * y_den if is_sq(SCREAMING_SNAKE_CASE_ ) and is_sq(SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE = int(sqrt(SCREAMING_SNAKE_CASE_ ) ) _SCREAMING_SNAKE_CASE = int(sqrt(SCREAMING_SNAKE_CASE_ ) ) _SCREAMING_SNAKE_CASE = gcd(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: _SCREAMING_SNAKE_CASE = add_three( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) unique_s.add(SCREAMING_SNAKE_CASE_ ) # n=-1 _SCREAMING_SNAKE_CASE = x_num * y_num _SCREAMING_SNAKE_CASE = x_den * y_num + x_num * y_den _SCREAMING_SNAKE_CASE = gcd(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: _SCREAMING_SNAKE_CASE = add_three( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) unique_s.add(SCREAMING_SNAKE_CASE_ ) # n=2 _SCREAMING_SNAKE_CASE = x_num * x_num * y_num * y_num _SCREAMING_SNAKE_CASE = ( x_den * x_den * y_num * y_num + x_num * x_num * y_den * y_den ) if is_sq(SCREAMING_SNAKE_CASE_ ) and is_sq(SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE = int(sqrt(SCREAMING_SNAKE_CASE_ ) ) _SCREAMING_SNAKE_CASE = int(sqrt(SCREAMING_SNAKE_CASE_ ) ) _SCREAMING_SNAKE_CASE = gcd(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: _SCREAMING_SNAKE_CASE = add_three( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) unique_s.add(SCREAMING_SNAKE_CASE_ ) for num, den in unique_s: total += Fraction(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return total.denominator + total.numerator if __name__ == "__main__": print(f"""{solution() = }""")
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 def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> None: """simple docstring""" _SCREAMING_SNAKE_CASE = {"""Content-Type""": """application/json"""} _SCREAMING_SNAKE_CASE = requests.post(SCREAMING_SNAKE_CASE_ , json={"""text""": message_body} , headers=SCREAMING_SNAKE_CASE_ ) if response.status_code != 2_00: _SCREAMING_SNAKE_CASE = ( """Request to slack returned an error """ F"{response.status_code}, the response is:\n{response.text}" ) raise ValueError(SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": # Set the slack url to the one provided by Slack when you create the webhook at # https://my.slack.com/services/new/incoming-webhook/ send_slack_message("<YOUR MESSAGE BODY>", "<SLACK CHANNEL URL>")
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 from collections.abc import Callable from typing import Any, Generic, TypeVar UpperCamelCase__ : List[Any] = TypeVar("T") class _a (Generic[T]): """simple docstring""" def __init__( self , A__ , A__ ) -> None: _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = len(A__ ) _SCREAMING_SNAKE_CASE = [any_type for _ in range(self.N )] + arr _SCREAMING_SNAKE_CASE = fnc self.build() def UpperCamelCase ( self ) -> None: for p in range(self.N - 1 , 0 , -1 ): _SCREAMING_SNAKE_CASE = self.fn(self.st[p * 2] , self.st[p * 2 + 1] ) def UpperCamelCase ( self , A__ , A__ ) -> None: p += self.N _SCREAMING_SNAKE_CASE = v while p > 1: _SCREAMING_SNAKE_CASE = p // 2 _SCREAMING_SNAKE_CASE = self.fn(self.st[p * 2] , self.st[p * 2 + 1] ) def UpperCamelCase ( self , A__ , A__ ) -> T | None: # noqa: E741 _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = l + self.N, r + self.N _SCREAMING_SNAKE_CASE = None while l <= r: if l % 2 == 1: _SCREAMING_SNAKE_CASE = self.st[l] if res is None else self.fn(A__ , self.st[l] ) if r % 2 == 0: _SCREAMING_SNAKE_CASE = self.st[r] if res is None else self.fn(A__ , self.st[r] ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = (l + 1) // 2, (r - 1) // 2 return res if __name__ == "__main__": from functools import reduce UpperCamelCase__ : Optional[int] = [1, 10, -2, 9, -3, 8, 4, -7, 5, 6, 11, -12] UpperCamelCase__ : Union[str, Any] = { 0: 7, 1: 2, 2: 6, 3: -14, 4: 5, 5: 4, 6: 7, 7: -10, 8: 9, 9: 10, 10: 12, 11: 1, } UpperCamelCase__ : List[str] = SegmentTree(test_array, min) UpperCamelCase__ : List[Any] = SegmentTree(test_array, max) UpperCamelCase__ : Optional[Any] = SegmentTree(test_array, lambda a, b: a + b) def lowerCAmelCase_ ( ) -> None: """simple docstring""" for i in range(len(SCREAMING_SNAKE_CASE_ ) ): for j in range(SCREAMING_SNAKE_CASE_ , len(SCREAMING_SNAKE_CASE_ ) ): _SCREAMING_SNAKE_CASE = reduce(SCREAMING_SNAKE_CASE_ , test_array[i : j + 1] ) _SCREAMING_SNAKE_CASE = reduce(SCREAMING_SNAKE_CASE_ , test_array[i : j + 1] ) _SCREAMING_SNAKE_CASE = reduce(lambda SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : a + b , test_array[i : j + 1] ) assert min_range == min_segment_tree.query(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert max_range == max_segment_tree.query(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert sum_range == sum_segment_tree.query(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) test_all_segments() for index, value in test_updates.items(): UpperCamelCase__ : List[str] = value min_segment_tree.update(index, value) max_segment_tree.update(index, value) sum_segment_tree.update(index, value) test_all_segments()
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 json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation UpperCamelCase__ : str = logging.get_logger(__name__) UpperCamelCase__ : List[Any] = {"vocab_file": "vocab.json", "merges_file": "merges.txt", "tokenizer_file": "tokenizer.json"} UpperCamelCase__ : Dict = { "tokenizer_file": { "EleutherAI/gpt-neox-20b": "https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/tokenizer.json", }, } UpperCamelCase__ : List[Any] = { "gpt-neox-20b": 2_048, } 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__=None , A__=None , A__=None , A__="<|endoftext|>" , A__="<|endoftext|>" , A__="<|endoftext|>" , A__=False , **A__ , ) -> List[Any]: super().__init__( A__ , A__ , tokenizer_file=A__ , unk_token=A__ , bos_token=A__ , eos_token=A__ , add_prefix_space=A__ , **A__ , ) _SCREAMING_SNAKE_CASE = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get("""add_prefix_space""" , A__ ) != add_prefix_space: _SCREAMING_SNAKE_CASE = getattr(A__ , pre_tok_state.pop("""type""" ) ) _SCREAMING_SNAKE_CASE = add_prefix_space _SCREAMING_SNAKE_CASE = pre_tok_class(**A__ ) _SCREAMING_SNAKE_CASE = add_prefix_space def UpperCamelCase ( self , A__ , A__ = None ) -> Tuple[str]: _SCREAMING_SNAKE_CASE = self._tokenizer.model.save(A__ , name=A__ ) return tuple(A__ ) def UpperCamelCase ( self , A__ ) -> List[int]: _SCREAMING_SNAKE_CASE = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(A__ , add_special_tokens=A__ ) + [self.eos_token_id] ) if len(A__ ) > self.model_max_length: _SCREAMING_SNAKE_CASE = input_ids[-self.model_max_length :] return input_ids
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 collections import OrderedDict from typing import Any, List, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import logging UpperCamelCase__ : Dict = logging.get_logger(__name__) UpperCamelCase__ : List[str] = { "EleutherAI/gpt-j-6B": "https://huggingface.co/EleutherAI/gpt-j-6B/resolve/main/config.json", # See all GPT-J models at https://huggingface.co/models?filter=gpt_j } class _a (_lowerCamelCase): """simple docstring""" SCREAMING_SNAKE_CASE = 'gptj' SCREAMING_SNAKE_CASE = { 'max_position_embeddings': 'n_positions', 'hidden_size': 'n_embd', 'num_attention_heads': 'n_head', 'num_hidden_layers': 'n_layer', } def __init__( self , A__=5_04_00 , A__=20_48 , A__=40_96 , A__=28 , A__=16 , A__=64 , A__=None , A__="gelu_new" , A__=0.0 , A__=0.0 , A__=0.0 , A__=1E-5 , A__=0.02 , A__=True , A__=5_02_56 , A__=5_02_56 , A__=False , **A__ , ) -> Tuple: _SCREAMING_SNAKE_CASE = vocab_size _SCREAMING_SNAKE_CASE = n_positions _SCREAMING_SNAKE_CASE = n_embd _SCREAMING_SNAKE_CASE = n_layer _SCREAMING_SNAKE_CASE = n_head _SCREAMING_SNAKE_CASE = n_inner _SCREAMING_SNAKE_CASE = rotary_dim _SCREAMING_SNAKE_CASE = activation_function _SCREAMING_SNAKE_CASE = resid_pdrop _SCREAMING_SNAKE_CASE = embd_pdrop _SCREAMING_SNAKE_CASE = attn_pdrop _SCREAMING_SNAKE_CASE = layer_norm_epsilon _SCREAMING_SNAKE_CASE = initializer_range _SCREAMING_SNAKE_CASE = use_cache _SCREAMING_SNAKE_CASE = bos_token_id _SCREAMING_SNAKE_CASE = eos_token_id super().__init__( bos_token_id=A__ , eos_token_id=A__ , tie_word_embeddings=A__ , **A__ ) class _a (_lowerCamelCase): """simple docstring""" def __init__( self , A__ , A__ = "default" , A__ = None , A__ = False , ) -> int: super().__init__(A__ , task=A__ , patching_specs=A__ , use_past=A__ ) if not getattr(self._config , """pad_token_id""" , A__ ): # TODO: how to do that better? _SCREAMING_SNAKE_CASE = 0 @property def UpperCamelCase ( self ) -> Mapping[str, Mapping[int, str]]: _SCREAMING_SNAKE_CASE = OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}} ) if self.use_past: self.fill_with_past_key_values_(A__ , direction="""inputs""" ) _SCREAMING_SNAKE_CASE = {0: """batch""", 1: """past_sequence + sequence"""} else: _SCREAMING_SNAKE_CASE = {0: """batch""", 1: """sequence"""} return common_inputs @property def UpperCamelCase ( self ) -> int: return self._config.n_layer @property def UpperCamelCase ( self ) -> int: return self._config.n_head def UpperCamelCase ( self , A__ , A__ = -1 , A__ = -1 , A__ = False , A__ = None , ) -> Mapping[str, Any]: _SCREAMING_SNAKE_CASE = super(A__ , self ).generate_dummy_inputs( A__ , batch_size=A__ , seq_length=A__ , is_pair=A__ , framework=A__ ) # We need to order the input in the way they appears in the forward() _SCREAMING_SNAKE_CASE = OrderedDict({"""input_ids""": common_inputs["""input_ids"""]} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed.""" ) else: import torch _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = common_inputs["""input_ids"""].shape # Not using the same length for past_key_values _SCREAMING_SNAKE_CASE = seqlen + 2 _SCREAMING_SNAKE_CASE = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) _SCREAMING_SNAKE_CASE = [ (torch.zeros(A__ ), torch.zeros(A__ )) for _ in range(self.num_layers ) ] _SCREAMING_SNAKE_CASE = common_inputs["""attention_mask"""] if self.use_past: _SCREAMING_SNAKE_CASE = ordered_inputs["""attention_mask"""].dtype _SCREAMING_SNAKE_CASE = torch.cat( [ordered_inputs["""attention_mask"""], torch.ones(A__ , A__ , dtype=A__ )] , dim=1 ) return ordered_inputs @property def UpperCamelCase ( self ) -> int: return 13
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''' 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 ( BertTokenizer, ViltConfig, ViltForImageAndTextRetrieval, ViltForImagesAndTextClassification, ViltForMaskedLM, ViltForQuestionAnswering, ViltImageProcessor, ViltProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase__ = logging.get_logger(__name__) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=False , SCREAMING_SNAKE_CASE_=False , SCREAMING_SNAKE_CASE_=False ) -> int: """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"transformer.blocks.{i}.norm1.weight", F"vilt.encoder.layer.{i}.layernorm_before.weight") ) rename_keys.append((F"transformer.blocks.{i}.norm1.bias", F"vilt.encoder.layer.{i}.layernorm_before.bias") ) rename_keys.append( (F"transformer.blocks.{i}.attn.proj.weight", F"vilt.encoder.layer.{i}.attention.output.dense.weight") ) rename_keys.append( (F"transformer.blocks.{i}.attn.proj.bias", F"vilt.encoder.layer.{i}.attention.output.dense.bias") ) rename_keys.append((F"transformer.blocks.{i}.norm2.weight", F"vilt.encoder.layer.{i}.layernorm_after.weight") ) rename_keys.append((F"transformer.blocks.{i}.norm2.bias", F"vilt.encoder.layer.{i}.layernorm_after.bias") ) rename_keys.append( (F"transformer.blocks.{i}.mlp.fc1.weight", F"vilt.encoder.layer.{i}.intermediate.dense.weight") ) rename_keys.append((F"transformer.blocks.{i}.mlp.fc1.bias", F"vilt.encoder.layer.{i}.intermediate.dense.bias") ) rename_keys.append((F"transformer.blocks.{i}.mlp.fc2.weight", F"vilt.encoder.layer.{i}.output.dense.weight") ) rename_keys.append((F"transformer.blocks.{i}.mlp.fc2.bias", F"vilt.encoder.layer.{i}.output.dense.bias") ) # embeddings rename_keys.extend( [ # text embeddings ("""text_embeddings.word_embeddings.weight""", """vilt.embeddings.text_embeddings.word_embeddings.weight"""), ( """text_embeddings.position_embeddings.weight""", """vilt.embeddings.text_embeddings.position_embeddings.weight""", ), ("""text_embeddings.position_ids""", """vilt.embeddings.text_embeddings.position_ids"""), ( """text_embeddings.token_type_embeddings.weight""", """vilt.embeddings.text_embeddings.token_type_embeddings.weight""", ), ("""text_embeddings.LayerNorm.weight""", """vilt.embeddings.text_embeddings.LayerNorm.weight"""), ("""text_embeddings.LayerNorm.bias""", """vilt.embeddings.text_embeddings.LayerNorm.bias"""), # patch embeddings ("""transformer.cls_token""", """vilt.embeddings.cls_token"""), ("""transformer.patch_embed.proj.weight""", """vilt.embeddings.patch_embeddings.projection.weight"""), ("""transformer.patch_embed.proj.bias""", """vilt.embeddings.patch_embeddings.projection.bias"""), ("""transformer.pos_embed""", """vilt.embeddings.position_embeddings"""), # token type embeddings ("""token_type_embeddings.weight""", """vilt.embeddings.token_type_embeddings.weight"""), ] ) # final layernorm + pooler rename_keys.extend( [ ("""transformer.norm.weight""", """vilt.layernorm.weight"""), ("""transformer.norm.bias""", """vilt.layernorm.bias"""), ("""pooler.dense.weight""", """vilt.pooler.dense.weight"""), ("""pooler.dense.bias""", """vilt.pooler.dense.bias"""), ] ) # classifier head(s) if vqa_model: # classification head rename_keys.extend( [ ("""vqa_classifier.0.weight""", """classifier.0.weight"""), ("""vqa_classifier.0.bias""", """classifier.0.bias"""), ("""vqa_classifier.1.weight""", """classifier.1.weight"""), ("""vqa_classifier.1.bias""", """classifier.1.bias"""), ("""vqa_classifier.3.weight""", """classifier.3.weight"""), ("""vqa_classifier.3.bias""", """classifier.3.bias"""), ] ) elif nlvr_model: # classification head rename_keys.extend( [ ("""nlvr2_classifier.0.weight""", """classifier.0.weight"""), ("""nlvr2_classifier.0.bias""", """classifier.0.bias"""), ("""nlvr2_classifier.1.weight""", """classifier.1.weight"""), ("""nlvr2_classifier.1.bias""", """classifier.1.bias"""), ("""nlvr2_classifier.3.weight""", """classifier.3.weight"""), ("""nlvr2_classifier.3.bias""", """classifier.3.bias"""), ] ) else: pass return rename_keys def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: """simple docstring""" for i in range(config.num_hidden_layers ): _SCREAMING_SNAKE_CASE = """vilt.""" # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) _SCREAMING_SNAKE_CASE = state_dict.pop(F"transformer.blocks.{i}.attn.qkv.weight" ) _SCREAMING_SNAKE_CASE = state_dict.pop(F"transformer.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_ ) -> 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_ ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE = dct.pop(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = val @torch.no_grad() def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE = ViltConfig(image_size=3_84 , patch_size=32 , tie_word_embeddings=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = False if "vqa" in checkpoint_url: _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = 31_29 _SCREAMING_SNAKE_CASE = """huggingface/label-files""" _SCREAMING_SNAKE_CASE = """vqa2-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()} _SCREAMING_SNAKE_CASE = ViltForQuestionAnswering(SCREAMING_SNAKE_CASE_ ) elif "nlvr" in checkpoint_url: _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = 2 _SCREAMING_SNAKE_CASE = {0: """False""", 1: """True"""} _SCREAMING_SNAKE_CASE = {v: k for k, v in config.idalabel.items()} _SCREAMING_SNAKE_CASE = 3 _SCREAMING_SNAKE_CASE = ViltForImagesAndTextClassification(SCREAMING_SNAKE_CASE_ ) elif "irtr" in checkpoint_url: _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = ViltForImageAndTextRetrieval(SCREAMING_SNAKE_CASE_ ) elif "mlm_itm" in checkpoint_url: _SCREAMING_SNAKE_CASE = True _SCREAMING_SNAKE_CASE = ViltForMaskedLM(SCREAMING_SNAKE_CASE_ ) else: raise ValueError("""Unknown model type""" ) # load state_dict of original model, remove and rename some keys _SCREAMING_SNAKE_CASE = torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE_ , map_location="""cpu""" )["""state_dict"""] _SCREAMING_SNAKE_CASE = create_rename_keys(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , 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_ ) if mlm_model or irtr_model: _SCREAMING_SNAKE_CASE = ["""itm_score.fc.weight""", """itm_score.fc.bias"""] for k in ignore_keys: state_dict.pop(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # load state dict into HuggingFace model model.eval() if mlm_model: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = model.load_state_dict(SCREAMING_SNAKE_CASE_ , strict=SCREAMING_SNAKE_CASE_ ) assert missing_keys == ["mlm_score.decoder.bias"] else: model.load_state_dict(SCREAMING_SNAKE_CASE_ ) # Define processor _SCREAMING_SNAKE_CASE = ViltImageProcessor(size=3_84 ) _SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained("""bert-base-uncased""" ) _SCREAMING_SNAKE_CASE = ViltProcessor(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # Forward pass on example inputs (image + text) if nlvr_model: _SCREAMING_SNAKE_CASE = Image.open(requests.get("""https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg""" , stream=SCREAMING_SNAKE_CASE_ ).raw ) _SCREAMING_SNAKE_CASE = Image.open(requests.get("""https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg""" , stream=SCREAMING_SNAKE_CASE_ ).raw ) _SCREAMING_SNAKE_CASE = ( """The left image contains twice the number of dogs as the right image, and at least two dogs in total are""" """ standing.""" ) _SCREAMING_SNAKE_CASE = processor(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , return_tensors="""pt""" ) _SCREAMING_SNAKE_CASE = processor(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , return_tensors="""pt""" ) _SCREAMING_SNAKE_CASE = model( input_ids=encoding_a.input_ids , pixel_values=encoding_a.pixel_values , pixel_values_a=encoding_a.pixel_values , ) else: _SCREAMING_SNAKE_CASE = Image.open(requests.get("""http://images.cocodataset.org/val2017/000000039769.jpg""" , stream=SCREAMING_SNAKE_CASE_ ).raw ) if mlm_model: _SCREAMING_SNAKE_CASE = """a bunch of [MASK] laying on a [MASK].""" else: _SCREAMING_SNAKE_CASE = """How many cats are there?""" _SCREAMING_SNAKE_CASE = processor(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , return_tensors="""pt""" ) _SCREAMING_SNAKE_CASE = model(**SCREAMING_SNAKE_CASE_ ) # Verify outputs if mlm_model: _SCREAMING_SNAKE_CASE = torch.Size([1, 11, 3_05_22] ) _SCREAMING_SNAKE_CASE = torch.tensor([-12.5061, -12.5123, -12.5174] ) assert outputs.logits.shape == expected_shape assert torch.allclose(outputs.logits[0, 0, :3] , SCREAMING_SNAKE_CASE_ , atol=1e-4 ) # verify masked token prediction equals "cats" _SCREAMING_SNAKE_CASE = outputs.logits[0, 4, :].argmax(-1 ).item() assert tokenizer.decode([predicted_id] ) == "cats" elif vqa_model: _SCREAMING_SNAKE_CASE = torch.Size([1, 31_29] ) _SCREAMING_SNAKE_CASE = torch.tensor([-15.9495, -18.1472, -10.3041] ) assert torch.allclose(outputs.logits[0, :3] , SCREAMING_SNAKE_CASE_ , atol=1e-4 ) assert outputs.logits.shape == expected_shape assert torch.allclose(outputs.logits[0, 0, :3] , SCREAMING_SNAKE_CASE_ , atol=1e-4 ) # verify vqa prediction equals "2" _SCREAMING_SNAKE_CASE = outputs.logits.argmax(-1 ).item() assert model.config.idalabel[predicted_idx] == "2" elif nlvr_model: _SCREAMING_SNAKE_CASE = torch.Size([1, 2] ) _SCREAMING_SNAKE_CASE = torch.tensor([-2.8721, 2.1291] ) assert torch.allclose(outputs.logits[0, :3] , SCREAMING_SNAKE_CASE_ , atol=1e-4 ) assert outputs.logits.shape == expected_shape Path(SCREAMING_SNAKE_CASE_ ).mkdir(exist_ok=SCREAMING_SNAKE_CASE_ ) print(F"Saving model and processor to {pytorch_dump_folder_path}" ) model.save_pretrained(SCREAMING_SNAKE_CASE_ ) processor.save_pretrained(SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": UpperCamelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--checkpoint_url", default="https://github.com/dandelin/ViLT/releases/download/200k/vilt_200k_mlm_itm.ckpt", type=str, help="URL of the checkpoint you'd like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) UpperCamelCase__ = parser.parse_args() convert_vilt_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
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 gc import random import unittest import numpy as np import torch from diffusers import DDIMScheduler, KandinskyVaaPipeline, KandinskyVaaPriorPipeline, UNetaDConditionModel, VQModel from diffusers.utils import floats_tensor, 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 (_lowerCamelCase , unittest.TestCase): """simple docstring""" SCREAMING_SNAKE_CASE = KandinskyVaaPipeline SCREAMING_SNAKE_CASE = [ 'image_embeds', 'negative_image_embeds', ] SCREAMING_SNAKE_CASE = ['image_embeds', 'negative_image_embeds'] SCREAMING_SNAKE_CASE = [ 'generator', 'height', 'width', 'latents', 'guidance_scale', '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 ) -> List[Any]: return 32 @property def UpperCamelCase ( self ) -> List[str]: return self.time_input_dim @property def UpperCamelCase ( self ) -> List[Any]: return self.time_input_dim * 4 @property def UpperCamelCase ( self ) -> Any: return 1_00 @property def UpperCamelCase ( self ) -> Any: 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""": """image""", """down_block_types""": ("""ResnetDownsampleBlock2D""", """SimpleCrossAttnDownBlock2D"""), """up_block_types""": ("""SimpleCrossAttnUpBlock2D""", """ResnetUpsampleBlock2D"""), """mid_block_type""": """UNetMidBlock2DSimpleCrossAttn""", """block_out_channels""": (self.block_out_channels_a, self.block_out_channels_a * 2), """layers_per_block""": 1, """encoder_hid_dim""": self.text_embedder_hidden_size, """encoder_hid_dim_type""": """image_proj""", """cross_attention_dim""": self.cross_attention_dim, """attention_head_dim""": 4, """resnet_time_scale_shift""": """scale_shift""", """class_embed_type""": None, } _SCREAMING_SNAKE_CASE = UNetaDConditionModel(**A__ ) return model @property def UpperCamelCase ( self ) -> List[Any]: return { "block_out_channels": [32, 64], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def UpperCamelCase ( self ) -> List[Any]: torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE = VQModel(**self.dummy_movq_kwargs ) return model def UpperCamelCase ( self ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE = self.dummy_unet _SCREAMING_SNAKE_CASE = self.dummy_movq _SCREAMING_SNAKE_CASE = DDIMScheduler( num_train_timesteps=10_00 , beta_schedule="""linear""" , beta_start=0.0_0085 , beta_end=0.012 , clip_sample=A__ , set_alpha_to_one=A__ , steps_offset=1 , prediction_type="""epsilon""" , thresholding=A__ , ) _SCREAMING_SNAKE_CASE = { """unet""": unet, """scheduler""": scheduler, """movq""": movq, } return components def UpperCamelCase ( self , A__ , A__=0 ) -> Tuple: _SCREAMING_SNAKE_CASE = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(A__ ) ).to(A__ ) _SCREAMING_SNAKE_CASE = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(seed + 1 ) ).to( A__ ) if str(A__ ).startswith("""mps""" ): _SCREAMING_SNAKE_CASE = torch.manual_seed(A__ ) else: _SCREAMING_SNAKE_CASE = torch.Generator(device=A__ ).manual_seed(A__ ) _SCREAMING_SNAKE_CASE = { """image_embeds""": image_embeds, """negative_image_embeds""": negative_image_embeds, """generator""": generator, """height""": 64, """width""": 64, """guidance_scale""": 4.0, """num_inference_steps""": 2, """output_type""": """np""", } return inputs def UpperCamelCase ( self ) -> Dict: _SCREAMING_SNAKE_CASE = """cpu""" _SCREAMING_SNAKE_CASE = self.get_dummy_components() _SCREAMING_SNAKE_CASE = self.pipeline_class(**A__ ) _SCREAMING_SNAKE_CASE = pipe.to(A__ ) pipe.set_progress_bar_config(disable=A__ ) _SCREAMING_SNAKE_CASE = pipe(**self.get_dummy_inputs(A__ ) ) _SCREAMING_SNAKE_CASE = output.images _SCREAMING_SNAKE_CASE = pipe( **self.get_dummy_inputs(A__ ) , return_dict=A__ , )[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.623_7976, 1.0, 0.3644_1332, 1.0, 0.7063_9634, 0.2987_7186, 0.8565_2125, 0.521_6843, 0.5445_4046] ) 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[str]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinskyv22/kandinskyv22_text2img_cat_fp16.npy""" ) _SCREAMING_SNAKE_CASE = KandinskyVaaPriorPipeline.from_pretrained( """kandinsky-community/kandinsky-2-2-prior""" , torch_dtype=torch.floataa ) pipe_prior.to(A__ ) _SCREAMING_SNAKE_CASE = KandinskyVaaPipeline.from_pretrained( """kandinsky-community/kandinsky-2-2-decoder""" , torch_dtype=torch.floataa ) _SCREAMING_SNAKE_CASE = pipeline.to(A__ ) pipeline.set_progress_bar_config(disable=A__ ) _SCREAMING_SNAKE_CASE = """red cat, 4k photo""" _SCREAMING_SNAKE_CASE = torch.Generator(device="""cuda""" ).manual_seed(0 ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = pipe_prior( A__ , generator=A__ , num_inference_steps=5 , negative_prompt="""""" , ).to_tuple() _SCREAMING_SNAKE_CASE = torch.Generator(device="""cuda""" ).manual_seed(0 ) _SCREAMING_SNAKE_CASE = pipeline( image_embeds=A__ , negative_image_embeds=A__ , generator=A__ , num_inference_steps=1_00 , output_type="""np""" , ) _SCREAMING_SNAKE_CASE = output.images[0] assert image.shape == (5_12, 5_12, 3) assert_mean_pixel_difference(A__ , 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 typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_torch_available from ...utils import OptionalDependencyNotAvailable UpperCamelCase__ : List[Any] = { "configuration_gpt_neox_japanese": ["GPT_NEOX_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP", "GPTNeoXJapaneseConfig"], "tokenization_gpt_neox_japanese": ["GPTNeoXJapaneseTokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Tuple = [ "GPT_NEOX_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST", "GPTNeoXJapaneseForCausalLM", "GPTNeoXJapaneseLayer", "GPTNeoXJapaneseModel", "GPTNeoXJapanesePreTrainedModel", ] if TYPE_CHECKING: from .configuration_gpt_neox_japanese import GPT_NEOX_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoXJapaneseConfig from .tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_neox_japanese import ( GPT_NEOX_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST, GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseLayer, GPTNeoXJapaneseModel, GPTNeoXJapanesePreTrainedModel, ) else: import sys UpperCamelCase__ : Tuple = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
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 TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) UpperCamelCase__ : Any = {"configuration_vit_mae": ["VIT_MAE_PRETRAINED_CONFIG_ARCHIVE_MAP", "ViTMAEConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Dict = [ "VIT_MAE_PRETRAINED_MODEL_ARCHIVE_LIST", "ViTMAEForPreTraining", "ViTMAELayer", "ViTMAEModel", "ViTMAEPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Tuple = [ "TFViTMAEForPreTraining", "TFViTMAEModel", "TFViTMAEPreTrainedModel", ] if TYPE_CHECKING: from .configuration_vit_mae import VIT_MAE_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMAEConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit_mae import ( VIT_MAE_PRETRAINED_MODEL_ARCHIVE_LIST, ViTMAEForPreTraining, ViTMAELayer, ViTMAEModel, ViTMAEPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vit_mae import TFViTMAEForPreTraining, TFViTMAEModel, TFViTMAEPreTrainedModel else: import sys UpperCamelCase__ : List[str] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
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
from __future__ import annotations def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = None ) -> None: """simple docstring""" if start is None: _SCREAMING_SNAKE_CASE = 0 if end is None: _SCREAMING_SNAKE_CASE = len(SCREAMING_SNAKE_CASE_ ) - 1 if start >= end: return _SCREAMING_SNAKE_CASE = (start + end) // 2 slowsort(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) slowsort(SCREAMING_SNAKE_CASE_ , mid + 1 , SCREAMING_SNAKE_CASE_ ) if sequence[end] < sequence[mid]: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = sequence[mid], sequence[end] slowsort(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , end - 1 ) if __name__ == "__main__": from doctest import testmod testmod()
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''' import argparse import json import math import os import time import traceback import zipfile from collections import Counter import requests def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE = None if token is not None: _SCREAMING_SNAKE_CASE = {"""Accept""": """application/vnd.github+json""", """Authorization""": F"Bearer {token}"} _SCREAMING_SNAKE_CASE = F"https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}/jobs?per_page=100" _SCREAMING_SNAKE_CASE = requests.get(SCREAMING_SNAKE_CASE_ , headers=SCREAMING_SNAKE_CASE_ ).json() _SCREAMING_SNAKE_CASE = {} try: job_links.update({job["""name"""]: job["""html_url"""] for job in result["""jobs"""]} ) _SCREAMING_SNAKE_CASE = math.ceil((result["""total_count"""] - 1_00) / 1_00 ) for i in range(SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE = requests.get(url + F"&page={i + 2}" , headers=SCREAMING_SNAKE_CASE_ ).json() job_links.update({job["""name"""]: job["""html_url"""] for job in result["""jobs"""]} ) return job_links except Exception: print(F"Unknown error, could not fetch links:\n{traceback.format_exc()}" ) return {} def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = None if token is not None: _SCREAMING_SNAKE_CASE = {"""Accept""": """application/vnd.github+json""", """Authorization""": F"Bearer {token}"} _SCREAMING_SNAKE_CASE = F"https://api.github.com/repos/huggingface/transformers/actions/runs/{worflow_run_id}/artifacts?per_page=100" _SCREAMING_SNAKE_CASE = requests.get(SCREAMING_SNAKE_CASE_ , headers=SCREAMING_SNAKE_CASE_ ).json() _SCREAMING_SNAKE_CASE = {} try: artifacts.update({artifact["""name"""]: artifact["""archive_download_url"""] for artifact in result["""artifacts"""]} ) _SCREAMING_SNAKE_CASE = math.ceil((result["""total_count"""] - 1_00) / 1_00 ) for i in range(SCREAMING_SNAKE_CASE_ ): _SCREAMING_SNAKE_CASE = requests.get(url + F"&page={i + 2}" , headers=SCREAMING_SNAKE_CASE_ ).json() artifacts.update({artifact["""name"""]: artifact["""archive_download_url"""] for artifact in result["""artifacts"""]} ) return artifacts except Exception: print(F"Unknown error, could not fetch links:\n{traceback.format_exc()}" ) return {} def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE = None if token is not None: _SCREAMING_SNAKE_CASE = {"""Accept""": """application/vnd.github+json""", """Authorization""": F"Bearer {token}"} _SCREAMING_SNAKE_CASE = requests.get(SCREAMING_SNAKE_CASE_ , headers=SCREAMING_SNAKE_CASE_ , allow_redirects=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = result.headers["""Location"""] _SCREAMING_SNAKE_CASE = requests.get(SCREAMING_SNAKE_CASE_ , allow_redirects=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = os.path.join(SCREAMING_SNAKE_CASE_ , F"{artifact_name}.zip" ) with open(SCREAMING_SNAKE_CASE_ , """wb""" ) as fp: fp.write(response.content ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = None with zipfile.ZipFile(SCREAMING_SNAKE_CASE_ ) as z: for filename in z.namelist(): if not os.path.isdir(SCREAMING_SNAKE_CASE_ ): # read the file if filename in ["failures_line.txt", "summary_short.txt", "job_name.txt"]: with z.open(SCREAMING_SNAKE_CASE_ ) as f: for line in f: _SCREAMING_SNAKE_CASE = line.decode("""UTF-8""" ).strip() if filename == "failures_line.txt": try: # `error_line` is the place where `error` occurs _SCREAMING_SNAKE_CASE = line[: line.index(""": """ )] _SCREAMING_SNAKE_CASE = line[line.index(""": """ ) + len(""": """ ) :] errors.append([error_line, error] ) except Exception: # skip un-related lines pass elif filename == "summary_short.txt" and line.startswith("""FAILED """ ): # `test` is the test method that failed _SCREAMING_SNAKE_CASE = line[len("""FAILED """ ) :] failed_tests.append(SCREAMING_SNAKE_CASE_ ) elif filename == "job_name.txt": _SCREAMING_SNAKE_CASE = line if len(SCREAMING_SNAKE_CASE_ ) != len(SCREAMING_SNAKE_CASE_ ): raise ValueError( F"`errors` and `failed_tests` should have the same number of elements. Got {len(SCREAMING_SNAKE_CASE_ )} for `errors` " F"and {len(SCREAMING_SNAKE_CASE_ )} for `failed_tests` instead. The test reports in {artifact_zip_path} have some" """ problem.""" ) _SCREAMING_SNAKE_CASE = None if job_name and job_links: _SCREAMING_SNAKE_CASE = job_links.get(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # A list with elements of the form (line of error, error, failed test) _SCREAMING_SNAKE_CASE = [x + [y] + [job_link] for x, y in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )] return result def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = [os.path.join(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) for p in os.listdir(SCREAMING_SNAKE_CASE_ ) if p.endswith(""".zip""" )] for p in paths: errors.extend(get_errors_from_single_artifact(SCREAMING_SNAKE_CASE_ , job_links=SCREAMING_SNAKE_CASE_ ) ) return errors def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = Counter() counter.update([x[1] for x in logs] ) _SCREAMING_SNAKE_CASE = counter.most_common() _SCREAMING_SNAKE_CASE = {} for error, count in counts: if error_filter is None or error not in error_filter: _SCREAMING_SNAKE_CASE = {"""count""": count, """failed_tests""": [(x[2], x[0]) for x in logs if x[1] == error]} _SCREAMING_SNAKE_CASE = dict(sorted(r.items() , key=lambda SCREAMING_SNAKE_CASE_ : item[1]["count"] , reverse=SCREAMING_SNAKE_CASE_ ) ) return r def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE = test.split("""::""" )[0] if test.startswith("""tests/models/""" ): _SCREAMING_SNAKE_CASE = test.split("""/""" )[2] else: _SCREAMING_SNAKE_CASE = None return test def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=None ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = [(x[0], x[1], get_model(x[2] )) for x in logs] _SCREAMING_SNAKE_CASE = [x for x in logs if x[2] is not None] _SCREAMING_SNAKE_CASE = {x[2] for x in logs} _SCREAMING_SNAKE_CASE = {} for test in tests: _SCREAMING_SNAKE_CASE = Counter() # count by errors in `test` counter.update([x[1] for x in logs if x[2] == test] ) _SCREAMING_SNAKE_CASE = counter.most_common() _SCREAMING_SNAKE_CASE = {error: count for error, count in counts if (error_filter is None or error not in error_filter)} _SCREAMING_SNAKE_CASE = sum(error_counts.values() ) if n_errors > 0: _SCREAMING_SNAKE_CASE = {"""count""": n_errors, """errors""": error_counts} _SCREAMING_SNAKE_CASE = dict(sorted(r.items() , key=lambda SCREAMING_SNAKE_CASE_ : item[1]["count"] , reverse=SCREAMING_SNAKE_CASE_ ) ) return r def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE = """| no. | error | status |""" _SCREAMING_SNAKE_CASE = """|-:|:-|:-|""" _SCREAMING_SNAKE_CASE = [header, sep] for error in reduced_by_error: _SCREAMING_SNAKE_CASE = reduced_by_error[error]["""count"""] _SCREAMING_SNAKE_CASE = F"| {count} | {error[:1_00]} | |" lines.append(SCREAMING_SNAKE_CASE_ ) return "\n".join(SCREAMING_SNAKE_CASE_ ) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE = """| model | no. of errors | major error | count |""" _SCREAMING_SNAKE_CASE = """|-:|-:|-:|-:|""" _SCREAMING_SNAKE_CASE = [header, sep] for model in reduced_by_model: _SCREAMING_SNAKE_CASE = reduced_by_model[model]["""count"""] _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = list(reduced_by_model[model]["""errors"""].items() )[0] _SCREAMING_SNAKE_CASE = F"| {model} | {count} | {error[:60]} | {_count} |" lines.append(SCREAMING_SNAKE_CASE_ ) return "\n".join(SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": UpperCamelCase__ : int = argparse.ArgumentParser() # Required parameters parser.add_argument("--workflow_run_id", type=str, required=True, help="A GitHub Actions workflow run id.") parser.add_argument( "--output_dir", type=str, required=True, help="Where to store the downloaded artifacts and other result files.", ) parser.add_argument("--token", default=None, type=str, help="A token that has actions:read permission.") UpperCamelCase__ : str = parser.parse_args() os.makedirs(args.output_dir, exist_ok=True) UpperCamelCase__ : List[Any] = get_job_links(args.workflow_run_id, token=args.token) UpperCamelCase__ : List[str] = {} # To deal with `workflow_call` event, where a job name is the combination of the job names in the caller and callee. # For example, `PyTorch 1.11 / Model tests (models/albert, single-gpu)`. if _job_links: for k, v in _job_links.items(): # This is how GitHub actions combine job names. if " / " in k: UpperCamelCase__ : Dict = k.find(" / ") UpperCamelCase__ : Optional[Any] = k[index + len(" / ") :] UpperCamelCase__ : List[str] = v with open(os.path.join(args.output_dir, "job_links.json"), "w", encoding="UTF-8") as fp: json.dump(job_links, fp, ensure_ascii=False, indent=4) UpperCamelCase__ : List[str] = get_artifacts_links(args.workflow_run_id, token=args.token) with open(os.path.join(args.output_dir, "artifacts.json"), "w", encoding="UTF-8") as fp: json.dump(artifacts, fp, ensure_ascii=False, indent=4) for idx, (name, url) in enumerate(artifacts.items()): download_artifact(name, url, args.output_dir, args.token) # Be gentle to GitHub time.sleep(1) UpperCamelCase__ : List[str] = get_all_errors(args.output_dir, job_links=job_links) # `e[1]` is the error UpperCamelCase__ : str = Counter() counter.update([e[1] for e in errors]) # print the top 30 most common test errors UpperCamelCase__ : List[str] = counter.most_common(30) for item in most_common: print(item) with open(os.path.join(args.output_dir, "errors.json"), "w", encoding="UTF-8") as fp: json.dump(errors, fp, ensure_ascii=False, indent=4) UpperCamelCase__ : Optional[Any] = reduce_by_error(errors) UpperCamelCase__ : Optional[int] = reduce_by_model(errors) UpperCamelCase__ : Optional[int] = make_github_table(reduced_by_error) UpperCamelCase__ : Optional[Any] = make_github_table_per_model(reduced_by_model) with open(os.path.join(args.output_dir, "reduced_by_error.txt"), "w", encoding="UTF-8") as fp: fp.write(sa) with open(os.path.join(args.output_dir, "reduced_by_model.txt"), "w", encoding="UTF-8") as fp: fp.write(sa)
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 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""" _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)
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_torch_available UpperCamelCase__ : Optional[int] = { "configuration_mctct": ["MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP", "MCTCTConfig"], "feature_extraction_mctct": ["MCTCTFeatureExtractor"], "processing_mctct": ["MCTCTProcessor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Optional[Any] = [ "MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST", "MCTCTForCTC", "MCTCTModel", "MCTCTPreTrainedModel", ] if TYPE_CHECKING: from .configuration_mctct import MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP, MCTCTConfig from .feature_extraction_mctct import MCTCTFeatureExtractor from .processing_mctct import MCTCTProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mctct import MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST, MCTCTForCTC, MCTCTModel, MCTCTPreTrainedModel else: import sys UpperCamelCase__ : int = _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 itertools import permutations def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> bool: """simple docstring""" if num[3] % 2 != 0: return False if (num[2] + num[3] + num[4]) % 3 != 0: return False if num[5] % 5 != 0: return False _SCREAMING_SNAKE_CASE = [7, 11, 13, 17] for i, test in enumerate(SCREAMING_SNAKE_CASE_ ): if (num[i + 4] * 1_00 + num[i + 5] * 10 + num[i + 6]) % test != 0: return False return True def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ = 10 ) -> int: """simple docstring""" return sum( int("""""".join(map(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) ) for num in permutations(range(SCREAMING_SNAKE_CASE_ ) ) if is_substring_divisible(SCREAMING_SNAKE_CASE_ ) ) if __name__ == "__main__": print(f"""{solution() = }""")
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 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__ : Optional[int] = logging.get_logger(__name__) def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=False , SCREAMING_SNAKE_CASE_=False ) -> List[str]: """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 ) -> Union[str, Any]: """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_ ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE = dct.pop(SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = val def lowerCAmelCase_ ( ) -> List[str]: """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_=False ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE = False if """rvlcdip""" in checkpoint_url else True _SCREAMING_SNAKE_CASE = BeitConfig(use_absolute_position_embeddings=SCREAMING_SNAKE_CASE_ , use_mask_token=SCREAMING_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(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()} # load state_dict of original model, remove and rename some keys _SCREAMING_SNAKE_CASE = torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE_ , map_location="""cpu""" )["""model"""] _SCREAMING_SNAKE_CASE = create_rename_keys(SCREAMING_SNAKE_CASE_ , has_lm_head=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_ , has_lm_head=SCREAMING_SNAKE_CASE_ ) # load HuggingFace model _SCREAMING_SNAKE_CASE = BeitForMaskedImageModeling(SCREAMING_SNAKE_CASE_ ) if has_lm_head else BeitForImageClassification(SCREAMING_SNAKE_CASE_ ) model.eval() model.load_state_dict(SCREAMING_SNAKE_CASE_ ) # Check outputs on an image _SCREAMING_SNAKE_CASE = BeitImageProcessor( size=config.image_size , resample=PILImageResampling.BILINEAR , do_center_crop=SCREAMING_SNAKE_CASE_ ) _SCREAMING_SNAKE_CASE = prepare_img() _SCREAMING_SNAKE_CASE = image_processor(images=SCREAMING_SNAKE_CASE_ , return_tensors="""pt""" ) _SCREAMING_SNAKE_CASE = encoding["""pixel_values"""] _SCREAMING_SNAKE_CASE = model(SCREAMING_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(SCREAMING_SNAKE_CASE_ ), "Shape of logits not as expected" Path(SCREAMING_SNAKE_CASE_ ).mkdir(exist_ok=SCREAMING_SNAKE_CASE_ ) print(F"Saving model 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 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(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) , organization="""nielsr""" , commit_message="""Add image processor""" , use_temp_dir=SCREAMING_SNAKE_CASE_ , ) model.push_to_hub( repo_path_or_name=Path(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) , organization="""nielsr""" , commit_message="""Add model""" , use_temp_dir=SCREAMING_SNAKE_CASE_ , ) if __name__ == "__main__": UpperCamelCase__ : Tuple = 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__ : Any = parser.parse_args() convert_dit_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub)
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''' def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> list: """simple docstring""" # bit count represents no. of bits in the gray code if bit_count < 0: raise ValueError("""The given input must be positive""" ) # get the generated string sequence _SCREAMING_SNAKE_CASE = gray_code_sequence_string(SCREAMING_SNAKE_CASE_ ) # # convert them to integers for i in range(len(SCREAMING_SNAKE_CASE_ ) ): _SCREAMING_SNAKE_CASE = int(sequence[i] , 2 ) return sequence def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE_ ) -> list: """simple docstring""" # The approach is a recursive one # Base case achieved when either n = 0 or n=1 if bit_count == 0: return ["0"] if bit_count == 1: return ["0", "1"] _SCREAMING_SNAKE_CASE = 1 << bit_count # defines the length of the sequence # 1<< n is equivalent to 2^n # recursive answer will generate answer for n-1 bits _SCREAMING_SNAKE_CASE = gray_code_sequence_string(bit_count - 1 ) _SCREAMING_SNAKE_CASE = [] # append 0 to first half of the smaller sequence generated for i in range(seq_len // 2 ): _SCREAMING_SNAKE_CASE = """0""" + smaller_sequence[i] sequence.append(SCREAMING_SNAKE_CASE_ ) # append 1 to second half ... start from the end of the list for i in reversed(range(seq_len // 2 ) ): _SCREAMING_SNAKE_CASE = """1""" + smaller_sequence[i] sequence.append(SCREAMING_SNAKE_CASE_ ) return sequence if __name__ == "__main__": import doctest doctest.testmod()
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 ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase__ : int = 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 (_lowerCamelCase): """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__ , ) -> 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__(**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
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_squeezebert import SqueezeBertTokenizer lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} lowerCAmelCase__ = { "vocab_file": { "squeezebert/squeezebert-uncased": ( "https://huggingface.co/squeezebert/squeezebert-uncased/resolve/main/vocab.txt" ), "squeezebert/squeezebert-mnli": "https://huggingface.co/squeezebert/squeezebert-mnli/resolve/main/vocab.txt", "squeezebert/squeezebert-mnli-headless": ( "https://huggingface.co/squeezebert/squeezebert-mnli-headless/resolve/main/vocab.txt" ), }, "tokenizer_file": { "squeezebert/squeezebert-uncased": ( "https://huggingface.co/squeezebert/squeezebert-uncased/resolve/main/tokenizer.json" ), "squeezebert/squeezebert-mnli": ( "https://huggingface.co/squeezebert/squeezebert-mnli/resolve/main/tokenizer.json" ), "squeezebert/squeezebert-mnli-headless": ( "https://huggingface.co/squeezebert/squeezebert-mnli-headless/resolve/main/tokenizer.json" ), }, } lowerCAmelCase__ = { "squeezebert/squeezebert-uncased": 512, "squeezebert/squeezebert-mnli": 512, "squeezebert/squeezebert-mnli-headless": 512, } lowerCAmelCase__ = { "squeezebert/squeezebert-uncased": {"do_lower_case": True}, "squeezebert/squeezebert-mnli": {"do_lower_case": True}, "squeezebert/squeezebert-mnli-headless": {"do_lower_case": True}, } class __magic_name__ ( _snake_case ): UpperCAmelCase = VOCAB_FILES_NAMES UpperCAmelCase = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase = PRETRAINED_INIT_CONFIGURATION UpperCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase = SqueezeBertTokenizer def __init__( self : Tuple , lowerCAmelCase__ : Any=None , lowerCAmelCase__ : int=None , lowerCAmelCase__ : Tuple=True , lowerCAmelCase__ : Union[str, Any]="[UNK]" , lowerCAmelCase__ : Any="[SEP]" , lowerCAmelCase__ : Optional[Any]="[PAD]" , lowerCAmelCase__ : str="[CLS]" , lowerCAmelCase__ : Dict="[MASK]" , lowerCAmelCase__ : str=True , lowerCAmelCase__ : Union[str, Any]=None , **lowerCAmelCase__ : Any , ) -> List[str]: super().__init__( lowerCAmelCase__ , tokenizer_file=lowerCAmelCase__ , do_lower_case=lowerCAmelCase__ , unk_token=lowerCAmelCase__ , sep_token=lowerCAmelCase__ , pad_token=lowerCAmelCase__ , cls_token=lowerCAmelCase__ , mask_token=lowerCAmelCase__ , tokenize_chinese_chars=lowerCAmelCase__ , strip_accents=lowerCAmelCase__ , **lowerCAmelCase__ , ) UpperCAmelCase = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("lowercase" , lowerCAmelCase__ ) != do_lower_case or normalizer_state.get("strip_accents" , lowerCAmelCase__ ) != strip_accents or normalizer_state.get("handle_chinese_chars" , lowerCAmelCase__ ) != tokenize_chinese_chars ): UpperCAmelCase = getattr(lowerCAmelCase__ , normalizer_state.pop("type" ) ) UpperCAmelCase = do_lower_case UpperCAmelCase = strip_accents UpperCAmelCase = tokenize_chinese_chars UpperCAmelCase = normalizer_class(**lowerCAmelCase__ ) UpperCAmelCase = do_lower_case def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : str=None ) -> List[Any]: UpperCAmelCase = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None ) -> List[int]: UpperCAmelCase = [self.sep_token_id] UpperCAmelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def _UpperCamelCase ( self : Dict , lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[str] = None ) -> Tuple[str]: UpperCAmelCase = self._tokenizer.model.save(lowerCAmelCase__ , name=lowerCAmelCase__ ) return tuple(lowerCAmelCase__ )
1
import copy import os import cva import numpy as np from matplotlib import pyplot as plt class __magic_name__ : def __init__( self : Optional[int] ) -> Optional[Any]: UpperCAmelCase = "" UpperCAmelCase = "" UpperCAmelCase = [] UpperCAmelCase = 0 UpperCAmelCase = 2_5_6 UpperCAmelCase = 0 UpperCAmelCase = 0 UpperCAmelCase = 0 UpperCAmelCase = 0 def _UpperCamelCase ( self : Any , lowerCAmelCase__ : Optional[Any] ) -> List[str]: UpperCAmelCase = cva.imread(lowerCAmelCase__ , 0 ) UpperCAmelCase = copy.deepcopy(self.img ) UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = plt.hist(self.img.ravel() , 2_5_6 , [0, 2_5_6] , label="x" ) UpperCAmelCase = np.sum(lowerCAmelCase__ ) for i in range(len(lowerCAmelCase__ ) ): UpperCAmelCase = x[i] / self.k self.sk += prk UpperCAmelCase = (self.L - 1) * self.sk if self.rem != 0: UpperCAmelCase = int(last % last ) UpperCAmelCase = int(last + 1 if self.rem >= 0.5 else last ) self.last_list.append(lowerCAmelCase__ ) UpperCAmelCase = int(np.ma.count(self.img ) / self.img[1].size ) UpperCAmelCase = self.img[1].size for i in range(self.number_of_cols ): for j in range(self.number_of_rows ): UpperCAmelCase = self.img[j][i] if num != self.last_list[num]: UpperCAmelCase = self.last_list[num] cva.imwrite("output_data/output.jpg" , self.img ) def _UpperCamelCase ( self : str ) -> int: plt.hist(self.img.ravel() , 2_5_6 , [0, 2_5_6] ) def _UpperCamelCase ( self : Dict ) -> Optional[Any]: cva.imshow("Output-Image" , self.img ) cva.imshow("Input-Image" , self.original_image ) cva.waitKey(5_0_0_0 ) cva.destroyAllWindows() if __name__ == "__main__": lowerCAmelCase__ = os.path.join(os.path.basename(__file__), "image_data/input.jpg") lowerCAmelCase__ = ConstantStretch() stretcher.stretch(file_path) stretcher.plot_histogram() stretcher.show_image()
1
1
import numpy as np from nltk.translate import meteor_score import datasets from datasets.config import importlib_metadata, version lowerCAmelCase__ = version.parse(importlib_metadata.version("nltk")) if NLTK_VERSION >= version.Version("3.6.4"): from nltk import word_tokenize lowerCAmelCase__ = "\\n@inproceedings{banarjee2005,\n title = {{METEOR}: An Automatic Metric for {MT} Evaluation with Improved Correlation with Human Judgments},\n author = {Banerjee, Satanjeev and Lavie, Alon},\n booktitle = {Proceedings of the {ACL} Workshop on Intrinsic and Extrinsic Evaluation Measures for Machine Translation and/or Summarization},\n month = jun,\n year = {2005},\n address = {Ann Arbor, Michigan},\n publisher = {Association for Computational Linguistics},\n url = {https://www.aclweb.org/anthology/W05-0909},\n pages = {65--72},\n}\n" lowerCAmelCase__ = "\\nMETEOR, an automatic metric for machine translation evaluation\nthat is based on a generalized concept of unigram matching between the\nmachine-produced translation and human-produced reference translations.\nUnigrams can be matched based on their surface forms, stemmed forms,\nand meanings; furthermore, METEOR can be easily extended to include more\nadvanced matching strategies. Once all generalized unigram matches\nbetween the two strings have been found, METEOR computes a score for\nthis matching using a combination of unigram-precision, unigram-recall, and\na measure of fragmentation that is designed to directly capture how\nwell-ordered the matched words in the machine translation are in relation\nto the reference.\n\nMETEOR gets an R correlation value of 0.347 with human evaluation on the Arabic\ndata and 0.331 on the Chinese data. This is shown to be an improvement on\nusing simply unigram-precision, unigram-recall and their harmonic F1\ncombination.\n" lowerCAmelCase__ = "\nComputes METEOR score of translated segments against one or more references.\nArgs:\n predictions: list of predictions to score. Each prediction\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\n alpha: Parameter for controlling relative weights of precision and recall. default: 0.9\n beta: Parameter for controlling shape of penalty as a function of fragmentation. default: 3\n gamma: Relative weight assigned to fragmentation penalty. default: 0.5\nReturns:\n 'meteor': meteor score.\nExamples:\n\n >>> meteor = datasets.load_metric('meteor')\n >>> predictions = [\"It is a guide to action which ensures that the military always obeys the commands of the party\"]\n >>> references = [\"It is a guide to action that ensures that the military will forever heed Party commands\"]\n >>> results = meteor.compute(predictions=predictions, references=references)\n >>> print(round(results[\"meteor\"], 4))\n 0.6944\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): def _UpperCamelCase ( self : int ) -> str: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Value("string" , id="sequence" ), } ) , codebase_urls=["https://github.com/nltk/nltk/blob/develop/nltk/translate/meteor_score.py"] , reference_urls=[ "https://www.nltk.org/api/nltk.translate.html#module-nltk.translate.meteor_score", "https://en.wikipedia.org/wiki/METEOR", ] , ) def _UpperCamelCase ( self : Dict , lowerCAmelCase__ : List[Any] ) -> Dict: import nltk nltk.download("wordnet" ) if NLTK_VERSION >= version.Version("3.6.5" ): nltk.download("punkt" ) if NLTK_VERSION >= version.Version("3.6.6" ): nltk.download("omw-1.4" ) def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Any=0.9 , lowerCAmelCase__ : Tuple=3 , lowerCAmelCase__ : Optional[int]=0.5 ) -> Any: if NLTK_VERSION >= version.Version("3.6.5" ): UpperCAmelCase = [ meteor_score.single_meteor_score( word_tokenize(lowerCAmelCase__ ) , word_tokenize(lowerCAmelCase__ ) , alpha=lowerCAmelCase__ , beta=lowerCAmelCase__ , gamma=lowerCAmelCase__ ) for ref, pred in zip(lowerCAmelCase__ , lowerCAmelCase__ ) ] else: UpperCAmelCase = [ meteor_score.single_meteor_score(lowerCAmelCase__ , lowerCAmelCase__ , alpha=lowerCAmelCase__ , beta=lowerCAmelCase__ , gamma=lowerCAmelCase__ ) for ref, pred in zip(lowerCAmelCase__ , lowerCAmelCase__ ) ] return {"meteor": np.mean(lowerCAmelCase__ )}
1
import json import os import unittest from transformers import BatchEncoding, LEDTokenizer, LEDTokenizerFast from transformers.models.led.tokenization_led import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, require_torch from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __magic_name__ ( _snake_case , unittest.TestCase ): UpperCAmelCase = LEDTokenizer UpperCAmelCase = LEDTokenizerFast UpperCAmelCase = True def _UpperCamelCase ( self : Tuple ) -> Union[str, Any]: super().setUp() UpperCAmelCase = [ "l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "\u0120", "\u0120l", "\u0120n", "\u0120lo", "\u0120low", "er", "\u0120lowest", "\u0120newer", "\u0120wider", "<unk>", ] UpperCAmelCase = dict(zip(lowerCAmelCase__ , range(len(lowerCAmelCase__ ) ) ) ) UpperCAmelCase = ["#version: 0.2", "\u0120 l", "\u0120l o", "\u0120lo w", "e r", ""] UpperCAmelCase = {"unk_token": "<unk>"} UpperCAmelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) UpperCAmelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as fp: fp.write(json.dumps(lowerCAmelCase__ ) + "\n" ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(lowerCAmelCase__ ) ) def _UpperCamelCase ( self : Union[str, Any] , **lowerCAmelCase__ : Optional[int] ) -> Optional[int]: kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **lowerCAmelCase__ ) def _UpperCamelCase ( self : str , **lowerCAmelCase__ : str ) -> Optional[int]: kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : List[Any] ) -> List[Any]: return "lower newer", "lower newer" @cached_property def _UpperCamelCase ( self : Dict ) -> str: return LEDTokenizer.from_pretrained("allenai/led-base-16384" ) @cached_property def _UpperCamelCase ( self : int ) -> Tuple: return LEDTokenizerFast.from_pretrained("allenai/led-base-16384" ) @require_torch def _UpperCamelCase ( self : Tuple ) -> List[str]: UpperCAmelCase = ["A long paragraph for summarization.", "Another paragraph for summarization."] UpperCAmelCase = [0, 2_5_0, 2_5_1, 1_7_8_1_8, 1_3, 3_9_1_8_6, 1_9_3_8, 4, 2] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer(lowerCAmelCase__ , max_length=len(lowerCAmelCase__ ) , padding=lowerCAmelCase__ , return_tensors="pt" ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual((2, 9) , batch.input_ids.shape ) self.assertEqual((2, 9) , batch.attention_mask.shape ) UpperCAmelCase = batch.input_ids.tolist()[0] self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ ) @require_torch def _UpperCamelCase ( self : Union[str, Any] ) -> List[Any]: UpperCAmelCase = ["A long paragraph for summarization.", "Another paragraph for summarization."] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer(lowerCAmelCase__ , padding=lowerCAmelCase__ , return_tensors="pt" ) self.assertIn("input_ids" , lowerCAmelCase__ ) self.assertIn("attention_mask" , lowerCAmelCase__ ) self.assertNotIn("labels" , lowerCAmelCase__ ) self.assertNotIn("decoder_attention_mask" , lowerCAmelCase__ ) @require_torch def _UpperCamelCase ( self : int ) -> int: UpperCAmelCase = [ "Summary of the text.", "Another summary.", ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer(text_target=lowerCAmelCase__ , max_length=3_2 , padding="max_length" , return_tensors="pt" ) self.assertEqual(3_2 , targets["input_ids"].shape[1] ) @require_torch def _UpperCamelCase ( self : Any ) -> int: for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer( ["I am a small frog" * 1_0_2_4, "I am a small frog"] , padding=lowerCAmelCase__ , truncation=lowerCAmelCase__ , return_tensors="pt" ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual(batch.input_ids.shape , (2, 5_1_2_2) ) @require_torch def _UpperCamelCase ( self : Dict ) -> Tuple: UpperCAmelCase = ["A long paragraph for summarization."] UpperCAmelCase = [ "Summary of the text.", ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer(lowerCAmelCase__ , return_tensors="pt" ) UpperCAmelCase = tokenizer(text_target=lowerCAmelCase__ , return_tensors="pt" ) UpperCAmelCase = inputs["input_ids"] UpperCAmelCase = targets["input_ids"] self.assertTrue((input_ids[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((labels[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((input_ids[:, -1] == tokenizer.eos_token_id).all().item() ) self.assertTrue((labels[:, -1] == tokenizer.eos_token_id).all().item() ) @require_torch def _UpperCamelCase ( self : Optional[int] ) -> Optional[Any]: for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = ["Summary of the text.", "Another summary."] UpperCAmelCase = [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -1, -1]] UpperCAmelCase = tokenizer(lowerCAmelCase__ , padding=lowerCAmelCase__ ) UpperCAmelCase = [[0] * len(lowerCAmelCase__ ) for x in encoded_output["input_ids"]] UpperCAmelCase = tokenizer.pad(lowerCAmelCase__ ) self.assertSequenceEqual(outputs["global_attention_mask"] , lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] ) -> int: pass def _UpperCamelCase ( self : Optional[Any] ) -> Union[str, Any]: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})" ): UpperCAmelCase = self.rust_tokenizer_class.from_pretrained(lowerCAmelCase__ , **lowerCAmelCase__ ) UpperCAmelCase = self.tokenizer_class.from_pretrained(lowerCAmelCase__ , **lowerCAmelCase__ ) UpperCAmelCase = "A, <mask> AllenNLP sentence." UpperCAmelCase = tokenizer_r.encode_plus(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ , return_token_type_ids=lowerCAmelCase__ ) UpperCAmelCase = tokenizer_p.encode_plus(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ , return_token_type_ids=lowerCAmelCase__ ) self.assertEqual(sum(tokens_r["token_type_ids"] ) , sum(tokens_p["token_type_ids"] ) ) self.assertEqual( sum(tokens_r["attention_mask"] ) / len(tokens_r["attention_mask"] ) , sum(tokens_p["attention_mask"] ) / len(tokens_p["attention_mask"] ) , ) UpperCAmelCase = tokenizer_r.convert_ids_to_tokens(tokens_r["input_ids"] ) UpperCAmelCase = tokenizer_p.convert_ids_to_tokens(tokens_p["input_ids"] ) self.assertSequenceEqual(tokens_p["input_ids"] , [0, 2_5_0, 6, 5_0_2_6_4, 3_8_2_3, 4_8_7, 2_1_9_9_2, 3_6_4_5, 4, 2] ) self.assertSequenceEqual(tokens_r["input_ids"] , [0, 2_5_0, 6, 5_0_2_6_4, 3_8_2_3, 4_8_7, 2_1_9_9_2, 3_6_4_5, 4, 2] ) self.assertSequenceEqual( lowerCAmelCase__ , ["<s>", "A", ",", "<mask>", "ĠAllen", "N", "LP", "Ġsentence", ".", "</s>"] ) self.assertSequenceEqual( lowerCAmelCase__ , ["<s>", "A", ",", "<mask>", "ĠAllen", "N", "LP", "Ġsentence", ".", "</s>"] )
1
1
def _lowerCAmelCase( __A , __A ): return (pointa[0] - pointa[0]) ** 2 + (pointa[1] - pointa[1]) ** 2 def _lowerCAmelCase( __A , __A=0 ): return sorted(__A , key=lambda __A : x[column] ) def _lowerCAmelCase( __A , __A , __A=float("inf" ) ): for i in range(points_counts - 1 ): for j in range(i + 1 , __A ): UpperCAmelCase = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: UpperCAmelCase = current_dis return min_dis def _lowerCAmelCase( __A , __A , __A=float("inf" ) ): for i in range(min(6 , points_counts - 1 ) , __A ): for j in range(max(0 , i - 6 ) , __A ): UpperCAmelCase = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: UpperCAmelCase = current_dis return min_dis def _lowerCAmelCase( __A , __A , __A ): # base case if points_counts <= 3: return dis_between_closest_pair(__A , __A ) # recursion UpperCAmelCase = points_counts // 2 UpperCAmelCase = closest_pair_of_points_sqr( __A , points_sorted_on_y[:mid] , __A ) UpperCAmelCase = closest_pair_of_points_sqr( __A , points_sorted_on_y[mid:] , points_counts - mid ) UpperCAmelCase = min(__A , __A ) UpperCAmelCase = [] for point in points_sorted_on_x: if abs(point[0] - points_sorted_on_x[mid][0] ) < closest_pair_dis: cross_strip.append(__A ) UpperCAmelCase = dis_between_closest_in_strip( __A , len(__A ) , __A ) return min(__A , __A ) def _lowerCAmelCase( __A , __A ): UpperCAmelCase = column_based_sort(__A , column=0 ) UpperCAmelCase = column_based_sort(__A , column=1 ) return ( closest_pair_of_points_sqr( __A , __A , __A ) ) ** 0.5 if __name__ == "__main__": lowerCAmelCase__ = [(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)] print("Distance:", closest_pair_of_points(points, len(points)))
1
import numpy as np from nltk.translate import meteor_score import datasets from datasets.config import importlib_metadata, version lowerCAmelCase__ = version.parse(importlib_metadata.version("nltk")) if NLTK_VERSION >= version.Version("3.6.4"): from nltk import word_tokenize lowerCAmelCase__ = "\\n@inproceedings{banarjee2005,\n title = {{METEOR}: An Automatic Metric for {MT} Evaluation with Improved Correlation with Human Judgments},\n author = {Banerjee, Satanjeev and Lavie, Alon},\n booktitle = {Proceedings of the {ACL} Workshop on Intrinsic and Extrinsic Evaluation Measures for Machine Translation and/or Summarization},\n month = jun,\n year = {2005},\n address = {Ann Arbor, Michigan},\n publisher = {Association for Computational Linguistics},\n url = {https://www.aclweb.org/anthology/W05-0909},\n pages = {65--72},\n}\n" lowerCAmelCase__ = "\\nMETEOR, an automatic metric for machine translation evaluation\nthat is based on a generalized concept of unigram matching between the\nmachine-produced translation and human-produced reference translations.\nUnigrams can be matched based on their surface forms, stemmed forms,\nand meanings; furthermore, METEOR can be easily extended to include more\nadvanced matching strategies. Once all generalized unigram matches\nbetween the two strings have been found, METEOR computes a score for\nthis matching using a combination of unigram-precision, unigram-recall, and\na measure of fragmentation that is designed to directly capture how\nwell-ordered the matched words in the machine translation are in relation\nto the reference.\n\nMETEOR gets an R correlation value of 0.347 with human evaluation on the Arabic\ndata and 0.331 on the Chinese data. This is shown to be an improvement on\nusing simply unigram-precision, unigram-recall and their harmonic F1\ncombination.\n" lowerCAmelCase__ = "\nComputes METEOR score of translated segments against one or more references.\nArgs:\n predictions: list of predictions to score. Each prediction\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\n alpha: Parameter for controlling relative weights of precision and recall. default: 0.9\n beta: Parameter for controlling shape of penalty as a function of fragmentation. default: 3\n gamma: Relative weight assigned to fragmentation penalty. default: 0.5\nReturns:\n 'meteor': meteor score.\nExamples:\n\n >>> meteor = datasets.load_metric('meteor')\n >>> predictions = [\"It is a guide to action which ensures that the military always obeys the commands of the party\"]\n >>> references = [\"It is a guide to action that ensures that the military will forever heed Party commands\"]\n >>> results = meteor.compute(predictions=predictions, references=references)\n >>> print(round(results[\"meteor\"], 4))\n 0.6944\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): def _UpperCamelCase ( self : int ) -> str: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Value("string" , id="sequence" ), } ) , codebase_urls=["https://github.com/nltk/nltk/blob/develop/nltk/translate/meteor_score.py"] , reference_urls=[ "https://www.nltk.org/api/nltk.translate.html#module-nltk.translate.meteor_score", "https://en.wikipedia.org/wiki/METEOR", ] , ) def _UpperCamelCase ( self : Dict , lowerCAmelCase__ : List[Any] ) -> Dict: import nltk nltk.download("wordnet" ) if NLTK_VERSION >= version.Version("3.6.5" ): nltk.download("punkt" ) if NLTK_VERSION >= version.Version("3.6.6" ): nltk.download("omw-1.4" ) def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Any=0.9 , lowerCAmelCase__ : Tuple=3 , lowerCAmelCase__ : Optional[int]=0.5 ) -> Any: if NLTK_VERSION >= version.Version("3.6.5" ): UpperCAmelCase = [ meteor_score.single_meteor_score( word_tokenize(lowerCAmelCase__ ) , word_tokenize(lowerCAmelCase__ ) , alpha=lowerCAmelCase__ , beta=lowerCAmelCase__ , gamma=lowerCAmelCase__ ) for ref, pred in zip(lowerCAmelCase__ , lowerCAmelCase__ ) ] else: UpperCAmelCase = [ meteor_score.single_meteor_score(lowerCAmelCase__ , lowerCAmelCase__ , alpha=lowerCAmelCase__ , beta=lowerCAmelCase__ , gamma=lowerCAmelCase__ ) for ref, pred in zip(lowerCAmelCase__ , lowerCAmelCase__ ) ] return {"meteor": np.mean(lowerCAmelCase__ )}
1
1
import argparse import requests import torch # pip3 install salesforce-lavis # I'm actually installing a slightly modified version: pip3 install git+https://github.com/nielsrogge/LAVIS.git@fix_lavis from lavis.models import load_model_and_preprocess from PIL import Image from transformers import ( AutoTokenizer, BlipaConfig, BlipaForConditionalGeneration, BlipaProcessor, BlipaVisionConfig, BlipImageProcessor, OPTConfig, TaConfig, ) from transformers.utils.constants import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD def _lowerCAmelCase( ): UpperCAmelCase = "https://storage.googleapis.com/sfr-vision-language-research/LAVIS/assets/merlion.png" UpperCAmelCase = Image.open(requests.get(__A , stream=__A ).raw ).convert("RGB" ) return image def _lowerCAmelCase( __A ): UpperCAmelCase = [] # fmt: off # vision encoder rename_keys.append(("visual_encoder.cls_token", "vision_model.embeddings.class_embedding") ) rename_keys.append(("visual_encoder.pos_embed", "vision_model.embeddings.position_embedding") ) rename_keys.append(("visual_encoder.patch_embed.proj.weight", "vision_model.embeddings.patch_embedding.weight") ) rename_keys.append(("visual_encoder.patch_embed.proj.bias", "vision_model.embeddings.patch_embedding.bias") ) rename_keys.append(("ln_vision.weight", "vision_model.post_layernorm.weight") ) rename_keys.append(("ln_vision.bias", "vision_model.post_layernorm.bias") ) for i in range(config.vision_config.num_hidden_layers ): rename_keys.append((F"visual_encoder.blocks.{i}.norm1.weight", F"vision_model.encoder.layers.{i}.layer_norm1.weight") ) rename_keys.append((F"visual_encoder.blocks.{i}.norm1.bias", F"vision_model.encoder.layers.{i}.layer_norm1.bias") ) rename_keys.append((F"visual_encoder.blocks.{i}.norm2.weight", F"vision_model.encoder.layers.{i}.layer_norm2.weight") ) rename_keys.append((F"visual_encoder.blocks.{i}.norm2.bias", F"vision_model.encoder.layers.{i}.layer_norm2.bias") ) rename_keys.append((F"visual_encoder.blocks.{i}.attn.qkv.weight", F"vision_model.encoder.layers.{i}.self_attn.qkv.weight") ) rename_keys.append((F"visual_encoder.blocks.{i}.attn.proj.weight", F"vision_model.encoder.layers.{i}.self_attn.projection.weight",) ) rename_keys.append((F"visual_encoder.blocks.{i}.attn.proj.bias", F"vision_model.encoder.layers.{i}.self_attn.projection.bias") ) rename_keys.append((F"visual_encoder.blocks.{i}.mlp.fc1.weight", F"vision_model.encoder.layers.{i}.mlp.fc1.weight") ) rename_keys.append((F"visual_encoder.blocks.{i}.mlp.fc1.bias", F"vision_model.encoder.layers.{i}.mlp.fc1.bias") ) rename_keys.append((F"visual_encoder.blocks.{i}.mlp.fc2.weight", F"vision_model.encoder.layers.{i}.mlp.fc2.weight") ) rename_keys.append((F"visual_encoder.blocks.{i}.mlp.fc2.bias", F"vision_model.encoder.layers.{i}.mlp.fc2.bias") ) # QFormer rename_keys.append(("Qformer.bert.embeddings.LayerNorm.weight", "qformer.layernorm.weight") ) rename_keys.append(("Qformer.bert.embeddings.LayerNorm.bias", "qformer.layernorm.bias") ) # fmt: on return rename_keys def _lowerCAmelCase( __A , __A , __A ): UpperCAmelCase = dct.pop(__A ) UpperCAmelCase = val def _lowerCAmelCase( __A , __A ): for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases UpperCAmelCase = state_dict.pop(F"visual_encoder.blocks.{i}.attn.q_bias" ) UpperCAmelCase = state_dict.pop(F"visual_encoder.blocks.{i}.attn.v_bias" ) # next, set bias in the state dict UpperCAmelCase = torch.cat((q_bias, torch.zeros_like(__A , requires_grad=__A ), v_bias) ) UpperCAmelCase = qkv_bias def _lowerCAmelCase( __A , __A ): UpperCAmelCase = 364 if "coco" in model_name else 224 UpperCAmelCase = BlipaVisionConfig(image_size=__A ).to_dict() # make sure the models have proper bos_token_id and eos_token_id set (important for generation) # seems like flan-T5 models don't have bos_token_id properly set? if "opt-2.7b" in model_name: UpperCAmelCase = OPTConfig.from_pretrained("facebook/opt-2.7b" , eos_token_id=__A ).to_dict() elif "opt-6.7b" in model_name: UpperCAmelCase = OPTConfig.from_pretrained("facebook/opt-6.7b" , eos_token_id=__A ).to_dict() elif "t5-xl" in model_name: UpperCAmelCase = TaConfig.from_pretrained("google/flan-t5-xl" , dense_act_fn="gelu" , bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: UpperCAmelCase = TaConfig.from_pretrained("google/flan-t5-xxl" , dense_act_fn="gelu" , bos_token_id=1 ).to_dict() UpperCAmelCase = BlipaConfig(vision_config=__A , text_config=__A ) return config, image_size @torch.no_grad() def _lowerCAmelCase( __A , __A=None , __A=False ): UpperCAmelCase = ( AutoTokenizer.from_pretrained("facebook/opt-2.7b" ) if "opt" in model_name else AutoTokenizer.from_pretrained("google/flan-t5-xl" ) ) UpperCAmelCase = tokenizer("\n" , add_special_tokens=__A ).input_ids[0] UpperCAmelCase , UpperCAmelCase = get_blipa_config(__A , eos_token_id=__A ) UpperCAmelCase = BlipaForConditionalGeneration(__A ).eval() UpperCAmelCase = { "blip2-opt-2.7b": ("blip2_opt", "pretrain_opt2.7b"), "blip2-opt-6.7b": ("blip2_opt", "pretrain_opt6.7b"), "blip2-opt-2.7b-coco": ("blip2_opt", "caption_coco_opt2.7b"), "blip2-opt-6.7b-coco": ("blip2_opt", "caption_coco_opt6.7b"), "blip2-flan-t5-xl": ("blip2_t5", "pretrain_flant5xl"), "blip2-flan-t5-xl-coco": ("blip2_t5", "caption_coco_flant5xl"), "blip2-flan-t5-xxl": ("blip2_t5", "pretrain_flant5xxl"), } UpperCAmelCase , UpperCAmelCase = model_name_to_original[model_name] # load original model print("Loading original model..." ) UpperCAmelCase = "cuda" if torch.cuda.is_available() else "cpu" UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = load_model_and_preprocess( name=__A , model_type=__A , is_eval=__A , device=__A ) original_model.eval() print("Done!" ) # update state dict keys UpperCAmelCase = original_model.state_dict() UpperCAmelCase = create_rename_keys(__A ) for src, dest in rename_keys: rename_key(__A , __A , __A ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): UpperCAmelCase = state_dict.pop(__A ) if key.startswith("Qformer.bert" ): UpperCAmelCase = key.replace("Qformer.bert" , "qformer" ) if "attention.self" in key: UpperCAmelCase = key.replace("self" , "attention" ) if "opt_proj" in key: UpperCAmelCase = key.replace("opt_proj" , "language_projection" ) if "t5_proj" in key: UpperCAmelCase = key.replace("t5_proj" , "language_projection" ) if key.startswith("opt" ): UpperCAmelCase = key.replace("opt" , "language" ) if key.startswith("t5" ): UpperCAmelCase = key.replace("t5" , "language" ) UpperCAmelCase = val # read in qv biases read_in_q_v_bias(__A , __A ) UpperCAmelCase , UpperCAmelCase = hf_model.load_state_dict(__A , strict=__A ) assert len(__A ) == 0 assert unexpected_keys == ["qformer.embeddings.position_ids"] UpperCAmelCase = load_demo_image() UpperCAmelCase = vis_processors["eval"](__A ).unsqueeze(0 ).to(__A ) UpperCAmelCase = tokenizer(["\n"] , return_tensors="pt" ).input_ids.to(__A ) # create processor UpperCAmelCase = BlipImageProcessor( size={"height": image_size, "width": image_size} , image_mean=__A , image_std=__A ) UpperCAmelCase = BlipaProcessor(image_processor=__A , tokenizer=__A ) UpperCAmelCase = processor(images=__A , return_tensors="pt" ).pixel_values.to(__A ) # make sure processor creates exact same pixel values assert torch.allclose(__A , __A ) original_model.to(__A ) hf_model.to(__A ) with torch.no_grad(): if "opt" in model_name: UpperCAmelCase = original_model({"image": original_pixel_values, "text_input": [""]} ).logits UpperCAmelCase = hf_model(__A , __A ).logits else: UpperCAmelCase = original_model( {"image": original_pixel_values, "text_input": ["\n"], "text_output": ["\n"]} ).logits UpperCAmelCase = input_ids.masked_fill(input_ids == tokenizer.pad_token_id , -100 ) UpperCAmelCase = hf_model(__A , __A , labels=__A ).logits assert original_logits.shape == logits.shape print("First values of original logits:" , original_logits[0, :3, :3] ) print("First values of HF logits:" , logits[0, :3, :3] ) # assert values if model_name == "blip2-flan-t5-xl": UpperCAmelCase = torch.tensor( [[-41.5850, -4.4440, -8.9922], [-47.4322, -5.9143, -1.7340]] , device=__A ) assert torch.allclose(logits[0, :3, :3] , __A , atol=1E-4 ) elif model_name == "blip2-flan-t5-xl-coco": UpperCAmelCase = torch.tensor( [[-57.0109, -9.8967, -12.6280], [-68.6578, -12.7191, -10.5065]] , device=__A ) else: # cast to same type UpperCAmelCase = logits.dtype assert torch.allclose(original_logits.to(__A ) , __A , atol=1E-2 ) print("Looks ok!" ) print("Generating a caption..." ) UpperCAmelCase = "" UpperCAmelCase = tokenizer(__A , return_tensors="pt" ).input_ids.to(__A ) UpperCAmelCase = original_model.generate({"image": original_pixel_values} ) UpperCAmelCase = hf_model.generate( __A , __A , do_sample=__A , num_beams=5 , max_length=30 , min_length=1 , top_p=0.9 , repetition_penalty=1.0 , length_penalty=1.0 , temperature=1 , ) print("Original generation:" , __A ) UpperCAmelCase = input_ids.shape[1] UpperCAmelCase = processor.batch_decode(outputs[:, prompt_length:] , skip_special_tokens=__A ) UpperCAmelCase = [text.strip() for text in output_text] print("HF generation:" , __A ) if pytorch_dump_folder_path is not None: processor.save_pretrained(__A ) hf_model.save_pretrained(__A ) if push_to_hub: processor.push_to_hub(F"nielsr/{model_name}" ) hf_model.push_to_hub(F"nielsr/{model_name}" ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() lowerCAmelCase__ = [ "blip2-opt-2.7b", "blip2-opt-6.7b", "blip2-opt-2.7b-coco", "blip2-opt-6.7b-coco", "blip2-flan-t5-xl", "blip2-flan-t5-xl-coco", "blip2-flan-t5-xxl", ] parser.add_argument( "--model_name", default="blip2-opt-2.7b", choices=choices, type=str, help="Path to hf config.json of model to convert", ) parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument( "--push_to_hub", action="store_true", help="Whether to push the model and processor to the hub after converting", ) lowerCAmelCase__ = parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
1
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { "unc-nlp/lxmert-base-uncased": "https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/config.json", } class __magic_name__ ( _snake_case ): UpperCAmelCase = """lxmert""" UpperCAmelCase = {} def __init__( self : int , lowerCAmelCase__ : Any=3_0_5_2_2 , lowerCAmelCase__ : List[str]=7_6_8 , lowerCAmelCase__ : Union[str, Any]=1_2 , lowerCAmelCase__ : List[Any]=9_5_0_0 , lowerCAmelCase__ : Any=1_6_0_0 , lowerCAmelCase__ : Union[str, Any]=4_0_0 , lowerCAmelCase__ : Tuple=3_0_7_2 , lowerCAmelCase__ : Dict="gelu" , lowerCAmelCase__ : Tuple=0.1 , lowerCAmelCase__ : Tuple=0.1 , lowerCAmelCase__ : int=5_1_2 , lowerCAmelCase__ : List[str]=2 , lowerCAmelCase__ : List[str]=0.02 , lowerCAmelCase__ : str=1e-1_2 , lowerCAmelCase__ : str=9 , lowerCAmelCase__ : int=5 , lowerCAmelCase__ : Optional[int]=5 , lowerCAmelCase__ : List[Any]=2_0_4_8 , lowerCAmelCase__ : Any=4 , lowerCAmelCase__ : Dict=6.67 , lowerCAmelCase__ : Any=True , lowerCAmelCase__ : Union[str, Any]=True , lowerCAmelCase__ : Any=True , lowerCAmelCase__ : Tuple=True , lowerCAmelCase__ : Optional[Any]=True , lowerCAmelCase__ : Optional[int]=True , lowerCAmelCase__ : Tuple=True , **lowerCAmelCase__ : List[Any] , ) -> Dict: UpperCAmelCase = vocab_size UpperCAmelCase = hidden_size UpperCAmelCase = num_attention_heads UpperCAmelCase = hidden_act UpperCAmelCase = intermediate_size UpperCAmelCase = hidden_dropout_prob UpperCAmelCase = attention_probs_dropout_prob UpperCAmelCase = max_position_embeddings UpperCAmelCase = type_vocab_size UpperCAmelCase = initializer_range UpperCAmelCase = layer_norm_eps UpperCAmelCase = num_qa_labels UpperCAmelCase = num_object_labels UpperCAmelCase = num_attr_labels UpperCAmelCase = l_layers UpperCAmelCase = x_layers UpperCAmelCase = r_layers UpperCAmelCase = visual_feat_dim UpperCAmelCase = visual_pos_dim UpperCAmelCase = visual_loss_normalizer UpperCAmelCase = task_matched UpperCAmelCase = task_mask_lm UpperCAmelCase = task_obj_predict UpperCAmelCase = task_qa UpperCAmelCase = visual_obj_loss UpperCAmelCase = visual_attr_loss UpperCAmelCase = visual_feat_loss UpperCAmelCase = {"vision": r_layers, "cross_encoder": x_layers, "language": l_layers} super().__init__(**lowerCAmelCase__ )
1
1
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow if is_tf_available(): import numpy as np import tensorflow as tf from transformers import TFCamembertModel @require_tf @require_sentencepiece @require_tokenizers class __magic_name__ ( unittest.TestCase ): @slow def _UpperCamelCase ( self : List[str] ) -> int: UpperCAmelCase = TFCamembertModel.from_pretrained("jplu/tf-camembert-base" ) UpperCAmelCase = tf.convert_to_tensor( [[5, 1_2_1, 1_1, 6_6_0, 1_6, 7_3_0, 2_5_5_4_3, 1_1_0, 8_3, 6]] , dtype=tf.intaa , ) # J'aime le camembert !" UpperCAmelCase = model(lowerCAmelCase__ )["last_hidden_state"] UpperCAmelCase = tf.TensorShape((1, 1_0, 7_6_8) ) self.assertEqual(output.shape , lowerCAmelCase__ ) # compare the actual values for a slice. UpperCAmelCase = tf.convert_to_tensor( [[[-0.0_254, 0.0_235, 0.1_027], [0.0_606, -0.1_811, -0.0_418], [-0.1_561, -0.1_127, 0.2_687]]] , dtype=tf.floataa , ) # camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0') # camembert.eval() # expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach() self.assertTrue(np.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1e-4 ) )
1
def _lowerCAmelCase( __A ): UpperCAmelCase = 0 while num > 0: digit_sum += num % 10 num //= 10 return digit_sum def _lowerCAmelCase( __A = 100 ): UpperCAmelCase = 1 UpperCAmelCase = 2 for i in range(2 , max_n + 1 ): UpperCAmelCase = pre_numerator UpperCAmelCase = 2 * i // 3 if i % 3 == 0 else 1 UpperCAmelCase = cur_numerator UpperCAmelCase = e_cont * pre_numerator + temp return sum_digits(__A ) if __name__ == "__main__": print(f"{solution() = }")
1
1
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from .tokenization_lxmert import LxmertTokenizer lowerCAmelCase__ = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} lowerCAmelCase__ = { "vocab_file": { "unc-nlp/lxmert-base-uncased": "https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/vocab.txt", }, "tokenizer_file": { "unc-nlp/lxmert-base-uncased": ( "https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/tokenizer.json" ), }, } lowerCAmelCase__ = { "unc-nlp/lxmert-base-uncased": 512, } lowerCAmelCase__ = { "unc-nlp/lxmert-base-uncased": {"do_lower_case": True}, } class __magic_name__ ( _snake_case ): UpperCAmelCase = VOCAB_FILES_NAMES UpperCAmelCase = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase = PRETRAINED_INIT_CONFIGURATION UpperCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase = LxmertTokenizer def __init__( self : Optional[int] , lowerCAmelCase__ : List[str]=None , lowerCAmelCase__ : List[str]=None , lowerCAmelCase__ : List[Any]=True , lowerCAmelCase__ : List[str]="[UNK]" , lowerCAmelCase__ : Dict="[SEP]" , lowerCAmelCase__ : Any="[PAD]" , lowerCAmelCase__ : List[str]="[CLS]" , lowerCAmelCase__ : Union[str, Any]="[MASK]" , lowerCAmelCase__ : Tuple=True , lowerCAmelCase__ : Any=None , **lowerCAmelCase__ : Optional[int] , ) -> List[str]: super().__init__( lowerCAmelCase__ , tokenizer_file=lowerCAmelCase__ , do_lower_case=lowerCAmelCase__ , unk_token=lowerCAmelCase__ , sep_token=lowerCAmelCase__ , pad_token=lowerCAmelCase__ , cls_token=lowerCAmelCase__ , mask_token=lowerCAmelCase__ , tokenize_chinese_chars=lowerCAmelCase__ , strip_accents=lowerCAmelCase__ , **lowerCAmelCase__ , ) UpperCAmelCase = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("lowercase" , lowerCAmelCase__ ) != do_lower_case or normalizer_state.get("strip_accents" , lowerCAmelCase__ ) != strip_accents or normalizer_state.get("handle_chinese_chars" , lowerCAmelCase__ ) != tokenize_chinese_chars ): UpperCAmelCase = getattr(lowerCAmelCase__ , normalizer_state.pop("type" ) ) UpperCAmelCase = do_lower_case UpperCAmelCase = strip_accents UpperCAmelCase = tokenize_chinese_chars UpperCAmelCase = normalizer_class(**lowerCAmelCase__ ) UpperCAmelCase = do_lower_case def _UpperCamelCase ( self : Dict , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Dict=None ) -> Optional[Any]: UpperCAmelCase = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def _UpperCamelCase ( self : Optional[Any] , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None ) -> List[int]: UpperCAmelCase = [self.sep_token_id] UpperCAmelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def _UpperCamelCase ( self : List[Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[str] = None ) -> Tuple[str]: UpperCAmelCase = self._tokenizer.model.save(lowerCAmelCase__ , name=lowerCAmelCase__ ) return tuple(lowerCAmelCase__ )
1
from . import ( albert, align, altclip, audio_spectrogram_transformer, auto, autoformer, bark, bart, barthez, bartpho, beit, bert, bert_generation, bert_japanese, bertweet, big_bird, bigbird_pegasus, biogpt, bit, blenderbot, blenderbot_small, blip, blip_a, bloom, bridgetower, byta, camembert, canine, chinese_clip, clap, clip, clipseg, codegen, conditional_detr, convbert, convnext, convnextva, cpm, cpmant, ctrl, cvt, dataavec, deberta, deberta_va, decision_transformer, deformable_detr, deit, deprecated, deta, detr, dialogpt, dinat, distilbert, dit, donut, dpr, dpt, efficientformer, efficientnet, electra, encodec, encoder_decoder, ernie, ernie_m, esm, falcon, flaubert, flava, fnet, focalnet, fsmt, funnel, git, glpn, gpta, gpt_bigcode, gpt_neo, gpt_neox, gpt_neox_japanese, gpt_swa, gptj, gptsan_japanese, graphormer, groupvit, herbert, hubert, ibert, imagegpt, informer, instructblip, jukebox, layoutlm, layoutlmva, layoutlmva, layoutxlm, led, levit, lilt, llama, longformer, longta, luke, lxmert, mam_aaa, marian, markuplm, maskaformer, maskformer, mbart, mbartaa, mega, megatron_bert, megatron_gpta, mgp_str, mluke, mobilebert, mobilenet_va, mobilenet_va, mobilevit, mobilevitva, mpnet, mra, mta, musicgen, mvp, nat, nezha, nllb, nllb_moe, nystromformer, oneformer, open_llama, openai, opt, owlvit, pegasus, pegasus_x, perceiver, phobert, pixastruct, plbart, poolformer, prophetnet, qdqbert, rag, realm, reformer, regnet, rembert, resnet, roberta, roberta_prelayernorm, roc_bert, roformer, rwkv, sam, segformer, sew, sew_d, speech_encoder_decoder, speech_to_text, speech_to_text_a, speechta, splinter, squeezebert, swiftformer, swin, swinasr, swinva, switch_transformers, ta, table_transformer, tapas, time_series_transformer, timesformer, timm_backbone, transfo_xl, trocr, tvlt, umta, unispeech, unispeech_sat, upernet, videomae, vilt, vision_encoder_decoder, vision_text_dual_encoder, visual_bert, vit, vit_hybrid, vit_mae, vit_msn, vivit, wavaveca, wavaveca_conformer, wavaveca_phoneme, wavaveca_with_lm, wavlm, whisper, x_clip, xglm, xlm, xlm_prophetnet, xlm_roberta, xlm_roberta_xl, xlnet, xmod, yolos, yoso, )
1
1
import json import os import re import shutil import tempfile import unittest from typing import Tuple from transformers import AddedToken, BatchEncoding, ByTaTokenizer from transformers.utils import cached_property, is_tf_available, is_torch_available from ...test_tokenization_common import TokenizerTesterMixin if is_torch_available(): lowerCAmelCase__ = "pt" elif is_tf_available(): lowerCAmelCase__ = "tf" else: lowerCAmelCase__ = "jax" class __magic_name__ ( _snake_case , unittest.TestCase ): UpperCAmelCase = ByTaTokenizer UpperCAmelCase = False def _UpperCamelCase ( self : List[str] ) -> Any: super().setUp() UpperCAmelCase = ByTaTokenizer() tokenizer.save_pretrained(self.tmpdirname ) @cached_property def _UpperCamelCase ( self : Dict ) -> Optional[int]: return ByTaTokenizer.from_pretrained("google/byt5-small" ) def _UpperCamelCase ( self : Any , **lowerCAmelCase__ : Union[str, Any] ) -> ByTaTokenizer: return self.tokenizer_class.from_pretrained(self.tmpdirname , **lowerCAmelCase__ ) def _UpperCamelCase ( self : Tuple , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Union[str, Any]=False , lowerCAmelCase__ : Optional[int]=2_0 , lowerCAmelCase__ : str=5 ) -> Tuple[str, list]: # XXX The default common tokenizer tests assume that every ID is decodable on its own. # This assumption is invalid for ByT5 because single bytes might not be # valid utf-8 (byte 128 for instance). # Here we're overriding the smallest possible method to provide # a clean sequence without making the same assumption. UpperCAmelCase = [] for i in range(len(lowerCAmelCase__ ) ): try: UpperCAmelCase = tokenizer.decode([i] , clean_up_tokenization_spaces=lowerCAmelCase__ ) except UnicodeDecodeError: pass toks.append((i, tok) ) UpperCAmelCase = list(filter(lambda lowerCAmelCase__ : re.match(r"^[ a-zA-Z]+$" , t[1] ) , lowerCAmelCase__ ) ) UpperCAmelCase = list(filter(lambda lowerCAmelCase__ : [t[0]] == tokenizer.encode(t[1] , add_special_tokens=lowerCAmelCase__ ) , lowerCAmelCase__ ) ) if max_length is not None and len(lowerCAmelCase__ ) > max_length: UpperCAmelCase = toks[:max_length] if min_length is not None and len(lowerCAmelCase__ ) < min_length and len(lowerCAmelCase__ ) > 0: while len(lowerCAmelCase__ ) < min_length: UpperCAmelCase = toks + toks # toks_str = [t[1] for t in toks] UpperCAmelCase = [t[0] for t in toks] # Ensure consistency UpperCAmelCase = tokenizer.decode(lowerCAmelCase__ , clean_up_tokenization_spaces=lowerCAmelCase__ ) if " " not in output_txt and len(lowerCAmelCase__ ) > 1: UpperCAmelCase = ( tokenizer.decode([toks_ids[0]] , clean_up_tokenization_spaces=lowerCAmelCase__ ) + " " + tokenizer.decode(toks_ids[1:] , clean_up_tokenization_spaces=lowerCAmelCase__ ) ) if with_prefix_space: UpperCAmelCase = " " + output_txt UpperCAmelCase = tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ ) return output_txt, output_ids def _UpperCamelCase ( self : List[Any] ) -> List[str]: UpperCAmelCase = self.ta_base_tokenizer UpperCAmelCase = tokenizer(["hi</s>", "I went to the gym</s>", "</s>"] ) UpperCAmelCase = tokenizer(["hi", "I went to the gym", ""] ) self.assertListEqual(batch_with_eos_added["input_ids"] , batch_without_eos_added["input_ids"] ) def _UpperCamelCase ( self : int ) -> List[Any]: UpperCAmelCase = self.ta_base_tokenizer UpperCAmelCase = "Unicode €." UpperCAmelCase = tokenizer(lowerCAmelCase__ ) UpperCAmelCase = [8_8, 1_1_3, 1_0_8, 1_0_2, 1_1_4, 1_0_3, 1_0_4, 3_5, 2_2_9, 1_3_3, 1_7_5, 4_9, 1] self.assertEqual(encoded["input_ids"] , lowerCAmelCase__ ) # decoding UpperCAmelCase = tokenizer.decode(lowerCAmelCase__ ) self.assertEqual(lowerCAmelCase__ , "Unicode €.</s>" ) UpperCAmelCase = tokenizer("e è é ê ë" ) UpperCAmelCase = [1_0_4, 3_5, 1_9_8, 1_7_1, 3_5, 1_9_8, 1_7_2, 3_5, 1_9_8, 1_7_3, 3_5, 1_9_8, 1_7_4, 1] self.assertEqual(encoded["input_ids"] , lowerCAmelCase__ ) # decoding UpperCAmelCase = tokenizer.decode(lowerCAmelCase__ ) self.assertEqual(lowerCAmelCase__ , "e è é ê ë</s>" ) # encode/decode, but with `encode` instead of `__call__` self.assertEqual(tokenizer.decode(tokenizer.encode("e è é ê ë" ) ) , "e è é ê ë</s>" ) def _UpperCamelCase ( self : Optional[Any] ) -> Optional[Any]: UpperCAmelCase = self.ta_base_tokenizer UpperCAmelCase = ["A long paragraph for summarization.", "Another paragraph for summarization."] # fmt: off UpperCAmelCase = [6_8, 3_5, 1_1_1, 1_1_4, 1_1_3, 1_0_6, 3_5, 1_1_5, 1_0_0, 1_1_7, 1_0_0, 1_0_6, 1_1_7, 1_0_0, 1_1_5, 1_0_7, 3_5, 1_0_5, 1_1_4, 1_1_7, 3_5, 1_1_8, 1_2_0, 1_1_2, 1_1_2, 1_0_0, 1_1_7, 1_0_8, 1_2_5, 1_0_0, 1_1_9, 1_0_8, 1_1_4, 1_1_3, 4_9, 1, 0] # fmt: on UpperCAmelCase = tokenizer(lowerCAmelCase__ , padding=lowerCAmelCase__ , return_tensors=lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) if FRAMEWORK != "jax": UpperCAmelCase = list(batch.input_ids.numpy()[0] ) else: UpperCAmelCase = list(batch.input_ids.tolist()[0] ) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual((2, 3_7) , batch.input_ids.shape ) self.assertEqual((2, 3_7) , batch.attention_mask.shape ) def _UpperCamelCase ( self : Optional[int] ) -> Optional[int]: UpperCAmelCase = self.ta_base_tokenizer UpperCAmelCase = ["A long paragraph for summarization.", "Another paragraph for summarization."] UpperCAmelCase = tokenizer(lowerCAmelCase__ , padding=lowerCAmelCase__ , return_tensors=lowerCAmelCase__ ) # check if input_ids are returned and no decoder_input_ids self.assertIn("input_ids" , lowerCAmelCase__ ) self.assertIn("attention_mask" , lowerCAmelCase__ ) self.assertNotIn("decoder_input_ids" , lowerCAmelCase__ ) self.assertNotIn("decoder_attention_mask" , lowerCAmelCase__ ) def _UpperCamelCase ( self : Tuple ) -> List[Any]: UpperCAmelCase = self.ta_base_tokenizer UpperCAmelCase = [ "Summary of the text.", "Another summary.", ] UpperCAmelCase = tokenizer( text_target=lowerCAmelCase__ , max_length=3_2 , padding="max_length" , truncation=lowerCAmelCase__ , return_tensors=lowerCAmelCase__ ) self.assertEqual(3_2 , targets["input_ids"].shape[1] ) def _UpperCamelCase ( self : Optional[Any] ) -> List[Any]: UpperCAmelCase = self.ta_base_tokenizer UpperCAmelCase = ["A long paragraph for summarization. </s>"] UpperCAmelCase = ["Summary of the text. </s>"] # fmt: off UpperCAmelCase = [6_8, 3_5, 1_1_1, 1_1_4, 1_1_3, 1_0_6, 3_5, 1_1_5, 1_0_0, 1_1_7, 1_0_0, 1_0_6, 1_1_7, 1_0_0, 1_1_5, 1_0_7, 3_5, 1_0_5, 1_1_4, 1_1_7, 3_5, 1_1_8, 1_2_0, 1_1_2, 1_1_2, 1_0_0, 1_1_7, 1_0_8, 1_2_5, 1_0_0, 1_1_9, 1_0_8, 1_1_4, 1_1_3, 4_9, 3_5, 1] UpperCAmelCase = [8_6, 1_2_0, 1_1_2, 1_1_2, 1_0_0, 1_1_7, 1_2_4, 3_5, 1_1_4, 1_0_5, 3_5, 1_1_9, 1_0_7, 1_0_4, 3_5, 1_1_9, 1_0_4, 1_2_3, 1_1_9, 4_9, 3_5, 1] # fmt: on UpperCAmelCase = tokenizer(lowerCAmelCase__ , text_target=lowerCAmelCase__ ) self.assertEqual(lowerCAmelCase__ , batch["input_ids"][0] ) self.assertEqual(lowerCAmelCase__ , batch["labels"][0] ) def _UpperCamelCase ( self : List[str] ) -> int: # safety check on max_len default value so we are sure the test works UpperCAmelCase = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f"{tokenizer.__class__.__name__}" ): self.assertNotEqual(tokenizer.model_max_length , 4_2 ) # Now let's start the test UpperCAmelCase = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f"{tokenizer.__class__.__name__}" ): # Isolate this from the other tests because we save additional tokens/etc UpperCAmelCase = tempfile.mkdtemp() UpperCAmelCase = " He is very happy, UNwant\u00E9d,running" UpperCAmelCase = tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ ) tokenizer.save_pretrained(lowerCAmelCase__ ) UpperCAmelCase = tokenizer.__class__.from_pretrained(lowerCAmelCase__ ) UpperCAmelCase = after_tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ ) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ ) shutil.rmtree(lowerCAmelCase__ ) UpperCAmelCase = self.get_tokenizers(model_max_length=4_2 ) for tokenizer in tokenizers: with self.subTest(f"{tokenizer.__class__.__name__}" ): # Isolate this from the other tests because we save additional tokens/etc UpperCAmelCase = tempfile.mkdtemp() UpperCAmelCase = " He is very happy, UNwant\u00E9d,running" tokenizer.add_tokens(["bim", "bambam"] ) UpperCAmelCase = tokenizer.additional_special_tokens additional_special_tokens.append("new_additional_special_token" ) tokenizer.add_special_tokens({"additional_special_tokens": additional_special_tokens} ) UpperCAmelCase = tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ ) tokenizer.save_pretrained(lowerCAmelCase__ ) UpperCAmelCase = tokenizer.__class__.from_pretrained(lowerCAmelCase__ ) UpperCAmelCase = after_tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ ) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertIn("new_additional_special_token" , after_tokenizer.additional_special_tokens ) self.assertEqual(after_tokenizer.model_max_length , 4_2 ) UpperCAmelCase = tokenizer.__class__.from_pretrained(lowerCAmelCase__ , model_max_length=4_3 ) self.assertEqual(tokenizer.model_max_length , 4_3 ) shutil.rmtree(lowerCAmelCase__ ) def _UpperCamelCase ( self : Any ) -> int: UpperCAmelCase = [] if self.test_slow_tokenizer: tokenizer_list.append((self.tokenizer_class, self.get_tokenizer()) ) if self.test_rust_tokenizer: tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer()) ) for tokenizer_class, tokenizer_utils in tokenizer_list: with tempfile.TemporaryDirectory() as tmp_dir: tokenizer_utils.save_pretrained(lowerCAmelCase__ ) with open(os.path.join(lowerCAmelCase__ , "special_tokens_map.json" ) , encoding="utf-8" ) as json_file: UpperCAmelCase = json.load(lowerCAmelCase__ ) with open(os.path.join(lowerCAmelCase__ , "tokenizer_config.json" ) , encoding="utf-8" ) as json_file: UpperCAmelCase = json.load(lowerCAmelCase__ ) UpperCAmelCase = [f"<extra_id_{i}>" for i in range(1_2_5 )] UpperCAmelCase = added_tokens_extra_ids + [ "an_additional_special_token" ] UpperCAmelCase = added_tokens_extra_ids + [ "an_additional_special_token" ] with open(os.path.join(lowerCAmelCase__ , "special_tokens_map.json" ) , "w" , encoding="utf-8" ) as outfile: json.dump(lowerCAmelCase__ , lowerCAmelCase__ ) with open(os.path.join(lowerCAmelCase__ , "tokenizer_config.json" ) , "w" , encoding="utf-8" ) as outfile: json.dump(lowerCAmelCase__ , lowerCAmelCase__ ) # the following checks allow us to verify that our test works as expected, i.e. that the tokenizer takes # into account the new value of additional_special_tokens given in the "tokenizer_config.json" and # "special_tokens_map.json" files UpperCAmelCase = tokenizer_class.from_pretrained( lowerCAmelCase__ , ) self.assertIn( "an_additional_special_token" , tokenizer_without_change_in_init.additional_special_tokens ) # self.assertIn("an_additional_special_token",tokenizer_without_change_in_init.get_vocab()) # ByT5Tokenization no vocab self.assertEqual( ["an_additional_special_token"] , tokenizer_without_change_in_init.convert_ids_to_tokens( tokenizer_without_change_in_init.convert_tokens_to_ids(["an_additional_special_token"] ) ) , ) # Now we test that we can change the value of additional_special_tokens in the from_pretrained UpperCAmelCase = added_tokens_extra_ids + [AddedToken("a_new_additional_special_token" , lstrip=lowerCAmelCase__ )] UpperCAmelCase = tokenizer_class.from_pretrained( lowerCAmelCase__ , additional_special_tokens=lowerCAmelCase__ , ) self.assertIn("a_new_additional_special_token" , tokenizer.additional_special_tokens ) self.assertEqual( ["a_new_additional_special_token"] , tokenizer.convert_ids_to_tokens( tokenizer.convert_tokens_to_ids(["a_new_additional_special_token"] ) ) , ) def _UpperCamelCase ( self : str ) -> Tuple: UpperCAmelCase = [] if self.test_slow_tokenizer: tokenizer_list.append((self.tokenizer_class, self.get_tokenizer()) ) if self.test_rust_tokenizer: tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer()) ) for tokenizer_class, tokenizer_utils in tokenizer_list: with tempfile.TemporaryDirectory() as tmp_dir: tokenizer_utils.save_pretrained(lowerCAmelCase__ ) UpperCAmelCase = tokenizer_class.from_pretrained(lowerCAmelCase__ ) self.assertTrue(tokenizer.decode([2_5_5] ) == "" ) def _UpperCamelCase ( self : Optional[Any] ) -> Dict: pass def _UpperCamelCase ( self : Optional[Any] ) -> Tuple: pass def _UpperCamelCase ( self : Dict ) -> List[str]: pass def _UpperCamelCase ( self : Optional[int] ) -> int: pass def _UpperCamelCase ( self : int ) -> int: # The default common tokenizer tests uses invalid tokens for ByT5 that can only accept one-character strings # and special added tokens as tokens UpperCAmelCase = self.get_tokenizers(fast=lowerCAmelCase__ , do_lower_case=lowerCAmelCase__ ) for tokenizer in tokenizers: with self.subTest(f"{tokenizer.__class__.__name__}" ): UpperCAmelCase = ["t", "h", "i", "s", " ", "i", "s", " ", "a", " ", "t", "e", "x", "t", "</s>"] UpperCAmelCase = tokenizer.convert_tokens_to_string(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) def _UpperCamelCase ( self : Optional[int] ) -> List[str]: UpperCAmelCase = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f"{tokenizer.__class__.__name__}" ): UpperCAmelCase = [ "bos_token", "eos_token", "unk_token", "sep_token", "pad_token", "cls_token", "mask_token", ] UpperCAmelCase = 0 UpperCAmelCase = tokenizer.convert_ids_to_tokens( lowerCAmelCase__ , skip_special_tokens=lowerCAmelCase__ ) for attr in attributes_list: setattr(lowerCAmelCase__ , attr + "_id" , lowerCAmelCase__ ) self.assertEqual(getattr(lowerCAmelCase__ , lowerCAmelCase__ ) , lowerCAmelCase__ ) self.assertEqual(getattr(lowerCAmelCase__ , attr + "_id" ) , lowerCAmelCase__ ) setattr(lowerCAmelCase__ , attr + "_id" , lowerCAmelCase__ ) self.assertEqual(getattr(lowerCAmelCase__ , lowerCAmelCase__ ) , lowerCAmelCase__ ) self.assertEqual(getattr(lowerCAmelCase__ , attr + "_id" ) , lowerCAmelCase__ ) setattr(lowerCAmelCase__ , "additional_special_tokens_ids" , [] ) self.assertListEqual(getattr(lowerCAmelCase__ , "additional_special_tokens" ) , [] ) self.assertListEqual(getattr(lowerCAmelCase__ , "additional_special_tokens_ids" ) , [] ) setattr(lowerCAmelCase__ , "additional_special_tokens_ids" , [token_id_to_test_setters] ) self.assertListEqual(getattr(lowerCAmelCase__ , "additional_special_tokens" ) , [token_to_test_setters] ) self.assertListEqual(getattr(lowerCAmelCase__ , "additional_special_tokens_ids" ) , [token_id_to_test_setters] )
1
import numpy # List of input, output pairs lowerCAmelCase__ = ( ((5, 2, 3), 15), ((6, 5, 9), 25), ((11, 12, 13), 41), ((1, 1, 1), 8), ((11, 12, 13), 41), ) lowerCAmelCase__ = (((515, 22, 13), 555), ((61, 35, 49), 150)) lowerCAmelCase__ = [2, 4, 1, 5] lowerCAmelCase__ = len(train_data) lowerCAmelCase__ = 0.0_0_9 def _lowerCAmelCase( __A , __A="train" ): return calculate_hypothesis_value(__A , __A ) - output( __A , __A ) def _lowerCAmelCase( __A ): UpperCAmelCase = 0 for i in range(len(__A ) - 1 ): hyp_val += data_input_tuple[i] * parameter_vector[i + 1] hyp_val += parameter_vector[0] return hyp_val def _lowerCAmelCase( __A , __A ): if data_set == "train": return train_data[example_no][1] elif data_set == "test": return test_data[example_no][1] return None def _lowerCAmelCase( __A , __A ): if data_set == "train": return _hypothesis_value(train_data[example_no][0] ) elif data_set == "test": return _hypothesis_value(test_data[example_no][0] ) return None def _lowerCAmelCase( __A , __A=m ): UpperCAmelCase = 0 for i in range(__A ): if index == -1: summation_value += _error(__A ) else: summation_value += _error(__A ) * train_data[i][0][index] return summation_value def _lowerCAmelCase( __A ): UpperCAmelCase = summation_of_cost_derivative(__A , __A ) / m return cost_derivative_value def _lowerCAmelCase( ): global parameter_vector # Tune these values to set a tolerance value for predicted output UpperCAmelCase = 0.000002 UpperCAmelCase = 0 UpperCAmelCase = 0 while True: j += 1 UpperCAmelCase = [0, 0, 0, 0] for i in range(0 , len(__A ) ): UpperCAmelCase = get_cost_derivative(i - 1 ) UpperCAmelCase = ( parameter_vector[i] - LEARNING_RATE * cost_derivative ) if numpy.allclose( __A , __A , atol=__A , rtol=__A , ): break UpperCAmelCase = temp_parameter_vector print(("Number of iterations:", j) ) def _lowerCAmelCase( ): for i in range(len(__A ) ): print(("Actual output value:", output(__A , "test" )) ) print(("Hypothesis output:", calculate_hypothesis_value(__A , "test" )) ) if __name__ == "__main__": run_gradient_descent() print("\nTesting gradient descent for a linear hypothesis function.\n") test_gradient_descent()
1
1
import gc import importlib.metadata import tempfile import unittest from packaging import version from transformers import ( AutoModel, AutoModelForCausalLM, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoTokenizer, BitsAndBytesConfig, pipeline, ) from transformers.testing_utils import ( is_torch_available, require_accelerate, require_bitsandbytes, require_torch, require_torch_gpu, require_torch_multi_gpu, slow, ) def _lowerCAmelCase( __A ): if model.config.model_type == "gpt2": return model.transformer.h[0].mlp.c_fc return model.transformer.h[0].mlp.dense_ah_to_h if is_torch_available(): import torch import torch.nn as nn class __magic_name__ ( nn.Module ): def __init__( self : Optional[int] , lowerCAmelCase__ : nn.Module , lowerCAmelCase__ : int ) -> Tuple: super().__init__() UpperCAmelCase = module UpperCAmelCase = nn.Sequential( nn.Linear(module.in_features , lowerCAmelCase__ , bias=lowerCAmelCase__ ) , nn.Linear(lowerCAmelCase__ , module.out_features , bias=lowerCAmelCase__ ) , ) UpperCAmelCase = (2.0 / (5 * min(module.in_features , module.out_features ))) ** 0.5 nn.init.normal_(self.adapter[0].weight , std=lowerCAmelCase__ ) nn.init.zeros_(self.adapter[1].weight ) self.adapter.to(module.weight.device ) def _UpperCamelCase ( self : Dict , lowerCAmelCase__ : Tuple , *lowerCAmelCase__ : List[str] , **lowerCAmelCase__ : List[str] ) -> Optional[Any]: return self.module(lowerCAmelCase__ , *lowerCAmelCase__ , **lowerCAmelCase__ ) + self.adapter(lowerCAmelCase__ ) @require_bitsandbytes @require_accelerate @require_torch @require_torch_gpu @slow class __magic_name__ ( unittest.TestCase ): # We keep the constants inside the init function and model loading inside setUp function # We need to test on relatively large models (aka >1b parameters otherwise the quantiztion may not work as expected) # Therefore here we use only bloom-1b3 to test our module UpperCAmelCase = """bigscience/bloom-1b7""" # Constant values UpperCAmelCase = 2.109_6595_5269_2574 UpperCAmelCase = """Hello my name is""" UpperCAmelCase = set() EXPECTED_OUTPUTS.add("""Hello my name is John and I am a professional photographer. I""" ) EXPECTED_OUTPUTS.add("""Hello my name is John.\nI am a friend of your father.\n""" ) EXPECTED_OUTPUTS.add("""Hello my name is John Doe, I am a student at the University""" ) UpperCAmelCase = 10 def _UpperCamelCase ( self : List[Any] ) -> List[Any]: # Models and tokenizer UpperCAmelCase = AutoTokenizer.from_pretrained(self.model_name ) class __magic_name__ ( _snake_case ): def _UpperCamelCase ( self : Union[str, Any] ) -> Dict: super().setUp() # Models and tokenizer UpperCAmelCase = AutoModelForCausalLM.from_pretrained( self.model_name , torch_dtype=torch.floataa , device_map="auto" ) UpperCAmelCase = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=lowerCAmelCase__ , device_map="auto" ) def _UpperCamelCase ( self : Optional[Any] ) -> List[str]: del self.model_fpaa del self.model_abit gc.collect() torch.cuda.empty_cache() def _UpperCamelCase ( self : Any ) -> Union[str, Any]: UpperCAmelCase = self.model_abit.config self.assertTrue(hasattr(lowerCAmelCase__ , "quantization_config" ) ) UpperCAmelCase = config.to_dict() UpperCAmelCase = config.to_diff_dict() UpperCAmelCase = config.to_json_string() def _UpperCamelCase ( self : Union[str, Any] ) -> Optional[int]: from bitsandbytes.nn import Paramsabit UpperCAmelCase = self.model_fpaa.get_memory_footprint() UpperCAmelCase = self.model_abit.get_memory_footprint() self.assertAlmostEqual(mem_fpaa / mem_abit , self.EXPECTED_RELATIVE_DIFFERENCE ) UpperCAmelCase = get_some_linear_layer(self.model_abit ) self.assertTrue(linear.weight.__class__ == Paramsabit ) def _UpperCamelCase ( self : Tuple ) -> Optional[Any]: from transformers import TaPreTrainedModel self.model_fpaa.get_memory_footprint() self.model_abit.get_memory_footprint() for name, module in self.model_abit.named_modules(): if isinstance(lowerCAmelCase__ , torch.nn.Linear ): if name not in ["lm_head"] + TaPreTrainedModel._keep_in_fpaa_modules: # 4-bit parameters are packed in uint8 variables self.assertTrue(module.weight.dtype == torch.uinta ) def _UpperCamelCase ( self : Optional[Any] ) -> int: UpperCAmelCase = self.tokenizer(self.input_text , return_tensors="pt" ) UpperCAmelCase = self.model_abit.generate(input_ids=encoded_input["input_ids"].to(0 ) , max_new_tokens=1_0 ) self.assertIn(self.tokenizer.decode(output_sequences[0] , skip_special_tokens=lowerCAmelCase__ ) , self.EXPECTED_OUTPUTS ) def _UpperCamelCase ( self : Dict ) -> List[str]: UpperCAmelCase = BitsAndBytesConfig() UpperCAmelCase = True UpperCAmelCase = AutoModelForCausalLM.from_pretrained( self.model_name , quantization_config=lowerCAmelCase__ , device_map="auto" ) UpperCAmelCase = self.tokenizer(self.input_text , return_tensors="pt" ) UpperCAmelCase = model_abit_from_config.generate( input_ids=encoded_input["input_ids"].to(0 ) , max_new_tokens=1_0 ) self.assertIn(self.tokenizer.decode(output_sequences[0] , skip_special_tokens=lowerCAmelCase__ ) , self.EXPECTED_OUTPUTS ) def _UpperCamelCase ( self : str ) -> Tuple: with self.assertRaises(lowerCAmelCase__ ), tempfile.TemporaryDirectory() as tmpdirname: self.model_abit.save_pretrained(lowerCAmelCase__ ) def _UpperCamelCase ( self : int ) -> Tuple: UpperCAmelCase = BitsAndBytesConfig() with self.assertRaises(lowerCAmelCase__ ): UpperCAmelCase = AutoModelForCausalLM.from_pretrained( self.model_name , quantization_config=lowerCAmelCase__ , load_in_abit=lowerCAmelCase__ , device_map="auto" , bnb_abit_quant_type="nf4" , ) def _UpperCamelCase ( self : Any ) -> Union[str, Any]: with self.assertRaises(lowerCAmelCase__ ): # Tries with `str` self.model_abit.to("cpu" ) with self.assertRaises(lowerCAmelCase__ ): # Tries with a `dtype`` self.model_abit.to(torch.floataa ) with self.assertRaises(lowerCAmelCase__ ): # Tries with a `device` self.model_abit.to(torch.device("cuda:0" ) ) with self.assertRaises(lowerCAmelCase__ ): # Tries with a `device` self.model_abit.float() with self.assertRaises(lowerCAmelCase__ ): # Tries with a `device` self.model_abit.half() # Test if we did not break anything UpperCAmelCase = self.tokenizer(self.input_text , return_tensors="pt" ) UpperCAmelCase = self.model_fpaa.to(torch.floataa ) UpperCAmelCase = self.model_fpaa.generate(input_ids=encoded_input["input_ids"].to(0 ) , max_new_tokens=1_0 ) # Check this does not throw an error UpperCAmelCase = self.model_fpaa.to("cpu" ) # Check this does not throw an error UpperCAmelCase = self.model_fpaa.half() # Check this does not throw an error UpperCAmelCase = self.model_fpaa.float() def _UpperCamelCase ( self : Any ) -> Dict: UpperCAmelCase = AutoModelForSeqaSeqLM.from_pretrained("t5-small" , load_in_abit=lowerCAmelCase__ , device_map="auto" ) self.assertTrue(model.decoder.block[0].layer[2].DenseReluDense.wo.weight.dtype == torch.floataa ) @require_bitsandbytes @require_accelerate @require_torch @require_torch_gpu @slow class __magic_name__ ( unittest.TestCase ): @classmethod def _UpperCamelCase ( cls : str ) -> Any: UpperCAmelCase = "t5-small" UpperCAmelCase = "google/flan-t5-small" # flan-t5 uses dense-act instead of dense-relu-dense UpperCAmelCase = AutoTokenizer.from_pretrained(cls.model_name ) UpperCAmelCase = "Translate in German: Hello, my dog is cute" def _UpperCamelCase ( self : Optional[int] ) -> List[str]: gc.collect() torch.cuda.empty_cache() def _UpperCamelCase ( self : List[str] ) -> List[str]: from transformers import TaForConditionalGeneration UpperCAmelCase = TaForConditionalGeneration._keep_in_fpaa_modules UpperCAmelCase = None # test with `t5-small` UpperCAmelCase = TaForConditionalGeneration.from_pretrained(self.model_name , load_in_abit=lowerCAmelCase__ , device_map="auto" ) UpperCAmelCase = self.tokenizer(self.input_text , return_tensors="pt" ).to(0 ) UpperCAmelCase = model.generate(**lowerCAmelCase__ ) # test with `flan-t5-small` UpperCAmelCase = TaForConditionalGeneration.from_pretrained( self.dense_act_model_name , load_in_abit=lowerCAmelCase__ , device_map="auto" ) UpperCAmelCase = self.tokenizer(self.input_text , return_tensors="pt" ).to(0 ) UpperCAmelCase = model.generate(**lowerCAmelCase__ ) UpperCAmelCase = modules def _UpperCamelCase ( self : Any ) -> int: import bitsandbytes as bnb from transformers import TaForConditionalGeneration # test with `t5-small` UpperCAmelCase = TaForConditionalGeneration.from_pretrained(self.model_name , load_in_abit=lowerCAmelCase__ , device_map="auto" ) # there was a bug with decoders - this test checks that it is fixed self.assertTrue(isinstance(model.decoder.block[0].layer[0].SelfAttention.q , bnb.nn.Linearabit ) ) UpperCAmelCase = self.tokenizer(self.input_text , return_tensors="pt" ).to(0 ) UpperCAmelCase = model.generate(**lowerCAmelCase__ ) # test with `flan-t5-small` UpperCAmelCase = TaForConditionalGeneration.from_pretrained( self.dense_act_model_name , load_in_abit=lowerCAmelCase__ , device_map="auto" ) UpperCAmelCase = self.tokenizer(self.input_text , return_tensors="pt" ).to(0 ) UpperCAmelCase = model.generate(**lowerCAmelCase__ ) class __magic_name__ ( _snake_case ): def _UpperCamelCase ( self : str ) -> str: super().setUp() # model_name UpperCAmelCase = "bigscience/bloom-560m" UpperCAmelCase = "t5-small" # Different types of model UpperCAmelCase = AutoModel.from_pretrained(self.model_name , load_in_abit=lowerCAmelCase__ , device_map="auto" ) # Sequence classification model UpperCAmelCase = AutoModelForSequenceClassification.from_pretrained( self.model_name , load_in_abit=lowerCAmelCase__ , device_map="auto" ) # CausalLM model UpperCAmelCase = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=lowerCAmelCase__ , device_map="auto" ) # Seq2seq model UpperCAmelCase = AutoModelForSeqaSeqLM.from_pretrained( self.seq_to_seq_name , load_in_abit=lowerCAmelCase__ , device_map="auto" ) def _UpperCamelCase ( self : Optional[int] ) -> Optional[int]: del self.base_model del self.sequence_model del self.model_abit del self.seq_to_seq_model gc.collect() torch.cuda.empty_cache() def _UpperCamelCase ( self : Any ) -> Dict: from bitsandbytes.nn import Paramsabit self.assertTrue(self.base_model.h[-1].mlp.dense_ah_to_h.weight.__class__ == Paramsabit ) # Other heads should be nn.Parameter self.assertTrue(self.model_abit.lm_head.weight.__class__ == torch.nn.Parameter ) self.assertTrue(self.sequence_model.score.weight.__class__ == torch.nn.Parameter ) self.assertTrue(self.seq_to_seq_model.lm_head.weight.__class__ == torch.nn.Parameter ) class __magic_name__ ( _snake_case ): def _UpperCamelCase ( self : Dict ) -> Optional[int]: super().setUp() def _UpperCamelCase ( self : Optional[Any] ) -> Optional[Any]: del self.pipe gc.collect() torch.cuda.empty_cache() def _UpperCamelCase ( self : Optional[Any] ) -> Any: UpperCAmelCase = pipeline( "text-generation" , model=self.model_name , model_kwargs={"device_map": "auto", "load_in_4bit": True, "torch_dtype": torch.floataa} , max_new_tokens=self.MAX_NEW_TOKENS , ) # Real second forward pass UpperCAmelCase = self.pipe(self.input_text ) self.assertIn(pipeline_output[0]["generated_text"] , self.EXPECTED_OUTPUTS ) @require_torch_multi_gpu class __magic_name__ ( _snake_case ): def _UpperCamelCase ( self : List[str] ) -> List[str]: super().setUp() def _UpperCamelCase ( self : List[Any] ) -> Tuple: UpperCAmelCase = AutoModelForCausalLM.from_pretrained( self.model_name , load_in_abit=lowerCAmelCase__ , device_map="balanced" ) # Check correct device map self.assertEqual(set(model_parallel.hf_device_map.values() ) , {0, 1} ) # Check that inference pass works on the model UpperCAmelCase = self.tokenizer(self.input_text , return_tensors="pt" ) # Second real batch UpperCAmelCase = model_parallel.generate(input_ids=encoded_input["input_ids"].to(0 ) , max_new_tokens=1_0 ) self.assertIn(self.tokenizer.decode(output_parallel[0] , skip_special_tokens=lowerCAmelCase__ ) , self.EXPECTED_OUTPUTS ) class __magic_name__ ( _snake_case ): def _UpperCamelCase ( self : Dict ) -> Dict: UpperCAmelCase = "facebook/opt-350m" super().setUp() def _UpperCamelCase ( self : Tuple ) -> int: if version.parse(importlib.metadata.version("bitsandbytes" ) ) < version.parse("0.37.0" ): return # Step 1: freeze all parameters UpperCAmelCase = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=lowerCAmelCase__ ) self.assertEqual(set(model.hf_device_map.values() ) , {torch.cuda.current_device()} ) for param in model.parameters(): UpperCAmelCase = False # freeze the model - train adapters later if param.ndim == 1: # cast the small parameters (e.g. layernorm) to fp32 for stability UpperCAmelCase = param.data.to(torch.floataa ) # Step 2: add adapters for _, module in model.named_modules(): if "OPTAttention" in repr(type(lowerCAmelCase__ ) ): UpperCAmelCase = LoRALayer(module.q_proj , rank=1_6 ) UpperCAmelCase = LoRALayer(module.k_proj , rank=1_6 ) UpperCAmelCase = LoRALayer(module.v_proj , rank=1_6 ) # Step 3: dummy batch UpperCAmelCase = self.tokenizer("Test batch " , return_tensors="pt" ).to(0 ) # Step 4: Check if the gradient is not None with torch.cuda.amp.autocast(): UpperCAmelCase = model.forward(**lowerCAmelCase__ ) out.logits.norm().backward() for module in model.modules(): if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): self.assertTrue(module.adapter[1].weight.grad is not None ) self.assertTrue(module.adapter[1].weight.grad.norm().item() > 0 ) elif isinstance(lowerCAmelCase__ , nn.Embedding ): self.assertTrue(module.weight.grad is None ) class __magic_name__ ( _snake_case ): UpperCAmelCase = """gpt2-xl""" UpperCAmelCase = 3.3191_8548_5415_2187
1
def _lowerCAmelCase( __A , __A , __A ): if n == 0: return 1 elif n % 2 == 1: return (binary_exponentiation(__A , n - 1 , __A ) * a) % mod else: UpperCAmelCase = binary_exponentiation(__A , n / 2 , __A ) return (b * b) % mod # a prime number lowerCAmelCase__ = 701 lowerCAmelCase__ = 1000000000 lowerCAmelCase__ = 10 # using binary exponentiation function, O(log(p)): print((a / b) % p == (a * binary_exponentiation(b, p - 2, p)) % p) print((a / b) % p == (a * b ** (p - 2)) % p)
1
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available lowerCAmelCase__ = { "configuration_groupvit": [ "GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP", "GroupViTConfig", "GroupViTOnnxConfig", "GroupViTTextConfig", "GroupViTVisionConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ "GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST", "GroupViTModel", "GroupViTPreTrainedModel", "GroupViTTextModel", "GroupViTVisionModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ "TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST", "TFGroupViTModel", "TFGroupViTPreTrainedModel", "TFGroupViTTextModel", "TFGroupViTVisionModel", ] if TYPE_CHECKING: from .configuration_groupvit import ( GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GroupViTConfig, GroupViTOnnxConfig, GroupViTTextConfig, GroupViTVisionConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_groupvit import ( GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, GroupViTModel, GroupViTPreTrainedModel, GroupViTTextModel, GroupViTVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_groupvit import ( TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFGroupViTModel, TFGroupViTPreTrainedModel, TFGroupViTTextModel, TFGroupViTVisionModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
1
lowerCAmelCase__ = { "a": "AAAAA", "b": "AAAAB", "c": "AAABA", "d": "AAABB", "e": "AABAA", "f": "AABAB", "g": "AABBA", "h": "AABBB", "i": "ABAAA", "j": "BBBAA", "k": "ABAAB", "l": "ABABA", "m": "ABABB", "n": "ABBAA", "o": "ABBAB", "p": "ABBBA", "q": "ABBBB", "r": "BAAAA", "s": "BAAAB", "t": "BAABA", "u": "BAABB", "v": "BBBAB", "w": "BABAA", "x": "BABAB", "y": "BABBA", "z": "BABBB", " ": " ", } lowerCAmelCase__ = {value: key for key, value in encode_dict.items()} def _lowerCAmelCase( __A ): UpperCAmelCase = "" 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( __A ): if set(__A ) - {"A", "B", " "} != set(): raise Exception("decode() accepts only 'A', 'B' and spaces" ) UpperCAmelCase = "" for word in coded.split(): while len(__A ) != 0: decoded += decode_dict[word[:5]] UpperCAmelCase = word[5:] decoded += " " return decoded.strip() if __name__ == "__main__": from doctest import testmod testmod()
1
1
from dataclasses import dataclass from typing import Optional, Tuple import torch from torch import nn from transformers import RobertaPreTrainedModel, XLMRobertaConfig, XLMRobertaModel from transformers.utils import ModelOutput @dataclass class __magic_name__ ( _snake_case ): UpperCAmelCase = None UpperCAmelCase = None UpperCAmelCase = None UpperCAmelCase = None class __magic_name__ ( _snake_case ): def __init__( self : int , lowerCAmelCase__ : Dict=1 , lowerCAmelCase__ : Optional[int]=0 , lowerCAmelCase__ : str=2 , lowerCAmelCase__ : List[Any]=5_1_2 , lowerCAmelCase__ : Dict="cls" , lowerCAmelCase__ : Tuple=False , lowerCAmelCase__ : List[Any]=True , **lowerCAmelCase__ : int , ) -> Optional[Any]: super().__init__(pad_token_id=lowerCAmelCase__ , bos_token_id=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__ , **lowerCAmelCase__ ) UpperCAmelCase = project_dim UpperCAmelCase = pooler_fn UpperCAmelCase = learn_encoder UpperCAmelCase = use_attention_mask class __magic_name__ ( _snake_case ): UpperCAmelCase = [r"""pooler""", r"""logit_scale"""] UpperCAmelCase = [r"""position_ids""", r"""predictions.decoder.bias"""] UpperCAmelCase = """roberta""" UpperCAmelCase = RobertaSeriesConfig def __init__( self : Tuple , lowerCAmelCase__ : Tuple ) -> Union[str, Any]: super().__init__(lowerCAmelCase__ ) UpperCAmelCase = XLMRobertaModel(lowerCAmelCase__ ) UpperCAmelCase = nn.Linear(config.hidden_size , config.project_dim ) UpperCAmelCase = getattr(lowerCAmelCase__ , "has_pre_transformation" , lowerCAmelCase__ ) if self.has_pre_transformation: UpperCAmelCase = nn.Linear(config.hidden_size , config.project_dim ) UpperCAmelCase = nn.LayerNorm(config.hidden_size , eps=config.layer_norm_eps ) self.post_init() def _UpperCamelCase ( self : str , lowerCAmelCase__ : Optional[torch.Tensor] = None , lowerCAmelCase__ : Optional[torch.Tensor] = None , lowerCAmelCase__ : Optional[torch.Tensor] = None , lowerCAmelCase__ : Optional[torch.Tensor] = None , lowerCAmelCase__ : Optional[torch.Tensor] = None , lowerCAmelCase__ : Optional[torch.Tensor] = None , lowerCAmelCase__ : Optional[torch.Tensor] = None , lowerCAmelCase__ : Optional[torch.Tensor] = None , lowerCAmelCase__ : Optional[bool] = None , lowerCAmelCase__ : Optional[bool] = None , lowerCAmelCase__ : Optional[bool] = None , ) -> Union[str, Any]: UpperCAmelCase = return_dict if return_dict is not None else self.config.use_return_dict UpperCAmelCase = self.base_model( input_ids=lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , position_ids=lowerCAmelCase__ , head_mask=lowerCAmelCase__ , inputs_embeds=lowerCAmelCase__ , encoder_hidden_states=lowerCAmelCase__ , encoder_attention_mask=lowerCAmelCase__ , output_attentions=lowerCAmelCase__ , output_hidden_states=True if self.has_pre_transformation else output_hidden_states , return_dict=lowerCAmelCase__ , ) if self.has_pre_transformation: UpperCAmelCase = outputs["hidden_states"][-2] UpperCAmelCase = self.pre_LN(lowerCAmelCase__ ) UpperCAmelCase = self.transformation_pre(lowerCAmelCase__ ) return TransformationModelOutput( projection_state=lowerCAmelCase__ , last_hidden_state=outputs.last_hidden_state , hidden_states=outputs.hidden_states , attentions=outputs.attentions , ) else: UpperCAmelCase = self.transformation(outputs.last_hidden_state ) return TransformationModelOutput( projection_state=lowerCAmelCase__ , last_hidden_state=outputs.last_hidden_state , hidden_states=outputs.hidden_states , attentions=outputs.attentions , )
1
from __future__ import annotations import json import requests from bsa import BeautifulSoup from fake_useragent import UserAgent lowerCAmelCase__ = {"UserAgent": UserAgent().random} def _lowerCAmelCase( __A ): UpperCAmelCase = script.contents[0] UpperCAmelCase = json.loads(data[data.find("{\"config\"" ) : -1] ) return info["entry_data"]["ProfilePage"][0]["graphql"]["user"] class __magic_name__ : def __init__( self : Optional[Any] , lowerCAmelCase__ : Optional[int] ) -> Any: UpperCAmelCase = f"https://www.instagram.com/{username}/" UpperCAmelCase = self.get_json() def _UpperCamelCase ( self : List[str] ) -> dict: UpperCAmelCase = requests.get(self.url , headers=lowerCAmelCase__ ).text UpperCAmelCase = BeautifulSoup(lowerCAmelCase__ , "html.parser" ).find_all("script" ) try: return extract_user_profile(scripts[4] ) except (json.decoder.JSONDecodeError, KeyError): return extract_user_profile(scripts[3] ) def __repr__( self : Tuple ) -> str: return f"{self.__class__.__name__}('{self.username}')" def __str__( self : Optional[int] ) -> str: return f"{self.fullname} ({self.username}) is {self.biography}" @property def _UpperCamelCase ( self : Any ) -> str: return self.user_data["username"] @property def _UpperCamelCase ( self : List[Any] ) -> str: return self.user_data["full_name"] @property def _UpperCamelCase ( self : List[str] ) -> str: return self.user_data["biography"] @property def _UpperCamelCase ( self : Optional[int] ) -> str: return self.user_data["business_email"] @property def _UpperCamelCase ( self : str ) -> str: return self.user_data["external_url"] @property def _UpperCamelCase ( self : int ) -> int: return self.user_data["edge_followed_by"]["count"] @property def _UpperCamelCase ( self : List[Any] ) -> int: return self.user_data["edge_follow"]["count"] @property def _UpperCamelCase ( self : List[str] ) -> int: return self.user_data["edge_owner_to_timeline_media"]["count"] @property def _UpperCamelCase ( self : Tuple ) -> str: return self.user_data["profile_pic_url_hd"] @property def _UpperCamelCase ( self : Optional[int] ) -> bool: return self.user_data["is_verified"] @property def _UpperCamelCase ( self : Optional[Any] ) -> bool: return self.user_data["is_private"] def _lowerCAmelCase( __A = "github" ): import os if os.environ.get("CI" ): return # test failing on GitHub Actions UpperCAmelCase = InstagramUser(__A ) assert instagram_user.user_data assert isinstance(instagram_user.user_data , __A ) assert instagram_user.username == username if username != "github": return assert instagram_user.fullname == "GitHub" assert instagram_user.biography == "Built for developers." assert instagram_user.number_of_posts > 150 assert instagram_user.number_of_followers > 120000 assert instagram_user.number_of_followings > 15 assert instagram_user.email == "support@github.com" assert instagram_user.website == "https://github.com/readme" assert instagram_user.profile_picture_url.startswith("https://instagram." ) assert instagram_user.is_verified is True assert instagram_user.is_private is False if __name__ == "__main__": import doctest doctest.testmod() lowerCAmelCase__ = InstagramUser("github") print(instagram_user) print(f"{instagram_user.number_of_posts = }") print(f"{instagram_user.number_of_followers = }") print(f"{instagram_user.number_of_followings = }") print(f"{instagram_user.email = }") print(f"{instagram_user.website = }") print(f"{instagram_user.profile_picture_url = }") print(f"{instagram_user.is_verified = }") print(f"{instagram_user.is_private = }")
1
1
import pytest from datasets import inspect_metric, list_metrics, load_metric @pytest.fixture def _lowerCAmelCase( __A ): monkeypatch.setattr("datasets.utils.deprecation_utils._emitted_deprecation_warnings" , set() ) @pytest.fixture def _lowerCAmelCase( __A ): class __magic_name__ : def __init__( self : Optional[int] , lowerCAmelCase__ : int ) -> int: UpperCAmelCase = metric_id class __magic_name__ : UpperCAmelCase = [MetricMock(_snake_case ) for metric_id in ["""accuracy""", """mse""", """precision""", """codeparrot/apps_metric"""]] def _UpperCamelCase ( self : List[str] ) -> Dict: return self._metrics monkeypatch.setattr("datasets.inspect.huggingface_hub" , HfhMock() ) @pytest.mark.parametrize( "func, args" , [(load_metric, ("metrics/mse",)), (list_metrics, ()), (inspect_metric, ("metrics/mse", "tmp_path"))] ) def _lowerCAmelCase( __A , __A , __A , __A , __A ): if "tmp_path" in args: UpperCAmelCase = tuple(arg if arg != "tmp_path" else tmp_path for arg in args ) with pytest.warns(__A , match="https://huggingface.co/docs/evaluate" ): func(*__A )
1
import unittest import numpy as np def _lowerCAmelCase( __A , __A , __A , __A = None , ): UpperCAmelCase = np.shape(__A ) UpperCAmelCase = np.shape(__A ) UpperCAmelCase = np.shape(__A ) if shape_a[0] != shape_b[0]: UpperCAmelCase = ( "Expected the same number of rows for A and B. " F"Instead found A of size {shape_a} and B of size {shape_b}" ) raise ValueError(__A ) if shape_b[1] != shape_c[1]: UpperCAmelCase = ( "Expected the same number of columns for B and C. " F"Instead found B of size {shape_b} and C of size {shape_c}" ) raise ValueError(__A ) UpperCAmelCase = pseudo_inv if a_inv is None: try: UpperCAmelCase = np.linalg.inv(__A ) except np.linalg.LinAlgError: raise ValueError( "Input matrix A is not invertible. Cannot compute Schur complement." ) return mat_c - mat_b.T @ a_inv @ mat_b class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : List[str] ) -> None: UpperCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase = np.array([[2, 1], [6, 3]] ) UpperCAmelCase = schur_complement(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase = np.block([[a, b], [b.T, c]] ) UpperCAmelCase = np.linalg.det(lowerCAmelCase__ ) UpperCAmelCase = np.linalg.det(lowerCAmelCase__ ) UpperCAmelCase = np.linalg.det(lowerCAmelCase__ ) self.assertAlmostEqual(lowerCAmelCase__ , det_a * det_s ) def _UpperCamelCase ( self : str ) -> None: UpperCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase = np.array([[2, 1], [6, 3]] ) with self.assertRaises(lowerCAmelCase__ ): schur_complement(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def _UpperCamelCase ( self : Dict ) -> None: UpperCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase = np.array([[2, 1, 3], [6, 3, 5]] ) with self.assertRaises(lowerCAmelCase__ ): schur_complement(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) if __name__ == "__main__": import doctest doctest.testmod() unittest.main()
1
1
from __future__ import annotations from collections import namedtuple def _lowerCAmelCase( __A , __A , __A ): UpperCAmelCase = namedtuple("result" , "name value" ) if (voltage, current, power).count(0 ) != 1: raise ValueError("Only one argument must be 0" ) elif power < 0: raise ValueError( "Power cannot be negative in any electrical/electronics system" ) elif voltage == 0: return result("voltage" , power / current ) elif current == 0: return result("current" , power / voltage ) elif power == 0: return result("power" , float(round(abs(voltage * current ) , 2 ) ) ) else: raise ValueError("Exactly one argument must be 0" ) if __name__ == "__main__": import doctest doctest.testmod()
1
import argparse import os import re import numpy as np import PIL import torch from timm import create_model from torch.optim.lr_scheduler import OneCycleLR from torch.utils.data import DataLoader, Dataset from torchvision.transforms import Compose, RandomResizedCrop, Resize, ToTensor from accelerate import Accelerator def _lowerCAmelCase( __A ): UpperCAmelCase = fname.split(os.path.sep )[-1] return re.search(r"^(.*)_\d+\.jpg$" , __A ).groups()[0] class __magic_name__ ( _snake_case ): def __init__( self : Any , lowerCAmelCase__ : int , lowerCAmelCase__ : Union[str, Any]=None , lowerCAmelCase__ : int=None ) -> Optional[Any]: UpperCAmelCase = file_names UpperCAmelCase = image_transform UpperCAmelCase = label_to_id def __len__( self : Tuple ) -> List[str]: return len(self.file_names ) def __getitem__( self : Optional[int] , lowerCAmelCase__ : Tuple ) -> Dict: UpperCAmelCase = self.file_names[idx] UpperCAmelCase = PIL.Image.open(lowerCAmelCase__ ) UpperCAmelCase = raw_image.convert("RGB" ) if self.image_transform is not None: UpperCAmelCase = self.image_transform(lowerCAmelCase__ ) UpperCAmelCase = extract_label(lowerCAmelCase__ ) if self.label_to_id is not None: UpperCAmelCase = self.label_to_id[label] return {"image": image, "label": label} def _lowerCAmelCase( __A , __A ): # Initialize accelerator if args.with_tracking: UpperCAmelCase = Accelerator( cpu=args.cpu , mixed_precision=args.mixed_precision , log_with="all" , project_dir=args.project_dir ) else: UpperCAmelCase = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs UpperCAmelCase = config["lr"] UpperCAmelCase = int(config["num_epochs"] ) UpperCAmelCase = int(config["seed"] ) UpperCAmelCase = int(config["batch_size"] ) UpperCAmelCase = config["image_size"] if not isinstance(__A , (list, tuple) ): UpperCAmelCase = (image_size, image_size) # Parse out whether we are saving every epoch or after a certain number of batches if hasattr(args.checkpointing_steps , "isdigit" ): if args.checkpointing_steps == "epoch": UpperCAmelCase = args.checkpointing_steps elif args.checkpointing_steps.isdigit(): UpperCAmelCase = int(args.checkpointing_steps ) else: raise ValueError( F"Argument `checkpointing_steps` must be either a number or `epoch`. `{args.checkpointing_steps}` passed." ) else: UpperCAmelCase = None # We need to initialize the trackers we use, and also store our configuration if args.with_tracking: UpperCAmelCase = os.path.split(__A )[-1].split("." )[0] accelerator.init_trackers(__A , __A ) # Grab all the image filenames UpperCAmelCase = [os.path.join(args.data_dir , __A ) for fname in os.listdir(args.data_dir ) if fname.endswith(".jpg" )] # Build the label correspondences UpperCAmelCase = [extract_label(__A ) for fname in file_names] UpperCAmelCase = list(set(__A ) ) id_to_label.sort() UpperCAmelCase = {lbl: i for i, lbl in enumerate(__A )} # Set the seed before splitting the data. np.random.seed(__A ) torch.manual_seed(__A ) torch.cuda.manual_seed_all(__A ) # Split our filenames between train and validation UpperCAmelCase = np.random.permutation(len(__A ) ) UpperCAmelCase = int(0.8 * len(__A ) ) UpperCAmelCase = random_perm[:cut] UpperCAmelCase = random_perm[cut:] # For training we use a simple RandomResizedCrop UpperCAmelCase = Compose([RandomResizedCrop(__A , scale=(0.5, 1.0) ), ToTensor()] ) UpperCAmelCase = PetsDataset( [file_names[i] for i in train_split] , image_transform=__A , label_to_id=__A ) # For evaluation, we use a deterministic Resize UpperCAmelCase = Compose([Resize(__A ), ToTensor()] ) UpperCAmelCase = PetsDataset([file_names[i] for i in eval_split] , image_transform=__A , label_to_id=__A ) # Instantiate dataloaders. UpperCAmelCase = DataLoader(__A , shuffle=__A , batch_size=__A , num_workers=4 ) UpperCAmelCase = DataLoader(__A , shuffle=__A , batch_size=__A , num_workers=4 ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) UpperCAmelCase = create_model("resnet50d" , pretrained=__A , num_classes=len(__A ) ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). UpperCAmelCase = model.to(accelerator.device ) # Freezing the base model for param in model.parameters(): UpperCAmelCase = False for param in model.get_classifier().parameters(): UpperCAmelCase = True # We normalize the batches of images to be a bit faster. UpperCAmelCase = torch.tensor(model.default_cfg["mean"] )[None, :, None, None].to(accelerator.device ) UpperCAmelCase = torch.tensor(model.default_cfg["std"] )[None, :, None, None].to(accelerator.device ) # Instantiate optimizer UpperCAmelCase = torch.optim.Adam(params=model.parameters() , lr=lr / 25 ) # Instantiate learning rate scheduler UpperCAmelCase = OneCycleLR(optimizer=__A , max_lr=__A , epochs=__A , steps_per_epoch=len(__A ) ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = accelerator.prepare( __A , __A , __A , __A , __A ) # We need to keep track of how many total steps we have iterated over UpperCAmelCase = 0 # We also need to keep track of the starting epoch so files are named properly UpperCAmelCase = 0 # Potentially load in the weights and states from a previous save if args.resume_from_checkpoint: if args.resume_from_checkpoint is not None or args.resume_from_checkpoint != "": accelerator.print(F"Resumed from checkpoint: {args.resume_from_checkpoint}" ) accelerator.load_state(args.resume_from_checkpoint ) UpperCAmelCase = os.path.basename(args.resume_from_checkpoint ) else: # Get the most recent checkpoint UpperCAmelCase = [f.name for f in os.scandir(os.getcwd() ) if f.is_dir()] dirs.sort(key=os.path.getctime ) UpperCAmelCase = dirs[-1] # Sorts folders by date modified, most recent checkpoint is the last # Extract `epoch_{i}` or `step_{i}` UpperCAmelCase = os.path.splitext(__A )[0] if "epoch" in training_difference: UpperCAmelCase = int(training_difference.replace("epoch_" , "" ) ) + 1 UpperCAmelCase = None else: UpperCAmelCase = int(training_difference.replace("step_" , "" ) ) UpperCAmelCase = resume_step // len(__A ) resume_step -= starting_epoch * len(__A ) # Now we train the model for epoch in range(__A , __A ): model.train() if args.with_tracking: UpperCAmelCase = 0 if args.resume_from_checkpoint and epoch == starting_epoch and resume_step is not None: # We need to skip steps until we reach the resumed step UpperCAmelCase = accelerator.skip_first_batches(__A , __A ) overall_step += resume_step else: # After the first iteration though, we need to go back to the original dataloader UpperCAmelCase = train_dataloader for batch in active_dataloader: # We could avoid this line since we set the accelerator with `device_placement=True`. UpperCAmelCase = {k: v.to(accelerator.device ) for k, v in batch.items()} UpperCAmelCase = (batch["image"] - mean) / std UpperCAmelCase = model(__A ) UpperCAmelCase = torch.nn.functional.cross_entropy(__A , batch["label"] ) # We keep track of the loss at each epoch if args.with_tracking: total_loss += loss.detach().float() accelerator.backward(__A ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 if isinstance(__A , __A ): UpperCAmelCase = F"step_{overall_step}" if overall_step % checkpointing_steps == 0: if args.output_dir is not None: UpperCAmelCase = os.path.join(args.output_dir , __A ) accelerator.save_state(__A ) model.eval() UpperCAmelCase = 0 UpperCAmelCase = 0 for step, batch in enumerate(__A ): # We could avoid this line since we set the accelerator with `device_placement=True`. UpperCAmelCase = {k: v.to(accelerator.device ) for k, v in batch.items()} UpperCAmelCase = (batch["image"] - mean) / std with torch.no_grad(): UpperCAmelCase = model(__A ) UpperCAmelCase = outputs.argmax(dim=-1 ) UpperCAmelCase , UpperCAmelCase = accelerator.gather_for_metrics((predictions, batch["label"]) ) UpperCAmelCase = predictions == references num_elems += accurate_preds.shape[0] accurate += accurate_preds.long().sum() UpperCAmelCase = accurate.item() / num_elems # Use accelerator.print to print only on the main process. accelerator.print(F"epoch {epoch}: {100 * eval_metric:.2f}" ) if args.with_tracking: accelerator.log( { "accuracy": 100 * eval_metric, "train_loss": total_loss.item() / len(__A ), "epoch": epoch, } , step=__A , ) if checkpointing_steps == "epoch": UpperCAmelCase = F"epoch_{epoch}" if args.output_dir is not None: UpperCAmelCase = os.path.join(args.output_dir , __A ) accelerator.save_state(__A ) if args.with_tracking: accelerator.end_training() def _lowerCAmelCase( ): UpperCAmelCase = argparse.ArgumentParser(description="Simple example of training script." ) parser.add_argument("--data_dir" , required=__A , help="The data folder on disk." ) parser.add_argument("--fp16" , action="store_true" , help="If passed, will use FP16 training." ) parser.add_argument( "--mixed_precision" , type=__A , default=__A , choices=["no", "fp16", "bf16", "fp8"] , help="Whether to use mixed precision. Choose" "between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10." "and an Nvidia Ampere GPU." , ) parser.add_argument("--cpu" , action="store_true" , help="If passed, will train on the CPU." ) parser.add_argument( "--checkpointing_steps" , type=__A , default=__A , help="Whether the various states should be saved at the end of every n steps, or 'epoch' for each epoch." , ) parser.add_argument( "--output_dir" , type=__A , default="." , help="Optional save directory where all checkpoint folders will be stored. Default is the current working directory." , ) parser.add_argument( "--resume_from_checkpoint" , type=__A , default=__A , help="If the training should continue from a checkpoint folder." , ) parser.add_argument( "--with_tracking" , action="store_true" , help="Whether to load in all available experiment trackers from the environment and use them for logging." , ) parser.add_argument( "--project_dir" , type=__A , default="logs" , help="Location on where to store experiment tracking logs` and relevent project information" , ) UpperCAmelCase = parser.parse_args() UpperCAmelCase = {"lr": 3E-2, "num_epochs": 3, "seed": 42, "batch_size": 64, "image_size": 224} training_function(__A , __A ) if __name__ == "__main__": main()
1
1
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, 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, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, logging lowerCAmelCase__ = logging.get_logger(__name__) class __magic_name__ ( _snake_case ): UpperCAmelCase = ["""pixel_values"""] def __init__( self : List[Any] , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : Optional[Dict[str, int]] = None , lowerCAmelCase__ : PILImageResampling = PILImageResampling.BILINEAR , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : Dict[str, int] = None , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : Union[int, float] = 1 / 2_5_5 , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : Optional[Union[float, List[float]]] = None , lowerCAmelCase__ : Optional[Union[float, List[float]]] = None , **lowerCAmelCase__ : List[str] , ) -> None: super().__init__(**lowerCAmelCase__ ) UpperCAmelCase = size if size is not None else {"shortest_edge": 2_5_6} UpperCAmelCase = get_size_dict(lowerCAmelCase__ , default_to_square=lowerCAmelCase__ ) UpperCAmelCase = crop_size if crop_size is not None else {"height": 2_2_4, "width": 2_2_4} UpperCAmelCase = get_size_dict(lowerCAmelCase__ ) UpperCAmelCase = do_resize UpperCAmelCase = size UpperCAmelCase = resample UpperCAmelCase = do_center_crop UpperCAmelCase = crop_size UpperCAmelCase = do_rescale UpperCAmelCase = rescale_factor UpperCAmelCase = do_normalize UpperCAmelCase = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN UpperCAmelCase = image_std if image_std is not None else IMAGENET_STANDARD_STD def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : Dict[str, int] , lowerCAmelCase__ : PILImageResampling = PILImageResampling.BICUBIC , lowerCAmelCase__ : Optional[Union[str, ChannelDimension]] = None , **lowerCAmelCase__ : Tuple , ) -> np.ndarray: UpperCAmelCase = get_size_dict(lowerCAmelCase__ , default_to_square=lowerCAmelCase__ ) if "shortest_edge" not in size: raise ValueError(f"The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}" ) UpperCAmelCase = get_resize_output_image_size(lowerCAmelCase__ , size=size["shortest_edge"] , default_to_square=lowerCAmelCase__ ) return resize(lowerCAmelCase__ , size=lowerCAmelCase__ , resample=lowerCAmelCase__ , data_format=lowerCAmelCase__ , **lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : Dict[str, int] , lowerCAmelCase__ : Optional[Union[str, ChannelDimension]] = None , **lowerCAmelCase__ : int , ) -> np.ndarray: UpperCAmelCase = get_size_dict(lowerCAmelCase__ ) return center_crop(lowerCAmelCase__ , size=(size["height"], size["width"]) , data_format=lowerCAmelCase__ , **lowerCAmelCase__ ) def _UpperCamelCase ( self : List[Any] , lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : float , lowerCAmelCase__ : Optional[Union[str, ChannelDimension]] = None , **lowerCAmelCase__ : Any ) -> np.ndarray: return rescale(lowerCAmelCase__ , scale=lowerCAmelCase__ , data_format=lowerCAmelCase__ , **lowerCAmelCase__ ) def _UpperCamelCase ( self : str , lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : Union[float, List[float]] , lowerCAmelCase__ : Union[float, List[float]] , lowerCAmelCase__ : Optional[Union[str, ChannelDimension]] = None , **lowerCAmelCase__ : Optional[int] , ) -> np.ndarray: return normalize(lowerCAmelCase__ , mean=lowerCAmelCase__ , std=lowerCAmelCase__ , data_format=lowerCAmelCase__ , **lowerCAmelCase__ ) def _UpperCamelCase ( self : Tuple , lowerCAmelCase__ : ImageInput , lowerCAmelCase__ : Optional[bool] = None , lowerCAmelCase__ : Dict[str, int] = None , lowerCAmelCase__ : PILImageResampling = None , lowerCAmelCase__ : bool = None , lowerCAmelCase__ : Dict[str, int] = None , lowerCAmelCase__ : Optional[bool] = None , lowerCAmelCase__ : Optional[float] = None , lowerCAmelCase__ : Optional[bool] = None , lowerCAmelCase__ : Optional[Union[float, List[float]]] = None , lowerCAmelCase__ : Optional[Union[float, List[float]]] = None , lowerCAmelCase__ : Optional[Union[str, TensorType]] = None , lowerCAmelCase__ : Union[str, ChannelDimension] = ChannelDimension.FIRST , **lowerCAmelCase__ : str , ) -> Dict: UpperCAmelCase = do_resize if do_resize is not None else self.do_resize UpperCAmelCase = size if size is not None else self.size UpperCAmelCase = get_size_dict(lowerCAmelCase__ , default_to_square=lowerCAmelCase__ ) UpperCAmelCase = resample if resample is not None else self.resample UpperCAmelCase = do_center_crop if do_center_crop is not None else self.do_center_crop UpperCAmelCase = crop_size if crop_size is not None else self.crop_size UpperCAmelCase = get_size_dict(lowerCAmelCase__ ) UpperCAmelCase = do_rescale if do_rescale is not None else self.do_rescale UpperCAmelCase = rescale_factor if rescale_factor is not None else self.rescale_factor UpperCAmelCase = do_normalize if do_normalize is not None else self.do_normalize UpperCAmelCase = image_mean if image_mean is not None else self.image_mean UpperCAmelCase = image_std if image_std is not None else self.image_std UpperCAmelCase = make_list_of_images(lowerCAmelCase__ ) if not valid_images(lowerCAmelCase__ ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None: raise ValueError("Size must be specified if do_resize is True." ) if do_center_crop and crop_size is None: raise ValueError("Crop size must be specified if do_center_crop is True." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("Image mean and std must be specified if do_normalize is True." ) # All transformations expect numpy arrays. UpperCAmelCase = [to_numpy_array(lowerCAmelCase__ ) for image in images] if do_resize: UpperCAmelCase = [self.resize(image=lowerCAmelCase__ , size=lowerCAmelCase__ , resample=lowerCAmelCase__ ) for image in images] if do_center_crop: UpperCAmelCase = [self.center_crop(image=lowerCAmelCase__ , size=lowerCAmelCase__ ) for image in images] if do_rescale: UpperCAmelCase = [self.rescale(image=lowerCAmelCase__ , scale=lowerCAmelCase__ ) for image in images] if do_normalize: UpperCAmelCase = [self.normalize(image=lowerCAmelCase__ , mean=lowerCAmelCase__ , std=lowerCAmelCase__ ) for image in images] UpperCAmelCase = [to_channel_dimension_format(lowerCAmelCase__ , lowerCAmelCase__ ) for image in images] UpperCAmelCase = {"pixel_values": images} return BatchFeature(data=lowerCAmelCase__ , tensor_type=lowerCAmelCase__ )
1
import glob import os import random from string import ascii_lowercase, digits import cva lowerCAmelCase__ = "" lowerCAmelCase__ = "" lowerCAmelCase__ = "" lowerCAmelCase__ = 1 # (0 is vertical, 1 is horizontal) def _lowerCAmelCase( ): UpperCAmelCase , UpperCAmelCase = get_dataset(__A , __A ) print("Processing..." ) UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = update_image_and_anno(__A , __A , __A ) for index, image in enumerate(__A ): # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' UpperCAmelCase = random_chars(32 ) UpperCAmelCase = paths[index].split(os.sep )[-1].rsplit("." , 1 )[0] UpperCAmelCase = F"{OUTPUT_DIR}/{file_name}_FLIP_{letter_code}" cva.imwrite(F"/{file_root}.jpg" , __A , [cva.IMWRITE_JPEG_QUALITY, 85] ) print(F"Success {index+1}/{len(__A )} with {file_name}" ) UpperCAmelCase = [] for anno in new_annos[index]: UpperCAmelCase = F"{anno[0]} {anno[1]} {anno[2]} {anno[3]} {anno[4]}" annos_list.append(__A ) with open(F"/{file_root}.txt" , "w" ) as outfile: outfile.write("\n".join(line for line in annos_list ) ) def _lowerCAmelCase( __A , __A ): UpperCAmelCase = [] UpperCAmelCase = [] for label_file in glob.glob(os.path.join(__A , "*.txt" ) ): UpperCAmelCase = label_file.split(os.sep )[-1].rsplit("." , 1 )[0] with open(__A ) as in_file: UpperCAmelCase = in_file.readlines() UpperCAmelCase = os.path.join(__A , F"{label_name}.jpg" ) UpperCAmelCase = [] for obj_list in obj_lists: UpperCAmelCase = obj_list.rstrip("\n" ).split(" " ) boxes.append( [ int(obj[0] ), float(obj[1] ), float(obj[2] ), float(obj[3] ), float(obj[4] ), ] ) if not boxes: continue img_paths.append(__A ) labels.append(__A ) return img_paths, labels def _lowerCAmelCase( __A , __A , __A = 1 ): UpperCAmelCase = [] UpperCAmelCase = [] UpperCAmelCase = [] for idx in range(len(__A ) ): UpperCAmelCase = [] UpperCAmelCase = img_list[idx] path_list.append(__A ) UpperCAmelCase = anno_list[idx] UpperCAmelCase = cva.imread(__A ) if flip_type == 1: UpperCAmelCase = cva.flip(__A , __A ) for bbox in img_annos: UpperCAmelCase = 1 - bbox[1] new_annos.append([bbox[0], x_center_new, bbox[2], bbox[3], bbox[4]] ) elif flip_type == 0: UpperCAmelCase = cva.flip(__A , __A ) for bbox in img_annos: UpperCAmelCase = 1 - bbox[2] new_annos.append([bbox[0], bbox[1], y_center_new, bbox[3], bbox[4]] ) new_annos_lists.append(__A ) new_imgs_list.append(__A ) return new_imgs_list, new_annos_lists, path_list def _lowerCAmelCase( __A = 32 ): assert number_char > 1, "The number of character should greater than 1" UpperCAmelCase = ascii_lowercase + digits return "".join(random.choice(__A ) for _ in range(__A ) ) if __name__ == "__main__": main() print("DONE ✅")
1
1
from __future__ import annotations def _lowerCAmelCase( __A ): if not nums: raise ValueError("List is empty" ) return sum(__A ) / len(__A ) if __name__ == "__main__": import doctest doctest.testmod()
1
def _lowerCAmelCase( __A ): if not isinstance(__A , __A ): raise TypeError("only integers accepted as input" ) else: UpperCAmelCase = str(abs(__A ) ) UpperCAmelCase = [list(__A ) for char in range(len(__A ) )] for index in range(len(__A ) ): num_transpositions[index].pop(__A ) return max( int("".join(list(__A ) ) ) for transposition in num_transpositions ) if __name__ == "__main__": __import__("doctest").testmod()
1
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase__ = { "configuration_convbert": ["CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "ConvBertConfig", "ConvBertOnnxConfig"], "tokenization_convbert": ["ConvBertTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ["ConvBertTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ "CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST", "ConvBertForMaskedLM", "ConvBertForMultipleChoice", "ConvBertForQuestionAnswering", "ConvBertForSequenceClassification", "ConvBertForTokenClassification", "ConvBertLayer", "ConvBertModel", "ConvBertPreTrainedModel", "load_tf_weights_in_convbert", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ "TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST", "TFConvBertForMaskedLM", "TFConvBertForMultipleChoice", "TFConvBertForQuestionAnswering", "TFConvBertForSequenceClassification", "TFConvBertForTokenClassification", "TFConvBertLayer", "TFConvBertModel", "TFConvBertPreTrainedModel", ] if TYPE_CHECKING: from .configuration_convbert import CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ConvBertConfig, ConvBertOnnxConfig from .tokenization_convbert import ConvBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_convbert_fast import ConvBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_convbert import ( CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST, ConvBertForMaskedLM, ConvBertForMultipleChoice, ConvBertForQuestionAnswering, ConvBertForSequenceClassification, ConvBertForTokenClassification, ConvBertLayer, ConvBertModel, ConvBertPreTrainedModel, load_tf_weights_in_convbert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_convbert import ( TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFConvBertForMaskedLM, TFConvBertForMultipleChoice, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertLayer, TFConvBertModel, TFConvBertPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
1
import logging import re import pytorch_quantization import pytorch_quantization.nn as quant_nn import torch from pytorch_quantization import calib from pytorch_quantization.tensor_quant import QuantDescriptor lowerCAmelCase__ = logging.getLogger(__name__) lowerCAmelCase__ = 50 # max width of layer names lowerCAmelCase__ = 70 # max width of quantizer names def _lowerCAmelCase( __A ): UpperCAmelCase = parser.add_argument_group("quant_trainer arguments" ) group.add_argument("--wprec" , type=__A , default=8 , help="weight precision" ) group.add_argument("--aprec" , type=__A , default=8 , help="activation precision" ) group.add_argument("--quant-per-tensor" , action="store_true" , help="per tensor weight scaling" ) group.add_argument("--quant-disable" , action="store_true" , help="disable all quantizers" ) group.add_argument("--quant-disable-embeddings" , action="store_true" , help="disable all embeddings quantizers" ) group.add_argument("--quant-disable-keyword" , type=__A , nargs="+" , help="disable quantizers by keyword" ) group.add_argument("--quant-disable-layer-module" , type=__A , help="disable quantizers by keyword under layer." ) group.add_argument("--quant-enable-layer-module" , type=__A , help="enable quantizers by keyword under layer" ) group.add_argument("--calibrator" , default="max" , help="which quantization range calibrator to use" ) group.add_argument("--percentile" , default=__A , type=__A , help="percentile for PercentileCalibrator" ) group.add_argument("--fuse-qkv" , action="store_true" , help="use the same scale factor for qkv" ) group.add_argument("--clip-gelu" , metavar="N" , type=__A , help="clip gelu output maximum value to N" ) group.add_argument( "--recalibrate-weights" , action="store_true" , help=( "recalibrate weight amaxes by taking the max of the weights." " amaxes will be computed with the current quantization granularity (axis)." ) , ) def _lowerCAmelCase( __A ): if args.calibrator == "max": UpperCAmelCase = "max" elif args.calibrator == "percentile": if args.percentile is None: raise ValueError("Specify --percentile when using percentile calibrator" ) UpperCAmelCase = "histogram" elif args.calibrator == "mse": UpperCAmelCase = "histogram" else: raise ValueError(F"Invalid calibrator {args.calibrator}" ) UpperCAmelCase = QuantDescriptor(num_bits=args.aprec , calib_method=__A ) UpperCAmelCase = QuantDescriptor(num_bits=args.wprec , axis=(None if args.quant_per_tensor else (0,)) ) quant_nn.QuantLinear.set_default_quant_desc_input(__A ) quant_nn.QuantLinear.set_default_quant_desc_weight(__A ) def _lowerCAmelCase( __A , __A , __A=False , __A=False ): logger.info("Configuring Model for Quantization" ) logger.info(F"using quantization package {pytorch_quantization.__file__}" ) if not calib: if args.quant_disable_embeddings: set_quantizer_by_name(__A , ["embeddings"] , which="weight" , _disabled=__A ) if args.quant_disable: set_quantizer_by_name(__A , [""] , _disabled=__A ) if args.quant_disable_keyword: set_quantizer_by_name(__A , args.quant_disable_keyword , _disabled=__A ) if args.quant_disable_layer_module: set_quantizer_by_name(__A , [r"layer.\d+." + args.quant_disable_layer_module] , _disabled=__A ) if args.quant_enable_layer_module: set_quantizer_by_name(__A , [r"layer.\d+." + args.quant_enable_layer_module] , _disabled=__A ) if args.recalibrate_weights: recalibrate_weights(__A ) if args.fuse_qkv: fuse_qkv(__A , __A ) if args.clip_gelu: clip_gelu(__A , args.clip_gelu ) # if args.local_rank in [-1, 0] and not calib: print_quant_summary(__A ) def _lowerCAmelCase( __A ): logger.info("Enabling Calibration" ) for name, module in model.named_modules(): if name.endswith("_quantizer" ): if module._calibrator is not None: module.disable_quant() module.enable_calib() else: module.disable() logger.info(F"{name:80}: {module}" ) def _lowerCAmelCase( __A , __A ): logger.info("Loading calibrated amax" ) for name, module in model.named_modules(): if name.endswith("_quantizer" ): if module._calibrator is not None: if isinstance(module._calibrator , calib.MaxCalibrator ): module.load_calib_amax() else: module.load_calib_amax("percentile" , percentile=args.percentile ) module.enable_quant() module.disable_calib() else: module.enable() model.cuda() print_quant_summary(__A ) def _lowerCAmelCase( __A , __A ): def fusea(__A , __A , __A ): for mod in [qq, qk, qv]: if not hasattr(__A , "_amax" ): print(" WARNING: NO AMAX BUFFER" ) return UpperCAmelCase = qq._amax.detach().item() UpperCAmelCase = qk._amax.detach().item() UpperCAmelCase = qv._amax.detach().item() UpperCAmelCase = max(__A , __A , __A ) qq._amax.fill_(__A ) qk._amax.fill_(__A ) qv._amax.fill_(__A ) logger.info(F" q={q:5.2f} k={k:5.2f} v={v:5.2f} -> {amax:5.2f}" ) for name, mod in model.named_modules(): if name.endswith(".attention.self" ): logger.info(F"FUSE_QKV: {name:{name_width}}" ) fusea(mod.matmul_q_input_quantizer , mod.matmul_k_input_quantizer , mod.matmul_v_input_quantizer ) if args.quant_per_tensor: fusea(mod.query._weight_quantizer , mod.key._weight_quantizer , mod.value._weight_quantizer ) def _lowerCAmelCase( __A , __A ): for name, mod in model.named_modules(): if name.endswith(".output.dense" ) and not name.endswith("attention.output.dense" ): UpperCAmelCase = mod._input_quantizer._amax.data.detach().item() mod._input_quantizer._amax.data.detach().clamp_(max=__A ) UpperCAmelCase = mod._input_quantizer._amax.data.detach().item() logger.info(F"CLIP_GELU: {name:{name_width}} amax: {amax_init:5.2f} -> {amax:5.2f}" ) def _lowerCAmelCase( __A ): for name, mod in model.named_modules(): if hasattr(__A , "_weight_quantizer" ) and mod._weight_quantizer.axis is not None: UpperCAmelCase = mod.weight.shape[0] UpperCAmelCase = mod._weight_quantizer._amax.detach() UpperCAmelCase = torch.ones(__A , dtype=amax.dtype , device=amax.device ) * amax print(F"expanding {name} {amax} -> {mod._weight_quantizer._amax}" ) def _lowerCAmelCase( __A ): for name, mod in model.named_modules(): if hasattr(__A , "_weight_quantizer" ): if not hasattr(mod.weight_quantizer , "_amax" ): print("RECALIB: {name:{name_width}} WARNING: NO AMAX BUFFER" ) continue # determine which axes to reduce across # e.g. a 4D tensor quantized per axis 0 should reduce over (1,2,3) UpperCAmelCase = set() if mod._weight_quantizer.axis is None else set(mod._weight_quantizer.axis ) UpperCAmelCase = set(range(len(mod.weight.size() ) ) ) - axis_set UpperCAmelCase = pytorch_quantization.utils.reduce_amax(mod.weight , axis=__A , keepdims=__A ).detach() logger.info(F"RECALIB: {name:{name_width}} {mod._weight_quantizer._amax.flatten()} -> {amax.flatten()}" ) UpperCAmelCase = amax def _lowerCAmelCase( __A , __A=25 , __A=180 , __A=None ): if ignore is None: UpperCAmelCase = [] elif not isinstance(__A , __A ): UpperCAmelCase = [ignore] UpperCAmelCase = 0 for name, mod in model.named_modules(): if not hasattr(__A , "weight" ): continue UpperCAmelCase = max(__A , len(__A ) ) for name, mod in model.named_modules(): UpperCAmelCase = getattr(__A , "_input_quantizer" , __A ) UpperCAmelCase = getattr(__A , "_weight_quantizer" , __A ) if not hasattr(__A , "weight" ): continue if type(__A ) in ignore: continue if [True for s in ignore if type(__A ) is str and s in name]: continue UpperCAmelCase = F"Act:{input_q.extra_repr()}" UpperCAmelCase = F"Wgt:{weight_q.extra_repr()}" UpperCAmelCase = F"{name:{name_width}} {act_str} {wgt_str}" if len(__A ) <= line_width: logger.info(__A ) else: logger.info(F"{name:{name_width}} {act_str}" ) logger.info(F"{' ':{name_width}} {wgt_str}" ) def _lowerCAmelCase( __A ): UpperCAmelCase = 0 for name, mod in model.named_modules(): if isinstance(__A , pytorch_quantization.nn.TensorQuantizer ): print(F"{name:80} {mod}" ) count += 1 print(F"{count} TensorQuantizers found in model" ) def _lowerCAmelCase( __A , __A , __A , __A , __A ): UpperCAmelCase = getattr(__A , __A , __A ) if quantizer_mod is not None: assert hasattr(__A , __A ) setattr(__A , __A , __A ) else: logger.warning(F"{name} has no {quantizer}" ) def _lowerCAmelCase( __A , __A , __A="both" , **__A ): UpperCAmelCase = F"Warning: changing {which} quantizers of {name:{qname_width}}" for k, v in kwargs.items(): s += F" {k}={v}" if which in ["input", "both"]: set_quantizer(__A , __A , "_input_quantizer" , __A , __A ) if which in ["weight", "both"]: set_quantizer(__A , __A , "_weight_quantizer" , __A , __A ) logger.info(__A ) def _lowerCAmelCase( __A , __A , **__A ): for name, mod in model.named_modules(): if hasattr(__A , "_input_quantizer" ) or hasattr(__A , "_weight_quantizer" ): for n in names: if re.search(__A , __A ): set_quantizers(__A , __A , **__A ) elif name.endswith("_quantizer" ): for n in names: if re.search(__A , __A ): UpperCAmelCase = F"Warning: changing {name:{name_width}}" for k, v in kwargs.items(): s += F" {k}={v}" setattr(__A , __A , __A ) logger.info(__A )
1
1
import math def _lowerCAmelCase( __A , __A ): return math.pow(__A , 2 ) - a def _lowerCAmelCase( __A ): return 2 * x def _lowerCAmelCase( __A ): UpperCAmelCase = 2.0 while start <= a: UpperCAmelCase = math.pow(__A , 2 ) return start def _lowerCAmelCase( __A , __A = 9999 , __A = 0.00000000000001 ): if a < 0: raise ValueError("math domain error" ) UpperCAmelCase = get_initial_point(__A ) for _ in range(__A ): UpperCAmelCase = value UpperCAmelCase = value - fx(__A , __A ) / fx_derivative(__A ) if abs(prev_value - value ) < tolerance: return value return value if __name__ == "__main__": from doctest import testmod testmod()
1
def _lowerCAmelCase( __A ): assert column_title.isupper() UpperCAmelCase = 0 UpperCAmelCase = len(__A ) - 1 UpperCAmelCase = 0 while index >= 0: UpperCAmelCase = (ord(column_title[index] ) - 64) * pow(26 , __A ) answer += value power += 1 index -= 1 return answer if __name__ == "__main__": from doctest import testmod testmod()
1
1
import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test sys.path.append(str(Path(__file__).parent.parent / "utils")) from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 lowerCAmelCase__ = get_tests_dir("fixtures") class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : Tuple ) -> Union[str, Any]: # A mock response for an HTTP head request to emulate server down UpperCAmelCase = mock.Mock() UpperCAmelCase = 5_0_0 UpperCAmelCase = {} UpperCAmelCase = HTTPError UpperCAmelCase = {} # Download this model to make sure it's in the cache. UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2" ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch("requests.Session.request" , return_value=lowerCAmelCase__ ) as mock_head: UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2" ) # This check we did call the fake head request mock_head.assert_called() def _UpperCamelCase ( self : List[Any] ) -> Dict: # This test is for deprecated behavior and can be removed in v5 UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained( "https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json" ) @is_staging_test class __magic_name__ ( unittest.TestCase ): @classmethod def _UpperCamelCase ( cls : List[str] ) -> List[Any]: UpperCAmelCase = TOKEN HfFolder.save_token(lowerCAmelCase__ ) @classmethod def _UpperCamelCase ( cls : Optional[int] ) -> Union[str, Any]: try: delete_repo(token=cls._token , repo_id="test-feature-extractor" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="valid_org/test-feature-extractor-org" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="test-dynamic-feature-extractor" ) except HTTPError: pass def _UpperCamelCase ( self : Any ) -> Any: UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(lowerCAmelCase__ ) feature_extractor.push_to_hub("test-feature-extractor" , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) # Reset repo delete_repo(token=self._token , repo_id="test-feature-extractor" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( lowerCAmelCase__ , repo_id="test-feature-extractor" , push_to_hub=lowerCAmelCase__ , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) def _UpperCamelCase ( self : List[Any] ) -> Tuple: UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(lowerCAmelCase__ ) feature_extractor.push_to_hub("valid_org/test-feature-extractor" , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("valid_org/test-feature-extractor" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) # Reset repo delete_repo(token=self._token , repo_id="valid_org/test-feature-extractor" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( lowerCAmelCase__ , repo_id="valid_org/test-feature-extractor-org" , push_to_hub=lowerCAmelCase__ , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("valid_org/test-feature-extractor-org" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) def _UpperCamelCase ( self : Dict ) -> List[str]: CustomFeatureExtractor.register_for_auto_class() UpperCAmelCase = CustomFeatureExtractor.from_pretrained(lowerCAmelCase__ ) feature_extractor.push_to_hub("test-dynamic-feature-extractor" , use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual( feature_extractor.auto_map , {"AutoFeatureExtractor": "custom_feature_extraction.CustomFeatureExtractor"} , ) UpperCAmelCase = AutoFeatureExtractor.from_pretrained( f"{USER}/test-dynamic-feature-extractor" , trust_remote_code=lowerCAmelCase__ ) # Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module self.assertEqual(new_feature_extractor.__class__.__name__ , "CustomFeatureExtractor" )
1
import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test sys.path.append(str(Path(__file__).parent.parent / "utils")) from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 lowerCAmelCase__ = get_tests_dir("fixtures") class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : Tuple ) -> Union[str, Any]: # A mock response for an HTTP head request to emulate server down UpperCAmelCase = mock.Mock() UpperCAmelCase = 5_0_0 UpperCAmelCase = {} UpperCAmelCase = HTTPError UpperCAmelCase = {} # Download this model to make sure it's in the cache. UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2" ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch("requests.Session.request" , return_value=lowerCAmelCase__ ) as mock_head: UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2" ) # This check we did call the fake head request mock_head.assert_called() def _UpperCamelCase ( self : List[Any] ) -> Dict: # This test is for deprecated behavior and can be removed in v5 UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained( "https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json" ) @is_staging_test class __magic_name__ ( unittest.TestCase ): @classmethod def _UpperCamelCase ( cls : List[str] ) -> List[Any]: UpperCAmelCase = TOKEN HfFolder.save_token(lowerCAmelCase__ ) @classmethod def _UpperCamelCase ( cls : Optional[int] ) -> Union[str, Any]: try: delete_repo(token=cls._token , repo_id="test-feature-extractor" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="valid_org/test-feature-extractor-org" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="test-dynamic-feature-extractor" ) except HTTPError: pass def _UpperCamelCase ( self : Any ) -> Any: UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(lowerCAmelCase__ ) feature_extractor.push_to_hub("test-feature-extractor" , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) # Reset repo delete_repo(token=self._token , repo_id="test-feature-extractor" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( lowerCAmelCase__ , repo_id="test-feature-extractor" , push_to_hub=lowerCAmelCase__ , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) def _UpperCamelCase ( self : List[Any] ) -> Tuple: UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(lowerCAmelCase__ ) feature_extractor.push_to_hub("valid_org/test-feature-extractor" , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("valid_org/test-feature-extractor" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) # Reset repo delete_repo(token=self._token , repo_id="valid_org/test-feature-extractor" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( lowerCAmelCase__ , repo_id="valid_org/test-feature-extractor-org" , push_to_hub=lowerCAmelCase__ , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("valid_org/test-feature-extractor-org" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) def _UpperCamelCase ( self : Dict ) -> List[str]: CustomFeatureExtractor.register_for_auto_class() UpperCAmelCase = CustomFeatureExtractor.from_pretrained(lowerCAmelCase__ ) feature_extractor.push_to_hub("test-dynamic-feature-extractor" , use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual( feature_extractor.auto_map , {"AutoFeatureExtractor": "custom_feature_extraction.CustomFeatureExtractor"} , ) UpperCAmelCase = AutoFeatureExtractor.from_pretrained( f"{USER}/test-dynamic-feature-extractor" , trust_remote_code=lowerCAmelCase__ ) # Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module self.assertEqual(new_feature_extractor.__class__.__name__ , "CustomFeatureExtractor" )
1
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase__ = { "configuration_time_series_transformer": [ "TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "TimeSeriesTransformerConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ "TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TimeSeriesTransformerForPrediction", "TimeSeriesTransformerModel", "TimeSeriesTransformerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimeSeriesTransformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimeSeriesTransformerForPrediction, TimeSeriesTransformerModel, TimeSeriesTransformerPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
1
import argparse import os import re # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_dummies.py lowerCAmelCase__ = "src/diffusers" # Matches is_xxx_available() lowerCAmelCase__ = re.compile(r"is\_([a-z_]*)_available\(\)") # Matches from xxx import bla lowerCAmelCase__ = re.compile(r"\s+from\s+\S*\s+import\s+([^\(\s].*)\n") lowerCAmelCase__ = "\n{0} = None\n" lowerCAmelCase__ = "\nclass {0}(metaclass=DummyObject):\n _backends = {1}\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, {1})\n\n @classmethod\n def from_config(cls, *args, **kwargs):\n requires_backends(cls, {1})\n\n @classmethod\n def from_pretrained(cls, *args, **kwargs):\n requires_backends(cls, {1})\n" lowerCAmelCase__ = "\ndef {0}(*args, **kwargs):\n requires_backends({0}, {1})\n" def _lowerCAmelCase( __A ): UpperCAmelCase = _re_backend.findall(__A ) if len(__A ) == 0: return None return "_and_".join(__A ) def _lowerCAmelCase( ): with open(os.path.join(__A , "__init__.py" ) , "r" , encoding="utf-8" , newline="\n" ) as f: UpperCAmelCase = f.readlines() # Get to the point we do the actual imports for type checking UpperCAmelCase = 0 UpperCAmelCase = {} # Go through the end of the file while line_index < len(__A ): # If the line contains is_backend_available, we grab all objects associated with the `else` block UpperCAmelCase = find_backend(lines[line_index] ) if backend is not None: while not lines[line_index].startswith("else:" ): line_index += 1 line_index += 1 UpperCAmelCase = [] # Until we unindent, add backend objects to the list while line_index < len(__A ) and len(lines[line_index] ) > 1: UpperCAmelCase = lines[line_index] UpperCAmelCase = _re_single_line_import.search(__A ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(", " ) ) elif line.startswith(" " * 8 ): objects.append(line[8:-2] ) line_index += 1 if len(__A ) > 0: UpperCAmelCase = objects else: line_index += 1 return backend_specific_objects def _lowerCAmelCase( __A , __A ): if name.isupper(): return DUMMY_CONSTANT.format(__A ) elif name.islower(): return DUMMY_FUNCTION.format(__A , __A ) else: return DUMMY_CLASS.format(__A , __A ) def _lowerCAmelCase( __A=None ): if backend_specific_objects is None: UpperCAmelCase = read_init() # For special correspondence backend to module name as used in the function requires_modulename UpperCAmelCase = {} for backend, objects in backend_specific_objects.items(): UpperCAmelCase = "[" + ", ".join(F"\"{b}\"" for b in backend.split("_and_" ) ) + "]" UpperCAmelCase = "# This file is autogenerated by the command `make fix-copies`, do not edit.\n" dummy_file += "from ..utils import DummyObject, requires_backends\n\n" dummy_file += "\n".join([create_dummy_object(__A , __A ) for o in objects] ) UpperCAmelCase = dummy_file return dummy_files def _lowerCAmelCase( __A=False ): UpperCAmelCase = create_dummy_files() # For special correspondence backend to shortcut as used in utils/dummy_xxx_objects.py UpperCAmelCase = {"torch": "pt"} # Locate actual dummy modules and read their content. UpperCAmelCase = os.path.join(__A , "utils" ) UpperCAmelCase = { backend: os.path.join(__A , F"dummy_{short_names.get(__A , __A )}_objects.py" ) for backend in dummy_files.keys() } UpperCAmelCase = {} for backend, file_path in dummy_file_paths.items(): if os.path.isfile(__A ): with open(__A , "r" , encoding="utf-8" , newline="\n" ) as f: UpperCAmelCase = f.read() else: UpperCAmelCase = "" for backend in dummy_files.keys(): if dummy_files[backend] != actual_dummies[backend]: if overwrite: print( F"Updating diffusers.utils.dummy_{short_names.get(__A , __A )}_objects.py as the main " "__init__ has new objects." ) with open(dummy_file_paths[backend] , "w" , encoding="utf-8" , newline="\n" ) as f: f.write(dummy_files[backend] ) else: raise ValueError( "The main __init__ has objects that are not present in " F"diffusers.utils.dummy_{short_names.get(__A , __A )}_objects.py. Run `make fix-copies` " "to fix this." ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.") lowerCAmelCase__ = parser.parse_args() check_dummies(args.fix_and_overwrite)
1
1
def _lowerCAmelCase( __A , __A , __A ): UpperCAmelCase = (num_of_terms / 2) * (2 * first_term + (num_of_terms - 1) * common_diff) # formula for sum of series return total def _lowerCAmelCase( ): print(sum_of_series(1 , 1 , 10 ) ) if __name__ == "__main__": import doctest doctest.testmod()
1
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { "facebook/convnextv2-tiny-1k-224": "https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json", } class __magic_name__ ( _snake_case , _snake_case ): UpperCAmelCase = """convnextv2""" def __init__( self : Optional[Any] , lowerCAmelCase__ : List[Any]=3 , lowerCAmelCase__ : str=4 , lowerCAmelCase__ : Dict=4 , lowerCAmelCase__ : Optional[Any]=None , lowerCAmelCase__ : Optional[Any]=None , lowerCAmelCase__ : str="gelu" , lowerCAmelCase__ : Optional[int]=0.02 , lowerCAmelCase__ : Dict=1e-1_2 , lowerCAmelCase__ : str=0.0 , lowerCAmelCase__ : str=2_2_4 , lowerCAmelCase__ : int=None , lowerCAmelCase__ : List[Any]=None , **lowerCAmelCase__ : List[Any] , ) -> List[Any]: super().__init__(**lowerCAmelCase__ ) UpperCAmelCase = num_channels UpperCAmelCase = patch_size UpperCAmelCase = num_stages UpperCAmelCase = [9_6, 1_9_2, 3_8_4, 7_6_8] if hidden_sizes is None else hidden_sizes UpperCAmelCase = [3, 3, 9, 3] if depths is None else depths UpperCAmelCase = hidden_act UpperCAmelCase = initializer_range UpperCAmelCase = layer_norm_eps UpperCAmelCase = drop_path_rate UpperCAmelCase = image_size UpperCAmelCase = ["stem"] + [f"stage{idx}" for idx in range(1 , len(self.depths ) + 1 )] UpperCAmelCase , UpperCAmelCase = get_aligned_output_features_output_indices( out_features=lowerCAmelCase__ , out_indices=lowerCAmelCase__ , stage_names=self.stage_names )
1
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available, ) lowerCAmelCase__ = { "configuration_perceiver": ["PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP", "PerceiverConfig", "PerceiverOnnxConfig"], "tokenization_perceiver": ["PerceiverTokenizer"], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ["PerceiverFeatureExtractor"] lowerCAmelCase__ = ["PerceiverImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ "PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST", "PerceiverForImageClassificationConvProcessing", "PerceiverForImageClassificationFourier", "PerceiverForImageClassificationLearned", "PerceiverForMaskedLM", "PerceiverForMultimodalAutoencoding", "PerceiverForOpticalFlow", "PerceiverForSequenceClassification", "PerceiverLayer", "PerceiverModel", "PerceiverPreTrainedModel", ] if TYPE_CHECKING: from .configuration_perceiver import PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP, PerceiverConfig, PerceiverOnnxConfig from .tokenization_perceiver import PerceiverTokenizer try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_perceiver import PerceiverFeatureExtractor from .image_processing_perceiver import PerceiverImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_perceiver import ( PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST, PerceiverForImageClassificationConvProcessing, PerceiverForImageClassificationFourier, PerceiverForImageClassificationLearned, PerceiverForMaskedLM, PerceiverForMultimodalAutoencoding, PerceiverForOpticalFlow, PerceiverForSequenceClassification, PerceiverLayer, PerceiverModel, PerceiverPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
1
lowerCAmelCase__ = "\n# Transformers 설치 방법\n! pip install transformers datasets\n# 마지막 릴리스 대신 소스에서 설치하려면, 위 명령을 주석으로 바꾸고 아래 명령을 해제하세요.\n# ! pip install git+https://github.com/huggingface/transformers.git\n" lowerCAmelCase__ = [{"type": "code", "content": INSTALL_CONTENT}] lowerCAmelCase__ = { "{processor_class}": "FakeProcessorClass", "{model_class}": "FakeModelClass", "{object_class}": "FakeObjectClass", }
1
1
def _lowerCAmelCase( __A ): if not isinstance(__A , __A ): raise TypeError("only integers accepted as input" ) else: UpperCAmelCase = str(abs(__A ) ) UpperCAmelCase = [list(__A ) for char in range(len(__A ) )] for index in range(len(__A ) ): num_transpositions[index].pop(__A ) return max( int("".join(list(__A ) ) ) for transposition in num_transpositions ) if __name__ == "__main__": __import__("doctest").testmod()
1
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyInpaintPipeline, 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 __magic_name__ ( _snake_case , unittest.TestCase ): UpperCAmelCase = KandinskyInpaintPipeline UpperCAmelCase = ["""prompt""", """image_embeds""", """negative_image_embeds""", """image""", """mask_image"""] UpperCAmelCase = [ """prompt""", """negative_prompt""", """image_embeds""", """negative_image_embeds""", """image""", """mask_image""", ] UpperCAmelCase = [ """generator""", """height""", """width""", """latents""", """guidance_scale""", """negative_prompt""", """num_inference_steps""", """return_dict""", """guidance_scale""", """num_images_per_prompt""", """output_type""", """return_dict""", ] UpperCAmelCase = False @property def _UpperCamelCase ( self : Union[str, Any] ) -> Tuple: return 3_2 @property def _UpperCamelCase ( self : int ) -> List[Any]: return 3_2 @property def _UpperCamelCase ( self : List[Any] ) -> List[Any]: return self.time_input_dim @property def _UpperCamelCase ( self : Tuple ) -> Tuple: return self.time_input_dim * 4 @property def _UpperCamelCase ( self : Any ) -> Optional[int]: return 1_0_0 @property def _UpperCamelCase ( self : Optional[int] ) -> Optional[int]: UpperCAmelCase = XLMRobertaTokenizerFast.from_pretrained("YiYiXu/tiny-random-mclip-base" ) return tokenizer @property def _UpperCamelCase ( self : int ) -> Dict: torch.manual_seed(0 ) UpperCAmelCase = MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=3_7 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=1_0_0_5 , ) UpperCAmelCase = MultilingualCLIP(lowerCAmelCase__ ) UpperCAmelCase = text_encoder.eval() return text_encoder @property def _UpperCamelCase ( self : Dict ) -> Optional[int]: torch.manual_seed(0 ) UpperCAmelCase = { "in_channels": 9, # 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, } UpperCAmelCase = UNetaDConditionModel(**lowerCAmelCase__ ) return model @property def _UpperCamelCase ( self : str ) -> Optional[Any]: return { "block_out_channels": [3_2, 6_4], "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": 1_2, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def _UpperCamelCase ( self : Dict ) -> List[Any]: torch.manual_seed(0 ) UpperCAmelCase = VQModel(**self.dummy_movq_kwargs ) return model def _UpperCamelCase ( self : Tuple ) -> Any: UpperCAmelCase = self.dummy_text_encoder UpperCAmelCase = self.dummy_tokenizer UpperCAmelCase = self.dummy_unet UpperCAmelCase = self.dummy_movq UpperCAmelCase = DDIMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule="linear" , beta_start=0.00_085 , beta_end=0.012 , clip_sample=lowerCAmelCase__ , set_alpha_to_one=lowerCAmelCase__ , steps_offset=1 , prediction_type="epsilon" , thresholding=lowerCAmelCase__ , ) UpperCAmelCase = { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "movq": movq, } return components def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Tuple=0 ) -> str: UpperCAmelCase = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(lowerCAmelCase__ ) ).to(lowerCAmelCase__ ) UpperCAmelCase = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(lowerCAmelCase__ ) # create init_image UpperCAmelCase = floats_tensor((1, 3, 6_4, 6_4) , rng=random.Random(lowerCAmelCase__ ) ).to(lowerCAmelCase__ ) UpperCAmelCase = image.cpu().permute(0 , 2 , 3 , 1 )[0] UpperCAmelCase = Image.fromarray(np.uinta(lowerCAmelCase__ ) ).convert("RGB" ).resize((2_5_6, 2_5_6) ) # create mask UpperCAmelCase = np.ones((6_4, 6_4) , dtype=np.floataa ) UpperCAmelCase = 0 if str(lowerCAmelCase__ ).startswith("mps" ): UpperCAmelCase = torch.manual_seed(lowerCAmelCase__ ) else: UpperCAmelCase = torch.Generator(device=lowerCAmelCase__ ).manual_seed(lowerCAmelCase__ ) UpperCAmelCase = { "prompt": "horse", "image": init_image, "mask_image": mask, "image_embeds": image_embeds, "negative_image_embeds": negative_image_embeds, "generator": generator, "height": 6_4, "width": 6_4, "num_inference_steps": 2, "guidance_scale": 4.0, "output_type": "np", } return inputs def _UpperCamelCase ( self : Dict ) -> List[str]: UpperCAmelCase = "cpu" UpperCAmelCase = self.get_dummy_components() UpperCAmelCase = self.pipeline_class(**lowerCAmelCase__ ) UpperCAmelCase = pipe.to(lowerCAmelCase__ ) pipe.set_progress_bar_config(disable=lowerCAmelCase__ ) UpperCAmelCase = pipe(**self.get_dummy_inputs(lowerCAmelCase__ ) ) UpperCAmelCase = output.images UpperCAmelCase = pipe( **self.get_dummy_inputs(lowerCAmelCase__ ) , return_dict=lowerCAmelCase__ , )[0] UpperCAmelCase = image[0, -3:, -3:, -1] UpperCAmelCase = image_from_tuple[0, -3:, -3:, -1] print(f"image.shape {image.shape}" ) assert image.shape == (1, 6_4, 6_4, 3) UpperCAmelCase = np.array( [0.8_326_919, 0.73_790_467, 0.20_918_581, 0.9_309_612, 0.5_511_791, 0.43_713_328, 0.5_513_321, 0.49_922_934, 0.59_497_786] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), f" expected_slice {expected_slice}, but got {image_slice.flatten()}" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), f" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}" def _UpperCamelCase ( self : str ) -> Tuple: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : str ) -> str: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _UpperCamelCase ( self : Tuple ) -> int: UpperCAmelCase = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/kandinsky_inpaint_cat_with_hat_fp16.npy" ) UpperCAmelCase = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png" ) UpperCAmelCase = np.ones((7_6_8, 7_6_8) , dtype=np.floataa ) UpperCAmelCase = 0 UpperCAmelCase = "a hat" UpperCAmelCase = KandinskyPriorPipeline.from_pretrained( "kandinsky-community/kandinsky-2-1-prior" , torch_dtype=torch.floataa ) pipe_prior.to(lowerCAmelCase__ ) UpperCAmelCase = KandinskyInpaintPipeline.from_pretrained( "kandinsky-community/kandinsky-2-1-inpaint" , torch_dtype=torch.floataa ) UpperCAmelCase = pipeline.to(lowerCAmelCase__ ) pipeline.set_progress_bar_config(disable=lowerCAmelCase__ ) UpperCAmelCase = torch.Generator(device="cpu" ).manual_seed(0 ) UpperCAmelCase , UpperCAmelCase = pipe_prior( lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=5 , negative_prompt="" , ).to_tuple() UpperCAmelCase = pipeline( lowerCAmelCase__ , image=lowerCAmelCase__ , mask_image=lowerCAmelCase__ , image_embeds=lowerCAmelCase__ , negative_image_embeds=lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=1_0_0 , height=7_6_8 , width=7_6_8 , output_type="np" , ) UpperCAmelCase = output.images[0] assert image.shape == (7_6_8, 7_6_8, 3) assert_mean_pixel_difference(lowerCAmelCase__ , lowerCAmelCase__ )
1
1
from typing import Any, Dict, List, Optional, Tuple, Union import torch from torch import nn from torch.utils.data import DistributedSampler, RandomSampler from transformers import PreTrainedModel, Trainer, logging from transformers.integrations import is_fairscale_available from transformers.models.fsmt.configuration_fsmt import FSMTConfig from transformers.optimization import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.trainer_pt_utils import get_tpu_sampler from transformers.training_args import ParallelMode from transformers.utils import is_torch_tpu_available if is_fairscale_available(): from fairscale.optim import OSS lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { "linear": get_linear_schedule_with_warmup, "cosine": get_cosine_schedule_with_warmup, "cosine_w_restarts": get_cosine_with_hard_restarts_schedule_with_warmup, "polynomial": get_polynomial_decay_schedule_with_warmup, "constant": get_constant_schedule, "constant_w_warmup": get_constant_schedule_with_warmup, } class __magic_name__ ( _snake_case ): def __init__( self : str , lowerCAmelCase__ : str=None , lowerCAmelCase__ : int=None , *lowerCAmelCase__ : int , **lowerCAmelCase__ : List[str] ) -> str: super().__init__(*lowerCAmelCase__ , **lowerCAmelCase__ ) if config is None: assert isinstance(self.model , lowerCAmelCase__ ), ( "If no `config` is passed the model to be trained has to be of type `PreTrainedModel`, but is" f" {self.model.__class__}" ) UpperCAmelCase = self.model.config else: UpperCAmelCase = config UpperCAmelCase = data_args UpperCAmelCase = self.config.tgt_vocab_size if isinstance(self.config , lowerCAmelCase__ ) else self.config.vocab_size if self.args.label_smoothing != 0 or (self.data_args is not None and self.data_args.ignore_pad_token_for_loss): assert self.config.pad_token_id is not None, ( "Make sure that `config.pad_token_id` is correcly defined when ignoring `pad_token` for loss" " calculation or doing label smoothing." ) if self.config.pad_token_id is None and self.config.eos_token_id is not None: logger.warning( f"The `config.pad_token_id` is `None`. Using `config.eos_token_id` = {self.config.eos_token_id} for" " padding.." ) if self.args.label_smoothing == 0: UpperCAmelCase = torch.nn.CrossEntropyLoss(ignore_index=self.config.pad_token_id ) else: # dynamically import label_smoothed_nll_loss from utils import label_smoothed_nll_loss UpperCAmelCase = label_smoothed_nll_loss def _UpperCamelCase ( self : Any , lowerCAmelCase__ : int ) -> Optional[Any]: if self.optimizer is None: UpperCAmelCase = ["bias", "LayerNorm.weight"] UpperCAmelCase = [ { "params": [p for n, p in self.model.named_parameters() if not any(nd in n for nd in no_decay )], "weight_decay": self.args.weight_decay, }, { "params": [p for n, p in self.model.named_parameters() if any(nd in n for nd in no_decay )], "weight_decay": 0.0, }, ] UpperCAmelCase = Adafactor if self.args.adafactor else AdamW if self.args.adafactor: UpperCAmelCase = Adafactor UpperCAmelCase = {"scale_parameter": False, "relative_step": False} else: UpperCAmelCase = AdamW UpperCAmelCase = { "betas": (self.args.adam_betaa, self.args.adam_betaa), "eps": self.args.adam_epsilon, } UpperCAmelCase = self.args.learning_rate if self.sharded_ddp: UpperCAmelCase = OSS( params=lowerCAmelCase__ , optim=lowerCAmelCase__ , **lowerCAmelCase__ , ) else: UpperCAmelCase = optimizer_cls(lowerCAmelCase__ , **lowerCAmelCase__ ) if self.lr_scheduler is None: UpperCAmelCase = self._get_lr_scheduler(lowerCAmelCase__ ) else: # ignoring --lr_scheduler logger.warning("scheduler is passed to `Seq2SeqTrainer`, `--lr_scheduler` arg is ignored." ) def _UpperCamelCase ( self : Dict , lowerCAmelCase__ : Any ) -> Optional[int]: UpperCAmelCase = arg_to_scheduler[self.args.lr_scheduler] if self.args.lr_scheduler == "constant": UpperCAmelCase = schedule_func(self.optimizer ) elif self.args.lr_scheduler == "constant_w_warmup": UpperCAmelCase = schedule_func(self.optimizer , num_warmup_steps=self.args.warmup_steps ) else: UpperCAmelCase = schedule_func( self.optimizer , num_warmup_steps=self.args.warmup_steps , num_training_steps=lowerCAmelCase__ ) return scheduler def _UpperCamelCase ( self : Tuple ) -> Optional[torch.utils.data.Sampler]: if isinstance(self.train_dataset , torch.utils.data.IterableDataset ): return None elif is_torch_tpu_available(): return get_tpu_sampler(self.train_dataset ) else: if self.args.sortish_sampler: self.train_dataset.make_sortish_sampler( self.args.per_device_train_batch_size , distributed=(self.args.parallel_mode == ParallelMode.DISTRIBUTED) , ) return ( RandomSampler(self.train_dataset ) if self.args.local_rank == -1 else DistributedSampler(self.train_dataset ) ) def _UpperCamelCase ( self : str , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : int ) -> List[Any]: if self.args.label_smoothing == 0: if self.data_args is not None and self.data_args.ignore_pad_token_for_loss: # force training to ignore pad token UpperCAmelCase = model(**lowerCAmelCase__ , use_cache=lowerCAmelCase__ )[0] UpperCAmelCase = self.loss_fn(logits.view(-1 , logits.shape[-1] ) , labels.view(-1 ) ) else: # compute usual loss via models UpperCAmelCase , UpperCAmelCase = model(**lowerCAmelCase__ , labels=lowerCAmelCase__ , use_cache=lowerCAmelCase__ )[:2] else: # compute label smoothed loss UpperCAmelCase = model(**lowerCAmelCase__ , use_cache=lowerCAmelCase__ )[0] UpperCAmelCase = torch.nn.functional.log_softmax(lowerCAmelCase__ , dim=-1 ) UpperCAmelCase , UpperCAmelCase = self.loss_fn(lowerCAmelCase__ , lowerCAmelCase__ , self.args.label_smoothing , ignore_index=self.config.pad_token_id ) return loss, logits def _UpperCamelCase ( self : Tuple , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Union[str, Any] ) -> List[Any]: UpperCAmelCase = inputs.pop("labels" ) UpperCAmelCase , UpperCAmelCase = self._compute_loss(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) return loss def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : nn.Module , lowerCAmelCase__ : Dict[str, Union[torch.Tensor, Any]] , lowerCAmelCase__ : bool , lowerCAmelCase__ : Optional[List[str]] = None , ) -> Tuple[Optional[float], Optional[torch.Tensor], Optional[torch.Tensor]]: UpperCAmelCase = self._prepare_inputs(lowerCAmelCase__ ) UpperCAmelCase = { "max_length": self.data_args.val_max_target_length if self.data_args is not None else self.config.max_length, "num_beams": self.data_args.eval_beams if self.data_args is not None else self.config.num_beams, } if self.args.predict_with_generate and not self.args.prediction_loss_only: UpperCAmelCase = self.model.generate( inputs["input_ids"] , attention_mask=inputs["attention_mask"] , **lowerCAmelCase__ , ) # in case the batch is shorter than max length, the output should be padded if generated_tokens.shape[-1] < gen_kwargs["max_length"]: UpperCAmelCase = self._pad_tensors_to_max_len(lowerCAmelCase__ , gen_kwargs["max_length"] ) UpperCAmelCase = inputs.pop("labels" ) with torch.no_grad(): # compute loss on predict data UpperCAmelCase , UpperCAmelCase = self._compute_loss(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase = loss.mean().detach() if self.args.prediction_loss_only: return (loss, None, None) UpperCAmelCase = generated_tokens if self.args.predict_with_generate else logits if labels.shape[-1] < gen_kwargs["max_length"]: UpperCAmelCase = self._pad_tensors_to_max_len(lowerCAmelCase__ , gen_kwargs["max_length"] ) return (loss, logits, labels) def _UpperCamelCase ( self : int , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Union[str, Any] ) -> Dict: # If PAD token is not defined at least EOS token has to be defined UpperCAmelCase = self.config.pad_token_id if self.config.pad_token_id is not None else self.config.eos_token_id if pad_token_id is None: raise ValueError( "Make sure that either `config.pad_token_id` or `config.eos_token_id` is defined if tensor has to be" f" padded to `max_length`={max_length}" ) UpperCAmelCase = pad_token_id * torch.ones( (tensor.shape[0], max_length) , dtype=tensor.dtype , device=tensor.device ) UpperCAmelCase = tensor return padded_tensor
1
def _lowerCAmelCase( __A , __A ): return (pointa[0] - pointa[0]) ** 2 + (pointa[1] - pointa[1]) ** 2 def _lowerCAmelCase( __A , __A=0 ): return sorted(__A , key=lambda __A : x[column] ) def _lowerCAmelCase( __A , __A , __A=float("inf" ) ): for i in range(points_counts - 1 ): for j in range(i + 1 , __A ): UpperCAmelCase = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: UpperCAmelCase = current_dis return min_dis def _lowerCAmelCase( __A , __A , __A=float("inf" ) ): for i in range(min(6 , points_counts - 1 ) , __A ): for j in range(max(0 , i - 6 ) , __A ): UpperCAmelCase = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: UpperCAmelCase = current_dis return min_dis def _lowerCAmelCase( __A , __A , __A ): # base case if points_counts <= 3: return dis_between_closest_pair(__A , __A ) # recursion UpperCAmelCase = points_counts // 2 UpperCAmelCase = closest_pair_of_points_sqr( __A , points_sorted_on_y[:mid] , __A ) UpperCAmelCase = closest_pair_of_points_sqr( __A , points_sorted_on_y[mid:] , points_counts - mid ) UpperCAmelCase = min(__A , __A ) UpperCAmelCase = [] for point in points_sorted_on_x: if abs(point[0] - points_sorted_on_x[mid][0] ) < closest_pair_dis: cross_strip.append(__A ) UpperCAmelCase = dis_between_closest_in_strip( __A , len(__A ) , __A ) return min(__A , __A ) def _lowerCAmelCase( __A , __A ): UpperCAmelCase = column_based_sort(__A , column=0 ) UpperCAmelCase = column_based_sort(__A , column=1 ) return ( closest_pair_of_points_sqr( __A , __A , __A ) ) ** 0.5 if __name__ == "__main__": lowerCAmelCase__ = [(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)] print("Distance:", closest_pair_of_points(points, len(points)))
1
1
import argparse import os import re import numpy as np import PIL import torch from timm import create_model from torch.optim.lr_scheduler import OneCycleLR from torch.utils.data import DataLoader, Dataset from torchvision.transforms import Compose, RandomResizedCrop, Resize, ToTensor from accelerate import Accelerator def _lowerCAmelCase( __A ): UpperCAmelCase = fname.split(os.path.sep )[-1] return re.search(r"^(.*)_\d+\.jpg$" , __A ).groups()[0] class __magic_name__ ( _snake_case ): def __init__( self : Any , lowerCAmelCase__ : int , lowerCAmelCase__ : Union[str, Any]=None , lowerCAmelCase__ : int=None ) -> Optional[Any]: UpperCAmelCase = file_names UpperCAmelCase = image_transform UpperCAmelCase = label_to_id def __len__( self : Tuple ) -> List[str]: return len(self.file_names ) def __getitem__( self : Optional[int] , lowerCAmelCase__ : Tuple ) -> Dict: UpperCAmelCase = self.file_names[idx] UpperCAmelCase = PIL.Image.open(lowerCAmelCase__ ) UpperCAmelCase = raw_image.convert("RGB" ) if self.image_transform is not None: UpperCAmelCase = self.image_transform(lowerCAmelCase__ ) UpperCAmelCase = extract_label(lowerCAmelCase__ ) if self.label_to_id is not None: UpperCAmelCase = self.label_to_id[label] return {"image": image, "label": label} def _lowerCAmelCase( __A , __A ): # Initialize accelerator if args.with_tracking: UpperCAmelCase = Accelerator( cpu=args.cpu , mixed_precision=args.mixed_precision , log_with="all" , project_dir=args.project_dir ) else: UpperCAmelCase = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs UpperCAmelCase = config["lr"] UpperCAmelCase = int(config["num_epochs"] ) UpperCAmelCase = int(config["seed"] ) UpperCAmelCase = int(config["batch_size"] ) UpperCAmelCase = config["image_size"] if not isinstance(__A , (list, tuple) ): UpperCAmelCase = (image_size, image_size) # Parse out whether we are saving every epoch or after a certain number of batches if hasattr(args.checkpointing_steps , "isdigit" ): if args.checkpointing_steps == "epoch": UpperCAmelCase = args.checkpointing_steps elif args.checkpointing_steps.isdigit(): UpperCAmelCase = int(args.checkpointing_steps ) else: raise ValueError( F"Argument `checkpointing_steps` must be either a number or `epoch`. `{args.checkpointing_steps}` passed." ) else: UpperCAmelCase = None # We need to initialize the trackers we use, and also store our configuration if args.with_tracking: UpperCAmelCase = os.path.split(__A )[-1].split("." )[0] accelerator.init_trackers(__A , __A ) # Grab all the image filenames UpperCAmelCase = [os.path.join(args.data_dir , __A ) for fname in os.listdir(args.data_dir ) if fname.endswith(".jpg" )] # Build the label correspondences UpperCAmelCase = [extract_label(__A ) for fname in file_names] UpperCAmelCase = list(set(__A ) ) id_to_label.sort() UpperCAmelCase = {lbl: i for i, lbl in enumerate(__A )} # Set the seed before splitting the data. np.random.seed(__A ) torch.manual_seed(__A ) torch.cuda.manual_seed_all(__A ) # Split our filenames between train and validation UpperCAmelCase = np.random.permutation(len(__A ) ) UpperCAmelCase = int(0.8 * len(__A ) ) UpperCAmelCase = random_perm[:cut] UpperCAmelCase = random_perm[cut:] # For training we use a simple RandomResizedCrop UpperCAmelCase = Compose([RandomResizedCrop(__A , scale=(0.5, 1.0) ), ToTensor()] ) UpperCAmelCase = PetsDataset( [file_names[i] for i in train_split] , image_transform=__A , label_to_id=__A ) # For evaluation, we use a deterministic Resize UpperCAmelCase = Compose([Resize(__A ), ToTensor()] ) UpperCAmelCase = PetsDataset([file_names[i] for i in eval_split] , image_transform=__A , label_to_id=__A ) # Instantiate dataloaders. UpperCAmelCase = DataLoader(__A , shuffle=__A , batch_size=__A , num_workers=4 ) UpperCAmelCase = DataLoader(__A , shuffle=__A , batch_size=__A , num_workers=4 ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) UpperCAmelCase = create_model("resnet50d" , pretrained=__A , num_classes=len(__A ) ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). UpperCAmelCase = model.to(accelerator.device ) # Freezing the base model for param in model.parameters(): UpperCAmelCase = False for param in model.get_classifier().parameters(): UpperCAmelCase = True # We normalize the batches of images to be a bit faster. UpperCAmelCase = torch.tensor(model.default_cfg["mean"] )[None, :, None, None].to(accelerator.device ) UpperCAmelCase = torch.tensor(model.default_cfg["std"] )[None, :, None, None].to(accelerator.device ) # Instantiate optimizer UpperCAmelCase = torch.optim.Adam(params=model.parameters() , lr=lr / 25 ) # Instantiate learning rate scheduler UpperCAmelCase = OneCycleLR(optimizer=__A , max_lr=__A , epochs=__A , steps_per_epoch=len(__A ) ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = accelerator.prepare( __A , __A , __A , __A , __A ) # We need to keep track of how many total steps we have iterated over UpperCAmelCase = 0 # We also need to keep track of the starting epoch so files are named properly UpperCAmelCase = 0 # Potentially load in the weights and states from a previous save if args.resume_from_checkpoint: if args.resume_from_checkpoint is not None or args.resume_from_checkpoint != "": accelerator.print(F"Resumed from checkpoint: {args.resume_from_checkpoint}" ) accelerator.load_state(args.resume_from_checkpoint ) UpperCAmelCase = os.path.basename(args.resume_from_checkpoint ) else: # Get the most recent checkpoint UpperCAmelCase = [f.name for f in os.scandir(os.getcwd() ) if f.is_dir()] dirs.sort(key=os.path.getctime ) UpperCAmelCase = dirs[-1] # Sorts folders by date modified, most recent checkpoint is the last # Extract `epoch_{i}` or `step_{i}` UpperCAmelCase = os.path.splitext(__A )[0] if "epoch" in training_difference: UpperCAmelCase = int(training_difference.replace("epoch_" , "" ) ) + 1 UpperCAmelCase = None else: UpperCAmelCase = int(training_difference.replace("step_" , "" ) ) UpperCAmelCase = resume_step // len(__A ) resume_step -= starting_epoch * len(__A ) # Now we train the model for epoch in range(__A , __A ): model.train() if args.with_tracking: UpperCAmelCase = 0 if args.resume_from_checkpoint and epoch == starting_epoch and resume_step is not None: # We need to skip steps until we reach the resumed step UpperCAmelCase = accelerator.skip_first_batches(__A , __A ) overall_step += resume_step else: # After the first iteration though, we need to go back to the original dataloader UpperCAmelCase = train_dataloader for batch in active_dataloader: # We could avoid this line since we set the accelerator with `device_placement=True`. UpperCAmelCase = {k: v.to(accelerator.device ) for k, v in batch.items()} UpperCAmelCase = (batch["image"] - mean) / std UpperCAmelCase = model(__A ) UpperCAmelCase = torch.nn.functional.cross_entropy(__A , batch["label"] ) # We keep track of the loss at each epoch if args.with_tracking: total_loss += loss.detach().float() accelerator.backward(__A ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 if isinstance(__A , __A ): UpperCAmelCase = F"step_{overall_step}" if overall_step % checkpointing_steps == 0: if args.output_dir is not None: UpperCAmelCase = os.path.join(args.output_dir , __A ) accelerator.save_state(__A ) model.eval() UpperCAmelCase = 0 UpperCAmelCase = 0 for step, batch in enumerate(__A ): # We could avoid this line since we set the accelerator with `device_placement=True`. UpperCAmelCase = {k: v.to(accelerator.device ) for k, v in batch.items()} UpperCAmelCase = (batch["image"] - mean) / std with torch.no_grad(): UpperCAmelCase = model(__A ) UpperCAmelCase = outputs.argmax(dim=-1 ) UpperCAmelCase , UpperCAmelCase = accelerator.gather_for_metrics((predictions, batch["label"]) ) UpperCAmelCase = predictions == references num_elems += accurate_preds.shape[0] accurate += accurate_preds.long().sum() UpperCAmelCase = accurate.item() / num_elems # Use accelerator.print to print only on the main process. accelerator.print(F"epoch {epoch}: {100 * eval_metric:.2f}" ) if args.with_tracking: accelerator.log( { "accuracy": 100 * eval_metric, "train_loss": total_loss.item() / len(__A ), "epoch": epoch, } , step=__A , ) if checkpointing_steps == "epoch": UpperCAmelCase = F"epoch_{epoch}" if args.output_dir is not None: UpperCAmelCase = os.path.join(args.output_dir , __A ) accelerator.save_state(__A ) if args.with_tracking: accelerator.end_training() def _lowerCAmelCase( ): UpperCAmelCase = argparse.ArgumentParser(description="Simple example of training script." ) parser.add_argument("--data_dir" , required=__A , help="The data folder on disk." ) parser.add_argument("--fp16" , action="store_true" , help="If passed, will use FP16 training." ) parser.add_argument( "--mixed_precision" , type=__A , default=__A , choices=["no", "fp16", "bf16", "fp8"] , help="Whether to use mixed precision. Choose" "between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10." "and an Nvidia Ampere GPU." , ) parser.add_argument("--cpu" , action="store_true" , help="If passed, will train on the CPU." ) parser.add_argument( "--checkpointing_steps" , type=__A , default=__A , help="Whether the various states should be saved at the end of every n steps, or 'epoch' for each epoch." , ) parser.add_argument( "--output_dir" , type=__A , default="." , help="Optional save directory where all checkpoint folders will be stored. Default is the current working directory." , ) parser.add_argument( "--resume_from_checkpoint" , type=__A , default=__A , help="If the training should continue from a checkpoint folder." , ) parser.add_argument( "--with_tracking" , action="store_true" , help="Whether to load in all available experiment trackers from the environment and use them for logging." , ) parser.add_argument( "--project_dir" , type=__A , default="logs" , help="Location on where to store experiment tracking logs` and relevent project information" , ) UpperCAmelCase = parser.parse_args() UpperCAmelCase = {"lr": 3E-2, "num_epochs": 3, "seed": 42, "batch_size": 64, "image_size": 224} training_function(__A , __A ) if __name__ == "__main__": main()
1
import copy import os import cva import numpy as np from matplotlib import pyplot as plt class __magic_name__ : def __init__( self : Optional[int] ) -> Optional[Any]: UpperCAmelCase = "" UpperCAmelCase = "" UpperCAmelCase = [] UpperCAmelCase = 0 UpperCAmelCase = 2_5_6 UpperCAmelCase = 0 UpperCAmelCase = 0 UpperCAmelCase = 0 UpperCAmelCase = 0 def _UpperCamelCase ( self : Any , lowerCAmelCase__ : Optional[Any] ) -> List[str]: UpperCAmelCase = cva.imread(lowerCAmelCase__ , 0 ) UpperCAmelCase = copy.deepcopy(self.img ) UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = plt.hist(self.img.ravel() , 2_5_6 , [0, 2_5_6] , label="x" ) UpperCAmelCase = np.sum(lowerCAmelCase__ ) for i in range(len(lowerCAmelCase__ ) ): UpperCAmelCase = x[i] / self.k self.sk += prk UpperCAmelCase = (self.L - 1) * self.sk if self.rem != 0: UpperCAmelCase = int(last % last ) UpperCAmelCase = int(last + 1 if self.rem >= 0.5 else last ) self.last_list.append(lowerCAmelCase__ ) UpperCAmelCase = int(np.ma.count(self.img ) / self.img[1].size ) UpperCAmelCase = self.img[1].size for i in range(self.number_of_cols ): for j in range(self.number_of_rows ): UpperCAmelCase = self.img[j][i] if num != self.last_list[num]: UpperCAmelCase = self.last_list[num] cva.imwrite("output_data/output.jpg" , self.img ) def _UpperCamelCase ( self : str ) -> int: plt.hist(self.img.ravel() , 2_5_6 , [0, 2_5_6] ) def _UpperCamelCase ( self : Dict ) -> Optional[Any]: cva.imshow("Output-Image" , self.img ) cva.imshow("Input-Image" , self.original_image ) cva.waitKey(5_0_0_0 ) cva.destroyAllWindows() if __name__ == "__main__": lowerCAmelCase__ = os.path.join(os.path.basename(__file__), "image_data/input.jpg") lowerCAmelCase__ = ConstantStretch() stretcher.stretch(file_path) stretcher.plot_histogram() stretcher.show_image()
1
1
def _lowerCAmelCase( __A , __A ): while b: UpperCAmelCase , UpperCAmelCase = b, a % b return a def _lowerCAmelCase( __A , __A ): return a if b == 0 else euclidean_gcd_recursive(__A , a % b ) def _lowerCAmelCase( ): print(F"euclidean_gcd(3, 5) = {euclidean_gcd(3 , 5 )}" ) print(F"euclidean_gcd(5, 3) = {euclidean_gcd(5 , 3 )}" ) print(F"euclidean_gcd(1, 3) = {euclidean_gcd(1 , 3 )}" ) print(F"euclidean_gcd(3, 6) = {euclidean_gcd(3 , 6 )}" ) print(F"euclidean_gcd(6, 3) = {euclidean_gcd(6 , 3 )}" ) print(F"euclidean_gcd_recursive(3, 5) = {euclidean_gcd_recursive(3 , 5 )}" ) print(F"euclidean_gcd_recursive(5, 3) = {euclidean_gcd_recursive(5 , 3 )}" ) print(F"euclidean_gcd_recursive(1, 3) = {euclidean_gcd_recursive(1 , 3 )}" ) print(F"euclidean_gcd_recursive(3, 6) = {euclidean_gcd_recursive(3 , 6 )}" ) print(F"euclidean_gcd_recursive(6, 3) = {euclidean_gcd_recursive(6 , 3 )}" ) if __name__ == "__main__": main()
1
import json import os import unittest from transformers import BatchEncoding, LEDTokenizer, LEDTokenizerFast from transformers.models.led.tokenization_led import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, require_torch from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __magic_name__ ( _snake_case , unittest.TestCase ): UpperCAmelCase = LEDTokenizer UpperCAmelCase = LEDTokenizerFast UpperCAmelCase = True def _UpperCamelCase ( self : Tuple ) -> Union[str, Any]: super().setUp() UpperCAmelCase = [ "l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "\u0120", "\u0120l", "\u0120n", "\u0120lo", "\u0120low", "er", "\u0120lowest", "\u0120newer", "\u0120wider", "<unk>", ] UpperCAmelCase = dict(zip(lowerCAmelCase__ , range(len(lowerCAmelCase__ ) ) ) ) UpperCAmelCase = ["#version: 0.2", "\u0120 l", "\u0120l o", "\u0120lo w", "e r", ""] UpperCAmelCase = {"unk_token": "<unk>"} UpperCAmelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) UpperCAmelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as fp: fp.write(json.dumps(lowerCAmelCase__ ) + "\n" ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(lowerCAmelCase__ ) ) def _UpperCamelCase ( self : Union[str, Any] , **lowerCAmelCase__ : Optional[int] ) -> Optional[int]: kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **lowerCAmelCase__ ) def _UpperCamelCase ( self : str , **lowerCAmelCase__ : str ) -> Optional[int]: kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : List[Any] ) -> List[Any]: return "lower newer", "lower newer" @cached_property def _UpperCamelCase ( self : Dict ) -> str: return LEDTokenizer.from_pretrained("allenai/led-base-16384" ) @cached_property def _UpperCamelCase ( self : int ) -> Tuple: return LEDTokenizerFast.from_pretrained("allenai/led-base-16384" ) @require_torch def _UpperCamelCase ( self : Tuple ) -> List[str]: UpperCAmelCase = ["A long paragraph for summarization.", "Another paragraph for summarization."] UpperCAmelCase = [0, 2_5_0, 2_5_1, 1_7_8_1_8, 1_3, 3_9_1_8_6, 1_9_3_8, 4, 2] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer(lowerCAmelCase__ , max_length=len(lowerCAmelCase__ ) , padding=lowerCAmelCase__ , return_tensors="pt" ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual((2, 9) , batch.input_ids.shape ) self.assertEqual((2, 9) , batch.attention_mask.shape ) UpperCAmelCase = batch.input_ids.tolist()[0] self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ ) @require_torch def _UpperCamelCase ( self : Union[str, Any] ) -> List[Any]: UpperCAmelCase = ["A long paragraph for summarization.", "Another paragraph for summarization."] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer(lowerCAmelCase__ , padding=lowerCAmelCase__ , return_tensors="pt" ) self.assertIn("input_ids" , lowerCAmelCase__ ) self.assertIn("attention_mask" , lowerCAmelCase__ ) self.assertNotIn("labels" , lowerCAmelCase__ ) self.assertNotIn("decoder_attention_mask" , lowerCAmelCase__ ) @require_torch def _UpperCamelCase ( self : int ) -> int: UpperCAmelCase = [ "Summary of the text.", "Another summary.", ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer(text_target=lowerCAmelCase__ , max_length=3_2 , padding="max_length" , return_tensors="pt" ) self.assertEqual(3_2 , targets["input_ids"].shape[1] ) @require_torch def _UpperCamelCase ( self : Any ) -> int: for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer( ["I am a small frog" * 1_0_2_4, "I am a small frog"] , padding=lowerCAmelCase__ , truncation=lowerCAmelCase__ , return_tensors="pt" ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual(batch.input_ids.shape , (2, 5_1_2_2) ) @require_torch def _UpperCamelCase ( self : Dict ) -> Tuple: UpperCAmelCase = ["A long paragraph for summarization."] UpperCAmelCase = [ "Summary of the text.", ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer(lowerCAmelCase__ , return_tensors="pt" ) UpperCAmelCase = tokenizer(text_target=lowerCAmelCase__ , return_tensors="pt" ) UpperCAmelCase = inputs["input_ids"] UpperCAmelCase = targets["input_ids"] self.assertTrue((input_ids[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((labels[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((input_ids[:, -1] == tokenizer.eos_token_id).all().item() ) self.assertTrue((labels[:, -1] == tokenizer.eos_token_id).all().item() ) @require_torch def _UpperCamelCase ( self : Optional[int] ) -> Optional[Any]: for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = ["Summary of the text.", "Another summary."] UpperCAmelCase = [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -1, -1]] UpperCAmelCase = tokenizer(lowerCAmelCase__ , padding=lowerCAmelCase__ ) UpperCAmelCase = [[0] * len(lowerCAmelCase__ ) for x in encoded_output["input_ids"]] UpperCAmelCase = tokenizer.pad(lowerCAmelCase__ ) self.assertSequenceEqual(outputs["global_attention_mask"] , lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] ) -> int: pass def _UpperCamelCase ( self : Optional[Any] ) -> Union[str, Any]: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})" ): UpperCAmelCase = self.rust_tokenizer_class.from_pretrained(lowerCAmelCase__ , **lowerCAmelCase__ ) UpperCAmelCase = self.tokenizer_class.from_pretrained(lowerCAmelCase__ , **lowerCAmelCase__ ) UpperCAmelCase = "A, <mask> AllenNLP sentence." UpperCAmelCase = tokenizer_r.encode_plus(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ , return_token_type_ids=lowerCAmelCase__ ) UpperCAmelCase = tokenizer_p.encode_plus(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ , return_token_type_ids=lowerCAmelCase__ ) self.assertEqual(sum(tokens_r["token_type_ids"] ) , sum(tokens_p["token_type_ids"] ) ) self.assertEqual( sum(tokens_r["attention_mask"] ) / len(tokens_r["attention_mask"] ) , sum(tokens_p["attention_mask"] ) / len(tokens_p["attention_mask"] ) , ) UpperCAmelCase = tokenizer_r.convert_ids_to_tokens(tokens_r["input_ids"] ) UpperCAmelCase = tokenizer_p.convert_ids_to_tokens(tokens_p["input_ids"] ) self.assertSequenceEqual(tokens_p["input_ids"] , [0, 2_5_0, 6, 5_0_2_6_4, 3_8_2_3, 4_8_7, 2_1_9_9_2, 3_6_4_5, 4, 2] ) self.assertSequenceEqual(tokens_r["input_ids"] , [0, 2_5_0, 6, 5_0_2_6_4, 3_8_2_3, 4_8_7, 2_1_9_9_2, 3_6_4_5, 4, 2] ) self.assertSequenceEqual( lowerCAmelCase__ , ["<s>", "A", ",", "<mask>", "ĠAllen", "N", "LP", "Ġsentence", ".", "</s>"] ) self.assertSequenceEqual( lowerCAmelCase__ , ["<s>", "A", ",", "<mask>", "ĠAllen", "N", "LP", "Ġsentence", ".", "</s>"] )
1
1
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import center_crop, normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL lowerCAmelCase__ = logging.get_logger(__name__) class __magic_name__ ( _snake_case ): UpperCAmelCase = ["""pixel_values"""] def __init__( self : Dict , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : Dict[str, int] = None , lowerCAmelCase__ : PILImageResampling = PIL.Image.BICUBIC , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : Dict[str, int] = None , lowerCAmelCase__ : Union[int, float] = 1 / 2_5_5 , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : Optional[Union[float, List[float]]] = None , lowerCAmelCase__ : Optional[Union[float, List[float]]] = None , **lowerCAmelCase__ : List[str] , ) -> None: super().__init__(**lowerCAmelCase__ ) UpperCAmelCase = size if size is not None else {"height": 2_5_6, "width": 2_5_6} UpperCAmelCase = get_size_dict(lowerCAmelCase__ ) UpperCAmelCase = crop_size if crop_size is not None else {"height": 2_2_4, "width": 2_2_4} UpperCAmelCase = get_size_dict(lowerCAmelCase__ , param_name="crop_size" ) UpperCAmelCase = do_resize UpperCAmelCase = size UpperCAmelCase = resample UpperCAmelCase = do_center_crop UpperCAmelCase = crop_size UpperCAmelCase = do_rescale UpperCAmelCase = rescale_factor UpperCAmelCase = do_normalize UpperCAmelCase = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN UpperCAmelCase = image_std if image_std is not None else IMAGENET_STANDARD_STD def _UpperCamelCase ( self : Dict , lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : Dict[str, int] , lowerCAmelCase__ : PILImageResampling = PIL.Image.BICUBIC , lowerCAmelCase__ : Optional[Union[str, ChannelDimension]] = None , **lowerCAmelCase__ : Optional[int] , ) -> np.ndarray: UpperCAmelCase = get_size_dict(lowerCAmelCase__ ) if "height" not in size or "width" not in size: raise ValueError(f"The size dictionary must have keys 'height' and 'width'. Got {size.keys()}" ) return resize( lowerCAmelCase__ , size=(size["height"], size["width"]) , resample=lowerCAmelCase__ , data_format=lowerCAmelCase__ , **lowerCAmelCase__ ) def _UpperCamelCase ( self : Any , lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : Dict[str, int] , lowerCAmelCase__ : Optional[Union[str, ChannelDimension]] = None , **lowerCAmelCase__ : List[str] , ) -> np.ndarray: UpperCAmelCase = get_size_dict(lowerCAmelCase__ ) if "height" not in size or "width" not in size: raise ValueError(f"The size dictionary must have keys 'height' and 'width'. Got {size.keys()}" ) return center_crop(lowerCAmelCase__ , size=(size["height"], size["width"]) , data_format=lowerCAmelCase__ , **lowerCAmelCase__ ) def _UpperCamelCase ( self : int , lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : Union[int, float] , lowerCAmelCase__ : Optional[Union[str, ChannelDimension]] = None , **lowerCAmelCase__ : Tuple , ) -> List[str]: return rescale(lowerCAmelCase__ , scale=lowerCAmelCase__ , data_format=lowerCAmelCase__ , **lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : Union[float, List[float]] , lowerCAmelCase__ : Union[float, List[float]] , lowerCAmelCase__ : Optional[Union[str, ChannelDimension]] = None , **lowerCAmelCase__ : Union[str, Any] , ) -> np.ndarray: return normalize(lowerCAmelCase__ , mean=lowerCAmelCase__ , std=lowerCAmelCase__ , data_format=lowerCAmelCase__ , **lowerCAmelCase__ ) def _UpperCamelCase ( self : Optional[Any] , lowerCAmelCase__ : ImageInput , lowerCAmelCase__ : bool = None , lowerCAmelCase__ : Dict[str, int] = None , lowerCAmelCase__ : Tuple=None , lowerCAmelCase__ : bool = None , lowerCAmelCase__ : Dict[str, int] = None , lowerCAmelCase__ : bool = None , lowerCAmelCase__ : float = None , lowerCAmelCase__ : bool = None , lowerCAmelCase__ : Optional[Union[float, List[float]]] = None , lowerCAmelCase__ : Optional[Union[float, List[float]]] = None , lowerCAmelCase__ : Optional[Union[str, TensorType]] = None , lowerCAmelCase__ : ChannelDimension = ChannelDimension.FIRST , **lowerCAmelCase__ : Optional[Any] , ) -> PIL.Image.Image: UpperCAmelCase = do_resize if do_resize is not None else self.do_resize UpperCAmelCase = resample if resample is not None else self.resample UpperCAmelCase = do_center_crop if do_center_crop is not None else self.do_center_crop UpperCAmelCase = do_rescale if do_rescale is not None else self.do_rescale UpperCAmelCase = rescale_factor if rescale_factor is not None else self.rescale_factor UpperCAmelCase = do_normalize if do_normalize is not None else self.do_normalize UpperCAmelCase = image_mean if image_mean is not None else self.image_mean UpperCAmelCase = image_std if image_std is not None else self.image_std UpperCAmelCase = size if size is not None else self.size UpperCAmelCase = get_size_dict(lowerCAmelCase__ ) UpperCAmelCase = crop_size if crop_size is not None else self.crop_size UpperCAmelCase = get_size_dict(lowerCAmelCase__ , param_name="crop_size" ) UpperCAmelCase = make_list_of_images(lowerCAmelCase__ ) if not valid_images(lowerCAmelCase__ ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None 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." ) # All transformations expect numpy arrays. UpperCAmelCase = [to_numpy_array(lowerCAmelCase__ ) for image in images] if do_resize: UpperCAmelCase = [self.resize(image=lowerCAmelCase__ , size=lowerCAmelCase__ , resample=lowerCAmelCase__ ) for image in images] if do_center_crop: UpperCAmelCase = [self.center_crop(image=lowerCAmelCase__ , size=lowerCAmelCase__ ) for image in images] if do_rescale: UpperCAmelCase = [self.rescale(image=lowerCAmelCase__ , scale=lowerCAmelCase__ ) for image in images] if do_normalize: UpperCAmelCase = [self.normalize(image=lowerCAmelCase__ , mean=lowerCAmelCase__ , std=lowerCAmelCase__ ) for image in images] UpperCAmelCase = [to_channel_dimension_format(lowerCAmelCase__ , lowerCAmelCase__ ) for image in images] UpperCAmelCase = {"pixel_values": images} return BatchFeature(data=lowerCAmelCase__ , tensor_type=lowerCAmelCase__ )
1
import numpy as np from nltk.translate import meteor_score import datasets from datasets.config import importlib_metadata, version lowerCAmelCase__ = version.parse(importlib_metadata.version("nltk")) if NLTK_VERSION >= version.Version("3.6.4"): from nltk import word_tokenize lowerCAmelCase__ = "\\n@inproceedings{banarjee2005,\n title = {{METEOR}: An Automatic Metric for {MT} Evaluation with Improved Correlation with Human Judgments},\n author = {Banerjee, Satanjeev and Lavie, Alon},\n booktitle = {Proceedings of the {ACL} Workshop on Intrinsic and Extrinsic Evaluation Measures for Machine Translation and/or Summarization},\n month = jun,\n year = {2005},\n address = {Ann Arbor, Michigan},\n publisher = {Association for Computational Linguistics},\n url = {https://www.aclweb.org/anthology/W05-0909},\n pages = {65--72},\n}\n" lowerCAmelCase__ = "\\nMETEOR, an automatic metric for machine translation evaluation\nthat is based on a generalized concept of unigram matching between the\nmachine-produced translation and human-produced reference translations.\nUnigrams can be matched based on their surface forms, stemmed forms,\nand meanings; furthermore, METEOR can be easily extended to include more\nadvanced matching strategies. Once all generalized unigram matches\nbetween the two strings have been found, METEOR computes a score for\nthis matching using a combination of unigram-precision, unigram-recall, and\na measure of fragmentation that is designed to directly capture how\nwell-ordered the matched words in the machine translation are in relation\nto the reference.\n\nMETEOR gets an R correlation value of 0.347 with human evaluation on the Arabic\ndata and 0.331 on the Chinese data. This is shown to be an improvement on\nusing simply unigram-precision, unigram-recall and their harmonic F1\ncombination.\n" lowerCAmelCase__ = "\nComputes METEOR score of translated segments against one or more references.\nArgs:\n predictions: list of predictions to score. Each prediction\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\n alpha: Parameter for controlling relative weights of precision and recall. default: 0.9\n beta: Parameter for controlling shape of penalty as a function of fragmentation. default: 3\n gamma: Relative weight assigned to fragmentation penalty. default: 0.5\nReturns:\n 'meteor': meteor score.\nExamples:\n\n >>> meteor = datasets.load_metric('meteor')\n >>> predictions = [\"It is a guide to action which ensures that the military always obeys the commands of the party\"]\n >>> references = [\"It is a guide to action that ensures that the military will forever heed Party commands\"]\n >>> results = meteor.compute(predictions=predictions, references=references)\n >>> print(round(results[\"meteor\"], 4))\n 0.6944\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): def _UpperCamelCase ( self : int ) -> str: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Value("string" , id="sequence" ), } ) , codebase_urls=["https://github.com/nltk/nltk/blob/develop/nltk/translate/meteor_score.py"] , reference_urls=[ "https://www.nltk.org/api/nltk.translate.html#module-nltk.translate.meteor_score", "https://en.wikipedia.org/wiki/METEOR", ] , ) def _UpperCamelCase ( self : Dict , lowerCAmelCase__ : List[Any] ) -> Dict: import nltk nltk.download("wordnet" ) if NLTK_VERSION >= version.Version("3.6.5" ): nltk.download("punkt" ) if NLTK_VERSION >= version.Version("3.6.6" ): nltk.download("omw-1.4" ) def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Any=0.9 , lowerCAmelCase__ : Tuple=3 , lowerCAmelCase__ : Optional[int]=0.5 ) -> Any: if NLTK_VERSION >= version.Version("3.6.5" ): UpperCAmelCase = [ meteor_score.single_meteor_score( word_tokenize(lowerCAmelCase__ ) , word_tokenize(lowerCAmelCase__ ) , alpha=lowerCAmelCase__ , beta=lowerCAmelCase__ , gamma=lowerCAmelCase__ ) for ref, pred in zip(lowerCAmelCase__ , lowerCAmelCase__ ) ] else: UpperCAmelCase = [ meteor_score.single_meteor_score(lowerCAmelCase__ , lowerCAmelCase__ , alpha=lowerCAmelCase__ , beta=lowerCAmelCase__ , gamma=lowerCAmelCase__ ) for ref, pred in zip(lowerCAmelCase__ , lowerCAmelCase__ ) ] return {"meteor": np.mean(lowerCAmelCase__ )}
1
1
from random import randint, random def _lowerCAmelCase( __A , __A , __A , __A = False , __A = False , __A = 5 , ): UpperCAmelCase = [[-1] * number_of_cells] # Create a highway without any car UpperCAmelCase = 0 UpperCAmelCase = max(__A , 0 ) while i < number_of_cells: UpperCAmelCase = ( randint(0 , __A ) if random_speed else initial_speed ) # Place the cars i += ( randint(1 , max_speed * 2 ) if random_frequency else frequency ) # Arbitrary number, may need tuning return highway def _lowerCAmelCase( __A , __A ): UpperCAmelCase = 0 UpperCAmelCase = highway_now[car_index + 1 :] for cell in range(len(__A ) ): # May need a better name for this if cells[cell] != -1: # If the cell is not empty then return distance # we have the distance we wanted distance += 1 # Here if the car is near the end of the highway return distance + get_distance(__A , -1 ) def _lowerCAmelCase( __A , __A , __A ): UpperCAmelCase = len(__A ) # Beforce calculations, the highway is empty UpperCAmelCase = [-1] * number_of_cells for car_index in range(__A ): if highway_now[car_index] != -1: # Add 1 to the current speed of the car and cap the speed UpperCAmelCase = min(highway_now[car_index] + 1 , __A ) # Number of empty cell before the next car UpperCAmelCase = get_distance(__A , __A ) - 1 # We can't have the car causing an accident UpperCAmelCase = min(next_highway[car_index] , __A ) if random() < probability: # Randomly, a driver will slow down UpperCAmelCase = max(next_highway[car_index] - 1 , 0 ) return next_highway def _lowerCAmelCase( __A , __A , __A , __A ): UpperCAmelCase = len(highway[0] ) for i in range(__A ): UpperCAmelCase = update(highway[i] , __A , __A ) UpperCAmelCase = [-1] * number_of_cells for car_index in range(__A ): UpperCAmelCase = next_speeds_calculated[car_index] if speed != -1: # Change the position based on the speed (with % to create the loop) UpperCAmelCase = (car_index + speed) % number_of_cells # Commit the change of position UpperCAmelCase = speed highway.append(__A ) return highway if __name__ == "__main__": import doctest doctest.testmod()
1
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { "unc-nlp/lxmert-base-uncased": "https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/config.json", } class __magic_name__ ( _snake_case ): UpperCAmelCase = """lxmert""" UpperCAmelCase = {} def __init__( self : int , lowerCAmelCase__ : Any=3_0_5_2_2 , lowerCAmelCase__ : List[str]=7_6_8 , lowerCAmelCase__ : Union[str, Any]=1_2 , lowerCAmelCase__ : List[Any]=9_5_0_0 , lowerCAmelCase__ : Any=1_6_0_0 , lowerCAmelCase__ : Union[str, Any]=4_0_0 , lowerCAmelCase__ : Tuple=3_0_7_2 , lowerCAmelCase__ : Dict="gelu" , lowerCAmelCase__ : Tuple=0.1 , lowerCAmelCase__ : Tuple=0.1 , lowerCAmelCase__ : int=5_1_2 , lowerCAmelCase__ : List[str]=2 , lowerCAmelCase__ : List[str]=0.02 , lowerCAmelCase__ : str=1e-1_2 , lowerCAmelCase__ : str=9 , lowerCAmelCase__ : int=5 , lowerCAmelCase__ : Optional[int]=5 , lowerCAmelCase__ : List[Any]=2_0_4_8 , lowerCAmelCase__ : Any=4 , lowerCAmelCase__ : Dict=6.67 , lowerCAmelCase__ : Any=True , lowerCAmelCase__ : Union[str, Any]=True , lowerCAmelCase__ : Any=True , lowerCAmelCase__ : Tuple=True , lowerCAmelCase__ : Optional[Any]=True , lowerCAmelCase__ : Optional[int]=True , lowerCAmelCase__ : Tuple=True , **lowerCAmelCase__ : List[Any] , ) -> Dict: UpperCAmelCase = vocab_size UpperCAmelCase = hidden_size UpperCAmelCase = num_attention_heads UpperCAmelCase = hidden_act UpperCAmelCase = intermediate_size UpperCAmelCase = hidden_dropout_prob UpperCAmelCase = attention_probs_dropout_prob UpperCAmelCase = max_position_embeddings UpperCAmelCase = type_vocab_size UpperCAmelCase = initializer_range UpperCAmelCase = layer_norm_eps UpperCAmelCase = num_qa_labels UpperCAmelCase = num_object_labels UpperCAmelCase = num_attr_labels UpperCAmelCase = l_layers UpperCAmelCase = x_layers UpperCAmelCase = r_layers UpperCAmelCase = visual_feat_dim UpperCAmelCase = visual_pos_dim UpperCAmelCase = visual_loss_normalizer UpperCAmelCase = task_matched UpperCAmelCase = task_mask_lm UpperCAmelCase = task_obj_predict UpperCAmelCase = task_qa UpperCAmelCase = visual_obj_loss UpperCAmelCase = visual_attr_loss UpperCAmelCase = visual_feat_loss UpperCAmelCase = {"vision": r_layers, "cross_encoder": x_layers, "language": l_layers} super().__init__(**lowerCAmelCase__ )
1
1
from __future__ import annotations from functools import lru_cache from math import ceil lowerCAmelCase__ = 100 lowerCAmelCase__ = set(range(3, NUM_PRIMES, 2)) primes.add(2) lowerCAmelCase__ = 42 for prime in range(3, ceil(NUM_PRIMES**0.5), 2): if prime not in primes: continue primes.difference_update(set(range(prime * prime, NUM_PRIMES, prime))) @lru_cache(maxsize=100 ) def _lowerCAmelCase( __A ): if number_to_partition < 0: return set() elif number_to_partition == 0: return {1} UpperCAmelCase = set() UpperCAmelCase = 42 UpperCAmelCase = 42 for prime in primes: if prime > number_to_partition: continue for sub in partition(number_to_partition - prime ): ret.add(sub * prime ) return ret def _lowerCAmelCase( __A = 5000 ): for number_to_partition in range(1 , __A ): if len(partition(__A ) ) > number_unique_partitions: return number_to_partition return None if __name__ == "__main__": print(f"{solution() = }")
1
def _lowerCAmelCase( __A ): UpperCAmelCase = 0 while num > 0: digit_sum += num % 10 num //= 10 return digit_sum def _lowerCAmelCase( __A = 100 ): UpperCAmelCase = 1 UpperCAmelCase = 2 for i in range(2 , max_n + 1 ): UpperCAmelCase = pre_numerator UpperCAmelCase = 2 * i // 3 if i % 3 == 0 else 1 UpperCAmelCase = cur_numerator UpperCAmelCase = e_cont * pre_numerator + temp return sum_digits(__A ) if __name__ == "__main__": print(f"{solution() = }")
1
1
import collections import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = "▁" lowerCAmelCase__ = {"vocab_file": "prophetnet.tokenizer"} lowerCAmelCase__ = { "vocab_file": { "microsoft/xprophetnet-large-wiki100-cased": ( "https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/prophetnet.tokenizer" ), } } lowerCAmelCase__ = { "microsoft/xprophetnet-large-wiki100-cased": {"do_lower_case": False}, } lowerCAmelCase__ = { "microsoft/xprophetnet-large-wiki100-cased": 512, } def _lowerCAmelCase( __A ): UpperCAmelCase = collections.OrderedDict() with open(__A , "r" , encoding="utf-8" ) as reader: UpperCAmelCase = reader.readlines() for index, token in enumerate(__A ): UpperCAmelCase = token.rstrip("\n" ) UpperCAmelCase = index return vocab class __magic_name__ ( _snake_case ): UpperCAmelCase = VOCAB_FILES_NAMES UpperCAmelCase = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase = ["""input_ids""", """attention_mask"""] def __init__( self : List[str] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Optional[Any]="[SEP]" , lowerCAmelCase__ : Any="[SEP]" , lowerCAmelCase__ : Tuple="[SEP]" , lowerCAmelCase__ : List[Any]="[UNK]" , lowerCAmelCase__ : Dict="[PAD]" , lowerCAmelCase__ : Tuple="[CLS]" , lowerCAmelCase__ : str="[MASK]" , lowerCAmelCase__ : Optional[Dict[str, Any]] = None , **lowerCAmelCase__ : int , ) -> None: UpperCAmelCase = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=lowerCAmelCase__ , eos_token=lowerCAmelCase__ , sep_token=lowerCAmelCase__ , unk_token=lowerCAmelCase__ , pad_token=lowerCAmelCase__ , cls_token=lowerCAmelCase__ , mask_token=lowerCAmelCase__ , sp_model_kwargs=self.sp_model_kwargs , **lowerCAmelCase__ , ) 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 UpperCAmelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(lowerCAmelCase__ ) ) UpperCAmelCase = 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 UpperCAmelCase = {"[PAD]": 0, "[CLS]": 1, "[SEP]": 2, "[UNK]": 3, "[MASK]": 4} for i in range(1_0 ): UpperCAmelCase = f"[unused{i}]" UpperCAmelCase = 5 + i # The first "real" token "," has position 15 in the embedding vocab and position 3 in the spm vocab UpperCAmelCase = 1_2 UpperCAmelCase = {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(lowerCAmelCase__ ) def __getstate__( self : Any ) -> List[Any]: UpperCAmelCase = self.__dict__.copy() UpperCAmelCase = None return state def __setstate__( self : List[Any] , lowerCAmelCase__ : str ) -> str: UpperCAmelCase = 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" ): UpperCAmelCase = {} UpperCAmelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def _UpperCamelCase ( self : Optional[Any] , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None , lowerCAmelCase__ : bool = False ) -> List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=lowerCAmelCase__ , token_ids_a=lowerCAmelCase__ , already_has_special_tokens=lowerCAmelCase__ ) if token_ids_a is None: return ([0] * len(lowerCAmelCase__ )) + [1] return ([0] * len(lowerCAmelCase__ )) + [1] + ([0] * len(lowerCAmelCase__ )) + [1] def _UpperCamelCase ( self : int , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None ) -> List[int]: UpperCAmelCase = [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 : int ) -> int: return len(self.sp_model ) + self.fairseq_offset def _UpperCamelCase ( self : Tuple ) -> Optional[int]: UpperCAmelCase = {self.convert_ids_to_tokens(lowerCAmelCase__ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def _UpperCamelCase ( self : Optional[Any] , lowerCAmelCase__ : str ) -> str: return self.sp_model.encode(lowerCAmelCase__ , out_type=lowerCAmelCase__ ) def _UpperCamelCase ( self : Any , lowerCAmelCase__ : int ) -> Tuple: if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] UpperCAmelCase = self.sp_model.PieceToId(lowerCAmelCase__ ) # 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 : str , lowerCAmelCase__ : int ) -> Tuple: 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 : Union[str, Any] , lowerCAmelCase__ : Any ) -> str: UpperCAmelCase = "".join(lowerCAmelCase__ ).replace(lowerCAmelCase__ , " " ).strip() return out_string def _UpperCamelCase ( self : Any , lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[str] = None ) -> Tuple[str]: if not os.path.isdir(lowerCAmelCase__ ): logger.error(f"Vocabulary path ({save_directory}) should be a directory" ) return UpperCAmelCase = os.path.join( lowerCAmelCase__ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(lowerCAmelCase__ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , lowerCAmelCase__ ) elif not os.path.isfile(self.vocab_file ): with open(lowerCAmelCase__ , "wb" ) as fi: UpperCAmelCase = self.sp_model.serialized_model_proto() fi.write(lowerCAmelCase__ ) return (out_vocab_file,) def _UpperCamelCase ( self : Dict , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None ) -> List[int]: if token_ids_a is None: return token_ids_a + [self.sep_token_id] UpperCAmelCase = [self.sep_token_id] return token_ids_a + sep + token_ids_a + sep
1
from . import ( albert, align, altclip, audio_spectrogram_transformer, auto, autoformer, bark, bart, barthez, bartpho, beit, bert, bert_generation, bert_japanese, bertweet, big_bird, bigbird_pegasus, biogpt, bit, blenderbot, blenderbot_small, blip, blip_a, bloom, bridgetower, byta, camembert, canine, chinese_clip, clap, clip, clipseg, codegen, conditional_detr, convbert, convnext, convnextva, cpm, cpmant, ctrl, cvt, dataavec, deberta, deberta_va, decision_transformer, deformable_detr, deit, deprecated, deta, detr, dialogpt, dinat, distilbert, dit, donut, dpr, dpt, efficientformer, efficientnet, electra, encodec, encoder_decoder, ernie, ernie_m, esm, falcon, flaubert, flava, fnet, focalnet, fsmt, funnel, git, glpn, gpta, gpt_bigcode, gpt_neo, gpt_neox, gpt_neox_japanese, gpt_swa, gptj, gptsan_japanese, graphormer, groupvit, herbert, hubert, ibert, imagegpt, informer, instructblip, jukebox, layoutlm, layoutlmva, layoutlmva, layoutxlm, led, levit, lilt, llama, longformer, longta, luke, lxmert, mam_aaa, marian, markuplm, maskaformer, maskformer, mbart, mbartaa, mega, megatron_bert, megatron_gpta, mgp_str, mluke, mobilebert, mobilenet_va, mobilenet_va, mobilevit, mobilevitva, mpnet, mra, mta, musicgen, mvp, nat, nezha, nllb, nllb_moe, nystromformer, oneformer, open_llama, openai, opt, owlvit, pegasus, pegasus_x, perceiver, phobert, pixastruct, plbart, poolformer, prophetnet, qdqbert, rag, realm, reformer, regnet, rembert, resnet, roberta, roberta_prelayernorm, roc_bert, roformer, rwkv, sam, segformer, sew, sew_d, speech_encoder_decoder, speech_to_text, speech_to_text_a, speechta, splinter, squeezebert, swiftformer, swin, swinasr, swinva, switch_transformers, ta, table_transformer, tapas, time_series_transformer, timesformer, timm_backbone, transfo_xl, trocr, tvlt, umta, unispeech, unispeech_sat, upernet, videomae, vilt, vision_encoder_decoder, vision_text_dual_encoder, visual_bert, vit, vit_hybrid, vit_mae, vit_msn, vivit, wavaveca, wavaveca_conformer, wavaveca_phoneme, wavaveca_with_lm, wavlm, whisper, x_clip, xglm, xlm, xlm_prophetnet, xlm_roberta, xlm_roberta_xl, xlnet, xmod, yolos, yoso, )
1
1
import warnings from ...utils import is_sklearn_available, requires_backends if is_sklearn_available(): from scipy.stats import pearsonr, spearmanr from sklearn.metrics import fa_score, matthews_corrcoef lowerCAmelCase__ = ( "This metric will be removed from the library soon, metrics should be handled with the 🤗 Evaluate " "library. You can have a look at this example script for pointers: " "https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py" ) def _lowerCAmelCase( __A , __A ): warnings.warn(__A , __A ) requires_backends(__A , "sklearn" ) return (preds == labels).mean() def _lowerCAmelCase( __A , __A ): warnings.warn(__A , __A ) requires_backends(__A , "sklearn" ) UpperCAmelCase = simple_accuracy(__A , __A ) UpperCAmelCase = fa_score(y_true=__A , y_pred=__A ) return { "acc": acc, "f1": fa, "acc_and_f1": (acc + fa) / 2, } def _lowerCAmelCase( __A , __A ): warnings.warn(__A , __A ) requires_backends(__A , "sklearn" ) UpperCAmelCase = pearsonr(__A , __A )[0] UpperCAmelCase = spearmanr(__A , __A )[0] return { "pearson": pearson_corr, "spearmanr": spearman_corr, "corr": (pearson_corr + spearman_corr) / 2, } def _lowerCAmelCase( __A , __A , __A ): warnings.warn(__A , __A ) requires_backends(__A , "sklearn" ) assert len(__A ) == len(__A ), F"Predictions and labels have mismatched lengths {len(__A )} and {len(__A )}" if task_name == "cola": return {"mcc": matthews_corrcoef(__A , __A )} elif task_name == "sst-2": return {"acc": simple_accuracy(__A , __A )} elif task_name == "mrpc": return acc_and_fa(__A , __A ) elif task_name == "sts-b": return pearson_and_spearman(__A , __A ) elif task_name == "qqp": return acc_and_fa(__A , __A ) elif task_name == "mnli": return {"mnli/acc": simple_accuracy(__A , __A )} elif task_name == "mnli-mm": return {"mnli-mm/acc": simple_accuracy(__A , __A )} elif task_name == "qnli": return {"acc": simple_accuracy(__A , __A )} elif task_name == "rte": return {"acc": simple_accuracy(__A , __A )} elif task_name == "wnli": return {"acc": simple_accuracy(__A , __A )} elif task_name == "hans": return {"acc": simple_accuracy(__A , __A )} else: raise KeyError(__A ) def _lowerCAmelCase( __A , __A , __A ): warnings.warn(__A , __A ) requires_backends(__A , "sklearn" ) if len(__A ) != len(__A ): raise ValueError(F"Predictions and labels have mismatched lengths {len(__A )} and {len(__A )}" ) if task_name == "xnli": return {"acc": simple_accuracy(__A , __A )} else: raise KeyError(__A )
1
import numpy # List of input, output pairs lowerCAmelCase__ = ( ((5, 2, 3), 15), ((6, 5, 9), 25), ((11, 12, 13), 41), ((1, 1, 1), 8), ((11, 12, 13), 41), ) lowerCAmelCase__ = (((515, 22, 13), 555), ((61, 35, 49), 150)) lowerCAmelCase__ = [2, 4, 1, 5] lowerCAmelCase__ = len(train_data) lowerCAmelCase__ = 0.0_0_9 def _lowerCAmelCase( __A , __A="train" ): return calculate_hypothesis_value(__A , __A ) - output( __A , __A ) def _lowerCAmelCase( __A ): UpperCAmelCase = 0 for i in range(len(__A ) - 1 ): hyp_val += data_input_tuple[i] * parameter_vector[i + 1] hyp_val += parameter_vector[0] return hyp_val def _lowerCAmelCase( __A , __A ): if data_set == "train": return train_data[example_no][1] elif data_set == "test": return test_data[example_no][1] return None def _lowerCAmelCase( __A , __A ): if data_set == "train": return _hypothesis_value(train_data[example_no][0] ) elif data_set == "test": return _hypothesis_value(test_data[example_no][0] ) return None def _lowerCAmelCase( __A , __A=m ): UpperCAmelCase = 0 for i in range(__A ): if index == -1: summation_value += _error(__A ) else: summation_value += _error(__A ) * train_data[i][0][index] return summation_value def _lowerCAmelCase( __A ): UpperCAmelCase = summation_of_cost_derivative(__A , __A ) / m return cost_derivative_value def _lowerCAmelCase( ): global parameter_vector # Tune these values to set a tolerance value for predicted output UpperCAmelCase = 0.000002 UpperCAmelCase = 0 UpperCAmelCase = 0 while True: j += 1 UpperCAmelCase = [0, 0, 0, 0] for i in range(0 , len(__A ) ): UpperCAmelCase = get_cost_derivative(i - 1 ) UpperCAmelCase = ( parameter_vector[i] - LEARNING_RATE * cost_derivative ) if numpy.allclose( __A , __A , atol=__A , rtol=__A , ): break UpperCAmelCase = temp_parameter_vector print(("Number of iterations:", j) ) def _lowerCAmelCase( ): for i in range(len(__A ) ): print(("Actual output value:", output(__A , "test" )) ) print(("Hypothesis output:", calculate_hypothesis_value(__A , "test" )) ) if __name__ == "__main__": run_gradient_descent() print("\nTesting gradient descent for a linear hypothesis function.\n") test_gradient_descent()
1
1
from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase__ = { "configuration_autoformer": [ "AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "AutoformerConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ "AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "AutoformerForPrediction", "AutoformerModel", "AutoformerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_autoformer import ( AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, AutoformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_autoformer import ( AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, AutoformerForPrediction, AutoformerModel, AutoformerPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
1
def _lowerCAmelCase( __A , __A , __A ): if n == 0: return 1 elif n % 2 == 1: return (binary_exponentiation(__A , n - 1 , __A ) * a) % mod else: UpperCAmelCase = binary_exponentiation(__A , n / 2 , __A ) return (b * b) % mod # a prime number lowerCAmelCase__ = 701 lowerCAmelCase__ = 1000000000 lowerCAmelCase__ = 10 # using binary exponentiation function, O(log(p)): print((a / b) % p == (a * binary_exponentiation(b, p - 2, p)) % p) print((a / b) % p == (a * b ** (p - 2)) % p)
1
1
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import convert_to_rgb, normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL lowerCAmelCase__ = logging.get_logger(__name__) class __magic_name__ ( _snake_case ): UpperCAmelCase = ["""pixel_values"""] def __init__( self : List[Any] , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : Dict[str, int] = None , lowerCAmelCase__ : PILImageResampling = PILImageResampling.BICUBIC , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : Union[int, float] = 1 / 2_5_5 , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : Optional[Union[float, List[float]]] = None , lowerCAmelCase__ : Optional[Union[float, List[float]]] = None , lowerCAmelCase__ : bool = True , **lowerCAmelCase__ : Any , ) -> None: super().__init__(**lowerCAmelCase__ ) UpperCAmelCase = size if size is not None else {"height": 3_8_4, "width": 3_8_4} UpperCAmelCase = get_size_dict(lowerCAmelCase__ , default_to_square=lowerCAmelCase__ ) UpperCAmelCase = do_resize UpperCAmelCase = size UpperCAmelCase = resample UpperCAmelCase = do_rescale UpperCAmelCase = rescale_factor UpperCAmelCase = do_normalize UpperCAmelCase = image_mean if image_mean is not None else OPENAI_CLIP_MEAN UpperCAmelCase = image_std if image_std is not None else OPENAI_CLIP_STD UpperCAmelCase = do_convert_rgb def _UpperCamelCase ( self : int , lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : Dict[str, int] , lowerCAmelCase__ : PILImageResampling = PILImageResampling.BICUBIC , lowerCAmelCase__ : Optional[Union[str, ChannelDimension]] = None , **lowerCAmelCase__ : List[Any] , ) -> np.ndarray: UpperCAmelCase = get_size_dict(lowerCAmelCase__ , default_to_square=lowerCAmelCase__ ) if "height" not in size or "width" not in size: raise ValueError(f"The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}" ) UpperCAmelCase = (size["height"], size["width"]) return resize(lowerCAmelCase__ , size=lowerCAmelCase__ , resample=lowerCAmelCase__ , data_format=lowerCAmelCase__ , **lowerCAmelCase__ ) def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : Union[int, float] , lowerCAmelCase__ : Optional[Union[str, ChannelDimension]] = None , **lowerCAmelCase__ : Optional[int] , ) -> Optional[Any]: return rescale(lowerCAmelCase__ , scale=lowerCAmelCase__ , data_format=lowerCAmelCase__ , **lowerCAmelCase__ ) def _UpperCamelCase ( self : Any , lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : Union[float, List[float]] , lowerCAmelCase__ : Union[float, List[float]] , lowerCAmelCase__ : Optional[Union[str, ChannelDimension]] = None , **lowerCAmelCase__ : List[Any] , ) -> np.ndarray: return normalize(lowerCAmelCase__ , mean=lowerCAmelCase__ , std=lowerCAmelCase__ , data_format=lowerCAmelCase__ , **lowerCAmelCase__ ) def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : ImageInput , lowerCAmelCase__ : Optional[bool] = None , lowerCAmelCase__ : Optional[Dict[str, int]] = None , lowerCAmelCase__ : PILImageResampling = None , lowerCAmelCase__ : Optional[bool] = None , lowerCAmelCase__ : Optional[float] = None , lowerCAmelCase__ : Optional[bool] = None , lowerCAmelCase__ : Optional[Union[float, List[float]]] = None , lowerCAmelCase__ : Optional[Union[float, List[float]]] = None , lowerCAmelCase__ : Optional[Union[str, TensorType]] = None , lowerCAmelCase__ : bool = None , lowerCAmelCase__ : ChannelDimension = ChannelDimension.FIRST , **lowerCAmelCase__ : int , ) -> PIL.Image.Image: UpperCAmelCase = do_resize if do_resize is not None else self.do_resize UpperCAmelCase = resample if resample is not None else self.resample UpperCAmelCase = do_rescale if do_rescale is not None else self.do_rescale UpperCAmelCase = rescale_factor if rescale_factor is not None else self.rescale_factor UpperCAmelCase = do_normalize if do_normalize is not None else self.do_normalize UpperCAmelCase = image_mean if image_mean is not None else self.image_mean UpperCAmelCase = image_std if image_std is not None else self.image_std UpperCAmelCase = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb UpperCAmelCase = size if size is not None else self.size UpperCAmelCase = get_size_dict(lowerCAmelCase__ , default_to_square=lowerCAmelCase__ ) UpperCAmelCase = make_list_of_images(lowerCAmelCase__ ) if not valid_images(lowerCAmelCase__ ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None or resample is None: raise ValueError("Size and resample must be specified if do_resize is True." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("Image mean and std must be specified if do_normalize is True." ) # PIL RGBA images are converted to RGB if do_convert_rgb: UpperCAmelCase = [convert_to_rgb(lowerCAmelCase__ ) for image in images] # All transformations expect numpy arrays. UpperCAmelCase = [to_numpy_array(lowerCAmelCase__ ) for image in images] if do_resize: UpperCAmelCase = [self.resize(image=lowerCAmelCase__ , size=lowerCAmelCase__ , resample=lowerCAmelCase__ ) for image in images] if do_rescale: UpperCAmelCase = [self.rescale(image=lowerCAmelCase__ , scale=lowerCAmelCase__ ) for image in images] if do_normalize: UpperCAmelCase = [self.normalize(image=lowerCAmelCase__ , mean=lowerCAmelCase__ , std=lowerCAmelCase__ ) for image in images] UpperCAmelCase = [to_channel_dimension_format(lowerCAmelCase__ , lowerCAmelCase__ ) for image in images] UpperCAmelCase = BatchFeature(data={"pixel_values": images} , tensor_type=lowerCAmelCase__ ) return encoded_outputs
1
lowerCAmelCase__ = { "a": "AAAAA", "b": "AAAAB", "c": "AAABA", "d": "AAABB", "e": "AABAA", "f": "AABAB", "g": "AABBA", "h": "AABBB", "i": "ABAAA", "j": "BBBAA", "k": "ABAAB", "l": "ABABA", "m": "ABABB", "n": "ABBAA", "o": "ABBAB", "p": "ABBBA", "q": "ABBBB", "r": "BAAAA", "s": "BAAAB", "t": "BAABA", "u": "BAABB", "v": "BBBAB", "w": "BABAA", "x": "BABAB", "y": "BABBA", "z": "BABBB", " ": " ", } lowerCAmelCase__ = {value: key for key, value in encode_dict.items()} def _lowerCAmelCase( __A ): UpperCAmelCase = "" 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( __A ): if set(__A ) - {"A", "B", " "} != set(): raise Exception("decode() accepts only 'A', 'B' and spaces" ) UpperCAmelCase = "" for word in coded.split(): while len(__A ) != 0: decoded += decode_dict[word[:5]] UpperCAmelCase = word[5:] decoded += " " return decoded.strip() if __name__ == "__main__": from doctest import testmod testmod()
1
1
import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES from ...utils import logging from ..auto import CONFIG_MAPPING lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { "salesforce/blip2-opt-2.7b": "https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json", } class __magic_name__ ( _snake_case ): UpperCAmelCase = """blip_2_vision_model""" def __init__( self : Union[str, Any] , lowerCAmelCase__ : Dict=1_4_0_8 , lowerCAmelCase__ : Optional[int]=6_1_4_4 , lowerCAmelCase__ : Union[str, Any]=3_9 , lowerCAmelCase__ : str=1_6 , lowerCAmelCase__ : Dict=2_2_4 , lowerCAmelCase__ : str=1_4 , lowerCAmelCase__ : int="gelu" , lowerCAmelCase__ : Optional[int]=0.00_001 , lowerCAmelCase__ : str=0.0 , lowerCAmelCase__ : List[str]=1e-1_0 , lowerCAmelCase__ : List[str]=True , **lowerCAmelCase__ : List[str] , ) -> int: super().__init__(**lowerCAmelCase__ ) UpperCAmelCase = hidden_size UpperCAmelCase = intermediate_size UpperCAmelCase = num_hidden_layers UpperCAmelCase = num_attention_heads UpperCAmelCase = patch_size UpperCAmelCase = image_size UpperCAmelCase = initializer_range UpperCAmelCase = attention_dropout UpperCAmelCase = layer_norm_eps UpperCAmelCase = hidden_act UpperCAmelCase = qkv_bias @classmethod def _UpperCamelCase ( cls : Optional[Any] , lowerCAmelCase__ : Union[str, os.PathLike] , **lowerCAmelCase__ : Union[str, Any] ) -> "PretrainedConfig": cls._set_token_in_kwargs(lowerCAmelCase__ ) UpperCAmelCase , UpperCAmelCase = cls.get_config_dict(lowerCAmelCase__ , **lowerCAmelCase__ ) # get the vision config dict if we are loading from Blip2Config if config_dict.get("model_type" ) == "blip-2": UpperCAmelCase = config_dict["vision_config"] if "model_type" in config_dict and hasattr(cls , "model_type" ) and config_dict["model_type"] != cls.model_type: logger.warning( f"You are using a model of type {config_dict['model_type']} to instantiate a model of type " f"{cls.model_type}. This is not supported for all configurations of models and can yield errors." ) return cls.from_dict(lowerCAmelCase__ , **lowerCAmelCase__ ) class __magic_name__ ( _snake_case ): UpperCAmelCase = """blip_2_qformer""" def __init__( self : Any , lowerCAmelCase__ : Dict=3_0_5_2_2 , lowerCAmelCase__ : str=7_6_8 , lowerCAmelCase__ : Union[str, Any]=1_2 , lowerCAmelCase__ : Any=1_2 , lowerCAmelCase__ : Tuple=3_0_7_2 , lowerCAmelCase__ : Any="gelu" , lowerCAmelCase__ : Union[str, Any]=0.1 , lowerCAmelCase__ : List[Any]=0.1 , lowerCAmelCase__ : Optional[Any]=5_1_2 , lowerCAmelCase__ : List[str]=0.02 , lowerCAmelCase__ : Union[str, Any]=1e-1_2 , lowerCAmelCase__ : Dict=0 , lowerCAmelCase__ : Optional[int]="absolute" , lowerCAmelCase__ : Union[str, Any]=2 , lowerCAmelCase__ : List[str]=1_4_0_8 , **lowerCAmelCase__ : Tuple , ) -> Union[str, Any]: super().__init__(pad_token_id=lowerCAmelCase__ , **lowerCAmelCase__ ) UpperCAmelCase = vocab_size UpperCAmelCase = hidden_size UpperCAmelCase = num_hidden_layers UpperCAmelCase = num_attention_heads UpperCAmelCase = hidden_act UpperCAmelCase = intermediate_size UpperCAmelCase = hidden_dropout_prob UpperCAmelCase = attention_probs_dropout_prob UpperCAmelCase = max_position_embeddings UpperCAmelCase = initializer_range UpperCAmelCase = layer_norm_eps UpperCAmelCase = position_embedding_type UpperCAmelCase = cross_attention_frequency UpperCAmelCase = encoder_hidden_size @classmethod def _UpperCamelCase ( cls : List[str] , lowerCAmelCase__ : Union[str, os.PathLike] , **lowerCAmelCase__ : Dict ) -> "PretrainedConfig": cls._set_token_in_kwargs(lowerCAmelCase__ ) UpperCAmelCase , UpperCAmelCase = cls.get_config_dict(lowerCAmelCase__ , **lowerCAmelCase__ ) # get the qformer config dict if we are loading from Blip2Config if config_dict.get("model_type" ) == "blip-2": UpperCAmelCase = config_dict["qformer_config"] if "model_type" in config_dict and hasattr(cls , "model_type" ) and config_dict["model_type"] != cls.model_type: logger.warning( f"You are using a model of type {config_dict['model_type']} to instantiate a model of type " f"{cls.model_type}. This is not supported for all configurations of models and can yield errors." ) return cls.from_dict(lowerCAmelCase__ , **lowerCAmelCase__ ) class __magic_name__ ( _snake_case ): UpperCAmelCase = """blip-2""" UpperCAmelCase = True def __init__( self : List[Any] , lowerCAmelCase__ : Tuple=None , lowerCAmelCase__ : int=None , lowerCAmelCase__ : Tuple=None , lowerCAmelCase__ : Tuple=3_2 , **lowerCAmelCase__ : Any ) -> Optional[int]: super().__init__(**lowerCAmelCase__ ) if vision_config is None: UpperCAmelCase = {} logger.info("vision_config is None. initializing the Blip2VisionConfig with default values." ) if qformer_config is None: UpperCAmelCase = {} logger.info("qformer_config is None. Initializing the Blip2QFormerConfig with default values." ) if text_config is None: UpperCAmelCase = {} logger.info("text_config is None. Initializing the text config with default values (`OPTConfig`)." ) UpperCAmelCase = BlipaVisionConfig(**lowerCAmelCase__ ) UpperCAmelCase = BlipaQFormerConfig(**lowerCAmelCase__ ) UpperCAmelCase = text_config["model_type"] if "model_type" in text_config else "opt" UpperCAmelCase = CONFIG_MAPPING[text_model_type](**lowerCAmelCase__ ) UpperCAmelCase = self.text_config.tie_word_embeddings UpperCAmelCase = self.text_config.is_encoder_decoder UpperCAmelCase = num_query_tokens UpperCAmelCase = self.vision_config.hidden_size UpperCAmelCase = self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES UpperCAmelCase = 1.0 UpperCAmelCase = 0.02 @classmethod def _UpperCamelCase ( cls : Optional[Any] , lowerCAmelCase__ : BlipaVisionConfig , lowerCAmelCase__ : BlipaQFormerConfig , lowerCAmelCase__ : PretrainedConfig , **lowerCAmelCase__ : int , ) -> str: return cls( vision_config=vision_config.to_dict() , qformer_config=qformer_config.to_dict() , text_config=text_config.to_dict() , **lowerCAmelCase__ , ) def _UpperCamelCase ( self : Dict ) -> Optional[Any]: UpperCAmelCase = copy.deepcopy(self.__dict__ ) UpperCAmelCase = self.vision_config.to_dict() UpperCAmelCase = self.qformer_config.to_dict() UpperCAmelCase = self.text_config.to_dict() UpperCAmelCase = self.__class__.model_type return output
1
from __future__ import annotations import json import requests from bsa import BeautifulSoup from fake_useragent import UserAgent lowerCAmelCase__ = {"UserAgent": UserAgent().random} def _lowerCAmelCase( __A ): UpperCAmelCase = script.contents[0] UpperCAmelCase = json.loads(data[data.find("{\"config\"" ) : -1] ) return info["entry_data"]["ProfilePage"][0]["graphql"]["user"] class __magic_name__ : def __init__( self : Optional[Any] , lowerCAmelCase__ : Optional[int] ) -> Any: UpperCAmelCase = f"https://www.instagram.com/{username}/" UpperCAmelCase = self.get_json() def _UpperCamelCase ( self : List[str] ) -> dict: UpperCAmelCase = requests.get(self.url , headers=lowerCAmelCase__ ).text UpperCAmelCase = BeautifulSoup(lowerCAmelCase__ , "html.parser" ).find_all("script" ) try: return extract_user_profile(scripts[4] ) except (json.decoder.JSONDecodeError, KeyError): return extract_user_profile(scripts[3] ) def __repr__( self : Tuple ) -> str: return f"{self.__class__.__name__}('{self.username}')" def __str__( self : Optional[int] ) -> str: return f"{self.fullname} ({self.username}) is {self.biography}" @property def _UpperCamelCase ( self : Any ) -> str: return self.user_data["username"] @property def _UpperCamelCase ( self : List[Any] ) -> str: return self.user_data["full_name"] @property def _UpperCamelCase ( self : List[str] ) -> str: return self.user_data["biography"] @property def _UpperCamelCase ( self : Optional[int] ) -> str: return self.user_data["business_email"] @property def _UpperCamelCase ( self : str ) -> str: return self.user_data["external_url"] @property def _UpperCamelCase ( self : int ) -> int: return self.user_data["edge_followed_by"]["count"] @property def _UpperCamelCase ( self : List[Any] ) -> int: return self.user_data["edge_follow"]["count"] @property def _UpperCamelCase ( self : List[str] ) -> int: return self.user_data["edge_owner_to_timeline_media"]["count"] @property def _UpperCamelCase ( self : Tuple ) -> str: return self.user_data["profile_pic_url_hd"] @property def _UpperCamelCase ( self : Optional[int] ) -> bool: return self.user_data["is_verified"] @property def _UpperCamelCase ( self : Optional[Any] ) -> bool: return self.user_data["is_private"] def _lowerCAmelCase( __A = "github" ): import os if os.environ.get("CI" ): return # test failing on GitHub Actions UpperCAmelCase = InstagramUser(__A ) assert instagram_user.user_data assert isinstance(instagram_user.user_data , __A ) assert instagram_user.username == username if username != "github": return assert instagram_user.fullname == "GitHub" assert instagram_user.biography == "Built for developers." assert instagram_user.number_of_posts > 150 assert instagram_user.number_of_followers > 120000 assert instagram_user.number_of_followings > 15 assert instagram_user.email == "support@github.com" assert instagram_user.website == "https://github.com/readme" assert instagram_user.profile_picture_url.startswith("https://instagram." ) assert instagram_user.is_verified is True assert instagram_user.is_private is False if __name__ == "__main__": import doctest doctest.testmod() lowerCAmelCase__ = InstagramUser("github") print(instagram_user) print(f"{instagram_user.number_of_posts = }") print(f"{instagram_user.number_of_followers = }") print(f"{instagram_user.number_of_followings = }") print(f"{instagram_user.email = }") print(f"{instagram_user.website = }") print(f"{instagram_user.profile_picture_url = }") print(f"{instagram_user.is_verified = }") print(f"{instagram_user.is_private = }")
1
1
from maths.is_square_free import is_square_free from maths.prime_factors import prime_factors def _lowerCAmelCase( __A ): UpperCAmelCase = prime_factors(__A ) if is_square_free(__A ): return -1 if len(__A ) % 2 else 1 return 0 if __name__ == "__main__": import doctest doctest.testmod()
1
import unittest import numpy as np def _lowerCAmelCase( __A , __A , __A , __A = None , ): UpperCAmelCase = np.shape(__A ) UpperCAmelCase = np.shape(__A ) UpperCAmelCase = np.shape(__A ) if shape_a[0] != shape_b[0]: UpperCAmelCase = ( "Expected the same number of rows for A and B. " F"Instead found A of size {shape_a} and B of size {shape_b}" ) raise ValueError(__A ) if shape_b[1] != shape_c[1]: UpperCAmelCase = ( "Expected the same number of columns for B and C. " F"Instead found B of size {shape_b} and C of size {shape_c}" ) raise ValueError(__A ) UpperCAmelCase = pseudo_inv if a_inv is None: try: UpperCAmelCase = np.linalg.inv(__A ) except np.linalg.LinAlgError: raise ValueError( "Input matrix A is not invertible. Cannot compute Schur complement." ) return mat_c - mat_b.T @ a_inv @ mat_b class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : List[str] ) -> None: UpperCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase = np.array([[2, 1], [6, 3]] ) UpperCAmelCase = schur_complement(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase = np.block([[a, b], [b.T, c]] ) UpperCAmelCase = np.linalg.det(lowerCAmelCase__ ) UpperCAmelCase = np.linalg.det(lowerCAmelCase__ ) UpperCAmelCase = np.linalg.det(lowerCAmelCase__ ) self.assertAlmostEqual(lowerCAmelCase__ , det_a * det_s ) def _UpperCamelCase ( self : str ) -> None: UpperCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase = np.array([[2, 1], [6, 3]] ) with self.assertRaises(lowerCAmelCase__ ): schur_complement(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def _UpperCamelCase ( self : Dict ) -> None: UpperCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase = np.array([[2, 1, 3], [6, 3, 5]] ) with self.assertRaises(lowerCAmelCase__ ): schur_complement(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) if __name__ == "__main__": import doctest doctest.testmod() unittest.main()
1
1
from typing import List, Optional, Union import numpy as np import tensorflow as tf from .utils import logging lowerCAmelCase__ = logging.get_logger(__name__) def _lowerCAmelCase( __A ): if isinstance(__A , np.ndarray ): return list(tensor.shape ) UpperCAmelCase = tf.shape(__A ) if tensor.shape == tf.TensorShape(__A ): return dynamic UpperCAmelCase = tensor.shape.as_list() return [dynamic[i] if s is None else s for i, s in enumerate(__A )] def _lowerCAmelCase( __A , __A = None , __A = None ): return tf.nn.softmax(logits=logits + 1E-9 , axis=__A , name=__A ) def _lowerCAmelCase( __A , __A , __A , __A=1E-5 , __A=-1 ): # This is a very simplified functional layernorm, designed to duplicate # the functionality of PyTorch nn.functional.layer_norm when this is needed to port # models in Transformers. if weight.shape.rank != 1 or bias.shape.rank != 1 or not isinstance(__A , __A ): raise NotImplementedError("Only 1D weight and bias tensors are supported for now, with only a single axis." ) # Get mean and variance on the axis to be normalized UpperCAmelCase , UpperCAmelCase = tf.nn.moments(__A , axes=[axis] , keepdims=__A ) if axis != -1: # Reshape scale and weight to have the same rank as inputs, but with 1 dimensions # on every dimension except axis UpperCAmelCase = [1] * inputs.shape.rank UpperCAmelCase = shape_list(__A )[axis] UpperCAmelCase = tf.reshape(__A , __A ) UpperCAmelCase = tf.reshape(__A , __A ) # Compute layer normalization using the batch_normalization # function. UpperCAmelCase = tf.nn.batch_normalization( __A , __A , __A , offset=__A , scale=__A , variance_epsilon=__A , ) return outputs def _lowerCAmelCase( __A , __A=0 , __A=-1 ): # Replicates the behavior of torch.flatten in TF # If end_dim or start_dim is negative, count them from the end if end_dim < 0: end_dim += input.shape.rank if start_dim < 0: start_dim += input.shape.rank if start_dim == end_dim: return input UpperCAmelCase = tf.shape(__A ) UpperCAmelCase = tf.math.reduce_prod(in_shape[start_dim : end_dim + 1] ) UpperCAmelCase = tf.concat([in_shape[:start_dim], [flattened_dim], in_shape[end_dim + 1 :]] , axis=0 ) return tf.reshape(__A , __A ) def _lowerCAmelCase( __A ): if not isinstance(__A , tf.Tensor ): UpperCAmelCase = tf.convert_to_tensor(__A ) # Catches stray NumPy inputs if encoder_attention_mask.shape.rank == 3: UpperCAmelCase = encoder_attention_mask[:, None, :, :] if encoder_attention_mask.shape.rank == 2: UpperCAmelCase = encoder_attention_mask[:, None, None, :] # T5 has a mask that can compare sequence ids, we can simulate this here with this transposition # Cf. https://github.com/tensorflow/mesh/blob/8d2465e9bc93129b913b5ccc6a59aa97abd96ec6/mesh_tensorflow # /transformer/transformer_layers.py#L270 # encoder_extended_attention_mask = (encoder_extended_attention_mask == # encoder_extended_attention_mask.transpose(-1, -2)) UpperCAmelCase = ( tf.cast(1 , encoder_attention_mask.dtype ) - encoder_extended_attention_mask ) * encoder_extended_attention_mask.dtype.min return encoder_extended_attention_mask def _lowerCAmelCase( __A , __A , __A = "input_ids" ): tf.debugging.assert_less( __A , tf.cast(__A , dtype=tensor.dtype ) , message=( F"The maximum value of {tensor_name} ({tf.math.reduce_max(__A )}) must be smaller than the embedding " F"layer's input dimension ({embed_dim}). The likely cause is some problem at tokenization time." ) , ) def _lowerCAmelCase( __A , __A , __A ): UpperCAmelCase = 64512 # Check that no item in `data` is larger than `HDF5_OBJECT_HEADER_LIMIT` # because in that case even chunking the array would not make the saving # possible. UpperCAmelCase = [x for x in data if len(__A ) > HDF5_OBJECT_HEADER_LIMIT] # Expecting this to never be true. if bad_attributes: raise RuntimeError( "The following attributes cannot be saved to HDF5 file because " F"they are larger than {HDF5_OBJECT_HEADER_LIMIT} " F"bytes: {bad_attributes}" ) UpperCAmelCase = np.asarray(__A ) UpperCAmelCase = 1 UpperCAmelCase = np.array_split(__A , __A ) # This will never loop forever thanks to the test above. while any(x.nbytes > HDF5_OBJECT_HEADER_LIMIT for x in chunked_data ): num_chunks += 1 UpperCAmelCase = np.array_split(__A , __A ) if num_chunks > 1: for chunk_id, chunk_data in enumerate(__A ): UpperCAmelCase = chunk_data else: UpperCAmelCase = data def _lowerCAmelCase( __A , __A ): if name in group.attrs: UpperCAmelCase = [n.decode("utf8" ) if hasattr(__A , "decode" ) else n for n in group.attrs[name]] else: UpperCAmelCase = [] UpperCAmelCase = 0 while "%s%d" % (name, chunk_id) in group.attrs: data.extend( [n.decode("utf8" ) if hasattr(__A , "decode" ) else n for n in group.attrs["%s%d" % (name, chunk_id)]] ) chunk_id += 1 return data def _lowerCAmelCase( __A ): def _expand_single_ad_tensor(__A ): if isinstance(__A , tf.Tensor ) and t.shape.rank == 1: return tf.expand_dims(__A , axis=-1 ) return t return tf.nest.map_structure(_expand_single_ad_tensor , __A )
1
import argparse import os import re import numpy as np import PIL import torch from timm import create_model from torch.optim.lr_scheduler import OneCycleLR from torch.utils.data import DataLoader, Dataset from torchvision.transforms import Compose, RandomResizedCrop, Resize, ToTensor from accelerate import Accelerator def _lowerCAmelCase( __A ): UpperCAmelCase = fname.split(os.path.sep )[-1] return re.search(r"^(.*)_\d+\.jpg$" , __A ).groups()[0] class __magic_name__ ( _snake_case ): def __init__( self : Any , lowerCAmelCase__ : int , lowerCAmelCase__ : Union[str, Any]=None , lowerCAmelCase__ : int=None ) -> Optional[Any]: UpperCAmelCase = file_names UpperCAmelCase = image_transform UpperCAmelCase = label_to_id def __len__( self : Tuple ) -> List[str]: return len(self.file_names ) def __getitem__( self : Optional[int] , lowerCAmelCase__ : Tuple ) -> Dict: UpperCAmelCase = self.file_names[idx] UpperCAmelCase = PIL.Image.open(lowerCAmelCase__ ) UpperCAmelCase = raw_image.convert("RGB" ) if self.image_transform is not None: UpperCAmelCase = self.image_transform(lowerCAmelCase__ ) UpperCAmelCase = extract_label(lowerCAmelCase__ ) if self.label_to_id is not None: UpperCAmelCase = self.label_to_id[label] return {"image": image, "label": label} def _lowerCAmelCase( __A , __A ): # Initialize accelerator if args.with_tracking: UpperCAmelCase = Accelerator( cpu=args.cpu , mixed_precision=args.mixed_precision , log_with="all" , project_dir=args.project_dir ) else: UpperCAmelCase = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs UpperCAmelCase = config["lr"] UpperCAmelCase = int(config["num_epochs"] ) UpperCAmelCase = int(config["seed"] ) UpperCAmelCase = int(config["batch_size"] ) UpperCAmelCase = config["image_size"] if not isinstance(__A , (list, tuple) ): UpperCAmelCase = (image_size, image_size) # Parse out whether we are saving every epoch or after a certain number of batches if hasattr(args.checkpointing_steps , "isdigit" ): if args.checkpointing_steps == "epoch": UpperCAmelCase = args.checkpointing_steps elif args.checkpointing_steps.isdigit(): UpperCAmelCase = int(args.checkpointing_steps ) else: raise ValueError( F"Argument `checkpointing_steps` must be either a number or `epoch`. `{args.checkpointing_steps}` passed." ) else: UpperCAmelCase = None # We need to initialize the trackers we use, and also store our configuration if args.with_tracking: UpperCAmelCase = os.path.split(__A )[-1].split("." )[0] accelerator.init_trackers(__A , __A ) # Grab all the image filenames UpperCAmelCase = [os.path.join(args.data_dir , __A ) for fname in os.listdir(args.data_dir ) if fname.endswith(".jpg" )] # Build the label correspondences UpperCAmelCase = [extract_label(__A ) for fname in file_names] UpperCAmelCase = list(set(__A ) ) id_to_label.sort() UpperCAmelCase = {lbl: i for i, lbl in enumerate(__A )} # Set the seed before splitting the data. np.random.seed(__A ) torch.manual_seed(__A ) torch.cuda.manual_seed_all(__A ) # Split our filenames between train and validation UpperCAmelCase = np.random.permutation(len(__A ) ) UpperCAmelCase = int(0.8 * len(__A ) ) UpperCAmelCase = random_perm[:cut] UpperCAmelCase = random_perm[cut:] # For training we use a simple RandomResizedCrop UpperCAmelCase = Compose([RandomResizedCrop(__A , scale=(0.5, 1.0) ), ToTensor()] ) UpperCAmelCase = PetsDataset( [file_names[i] for i in train_split] , image_transform=__A , label_to_id=__A ) # For evaluation, we use a deterministic Resize UpperCAmelCase = Compose([Resize(__A ), ToTensor()] ) UpperCAmelCase = PetsDataset([file_names[i] for i in eval_split] , image_transform=__A , label_to_id=__A ) # Instantiate dataloaders. UpperCAmelCase = DataLoader(__A , shuffle=__A , batch_size=__A , num_workers=4 ) UpperCAmelCase = DataLoader(__A , shuffle=__A , batch_size=__A , num_workers=4 ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) UpperCAmelCase = create_model("resnet50d" , pretrained=__A , num_classes=len(__A ) ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). UpperCAmelCase = model.to(accelerator.device ) # Freezing the base model for param in model.parameters(): UpperCAmelCase = False for param in model.get_classifier().parameters(): UpperCAmelCase = True # We normalize the batches of images to be a bit faster. UpperCAmelCase = torch.tensor(model.default_cfg["mean"] )[None, :, None, None].to(accelerator.device ) UpperCAmelCase = torch.tensor(model.default_cfg["std"] )[None, :, None, None].to(accelerator.device ) # Instantiate optimizer UpperCAmelCase = torch.optim.Adam(params=model.parameters() , lr=lr / 25 ) # Instantiate learning rate scheduler UpperCAmelCase = OneCycleLR(optimizer=__A , max_lr=__A , epochs=__A , steps_per_epoch=len(__A ) ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = accelerator.prepare( __A , __A , __A , __A , __A ) # We need to keep track of how many total steps we have iterated over UpperCAmelCase = 0 # We also need to keep track of the starting epoch so files are named properly UpperCAmelCase = 0 # Potentially load in the weights and states from a previous save if args.resume_from_checkpoint: if args.resume_from_checkpoint is not None or args.resume_from_checkpoint != "": accelerator.print(F"Resumed from checkpoint: {args.resume_from_checkpoint}" ) accelerator.load_state(args.resume_from_checkpoint ) UpperCAmelCase = os.path.basename(args.resume_from_checkpoint ) else: # Get the most recent checkpoint UpperCAmelCase = [f.name for f in os.scandir(os.getcwd() ) if f.is_dir()] dirs.sort(key=os.path.getctime ) UpperCAmelCase = dirs[-1] # Sorts folders by date modified, most recent checkpoint is the last # Extract `epoch_{i}` or `step_{i}` UpperCAmelCase = os.path.splitext(__A )[0] if "epoch" in training_difference: UpperCAmelCase = int(training_difference.replace("epoch_" , "" ) ) + 1 UpperCAmelCase = None else: UpperCAmelCase = int(training_difference.replace("step_" , "" ) ) UpperCAmelCase = resume_step // len(__A ) resume_step -= starting_epoch * len(__A ) # Now we train the model for epoch in range(__A , __A ): model.train() if args.with_tracking: UpperCAmelCase = 0 if args.resume_from_checkpoint and epoch == starting_epoch and resume_step is not None: # We need to skip steps until we reach the resumed step UpperCAmelCase = accelerator.skip_first_batches(__A , __A ) overall_step += resume_step else: # After the first iteration though, we need to go back to the original dataloader UpperCAmelCase = train_dataloader for batch in active_dataloader: # We could avoid this line since we set the accelerator with `device_placement=True`. UpperCAmelCase = {k: v.to(accelerator.device ) for k, v in batch.items()} UpperCAmelCase = (batch["image"] - mean) / std UpperCAmelCase = model(__A ) UpperCAmelCase = torch.nn.functional.cross_entropy(__A , batch["label"] ) # We keep track of the loss at each epoch if args.with_tracking: total_loss += loss.detach().float() accelerator.backward(__A ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 if isinstance(__A , __A ): UpperCAmelCase = F"step_{overall_step}" if overall_step % checkpointing_steps == 0: if args.output_dir is not None: UpperCAmelCase = os.path.join(args.output_dir , __A ) accelerator.save_state(__A ) model.eval() UpperCAmelCase = 0 UpperCAmelCase = 0 for step, batch in enumerate(__A ): # We could avoid this line since we set the accelerator with `device_placement=True`. UpperCAmelCase = {k: v.to(accelerator.device ) for k, v in batch.items()} UpperCAmelCase = (batch["image"] - mean) / std with torch.no_grad(): UpperCAmelCase = model(__A ) UpperCAmelCase = outputs.argmax(dim=-1 ) UpperCAmelCase , UpperCAmelCase = accelerator.gather_for_metrics((predictions, batch["label"]) ) UpperCAmelCase = predictions == references num_elems += accurate_preds.shape[0] accurate += accurate_preds.long().sum() UpperCAmelCase = accurate.item() / num_elems # Use accelerator.print to print only on the main process. accelerator.print(F"epoch {epoch}: {100 * eval_metric:.2f}" ) if args.with_tracking: accelerator.log( { "accuracy": 100 * eval_metric, "train_loss": total_loss.item() / len(__A ), "epoch": epoch, } , step=__A , ) if checkpointing_steps == "epoch": UpperCAmelCase = F"epoch_{epoch}" if args.output_dir is not None: UpperCAmelCase = os.path.join(args.output_dir , __A ) accelerator.save_state(__A ) if args.with_tracking: accelerator.end_training() def _lowerCAmelCase( ): UpperCAmelCase = argparse.ArgumentParser(description="Simple example of training script." ) parser.add_argument("--data_dir" , required=__A , help="The data folder on disk." ) parser.add_argument("--fp16" , action="store_true" , help="If passed, will use FP16 training." ) parser.add_argument( "--mixed_precision" , type=__A , default=__A , choices=["no", "fp16", "bf16", "fp8"] , help="Whether to use mixed precision. Choose" "between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10." "and an Nvidia Ampere GPU." , ) parser.add_argument("--cpu" , action="store_true" , help="If passed, will train on the CPU." ) parser.add_argument( "--checkpointing_steps" , type=__A , default=__A , help="Whether the various states should be saved at the end of every n steps, or 'epoch' for each epoch." , ) parser.add_argument( "--output_dir" , type=__A , default="." , help="Optional save directory where all checkpoint folders will be stored. Default is the current working directory." , ) parser.add_argument( "--resume_from_checkpoint" , type=__A , default=__A , help="If the training should continue from a checkpoint folder." , ) parser.add_argument( "--with_tracking" , action="store_true" , help="Whether to load in all available experiment trackers from the environment and use them for logging." , ) parser.add_argument( "--project_dir" , type=__A , default="logs" , help="Location on where to store experiment tracking logs` and relevent project information" , ) UpperCAmelCase = parser.parse_args() UpperCAmelCase = {"lr": 3E-2, "num_epochs": 3, "seed": 42, "batch_size": 64, "image_size": 224} training_function(__A , __A ) if __name__ == "__main__": main()
1
1
import math import sys def _lowerCAmelCase( __A ): UpperCAmelCase = "" try: with open(__A , "rb" ) as binary_file: UpperCAmelCase = binary_file.read() for dat in data: UpperCAmelCase = F"{dat:08b}" result += curr_byte return result except OSError: print("File not accessible" ) sys.exit() def _lowerCAmelCase( __A ): UpperCAmelCase = {"0": "0", "1": "1"} UpperCAmelCase , UpperCAmelCase = "", "" UpperCAmelCase = len(__A ) for i in range(len(__A ) ): curr_string += data_bits[i] if curr_string not in lexicon: continue UpperCAmelCase = lexicon[curr_string] result += last_match_id UpperCAmelCase = last_match_id + "0" if math.loga(__A ).is_integer(): UpperCAmelCase = {} for curr_key in list(__A ): UpperCAmelCase = lexicon.pop(__A ) UpperCAmelCase = new_lex UpperCAmelCase = last_match_id + "1" index += 1 UpperCAmelCase = "" return result def _lowerCAmelCase( __A , __A ): UpperCAmelCase = 8 try: with open(__A , "wb" ) as opened_file: UpperCAmelCase = [ to_write[i : i + byte_length] for i in range(0 , len(__A ) , __A ) ] if len(result_byte_array[-1] ) % byte_length == 0: result_byte_array.append("10000000" ) else: result_byte_array[-1] += "1" + "0" * ( byte_length - len(result_byte_array[-1] ) - 1 ) for elem in result_byte_array[:-1]: opened_file.write(int(__A , 2 ).to_bytes(1 , byteorder="big" ) ) except OSError: print("File not accessible" ) sys.exit() def _lowerCAmelCase( __A ): UpperCAmelCase = 0 for letter in data_bits: if letter == "1": break counter += 1 UpperCAmelCase = data_bits[counter:] UpperCAmelCase = data_bits[counter + 1 :] return data_bits def _lowerCAmelCase( __A , __A ): UpperCAmelCase = read_file_binary(__A ) UpperCAmelCase = remove_prefix(__A ) UpperCAmelCase = decompress_data(__A ) write_file_binary(__A , __A ) if __name__ == "__main__": compress(sys.argv[1], sys.argv[2])
1
import glob import os import random from string import ascii_lowercase, digits import cva lowerCAmelCase__ = "" lowerCAmelCase__ = "" lowerCAmelCase__ = "" lowerCAmelCase__ = 1 # (0 is vertical, 1 is horizontal) def _lowerCAmelCase( ): UpperCAmelCase , UpperCAmelCase = get_dataset(__A , __A ) print("Processing..." ) UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = update_image_and_anno(__A , __A , __A ) for index, image in enumerate(__A ): # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' UpperCAmelCase = random_chars(32 ) UpperCAmelCase = paths[index].split(os.sep )[-1].rsplit("." , 1 )[0] UpperCAmelCase = F"{OUTPUT_DIR}/{file_name}_FLIP_{letter_code}" cva.imwrite(F"/{file_root}.jpg" , __A , [cva.IMWRITE_JPEG_QUALITY, 85] ) print(F"Success {index+1}/{len(__A )} with {file_name}" ) UpperCAmelCase = [] for anno in new_annos[index]: UpperCAmelCase = F"{anno[0]} {anno[1]} {anno[2]} {anno[3]} {anno[4]}" annos_list.append(__A ) with open(F"/{file_root}.txt" , "w" ) as outfile: outfile.write("\n".join(line for line in annos_list ) ) def _lowerCAmelCase( __A , __A ): UpperCAmelCase = [] UpperCAmelCase = [] for label_file in glob.glob(os.path.join(__A , "*.txt" ) ): UpperCAmelCase = label_file.split(os.sep )[-1].rsplit("." , 1 )[0] with open(__A ) as in_file: UpperCAmelCase = in_file.readlines() UpperCAmelCase = os.path.join(__A , F"{label_name}.jpg" ) UpperCAmelCase = [] for obj_list in obj_lists: UpperCAmelCase = obj_list.rstrip("\n" ).split(" " ) boxes.append( [ int(obj[0] ), float(obj[1] ), float(obj[2] ), float(obj[3] ), float(obj[4] ), ] ) if not boxes: continue img_paths.append(__A ) labels.append(__A ) return img_paths, labels def _lowerCAmelCase( __A , __A , __A = 1 ): UpperCAmelCase = [] UpperCAmelCase = [] UpperCAmelCase = [] for idx in range(len(__A ) ): UpperCAmelCase = [] UpperCAmelCase = img_list[idx] path_list.append(__A ) UpperCAmelCase = anno_list[idx] UpperCAmelCase = cva.imread(__A ) if flip_type == 1: UpperCAmelCase = cva.flip(__A , __A ) for bbox in img_annos: UpperCAmelCase = 1 - bbox[1] new_annos.append([bbox[0], x_center_new, bbox[2], bbox[3], bbox[4]] ) elif flip_type == 0: UpperCAmelCase = cva.flip(__A , __A ) for bbox in img_annos: UpperCAmelCase = 1 - bbox[2] new_annos.append([bbox[0], bbox[1], y_center_new, bbox[3], bbox[4]] ) new_annos_lists.append(__A ) new_imgs_list.append(__A ) return new_imgs_list, new_annos_lists, path_list def _lowerCAmelCase( __A = 32 ): assert number_char > 1, "The number of character should greater than 1" UpperCAmelCase = ascii_lowercase + digits return "".join(random.choice(__A ) for _ in range(__A ) ) if __name__ == "__main__": main() print("DONE ✅")
1
1
def _lowerCAmelCase( __A ): UpperCAmelCase = len(__A ) UpperCAmelCase = len(matrix[0] ) UpperCAmelCase = min(__A , __A ) for row in range(__A ): # Check if diagonal element is not zero if matrix[row][row] != 0: # Eliminate all the elements below the diagonal for col in range(row + 1 , __A ): UpperCAmelCase = matrix[col][row] / matrix[row][row] for i in range(__A , __A ): matrix[col][i] -= multiplier * matrix[row][i] else: # Find a non-zero diagonal element to swap rows UpperCAmelCase = True for i in range(row + 1 , __A ): if matrix[i][row] != 0: UpperCAmelCase , UpperCAmelCase = matrix[i], matrix[row] UpperCAmelCase = False break if reduce: rank -= 1 for i in range(__A ): UpperCAmelCase = matrix[i][rank] # Reduce the row pointer by one to stay on the same row row -= 1 return rank if __name__ == "__main__": import doctest doctest.testmod()
1
def _lowerCAmelCase( __A ): if not isinstance(__A , __A ): raise TypeError("only integers accepted as input" ) else: UpperCAmelCase = str(abs(__A ) ) UpperCAmelCase = [list(__A ) for char in range(len(__A ) )] for index in range(len(__A ) ): num_transpositions[index].pop(__A ) return max( int("".join(list(__A ) ) ) for transposition in num_transpositions ) if __name__ == "__main__": __import__("doctest").testmod()
1
1
import datetime import platform import subprocess from typing import Optional, Tuple, Union import numpy as np def _lowerCAmelCase( __A , __A ): UpperCAmelCase = F"{sampling_rate}" UpperCAmelCase = "1" UpperCAmelCase = "f32le" UpperCAmelCase = [ "ffmpeg", "-i", "pipe:0", "-ac", ac, "-ar", ar, "-f", format_for_conversion, "-hide_banner", "-loglevel", "quiet", "pipe:1", ] try: with subprocess.Popen(__A , stdin=subprocess.PIPE , stdout=subprocess.PIPE ) as ffmpeg_process: UpperCAmelCase = ffmpeg_process.communicate(__A ) except FileNotFoundError as error: raise ValueError("ffmpeg was not found but is required to load audio files from filename" ) from error UpperCAmelCase = output_stream[0] UpperCAmelCase = np.frombuffer(__A , np.floataa ) if audio.shape[0] == 0: raise ValueError("Malformed soundfile" ) return audio def _lowerCAmelCase( __A , __A , __A = "f32le" , ): UpperCAmelCase = F"{sampling_rate}" UpperCAmelCase = "1" if format_for_conversion == "s16le": UpperCAmelCase = 2 elif format_for_conversion == "f32le": UpperCAmelCase = 4 else: raise ValueError(F"Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`" ) UpperCAmelCase = platform.system() if system == "Linux": UpperCAmelCase = "alsa" UpperCAmelCase = "default" elif system == "Darwin": UpperCAmelCase = "avfoundation" UpperCAmelCase = ":0" elif system == "Windows": UpperCAmelCase = "dshow" UpperCAmelCase = "default" UpperCAmelCase = [ "ffmpeg", "-f", format_, "-i", input_, "-ac", ac, "-ar", ar, "-f", format_for_conversion, "-fflags", "nobuffer", "-hide_banner", "-loglevel", "quiet", "pipe:1", ] UpperCAmelCase = int(round(sampling_rate * chunk_length_s ) ) * size_of_sample UpperCAmelCase = _ffmpeg_stream(__A , __A ) for item in iterator: yield item def _lowerCAmelCase( __A , __A , __A = None , __A = None , __A = "f32le" , ): if stream_chunk_s is not None: UpperCAmelCase = stream_chunk_s else: UpperCAmelCase = chunk_length_s UpperCAmelCase = ffmpeg_microphone(__A , __A , format_for_conversion=__A ) if format_for_conversion == "s16le": UpperCAmelCase = np.intaa UpperCAmelCase = 2 elif format_for_conversion == "f32le": UpperCAmelCase = np.floataa UpperCAmelCase = 4 else: raise ValueError(F"Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`" ) if stride_length_s is None: UpperCAmelCase = chunk_length_s / 6 UpperCAmelCase = int(round(sampling_rate * chunk_length_s ) ) * size_of_sample if isinstance(__A , (int, float) ): UpperCAmelCase = [stride_length_s, stride_length_s] UpperCAmelCase = int(round(sampling_rate * stride_length_s[0] ) ) * size_of_sample UpperCAmelCase = int(round(sampling_rate * stride_length_s[1] ) ) * size_of_sample UpperCAmelCase = datetime.datetime.now() UpperCAmelCase = datetime.timedelta(seconds=__A ) for item in chunk_bytes_iter(__A , __A , stride=(stride_left, stride_right) , stream=__A ): # Put everything back in numpy scale UpperCAmelCase = np.frombuffer(item["raw"] , dtype=__A ) UpperCAmelCase = ( item["stride"][0] // size_of_sample, item["stride"][1] // size_of_sample, ) UpperCAmelCase = sampling_rate audio_time += delta if datetime.datetime.now() > audio_time + 10 * delta: # We're late !! SKIP continue yield item def _lowerCAmelCase( __A , __A , __A , __A = False ): UpperCAmelCase = B"" UpperCAmelCase , UpperCAmelCase = stride if stride_left + stride_right >= chunk_len: raise ValueError( F"Stride needs to be strictly smaller than chunk_len: ({stride_left}, {stride_right}) vs {chunk_len}" ) UpperCAmelCase = 0 for raw in iterator: acc += raw if stream and len(__A ) < chunk_len: UpperCAmelCase = (_stride_left, 0) yield {"raw": acc[:chunk_len], "stride": stride, "partial": True} else: while len(__A ) >= chunk_len: # We are flushing the accumulator UpperCAmelCase = (_stride_left, stride_right) UpperCAmelCase = {"raw": acc[:chunk_len], "stride": stride} if stream: UpperCAmelCase = False yield item UpperCAmelCase = stride_left UpperCAmelCase = acc[chunk_len - stride_left - stride_right :] # Last chunk if len(__A ) > stride_left: UpperCAmelCase = {"raw": acc, "stride": (_stride_left, 0)} if stream: UpperCAmelCase = False yield item def _lowerCAmelCase( __A , __A ): UpperCAmelCase = 2**24 # 16Mo try: with subprocess.Popen(__A , stdout=subprocess.PIPE , bufsize=__A ) as ffmpeg_process: while True: UpperCAmelCase = ffmpeg_process.stdout.read(__A ) if raw == b"": break yield raw except FileNotFoundError as error: raise ValueError("ffmpeg was not found but is required to stream audio files from filename" ) from error
1
import logging import re import pytorch_quantization import pytorch_quantization.nn as quant_nn import torch from pytorch_quantization import calib from pytorch_quantization.tensor_quant import QuantDescriptor lowerCAmelCase__ = logging.getLogger(__name__) lowerCAmelCase__ = 50 # max width of layer names lowerCAmelCase__ = 70 # max width of quantizer names def _lowerCAmelCase( __A ): UpperCAmelCase = parser.add_argument_group("quant_trainer arguments" ) group.add_argument("--wprec" , type=__A , default=8 , help="weight precision" ) group.add_argument("--aprec" , type=__A , default=8 , help="activation precision" ) group.add_argument("--quant-per-tensor" , action="store_true" , help="per tensor weight scaling" ) group.add_argument("--quant-disable" , action="store_true" , help="disable all quantizers" ) group.add_argument("--quant-disable-embeddings" , action="store_true" , help="disable all embeddings quantizers" ) group.add_argument("--quant-disable-keyword" , type=__A , nargs="+" , help="disable quantizers by keyword" ) group.add_argument("--quant-disable-layer-module" , type=__A , help="disable quantizers by keyword under layer." ) group.add_argument("--quant-enable-layer-module" , type=__A , help="enable quantizers by keyword under layer" ) group.add_argument("--calibrator" , default="max" , help="which quantization range calibrator to use" ) group.add_argument("--percentile" , default=__A , type=__A , help="percentile for PercentileCalibrator" ) group.add_argument("--fuse-qkv" , action="store_true" , help="use the same scale factor for qkv" ) group.add_argument("--clip-gelu" , metavar="N" , type=__A , help="clip gelu output maximum value to N" ) group.add_argument( "--recalibrate-weights" , action="store_true" , help=( "recalibrate weight amaxes by taking the max of the weights." " amaxes will be computed with the current quantization granularity (axis)." ) , ) def _lowerCAmelCase( __A ): if args.calibrator == "max": UpperCAmelCase = "max" elif args.calibrator == "percentile": if args.percentile is None: raise ValueError("Specify --percentile when using percentile calibrator" ) UpperCAmelCase = "histogram" elif args.calibrator == "mse": UpperCAmelCase = "histogram" else: raise ValueError(F"Invalid calibrator {args.calibrator}" ) UpperCAmelCase = QuantDescriptor(num_bits=args.aprec , calib_method=__A ) UpperCAmelCase = QuantDescriptor(num_bits=args.wprec , axis=(None if args.quant_per_tensor else (0,)) ) quant_nn.QuantLinear.set_default_quant_desc_input(__A ) quant_nn.QuantLinear.set_default_quant_desc_weight(__A ) def _lowerCAmelCase( __A , __A , __A=False , __A=False ): logger.info("Configuring Model for Quantization" ) logger.info(F"using quantization package {pytorch_quantization.__file__}" ) if not calib: if args.quant_disable_embeddings: set_quantizer_by_name(__A , ["embeddings"] , which="weight" , _disabled=__A ) if args.quant_disable: set_quantizer_by_name(__A , [""] , _disabled=__A ) if args.quant_disable_keyword: set_quantizer_by_name(__A , args.quant_disable_keyword , _disabled=__A ) if args.quant_disable_layer_module: set_quantizer_by_name(__A , [r"layer.\d+." + args.quant_disable_layer_module] , _disabled=__A ) if args.quant_enable_layer_module: set_quantizer_by_name(__A , [r"layer.\d+." + args.quant_enable_layer_module] , _disabled=__A ) if args.recalibrate_weights: recalibrate_weights(__A ) if args.fuse_qkv: fuse_qkv(__A , __A ) if args.clip_gelu: clip_gelu(__A , args.clip_gelu ) # if args.local_rank in [-1, 0] and not calib: print_quant_summary(__A ) def _lowerCAmelCase( __A ): logger.info("Enabling Calibration" ) for name, module in model.named_modules(): if name.endswith("_quantizer" ): if module._calibrator is not None: module.disable_quant() module.enable_calib() else: module.disable() logger.info(F"{name:80}: {module}" ) def _lowerCAmelCase( __A , __A ): logger.info("Loading calibrated amax" ) for name, module in model.named_modules(): if name.endswith("_quantizer" ): if module._calibrator is not None: if isinstance(module._calibrator , calib.MaxCalibrator ): module.load_calib_amax() else: module.load_calib_amax("percentile" , percentile=args.percentile ) module.enable_quant() module.disable_calib() else: module.enable() model.cuda() print_quant_summary(__A ) def _lowerCAmelCase( __A , __A ): def fusea(__A , __A , __A ): for mod in [qq, qk, qv]: if not hasattr(__A , "_amax" ): print(" WARNING: NO AMAX BUFFER" ) return UpperCAmelCase = qq._amax.detach().item() UpperCAmelCase = qk._amax.detach().item() UpperCAmelCase = qv._amax.detach().item() UpperCAmelCase = max(__A , __A , __A ) qq._amax.fill_(__A ) qk._amax.fill_(__A ) qv._amax.fill_(__A ) logger.info(F" q={q:5.2f} k={k:5.2f} v={v:5.2f} -> {amax:5.2f}" ) for name, mod in model.named_modules(): if name.endswith(".attention.self" ): logger.info(F"FUSE_QKV: {name:{name_width}}" ) fusea(mod.matmul_q_input_quantizer , mod.matmul_k_input_quantizer , mod.matmul_v_input_quantizer ) if args.quant_per_tensor: fusea(mod.query._weight_quantizer , mod.key._weight_quantizer , mod.value._weight_quantizer ) def _lowerCAmelCase( __A , __A ): for name, mod in model.named_modules(): if name.endswith(".output.dense" ) and not name.endswith("attention.output.dense" ): UpperCAmelCase = mod._input_quantizer._amax.data.detach().item() mod._input_quantizer._amax.data.detach().clamp_(max=__A ) UpperCAmelCase = mod._input_quantizer._amax.data.detach().item() logger.info(F"CLIP_GELU: {name:{name_width}} amax: {amax_init:5.2f} -> {amax:5.2f}" ) def _lowerCAmelCase( __A ): for name, mod in model.named_modules(): if hasattr(__A , "_weight_quantizer" ) and mod._weight_quantizer.axis is not None: UpperCAmelCase = mod.weight.shape[0] UpperCAmelCase = mod._weight_quantizer._amax.detach() UpperCAmelCase = torch.ones(__A , dtype=amax.dtype , device=amax.device ) * amax print(F"expanding {name} {amax} -> {mod._weight_quantizer._amax}" ) def _lowerCAmelCase( __A ): for name, mod in model.named_modules(): if hasattr(__A , "_weight_quantizer" ): if not hasattr(mod.weight_quantizer , "_amax" ): print("RECALIB: {name:{name_width}} WARNING: NO AMAX BUFFER" ) continue # determine which axes to reduce across # e.g. a 4D tensor quantized per axis 0 should reduce over (1,2,3) UpperCAmelCase = set() if mod._weight_quantizer.axis is None else set(mod._weight_quantizer.axis ) UpperCAmelCase = set(range(len(mod.weight.size() ) ) ) - axis_set UpperCAmelCase = pytorch_quantization.utils.reduce_amax(mod.weight , axis=__A , keepdims=__A ).detach() logger.info(F"RECALIB: {name:{name_width}} {mod._weight_quantizer._amax.flatten()} -> {amax.flatten()}" ) UpperCAmelCase = amax def _lowerCAmelCase( __A , __A=25 , __A=180 , __A=None ): if ignore is None: UpperCAmelCase = [] elif not isinstance(__A , __A ): UpperCAmelCase = [ignore] UpperCAmelCase = 0 for name, mod in model.named_modules(): if not hasattr(__A , "weight" ): continue UpperCAmelCase = max(__A , len(__A ) ) for name, mod in model.named_modules(): UpperCAmelCase = getattr(__A , "_input_quantizer" , __A ) UpperCAmelCase = getattr(__A , "_weight_quantizer" , __A ) if not hasattr(__A , "weight" ): continue if type(__A ) in ignore: continue if [True for s in ignore if type(__A ) is str and s in name]: continue UpperCAmelCase = F"Act:{input_q.extra_repr()}" UpperCAmelCase = F"Wgt:{weight_q.extra_repr()}" UpperCAmelCase = F"{name:{name_width}} {act_str} {wgt_str}" if len(__A ) <= line_width: logger.info(__A ) else: logger.info(F"{name:{name_width}} {act_str}" ) logger.info(F"{' ':{name_width}} {wgt_str}" ) def _lowerCAmelCase( __A ): UpperCAmelCase = 0 for name, mod in model.named_modules(): if isinstance(__A , pytorch_quantization.nn.TensorQuantizer ): print(F"{name:80} {mod}" ) count += 1 print(F"{count} TensorQuantizers found in model" ) def _lowerCAmelCase( __A , __A , __A , __A , __A ): UpperCAmelCase = getattr(__A , __A , __A ) if quantizer_mod is not None: assert hasattr(__A , __A ) setattr(__A , __A , __A ) else: logger.warning(F"{name} has no {quantizer}" ) def _lowerCAmelCase( __A , __A , __A="both" , **__A ): UpperCAmelCase = F"Warning: changing {which} quantizers of {name:{qname_width}}" for k, v in kwargs.items(): s += F" {k}={v}" if which in ["input", "both"]: set_quantizer(__A , __A , "_input_quantizer" , __A , __A ) if which in ["weight", "both"]: set_quantizer(__A , __A , "_weight_quantizer" , __A , __A ) logger.info(__A ) def _lowerCAmelCase( __A , __A , **__A ): for name, mod in model.named_modules(): if hasattr(__A , "_input_quantizer" ) or hasattr(__A , "_weight_quantizer" ): for n in names: if re.search(__A , __A ): set_quantizers(__A , __A , **__A ) elif name.endswith("_quantizer" ): for n in names: if re.search(__A , __A ): UpperCAmelCase = F"Warning: changing {name:{name_width}}" for k, v in kwargs.items(): s += F" {k}={v}" setattr(__A , __A , __A ) logger.info(__A )
1
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available, is_vision_available, ) lowerCAmelCase__ = {"configuration_beit": ["BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP", "BeitConfig", "BeitOnnxConfig"]} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ["BeitFeatureExtractor"] lowerCAmelCase__ = ["BeitImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ "BEIT_PRETRAINED_MODEL_ARCHIVE_LIST", "BeitForImageClassification", "BeitForMaskedImageModeling", "BeitForSemanticSegmentation", "BeitModel", "BeitPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ "FlaxBeitForImageClassification", "FlaxBeitForMaskedImageModeling", "FlaxBeitModel", "FlaxBeitPreTrainedModel", ] if TYPE_CHECKING: from .configuration_beit import BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP, BeitConfig, BeitOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_beit import BeitFeatureExtractor from .image_processing_beit import BeitImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_beit import ( BEIT_PRETRAINED_MODEL_ARCHIVE_LIST, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, BeitPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_beit import ( FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling, FlaxBeitModel, FlaxBeitPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
1
def _lowerCAmelCase( __A ): assert column_title.isupper() UpperCAmelCase = 0 UpperCAmelCase = len(__A ) - 1 UpperCAmelCase = 0 while index >= 0: UpperCAmelCase = (ord(column_title[index] ) - 64) * pow(26 , __A ) answer += value power += 1 index -= 1 return answer if __name__ == "__main__": from doctest import testmod testmod()
1
1
from typing import Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature from ...image_transforms import get_image_size, pad, rescale, to_channel_dimension_format from ...image_utils import ChannelDimension, ImageInput, make_list_of_images, to_numpy_array, valid_images from ...utils import TensorType, logging lowerCAmelCase__ = logging.get_logger(__name__) class __magic_name__ ( _snake_case ): UpperCAmelCase = ["""pixel_values"""] def __init__( self : Optional[Any] , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : Union[int, float] = 1 / 2_5_5 , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : int = 8 , **lowerCAmelCase__ : Optional[Any] , ) -> None: super().__init__(**lowerCAmelCase__ ) UpperCAmelCase = do_rescale UpperCAmelCase = rescale_factor UpperCAmelCase = do_pad UpperCAmelCase = pad_size def _UpperCamelCase ( self : Dict , lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : float , lowerCAmelCase__ : Optional[Union[str, ChannelDimension]] = None , **lowerCAmelCase__ : int ) -> np.ndarray: return rescale(lowerCAmelCase__ , scale=lowerCAmelCase__ , data_format=lowerCAmelCase__ , **lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : int , lowerCAmelCase__ : Optional[Union[str, ChannelDimension]] = None ) -> Dict: UpperCAmelCase , UpperCAmelCase = get_image_size(lowerCAmelCase__ ) UpperCAmelCase = (old_height // size + 1) * size - old_height UpperCAmelCase = (old_width // size + 1) * size - old_width return pad(lowerCAmelCase__ , ((0, pad_height), (0, pad_width)) , mode="symmetric" , data_format=lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : ImageInput , lowerCAmelCase__ : Optional[bool] = None , lowerCAmelCase__ : Optional[float] = None , lowerCAmelCase__ : Optional[bool] = None , lowerCAmelCase__ : Optional[int] = None , lowerCAmelCase__ : Optional[Union[str, TensorType]] = None , lowerCAmelCase__ : Union[str, ChannelDimension] = ChannelDimension.FIRST , **lowerCAmelCase__ : Union[str, Any] , ) -> str: UpperCAmelCase = do_rescale if do_rescale is not None else self.do_rescale UpperCAmelCase = rescale_factor if rescale_factor is not None else self.rescale_factor UpperCAmelCase = do_pad if do_pad is not None else self.do_pad UpperCAmelCase = pad_size if pad_size is not None else self.pad_size UpperCAmelCase = make_list_of_images(lowerCAmelCase__ ) if not valid_images(lowerCAmelCase__ ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) # All transformations expect numpy arrays. UpperCAmelCase = [to_numpy_array(lowerCAmelCase__ ) for image in images] if do_rescale: UpperCAmelCase = [self.rescale(image=lowerCAmelCase__ , scale=lowerCAmelCase__ ) for image in images] if do_pad: UpperCAmelCase = [self.pad(lowerCAmelCase__ , size=lowerCAmelCase__ ) for image in images] UpperCAmelCase = [to_channel_dimension_format(lowerCAmelCase__ , lowerCAmelCase__ ) for image in images] UpperCAmelCase = {"pixel_values": images} return BatchFeature(data=lowerCAmelCase__ , tensor_type=lowerCAmelCase__ )
1
import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test sys.path.append(str(Path(__file__).parent.parent / "utils")) from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 lowerCAmelCase__ = get_tests_dir("fixtures") class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : Tuple ) -> Union[str, Any]: # A mock response for an HTTP head request to emulate server down UpperCAmelCase = mock.Mock() UpperCAmelCase = 5_0_0 UpperCAmelCase = {} UpperCAmelCase = HTTPError UpperCAmelCase = {} # Download this model to make sure it's in the cache. UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2" ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch("requests.Session.request" , return_value=lowerCAmelCase__ ) as mock_head: UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2" ) # This check we did call the fake head request mock_head.assert_called() def _UpperCamelCase ( self : List[Any] ) -> Dict: # This test is for deprecated behavior and can be removed in v5 UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained( "https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json" ) @is_staging_test class __magic_name__ ( unittest.TestCase ): @classmethod def _UpperCamelCase ( cls : List[str] ) -> List[Any]: UpperCAmelCase = TOKEN HfFolder.save_token(lowerCAmelCase__ ) @classmethod def _UpperCamelCase ( cls : Optional[int] ) -> Union[str, Any]: try: delete_repo(token=cls._token , repo_id="test-feature-extractor" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="valid_org/test-feature-extractor-org" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="test-dynamic-feature-extractor" ) except HTTPError: pass def _UpperCamelCase ( self : Any ) -> Any: UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(lowerCAmelCase__ ) feature_extractor.push_to_hub("test-feature-extractor" , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) # Reset repo delete_repo(token=self._token , repo_id="test-feature-extractor" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( lowerCAmelCase__ , repo_id="test-feature-extractor" , push_to_hub=lowerCAmelCase__ , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) def _UpperCamelCase ( self : List[Any] ) -> Tuple: UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(lowerCAmelCase__ ) feature_extractor.push_to_hub("valid_org/test-feature-extractor" , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("valid_org/test-feature-extractor" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) # Reset repo delete_repo(token=self._token , repo_id="valid_org/test-feature-extractor" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( lowerCAmelCase__ , repo_id="valid_org/test-feature-extractor-org" , push_to_hub=lowerCAmelCase__ , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("valid_org/test-feature-extractor-org" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) def _UpperCamelCase ( self : Dict ) -> List[str]: CustomFeatureExtractor.register_for_auto_class() UpperCAmelCase = CustomFeatureExtractor.from_pretrained(lowerCAmelCase__ ) feature_extractor.push_to_hub("test-dynamic-feature-extractor" , use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual( feature_extractor.auto_map , {"AutoFeatureExtractor": "custom_feature_extraction.CustomFeatureExtractor"} , ) UpperCAmelCase = AutoFeatureExtractor.from_pretrained( f"{USER}/test-dynamic-feature-extractor" , trust_remote_code=lowerCAmelCase__ ) # Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module self.assertEqual(new_feature_extractor.__class__.__name__ , "CustomFeatureExtractor" )
1
1
import baseaa def _lowerCAmelCase( __A ): return baseaa.aaaencode(string.encode("utf-8" ) ) def _lowerCAmelCase( __A ): return baseaa.aaadecode(__A ).decode("utf-8" ) if __name__ == "__main__": import doctest doctest.testmod()
1
import argparse import os import re # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_dummies.py lowerCAmelCase__ = "src/diffusers" # Matches is_xxx_available() lowerCAmelCase__ = re.compile(r"is\_([a-z_]*)_available\(\)") # Matches from xxx import bla lowerCAmelCase__ = re.compile(r"\s+from\s+\S*\s+import\s+([^\(\s].*)\n") lowerCAmelCase__ = "\n{0} = None\n" lowerCAmelCase__ = "\nclass {0}(metaclass=DummyObject):\n _backends = {1}\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, {1})\n\n @classmethod\n def from_config(cls, *args, **kwargs):\n requires_backends(cls, {1})\n\n @classmethod\n def from_pretrained(cls, *args, **kwargs):\n requires_backends(cls, {1})\n" lowerCAmelCase__ = "\ndef {0}(*args, **kwargs):\n requires_backends({0}, {1})\n" def _lowerCAmelCase( __A ): UpperCAmelCase = _re_backend.findall(__A ) if len(__A ) == 0: return None return "_and_".join(__A ) def _lowerCAmelCase( ): with open(os.path.join(__A , "__init__.py" ) , "r" , encoding="utf-8" , newline="\n" ) as f: UpperCAmelCase = f.readlines() # Get to the point we do the actual imports for type checking UpperCAmelCase = 0 UpperCAmelCase = {} # Go through the end of the file while line_index < len(__A ): # If the line contains is_backend_available, we grab all objects associated with the `else` block UpperCAmelCase = find_backend(lines[line_index] ) if backend is not None: while not lines[line_index].startswith("else:" ): line_index += 1 line_index += 1 UpperCAmelCase = [] # Until we unindent, add backend objects to the list while line_index < len(__A ) and len(lines[line_index] ) > 1: UpperCAmelCase = lines[line_index] UpperCAmelCase = _re_single_line_import.search(__A ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(", " ) ) elif line.startswith(" " * 8 ): objects.append(line[8:-2] ) line_index += 1 if len(__A ) > 0: UpperCAmelCase = objects else: line_index += 1 return backend_specific_objects def _lowerCAmelCase( __A , __A ): if name.isupper(): return DUMMY_CONSTANT.format(__A ) elif name.islower(): return DUMMY_FUNCTION.format(__A , __A ) else: return DUMMY_CLASS.format(__A , __A ) def _lowerCAmelCase( __A=None ): if backend_specific_objects is None: UpperCAmelCase = read_init() # For special correspondence backend to module name as used in the function requires_modulename UpperCAmelCase = {} for backend, objects in backend_specific_objects.items(): UpperCAmelCase = "[" + ", ".join(F"\"{b}\"" for b in backend.split("_and_" ) ) + "]" UpperCAmelCase = "# This file is autogenerated by the command `make fix-copies`, do not edit.\n" dummy_file += "from ..utils import DummyObject, requires_backends\n\n" dummy_file += "\n".join([create_dummy_object(__A , __A ) for o in objects] ) UpperCAmelCase = dummy_file return dummy_files def _lowerCAmelCase( __A=False ): UpperCAmelCase = create_dummy_files() # For special correspondence backend to shortcut as used in utils/dummy_xxx_objects.py UpperCAmelCase = {"torch": "pt"} # Locate actual dummy modules and read their content. UpperCAmelCase = os.path.join(__A , "utils" ) UpperCAmelCase = { backend: os.path.join(__A , F"dummy_{short_names.get(__A , __A )}_objects.py" ) for backend in dummy_files.keys() } UpperCAmelCase = {} for backend, file_path in dummy_file_paths.items(): if os.path.isfile(__A ): with open(__A , "r" , encoding="utf-8" , newline="\n" ) as f: UpperCAmelCase = f.read() else: UpperCAmelCase = "" for backend in dummy_files.keys(): if dummy_files[backend] != actual_dummies[backend]: if overwrite: print( F"Updating diffusers.utils.dummy_{short_names.get(__A , __A )}_objects.py as the main " "__init__ has new objects." ) with open(dummy_file_paths[backend] , "w" , encoding="utf-8" , newline="\n" ) as f: f.write(dummy_files[backend] ) else: raise ValueError( "The main __init__ has objects that are not present in " F"diffusers.utils.dummy_{short_names.get(__A , __A )}_objects.py. Run `make fix-copies` " "to fix this." ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.") lowerCAmelCase__ = parser.parse_args() check_dummies(args.fix_and_overwrite)
1
1
import argparse import gc import json import os import re import torch from huggingface_hub import hf_hub_download from transformers import AutoModelForCausalLM, AutoTokenizer, PreTrainedTokenizerFast, RwkvConfig from transformers.modeling_utils import WEIGHTS_INDEX_NAME, shard_checkpoint lowerCAmelCase__ = { "169M": 12, "430M": 24, "1B5": 24, "3B": 32, "7B": 32, "14B": 40, } lowerCAmelCase__ = { "169M": 768, "430M": 1024, "1B5": 2048, "3B": 2560, "7B": 4096, "14B": 5120, } def _lowerCAmelCase( __A ): UpperCAmelCase = list(state_dict.keys() ) for name in state_dict_keys: UpperCAmelCase = state_dict.pop(__A ) # emb -> embedding if name.startswith("emb." ): UpperCAmelCase = name.replace("emb." , "embeddings." ) # ln_0 -> pre_ln (only present at block 0) if name.startswith("blocks.0.ln0" ): UpperCAmelCase = name.replace("blocks.0.ln0" , "blocks.0.pre_ln" ) # att -> attention UpperCAmelCase = re.sub(r"blocks\.(\d+)\.att" , r"blocks.\1.attention" , __A ) # ffn -> feed_forward UpperCAmelCase = re.sub(r"blocks\.(\d+)\.ffn" , r"blocks.\1.feed_forward" , __A ) # time_mix_k -> time_mix_key and reshape if name.endswith(".time_mix_k" ): UpperCAmelCase = name.replace(".time_mix_k" , ".time_mix_key" ) # time_mix_v -> time_mix_value and reshape if name.endswith(".time_mix_v" ): UpperCAmelCase = name.replace(".time_mix_v" , ".time_mix_value" ) # time_mix_r -> time_mix_key and reshape if name.endswith(".time_mix_r" ): UpperCAmelCase = name.replace(".time_mix_r" , ".time_mix_receptance" ) if name != "head.weight": UpperCAmelCase = "rwkv." + name UpperCAmelCase = weight return state_dict def _lowerCAmelCase( __A , __A , __A , __A=None , __A=None , __A=False , __A=None ): # 1. If possible, build the tokenizer. if tokenizer_file is None: print("No `--tokenizer_file` provided, we will use the default tokenizer." ) UpperCAmelCase = 50277 UpperCAmelCase = AutoTokenizer.from_pretrained("EleutherAI/gpt-neox-20b" ) else: UpperCAmelCase = PreTrainedTokenizerFast(tokenizer_file=__A ) UpperCAmelCase = len(__A ) tokenizer.save_pretrained(__A ) # 2. Build the config UpperCAmelCase = list(NUM_HIDDEN_LAYERS_MAPPING.keys() ) if size is None: # Try to infer size from the checkpoint name for candidate in possible_sizes: if candidate in checkpoint_file: UpperCAmelCase = candidate break if size is None: raise ValueError("Could not infer the size, please provide it with the `--size` argument." ) if size not in possible_sizes: raise ValueError(F"`size` should be one of {possible_sizes}, got {size}." ) UpperCAmelCase = RwkvConfig( vocab_size=__A , num_hidden_layers=NUM_HIDDEN_LAYERS_MAPPING[size] , hidden_size=HIDEN_SIZE_MAPPING[size] , ) config.save_pretrained(__A ) # 3. Download model file then convert state_dict UpperCAmelCase = hf_hub_download(__A , __A ) UpperCAmelCase = torch.load(__A , map_location="cpu" ) UpperCAmelCase = convert_state_dict(__A ) # 4. Split in shards and save UpperCAmelCase , UpperCAmelCase = shard_checkpoint(__A ) for shard_file, shard in shards.items(): torch.save(__A , os.path.join(__A , __A ) ) if index is not None: UpperCAmelCase = os.path.join(__A , __A ) # Save the index as well with open(__A , "w" , encoding="utf-8" ) as f: UpperCAmelCase = json.dumps(__A , indent=2 , sort_keys=__A ) + "\n" f.write(__A ) # 5. Clean up shards (for some reason the file PyTorch saves take the same space as the whole state_dict print( "Cleaning up shards. This may error with an OOM error, it this is the case don't worry you still have converted the model." ) UpperCAmelCase = list(shards.keys() ) del state_dict del shards gc.collect() for shard_file in shard_files: UpperCAmelCase = torch.load(os.path.join(__A , __A ) ) torch.save({k: v.cpu().clone() for k, v in state_dict.items()} , os.path.join(__A , __A ) ) del state_dict gc.collect() if push_to_hub: if model_name is None: raise ValueError("Please provide a `model_name` to push the model to the Hub." ) UpperCAmelCase = AutoModelForCausalLM.from_pretrained(__A ) model.push_to_hub(__A , max_shard_size="2GB" ) tokenizer.push_to_hub(__A ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--repo_id", default=None, type=str, required=True, help="Repo ID from which to pull the checkpoint." ) parser.add_argument( "--checkpoint_file", default=None, type=str, required=True, help="Name of the checkpoint file in the repo." ) parser.add_argument( "--output_dir", default=None, type=str, required=True, help="Where to save the converted model." ) parser.add_argument( "--tokenizer_file", default=None, type=str, help="Path to the tokenizer file to use (if not provided, only the model is converted).", ) parser.add_argument( "--size", default=None, type=str, help="Size of the model. Will be inferred from the `checkpoint_file` if not passed.", ) parser.add_argument( "--push_to_hub", action="store_true", help="Push to the Hub the converted model.", ) parser.add_argument( "--model_name", default=None, type=str, help="Name of the pushed model on the Hub, including the username / organization.", ) lowerCAmelCase__ = parser.parse_args() convert_rmkv_checkpoint_to_hf_format( args.repo_id, args.checkpoint_file, args.output_dir, size=args.size, tokenizer_file=args.tokenizer_file, push_to_hub=args.push_to_hub, model_name=args.model_name, )
1
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { "facebook/convnextv2-tiny-1k-224": "https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json", } class __magic_name__ ( _snake_case , _snake_case ): UpperCAmelCase = """convnextv2""" def __init__( self : Optional[Any] , lowerCAmelCase__ : List[Any]=3 , lowerCAmelCase__ : str=4 , lowerCAmelCase__ : Dict=4 , lowerCAmelCase__ : Optional[Any]=None , lowerCAmelCase__ : Optional[Any]=None , lowerCAmelCase__ : str="gelu" , lowerCAmelCase__ : Optional[int]=0.02 , lowerCAmelCase__ : Dict=1e-1_2 , lowerCAmelCase__ : str=0.0 , lowerCAmelCase__ : str=2_2_4 , lowerCAmelCase__ : int=None , lowerCAmelCase__ : List[Any]=None , **lowerCAmelCase__ : List[Any] , ) -> List[Any]: super().__init__(**lowerCAmelCase__ ) UpperCAmelCase = num_channels UpperCAmelCase = patch_size UpperCAmelCase = num_stages UpperCAmelCase = [9_6, 1_9_2, 3_8_4, 7_6_8] if hidden_sizes is None else hidden_sizes UpperCAmelCase = [3, 3, 9, 3] if depths is None else depths UpperCAmelCase = hidden_act UpperCAmelCase = initializer_range UpperCAmelCase = layer_norm_eps UpperCAmelCase = drop_path_rate UpperCAmelCase = image_size UpperCAmelCase = ["stem"] + [f"stage{idx}" for idx in range(1 , len(self.depths ) + 1 )] UpperCAmelCase , UpperCAmelCase = get_aligned_output_features_output_indices( out_features=lowerCAmelCase__ , out_indices=lowerCAmelCase__ , stage_names=self.stage_names )
1
1
class __magic_name__ : def __init__( self : Optional[Any] , lowerCAmelCase__ : Union[str, Any] ) -> List[str]: UpperCAmelCase = val UpperCAmelCase = None UpperCAmelCase = None def _UpperCamelCase ( self : Optional[int] , lowerCAmelCase__ : List[str] ) -> str: if self.val: if val < self.val: if self.left is None: UpperCAmelCase = Node(lowerCAmelCase__ ) else: self.left.insert(lowerCAmelCase__ ) elif val > self.val: if self.right is None: UpperCAmelCase = Node(lowerCAmelCase__ ) else: self.right.insert(lowerCAmelCase__ ) else: UpperCAmelCase = val def _lowerCAmelCase( __A , __A ): # Recursive traversal if root: inorder(root.left , __A ) res.append(root.val ) inorder(root.right , __A ) def _lowerCAmelCase( __A ): # Build BST if len(__A ) == 0: return arr UpperCAmelCase = Node(arr[0] ) for i in range(1 , len(__A ) ): root.insert(arr[i] ) # Traverse BST in order. UpperCAmelCase = [] inorder(__A , __A ) return res if __name__ == "__main__": print(tree_sort([10, 1, 3, 2, 9, 14, 13]))
1
lowerCAmelCase__ = "\n# Transformers 설치 방법\n! pip install transformers datasets\n# 마지막 릴리스 대신 소스에서 설치하려면, 위 명령을 주석으로 바꾸고 아래 명령을 해제하세요.\n# ! pip install git+https://github.com/huggingface/transformers.git\n" lowerCAmelCase__ = [{"type": "code", "content": INSTALL_CONTENT}] lowerCAmelCase__ = { "{processor_class}": "FakeProcessorClass", "{model_class}": "FakeModelClass", "{object_class}": "FakeObjectClass", }
1
1
import ast import os import re import shutil import tempfile import unittest from unittest import mock import torch from accelerate.test_utils.examples import compare_against_test from accelerate.test_utils.testing import TempDirTestCase, require_trackers, run_command, slow from accelerate.utils import write_basic_config # DataLoaders built from `test_samples/MRPC` for quick testing # Should mock `{script_name}.get_dataloaders` via: # @mock.patch("{script_name}.get_dataloaders", mocked_dataloaders) lowerCAmelCase__ = [ "cross_validation.py", "gradient_accumulation.py", "local_sgd.py", "multi_process_metrics.py", "memory.py", "automatic_gradient_accumulation.py", "fsdp_with_peak_mem_tracking.py", "deepspeed_with_config_support.py", "megatron_lm_gpt_pretraining.py", ] class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : bool , lowerCAmelCase__ : str = None , lowerCAmelCase__ : list = None ) -> List[Any]: UpperCAmelCase = None UpperCAmelCase = os.path.abspath(os.path.join("examples" , "by_feature" ) ) UpperCAmelCase = os.path.abspath("examples" ) for item in os.listdir(lowerCAmelCase__ ): if item not in EXCLUDE_EXAMPLES: UpperCAmelCase = os.path.join(lowerCAmelCase__ , lowerCAmelCase__ ) if os.path.isfile(lowerCAmelCase__ ) and ".py" in item_path: with self.subTest( tested_script=lowerCAmelCase__ , feature_script=lowerCAmelCase__ , tested_section="main()" if parser_only else "training_function()" , ): UpperCAmelCase = compare_against_test( os.path.join(lowerCAmelCase__ , lowerCAmelCase__ ) , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase = "\n".join(lowerCAmelCase__ ) if special_strings is not None: for string in special_strings: UpperCAmelCase = diff.replace(lowerCAmelCase__ , "" ) self.assertEqual(lowerCAmelCase__ , "" ) def _UpperCamelCase ( self : str ) -> Dict: self.one_complete_example("complete_nlp_example.py" , lowerCAmelCase__ ) self.one_complete_example("complete_nlp_example.py" , lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] ) -> Optional[int]: UpperCAmelCase = os.path.abspath(os.path.join("examples" , "cv_example.py" ) ) UpperCAmelCase = [ " " * 1_6 + "{\n\n", " " * 2_0 + "\"accuracy\": eval_metric[\"accuracy\"],\n\n", " " * 2_0 + "\"f1\": eval_metric[\"f1\"],\n\n", " " * 2_0 + "\"train_loss\": total_loss.item() / len(train_dataloader),\n\n", " " * 2_0 + "\"epoch\": epoch,\n\n", " " * 1_6 + "},\n\n", " " * 1_6 + "step=epoch,\n", " " * 1_2, " " * 8 + "for step, batch in enumerate(active_dataloader):\n", ] self.one_complete_example("complete_cv_example.py" , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) self.one_complete_example("complete_cv_example.py" , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) @mock.patch.dict(os.environ , {"""TESTING_MOCKED_DATALOADERS""": """1"""} ) class __magic_name__ ( _snake_case ): UpperCAmelCase = False @classmethod def _UpperCamelCase ( cls : int ) -> Optional[Any]: super().setUpClass() UpperCAmelCase = tempfile.mkdtemp() UpperCAmelCase = os.path.join(cls._tmpdir , "default_config.yml" ) write_basic_config(save_location=cls.configPath ) UpperCAmelCase = ["accelerate", "launch", "--config_file", cls.configPath] @classmethod def _UpperCamelCase ( cls : Optional[Any] ) -> str: super().tearDownClass() shutil.rmtree(cls._tmpdir ) def _UpperCamelCase ( self : List[Any] ) -> Optional[Any]: UpperCAmelCase = f"\n examples/by_feature/checkpointing.py\n --checkpointing_steps epoch\n --output_dir {self.tmpdir}\n ".split() run_command(self._launch_args + testargs ) self.assertTrue(os.path.exists(os.path.join(self.tmpdir , "epoch_0" ) ) ) def _UpperCamelCase ( self : str ) -> Optional[int]: UpperCAmelCase = f"\n examples/by_feature/checkpointing.py\n --checkpointing_steps 1\n --output_dir {self.tmpdir}\n ".split() UpperCAmelCase = run_command(self._launch_args + testargs ) self.assertTrue(os.path.exists(os.path.join(self.tmpdir , "step_2" ) ) ) def _UpperCamelCase ( self : str ) -> Optional[int]: UpperCAmelCase = f"\n examples/by_feature/checkpointing.py\n --resume_from_checkpoint {os.path.join(self.tmpdir , 'epoch_0' )}\n ".split() UpperCAmelCase = run_command(self._launch_args + testargs , return_stdout=lowerCAmelCase__ ) self.assertNotIn("epoch 0:" , lowerCAmelCase__ ) self.assertIn("epoch 1:" , lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] ) -> Optional[int]: UpperCAmelCase = f"\n examples/by_feature/checkpointing.py\n --resume_from_checkpoint {os.path.join(self.tmpdir , 'step_2' )}\n ".split() UpperCAmelCase = run_command(self._launch_args + testargs , return_stdout=lowerCAmelCase__ ) if torch.cuda.is_available(): UpperCAmelCase = torch.cuda.device_count() else: UpperCAmelCase = 1 if num_processes > 1: self.assertNotIn("epoch 0:" , lowerCAmelCase__ ) self.assertIn("epoch 1:" , lowerCAmelCase__ ) else: self.assertIn("epoch 0:" , lowerCAmelCase__ ) self.assertIn("epoch 1:" , lowerCAmelCase__ ) @slow def _UpperCamelCase ( self : str ) -> Dict: UpperCAmelCase = "\n examples/by_feature/cross_validation.py\n --num_folds 2\n ".split() with mock.patch.dict(os.environ , {"TESTING_MOCKED_DATALOADERS": "0"} ): UpperCAmelCase = run_command(self._launch_args + testargs , return_stdout=lowerCAmelCase__ ) UpperCAmelCase = re.findall("({.+})" , lowerCAmelCase__ ) UpperCAmelCase = [r for r in results if "accuracy" in r][-1] UpperCAmelCase = ast.literal_eval(lowerCAmelCase__ ) self.assertGreaterEqual(results["accuracy"] , 0.75 ) def _UpperCamelCase ( self : List[str] ) -> Any: UpperCAmelCase = ["examples/by_feature/multi_process_metrics.py"] run_command(self._launch_args + testargs ) @require_trackers @mock.patch.dict(os.environ , {"WANDB_MODE": "offline"} ) def _UpperCamelCase ( self : Tuple ) -> Union[str, Any]: with tempfile.TemporaryDirectory() as tmpdir: UpperCAmelCase = f"\n examples/by_feature/tracking.py\n --with_tracking\n --project_dir {tmpdir}\n ".split() run_command(self._launch_args + testargs ) self.assertTrue(os.path.exists(os.path.join(lowerCAmelCase__ , "tracking" ) ) ) def _UpperCamelCase ( self : Optional[Any] ) -> Tuple: UpperCAmelCase = ["examples/by_feature/gradient_accumulation.py"] run_command(self._launch_args + testargs ) def _UpperCamelCase ( self : List[Any] ) -> int: UpperCAmelCase = ["examples/by_feature/local_sgd.py"] run_command(self._launch_args + testargs )
1
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyInpaintPipeline, 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 __magic_name__ ( _snake_case , unittest.TestCase ): UpperCAmelCase = KandinskyInpaintPipeline UpperCAmelCase = ["""prompt""", """image_embeds""", """negative_image_embeds""", """image""", """mask_image"""] UpperCAmelCase = [ """prompt""", """negative_prompt""", """image_embeds""", """negative_image_embeds""", """image""", """mask_image""", ] UpperCAmelCase = [ """generator""", """height""", """width""", """latents""", """guidance_scale""", """negative_prompt""", """num_inference_steps""", """return_dict""", """guidance_scale""", """num_images_per_prompt""", """output_type""", """return_dict""", ] UpperCAmelCase = False @property def _UpperCamelCase ( self : Union[str, Any] ) -> Tuple: return 3_2 @property def _UpperCamelCase ( self : int ) -> List[Any]: return 3_2 @property def _UpperCamelCase ( self : List[Any] ) -> List[Any]: return self.time_input_dim @property def _UpperCamelCase ( self : Tuple ) -> Tuple: return self.time_input_dim * 4 @property def _UpperCamelCase ( self : Any ) -> Optional[int]: return 1_0_0 @property def _UpperCamelCase ( self : Optional[int] ) -> Optional[int]: UpperCAmelCase = XLMRobertaTokenizerFast.from_pretrained("YiYiXu/tiny-random-mclip-base" ) return tokenizer @property def _UpperCamelCase ( self : int ) -> Dict: torch.manual_seed(0 ) UpperCAmelCase = MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=3_7 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=1_0_0_5 , ) UpperCAmelCase = MultilingualCLIP(lowerCAmelCase__ ) UpperCAmelCase = text_encoder.eval() return text_encoder @property def _UpperCamelCase ( self : Dict ) -> Optional[int]: torch.manual_seed(0 ) UpperCAmelCase = { "in_channels": 9, # 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, } UpperCAmelCase = UNetaDConditionModel(**lowerCAmelCase__ ) return model @property def _UpperCamelCase ( self : str ) -> Optional[Any]: return { "block_out_channels": [3_2, 6_4], "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": 1_2, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def _UpperCamelCase ( self : Dict ) -> List[Any]: torch.manual_seed(0 ) UpperCAmelCase = VQModel(**self.dummy_movq_kwargs ) return model def _UpperCamelCase ( self : Tuple ) -> Any: UpperCAmelCase = self.dummy_text_encoder UpperCAmelCase = self.dummy_tokenizer UpperCAmelCase = self.dummy_unet UpperCAmelCase = self.dummy_movq UpperCAmelCase = DDIMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule="linear" , beta_start=0.00_085 , beta_end=0.012 , clip_sample=lowerCAmelCase__ , set_alpha_to_one=lowerCAmelCase__ , steps_offset=1 , prediction_type="epsilon" , thresholding=lowerCAmelCase__ , ) UpperCAmelCase = { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "movq": movq, } return components def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Tuple=0 ) -> str: UpperCAmelCase = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(lowerCAmelCase__ ) ).to(lowerCAmelCase__ ) UpperCAmelCase = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(lowerCAmelCase__ ) # create init_image UpperCAmelCase = floats_tensor((1, 3, 6_4, 6_4) , rng=random.Random(lowerCAmelCase__ ) ).to(lowerCAmelCase__ ) UpperCAmelCase = image.cpu().permute(0 , 2 , 3 , 1 )[0] UpperCAmelCase = Image.fromarray(np.uinta(lowerCAmelCase__ ) ).convert("RGB" ).resize((2_5_6, 2_5_6) ) # create mask UpperCAmelCase = np.ones((6_4, 6_4) , dtype=np.floataa ) UpperCAmelCase = 0 if str(lowerCAmelCase__ ).startswith("mps" ): UpperCAmelCase = torch.manual_seed(lowerCAmelCase__ ) else: UpperCAmelCase = torch.Generator(device=lowerCAmelCase__ ).manual_seed(lowerCAmelCase__ ) UpperCAmelCase = { "prompt": "horse", "image": init_image, "mask_image": mask, "image_embeds": image_embeds, "negative_image_embeds": negative_image_embeds, "generator": generator, "height": 6_4, "width": 6_4, "num_inference_steps": 2, "guidance_scale": 4.0, "output_type": "np", } return inputs def _UpperCamelCase ( self : Dict ) -> List[str]: UpperCAmelCase = "cpu" UpperCAmelCase = self.get_dummy_components() UpperCAmelCase = self.pipeline_class(**lowerCAmelCase__ ) UpperCAmelCase = pipe.to(lowerCAmelCase__ ) pipe.set_progress_bar_config(disable=lowerCAmelCase__ ) UpperCAmelCase = pipe(**self.get_dummy_inputs(lowerCAmelCase__ ) ) UpperCAmelCase = output.images UpperCAmelCase = pipe( **self.get_dummy_inputs(lowerCAmelCase__ ) , return_dict=lowerCAmelCase__ , )[0] UpperCAmelCase = image[0, -3:, -3:, -1] UpperCAmelCase = image_from_tuple[0, -3:, -3:, -1] print(f"image.shape {image.shape}" ) assert image.shape == (1, 6_4, 6_4, 3) UpperCAmelCase = np.array( [0.8_326_919, 0.73_790_467, 0.20_918_581, 0.9_309_612, 0.5_511_791, 0.43_713_328, 0.5_513_321, 0.49_922_934, 0.59_497_786] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), f" expected_slice {expected_slice}, but got {image_slice.flatten()}" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), f" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}" def _UpperCamelCase ( self : str ) -> Tuple: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : str ) -> str: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _UpperCamelCase ( self : Tuple ) -> int: UpperCAmelCase = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/kandinsky_inpaint_cat_with_hat_fp16.npy" ) UpperCAmelCase = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png" ) UpperCAmelCase = np.ones((7_6_8, 7_6_8) , dtype=np.floataa ) UpperCAmelCase = 0 UpperCAmelCase = "a hat" UpperCAmelCase = KandinskyPriorPipeline.from_pretrained( "kandinsky-community/kandinsky-2-1-prior" , torch_dtype=torch.floataa ) pipe_prior.to(lowerCAmelCase__ ) UpperCAmelCase = KandinskyInpaintPipeline.from_pretrained( "kandinsky-community/kandinsky-2-1-inpaint" , torch_dtype=torch.floataa ) UpperCAmelCase = pipeline.to(lowerCAmelCase__ ) pipeline.set_progress_bar_config(disable=lowerCAmelCase__ ) UpperCAmelCase = torch.Generator(device="cpu" ).manual_seed(0 ) UpperCAmelCase , UpperCAmelCase = pipe_prior( lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=5 , negative_prompt="" , ).to_tuple() UpperCAmelCase = pipeline( lowerCAmelCase__ , image=lowerCAmelCase__ , mask_image=lowerCAmelCase__ , image_embeds=lowerCAmelCase__ , negative_image_embeds=lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=1_0_0 , height=7_6_8 , width=7_6_8 , output_type="np" , ) UpperCAmelCase = output.images[0] assert image.shape == (7_6_8, 7_6_8, 3) assert_mean_pixel_difference(lowerCAmelCase__ , lowerCAmelCase__ )
1
1
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { "facebook/levit-128S": "https://huggingface.co/facebook/levit-128S/resolve/main/config.json", # See all LeViT models at https://huggingface.co/models?filter=levit } class __magic_name__ ( _snake_case ): UpperCAmelCase = """levit""" def __init__( self : int , lowerCAmelCase__ : List[Any]=2_2_4 , lowerCAmelCase__ : Dict=3 , lowerCAmelCase__ : str=3 , lowerCAmelCase__ : List[str]=2 , lowerCAmelCase__ : Any=1 , lowerCAmelCase__ : Union[str, Any]=1_6 , lowerCAmelCase__ : Dict=[1_2_8, 2_5_6, 3_8_4] , lowerCAmelCase__ : List[Any]=[4, 8, 1_2] , lowerCAmelCase__ : Dict=[4, 4, 4] , lowerCAmelCase__ : List[Any]=[1_6, 1_6, 1_6] , lowerCAmelCase__ : Dict=0 , lowerCAmelCase__ : str=[2, 2, 2] , lowerCAmelCase__ : Optional[int]=[2, 2, 2] , lowerCAmelCase__ : Optional[Any]=0.02 , **lowerCAmelCase__ : str , ) -> str: super().__init__(**lowerCAmelCase__ ) UpperCAmelCase = image_size UpperCAmelCase = num_channels UpperCAmelCase = kernel_size UpperCAmelCase = stride UpperCAmelCase = padding UpperCAmelCase = hidden_sizes UpperCAmelCase = num_attention_heads UpperCAmelCase = depths UpperCAmelCase = key_dim UpperCAmelCase = drop_path_rate UpperCAmelCase = patch_size UpperCAmelCase = attention_ratio UpperCAmelCase = mlp_ratio UpperCAmelCase = initializer_range UpperCAmelCase = [ ["Subsample", key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2], ["Subsample", key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2], ] class __magic_name__ ( _snake_case ): UpperCAmelCase = version.parse("""1.11""" ) @property def _UpperCamelCase ( self : Tuple ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def _UpperCamelCase ( self : Tuple ) -> float: return 1e-4
1
def _lowerCAmelCase( __A , __A ): return (pointa[0] - pointa[0]) ** 2 + (pointa[1] - pointa[1]) ** 2 def _lowerCAmelCase( __A , __A=0 ): return sorted(__A , key=lambda __A : x[column] ) def _lowerCAmelCase( __A , __A , __A=float("inf" ) ): for i in range(points_counts - 1 ): for j in range(i + 1 , __A ): UpperCAmelCase = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: UpperCAmelCase = current_dis return min_dis def _lowerCAmelCase( __A , __A , __A=float("inf" ) ): for i in range(min(6 , points_counts - 1 ) , __A ): for j in range(max(0 , i - 6 ) , __A ): UpperCAmelCase = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: UpperCAmelCase = current_dis return min_dis def _lowerCAmelCase( __A , __A , __A ): # base case if points_counts <= 3: return dis_between_closest_pair(__A , __A ) # recursion UpperCAmelCase = points_counts // 2 UpperCAmelCase = closest_pair_of_points_sqr( __A , points_sorted_on_y[:mid] , __A ) UpperCAmelCase = closest_pair_of_points_sqr( __A , points_sorted_on_y[mid:] , points_counts - mid ) UpperCAmelCase = min(__A , __A ) UpperCAmelCase = [] for point in points_sorted_on_x: if abs(point[0] - points_sorted_on_x[mid][0] ) < closest_pair_dis: cross_strip.append(__A ) UpperCAmelCase = dis_between_closest_in_strip( __A , len(__A ) , __A ) return min(__A , __A ) def _lowerCAmelCase( __A , __A ): UpperCAmelCase = column_based_sort(__A , column=0 ) UpperCAmelCase = column_based_sort(__A , column=1 ) return ( closest_pair_of_points_sqr( __A , __A , __A ) ) ** 0.5 if __name__ == "__main__": lowerCAmelCase__ = [(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)] print("Distance:", closest_pair_of_points(points, len(points)))
1
1
import random import unittest import numpy as np from diffusers import ( DPMSolverMultistepScheduler, EulerAncestralDiscreteScheduler, EulerDiscreteScheduler, LMSDiscreteScheduler, OnnxStableDiffusionImgaImgPipeline, PNDMScheduler, ) from diffusers.utils import floats_tensor from diffusers.utils.testing_utils import ( is_onnx_available, load_image, nightly, require_onnxruntime, require_torch_gpu, ) from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class __magic_name__ ( _snake_case , unittest.TestCase ): UpperCAmelCase = """hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline""" def _UpperCamelCase ( self : Any , lowerCAmelCase__ : Optional[int]=0 ) -> Dict: UpperCAmelCase = floats_tensor((1, 3, 1_2_8, 1_2_8) , rng=random.Random(lowerCAmelCase__ ) ) UpperCAmelCase = np.random.RandomState(lowerCAmelCase__ ) UpperCAmelCase = { "prompt": "A painting of a squirrel eating a burger", "image": image, "generator": generator, "num_inference_steps": 3, "strength": 0.75, "guidance_scale": 7.5, "output_type": "numpy", } return inputs def _UpperCamelCase ( self : List[str] ) -> int: UpperCAmelCase = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider="CPUExecutionProvider" ) pipe.set_progress_bar_config(disable=lowerCAmelCase__ ) UpperCAmelCase = self.get_dummy_inputs() UpperCAmelCase = pipe(**lowerCAmelCase__ ).images UpperCAmelCase = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 1_2_8, 1_2_8, 3) UpperCAmelCase = np.array([0.69_643, 0.58_484, 0.50_314, 0.58_760, 0.55_368, 0.59_643, 0.51_529, 0.41_217, 0.49_087] ) assert np.abs(image_slice - expected_slice ).max() < 1e-1 def _UpperCamelCase ( self : str ) -> List[str]: UpperCAmelCase = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider="CPUExecutionProvider" ) UpperCAmelCase = PNDMScheduler.from_config(pipe.scheduler.config , skip_prk_steps=lowerCAmelCase__ ) pipe.set_progress_bar_config(disable=lowerCAmelCase__ ) UpperCAmelCase = self.get_dummy_inputs() UpperCAmelCase = pipe(**lowerCAmelCase__ ).images UpperCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 1_2_8, 1_2_8, 3) UpperCAmelCase = np.array([0.61_737, 0.54_642, 0.53_183, 0.54_465, 0.52_742, 0.60_525, 0.49_969, 0.40_655, 0.48_154] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 def _UpperCamelCase ( self : Tuple ) -> Dict: UpperCAmelCase = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider="CPUExecutionProvider" ) UpperCAmelCase = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=lowerCAmelCase__ ) # warmup pass to apply optimizations UpperCAmelCase = pipe(**self.get_dummy_inputs() ) UpperCAmelCase = self.get_dummy_inputs() UpperCAmelCase = pipe(**lowerCAmelCase__ ).images UpperCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 1_2_8, 1_2_8, 3) UpperCAmelCase = np.array([0.52_761, 0.59_977, 0.49_033, 0.49_619, 0.54_282, 0.50_311, 0.47_600, 0.40_918, 0.45_203] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 def _UpperCamelCase ( self : Union[str, Any] ) -> Union[str, Any]: UpperCAmelCase = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider="CPUExecutionProvider" ) UpperCAmelCase = EulerDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=lowerCAmelCase__ ) UpperCAmelCase = self.get_dummy_inputs() UpperCAmelCase = pipe(**lowerCAmelCase__ ).images UpperCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 1_2_8, 1_2_8, 3) UpperCAmelCase = np.array([0.52_911, 0.60_004, 0.49_229, 0.49_805, 0.54_502, 0.50_680, 0.47_777, 0.41_028, 0.45_304] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 def _UpperCamelCase ( self : Dict ) -> Union[str, Any]: UpperCAmelCase = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider="CPUExecutionProvider" ) UpperCAmelCase = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=lowerCAmelCase__ ) UpperCAmelCase = self.get_dummy_inputs() UpperCAmelCase = pipe(**lowerCAmelCase__ ).images UpperCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 1_2_8, 1_2_8, 3) UpperCAmelCase = np.array([0.52_911, 0.60_004, 0.49_229, 0.49_805, 0.54_502, 0.50_680, 0.47_777, 0.41_028, 0.45_304] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 def _UpperCamelCase ( self : Union[str, Any] ) -> Optional[Any]: UpperCAmelCase = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider="CPUExecutionProvider" ) UpperCAmelCase = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=lowerCAmelCase__ ) UpperCAmelCase = self.get_dummy_inputs() UpperCAmelCase = pipe(**lowerCAmelCase__ ).images UpperCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 1_2_8, 1_2_8, 3) UpperCAmelCase = np.array([0.65_331, 0.58_277, 0.48_204, 0.56_059, 0.53_665, 0.56_235, 0.50_969, 0.40_009, 0.46_552] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 @nightly @require_onnxruntime @require_torch_gpu class __magic_name__ ( unittest.TestCase ): @property def _UpperCamelCase ( self : Any ) -> str: return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def _UpperCamelCase ( self : Dict ) -> Tuple: UpperCAmelCase = ort.SessionOptions() UpperCAmelCase = False return options def _UpperCamelCase ( self : Dict ) -> Any: UpperCAmelCase = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/img2img/sketch-mountains-input.jpg" ) UpperCAmelCase = init_image.resize((7_6_8, 5_1_2) ) # using the PNDM scheduler by default UpperCAmelCase = OnnxStableDiffusionImgaImgPipeline.from_pretrained( "CompVis/stable-diffusion-v1-4" , revision="onnx" , safety_checker=lowerCAmelCase__ , feature_extractor=lowerCAmelCase__ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=lowerCAmelCase__ ) UpperCAmelCase = "A fantasy landscape, trending on artstation" UpperCAmelCase = np.random.RandomState(0 ) UpperCAmelCase = pipe( prompt=lowerCAmelCase__ , image=lowerCAmelCase__ , strength=0.75 , guidance_scale=7.5 , num_inference_steps=1_0 , generator=lowerCAmelCase__ , output_type="np" , ) UpperCAmelCase = output.images UpperCAmelCase = images[0, 2_5_5:2_5_8, 3_8_3:3_8_6, -1] assert images.shape == (1, 5_1_2, 7_6_8, 3) UpperCAmelCase = np.array([0.4_909, 0.5_059, 0.5_372, 0.4_623, 0.4_876, 0.5_049, 0.4_820, 0.4_956, 0.5_019] ) # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues assert np.abs(image_slice.flatten() - expected_slice ).max() < 2e-2 def _UpperCamelCase ( self : Union[str, Any] ) -> List[Any]: UpperCAmelCase = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/img2img/sketch-mountains-input.jpg" ) UpperCAmelCase = init_image.resize((7_6_8, 5_1_2) ) UpperCAmelCase = LMSDiscreteScheduler.from_pretrained( "runwayml/stable-diffusion-v1-5" , subfolder="scheduler" , revision="onnx" ) UpperCAmelCase = OnnxStableDiffusionImgaImgPipeline.from_pretrained( "runwayml/stable-diffusion-v1-5" , revision="onnx" , scheduler=lowerCAmelCase__ , safety_checker=lowerCAmelCase__ , feature_extractor=lowerCAmelCase__ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=lowerCAmelCase__ ) UpperCAmelCase = "A fantasy landscape, trending on artstation" UpperCAmelCase = np.random.RandomState(0 ) UpperCAmelCase = pipe( prompt=lowerCAmelCase__ , image=lowerCAmelCase__ , strength=0.75 , guidance_scale=7.5 , num_inference_steps=2_0 , generator=lowerCAmelCase__ , output_type="np" , ) UpperCAmelCase = output.images UpperCAmelCase = images[0, 2_5_5:2_5_8, 3_8_3:3_8_6, -1] assert images.shape == (1, 5_1_2, 7_6_8, 3) UpperCAmelCase = np.array([0.8_043, 0.926, 0.9_581, 0.8_119, 0.8_954, 0.913, 0.7_209, 0.7_463, 0.7_431] ) # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues assert np.abs(image_slice.flatten() - expected_slice ).max() < 2e-2
1
import copy import os import cva import numpy as np from matplotlib import pyplot as plt class __magic_name__ : def __init__( self : Optional[int] ) -> Optional[Any]: UpperCAmelCase = "" UpperCAmelCase = "" UpperCAmelCase = [] UpperCAmelCase = 0 UpperCAmelCase = 2_5_6 UpperCAmelCase = 0 UpperCAmelCase = 0 UpperCAmelCase = 0 UpperCAmelCase = 0 def _UpperCamelCase ( self : Any , lowerCAmelCase__ : Optional[Any] ) -> List[str]: UpperCAmelCase = cva.imread(lowerCAmelCase__ , 0 ) UpperCAmelCase = copy.deepcopy(self.img ) UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = plt.hist(self.img.ravel() , 2_5_6 , [0, 2_5_6] , label="x" ) UpperCAmelCase = np.sum(lowerCAmelCase__ ) for i in range(len(lowerCAmelCase__ ) ): UpperCAmelCase = x[i] / self.k self.sk += prk UpperCAmelCase = (self.L - 1) * self.sk if self.rem != 0: UpperCAmelCase = int(last % last ) UpperCAmelCase = int(last + 1 if self.rem >= 0.5 else last ) self.last_list.append(lowerCAmelCase__ ) UpperCAmelCase = int(np.ma.count(self.img ) / self.img[1].size ) UpperCAmelCase = self.img[1].size for i in range(self.number_of_cols ): for j in range(self.number_of_rows ): UpperCAmelCase = self.img[j][i] if num != self.last_list[num]: UpperCAmelCase = self.last_list[num] cva.imwrite("output_data/output.jpg" , self.img ) def _UpperCamelCase ( self : str ) -> int: plt.hist(self.img.ravel() , 2_5_6 , [0, 2_5_6] ) def _UpperCamelCase ( self : Dict ) -> Optional[Any]: cva.imshow("Output-Image" , self.img ) cva.imshow("Input-Image" , self.original_image ) cva.waitKey(5_0_0_0 ) cva.destroyAllWindows() if __name__ == "__main__": lowerCAmelCase__ = os.path.join(os.path.basename(__file__), "image_data/input.jpg") lowerCAmelCase__ = ConstantStretch() stretcher.stretch(file_path) stretcher.plot_histogram() stretcher.show_image()
1
1
from __future__ import annotations def _lowerCAmelCase( __A , __A , __A ): if len(__A ) == 0: raise ValueError("find_max() arg is an empty sequence" ) if ( left >= len(__A ) or left < -len(__A ) or right >= len(__A ) or right < -len(__A ) ): raise IndexError("list index out of range" ) if left == right: return nums[left] UpperCAmelCase = (left + right) >> 1 # the middle UpperCAmelCase = find_max(__A , __A , __A ) # find max in range[left, mid] UpperCAmelCase = find_max(__A , mid + 1 , __A ) # find max in range[mid + 1, right] return left_max if left_max >= right_max else right_max if __name__ == "__main__": import doctest doctest.testmod(verbose=True)
1
import json import os import unittest from transformers import BatchEncoding, LEDTokenizer, LEDTokenizerFast from transformers.models.led.tokenization_led import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, require_torch from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __magic_name__ ( _snake_case , unittest.TestCase ): UpperCAmelCase = LEDTokenizer UpperCAmelCase = LEDTokenizerFast UpperCAmelCase = True def _UpperCamelCase ( self : Tuple ) -> Union[str, Any]: super().setUp() UpperCAmelCase = [ "l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "\u0120", "\u0120l", "\u0120n", "\u0120lo", "\u0120low", "er", "\u0120lowest", "\u0120newer", "\u0120wider", "<unk>", ] UpperCAmelCase = dict(zip(lowerCAmelCase__ , range(len(lowerCAmelCase__ ) ) ) ) UpperCAmelCase = ["#version: 0.2", "\u0120 l", "\u0120l o", "\u0120lo w", "e r", ""] UpperCAmelCase = {"unk_token": "<unk>"} UpperCAmelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) UpperCAmelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as fp: fp.write(json.dumps(lowerCAmelCase__ ) + "\n" ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(lowerCAmelCase__ ) ) def _UpperCamelCase ( self : Union[str, Any] , **lowerCAmelCase__ : Optional[int] ) -> Optional[int]: kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **lowerCAmelCase__ ) def _UpperCamelCase ( self : str , **lowerCAmelCase__ : str ) -> Optional[int]: kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : List[Any] ) -> List[Any]: return "lower newer", "lower newer" @cached_property def _UpperCamelCase ( self : Dict ) -> str: return LEDTokenizer.from_pretrained("allenai/led-base-16384" ) @cached_property def _UpperCamelCase ( self : int ) -> Tuple: return LEDTokenizerFast.from_pretrained("allenai/led-base-16384" ) @require_torch def _UpperCamelCase ( self : Tuple ) -> List[str]: UpperCAmelCase = ["A long paragraph for summarization.", "Another paragraph for summarization."] UpperCAmelCase = [0, 2_5_0, 2_5_1, 1_7_8_1_8, 1_3, 3_9_1_8_6, 1_9_3_8, 4, 2] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer(lowerCAmelCase__ , max_length=len(lowerCAmelCase__ ) , padding=lowerCAmelCase__ , return_tensors="pt" ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual((2, 9) , batch.input_ids.shape ) self.assertEqual((2, 9) , batch.attention_mask.shape ) UpperCAmelCase = batch.input_ids.tolist()[0] self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ ) @require_torch def _UpperCamelCase ( self : Union[str, Any] ) -> List[Any]: UpperCAmelCase = ["A long paragraph for summarization.", "Another paragraph for summarization."] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer(lowerCAmelCase__ , padding=lowerCAmelCase__ , return_tensors="pt" ) self.assertIn("input_ids" , lowerCAmelCase__ ) self.assertIn("attention_mask" , lowerCAmelCase__ ) self.assertNotIn("labels" , lowerCAmelCase__ ) self.assertNotIn("decoder_attention_mask" , lowerCAmelCase__ ) @require_torch def _UpperCamelCase ( self : int ) -> int: UpperCAmelCase = [ "Summary of the text.", "Another summary.", ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer(text_target=lowerCAmelCase__ , max_length=3_2 , padding="max_length" , return_tensors="pt" ) self.assertEqual(3_2 , targets["input_ids"].shape[1] ) @require_torch def _UpperCamelCase ( self : Any ) -> int: for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer( ["I am a small frog" * 1_0_2_4, "I am a small frog"] , padding=lowerCAmelCase__ , truncation=lowerCAmelCase__ , return_tensors="pt" ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual(batch.input_ids.shape , (2, 5_1_2_2) ) @require_torch def _UpperCamelCase ( self : Dict ) -> Tuple: UpperCAmelCase = ["A long paragraph for summarization."] UpperCAmelCase = [ "Summary of the text.", ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer(lowerCAmelCase__ , return_tensors="pt" ) UpperCAmelCase = tokenizer(text_target=lowerCAmelCase__ , return_tensors="pt" ) UpperCAmelCase = inputs["input_ids"] UpperCAmelCase = targets["input_ids"] self.assertTrue((input_ids[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((labels[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((input_ids[:, -1] == tokenizer.eos_token_id).all().item() ) self.assertTrue((labels[:, -1] == tokenizer.eos_token_id).all().item() ) @require_torch def _UpperCamelCase ( self : Optional[int] ) -> Optional[Any]: for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = ["Summary of the text.", "Another summary."] UpperCAmelCase = [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -1, -1]] UpperCAmelCase = tokenizer(lowerCAmelCase__ , padding=lowerCAmelCase__ ) UpperCAmelCase = [[0] * len(lowerCAmelCase__ ) for x in encoded_output["input_ids"]] UpperCAmelCase = tokenizer.pad(lowerCAmelCase__ ) self.assertSequenceEqual(outputs["global_attention_mask"] , lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] ) -> int: pass def _UpperCamelCase ( self : Optional[Any] ) -> Union[str, Any]: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})" ): UpperCAmelCase = self.rust_tokenizer_class.from_pretrained(lowerCAmelCase__ , **lowerCAmelCase__ ) UpperCAmelCase = self.tokenizer_class.from_pretrained(lowerCAmelCase__ , **lowerCAmelCase__ ) UpperCAmelCase = "A, <mask> AllenNLP sentence." UpperCAmelCase = tokenizer_r.encode_plus(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ , return_token_type_ids=lowerCAmelCase__ ) UpperCAmelCase = tokenizer_p.encode_plus(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ , return_token_type_ids=lowerCAmelCase__ ) self.assertEqual(sum(tokens_r["token_type_ids"] ) , sum(tokens_p["token_type_ids"] ) ) self.assertEqual( sum(tokens_r["attention_mask"] ) / len(tokens_r["attention_mask"] ) , sum(tokens_p["attention_mask"] ) / len(tokens_p["attention_mask"] ) , ) UpperCAmelCase = tokenizer_r.convert_ids_to_tokens(tokens_r["input_ids"] ) UpperCAmelCase = tokenizer_p.convert_ids_to_tokens(tokens_p["input_ids"] ) self.assertSequenceEqual(tokens_p["input_ids"] , [0, 2_5_0, 6, 5_0_2_6_4, 3_8_2_3, 4_8_7, 2_1_9_9_2, 3_6_4_5, 4, 2] ) self.assertSequenceEqual(tokens_r["input_ids"] , [0, 2_5_0, 6, 5_0_2_6_4, 3_8_2_3, 4_8_7, 2_1_9_9_2, 3_6_4_5, 4, 2] ) self.assertSequenceEqual( lowerCAmelCase__ , ["<s>", "A", ",", "<mask>", "ĠAllen", "N", "LP", "Ġsentence", ".", "</s>"] ) self.assertSequenceEqual( lowerCAmelCase__ , ["<s>", "A", ",", "<mask>", "ĠAllen", "N", "LP", "Ġsentence", ".", "</s>"] )
1
1
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { "xlm-roberta-base": "https://huggingface.co/xlm-roberta-base/resolve/main/config.json", "xlm-roberta-large": "https://huggingface.co/xlm-roberta-large/resolve/main/config.json", "xlm-roberta-large-finetuned-conll02-dutch": ( "https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json" ), "xlm-roberta-large-finetuned-conll02-spanish": ( "https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json" ), "xlm-roberta-large-finetuned-conll03-english": ( "https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json" ), "xlm-roberta-large-finetuned-conll03-german": ( "https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json" ), } class __magic_name__ ( _snake_case ): UpperCAmelCase = """xlm-roberta""" def __init__( self : Union[str, Any] , lowerCAmelCase__ : List[Any]=3_0_5_2_2 , lowerCAmelCase__ : Optional[int]=7_6_8 , lowerCAmelCase__ : Optional[Any]=1_2 , lowerCAmelCase__ : Tuple=1_2 , lowerCAmelCase__ : Any=3_0_7_2 , lowerCAmelCase__ : Union[str, Any]="gelu" , lowerCAmelCase__ : str=0.1 , lowerCAmelCase__ : List[str]=0.1 , lowerCAmelCase__ : Any=5_1_2 , lowerCAmelCase__ : Optional[Any]=2 , lowerCAmelCase__ : Tuple=0.02 , lowerCAmelCase__ : List[str]=1e-1_2 , lowerCAmelCase__ : List[Any]=1 , lowerCAmelCase__ : Tuple=0 , lowerCAmelCase__ : Optional[int]=2 , lowerCAmelCase__ : List[Any]="absolute" , lowerCAmelCase__ : Optional[Any]=True , lowerCAmelCase__ : Any=None , **lowerCAmelCase__ : Tuple , ) -> Optional[Any]: super().__init__(pad_token_id=lowerCAmelCase__ , bos_token_id=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__ , **lowerCAmelCase__ ) UpperCAmelCase = vocab_size UpperCAmelCase = hidden_size UpperCAmelCase = num_hidden_layers UpperCAmelCase = num_attention_heads UpperCAmelCase = hidden_act UpperCAmelCase = intermediate_size UpperCAmelCase = hidden_dropout_prob UpperCAmelCase = attention_probs_dropout_prob UpperCAmelCase = max_position_embeddings UpperCAmelCase = type_vocab_size UpperCAmelCase = initializer_range UpperCAmelCase = layer_norm_eps UpperCAmelCase = position_embedding_type UpperCAmelCase = use_cache UpperCAmelCase = classifier_dropout class __magic_name__ ( _snake_case ): @property def _UpperCamelCase ( self : Union[str, Any] ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": UpperCAmelCase = {0: "batch", 1: "choice", 2: "sequence"} else: UpperCAmelCase = {0: "batch", 1: "sequence"} return OrderedDict( [ ("input_ids", dynamic_axis), ("attention_mask", dynamic_axis), ] )
1
import numpy as np from nltk.translate import meteor_score import datasets from datasets.config import importlib_metadata, version lowerCAmelCase__ = version.parse(importlib_metadata.version("nltk")) if NLTK_VERSION >= version.Version("3.6.4"): from nltk import word_tokenize lowerCAmelCase__ = "\\n@inproceedings{banarjee2005,\n title = {{METEOR}: An Automatic Metric for {MT} Evaluation with Improved Correlation with Human Judgments},\n author = {Banerjee, Satanjeev and Lavie, Alon},\n booktitle = {Proceedings of the {ACL} Workshop on Intrinsic and Extrinsic Evaluation Measures for Machine Translation and/or Summarization},\n month = jun,\n year = {2005},\n address = {Ann Arbor, Michigan},\n publisher = {Association for Computational Linguistics},\n url = {https://www.aclweb.org/anthology/W05-0909},\n pages = {65--72},\n}\n" lowerCAmelCase__ = "\\nMETEOR, an automatic metric for machine translation evaluation\nthat is based on a generalized concept of unigram matching between the\nmachine-produced translation and human-produced reference translations.\nUnigrams can be matched based on their surface forms, stemmed forms,\nand meanings; furthermore, METEOR can be easily extended to include more\nadvanced matching strategies. Once all generalized unigram matches\nbetween the two strings have been found, METEOR computes a score for\nthis matching using a combination of unigram-precision, unigram-recall, and\na measure of fragmentation that is designed to directly capture how\nwell-ordered the matched words in the machine translation are in relation\nto the reference.\n\nMETEOR gets an R correlation value of 0.347 with human evaluation on the Arabic\ndata and 0.331 on the Chinese data. This is shown to be an improvement on\nusing simply unigram-precision, unigram-recall and their harmonic F1\ncombination.\n" lowerCAmelCase__ = "\nComputes METEOR score of translated segments against one or more references.\nArgs:\n predictions: list of predictions to score. Each prediction\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\n alpha: Parameter for controlling relative weights of precision and recall. default: 0.9\n beta: Parameter for controlling shape of penalty as a function of fragmentation. default: 3\n gamma: Relative weight assigned to fragmentation penalty. default: 0.5\nReturns:\n 'meteor': meteor score.\nExamples:\n\n >>> meteor = datasets.load_metric('meteor')\n >>> predictions = [\"It is a guide to action which ensures that the military always obeys the commands of the party\"]\n >>> references = [\"It is a guide to action that ensures that the military will forever heed Party commands\"]\n >>> results = meteor.compute(predictions=predictions, references=references)\n >>> print(round(results[\"meteor\"], 4))\n 0.6944\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): def _UpperCamelCase ( self : int ) -> str: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Value("string" , id="sequence" ), } ) , codebase_urls=["https://github.com/nltk/nltk/blob/develop/nltk/translate/meteor_score.py"] , reference_urls=[ "https://www.nltk.org/api/nltk.translate.html#module-nltk.translate.meteor_score", "https://en.wikipedia.org/wiki/METEOR", ] , ) def _UpperCamelCase ( self : Dict , lowerCAmelCase__ : List[Any] ) -> Dict: import nltk nltk.download("wordnet" ) if NLTK_VERSION >= version.Version("3.6.5" ): nltk.download("punkt" ) if NLTK_VERSION >= version.Version("3.6.6" ): nltk.download("omw-1.4" ) def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Any=0.9 , lowerCAmelCase__ : Tuple=3 , lowerCAmelCase__ : Optional[int]=0.5 ) -> Any: if NLTK_VERSION >= version.Version("3.6.5" ): UpperCAmelCase = [ meteor_score.single_meteor_score( word_tokenize(lowerCAmelCase__ ) , word_tokenize(lowerCAmelCase__ ) , alpha=lowerCAmelCase__ , beta=lowerCAmelCase__ , gamma=lowerCAmelCase__ ) for ref, pred in zip(lowerCAmelCase__ , lowerCAmelCase__ ) ] else: UpperCAmelCase = [ meteor_score.single_meteor_score(lowerCAmelCase__ , lowerCAmelCase__ , alpha=lowerCAmelCase__ , beta=lowerCAmelCase__ , gamma=lowerCAmelCase__ ) for ref, pred in zip(lowerCAmelCase__ , lowerCAmelCase__ ) ] return {"meteor": np.mean(lowerCAmelCase__ )}
1
1
def _lowerCAmelCase( ): UpperCAmelCase = 0 for i in range(1 , 1001 ): total += i**i return str(__A )[-10:] if __name__ == "__main__": print(solution())
1
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { "unc-nlp/lxmert-base-uncased": "https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/config.json", } class __magic_name__ ( _snake_case ): UpperCAmelCase = """lxmert""" UpperCAmelCase = {} def __init__( self : int , lowerCAmelCase__ : Any=3_0_5_2_2 , lowerCAmelCase__ : List[str]=7_6_8 , lowerCAmelCase__ : Union[str, Any]=1_2 , lowerCAmelCase__ : List[Any]=9_5_0_0 , lowerCAmelCase__ : Any=1_6_0_0 , lowerCAmelCase__ : Union[str, Any]=4_0_0 , lowerCAmelCase__ : Tuple=3_0_7_2 , lowerCAmelCase__ : Dict="gelu" , lowerCAmelCase__ : Tuple=0.1 , lowerCAmelCase__ : Tuple=0.1 , lowerCAmelCase__ : int=5_1_2 , lowerCAmelCase__ : List[str]=2 , lowerCAmelCase__ : List[str]=0.02 , lowerCAmelCase__ : str=1e-1_2 , lowerCAmelCase__ : str=9 , lowerCAmelCase__ : int=5 , lowerCAmelCase__ : Optional[int]=5 , lowerCAmelCase__ : List[Any]=2_0_4_8 , lowerCAmelCase__ : Any=4 , lowerCAmelCase__ : Dict=6.67 , lowerCAmelCase__ : Any=True , lowerCAmelCase__ : Union[str, Any]=True , lowerCAmelCase__ : Any=True , lowerCAmelCase__ : Tuple=True , lowerCAmelCase__ : Optional[Any]=True , lowerCAmelCase__ : Optional[int]=True , lowerCAmelCase__ : Tuple=True , **lowerCAmelCase__ : List[Any] , ) -> Dict: UpperCAmelCase = vocab_size UpperCAmelCase = hidden_size UpperCAmelCase = num_attention_heads UpperCAmelCase = hidden_act UpperCAmelCase = intermediate_size UpperCAmelCase = hidden_dropout_prob UpperCAmelCase = attention_probs_dropout_prob UpperCAmelCase = max_position_embeddings UpperCAmelCase = type_vocab_size UpperCAmelCase = initializer_range UpperCAmelCase = layer_norm_eps UpperCAmelCase = num_qa_labels UpperCAmelCase = num_object_labels UpperCAmelCase = num_attr_labels UpperCAmelCase = l_layers UpperCAmelCase = x_layers UpperCAmelCase = r_layers UpperCAmelCase = visual_feat_dim UpperCAmelCase = visual_pos_dim UpperCAmelCase = visual_loss_normalizer UpperCAmelCase = task_matched UpperCAmelCase = task_mask_lm UpperCAmelCase = task_obj_predict UpperCAmelCase = task_qa UpperCAmelCase = visual_obj_loss UpperCAmelCase = visual_attr_loss UpperCAmelCase = visual_feat_loss UpperCAmelCase = {"vision": r_layers, "cross_encoder": x_layers, "language": l_layers} super().__init__(**lowerCAmelCase__ )
1
1
import argparse import json import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils.deepspeed import DummyOptim, DummyScheduler lowerCAmelCase__ = 16 lowerCAmelCase__ = 32 def _lowerCAmelCase( __A , __A = 16 , __A = "bert-base-cased" ): UpperCAmelCase = AutoTokenizer.from_pretrained(__A ) UpperCAmelCase = load_dataset("glue" , "mrpc" ) def tokenize_function(__A ): # max_length=None => use the model max length (it's actually the default) UpperCAmelCase = tokenizer(examples["sentence1"] , examples["sentence2"] , truncation=__A , max_length=__A ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset UpperCAmelCase = datasets.map( __A , batched=__A , remove_columns=["idx", "sentence1", "sentence2"] , load_from_cache_file=__A ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library UpperCAmelCase = tokenized_datasets.rename_column("label" , "labels" ) def collate_fn(__A ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(__A , padding="max_length" , max_length=128 , return_tensors="pt" ) return tokenizer.pad(__A , padding="longest" , return_tensors="pt" ) # Instantiate dataloaders. UpperCAmelCase = DataLoader( tokenized_datasets["train"] , shuffle=__A , collate_fn=__A , batch_size=__A ) UpperCAmelCase = DataLoader( tokenized_datasets["validation"] , shuffle=__A , collate_fn=__A , batch_size=__A ) return train_dataloader, eval_dataloader def _lowerCAmelCase( __A , __A ): # Initialize accelerator UpperCAmelCase = Accelerator() # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs UpperCAmelCase = config["lr"] UpperCAmelCase = int(config["num_epochs"] ) UpperCAmelCase = int(config["seed"] ) UpperCAmelCase = int(config["batch_size"] ) UpperCAmelCase = args.model_name_or_path set_seed(__A ) UpperCAmelCase , UpperCAmelCase = get_dataloaders(__A , __A , __A ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) UpperCAmelCase = AutoModelForSequenceClassification.from_pretrained(__A , return_dict=__A ) # Instantiate optimizer UpperCAmelCase = ( AdamW if accelerator.state.deepspeed_plugin is None or "optimizer" not in accelerator.state.deepspeed_plugin.deepspeed_config else DummyOptim ) UpperCAmelCase = optimizer_cls(params=model.parameters() , lr=__A ) if accelerator.state.deepspeed_plugin is not None: UpperCAmelCase = accelerator.state.deepspeed_plugin.deepspeed_config[ "gradient_accumulation_steps" ] else: UpperCAmelCase = 1 UpperCAmelCase = (len(__A ) * num_epochs) // gradient_accumulation_steps # Instantiate scheduler if ( accelerator.state.deepspeed_plugin is None or "scheduler" not in accelerator.state.deepspeed_plugin.deepspeed_config ): UpperCAmelCase = get_linear_schedule_with_warmup( optimizer=__A , num_warmup_steps=0 , num_training_steps=__A , ) else: UpperCAmelCase = DummyScheduler(__A , total_num_steps=__A , warmup_num_steps=0 ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = accelerator.prepare( __A , __A , __A , __A , __A ) # We need to keep track of how many total steps we have iterated over UpperCAmelCase = 0 # We also need to keep track of the stating epoch so files are named properly UpperCAmelCase = 0 # Now we train the model UpperCAmelCase = evaluate.load("glue" , "mrpc" ) UpperCAmelCase = 0 UpperCAmelCase = {} for epoch in range(__A , __A ): model.train() for step, batch in enumerate(__A ): UpperCAmelCase = model(**__A ) UpperCAmelCase = outputs.loss UpperCAmelCase = loss / gradient_accumulation_steps accelerator.backward(__A ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 model.eval() UpperCAmelCase = 0 for step, batch in enumerate(__A ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): UpperCAmelCase = model(**__A ) UpperCAmelCase = outputs.logits.argmax(dim=-1 ) # It is slightly faster to call this once, than multiple times UpperCAmelCase , UpperCAmelCase = accelerator.gather( (predictions, batch["labels"]) ) # If we are in a multiprocess environment, the last batch has duplicates if accelerator.use_distributed: if step == len(__A ) - 1: UpperCAmelCase = predictions[: len(eval_dataloader.dataset ) - samples_seen] UpperCAmelCase = references[: len(eval_dataloader.dataset ) - samples_seen] else: samples_seen += references.shape[0] metric.add_batch( predictions=__A , references=__A , ) UpperCAmelCase = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F"epoch {epoch}:" , __A ) UpperCAmelCase = eval_metric["accuracy"] if best_performance < eval_metric["accuracy"]: UpperCAmelCase = eval_metric["accuracy"] if args.performance_lower_bound is not None: assert ( args.performance_lower_bound <= best_performance ), F"Best performance metric {best_performance} is lower than the lower bound {args.performance_lower_bound}" accelerator.wait_for_everyone() if accelerator.is_main_process: with open(os.path.join(args.output_dir , "all_results.json" ) , "w" ) as f: json.dump(__A , __A ) def _lowerCAmelCase( ): UpperCAmelCase = argparse.ArgumentParser(description="Simple example of training script tracking peak GPU memory usage." ) parser.add_argument( "--model_name_or_path" , type=__A , default="bert-base-cased" , help="Path to pretrained model or model identifier from huggingface.co/models." , required=__A , ) parser.add_argument( "--output_dir" , type=__A , default="." , help="Optional save directory where all checkpoint folders will be stored. Default is the current working directory." , ) parser.add_argument( "--performance_lower_bound" , type=__A , default=__A , help="Optional lower bound for the performance metric. If set, the training will throw error when the performance metric drops below this value." , ) parser.add_argument( "--num_epochs" , type=__A , default=3 , help="Number of train epochs." , ) UpperCAmelCase = parser.parse_args() UpperCAmelCase = {"lr": 2E-5, "num_epochs": args.num_epochs, "seed": 42, "batch_size": 16} training_function(__A , __A ) if __name__ == "__main__": main()
1
def _lowerCAmelCase( __A ): UpperCAmelCase = 0 while num > 0: digit_sum += num % 10 num //= 10 return digit_sum def _lowerCAmelCase( __A = 100 ): UpperCAmelCase = 1 UpperCAmelCase = 2 for i in range(2 , max_n + 1 ): UpperCAmelCase = pre_numerator UpperCAmelCase = 2 * i // 3 if i % 3 == 0 else 1 UpperCAmelCase = cur_numerator UpperCAmelCase = e_cont * pre_numerator + temp return sum_digits(__A ) if __name__ == "__main__": print(f"{solution() = }")
1
1
def _lowerCAmelCase( __A = 1000 ): UpperCAmelCase = 2**power UpperCAmelCase = str(__A ) UpperCAmelCase = list(__A ) UpperCAmelCase = 0 for i in list_num: sum_of_num += int(__A ) return sum_of_num if __name__ == "__main__": lowerCAmelCase__ = int(input("Enter the power of 2: ").strip()) print("2 ^ ", power, " = ", 2**power) lowerCAmelCase__ = solution(power) print("Sum of the digits is: ", result)
1
from . import ( albert, align, altclip, audio_spectrogram_transformer, auto, autoformer, bark, bart, barthez, bartpho, beit, bert, bert_generation, bert_japanese, bertweet, big_bird, bigbird_pegasus, biogpt, bit, blenderbot, blenderbot_small, blip, blip_a, bloom, bridgetower, byta, camembert, canine, chinese_clip, clap, clip, clipseg, codegen, conditional_detr, convbert, convnext, convnextva, cpm, cpmant, ctrl, cvt, dataavec, deberta, deberta_va, decision_transformer, deformable_detr, deit, deprecated, deta, detr, dialogpt, dinat, distilbert, dit, donut, dpr, dpt, efficientformer, efficientnet, electra, encodec, encoder_decoder, ernie, ernie_m, esm, falcon, flaubert, flava, fnet, focalnet, fsmt, funnel, git, glpn, gpta, gpt_bigcode, gpt_neo, gpt_neox, gpt_neox_japanese, gpt_swa, gptj, gptsan_japanese, graphormer, groupvit, herbert, hubert, ibert, imagegpt, informer, instructblip, jukebox, layoutlm, layoutlmva, layoutlmva, layoutxlm, led, levit, lilt, llama, longformer, longta, luke, lxmert, mam_aaa, marian, markuplm, maskaformer, maskformer, mbart, mbartaa, mega, megatron_bert, megatron_gpta, mgp_str, mluke, mobilebert, mobilenet_va, mobilenet_va, mobilevit, mobilevitva, mpnet, mra, mta, musicgen, mvp, nat, nezha, nllb, nllb_moe, nystromformer, oneformer, open_llama, openai, opt, owlvit, pegasus, pegasus_x, perceiver, phobert, pixastruct, plbart, poolformer, prophetnet, qdqbert, rag, realm, reformer, regnet, rembert, resnet, roberta, roberta_prelayernorm, roc_bert, roformer, rwkv, sam, segformer, sew, sew_d, speech_encoder_decoder, speech_to_text, speech_to_text_a, speechta, splinter, squeezebert, swiftformer, swin, swinasr, swinva, switch_transformers, ta, table_transformer, tapas, time_series_transformer, timesformer, timm_backbone, transfo_xl, trocr, tvlt, umta, unispeech, unispeech_sat, upernet, videomae, vilt, vision_encoder_decoder, vision_text_dual_encoder, visual_bert, vit, vit_hybrid, vit_mae, vit_msn, vivit, wavaveca, wavaveca_conformer, wavaveca_phoneme, wavaveca_with_lm, wavlm, whisper, x_clip, xglm, xlm, xlm_prophetnet, xlm_roberta, xlm_roberta_xl, xlnet, xmod, yolos, yoso, )
1
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available lowerCAmelCase__ = { "configuration_graphormer": ["GRAPHORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "GraphormerConfig"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ "GRAPHORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "GraphormerForGraphClassification", "GraphormerModel", "GraphormerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_graphormer import GRAPHORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, GraphormerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_graphormer import ( GRAPHORMER_PRETRAINED_MODEL_ARCHIVE_LIST, GraphormerForGraphClassification, GraphormerModel, GraphormerPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
1
import numpy # List of input, output pairs lowerCAmelCase__ = ( ((5, 2, 3), 15), ((6, 5, 9), 25), ((11, 12, 13), 41), ((1, 1, 1), 8), ((11, 12, 13), 41), ) lowerCAmelCase__ = (((515, 22, 13), 555), ((61, 35, 49), 150)) lowerCAmelCase__ = [2, 4, 1, 5] lowerCAmelCase__ = len(train_data) lowerCAmelCase__ = 0.0_0_9 def _lowerCAmelCase( __A , __A="train" ): return calculate_hypothesis_value(__A , __A ) - output( __A , __A ) def _lowerCAmelCase( __A ): UpperCAmelCase = 0 for i in range(len(__A ) - 1 ): hyp_val += data_input_tuple[i] * parameter_vector[i + 1] hyp_val += parameter_vector[0] return hyp_val def _lowerCAmelCase( __A , __A ): if data_set == "train": return train_data[example_no][1] elif data_set == "test": return test_data[example_no][1] return None def _lowerCAmelCase( __A , __A ): if data_set == "train": return _hypothesis_value(train_data[example_no][0] ) elif data_set == "test": return _hypothesis_value(test_data[example_no][0] ) return None def _lowerCAmelCase( __A , __A=m ): UpperCAmelCase = 0 for i in range(__A ): if index == -1: summation_value += _error(__A ) else: summation_value += _error(__A ) * train_data[i][0][index] return summation_value def _lowerCAmelCase( __A ): UpperCAmelCase = summation_of_cost_derivative(__A , __A ) / m return cost_derivative_value def _lowerCAmelCase( ): global parameter_vector # Tune these values to set a tolerance value for predicted output UpperCAmelCase = 0.000002 UpperCAmelCase = 0 UpperCAmelCase = 0 while True: j += 1 UpperCAmelCase = [0, 0, 0, 0] for i in range(0 , len(__A ) ): UpperCAmelCase = get_cost_derivative(i - 1 ) UpperCAmelCase = ( parameter_vector[i] - LEARNING_RATE * cost_derivative ) if numpy.allclose( __A , __A , atol=__A , rtol=__A , ): break UpperCAmelCase = temp_parameter_vector print(("Number of iterations:", j) ) def _lowerCAmelCase( ): for i in range(len(__A ) ): print(("Actual output value:", output(__A , "test" )) ) print(("Hypothesis output:", calculate_hypothesis_value(__A , "test" )) ) if __name__ == "__main__": run_gradient_descent() print("\nTesting gradient descent for a linear hypothesis function.\n") test_gradient_descent()
1
1
lowerCAmelCase__ = "\n# Transformers 설치 방법\n! pip install transformers datasets\n# 마지막 릴리스 대신 소스에서 설치하려면, 위 명령을 주석으로 바꾸고 아래 명령을 해제하세요.\n# ! pip install git+https://github.com/huggingface/transformers.git\n" lowerCAmelCase__ = [{"type": "code", "content": INSTALL_CONTENT}] lowerCAmelCase__ = { "{processor_class}": "FakeProcessorClass", "{model_class}": "FakeModelClass", "{object_class}": "FakeObjectClass", }
1
def _lowerCAmelCase( __A , __A , __A ): if n == 0: return 1 elif n % 2 == 1: return (binary_exponentiation(__A , n - 1 , __A ) * a) % mod else: UpperCAmelCase = binary_exponentiation(__A , n / 2 , __A ) return (b * b) % mod # a prime number lowerCAmelCase__ = 701 lowerCAmelCase__ = 1000000000 lowerCAmelCase__ = 10 # using binary exponentiation function, O(log(p)): print((a / b) % p == (a * binary_exponentiation(b, p - 2, p)) % p) print((a / b) % p == (a * b ** (p - 2)) % p)
1
1
import argparse import logging import os from pathlib import Path from typing import Any, Dict import pytorch_lightning as pl from pytorch_lightning.utilities import rank_zero_info from transformers import ( AdamW, AutoConfig, AutoModel, AutoModelForPreTraining, AutoModelForQuestionAnswering, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoModelForTokenClassification, AutoModelWithLMHead, AutoTokenizer, PretrainedConfig, PreTrainedTokenizer, ) from transformers.optimization import ( Adafactor, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.utils.versions import require_version lowerCAmelCase__ = logging.getLogger(__name__) require_version("pytorch_lightning>=1.0.4") lowerCAmelCase__ = { "base": AutoModel, "sequence-classification": AutoModelForSequenceClassification, "question-answering": AutoModelForQuestionAnswering, "pretraining": AutoModelForPreTraining, "token-classification": AutoModelForTokenClassification, "language-modeling": AutoModelWithLMHead, "summarization": AutoModelForSeqaSeqLM, "translation": AutoModelForSeqaSeqLM, } # update this and the import above to support new schedulers from transformers.optimization lowerCAmelCase__ = { "linear": get_linear_schedule_with_warmup, "cosine": get_cosine_schedule_with_warmup, "cosine_w_restarts": get_cosine_with_hard_restarts_schedule_with_warmup, "polynomial": get_polynomial_decay_schedule_with_warmup, # '': get_constant_schedule, # not supported for now # '': get_constant_schedule_with_warmup, # not supported for now } lowerCAmelCase__ = sorted(arg_to_scheduler.keys()) lowerCAmelCase__ = "{" + ", ".join(arg_to_scheduler_choices) + "}" class __magic_name__ ( pl.LightningModule ): def __init__( self : int , lowerCAmelCase__ : argparse.Namespace , lowerCAmelCase__ : Optional[Any]=None , lowerCAmelCase__ : int="base" , lowerCAmelCase__ : Dict=None , lowerCAmelCase__ : Tuple=None , lowerCAmelCase__ : Dict=None , **lowerCAmelCase__ : List[str] , ) -> str: super().__init__() # TODO: move to self.save_hyperparameters() # self.save_hyperparameters() # can also expand arguments into trainer signature for easier reading self.save_hyperparameters(lowerCAmelCase__ ) UpperCAmelCase = 0 UpperCAmelCase = Path(self.hparams.output_dir ) UpperCAmelCase = self.hparams.cache_dir if self.hparams.cache_dir else None if config is None: UpperCAmelCase = AutoConfig.from_pretrained( self.hparams.config_name if self.hparams.config_name else self.hparams.model_name_or_path , **({"num_labels": num_labels} if num_labels is not None else {}) , cache_dir=lowerCAmelCase__ , **lowerCAmelCase__ , ) else: UpperCAmelCase = config UpperCAmelCase = ("encoder_layerdrop", "decoder_layerdrop", "dropout", "attention_dropout") for p in extra_model_params: if getattr(self.hparams , lowerCAmelCase__ , lowerCAmelCase__ ): assert hasattr(self.config , lowerCAmelCase__ ), f"model config doesn't have a `{p}` attribute" setattr(self.config , lowerCAmelCase__ , getattr(self.hparams , lowerCAmelCase__ ) ) if tokenizer is None: UpperCAmelCase = AutoTokenizer.from_pretrained( self.hparams.tokenizer_name if self.hparams.tokenizer_name else self.hparams.model_name_or_path , cache_dir=lowerCAmelCase__ , ) else: UpperCAmelCase = tokenizer UpperCAmelCase = MODEL_MODES[mode] if model is None: UpperCAmelCase = self.model_type.from_pretrained( self.hparams.model_name_or_path , from_tf=bool(".ckpt" in self.hparams.model_name_or_path ) , config=self.config , cache_dir=lowerCAmelCase__ , ) else: UpperCAmelCase = model def _UpperCamelCase ( self : int , *lowerCAmelCase__ : List[Any] , **lowerCAmelCase__ : Optional[int] ) -> Tuple: UpperCAmelCase = self.model_type.from_pretrained(*lowerCAmelCase__ , **lowerCAmelCase__ ) def _UpperCamelCase ( self : Dict ) -> List[str]: UpperCAmelCase = arg_to_scheduler[self.hparams.lr_scheduler] UpperCAmelCase = get_schedule_func( self.opt , num_warmup_steps=self.hparams.warmup_steps , num_training_steps=self.total_steps() ) UpperCAmelCase = {"scheduler": scheduler, "interval": "step", "frequency": 1} return scheduler def _UpperCamelCase ( self : Union[str, Any] ) -> Union[str, Any]: UpperCAmelCase = self.model UpperCAmelCase = ["bias", "LayerNorm.weight"] UpperCAmelCase = [ { "params": [ p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay ) ], # check this named paramters "weight_decay": self.hparams.weight_decay, }, { "params": [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay )], "weight_decay": 0.0, }, ] if self.hparams.adafactor: UpperCAmelCase = Adafactor( lowerCAmelCase__ , lr=self.hparams.learning_rate , scale_parameter=lowerCAmelCase__ , relative_step=lowerCAmelCase__ ) else: UpperCAmelCase = AdamW( lowerCAmelCase__ , lr=self.hparams.learning_rate , eps=self.hparams.adam_epsilon ) UpperCAmelCase = optimizer UpperCAmelCase = self.get_lr_scheduler() return [optimizer], [scheduler] def _UpperCamelCase ( self : str , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[Any] ) -> Optional[int]: return self.validation_step(lowerCAmelCase__ , lowerCAmelCase__ ) def _UpperCamelCase ( self : List[Any] , lowerCAmelCase__ : Tuple ) -> List[str]: return self.validation_end(lowerCAmelCase__ ) def _UpperCamelCase ( self : Optional[Any] ) -> int: UpperCAmelCase = max(1 , self.hparams.gpus ) # TODO: consider num_tpu_cores UpperCAmelCase = self.hparams.train_batch_size * self.hparams.accumulate_grad_batches * num_devices return (self.dataset_size / effective_batch_size) * self.hparams.max_epochs def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : List[str] ) -> Union[str, Any]: if stage == "test": UpperCAmelCase = len(self.test_dataloader().dataset ) else: UpperCAmelCase = self.get_dataloader("train" , self.hparams.train_batch_size , shuffle=lowerCAmelCase__ ) UpperCAmelCase = len(self.train_dataloader().dataset ) def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : str , lowerCAmelCase__ : int , lowerCAmelCase__ : bool = False ) -> str: raise NotImplementedError("You must implement this for your task" ) def _UpperCamelCase ( self : str ) -> Optional[int]: return self.train_loader def _UpperCamelCase ( self : Union[str, Any] ) -> Optional[Any]: return self.get_dataloader("dev" , self.hparams.eval_batch_size , shuffle=lowerCAmelCase__ ) def _UpperCamelCase ( self : Tuple ) -> List[str]: return self.get_dataloader("test" , self.hparams.eval_batch_size , shuffle=lowerCAmelCase__ ) def _UpperCamelCase ( self : int , lowerCAmelCase__ : Tuple ) -> str: return os.path.join( self.hparams.data_dir , "cached_{}_{}_{}".format( lowerCAmelCase__ , list(filter(lowerCAmelCase__ , self.hparams.model_name_or_path.split("/" ) ) ).pop() , str(self.hparams.max_seq_length ) , ) , ) @pl.utilities.rank_zero_only def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : Dict[str, Any] ) -> None: UpperCAmelCase = self.output_dir.joinpath("best_tfmr" ) UpperCAmelCase = self.step_count self.model.save_pretrained(lowerCAmelCase__ ) self.tokenizer.save_pretrained(lowerCAmelCase__ ) @staticmethod def _UpperCamelCase ( lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[Any] ) -> Optional[Any]: parser.add_argument( "--model_name_or_path" , default=lowerCAmelCase__ , type=lowerCAmelCase__ , required=lowerCAmelCase__ , help="Path to pretrained model or model identifier from huggingface.co/models" , ) parser.add_argument( "--config_name" , default="" , type=lowerCAmelCase__ , help="Pretrained config name or path if not the same as model_name" ) parser.add_argument( "--tokenizer_name" , default=lowerCAmelCase__ , type=lowerCAmelCase__ , help="Pretrained tokenizer name or path if not the same as model_name" , ) parser.add_argument( "--cache_dir" , default=str(Path(lowerCAmelCase__ ).parent / "test_run" / "cache" ) , type=lowerCAmelCase__ , help="Where do you want to store the pre-trained models downloaded from huggingface.co" , ) parser.add_argument( "--encoder_layerdrop" , type=lowerCAmelCase__ , help="Encoder layer dropout probability (Optional). Goes into model.config" , ) parser.add_argument( "--decoder_layerdrop" , type=lowerCAmelCase__ , help="Decoder layer dropout probability (Optional). Goes into model.config" , ) parser.add_argument( "--dropout" , type=lowerCAmelCase__ , help="Dropout probability (Optional). Goes into model.config" , ) parser.add_argument( "--attention_dropout" , type=lowerCAmelCase__ , help="Attention dropout probability (Optional). Goes into model.config" , ) parser.add_argument("--learning_rate" , default=5e-5 , type=lowerCAmelCase__ , help="The initial learning rate for Adam." ) parser.add_argument( "--lr_scheduler" , default="linear" , choices=lowerCAmelCase__ , metavar=lowerCAmelCase__ , type=lowerCAmelCase__ , help="Learning rate scheduler" , ) parser.add_argument("--weight_decay" , default=0.0 , type=lowerCAmelCase__ , help="Weight decay if we apply some." ) parser.add_argument("--adam_epsilon" , default=1e-8 , type=lowerCAmelCase__ , help="Epsilon for Adam optimizer." ) parser.add_argument("--warmup_steps" , default=0 , type=lowerCAmelCase__ , help="Linear warmup over warmup_steps." ) parser.add_argument("--num_workers" , default=4 , type=lowerCAmelCase__ , help="kwarg passed to DataLoader" ) parser.add_argument("--num_train_epochs" , dest="max_epochs" , default=3 , type=lowerCAmelCase__ ) parser.add_argument("--train_batch_size" , default=3_2 , type=lowerCAmelCase__ ) parser.add_argument("--eval_batch_size" , default=3_2 , type=lowerCAmelCase__ ) parser.add_argument("--adafactor" , action="store_true" ) class __magic_name__ ( pl.Callback ): def _UpperCamelCase ( self : int , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Tuple ) -> Any: if ( trainer.is_global_zero and trainer.global_rank == 0 ): # we initialize the retriever only on master worker with RAY. In new pytorch-lightning accelorators are removed. pl_module.model.rag.retriever.init_retrieval() # better to use hook functions. class __magic_name__ ( pl.Callback ): def _UpperCamelCase ( self : int , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Optional[int] ) -> Any: # print(pl_module.model.rag) for name, param in pl_module.model.rag.named_parameters(): if param.grad is None: print(lowerCAmelCase__ ) class __magic_name__ ( pl.Callback ): def _UpperCamelCase ( self : Optional[Any] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Tuple ) -> int: UpperCAmelCase = trainer.lr_schedulers[0]["scheduler"] UpperCAmelCase = {f"lr_group_{i}": lr for i, lr in enumerate(lr_scheduler.get_lr() )} pl_module.logger.log_metrics(lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : pl.Trainer , lowerCAmelCase__ : pl.LightningModule ) -> Union[str, Any]: rank_zero_info("***** Validation results *****" ) UpperCAmelCase = trainer.callback_metrics # Log results for key in sorted(lowerCAmelCase__ ): if key not in ["log", "progress_bar"]: rank_zero_info("{} = {}\n".format(lowerCAmelCase__ , str(metrics[key] ) ) ) def _UpperCamelCase ( self : Any , lowerCAmelCase__ : pl.Trainer , lowerCAmelCase__ : pl.LightningModule ) -> Optional[int]: rank_zero_info("***** Test results *****" ) UpperCAmelCase = trainer.callback_metrics # Log and save results to file UpperCAmelCase = os.path.join(pl_module.hparams.output_dir , "test_results.txt" ) with open(lowerCAmelCase__ , "w" ) as writer: for key in sorted(lowerCAmelCase__ ): if key not in ["log", "progress_bar"]: rank_zero_info("{} = {}\n".format(lowerCAmelCase__ , str(metrics[key] ) ) ) writer.write("{} = {}\n".format(lowerCAmelCase__ , str(metrics[key] ) ) ) def _lowerCAmelCase( __A , __A ): # To allow all pl args uncomment the following line # parser = pl.Trainer.add_argparse_args(parser) parser.add_argument( "--output_dir" , default=str(Path(__A ).parent / "test_run" / "model_checkpoints" ) , type=__A , help="The output directory where the model predictions and checkpoints will be written." , ) parser.add_argument( "--fp16" , action="store_true" , help="Whether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bit" , ) parser.add_argument( "--fp16_opt_level" , type=__A , default="O2" , help=( "For fp16: Apex AMP optimization level selected in ['O0', 'O1', 'O2', and 'O3']." "See details at https://nvidia.github.io/apex/amp.html" ) , ) parser.add_argument("--n_tpu_cores" , dest="tpu_cores" , type=__A ) parser.add_argument("--max_grad_norm" , dest="gradient_clip_val" , default=1.0 , type=__A , help="Max gradient norm" ) parser.add_argument("--do_train" , action="store_true" , help="Whether to run training." ) parser.add_argument("--do_predict" , action="store_true" , help="Whether to run predictions on the test set." ) parser.add_argument( "--gradient_accumulation_steps" , dest="accumulate_grad_batches" , type=__A , default=1 , help="Number of updates steps to accumulate before performing a backward/update pass." , ) parser.add_argument("--seed" , type=__A , default=42 , help="random seed for initialization" ) parser.add_argument( "--data_dir" , default=str(Path(__A ).parent / "test_run" / "dummy-train-data" ) , type=__A , help="The input data dir. Should contain the training files for the CoNLL-2003 NER task." , ) def _lowerCAmelCase( __A , __A , __A=None , __A=True , __A=[] , __A=None , __A=None , **__A , ): pl.seed_everything(args.seed ) # init model UpperCAmelCase = Path(model.hparams.output_dir ) odir.mkdir(exist_ok=__A ) # add custom checkpoints if checkpoint_callback is None: UpperCAmelCase = pl.callbacks.ModelCheckpoint( filepath=args.output_dir , prefix="checkpoint" , monitor="val_loss" , mode="min" , save_top_k=1 ) if early_stopping_callback: extra_callbacks.append(__A ) if logging_callback is None: UpperCAmelCase = LoggingCallback() UpperCAmelCase = {} if args.fpaa: UpperCAmelCase = 16 if args.gpus > 1: UpperCAmelCase = "auto" UpperCAmelCase = "ddp" UpperCAmelCase = args.accumulate_grad_batches UpperCAmelCase = None UpperCAmelCase = "auto" UpperCAmelCase = pl.Trainer.from_argparse_args( __A , weights_summary=__A , callbacks=[logging_callback] + extra_callbacks + [InitCallback()] + [checkpoint_callback] , logger=__A , val_check_interval=1 , num_sanity_val_steps=2 , **__A , ) if args.do_train: trainer.fit(__A ) else: print("RAG modeling tests with new set functions successfuly executed!" ) return trainer
1
lowerCAmelCase__ = { "a": "AAAAA", "b": "AAAAB", "c": "AAABA", "d": "AAABB", "e": "AABAA", "f": "AABAB", "g": "AABBA", "h": "AABBB", "i": "ABAAA", "j": "BBBAA", "k": "ABAAB", "l": "ABABA", "m": "ABABB", "n": "ABBAA", "o": "ABBAB", "p": "ABBBA", "q": "ABBBB", "r": "BAAAA", "s": "BAAAB", "t": "BAABA", "u": "BAABB", "v": "BBBAB", "w": "BABAA", "x": "BABAB", "y": "BABBA", "z": "BABBB", " ": " ", } lowerCAmelCase__ = {value: key for key, value in encode_dict.items()} def _lowerCAmelCase( __A ): UpperCAmelCase = "" 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( __A ): if set(__A ) - {"A", "B", " "} != set(): raise Exception("decode() accepts only 'A', 'B' and spaces" ) UpperCAmelCase = "" for word in coded.split(): while len(__A ) != 0: decoded += decode_dict[word[:5]] UpperCAmelCase = word[5:] decoded += " " return decoded.strip() if __name__ == "__main__": from doctest import testmod testmod()
1
1
import numpy as np from PIL import Image def _lowerCAmelCase( __A , __A , __A ): UpperCAmelCase = np.array(__A ) if arr.shape[0] != arr.shape[1]: raise ValueError("The input array is not a square matrix" ) UpperCAmelCase = 0 UpperCAmelCase = 0 UpperCAmelCase = 0 UpperCAmelCase = 0 # compute the shape of the output matrix UpperCAmelCase = (arr.shape[0] - size) // stride + 1 # initialize the output matrix with zeros of shape maxpool_shape UpperCAmelCase = np.zeros((maxpool_shape, maxpool_shape) ) while i < arr.shape[0]: if i + size > arr.shape[0]: # if the end of the matrix is reached, break break while j < arr.shape[1]: # if the end of the matrix is reached, break if j + size > arr.shape[1]: break # compute the maximum of the pooling matrix UpperCAmelCase = np.max(arr[i : i + size, j : j + size] ) # shift the pooling matrix by stride of column pixels j += stride mat_j += 1 # shift the pooling matrix by stride of row pixels i += stride mat_i += 1 # reset the column index to 0 UpperCAmelCase = 0 UpperCAmelCase = 0 return updated_arr def _lowerCAmelCase( __A , __A , __A ): UpperCAmelCase = np.array(__A ) if arr.shape[0] != arr.shape[1]: raise ValueError("The input array is not a square matrix" ) UpperCAmelCase = 0 UpperCAmelCase = 0 UpperCAmelCase = 0 UpperCAmelCase = 0 # compute the shape of the output matrix UpperCAmelCase = (arr.shape[0] - size) // stride + 1 # initialize the output matrix with zeros of shape avgpool_shape UpperCAmelCase = np.zeros((avgpool_shape, avgpool_shape) ) while i < arr.shape[0]: # if the end of the matrix is reached, break if i + size > arr.shape[0]: break while j < arr.shape[1]: # if the end of the matrix is reached, break if j + size > arr.shape[1]: break # compute the average of the pooling matrix UpperCAmelCase = int(np.average(arr[i : i + size, j : j + size] ) ) # shift the pooling matrix by stride of column pixels j += stride mat_j += 1 # shift the pooling matrix by stride of row pixels i += stride mat_i += 1 # reset the column index to 0 UpperCAmelCase = 0 UpperCAmelCase = 0 return updated_arr # Main Function if __name__ == "__main__": from doctest import testmod testmod(name="avgpooling", verbose=True) # Loading the image lowerCAmelCase__ = Image.open("path_to_image") # Converting the image to numpy array and maxpooling, displaying the result # Ensure that the image is a square matrix Image.fromarray(maxpooling(np.array(image), size=3, stride=2)).show() # Converting the image to numpy array and averagepooling, displaying the result # Ensure that the image is a square matrix Image.fromarray(avgpooling(np.array(image), size=3, stride=2)).show()
1
from __future__ import annotations import json import requests from bsa import BeautifulSoup from fake_useragent import UserAgent lowerCAmelCase__ = {"UserAgent": UserAgent().random} def _lowerCAmelCase( __A ): UpperCAmelCase = script.contents[0] UpperCAmelCase = json.loads(data[data.find("{\"config\"" ) : -1] ) return info["entry_data"]["ProfilePage"][0]["graphql"]["user"] class __magic_name__ : def __init__( self : Optional[Any] , lowerCAmelCase__ : Optional[int] ) -> Any: UpperCAmelCase = f"https://www.instagram.com/{username}/" UpperCAmelCase = self.get_json() def _UpperCamelCase ( self : List[str] ) -> dict: UpperCAmelCase = requests.get(self.url , headers=lowerCAmelCase__ ).text UpperCAmelCase = BeautifulSoup(lowerCAmelCase__ , "html.parser" ).find_all("script" ) try: return extract_user_profile(scripts[4] ) except (json.decoder.JSONDecodeError, KeyError): return extract_user_profile(scripts[3] ) def __repr__( self : Tuple ) -> str: return f"{self.__class__.__name__}('{self.username}')" def __str__( self : Optional[int] ) -> str: return f"{self.fullname} ({self.username}) is {self.biography}" @property def _UpperCamelCase ( self : Any ) -> str: return self.user_data["username"] @property def _UpperCamelCase ( self : List[Any] ) -> str: return self.user_data["full_name"] @property def _UpperCamelCase ( self : List[str] ) -> str: return self.user_data["biography"] @property def _UpperCamelCase ( self : Optional[int] ) -> str: return self.user_data["business_email"] @property def _UpperCamelCase ( self : str ) -> str: return self.user_data["external_url"] @property def _UpperCamelCase ( self : int ) -> int: return self.user_data["edge_followed_by"]["count"] @property def _UpperCamelCase ( self : List[Any] ) -> int: return self.user_data["edge_follow"]["count"] @property def _UpperCamelCase ( self : List[str] ) -> int: return self.user_data["edge_owner_to_timeline_media"]["count"] @property def _UpperCamelCase ( self : Tuple ) -> str: return self.user_data["profile_pic_url_hd"] @property def _UpperCamelCase ( self : Optional[int] ) -> bool: return self.user_data["is_verified"] @property def _UpperCamelCase ( self : Optional[Any] ) -> bool: return self.user_data["is_private"] def _lowerCAmelCase( __A = "github" ): import os if os.environ.get("CI" ): return # test failing on GitHub Actions UpperCAmelCase = InstagramUser(__A ) assert instagram_user.user_data assert isinstance(instagram_user.user_data , __A ) assert instagram_user.username == username if username != "github": return assert instagram_user.fullname == "GitHub" assert instagram_user.biography == "Built for developers." assert instagram_user.number_of_posts > 150 assert instagram_user.number_of_followers > 120000 assert instagram_user.number_of_followings > 15 assert instagram_user.email == "support@github.com" assert instagram_user.website == "https://github.com/readme" assert instagram_user.profile_picture_url.startswith("https://instagram." ) assert instagram_user.is_verified is True assert instagram_user.is_private is False if __name__ == "__main__": import doctest doctest.testmod() lowerCAmelCase__ = InstagramUser("github") print(instagram_user) print(f"{instagram_user.number_of_posts = }") print(f"{instagram_user.number_of_followers = }") print(f"{instagram_user.number_of_followings = }") print(f"{instagram_user.email = }") print(f"{instagram_user.website = }") print(f"{instagram_user.profile_picture_url = }") print(f"{instagram_user.is_verified = }") print(f"{instagram_user.is_private = }")
1
1
def _lowerCAmelCase( __A , __A ): if density <= 0: raise ValueError("Impossible fluid density" ) if bulk_modulus <= 0: raise ValueError("Impossible bulk modulus" ) return (bulk_modulus / density) ** 0.5 if __name__ == "__main__": import doctest doctest.testmod()
1
import unittest import numpy as np def _lowerCAmelCase( __A , __A , __A , __A = None , ): UpperCAmelCase = np.shape(__A ) UpperCAmelCase = np.shape(__A ) UpperCAmelCase = np.shape(__A ) if shape_a[0] != shape_b[0]: UpperCAmelCase = ( "Expected the same number of rows for A and B. " F"Instead found A of size {shape_a} and B of size {shape_b}" ) raise ValueError(__A ) if shape_b[1] != shape_c[1]: UpperCAmelCase = ( "Expected the same number of columns for B and C. " F"Instead found B of size {shape_b} and C of size {shape_c}" ) raise ValueError(__A ) UpperCAmelCase = pseudo_inv if a_inv is None: try: UpperCAmelCase = np.linalg.inv(__A ) except np.linalg.LinAlgError: raise ValueError( "Input matrix A is not invertible. Cannot compute Schur complement." ) return mat_c - mat_b.T @ a_inv @ mat_b class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : List[str] ) -> None: UpperCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase = np.array([[2, 1], [6, 3]] ) UpperCAmelCase = schur_complement(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase = np.block([[a, b], [b.T, c]] ) UpperCAmelCase = np.linalg.det(lowerCAmelCase__ ) UpperCAmelCase = np.linalg.det(lowerCAmelCase__ ) UpperCAmelCase = np.linalg.det(lowerCAmelCase__ ) self.assertAlmostEqual(lowerCAmelCase__ , det_a * det_s ) def _UpperCamelCase ( self : str ) -> None: UpperCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase = np.array([[2, 1], [6, 3]] ) with self.assertRaises(lowerCAmelCase__ ): schur_complement(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def _UpperCamelCase ( self : Dict ) -> None: UpperCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase = np.array([[2, 1, 3], [6, 3, 5]] ) with self.assertRaises(lowerCAmelCase__ ): schur_complement(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) if __name__ == "__main__": import doctest doctest.testmod() unittest.main()
1
1
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available lowerCAmelCase__ = { "configuration_efficientnet": [ "EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP", "EfficientNetConfig", "EfficientNetOnnxConfig", ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ["EfficientNetImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ "EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST", "EfficientNetForImageClassification", "EfficientNetModel", "EfficientNetPreTrainedModel", ] if TYPE_CHECKING: from .configuration_efficientnet import ( EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP, EfficientNetConfig, EfficientNetOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_efficientnet import EfficientNetImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_efficientnet import ( EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST, EfficientNetForImageClassification, EfficientNetModel, EfficientNetPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure)
1
import argparse import os import re import numpy as np import PIL import torch from timm import create_model from torch.optim.lr_scheduler import OneCycleLR from torch.utils.data import DataLoader, Dataset from torchvision.transforms import Compose, RandomResizedCrop, Resize, ToTensor from accelerate import Accelerator def _lowerCAmelCase( __A ): UpperCAmelCase = fname.split(os.path.sep )[-1] return re.search(r"^(.*)_\d+\.jpg$" , __A ).groups()[0] class __magic_name__ ( _snake_case ): def __init__( self : Any , lowerCAmelCase__ : int , lowerCAmelCase__ : Union[str, Any]=None , lowerCAmelCase__ : int=None ) -> Optional[Any]: UpperCAmelCase = file_names UpperCAmelCase = image_transform UpperCAmelCase = label_to_id def __len__( self : Tuple ) -> List[str]: return len(self.file_names ) def __getitem__( self : Optional[int] , lowerCAmelCase__ : Tuple ) -> Dict: UpperCAmelCase = self.file_names[idx] UpperCAmelCase = PIL.Image.open(lowerCAmelCase__ ) UpperCAmelCase = raw_image.convert("RGB" ) if self.image_transform is not None: UpperCAmelCase = self.image_transform(lowerCAmelCase__ ) UpperCAmelCase = extract_label(lowerCAmelCase__ ) if self.label_to_id is not None: UpperCAmelCase = self.label_to_id[label] return {"image": image, "label": label} def _lowerCAmelCase( __A , __A ): # Initialize accelerator if args.with_tracking: UpperCAmelCase = Accelerator( cpu=args.cpu , mixed_precision=args.mixed_precision , log_with="all" , project_dir=args.project_dir ) else: UpperCAmelCase = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs UpperCAmelCase = config["lr"] UpperCAmelCase = int(config["num_epochs"] ) UpperCAmelCase = int(config["seed"] ) UpperCAmelCase = int(config["batch_size"] ) UpperCAmelCase = config["image_size"] if not isinstance(__A , (list, tuple) ): UpperCAmelCase = (image_size, image_size) # Parse out whether we are saving every epoch or after a certain number of batches if hasattr(args.checkpointing_steps , "isdigit" ): if args.checkpointing_steps == "epoch": UpperCAmelCase = args.checkpointing_steps elif args.checkpointing_steps.isdigit(): UpperCAmelCase = int(args.checkpointing_steps ) else: raise ValueError( F"Argument `checkpointing_steps` must be either a number or `epoch`. `{args.checkpointing_steps}` passed." ) else: UpperCAmelCase = None # We need to initialize the trackers we use, and also store our configuration if args.with_tracking: UpperCAmelCase = os.path.split(__A )[-1].split("." )[0] accelerator.init_trackers(__A , __A ) # Grab all the image filenames UpperCAmelCase = [os.path.join(args.data_dir , __A ) for fname in os.listdir(args.data_dir ) if fname.endswith(".jpg" )] # Build the label correspondences UpperCAmelCase = [extract_label(__A ) for fname in file_names] UpperCAmelCase = list(set(__A ) ) id_to_label.sort() UpperCAmelCase = {lbl: i for i, lbl in enumerate(__A )} # Set the seed before splitting the data. np.random.seed(__A ) torch.manual_seed(__A ) torch.cuda.manual_seed_all(__A ) # Split our filenames between train and validation UpperCAmelCase = np.random.permutation(len(__A ) ) UpperCAmelCase = int(0.8 * len(__A ) ) UpperCAmelCase = random_perm[:cut] UpperCAmelCase = random_perm[cut:] # For training we use a simple RandomResizedCrop UpperCAmelCase = Compose([RandomResizedCrop(__A , scale=(0.5, 1.0) ), ToTensor()] ) UpperCAmelCase = PetsDataset( [file_names[i] for i in train_split] , image_transform=__A , label_to_id=__A ) # For evaluation, we use a deterministic Resize UpperCAmelCase = Compose([Resize(__A ), ToTensor()] ) UpperCAmelCase = PetsDataset([file_names[i] for i in eval_split] , image_transform=__A , label_to_id=__A ) # Instantiate dataloaders. UpperCAmelCase = DataLoader(__A , shuffle=__A , batch_size=__A , num_workers=4 ) UpperCAmelCase = DataLoader(__A , shuffle=__A , batch_size=__A , num_workers=4 ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) UpperCAmelCase = create_model("resnet50d" , pretrained=__A , num_classes=len(__A ) ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). UpperCAmelCase = model.to(accelerator.device ) # Freezing the base model for param in model.parameters(): UpperCAmelCase = False for param in model.get_classifier().parameters(): UpperCAmelCase = True # We normalize the batches of images to be a bit faster. UpperCAmelCase = torch.tensor(model.default_cfg["mean"] )[None, :, None, None].to(accelerator.device ) UpperCAmelCase = torch.tensor(model.default_cfg["std"] )[None, :, None, None].to(accelerator.device ) # Instantiate optimizer UpperCAmelCase = torch.optim.Adam(params=model.parameters() , lr=lr / 25 ) # Instantiate learning rate scheduler UpperCAmelCase = OneCycleLR(optimizer=__A , max_lr=__A , epochs=__A , steps_per_epoch=len(__A ) ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = accelerator.prepare( __A , __A , __A , __A , __A ) # We need to keep track of how many total steps we have iterated over UpperCAmelCase = 0 # We also need to keep track of the starting epoch so files are named properly UpperCAmelCase = 0 # Potentially load in the weights and states from a previous save if args.resume_from_checkpoint: if args.resume_from_checkpoint is not None or args.resume_from_checkpoint != "": accelerator.print(F"Resumed from checkpoint: {args.resume_from_checkpoint}" ) accelerator.load_state(args.resume_from_checkpoint ) UpperCAmelCase = os.path.basename(args.resume_from_checkpoint ) else: # Get the most recent checkpoint UpperCAmelCase = [f.name for f in os.scandir(os.getcwd() ) if f.is_dir()] dirs.sort(key=os.path.getctime ) UpperCAmelCase = dirs[-1] # Sorts folders by date modified, most recent checkpoint is the last # Extract `epoch_{i}` or `step_{i}` UpperCAmelCase = os.path.splitext(__A )[0] if "epoch" in training_difference: UpperCAmelCase = int(training_difference.replace("epoch_" , "" ) ) + 1 UpperCAmelCase = None else: UpperCAmelCase = int(training_difference.replace("step_" , "" ) ) UpperCAmelCase = resume_step // len(__A ) resume_step -= starting_epoch * len(__A ) # Now we train the model for epoch in range(__A , __A ): model.train() if args.with_tracking: UpperCAmelCase = 0 if args.resume_from_checkpoint and epoch == starting_epoch and resume_step is not None: # We need to skip steps until we reach the resumed step UpperCAmelCase = accelerator.skip_first_batches(__A , __A ) overall_step += resume_step else: # After the first iteration though, we need to go back to the original dataloader UpperCAmelCase = train_dataloader for batch in active_dataloader: # We could avoid this line since we set the accelerator with `device_placement=True`. UpperCAmelCase = {k: v.to(accelerator.device ) for k, v in batch.items()} UpperCAmelCase = (batch["image"] - mean) / std UpperCAmelCase = model(__A ) UpperCAmelCase = torch.nn.functional.cross_entropy(__A , batch["label"] ) # We keep track of the loss at each epoch if args.with_tracking: total_loss += loss.detach().float() accelerator.backward(__A ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 if isinstance(__A , __A ): UpperCAmelCase = F"step_{overall_step}" if overall_step % checkpointing_steps == 0: if args.output_dir is not None: UpperCAmelCase = os.path.join(args.output_dir , __A ) accelerator.save_state(__A ) model.eval() UpperCAmelCase = 0 UpperCAmelCase = 0 for step, batch in enumerate(__A ): # We could avoid this line since we set the accelerator with `device_placement=True`. UpperCAmelCase = {k: v.to(accelerator.device ) for k, v in batch.items()} UpperCAmelCase = (batch["image"] - mean) / std with torch.no_grad(): UpperCAmelCase = model(__A ) UpperCAmelCase = outputs.argmax(dim=-1 ) UpperCAmelCase , UpperCAmelCase = accelerator.gather_for_metrics((predictions, batch["label"]) ) UpperCAmelCase = predictions == references num_elems += accurate_preds.shape[0] accurate += accurate_preds.long().sum() UpperCAmelCase = accurate.item() / num_elems # Use accelerator.print to print only on the main process. accelerator.print(F"epoch {epoch}: {100 * eval_metric:.2f}" ) if args.with_tracking: accelerator.log( { "accuracy": 100 * eval_metric, "train_loss": total_loss.item() / len(__A ), "epoch": epoch, } , step=__A , ) if checkpointing_steps == "epoch": UpperCAmelCase = F"epoch_{epoch}" if args.output_dir is not None: UpperCAmelCase = os.path.join(args.output_dir , __A ) accelerator.save_state(__A ) if args.with_tracking: accelerator.end_training() def _lowerCAmelCase( ): UpperCAmelCase = argparse.ArgumentParser(description="Simple example of training script." ) parser.add_argument("--data_dir" , required=__A , help="The data folder on disk." ) parser.add_argument("--fp16" , action="store_true" , help="If passed, will use FP16 training." ) parser.add_argument( "--mixed_precision" , type=__A , default=__A , choices=["no", "fp16", "bf16", "fp8"] , help="Whether to use mixed precision. Choose" "between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10." "and an Nvidia Ampere GPU." , ) parser.add_argument("--cpu" , action="store_true" , help="If passed, will train on the CPU." ) parser.add_argument( "--checkpointing_steps" , type=__A , default=__A , help="Whether the various states should be saved at the end of every n steps, or 'epoch' for each epoch." , ) parser.add_argument( "--output_dir" , type=__A , default="." , help="Optional save directory where all checkpoint folders will be stored. Default is the current working directory." , ) parser.add_argument( "--resume_from_checkpoint" , type=__A , default=__A , help="If the training should continue from a checkpoint folder." , ) parser.add_argument( "--with_tracking" , action="store_true" , help="Whether to load in all available experiment trackers from the environment and use them for logging." , ) parser.add_argument( "--project_dir" , type=__A , default="logs" , help="Location on where to store experiment tracking logs` and relevent project information" , ) UpperCAmelCase = parser.parse_args() UpperCAmelCase = {"lr": 3E-2, "num_epochs": 3, "seed": 42, "batch_size": 64, "image_size": 224} training_function(__A , __A ) if __name__ == "__main__": main()
1
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) lowerCAmelCase__ = {"configuration_unispeech": ["UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP", "UniSpeechConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ "UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST", "UniSpeechForCTC", "UniSpeechForPreTraining", "UniSpeechForSequenceClassification", "UniSpeechModel", "UniSpeechPreTrainedModel", ] if TYPE_CHECKING: from .configuration_unispeech import UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP, UniSpeechConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_unispeech import ( UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST, UniSpeechForCTC, UniSpeechForPreTraining, UniSpeechForSequenceClassification, UniSpeechModel, UniSpeechPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
1
import glob import os import random from string import ascii_lowercase, digits import cva lowerCAmelCase__ = "" lowerCAmelCase__ = "" lowerCAmelCase__ = "" lowerCAmelCase__ = 1 # (0 is vertical, 1 is horizontal) def _lowerCAmelCase( ): UpperCAmelCase , UpperCAmelCase = get_dataset(__A , __A ) print("Processing..." ) UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = update_image_and_anno(__A , __A , __A ) for index, image in enumerate(__A ): # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' UpperCAmelCase = random_chars(32 ) UpperCAmelCase = paths[index].split(os.sep )[-1].rsplit("." , 1 )[0] UpperCAmelCase = F"{OUTPUT_DIR}/{file_name}_FLIP_{letter_code}" cva.imwrite(F"/{file_root}.jpg" , __A , [cva.IMWRITE_JPEG_QUALITY, 85] ) print(F"Success {index+1}/{len(__A )} with {file_name}" ) UpperCAmelCase = [] for anno in new_annos[index]: UpperCAmelCase = F"{anno[0]} {anno[1]} {anno[2]} {anno[3]} {anno[4]}" annos_list.append(__A ) with open(F"/{file_root}.txt" , "w" ) as outfile: outfile.write("\n".join(line for line in annos_list ) ) def _lowerCAmelCase( __A , __A ): UpperCAmelCase = [] UpperCAmelCase = [] for label_file in glob.glob(os.path.join(__A , "*.txt" ) ): UpperCAmelCase = label_file.split(os.sep )[-1].rsplit("." , 1 )[0] with open(__A ) as in_file: UpperCAmelCase = in_file.readlines() UpperCAmelCase = os.path.join(__A , F"{label_name}.jpg" ) UpperCAmelCase = [] for obj_list in obj_lists: UpperCAmelCase = obj_list.rstrip("\n" ).split(" " ) boxes.append( [ int(obj[0] ), float(obj[1] ), float(obj[2] ), float(obj[3] ), float(obj[4] ), ] ) if not boxes: continue img_paths.append(__A ) labels.append(__A ) return img_paths, labels def _lowerCAmelCase( __A , __A , __A = 1 ): UpperCAmelCase = [] UpperCAmelCase = [] UpperCAmelCase = [] for idx in range(len(__A ) ): UpperCAmelCase = [] UpperCAmelCase = img_list[idx] path_list.append(__A ) UpperCAmelCase = anno_list[idx] UpperCAmelCase = cva.imread(__A ) if flip_type == 1: UpperCAmelCase = cva.flip(__A , __A ) for bbox in img_annos: UpperCAmelCase = 1 - bbox[1] new_annos.append([bbox[0], x_center_new, bbox[2], bbox[3], bbox[4]] ) elif flip_type == 0: UpperCAmelCase = cva.flip(__A , __A ) for bbox in img_annos: UpperCAmelCase = 1 - bbox[2] new_annos.append([bbox[0], bbox[1], y_center_new, bbox[3], bbox[4]] ) new_annos_lists.append(__A ) new_imgs_list.append(__A ) return new_imgs_list, new_annos_lists, path_list def _lowerCAmelCase( __A = 32 ): assert number_char > 1, "The number of character should greater than 1" UpperCAmelCase = ascii_lowercase + digits return "".join(random.choice(__A ) for _ in range(__A ) ) if __name__ == "__main__": main() print("DONE ✅")
1
1
from __future__ import annotations import numpy as np def _lowerCAmelCase( __A ): return np.maximum(0 , __A ) if __name__ == "__main__": print(np.array(relu([-1, 0, 5]))) # --> [0, 0, 5]
1
def _lowerCAmelCase( __A ): if not isinstance(__A , __A ): raise TypeError("only integers accepted as input" ) else: UpperCAmelCase = str(abs(__A ) ) UpperCAmelCase = [list(__A ) for char in range(len(__A ) )] for index in range(len(__A ) ): num_transpositions[index].pop(__A ) return max( int("".join(list(__A ) ) ) for transposition in num_transpositions ) if __name__ == "__main__": __import__("doctest").testmod()
1
1
import unittest from typing import Dict, List, Optional, Union import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import BridgeTowerImageProcessor class __magic_name__ ( unittest.TestCase ): def __init__( self : Any , lowerCAmelCase__ : str , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : Dict[str, int] = None , lowerCAmelCase__ : int = 3_2 , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : Union[int, float] = 1 / 2_5_5 , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : Optional[Union[float, List[float]]] = [0.48_145_466, 0.4_578_275, 0.40_821_073] , lowerCAmelCase__ : Optional[Union[float, List[float]]] = [0.26_862_954, 0.26_130_258, 0.27_577_711] , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : Union[str, Any]=7 , lowerCAmelCase__ : Dict=3_0 , lowerCAmelCase__ : Dict=4_0_0 , lowerCAmelCase__ : Dict=3 , ) -> Union[str, Any]: UpperCAmelCase = parent UpperCAmelCase = do_resize UpperCAmelCase = size if size is not None else {"shortest_edge": 2_8_8} UpperCAmelCase = size_divisor UpperCAmelCase = do_rescale UpperCAmelCase = rescale_factor UpperCAmelCase = do_normalize UpperCAmelCase = do_center_crop UpperCAmelCase = image_mean UpperCAmelCase = image_std UpperCAmelCase = do_pad UpperCAmelCase = batch_size UpperCAmelCase = num_channels UpperCAmelCase = min_resolution UpperCAmelCase = max_resolution def _UpperCamelCase ( self : Union[str, Any] ) -> str: return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, "size_divisor": self.size_divisor, } def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Union[str, Any]=False ) -> int: if not batched: UpperCAmelCase = self.size["shortest_edge"] UpperCAmelCase = image_inputs[0] if isinstance(lowerCAmelCase__ , Image.Image ): UpperCAmelCase , UpperCAmelCase = image.size else: UpperCAmelCase , UpperCAmelCase = image.shape[1], image.shape[2] UpperCAmelCase = size / min(lowerCAmelCase__ , lowerCAmelCase__ ) if h < w: UpperCAmelCase , UpperCAmelCase = size, scale * w else: UpperCAmelCase , UpperCAmelCase = scale * h, size UpperCAmelCase = int((1_3_3_3 / 8_0_0) * size ) if max(lowerCAmelCase__ , lowerCAmelCase__ ) > max_size: UpperCAmelCase = max_size / max(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase = newh * scale UpperCAmelCase = neww * scale UpperCAmelCase , UpperCAmelCase = int(newh + 0.5 ), int(neww + 0.5 ) UpperCAmelCase , UpperCAmelCase = ( newh // self.size_divisor * self.size_divisor, neww // self.size_divisor * self.size_divisor, ) else: UpperCAmelCase = [] for image in image_inputs: UpperCAmelCase , UpperCAmelCase = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) UpperCAmelCase = max(lowerCAmelCase__ , key=lambda lowerCAmelCase__ : item[0] )[0] UpperCAmelCase = max(lowerCAmelCase__ , key=lambda lowerCAmelCase__ : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class __magic_name__ ( _snake_case , unittest.TestCase ): UpperCAmelCase = BridgeTowerImageProcessor if is_vision_available() else None def _UpperCamelCase ( self : Dict ) -> Tuple: UpperCAmelCase = BridgeTowerImageProcessingTester(self ) @property def _UpperCamelCase ( self : List[str] ) -> Union[str, Any]: return self.image_processor_tester.prepare_image_processor_dict() def _UpperCamelCase ( self : str ) -> Dict: UpperCAmelCase = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(lowerCAmelCase__ , "image_mean" ) ) self.assertTrue(hasattr(lowerCAmelCase__ , "image_std" ) ) self.assertTrue(hasattr(lowerCAmelCase__ , "do_normalize" ) ) self.assertTrue(hasattr(lowerCAmelCase__ , "do_resize" ) ) self.assertTrue(hasattr(lowerCAmelCase__ , "size" ) ) self.assertTrue(hasattr(lowerCAmelCase__ , "size_divisor" ) ) def _UpperCamelCase ( self : Dict ) -> Tuple: pass def _UpperCamelCase ( self : Dict ) -> int: # Initialize image processor UpperCAmelCase = self.image_processing_class(**self.image_processor_dict ) # create random PIL images UpperCAmelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCAmelCase__ , Image.Image ) # Test not batched input UpperCAmelCase = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values UpperCAmelCase , UpperCAmelCase = self.image_processor_tester.get_expected_values(lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched UpperCAmelCase = image_processing(lowerCAmelCase__ , return_tensors="pt" ).pixel_values UpperCAmelCase , UpperCAmelCase = self.image_processor_tester.get_expected_values(lowerCAmelCase__ , batched=lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def _UpperCamelCase ( self : Any ) -> Union[str, Any]: # Initialize image processor UpperCAmelCase = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors UpperCAmelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase__ , numpify=lowerCAmelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCAmelCase__ , np.ndarray ) # Test not batched input UpperCAmelCase = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values UpperCAmelCase , UpperCAmelCase = self.image_processor_tester.get_expected_values(lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched UpperCAmelCase = image_processing(lowerCAmelCase__ , return_tensors="pt" ).pixel_values UpperCAmelCase , UpperCAmelCase = self.image_processor_tester.get_expected_values(lowerCAmelCase__ , batched=lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def _UpperCamelCase ( self : Optional[int] ) -> Dict: # Initialize image processor UpperCAmelCase = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors UpperCAmelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase__ , torchify=lowerCAmelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCAmelCase__ , torch.Tensor ) # Test not batched input UpperCAmelCase = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values UpperCAmelCase , UpperCAmelCase = self.image_processor_tester.get_expected_values(lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched UpperCAmelCase = image_processing(lowerCAmelCase__ , return_tensors="pt" ).pixel_values UpperCAmelCase , UpperCAmelCase = self.image_processor_tester.get_expected_values(lowerCAmelCase__ , batched=lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , )
1
import logging import re import pytorch_quantization import pytorch_quantization.nn as quant_nn import torch from pytorch_quantization import calib from pytorch_quantization.tensor_quant import QuantDescriptor lowerCAmelCase__ = logging.getLogger(__name__) lowerCAmelCase__ = 50 # max width of layer names lowerCAmelCase__ = 70 # max width of quantizer names def _lowerCAmelCase( __A ): UpperCAmelCase = parser.add_argument_group("quant_trainer arguments" ) group.add_argument("--wprec" , type=__A , default=8 , help="weight precision" ) group.add_argument("--aprec" , type=__A , default=8 , help="activation precision" ) group.add_argument("--quant-per-tensor" , action="store_true" , help="per tensor weight scaling" ) group.add_argument("--quant-disable" , action="store_true" , help="disable all quantizers" ) group.add_argument("--quant-disable-embeddings" , action="store_true" , help="disable all embeddings quantizers" ) group.add_argument("--quant-disable-keyword" , type=__A , nargs="+" , help="disable quantizers by keyword" ) group.add_argument("--quant-disable-layer-module" , type=__A , help="disable quantizers by keyword under layer." ) group.add_argument("--quant-enable-layer-module" , type=__A , help="enable quantizers by keyword under layer" ) group.add_argument("--calibrator" , default="max" , help="which quantization range calibrator to use" ) group.add_argument("--percentile" , default=__A , type=__A , help="percentile for PercentileCalibrator" ) group.add_argument("--fuse-qkv" , action="store_true" , help="use the same scale factor for qkv" ) group.add_argument("--clip-gelu" , metavar="N" , type=__A , help="clip gelu output maximum value to N" ) group.add_argument( "--recalibrate-weights" , action="store_true" , help=( "recalibrate weight amaxes by taking the max of the weights." " amaxes will be computed with the current quantization granularity (axis)." ) , ) def _lowerCAmelCase( __A ): if args.calibrator == "max": UpperCAmelCase = "max" elif args.calibrator == "percentile": if args.percentile is None: raise ValueError("Specify --percentile when using percentile calibrator" ) UpperCAmelCase = "histogram" elif args.calibrator == "mse": UpperCAmelCase = "histogram" else: raise ValueError(F"Invalid calibrator {args.calibrator}" ) UpperCAmelCase = QuantDescriptor(num_bits=args.aprec , calib_method=__A ) UpperCAmelCase = QuantDescriptor(num_bits=args.wprec , axis=(None if args.quant_per_tensor else (0,)) ) quant_nn.QuantLinear.set_default_quant_desc_input(__A ) quant_nn.QuantLinear.set_default_quant_desc_weight(__A ) def _lowerCAmelCase( __A , __A , __A=False , __A=False ): logger.info("Configuring Model for Quantization" ) logger.info(F"using quantization package {pytorch_quantization.__file__}" ) if not calib: if args.quant_disable_embeddings: set_quantizer_by_name(__A , ["embeddings"] , which="weight" , _disabled=__A ) if args.quant_disable: set_quantizer_by_name(__A , [""] , _disabled=__A ) if args.quant_disable_keyword: set_quantizer_by_name(__A , args.quant_disable_keyword , _disabled=__A ) if args.quant_disable_layer_module: set_quantizer_by_name(__A , [r"layer.\d+." + args.quant_disable_layer_module] , _disabled=__A ) if args.quant_enable_layer_module: set_quantizer_by_name(__A , [r"layer.\d+." + args.quant_enable_layer_module] , _disabled=__A ) if args.recalibrate_weights: recalibrate_weights(__A ) if args.fuse_qkv: fuse_qkv(__A , __A ) if args.clip_gelu: clip_gelu(__A , args.clip_gelu ) # if args.local_rank in [-1, 0] and not calib: print_quant_summary(__A ) def _lowerCAmelCase( __A ): logger.info("Enabling Calibration" ) for name, module in model.named_modules(): if name.endswith("_quantizer" ): if module._calibrator is not None: module.disable_quant() module.enable_calib() else: module.disable() logger.info(F"{name:80}: {module}" ) def _lowerCAmelCase( __A , __A ): logger.info("Loading calibrated amax" ) for name, module in model.named_modules(): if name.endswith("_quantizer" ): if module._calibrator is not None: if isinstance(module._calibrator , calib.MaxCalibrator ): module.load_calib_amax() else: module.load_calib_amax("percentile" , percentile=args.percentile ) module.enable_quant() module.disable_calib() else: module.enable() model.cuda() print_quant_summary(__A ) def _lowerCAmelCase( __A , __A ): def fusea(__A , __A , __A ): for mod in [qq, qk, qv]: if not hasattr(__A , "_amax" ): print(" WARNING: NO AMAX BUFFER" ) return UpperCAmelCase = qq._amax.detach().item() UpperCAmelCase = qk._amax.detach().item() UpperCAmelCase = qv._amax.detach().item() UpperCAmelCase = max(__A , __A , __A ) qq._amax.fill_(__A ) qk._amax.fill_(__A ) qv._amax.fill_(__A ) logger.info(F" q={q:5.2f} k={k:5.2f} v={v:5.2f} -> {amax:5.2f}" ) for name, mod in model.named_modules(): if name.endswith(".attention.self" ): logger.info(F"FUSE_QKV: {name:{name_width}}" ) fusea(mod.matmul_q_input_quantizer , mod.matmul_k_input_quantizer , mod.matmul_v_input_quantizer ) if args.quant_per_tensor: fusea(mod.query._weight_quantizer , mod.key._weight_quantizer , mod.value._weight_quantizer ) def _lowerCAmelCase( __A , __A ): for name, mod in model.named_modules(): if name.endswith(".output.dense" ) and not name.endswith("attention.output.dense" ): UpperCAmelCase = mod._input_quantizer._amax.data.detach().item() mod._input_quantizer._amax.data.detach().clamp_(max=__A ) UpperCAmelCase = mod._input_quantizer._amax.data.detach().item() logger.info(F"CLIP_GELU: {name:{name_width}} amax: {amax_init:5.2f} -> {amax:5.2f}" ) def _lowerCAmelCase( __A ): for name, mod in model.named_modules(): if hasattr(__A , "_weight_quantizer" ) and mod._weight_quantizer.axis is not None: UpperCAmelCase = mod.weight.shape[0] UpperCAmelCase = mod._weight_quantizer._amax.detach() UpperCAmelCase = torch.ones(__A , dtype=amax.dtype , device=amax.device ) * amax print(F"expanding {name} {amax} -> {mod._weight_quantizer._amax}" ) def _lowerCAmelCase( __A ): for name, mod in model.named_modules(): if hasattr(__A , "_weight_quantizer" ): if not hasattr(mod.weight_quantizer , "_amax" ): print("RECALIB: {name:{name_width}} WARNING: NO AMAX BUFFER" ) continue # determine which axes to reduce across # e.g. a 4D tensor quantized per axis 0 should reduce over (1,2,3) UpperCAmelCase = set() if mod._weight_quantizer.axis is None else set(mod._weight_quantizer.axis ) UpperCAmelCase = set(range(len(mod.weight.size() ) ) ) - axis_set UpperCAmelCase = pytorch_quantization.utils.reduce_amax(mod.weight , axis=__A , keepdims=__A ).detach() logger.info(F"RECALIB: {name:{name_width}} {mod._weight_quantizer._amax.flatten()} -> {amax.flatten()}" ) UpperCAmelCase = amax def _lowerCAmelCase( __A , __A=25 , __A=180 , __A=None ): if ignore is None: UpperCAmelCase = [] elif not isinstance(__A , __A ): UpperCAmelCase = [ignore] UpperCAmelCase = 0 for name, mod in model.named_modules(): if not hasattr(__A , "weight" ): continue UpperCAmelCase = max(__A , len(__A ) ) for name, mod in model.named_modules(): UpperCAmelCase = getattr(__A , "_input_quantizer" , __A ) UpperCAmelCase = getattr(__A , "_weight_quantizer" , __A ) if not hasattr(__A , "weight" ): continue if type(__A ) in ignore: continue if [True for s in ignore if type(__A ) is str and s in name]: continue UpperCAmelCase = F"Act:{input_q.extra_repr()}" UpperCAmelCase = F"Wgt:{weight_q.extra_repr()}" UpperCAmelCase = F"{name:{name_width}} {act_str} {wgt_str}" if len(__A ) <= line_width: logger.info(__A ) else: logger.info(F"{name:{name_width}} {act_str}" ) logger.info(F"{' ':{name_width}} {wgt_str}" ) def _lowerCAmelCase( __A ): UpperCAmelCase = 0 for name, mod in model.named_modules(): if isinstance(__A , pytorch_quantization.nn.TensorQuantizer ): print(F"{name:80} {mod}" ) count += 1 print(F"{count} TensorQuantizers found in model" ) def _lowerCAmelCase( __A , __A , __A , __A , __A ): UpperCAmelCase = getattr(__A , __A , __A ) if quantizer_mod is not None: assert hasattr(__A , __A ) setattr(__A , __A , __A ) else: logger.warning(F"{name} has no {quantizer}" ) def _lowerCAmelCase( __A , __A , __A="both" , **__A ): UpperCAmelCase = F"Warning: changing {which} quantizers of {name:{qname_width}}" for k, v in kwargs.items(): s += F" {k}={v}" if which in ["input", "both"]: set_quantizer(__A , __A , "_input_quantizer" , __A , __A ) if which in ["weight", "both"]: set_quantizer(__A , __A , "_weight_quantizer" , __A , __A ) logger.info(__A ) def _lowerCAmelCase( __A , __A , **__A ): for name, mod in model.named_modules(): if hasattr(__A , "_input_quantizer" ) or hasattr(__A , "_weight_quantizer" ): for n in names: if re.search(__A , __A ): set_quantizers(__A , __A , **__A ) elif name.endswith("_quantizer" ): for n in names: if re.search(__A , __A ): UpperCAmelCase = F"Warning: changing {name:{name_width}}" for k, v in kwargs.items(): s += F" {k}={v}" setattr(__A , __A , __A ) logger.info(__A )
1
1
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 __magic_name__ ( _snake_case ): def __init__( self : Optional[Any] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Union[str, Any]=1_3 , lowerCAmelCase__ : List[Any]=7 , lowerCAmelCase__ : Dict=True , lowerCAmelCase__ : int=True , lowerCAmelCase__ : List[str]=False , lowerCAmelCase__ : Union[str, Any]=True , lowerCAmelCase__ : str=9_9 , lowerCAmelCase__ : int=3_2 , lowerCAmelCase__ : Optional[Any]=5 , lowerCAmelCase__ : Tuple=4 , lowerCAmelCase__ : Optional[int]=6_4 , lowerCAmelCase__ : int="gelu" , lowerCAmelCase__ : Optional[int]=0.1 , lowerCAmelCase__ : int=0.1 , lowerCAmelCase__ : Any=5_1_2 , lowerCAmelCase__ : Dict=1_6 , lowerCAmelCase__ : List[str]=2 , lowerCAmelCase__ : Tuple=0.02 , lowerCAmelCase__ : Any=3 , lowerCAmelCase__ : Optional[int]=4 , lowerCAmelCase__ : List[str]=None , lowerCAmelCase__ : Union[str, Any]=2 , lowerCAmelCase__ : Dict=2 , lowerCAmelCase__ : Dict=2 , lowerCAmelCase__ : int=2 , lowerCAmelCase__ : List[str]=4 , lowerCAmelCase__ : Any=1 , ) -> List[str]: UpperCAmelCase = parent UpperCAmelCase = batch_size UpperCAmelCase = seq_length UpperCAmelCase = is_training UpperCAmelCase = use_input_mask UpperCAmelCase = use_token_type_ids UpperCAmelCase = use_labels UpperCAmelCase = vocab_size UpperCAmelCase = hidden_size UpperCAmelCase = num_hidden_layers UpperCAmelCase = num_attention_heads UpperCAmelCase = intermediate_size UpperCAmelCase = hidden_act UpperCAmelCase = hidden_dropout_prob UpperCAmelCase = attention_probs_dropout_prob UpperCAmelCase = max_position_embeddings UpperCAmelCase = type_vocab_size UpperCAmelCase = type_sequence_label_size UpperCAmelCase = initializer_range UpperCAmelCase = num_labels UpperCAmelCase = num_choices UpperCAmelCase = scope UpperCAmelCase = q_groups UpperCAmelCase = k_groups UpperCAmelCase = v_groups UpperCAmelCase = post_attention_groups UpperCAmelCase = intermediate_groups UpperCAmelCase = output_groups def _UpperCamelCase ( self : Optional[Any] ) -> str: UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase = None if self.use_input_mask: UpperCAmelCase = random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase = None UpperCAmelCase = None UpperCAmelCase = None if self.use_labels: UpperCAmelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCAmelCase = ids_tensor([self.batch_size] , self.num_choices ) UpperCAmelCase = self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def _UpperCamelCase ( self : str ) -> Optional[int]: 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 : Union[str, Any] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : str , lowerCAmelCase__ : int , lowerCAmelCase__ : Any ) -> Optional[Any]: UpperCAmelCase = SqueezeBertModel(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() UpperCAmelCase = model(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase = model(lowerCAmelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _UpperCamelCase ( self : Tuple , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Dict ) -> Optional[int]: UpperCAmelCase = SqueezeBertForMaskedLM(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() UpperCAmelCase = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _UpperCamelCase ( self : Optional[int] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : str , lowerCAmelCase__ : Any , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[Any] ) -> int: UpperCAmelCase = SqueezeBertForQuestionAnswering(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() UpperCAmelCase = model( lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , start_positions=lowerCAmelCase__ , end_positions=lowerCAmelCase__ ) 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 : Dict , lowerCAmelCase__ : str , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : str , lowerCAmelCase__ : str , lowerCAmelCase__ : Any ) -> List[Any]: UpperCAmelCase = self.num_labels UpperCAmelCase = SqueezeBertForSequenceClassification(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() UpperCAmelCase = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _UpperCamelCase ( self : str , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : Union[str, Any] ) -> Union[str, Any]: UpperCAmelCase = self.num_labels UpperCAmelCase = SqueezeBertForTokenClassification(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() UpperCAmelCase = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def _UpperCamelCase ( self : Optional[int] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Dict ) -> Any: UpperCAmelCase = self.num_choices UpperCAmelCase = SqueezeBertForMultipleChoice(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() UpperCAmelCase = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() UpperCAmelCase = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() UpperCAmelCase = model( lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , labels=lowerCAmelCase__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def _UpperCamelCase ( self : int ) -> Dict: UpperCAmelCase = self.prepare_config_and_inputs() ((UpperCAmelCase) , (UpperCAmelCase) , (UpperCAmelCase) , (UpperCAmelCase) , (UpperCAmelCase) , (UpperCAmelCase)) = config_and_inputs UpperCAmelCase = {"input_ids": input_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class __magic_name__ ( _snake_case , _snake_case , unittest.TestCase ): UpperCAmelCase = ( ( SqueezeBertModel, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, ) if is_torch_available() else None ) UpperCAmelCase = ( { """feature-extraction""": SqueezeBertModel, """fill-mask""": SqueezeBertForMaskedLM, """question-answering""": SqueezeBertForQuestionAnswering, """text-classification""": SqueezeBertForSequenceClassification, """token-classification""": SqueezeBertForTokenClassification, """zero-shot""": SqueezeBertForSequenceClassification, } if is_torch_available() else {} ) UpperCAmelCase = False UpperCAmelCase = True UpperCAmelCase = False def _UpperCamelCase ( self : List[Any] ) -> Optional[Any]: UpperCAmelCase = SqueezeBertModelTester(self ) UpperCAmelCase = ConfigTester(self , config_class=lowerCAmelCase__ , dim=3_7 ) def _UpperCamelCase ( self : str ) -> Any: self.config_tester.run_common_tests() def _UpperCamelCase ( self : Any ) -> Optional[int]: UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_model(*lowerCAmelCase__ ) def _UpperCamelCase ( self : Union[str, Any] ) -> int: UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_masked_lm(*lowerCAmelCase__ ) def _UpperCamelCase ( self : List[Any] ) -> Dict: UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_question_answering(*lowerCAmelCase__ ) def _UpperCamelCase ( self : Optional[Any] ) -> List[str]: UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_sequence_classification(*lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] ) -> str: UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_token_classification(*lowerCAmelCase__ ) def _UpperCamelCase ( self : List[Any] ) -> List[str]: UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_multiple_choice(*lowerCAmelCase__ ) @slow def _UpperCamelCase ( self : Dict ) -> Optional[int]: for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase = SqueezeBertModel.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) @require_sentencepiece @require_tokenizers @require_torch class __magic_name__ ( unittest.TestCase ): @slow def _UpperCamelCase ( self : Any ) -> Any: UpperCAmelCase = SqueezeBertForSequenceClassification.from_pretrained("squeezebert/squeezebert-mnli" ) UpperCAmelCase = torch.tensor([[1, 2_9_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 1_3, 1_5_8_8, 2]] ) UpperCAmelCase = model(lowerCAmelCase__ )[0] UpperCAmelCase = torch.Size((1, 3) ) self.assertEqual(output.shape , lowerCAmelCase__ ) UpperCAmelCase = torch.tensor([[0.6_401, -0.0_349, -0.6_041]] ) self.assertTrue(torch.allclose(lowerCAmelCase__ , lowerCAmelCase__ , atol=1e-4 ) )
1
def _lowerCAmelCase( __A ): assert column_title.isupper() UpperCAmelCase = 0 UpperCAmelCase = len(__A ) - 1 UpperCAmelCase = 0 while index >= 0: UpperCAmelCase = (ord(column_title[index] ) - 64) * pow(26 , __A ) answer += value power += 1 index -= 1 return answer if __name__ == "__main__": from doctest import testmod testmod()
1
1