code
stringlengths
82
54.1k
code_codestyle
int64
0
699
style_context
stringlengths
111
35.6k
style_context_codestyle
int64
0
699
label
int64
0
1
def A ( __UpperCamelCase ) -> list[list[float]]: A__ = [] for data in source_data: for i, el in enumerate(__UpperCamelCase ): if len(__UpperCamelCase ) < i + 1: data_lists.append([] ) data_lists[i].append(float(__UpperCamelCase ) ) return data_lists def A ( __UpperCamelCase , __UpperCamelCase ) -> list[list[float]]: A__ = [] for dlist, weight in zip(__UpperCamelCase , __UpperCamelCase ): A__ = min(__UpperCamelCase ) A__ = max(__UpperCamelCase ) A__ = [] # for weight 0 score is 1 - actual score if weight == 0: for item in dlist: try: score.append(1 - ((item - mind) / (maxd - mind)) ) except ZeroDivisionError: score.append(1 ) elif weight == 1: for item in dlist: try: score.append((item - mind) / (maxd - mind) ) except ZeroDivisionError: score.append(0 ) # weight not 0 or 1 else: A__ = f'''Invalid weight of {weight:f} provided''' raise ValueError(__UpperCamelCase ) score_lists.append(__UpperCamelCase ) return score_lists def A ( __UpperCamelCase ) -> list[float]: A__ = [0 for i in range(len(score_lists[0] ) )] for slist in score_lists: for j, ele in enumerate(__UpperCamelCase ): A__ = final_scores[j] + ele return final_scores def A ( __UpperCamelCase , __UpperCamelCase ) -> list[list[float]]: A__ = get_data(__UpperCamelCase ) A__ = calculate_each_score(__UpperCamelCase , __UpperCamelCase ) A__ = generate_final_scores(__UpperCamelCase ) # append scores to source data for i, ele in enumerate(__UpperCamelCase ): source_data[i].append(__UpperCamelCase ) return source_data
9
def A ( __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: A__ = 0 A__ = len(__UpperCamelCase ) - 1 while left <= right: # avoid divided by 0 during interpolation if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: return left else: return None A__ = left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left] ) # out of range check if point < 0 or point >= len(__UpperCamelCase ): return None A__ = sorted_collection[point] if current_item == item: return point else: if point < left: A__ = left A__ = point elif point > right: A__ = right A__ = point else: if item < current_item: A__ = point - 1 else: A__ = point + 1 return None def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: # avoid divided by 0 during interpolation if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: return left else: return None A__ = left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left] ) # out of range check if point < 0 or point >= len(__UpperCamelCase ): return None if sorted_collection[point] == item: return point elif point < left: return interpolation_search_by_recursion(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) elif point > right: return interpolation_search_by_recursion(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) else: if sorted_collection[point] > item: return interpolation_search_by_recursion( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , point - 1 ) else: return interpolation_search_by_recursion( __UpperCamelCase , __UpperCamelCase , point + 1 , __UpperCamelCase ) def A ( __UpperCamelCase ) -> List[str]: if collection != sorted(__UpperCamelCase ): raise ValueError('Collection must be ascending sorted' ) return True if __name__ == "__main__": import sys SCREAMING_SNAKE_CASE__ = 0 if debug == 1: SCREAMING_SNAKE_CASE__ = [1_0, 3_0, 4_0, 4_5, 5_0, 6_6, 7_7, 9_3] try: __assert_sorted(collection) except ValueError: sys.exit('''Sequence must be ascending sorted to apply interpolation search''') SCREAMING_SNAKE_CASE__ = 6_7 SCREAMING_SNAKE_CASE__ = interpolation_search(collection, target) if result is not None: print(f'{target} found at positions: {result}') else: print('''Not found''')
9
1
from dataclasses import dataclass, field from typing import Optional from transformers import AutoConfig, AutoImageProcessor, AutoTokenizer, FlaxVisionEncoderDecoderModel, HfArgumentParser @dataclass class __lowerCAmelCase : """simple docstring""" A__ : str = field( metadata={"help": "The output directory where the model will be written."} , ) A__ : str = field( metadata={ "help": ( "The encoder model checkpoint for weights initialization." "Don't set if you want to train an encoder model from scratch." ) } , ) A__ : str = field( metadata={ "help": ( "The decoder model checkpoint for weights initialization." "Don't set if you want to train a decoder model from scratch." ) } , ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "Pretrained encoder config name or path if not the same as encoder_model_name"} ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "Pretrained decoder config name or path if not the same as decoder_model_name"} ) def A ( ) -> Dict: A__ = HfArgumentParser((ModelArguments,) ) ((A__) , ) = parser.parse_args_into_dataclasses() # Load pretrained model and tokenizer # Use explicit specified encoder config if model_args.encoder_config_name: A__ = AutoConfig.from_pretrained(model_args.encoder_config_name ) # Use pretrained encoder model's config else: A__ = AutoConfig.from_pretrained(model_args.encoder_model_name_or_path ) # Use explicit specified decoder config if model_args.decoder_config_name: A__ = AutoConfig.from_pretrained(model_args.decoder_config_name ) # Use pretrained decoder model's config else: A__ = AutoConfig.from_pretrained(model_args.decoder_model_name_or_path ) # necessary for `from_encoder_decoder_pretrained` when `decoder_config` is passed A__ = True A__ = True A__ = FlaxVisionEncoderDecoderModel.from_encoder_decoder_pretrained( encoder_pretrained_model_name_or_path=model_args.encoder_model_name_or_path , decoder_pretrained_model_name_or_path=model_args.decoder_model_name_or_path , encoder_config=__UpperCamelCase , decoder_config=__UpperCamelCase , ) # GPT2 only has bos/eos tokens but not decoder_start/pad tokens A__ = decoder_config.decoder_start_token_id A__ = decoder_config.pad_token_id if decoder_start_token_id is None: A__ = decoder_config.bos_token_id if pad_token_id is None: A__ = decoder_config.eos_token_id # This is necessary to make Flax's generate() work A__ = decoder_config.eos_token_id A__ = decoder_start_token_id A__ = pad_token_id A__ = AutoImageProcessor.from_pretrained(model_args.encoder_model_name_or_path ) A__ = AutoTokenizer.from_pretrained(model_args.decoder_model_name_or_path ) A__ = tokenizer.convert_ids_to_tokens(model.config.pad_token_id ) model.save_pretrained(model_args.output_dir ) image_processor.save_pretrained(model_args.output_dir ) tokenizer.save_pretrained(model_args.output_dir ) if __name__ == "__main__": main()
9
import warnings from ...utils import logging from .image_processing_clip import CLIPImageProcessor SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Dict , *_snake_case : int , **_snake_case : Optional[int] ): """simple docstring""" warnings.warn( 'The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please' ' use CLIPImageProcessor instead.' , _snake_case , ) super().__init__(*_snake_case , **_snake_case )
9
1
SCREAMING_SNAKE_CASE__ = [ '''DownloadConfig''', '''DownloadManager''', '''DownloadMode''', '''StreamingDownloadManager''', ] from .download_config import DownloadConfig from .download_manager import DownloadManager, DownloadMode from .streaming_download_manager import StreamingDownloadManager
9
import numpy as np import skfuzzy as fuzz if __name__ == "__main__": # Create universe of discourse in Python using linspace () SCREAMING_SNAKE_CASE__ = np.linspace(start=0, stop=7_5, num=7_5, endpoint=True, retstep=False) # Create two fuzzy sets by defining any membership function # (trapmf(), gbellmf(), gaussmf(), etc). SCREAMING_SNAKE_CASE__ = [0, 2_5, 5_0] SCREAMING_SNAKE_CASE__ = [2_5, 5_0, 7_5] SCREAMING_SNAKE_CASE__ = fuzz.membership.trimf(X, abca) SCREAMING_SNAKE_CASE__ = fuzz.membership.trimf(X, abca) # Compute the different operations using inbuilt functions. SCREAMING_SNAKE_CASE__ = np.ones(7_5) SCREAMING_SNAKE_CASE__ = np.zeros((7_5,)) # 1. Union = max(µA(x), µB(x)) SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_or(X, young, X, middle_aged)[1] # 2. Intersection = min(µA(x), µB(x)) SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_and(X, young, X, middle_aged)[1] # 3. Complement (A) = (1- min(µA(x)) SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_not(young) # 4. Difference (A/B) = min(µA(x),(1- µB(x))) SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_and(X, young, X, fuzz.fuzzy_not(middle_aged)[1])[1] # 5. Algebraic Sum = [µA(x) + µB(x) – (µA(x) * µB(x))] SCREAMING_SNAKE_CASE__ = young + middle_aged - (young * middle_aged) # 6. Algebraic Product = (µA(x) * µB(x)) SCREAMING_SNAKE_CASE__ = young * middle_aged # 7. Bounded Sum = min[1,(µA(x), µB(x))] SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_and(X, one, X, young + middle_aged)[1] # 8. Bounded difference = min[0,(µA(x), µB(x))] SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_or(X, zero, X, young - middle_aged)[1] # max-min composition # max-product composition # Plot each set A, set B and each operation result using plot() and subplot(). from matplotlib import pyplot as plt plt.figure() plt.subplot(4, 3, 1) plt.plot(X, young) plt.title('''Young''') plt.grid(True) plt.subplot(4, 3, 2) plt.plot(X, middle_aged) plt.title('''Middle aged''') plt.grid(True) plt.subplot(4, 3, 3) plt.plot(X, union) plt.title('''union''') plt.grid(True) plt.subplot(4, 3, 4) plt.plot(X, intersection) plt.title('''intersection''') plt.grid(True) plt.subplot(4, 3, 5) plt.plot(X, complement_a) plt.title('''complement_a''') plt.grid(True) plt.subplot(4, 3, 6) plt.plot(X, difference) plt.title('''difference a/b''') plt.grid(True) plt.subplot(4, 3, 7) plt.plot(X, alg_sum) plt.title('''alg_sum''') plt.grid(True) plt.subplot(4, 3, 8) plt.plot(X, alg_product) plt.title('''alg_product''') plt.grid(True) plt.subplot(4, 3, 9) plt.plot(X, bdd_sum) plt.title('''bdd_sum''') plt.grid(True) plt.subplot(4, 3, 1_0) plt.plot(X, bdd_difference) plt.title('''bdd_difference''') plt.grid(True) plt.subplots_adjust(hspace=0.5) plt.show()
9
1
import unittest from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __lowerCAmelCase : """simple docstring""" @staticmethod def _a ( *_snake_case : int , **_snake_case : List[str] ): """simple docstring""" pass @is_pipeline_test @require_vision @require_torch class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" A__ : List[str] = MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING def _a ( self : Any , _snake_case : Union[str, Any] , _snake_case : Tuple , _snake_case : Optional[Any] ): """simple docstring""" A__ = pipeline( 'zero-shot-object-detection' , model='hf-internal-testing/tiny-random-owlvit-object-detection' ) A__ = [ { 'image': './tests/fixtures/tests_samples/COCO/000000039769.png', 'candidate_labels': ['cat', 'remote', 'couch'], } ] return object_detector, examples def _a ( self : int , _snake_case : int , _snake_case : List[str] ): """simple docstring""" A__ = object_detector(examples[0] , threshold=0.0 ) A__ = len(_snake_case ) self.assertGreater(_snake_case , 0 ) self.assertEqual( _snake_case , [ { 'score': ANY(_snake_case ), 'label': ANY(_snake_case ), 'box': {'xmin': ANY(_snake_case ), 'ymin': ANY(_snake_case ), 'xmax': ANY(_snake_case ), 'ymax': ANY(_snake_case )}, } for i in range(_snake_case ) ] , ) @require_tf @unittest.skip('Zero Shot Object Detection not implemented in TF' ) def _a ( self : List[str] ): """simple docstring""" pass @require_torch def _a ( self : Optional[int] ): """simple docstring""" A__ = pipeline( 'zero-shot-object-detection' , model='hf-internal-testing/tiny-random-owlvit-object-detection' ) A__ = object_detector( './tests/fixtures/tests_samples/COCO/000000039769.png' , candidate_labels=['cat', 'remote', 'couch'] , threshold=0.64 , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}}, {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}}, {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}}, ] , ) A__ = object_detector( [ { 'image': './tests/fixtures/tests_samples/COCO/000000039769.png', 'candidate_labels': ['cat', 'remote', 'couch'], } ] , threshold=0.64 , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ [ {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}}, {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}}, {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}}, ] ] , ) @require_torch @slow def _a ( self : int ): """simple docstring""" A__ = pipeline('zero-shot-object-detection' ) A__ = object_detector( 'http://images.cocodataset.org/val2017/000000039769.jpg' , candidate_labels=['cat', 'remote', 'couch'] , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}}, {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}}, {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}}, ] , ) A__ = object_detector( [ { 'image': 'http://images.cocodataset.org/val2017/000000039769.jpg', 'candidate_labels': ['cat', 'remote', 'couch'], }, { 'image': 'http://images.cocodataset.org/val2017/000000039769.jpg', 'candidate_labels': ['cat', 'remote', 'couch'], }, ] , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}}, {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}}, {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}}, ], [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}}, {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}}, {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}}, ], ] , ) @require_tf @unittest.skip('Zero Shot Object Detection not implemented in TF' ) def _a ( self : int ): """simple docstring""" pass @require_torch @slow def _a ( self : str ): """simple docstring""" A__ = 0.2 A__ = pipeline('zero-shot-object-detection' ) A__ = object_detector( 'http://images.cocodataset.org/val2017/000000039769.jpg' , candidate_labels=['cat', 'remote', 'couch'] , threshold=_snake_case , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}}, ] , ) @require_torch @slow def _a ( self : Any ): """simple docstring""" A__ = 2 A__ = pipeline('zero-shot-object-detection' ) A__ = object_detector( 'http://images.cocodataset.org/val2017/000000039769.jpg' , candidate_labels=['cat', 'remote', 'couch'] , top_k=_snake_case , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, ] , )
9
import unittest from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __lowerCAmelCase : """simple docstring""" @staticmethod def _a ( *_snake_case : int , **_snake_case : List[str] ): """simple docstring""" pass @is_pipeline_test @require_vision @require_torch class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" A__ : List[str] = MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING def _a ( self : Any , _snake_case : Union[str, Any] , _snake_case : Tuple , _snake_case : Optional[Any] ): """simple docstring""" A__ = pipeline( 'zero-shot-object-detection' , model='hf-internal-testing/tiny-random-owlvit-object-detection' ) A__ = [ { 'image': './tests/fixtures/tests_samples/COCO/000000039769.png', 'candidate_labels': ['cat', 'remote', 'couch'], } ] return object_detector, examples def _a ( self : int , _snake_case : int , _snake_case : List[str] ): """simple docstring""" A__ = object_detector(examples[0] , threshold=0.0 ) A__ = len(_snake_case ) self.assertGreater(_snake_case , 0 ) self.assertEqual( _snake_case , [ { 'score': ANY(_snake_case ), 'label': ANY(_snake_case ), 'box': {'xmin': ANY(_snake_case ), 'ymin': ANY(_snake_case ), 'xmax': ANY(_snake_case ), 'ymax': ANY(_snake_case )}, } for i in range(_snake_case ) ] , ) @require_tf @unittest.skip('Zero Shot Object Detection not implemented in TF' ) def _a ( self : List[str] ): """simple docstring""" pass @require_torch def _a ( self : Optional[int] ): """simple docstring""" A__ = pipeline( 'zero-shot-object-detection' , model='hf-internal-testing/tiny-random-owlvit-object-detection' ) A__ = object_detector( './tests/fixtures/tests_samples/COCO/000000039769.png' , candidate_labels=['cat', 'remote', 'couch'] , threshold=0.64 , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}}, {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}}, {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}}, ] , ) A__ = object_detector( [ { 'image': './tests/fixtures/tests_samples/COCO/000000039769.png', 'candidate_labels': ['cat', 'remote', 'couch'], } ] , threshold=0.64 , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ [ {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}}, {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}}, {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}}, ] ] , ) @require_torch @slow def _a ( self : int ): """simple docstring""" A__ = pipeline('zero-shot-object-detection' ) A__ = object_detector( 'http://images.cocodataset.org/val2017/000000039769.jpg' , candidate_labels=['cat', 'remote', 'couch'] , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}}, {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}}, {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}}, ] , ) A__ = object_detector( [ { 'image': 'http://images.cocodataset.org/val2017/000000039769.jpg', 'candidate_labels': ['cat', 'remote', 'couch'], }, { 'image': 'http://images.cocodataset.org/val2017/000000039769.jpg', 'candidate_labels': ['cat', 'remote', 'couch'], }, ] , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}}, {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}}, {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}}, ], [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}}, {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}}, {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}}, ], ] , ) @require_tf @unittest.skip('Zero Shot Object Detection not implemented in TF' ) def _a ( self : int ): """simple docstring""" pass @require_torch @slow def _a ( self : str ): """simple docstring""" A__ = 0.2 A__ = pipeline('zero-shot-object-detection' ) A__ = object_detector( 'http://images.cocodataset.org/val2017/000000039769.jpg' , candidate_labels=['cat', 'remote', 'couch'] , threshold=_snake_case , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}}, ] , ) @require_torch @slow def _a ( self : Any ): """simple docstring""" A__ = 2 A__ = pipeline('zero-shot-object-detection' ) A__ = object_detector( 'http://images.cocodataset.org/val2017/000000039769.jpg' , candidate_labels=['cat', 'remote', 'couch'] , top_k=_snake_case , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, ] , )
9
1
import json import os from functools import lru_cache from typing import List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt'''} # See all BART models at https://huggingface.co/models?filter=bart SCREAMING_SNAKE_CASE__ = { '''vocab_file''': { '''facebook/bart-base''': '''https://huggingface.co/facebook/bart-base/resolve/main/vocab.json''', '''facebook/bart-large''': '''https://huggingface.co/facebook/bart-large/resolve/main/vocab.json''', '''facebook/bart-large-mnli''': '''https://huggingface.co/facebook/bart-large-mnli/resolve/main/vocab.json''', '''facebook/bart-large-cnn''': '''https://huggingface.co/facebook/bart-large-cnn/resolve/main/vocab.json''', '''facebook/bart-large-xsum''': '''https://huggingface.co/facebook/bart-large-xsum/resolve/main/vocab.json''', '''yjernite/bart_eli5''': '''https://huggingface.co/yjernite/bart_eli5/resolve/main/vocab.json''', }, '''merges_file''': { '''facebook/bart-base''': '''https://huggingface.co/facebook/bart-base/resolve/main/merges.txt''', '''facebook/bart-large''': '''https://huggingface.co/facebook/bart-large/resolve/main/merges.txt''', '''facebook/bart-large-mnli''': '''https://huggingface.co/facebook/bart-large-mnli/resolve/main/merges.txt''', '''facebook/bart-large-cnn''': '''https://huggingface.co/facebook/bart-large-cnn/resolve/main/merges.txt''', '''facebook/bart-large-xsum''': '''https://huggingface.co/facebook/bart-large-xsum/resolve/main/merges.txt''', '''yjernite/bart_eli5''': '''https://huggingface.co/yjernite/bart_eli5/resolve/main/merges.txt''', }, } SCREAMING_SNAKE_CASE__ = { '''facebook/bart-base''': 1_0_2_4, '''facebook/bart-large''': 1_0_2_4, '''facebook/bart-large-mnli''': 1_0_2_4, '''facebook/bart-large-cnn''': 1_0_2_4, '''facebook/bart-large-xsum''': 1_0_2_4, '''yjernite/bart_eli5''': 1_0_2_4, } @lru_cache() def A ( ) -> List[str]: A__ = ( list(range(ord('!' ) , ord('~' ) + 1 ) ) + list(range(ord('¡' ) , ord('¬' ) + 1 ) ) + list(range(ord('®' ) , ord('ÿ' ) + 1 ) ) ) A__ = bs[:] A__ = 0 for b in range(2**8 ): if b not in bs: bs.append(__UpperCamelCase ) cs.append(2**8 + n ) n += 1 A__ = [chr(__UpperCamelCase ) for n in cs] return dict(zip(__UpperCamelCase , __UpperCamelCase ) ) def A ( __UpperCamelCase ) -> Union[str, Any]: A__ = set() A__ = word[0] for char in word[1:]: pairs.add((prev_char, char) ) A__ = char return pairs class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Union[str, Any] = VOCAB_FILES_NAMES A__ : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP A__ : Any = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A__ : Tuple = ["input_ids", "attention_mask"] def __init__( self : Any , _snake_case : List[Any] , _snake_case : str , _snake_case : Optional[int]="replace" , _snake_case : Optional[int]="<s>" , _snake_case : int="</s>" , _snake_case : Optional[int]="</s>" , _snake_case : Any="<s>" , _snake_case : int="<unk>" , _snake_case : str="<pad>" , _snake_case : List[Any]="<mask>" , _snake_case : Dict=False , **_snake_case : List[Any] , ): """simple docstring""" A__ = AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case ) if isinstance(_snake_case , _snake_case ) else bos_token A__ = AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case ) if isinstance(_snake_case , _snake_case ) else eos_token A__ = AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case ) if isinstance(_snake_case , _snake_case ) else sep_token A__ = AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case ) if isinstance(_snake_case , _snake_case ) else cls_token A__ = AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case ) if isinstance(_snake_case , _snake_case ) else unk_token A__ = AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case ) if isinstance(_snake_case , _snake_case ) else pad_token # Mask token behave like a normal word, i.e. include the space before it A__ = AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case ) if isinstance(_snake_case , _snake_case ) else mask_token super().__init__( errors=_snake_case , bos_token=_snake_case , eos_token=_snake_case , unk_token=_snake_case , sep_token=_snake_case , cls_token=_snake_case , pad_token=_snake_case , mask_token=_snake_case , add_prefix_space=_snake_case , **_snake_case , ) with open(_snake_case , encoding='utf-8' ) as vocab_handle: A__ = json.load(_snake_case ) A__ = {v: k for k, v in self.encoder.items()} A__ = errors # how to handle errors in decoding A__ = bytes_to_unicode() A__ = {v: k for k, v in self.byte_encoder.items()} with open(_snake_case , encoding='utf-8' ) as merges_handle: A__ = merges_handle.read().split('\n' )[1:-1] A__ = [tuple(merge.split() ) for merge in bpe_merges] A__ = dict(zip(_snake_case , range(len(_snake_case ) ) ) ) A__ = {} A__ = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions A__ = re.compile(R'\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+' ) @property def _a ( self : List[Any] ): """simple docstring""" return len(self.encoder ) def _a ( self : List[Any] ): """simple docstring""" return dict(self.encoder , **self.added_tokens_encoder ) def _a ( self : Tuple , _snake_case : Tuple ): """simple docstring""" if token in self.cache: return self.cache[token] A__ = tuple(_snake_case ) A__ = get_pairs(_snake_case ) if not pairs: return token while True: A__ = min(_snake_case , key=lambda _snake_case : self.bpe_ranks.get(_snake_case , float('inf' ) ) ) if bigram not in self.bpe_ranks: break A__ , A__ = bigram A__ = [] A__ = 0 while i < len(_snake_case ): try: A__ = word.index(_snake_case , _snake_case ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) A__ = j if word[i] == first and i < len(_snake_case ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 A__ = tuple(_snake_case ) A__ = new_word if len(_snake_case ) == 1: break else: A__ = get_pairs(_snake_case ) A__ = ' '.join(_snake_case ) A__ = word return word def _a ( self : Dict , _snake_case : str ): """simple docstring""" A__ = [] for token in re.findall(self.pat , _snake_case ): A__ = ''.join( self.byte_encoder[b] for b in token.encode('utf-8' ) ) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(_snake_case ).split(' ' ) ) return bpe_tokens def _a ( self : Union[str, Any] , _snake_case : Union[str, Any] ): """simple docstring""" return self.encoder.get(_snake_case , self.encoder.get(self.unk_token ) ) def _a ( self : Tuple , _snake_case : List[Any] ): """simple docstring""" return self.decoder.get(_snake_case ) def _a ( self : Optional[Any] , _snake_case : Dict ): """simple docstring""" A__ = ''.join(_snake_case ) A__ = bytearray([self.byte_decoder[c] for c in text] ).decode('utf-8' , errors=self.errors ) return text def _a ( self : List[str] , _snake_case : str , _snake_case : Optional[str] = None ): """simple docstring""" if not os.path.isdir(_snake_case ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return A__ = os.path.join( _snake_case , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) A__ = os.path.join( _snake_case , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['merges_file'] ) with open(_snake_case , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=_snake_case , ensure_ascii=_snake_case ) + '\n' ) A__ = 0 with open(_snake_case , 'w' , encoding='utf-8' ) as writer: writer.write('#version: 0.2\n' ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda _snake_case : kv[1] ): if index != token_index: logger.warning( F'''Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.''' ' Please check that the tokenizer is not corrupted!' ) A__ = token_index writer.write(' '.join(_snake_case ) + '\n' ) index += 1 return vocab_file, merge_file def _a ( self : Tuple , _snake_case : List[int] , _snake_case : Optional[List[int]] = None ): """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] A__ = [self.cls_token_id] A__ = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def _a ( self : Tuple , _snake_case : List[int] , _snake_case : Optional[List[int]] = None , _snake_case : bool = False ): """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_snake_case , token_ids_a=_snake_case , already_has_special_tokens=_snake_case ) if token_ids_a is None: return [1] + ([0] * len(_snake_case )) + [1] return [1] + ([0] * len(_snake_case )) + [1, 1] + ([0] * len(_snake_case )) + [1] def _a ( self : Optional[Any] , _snake_case : List[int] , _snake_case : Optional[List[int]] = None ): """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def _a ( self : int , _snake_case : Union[str, Any] , _snake_case : Tuple=False , **_snake_case : Tuple ): """simple docstring""" A__ = kwargs.pop('add_prefix_space' , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(_snake_case ) > 0 and not text[0].isspace()): A__ = ' ' + text return (text, kwargs)
9
import dataclasses import json import sys import types from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, ArgumentTypeError from copy import copy from enum import Enum from inspect import isclass from pathlib import Path from typing import Any, Callable, Dict, Iterable, List, Literal, NewType, Optional, Tuple, Union, get_type_hints import yaml SCREAMING_SNAKE_CASE__ = NewType('''DataClass''', Any) SCREAMING_SNAKE_CASE__ = NewType('''DataClassType''', Any) def A ( __UpperCamelCase ) -> List[Any]: if isinstance(__UpperCamelCase , __UpperCamelCase ): return v if v.lower() in ("yes", "true", "t", "y", "1"): return True elif v.lower() in ("no", "false", "f", "n", "0"): return False else: raise ArgumentTypeError( f'''Truthy value expected: got {v} but expected one of yes/no, true/false, t/f, y/n, 1/0 (case insensitive).''' ) def A ( __UpperCamelCase ) -> Callable[[str], Any]: A__ = {str(__UpperCamelCase ): choice for choice in choices} return lambda __UpperCamelCase : str_to_choice.get(__UpperCamelCase , __UpperCamelCase ) def A ( *, __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = dataclasses.MISSING , __UpperCamelCase = dataclasses.MISSING , __UpperCamelCase = None , **__UpperCamelCase , ) -> dataclasses.Field: if metadata is None: # Important, don't use as default param in function signature because dict is mutable and shared across function calls A__ = {} if aliases is not None: A__ = aliases if help is not None: A__ = help return dataclasses.field(metadata=__UpperCamelCase , default=__UpperCamelCase , default_factory=__UpperCamelCase , **__UpperCamelCase ) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Iterable[DataClassType] def __init__( self : Optional[int] , _snake_case : Union[DataClassType, Iterable[DataClassType]] , **_snake_case : Tuple ): """simple docstring""" if "formatter_class" not in kwargs: A__ = ArgumentDefaultsHelpFormatter super().__init__(**_snake_case ) if dataclasses.is_dataclass(_snake_case ): A__ = [dataclass_types] A__ = list(_snake_case ) for dtype in self.dataclass_types: self._add_dataclass_arguments(_snake_case ) @staticmethod def _a ( _snake_case : ArgumentParser , _snake_case : dataclasses.Field ): """simple docstring""" A__ = F'''--{field.name}''' A__ = field.metadata.copy() # field.metadata is not used at all by Data Classes, # it is provided as a third-party extension mechanism. if isinstance(field.type , _snake_case ): raise RuntimeError( 'Unresolved type detected, which should have been done with the help of ' '`typing.get_type_hints` method by default' ) A__ = kwargs.pop('aliases' , [] ) if isinstance(_snake_case , _snake_case ): A__ = [aliases] A__ = getattr(field.type , '__origin__' , field.type ) if origin_type is Union or (hasattr(_snake_case , 'UnionType' ) and isinstance(_snake_case , types.UnionType )): if str not in field.type.__args__ and ( len(field.type.__args__ ) != 2 or type(_snake_case ) not in field.type.__args__ ): raise ValueError( 'Only `Union[X, NoneType]` (i.e., `Optional[X]`) is allowed for `Union` because' ' the argument parser only supports one type per argument.' F''' Problem encountered in field \'{field.name}\'.''' ) if type(_snake_case ) not in field.type.__args__: # filter `str` in Union A__ = field.type.__args__[0] if field.type.__args__[1] == str else field.type.__args__[1] A__ = getattr(field.type , '__origin__' , field.type ) elif bool not in field.type.__args__: # filter `NoneType` in Union (except for `Union[bool, NoneType]`) A__ = ( field.type.__args__[0] if isinstance(_snake_case , field.type.__args__[1] ) else field.type.__args__[1] ) A__ = getattr(field.type , '__origin__' , field.type ) # A variable to store kwargs for a boolean field, if needed # so that we can init a `no_*` complement argument (see below) A__ = {} if origin_type is Literal or (isinstance(field.type , _snake_case ) and issubclass(field.type , _snake_case )): if origin_type is Literal: A__ = field.type.__args__ else: A__ = [x.value for x in field.type] A__ = make_choice_type_function(kwargs['choices'] ) if field.default is not dataclasses.MISSING: A__ = field.default else: A__ = True elif field.type is bool or field.type == Optional[bool]: # Copy the currect kwargs to use to instantiate a `no_*` complement argument below. # We do not initialize it here because the `no_*` alternative must be instantiated after the real argument A__ = copy(_snake_case ) # Hack because type=bool in argparse does not behave as we want. A__ = string_to_bool if field.type is bool or (field.default is not None and field.default is not dataclasses.MISSING): # Default value is False if we have no default when of type bool. A__ = False if field.default is dataclasses.MISSING else field.default # This is the value that will get picked if we don't include --field_name in any way A__ = default # This tells argparse we accept 0 or 1 value after --field_name A__ = '?' # This is the value that will get picked if we do --field_name (without value) A__ = True elif isclass(_snake_case ) and issubclass(_snake_case , _snake_case ): A__ = field.type.__args__[0] A__ = '+' if field.default_factory is not dataclasses.MISSING: A__ = field.default_factory() elif field.default is dataclasses.MISSING: A__ = True else: A__ = field.type if field.default is not dataclasses.MISSING: A__ = field.default elif field.default_factory is not dataclasses.MISSING: A__ = field.default_factory() else: A__ = True parser.add_argument(_snake_case , *_snake_case , **_snake_case ) # Add a complement `no_*` argument for a boolean field AFTER the initial field has already been added. # Order is important for arguments with the same destination! # We use a copy of earlier kwargs because the original kwargs have changed a lot before reaching down # here and we do not need those changes/additional keys. if field.default is True and (field.type is bool or field.type == Optional[bool]): A__ = False parser.add_argument(F'''--no_{field.name}''' , action='store_false' , dest=field.name , **_snake_case ) def _a ( self : Any , _snake_case : DataClassType ): """simple docstring""" if hasattr(_snake_case , '_argument_group_name' ): A__ = self.add_argument_group(dtype._argument_group_name ) else: A__ = self try: A__ = get_type_hints(_snake_case ) except NameError: raise RuntimeError( F'''Type resolution failed for {dtype}. Try declaring the class in global scope or ''' 'removing line of `from __future__ import annotations` which opts in Postponed ' 'Evaluation of Annotations (PEP 563)' ) except TypeError as ex: # Remove this block when we drop Python 3.9 support if sys.version_info[:2] < (3, 10) and "unsupported operand type(s) for |" in str(_snake_case ): A__ = '.'.join(map(_snake_case , sys.version_info[:3] ) ) raise RuntimeError( F'''Type resolution failed for {dtype} on Python {python_version}. Try removing ''' 'line of `from __future__ import annotations` which opts in union types as ' '`X | Y` (PEP 604) via Postponed Evaluation of Annotations (PEP 563). To ' 'support Python versions that lower than 3.10, you need to use ' '`typing.Union[X, Y]` instead of `X | Y` and `typing.Optional[X]` instead of ' '`X | None`.' ) from ex raise for field in dataclasses.fields(_snake_case ): if not field.init: continue A__ = type_hints[field.name] self._parse_dataclass_field(_snake_case , _snake_case ) def _a ( self : Optional[int] , _snake_case : Optional[Any]=None , _snake_case : Any=False , _snake_case : int=True , _snake_case : List[Any]=None , _snake_case : int=None , ): """simple docstring""" if args_file_flag or args_filename or (look_for_args_file and len(sys.argv )): A__ = [] if args_filename: args_files.append(Path(_snake_case ) ) elif look_for_args_file and len(sys.argv ): args_files.append(Path(sys.argv[0] ).with_suffix('.args' ) ) # args files specified via command line flag should overwrite default args files so we add them last if args_file_flag: # Create special parser just to extract the args_file_flag values A__ = ArgumentParser() args_file_parser.add_argument(_snake_case , type=_snake_case , action='append' ) # Use only remaining args for further parsing (remove the args_file_flag) A__ , A__ = args_file_parser.parse_known_args(args=_snake_case ) A__ = vars(_snake_case ).get(args_file_flag.lstrip('-' ) , _snake_case ) if cmd_args_file_paths: args_files.extend([Path(_snake_case ) for p in cmd_args_file_paths] ) A__ = [] for args_file in args_files: if args_file.exists(): file_args += args_file.read_text().split() # in case of duplicate arguments the last one has precedence # args specified via the command line should overwrite args from files, so we add them last A__ = file_args + args if args is not None else file_args + sys.argv[1:] A__ , A__ = self.parse_known_args(args=_snake_case ) A__ = [] for dtype in self.dataclass_types: A__ = {f.name for f in dataclasses.fields(_snake_case ) if f.init} A__ = {k: v for k, v in vars(_snake_case ).items() if k in keys} for k in keys: delattr(_snake_case , _snake_case ) A__ = dtype(**_snake_case ) outputs.append(_snake_case ) if len(namespace.__dict__ ) > 0: # additional namespace. outputs.append(_snake_case ) if return_remaining_strings: return (*outputs, remaining_args) else: if remaining_args: raise ValueError(F'''Some specified arguments are not used by the HfArgumentParser: {remaining_args}''' ) return (*outputs,) def _a ( self : Dict , _snake_case : Dict[str, Any] , _snake_case : bool = False ): """simple docstring""" A__ = set(args.keys() ) A__ = [] for dtype in self.dataclass_types: A__ = {f.name for f in dataclasses.fields(_snake_case ) if f.init} A__ = {k: v for k, v in args.items() if k in keys} unused_keys.difference_update(inputs.keys() ) A__ = dtype(**_snake_case ) outputs.append(_snake_case ) if not allow_extra_keys and unused_keys: raise ValueError(F'''Some keys are not used by the HfArgumentParser: {sorted(_snake_case )}''' ) return tuple(_snake_case ) def _a ( self : Dict , _snake_case : str , _snake_case : bool = False ): """simple docstring""" with open(Path(_snake_case ) , encoding='utf-8' ) as open_json_file: A__ = json.loads(open_json_file.read() ) A__ = self.parse_dict(_snake_case , allow_extra_keys=_snake_case ) return tuple(_snake_case ) def _a ( self : Tuple , _snake_case : str , _snake_case : bool = False ): """simple docstring""" A__ = self.parse_dict(yaml.safe_load(Path(_snake_case ).read_text() ) , allow_extra_keys=_snake_case ) return tuple(_snake_case )
9
1
from typing import Optional, Tuple, Union import flax import flax.linen as nn import jax import jax.numpy as jnp from flax.core.frozen_dict import FrozenDict from ..configuration_utils import ConfigMixin, flax_register_to_config from ..utils import BaseOutput from .embeddings_flax import FlaxTimestepEmbedding, FlaxTimesteps from .modeling_flax_utils import FlaxModelMixin from .unet_ad_blocks_flax import ( FlaxCrossAttnDownBlockaD, FlaxDownBlockaD, FlaxUNetMidBlockaDCrossAttn, ) @flax.struct.dataclass class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : jnp.ndarray A__ : jnp.ndarray class __lowerCAmelCase ( nn.Module ): """simple docstring""" A__ : int A__ : Tuple[int] = (16, 32, 96, 2_56) A__ : jnp.dtype = jnp.floataa def _a ( self : Optional[int] ): """simple docstring""" A__ = nn.Conv( self.block_out_channels[0] , kernel_size=(3, 3) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) A__ = [] for i in range(len(self.block_out_channels ) - 1 ): A__ = self.block_out_channels[i] A__ = self.block_out_channels[i + 1] A__ = nn.Conv( _snake_case , kernel_size=(3, 3) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) blocks.append(_snake_case ) A__ = nn.Conv( _snake_case , kernel_size=(3, 3) , strides=(2, 2) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) blocks.append(_snake_case ) A__ = blocks A__ = nn.Conv( self.conditioning_embedding_channels , kernel_size=(3, 3) , padding=((1, 1), (1, 1)) , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) def __call__( self : List[str] , _snake_case : Optional[Any] ): """simple docstring""" A__ = self.conv_in(_snake_case ) A__ = nn.silu(_snake_case ) for block in self.blocks: A__ = block(_snake_case ) A__ = nn.silu(_snake_case ) A__ = self.conv_out(_snake_case ) return embedding @flax_register_to_config class __lowerCAmelCase ( nn.Module , UpperCAmelCase_ , UpperCAmelCase_ ): """simple docstring""" A__ : int = 32 A__ : int = 4 A__ : Tuple[str] = ( "CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "DownBlock2D", ) A__ : Union[bool, Tuple[bool]] = False A__ : Tuple[int] = (3_20, 6_40, 12_80, 12_80) A__ : int = 2 A__ : Union[int, Tuple[int]] = 8 A__ : Optional[Union[int, Tuple[int]]] = None A__ : int = 12_80 A__ : float = 0.0 A__ : bool = False A__ : jnp.dtype = jnp.floataa A__ : bool = True A__ : int = 0 A__ : str = "rgb" A__ : Tuple[int] = (16, 32, 96, 2_56) def _a ( self : List[Any] , _snake_case : jax.random.KeyArray ): """simple docstring""" A__ = (1, self.in_channels, self.sample_size, self.sample_size) A__ = jnp.zeros(_snake_case , dtype=jnp.floataa ) A__ = jnp.ones((1,) , dtype=jnp.intaa ) A__ = jnp.zeros((1, 1, self.cross_attention_dim) , dtype=jnp.floataa ) A__ = (1, 3, self.sample_size * 8, self.sample_size * 8) A__ = jnp.zeros(_snake_case , dtype=jnp.floataa ) A__ , A__ = jax.random.split(_snake_case ) A__ = {'params': params_rng, 'dropout': dropout_rng} return self.init(_snake_case , _snake_case , _snake_case , _snake_case , _snake_case )["params"] def _a ( self : str ): """simple docstring""" A__ = self.block_out_channels A__ = block_out_channels[0] * 4 # If `num_attention_heads` is not defined (which is the case for most models) # it will default to `attention_head_dim`. This looks weird upon first reading it and it is. # The reason for this behavior is to correct for incorrectly named variables that were introduced # when this library was created. The incorrect naming was only discovered much later in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131 # Changing `attention_head_dim` to `num_attention_heads` for 40,000+ configurations is too backwards breaking # which is why we correct for the naming here. A__ = self.num_attention_heads or self.attention_head_dim # input A__ = nn.Conv( block_out_channels[0] , kernel_size=(3, 3) , strides=(1, 1) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) # time A__ = FlaxTimesteps( block_out_channels[0] , flip_sin_to_cos=self.flip_sin_to_cos , freq_shift=self.config.freq_shift ) A__ = FlaxTimestepEmbedding(_snake_case , dtype=self.dtype ) A__ = FlaxControlNetConditioningEmbedding( conditioning_embedding_channels=block_out_channels[0] , block_out_channels=self.conditioning_embedding_out_channels , ) A__ = self.only_cross_attention if isinstance(_snake_case , _snake_case ): A__ = (only_cross_attention,) * len(self.down_block_types ) if isinstance(_snake_case , _snake_case ): A__ = (num_attention_heads,) * len(self.down_block_types ) # down A__ = [] A__ = [] A__ = block_out_channels[0] A__ = nn.Conv( _snake_case , kernel_size=(1, 1) , padding='VALID' , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) controlnet_down_blocks.append(_snake_case ) for i, down_block_type in enumerate(self.down_block_types ): A__ = output_channel A__ = block_out_channels[i] A__ = i == len(_snake_case ) - 1 if down_block_type == "CrossAttnDownBlock2D": A__ = FlaxCrossAttnDownBlockaD( in_channels=_snake_case , out_channels=_snake_case , dropout=self.dropout , num_layers=self.layers_per_block , num_attention_heads=num_attention_heads[i] , add_downsample=not is_final_block , use_linear_projection=self.use_linear_projection , only_cross_attention=only_cross_attention[i] , dtype=self.dtype , ) else: A__ = FlaxDownBlockaD( in_channels=_snake_case , out_channels=_snake_case , dropout=self.dropout , num_layers=self.layers_per_block , add_downsample=not is_final_block , dtype=self.dtype , ) down_blocks.append(_snake_case ) for _ in range(self.layers_per_block ): A__ = nn.Conv( _snake_case , kernel_size=(1, 1) , padding='VALID' , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) controlnet_down_blocks.append(_snake_case ) if not is_final_block: A__ = nn.Conv( _snake_case , kernel_size=(1, 1) , padding='VALID' , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) controlnet_down_blocks.append(_snake_case ) A__ = down_blocks A__ = controlnet_down_blocks # mid A__ = block_out_channels[-1] A__ = FlaxUNetMidBlockaDCrossAttn( in_channels=_snake_case , dropout=self.dropout , num_attention_heads=num_attention_heads[-1] , use_linear_projection=self.use_linear_projection , dtype=self.dtype , ) A__ = nn.Conv( _snake_case , kernel_size=(1, 1) , padding='VALID' , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) def __call__( self : Optional[int] , _snake_case : Dict , _snake_case : Dict , _snake_case : List[str] , _snake_case : Dict , _snake_case : float = 1.0 , _snake_case : bool = True , _snake_case : bool = False , ): """simple docstring""" A__ = self.controlnet_conditioning_channel_order if channel_order == "bgr": A__ = jnp.flip(_snake_case , axis=1 ) # 1. time if not isinstance(_snake_case , jnp.ndarray ): A__ = jnp.array([timesteps] , dtype=jnp.intaa ) elif isinstance(_snake_case , jnp.ndarray ) and len(timesteps.shape ) == 0: A__ = timesteps.astype(dtype=jnp.floataa ) A__ = jnp.expand_dims(_snake_case , 0 ) A__ = self.time_proj(_snake_case ) A__ = self.time_embedding(_snake_case ) # 2. pre-process A__ = jnp.transpose(_snake_case , (0, 2, 3, 1) ) A__ = self.conv_in(_snake_case ) A__ = jnp.transpose(_snake_case , (0, 2, 3, 1) ) A__ = self.controlnet_cond_embedding(_snake_case ) sample += controlnet_cond # 3. down A__ = (sample,) for down_block in self.down_blocks: if isinstance(_snake_case , _snake_case ): A__ , A__ = down_block(_snake_case , _snake_case , _snake_case , deterministic=not train ) else: A__ , A__ = down_block(_snake_case , _snake_case , deterministic=not train ) down_block_res_samples += res_samples # 4. mid A__ = self.mid_block(_snake_case , _snake_case , _snake_case , deterministic=not train ) # 5. contronet blocks A__ = () for down_block_res_sample, controlnet_block in zip(_snake_case , self.controlnet_down_blocks ): A__ = controlnet_block(_snake_case ) controlnet_down_block_res_samples += (down_block_res_sample,) A__ = controlnet_down_block_res_samples A__ = self.controlnet_mid_block(_snake_case ) # 6. scaling A__ = [sample * conditioning_scale for sample in down_block_res_samples] mid_block_res_sample *= conditioning_scale if not return_dict: return (down_block_res_samples, mid_block_res_sample) return FlaxControlNetOutput( down_block_res_samples=_snake_case , mid_block_res_sample=_snake_case )
9
import argparse import collections import json from pathlib import Path import requests import torch import yaml from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( MobileViTImageProcessor, MobileViTVaConfig, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation, ) from transformers.utils import logging logging.set_verbosity_info() SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) def A ( __UpperCamelCase ) -> List[Any]: print('Loading config file...' ) def flatten_yaml_as_dict(__UpperCamelCase , __UpperCamelCase="" , __UpperCamelCase="." ): A__ = [] for k, v in d.items(): A__ = parent_key + sep + k if parent_key else k if isinstance(__UpperCamelCase , collections.abc.MutableMapping ): items.extend(flatten_yaml_as_dict(__UpperCamelCase , __UpperCamelCase , sep=__UpperCamelCase ).items() ) else: items.append((new_key, v) ) return dict(__UpperCamelCase ) A__ = argparse.Namespace() with open(__UpperCamelCase , 'r' ) as yaml_file: try: A__ = yaml.load(__UpperCamelCase , Loader=yaml.FullLoader ) A__ = flatten_yaml_as_dict(__UpperCamelCase ) for k, v in flat_cfg.items(): setattr(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) except yaml.YAMLError as exc: logger.error('Error while loading config file: {}. Error message: {}'.format(__UpperCamelCase , str(__UpperCamelCase ) ) ) return config def A ( __UpperCamelCase , __UpperCamelCase ) -> Optional[Any]: A__ = MobileViTVaConfig() A__ = False # dataset if task_name.startswith('imagenet1k_' ): A__ = 1_000 if int(task_name.strip().split('_' )[-1] ) == 384: A__ = 384 else: A__ = 256 A__ = 'imagenet-1k-id2label.json' elif task_name.startswith('imagenet21k_to_1k_' ): A__ = 21_000 if int(task_name.strip().split('_' )[-1] ) == 384: A__ = 384 else: A__ = 256 A__ = 'imagenet-22k-id2label.json' elif task_name.startswith('ade20k_' ): A__ = 151 A__ = 512 A__ = 'ade20k-id2label.json' A__ = True elif task_name.startswith('voc_' ): A__ = 21 A__ = 512 A__ = 'pascal-voc-id2label.json' A__ = True # orig_config A__ = load_orig_config_file(__UpperCamelCase ) assert getattr(__UpperCamelCase , 'model.classification.name' , -1 ) == "mobilevit_v2", "Invalid model" A__ = getattr(__UpperCamelCase , 'model.classification.mitv2.width_multiplier' , 1.0 ) assert ( getattr(__UpperCamelCase , 'model.classification.mitv2.attn_norm_layer' , -1 ) == "layer_norm_2d" ), "Norm layers other than layer_norm_2d is not supported" A__ = getattr(__UpperCamelCase , 'model.classification.activation.name' , 'swish' ) # config.image_size == getattr(orig_config, 'sampler.bs.crop_size_width', 256) if is_segmentation_model: A__ = getattr(__UpperCamelCase , 'model.segmentation.output_stride' , 16 ) if "_deeplabv3" in task_name: A__ = getattr(__UpperCamelCase , 'model.segmentation.deeplabv3.aspp_rates' , [12, 24, 36] ) A__ = getattr(__UpperCamelCase , 'model.segmentation.deeplabv3.aspp_out_channels' , 512 ) A__ = getattr(__UpperCamelCase , 'model.segmentation.deeplabv3.aspp_dropout' , 0.1 ) # id2label A__ = 'huggingface/label-files' A__ = json.load(open(hf_hub_download(__UpperCamelCase , __UpperCamelCase , repo_type='dataset' ) , 'r' ) ) A__ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} A__ = idalabel A__ = {v: k for k, v in idalabel.items()} return config def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[str]: A__ = dct.pop(__UpperCamelCase ) A__ = val def A ( __UpperCamelCase , __UpperCamelCase=False ) -> Dict: if base_model: A__ = '' else: A__ = 'mobilevitv2.' A__ = [] for k in state_dict.keys(): if k[:8] == "encoder.": A__ = k[8:] else: A__ = k if ".block." in k: A__ = k_new.replace('.block.' , '.' ) if ".conv." in k: A__ = k_new.replace('.conv.' , '.convolution.' ) if ".norm." in k: A__ = k_new.replace('.norm.' , '.normalization.' ) if "conv_1." in k: A__ = k_new.replace('conv_1.' , f'''{model_prefix}conv_stem.''' ) for i in [1, 2]: if f'''layer_{i}.''' in k: A__ = k_new.replace(f'''layer_{i}.''' , f'''{model_prefix}encoder.layer.{i-1}.layer.''' ) if ".exp_1x1." in k: A__ = k_new.replace('.exp_1x1.' , '.expand_1x1.' ) if ".red_1x1." in k: A__ = k_new.replace('.red_1x1.' , '.reduce_1x1.' ) for i in [3, 4, 5]: if f'''layer_{i}.0.''' in k: A__ = k_new.replace(f'''layer_{i}.0.''' , f'''{model_prefix}encoder.layer.{i-1}.downsampling_layer.''' ) if f'''layer_{i}.1.local_rep.0.''' in k: A__ = k_new.replace(f'''layer_{i}.1.local_rep.0.''' , f'''{model_prefix}encoder.layer.{i-1}.conv_kxk.''' ) if f'''layer_{i}.1.local_rep.1.''' in k: A__ = k_new.replace(f'''layer_{i}.1.local_rep.1.''' , f'''{model_prefix}encoder.layer.{i-1}.conv_1x1.''' ) for i in [3, 4, 5]: if i == 3: A__ = [0, 1] elif i == 4: A__ = [0, 1, 2, 3] elif i == 5: A__ = [0, 1, 2] for j in j_in: if f'''layer_{i}.1.global_rep.{j}.''' in k: A__ = k_new.replace( f'''layer_{i}.1.global_rep.{j}.''' , f'''{model_prefix}encoder.layer.{i-1}.transformer.layer.{j}.''' ) if f'''layer_{i}.1.global_rep.{j+1}.''' in k: A__ = k_new.replace( f'''layer_{i}.1.global_rep.{j+1}.''' , f'''{model_prefix}encoder.layer.{i-1}.layernorm.''' ) if f'''layer_{i}.1.conv_proj.''' in k: A__ = k_new.replace(f'''layer_{i}.1.conv_proj.''' , f'''{model_prefix}encoder.layer.{i-1}.conv_projection.''' ) if "pre_norm_attn.0." in k: A__ = k_new.replace('pre_norm_attn.0.' , 'layernorm_before.' ) if "pre_norm_attn.1." in k: A__ = k_new.replace('pre_norm_attn.1.' , 'attention.' ) if "pre_norm_ffn.0." in k: A__ = k_new.replace('pre_norm_ffn.0.' , 'layernorm_after.' ) if "pre_norm_ffn.1." in k: A__ = k_new.replace('pre_norm_ffn.1.' , 'ffn.conv1.' ) if "pre_norm_ffn.3." in k: A__ = k_new.replace('pre_norm_ffn.3.' , 'ffn.conv2.' ) if "classifier.1." in k: A__ = k_new.replace('classifier.1.' , 'classifier.' ) if "seg_head." in k: A__ = k_new.replace('seg_head.' , 'segmentation_head.' ) if ".aspp_layer." in k: A__ = k_new.replace('.aspp_layer.' , '.' ) if ".aspp_pool." in k: A__ = k_new.replace('.aspp_pool.' , '.' ) rename_keys.append((k, k_new) ) return rename_keys def A ( __UpperCamelCase ) -> Tuple: A__ = [] for k in state_dict.keys(): if k.startswith('seg_head.aux_head.' ): keys_to_ignore.append(__UpperCamelCase ) for k in keys_to_ignore: state_dict.pop(__UpperCamelCase , __UpperCamelCase ) def A ( ) -> str: A__ = 'http://images.cocodataset.org/val2017/000000039769.jpg' # url = "https://cdn.britannica.com/86/141086-050-9D7C75EE/Gulfstream-G450-business-jet-passengers.jpg" A__ = Image.open(requests.get(__UpperCamelCase , stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Optional[Any]: A__ = get_mobilevitva_config(__UpperCamelCase , __UpperCamelCase ) # load original state_dict A__ = torch.load(__UpperCamelCase , map_location='cpu' ) # load huggingface model if task_name.startswith('ade20k_' ) or task_name.startswith('voc_' ): A__ = MobileViTVaForSemanticSegmentation(__UpperCamelCase ).eval() A__ = False else: A__ = MobileViTVaForImageClassification(__UpperCamelCase ).eval() A__ = False # remove and rename some keys of load the original model A__ = checkpoint remove_unused_keys(__UpperCamelCase ) A__ = create_rename_keys(__UpperCamelCase , base_model=__UpperCamelCase ) for rename_key_src, rename_key_dest in rename_keys: rename_key(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) # load modified state_dict model.load_state_dict(__UpperCamelCase ) # Check outputs on an image, prepared by MobileViTImageProcessor A__ = MobileViTImageProcessor(crop_size=config.image_size , size=config.image_size + 32 ) A__ = image_processor(images=prepare_img() , return_tensors='pt' ) A__ = model(**__UpperCamelCase ) # verify classification model if task_name.startswith('imagenet' ): A__ = outputs.logits A__ = logits.argmax(-1 ).item() print('Predicted class:' , model.config.idalabel[predicted_class_idx] ) if task_name.startswith('imagenet1k_256' ) and config.width_multiplier == 1.0: # expected_logits for base variant A__ = torch.tensor([-1.6336E00, -7.3204E-02, -5.1883E-01] ) assert torch.allclose(logits[0, :3] , __UpperCamelCase , atol=1E-4 ) Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) print(f'''Saving model {task_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(__UpperCamelCase ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(__UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--task''', default='''imagenet1k_256''', type=str, help=( '''Name of the task for which the MobileViTV2 model you\'d like to convert is trained on . ''' ''' Classification (ImageNet-1k) - MobileViTV2 (256x256) : imagenet1k_256 - MobileViTV2 (Trained on 256x256 and Finetuned on 384x384) : imagenet1k_384 - MobileViTV2 (Trained on ImageNet-21k and Finetuned on ImageNet-1k 256x256) : imagenet21k_to_1k_256 - MobileViTV2 (Trained on ImageNet-21k, Finetuned on ImageNet-1k 256x256, and Finetuned on ImageNet-1k 384x384) : imagenet21k_to_1k_384 Segmentation - ADE20K Dataset : ade20k_deeplabv3 - Pascal VOC 2012 Dataset: voc_deeplabv3 ''' ), choices=[ '''imagenet1k_256''', '''imagenet1k_384''', '''imagenet21k_to_1k_256''', '''imagenet21k_to_1k_384''', '''ade20k_deeplabv3''', '''voc_deeplabv3''', ], ) parser.add_argument( '''--orig_checkpoint_path''', required=True, type=str, help='''Path to the original state dict (.pt file).''' ) parser.add_argument('''--orig_config_path''', required=True, type=str, help='''Path to the original config file.''') parser.add_argument( '''--pytorch_dump_folder_path''', required=True, type=str, help='''Path to the output PyTorch model directory.''' ) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_mobilevitva_checkpoint( args.task, args.orig_checkpoint_path, args.orig_config_path, args.pytorch_dump_folder_path )
9
1
from ...configuration_utils import PretrainedConfig from ...utils import logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = { '''sayakpaul/vit-msn-base''': '''https://huggingface.co/sayakpaul/vit-msn-base/resolve/main/config.json''', # See all ViT MSN models at https://huggingface.co/models?filter=vit_msn } class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Optional[Any] = "vit_msn" def __init__( self : Tuple , _snake_case : Any=7_68 , _snake_case : List[str]=12 , _snake_case : Dict=12 , _snake_case : str=30_72 , _snake_case : str="gelu" , _snake_case : Tuple=0.0 , _snake_case : Dict=0.0 , _snake_case : int=0.02 , _snake_case : Any=1E-06 , _snake_case : str=2_24 , _snake_case : List[str]=16 , _snake_case : Optional[int]=3 , _snake_case : Optional[Any]=True , **_snake_case : Dict , ): """simple docstring""" super().__init__(**_snake_case ) A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = initializer_range A__ = layer_norm_eps A__ = image_size A__ = patch_size A__ = num_channels A__ = qkv_bias
9
import argparse from collections import defaultdict import yaml SCREAMING_SNAKE_CASE__ = '''docs/source/en/_toctree.yml''' def A ( __UpperCamelCase ) -> Optional[Any]: A__ = defaultdict(__UpperCamelCase ) for doc in model_doc: counts[doc["local"]] += 1 A__ = [key for key, value in counts.items() if value > 1] A__ = [] for duplicate_key in duplicates: A__ = list({doc['title'] for doc in model_doc if doc['local'] == duplicate_key} ) if len(__UpperCamelCase ) > 1: raise ValueError( f'''{duplicate_key} is present several times in the documentation table of content at ''' '`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the ' 'others.' ) # Only add this once new_doc.append({'local': duplicate_key, 'title': titles[0]} ) # Add none duplicate-keys new_doc.extend([doc for doc in model_doc if counts[doc['local']] == 1] ) # Sort return sorted(__UpperCamelCase , key=lambda __UpperCamelCase : s["title"].lower() ) def A ( __UpperCamelCase=False ) -> str: with open(__UpperCamelCase , encoding='utf-8' ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]['sections'] # Then to the model doc A__ = 0 while api_doc[model_idx]["title"] != "Models": model_idx += 1 A__ = api_doc[model_idx]['sections'] A__ = [(idx, section) for idx, section in enumerate(__UpperCamelCase ) if 'sections' in section] A__ = False for idx, modality_doc in modalities_docs: A__ = modality_doc['sections'] A__ = clean_model_doc_toc(__UpperCamelCase ) if old_modality_doc != new_modality_doc: A__ = True if overwrite: A__ = new_modality_doc if diff: if overwrite: A__ = model_doc A__ = api_doc with open(__UpperCamelCase , 'w' , encoding='utf-8' ) as f: f.write(yaml.dump(__UpperCamelCase , allow_unicode=__UpperCamelCase ) ) else: raise ValueError( 'The model doc part of the table of content is not properly sorted, run `make style` to fix this.' ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') SCREAMING_SNAKE_CASE__ = parser.parse_args() check_model_doc(args.fix_and_overwrite)
9
1
import warnings from ...utils import logging from .image_processing_deit import DeiTImageProcessor SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : int , *_snake_case : List[str] , **_snake_case : Optional[int] ): """simple docstring""" warnings.warn( 'The class DeiTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please' ' use DeiTImageProcessor instead.' , _snake_case , ) super().__init__(*_snake_case , **_snake_case )
9
import inspect import unittest from transformers import SegformerConfig, is_torch_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_MAPPING, SegformerForImageClassification, SegformerForSemanticSegmentation, SegformerModel, ) from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import SegformerImageProcessor class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def _a ( self : List[str] ): """simple docstring""" A__ = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(_snake_case , 'hidden_sizes' ) ) self.parent.assertTrue(hasattr(_snake_case , 'num_attention_heads' ) ) self.parent.assertTrue(hasattr(_snake_case , 'num_encoder_blocks' ) ) class __lowerCAmelCase : """simple docstring""" def __init__( self : Any , _snake_case : str , _snake_case : Union[str, Any]=13 , _snake_case : Any=64 , _snake_case : Optional[Any]=3 , _snake_case : Dict=4 , _snake_case : Tuple=[2, 2, 2, 2] , _snake_case : str=[8, 4, 2, 1] , _snake_case : Union[str, Any]=[16, 32, 64, 1_28] , _snake_case : int=[1, 4, 8, 16] , _snake_case : List[str]=[1, 2, 4, 8] , _snake_case : int=True , _snake_case : int=True , _snake_case : Union[str, Any]="gelu" , _snake_case : Optional[int]=0.1 , _snake_case : Tuple=0.1 , _snake_case : Dict=0.02 , _snake_case : Tuple=3 , _snake_case : int=None , ): """simple docstring""" A__ = parent A__ = batch_size A__ = image_size A__ = num_channels A__ = num_encoder_blocks A__ = sr_ratios A__ = depths A__ = hidden_sizes A__ = downsampling_rates A__ = num_attention_heads A__ = is_training A__ = use_labels A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = initializer_range A__ = num_labels A__ = scope def _a ( self : int ): """simple docstring""" A__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) A__ = self.get_config() return config, pixel_values, labels def _a ( self : int ): """simple docstring""" return SegformerConfig( image_size=self.image_size , num_channels=self.num_channels , num_encoder_blocks=self.num_encoder_blocks , depths=self.depths , hidden_sizes=self.hidden_sizes , num_attention_heads=self.num_attention_heads , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , initializer_range=self.initializer_range , ) def _a ( self : int , _snake_case : Optional[Any] , _snake_case : int , _snake_case : Any ): """simple docstring""" A__ = SegformerModel(config=_snake_case ) model.to(_snake_case ) model.eval() A__ = model(_snake_case ) A__ = A__ = self.image_size // (self.downsampling_rates[-1] * 2) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width) ) def _a ( self : Union[str, Any] , _snake_case : Union[str, Any] , _snake_case : Tuple , _snake_case : Dict ): """simple docstring""" A__ = self.num_labels A__ = SegformerForSemanticSegmentation(_snake_case ) model.to(_snake_case ) model.eval() A__ = model(_snake_case ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4) ) A__ = model(_snake_case , labels=_snake_case ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4) ) self.parent.assertGreater(result.loss , 0.0 ) def _a ( self : List[str] , _snake_case : Optional[Any] , _snake_case : Union[str, Any] , _snake_case : List[str] ): """simple docstring""" A__ = 1 A__ = SegformerForSemanticSegmentation(config=_snake_case ) model.to(_snake_case ) model.eval() A__ = torch.randint(0 , 1 , (self.batch_size, self.image_size, self.image_size) ).to(_snake_case ) A__ = model(_snake_case , labels=_snake_case ) self.parent.assertGreater(result.loss , 0.0 ) def _a ( self : List[Any] ): """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ = config_and_inputs A__ = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class __lowerCAmelCase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Optional[int] = ( ( SegformerModel, SegformerForSemanticSegmentation, SegformerForImageClassification, ) if is_torch_available() else () ) A__ : Union[str, Any] = ( { "feature-extraction": SegformerModel, "image-classification": SegformerForImageClassification, "image-segmentation": SegformerForSemanticSegmentation, } if is_torch_available() else {} ) A__ : Optional[Any] = True A__ : str = False A__ : Tuple = False A__ : Dict = False def _a ( self : Union[str, Any] ): """simple docstring""" A__ = SegformerModelTester(self ) A__ = SegformerConfigTester(self , config_class=_snake_case ) def _a ( self : Optional[int] ): """simple docstring""" self.config_tester.run_common_tests() def _a ( self : Optional[Any] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def _a ( self : List[Any] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_binary_image_segmentation(*_snake_case ) def _a ( self : Tuple ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_segmentation(*_snake_case ) @unittest.skip('SegFormer does not use inputs_embeds' ) def _a ( self : List[Any] ): """simple docstring""" pass @unittest.skip('SegFormer does not have get_input_embeddings method and get_output_embeddings methods' ) def _a ( self : Dict ): """simple docstring""" pass def _a ( self : Dict ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(_snake_case ) A__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ = [*signature.parameters.keys()] A__ = ['pixel_values'] self.assertListEqual(arg_names[:1] , _snake_case ) def _a ( self : Dict ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = True for model_class in self.all_model_classes: A__ = True A__ = False A__ = True A__ = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) ) A__ = outputs.attentions A__ = sum(self.model_tester.depths ) self.assertEqual(len(_snake_case ) , _snake_case ) # check that output_attentions also work using config del inputs_dict["output_attentions"] A__ = True A__ = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) ) A__ = outputs.attentions self.assertEqual(len(_snake_case ) , _snake_case ) # verify the first attentions (first block, first layer) A__ = (self.model_tester.image_size // 4) ** 2 A__ = (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2 self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len] , ) # verify the last attentions (last block, last layer) A__ = (self.model_tester.image_size // 32) ** 2 A__ = (self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2 self.assertListEqual( list(attentions[-1].shape[-3:] ) , [self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len] , ) A__ = len(_snake_case ) # Check attention is always last and order is fine A__ = True A__ = True A__ = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) ) self.assertEqual(out_len + 1 , len(_snake_case ) ) A__ = outputs.attentions self.assertEqual(len(_snake_case ) , _snake_case ) # verify the first attentions (first block, first layer) A__ = (self.model_tester.image_size // 4) ** 2 A__ = (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2 self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len] , ) def _a ( self : Union[str, Any] ): """simple docstring""" def check_hidden_states_output(_snake_case : Dict , _snake_case : int , _snake_case : List[Any] ): A__ = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) ) A__ = outputs.hidden_states A__ = self.model_tester.num_encoder_blocks self.assertEqual(len(_snake_case ) , _snake_case ) # verify the first hidden states (first block) self.assertListEqual( list(hidden_states[0].shape[-3:] ) , [ self.model_tester.hidden_sizes[0], self.model_tester.image_size // 4, self.model_tester.image_size // 4, ] , ) A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = True check_hidden_states_output(_snake_case , _snake_case , _snake_case ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A__ = True check_hidden_states_output(_snake_case , _snake_case , _snake_case ) def _a ( self : Tuple ): """simple docstring""" if not self.model_tester.is_training: return A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = True for model_class in self.all_model_classes: if model_class in get_values(_snake_case ): continue A__ = model_class(_snake_case ) model.to(_snake_case ) model.train() A__ = self._prepare_for_class(_snake_case , _snake_case , return_labels=_snake_case ) A__ = model(**_snake_case ).loss loss.backward() @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.' ) def _a ( self : Optional[Any] ): """simple docstring""" pass @slow def _a ( self : Tuple ): """simple docstring""" for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = SegformerModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def A ( ) -> str: A__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @slow def _a ( self : Dict ): """simple docstring""" A__ = SegformerImageProcessor( image_scale=(5_12, 5_12) , keep_ratio=_snake_case , align=_snake_case , do_random_crop=_snake_case ) A__ = SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512' ).to( _snake_case ) A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ) A__ = encoded_inputs.pixel_values.to(_snake_case ) with torch.no_grad(): A__ = model(_snake_case ) A__ = torch.Size((1, model.config.num_labels, 1_28, 1_28) ) self.assertEqual(outputs.logits.shape , _snake_case ) A__ = torch.tensor( [ [[-4.6310, -5.5232, -6.2356], [-5.1921, -6.1444, -6.5996], [-5.4424, -6.2790, -6.7574]], [[-12.1391, -13.3122, -13.9554], [-12.8732, -13.9352, -14.3563], [-12.9438, -13.8226, -14.2513]], [[-12.5134, -13.4686, -14.4915], [-12.8669, -14.4343, -14.7758], [-13.2523, -14.5819, -15.0694]], ] ).to(_snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3] , _snake_case , atol=1E-4 ) ) @slow def _a ( self : Optional[Any] ): """simple docstring""" A__ = SegformerImageProcessor( image_scale=(5_12, 5_12) , keep_ratio=_snake_case , align=_snake_case , do_random_crop=_snake_case ) A__ = SegformerForSemanticSegmentation.from_pretrained( 'nvidia/segformer-b1-finetuned-cityscapes-1024-1024' ).to(_snake_case ) A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ) A__ = encoded_inputs.pixel_values.to(_snake_case ) with torch.no_grad(): A__ = model(_snake_case ) A__ = torch.Size((1, model.config.num_labels, 1_28, 1_28) ) self.assertEqual(outputs.logits.shape , _snake_case ) A__ = torch.tensor( [ [[-13.5748, -13.9111, -12.6500], [-14.3500, -15.3683, -14.2328], [-14.7532, -16.0424, -15.6087]], [[-17.1651, -15.8725, -12.9653], [-17.2580, -17.3718, -14.8223], [-16.6058, -16.8783, -16.7452]], [[-3.6456, -3.0209, -1.4203], [-3.0797, -3.1959, -2.0000], [-1.8757, -1.9217, -1.6997]], ] ).to(_snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3] , _snake_case , atol=1E-1 ) ) @slow def _a ( self : Any ): """simple docstring""" A__ = SegformerImageProcessor( image_scale=(5_12, 5_12) , keep_ratio=_snake_case , align=_snake_case , do_random_crop=_snake_case ) A__ = SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512' ).to( _snake_case ) A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ) A__ = encoded_inputs.pixel_values.to(_snake_case ) with torch.no_grad(): A__ = model(_snake_case ) A__ = outputs.logits.detach().cpu() A__ = image_processor.post_process_semantic_segmentation(outputs=_snake_case , target_sizes=[(5_00, 3_00)] ) A__ = torch.Size((5_00, 3_00) ) self.assertEqual(segmentation[0].shape , _snake_case ) A__ = image_processor.post_process_semantic_segmentation(outputs=_snake_case ) A__ = torch.Size((1_28, 1_28) ) self.assertEqual(segmentation[0].shape , _snake_case )
9
1
def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: if exponent == 1: return base if exponent % 2 == 0: A__ = _modexpt(__UpperCamelCase , exponent // 2 , __UpperCamelCase ) % modulo_value return (x * x) % modulo_value else: return (base * _modexpt(__UpperCamelCase , exponent - 1 , __UpperCamelCase )) % modulo_value def A ( __UpperCamelCase = 1_777 , __UpperCamelCase = 1_855 , __UpperCamelCase = 8 ) -> int: A__ = base for _ in range(1 , __UpperCamelCase ): A__ = _modexpt(__UpperCamelCase , __UpperCamelCase , 10**digits ) return result if __name__ == "__main__": print(f'{solution() = }')
9
import logging from pathlib import Path import numpy as np import pytorch_lightning as pl import torch from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint from pytorch_lightning.utilities import rank_zero_only from utils_rag import save_json def A ( __UpperCamelCase ) -> Optional[int]: A__ = filter(lambda __UpperCamelCase : p.requires_grad , model.parameters() ) A__ = sum([np.prod(p.size() ) for p in model_parameters] ) return params SCREAMING_SNAKE_CASE__ = logging.getLogger(__name__) def A ( __UpperCamelCase , __UpperCamelCase ) -> Dict: if metric == "rouge2": A__ = '{val_avg_rouge2:.4f}-{step_count}' elif metric == "bleu": A__ = '{val_avg_bleu:.4f}-{step_count}' elif metric == "em": A__ = '{val_avg_em:.4f}-{step_count}' elif metric == "loss": A__ = '{val_avg_loss:.4f}-{step_count}' else: raise NotImplementedError( f'''seq2seq callbacks only support rouge2 and bleu, got {metric}, You can make your own by adding to this''' ' function.' ) A__ = ModelCheckpoint( dirpath=__UpperCamelCase , filename=__UpperCamelCase , monitor=f'''val_{metric}''' , mode='max' , save_top_k=1 , every_n_epochs=1 , ) return checkpoint_callback def A ( __UpperCamelCase , __UpperCamelCase ) -> Any: return EarlyStopping( monitor=f'''val_{metric}''' , mode='min' if 'loss' in metric else 'max' , patience=__UpperCamelCase , verbose=__UpperCamelCase , ) class __lowerCAmelCase ( pl.Callback ): """simple docstring""" def _a ( self : Dict , _snake_case : Union[str, Any] , _snake_case : str ): """simple docstring""" A__ = {F'''lr_group_{i}''': param['lr'] for i, param in enumerate(pl_module.trainer.optimizers[0].param_groups )} pl_module.logger.log_metrics(_snake_case ) @rank_zero_only def _a ( self : Union[str, Any] , _snake_case : pl.Trainer , _snake_case : pl.LightningModule , _snake_case : str , _snake_case : Optional[Any]=True ): """simple docstring""" logger.info(F'''***** {type_path} results at step {trainer.global_step:05d} *****''' ) A__ = trainer.callback_metrics trainer.logger.log_metrics({k: v for k, v in metrics.items() if k not in ['log', 'progress_bar', 'preds']} ) # Log results A__ = Path(pl_module.hparams.output_dir ) if type_path == "test": A__ = od / 'test_results.txt' A__ = od / 'test_generations.txt' else: # this never gets hit. I prefer not to save intermediate generations, and results are in metrics.json # If people want this it will be easy enough to add back. A__ = od / F'''{type_path}_results/{trainer.global_step:05d}.txt''' A__ = od / F'''{type_path}_generations/{trainer.global_step:05d}.txt''' results_file.parent.mkdir(exist_ok=_snake_case ) generations_file.parent.mkdir(exist_ok=_snake_case ) with open(_snake_case , 'a+' ) as writer: for key in sorted(_snake_case ): if key in ["log", "progress_bar", "preds"]: continue A__ = metrics[key] if isinstance(_snake_case , torch.Tensor ): A__ = val.item() A__ = F'''{key}: {val:.6f}\n''' writer.write(_snake_case ) if not save_generations: return if "preds" in metrics: A__ = '\n'.join(metrics['preds'] ) generations_file.open('w+' ).write(_snake_case ) @rank_zero_only def _a ( self : Dict , _snake_case : List[str] , _snake_case : List[Any] ): """simple docstring""" try: A__ = pl_module.model.model.num_parameters() except AttributeError: A__ = pl_module.model.num_parameters() A__ = count_trainable_parameters(_snake_case ) # mp stands for million parameters trainer.logger.log_metrics({'n_params': npars, 'mp': npars / 1E6, 'grad_mp': n_trainable_pars / 1E6} ) @rank_zero_only def _a ( self : int , _snake_case : pl.Trainer , _snake_case : pl.LightningModule ): """simple docstring""" save_json(pl_module.metrics , pl_module.metrics_save_path ) return self._write_logs(_snake_case , _snake_case , 'test' ) @rank_zero_only def _a ( self : Optional[Any] , _snake_case : pl.Trainer , _snake_case : List[Any] ): """simple docstring""" save_json(pl_module.metrics , pl_module.metrics_save_path ) # Uncommenting this will save val generations # return self._write_logs(trainer, pl_module, "valid")
9
1
from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE__ = { '''configuration_informer''': [ '''INFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''InformerConfig''', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''INFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''InformerForPrediction''', '''InformerModel''', '''InformerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_informer import INFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, InformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_informer import ( INFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, InformerForPrediction, InformerModel, InformerPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
9
import warnings from typing import Any, Dict, List, Optional, Union import numpy as np from ...audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Optional[Any] = ["input_values", "attention_mask"] def __init__( self : str , _snake_case : int = 1 , _snake_case : int = 1_60_00 , _snake_case : float = 0.0 , _snake_case : bool = False , _snake_case : int = 80 , _snake_case : int = 16 , _snake_case : int = 64 , _snake_case : str = "hann_window" , _snake_case : float = 1.0 , _snake_case : float = 80 , _snake_case : float = 76_00 , _snake_case : float = 1E-10 , _snake_case : int = 2 , _snake_case : bool = True , **_snake_case : Union[str, Any] , ): """simple docstring""" super().__init__(feature_size=_snake_case , sampling_rate=_snake_case , padding_value=_snake_case , **_snake_case ) A__ = do_normalize A__ = return_attention_mask A__ = num_mel_bins A__ = hop_length A__ = win_length A__ = win_function A__ = frame_signal_scale A__ = fmin A__ = fmax A__ = mel_floor A__ = reduction_factor A__ = win_length * sampling_rate // 10_00 A__ = hop_length * sampling_rate // 10_00 A__ = optimal_fft_length(self.sample_size ) A__ = (self.n_fft // 2) + 1 A__ = window_function(window_length=self.sample_size , name=self.win_function , periodic=_snake_case ) A__ = mel_filter_bank( num_frequency_bins=self.n_freqs , num_mel_filters=self.num_mel_bins , min_frequency=self.fmin , max_frequency=self.fmax , sampling_rate=self.sampling_rate , norm='slaney' , mel_scale='slaney' , ) if frame_signal_scale != 1.0: warnings.warn( 'The argument `frame_signal_scale` is deprecated and will be removed in version 4.30.0 of Transformers' , _snake_case , ) if reduction_factor != 2.0: warnings.warn( 'The argument `reduction_factor` is deprecated and will be removed in version 4.30.0 of Transformers' , _snake_case , ) @staticmethod # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm def _a ( _snake_case : List[np.ndarray] , _snake_case : List[np.ndarray] , _snake_case : float = 0.0 ): """simple docstring""" if attention_mask is not None: A__ = np.array(_snake_case , np.intaa ) A__ = [] for vector, length in zip(_snake_case , attention_mask.sum(-1 ) ): A__ = (vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1E-7 ) if length < normed_slice.shape[0]: A__ = padding_value normed_input_values.append(_snake_case ) else: A__ = [(x - x.mean()) / np.sqrt(x.var() + 1E-7 ) for x in input_values] return normed_input_values def _a ( self : Tuple , _snake_case : np.ndarray , ): """simple docstring""" A__ = spectrogram( _snake_case , window=self.window , frame_length=self.sample_size , hop_length=self.sample_stride , fft_length=self.n_fft , mel_filters=self.mel_filters , mel_floor=self.mel_floor , log_mel='log10' , ) return log_mel_spec.T def __call__( self : List[str] , _snake_case : Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None , _snake_case : Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None , _snake_case : Union[bool, str, PaddingStrategy] = False , _snake_case : Optional[int] = None , _snake_case : bool = False , _snake_case : Optional[int] = None , _snake_case : Optional[bool] = None , _snake_case : Optional[Union[str, TensorType]] = None , _snake_case : Optional[int] = None , **_snake_case : Tuple , ): """simple docstring""" if audio is None and audio_target is None: raise ValueError('You must provide either `audio` or `audio_target` values.' ) if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' F''' {self.sampling_rate}. Please make sure that the provided audio input was sampled with''' F''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( 'It is strongly recommended to pass the ``sampling_rate`` argument to this function. ' 'Failing to do so can result in silent errors that might be hard to debug.' ) if audio is not None: A__ = self._process_audio( _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , **_snake_case , ) else: A__ = None if audio_target is not None: A__ = self._process_audio( _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , **_snake_case , ) if inputs is None: return inputs_target else: A__ = inputs_target['input_values'] A__ = inputs_target.get('attention_mask' ) if decoder_attention_mask is not None: A__ = decoder_attention_mask return inputs def _a ( self : Tuple , _snake_case : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , _snake_case : bool = False , _snake_case : Union[bool, str, PaddingStrategy] = False , _snake_case : Optional[int] = None , _snake_case : bool = False , _snake_case : Optional[int] = None , _snake_case : Optional[bool] = None , _snake_case : Optional[Union[str, TensorType]] = None , **_snake_case : Tuple , ): """simple docstring""" A__ = isinstance(_snake_case , np.ndarray ) and len(speech.shape ) > 1 if is_batched_numpy and len(speech.shape ) > 2: raise ValueError(F'''Only mono-channel audio is supported for input to {self}''' ) A__ = is_batched_numpy or ( isinstance(_snake_case , (list, tuple) ) and (isinstance(speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: A__ = [np.asarray(_snake_case , dtype=np.floataa ) for speech in speech] elif not is_batched and not isinstance(_snake_case , np.ndarray ): A__ = np.asarray(_snake_case , dtype=np.floataa ) elif isinstance(_snake_case , np.ndarray ) and speech.dtype is np.dtype(np.floataa ): A__ = speech.astype(np.floataa ) # always return batch if not is_batched: A__ = [speech] # needed to make pad() work on spectrogram inputs A__ = self.feature_size # convert into correct format for padding if is_target: A__ = [self._extract_mel_features(_snake_case ) for waveform in speech] A__ = BatchFeature({'input_values': features} ) A__ = self.num_mel_bins else: A__ = BatchFeature({'input_values': speech} ) A__ = self.pad( _snake_case , padding=_snake_case , max_length=_snake_case , truncation=_snake_case , pad_to_multiple_of=_snake_case , return_attention_mask=_snake_case , **_snake_case , ) A__ = feature_size_hack # convert input values to correct format A__ = padded_inputs['input_values'] if not isinstance(input_values[0] , np.ndarray ): A__ = [np.asarray(_snake_case , dtype=np.floataa ) for array in input_values] elif ( not isinstance(_snake_case , np.ndarray ) and isinstance(input_values[0] , np.ndarray ) and input_values[0].dtype is np.dtype(np.floataa ) ): A__ = [array.astype(np.floataa ) for array in input_values] elif isinstance(_snake_case , np.ndarray ) and input_values.dtype is np.dtype(np.floataa ): A__ = input_values.astype(np.floataa ) # convert attention_mask to correct format A__ = padded_inputs.get('attention_mask' ) if attention_mask is not None: A__ = [np.asarray(_snake_case , dtype=np.intaa ) for array in attention_mask] # zero-mean and unit-variance normalization if not is_target and self.do_normalize: A__ = ( attention_mask if self._get_padding_strategies(_snake_case , max_length=_snake_case ) is not PaddingStrategy.DO_NOT_PAD else None ) A__ = self.zero_mean_unit_var_norm( padded_inputs['input_values'] , attention_mask=_snake_case , padding_value=self.padding_value ) if return_tensors is not None: A__ = padded_inputs.convert_to_tensors(_snake_case ) return padded_inputs def _a ( self : Optional[Any] ): """simple docstring""" A__ = super().to_dict() # Don't serialize these as they are derived from the other properties. A__ = ['window', 'mel_filters', 'sample_size', 'sample_stride', 'n_fft', 'n_freqs'] for name in names: if name in output: del output[name] return output
9
1
def A ( __UpperCamelCase ) -> int: A__ = [[0 for _ in range(__UpperCamelCase )] for _ in range(m + 1 )] for i in range(m + 1 ): A__ = 1 for n in range(m + 1 ): for k in range(1 , __UpperCamelCase ): memo[n][k] += memo[n][k - 1] if n - k > 0: memo[n][k] += memo[n - k - 1][k] return memo[m][m - 1] if __name__ == "__main__": import sys if len(sys.argv) == 1: try: SCREAMING_SNAKE_CASE__ = int(input('''Enter a number: ''').strip()) print(partition(n)) except ValueError: print('''Please enter a number.''') else: try: SCREAMING_SNAKE_CASE__ = int(sys.argv[1]) print(partition(n)) except ValueError: print('''Please pass a number.''')
9
import argparse import OmegaConf import torch from diffusers import DDIMScheduler, LDMPipeline, UNetLDMModel, VQModel def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: A__ = OmegaConf.load(__UpperCamelCase ) A__ = torch.load(__UpperCamelCase , map_location='cpu' )['model'] A__ = list(state_dict.keys() ) # extract state_dict for VQVAE A__ = {} A__ = 'first_stage_model.' for key in keys: if key.startswith(__UpperCamelCase ): A__ = state_dict[key] # extract state_dict for UNetLDM A__ = {} A__ = 'model.diffusion_model.' for key in keys: if key.startswith(__UpperCamelCase ): A__ = state_dict[key] A__ = config.model.params.first_stage_config.params A__ = config.model.params.unet_config.params A__ = VQModel(**__UpperCamelCase ).eval() vqvae.load_state_dict(__UpperCamelCase ) A__ = UNetLDMModel(**__UpperCamelCase ).eval() unet.load_state_dict(__UpperCamelCase ) A__ = DDIMScheduler( timesteps=config.model.params.timesteps , beta_schedule='scaled_linear' , beta_start=config.model.params.linear_start , beta_end=config.model.params.linear_end , clip_sample=__UpperCamelCase , ) A__ = LDMPipeline(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) pipeline.save_pretrained(__UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument('''--checkpoint_path''', type=str, required=True) parser.add_argument('''--config_path''', type=str, required=True) parser.add_argument('''--output_path''', type=str, required=True) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_ldm_original(args.checkpoint_path, args.config_path, args.output_path)
9
1
import gc import unittest from diffusers import FlaxStableDiffusionInpaintPipeline from diffusers.utils import is_flax_available, load_image, slow from diffusers.utils.testing_utils import require_flax if is_flax_available(): import jax import jax.numpy as jnp from flax.jax_utils import replicate from flax.training.common_utils import shard @slow @require_flax class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def _a ( self : Union[str, Any] ): """simple docstring""" super().tearDown() gc.collect() def _a ( self : str ): """simple docstring""" A__ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/sd2-inpaint/init_image.png' ) A__ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png' ) A__ = 'xvjiarui/stable-diffusion-2-inpainting' A__ , A__ = FlaxStableDiffusionInpaintPipeline.from_pretrained(_snake_case , safety_checker=_snake_case ) A__ = 'Face of a yellow cat, high resolution, sitting on a park bench' A__ = jax.random.PRNGKey(0 ) A__ = 50 A__ = jax.device_count() A__ = num_samples * [prompt] A__ = num_samples * [init_image] A__ = num_samples * [mask_image] A__ , A__ , A__ = pipeline.prepare_inputs(_snake_case , _snake_case , _snake_case ) # shard inputs and rng A__ = replicate(_snake_case ) A__ = jax.random.split(_snake_case , jax.device_count() ) A__ = shard(_snake_case ) A__ = shard(_snake_case ) A__ = shard(_snake_case ) A__ = pipeline( _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , jit=_snake_case ) A__ = output.images.reshape(_snake_case , 5_12 , 5_12 , 3 ) A__ = images[0, 2_53:2_56, 2_53:2_56, -1] A__ = jnp.asarray(jax.device_get(image_slice.flatten() ) ) A__ = jnp.array( [0.361_1307, 0.3764_9736, 0.375_7408, 0.3821_3953, 0.3929_5167, 0.384_1631, 0.4155_4978, 0.413_7475, 0.421_7084] ) print(F'''output_slice: {output_slice}''' ) assert jnp.abs(output_slice - expected_slice ).max() < 1E-2
9
import json import os import torch from diffusers import UNetaDModel os.makedirs('''hub/hopper-medium-v2/unet/hor32''', exist_ok=True) os.makedirs('''hub/hopper-medium-v2/unet/hor128''', exist_ok=True) os.makedirs('''hub/hopper-medium-v2/value_function''', exist_ok=True) def A ( __UpperCamelCase ) -> Union[str, Any]: if hor == 128: A__ = ('DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D') A__ = (32, 128, 256) A__ = ('UpResnetBlock1D', 'UpResnetBlock1D') elif hor == 32: A__ = ('DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D') A__ = (32, 64, 128, 256) A__ = ('UpResnetBlock1D', 'UpResnetBlock1D', 'UpResnetBlock1D') A__ = torch.load(f'''/Users/bglickenhaus/Documents/diffuser/temporal_unet-hopper-mediumv2-hor{hor}.torch''' ) A__ = model.state_dict() A__ = { 'down_block_types': down_block_types, 'block_out_channels': block_out_channels, 'up_block_types': up_block_types, 'layers_per_block': 1, 'use_timestep_embedding': True, 'out_block_type': 'OutConv1DBlock', 'norm_num_groups': 8, 'downsample_each_block': False, 'in_channels': 14, 'out_channels': 14, 'extra_in_channels': 0, 'time_embedding_type': 'positional', 'flip_sin_to_cos': False, 'freq_shift': 1, 'sample_size': 65_536, 'mid_block_type': 'MidResTemporalBlock1D', 'act_fn': 'mish', } A__ = UNetaDModel(**__UpperCamelCase ) print(f'''length of state dict: {len(state_dict.keys() )}''' ) print(f'''length of value function dict: {len(hf_value_function.state_dict().keys() )}''' ) A__ = dict(zip(model.state_dict().keys() , hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): A__ = state_dict.pop(__UpperCamelCase ) hf_value_function.load_state_dict(__UpperCamelCase ) torch.save(hf_value_function.state_dict() , f'''hub/hopper-medium-v2/unet/hor{hor}/diffusion_pytorch_model.bin''' ) with open(f'''hub/hopper-medium-v2/unet/hor{hor}/config.json''' , 'w' ) as f: json.dump(__UpperCamelCase , __UpperCamelCase ) def A ( ) -> List[str]: A__ = { 'in_channels': 14, 'down_block_types': ('DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D'), 'up_block_types': (), 'out_block_type': 'ValueFunction', 'mid_block_type': 'ValueFunctionMidBlock1D', 'block_out_channels': (32, 64, 128, 256), 'layers_per_block': 1, 'downsample_each_block': True, 'sample_size': 65_536, 'out_channels': 14, 'extra_in_channels': 0, 'time_embedding_type': 'positional', 'use_timestep_embedding': True, 'flip_sin_to_cos': False, 'freq_shift': 1, 'norm_num_groups': 8, 'act_fn': 'mish', } A__ = torch.load('/Users/bglickenhaus/Documents/diffuser/value_function-hopper-mediumv2-hor32.torch' ) A__ = model A__ = UNetaDModel(**__UpperCamelCase ) print(f'''length of state dict: {len(state_dict.keys() )}''' ) print(f'''length of value function dict: {len(hf_value_function.state_dict().keys() )}''' ) A__ = dict(zip(state_dict.keys() , hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): A__ = state_dict.pop(__UpperCamelCase ) hf_value_function.load_state_dict(__UpperCamelCase ) torch.save(hf_value_function.state_dict() , 'hub/hopper-medium-v2/value_function/diffusion_pytorch_model.bin' ) with open('hub/hopper-medium-v2/value_function/config.json' , 'w' ) as f: json.dump(__UpperCamelCase , __UpperCamelCase ) if __name__ == "__main__": unet(3_2) # unet(128) value_function()
9
1
SCREAMING_SNAKE_CASE__ = [sum(int(c, 1_0) ** 2 for c in i.__str__()) for i in range(1_0_0_0_0_0)] def A ( __UpperCamelCase ) -> int: A__ = 0 while number: # Increased Speed Slightly by checking every 5 digits together. sum_of_digits_squared += DIGITS_SQUARED[number % 100_000] number //= 100_000 return sum_of_digits_squared # There are 2 Chains made, # One ends with 89 with the chain member 58 being the one which when declared first, # there will be the least number of iterations for all the members to be checked. # The other one ends with 1 and has only one element 1. # So 58 and 1 are chosen to be declared at the starting. # Changed dictionary to an array to quicken the solution SCREAMING_SNAKE_CASE__ = [None] * 1_0_0_0_0_0_0_0 SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = False def A ( __UpperCamelCase ) -> bool: if CHAINS[number - 1] is not None: return CHAINS[number - 1] # type: ignore A__ = chain(next_number(__UpperCamelCase ) ) A__ = number_chain while number < 10_000_000: A__ = number_chain number *= 10 return number_chain def A ( __UpperCamelCase = 10_000_000 ) -> int: for i in range(1 , __UpperCamelCase ): if CHAINS[i] is None: chain(i + 1 ) return CHAINS[:number].count(__UpperCamelCase ) if __name__ == "__main__": import doctest doctest.testmod() print(f'{solution() = }')
9
from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class __lowerCAmelCase : """simple docstring""" def __init__( self : Dict , _snake_case : Union[str, Any] , _snake_case : Optional[Any]=12 , _snake_case : Any=7 , _snake_case : List[str]=True , _snake_case : int=True , _snake_case : int=True , _snake_case : Tuple=99 , _snake_case : List[Any]=32 , _snake_case : Optional[int]=32 , _snake_case : List[str]=2 , _snake_case : List[str]=4 , _snake_case : List[Any]=37 , _snake_case : Union[str, Any]=0.1 , _snake_case : Tuple=0.1 , _snake_case : Dict=5_12 , _snake_case : Union[str, Any]=0.02 , _snake_case : Any=0 , _snake_case : Optional[Any]=None , ): """simple docstring""" A__ = parent A__ = batch_size A__ = seq_length A__ = is_training A__ = use_input_mask A__ = use_labels A__ = vocab_size A__ = hidden_size A__ = projection_dim A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = dropout A__ = attention_dropout A__ = max_position_embeddings A__ = initializer_range A__ = scope A__ = bos_token_id def _a ( self : Optional[Any] ): """simple docstring""" A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = None if self.use_input_mask: A__ = random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: A__ = input_mask.numpy() A__ , A__ = input_mask.shape A__ = np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(_snake_case ): A__ = 1 A__ = 0 A__ = self.get_config() return config, input_ids, tf.convert_to_tensor(_snake_case ) def _a ( self : Tuple ): """simple docstring""" return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def _a ( self : int , _snake_case : Union[str, Any] , _snake_case : Any , _snake_case : List[str] ): """simple docstring""" A__ = TFBlipTextModel(config=_snake_case ) A__ = model(_snake_case , attention_mask=_snake_case , training=_snake_case ) A__ = model(_snake_case , training=_snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def _a ( self : str ): """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ = config_and_inputs A__ = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_tf class __lowerCAmelCase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Tuple = (TFBlipTextModel,) if is_tf_available() else () A__ : Optional[int] = False A__ : Union[str, Any] = False A__ : Union[str, Any] = False def _a ( self : Any ): """simple docstring""" A__ = BlipTextModelTester(self ) A__ = ConfigTester(self , config_class=_snake_case , hidden_size=37 ) def _a ( self : List[str] ): """simple docstring""" self.config_tester.run_common_tests() def _a ( self : Union[str, Any] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def _a ( self : Tuple ): """simple docstring""" pass def _a ( self : int ): """simple docstring""" pass @unittest.skip(reason='Blip does not use inputs_embeds' ) def _a ( self : Any ): """simple docstring""" pass @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING' ) def _a ( self : str ): """simple docstring""" pass @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING' ) def _a ( self : Optional[Any] ): """simple docstring""" pass @slow def _a ( self : Union[str, Any] ): """simple docstring""" for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = TFBlipTextModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def _a ( self : int , _snake_case : int=True ): """simple docstring""" super().test_pt_tf_model_equivalence(allow_missing_keys=_snake_case )
9
1
import argparse from collections import defaultdict def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Any: A__ = f'''{file}_{class_name}_{test_name}''' done_test[_id] += 1 with open(__UpperCamelCase , 'r' ) as f: A__ = f.readlines() A__ = f'''class {class_name}(''' A__ = f'''{4 * " "}def {test_name}(''' A__ = f'''{8 * " "}{correct_line.split()[0]}''' A__ = f'''{16 * " "}{correct_line.split()[0]}''' A__ = False A__ = False A__ = False A__ = False A__ = 0 A__ = 0 A__ = [] for line in lines: if line.startswith(__UpperCamelCase ): A__ = True elif in_class and line.startswith(__UpperCamelCase ): A__ = True elif in_class and in_func and (line.startswith(__UpperCamelCase ) or line.startswith(__UpperCamelCase )): A__ = len(line.split(correct_line.split()[0] )[0] ) count += 1 if count == done_test[_id]: A__ = True if in_class and in_func and in_line: if ")" not in line: continue else: A__ = True if in_class and in_func and in_line and insert_line: new_lines.append(f'''{spaces * " "}{correct_line}''' ) A__ = A__ = A__ = A__ = False else: new_lines.append(__UpperCamelCase ) with open(__UpperCamelCase , 'w' ) as f: for line in new_lines: f.write(__UpperCamelCase ) def A ( __UpperCamelCase , __UpperCamelCase=None ) -> str: if fail is not None: with open(__UpperCamelCase , 'r' ) as f: A__ = {l.strip() for l in f.readlines()} else: A__ = None with open(__UpperCamelCase , 'r' ) as f: A__ = f.readlines() A__ = defaultdict(__UpperCamelCase ) for line in correct_lines: A__ , A__ , A__ , A__ = line.split(';' ) if test_failures is None or "::".join([file, class_name, test_name] ) in test_failures: overwrite_file(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument('''--correct_filename''', help='''filename of tests with expected result''') parser.add_argument('''--fail_filename''', help='''filename of test failures''', type=str, default=None) SCREAMING_SNAKE_CASE__ = parser.parse_args() main(args.correct_filename, args.fail_filename)
9
from __future__ import annotations from typing import Any def A ( __UpperCamelCase ) -> int: if not postfix_notation: return 0 A__ = {'+', '-', '*', '/'} A__ = [] for token in postfix_notation: if token in operations: A__ , A__ = stack.pop(), stack.pop() if token == "+": stack.append(a + b ) elif token == "-": stack.append(a - b ) elif token == "*": stack.append(a * b ) else: if a * b < 0 and a % b != 0: stack.append(a // b + 1 ) else: stack.append(a // b ) else: stack.append(int(__UpperCamelCase ) ) return stack.pop() if __name__ == "__main__": import doctest doctest.testmod()
9
1
import argparse import json import pickle from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import MaskFormerConfig, MaskFormerForInstanceSegmentation, MaskFormerImageProcessor, SwinConfig from transformers.utils import logging logging.set_verbosity_info() SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) def A ( __UpperCamelCase ) -> Union[str, Any]: A__ = SwinConfig.from_pretrained( 'microsoft/swin-tiny-patch4-window7-224' , out_features=['stage1', 'stage2', 'stage3', 'stage4'] ) A__ = MaskFormerConfig(backbone_config=__UpperCamelCase ) A__ = 'huggingface/label-files' if "ade20k-full" in model_name: # this should be ok A__ = 847 A__ = 'maskformer-ade20k-full-id2label.json' elif "ade" in model_name: # this should be ok A__ = 150 A__ = 'ade20k-id2label.json' elif "coco-stuff" in model_name: # this should be ok A__ = 171 A__ = 'maskformer-coco-stuff-id2label.json' elif "coco" in model_name: # TODO A__ = 133 A__ = 'coco-panoptic-id2label.json' elif "cityscapes" in model_name: # this should be ok A__ = 19 A__ = 'cityscapes-id2label.json' elif "vistas" in model_name: # this should be ok A__ = 65 A__ = 'mapillary-vistas-id2label.json' A__ = json.load(open(hf_hub_download(__UpperCamelCase , __UpperCamelCase , repo_type='dataset' ) , 'r' ) ) A__ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} return config def A ( __UpperCamelCase ) -> Dict: A__ = [] # stem # fmt: off rename_keys.append(('backbone.patch_embed.proj.weight', 'model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.weight') ) rename_keys.append(('backbone.patch_embed.proj.bias', 'model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.bias') ) rename_keys.append(('backbone.patch_embed.norm.weight', 'model.pixel_level_module.encoder.model.embeddings.norm.weight') ) rename_keys.append(('backbone.patch_embed.norm.bias', 'model.pixel_level_module.encoder.model.embeddings.norm.bias') ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((f'''backbone.layers.{i}.blocks.{j}.norm1.weight''', f'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight''') ) rename_keys.append((f'''backbone.layers.{i}.blocks.{j}.norm1.bias''', f'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias''') ) rename_keys.append((f'''backbone.layers.{i}.blocks.{j}.attn.relative_position_bias_table''', f'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table''') ) rename_keys.append((f'''backbone.layers.{i}.blocks.{j}.attn.relative_position_index''', f'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index''') ) rename_keys.append((f'''backbone.layers.{i}.blocks.{j}.attn.proj.weight''', f'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight''') ) rename_keys.append((f'''backbone.layers.{i}.blocks.{j}.attn.proj.bias''', f'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias''') ) rename_keys.append((f'''backbone.layers.{i}.blocks.{j}.norm2.weight''', f'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight''') ) rename_keys.append((f'''backbone.layers.{i}.blocks.{j}.norm2.bias''', f'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias''') ) rename_keys.append((f'''backbone.layers.{i}.blocks.{j}.mlp.fc1.weight''', f'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight''') ) rename_keys.append((f'''backbone.layers.{i}.blocks.{j}.mlp.fc1.bias''', f'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias''') ) rename_keys.append((f'''backbone.layers.{i}.blocks.{j}.mlp.fc2.weight''', f'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.weight''') ) rename_keys.append((f'''backbone.layers.{i}.blocks.{j}.mlp.fc2.bias''', f'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.bias''') ) if i < 3: rename_keys.append((f'''backbone.layers.{i}.downsample.reduction.weight''', f'''model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.reduction.weight''') ) rename_keys.append((f'''backbone.layers.{i}.downsample.norm.weight''', f'''model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.weight''') ) rename_keys.append((f'''backbone.layers.{i}.downsample.norm.bias''', f'''model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.bias''') ) rename_keys.append((f'''backbone.norm{i}.weight''', f'''model.pixel_level_module.encoder.hidden_states_norms.{i}.weight''') ) rename_keys.append((f'''backbone.norm{i}.bias''', f'''model.pixel_level_module.encoder.hidden_states_norms.{i}.bias''') ) # FPN rename_keys.append(('sem_seg_head.layer_4.weight', 'model.pixel_level_module.decoder.fpn.stem.0.weight') ) rename_keys.append(('sem_seg_head.layer_4.norm.weight', 'model.pixel_level_module.decoder.fpn.stem.1.weight') ) rename_keys.append(('sem_seg_head.layer_4.norm.bias', 'model.pixel_level_module.decoder.fpn.stem.1.bias') ) for source_index, target_index in zip(range(3 , 0 , -1 ) , range(0 , 3 ) ): rename_keys.append((f'''sem_seg_head.adapter_{source_index}.weight''', f'''model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.0.weight''') ) rename_keys.append((f'''sem_seg_head.adapter_{source_index}.norm.weight''', f'''model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.weight''') ) rename_keys.append((f'''sem_seg_head.adapter_{source_index}.norm.bias''', f'''model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.bias''') ) rename_keys.append((f'''sem_seg_head.layer_{source_index}.weight''', f'''model.pixel_level_module.decoder.fpn.layers.{target_index}.block.0.weight''') ) rename_keys.append((f'''sem_seg_head.layer_{source_index}.norm.weight''', f'''model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.weight''') ) rename_keys.append((f'''sem_seg_head.layer_{source_index}.norm.bias''', f'''model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.bias''') ) rename_keys.append(('sem_seg_head.mask_features.weight', 'model.pixel_level_module.decoder.mask_projection.weight') ) rename_keys.append(('sem_seg_head.mask_features.bias', 'model.pixel_level_module.decoder.mask_projection.bias') ) # Transformer decoder for idx in range(config.decoder_config.decoder_layers ): # self-attention out projection rename_keys.append((f'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.weight''', f'''model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.weight''') ) rename_keys.append((f'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.bias''', f'''model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.bias''') ) # cross-attention out projection rename_keys.append((f'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.weight''', f'''model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.weight''') ) rename_keys.append((f'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.bias''', f'''model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.bias''') ) # MLP 1 rename_keys.append((f'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.weight''', f'''model.transformer_module.decoder.layers.{idx}.fc1.weight''') ) rename_keys.append((f'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.bias''', f'''model.transformer_module.decoder.layers.{idx}.fc1.bias''') ) # MLP 2 rename_keys.append((f'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.weight''', f'''model.transformer_module.decoder.layers.{idx}.fc2.weight''') ) rename_keys.append((f'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.bias''', f'''model.transformer_module.decoder.layers.{idx}.fc2.bias''') ) # layernorm 1 (self-attention layernorm) rename_keys.append((f'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.weight''', f'''model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.weight''') ) rename_keys.append((f'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.bias''', f'''model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.bias''') ) # layernorm 2 (cross-attention layernorm) rename_keys.append((f'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.weight''', f'''model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.weight''') ) rename_keys.append((f'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.bias''', f'''model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.bias''') ) # layernorm 3 (final layernorm) rename_keys.append((f'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.weight''', f'''model.transformer_module.decoder.layers.{idx}.final_layer_norm.weight''') ) rename_keys.append((f'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.bias''', f'''model.transformer_module.decoder.layers.{idx}.final_layer_norm.bias''') ) rename_keys.append(('sem_seg_head.predictor.transformer.decoder.norm.weight', 'model.transformer_module.decoder.layernorm.weight') ) rename_keys.append(('sem_seg_head.predictor.transformer.decoder.norm.bias', 'model.transformer_module.decoder.layernorm.bias') ) # heads on top rename_keys.append(('sem_seg_head.predictor.query_embed.weight', 'model.transformer_module.queries_embedder.weight') ) rename_keys.append(('sem_seg_head.predictor.input_proj.weight', 'model.transformer_module.input_projection.weight') ) rename_keys.append(('sem_seg_head.predictor.input_proj.bias', 'model.transformer_module.input_projection.bias') ) rename_keys.append(('sem_seg_head.predictor.class_embed.weight', 'class_predictor.weight') ) rename_keys.append(('sem_seg_head.predictor.class_embed.bias', 'class_predictor.bias') ) for i in range(3 ): rename_keys.append((f'''sem_seg_head.predictor.mask_embed.layers.{i}.weight''', f'''mask_embedder.{i}.0.weight''') ) rename_keys.append((f'''sem_seg_head.predictor.mask_embed.layers.{i}.bias''', f'''mask_embedder.{i}.0.bias''') ) # fmt: on return rename_keys def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[str]: A__ = dct.pop(__UpperCamelCase ) A__ = val def A ( __UpperCamelCase , __UpperCamelCase ) -> str: A__ = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): A__ = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) A__ = state_dict.pop(f'''backbone.layers.{i}.blocks.{j}.attn.qkv.weight''' ) A__ = state_dict.pop(f'''backbone.layers.{i}.blocks.{j}.attn.qkv.bias''' ) # next, add query, keys and values (in that order) to the state dict A__ = in_proj_weight[:dim, :] A__ = in_proj_bias[: dim] A__ = in_proj_weight[ dim : dim * 2, : ] A__ = in_proj_bias[ dim : dim * 2 ] A__ = in_proj_weight[ -dim :, : ] A__ = in_proj_bias[-dim :] # fmt: on def A ( __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: # fmt: off A__ = config.decoder_config.hidden_size for idx in range(config.decoder_config.decoder_layers ): # read in weights + bias of self-attention input projection layer (in the original implementation, this is a single matrix + bias) A__ = state_dict.pop(f'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_weight''' ) A__ = state_dict.pop(f'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_bias''' ) # next, add query, keys and values (in that order) to the state dict A__ = in_proj_weight[: hidden_size, :] A__ = in_proj_bias[:config.hidden_size] A__ = in_proj_weight[hidden_size : hidden_size * 2, :] A__ = in_proj_bias[hidden_size : hidden_size * 2] A__ = in_proj_weight[-hidden_size :, :] A__ = in_proj_bias[-hidden_size :] # read in weights + bias of cross-attention input projection layer (in the original implementation, this is a single matrix + bias) A__ = state_dict.pop(f'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_weight''' ) A__ = state_dict.pop(f'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_bias''' ) # next, add query, keys and values (in that order) to the state dict A__ = in_proj_weight[: hidden_size, :] A__ = in_proj_bias[:config.hidden_size] A__ = in_proj_weight[hidden_size : hidden_size * 2, :] A__ = in_proj_bias[hidden_size : hidden_size * 2] A__ = in_proj_weight[-hidden_size :, :] A__ = in_proj_bias[-hidden_size :] # fmt: on def A ( ) -> torch.Tensor: A__ = 'http://images.cocodataset.org/val2017/000000039769.jpg' A__ = Image.open(requests.get(__UpperCamelCase , stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = False ) -> List[Any]: A__ = get_maskformer_config(__UpperCamelCase ) # load original state_dict with open(__UpperCamelCase , 'rb' ) as f: A__ = pickle.load(__UpperCamelCase ) A__ = data['model'] # for name, param in state_dict.items(): # print(name, param.shape) # rename keys A__ = create_rename_keys(__UpperCamelCase ) for src, dest in rename_keys: rename_key(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) read_in_swin_q_k_v(__UpperCamelCase , config.backbone_config ) read_in_decoder_q_k_v(__UpperCamelCase , __UpperCamelCase ) # update to torch tensors for key, value in state_dict.items(): A__ = torch.from_numpy(__UpperCamelCase ) # load 🤗 model A__ = MaskFormerForInstanceSegmentation(__UpperCamelCase ) model.eval() for name, param in model.named_parameters(): print(__UpperCamelCase , param.shape ) A__ , A__ = model.load_state_dict(__UpperCamelCase , strict=__UpperCamelCase ) assert missing_keys == [ "model.pixel_level_module.encoder.model.layernorm.weight", "model.pixel_level_module.encoder.model.layernorm.bias", ] assert len(__UpperCamelCase ) == 0, f'''Unexpected keys: {unexpected_keys}''' # verify results A__ = prepare_img() if "vistas" in model_name: A__ = 65 elif "cityscapes" in model_name: A__ = 65_535 else: A__ = 255 A__ = True if 'ade' in model_name else False A__ = MaskFormerImageProcessor(ignore_index=__UpperCamelCase , reduce_labels=__UpperCamelCase ) A__ = image_processor(__UpperCamelCase , return_tensors='pt' ) A__ = model(**__UpperCamelCase ) print('Logits:' , outputs.class_queries_logits[0, :3, :3] ) if model_name == "maskformer-swin-tiny-ade": A__ = torch.tensor( [[3.6353, -4.4770, -2.6065], [0.5081, -4.2394, -3.5343], [2.1909, -5.0353, -1.9323]] ) assert torch.allclose(outputs.class_queries_logits[0, :3, :3] , __UpperCamelCase , atol=1E-4 ) print('Looks ok!' ) if pytorch_dump_folder_path is not None: print(f'''Saving model and image processor to {pytorch_dump_folder_path}''' ) Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) model.save_pretrained(__UpperCamelCase ) image_processor.save_pretrained(__UpperCamelCase ) if push_to_hub: print('Pushing model and image processor to the hub...' ) model.push_to_hub(f'''nielsr/{model_name}''' ) image_processor.push_to_hub(f'''nielsr/{model_name}''' ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''maskformer-swin-tiny-ade''', type=str, help=('''Name of the MaskFormer model you\'d like to convert''',), ) parser.add_argument( '''--checkpoint_path''', default='''/Users/nielsrogge/Documents/MaskFormer_checkpoints/MaskFormer-Swin-tiny-ADE20k/model.pkl''', type=str, help='''Path to the original state dict (.pth file).''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_maskformer_checkpoint( args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
9
from __future__ import annotations def A ( __UpperCamelCase = 4 ) -> list[list[int]]: A__ = abs(__UpperCamelCase ) or 4 return [[1 + x + y * row_size for x in range(__UpperCamelCase )] for y in range(__UpperCamelCase )] def A ( __UpperCamelCase ) -> list[list[int]]: return reverse_row(transpose(__UpperCamelCase ) ) # OR.. transpose(reverse_column(matrix)) def A ( __UpperCamelCase ) -> list[list[int]]: return reverse_row(reverse_column(__UpperCamelCase ) ) # OR.. reverse_column(reverse_row(matrix)) def A ( __UpperCamelCase ) -> list[list[int]]: return reverse_column(transpose(__UpperCamelCase ) ) # OR.. transpose(reverse_row(matrix)) def A ( __UpperCamelCase ) -> list[list[int]]: A__ = [list(__UpperCamelCase ) for x in zip(*__UpperCamelCase )] return matrix def A ( __UpperCamelCase ) -> list[list[int]]: A__ = matrix[::-1] return matrix def A ( __UpperCamelCase ) -> list[list[int]]: A__ = [x[::-1] for x in matrix] return matrix def A ( __UpperCamelCase ) -> None: for i in matrix: print(*__UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = make_matrix() print('''\norigin:\n''') print_matrix(matrix) print('''\nrotate 90 counterclockwise:\n''') print_matrix(rotate_aa(matrix)) SCREAMING_SNAKE_CASE__ = make_matrix() print('''\norigin:\n''') print_matrix(matrix) print('''\nrotate 180:\n''') print_matrix(rotate_aaa(matrix)) SCREAMING_SNAKE_CASE__ = make_matrix() print('''\norigin:\n''') print_matrix(matrix) print('''\nrotate 270 counterclockwise:\n''') print_matrix(rotate_aaa(matrix))
9
1
import os from typing import List, Optional, Union from ...image_processing_utils import BatchFeature from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType from ..auto import AutoTokenizer class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : int = ["image_processor", "tokenizer"] A__ : List[str] = "BlipImageProcessor" A__ : Union[str, Any] = "AutoTokenizer" def __init__( self : List[Any] , _snake_case : List[str] , _snake_case : List[Any] , _snake_case : List[Any] ): """simple docstring""" super().__init__(_snake_case , _snake_case ) # add QFormer tokenizer A__ = qformer_tokenizer def __call__( self : int , _snake_case : ImageInput = None , _snake_case : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , _snake_case : bool = True , _snake_case : Union[bool, str, PaddingStrategy] = False , _snake_case : Union[bool, str, TruncationStrategy] = None , _snake_case : Optional[int] = None , _snake_case : int = 0 , _snake_case : Optional[int] = None , _snake_case : Optional[bool] = None , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = True , _snake_case : Optional[Union[str, TensorType]] = None , **_snake_case : Any , ): """simple docstring""" if images is None and text is None: raise ValueError('You have to specify at least images or text.' ) A__ = BatchFeature() if text is not None: A__ = self.tokenizer( text=_snake_case , add_special_tokens=_snake_case , padding=_snake_case , truncation=_snake_case , max_length=_snake_case , stride=_snake_case , pad_to_multiple_of=_snake_case , return_attention_mask=_snake_case , return_overflowing_tokens=_snake_case , return_special_tokens_mask=_snake_case , return_offsets_mapping=_snake_case , return_token_type_ids=_snake_case , return_length=_snake_case , verbose=_snake_case , return_tensors=_snake_case , **_snake_case , ) encoding.update(_snake_case ) A__ = self.qformer_tokenizer( text=_snake_case , add_special_tokens=_snake_case , padding=_snake_case , truncation=_snake_case , max_length=_snake_case , stride=_snake_case , pad_to_multiple_of=_snake_case , return_attention_mask=_snake_case , return_overflowing_tokens=_snake_case , return_special_tokens_mask=_snake_case , return_offsets_mapping=_snake_case , return_token_type_ids=_snake_case , return_length=_snake_case , verbose=_snake_case , return_tensors=_snake_case , **_snake_case , ) A__ = qformer_text_encoding.pop('input_ids' ) A__ = qformer_text_encoding.pop('attention_mask' ) if images is not None: A__ = self.image_processor(_snake_case , return_tensors=_snake_case ) encoding.update(_snake_case ) return encoding def _a ( self : Optional[int] , *_snake_case : List[str] , **_snake_case : List[str] ): """simple docstring""" return self.tokenizer.batch_decode(*_snake_case , **_snake_case ) def _a ( self : Any , *_snake_case : Union[str, Any] , **_snake_case : Any ): """simple docstring""" return self.tokenizer.decode(*_snake_case , **_snake_case ) @property # Copied from transformers.models.blip.processing_blip.BlipProcessor.model_input_names def _a ( self : Optional[Any] ): """simple docstring""" A__ = self.tokenizer.model_input_names A__ = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) def _a ( self : int , _snake_case : List[Any] , **_snake_case : List[Any] ): """simple docstring""" if os.path.isfile(_snake_case ): raise ValueError(F'''Provided path ({save_directory}) should be a directory, not a file''' ) os.makedirs(_snake_case , exist_ok=_snake_case ) A__ = os.path.join(_snake_case , 'qformer_tokenizer' ) self.qformer_tokenizer.save_pretrained(_snake_case ) return super().save_pretrained(_snake_case , **_snake_case ) @classmethod def _a ( cls : int , _snake_case : Optional[Any] , **_snake_case : Optional[int] ): """simple docstring""" A__ = AutoTokenizer.from_pretrained(_snake_case , subfolder='qformer_tokenizer' ) A__ = cls._get_arguments_from_pretrained(_snake_case , **_snake_case ) args.append(_snake_case ) return cls(*_snake_case )
9
from __future__ import annotations from fractions import Fraction def A ( __UpperCamelCase , __UpperCamelCase ) -> bool: return ( num != den and num % 10 == den // 10 and (num // 10) / (den % 10) == num / den ) def A ( __UpperCamelCase ) -> list[str]: A__ = [] A__ = 11 A__ = int('1' + '0' * digit_len ) for num in range(__UpperCamelCase , __UpperCamelCase ): while den <= 99: if (num != den) and (num % 10 == den // 10) and (den % 10 != 0): if is_digit_cancelling(__UpperCamelCase , __UpperCamelCase ): solutions.append(f'''{num}/{den}''' ) den += 1 num += 1 A__ = 10 return solutions def A ( __UpperCamelCase = 2 ) -> int: A__ = 1.0 for fraction in fraction_list(__UpperCamelCase ): A__ = Fraction(__UpperCamelCase ) result *= frac.denominator / frac.numerator return int(__UpperCamelCase ) if __name__ == "__main__": print(solution())
9
1
def A ( __UpperCamelCase ) -> str: if number > 0: raise ValueError('input must be a negative integer' ) A__ = len(bin(__UpperCamelCase )[3:] ) A__ = bin(abs(__UpperCamelCase ) - (1 << binary_number_length) )[3:] A__ = ( ( '1' + '0' * (binary_number_length - len(__UpperCamelCase )) + twos_complement_number ) if number < 0 else '0' ) return "0b" + twos_complement_number if __name__ == "__main__": import doctest doctest.testmod()
9
# 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_tokenizers_available, is_torch_available SCREAMING_SNAKE_CASE__ = {'''configuration_mra''': ['''MRA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MraConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''MRA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MraForMaskedLM''', '''MraForMultipleChoice''', '''MraForQuestionAnswering''', '''MraForSequenceClassification''', '''MraForTokenClassification''', '''MraLayer''', '''MraModel''', '''MraPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_mra import MRA_PRETRAINED_CONFIG_ARCHIVE_MAP, MraConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mra import ( MRA_PRETRAINED_MODEL_ARCHIVE_LIST, MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, MraLayer, MraModel, MraPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
9
1
import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( UniSpeechConfig, UniSpeechForCTC, UniSpeechForPreTraining, WavaVecaFeatureExtractor, WavaVecaPhonemeCTCTokenizer, WavaVecaProcessor, logging, ) logging.set_verbosity_info() SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = { '''post_extract_proj''': '''feature_projection.projection''', '''encoder.pos_conv.0''': '''encoder.pos_conv_embed.conv''', '''self_attn.k_proj''': '''encoder.layers.*.attention.k_proj''', '''self_attn.v_proj''': '''encoder.layers.*.attention.v_proj''', '''self_attn.q_proj''': '''encoder.layers.*.attention.q_proj''', '''self_attn.out_proj''': '''encoder.layers.*.attention.out_proj''', '''self_attn_layer_norm''': '''encoder.layers.*.layer_norm''', '''fc1''': '''encoder.layers.*.feed_forward.intermediate_dense''', '''fc2''': '''encoder.layers.*.feed_forward.output_dense''', '''final_layer_norm''': '''encoder.layers.*.final_layer_norm''', '''encoder.layer_norm''': '''encoder.layer_norm''', '''w2v_model.layer_norm''': '''feature_projection.layer_norm''', '''quantizer.weight_proj''': '''quantizer.weight_proj''', '''quantizer.vars''': '''quantizer.codevectors''', '''project_q''': '''project_q''', '''final_proj''': '''project_hid''', '''w2v_encoder.proj''': '''ctc_proj''', '''mask_emb''': '''masked_spec_embed''', } SCREAMING_SNAKE_CASE__ = [ '''ctc_proj''', '''quantizer.weight_proj''', '''quantizer.codevectors''', '''project_q''', '''project_hid''', ] def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Dict: for attribute in key.split('.' ): if is_finetuned: if attribute in ["quantizer", "project_q", "project_hid"]: # those layers are only relevant for pretraining and should be dropped return if attribute == "ctc_proj": # we should rename `ctc_proj` to `lm_head` for fine-tuned phoneme models A__ = 'lm_head' A__ = getattr(__UpperCamelCase , __UpperCamelCase ) if weight_type is not None: A__ = getattr(__UpperCamelCase , __UpperCamelCase ).shape else: A__ = hf_pointer.shape assert hf_shape == value.shape, ( f'''Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be''' f''' {value.shape} for {full_name}''' ) if weight_type == "weight": A__ = value elif weight_type == "weight_g": A__ = value elif weight_type == "weight_v": A__ = value elif weight_type == "bias": A__ = value else: A__ = value logger.info(f'''{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.''' ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[Any]: A__ = [] A__ = fairseq_model.state_dict() A__ = hf_model.unispeech.feature_extractor for name, value in fairseq_dict.items(): A__ = False if "conv_layers" in name: load_conv_layer( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , hf_model.config.feat_extract_norm == 'group' , ) A__ = True else: for key, mapped_key in MAPPING.items(): A__ = 'unispeech.' + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if key in name or key.split('w2v_model.' )[-1] == name.split('.' )[0]: A__ = True if "*" in mapped_key: A__ = name.split(__UpperCamelCase )[0].split('.' )[-2] A__ = mapped_key.replace('*' , __UpperCamelCase ) if "weight_g" in name: A__ = 'weight_g' elif "weight_v" in name: A__ = 'weight_v' elif "bias" in name: A__ = 'bias' elif "weight" in name: # TODO: don't match quantizer.weight_proj A__ = 'weight' else: A__ = None set_recursively(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) continue if not is_used: unused_weights.append(__UpperCamelCase ) logger.warning(f'''Unused weights: {unused_weights}''' ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Tuple: A__ = full_name.split('conv_layers.' )[-1] A__ = name.split('.' ) A__ = int(items[0] ) A__ = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.''' ) A__ = value logger.info(f'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.''' ) A__ = value logger.info(f'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( f'''{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was''' " found." ) A__ = value logger.info(f'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.''' ) A__ = value logger.info(f'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) else: unused_weights.append(__UpperCamelCase ) @torch.no_grad() def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=None , __UpperCamelCase=None , __UpperCamelCase=True ) -> Optional[Any]: if config_path is not None: A__ = UniSpeechConfig.from_pretrained(__UpperCamelCase ) else: A__ = UniSpeechConfig() if is_finetuned: if dict_path: A__ = Dictionary.load_from_json(__UpperCamelCase ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq A__ = target_dict.pad_index A__ = target_dict.bos_index A__ = target_dict.eos_index A__ = len(target_dict.symbols ) A__ = os.path.join(__UpperCamelCase , 'vocab.json' ) if not os.path.isdir(__UpperCamelCase ): logger.error('--pytorch_dump_folder_path ({}) should be a directory'.format(__UpperCamelCase ) ) return os.makedirs(__UpperCamelCase , exist_ok=__UpperCamelCase ) A__ = target_dict.indices # fairseq has the <pad> and <s> switched A__ = 42 A__ = 43 with open(__UpperCamelCase , 'w' , encoding='utf-8' ) as vocab_handle: json.dump(__UpperCamelCase , __UpperCamelCase ) A__ = WavaVecaPhonemeCTCTokenizer( __UpperCamelCase , unk_token=target_dict.unk_word , pad_token=target_dict.pad_word , bos_token=target_dict.bos_word , eos_token=target_dict.eos_word , word_delimiter_token='|' , do_lower_case=__UpperCamelCase , ) A__ = True if config.feat_extract_norm == 'layer' else False A__ = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16_000 , padding_value=0 , do_normalize=__UpperCamelCase , return_attention_mask=__UpperCamelCase , ) A__ = WavaVecaProcessor(feature_extractor=__UpperCamelCase , tokenizer=__UpperCamelCase ) processor.save_pretrained(__UpperCamelCase ) A__ = UniSpeechForCTC(__UpperCamelCase ) else: A__ = UniSpeechForPreTraining(__UpperCamelCase ) if is_finetuned: A__ , A__ , A__ = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={'data': '/'.join(dict_path.split('/' )[:-1] ), 'w2v_path': checkpoint_path} ) else: A__ , A__ , A__ = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] ) A__ = model[0].eval() recursively_load_weights(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) hf_unispeech.save_pretrained(__UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument('''--checkpoint_path''', default=None, type=str, help='''Path to fairseq checkpoint''') parser.add_argument('''--dict_path''', default=None, type=str, help='''Path to dict of fine-tuned model''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') parser.add_argument( '''--not_finetuned''', action='''store_true''', help='''Whether the model to convert is a fine-tuned model or not''' ) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_unispeech_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned )
9
SCREAMING_SNAKE_CASE__ = ''' # Transformers installation ! pip install transformers datasets # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git ''' SCREAMING_SNAKE_CASE__ = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] SCREAMING_SNAKE_CASE__ = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
9
1
def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Optional[Any]: if n == 0: return 1 elif n % 2 == 1: return (binary_exponentiation(__UpperCamelCase , n - 1 , __UpperCamelCase ) * a) % mod else: A__ = binary_exponentiation(__UpperCamelCase , n / 2 , __UpperCamelCase ) return (b * b) % mod # a prime number SCREAMING_SNAKE_CASE__ = 7_0_1 SCREAMING_SNAKE_CASE__ = 1_0_0_0_0_0_0_0_0_0 SCREAMING_SNAKE_CASE__ = 1_0 # 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)
9
import unittest from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __lowerCAmelCase : """simple docstring""" @staticmethod def _a ( *_snake_case : Any , **_snake_case : Optional[int] ): """simple docstring""" pass @is_pipeline_test @require_torch @require_vision class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" A__ : Union[str, Any] = MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING def _a ( self : List[Any] , _snake_case : Union[str, Any] , _snake_case : Tuple , _snake_case : Union[str, Any] ): """simple docstring""" A__ = pipeline('visual-question-answering' , model='hf-internal-testing/tiny-vilt-random-vqa' ) A__ = [ { 'image': Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ), 'question': 'How many cats are there?', }, { 'image': './tests/fixtures/tests_samples/COCO/000000039769.png', 'question': 'How many cats are there?', }, ] return vqa_pipeline, examples def _a ( self : Optional[Any] , _snake_case : Union[str, Any] , _snake_case : List[str] ): """simple docstring""" A__ = vqa_pipeline(_snake_case , top_k=1 ) self.assertEqual( _snake_case , [ [{'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}], [{'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}], ] , ) @require_torch def _a ( self : Any ): """simple docstring""" A__ = pipeline('visual-question-answering' , model='hf-internal-testing/tiny-vilt-random-vqa' ) A__ = './tests/fixtures/tests_samples/COCO/000000039769.png' A__ = 'How many cats are there?' A__ = vqa_pipeline(image=_snake_case , question='How many cats are there?' , top_k=2 ) self.assertEqual( _snake_case , [{'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}, {'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}] ) A__ = vqa_pipeline({'image': image, 'question': question} , top_k=2 ) self.assertEqual( _snake_case , [{'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}, {'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}] ) @slow @require_torch def _a ( self : Any ): """simple docstring""" A__ = pipeline('visual-question-answering' , model='dandelin/vilt-b32-finetuned-vqa' ) A__ = './tests/fixtures/tests_samples/COCO/000000039769.png' A__ = 'How many cats are there?' A__ = vqa_pipeline(image=_snake_case , question=_snake_case , top_k=2 ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [{'score': 0.8799, 'answer': '2'}, {'score': 0.296, 'answer': '1'}] ) A__ = vqa_pipeline({'image': image, 'question': question} , top_k=2 ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [{'score': 0.8799, 'answer': '2'}, {'score': 0.296, 'answer': '1'}] ) A__ = vqa_pipeline( [{'image': image, 'question': question}, {'image': image, 'question': question}] , top_k=2 ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [[{'score': 0.8799, 'answer': '2'}, {'score': 0.296, 'answer': '1'}]] * 2 , ) @require_tf @unittest.skip('Visual question answering not implemented in TF' ) def _a ( self : Dict ): """simple docstring""" pass
9
1
import inspect import unittest from transformers import SegformerConfig, is_torch_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_MAPPING, SegformerForImageClassification, SegformerForSemanticSegmentation, SegformerModel, ) from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import SegformerImageProcessor class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def _a ( self : List[str] ): """simple docstring""" A__ = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(_snake_case , 'hidden_sizes' ) ) self.parent.assertTrue(hasattr(_snake_case , 'num_attention_heads' ) ) self.parent.assertTrue(hasattr(_snake_case , 'num_encoder_blocks' ) ) class __lowerCAmelCase : """simple docstring""" def __init__( self : Any , _snake_case : str , _snake_case : Union[str, Any]=13 , _snake_case : Any=64 , _snake_case : Optional[Any]=3 , _snake_case : Dict=4 , _snake_case : Tuple=[2, 2, 2, 2] , _snake_case : str=[8, 4, 2, 1] , _snake_case : Union[str, Any]=[16, 32, 64, 1_28] , _snake_case : int=[1, 4, 8, 16] , _snake_case : List[str]=[1, 2, 4, 8] , _snake_case : int=True , _snake_case : int=True , _snake_case : Union[str, Any]="gelu" , _snake_case : Optional[int]=0.1 , _snake_case : Tuple=0.1 , _snake_case : Dict=0.02 , _snake_case : Tuple=3 , _snake_case : int=None , ): """simple docstring""" A__ = parent A__ = batch_size A__ = image_size A__ = num_channels A__ = num_encoder_blocks A__ = sr_ratios A__ = depths A__ = hidden_sizes A__ = downsampling_rates A__ = num_attention_heads A__ = is_training A__ = use_labels A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = initializer_range A__ = num_labels A__ = scope def _a ( self : int ): """simple docstring""" A__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) A__ = self.get_config() return config, pixel_values, labels def _a ( self : int ): """simple docstring""" return SegformerConfig( image_size=self.image_size , num_channels=self.num_channels , num_encoder_blocks=self.num_encoder_blocks , depths=self.depths , hidden_sizes=self.hidden_sizes , num_attention_heads=self.num_attention_heads , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , initializer_range=self.initializer_range , ) def _a ( self : int , _snake_case : Optional[Any] , _snake_case : int , _snake_case : Any ): """simple docstring""" A__ = SegformerModel(config=_snake_case ) model.to(_snake_case ) model.eval() A__ = model(_snake_case ) A__ = A__ = self.image_size // (self.downsampling_rates[-1] * 2) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width) ) def _a ( self : Union[str, Any] , _snake_case : Union[str, Any] , _snake_case : Tuple , _snake_case : Dict ): """simple docstring""" A__ = self.num_labels A__ = SegformerForSemanticSegmentation(_snake_case ) model.to(_snake_case ) model.eval() A__ = model(_snake_case ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4) ) A__ = model(_snake_case , labels=_snake_case ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4) ) self.parent.assertGreater(result.loss , 0.0 ) def _a ( self : List[str] , _snake_case : Optional[Any] , _snake_case : Union[str, Any] , _snake_case : List[str] ): """simple docstring""" A__ = 1 A__ = SegformerForSemanticSegmentation(config=_snake_case ) model.to(_snake_case ) model.eval() A__ = torch.randint(0 , 1 , (self.batch_size, self.image_size, self.image_size) ).to(_snake_case ) A__ = model(_snake_case , labels=_snake_case ) self.parent.assertGreater(result.loss , 0.0 ) def _a ( self : List[Any] ): """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ = config_and_inputs A__ = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class __lowerCAmelCase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Optional[int] = ( ( SegformerModel, SegformerForSemanticSegmentation, SegformerForImageClassification, ) if is_torch_available() else () ) A__ : Union[str, Any] = ( { "feature-extraction": SegformerModel, "image-classification": SegformerForImageClassification, "image-segmentation": SegformerForSemanticSegmentation, } if is_torch_available() else {} ) A__ : Optional[Any] = True A__ : str = False A__ : Tuple = False A__ : Dict = False def _a ( self : Union[str, Any] ): """simple docstring""" A__ = SegformerModelTester(self ) A__ = SegformerConfigTester(self , config_class=_snake_case ) def _a ( self : Optional[int] ): """simple docstring""" self.config_tester.run_common_tests() def _a ( self : Optional[Any] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def _a ( self : List[Any] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_binary_image_segmentation(*_snake_case ) def _a ( self : Tuple ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_segmentation(*_snake_case ) @unittest.skip('SegFormer does not use inputs_embeds' ) def _a ( self : List[Any] ): """simple docstring""" pass @unittest.skip('SegFormer does not have get_input_embeddings method and get_output_embeddings methods' ) def _a ( self : Dict ): """simple docstring""" pass def _a ( self : Dict ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(_snake_case ) A__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ = [*signature.parameters.keys()] A__ = ['pixel_values'] self.assertListEqual(arg_names[:1] , _snake_case ) def _a ( self : Dict ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = True for model_class in self.all_model_classes: A__ = True A__ = False A__ = True A__ = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) ) A__ = outputs.attentions A__ = sum(self.model_tester.depths ) self.assertEqual(len(_snake_case ) , _snake_case ) # check that output_attentions also work using config del inputs_dict["output_attentions"] A__ = True A__ = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) ) A__ = outputs.attentions self.assertEqual(len(_snake_case ) , _snake_case ) # verify the first attentions (first block, first layer) A__ = (self.model_tester.image_size // 4) ** 2 A__ = (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2 self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len] , ) # verify the last attentions (last block, last layer) A__ = (self.model_tester.image_size // 32) ** 2 A__ = (self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2 self.assertListEqual( list(attentions[-1].shape[-3:] ) , [self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len] , ) A__ = len(_snake_case ) # Check attention is always last and order is fine A__ = True A__ = True A__ = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) ) self.assertEqual(out_len + 1 , len(_snake_case ) ) A__ = outputs.attentions self.assertEqual(len(_snake_case ) , _snake_case ) # verify the first attentions (first block, first layer) A__ = (self.model_tester.image_size // 4) ** 2 A__ = (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2 self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len] , ) def _a ( self : Union[str, Any] ): """simple docstring""" def check_hidden_states_output(_snake_case : Dict , _snake_case : int , _snake_case : List[Any] ): A__ = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) ) A__ = outputs.hidden_states A__ = self.model_tester.num_encoder_blocks self.assertEqual(len(_snake_case ) , _snake_case ) # verify the first hidden states (first block) self.assertListEqual( list(hidden_states[0].shape[-3:] ) , [ self.model_tester.hidden_sizes[0], self.model_tester.image_size // 4, self.model_tester.image_size // 4, ] , ) A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = True check_hidden_states_output(_snake_case , _snake_case , _snake_case ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A__ = True check_hidden_states_output(_snake_case , _snake_case , _snake_case ) def _a ( self : Tuple ): """simple docstring""" if not self.model_tester.is_training: return A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = True for model_class in self.all_model_classes: if model_class in get_values(_snake_case ): continue A__ = model_class(_snake_case ) model.to(_snake_case ) model.train() A__ = self._prepare_for_class(_snake_case , _snake_case , return_labels=_snake_case ) A__ = model(**_snake_case ).loss loss.backward() @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.' ) def _a ( self : Optional[Any] ): """simple docstring""" pass @slow def _a ( self : Tuple ): """simple docstring""" for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = SegformerModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def A ( ) -> str: A__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @slow def _a ( self : Dict ): """simple docstring""" A__ = SegformerImageProcessor( image_scale=(5_12, 5_12) , keep_ratio=_snake_case , align=_snake_case , do_random_crop=_snake_case ) A__ = SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512' ).to( _snake_case ) A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ) A__ = encoded_inputs.pixel_values.to(_snake_case ) with torch.no_grad(): A__ = model(_snake_case ) A__ = torch.Size((1, model.config.num_labels, 1_28, 1_28) ) self.assertEqual(outputs.logits.shape , _snake_case ) A__ = torch.tensor( [ [[-4.6310, -5.5232, -6.2356], [-5.1921, -6.1444, -6.5996], [-5.4424, -6.2790, -6.7574]], [[-12.1391, -13.3122, -13.9554], [-12.8732, -13.9352, -14.3563], [-12.9438, -13.8226, -14.2513]], [[-12.5134, -13.4686, -14.4915], [-12.8669, -14.4343, -14.7758], [-13.2523, -14.5819, -15.0694]], ] ).to(_snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3] , _snake_case , atol=1E-4 ) ) @slow def _a ( self : Optional[Any] ): """simple docstring""" A__ = SegformerImageProcessor( image_scale=(5_12, 5_12) , keep_ratio=_snake_case , align=_snake_case , do_random_crop=_snake_case ) A__ = SegformerForSemanticSegmentation.from_pretrained( 'nvidia/segformer-b1-finetuned-cityscapes-1024-1024' ).to(_snake_case ) A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ) A__ = encoded_inputs.pixel_values.to(_snake_case ) with torch.no_grad(): A__ = model(_snake_case ) A__ = torch.Size((1, model.config.num_labels, 1_28, 1_28) ) self.assertEqual(outputs.logits.shape , _snake_case ) A__ = torch.tensor( [ [[-13.5748, -13.9111, -12.6500], [-14.3500, -15.3683, -14.2328], [-14.7532, -16.0424, -15.6087]], [[-17.1651, -15.8725, -12.9653], [-17.2580, -17.3718, -14.8223], [-16.6058, -16.8783, -16.7452]], [[-3.6456, -3.0209, -1.4203], [-3.0797, -3.1959, -2.0000], [-1.8757, -1.9217, -1.6997]], ] ).to(_snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3] , _snake_case , atol=1E-1 ) ) @slow def _a ( self : Any ): """simple docstring""" A__ = SegformerImageProcessor( image_scale=(5_12, 5_12) , keep_ratio=_snake_case , align=_snake_case , do_random_crop=_snake_case ) A__ = SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512' ).to( _snake_case ) A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ) A__ = encoded_inputs.pixel_values.to(_snake_case ) with torch.no_grad(): A__ = model(_snake_case ) A__ = outputs.logits.detach().cpu() A__ = image_processor.post_process_semantic_segmentation(outputs=_snake_case , target_sizes=[(5_00, 3_00)] ) A__ = torch.Size((5_00, 3_00) ) self.assertEqual(segmentation[0].shape , _snake_case ) A__ = image_processor.post_process_semantic_segmentation(outputs=_snake_case ) A__ = torch.Size((1_28, 1_28) ) self.assertEqual(segmentation[0].shape , _snake_case )
9
def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: if exponent == 1: return base if exponent % 2 == 0: A__ = _modexpt(__UpperCamelCase , exponent // 2 , __UpperCamelCase ) % modulo_value return (x * x) % modulo_value else: return (base * _modexpt(__UpperCamelCase , exponent - 1 , __UpperCamelCase )) % modulo_value def A ( __UpperCamelCase = 1_777 , __UpperCamelCase = 1_855 , __UpperCamelCase = 8 ) -> int: A__ = base for _ in range(1 , __UpperCamelCase ): A__ = _modexpt(__UpperCamelCase , __UpperCamelCase , 10**digits ) return result if __name__ == "__main__": print(f'{solution() = }')
9
1
import logging import os import sys from dataclasses import dataclass, field from typing import Optional from seqaseq_trainer import SeqaSeqTrainer from seqaseq_training_args import SeqaSeqTrainingArguments import transformers from transformers import ( AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer, HfArgumentParser, MBartTokenizer, MBartTokenizerFast, set_seed, ) from transformers.trainer_utils import EvaluationStrategy, is_main_process from transformers.training_args import ParallelMode from utils import ( SeqaSeqDataCollator, SeqaSeqDataset, assert_all_frozen, build_compute_metrics_fn, check_output_dir, freeze_embeds, freeze_params, lmap, save_json, use_task_specific_params, write_txt_file, ) SCREAMING_SNAKE_CASE__ = logging.getLogger(__name__) @dataclass class __lowerCAmelCase : """simple docstring""" A__ : str = field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "Pretrained config name or path if not the same as model_name"} ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"} ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) A__ : bool = field(default=UpperCAmelCase_ , metadata={"help": "Whether tp freeze the encoder."} ) A__ : bool = field(default=UpperCAmelCase_ , metadata={"help": "Whether to freeze the embeddings."} ) @dataclass class __lowerCAmelCase : """simple docstring""" A__ : str = field( metadata={"help": "The input data dir. Should contain the .tsv files (or other data files) for the task."} ) A__ : Optional[str] = field( default="summarization" , metadata={"help": "Task name, summarization (or summarization_{dataset} for pegasus) or translation"} , ) A__ : Optional[int] = field( default=10_24 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) A__ : Optional[int] = field( default=1_28 , metadata={ "help": ( "The maximum total sequence length for target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) A__ : Optional[int] = field( default=1_42 , metadata={ "help": ( "The maximum total sequence length for validation target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded. " "This argument is also used to override the ``max_length`` param of ``model.generate``, which is used " "during ``evaluate`` and ``predict``." ) } , ) A__ : Optional[int] = field( default=1_42 , metadata={ "help": ( "The maximum total sequence length for test target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) A__ : Optional[int] = field(default=-1 , metadata={"help": "# training examples. -1 means use all."} ) A__ : Optional[int] = field(default=-1 , metadata={"help": "# validation examples. -1 means use all."} ) A__ : Optional[int] = field(default=-1 , metadata={"help": "# test examples. -1 means use all."} ) A__ : Optional[str] = field(default=UpperCAmelCase_ , metadata={"help": "Source language id for translation."} ) A__ : Optional[str] = field(default=UpperCAmelCase_ , metadata={"help": "Target language id for translation."} ) A__ : Optional[int] = field(default=UpperCAmelCase_ , metadata={"help": "# num_beams to use for evaluation."} ) A__ : bool = field( default=UpperCAmelCase_ , metadata={"help": "If only pad tokens should be ignored. This assumes that `config.pad_token_id` is defined."} , ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: logger.info(f'''***** {split} metrics *****''' ) for key in sorted(metrics.keys() ): logger.info(f''' {key} = {metrics[key]}''' ) save_json(__UpperCamelCase , os.path.join(__UpperCamelCase , f'''{split}_results.json''' ) ) def A ( ) -> List[str]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. A__ = HfArgumentParser((ModelArguments, DataTrainingArguments, SeqaSeqTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('.json' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. A__ , A__ , A__ = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: A__ , A__ , A__ = parser.parse_args_into_dataclasses() check_output_dir(__UpperCamelCase ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( 'Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s' , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.parallel_mode == ParallelMode.DISTRIBUTED ) , training_args.fpaa , ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() logger.info('Training/evaluation parameters %s' , __UpperCamelCase ) # Set seed set_seed(training_args.seed ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. A__ = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) A__ = ('encoder_layerdrop', 'decoder_layerdrop', 'dropout', 'attention_dropout') for p in extra_model_params: if getattr(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): assert hasattr(__UpperCamelCase , __UpperCamelCase ), f'''({config.__class__.__name__}) doesn\'t have a `{p}` attribute''' setattr(__UpperCamelCase , __UpperCamelCase , getattr(__UpperCamelCase , __UpperCamelCase ) ) A__ = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) A__ = AutoModelForSeqaSeqLM.from_pretrained( model_args.model_name_or_path , from_tf='.ckpt' in model_args.model_name_or_path , config=__UpperCamelCase , cache_dir=model_args.cache_dir , ) # use task specific params use_task_specific_params(__UpperCamelCase , data_args.task ) # set num_beams for evaluation if data_args.eval_beams is None: A__ = model.config.num_beams # set decoder_start_token_id for MBart if model.config.decoder_start_token_id is None and isinstance(__UpperCamelCase , (MBartTokenizer, MBartTokenizerFast) ): assert ( data_args.tgt_lang is not None and data_args.src_lang is not None ), "mBart requires --tgt_lang and --src_lang" if isinstance(__UpperCamelCase , __UpperCamelCase ): A__ = tokenizer.lang_code_to_id[data_args.tgt_lang] else: A__ = tokenizer.convert_tokens_to_ids(data_args.tgt_lang ) if model_args.freeze_embeds: freeze_embeds(__UpperCamelCase ) if model_args.freeze_encoder: freeze_params(model.get_encoder() ) assert_all_frozen(model.get_encoder() ) A__ = SeqaSeqDataset # Get datasets A__ = ( dataset_class( __UpperCamelCase , type_path='train' , data_dir=data_args.data_dir , n_obs=data_args.n_train , max_target_length=data_args.max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or '' , ) if training_args.do_train else None ) A__ = ( dataset_class( __UpperCamelCase , type_path='val' , data_dir=data_args.data_dir , n_obs=data_args.n_val , max_target_length=data_args.val_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or '' , ) if training_args.do_eval or training_args.evaluation_strategy != EvaluationStrategy.NO else None ) A__ = ( dataset_class( __UpperCamelCase , type_path='test' , data_dir=data_args.data_dir , n_obs=data_args.n_test , max_target_length=data_args.test_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or '' , ) if training_args.do_predict else None ) # Initialize our Trainer A__ = ( build_compute_metrics_fn(data_args.task , __UpperCamelCase ) if training_args.predict_with_generate else None ) A__ = SeqaSeqTrainer( model=__UpperCamelCase , args=__UpperCamelCase , data_args=__UpperCamelCase , train_dataset=__UpperCamelCase , eval_dataset=__UpperCamelCase , data_collator=SeqaSeqDataCollator( __UpperCamelCase , __UpperCamelCase , model.config.decoder_start_token_id , training_args.tpu_num_cores ) , compute_metrics=__UpperCamelCase , tokenizer=__UpperCamelCase , ) A__ = {} # Training if training_args.do_train: logger.info('*** Train ***' ) A__ = trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) A__ = train_result.metrics A__ = data_args.n_train trainer.save_model() # this also saves the tokenizer if trainer.is_world_process_zero(): handle_metrics('train' , __UpperCamelCase , training_args.output_dir ) all_metrics.update(__UpperCamelCase ) # Need to save the state, since Trainer.save_model saves only the tokenizer with the model trainer.state.save_to_json(os.path.join(training_args.output_dir , 'trainer_state.json' ) ) # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) tokenizer.save_pretrained(training_args.output_dir ) # Evaluation if training_args.do_eval: logger.info('*** Evaluate ***' ) A__ = trainer.evaluate(metric_key_prefix='val' ) A__ = data_args.n_val A__ = round(metrics['val_loss'] , 4 ) if trainer.is_world_process_zero(): handle_metrics('val' , __UpperCamelCase , training_args.output_dir ) all_metrics.update(__UpperCamelCase ) if training_args.do_predict: logger.info('*** Predict ***' ) A__ = trainer.predict(test_dataset=__UpperCamelCase , metric_key_prefix='test' ) A__ = test_output.metrics A__ = data_args.n_test if trainer.is_world_process_zero(): A__ = round(metrics['test_loss'] , 4 ) handle_metrics('test' , __UpperCamelCase , training_args.output_dir ) all_metrics.update(__UpperCamelCase ) if training_args.predict_with_generate: A__ = tokenizer.batch_decode( test_output.predictions , skip_special_tokens=__UpperCamelCase , clean_up_tokenization_spaces=__UpperCamelCase ) A__ = lmap(str.strip , __UpperCamelCase ) write_txt_file(__UpperCamelCase , os.path.join(training_args.output_dir , 'test_generations.txt' ) ) if trainer.is_world_process_zero(): save_json(__UpperCamelCase , os.path.join(training_args.output_dir , 'all_results.json' ) ) return all_metrics def A ( __UpperCamelCase ) -> Any: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
9
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() SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) def A ( __UpperCamelCase , __UpperCamelCase=False , __UpperCamelCase=False ) -> Dict: A__ = 'backbone.' if is_semantic else '' A__ = [] 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 A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=False , __UpperCamelCase=False ) -> Optional[Any]: for i in range(config.num_hidden_layers ): A__ = 'backbone.' if is_semantic else '' # queries, keys and values A__ = state_dict.pop(f'''{prefix}blocks.{i}.attn.qkv.weight''' ) A__ = state_dict.pop(f'''{prefix}blocks.{i}.attn.q_bias''' ) A__ = state_dict.pop(f'''{prefix}blocks.{i}.attn.v_bias''' ) A__ = in_proj_weight[ : config.hidden_size, : ] A__ = q_bias A__ = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] A__ = in_proj_weight[ -config.hidden_size :, : ] A__ = v_bias # gamma_1 and gamma_2 # we call them lambda because otherwise they are renamed when using .from_pretrained A__ = state_dict.pop(f'''{prefix}blocks.{i}.gamma_1''' ) A__ = state_dict.pop(f'''{prefix}blocks.{i}.gamma_2''' ) A__ = gamma_a A__ = gamma_a def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Union[str, Any]: A__ = dct.pop(__UpperCamelCase ) A__ = val def A ( ) -> Dict: A__ = 'http://images.cocodataset.org/val2017/000000039769.jpg' A__ = Image.open(requests.get(__UpperCamelCase , stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=False ) -> str: A__ = False if 'rvlcdip' in checkpoint_url else True A__ = BeitConfig(use_absolute_position_embeddings=__UpperCamelCase , use_mask_token=__UpperCamelCase ) # size of the architecture if "large" in checkpoint_url or "dit-l" in checkpoint_url: A__ = 1_024 A__ = 4_096 A__ = 24 A__ = 16 # labels if "rvlcdip" in checkpoint_url: A__ = 16 A__ = 'huggingface/label-files' A__ = 'rvlcdip-id2label.json' A__ = json.load(open(hf_hub_download(__UpperCamelCase , __UpperCamelCase , repo_type='dataset' ) , 'r' ) ) A__ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} A__ = idalabel A__ = {v: k for k, v in idalabel.items()} # load state_dict of original model, remove and rename some keys A__ = torch.hub.load_state_dict_from_url(__UpperCamelCase , map_location='cpu' )['model'] A__ = create_rename_keys(__UpperCamelCase , has_lm_head=__UpperCamelCase ) for src, dest in rename_keys: rename_key(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) read_in_q_k_v(__UpperCamelCase , __UpperCamelCase , has_lm_head=__UpperCamelCase ) # load HuggingFace model A__ = BeitForMaskedImageModeling(__UpperCamelCase ) if has_lm_head else BeitForImageClassification(__UpperCamelCase ) model.eval() model.load_state_dict(__UpperCamelCase ) # Check outputs on an image A__ = BeitImageProcessor( size=config.image_size , resample=PILImageResampling.BILINEAR , do_center_crop=__UpperCamelCase ) A__ = prepare_img() A__ = image_processor(images=__UpperCamelCase , return_tensors='pt' ) A__ = encoding['pixel_values'] A__ = model(__UpperCamelCase ) A__ = outputs.logits # verify logits A__ = [1, 16] if 'rvlcdip' in checkpoint_url else [1, 196, 8_192] assert logits.shape == torch.Size(__UpperCamelCase ), "Shape of logits not as expected" Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) print(f'''Saving model to {pytorch_dump_folder_path}''' ) model.save_pretrained(__UpperCamelCase ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(__UpperCamelCase ) if push_to_hub: if has_lm_head: A__ = 'dit-base' if 'base' in checkpoint_url else 'dit-large' else: A__ = '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(__UpperCamelCase , __UpperCamelCase ) , organization='nielsr' , commit_message='Add image processor' , use_temp_dir=__UpperCamelCase , ) model.push_to_hub( repo_path_or_name=Path(__UpperCamelCase , __UpperCamelCase ) , organization='nielsr' , commit_message='Add model' , use_temp_dir=__UpperCamelCase , ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = 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''', ) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_dit_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub)
9
1
from __future__ import annotations def A ( __UpperCamelCase ) -> bool: A__ = len(__UpperCamelCase ) # We need to create solution object to save path. A__ = [[0 for _ in range(__UpperCamelCase )] for _ in range(__UpperCamelCase )] A__ = run_maze(__UpperCamelCase , 0 , 0 , __UpperCamelCase ) if solved: print('\n'.join(str(__UpperCamelCase ) for row in solutions ) ) else: print('No solution exists!' ) return solved def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> bool: A__ = len(__UpperCamelCase ) # Final check point. if i == j == (size - 1): A__ = 1 return True A__ = (not i < 0) and (not j < 0) # Check lower bounds A__ = (i < size) and (j < size) # Check upper bounds if lower_flag and upper_flag: # check for already visited and block points. A__ = (not solutions[i][j]) and (not maze[i][j]) if block_flag: # check visited A__ = 1 # check for directions if ( run_maze(__UpperCamelCase , i + 1 , __UpperCamelCase , __UpperCamelCase ) or run_maze(__UpperCamelCase , __UpperCamelCase , j + 1 , __UpperCamelCase ) or run_maze(__UpperCamelCase , i - 1 , __UpperCamelCase , __UpperCamelCase ) or run_maze(__UpperCamelCase , __UpperCamelCase , j - 1 , __UpperCamelCase ) ): return True A__ = 0 return False return False if __name__ == "__main__": import doctest doctest.testmod()
9
SCREAMING_SNAKE_CASE__ = { '''A''': ['''B''', '''C''', '''E'''], '''B''': ['''A''', '''D''', '''E'''], '''C''': ['''A''', '''F''', '''G'''], '''D''': ['''B'''], '''E''': ['''A''', '''B''', '''D'''], '''F''': ['''C'''], '''G''': ['''C'''], } def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> list[str]: A__ = set() # keep track of all the paths to be checked A__ = [[start]] # return path if start is goal if start == goal: return [start] # keeps looping until all possible paths have been checked while queue: # pop the first path from the queue A__ = queue.pop(0 ) # get the last node from the path A__ = path[-1] if node not in explored: A__ = graph[node] # go through all neighbour nodes, construct a new path and # push it into the queue for neighbour in neighbours: A__ = list(__UpperCamelCase ) new_path.append(__UpperCamelCase ) queue.append(__UpperCamelCase ) # return path if neighbour is goal if neighbour == goal: return new_path # mark node as explored explored.add(__UpperCamelCase ) # in case there's no path between the 2 nodes return [] def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: if not graph or start not in graph or target not in graph: return -1 if start == target: return 0 A__ = [start] A__ = set(__UpperCamelCase ) # Keep tab on distances from `start` node. A__ = {start: 0, target: -1} while queue: A__ = queue.pop(0 ) if node == target: A__ = ( dist[node] if dist[target] == -1 else min(dist[target] , dist[node] ) ) for adjacent in graph[node]: if adjacent not in visited: visited.add(__UpperCamelCase ) queue.append(__UpperCamelCase ) A__ = dist[node] + 1 return dist[target] if __name__ == "__main__": print(bfs_shortest_path(demo_graph, '''G''', '''D''')) # returns ['G', 'C', 'A', 'B', 'D'] print(bfs_shortest_path_distance(demo_graph, '''G''', '''D''')) # returns 4
9
1
def A ( __UpperCamelCase ) -> list[int]: if length <= 0 or not isinstance(__UpperCamelCase , __UpperCamelCase ): raise ValueError('Length must be a positive integer.' ) return [n * (2 * n - 1) for n in range(__UpperCamelCase )] if __name__ == "__main__": print(hexagonal_numbers(length=5)) print(hexagonal_numbers(length=1_0))
9
def A ( __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: A__ = 0 A__ = len(__UpperCamelCase ) - 1 while left <= right: # avoid divided by 0 during interpolation if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: return left else: return None A__ = left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left] ) # out of range check if point < 0 or point >= len(__UpperCamelCase ): return None A__ = sorted_collection[point] if current_item == item: return point else: if point < left: A__ = left A__ = point elif point > right: A__ = right A__ = point else: if item < current_item: A__ = point - 1 else: A__ = point + 1 return None def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: # avoid divided by 0 during interpolation if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: return left else: return None A__ = left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left] ) # out of range check if point < 0 or point >= len(__UpperCamelCase ): return None if sorted_collection[point] == item: return point elif point < left: return interpolation_search_by_recursion(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) elif point > right: return interpolation_search_by_recursion(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) else: if sorted_collection[point] > item: return interpolation_search_by_recursion( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , point - 1 ) else: return interpolation_search_by_recursion( __UpperCamelCase , __UpperCamelCase , point + 1 , __UpperCamelCase ) def A ( __UpperCamelCase ) -> List[str]: if collection != sorted(__UpperCamelCase ): raise ValueError('Collection must be ascending sorted' ) return True if __name__ == "__main__": import sys SCREAMING_SNAKE_CASE__ = 0 if debug == 1: SCREAMING_SNAKE_CASE__ = [1_0, 3_0, 4_0, 4_5, 5_0, 6_6, 7_7, 9_3] try: __assert_sorted(collection) except ValueError: sys.exit('''Sequence must be ascending sorted to apply interpolation search''') SCREAMING_SNAKE_CASE__ = 6_7 SCREAMING_SNAKE_CASE__ = interpolation_search(collection, target) if result is not None: print(f'{target} found at positions: {result}') else: print('''Not found''')
9
1
from __future__ import annotations import os from typing import Any import requests SCREAMING_SNAKE_CASE__ = '''https://api.github.com''' # https://docs.github.com/en/free-pro-team@latest/rest/reference/users#get-the-authenticated-user SCREAMING_SNAKE_CASE__ = BASE_URL + '''/user''' # https://github.com/settings/tokens SCREAMING_SNAKE_CASE__ = os.environ.get('''USER_TOKEN''', '''''') def A ( __UpperCamelCase ) -> dict[Any, Any]: A__ = { 'Authorization': f'''token {auth_token}''', 'Accept': 'application/vnd.github.v3+json', } return requests.get(__UpperCamelCase , headers=__UpperCamelCase ).json() if __name__ == "__main__": # pragma: no cover if USER_TOKEN: for key, value in fetch_github_info(USER_TOKEN).items(): print(f'{key}: {value}') else: raise ValueError('''\'USER_TOKEN\' field cannot be empty.''')
9
import warnings from ...utils import logging from .image_processing_clip import CLIPImageProcessor SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Dict , *_snake_case : int , **_snake_case : Optional[int] ): """simple docstring""" warnings.warn( 'The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please' ' use CLIPImageProcessor instead.' , _snake_case , ) super().__init__(*_snake_case , **_snake_case )
9
1
from __future__ import annotations def A ( __UpperCamelCase , __UpperCamelCase ) -> list[int]: A__ = 0 A__ = len(__UpperCamelCase ) - 1 while i < j: if nums[i] + nums[j] == target: return [i, j] elif nums[i] + nums[j] < target: A__ = i + 1 else: A__ = j - 1 return [] if __name__ == "__main__": import doctest doctest.testmod() print(f'{two_pointer([2, 7, 1_1, 1_5], 9) = }')
9
import numpy as np import skfuzzy as fuzz if __name__ == "__main__": # Create universe of discourse in Python using linspace () SCREAMING_SNAKE_CASE__ = np.linspace(start=0, stop=7_5, num=7_5, endpoint=True, retstep=False) # Create two fuzzy sets by defining any membership function # (trapmf(), gbellmf(), gaussmf(), etc). SCREAMING_SNAKE_CASE__ = [0, 2_5, 5_0] SCREAMING_SNAKE_CASE__ = [2_5, 5_0, 7_5] SCREAMING_SNAKE_CASE__ = fuzz.membership.trimf(X, abca) SCREAMING_SNAKE_CASE__ = fuzz.membership.trimf(X, abca) # Compute the different operations using inbuilt functions. SCREAMING_SNAKE_CASE__ = np.ones(7_5) SCREAMING_SNAKE_CASE__ = np.zeros((7_5,)) # 1. Union = max(µA(x), µB(x)) SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_or(X, young, X, middle_aged)[1] # 2. Intersection = min(µA(x), µB(x)) SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_and(X, young, X, middle_aged)[1] # 3. Complement (A) = (1- min(µA(x)) SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_not(young) # 4. Difference (A/B) = min(µA(x),(1- µB(x))) SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_and(X, young, X, fuzz.fuzzy_not(middle_aged)[1])[1] # 5. Algebraic Sum = [µA(x) + µB(x) – (µA(x) * µB(x))] SCREAMING_SNAKE_CASE__ = young + middle_aged - (young * middle_aged) # 6. Algebraic Product = (µA(x) * µB(x)) SCREAMING_SNAKE_CASE__ = young * middle_aged # 7. Bounded Sum = min[1,(µA(x), µB(x))] SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_and(X, one, X, young + middle_aged)[1] # 8. Bounded difference = min[0,(µA(x), µB(x))] SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_or(X, zero, X, young - middle_aged)[1] # max-min composition # max-product composition # Plot each set A, set B and each operation result using plot() and subplot(). from matplotlib import pyplot as plt plt.figure() plt.subplot(4, 3, 1) plt.plot(X, young) plt.title('''Young''') plt.grid(True) plt.subplot(4, 3, 2) plt.plot(X, middle_aged) plt.title('''Middle aged''') plt.grid(True) plt.subplot(4, 3, 3) plt.plot(X, union) plt.title('''union''') plt.grid(True) plt.subplot(4, 3, 4) plt.plot(X, intersection) plt.title('''intersection''') plt.grid(True) plt.subplot(4, 3, 5) plt.plot(X, complement_a) plt.title('''complement_a''') plt.grid(True) plt.subplot(4, 3, 6) plt.plot(X, difference) plt.title('''difference a/b''') plt.grid(True) plt.subplot(4, 3, 7) plt.plot(X, alg_sum) plt.title('''alg_sum''') plt.grid(True) plt.subplot(4, 3, 8) plt.plot(X, alg_product) plt.title('''alg_product''') plt.grid(True) plt.subplot(4, 3, 9) plt.plot(X, bdd_sum) plt.title('''bdd_sum''') plt.grid(True) plt.subplot(4, 3, 1_0) plt.plot(X, bdd_difference) plt.title('''bdd_difference''') plt.grid(True) plt.subplots_adjust(hspace=0.5) plt.show()
9
1
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import cached_download, hf_hub_url from PIL import Image from transformers import DPTConfig, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTImageProcessor from transformers.utils import logging logging.set_verbosity_info() SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) def A ( __UpperCamelCase ) -> List[Any]: A__ = DPTConfig() if "large" in checkpoint_url: A__ = 1_024 A__ = 4_096 A__ = 24 A__ = 16 A__ = [5, 11, 17, 23] A__ = [256, 512, 1_024, 1_024] A__ = (1, 384, 384) if "ade" in checkpoint_url: A__ = True A__ = 150 A__ = 'huggingface/label-files' A__ = 'ade20k-id2label.json' A__ = json.load(open(cached_download(hf_hub_url(__UpperCamelCase , __UpperCamelCase , repo_type='dataset' ) ) , 'r' ) ) A__ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} A__ = idalabel A__ = {v: k for k, v in idalabel.items()} A__ = [1, 150, 480, 480] return config, expected_shape def A ( __UpperCamelCase ) -> List[str]: A__ = ['pretrained.model.head.weight', 'pretrained.model.head.bias'] for k in ignore_keys: state_dict.pop(__UpperCamelCase , __UpperCamelCase ) def A ( __UpperCamelCase ) -> Union[str, Any]: if ( "pretrained.model" in name and "cls_token" not in name and "pos_embed" not in name and "patch_embed" not in name ): A__ = name.replace('pretrained.model' , 'dpt.encoder' ) if "pretrained.model" in name: A__ = name.replace('pretrained.model' , 'dpt.embeddings' ) if "patch_embed" in name: A__ = name.replace('patch_embed' , 'patch_embeddings' ) if "pos_embed" in name: A__ = name.replace('pos_embed' , 'position_embeddings' ) if "attn.proj" in name: A__ = name.replace('attn.proj' , 'attention.output.dense' ) if "proj" in name and "project" not in name: A__ = name.replace('proj' , 'projection' ) if "blocks" in name: A__ = name.replace('blocks' , 'layer' ) if "mlp.fc1" in name: A__ = name.replace('mlp.fc1' , 'intermediate.dense' ) if "mlp.fc2" in name: A__ = name.replace('mlp.fc2' , 'output.dense' ) if "norm1" in name: A__ = name.replace('norm1' , 'layernorm_before' ) if "norm2" in name: A__ = name.replace('norm2' , 'layernorm_after' ) if "scratch.output_conv" in name: A__ = name.replace('scratch.output_conv' , 'head' ) if "scratch" in name: A__ = name.replace('scratch' , 'neck' ) if "layer1_rn" in name: A__ = name.replace('layer1_rn' , 'convs.0' ) if "layer2_rn" in name: A__ = name.replace('layer2_rn' , 'convs.1' ) if "layer3_rn" in name: A__ = name.replace('layer3_rn' , 'convs.2' ) if "layer4_rn" in name: A__ = name.replace('layer4_rn' , 'convs.3' ) if "refinenet" in name: A__ = int(name[len('neck.refinenet' ) : len('neck.refinenet' ) + 1] ) # tricky here: we need to map 4 to 0, 3 to 1, 2 to 2 and 1 to 3 A__ = name.replace(f'''refinenet{layer_idx}''' , f'''fusion_stage.layers.{abs(layer_idx-4 )}''' ) if "out_conv" in name: A__ = name.replace('out_conv' , 'projection' ) if "resConfUnit1" in name: A__ = name.replace('resConfUnit1' , 'residual_layer1' ) if "resConfUnit2" in name: A__ = name.replace('resConfUnit2' , 'residual_layer2' ) if "conv1" in name: A__ = name.replace('conv1' , 'convolution1' ) if "conv2" in name: A__ = name.replace('conv2' , 'convolution2' ) # readout blocks if "pretrained.act_postprocess1.0.project.0" in name: A__ = name.replace('pretrained.act_postprocess1.0.project.0' , 'neck.reassemble_stage.readout_projects.0.0' ) if "pretrained.act_postprocess2.0.project.0" in name: A__ = name.replace('pretrained.act_postprocess2.0.project.0' , 'neck.reassemble_stage.readout_projects.1.0' ) if "pretrained.act_postprocess3.0.project.0" in name: A__ = name.replace('pretrained.act_postprocess3.0.project.0' , 'neck.reassemble_stage.readout_projects.2.0' ) if "pretrained.act_postprocess4.0.project.0" in name: A__ = name.replace('pretrained.act_postprocess4.0.project.0' , 'neck.reassemble_stage.readout_projects.3.0' ) # resize blocks if "pretrained.act_postprocess1.3" in name: A__ = name.replace('pretrained.act_postprocess1.3' , 'neck.reassemble_stage.layers.0.projection' ) if "pretrained.act_postprocess1.4" in name: A__ = name.replace('pretrained.act_postprocess1.4' , 'neck.reassemble_stage.layers.0.resize' ) if "pretrained.act_postprocess2.3" in name: A__ = name.replace('pretrained.act_postprocess2.3' , 'neck.reassemble_stage.layers.1.projection' ) if "pretrained.act_postprocess2.4" in name: A__ = name.replace('pretrained.act_postprocess2.4' , 'neck.reassemble_stage.layers.1.resize' ) if "pretrained.act_postprocess3.3" in name: A__ = name.replace('pretrained.act_postprocess3.3' , 'neck.reassemble_stage.layers.2.projection' ) if "pretrained.act_postprocess4.3" in name: A__ = name.replace('pretrained.act_postprocess4.3' , 'neck.reassemble_stage.layers.3.projection' ) if "pretrained.act_postprocess4.4" in name: A__ = name.replace('pretrained.act_postprocess4.4' , 'neck.reassemble_stage.layers.3.resize' ) if "pretrained" in name: A__ = name.replace('pretrained' , 'dpt' ) if "bn" in name: A__ = name.replace('bn' , 'batch_norm' ) if "head" in name: A__ = name.replace('head' , 'head.head' ) if "encoder.norm" in name: A__ = name.replace('encoder.norm' , 'layernorm' ) if "auxlayer" in name: A__ = name.replace('auxlayer' , 'auxiliary_head.head' ) return name def A ( __UpperCamelCase , __UpperCamelCase ) -> Any: for i in range(config.num_hidden_layers ): # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) A__ = state_dict.pop(f'''dpt.encoder.layer.{i}.attn.qkv.weight''' ) A__ = state_dict.pop(f'''dpt.encoder.layer.{i}.attn.qkv.bias''' ) # next, add query, keys and values (in that order) to the state dict A__ = in_proj_weight[: config.hidden_size, :] A__ = in_proj_bias[: config.hidden_size] A__ = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] A__ = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] A__ = in_proj_weight[ -config.hidden_size :, : ] A__ = in_proj_bias[-config.hidden_size :] def A ( ) -> str: A__ = 'http://images.cocodataset.org/val2017/000000039769.jpg' A__ = Image.open(requests.get(__UpperCamelCase , stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Any: A__ , A__ = get_dpt_config(__UpperCamelCase ) # load original state_dict from URL A__ = torch.hub.load_state_dict_from_url(__UpperCamelCase , map_location='cpu' ) # remove certain keys remove_ignore_keys_(__UpperCamelCase ) # rename keys for key in state_dict.copy().keys(): A__ = state_dict.pop(__UpperCamelCase ) A__ = val # read in qkv matrices read_in_q_k_v(__UpperCamelCase , __UpperCamelCase ) # load HuggingFace model A__ = DPTForSemanticSegmentation(__UpperCamelCase ) if 'ade' in checkpoint_url else DPTForDepthEstimation(__UpperCamelCase ) model.load_state_dict(__UpperCamelCase ) model.eval() # Check outputs on an image A__ = 480 if 'ade' in checkpoint_url else 384 A__ = DPTImageProcessor(size=__UpperCamelCase ) A__ = prepare_img() A__ = image_processor(__UpperCamelCase , return_tensors='pt' ) # forward pass A__ = model(**__UpperCamelCase ).logits if 'ade' in checkpoint_url else model(**__UpperCamelCase ).predicted_depth # Assert logits A__ = torch.tensor([[6.3199, 6.3629, 6.4148], [6.3850, 6.3615, 6.4166], [6.3519, 6.3176, 6.3575]] ) if "ade" in checkpoint_url: A__ = torch.tensor([[4.0480, 4.2420, 4.4360], [4.3124, 4.5693, 4.8261], [4.5768, 4.8965, 5.2163]] ) assert outputs.shape == torch.Size(__UpperCamelCase ) assert ( torch.allclose(outputs[0, 0, :3, :3] , __UpperCamelCase , atol=1E-4 ) if "ade" in checkpoint_url else torch.allclose(outputs[0, :3, :3] , __UpperCamelCase ) ) Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) print(f'''Saving model to {pytorch_dump_folder_path}''' ) model.save_pretrained(__UpperCamelCase ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(__UpperCamelCase ) if push_to_hub: print('Pushing model to hub...' ) model.push_to_hub( repo_path_or_name=Path(__UpperCamelCase , __UpperCamelCase ) , organization='nielsr' , commit_message='Add model' , use_temp_dir=__UpperCamelCase , ) image_processor.push_to_hub( repo_path_or_name=Path(__UpperCamelCase , __UpperCamelCase ) , organization='nielsr' , commit_message='Add image processor' , use_temp_dir=__UpperCamelCase , ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint_url''', default='''https://github.com/intel-isl/DPT/releases/download/1_0/dpt_large-midas-2f21e586.pt''', type=str, help='''URL of the original DPT checkpoint you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model directory.''', ) parser.add_argument( '''--push_to_hub''', action='''store_true''', ) parser.add_argument( '''--model_name''', default='''dpt-large''', type=str, help='''Name of the model, in case you\'re pushing to the hub.''', ) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_dpt_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub, args.model_name)
9
import unittest from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __lowerCAmelCase : """simple docstring""" @staticmethod def _a ( *_snake_case : int , **_snake_case : List[str] ): """simple docstring""" pass @is_pipeline_test @require_vision @require_torch class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" A__ : List[str] = MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING def _a ( self : Any , _snake_case : Union[str, Any] , _snake_case : Tuple , _snake_case : Optional[Any] ): """simple docstring""" A__ = pipeline( 'zero-shot-object-detection' , model='hf-internal-testing/tiny-random-owlvit-object-detection' ) A__ = [ { 'image': './tests/fixtures/tests_samples/COCO/000000039769.png', 'candidate_labels': ['cat', 'remote', 'couch'], } ] return object_detector, examples def _a ( self : int , _snake_case : int , _snake_case : List[str] ): """simple docstring""" A__ = object_detector(examples[0] , threshold=0.0 ) A__ = len(_snake_case ) self.assertGreater(_snake_case , 0 ) self.assertEqual( _snake_case , [ { 'score': ANY(_snake_case ), 'label': ANY(_snake_case ), 'box': {'xmin': ANY(_snake_case ), 'ymin': ANY(_snake_case ), 'xmax': ANY(_snake_case ), 'ymax': ANY(_snake_case )}, } for i in range(_snake_case ) ] , ) @require_tf @unittest.skip('Zero Shot Object Detection not implemented in TF' ) def _a ( self : List[str] ): """simple docstring""" pass @require_torch def _a ( self : Optional[int] ): """simple docstring""" A__ = pipeline( 'zero-shot-object-detection' , model='hf-internal-testing/tiny-random-owlvit-object-detection' ) A__ = object_detector( './tests/fixtures/tests_samples/COCO/000000039769.png' , candidate_labels=['cat', 'remote', 'couch'] , threshold=0.64 , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}}, {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}}, {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}}, ] , ) A__ = object_detector( [ { 'image': './tests/fixtures/tests_samples/COCO/000000039769.png', 'candidate_labels': ['cat', 'remote', 'couch'], } ] , threshold=0.64 , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ [ {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}}, {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}}, {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}}, ] ] , ) @require_torch @slow def _a ( self : int ): """simple docstring""" A__ = pipeline('zero-shot-object-detection' ) A__ = object_detector( 'http://images.cocodataset.org/val2017/000000039769.jpg' , candidate_labels=['cat', 'remote', 'couch'] , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}}, {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}}, {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}}, ] , ) A__ = object_detector( [ { 'image': 'http://images.cocodataset.org/val2017/000000039769.jpg', 'candidate_labels': ['cat', 'remote', 'couch'], }, { 'image': 'http://images.cocodataset.org/val2017/000000039769.jpg', 'candidate_labels': ['cat', 'remote', 'couch'], }, ] , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}}, {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}}, {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}}, ], [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}}, {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}}, {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}}, ], ] , ) @require_tf @unittest.skip('Zero Shot Object Detection not implemented in TF' ) def _a ( self : int ): """simple docstring""" pass @require_torch @slow def _a ( self : str ): """simple docstring""" A__ = 0.2 A__ = pipeline('zero-shot-object-detection' ) A__ = object_detector( 'http://images.cocodataset.org/val2017/000000039769.jpg' , candidate_labels=['cat', 'remote', 'couch'] , threshold=_snake_case , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}}, ] , ) @require_torch @slow def _a ( self : Any ): """simple docstring""" A__ = 2 A__ = pipeline('zero-shot-object-detection' ) A__ = object_detector( 'http://images.cocodataset.org/val2017/000000039769.jpg' , candidate_labels=['cat', 'remote', 'couch'] , top_k=_snake_case , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, ] , )
9
1
def A ( __UpperCamelCase ) -> int: A__ = abs(__UpperCamelCase ) A__ = 0 while n > 0: res += n % 10 n //= 10 return res def A ( __UpperCamelCase ) -> int: A__ = abs(__UpperCamelCase ) return n if n < 10 else n % 10 + sum_of_digits(n // 10 ) def A ( __UpperCamelCase ) -> int: return sum(int(__UpperCamelCase ) for c in str(abs(__UpperCamelCase ) ) ) def A ( ) -> None: from collections.abc import Callable from timeit import timeit def benchmark_a_function(__UpperCamelCase , __UpperCamelCase ) -> None: A__ = f'''{func.__name__}({value})''' A__ = timeit(f'''__main__.{call}''' , setup='import __main__' ) print(f'''{call:56} = {func(__UpperCamelCase )} -- {timing:.4f} seconds''' ) for value in (262_144, 1_125_899_906_842_624, 1_267_650_600_228_229_401_496_703_205_376): for func in (sum_of_digits, sum_of_digits_recursion, sum_of_digits_compact): benchmark_a_function(__UpperCamelCase , __UpperCamelCase ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
9
import dataclasses import json import sys import types from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, ArgumentTypeError from copy import copy from enum import Enum from inspect import isclass from pathlib import Path from typing import Any, Callable, Dict, Iterable, List, Literal, NewType, Optional, Tuple, Union, get_type_hints import yaml SCREAMING_SNAKE_CASE__ = NewType('''DataClass''', Any) SCREAMING_SNAKE_CASE__ = NewType('''DataClassType''', Any) def A ( __UpperCamelCase ) -> List[Any]: if isinstance(__UpperCamelCase , __UpperCamelCase ): return v if v.lower() in ("yes", "true", "t", "y", "1"): return True elif v.lower() in ("no", "false", "f", "n", "0"): return False else: raise ArgumentTypeError( f'''Truthy value expected: got {v} but expected one of yes/no, true/false, t/f, y/n, 1/0 (case insensitive).''' ) def A ( __UpperCamelCase ) -> Callable[[str], Any]: A__ = {str(__UpperCamelCase ): choice for choice in choices} return lambda __UpperCamelCase : str_to_choice.get(__UpperCamelCase , __UpperCamelCase ) def A ( *, __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = dataclasses.MISSING , __UpperCamelCase = dataclasses.MISSING , __UpperCamelCase = None , **__UpperCamelCase , ) -> dataclasses.Field: if metadata is None: # Important, don't use as default param in function signature because dict is mutable and shared across function calls A__ = {} if aliases is not None: A__ = aliases if help is not None: A__ = help return dataclasses.field(metadata=__UpperCamelCase , default=__UpperCamelCase , default_factory=__UpperCamelCase , **__UpperCamelCase ) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Iterable[DataClassType] def __init__( self : Optional[int] , _snake_case : Union[DataClassType, Iterable[DataClassType]] , **_snake_case : Tuple ): """simple docstring""" if "formatter_class" not in kwargs: A__ = ArgumentDefaultsHelpFormatter super().__init__(**_snake_case ) if dataclasses.is_dataclass(_snake_case ): A__ = [dataclass_types] A__ = list(_snake_case ) for dtype in self.dataclass_types: self._add_dataclass_arguments(_snake_case ) @staticmethod def _a ( _snake_case : ArgumentParser , _snake_case : dataclasses.Field ): """simple docstring""" A__ = F'''--{field.name}''' A__ = field.metadata.copy() # field.metadata is not used at all by Data Classes, # it is provided as a third-party extension mechanism. if isinstance(field.type , _snake_case ): raise RuntimeError( 'Unresolved type detected, which should have been done with the help of ' '`typing.get_type_hints` method by default' ) A__ = kwargs.pop('aliases' , [] ) if isinstance(_snake_case , _snake_case ): A__ = [aliases] A__ = getattr(field.type , '__origin__' , field.type ) if origin_type is Union or (hasattr(_snake_case , 'UnionType' ) and isinstance(_snake_case , types.UnionType )): if str not in field.type.__args__ and ( len(field.type.__args__ ) != 2 or type(_snake_case ) not in field.type.__args__ ): raise ValueError( 'Only `Union[X, NoneType]` (i.e., `Optional[X]`) is allowed for `Union` because' ' the argument parser only supports one type per argument.' F''' Problem encountered in field \'{field.name}\'.''' ) if type(_snake_case ) not in field.type.__args__: # filter `str` in Union A__ = field.type.__args__[0] if field.type.__args__[1] == str else field.type.__args__[1] A__ = getattr(field.type , '__origin__' , field.type ) elif bool not in field.type.__args__: # filter `NoneType` in Union (except for `Union[bool, NoneType]`) A__ = ( field.type.__args__[0] if isinstance(_snake_case , field.type.__args__[1] ) else field.type.__args__[1] ) A__ = getattr(field.type , '__origin__' , field.type ) # A variable to store kwargs for a boolean field, if needed # so that we can init a `no_*` complement argument (see below) A__ = {} if origin_type is Literal or (isinstance(field.type , _snake_case ) and issubclass(field.type , _snake_case )): if origin_type is Literal: A__ = field.type.__args__ else: A__ = [x.value for x in field.type] A__ = make_choice_type_function(kwargs['choices'] ) if field.default is not dataclasses.MISSING: A__ = field.default else: A__ = True elif field.type is bool or field.type == Optional[bool]: # Copy the currect kwargs to use to instantiate a `no_*` complement argument below. # We do not initialize it here because the `no_*` alternative must be instantiated after the real argument A__ = copy(_snake_case ) # Hack because type=bool in argparse does not behave as we want. A__ = string_to_bool if field.type is bool or (field.default is not None and field.default is not dataclasses.MISSING): # Default value is False if we have no default when of type bool. A__ = False if field.default is dataclasses.MISSING else field.default # This is the value that will get picked if we don't include --field_name in any way A__ = default # This tells argparse we accept 0 or 1 value after --field_name A__ = '?' # This is the value that will get picked if we do --field_name (without value) A__ = True elif isclass(_snake_case ) and issubclass(_snake_case , _snake_case ): A__ = field.type.__args__[0] A__ = '+' if field.default_factory is not dataclasses.MISSING: A__ = field.default_factory() elif field.default is dataclasses.MISSING: A__ = True else: A__ = field.type if field.default is not dataclasses.MISSING: A__ = field.default elif field.default_factory is not dataclasses.MISSING: A__ = field.default_factory() else: A__ = True parser.add_argument(_snake_case , *_snake_case , **_snake_case ) # Add a complement `no_*` argument for a boolean field AFTER the initial field has already been added. # Order is important for arguments with the same destination! # We use a copy of earlier kwargs because the original kwargs have changed a lot before reaching down # here and we do not need those changes/additional keys. if field.default is True and (field.type is bool or field.type == Optional[bool]): A__ = False parser.add_argument(F'''--no_{field.name}''' , action='store_false' , dest=field.name , **_snake_case ) def _a ( self : Any , _snake_case : DataClassType ): """simple docstring""" if hasattr(_snake_case , '_argument_group_name' ): A__ = self.add_argument_group(dtype._argument_group_name ) else: A__ = self try: A__ = get_type_hints(_snake_case ) except NameError: raise RuntimeError( F'''Type resolution failed for {dtype}. Try declaring the class in global scope or ''' 'removing line of `from __future__ import annotations` which opts in Postponed ' 'Evaluation of Annotations (PEP 563)' ) except TypeError as ex: # Remove this block when we drop Python 3.9 support if sys.version_info[:2] < (3, 10) and "unsupported operand type(s) for |" in str(_snake_case ): A__ = '.'.join(map(_snake_case , sys.version_info[:3] ) ) raise RuntimeError( F'''Type resolution failed for {dtype} on Python {python_version}. Try removing ''' 'line of `from __future__ import annotations` which opts in union types as ' '`X | Y` (PEP 604) via Postponed Evaluation of Annotations (PEP 563). To ' 'support Python versions that lower than 3.10, you need to use ' '`typing.Union[X, Y]` instead of `X | Y` and `typing.Optional[X]` instead of ' '`X | None`.' ) from ex raise for field in dataclasses.fields(_snake_case ): if not field.init: continue A__ = type_hints[field.name] self._parse_dataclass_field(_snake_case , _snake_case ) def _a ( self : Optional[int] , _snake_case : Optional[Any]=None , _snake_case : Any=False , _snake_case : int=True , _snake_case : List[Any]=None , _snake_case : int=None , ): """simple docstring""" if args_file_flag or args_filename or (look_for_args_file and len(sys.argv )): A__ = [] if args_filename: args_files.append(Path(_snake_case ) ) elif look_for_args_file and len(sys.argv ): args_files.append(Path(sys.argv[0] ).with_suffix('.args' ) ) # args files specified via command line flag should overwrite default args files so we add them last if args_file_flag: # Create special parser just to extract the args_file_flag values A__ = ArgumentParser() args_file_parser.add_argument(_snake_case , type=_snake_case , action='append' ) # Use only remaining args for further parsing (remove the args_file_flag) A__ , A__ = args_file_parser.parse_known_args(args=_snake_case ) A__ = vars(_snake_case ).get(args_file_flag.lstrip('-' ) , _snake_case ) if cmd_args_file_paths: args_files.extend([Path(_snake_case ) for p in cmd_args_file_paths] ) A__ = [] for args_file in args_files: if args_file.exists(): file_args += args_file.read_text().split() # in case of duplicate arguments the last one has precedence # args specified via the command line should overwrite args from files, so we add them last A__ = file_args + args if args is not None else file_args + sys.argv[1:] A__ , A__ = self.parse_known_args(args=_snake_case ) A__ = [] for dtype in self.dataclass_types: A__ = {f.name for f in dataclasses.fields(_snake_case ) if f.init} A__ = {k: v for k, v in vars(_snake_case ).items() if k in keys} for k in keys: delattr(_snake_case , _snake_case ) A__ = dtype(**_snake_case ) outputs.append(_snake_case ) if len(namespace.__dict__ ) > 0: # additional namespace. outputs.append(_snake_case ) if return_remaining_strings: return (*outputs, remaining_args) else: if remaining_args: raise ValueError(F'''Some specified arguments are not used by the HfArgumentParser: {remaining_args}''' ) return (*outputs,) def _a ( self : Dict , _snake_case : Dict[str, Any] , _snake_case : bool = False ): """simple docstring""" A__ = set(args.keys() ) A__ = [] for dtype in self.dataclass_types: A__ = {f.name for f in dataclasses.fields(_snake_case ) if f.init} A__ = {k: v for k, v in args.items() if k in keys} unused_keys.difference_update(inputs.keys() ) A__ = dtype(**_snake_case ) outputs.append(_snake_case ) if not allow_extra_keys and unused_keys: raise ValueError(F'''Some keys are not used by the HfArgumentParser: {sorted(_snake_case )}''' ) return tuple(_snake_case ) def _a ( self : Dict , _snake_case : str , _snake_case : bool = False ): """simple docstring""" with open(Path(_snake_case ) , encoding='utf-8' ) as open_json_file: A__ = json.loads(open_json_file.read() ) A__ = self.parse_dict(_snake_case , allow_extra_keys=_snake_case ) return tuple(_snake_case ) def _a ( self : Tuple , _snake_case : str , _snake_case : bool = False ): """simple docstring""" A__ = self.parse_dict(yaml.safe_load(Path(_snake_case ).read_text() ) , allow_extra_keys=_snake_case ) return tuple(_snake_case )
9
1
import tempfile import unittest import numpy as np from diffusers import ( DDIMScheduler, DPMSolverMultistepScheduler, EulerAncestralDiscreteScheduler, EulerDiscreteScheduler, LMSDiscreteScheduler, OnnxStableDiffusionPipeline, PNDMScheduler, ) from diffusers.utils.testing_utils import is_onnx_available, nightly, require_onnxruntime, require_torch_gpu from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class __lowerCAmelCase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : int = "hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline" def _a ( self : Dict , _snake_case : str=0 ): """simple docstring""" A__ = np.random.RandomState(_snake_case ) A__ = { 'prompt': 'A painting of a squirrel eating a burger', 'generator': generator, 'num_inference_steps': 2, 'guidance_scale': 7.5, 'output_type': 'numpy', } return inputs def _a ( self : List[str] ): """simple docstring""" A__ = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint , provider='CPUExecutionProvider' ) pipe.set_progress_bar_config(disable=_snake_case ) A__ = self.get_dummy_inputs() A__ = pipe(**_snake_case ).images A__ = image[0, -3:, -3:, -1] assert image.shape == (1, 1_28, 1_28, 3) A__ = np.array([0.6_5072, 0.5_8492, 0.4_8219, 0.5_5521, 0.5_3180, 0.5_5939, 0.5_0697, 0.3_9800, 0.4_6455] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def _a ( self : Dict ): """simple docstring""" A__ = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint , provider='CPUExecutionProvider' ) A__ = PNDMScheduler.from_config(pipe.scheduler.config , skip_prk_steps=_snake_case ) pipe.set_progress_bar_config(disable=_snake_case ) A__ = self.get_dummy_inputs() A__ = pipe(**_snake_case ).images A__ = image[0, -3:, -3:, -1] assert image.shape == (1, 1_28, 1_28, 3) A__ = np.array([0.6_5863, 0.5_9425, 0.4_9326, 0.5_6313, 0.5_3875, 0.5_6627, 0.5_1065, 0.3_9777, 0.4_6330] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def _a ( self : List[Any] ): """simple docstring""" A__ = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint , provider='CPUExecutionProvider' ) A__ = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=_snake_case ) A__ = self.get_dummy_inputs() A__ = pipe(**_snake_case ).images A__ = image[0, -3:, -3:, -1] assert image.shape == (1, 1_28, 1_28, 3) A__ = np.array([0.5_3755, 0.6_0786, 0.4_7402, 0.4_9488, 0.5_1869, 0.4_9819, 0.4_7985, 0.3_8957, 0.4_4279] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def _a ( self : List[str] ): """simple docstring""" A__ = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint , provider='CPUExecutionProvider' ) A__ = EulerDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=_snake_case ) A__ = self.get_dummy_inputs() A__ = pipe(**_snake_case ).images A__ = image[0, -3:, -3:, -1] assert image.shape == (1, 1_28, 1_28, 3) A__ = np.array([0.5_3755, 0.6_0786, 0.4_7402, 0.4_9488, 0.5_1869, 0.4_9819, 0.4_7985, 0.3_8957, 0.4_4279] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def _a ( self : Tuple ): """simple docstring""" A__ = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint , provider='CPUExecutionProvider' ) A__ = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=_snake_case ) A__ = self.get_dummy_inputs() A__ = pipe(**_snake_case ).images A__ = image[0, -3:, -3:, -1] assert image.shape == (1, 1_28, 1_28, 3) A__ = np.array([0.5_3817, 0.6_0812, 0.4_7384, 0.4_9530, 0.5_1894, 0.4_9814, 0.4_7984, 0.3_8958, 0.4_4271] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def _a ( self : List[str] ): """simple docstring""" A__ = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint , provider='CPUExecutionProvider' ) A__ = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=_snake_case ) A__ = self.get_dummy_inputs() A__ = pipe(**_snake_case ).images A__ = image[0, -3:, -3:, -1] assert image.shape == (1, 1_28, 1_28, 3) A__ = np.array([0.5_3895, 0.6_0808, 0.4_7933, 0.4_9608, 0.5_1886, 0.4_9950, 0.4_8053, 0.3_8957, 0.4_4200] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def _a ( self : Dict ): """simple docstring""" A__ = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint , provider='CPUExecutionProvider' ) pipe.set_progress_bar_config(disable=_snake_case ) A__ = self.get_dummy_inputs() A__ = 3 * [inputs['prompt']] # forward A__ = pipe(**_snake_case ) A__ = output.images[0, -3:, -3:, -1] A__ = self.get_dummy_inputs() A__ = 3 * [inputs.pop('prompt' )] A__ = pipe.tokenizer( _snake_case , padding='max_length' , max_length=pipe.tokenizer.model_max_length , truncation=_snake_case , return_tensors='np' , ) A__ = text_inputs['input_ids'] A__ = pipe.text_encoder(input_ids=text_inputs.astype(np.intaa ) )[0] A__ = prompt_embeds # forward A__ = pipe(**_snake_case ) A__ = output.images[0, -3:, -3:, -1] assert np.abs(image_slice_a.flatten() - image_slice_a.flatten() ).max() < 1E-4 def _a ( self : List[Any] ): """simple docstring""" A__ = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint , provider='CPUExecutionProvider' ) pipe.set_progress_bar_config(disable=_snake_case ) A__ = self.get_dummy_inputs() A__ = 3 * ['this is a negative prompt'] A__ = negative_prompt A__ = 3 * [inputs['prompt']] # forward A__ = pipe(**_snake_case ) A__ = output.images[0, -3:, -3:, -1] A__ = self.get_dummy_inputs() A__ = 3 * [inputs.pop('prompt' )] A__ = [] for p in [prompt, negative_prompt]: A__ = pipe.tokenizer( _snake_case , padding='max_length' , max_length=pipe.tokenizer.model_max_length , truncation=_snake_case , return_tensors='np' , ) A__ = text_inputs['input_ids'] embeds.append(pipe.text_encoder(input_ids=text_inputs.astype(np.intaa ) )[0] ) A__ , A__ = embeds # forward A__ = pipe(**_snake_case ) A__ = output.images[0, -3:, -3:, -1] assert np.abs(image_slice_a.flatten() - image_slice_a.flatten() ).max() < 1E-4 @nightly @require_onnxruntime @require_torch_gpu class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @property def _a ( self : Any ): """simple docstring""" return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def _a ( self : Optional[Any] ): """simple docstring""" A__ = ort.SessionOptions() A__ = False return options def _a ( self : Dict ): """simple docstring""" A__ = OnnxStableDiffusionPipeline.from_pretrained( 'CompVis/stable-diffusion-v1-4' , revision='onnx' , safety_checker=_snake_case , feature_extractor=_snake_case , provider=self.gpu_provider , sess_options=self.gpu_options , ) sd_pipe.set_progress_bar_config(disable=_snake_case ) A__ = 'A painting of a squirrel eating a burger' np.random.seed(0 ) A__ = sd_pipe([prompt] , guidance_scale=6.0 , num_inference_steps=10 , output_type='np' ) A__ = output.images A__ = image[0, -3:, -3:, -1] assert image.shape == (1, 5_12, 5_12, 3) A__ = np.array([0.0452, 0.0390, 0.0087, 0.0350, 0.0617, 0.0364, 0.0544, 0.0523, 0.0720] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def _a ( self : Dict ): """simple docstring""" A__ = DDIMScheduler.from_pretrained( 'runwayml/stable-diffusion-v1-5' , subfolder='scheduler' , revision='onnx' ) A__ = OnnxStableDiffusionPipeline.from_pretrained( 'runwayml/stable-diffusion-v1-5' , revision='onnx' , scheduler=_snake_case , safety_checker=_snake_case , feature_extractor=_snake_case , provider=self.gpu_provider , sess_options=self.gpu_options , ) sd_pipe.set_progress_bar_config(disable=_snake_case ) A__ = 'open neural network exchange' A__ = np.random.RandomState(0 ) A__ = sd_pipe([prompt] , guidance_scale=7.5 , num_inference_steps=10 , generator=_snake_case , output_type='np' ) A__ = output.images A__ = image[0, -3:, -3:, -1] assert image.shape == (1, 5_12, 5_12, 3) A__ = np.array([0.2867, 0.1974, 0.1481, 0.7294, 0.7251, 0.6667, 0.4194, 0.5642, 0.6486] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def _a ( self : List[str] ): """simple docstring""" A__ = LMSDiscreteScheduler.from_pretrained( 'runwayml/stable-diffusion-v1-5' , subfolder='scheduler' , revision='onnx' ) A__ = OnnxStableDiffusionPipeline.from_pretrained( 'runwayml/stable-diffusion-v1-5' , revision='onnx' , scheduler=_snake_case , safety_checker=_snake_case , feature_extractor=_snake_case , provider=self.gpu_provider , sess_options=self.gpu_options , ) sd_pipe.set_progress_bar_config(disable=_snake_case ) A__ = 'open neural network exchange' A__ = np.random.RandomState(0 ) A__ = sd_pipe([prompt] , guidance_scale=7.5 , num_inference_steps=10 , generator=_snake_case , output_type='np' ) A__ = output.images A__ = image[0, -3:, -3:, -1] assert image.shape == (1, 5_12, 5_12, 3) A__ = np.array([0.2306, 0.1959, 0.1593, 0.6549, 0.6394, 0.5408, 0.5065, 0.6010, 0.6161] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def _a ( self : Tuple ): """simple docstring""" A__ = 0 def test_callback_fn(_snake_case : int , _snake_case : int , _snake_case : np.ndarray ) -> None: A__ = True nonlocal number_of_steps number_of_steps += 1 if step == 0: assert latents.shape == (1, 4, 64, 64) A__ = latents[0, -3:, -3:, -1] A__ = np.array( [-0.6772, -0.3835, -1.2456, 0.1905, -1.0974, 0.6967, -1.9353, 0.0178, 1.0167] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 1E-3 elif step == 5: assert latents.shape == (1, 4, 64, 64) A__ = latents[0, -3:, -3:, -1] A__ = np.array( [-0.3351, 0.2241, -0.1837, -0.2325, -0.6577, 0.3393, -0.0241, 0.5899, 1.3875] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 1E-3 A__ = False A__ = OnnxStableDiffusionPipeline.from_pretrained( 'runwayml/stable-diffusion-v1-5' , revision='onnx' , safety_checker=_snake_case , feature_extractor=_snake_case , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=_snake_case ) A__ = 'Andromeda galaxy in a bottle' A__ = np.random.RandomState(0 ) pipe( prompt=_snake_case , num_inference_steps=5 , guidance_scale=7.5 , generator=_snake_case , callback=_snake_case , callback_steps=1 , ) assert test_callback_fn.has_been_called assert number_of_steps == 6 def _a ( self : Optional[int] ): """simple docstring""" A__ = OnnxStableDiffusionPipeline.from_pretrained( 'runwayml/stable-diffusion-v1-5' , revision='onnx' , safety_checker=_snake_case , feature_extractor=_snake_case , provider=self.gpu_provider , sess_options=self.gpu_options , ) assert isinstance(_snake_case , _snake_case ) assert pipe.safety_checker is None A__ = pipe('example prompt' , num_inference_steps=2 ).images[0] assert image is not None # check that there's no error when saving a pipeline with one of the models being None with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(_snake_case ) A__ = OnnxStableDiffusionPipeline.from_pretrained(_snake_case ) # sanity check that the pipeline still works assert pipe.safety_checker is None A__ = pipe('example prompt' , num_inference_steps=2 ).images[0] assert image is not None
9
import argparse import collections import json from pathlib import Path import requests import torch import yaml from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( MobileViTImageProcessor, MobileViTVaConfig, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation, ) from transformers.utils import logging logging.set_verbosity_info() SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) def A ( __UpperCamelCase ) -> List[Any]: print('Loading config file...' ) def flatten_yaml_as_dict(__UpperCamelCase , __UpperCamelCase="" , __UpperCamelCase="." ): A__ = [] for k, v in d.items(): A__ = parent_key + sep + k if parent_key else k if isinstance(__UpperCamelCase , collections.abc.MutableMapping ): items.extend(flatten_yaml_as_dict(__UpperCamelCase , __UpperCamelCase , sep=__UpperCamelCase ).items() ) else: items.append((new_key, v) ) return dict(__UpperCamelCase ) A__ = argparse.Namespace() with open(__UpperCamelCase , 'r' ) as yaml_file: try: A__ = yaml.load(__UpperCamelCase , Loader=yaml.FullLoader ) A__ = flatten_yaml_as_dict(__UpperCamelCase ) for k, v in flat_cfg.items(): setattr(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) except yaml.YAMLError as exc: logger.error('Error while loading config file: {}. Error message: {}'.format(__UpperCamelCase , str(__UpperCamelCase ) ) ) return config def A ( __UpperCamelCase , __UpperCamelCase ) -> Optional[Any]: A__ = MobileViTVaConfig() A__ = False # dataset if task_name.startswith('imagenet1k_' ): A__ = 1_000 if int(task_name.strip().split('_' )[-1] ) == 384: A__ = 384 else: A__ = 256 A__ = 'imagenet-1k-id2label.json' elif task_name.startswith('imagenet21k_to_1k_' ): A__ = 21_000 if int(task_name.strip().split('_' )[-1] ) == 384: A__ = 384 else: A__ = 256 A__ = 'imagenet-22k-id2label.json' elif task_name.startswith('ade20k_' ): A__ = 151 A__ = 512 A__ = 'ade20k-id2label.json' A__ = True elif task_name.startswith('voc_' ): A__ = 21 A__ = 512 A__ = 'pascal-voc-id2label.json' A__ = True # orig_config A__ = load_orig_config_file(__UpperCamelCase ) assert getattr(__UpperCamelCase , 'model.classification.name' , -1 ) == "mobilevit_v2", "Invalid model" A__ = getattr(__UpperCamelCase , 'model.classification.mitv2.width_multiplier' , 1.0 ) assert ( getattr(__UpperCamelCase , 'model.classification.mitv2.attn_norm_layer' , -1 ) == "layer_norm_2d" ), "Norm layers other than layer_norm_2d is not supported" A__ = getattr(__UpperCamelCase , 'model.classification.activation.name' , 'swish' ) # config.image_size == getattr(orig_config, 'sampler.bs.crop_size_width', 256) if is_segmentation_model: A__ = getattr(__UpperCamelCase , 'model.segmentation.output_stride' , 16 ) if "_deeplabv3" in task_name: A__ = getattr(__UpperCamelCase , 'model.segmentation.deeplabv3.aspp_rates' , [12, 24, 36] ) A__ = getattr(__UpperCamelCase , 'model.segmentation.deeplabv3.aspp_out_channels' , 512 ) A__ = getattr(__UpperCamelCase , 'model.segmentation.deeplabv3.aspp_dropout' , 0.1 ) # id2label A__ = 'huggingface/label-files' A__ = json.load(open(hf_hub_download(__UpperCamelCase , __UpperCamelCase , repo_type='dataset' ) , 'r' ) ) A__ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} A__ = idalabel A__ = {v: k for k, v in idalabel.items()} return config def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[str]: A__ = dct.pop(__UpperCamelCase ) A__ = val def A ( __UpperCamelCase , __UpperCamelCase=False ) -> Dict: if base_model: A__ = '' else: A__ = 'mobilevitv2.' A__ = [] for k in state_dict.keys(): if k[:8] == "encoder.": A__ = k[8:] else: A__ = k if ".block." in k: A__ = k_new.replace('.block.' , '.' ) if ".conv." in k: A__ = k_new.replace('.conv.' , '.convolution.' ) if ".norm." in k: A__ = k_new.replace('.norm.' , '.normalization.' ) if "conv_1." in k: A__ = k_new.replace('conv_1.' , f'''{model_prefix}conv_stem.''' ) for i in [1, 2]: if f'''layer_{i}.''' in k: A__ = k_new.replace(f'''layer_{i}.''' , f'''{model_prefix}encoder.layer.{i-1}.layer.''' ) if ".exp_1x1." in k: A__ = k_new.replace('.exp_1x1.' , '.expand_1x1.' ) if ".red_1x1." in k: A__ = k_new.replace('.red_1x1.' , '.reduce_1x1.' ) for i in [3, 4, 5]: if f'''layer_{i}.0.''' in k: A__ = k_new.replace(f'''layer_{i}.0.''' , f'''{model_prefix}encoder.layer.{i-1}.downsampling_layer.''' ) if f'''layer_{i}.1.local_rep.0.''' in k: A__ = k_new.replace(f'''layer_{i}.1.local_rep.0.''' , f'''{model_prefix}encoder.layer.{i-1}.conv_kxk.''' ) if f'''layer_{i}.1.local_rep.1.''' in k: A__ = k_new.replace(f'''layer_{i}.1.local_rep.1.''' , f'''{model_prefix}encoder.layer.{i-1}.conv_1x1.''' ) for i in [3, 4, 5]: if i == 3: A__ = [0, 1] elif i == 4: A__ = [0, 1, 2, 3] elif i == 5: A__ = [0, 1, 2] for j in j_in: if f'''layer_{i}.1.global_rep.{j}.''' in k: A__ = k_new.replace( f'''layer_{i}.1.global_rep.{j}.''' , f'''{model_prefix}encoder.layer.{i-1}.transformer.layer.{j}.''' ) if f'''layer_{i}.1.global_rep.{j+1}.''' in k: A__ = k_new.replace( f'''layer_{i}.1.global_rep.{j+1}.''' , f'''{model_prefix}encoder.layer.{i-1}.layernorm.''' ) if f'''layer_{i}.1.conv_proj.''' in k: A__ = k_new.replace(f'''layer_{i}.1.conv_proj.''' , f'''{model_prefix}encoder.layer.{i-1}.conv_projection.''' ) if "pre_norm_attn.0." in k: A__ = k_new.replace('pre_norm_attn.0.' , 'layernorm_before.' ) if "pre_norm_attn.1." in k: A__ = k_new.replace('pre_norm_attn.1.' , 'attention.' ) if "pre_norm_ffn.0." in k: A__ = k_new.replace('pre_norm_ffn.0.' , 'layernorm_after.' ) if "pre_norm_ffn.1." in k: A__ = k_new.replace('pre_norm_ffn.1.' , 'ffn.conv1.' ) if "pre_norm_ffn.3." in k: A__ = k_new.replace('pre_norm_ffn.3.' , 'ffn.conv2.' ) if "classifier.1." in k: A__ = k_new.replace('classifier.1.' , 'classifier.' ) if "seg_head." in k: A__ = k_new.replace('seg_head.' , 'segmentation_head.' ) if ".aspp_layer." in k: A__ = k_new.replace('.aspp_layer.' , '.' ) if ".aspp_pool." in k: A__ = k_new.replace('.aspp_pool.' , '.' ) rename_keys.append((k, k_new) ) return rename_keys def A ( __UpperCamelCase ) -> Tuple: A__ = [] for k in state_dict.keys(): if k.startswith('seg_head.aux_head.' ): keys_to_ignore.append(__UpperCamelCase ) for k in keys_to_ignore: state_dict.pop(__UpperCamelCase , __UpperCamelCase ) def A ( ) -> str: A__ = 'http://images.cocodataset.org/val2017/000000039769.jpg' # url = "https://cdn.britannica.com/86/141086-050-9D7C75EE/Gulfstream-G450-business-jet-passengers.jpg" A__ = Image.open(requests.get(__UpperCamelCase , stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Optional[Any]: A__ = get_mobilevitva_config(__UpperCamelCase , __UpperCamelCase ) # load original state_dict A__ = torch.load(__UpperCamelCase , map_location='cpu' ) # load huggingface model if task_name.startswith('ade20k_' ) or task_name.startswith('voc_' ): A__ = MobileViTVaForSemanticSegmentation(__UpperCamelCase ).eval() A__ = False else: A__ = MobileViTVaForImageClassification(__UpperCamelCase ).eval() A__ = False # remove and rename some keys of load the original model A__ = checkpoint remove_unused_keys(__UpperCamelCase ) A__ = create_rename_keys(__UpperCamelCase , base_model=__UpperCamelCase ) for rename_key_src, rename_key_dest in rename_keys: rename_key(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) # load modified state_dict model.load_state_dict(__UpperCamelCase ) # Check outputs on an image, prepared by MobileViTImageProcessor A__ = MobileViTImageProcessor(crop_size=config.image_size , size=config.image_size + 32 ) A__ = image_processor(images=prepare_img() , return_tensors='pt' ) A__ = model(**__UpperCamelCase ) # verify classification model if task_name.startswith('imagenet' ): A__ = outputs.logits A__ = logits.argmax(-1 ).item() print('Predicted class:' , model.config.idalabel[predicted_class_idx] ) if task_name.startswith('imagenet1k_256' ) and config.width_multiplier == 1.0: # expected_logits for base variant A__ = torch.tensor([-1.6336E00, -7.3204E-02, -5.1883E-01] ) assert torch.allclose(logits[0, :3] , __UpperCamelCase , atol=1E-4 ) Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) print(f'''Saving model {task_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(__UpperCamelCase ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(__UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--task''', default='''imagenet1k_256''', type=str, help=( '''Name of the task for which the MobileViTV2 model you\'d like to convert is trained on . ''' ''' Classification (ImageNet-1k) - MobileViTV2 (256x256) : imagenet1k_256 - MobileViTV2 (Trained on 256x256 and Finetuned on 384x384) : imagenet1k_384 - MobileViTV2 (Trained on ImageNet-21k and Finetuned on ImageNet-1k 256x256) : imagenet21k_to_1k_256 - MobileViTV2 (Trained on ImageNet-21k, Finetuned on ImageNet-1k 256x256, and Finetuned on ImageNet-1k 384x384) : imagenet21k_to_1k_384 Segmentation - ADE20K Dataset : ade20k_deeplabv3 - Pascal VOC 2012 Dataset: voc_deeplabv3 ''' ), choices=[ '''imagenet1k_256''', '''imagenet1k_384''', '''imagenet21k_to_1k_256''', '''imagenet21k_to_1k_384''', '''ade20k_deeplabv3''', '''voc_deeplabv3''', ], ) parser.add_argument( '''--orig_checkpoint_path''', required=True, type=str, help='''Path to the original state dict (.pt file).''' ) parser.add_argument('''--orig_config_path''', required=True, type=str, help='''Path to the original config file.''') parser.add_argument( '''--pytorch_dump_folder_path''', required=True, type=str, help='''Path to the output PyTorch model directory.''' ) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_mobilevitva_checkpoint( args.task, args.orig_checkpoint_path, args.orig_config_path, args.pytorch_dump_folder_path )
9
1
from __future__ import annotations import inspect import unittest from typing import List, Tuple from transformers import RegNetConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFRegNetForImageClassification, TFRegNetModel if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class __lowerCAmelCase : """simple docstring""" def __init__( self : Any , _snake_case : Any , _snake_case : Dict=3 , _snake_case : Optional[int]=32 , _snake_case : Optional[int]=3 , _snake_case : Tuple=10 , _snake_case : Dict=[10, 20, 30, 40] , _snake_case : List[Any]=[1, 1, 2, 1] , _snake_case : List[str]=True , _snake_case : Optional[int]=True , _snake_case : Dict="relu" , _snake_case : Tuple=3 , _snake_case : Union[str, Any]=None , ): """simple docstring""" A__ = parent A__ = batch_size A__ = image_size A__ = num_channels A__ = embeddings_size A__ = hidden_sizes A__ = depths A__ = is_training A__ = use_labels A__ = hidden_act A__ = num_labels A__ = scope A__ = len(_snake_case ) def _a ( self : List[Any] ): """simple docstring""" A__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size] , self.num_labels ) A__ = self.get_config() return config, pixel_values, labels def _a ( self : Union[str, Any] ): """simple docstring""" return RegNetConfig( num_channels=self.num_channels , embeddings_size=self.embeddings_size , hidden_sizes=self.hidden_sizes , depths=self.depths , hidden_act=self.hidden_act , num_labels=self.num_labels , ) def _a ( self : int , _snake_case : Union[str, Any] , _snake_case : Dict , _snake_case : List[str] ): """simple docstring""" A__ = TFRegNetModel(config=_snake_case ) A__ = model(_snake_case , training=_snake_case ) # expected last hidden states: B, C, H // 32, W // 32 self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def _a ( self : Optional[int] , _snake_case : str , _snake_case : List[Any] , _snake_case : Dict ): """simple docstring""" A__ = self.num_labels A__ = TFRegNetForImageClassification(_snake_case ) A__ = model(_snake_case , labels=_snake_case , training=_snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _a ( self : Tuple ): """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ = config_and_inputs A__ = {'pixel_values': pixel_values} return config, inputs_dict @require_tf class __lowerCAmelCase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Optional[int] = (TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else () A__ : Dict = ( {"feature-extraction": TFRegNetModel, "image-classification": TFRegNetForImageClassification} if is_tf_available() else {} ) A__ : List[str] = False A__ : List[Any] = False A__ : List[str] = False A__ : List[Any] = False A__ : int = False def _a ( self : Optional[Any] ): """simple docstring""" A__ = TFRegNetModelTester(self ) A__ = ConfigTester(self , config_class=_snake_case , has_text_modality=_snake_case ) def _a ( self : Optional[Any] ): """simple docstring""" return @unittest.skip(reason='RegNet does not use inputs_embeds' ) def _a ( self : List[Any] ): """simple docstring""" pass @unittest.skipIf( not is_tf_available() or len(tf.config.list_physical_devices('GPU' ) ) == 0 , reason='TF does not support backprop for grouped convolutions on CPU.' , ) @slow def _a ( self : Optional[int] ): """simple docstring""" super().test_keras_fit() @unittest.skip(reason='RegNet does not support input and output embeddings' ) def _a ( self : List[Any] ): """simple docstring""" pass def _a ( self : int ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(_snake_case ) A__ = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ = [*signature.parameters.keys()] A__ = ['pixel_values'] self.assertListEqual(arg_names[:1] , _snake_case ) def _a ( self : Dict ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def _a ( self : int ): """simple docstring""" def check_hidden_states_output(_snake_case : Optional[Any] , _snake_case : Union[str, Any] , _snake_case : int ): A__ = model_class(_snake_case ) A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) , training=_snake_case ) A__ = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states A__ = self.model_tester.num_stages self.assertEqual(len(_snake_case ) , expected_num_stages + 1 ) # RegNet's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 2, self.model_tester.image_size // 2] , ) A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = ['basic', 'bottleneck'] for model_class in self.all_model_classes: for layer_type in layers_type: A__ = layer_type A__ = True check_hidden_states_output(_snake_case , _snake_case , _snake_case ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A__ = True check_hidden_states_output(_snake_case , _snake_case , _snake_case ) def _a ( self : List[Any] ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() def check_equivalence(_snake_case : Tuple , _snake_case : List[Any] , _snake_case : Optional[int] , _snake_case : Dict={} ): A__ = model(_snake_case , return_dict=_snake_case , **_snake_case ) A__ = model(_snake_case , return_dict=_snake_case , **_snake_case ).to_tuple() def recursive_check(_snake_case : int , _snake_case : int ): if isinstance(_snake_case , (List, Tuple) ): for tuple_iterable_value, dict_iterable_value in zip(_snake_case , _snake_case ): recursive_check(_snake_case , _snake_case ) elif tuple_object is None: return else: self.assertTrue( all(tf.equal(_snake_case , _snake_case ) ) , msg=( 'Tuple and dict output are not equal. Difference:' F''' {tf.math.reduce_max(tf.abs(tuple_object - dict_object ) )}''' ) , ) recursive_check(_snake_case , _snake_case ) for model_class in self.all_model_classes: A__ = model_class(_snake_case ) A__ = self._prepare_for_class(_snake_case , _snake_case ) A__ = self._prepare_for_class(_snake_case , _snake_case ) check_equivalence(_snake_case , _snake_case , _snake_case ) A__ = self._prepare_for_class(_snake_case , _snake_case , return_labels=_snake_case ) A__ = self._prepare_for_class(_snake_case , _snake_case , return_labels=_snake_case ) check_equivalence(_snake_case , _snake_case , _snake_case ) A__ = self._prepare_for_class(_snake_case , _snake_case ) A__ = self._prepare_for_class(_snake_case , _snake_case ) check_equivalence(_snake_case , _snake_case , _snake_case , {'output_hidden_states': True} ) A__ = self._prepare_for_class(_snake_case , _snake_case , return_labels=_snake_case ) A__ = self._prepare_for_class(_snake_case , _snake_case , return_labels=_snake_case ) check_equivalence(_snake_case , _snake_case , _snake_case , {'output_hidden_states': True} ) def _a ( self : Union[str, Any] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_snake_case ) @slow def _a ( self : List[Any] ): """simple docstring""" for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = TFRegNetModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def A ( ) -> Dict: A__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_tf @require_vision class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @cached_property def _a ( self : Union[str, Any] ): """simple docstring""" return ( AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def _a ( self : int ): """simple docstring""" A__ = TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) A__ = self.default_image_processor A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='tf' ) # forward pass A__ = model(**_snake_case , training=_snake_case ) # verify the logits A__ = tf.TensorShape((1, 10_00) ) self.assertEqual(outputs.logits.shape , _snake_case ) A__ = tf.constant([-0.4180, -1.5051, -3.4836] ) tf.debugging.assert_near(outputs.logits[0, :3] , _snake_case , atol=1E-4 )
9
import argparse from collections import defaultdict import yaml SCREAMING_SNAKE_CASE__ = '''docs/source/en/_toctree.yml''' def A ( __UpperCamelCase ) -> Optional[Any]: A__ = defaultdict(__UpperCamelCase ) for doc in model_doc: counts[doc["local"]] += 1 A__ = [key for key, value in counts.items() if value > 1] A__ = [] for duplicate_key in duplicates: A__ = list({doc['title'] for doc in model_doc if doc['local'] == duplicate_key} ) if len(__UpperCamelCase ) > 1: raise ValueError( f'''{duplicate_key} is present several times in the documentation table of content at ''' '`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the ' 'others.' ) # Only add this once new_doc.append({'local': duplicate_key, 'title': titles[0]} ) # Add none duplicate-keys new_doc.extend([doc for doc in model_doc if counts[doc['local']] == 1] ) # Sort return sorted(__UpperCamelCase , key=lambda __UpperCamelCase : s["title"].lower() ) def A ( __UpperCamelCase=False ) -> str: with open(__UpperCamelCase , encoding='utf-8' ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]['sections'] # Then to the model doc A__ = 0 while api_doc[model_idx]["title"] != "Models": model_idx += 1 A__ = api_doc[model_idx]['sections'] A__ = [(idx, section) for idx, section in enumerate(__UpperCamelCase ) if 'sections' in section] A__ = False for idx, modality_doc in modalities_docs: A__ = modality_doc['sections'] A__ = clean_model_doc_toc(__UpperCamelCase ) if old_modality_doc != new_modality_doc: A__ = True if overwrite: A__ = new_modality_doc if diff: if overwrite: A__ = model_doc A__ = api_doc with open(__UpperCamelCase , 'w' , encoding='utf-8' ) as f: f.write(yaml.dump(__UpperCamelCase , allow_unicode=__UpperCamelCase ) ) else: raise ValueError( 'The model doc part of the table of content is not properly sorted, run `make style` to fix this.' ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') SCREAMING_SNAKE_CASE__ = parser.parse_args() check_model_doc(args.fix_and_overwrite)
9
1
from dataclasses import dataclass, field from typing import Tuple from ..utils import cached_property, is_tf_available, logging, requires_backends from .benchmark_args_utils import BenchmarkArguments if is_tf_available(): import tensorflow as tf SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) @dataclass class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : List[Any] = [ "no_inference", "no_cuda", "no_tpu", "no_speed", "no_memory", "no_env_print", "no_multi_process", ] def __init__( self : Union[str, Any] , **_snake_case : List[str] ): """simple docstring""" for deprecated_arg in self.deprecated_args: if deprecated_arg in kwargs: A__ = deprecated_arg[3:] A__ = not kwargs.pop(_snake_case ) logger.warning( F'''{deprecated_arg} is depreciated. Please use --no-{positive_arg} or''' F''' {positive_arg}={kwargs[positive_arg]}''' ) A__ = kwargs.pop('tpu_name' , self.tpu_name ) A__ = kwargs.pop('device_idx' , self.device_idx ) A__ = kwargs.pop('eager_mode' , self.eager_mode ) A__ = kwargs.pop('use_xla' , self.use_xla ) super().__init__(**_snake_case ) A__ : str = field( default=UpperCAmelCase_ , metadata={"help": "Name of TPU"} , ) A__ : int = field( default=0 , metadata={"help": "CPU / GPU device index. Defaults to 0."} , ) A__ : bool = field(default=UpperCAmelCase_ , metadata={"help": "Benchmark models in eager model."} ) A__ : bool = field( default=UpperCAmelCase_ , metadata={ "help": "Benchmark models using XLA JIT compilation. Note that `eager_model` has to be set to `False`." } , ) @cached_property def _a ( self : Dict ): """simple docstring""" requires_backends(self , ['tf'] ) A__ = None if self.tpu: try: if self.tpu_name: A__ = tf.distribute.cluster_resolver.TPUClusterResolver(self.tpu_name ) else: A__ = tf.distribute.cluster_resolver.TPUClusterResolver() except ValueError: A__ = None return tpu @cached_property def _a ( self : str ): """simple docstring""" requires_backends(self , ['tf'] ) if self.is_tpu: tf.config.experimental_connect_to_cluster(self._setup_tpu ) tf.tpu.experimental.initialize_tpu_system(self._setup_tpu ) A__ = tf.distribute.TPUStrategy(self._setup_tpu ) else: # currently no multi gpu is allowed if self.is_gpu: # TODO: Currently only single GPU is supported tf.config.set_visible_devices(self.gpu_list[self.device_idx] , 'GPU' ) A__ = tf.distribute.OneDeviceStrategy(device=F'''/gpu:{self.device_idx}''' ) else: tf.config.set_visible_devices([] , 'GPU' ) # disable GPU A__ = tf.distribute.OneDeviceStrategy(device=F'''/cpu:{self.device_idx}''' ) return strategy @property def _a ( self : Optional[Any] ): """simple docstring""" requires_backends(self , ['tf'] ) return self._setup_tpu is not None @property def _a ( self : Tuple ): """simple docstring""" requires_backends(self , ['tf'] ) return self._setup_strategy @property def _a ( self : Any ): """simple docstring""" requires_backends(self , ['tf'] ) return tf.config.list_physical_devices('GPU' ) @property def _a ( self : Dict ): """simple docstring""" requires_backends(self , ['tf'] ) if self.cuda: return len(self.gpu_list ) return 0 @property def _a ( self : List[Any] ): """simple docstring""" return self.n_gpu > 0
9
import inspect import unittest from transformers import SegformerConfig, is_torch_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_MAPPING, SegformerForImageClassification, SegformerForSemanticSegmentation, SegformerModel, ) from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import SegformerImageProcessor class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def _a ( self : List[str] ): """simple docstring""" A__ = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(_snake_case , 'hidden_sizes' ) ) self.parent.assertTrue(hasattr(_snake_case , 'num_attention_heads' ) ) self.parent.assertTrue(hasattr(_snake_case , 'num_encoder_blocks' ) ) class __lowerCAmelCase : """simple docstring""" def __init__( self : Any , _snake_case : str , _snake_case : Union[str, Any]=13 , _snake_case : Any=64 , _snake_case : Optional[Any]=3 , _snake_case : Dict=4 , _snake_case : Tuple=[2, 2, 2, 2] , _snake_case : str=[8, 4, 2, 1] , _snake_case : Union[str, Any]=[16, 32, 64, 1_28] , _snake_case : int=[1, 4, 8, 16] , _snake_case : List[str]=[1, 2, 4, 8] , _snake_case : int=True , _snake_case : int=True , _snake_case : Union[str, Any]="gelu" , _snake_case : Optional[int]=0.1 , _snake_case : Tuple=0.1 , _snake_case : Dict=0.02 , _snake_case : Tuple=3 , _snake_case : int=None , ): """simple docstring""" A__ = parent A__ = batch_size A__ = image_size A__ = num_channels A__ = num_encoder_blocks A__ = sr_ratios A__ = depths A__ = hidden_sizes A__ = downsampling_rates A__ = num_attention_heads A__ = is_training A__ = use_labels A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = initializer_range A__ = num_labels A__ = scope def _a ( self : int ): """simple docstring""" A__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) A__ = self.get_config() return config, pixel_values, labels def _a ( self : int ): """simple docstring""" return SegformerConfig( image_size=self.image_size , num_channels=self.num_channels , num_encoder_blocks=self.num_encoder_blocks , depths=self.depths , hidden_sizes=self.hidden_sizes , num_attention_heads=self.num_attention_heads , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , initializer_range=self.initializer_range , ) def _a ( self : int , _snake_case : Optional[Any] , _snake_case : int , _snake_case : Any ): """simple docstring""" A__ = SegformerModel(config=_snake_case ) model.to(_snake_case ) model.eval() A__ = model(_snake_case ) A__ = A__ = self.image_size // (self.downsampling_rates[-1] * 2) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width) ) def _a ( self : Union[str, Any] , _snake_case : Union[str, Any] , _snake_case : Tuple , _snake_case : Dict ): """simple docstring""" A__ = self.num_labels A__ = SegformerForSemanticSegmentation(_snake_case ) model.to(_snake_case ) model.eval() A__ = model(_snake_case ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4) ) A__ = model(_snake_case , labels=_snake_case ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4) ) self.parent.assertGreater(result.loss , 0.0 ) def _a ( self : List[str] , _snake_case : Optional[Any] , _snake_case : Union[str, Any] , _snake_case : List[str] ): """simple docstring""" A__ = 1 A__ = SegformerForSemanticSegmentation(config=_snake_case ) model.to(_snake_case ) model.eval() A__ = torch.randint(0 , 1 , (self.batch_size, self.image_size, self.image_size) ).to(_snake_case ) A__ = model(_snake_case , labels=_snake_case ) self.parent.assertGreater(result.loss , 0.0 ) def _a ( self : List[Any] ): """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ = config_and_inputs A__ = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class __lowerCAmelCase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Optional[int] = ( ( SegformerModel, SegformerForSemanticSegmentation, SegformerForImageClassification, ) if is_torch_available() else () ) A__ : Union[str, Any] = ( { "feature-extraction": SegformerModel, "image-classification": SegformerForImageClassification, "image-segmentation": SegformerForSemanticSegmentation, } if is_torch_available() else {} ) A__ : Optional[Any] = True A__ : str = False A__ : Tuple = False A__ : Dict = False def _a ( self : Union[str, Any] ): """simple docstring""" A__ = SegformerModelTester(self ) A__ = SegformerConfigTester(self , config_class=_snake_case ) def _a ( self : Optional[int] ): """simple docstring""" self.config_tester.run_common_tests() def _a ( self : Optional[Any] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def _a ( self : List[Any] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_binary_image_segmentation(*_snake_case ) def _a ( self : Tuple ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_segmentation(*_snake_case ) @unittest.skip('SegFormer does not use inputs_embeds' ) def _a ( self : List[Any] ): """simple docstring""" pass @unittest.skip('SegFormer does not have get_input_embeddings method and get_output_embeddings methods' ) def _a ( self : Dict ): """simple docstring""" pass def _a ( self : Dict ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(_snake_case ) A__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ = [*signature.parameters.keys()] A__ = ['pixel_values'] self.assertListEqual(arg_names[:1] , _snake_case ) def _a ( self : Dict ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = True for model_class in self.all_model_classes: A__ = True A__ = False A__ = True A__ = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) ) A__ = outputs.attentions A__ = sum(self.model_tester.depths ) self.assertEqual(len(_snake_case ) , _snake_case ) # check that output_attentions also work using config del inputs_dict["output_attentions"] A__ = True A__ = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) ) A__ = outputs.attentions self.assertEqual(len(_snake_case ) , _snake_case ) # verify the first attentions (first block, first layer) A__ = (self.model_tester.image_size // 4) ** 2 A__ = (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2 self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len] , ) # verify the last attentions (last block, last layer) A__ = (self.model_tester.image_size // 32) ** 2 A__ = (self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2 self.assertListEqual( list(attentions[-1].shape[-3:] ) , [self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len] , ) A__ = len(_snake_case ) # Check attention is always last and order is fine A__ = True A__ = True A__ = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) ) self.assertEqual(out_len + 1 , len(_snake_case ) ) A__ = outputs.attentions self.assertEqual(len(_snake_case ) , _snake_case ) # verify the first attentions (first block, first layer) A__ = (self.model_tester.image_size // 4) ** 2 A__ = (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2 self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len] , ) def _a ( self : Union[str, Any] ): """simple docstring""" def check_hidden_states_output(_snake_case : Dict , _snake_case : int , _snake_case : List[Any] ): A__ = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) ) A__ = outputs.hidden_states A__ = self.model_tester.num_encoder_blocks self.assertEqual(len(_snake_case ) , _snake_case ) # verify the first hidden states (first block) self.assertListEqual( list(hidden_states[0].shape[-3:] ) , [ self.model_tester.hidden_sizes[0], self.model_tester.image_size // 4, self.model_tester.image_size // 4, ] , ) A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = True check_hidden_states_output(_snake_case , _snake_case , _snake_case ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A__ = True check_hidden_states_output(_snake_case , _snake_case , _snake_case ) def _a ( self : Tuple ): """simple docstring""" if not self.model_tester.is_training: return A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = True for model_class in self.all_model_classes: if model_class in get_values(_snake_case ): continue A__ = model_class(_snake_case ) model.to(_snake_case ) model.train() A__ = self._prepare_for_class(_snake_case , _snake_case , return_labels=_snake_case ) A__ = model(**_snake_case ).loss loss.backward() @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.' ) def _a ( self : Optional[Any] ): """simple docstring""" pass @slow def _a ( self : Tuple ): """simple docstring""" for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = SegformerModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def A ( ) -> str: A__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @slow def _a ( self : Dict ): """simple docstring""" A__ = SegformerImageProcessor( image_scale=(5_12, 5_12) , keep_ratio=_snake_case , align=_snake_case , do_random_crop=_snake_case ) A__ = SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512' ).to( _snake_case ) A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ) A__ = encoded_inputs.pixel_values.to(_snake_case ) with torch.no_grad(): A__ = model(_snake_case ) A__ = torch.Size((1, model.config.num_labels, 1_28, 1_28) ) self.assertEqual(outputs.logits.shape , _snake_case ) A__ = torch.tensor( [ [[-4.6310, -5.5232, -6.2356], [-5.1921, -6.1444, -6.5996], [-5.4424, -6.2790, -6.7574]], [[-12.1391, -13.3122, -13.9554], [-12.8732, -13.9352, -14.3563], [-12.9438, -13.8226, -14.2513]], [[-12.5134, -13.4686, -14.4915], [-12.8669, -14.4343, -14.7758], [-13.2523, -14.5819, -15.0694]], ] ).to(_snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3] , _snake_case , atol=1E-4 ) ) @slow def _a ( self : Optional[Any] ): """simple docstring""" A__ = SegformerImageProcessor( image_scale=(5_12, 5_12) , keep_ratio=_snake_case , align=_snake_case , do_random_crop=_snake_case ) A__ = SegformerForSemanticSegmentation.from_pretrained( 'nvidia/segformer-b1-finetuned-cityscapes-1024-1024' ).to(_snake_case ) A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ) A__ = encoded_inputs.pixel_values.to(_snake_case ) with torch.no_grad(): A__ = model(_snake_case ) A__ = torch.Size((1, model.config.num_labels, 1_28, 1_28) ) self.assertEqual(outputs.logits.shape , _snake_case ) A__ = torch.tensor( [ [[-13.5748, -13.9111, -12.6500], [-14.3500, -15.3683, -14.2328], [-14.7532, -16.0424, -15.6087]], [[-17.1651, -15.8725, -12.9653], [-17.2580, -17.3718, -14.8223], [-16.6058, -16.8783, -16.7452]], [[-3.6456, -3.0209, -1.4203], [-3.0797, -3.1959, -2.0000], [-1.8757, -1.9217, -1.6997]], ] ).to(_snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3] , _snake_case , atol=1E-1 ) ) @slow def _a ( self : Any ): """simple docstring""" A__ = SegformerImageProcessor( image_scale=(5_12, 5_12) , keep_ratio=_snake_case , align=_snake_case , do_random_crop=_snake_case ) A__ = SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512' ).to( _snake_case ) A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ) A__ = encoded_inputs.pixel_values.to(_snake_case ) with torch.no_grad(): A__ = model(_snake_case ) A__ = outputs.logits.detach().cpu() A__ = image_processor.post_process_semantic_segmentation(outputs=_snake_case , target_sizes=[(5_00, 3_00)] ) A__ = torch.Size((5_00, 3_00) ) self.assertEqual(segmentation[0].shape , _snake_case ) A__ = image_processor.post_process_semantic_segmentation(outputs=_snake_case ) A__ = torch.Size((1_28, 1_28) ) self.assertEqual(segmentation[0].shape , _snake_case )
9
1
def A ( __UpperCamelCase ) -> bool: return number & 1 == 0 if __name__ == "__main__": import doctest doctest.testmod()
9
import logging from pathlib import Path import numpy as np import pytorch_lightning as pl import torch from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint from pytorch_lightning.utilities import rank_zero_only from utils_rag import save_json def A ( __UpperCamelCase ) -> Optional[int]: A__ = filter(lambda __UpperCamelCase : p.requires_grad , model.parameters() ) A__ = sum([np.prod(p.size() ) for p in model_parameters] ) return params SCREAMING_SNAKE_CASE__ = logging.getLogger(__name__) def A ( __UpperCamelCase , __UpperCamelCase ) -> Dict: if metric == "rouge2": A__ = '{val_avg_rouge2:.4f}-{step_count}' elif metric == "bleu": A__ = '{val_avg_bleu:.4f}-{step_count}' elif metric == "em": A__ = '{val_avg_em:.4f}-{step_count}' elif metric == "loss": A__ = '{val_avg_loss:.4f}-{step_count}' else: raise NotImplementedError( f'''seq2seq callbacks only support rouge2 and bleu, got {metric}, You can make your own by adding to this''' ' function.' ) A__ = ModelCheckpoint( dirpath=__UpperCamelCase , filename=__UpperCamelCase , monitor=f'''val_{metric}''' , mode='max' , save_top_k=1 , every_n_epochs=1 , ) return checkpoint_callback def A ( __UpperCamelCase , __UpperCamelCase ) -> Any: return EarlyStopping( monitor=f'''val_{metric}''' , mode='min' if 'loss' in metric else 'max' , patience=__UpperCamelCase , verbose=__UpperCamelCase , ) class __lowerCAmelCase ( pl.Callback ): """simple docstring""" def _a ( self : Dict , _snake_case : Union[str, Any] , _snake_case : str ): """simple docstring""" A__ = {F'''lr_group_{i}''': param['lr'] for i, param in enumerate(pl_module.trainer.optimizers[0].param_groups )} pl_module.logger.log_metrics(_snake_case ) @rank_zero_only def _a ( self : Union[str, Any] , _snake_case : pl.Trainer , _snake_case : pl.LightningModule , _snake_case : str , _snake_case : Optional[Any]=True ): """simple docstring""" logger.info(F'''***** {type_path} results at step {trainer.global_step:05d} *****''' ) A__ = trainer.callback_metrics trainer.logger.log_metrics({k: v for k, v in metrics.items() if k not in ['log', 'progress_bar', 'preds']} ) # Log results A__ = Path(pl_module.hparams.output_dir ) if type_path == "test": A__ = od / 'test_results.txt' A__ = od / 'test_generations.txt' else: # this never gets hit. I prefer not to save intermediate generations, and results are in metrics.json # If people want this it will be easy enough to add back. A__ = od / F'''{type_path}_results/{trainer.global_step:05d}.txt''' A__ = od / F'''{type_path}_generations/{trainer.global_step:05d}.txt''' results_file.parent.mkdir(exist_ok=_snake_case ) generations_file.parent.mkdir(exist_ok=_snake_case ) with open(_snake_case , 'a+' ) as writer: for key in sorted(_snake_case ): if key in ["log", "progress_bar", "preds"]: continue A__ = metrics[key] if isinstance(_snake_case , torch.Tensor ): A__ = val.item() A__ = F'''{key}: {val:.6f}\n''' writer.write(_snake_case ) if not save_generations: return if "preds" in metrics: A__ = '\n'.join(metrics['preds'] ) generations_file.open('w+' ).write(_snake_case ) @rank_zero_only def _a ( self : Dict , _snake_case : List[str] , _snake_case : List[Any] ): """simple docstring""" try: A__ = pl_module.model.model.num_parameters() except AttributeError: A__ = pl_module.model.num_parameters() A__ = count_trainable_parameters(_snake_case ) # mp stands for million parameters trainer.logger.log_metrics({'n_params': npars, 'mp': npars / 1E6, 'grad_mp': n_trainable_pars / 1E6} ) @rank_zero_only def _a ( self : int , _snake_case : pl.Trainer , _snake_case : pl.LightningModule ): """simple docstring""" save_json(pl_module.metrics , pl_module.metrics_save_path ) return self._write_logs(_snake_case , _snake_case , 'test' ) @rank_zero_only def _a ( self : Optional[Any] , _snake_case : pl.Trainer , _snake_case : List[Any] ): """simple docstring""" save_json(pl_module.metrics , pl_module.metrics_save_path ) # Uncommenting this will save val generations # return self._write_logs(trainer, pl_module, "valid")
9
1
from __future__ import annotations import copy import inspect import json import math import os import tempfile import unittest from importlib import import_module import numpy as np from transformers import ViTMAEConfig from transformers.file_utils import cached_property, is_tf_available, is_vision_available from transformers.testing_utils import require_tf, require_vision, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFViTMAEForPreTraining, TFViTMAEModel if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __lowerCAmelCase : """simple docstring""" def __init__( self : Any , _snake_case : str , _snake_case : Optional[Any]=13 , _snake_case : Dict=30 , _snake_case : Union[str, Any]=2 , _snake_case : Union[str, Any]=3 , _snake_case : Any=True , _snake_case : List[str]=True , _snake_case : str=32 , _snake_case : str=2 , _snake_case : Union[str, Any]=4 , _snake_case : Union[str, Any]=37 , _snake_case : Union[str, Any]="gelu" , _snake_case : Union[str, Any]=0.1 , _snake_case : int=0.1 , _snake_case : Union[str, Any]=10 , _snake_case : Any=0.02 , _snake_case : List[Any]=3 , _snake_case : str=0.6 , _snake_case : Tuple=None , ): """simple docstring""" A__ = parent A__ = batch_size A__ = image_size A__ = patch_size A__ = num_channels A__ = is_training A__ = use_labels A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = type_sequence_label_size A__ = initializer_range A__ = mask_ratio A__ = scope # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above # (we add 1 for the [CLS] token) A__ = (image_size // patch_size) ** 2 A__ = int(math.ceil((1 - mask_ratio) * (num_patches + 1) ) ) def _a ( self : List[str] ): """simple docstring""" A__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A__ = self.get_config() return config, pixel_values, labels def _a ( self : Optional[Any] ): """simple docstring""" return ViTMAEConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , decoder_hidden_size=self.hidden_size , decoder_num_hidden_layers=self.num_hidden_layers , decoder_num_attention_heads=self.num_attention_heads , decoder_intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_snake_case , initializer_range=self.initializer_range , mask_ratio=self.mask_ratio , ) def _a ( self : List[Any] , _snake_case : Any , _snake_case : Dict , _snake_case : Any ): """simple docstring""" A__ = TFViTMAEModel(config=_snake_case ) A__ = model(_snake_case , training=_snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _a ( self : Any , _snake_case : Dict , _snake_case : int , _snake_case : Any ): """simple docstring""" A__ = TFViTMAEForPreTraining(_snake_case ) A__ = model(_snake_case , training=_snake_case ) # expected sequence length = num_patches A__ = (self.image_size // self.patch_size) ** 2 A__ = self.patch_size**2 * self.num_channels self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) # test greyscale images A__ = 1 A__ = TFViTMAEForPreTraining(_snake_case ) A__ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A__ = model(_snake_case , training=_snake_case ) A__ = self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) def _a ( self : int ): """simple docstring""" A__ = self.prepare_config_and_inputs() ((A__) , (A__) , (A__)) = config_and_inputs A__ = {'pixel_values': pixel_values} return config, inputs_dict @require_tf class __lowerCAmelCase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Any = (TFViTMAEModel, TFViTMAEForPreTraining) if is_tf_available() else () A__ : Tuple = {"feature-extraction": TFViTMAEModel} if is_tf_available() else {} A__ : Dict = False A__ : Dict = False A__ : str = False A__ : List[Any] = False def _a ( self : Any ): """simple docstring""" A__ = TFViTMAEModelTester(self ) A__ = ConfigTester(self , config_class=_snake_case , has_text_modality=_snake_case , hidden_size=37 ) def _a ( self : List[str] ): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='ViTMAE does not use inputs_embeds' ) def _a ( self : Dict ): """simple docstring""" pass def _a ( self : Union[str, Any] ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(_snake_case ) self.assertIsInstance(model.get_input_embeddings() , (tf.keras.layers.Layer) ) A__ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_snake_case , tf.keras.layers.Layer ) ) def _a ( self : List[str] ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(_snake_case ) A__ = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ = [*signature.parameters.keys()] A__ = ['pixel_values'] self.assertListEqual(arg_names[:1] , _snake_case ) def _a ( self : str ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def _a ( self : Dict ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*_snake_case ) def _a ( self : Tuple ): """simple docstring""" np.random.seed(2 ) A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = int((config.image_size // config.patch_size) ** 2 ) A__ = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: A__ = model_class(_snake_case ) A__ = self._prepare_for_class(_snake_case , _snake_case ) A__ = model(_snake_case , noise=_snake_case ) A__ = copy.deepcopy(self._prepare_for_class(_snake_case , _snake_case ) ) A__ = model(**_snake_case , noise=_snake_case ) A__ = outputs_dict[0].numpy() A__ = outputs_keywords[0].numpy() self.assertLess(np.sum(np.abs(output_dict - output_keywords ) ) , 1E-6 ) def _a ( self : Optional[int] ): """simple docstring""" np.random.seed(2 ) A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = int((config.image_size // config.patch_size) ** 2 ) A__ = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) def prepare_numpy_arrays(_snake_case : str ): A__ = {} for k, v in inputs_dict.items(): if tf.is_tensor(_snake_case ): A__ = v.numpy() else: A__ = np.array(_snake_case ) return inputs_np_dict for model_class in self.all_model_classes: A__ = model_class(_snake_case ) A__ = self._prepare_for_class(_snake_case , _snake_case ) A__ = prepare_numpy_arrays(_snake_case ) A__ = model(_snake_case , noise=_snake_case ) A__ = model(**_snake_case , noise=_snake_case ) self.assert_outputs_same(_snake_case , _snake_case ) def _a ( self : Union[str, Any] , _snake_case : Dict , _snake_case : Any , _snake_case : Optional[Any] ): """simple docstring""" np.random.seed(2 ) A__ = int((tf_model.config.image_size // tf_model.config.patch_size) ** 2 ) A__ = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) A__ = tf.constant(_snake_case ) # Add `noise` argument. # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument A__ = tf_noise super().check_pt_tf_models(_snake_case , _snake_case , _snake_case ) def _a ( self : Union[str, Any] ): """simple docstring""" np.random.seed(2 ) A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = { module_member for model_class in self.all_model_classes for module in (import_module(model_class.__module__ ),) for module_member_name in dir(_snake_case ) if module_member_name.endswith('MainLayer' ) # This condition is required, since `modeling_tf_clip.py` has 3 classes whose names end with `MainLayer`. and module_member_name[: -len('MainLayer' )] == model_class.__name__[: -len('Model' )] for module_member in (getattr(_snake_case , _snake_case ),) if isinstance(_snake_case , _snake_case ) and tf.keras.layers.Layer in module_member.__bases__ and getattr(_snake_case , '_keras_serializable' , _snake_case ) } A__ = int((config.image_size // config.patch_size) ** 2 ) A__ = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) A__ = tf.convert_to_tensor(_snake_case ) inputs_dict.update({'noise': noise} ) for main_layer_class in tf_main_layer_classes: A__ = main_layer_class(_snake_case ) A__ = { name: tf.keras.Input(tensor.shape[1:] , dtype=tensor.dtype ) for name, tensor in inputs_dict.items() } A__ = tf.keras.Model(_snake_case , outputs=main_layer(_snake_case ) ) A__ = model(_snake_case ) with tempfile.TemporaryDirectory() as tmpdirname: A__ = os.path.join(_snake_case , 'keras_model.h5' ) model.save(_snake_case ) A__ = tf.keras.models.load_model( _snake_case , custom_objects={main_layer_class.__name__: main_layer_class} ) assert isinstance(_snake_case , tf.keras.Model ) A__ = model(_snake_case ) self.assert_outputs_same(_snake_case , _snake_case ) @slow def _a ( self : List[str] ): """simple docstring""" np.random.seed(2 ) A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = int((config.image_size // config.patch_size) ** 2 ) A__ = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: A__ = model_class(_snake_case ) A__ = self._prepare_for_class(_snake_case , _snake_case ) A__ = model(_snake_case , noise=_snake_case ) if model_class.__name__ == "TFViTMAEModel": A__ = outputs.last_hidden_state.numpy() A__ = 0 else: A__ = outputs.logits.numpy() A__ = 0 with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(_snake_case , saved_model=_snake_case ) A__ = model_class.from_pretrained(_snake_case ) A__ = model(_snake_case , noise=_snake_case ) if model_class.__name__ == "TFViTMAEModel": A__ = after_outputs['last_hidden_state'].numpy() A__ = 0 else: A__ = after_outputs['logits'].numpy() A__ = 0 A__ = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(_snake_case , 1E-5 ) def _a ( self : int ): """simple docstring""" np.random.seed(2 ) A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = int((config.image_size // config.patch_size) ** 2 ) A__ = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: A__ = model_class(_snake_case ) A__ = self._prepare_for_class(_snake_case , _snake_case ) A__ = model(_snake_case , noise=_snake_case ) A__ = model.get_config() # make sure that returned config is jsonifiable, which is required by keras json.dumps(_snake_case ) A__ = model_class.from_config(model.get_config() ) # make sure it also accepts a normal config A__ = model_class.from_config(model.config ) A__ = new_model(_snake_case ) # Build model new_model.set_weights(model.get_weights() ) A__ = new_model(_snake_case , noise=_snake_case ) self.assert_outputs_same(_snake_case , _snake_case ) @unittest.skip( reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n to get deterministic results.' ) def _a ( self : Optional[Any] ): """simple docstring""" pass @unittest.skip(reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load' ) def _a ( self : Optional[int] ): """simple docstring""" pass @slow def _a ( self : Optional[Any] ): """simple docstring""" A__ = TFViTMAEModel.from_pretrained('google/vit-base-patch16-224' ) self.assertIsNotNone(_snake_case ) def A ( ) -> Dict: A__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_tf @require_vision class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @cached_property def _a ( self : List[str] ): """simple docstring""" return ViTImageProcessor.from_pretrained('facebook/vit-mae-base' ) if is_vision_available() else None @slow def _a ( self : Optional[int] ): """simple docstring""" np.random.seed(2 ) A__ = TFViTMAEForPreTraining.from_pretrained('facebook/vit-mae-base' ) A__ = self.default_image_processor A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='tf' ) # prepare a noise vector that will be also used for testing the TF model # (this way we can ensure that the PT and TF models operate on the same inputs) A__ = ViTMAEConfig() A__ = int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2 ) A__ = np.random.uniform(size=(1, num_patches) ) # forward pass A__ = model(**_snake_case , noise=_snake_case ) # verify the logits A__ = tf.convert_to_tensor([1, 1_96, 7_68] ) self.assertEqual(outputs.logits.shape , _snake_case ) A__ = tf.convert_to_tensor( [[-0.0548, -1.7023, -0.9325], [0.3721, -0.5670, -0.2233], [0.8235, -1.3878, -0.3524]] ) tf.debugging.assert_near(outputs.logits[0, :3, :3] , _snake_case , atol=1E-4 )
9
import warnings from typing import Any, Dict, List, Optional, Union import numpy as np from ...audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Optional[Any] = ["input_values", "attention_mask"] def __init__( self : str , _snake_case : int = 1 , _snake_case : int = 1_60_00 , _snake_case : float = 0.0 , _snake_case : bool = False , _snake_case : int = 80 , _snake_case : int = 16 , _snake_case : int = 64 , _snake_case : str = "hann_window" , _snake_case : float = 1.0 , _snake_case : float = 80 , _snake_case : float = 76_00 , _snake_case : float = 1E-10 , _snake_case : int = 2 , _snake_case : bool = True , **_snake_case : Union[str, Any] , ): """simple docstring""" super().__init__(feature_size=_snake_case , sampling_rate=_snake_case , padding_value=_snake_case , **_snake_case ) A__ = do_normalize A__ = return_attention_mask A__ = num_mel_bins A__ = hop_length A__ = win_length A__ = win_function A__ = frame_signal_scale A__ = fmin A__ = fmax A__ = mel_floor A__ = reduction_factor A__ = win_length * sampling_rate // 10_00 A__ = hop_length * sampling_rate // 10_00 A__ = optimal_fft_length(self.sample_size ) A__ = (self.n_fft // 2) + 1 A__ = window_function(window_length=self.sample_size , name=self.win_function , periodic=_snake_case ) A__ = mel_filter_bank( num_frequency_bins=self.n_freqs , num_mel_filters=self.num_mel_bins , min_frequency=self.fmin , max_frequency=self.fmax , sampling_rate=self.sampling_rate , norm='slaney' , mel_scale='slaney' , ) if frame_signal_scale != 1.0: warnings.warn( 'The argument `frame_signal_scale` is deprecated and will be removed in version 4.30.0 of Transformers' , _snake_case , ) if reduction_factor != 2.0: warnings.warn( 'The argument `reduction_factor` is deprecated and will be removed in version 4.30.0 of Transformers' , _snake_case , ) @staticmethod # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm def _a ( _snake_case : List[np.ndarray] , _snake_case : List[np.ndarray] , _snake_case : float = 0.0 ): """simple docstring""" if attention_mask is not None: A__ = np.array(_snake_case , np.intaa ) A__ = [] for vector, length in zip(_snake_case , attention_mask.sum(-1 ) ): A__ = (vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1E-7 ) if length < normed_slice.shape[0]: A__ = padding_value normed_input_values.append(_snake_case ) else: A__ = [(x - x.mean()) / np.sqrt(x.var() + 1E-7 ) for x in input_values] return normed_input_values def _a ( self : Tuple , _snake_case : np.ndarray , ): """simple docstring""" A__ = spectrogram( _snake_case , window=self.window , frame_length=self.sample_size , hop_length=self.sample_stride , fft_length=self.n_fft , mel_filters=self.mel_filters , mel_floor=self.mel_floor , log_mel='log10' , ) return log_mel_spec.T def __call__( self : List[str] , _snake_case : Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None , _snake_case : Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None , _snake_case : Union[bool, str, PaddingStrategy] = False , _snake_case : Optional[int] = None , _snake_case : bool = False , _snake_case : Optional[int] = None , _snake_case : Optional[bool] = None , _snake_case : Optional[Union[str, TensorType]] = None , _snake_case : Optional[int] = None , **_snake_case : Tuple , ): """simple docstring""" if audio is None and audio_target is None: raise ValueError('You must provide either `audio` or `audio_target` values.' ) if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' F''' {self.sampling_rate}. Please make sure that the provided audio input was sampled with''' F''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( 'It is strongly recommended to pass the ``sampling_rate`` argument to this function. ' 'Failing to do so can result in silent errors that might be hard to debug.' ) if audio is not None: A__ = self._process_audio( _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , **_snake_case , ) else: A__ = None if audio_target is not None: A__ = self._process_audio( _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , **_snake_case , ) if inputs is None: return inputs_target else: A__ = inputs_target['input_values'] A__ = inputs_target.get('attention_mask' ) if decoder_attention_mask is not None: A__ = decoder_attention_mask return inputs def _a ( self : Tuple , _snake_case : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , _snake_case : bool = False , _snake_case : Union[bool, str, PaddingStrategy] = False , _snake_case : Optional[int] = None , _snake_case : bool = False , _snake_case : Optional[int] = None , _snake_case : Optional[bool] = None , _snake_case : Optional[Union[str, TensorType]] = None , **_snake_case : Tuple , ): """simple docstring""" A__ = isinstance(_snake_case , np.ndarray ) and len(speech.shape ) > 1 if is_batched_numpy and len(speech.shape ) > 2: raise ValueError(F'''Only mono-channel audio is supported for input to {self}''' ) A__ = is_batched_numpy or ( isinstance(_snake_case , (list, tuple) ) and (isinstance(speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: A__ = [np.asarray(_snake_case , dtype=np.floataa ) for speech in speech] elif not is_batched and not isinstance(_snake_case , np.ndarray ): A__ = np.asarray(_snake_case , dtype=np.floataa ) elif isinstance(_snake_case , np.ndarray ) and speech.dtype is np.dtype(np.floataa ): A__ = speech.astype(np.floataa ) # always return batch if not is_batched: A__ = [speech] # needed to make pad() work on spectrogram inputs A__ = self.feature_size # convert into correct format for padding if is_target: A__ = [self._extract_mel_features(_snake_case ) for waveform in speech] A__ = BatchFeature({'input_values': features} ) A__ = self.num_mel_bins else: A__ = BatchFeature({'input_values': speech} ) A__ = self.pad( _snake_case , padding=_snake_case , max_length=_snake_case , truncation=_snake_case , pad_to_multiple_of=_snake_case , return_attention_mask=_snake_case , **_snake_case , ) A__ = feature_size_hack # convert input values to correct format A__ = padded_inputs['input_values'] if not isinstance(input_values[0] , np.ndarray ): A__ = [np.asarray(_snake_case , dtype=np.floataa ) for array in input_values] elif ( not isinstance(_snake_case , np.ndarray ) and isinstance(input_values[0] , np.ndarray ) and input_values[0].dtype is np.dtype(np.floataa ) ): A__ = [array.astype(np.floataa ) for array in input_values] elif isinstance(_snake_case , np.ndarray ) and input_values.dtype is np.dtype(np.floataa ): A__ = input_values.astype(np.floataa ) # convert attention_mask to correct format A__ = padded_inputs.get('attention_mask' ) if attention_mask is not None: A__ = [np.asarray(_snake_case , dtype=np.intaa ) for array in attention_mask] # zero-mean and unit-variance normalization if not is_target and self.do_normalize: A__ = ( attention_mask if self._get_padding_strategies(_snake_case , max_length=_snake_case ) is not PaddingStrategy.DO_NOT_PAD else None ) A__ = self.zero_mean_unit_var_norm( padded_inputs['input_values'] , attention_mask=_snake_case , padding_value=self.padding_value ) if return_tensors is not None: A__ = padded_inputs.convert_to_tensors(_snake_case ) return padded_inputs def _a ( self : Optional[Any] ): """simple docstring""" A__ = super().to_dict() # Don't serialize these as they are derived from the other properties. A__ = ['window', 'mel_filters', 'sample_size', 'sample_stride', 'n_fft', 'n_freqs'] for name in names: if name in output: del output[name] return output
9
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) SCREAMING_SNAKE_CASE__ = { '''configuration_convnext''': ['''CONVNEXT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ConvNextConfig''', '''ConvNextOnnxConfig'''] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = ['''ConvNextFeatureExtractor'''] SCREAMING_SNAKE_CASE__ = ['''ConvNextImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ConvNextForImageClassification''', '''ConvNextModel''', '''ConvNextPreTrainedModel''', '''ConvNextBackbone''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''TFConvNextForImageClassification''', '''TFConvNextModel''', '''TFConvNextPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_convnext import CONVNEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, ConvNextConfig, ConvNextOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_convnext import ConvNextFeatureExtractor from .image_processing_convnext import ConvNextImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_convnext import ( CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST, ConvNextBackbone, ConvNextForImageClassification, ConvNextModel, ConvNextPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_convnext import TFConvNextForImageClassification, TFConvNextModel, TFConvNextPreTrainedModel else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
9
import argparse import OmegaConf import torch from diffusers import DDIMScheduler, LDMPipeline, UNetLDMModel, VQModel def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: A__ = OmegaConf.load(__UpperCamelCase ) A__ = torch.load(__UpperCamelCase , map_location='cpu' )['model'] A__ = list(state_dict.keys() ) # extract state_dict for VQVAE A__ = {} A__ = 'first_stage_model.' for key in keys: if key.startswith(__UpperCamelCase ): A__ = state_dict[key] # extract state_dict for UNetLDM A__ = {} A__ = 'model.diffusion_model.' for key in keys: if key.startswith(__UpperCamelCase ): A__ = state_dict[key] A__ = config.model.params.first_stage_config.params A__ = config.model.params.unet_config.params A__ = VQModel(**__UpperCamelCase ).eval() vqvae.load_state_dict(__UpperCamelCase ) A__ = UNetLDMModel(**__UpperCamelCase ).eval() unet.load_state_dict(__UpperCamelCase ) A__ = DDIMScheduler( timesteps=config.model.params.timesteps , beta_schedule='scaled_linear' , beta_start=config.model.params.linear_start , beta_end=config.model.params.linear_end , clip_sample=__UpperCamelCase , ) A__ = LDMPipeline(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) pipeline.save_pretrained(__UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument('''--checkpoint_path''', type=str, required=True) parser.add_argument('''--config_path''', type=str, required=True) parser.add_argument('''--output_path''', type=str, required=True) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_ldm_original(args.checkpoint_path, args.config_path, args.output_path)
9
1
def A ( __UpperCamelCase ) -> int: if not isinstance(__UpperCamelCase , __UpperCamelCase ) or number < 0: raise ValueError('Input must be a non-negative integer' ) A__ = 0 while number: # This way we arrive at next set bit (next 1) instead of looping # through each bit and checking for 1s hence the # loop won't run 32 times it will only run the number of `1` times number &= number - 1 count += 1 return count if __name__ == "__main__": import doctest doctest.testmod()
9
import json import os import torch from diffusers import UNetaDModel os.makedirs('''hub/hopper-medium-v2/unet/hor32''', exist_ok=True) os.makedirs('''hub/hopper-medium-v2/unet/hor128''', exist_ok=True) os.makedirs('''hub/hopper-medium-v2/value_function''', exist_ok=True) def A ( __UpperCamelCase ) -> Union[str, Any]: if hor == 128: A__ = ('DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D') A__ = (32, 128, 256) A__ = ('UpResnetBlock1D', 'UpResnetBlock1D') elif hor == 32: A__ = ('DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D') A__ = (32, 64, 128, 256) A__ = ('UpResnetBlock1D', 'UpResnetBlock1D', 'UpResnetBlock1D') A__ = torch.load(f'''/Users/bglickenhaus/Documents/diffuser/temporal_unet-hopper-mediumv2-hor{hor}.torch''' ) A__ = model.state_dict() A__ = { 'down_block_types': down_block_types, 'block_out_channels': block_out_channels, 'up_block_types': up_block_types, 'layers_per_block': 1, 'use_timestep_embedding': True, 'out_block_type': 'OutConv1DBlock', 'norm_num_groups': 8, 'downsample_each_block': False, 'in_channels': 14, 'out_channels': 14, 'extra_in_channels': 0, 'time_embedding_type': 'positional', 'flip_sin_to_cos': False, 'freq_shift': 1, 'sample_size': 65_536, 'mid_block_type': 'MidResTemporalBlock1D', 'act_fn': 'mish', } A__ = UNetaDModel(**__UpperCamelCase ) print(f'''length of state dict: {len(state_dict.keys() )}''' ) print(f'''length of value function dict: {len(hf_value_function.state_dict().keys() )}''' ) A__ = dict(zip(model.state_dict().keys() , hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): A__ = state_dict.pop(__UpperCamelCase ) hf_value_function.load_state_dict(__UpperCamelCase ) torch.save(hf_value_function.state_dict() , f'''hub/hopper-medium-v2/unet/hor{hor}/diffusion_pytorch_model.bin''' ) with open(f'''hub/hopper-medium-v2/unet/hor{hor}/config.json''' , 'w' ) as f: json.dump(__UpperCamelCase , __UpperCamelCase ) def A ( ) -> List[str]: A__ = { 'in_channels': 14, 'down_block_types': ('DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D'), 'up_block_types': (), 'out_block_type': 'ValueFunction', 'mid_block_type': 'ValueFunctionMidBlock1D', 'block_out_channels': (32, 64, 128, 256), 'layers_per_block': 1, 'downsample_each_block': True, 'sample_size': 65_536, 'out_channels': 14, 'extra_in_channels': 0, 'time_embedding_type': 'positional', 'use_timestep_embedding': True, 'flip_sin_to_cos': False, 'freq_shift': 1, 'norm_num_groups': 8, 'act_fn': 'mish', } A__ = torch.load('/Users/bglickenhaus/Documents/diffuser/value_function-hopper-mediumv2-hor32.torch' ) A__ = model A__ = UNetaDModel(**__UpperCamelCase ) print(f'''length of state dict: {len(state_dict.keys() )}''' ) print(f'''length of value function dict: {len(hf_value_function.state_dict().keys() )}''' ) A__ = dict(zip(state_dict.keys() , hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): A__ = state_dict.pop(__UpperCamelCase ) hf_value_function.load_state_dict(__UpperCamelCase ) torch.save(hf_value_function.state_dict() , 'hub/hopper-medium-v2/value_function/diffusion_pytorch_model.bin' ) with open('hub/hopper-medium-v2/value_function/config.json' , 'w' ) as f: json.dump(__UpperCamelCase , __UpperCamelCase ) if __name__ == "__main__": unet(3_2) # unet(128) value_function()
9
1
def A ( __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: A__ = 0 A__ = len(__UpperCamelCase ) - 1 while left <= right: # avoid divided by 0 during interpolation if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: return left else: return None A__ = left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left] ) # out of range check if point < 0 or point >= len(__UpperCamelCase ): return None A__ = sorted_collection[point] if current_item == item: return point else: if point < left: A__ = left A__ = point elif point > right: A__ = right A__ = point else: if item < current_item: A__ = point - 1 else: A__ = point + 1 return None def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: # avoid divided by 0 during interpolation if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: return left else: return None A__ = left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left] ) # out of range check if point < 0 or point >= len(__UpperCamelCase ): return None if sorted_collection[point] == item: return point elif point < left: return interpolation_search_by_recursion(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) elif point > right: return interpolation_search_by_recursion(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) else: if sorted_collection[point] > item: return interpolation_search_by_recursion( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , point - 1 ) else: return interpolation_search_by_recursion( __UpperCamelCase , __UpperCamelCase , point + 1 , __UpperCamelCase ) def A ( __UpperCamelCase ) -> List[str]: if collection != sorted(__UpperCamelCase ): raise ValueError('Collection must be ascending sorted' ) return True if __name__ == "__main__": import sys SCREAMING_SNAKE_CASE__ = 0 if debug == 1: SCREAMING_SNAKE_CASE__ = [1_0, 3_0, 4_0, 4_5, 5_0, 6_6, 7_7, 9_3] try: __assert_sorted(collection) except ValueError: sys.exit('''Sequence must be ascending sorted to apply interpolation search''') SCREAMING_SNAKE_CASE__ = 6_7 SCREAMING_SNAKE_CASE__ = interpolation_search(collection, target) if result is not None: print(f'{target} found at positions: {result}') else: print('''Not found''')
9
from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class __lowerCAmelCase : """simple docstring""" def __init__( self : Dict , _snake_case : Union[str, Any] , _snake_case : Optional[Any]=12 , _snake_case : Any=7 , _snake_case : List[str]=True , _snake_case : int=True , _snake_case : int=True , _snake_case : Tuple=99 , _snake_case : List[Any]=32 , _snake_case : Optional[int]=32 , _snake_case : List[str]=2 , _snake_case : List[str]=4 , _snake_case : List[Any]=37 , _snake_case : Union[str, Any]=0.1 , _snake_case : Tuple=0.1 , _snake_case : Dict=5_12 , _snake_case : Union[str, Any]=0.02 , _snake_case : Any=0 , _snake_case : Optional[Any]=None , ): """simple docstring""" A__ = parent A__ = batch_size A__ = seq_length A__ = is_training A__ = use_input_mask A__ = use_labels A__ = vocab_size A__ = hidden_size A__ = projection_dim A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = dropout A__ = attention_dropout A__ = max_position_embeddings A__ = initializer_range A__ = scope A__ = bos_token_id def _a ( self : Optional[Any] ): """simple docstring""" A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = None if self.use_input_mask: A__ = random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: A__ = input_mask.numpy() A__ , A__ = input_mask.shape A__ = np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(_snake_case ): A__ = 1 A__ = 0 A__ = self.get_config() return config, input_ids, tf.convert_to_tensor(_snake_case ) def _a ( self : Tuple ): """simple docstring""" return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def _a ( self : int , _snake_case : Union[str, Any] , _snake_case : Any , _snake_case : List[str] ): """simple docstring""" A__ = TFBlipTextModel(config=_snake_case ) A__ = model(_snake_case , attention_mask=_snake_case , training=_snake_case ) A__ = model(_snake_case , training=_snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def _a ( self : str ): """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ = config_and_inputs A__ = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_tf class __lowerCAmelCase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Tuple = (TFBlipTextModel,) if is_tf_available() else () A__ : Optional[int] = False A__ : Union[str, Any] = False A__ : Union[str, Any] = False def _a ( self : Any ): """simple docstring""" A__ = BlipTextModelTester(self ) A__ = ConfigTester(self , config_class=_snake_case , hidden_size=37 ) def _a ( self : List[str] ): """simple docstring""" self.config_tester.run_common_tests() def _a ( self : Union[str, Any] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def _a ( self : Tuple ): """simple docstring""" pass def _a ( self : int ): """simple docstring""" pass @unittest.skip(reason='Blip does not use inputs_embeds' ) def _a ( self : Any ): """simple docstring""" pass @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING' ) def _a ( self : str ): """simple docstring""" pass @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING' ) def _a ( self : Optional[Any] ): """simple docstring""" pass @slow def _a ( self : Union[str, Any] ): """simple docstring""" for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = TFBlipTextModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def _a ( self : int , _snake_case : int=True ): """simple docstring""" super().test_pt_tf_model_equivalence(allow_missing_keys=_snake_case )
9
1
from __future__ import annotations from collections import deque from collections.abc import Iterator from dataclasses import dataclass @dataclass class __lowerCAmelCase : """simple docstring""" A__ : int A__ : int class __lowerCAmelCase : """simple docstring""" def __init__( self : Any , _snake_case : int ): """simple docstring""" A__ = [[] for _ in range(_snake_case )] A__ = size def __getitem__( self : Union[str, Any] , _snake_case : int ): """simple docstring""" return iter(self._graph[vertex] ) @property def _a ( self : Any ): """simple docstring""" return self._size def _a ( self : Optional[int] , _snake_case : int , _snake_case : int , _snake_case : int ): """simple docstring""" if weight not in (0, 1): raise ValueError('Edge weight must be either 0 or 1.' ) if to_vertex < 0 or to_vertex >= self.size: raise ValueError('Vertex indexes must be in [0; size).' ) self._graph[from_vertex].append(Edge(_snake_case , _snake_case ) ) def _a ( self : int , _snake_case : int , _snake_case : int ): """simple docstring""" A__ = deque([start_vertex] ) A__ = [None] * self.size A__ = 0 while queue: A__ = queue.popleft() A__ = distances[current_vertex] if current_distance is None: continue for edge in self[current_vertex]: A__ = current_distance + edge.weight A__ = distances[edge.destination_vertex] if ( isinstance(_snake_case , _snake_case ) and new_distance >= dest_vertex_distance ): continue A__ = new_distance if edge.weight == 0: queue.appendleft(edge.destination_vertex ) else: queue.append(edge.destination_vertex ) if distances[finish_vertex] is None: raise ValueError('No path from start_vertex to finish_vertex.' ) return distances[finish_vertex] if __name__ == "__main__": import doctest doctest.testmod()
9
from __future__ import annotations from typing import Any def A ( __UpperCamelCase ) -> int: if not postfix_notation: return 0 A__ = {'+', '-', '*', '/'} A__ = [] for token in postfix_notation: if token in operations: A__ , A__ = stack.pop(), stack.pop() if token == "+": stack.append(a + b ) elif token == "-": stack.append(a - b ) elif token == "*": stack.append(a * b ) else: if a * b < 0 and a % b != 0: stack.append(a // b + 1 ) else: stack.append(a // b ) else: stack.append(int(__UpperCamelCase ) ) return stack.pop() if __name__ == "__main__": import doctest doctest.testmod()
9
1
from collections import UserDict from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) @add_end_docstrings(UpperCAmelCase_ ) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : int , **_snake_case : Dict ): """simple docstring""" super().__init__(**_snake_case ) requires_backends(self , 'vision' ) self.check_model_type( TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if self.framework == 'tf' else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING ) def __call__( self : Dict , _snake_case : Union[str, List[str], "Image", List["Image"]] , **_snake_case : Optional[int] ): """simple docstring""" return super().__call__(_snake_case , **_snake_case ) def _a ( self : Tuple , **_snake_case : Union[str, Any] ): """simple docstring""" A__ = {} if "candidate_labels" in kwargs: A__ = kwargs['candidate_labels'] if "hypothesis_template" in kwargs: A__ = kwargs['hypothesis_template'] return preprocess_params, {}, {} def _a ( self : Union[str, Any] , _snake_case : Union[str, Any] , _snake_case : int=None , _snake_case : Optional[Any]="This is a photo of {}." ): """simple docstring""" A__ = load_image(_snake_case ) A__ = self.image_processor(images=[image] , return_tensors=self.framework ) A__ = candidate_labels A__ = [hypothesis_template.format(_snake_case ) for x in candidate_labels] A__ = self.tokenizer(_snake_case , return_tensors=self.framework , padding=_snake_case ) A__ = [text_inputs] return inputs def _a ( self : Optional[int] , _snake_case : int ): """simple docstring""" A__ = model_inputs.pop('candidate_labels' ) A__ = model_inputs.pop('text_inputs' ) if isinstance(text_inputs[0] , _snake_case ): A__ = text_inputs[0] else: # Batching case. A__ = text_inputs[0][0] A__ = self.model(**_snake_case , **_snake_case ) A__ = { 'candidate_labels': candidate_labels, 'logits': outputs.logits_per_image, } return model_outputs def _a ( self : List[Any] , _snake_case : List[Any] ): """simple docstring""" A__ = model_outputs.pop('candidate_labels' ) A__ = model_outputs['logits'][0] if self.framework == "pt": A__ = logits.softmax(dim=-1 ).squeeze(-1 ) A__ = probs.tolist() if not isinstance(_snake_case , _snake_case ): A__ = [scores] elif self.framework == "tf": A__ = stable_softmax(_snake_case , axis=-1 ) A__ = probs.numpy().tolist() else: raise ValueError(F'''Unsupported framework: {self.framework}''' ) A__ = [ {'score': score, 'label': candidate_label} for score, candidate_label in sorted(zip(_snake_case , _snake_case ) , key=lambda _snake_case : -x[0] ) ] return result
9
from __future__ import annotations def A ( __UpperCamelCase = 4 ) -> list[list[int]]: A__ = abs(__UpperCamelCase ) or 4 return [[1 + x + y * row_size for x in range(__UpperCamelCase )] for y in range(__UpperCamelCase )] def A ( __UpperCamelCase ) -> list[list[int]]: return reverse_row(transpose(__UpperCamelCase ) ) # OR.. transpose(reverse_column(matrix)) def A ( __UpperCamelCase ) -> list[list[int]]: return reverse_row(reverse_column(__UpperCamelCase ) ) # OR.. reverse_column(reverse_row(matrix)) def A ( __UpperCamelCase ) -> list[list[int]]: return reverse_column(transpose(__UpperCamelCase ) ) # OR.. transpose(reverse_row(matrix)) def A ( __UpperCamelCase ) -> list[list[int]]: A__ = [list(__UpperCamelCase ) for x in zip(*__UpperCamelCase )] return matrix def A ( __UpperCamelCase ) -> list[list[int]]: A__ = matrix[::-1] return matrix def A ( __UpperCamelCase ) -> list[list[int]]: A__ = [x[::-1] for x in matrix] return matrix def A ( __UpperCamelCase ) -> None: for i in matrix: print(*__UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = make_matrix() print('''\norigin:\n''') print_matrix(matrix) print('''\nrotate 90 counterclockwise:\n''') print_matrix(rotate_aa(matrix)) SCREAMING_SNAKE_CASE__ = make_matrix() print('''\norigin:\n''') print_matrix(matrix) print('''\nrotate 180:\n''') print_matrix(rotate_aaa(matrix)) SCREAMING_SNAKE_CASE__ = make_matrix() print('''\norigin:\n''') print_matrix(matrix) print('''\nrotate 270 counterclockwise:\n''') print_matrix(rotate_aaa(matrix))
9
1
from __future__ import annotations from typing import Any def A ( __UpperCamelCase ) -> int: if not postfix_notation: return 0 A__ = {'+', '-', '*', '/'} A__ = [] for token in postfix_notation: if token in operations: A__ , A__ = stack.pop(), stack.pop() if token == "+": stack.append(a + b ) elif token == "-": stack.append(a - b ) elif token == "*": stack.append(a * b ) else: if a * b < 0 and a % b != 0: stack.append(a // b + 1 ) else: stack.append(a // b ) else: stack.append(int(__UpperCamelCase ) ) return stack.pop() if __name__ == "__main__": import doctest doctest.testmod()
9
from __future__ import annotations from fractions import Fraction def A ( __UpperCamelCase , __UpperCamelCase ) -> bool: return ( num != den and num % 10 == den // 10 and (num // 10) / (den % 10) == num / den ) def A ( __UpperCamelCase ) -> list[str]: A__ = [] A__ = 11 A__ = int('1' + '0' * digit_len ) for num in range(__UpperCamelCase , __UpperCamelCase ): while den <= 99: if (num != den) and (num % 10 == den // 10) and (den % 10 != 0): if is_digit_cancelling(__UpperCamelCase , __UpperCamelCase ): solutions.append(f'''{num}/{den}''' ) den += 1 num += 1 A__ = 10 return solutions def A ( __UpperCamelCase = 2 ) -> int: A__ = 1.0 for fraction in fraction_list(__UpperCamelCase ): A__ = Fraction(__UpperCamelCase ) result *= frac.denominator / frac.numerator return int(__UpperCamelCase ) if __name__ == "__main__": print(solution())
9
1
import os # Precomputes a list of the 100 first triangular numbers SCREAMING_SNAKE_CASE__ = [int(0.5 * n * (n + 1)) for n in range(1, 1_0_1)] def A ( ) -> List[str]: A__ = os.path.dirname(os.path.realpath(__UpperCamelCase ) ) A__ = os.path.join(__UpperCamelCase , 'words.txt' ) A__ = '' with open(__UpperCamelCase ) as f: A__ = f.readline() A__ = [word.strip('"' ) for word in words.strip('\r\n' ).split(',' )] A__ = [ word for word in [sum(ord(__UpperCamelCase ) - 64 for x in word ) for word in words] if word in TRIANGULAR_NUMBERS ] return len(__UpperCamelCase ) if __name__ == "__main__": print(solution())
9
# 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_tokenizers_available, is_torch_available SCREAMING_SNAKE_CASE__ = {'''configuration_mra''': ['''MRA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MraConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''MRA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MraForMaskedLM''', '''MraForMultipleChoice''', '''MraForQuestionAnswering''', '''MraForSequenceClassification''', '''MraForTokenClassification''', '''MraLayer''', '''MraModel''', '''MraPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_mra import MRA_PRETRAINED_CONFIG_ARCHIVE_MAP, MraConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mra import ( MRA_PRETRAINED_MODEL_ARCHIVE_LIST, MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, MraLayer, MraModel, MraPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
9
1
import unittest from typing import Tuple import torch from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device from diffusers.utils.testing_utils import require_torch @require_torch class __lowerCAmelCase : """simple docstring""" @property def _a ( self : Tuple ): """simple docstring""" return self.get_dummy_input() @property def _a ( self : List[str] ): """simple docstring""" if self.block_type == "down": return (4, 32, 16, 16) elif self.block_type == "mid": return (4, 32, 32, 32) elif self.block_type == "up": return (4, 32, 64, 64) raise ValueError(F'''\'{self.block_type}\' is not a supported block_type. Set it to \'up\', \'mid\', or \'down\'.''' ) def _a ( self : List[Any] , _snake_case : List[Any]=True , _snake_case : int=False , _snake_case : List[str]=False , _snake_case : str=False , ): """simple docstring""" A__ = 4 A__ = 32 A__ = (32, 32) A__ = torch.manual_seed(0 ) A__ = torch.device(_snake_case ) A__ = (batch_size, num_channels) + sizes A__ = randn_tensor(_snake_case , generator=_snake_case , device=_snake_case ) A__ = {'hidden_states': hidden_states} if include_temb: A__ = 1_28 A__ = randn_tensor((batch_size, temb_channels) , generator=_snake_case , device=_snake_case ) if include_res_hidden_states_tuple: A__ = torch.manual_seed(1 ) A__ = (randn_tensor(_snake_case , generator=_snake_case , device=_snake_case ),) if include_encoder_hidden_states: A__ = floats_tensor((batch_size, 32, 32) ).to(_snake_case ) if include_skip_sample: A__ = randn_tensor(((batch_size, 3) + sizes) , generator=_snake_case , device=_snake_case ) return dummy_input def _a ( self : Dict ): """simple docstring""" A__ = { 'in_channels': 32, 'out_channels': 32, 'temb_channels': 1_28, } if self.block_type == "up": A__ = 32 if self.block_type == "mid": init_dict.pop('out_channels' ) A__ = self.dummy_input return init_dict, inputs_dict def _a ( self : Tuple , _snake_case : Optional[int] ): """simple docstring""" A__ , A__ = self.prepare_init_args_and_inputs_for_common() A__ = self.block_class(**_snake_case ) unet_block.to(_snake_case ) unet_block.eval() with torch.no_grad(): A__ = unet_block(**_snake_case ) if isinstance(_snake_case , _snake_case ): A__ = output[0] self.assertEqual(output.shape , self.output_shape ) A__ = output[0, -1, -3:, -3:] A__ = torch.tensor(_snake_case ).to(_snake_case ) assert torch_all_close(output_slice.flatten() , _snake_case , atol=5E-3 ) @unittest.skipIf(torch_device == 'mps' , 'Training is not supported in mps' ) def _a ( self : Union[str, Any] ): """simple docstring""" A__ , A__ = self.prepare_init_args_and_inputs_for_common() A__ = self.block_class(**_snake_case ) model.to(_snake_case ) model.train() A__ = model(**_snake_case ) if isinstance(_snake_case , _snake_case ): A__ = output[0] A__ = torch.device(_snake_case ) A__ = randn_tensor(output.shape , device=_snake_case ) A__ = torch.nn.functional.mse_loss(_snake_case , _snake_case ) loss.backward()
9
SCREAMING_SNAKE_CASE__ = ''' # Transformers installation ! pip install transformers datasets # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git ''' SCREAMING_SNAKE_CASE__ = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] SCREAMING_SNAKE_CASE__ = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
9
1
from .configuration_bert_masked import MaskedBertConfig from .modeling_bert_masked import ( MaskedBertForMultipleChoice, MaskedBertForQuestionAnswering, MaskedBertForSequenceClassification, MaskedBertForTokenClassification, MaskedBertModel, ) from .modules import *
9
import unittest from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __lowerCAmelCase : """simple docstring""" @staticmethod def _a ( *_snake_case : Any , **_snake_case : Optional[int] ): """simple docstring""" pass @is_pipeline_test @require_torch @require_vision class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" A__ : Union[str, Any] = MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING def _a ( self : List[Any] , _snake_case : Union[str, Any] , _snake_case : Tuple , _snake_case : Union[str, Any] ): """simple docstring""" A__ = pipeline('visual-question-answering' , model='hf-internal-testing/tiny-vilt-random-vqa' ) A__ = [ { 'image': Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ), 'question': 'How many cats are there?', }, { 'image': './tests/fixtures/tests_samples/COCO/000000039769.png', 'question': 'How many cats are there?', }, ] return vqa_pipeline, examples def _a ( self : Optional[Any] , _snake_case : Union[str, Any] , _snake_case : List[str] ): """simple docstring""" A__ = vqa_pipeline(_snake_case , top_k=1 ) self.assertEqual( _snake_case , [ [{'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}], [{'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}], ] , ) @require_torch def _a ( self : Any ): """simple docstring""" A__ = pipeline('visual-question-answering' , model='hf-internal-testing/tiny-vilt-random-vqa' ) A__ = './tests/fixtures/tests_samples/COCO/000000039769.png' A__ = 'How many cats are there?' A__ = vqa_pipeline(image=_snake_case , question='How many cats are there?' , top_k=2 ) self.assertEqual( _snake_case , [{'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}, {'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}] ) A__ = vqa_pipeline({'image': image, 'question': question} , top_k=2 ) self.assertEqual( _snake_case , [{'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}, {'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}] ) @slow @require_torch def _a ( self : Any ): """simple docstring""" A__ = pipeline('visual-question-answering' , model='dandelin/vilt-b32-finetuned-vqa' ) A__ = './tests/fixtures/tests_samples/COCO/000000039769.png' A__ = 'How many cats are there?' A__ = vqa_pipeline(image=_snake_case , question=_snake_case , top_k=2 ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [{'score': 0.8799, 'answer': '2'}, {'score': 0.296, 'answer': '1'}] ) A__ = vqa_pipeline({'image': image, 'question': question} , top_k=2 ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [{'score': 0.8799, 'answer': '2'}, {'score': 0.296, 'answer': '1'}] ) A__ = vqa_pipeline( [{'image': image, 'question': question}, {'image': image, 'question': question}] , top_k=2 ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [[{'score': 0.8799, 'answer': '2'}, {'score': 0.296, 'answer': '1'}]] * 2 , ) @require_tf @unittest.skip('Visual question answering not implemented in TF' ) def _a ( self : Dict ): """simple docstring""" pass
9
1
import shutil import tempfile import unittest from transformers import ( SPIECE_UNDERLINE, AddedToken, BatchEncoding, NllbTokenizer, NllbTokenizerFast, is_torch_available, ) from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, ) from ...test_tokenization_common import TokenizerTesterMixin SCREAMING_SNAKE_CASE__ = get_tests_dir('''fixtures/test_sentencepiece.model''') if is_torch_available(): from transformers.models.mam_aaa.modeling_mam_aaa import shift_tokens_right SCREAMING_SNAKE_CASE__ = 2_5_6_0_4_7 SCREAMING_SNAKE_CASE__ = 2_5_6_1_4_5 @require_sentencepiece @require_tokenizers class __lowerCAmelCase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : int = NllbTokenizer A__ : Optional[int] = NllbTokenizerFast A__ : Optional[int] = True A__ : Tuple = True A__ : Optional[int] = {} def _a ( self : Any ): """simple docstring""" super().setUp() # We have a SentencePiece fixture for testing A__ = NllbTokenizer(_snake_case , keep_accents=_snake_case ) tokenizer.save_pretrained(self.tmpdirname ) def _a ( self : Optional[int] ): """simple docstring""" A__ = NllbTokenizer(_snake_case , keep_accents=_snake_case ) A__ = tokenizer.tokenize('This is a test' ) self.assertListEqual(_snake_case , ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_snake_case ) , [value + tokenizer.fairseq_offset for value in [2_85, 46, 10, 1_70, 3_82]] , ) A__ = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( _snake_case , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '9', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', 'é', '.', ] , ) A__ = tokenizer.convert_tokens_to_ids(_snake_case ) self.assertListEqual( _snake_case , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 6_02, 3_47, 3_47, 3_47, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) A__ = tokenizer.convert_ids_to_tokens(_snake_case ) self.assertListEqual( _snake_case , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '<unk>', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', '<unk>', '.', ] , ) def _a ( self : Any ): """simple docstring""" A__ = (self.rust_tokenizer_class, 'hf-internal-testing/tiny-random-nllb', {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): A__ = self.rust_tokenizer_class.from_pretrained(_snake_case , **_snake_case ) A__ = self.tokenizer_class.from_pretrained(_snake_case , **_snake_case ) A__ = tempfile.mkdtemp() A__ = tokenizer_r.save_pretrained(_snake_case ) A__ = tokenizer_p.save_pretrained(_snake_case ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any('tokenizer.json' in f for f in tokenizer_r_files ) ) A__ = tuple(f for f in tokenizer_r_files if 'tokenizer.json' not in f ) self.assertSequenceEqual(_snake_case , _snake_case ) # Checks everything loads correctly in the same way A__ = tokenizer_r.from_pretrained(_snake_case ) A__ = tokenizer_p.from_pretrained(_snake_case ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(_snake_case , _snake_case ) ) shutil.rmtree(_snake_case ) # Save tokenizer rust, legacy_format=True A__ = tempfile.mkdtemp() A__ = tokenizer_r.save_pretrained(_snake_case , legacy_format=_snake_case ) A__ = tokenizer_p.save_pretrained(_snake_case ) # Checks it save with the same files self.assertSequenceEqual(_snake_case , _snake_case ) # Checks everything loads correctly in the same way A__ = tokenizer_r.from_pretrained(_snake_case ) A__ = tokenizer_p.from_pretrained(_snake_case ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(_snake_case , _snake_case ) ) shutil.rmtree(_snake_case ) # Save tokenizer rust, legacy_format=False A__ = tempfile.mkdtemp() A__ = tokenizer_r.save_pretrained(_snake_case , legacy_format=_snake_case ) A__ = tokenizer_p.save_pretrained(_snake_case ) # Checks it saved the tokenizer.json file self.assertTrue(any('tokenizer.json' in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way A__ = tokenizer_r.from_pretrained(_snake_case ) A__ = tokenizer_p.from_pretrained(_snake_case ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(_snake_case , _snake_case ) ) shutil.rmtree(_snake_case ) @require_torch def _a ( self : str ): """simple docstring""" if not self.test_seqaseq: return A__ = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F'''{tokenizer.__class__.__name__}''' ): # Longer text that will definitely require truncation. A__ = [ ' UN Chief Says There Is No Military Solution in Syria', ' Secretary-General Ban Ki-moon says his response to Russia\'s stepped up military support for' ' Syria is that \'there is no military solution\' to the nearly five-year conflict and more weapons' ' will only worsen the violence and misery for millions of people.', ] A__ = [ 'Şeful ONU declară că nu există o soluţie militară în Siria', 'Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al' ' Rusiei pentru Siria este că "nu există o soluţie militară" la conflictul de aproape cinci ani şi' ' că noi arme nu vor face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.', ] try: A__ = tokenizer.prepare_seqaseq_batch( src_texts=_snake_case , tgt_texts=_snake_case , max_length=3 , max_target_length=10 , return_tensors='pt' , src_lang='eng_Latn' , tgt_lang='ron_Latn' , ) except NotImplementedError: return self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.labels.shape[1] , 10 ) # max_target_length will default to max_length if not specified A__ = tokenizer.prepare_seqaseq_batch( _snake_case , tgt_texts=_snake_case , max_length=3 , return_tensors='pt' ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.labels.shape[1] , 3 ) A__ = tokenizer.prepare_seqaseq_batch( src_texts=_snake_case , max_length=3 , max_target_length=10 , return_tensors='pt' ) self.assertEqual(batch_encoder_only.input_ids.shape[1] , 3 ) self.assertEqual(batch_encoder_only.attention_mask.shape[1] , 3 ) self.assertNotIn('decoder_input_ids' , _snake_case ) @unittest.skip('Unfortunately way too slow to build a BPE with SentencePiece.' ) def _a ( self : Dict ): """simple docstring""" pass def _a ( self : List[Any] ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): A__ = [AddedToken('<special>' , lstrip=_snake_case )] A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , additional_special_tokens=_snake_case , **_snake_case ) A__ = tokenizer_r.encode('Hey this is a <special> token' ) A__ = tokenizer_r.encode('<special>' , add_special_tokens=_snake_case )[0] self.assertTrue(special_token_id in r_output ) if self.test_slow_tokenizer: A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , additional_special_tokens=_snake_case , **_snake_case , ) A__ = self.tokenizer_class.from_pretrained( _snake_case , additional_special_tokens=_snake_case , **_snake_case ) A__ = tokenizer_p.encode('Hey this is a <special> token' ) A__ = tokenizer_cr.encode('Hey this is a <special> token' ) self.assertEqual(_snake_case , _snake_case ) self.assertEqual(_snake_case , _snake_case ) self.assertTrue(special_token_id in p_output ) self.assertTrue(special_token_id in cr_output ) @require_torch @require_sentencepiece @require_tokenizers class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" A__ : int = "facebook/nllb-200-distilled-600M" A__ : List[Any] = [ " UN Chief Says There Is No Military Solution in Syria", " Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for Syria is that \"there is no military solution\" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.", ] A__ : Dict = [ "Şeful ONU declară că nu există o soluţie militară în Siria", "Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei" " pentru Siria este că \"nu există o soluţie militară\" la conflictul de aproape cinci ani şi că noi arme nu vor" " face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.", ] A__ : List[Any] = [ 25_60_47, 1_62_97, 13_44_08, 81_65, 24_80_66, 1_47_34, 9_50, 11_35, 10_57_21, 35_73, 83, 2_73_52, 1_08, 4_94_86, 2, ] @classmethod def _a ( cls : Tuple ): """simple docstring""" A__ = NllbTokenizer.from_pretrained( cls.checkpoint_name , src_lang='eng_Latn' , tgt_lang='ron_Latn' ) A__ = 1 return cls def _a ( self : Optional[Any] ): """simple docstring""" self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['ace_Arab'] , 25_60_01 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['ace_Latn'] , 25_60_02 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['fra_Latn'] , 25_60_57 ) def _a ( self : str ): """simple docstring""" A__ = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , _snake_case ) def _a ( self : Tuple ): """simple docstring""" self.assertIn(_snake_case , self.tokenizer.all_special_ids ) # fmt: off A__ = [RO_CODE, 42_54, 9_80_68, 11_29_23, 3_90_72, 39_09, 7_13, 10_27_67, 26, 1_73_14, 3_56_42, 1_46_83, 3_31_18, 20_22, 6_69_87, 2, 25_60_47] # fmt: on A__ = self.tokenizer.decode(_snake_case , skip_special_tokens=_snake_case ) A__ = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=_snake_case ) self.assertEqual(_snake_case , _snake_case ) self.assertNotIn(self.tokenizer.eos_token , _snake_case ) def _a ( self : Dict ): """simple docstring""" A__ = ['this is gunna be a long sentence ' * 20] assert isinstance(src_text[0] , _snake_case ) A__ = 10 A__ = self.tokenizer(_snake_case , max_length=_snake_case , truncation=_snake_case ).input_ids[0] self.assertEqual(ids[-1] , 2 ) self.assertEqual(ids[0] , _snake_case ) self.assertEqual(len(_snake_case ) , _snake_case ) def _a ( self : str ): """simple docstring""" self.assertListEqual(self.tokenizer.convert_tokens_to_ids(['<mask>', 'ar_AR'] ) , [25_62_03, 3] ) def _a ( self : Union[str, Any] ): """simple docstring""" A__ = tempfile.mkdtemp() A__ = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(_snake_case ) A__ = NllbTokenizer.from_pretrained(_snake_case ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , _snake_case ) @require_torch def _a ( self : Union[str, Any] ): """simple docstring""" A__ = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=_snake_case , truncation=_snake_case , max_length=len(self.expected_src_tokens ) , return_tensors='pt' , ) A__ = shift_tokens_right( batch['labels'] , self.tokenizer.pad_token_id , self.tokenizer.lang_code_to_id['ron_Latn'] ) self.assertIsInstance(_snake_case , _snake_case ) self.assertEqual((2, 15) , batch.input_ids.shape ) self.assertEqual((2, 15) , batch.attention_mask.shape ) A__ = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , _snake_case ) self.assertEqual(_snake_case , batch.decoder_input_ids[0, 0] ) # EOS # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [EN_CODE] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) def _a ( self : List[Any] ): """simple docstring""" A__ = self.tokenizer(self.src_text , padding=_snake_case , truncation=_snake_case , max_length=3 , return_tensors='pt' ) A__ = self.tokenizer( text_target=self.tgt_text , padding=_snake_case , truncation=_snake_case , max_length=10 , return_tensors='pt' ) A__ = targets['input_ids'] A__ = shift_tokens_right( _snake_case , self.tokenizer.pad_token_id , decoder_start_token_id=self.tokenizer.lang_code_to_id[self.tokenizer.tgt_lang] , ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.decoder_input_ids.shape[1] , 10 ) @require_torch def _a ( self : str ): """simple docstring""" A__ = self.tokenizer._build_translation_inputs( 'A test' , return_tensors='pt' , src_lang='eng_Latn' , tgt_lang='fra_Latn' ) self.assertEqual( nested_simplify(_snake_case ) , { # A, test, EOS, en_XX 'input_ids': [[25_60_47, 70, 73_56, 2]], 'attention_mask': [[1, 1, 1, 1]], # ar_AR 'forced_bos_token_id': 25_60_57, } , ) @require_torch def _a ( self : List[str] ): """simple docstring""" A__ = True A__ = self.tokenizer( 'UN Chief says there is no military solution in Syria' , src_lang='eng_Latn' , tgt_lang='fra_Latn' ) self.assertEqual( inputs.input_ids , [1_62_97, 13_44_08, 2_56_53, 63_70, 2_48, 2_54, 10_39_29, 9_49_95, 1_08, 4_94_86, 2, 25_60_47] ) A__ = False A__ = self.tokenizer( 'UN Chief says there is no military solution in Syria' , src_lang='eng_Latn' , tgt_lang='fra_Latn' ) self.assertEqual( inputs.input_ids , [25_60_47, 1_62_97, 13_44_08, 2_56_53, 63_70, 2_48, 2_54, 10_39_29, 9_49_95, 1_08, 4_94_86, 2] )
9
def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: if exponent == 1: return base if exponent % 2 == 0: A__ = _modexpt(__UpperCamelCase , exponent // 2 , __UpperCamelCase ) % modulo_value return (x * x) % modulo_value else: return (base * _modexpt(__UpperCamelCase , exponent - 1 , __UpperCamelCase )) % modulo_value def A ( __UpperCamelCase = 1_777 , __UpperCamelCase = 1_855 , __UpperCamelCase = 8 ) -> int: A__ = base for _ in range(1 , __UpperCamelCase ): A__ = _modexpt(__UpperCamelCase , __UpperCamelCase , 10**digits ) return result if __name__ == "__main__": print(f'{solution() = }')
9
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) SCREAMING_SNAKE_CASE__ = { '''configuration_encodec''': [ '''ENCODEC_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''EncodecConfig''', ], '''feature_extraction_encodec''': ['''EncodecFeatureExtractor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''ENCODEC_PRETRAINED_MODEL_ARCHIVE_LIST''', '''EncodecModel''', '''EncodecPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_encodec import ( ENCODEC_PRETRAINED_CONFIG_ARCHIVE_MAP, EncodecConfig, ) from .feature_extraction_encodec import EncodecFeatureExtractor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_encodec import ( ENCODEC_PRETRAINED_MODEL_ARCHIVE_LIST, EncodecModel, EncodecPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
9
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() SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) def A ( __UpperCamelCase , __UpperCamelCase=False , __UpperCamelCase=False ) -> Dict: A__ = 'backbone.' if is_semantic else '' A__ = [] 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 A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=False , __UpperCamelCase=False ) -> Optional[Any]: for i in range(config.num_hidden_layers ): A__ = 'backbone.' if is_semantic else '' # queries, keys and values A__ = state_dict.pop(f'''{prefix}blocks.{i}.attn.qkv.weight''' ) A__ = state_dict.pop(f'''{prefix}blocks.{i}.attn.q_bias''' ) A__ = state_dict.pop(f'''{prefix}blocks.{i}.attn.v_bias''' ) A__ = in_proj_weight[ : config.hidden_size, : ] A__ = q_bias A__ = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] A__ = in_proj_weight[ -config.hidden_size :, : ] A__ = v_bias # gamma_1 and gamma_2 # we call them lambda because otherwise they are renamed when using .from_pretrained A__ = state_dict.pop(f'''{prefix}blocks.{i}.gamma_1''' ) A__ = state_dict.pop(f'''{prefix}blocks.{i}.gamma_2''' ) A__ = gamma_a A__ = gamma_a def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Union[str, Any]: A__ = dct.pop(__UpperCamelCase ) A__ = val def A ( ) -> Dict: A__ = 'http://images.cocodataset.org/val2017/000000039769.jpg' A__ = Image.open(requests.get(__UpperCamelCase , stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=False ) -> str: A__ = False if 'rvlcdip' in checkpoint_url else True A__ = BeitConfig(use_absolute_position_embeddings=__UpperCamelCase , use_mask_token=__UpperCamelCase ) # size of the architecture if "large" in checkpoint_url or "dit-l" in checkpoint_url: A__ = 1_024 A__ = 4_096 A__ = 24 A__ = 16 # labels if "rvlcdip" in checkpoint_url: A__ = 16 A__ = 'huggingface/label-files' A__ = 'rvlcdip-id2label.json' A__ = json.load(open(hf_hub_download(__UpperCamelCase , __UpperCamelCase , repo_type='dataset' ) , 'r' ) ) A__ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} A__ = idalabel A__ = {v: k for k, v in idalabel.items()} # load state_dict of original model, remove and rename some keys A__ = torch.hub.load_state_dict_from_url(__UpperCamelCase , map_location='cpu' )['model'] A__ = create_rename_keys(__UpperCamelCase , has_lm_head=__UpperCamelCase ) for src, dest in rename_keys: rename_key(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) read_in_q_k_v(__UpperCamelCase , __UpperCamelCase , has_lm_head=__UpperCamelCase ) # load HuggingFace model A__ = BeitForMaskedImageModeling(__UpperCamelCase ) if has_lm_head else BeitForImageClassification(__UpperCamelCase ) model.eval() model.load_state_dict(__UpperCamelCase ) # Check outputs on an image A__ = BeitImageProcessor( size=config.image_size , resample=PILImageResampling.BILINEAR , do_center_crop=__UpperCamelCase ) A__ = prepare_img() A__ = image_processor(images=__UpperCamelCase , return_tensors='pt' ) A__ = encoding['pixel_values'] A__ = model(__UpperCamelCase ) A__ = outputs.logits # verify logits A__ = [1, 16] if 'rvlcdip' in checkpoint_url else [1, 196, 8_192] assert logits.shape == torch.Size(__UpperCamelCase ), "Shape of logits not as expected" Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) print(f'''Saving model to {pytorch_dump_folder_path}''' ) model.save_pretrained(__UpperCamelCase ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(__UpperCamelCase ) if push_to_hub: if has_lm_head: A__ = 'dit-base' if 'base' in checkpoint_url else 'dit-large' else: A__ = '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(__UpperCamelCase , __UpperCamelCase ) , organization='nielsr' , commit_message='Add image processor' , use_temp_dir=__UpperCamelCase , ) model.push_to_hub( repo_path_or_name=Path(__UpperCamelCase , __UpperCamelCase ) , organization='nielsr' , commit_message='Add model' , use_temp_dir=__UpperCamelCase , ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = 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''', ) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_dit_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub)
9
1
import csv import tweepy # Twitter API credentials SCREAMING_SNAKE_CASE__ = '''''' SCREAMING_SNAKE_CASE__ = '''''' SCREAMING_SNAKE_CASE__ = '''''' SCREAMING_SNAKE_CASE__ = '''''' def A ( __UpperCamelCase ) -> None: # authorize twitter, initialize tweepy A__ = tweepy.OAuthHandler(__UpperCamelCase , __UpperCamelCase ) auth.set_access_token(__UpperCamelCase , __UpperCamelCase ) A__ = tweepy.API(__UpperCamelCase ) # initialize a list to hold all the tweepy Tweets A__ = [] # make initial request for most recent tweets (200 is the maximum allowed count) A__ = api.user_timeline(screen_name=__UpperCamelCase , count=200 ) # save most recent tweets alltweets.extend(__UpperCamelCase ) # save the id of the oldest tweet less one A__ = alltweets[-1].id - 1 # keep grabbing tweets until there are no tweets left to grab while len(__UpperCamelCase ) > 0: print(f'''getting tweets before {oldest}''' ) # all subsequent requests use the max_id param to prevent duplicates A__ = api.user_timeline( screen_name=__UpperCamelCase , count=200 , max_id=__UpperCamelCase ) # save most recent tweets alltweets.extend(__UpperCamelCase ) # update the id of the oldest tweet less one A__ = alltweets[-1].id - 1 print(f'''...{len(__UpperCamelCase )} tweets downloaded so far''' ) # transform the tweepy tweets into a 2D array that will populate the csv A__ = [[tweet.id_str, tweet.created_at, tweet.text] for tweet in alltweets] # write the csv with open(f'''new_{screen_name}_tweets.csv''' , 'w' ) as f: A__ = csv.writer(__UpperCamelCase ) writer.writerow(['id', 'created_at', 'text'] ) writer.writerows(__UpperCamelCase ) if __name__ == "__main__": # pass in the username of the account you want to download get_all_tweets('''FirePing32''')
9
SCREAMING_SNAKE_CASE__ = { '''A''': ['''B''', '''C''', '''E'''], '''B''': ['''A''', '''D''', '''E'''], '''C''': ['''A''', '''F''', '''G'''], '''D''': ['''B'''], '''E''': ['''A''', '''B''', '''D'''], '''F''': ['''C'''], '''G''': ['''C'''], } def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> list[str]: A__ = set() # keep track of all the paths to be checked A__ = [[start]] # return path if start is goal if start == goal: return [start] # keeps looping until all possible paths have been checked while queue: # pop the first path from the queue A__ = queue.pop(0 ) # get the last node from the path A__ = path[-1] if node not in explored: A__ = graph[node] # go through all neighbour nodes, construct a new path and # push it into the queue for neighbour in neighbours: A__ = list(__UpperCamelCase ) new_path.append(__UpperCamelCase ) queue.append(__UpperCamelCase ) # return path if neighbour is goal if neighbour == goal: return new_path # mark node as explored explored.add(__UpperCamelCase ) # in case there's no path between the 2 nodes return [] def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: if not graph or start not in graph or target not in graph: return -1 if start == target: return 0 A__ = [start] A__ = set(__UpperCamelCase ) # Keep tab on distances from `start` node. A__ = {start: 0, target: -1} while queue: A__ = queue.pop(0 ) if node == target: A__ = ( dist[node] if dist[target] == -1 else min(dist[target] , dist[node] ) ) for adjacent in graph[node]: if adjacent not in visited: visited.add(__UpperCamelCase ) queue.append(__UpperCamelCase ) A__ = dist[node] + 1 return dist[target] if __name__ == "__main__": print(bfs_shortest_path(demo_graph, '''G''', '''D''')) # returns ['G', 'C', 'A', 'B', 'D'] print(bfs_shortest_path_distance(demo_graph, '''G''', '''D''')) # returns 4
9
1
import copy from typing import Dict, List, Optional from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING SCREAMING_SNAKE_CASE__ = { '''facebook/mask2former-swin-small-coco-instance''': ( '''https://huggingface.co/facebook/mask2former-swin-small-coco-instance/blob/main/config.json''' ) # See all Mask2Former models at https://huggingface.co/models?filter=mask2former } SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Any = "mask2former" A__ : int = ["swin"] A__ : Union[str, Any] = {"hidden_size": "hidden_dim"} def __init__( self : Union[str, Any] , _snake_case : Optional[Dict] = None , _snake_case : int = 2_56 , _snake_case : int = 2_56 , _snake_case : int = 2_56 , _snake_case : int = 10_24 , _snake_case : str = "relu" , _snake_case : int = 6 , _snake_case : int = 10 , _snake_case : int = 8 , _snake_case : float = 0.0 , _snake_case : int = 20_48 , _snake_case : bool = False , _snake_case : bool = False , _snake_case : int = 4 , _snake_case : int = 2_55 , _snake_case : int = 1_00 , _snake_case : float = 0.1 , _snake_case : float = 2.0 , _snake_case : float = 5.0 , _snake_case : float = 5.0 , _snake_case : int = 1_25_44 , _snake_case : float = 3.0 , _snake_case : float = 0.75 , _snake_case : float = 0.02 , _snake_case : float = 1.0 , _snake_case : bool = True , _snake_case : List[int] = [4, 8, 16, 32] , _snake_case : bool = None , **_snake_case : str , ): """simple docstring""" if backbone_config is None: logger.info('`backbone_config` is `None`. Initializing the config with the default `Swin` backbone.' ) A__ = CONFIG_MAPPING['swin']( image_size=2_24 , in_channels=3 , patch_size=4 , embed_dim=96 , depths=[2, 2, 18, 2] , num_heads=[3, 6, 12, 24] , window_size=7 , drop_path_rate=0.3 , use_absolute_embeddings=_snake_case , out_features=['stage1', 'stage2', 'stage3', 'stage4'] , ) if isinstance(_snake_case , _snake_case ): A__ = backbone_config.pop('model_type' ) A__ = CONFIG_MAPPING[backbone_model_type] A__ = config_class.from_dict(_snake_case ) # verify that the backbone is supported if backbone_config.model_type not in self.backbones_supported: logger.warning_once( F'''Backbone {backbone_config.model_type} is not a supported model and may not be compatible with Mask2Former. ''' F'''Supported model types: {",".join(self.backbones_supported )}''' ) A__ = backbone_config A__ = feature_size A__ = mask_feature_size A__ = hidden_dim A__ = encoder_feedforward_dim A__ = activation_function A__ = encoder_layers A__ = decoder_layers A__ = num_attention_heads A__ = dropout A__ = dim_feedforward A__ = pre_norm A__ = enforce_input_projection A__ = common_stride A__ = ignore_value A__ = num_queries A__ = no_object_weight A__ = class_weight A__ = mask_weight A__ = dice_weight A__ = train_num_points A__ = oversample_ratio A__ = importance_sample_ratio A__ = init_std A__ = init_xavier_std A__ = use_auxiliary_loss A__ = feature_strides A__ = output_auxiliary_logits A__ = decoder_layers super().__init__(**_snake_case ) @classmethod def _a ( cls : Union[str, Any] , _snake_case : PretrainedConfig , **_snake_case : Tuple ): """simple docstring""" return cls( backbone_config=_snake_case , **_snake_case , ) def _a ( self : Dict ): """simple docstring""" A__ = copy.deepcopy(self.__dict__ ) A__ = self.backbone_config.to_dict() A__ = self.__class__.model_type return output
9
def A ( __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: A__ = 0 A__ = len(__UpperCamelCase ) - 1 while left <= right: # avoid divided by 0 during interpolation if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: return left else: return None A__ = left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left] ) # out of range check if point < 0 or point >= len(__UpperCamelCase ): return None A__ = sorted_collection[point] if current_item == item: return point else: if point < left: A__ = left A__ = point elif point > right: A__ = right A__ = point else: if item < current_item: A__ = point - 1 else: A__ = point + 1 return None def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: # avoid divided by 0 during interpolation if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: return left else: return None A__ = left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left] ) # out of range check if point < 0 or point >= len(__UpperCamelCase ): return None if sorted_collection[point] == item: return point elif point < left: return interpolation_search_by_recursion(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) elif point > right: return interpolation_search_by_recursion(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) else: if sorted_collection[point] > item: return interpolation_search_by_recursion( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , point - 1 ) else: return interpolation_search_by_recursion( __UpperCamelCase , __UpperCamelCase , point + 1 , __UpperCamelCase ) def A ( __UpperCamelCase ) -> List[str]: if collection != sorted(__UpperCamelCase ): raise ValueError('Collection must be ascending sorted' ) return True if __name__ == "__main__": import sys SCREAMING_SNAKE_CASE__ = 0 if debug == 1: SCREAMING_SNAKE_CASE__ = [1_0, 3_0, 4_0, 4_5, 5_0, 6_6, 7_7, 9_3] try: __assert_sorted(collection) except ValueError: sys.exit('''Sequence must be ascending sorted to apply interpolation search''') SCREAMING_SNAKE_CASE__ = 6_7 SCREAMING_SNAKE_CASE__ = interpolation_search(collection, target) if result is not None: print(f'{target} found at positions: {result}') else: print('''Not found''')
9
1
import argparse import dataclasses import json import logging import os import shutil from typing import List, Optional import datasets from accelerate import Accelerator from datasets import load_dataset from finetuning import finetune from tqdm.auto import tqdm import transformers from transformers import AutoConfig, set_seed from transformers.trainer_utils import IntervalStrategy SCREAMING_SNAKE_CASE__ = logging.getLogger(__name__) SCREAMING_SNAKE_CASE__ = '''pytorch_model.bin''' @dataclasses.dataclass class __lowerCAmelCase : """simple docstring""" A__ : str = dataclasses.field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models."} ) A__ : Optional[str] = dataclasses.field( default=UpperCAmelCase_ , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co."} , ) @dataclasses.dataclass class __lowerCAmelCase : """simple docstring""" A__ : str = dataclasses.field(metadata={"help": "A csv or a json file containing the training data."} ) A__ : str = dataclasses.field(metadata={"help": "A csv or a json file containing the data to predict on."} ) A__ : Optional[str] = dataclasses.field( default=UpperCAmelCase_ , metadata={"help": "A csv or a json file containing the validation data."} ) A__ : Optional[str] = dataclasses.field( default=UpperCAmelCase_ , metadata={"help": "The name of the task to train on."} , ) A__ : Optional[List[str]] = dataclasses.field( default=UpperCAmelCase_ , metadata={"help": "The list of labels for the task."} ) @dataclasses.dataclass class __lowerCAmelCase : """simple docstring""" A__ : str = dataclasses.field( metadata={"help": "The output directory where the model predictions and checkpoints will be written."} ) A__ : Optional[str] = dataclasses.field( default="accuracy" , metadata={"help": "The evaluation metric used for the task."} ) A__ : Optional[str] = dataclasses.field( default="no" , metadata={ "help": "The evaluation strategy to adopt during training. Possible values are: [\"no\", \"step\", \"epoch]" } , ) A__ : Optional[int] = dataclasses.field( default=10 , metadata={"help": "Number of evaluation calls with no improvement after which training will be stopped."} , ) A__ : Optional[float] = dataclasses.field( default=0.0 , metadata={ "help": "How much the specified evaluation metric must improve to satisfy early stopping conditions." } , ) A__ : Optional[bool] = dataclasses.field( default=UpperCAmelCase_ , metadata={"help": "Whether to filter the pseudo-labeled data based on the confidence score."} , ) A__ : Optional[bool] = dataclasses.field( default=UpperCAmelCase_ , metadata={"help": "Whether to filter the pseudo-labeled data based on the validation performance."} , ) A__ : Optional[bool] = dataclasses.field( default=UpperCAmelCase_ , metadata={"help": "Whether to fine-tune on labeled data after pseudo training."} , ) A__ : Optional[float] = dataclasses.field( default=0.0 , metadata={"help": "Confidence threshold for pseudo-labeled data filtering."} , ) A__ : Optional[int] = dataclasses.field( default=1_00 , metadata={"help": "Number of evaluation calls with no improvement after which training will be stopped."} , ) A__ : Optional[int] = dataclasses.field( default=UpperCAmelCase_ , metadata={"help": "Random seed for initialization."} , ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Optional[Any]: A__ = datasets.concatenate_datasets([infer_input, infer_output] , axis=1 ) if args.do_filter_by_confidence: A__ = dataset.filter(lambda __UpperCamelCase : example["probability"] > args.confidence_threshold ) if args.do_filter_by_val_performance: assert eval_result >= 0.0 and eval_result <= 1.0 A__ = int(eval_result * len(__UpperCamelCase ) ) print(__UpperCamelCase ) A__ = dataset.sort('probability' , reverse=__UpperCamelCase ) A__ = dataset.select(range(__UpperCamelCase ) ) A__ = dataset.remove_columns(['label', 'probability'] ) A__ = dataset.rename_column('prediction' , 'label' ) A__ = dataset.map(lambda __UpperCamelCase : {"label": idalabel[example["label"]]} ) A__ = dataset.shuffle(seed=args.seed ) A__ = os.path.join(__UpperCamelCase , f'''train_pseudo.{args.data_file_extension}''' ) if args.data_file_extension == "csv": dataset.to_csv(__UpperCamelCase , index=__UpperCamelCase ) else: dataset.to_json(__UpperCamelCase ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , **__UpperCamelCase ) -> int: A__ = Accelerator() # Make one log on every process with the configuration for debugging. logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , level=logging.INFO , ) logger.info(accelerator.state ) # Setup logging, we only want one process per machine to log things on the # screen. accelerator.is_local_main_process is only True for one process per # machine. logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR ) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() A__ = STModelArguments(model_name_or_path=__UpperCamelCase ) A__ = STDataArguments(train_file=__UpperCamelCase , infer_file=__UpperCamelCase ) A__ = STTrainingArguments(output_dir=__UpperCamelCase ) A__ = argparse.Namespace() for arg_class in (model_args, data_args, training_args): for key, value in vars(__UpperCamelCase ).items(): setattr(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) for key, value in kwargs.items(): if hasattr(__UpperCamelCase , __UpperCamelCase ): setattr(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) # Sanity checks A__ = {} A__ = None # You need to provide the training data and the data to predict on assert args.train_file is not None assert args.infer_file is not None A__ = args.train_file A__ = args.infer_file if args.evaluation_strategy != IntervalStrategy.NO.value: assert args.eval_file is not None A__ = args.eval_file for key in data_files: A__ = data_files[key].split('.' )[-1] assert extension in ["csv", "json"], f'''`{key}_file` should be a csv or a json file.''' if args.data_file_extension is None: A__ = extension else: assert extension == args.data_file_extension, f'''`{key}_file` should be a {args.data_file_extension} file`.''' assert ( args.eval_metric in datasets.list_metrics() ), f'''{args.eval_metric} not in the list of supported metrics {datasets.list_metrics()}.''' # If passed along, set the training seed now. if args.seed is not None: set_seed(args.seed ) logger.info('Creating the initial data directory for self-training...' ) A__ = f'''{args.output_dir}/self-train_iter-{{}}'''.format A__ = data_dir_format(0 ) if accelerator.is_main_process: if args.output_dir is not None: os.makedirs(args.output_dir , exist_ok=__UpperCamelCase ) os.makedirs(__UpperCamelCase , exist_ok=__UpperCamelCase ) accelerator.wait_for_everyone() A__ = None A__ = None A__ = 0 A__ = False # Show the progress bar A__ = tqdm(range(args.max_selftrain_iterations ) , disable=not accelerator.is_local_main_process ) # Self-train for iteration in range(0 , int(args.max_selftrain_iterations ) ): A__ = data_dir_format(__UpperCamelCase ) assert os.path.exists(__UpperCamelCase ) # Stage 1: initial fine-tuning for iteration = 0 or pseudo-training for # iteration > 0 A__ = os.path.join(__UpperCamelCase , 'stage-1' ) A__ = { 'accelerator': accelerator, 'model_name_or_path': args.model_name_or_path, 'cache_dir': args.cache_dir, 'do_train': True, 'train_file': data_files['train'] if iteration == 0 else data_files['train_pseudo'], 'do_eval': True if args.eval_file is not None else False, 'eval_file': data_files['eval'], 'do_predict': True, 'infer_file': data_files['infer'], 'task_name': args.task_name, 'label_list': args.label_list, 'output_dir': current_output_dir, 'eval_metric': args.eval_metric, 'evaluation_strategy': args.evaluation_strategy, 'early_stopping_patience': args.early_stopping_patience, 'early_stopping_threshold': args.early_stopping_threshold, 'seed': args.seed, } # Add additional training arguments for key, value in kwargs.items(): if key not in arguments_dict and not hasattr(__UpperCamelCase , __UpperCamelCase ): arguments_dict.update({key: value} ) A__ = os.path.join(__UpperCamelCase , 'best-checkpoint' , __UpperCamelCase ) if os.path.exists(__UpperCamelCase ): logger.info( 'Found existing model checkpoint at %s. Skipping self-training: iteration: %d, stage: 1.' , __UpperCamelCase , __UpperCamelCase , ) else: logger.info('***** Running self-training: iteration: %d, stage: 1 *****' , __UpperCamelCase ) finetune(**__UpperCamelCase ) accelerator.wait_for_everyone() assert os.path.exists(__UpperCamelCase ) logger.info('Self-training job completed: iteration: %d, stage: 1.' , __UpperCamelCase ) if iteration > 0 and args.finetune_on_labeled_data: # Stage 2 (optional): fine-tuning on the original labeled data A__ = os.path.join(__UpperCamelCase , 'best-checkpoint' ) A__ = os.path.join(__UpperCamelCase , 'stage-2' ) # Update arguments_dict A__ = model_path A__ = data_files['train'] A__ = current_output_dir A__ = os.path.join(__UpperCamelCase , 'best-checkpoint' , __UpperCamelCase ) if os.path.exists(__UpperCamelCase ): logger.info( 'Found existing model checkpoint at %s. Skipping self-training: iteration: %d, stage: 2.' , __UpperCamelCase , __UpperCamelCase , ) else: logger.info('***** Running self-training: iteration: %d, stage: 2 *****' , __UpperCamelCase ) finetune(**__UpperCamelCase ) accelerator.wait_for_everyone() assert os.path.exists(__UpperCamelCase ) logger.info('Self-training job completed: iteration: %d, stage: 2.' , __UpperCamelCase ) A__ = iteration A__ = data_dir_format(iteration + 1 ) A__ = AutoConfig.from_pretrained(os.path.join(__UpperCamelCase , 'best-checkpoint' ) ) A__ = config.idalabel A__ = os.path.join(__UpperCamelCase , 'eval_results_best-checkpoint.json' ) A__ = os.path.join(__UpperCamelCase , 'test_results_best-checkpoint.json' ) assert os.path.exists(__UpperCamelCase ) with open(__UpperCamelCase , 'r' ) as f: A__ = float(json.load(__UpperCamelCase )[args.eval_metric] ) A__ = os.path.join(__UpperCamelCase , 'infer_output_best-checkpoint.csv' ) assert os.path.exists(__UpperCamelCase ) # Loading the dataset from local csv or json files. A__ = load_dataset(args.data_file_extension , data_files={'data': data_files['infer']} )['data'] A__ = load_dataset('csv' , data_files={'data': infer_output_file} )['data'] if accelerator.is_main_process: os.makedirs(__UpperCamelCase , exist_ok=__UpperCamelCase ) shutil.copy(__UpperCamelCase , os.path.join(__UpperCamelCase , f'''eval_results_iter-{iteration}.json''' ) ) if os.path.exists(__UpperCamelCase ): shutil.copy(__UpperCamelCase , os.path.join(__UpperCamelCase , f'''test_results_iter-{iteration}.json''' ) ) create_pseudo_labeled_data(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) accelerator.wait_for_everyone() A__ = os.path.join(__UpperCamelCase , f'''train_pseudo.{args.data_file_extension}''' ) if args.evaluation_strategy != IntervalStrategy.NO.value: A__ = eval_result if best_iteration is None: A__ = new_iteration A__ = new_eval_result else: if new_eval_result - best_eval_result > args.early_stopping_threshold: A__ = new_iteration A__ = new_eval_result A__ = 0 else: if new_eval_result == best_eval_result: A__ = new_iteration A__ = new_eval_result early_stopping_patience_counter += 1 if early_stopping_patience_counter >= args.early_stopping_patience: A__ = True progress_bar.update(1 ) if should_training_stop: break if best_iteration is not None: # Save the best iteration logger.info('Best iteration: %d' , __UpperCamelCase ) logger.info('Best evaluation result: %s = %f' , args.eval_metric , __UpperCamelCase ) accelerator.wait_for_everyone() if accelerator.is_main_process: shutil.copy( os.path.join(__UpperCamelCase , f'''eval_results_iter-{iteration}.json''' ) , os.path.join(__UpperCamelCase , 'eval_results_best-iteration.json' ) , ) else: # Assume that the last iteration is the best logger.info('Best iteration: %d' , args.max_selftrain_iterations - 1 ) logger.info('Best evaluation result: %s = %f' , args.eval_metric , __UpperCamelCase ) accelerator.wait_for_everyone() if accelerator.is_main_process: shutil.copy( os.path.join(__UpperCamelCase , f'''eval_results_iter-{args.max_selftrain_iterations - 1}.json''' ) , os.path.join(__UpperCamelCase , 'eval_results_best-iteration.json' ) , )
9
import warnings from ...utils import logging from .image_processing_clip import CLIPImageProcessor SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Dict , *_snake_case : int , **_snake_case : Optional[int] ): """simple docstring""" warnings.warn( 'The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please' ' use CLIPImageProcessor instead.' , _snake_case , ) super().__init__(*_snake_case , **_snake_case )
9
1
from __future__ import annotations from math import pow, sqrt def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> dict[str, float]: if (resistance, reactance, impedance).count(0 ) != 1: raise ValueError('One and only one argument must be 0' ) if resistance == 0: return {"resistance": sqrt(pow(__UpperCamelCase , 2 ) - pow(__UpperCamelCase , 2 ) )} elif reactance == 0: return {"reactance": sqrt(pow(__UpperCamelCase , 2 ) - pow(__UpperCamelCase , 2 ) )} elif impedance == 0: return {"impedance": sqrt(pow(__UpperCamelCase , 2 ) + pow(__UpperCamelCase , 2 ) )} else: raise ValueError('Exactly one argument must be 0' ) if __name__ == "__main__": import doctest doctest.testmod()
9
import numpy as np import skfuzzy as fuzz if __name__ == "__main__": # Create universe of discourse in Python using linspace () SCREAMING_SNAKE_CASE__ = np.linspace(start=0, stop=7_5, num=7_5, endpoint=True, retstep=False) # Create two fuzzy sets by defining any membership function # (trapmf(), gbellmf(), gaussmf(), etc). SCREAMING_SNAKE_CASE__ = [0, 2_5, 5_0] SCREAMING_SNAKE_CASE__ = [2_5, 5_0, 7_5] SCREAMING_SNAKE_CASE__ = fuzz.membership.trimf(X, abca) SCREAMING_SNAKE_CASE__ = fuzz.membership.trimf(X, abca) # Compute the different operations using inbuilt functions. SCREAMING_SNAKE_CASE__ = np.ones(7_5) SCREAMING_SNAKE_CASE__ = np.zeros((7_5,)) # 1. Union = max(µA(x), µB(x)) SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_or(X, young, X, middle_aged)[1] # 2. Intersection = min(µA(x), µB(x)) SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_and(X, young, X, middle_aged)[1] # 3. Complement (A) = (1- min(µA(x)) SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_not(young) # 4. Difference (A/B) = min(µA(x),(1- µB(x))) SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_and(X, young, X, fuzz.fuzzy_not(middle_aged)[1])[1] # 5. Algebraic Sum = [µA(x) + µB(x) – (µA(x) * µB(x))] SCREAMING_SNAKE_CASE__ = young + middle_aged - (young * middle_aged) # 6. Algebraic Product = (µA(x) * µB(x)) SCREAMING_SNAKE_CASE__ = young * middle_aged # 7. Bounded Sum = min[1,(µA(x), µB(x))] SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_and(X, one, X, young + middle_aged)[1] # 8. Bounded difference = min[0,(µA(x), µB(x))] SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_or(X, zero, X, young - middle_aged)[1] # max-min composition # max-product composition # Plot each set A, set B and each operation result using plot() and subplot(). from matplotlib import pyplot as plt plt.figure() plt.subplot(4, 3, 1) plt.plot(X, young) plt.title('''Young''') plt.grid(True) plt.subplot(4, 3, 2) plt.plot(X, middle_aged) plt.title('''Middle aged''') plt.grid(True) plt.subplot(4, 3, 3) plt.plot(X, union) plt.title('''union''') plt.grid(True) plt.subplot(4, 3, 4) plt.plot(X, intersection) plt.title('''intersection''') plt.grid(True) plt.subplot(4, 3, 5) plt.plot(X, complement_a) plt.title('''complement_a''') plt.grid(True) plt.subplot(4, 3, 6) plt.plot(X, difference) plt.title('''difference a/b''') plt.grid(True) plt.subplot(4, 3, 7) plt.plot(X, alg_sum) plt.title('''alg_sum''') plt.grid(True) plt.subplot(4, 3, 8) plt.plot(X, alg_product) plt.title('''alg_product''') plt.grid(True) plt.subplot(4, 3, 9) plt.plot(X, bdd_sum) plt.title('''bdd_sum''') plt.grid(True) plt.subplot(4, 3, 1_0) plt.plot(X, bdd_difference) plt.title('''bdd_difference''') plt.grid(True) plt.subplots_adjust(hspace=0.5) plt.show()
9
1
from ...configuration_utils import PretrainedConfig from ...utils import logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = { '''naver-clova-ix/donut-base''': '''https://huggingface.co/naver-clova-ix/donut-base/resolve/main/config.json''', # See all Donut models at https://huggingface.co/models?filter=donut-swin } class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : str = "donut-swin" A__ : Tuple = { "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self : int , _snake_case : int=2_24 , _snake_case : Optional[Any]=4 , _snake_case : int=3 , _snake_case : Optional[Any]=96 , _snake_case : Tuple=[2, 2, 6, 2] , _snake_case : List[str]=[3, 6, 12, 24] , _snake_case : List[Any]=7 , _snake_case : Union[str, Any]=4.0 , _snake_case : List[Any]=True , _snake_case : Optional[int]=0.0 , _snake_case : Optional[Any]=0.0 , _snake_case : Any=0.1 , _snake_case : Tuple="gelu" , _snake_case : str=False , _snake_case : Union[str, Any]=0.02 , _snake_case : List[str]=1E-5 , **_snake_case : List[str] , ): """simple docstring""" super().__init__(**_snake_case ) A__ = image_size A__ = patch_size A__ = num_channels A__ = embed_dim A__ = depths A__ = len(_snake_case ) A__ = num_heads A__ = window_size A__ = mlp_ratio A__ = qkv_bias A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = drop_path_rate A__ = hidden_act A__ = use_absolute_embeddings A__ = layer_norm_eps A__ = initializer_range # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model A__ = int(embed_dim * 2 ** (len(_snake_case ) - 1) )
9
import unittest from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __lowerCAmelCase : """simple docstring""" @staticmethod def _a ( *_snake_case : int , **_snake_case : List[str] ): """simple docstring""" pass @is_pipeline_test @require_vision @require_torch class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" A__ : List[str] = MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING def _a ( self : Any , _snake_case : Union[str, Any] , _snake_case : Tuple , _snake_case : Optional[Any] ): """simple docstring""" A__ = pipeline( 'zero-shot-object-detection' , model='hf-internal-testing/tiny-random-owlvit-object-detection' ) A__ = [ { 'image': './tests/fixtures/tests_samples/COCO/000000039769.png', 'candidate_labels': ['cat', 'remote', 'couch'], } ] return object_detector, examples def _a ( self : int , _snake_case : int , _snake_case : List[str] ): """simple docstring""" A__ = object_detector(examples[0] , threshold=0.0 ) A__ = len(_snake_case ) self.assertGreater(_snake_case , 0 ) self.assertEqual( _snake_case , [ { 'score': ANY(_snake_case ), 'label': ANY(_snake_case ), 'box': {'xmin': ANY(_snake_case ), 'ymin': ANY(_snake_case ), 'xmax': ANY(_snake_case ), 'ymax': ANY(_snake_case )}, } for i in range(_snake_case ) ] , ) @require_tf @unittest.skip('Zero Shot Object Detection not implemented in TF' ) def _a ( self : List[str] ): """simple docstring""" pass @require_torch def _a ( self : Optional[int] ): """simple docstring""" A__ = pipeline( 'zero-shot-object-detection' , model='hf-internal-testing/tiny-random-owlvit-object-detection' ) A__ = object_detector( './tests/fixtures/tests_samples/COCO/000000039769.png' , candidate_labels=['cat', 'remote', 'couch'] , threshold=0.64 , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}}, {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}}, {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}}, ] , ) A__ = object_detector( [ { 'image': './tests/fixtures/tests_samples/COCO/000000039769.png', 'candidate_labels': ['cat', 'remote', 'couch'], } ] , threshold=0.64 , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ [ {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}}, {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}}, {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}}, ] ] , ) @require_torch @slow def _a ( self : int ): """simple docstring""" A__ = pipeline('zero-shot-object-detection' ) A__ = object_detector( 'http://images.cocodataset.org/val2017/000000039769.jpg' , candidate_labels=['cat', 'remote', 'couch'] , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}}, {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}}, {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}}, ] , ) A__ = object_detector( [ { 'image': 'http://images.cocodataset.org/val2017/000000039769.jpg', 'candidate_labels': ['cat', 'remote', 'couch'], }, { 'image': 'http://images.cocodataset.org/val2017/000000039769.jpg', 'candidate_labels': ['cat', 'remote', 'couch'], }, ] , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}}, {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}}, {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}}, ], [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}}, {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}}, {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}}, ], ] , ) @require_tf @unittest.skip('Zero Shot Object Detection not implemented in TF' ) def _a ( self : int ): """simple docstring""" pass @require_torch @slow def _a ( self : str ): """simple docstring""" A__ = 0.2 A__ = pipeline('zero-shot-object-detection' ) A__ = object_detector( 'http://images.cocodataset.org/val2017/000000039769.jpg' , candidate_labels=['cat', 'remote', 'couch'] , threshold=_snake_case , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}}, ] , ) @require_torch @slow def _a ( self : Any ): """simple docstring""" A__ = 2 A__ = pipeline('zero-shot-object-detection' ) A__ = object_detector( 'http://images.cocodataset.org/val2017/000000039769.jpg' , candidate_labels=['cat', 'remote', 'couch'] , top_k=_snake_case , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, ] , )
9
1
from __future__ import annotations def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> tuple[float, list[float]]: A__ = list(range(len(__UpperCamelCase ) ) ) A__ = [v / w for v, w in zip(__UpperCamelCase , __UpperCamelCase )] index.sort(key=lambda __UpperCamelCase : ratio[i] , reverse=__UpperCamelCase ) A__ = 0 A__ = [0] * len(__UpperCamelCase ) for i in index: if weight[i] <= capacity: A__ = 1 max_value += value[i] capacity -= weight[i] else: A__ = capacity / weight[i] max_value += value[i] * capacity / weight[i] break return max_value, fractions if __name__ == "__main__": import doctest doctest.testmod()
9
import dataclasses import json import sys import types from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, ArgumentTypeError from copy import copy from enum import Enum from inspect import isclass from pathlib import Path from typing import Any, Callable, Dict, Iterable, List, Literal, NewType, Optional, Tuple, Union, get_type_hints import yaml SCREAMING_SNAKE_CASE__ = NewType('''DataClass''', Any) SCREAMING_SNAKE_CASE__ = NewType('''DataClassType''', Any) def A ( __UpperCamelCase ) -> List[Any]: if isinstance(__UpperCamelCase , __UpperCamelCase ): return v if v.lower() in ("yes", "true", "t", "y", "1"): return True elif v.lower() in ("no", "false", "f", "n", "0"): return False else: raise ArgumentTypeError( f'''Truthy value expected: got {v} but expected one of yes/no, true/false, t/f, y/n, 1/0 (case insensitive).''' ) def A ( __UpperCamelCase ) -> Callable[[str], Any]: A__ = {str(__UpperCamelCase ): choice for choice in choices} return lambda __UpperCamelCase : str_to_choice.get(__UpperCamelCase , __UpperCamelCase ) def A ( *, __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = dataclasses.MISSING , __UpperCamelCase = dataclasses.MISSING , __UpperCamelCase = None , **__UpperCamelCase , ) -> dataclasses.Field: if metadata is None: # Important, don't use as default param in function signature because dict is mutable and shared across function calls A__ = {} if aliases is not None: A__ = aliases if help is not None: A__ = help return dataclasses.field(metadata=__UpperCamelCase , default=__UpperCamelCase , default_factory=__UpperCamelCase , **__UpperCamelCase ) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Iterable[DataClassType] def __init__( self : Optional[int] , _snake_case : Union[DataClassType, Iterable[DataClassType]] , **_snake_case : Tuple ): """simple docstring""" if "formatter_class" not in kwargs: A__ = ArgumentDefaultsHelpFormatter super().__init__(**_snake_case ) if dataclasses.is_dataclass(_snake_case ): A__ = [dataclass_types] A__ = list(_snake_case ) for dtype in self.dataclass_types: self._add_dataclass_arguments(_snake_case ) @staticmethod def _a ( _snake_case : ArgumentParser , _snake_case : dataclasses.Field ): """simple docstring""" A__ = F'''--{field.name}''' A__ = field.metadata.copy() # field.metadata is not used at all by Data Classes, # it is provided as a third-party extension mechanism. if isinstance(field.type , _snake_case ): raise RuntimeError( 'Unresolved type detected, which should have been done with the help of ' '`typing.get_type_hints` method by default' ) A__ = kwargs.pop('aliases' , [] ) if isinstance(_snake_case , _snake_case ): A__ = [aliases] A__ = getattr(field.type , '__origin__' , field.type ) if origin_type is Union or (hasattr(_snake_case , 'UnionType' ) and isinstance(_snake_case , types.UnionType )): if str not in field.type.__args__ and ( len(field.type.__args__ ) != 2 or type(_snake_case ) not in field.type.__args__ ): raise ValueError( 'Only `Union[X, NoneType]` (i.e., `Optional[X]`) is allowed for `Union` because' ' the argument parser only supports one type per argument.' F''' Problem encountered in field \'{field.name}\'.''' ) if type(_snake_case ) not in field.type.__args__: # filter `str` in Union A__ = field.type.__args__[0] if field.type.__args__[1] == str else field.type.__args__[1] A__ = getattr(field.type , '__origin__' , field.type ) elif bool not in field.type.__args__: # filter `NoneType` in Union (except for `Union[bool, NoneType]`) A__ = ( field.type.__args__[0] if isinstance(_snake_case , field.type.__args__[1] ) else field.type.__args__[1] ) A__ = getattr(field.type , '__origin__' , field.type ) # A variable to store kwargs for a boolean field, if needed # so that we can init a `no_*` complement argument (see below) A__ = {} if origin_type is Literal or (isinstance(field.type , _snake_case ) and issubclass(field.type , _snake_case )): if origin_type is Literal: A__ = field.type.__args__ else: A__ = [x.value for x in field.type] A__ = make_choice_type_function(kwargs['choices'] ) if field.default is not dataclasses.MISSING: A__ = field.default else: A__ = True elif field.type is bool or field.type == Optional[bool]: # Copy the currect kwargs to use to instantiate a `no_*` complement argument below. # We do not initialize it here because the `no_*` alternative must be instantiated after the real argument A__ = copy(_snake_case ) # Hack because type=bool in argparse does not behave as we want. A__ = string_to_bool if field.type is bool or (field.default is not None and field.default is not dataclasses.MISSING): # Default value is False if we have no default when of type bool. A__ = False if field.default is dataclasses.MISSING else field.default # This is the value that will get picked if we don't include --field_name in any way A__ = default # This tells argparse we accept 0 or 1 value after --field_name A__ = '?' # This is the value that will get picked if we do --field_name (without value) A__ = True elif isclass(_snake_case ) and issubclass(_snake_case , _snake_case ): A__ = field.type.__args__[0] A__ = '+' if field.default_factory is not dataclasses.MISSING: A__ = field.default_factory() elif field.default is dataclasses.MISSING: A__ = True else: A__ = field.type if field.default is not dataclasses.MISSING: A__ = field.default elif field.default_factory is not dataclasses.MISSING: A__ = field.default_factory() else: A__ = True parser.add_argument(_snake_case , *_snake_case , **_snake_case ) # Add a complement `no_*` argument for a boolean field AFTER the initial field has already been added. # Order is important for arguments with the same destination! # We use a copy of earlier kwargs because the original kwargs have changed a lot before reaching down # here and we do not need those changes/additional keys. if field.default is True and (field.type is bool or field.type == Optional[bool]): A__ = False parser.add_argument(F'''--no_{field.name}''' , action='store_false' , dest=field.name , **_snake_case ) def _a ( self : Any , _snake_case : DataClassType ): """simple docstring""" if hasattr(_snake_case , '_argument_group_name' ): A__ = self.add_argument_group(dtype._argument_group_name ) else: A__ = self try: A__ = get_type_hints(_snake_case ) except NameError: raise RuntimeError( F'''Type resolution failed for {dtype}. Try declaring the class in global scope or ''' 'removing line of `from __future__ import annotations` which opts in Postponed ' 'Evaluation of Annotations (PEP 563)' ) except TypeError as ex: # Remove this block when we drop Python 3.9 support if sys.version_info[:2] < (3, 10) and "unsupported operand type(s) for |" in str(_snake_case ): A__ = '.'.join(map(_snake_case , sys.version_info[:3] ) ) raise RuntimeError( F'''Type resolution failed for {dtype} on Python {python_version}. Try removing ''' 'line of `from __future__ import annotations` which opts in union types as ' '`X | Y` (PEP 604) via Postponed Evaluation of Annotations (PEP 563). To ' 'support Python versions that lower than 3.10, you need to use ' '`typing.Union[X, Y]` instead of `X | Y` and `typing.Optional[X]` instead of ' '`X | None`.' ) from ex raise for field in dataclasses.fields(_snake_case ): if not field.init: continue A__ = type_hints[field.name] self._parse_dataclass_field(_snake_case , _snake_case ) def _a ( self : Optional[int] , _snake_case : Optional[Any]=None , _snake_case : Any=False , _snake_case : int=True , _snake_case : List[Any]=None , _snake_case : int=None , ): """simple docstring""" if args_file_flag or args_filename or (look_for_args_file and len(sys.argv )): A__ = [] if args_filename: args_files.append(Path(_snake_case ) ) elif look_for_args_file and len(sys.argv ): args_files.append(Path(sys.argv[0] ).with_suffix('.args' ) ) # args files specified via command line flag should overwrite default args files so we add them last if args_file_flag: # Create special parser just to extract the args_file_flag values A__ = ArgumentParser() args_file_parser.add_argument(_snake_case , type=_snake_case , action='append' ) # Use only remaining args for further parsing (remove the args_file_flag) A__ , A__ = args_file_parser.parse_known_args(args=_snake_case ) A__ = vars(_snake_case ).get(args_file_flag.lstrip('-' ) , _snake_case ) if cmd_args_file_paths: args_files.extend([Path(_snake_case ) for p in cmd_args_file_paths] ) A__ = [] for args_file in args_files: if args_file.exists(): file_args += args_file.read_text().split() # in case of duplicate arguments the last one has precedence # args specified via the command line should overwrite args from files, so we add them last A__ = file_args + args if args is not None else file_args + sys.argv[1:] A__ , A__ = self.parse_known_args(args=_snake_case ) A__ = [] for dtype in self.dataclass_types: A__ = {f.name for f in dataclasses.fields(_snake_case ) if f.init} A__ = {k: v for k, v in vars(_snake_case ).items() if k in keys} for k in keys: delattr(_snake_case , _snake_case ) A__ = dtype(**_snake_case ) outputs.append(_snake_case ) if len(namespace.__dict__ ) > 0: # additional namespace. outputs.append(_snake_case ) if return_remaining_strings: return (*outputs, remaining_args) else: if remaining_args: raise ValueError(F'''Some specified arguments are not used by the HfArgumentParser: {remaining_args}''' ) return (*outputs,) def _a ( self : Dict , _snake_case : Dict[str, Any] , _snake_case : bool = False ): """simple docstring""" A__ = set(args.keys() ) A__ = [] for dtype in self.dataclass_types: A__ = {f.name for f in dataclasses.fields(_snake_case ) if f.init} A__ = {k: v for k, v in args.items() if k in keys} unused_keys.difference_update(inputs.keys() ) A__ = dtype(**_snake_case ) outputs.append(_snake_case ) if not allow_extra_keys and unused_keys: raise ValueError(F'''Some keys are not used by the HfArgumentParser: {sorted(_snake_case )}''' ) return tuple(_snake_case ) def _a ( self : Dict , _snake_case : str , _snake_case : bool = False ): """simple docstring""" with open(Path(_snake_case ) , encoding='utf-8' ) as open_json_file: A__ = json.loads(open_json_file.read() ) A__ = self.parse_dict(_snake_case , allow_extra_keys=_snake_case ) return tuple(_snake_case ) def _a ( self : Tuple , _snake_case : str , _snake_case : bool = False ): """simple docstring""" A__ = self.parse_dict(yaml.safe_load(Path(_snake_case ).read_text() ) , allow_extra_keys=_snake_case ) return tuple(_snake_case )
9
1
import collections import os import re from pathlib import Path SCREAMING_SNAKE_CASE__ = '''src/transformers''' # Matches is_xxx_available() SCREAMING_SNAKE_CASE__ = re.compile(r'''is\_([a-z_]*)_available()''') # Catches a one-line _import_struct = {xxx} SCREAMING_SNAKE_CASE__ = re.compile(r'''^_import_structure\s+=\s+\{([^\}]+)\}''') # Catches a line with a key-values pattern: "bla": ["foo", "bar"] SCREAMING_SNAKE_CASE__ = re.compile(r'''\s+"\S*":\s+\[([^\]]*)\]''') # Catches a line if not is_foo_available SCREAMING_SNAKE_CASE__ = re.compile(r'''^\s*if\s+not\s+is\_[a-z_]*\_available\(\)''') # Catches a line _import_struct["bla"].append("foo") SCREAMING_SNAKE_CASE__ = re.compile(r'''^\s*_import_structure\["\S*"\]\.append\("(\S*)"\)''') # Catches a line _import_struct["bla"].extend(["foo", "bar"]) or _import_struct["bla"] = ["foo", "bar"] SCREAMING_SNAKE_CASE__ = re.compile(r'''^\s*_import_structure\[\S*\](?:\.extend\(|\s*=\s+)\[([^\]]*)\]''') # Catches a line with an object between quotes and a comma: "MyModel", SCREAMING_SNAKE_CASE__ = re.compile(r'''^\s+"([^"]+)",''') # Catches a line with objects between brackets only: ["foo", "bar"], SCREAMING_SNAKE_CASE__ = re.compile(r'''^\s+\[([^\]]+)\]''') # Catches a line with from foo import bar, bla, boo SCREAMING_SNAKE_CASE__ = re.compile(r'''\s+from\s+\S*\s+import\s+([^\(\s].*)\n''') # Catches a line with try: SCREAMING_SNAKE_CASE__ = re.compile(r'''^\s*try:''') # Catches a line with else: SCREAMING_SNAKE_CASE__ = re.compile(r'''^\s*else:''') def A ( __UpperCamelCase ) -> Union[str, Any]: if _re_test_backend.search(__UpperCamelCase ) is None: return None A__ = [b[0] for b in _re_backend.findall(__UpperCamelCase )] backends.sort() return "_and_".join(__UpperCamelCase ) def A ( __UpperCamelCase ) -> List[Any]: with open(__UpperCamelCase , 'r' , encoding='utf-8' , newline='\n' ) as f: A__ = f.readlines() A__ = 0 while line_index < len(__UpperCamelCase ) and not lines[line_index].startswith('_import_structure = {' ): line_index += 1 # If this is a traditional init, just return. if line_index >= len(__UpperCamelCase ): return None # First grab the objects without a specific backend in _import_structure A__ = [] while not lines[line_index].startswith('if TYPE_CHECKING' ) and find_backend(lines[line_index] ) is None: A__ = lines[line_index] # If we have everything on a single line, let's deal with it. if _re_one_line_import_struct.search(__UpperCamelCase ): A__ = _re_one_line_import_struct.search(__UpperCamelCase ).groups()[0] A__ = re.findall(r'\[([^\]]+)\]' , __UpperCamelCase ) for imp in imports: objects.extend([obj[1:-1] for obj in imp.split(', ' )] ) line_index += 1 continue A__ = _re_import_struct_key_value.search(__UpperCamelCase ) if single_line_import_search is not None: A__ = [obj[1:-1] for obj in single_line_import_search.groups()[0].split(', ' ) if len(__UpperCamelCase ) > 0] objects.extend(__UpperCamelCase ) elif line.startswith(' ' * 8 + '"' ): objects.append(line[9:-3] ) line_index += 1 A__ = {'none': objects} # Let's continue with backend-specific objects in _import_structure while not lines[line_index].startswith('if TYPE_CHECKING' ): # If the line is an if not is_backend_available, we grab all objects associated. A__ = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: A__ = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 A__ = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(' ' * 4 ): A__ = lines[line_index] if _re_import_struct_add_one.search(__UpperCamelCase ) is not None: objects.append(_re_import_struct_add_one.search(__UpperCamelCase ).groups()[0] ) elif _re_import_struct_add_many.search(__UpperCamelCase ) is not None: A__ = _re_import_struct_add_many.search(__UpperCamelCase ).groups()[0].split(', ' ) A__ = [obj[1:-1] for obj in imports if len(__UpperCamelCase ) > 0] objects.extend(__UpperCamelCase ) elif _re_between_brackets.search(__UpperCamelCase ) is not None: A__ = _re_between_brackets.search(__UpperCamelCase ).groups()[0].split(', ' ) A__ = [obj[1:-1] for obj in imports if len(__UpperCamelCase ) > 0] objects.extend(__UpperCamelCase ) elif _re_quote_object.search(__UpperCamelCase ) is not None: objects.append(_re_quote_object.search(__UpperCamelCase ).groups()[0] ) elif line.startswith(' ' * 8 + '"' ): objects.append(line[9:-3] ) elif line.startswith(' ' * 12 + '"' ): objects.append(line[13:-3] ) line_index += 1 A__ = objects else: line_index += 1 # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend A__ = [] while ( line_index < len(__UpperCamelCase ) and find_backend(lines[line_index] ) is None and not lines[line_index].startswith('else' ) ): A__ = lines[line_index] A__ = _re_import.search(__UpperCamelCase ) 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 A__ = {'none': objects} # Let's continue with backend-specific objects while line_index < len(__UpperCamelCase ): # If the line is an if is_backend_available, we grab all objects associated. A__ = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: A__ = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 A__ = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(' ' * 8 ): A__ = lines[line_index] A__ = _re_import.search(__UpperCamelCase ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(', ' ) ) elif line.startswith(' ' * 12 ): objects.append(line[12:-2] ) line_index += 1 A__ = objects else: line_index += 1 return import_dict_objects, type_hint_objects def A ( __UpperCamelCase , __UpperCamelCase ) -> Union[str, Any]: def find_duplicates(__UpperCamelCase ): return [k for k, v in collections.Counter(__UpperCamelCase ).items() if v > 1] if list(import_dict_objects.keys() ) != list(type_hint_objects.keys() ): return ["Both sides of the init do not have the same backends!"] A__ = [] for key in import_dict_objects.keys(): A__ = find_duplicates(import_dict_objects[key] ) if duplicate_imports: errors.append(f'''Duplicate _import_structure definitions for: {duplicate_imports}''' ) A__ = find_duplicates(type_hint_objects[key] ) if duplicate_type_hints: errors.append(f'''Duplicate TYPE_CHECKING objects for: {duplicate_type_hints}''' ) if sorted(set(import_dict_objects[key] ) ) != sorted(set(type_hint_objects[key] ) ): A__ = 'base imports' if key == 'none' else f'''{key} backend''' errors.append(f'''Differences for {name}:''' ) for a in type_hint_objects[key]: if a not in import_dict_objects[key]: errors.append(f''' {a} in TYPE_HINT but not in _import_structure.''' ) for a in import_dict_objects[key]: if a not in type_hint_objects[key]: errors.append(f''' {a} in _import_structure but not in TYPE_HINT.''' ) return errors def A ( ) -> Optional[Any]: A__ = [] for root, _, files in os.walk(__UpperCamelCase ): if "__init__.py" in files: A__ = os.path.join(__UpperCamelCase , '__init__.py' ) A__ = parse_init(__UpperCamelCase ) if objects is not None: A__ = analyze_results(*__UpperCamelCase ) if len(__UpperCamelCase ) > 0: A__ = f'''Problem in {fname}, both halves do not define the same objects.\n{errors[0]}''' failures.append('\n'.join(__UpperCamelCase ) ) if len(__UpperCamelCase ) > 0: raise ValueError('\n\n'.join(__UpperCamelCase ) ) def A ( ) -> str: A__ = [] for path, directories, files in os.walk(__UpperCamelCase ): for folder in directories: # Ignore private modules if folder.startswith('_' ): directories.remove(__UpperCamelCase ) continue # Ignore leftovers from branches (empty folders apart from pycache) if len(list((Path(__UpperCamelCase ) / folder).glob('*.py' ) ) ) == 0: continue A__ = str((Path(__UpperCamelCase ) / folder).relative_to(__UpperCamelCase ) ) A__ = short_path.replace(os.path.sep , '.' ) submodules.append(__UpperCamelCase ) for fname in files: if fname == "__init__.py": continue A__ = str((Path(__UpperCamelCase ) / fname).relative_to(__UpperCamelCase ) ) A__ = short_path.replace('.py' , '' ).replace(os.path.sep , '.' ) if len(submodule.split('.' ) ) == 1: submodules.append(__UpperCamelCase ) return submodules SCREAMING_SNAKE_CASE__ = [ '''convert_pytorch_checkpoint_to_tf2''', '''modeling_flax_pytorch_utils''', '''models.esm.openfold_utils''', ] def A ( ) -> Any: # This is to make sure the transformers module imported is the one in the repo. from transformers.utils import direct_transformers_import A__ = direct_transformers_import(__UpperCamelCase ) A__ = set(transformers._import_structure.keys() ) # This contains all the base keys of the _import_structure object defined in the init, but if the user is missing # some optional dependencies, they may not have all of them. Thus we read the init to read all additions and # (potentiall re-) add them. with open(os.path.join(__UpperCamelCase , '__init__.py' ) , 'r' ) as f: A__ = f.read() import_structure_keys.update(set(re.findall(r'import_structure\[\"([^\"]*)\"\]' , __UpperCamelCase ) ) ) A__ = [ module for module in get_transformers_submodules() if module not in IGNORE_SUBMODULES and module not in import_structure_keys ] if len(__UpperCamelCase ) > 0: A__ = '\n'.join(f'''- {module}''' for module in module_not_registered ) raise ValueError( 'The following submodules are not properly registed in the main init of Transformers:\n' f'''{list_of_modules}\n''' 'Make sure they appear somewhere in the keys of `_import_structure` with an empty list as value.' ) if __name__ == "__main__": check_all_inits() check_submodules()
9
import argparse import collections import json from pathlib import Path import requests import torch import yaml from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( MobileViTImageProcessor, MobileViTVaConfig, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation, ) from transformers.utils import logging logging.set_verbosity_info() SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) def A ( __UpperCamelCase ) -> List[Any]: print('Loading config file...' ) def flatten_yaml_as_dict(__UpperCamelCase , __UpperCamelCase="" , __UpperCamelCase="." ): A__ = [] for k, v in d.items(): A__ = parent_key + sep + k if parent_key else k if isinstance(__UpperCamelCase , collections.abc.MutableMapping ): items.extend(flatten_yaml_as_dict(__UpperCamelCase , __UpperCamelCase , sep=__UpperCamelCase ).items() ) else: items.append((new_key, v) ) return dict(__UpperCamelCase ) A__ = argparse.Namespace() with open(__UpperCamelCase , 'r' ) as yaml_file: try: A__ = yaml.load(__UpperCamelCase , Loader=yaml.FullLoader ) A__ = flatten_yaml_as_dict(__UpperCamelCase ) for k, v in flat_cfg.items(): setattr(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) except yaml.YAMLError as exc: logger.error('Error while loading config file: {}. Error message: {}'.format(__UpperCamelCase , str(__UpperCamelCase ) ) ) return config def A ( __UpperCamelCase , __UpperCamelCase ) -> Optional[Any]: A__ = MobileViTVaConfig() A__ = False # dataset if task_name.startswith('imagenet1k_' ): A__ = 1_000 if int(task_name.strip().split('_' )[-1] ) == 384: A__ = 384 else: A__ = 256 A__ = 'imagenet-1k-id2label.json' elif task_name.startswith('imagenet21k_to_1k_' ): A__ = 21_000 if int(task_name.strip().split('_' )[-1] ) == 384: A__ = 384 else: A__ = 256 A__ = 'imagenet-22k-id2label.json' elif task_name.startswith('ade20k_' ): A__ = 151 A__ = 512 A__ = 'ade20k-id2label.json' A__ = True elif task_name.startswith('voc_' ): A__ = 21 A__ = 512 A__ = 'pascal-voc-id2label.json' A__ = True # orig_config A__ = load_orig_config_file(__UpperCamelCase ) assert getattr(__UpperCamelCase , 'model.classification.name' , -1 ) == "mobilevit_v2", "Invalid model" A__ = getattr(__UpperCamelCase , 'model.classification.mitv2.width_multiplier' , 1.0 ) assert ( getattr(__UpperCamelCase , 'model.classification.mitv2.attn_norm_layer' , -1 ) == "layer_norm_2d" ), "Norm layers other than layer_norm_2d is not supported" A__ = getattr(__UpperCamelCase , 'model.classification.activation.name' , 'swish' ) # config.image_size == getattr(orig_config, 'sampler.bs.crop_size_width', 256) if is_segmentation_model: A__ = getattr(__UpperCamelCase , 'model.segmentation.output_stride' , 16 ) if "_deeplabv3" in task_name: A__ = getattr(__UpperCamelCase , 'model.segmentation.deeplabv3.aspp_rates' , [12, 24, 36] ) A__ = getattr(__UpperCamelCase , 'model.segmentation.deeplabv3.aspp_out_channels' , 512 ) A__ = getattr(__UpperCamelCase , 'model.segmentation.deeplabv3.aspp_dropout' , 0.1 ) # id2label A__ = 'huggingface/label-files' A__ = json.load(open(hf_hub_download(__UpperCamelCase , __UpperCamelCase , repo_type='dataset' ) , 'r' ) ) A__ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} A__ = idalabel A__ = {v: k for k, v in idalabel.items()} return config def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[str]: A__ = dct.pop(__UpperCamelCase ) A__ = val def A ( __UpperCamelCase , __UpperCamelCase=False ) -> Dict: if base_model: A__ = '' else: A__ = 'mobilevitv2.' A__ = [] for k in state_dict.keys(): if k[:8] == "encoder.": A__ = k[8:] else: A__ = k if ".block." in k: A__ = k_new.replace('.block.' , '.' ) if ".conv." in k: A__ = k_new.replace('.conv.' , '.convolution.' ) if ".norm." in k: A__ = k_new.replace('.norm.' , '.normalization.' ) if "conv_1." in k: A__ = k_new.replace('conv_1.' , f'''{model_prefix}conv_stem.''' ) for i in [1, 2]: if f'''layer_{i}.''' in k: A__ = k_new.replace(f'''layer_{i}.''' , f'''{model_prefix}encoder.layer.{i-1}.layer.''' ) if ".exp_1x1." in k: A__ = k_new.replace('.exp_1x1.' , '.expand_1x1.' ) if ".red_1x1." in k: A__ = k_new.replace('.red_1x1.' , '.reduce_1x1.' ) for i in [3, 4, 5]: if f'''layer_{i}.0.''' in k: A__ = k_new.replace(f'''layer_{i}.0.''' , f'''{model_prefix}encoder.layer.{i-1}.downsampling_layer.''' ) if f'''layer_{i}.1.local_rep.0.''' in k: A__ = k_new.replace(f'''layer_{i}.1.local_rep.0.''' , f'''{model_prefix}encoder.layer.{i-1}.conv_kxk.''' ) if f'''layer_{i}.1.local_rep.1.''' in k: A__ = k_new.replace(f'''layer_{i}.1.local_rep.1.''' , f'''{model_prefix}encoder.layer.{i-1}.conv_1x1.''' ) for i in [3, 4, 5]: if i == 3: A__ = [0, 1] elif i == 4: A__ = [0, 1, 2, 3] elif i == 5: A__ = [0, 1, 2] for j in j_in: if f'''layer_{i}.1.global_rep.{j}.''' in k: A__ = k_new.replace( f'''layer_{i}.1.global_rep.{j}.''' , f'''{model_prefix}encoder.layer.{i-1}.transformer.layer.{j}.''' ) if f'''layer_{i}.1.global_rep.{j+1}.''' in k: A__ = k_new.replace( f'''layer_{i}.1.global_rep.{j+1}.''' , f'''{model_prefix}encoder.layer.{i-1}.layernorm.''' ) if f'''layer_{i}.1.conv_proj.''' in k: A__ = k_new.replace(f'''layer_{i}.1.conv_proj.''' , f'''{model_prefix}encoder.layer.{i-1}.conv_projection.''' ) if "pre_norm_attn.0." in k: A__ = k_new.replace('pre_norm_attn.0.' , 'layernorm_before.' ) if "pre_norm_attn.1." in k: A__ = k_new.replace('pre_norm_attn.1.' , 'attention.' ) if "pre_norm_ffn.0." in k: A__ = k_new.replace('pre_norm_ffn.0.' , 'layernorm_after.' ) if "pre_norm_ffn.1." in k: A__ = k_new.replace('pre_norm_ffn.1.' , 'ffn.conv1.' ) if "pre_norm_ffn.3." in k: A__ = k_new.replace('pre_norm_ffn.3.' , 'ffn.conv2.' ) if "classifier.1." in k: A__ = k_new.replace('classifier.1.' , 'classifier.' ) if "seg_head." in k: A__ = k_new.replace('seg_head.' , 'segmentation_head.' ) if ".aspp_layer." in k: A__ = k_new.replace('.aspp_layer.' , '.' ) if ".aspp_pool." in k: A__ = k_new.replace('.aspp_pool.' , '.' ) rename_keys.append((k, k_new) ) return rename_keys def A ( __UpperCamelCase ) -> Tuple: A__ = [] for k in state_dict.keys(): if k.startswith('seg_head.aux_head.' ): keys_to_ignore.append(__UpperCamelCase ) for k in keys_to_ignore: state_dict.pop(__UpperCamelCase , __UpperCamelCase ) def A ( ) -> str: A__ = 'http://images.cocodataset.org/val2017/000000039769.jpg' # url = "https://cdn.britannica.com/86/141086-050-9D7C75EE/Gulfstream-G450-business-jet-passengers.jpg" A__ = Image.open(requests.get(__UpperCamelCase , stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Optional[Any]: A__ = get_mobilevitva_config(__UpperCamelCase , __UpperCamelCase ) # load original state_dict A__ = torch.load(__UpperCamelCase , map_location='cpu' ) # load huggingface model if task_name.startswith('ade20k_' ) or task_name.startswith('voc_' ): A__ = MobileViTVaForSemanticSegmentation(__UpperCamelCase ).eval() A__ = False else: A__ = MobileViTVaForImageClassification(__UpperCamelCase ).eval() A__ = False # remove and rename some keys of load the original model A__ = checkpoint remove_unused_keys(__UpperCamelCase ) A__ = create_rename_keys(__UpperCamelCase , base_model=__UpperCamelCase ) for rename_key_src, rename_key_dest in rename_keys: rename_key(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) # load modified state_dict model.load_state_dict(__UpperCamelCase ) # Check outputs on an image, prepared by MobileViTImageProcessor A__ = MobileViTImageProcessor(crop_size=config.image_size , size=config.image_size + 32 ) A__ = image_processor(images=prepare_img() , return_tensors='pt' ) A__ = model(**__UpperCamelCase ) # verify classification model if task_name.startswith('imagenet' ): A__ = outputs.logits A__ = logits.argmax(-1 ).item() print('Predicted class:' , model.config.idalabel[predicted_class_idx] ) if task_name.startswith('imagenet1k_256' ) and config.width_multiplier == 1.0: # expected_logits for base variant A__ = torch.tensor([-1.6336E00, -7.3204E-02, -5.1883E-01] ) assert torch.allclose(logits[0, :3] , __UpperCamelCase , atol=1E-4 ) Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) print(f'''Saving model {task_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(__UpperCamelCase ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(__UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--task''', default='''imagenet1k_256''', type=str, help=( '''Name of the task for which the MobileViTV2 model you\'d like to convert is trained on . ''' ''' Classification (ImageNet-1k) - MobileViTV2 (256x256) : imagenet1k_256 - MobileViTV2 (Trained on 256x256 and Finetuned on 384x384) : imagenet1k_384 - MobileViTV2 (Trained on ImageNet-21k and Finetuned on ImageNet-1k 256x256) : imagenet21k_to_1k_256 - MobileViTV2 (Trained on ImageNet-21k, Finetuned on ImageNet-1k 256x256, and Finetuned on ImageNet-1k 384x384) : imagenet21k_to_1k_384 Segmentation - ADE20K Dataset : ade20k_deeplabv3 - Pascal VOC 2012 Dataset: voc_deeplabv3 ''' ), choices=[ '''imagenet1k_256''', '''imagenet1k_384''', '''imagenet21k_to_1k_256''', '''imagenet21k_to_1k_384''', '''ade20k_deeplabv3''', '''voc_deeplabv3''', ], ) parser.add_argument( '''--orig_checkpoint_path''', required=True, type=str, help='''Path to the original state dict (.pt file).''' ) parser.add_argument('''--orig_config_path''', required=True, type=str, help='''Path to the original config file.''') parser.add_argument( '''--pytorch_dump_folder_path''', required=True, type=str, help='''Path to the output PyTorch model directory.''' ) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_mobilevitva_checkpoint( args.task, args.orig_checkpoint_path, args.orig_config_path, args.pytorch_dump_folder_path )
9
1
from queue import PriorityQueue from typing import Any import numpy as np def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , ) -> float | int: for nxt, d in graph[v]: if nxt in visited_forward: continue A__ = cst_fwd.get(__UpperCamelCase , np.inf ) A__ = cst_fwd[v] + d if new_cost_f < old_cost_f: queue.put((new_cost_f, nxt) ) A__ = new_cost_f A__ = v if nxt in visited_backward: if cst_fwd[v] + d + cst_bwd[nxt] < shortest_distance: A__ = cst_fwd[v] + d + cst_bwd[nxt] return shortest_distance def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: A__ = -1 A__ = set() A__ = set() A__ = {source: 0} A__ = {destination: 0} A__ = {source: None} A__ = {destination: None} A__ = PriorityQueue() A__ = PriorityQueue() A__ = np.inf queue_forward.put((0, source) ) queue_backward.put((0, destination) ) if source == destination: return 0 while not queue_forward.empty() and not queue_backward.empty(): A__ , A__ = queue_forward.get() visited_forward.add(__UpperCamelCase ) A__ , A__ = queue_backward.get() visited_backward.add(__UpperCamelCase ) A__ = pass_and_relaxation( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , ) A__ = pass_and_relaxation( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , ) if cst_fwd[v_fwd] + cst_bwd[v_bwd] >= shortest_distance: break if shortest_distance != np.inf: A__ = shortest_distance return shortest_path_distance SCREAMING_SNAKE_CASE__ = { '''B''': [['''C''', 1]], '''C''': [['''D''', 1]], '''D''': [['''F''', 1]], '''E''': [['''B''', 1], ['''G''', 2]], '''F''': [], '''G''': [['''F''', 1]], } SCREAMING_SNAKE_CASE__ = { '''B''': [['''E''', 1]], '''C''': [['''B''', 1]], '''D''': [['''C''', 1]], '''F''': [['''D''', 1], ['''G''', 1]], '''E''': [[None, np.inf]], '''G''': [['''E''', 2]], } if __name__ == "__main__": import doctest doctest.testmod()
9
import argparse from collections import defaultdict import yaml SCREAMING_SNAKE_CASE__ = '''docs/source/en/_toctree.yml''' def A ( __UpperCamelCase ) -> Optional[Any]: A__ = defaultdict(__UpperCamelCase ) for doc in model_doc: counts[doc["local"]] += 1 A__ = [key for key, value in counts.items() if value > 1] A__ = [] for duplicate_key in duplicates: A__ = list({doc['title'] for doc in model_doc if doc['local'] == duplicate_key} ) if len(__UpperCamelCase ) > 1: raise ValueError( f'''{duplicate_key} is present several times in the documentation table of content at ''' '`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the ' 'others.' ) # Only add this once new_doc.append({'local': duplicate_key, 'title': titles[0]} ) # Add none duplicate-keys new_doc.extend([doc for doc in model_doc if counts[doc['local']] == 1] ) # Sort return sorted(__UpperCamelCase , key=lambda __UpperCamelCase : s["title"].lower() ) def A ( __UpperCamelCase=False ) -> str: with open(__UpperCamelCase , encoding='utf-8' ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]['sections'] # Then to the model doc A__ = 0 while api_doc[model_idx]["title"] != "Models": model_idx += 1 A__ = api_doc[model_idx]['sections'] A__ = [(idx, section) for idx, section in enumerate(__UpperCamelCase ) if 'sections' in section] A__ = False for idx, modality_doc in modalities_docs: A__ = modality_doc['sections'] A__ = clean_model_doc_toc(__UpperCamelCase ) if old_modality_doc != new_modality_doc: A__ = True if overwrite: A__ = new_modality_doc if diff: if overwrite: A__ = model_doc A__ = api_doc with open(__UpperCamelCase , 'w' , encoding='utf-8' ) as f: f.write(yaml.dump(__UpperCamelCase , allow_unicode=__UpperCamelCase ) ) else: raise ValueError( 'The model doc part of the table of content is not properly sorted, run `make style` to fix this.' ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') SCREAMING_SNAKE_CASE__ = parser.parse_args() check_model_doc(args.fix_and_overwrite)
9
1
import logging import os import sys import warnings from dataclasses import dataclass, field from random import randint from typing import Optional import datasets import evaluate import numpy as np from datasets import DatasetDict, load_dataset import transformers from transformers import ( AutoConfig, AutoFeatureExtractor, AutoModelForAudioClassification, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version SCREAMING_SNAKE_CASE__ = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version('''4.31.0''') require_version('''datasets>=1.14.0''', '''To fix: pip install -r examples/pytorch/audio-classification/requirements.txt''') def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = 16_000 ) -> Union[str, Any]: A__ = int(round(sample_rate * max_length ) ) if len(__UpperCamelCase ) <= sample_length: return wav A__ = randint(0 , len(__UpperCamelCase ) - sample_length - 1 ) return wav[random_offset : random_offset + sample_length] @dataclass class __lowerCAmelCase : """simple docstring""" A__ : Optional[str] = field(default=UpperCAmelCase_ , metadata={"help": "Name of a dataset from the datasets package"} ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "The configuration name of the dataset to use (via the datasets library)."} ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "A file containing the training audio paths and labels."} ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "A file containing the validation audio paths and labels."} ) A__ : str = field( default="train" , metadata={ "help": "The name of the training data set split to use (via the datasets library). Defaults to 'train'" } , ) A__ : str = field( default="validation" , metadata={ "help": ( "The name of the training data set split to use (via the datasets library). Defaults to 'validation'" ) } , ) A__ : str = field( default="audio" , metadata={"help": "The name of the dataset column containing the audio data. Defaults to 'audio'"} , ) A__ : str = field( default="label" , metadata={"help": "The name of the dataset column containing the labels. Defaults to 'label'"} ) A__ : Optional[int] = field( default=UpperCAmelCase_ , metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of training examples to this " "value if set." ) } , ) A__ : Optional[int] = field( default=UpperCAmelCase_ , metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of evaluation examples to this " "value if set." ) } , ) A__ : float = field( default=20 , metadata={"help": "Audio clips will be randomly cut to this length during training if the value is set."} , ) @dataclass class __lowerCAmelCase : """simple docstring""" A__ : str = field( default="facebook/wav2vec2-base" , metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} , ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "Pretrained config name or path if not the same as model_name"} ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "Where do you want to store the pretrained models downloaded from the Hub"} ) A__ : str = field( default="main" , metadata={"help": "The specific model version to use (can be a branch name, tag name or commit id)."} , ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "Name or path of preprocessor config."} ) A__ : bool = field( default=UpperCAmelCase_ , metadata={"help": "Whether to freeze the feature encoder layers of the model."} ) A__ : bool = field( default=UpperCAmelCase_ , metadata={"help": "Whether to generate an attention mask in the feature extractor."} ) A__ : bool = field( default=UpperCAmelCase_ , metadata={ "help": ( "Will use the token generated when running `huggingface-cli login` (necessary to use this script " "with private models)." ) } , ) A__ : Optional[bool] = field( default=UpperCAmelCase_ , metadata={"help": "Whether to freeze the feature extractor layers of the model."} ) A__ : bool = field( default=UpperCAmelCase_ , metadata={"help": "Will enable to load a pretrained model whose head dimensions are different."} , ) def _a ( self : Dict ): """simple docstring""" if not self.freeze_feature_extractor and self.freeze_feature_encoder: warnings.warn( 'The argument `--freeze_feature_extractor` is deprecated and ' 'will be removed in a future version. Use `--freeze_feature_encoder`' 'instead. Setting `freeze_feature_encoder==True`.' , _snake_case , ) if self.freeze_feature_extractor and not self.freeze_feature_encoder: raise ValueError( 'The argument `--freeze_feature_extractor` is deprecated and ' 'should not be used in combination with `--freeze_feature_encoder`.' 'Only make use of `--freeze_feature_encoder`.' ) def A ( ) -> List[str]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. A__ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('.json' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. A__ , A__ , A__ = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: A__ , A__ , A__ = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry('run_audio_classification' , __UpperCamelCase , __UpperCamelCase ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() A__ = training_args.get_process_log_level() logger.setLevel(__UpperCamelCase ) transformers.utils.logging.set_verbosity(__UpperCamelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( f'''Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu} ''' + f'''distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}''' ) logger.info(f'''Training/evaluation parameters {training_args}''' ) # Set seed before initializing model. set_seed(training_args.seed ) # Detecting last checkpoint. A__ = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: A__ = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( f'''Output directory ({training_args.output_dir}) already exists and is not empty. ''' 'Use --overwrite_output_dir to train from scratch.' ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( f'''Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change ''' 'the `--output_dir` or add `--overwrite_output_dir` to train from scratch.' ) # Initialize our dataset and prepare it for the audio classification task. A__ = DatasetDict() A__ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=data_args.train_split_name , use_auth_token=True if model_args.use_auth_token else None , ) A__ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=data_args.eval_split_name , use_auth_token=True if model_args.use_auth_token else None , ) if data_args.audio_column_name not in raw_datasets["train"].column_names: raise ValueError( f'''--audio_column_name {data_args.audio_column_name} not found in dataset \'{data_args.dataset_name}\'. ''' 'Make sure to set `--audio_column_name` to the correct audio column - one of ' f'''{", ".join(raw_datasets["train"].column_names )}.''' ) if data_args.label_column_name not in raw_datasets["train"].column_names: raise ValueError( f'''--label_column_name {data_args.label_column_name} not found in dataset \'{data_args.dataset_name}\'. ''' 'Make sure to set `--label_column_name` to the correct text column - one of ' f'''{", ".join(raw_datasets["train"].column_names )}.''' ) # Setting `return_attention_mask=True` is the way to get a correctly masked mean-pooling over # transformer outputs in the classifier, but it doesn't always lead to better accuracy A__ = AutoFeatureExtractor.from_pretrained( model_args.feature_extractor_name or model_args.model_name_or_path , return_attention_mask=model_args.attention_mask , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # `datasets` takes care of automatically loading and resampling the audio, # so we just need to set the correct target sampling rate. A__ = raw_datasets.cast_column( data_args.audio_column_name , datasets.features.Audio(sampling_rate=feature_extractor.sampling_rate ) ) A__ = feature_extractor.model_input_names[0] def train_transforms(__UpperCamelCase ): A__ = [] for audio in batch[data_args.audio_column_name]: A__ = random_subsample( audio['array'] , max_length=data_args.max_length_seconds , sample_rate=feature_extractor.sampling_rate ) subsampled_wavs.append(__UpperCamelCase ) A__ = feature_extractor(__UpperCamelCase , sampling_rate=feature_extractor.sampling_rate ) A__ = {model_input_name: inputs.get(__UpperCamelCase )} A__ = list(batch[data_args.label_column_name] ) return output_batch def val_transforms(__UpperCamelCase ): A__ = [audio['array'] for audio in batch[data_args.audio_column_name]] A__ = feature_extractor(__UpperCamelCase , sampling_rate=feature_extractor.sampling_rate ) A__ = {model_input_name: inputs.get(__UpperCamelCase )} A__ = list(batch[data_args.label_column_name] ) return output_batch # Prepare label mappings. # We'll include these in the model's config to get human readable labels in the Inference API. A__ = raw_datasets['train'].features[data_args.label_column_name].names A__ , A__ = {}, {} for i, label in enumerate(__UpperCamelCase ): A__ = str(__UpperCamelCase ) A__ = label # Load the accuracy metric from the datasets package A__ = evaluate.load('accuracy' ) # Define our compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with # `predictions` and `label_ids` fields) and has to return a dictionary string to float. def compute_metrics(__UpperCamelCase ): A__ = np.argmax(eval_pred.predictions , axis=1 ) return metric.compute(predictions=__UpperCamelCase , references=eval_pred.label_ids ) A__ = AutoConfig.from_pretrained( model_args.config_name or model_args.model_name_or_path , num_labels=len(__UpperCamelCase ) , labelaid=__UpperCamelCase , idalabel=__UpperCamelCase , finetuning_task='audio-classification' , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) A__ = AutoModelForAudioClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool('.ckpt' in model_args.model_name_or_path ) , config=__UpperCamelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ignore_mismatched_sizes=model_args.ignore_mismatched_sizes , ) # freeze the convolutional waveform encoder if model_args.freeze_feature_encoder: model.freeze_feature_encoder() if training_args.do_train: if data_args.max_train_samples is not None: A__ = ( raw_datasets['train'].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) ) # Set the training transforms raw_datasets["train"].set_transform(__UpperCamelCase , output_all_columns=__UpperCamelCase ) if training_args.do_eval: if data_args.max_eval_samples is not None: A__ = ( raw_datasets['eval'].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms raw_datasets["eval"].set_transform(__UpperCamelCase , output_all_columns=__UpperCamelCase ) # Initialize our trainer A__ = Trainer( model=__UpperCamelCase , args=__UpperCamelCase , train_dataset=raw_datasets['train'] if training_args.do_train else None , eval_dataset=raw_datasets['eval'] if training_args.do_eval else None , compute_metrics=__UpperCamelCase , tokenizer=__UpperCamelCase , ) # Training if training_args.do_train: A__ = None if training_args.resume_from_checkpoint is not None: A__ = training_args.resume_from_checkpoint elif last_checkpoint is not None: A__ = last_checkpoint A__ = trainer.train(resume_from_checkpoint=__UpperCamelCase ) trainer.save_model() trainer.log_metrics('train' , train_result.metrics ) trainer.save_metrics('train' , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: A__ = trainer.evaluate() trainer.log_metrics('eval' , __UpperCamelCase ) trainer.save_metrics('eval' , __UpperCamelCase ) # Write model card and (optionally) push to hub A__ = { 'finetuned_from': model_args.model_name_or_path, 'tasks': 'audio-classification', 'dataset': data_args.dataset_name, 'tags': ['audio-classification'], } if training_args.push_to_hub: trainer.push_to_hub(**__UpperCamelCase ) else: trainer.create_model_card(**__UpperCamelCase ) if __name__ == "__main__": main()
9
import inspect import unittest from transformers import SegformerConfig, is_torch_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_MAPPING, SegformerForImageClassification, SegformerForSemanticSegmentation, SegformerModel, ) from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import SegformerImageProcessor class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def _a ( self : List[str] ): """simple docstring""" A__ = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(_snake_case , 'hidden_sizes' ) ) self.parent.assertTrue(hasattr(_snake_case , 'num_attention_heads' ) ) self.parent.assertTrue(hasattr(_snake_case , 'num_encoder_blocks' ) ) class __lowerCAmelCase : """simple docstring""" def __init__( self : Any , _snake_case : str , _snake_case : Union[str, Any]=13 , _snake_case : Any=64 , _snake_case : Optional[Any]=3 , _snake_case : Dict=4 , _snake_case : Tuple=[2, 2, 2, 2] , _snake_case : str=[8, 4, 2, 1] , _snake_case : Union[str, Any]=[16, 32, 64, 1_28] , _snake_case : int=[1, 4, 8, 16] , _snake_case : List[str]=[1, 2, 4, 8] , _snake_case : int=True , _snake_case : int=True , _snake_case : Union[str, Any]="gelu" , _snake_case : Optional[int]=0.1 , _snake_case : Tuple=0.1 , _snake_case : Dict=0.02 , _snake_case : Tuple=3 , _snake_case : int=None , ): """simple docstring""" A__ = parent A__ = batch_size A__ = image_size A__ = num_channels A__ = num_encoder_blocks A__ = sr_ratios A__ = depths A__ = hidden_sizes A__ = downsampling_rates A__ = num_attention_heads A__ = is_training A__ = use_labels A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = initializer_range A__ = num_labels A__ = scope def _a ( self : int ): """simple docstring""" A__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) A__ = self.get_config() return config, pixel_values, labels def _a ( self : int ): """simple docstring""" return SegformerConfig( image_size=self.image_size , num_channels=self.num_channels , num_encoder_blocks=self.num_encoder_blocks , depths=self.depths , hidden_sizes=self.hidden_sizes , num_attention_heads=self.num_attention_heads , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , initializer_range=self.initializer_range , ) def _a ( self : int , _snake_case : Optional[Any] , _snake_case : int , _snake_case : Any ): """simple docstring""" A__ = SegformerModel(config=_snake_case ) model.to(_snake_case ) model.eval() A__ = model(_snake_case ) A__ = A__ = self.image_size // (self.downsampling_rates[-1] * 2) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width) ) def _a ( self : Union[str, Any] , _snake_case : Union[str, Any] , _snake_case : Tuple , _snake_case : Dict ): """simple docstring""" A__ = self.num_labels A__ = SegformerForSemanticSegmentation(_snake_case ) model.to(_snake_case ) model.eval() A__ = model(_snake_case ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4) ) A__ = model(_snake_case , labels=_snake_case ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4) ) self.parent.assertGreater(result.loss , 0.0 ) def _a ( self : List[str] , _snake_case : Optional[Any] , _snake_case : Union[str, Any] , _snake_case : List[str] ): """simple docstring""" A__ = 1 A__ = SegformerForSemanticSegmentation(config=_snake_case ) model.to(_snake_case ) model.eval() A__ = torch.randint(0 , 1 , (self.batch_size, self.image_size, self.image_size) ).to(_snake_case ) A__ = model(_snake_case , labels=_snake_case ) self.parent.assertGreater(result.loss , 0.0 ) def _a ( self : List[Any] ): """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ = config_and_inputs A__ = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class __lowerCAmelCase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Optional[int] = ( ( SegformerModel, SegformerForSemanticSegmentation, SegformerForImageClassification, ) if is_torch_available() else () ) A__ : Union[str, Any] = ( { "feature-extraction": SegformerModel, "image-classification": SegformerForImageClassification, "image-segmentation": SegformerForSemanticSegmentation, } if is_torch_available() else {} ) A__ : Optional[Any] = True A__ : str = False A__ : Tuple = False A__ : Dict = False def _a ( self : Union[str, Any] ): """simple docstring""" A__ = SegformerModelTester(self ) A__ = SegformerConfigTester(self , config_class=_snake_case ) def _a ( self : Optional[int] ): """simple docstring""" self.config_tester.run_common_tests() def _a ( self : Optional[Any] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def _a ( self : List[Any] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_binary_image_segmentation(*_snake_case ) def _a ( self : Tuple ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_segmentation(*_snake_case ) @unittest.skip('SegFormer does not use inputs_embeds' ) def _a ( self : List[Any] ): """simple docstring""" pass @unittest.skip('SegFormer does not have get_input_embeddings method and get_output_embeddings methods' ) def _a ( self : Dict ): """simple docstring""" pass def _a ( self : Dict ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(_snake_case ) A__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ = [*signature.parameters.keys()] A__ = ['pixel_values'] self.assertListEqual(arg_names[:1] , _snake_case ) def _a ( self : Dict ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = True for model_class in self.all_model_classes: A__ = True A__ = False A__ = True A__ = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) ) A__ = outputs.attentions A__ = sum(self.model_tester.depths ) self.assertEqual(len(_snake_case ) , _snake_case ) # check that output_attentions also work using config del inputs_dict["output_attentions"] A__ = True A__ = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) ) A__ = outputs.attentions self.assertEqual(len(_snake_case ) , _snake_case ) # verify the first attentions (first block, first layer) A__ = (self.model_tester.image_size // 4) ** 2 A__ = (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2 self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len] , ) # verify the last attentions (last block, last layer) A__ = (self.model_tester.image_size // 32) ** 2 A__ = (self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2 self.assertListEqual( list(attentions[-1].shape[-3:] ) , [self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len] , ) A__ = len(_snake_case ) # Check attention is always last and order is fine A__ = True A__ = True A__ = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) ) self.assertEqual(out_len + 1 , len(_snake_case ) ) A__ = outputs.attentions self.assertEqual(len(_snake_case ) , _snake_case ) # verify the first attentions (first block, first layer) A__ = (self.model_tester.image_size // 4) ** 2 A__ = (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2 self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len] , ) def _a ( self : Union[str, Any] ): """simple docstring""" def check_hidden_states_output(_snake_case : Dict , _snake_case : int , _snake_case : List[Any] ): A__ = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) ) A__ = outputs.hidden_states A__ = self.model_tester.num_encoder_blocks self.assertEqual(len(_snake_case ) , _snake_case ) # verify the first hidden states (first block) self.assertListEqual( list(hidden_states[0].shape[-3:] ) , [ self.model_tester.hidden_sizes[0], self.model_tester.image_size // 4, self.model_tester.image_size // 4, ] , ) A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = True check_hidden_states_output(_snake_case , _snake_case , _snake_case ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A__ = True check_hidden_states_output(_snake_case , _snake_case , _snake_case ) def _a ( self : Tuple ): """simple docstring""" if not self.model_tester.is_training: return A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = True for model_class in self.all_model_classes: if model_class in get_values(_snake_case ): continue A__ = model_class(_snake_case ) model.to(_snake_case ) model.train() A__ = self._prepare_for_class(_snake_case , _snake_case , return_labels=_snake_case ) A__ = model(**_snake_case ).loss loss.backward() @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.' ) def _a ( self : Optional[Any] ): """simple docstring""" pass @slow def _a ( self : Tuple ): """simple docstring""" for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = SegformerModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def A ( ) -> str: A__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @slow def _a ( self : Dict ): """simple docstring""" A__ = SegformerImageProcessor( image_scale=(5_12, 5_12) , keep_ratio=_snake_case , align=_snake_case , do_random_crop=_snake_case ) A__ = SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512' ).to( _snake_case ) A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ) A__ = encoded_inputs.pixel_values.to(_snake_case ) with torch.no_grad(): A__ = model(_snake_case ) A__ = torch.Size((1, model.config.num_labels, 1_28, 1_28) ) self.assertEqual(outputs.logits.shape , _snake_case ) A__ = torch.tensor( [ [[-4.6310, -5.5232, -6.2356], [-5.1921, -6.1444, -6.5996], [-5.4424, -6.2790, -6.7574]], [[-12.1391, -13.3122, -13.9554], [-12.8732, -13.9352, -14.3563], [-12.9438, -13.8226, -14.2513]], [[-12.5134, -13.4686, -14.4915], [-12.8669, -14.4343, -14.7758], [-13.2523, -14.5819, -15.0694]], ] ).to(_snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3] , _snake_case , atol=1E-4 ) ) @slow def _a ( self : Optional[Any] ): """simple docstring""" A__ = SegformerImageProcessor( image_scale=(5_12, 5_12) , keep_ratio=_snake_case , align=_snake_case , do_random_crop=_snake_case ) A__ = SegformerForSemanticSegmentation.from_pretrained( 'nvidia/segformer-b1-finetuned-cityscapes-1024-1024' ).to(_snake_case ) A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ) A__ = encoded_inputs.pixel_values.to(_snake_case ) with torch.no_grad(): A__ = model(_snake_case ) A__ = torch.Size((1, model.config.num_labels, 1_28, 1_28) ) self.assertEqual(outputs.logits.shape , _snake_case ) A__ = torch.tensor( [ [[-13.5748, -13.9111, -12.6500], [-14.3500, -15.3683, -14.2328], [-14.7532, -16.0424, -15.6087]], [[-17.1651, -15.8725, -12.9653], [-17.2580, -17.3718, -14.8223], [-16.6058, -16.8783, -16.7452]], [[-3.6456, -3.0209, -1.4203], [-3.0797, -3.1959, -2.0000], [-1.8757, -1.9217, -1.6997]], ] ).to(_snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3] , _snake_case , atol=1E-1 ) ) @slow def _a ( self : Any ): """simple docstring""" A__ = SegformerImageProcessor( image_scale=(5_12, 5_12) , keep_ratio=_snake_case , align=_snake_case , do_random_crop=_snake_case ) A__ = SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512' ).to( _snake_case ) A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ) A__ = encoded_inputs.pixel_values.to(_snake_case ) with torch.no_grad(): A__ = model(_snake_case ) A__ = outputs.logits.detach().cpu() A__ = image_processor.post_process_semantic_segmentation(outputs=_snake_case , target_sizes=[(5_00, 3_00)] ) A__ = torch.Size((5_00, 3_00) ) self.assertEqual(segmentation[0].shape , _snake_case ) A__ = image_processor.post_process_semantic_segmentation(outputs=_snake_case ) A__ = torch.Size((1_28, 1_28) ) self.assertEqual(segmentation[0].shape , _snake_case )
9
1
from collections import OrderedDict from typing import TYPE_CHECKING, Any, List, Mapping, Optional from packaging import version if TYPE_CHECKING: from ... import PreTrainedTokenizer, TensorType from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import is_torch_available, logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = { '''bigscience/bloom''': '''https://huggingface.co/bigscience/bloom/resolve/main/config.json''', '''bigscience/bloom-560m''': '''https://huggingface.co/bigscience/bloom-560m/blob/main/config.json''', '''bigscience/bloom-1b1''': '''https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json''', '''bigscience/bloom-1b7''': '''https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json''', '''bigscience/bloom-3b''': '''https://huggingface.co/bigscience/bloom-3b/blob/main/config.json''', '''bigscience/bloom-7b1''': '''https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json''', } class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Optional[int] = "bloom" A__ : Union[str, Any] = ["past_key_values"] A__ : int = { "num_hidden_layers": "n_layer", "num_attention_heads": "n_head", } def __init__( self : Dict , _snake_case : Tuple=25_08_80 , _snake_case : Dict=64 , _snake_case : Optional[int]=2 , _snake_case : int=8 , _snake_case : Optional[int]=1E-5 , _snake_case : List[Any]=0.02 , _snake_case : Optional[Any]=True , _snake_case : Union[str, Any]=1 , _snake_case : List[Any]=2 , _snake_case : Optional[int]=False , _snake_case : Union[str, Any]=0.0 , _snake_case : Union[str, Any]=0.0 , _snake_case : int=1 , _snake_case : List[Any]=False , **_snake_case : str , ): """simple docstring""" A__ = vocab_size # Backward compatibility with n_embed kwarg A__ = kwargs.pop('n_embed' , _snake_case ) A__ = hidden_size if n_embed is None else n_embed A__ = n_layer A__ = n_head A__ = layer_norm_epsilon A__ = initializer_range A__ = use_cache A__ = pretraining_tp A__ = apply_residual_connection_post_layernorm A__ = hidden_dropout A__ = attention_dropout A__ = bos_token_id A__ = eos_token_id A__ = slow_but_exact super().__init__(bos_token_id=_snake_case , eos_token_id=_snake_case , **_snake_case ) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : List[Any] = version.parse("1.12" ) def __init__( self : str , _snake_case : PretrainedConfig , _snake_case : str = "default" , _snake_case : List[PatchingSpec] = None , _snake_case : bool = False , ): """simple docstring""" super().__init__(_snake_case , task=_snake_case , patching_specs=_snake_case , use_past=_snake_case ) if not getattr(self._config , 'pad_token_id' , _snake_case ): # TODO: how to do that better? A__ = 0 @property def _a ( self : Union[str, Any] ): """simple docstring""" A__ = OrderedDict({'input_ids': {0: 'batch', 1: 'sequence'}} ) if self.use_past: # BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344 self.fill_with_past_key_values_(_snake_case , direction='inputs' , inverted_values_shape=_snake_case ) A__ = {0: 'batch', 1: 'past_sequence + sequence'} else: A__ = {0: 'batch', 1: 'sequence'} return common_inputs @property def _a ( self : List[Any] ): """simple docstring""" return self._config.n_layer @property def _a ( self : Union[str, Any] ): """simple docstring""" return self._config.n_head @property def _a ( self : Union[str, Any] ): """simple docstring""" return 1E-3 def _a ( self : Dict , _snake_case : "PreTrainedTokenizer" , _snake_case : int = -1 , _snake_case : int = -1 , _snake_case : bool = False , _snake_case : Optional["TensorType"] = None , ): """simple docstring""" A__ = super(_snake_case , self ).generate_dummy_inputs( _snake_case , batch_size=_snake_case , seq_length=_snake_case , is_pair=_snake_case , framework=_snake_case ) # We need to order the input in the way they appears in the forward() A__ = 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 A__ , A__ = common_inputs['input_ids'].shape # Not using the same length for past_key_values A__ = seqlen + 2 A__ = self._config.hidden_size // self.num_attention_heads A__ = ( batch * self.num_attention_heads, head_dim, past_key_values_length, ) A__ = ( batch * self.num_attention_heads, past_key_values_length, head_dim, ) A__ = [ (torch.zeros(_snake_case ), torch.zeros(_snake_case )) for _ in range(self.num_layers ) ] A__ = common_inputs['attention_mask'] if self.use_past: A__ = ordered_inputs['attention_mask'].dtype A__ = torch.cat( [ordered_inputs['attention_mask'], torch.ones(_snake_case , _snake_case , dtype=_snake_case )] , dim=1 ) return ordered_inputs @property def _a ( self : Optional[Any] ): """simple docstring""" return 13
9
import logging from pathlib import Path import numpy as np import pytorch_lightning as pl import torch from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint from pytorch_lightning.utilities import rank_zero_only from utils_rag import save_json def A ( __UpperCamelCase ) -> Optional[int]: A__ = filter(lambda __UpperCamelCase : p.requires_grad , model.parameters() ) A__ = sum([np.prod(p.size() ) for p in model_parameters] ) return params SCREAMING_SNAKE_CASE__ = logging.getLogger(__name__) def A ( __UpperCamelCase , __UpperCamelCase ) -> Dict: if metric == "rouge2": A__ = '{val_avg_rouge2:.4f}-{step_count}' elif metric == "bleu": A__ = '{val_avg_bleu:.4f}-{step_count}' elif metric == "em": A__ = '{val_avg_em:.4f}-{step_count}' elif metric == "loss": A__ = '{val_avg_loss:.4f}-{step_count}' else: raise NotImplementedError( f'''seq2seq callbacks only support rouge2 and bleu, got {metric}, You can make your own by adding to this''' ' function.' ) A__ = ModelCheckpoint( dirpath=__UpperCamelCase , filename=__UpperCamelCase , monitor=f'''val_{metric}''' , mode='max' , save_top_k=1 , every_n_epochs=1 , ) return checkpoint_callback def A ( __UpperCamelCase , __UpperCamelCase ) -> Any: return EarlyStopping( monitor=f'''val_{metric}''' , mode='min' if 'loss' in metric else 'max' , patience=__UpperCamelCase , verbose=__UpperCamelCase , ) class __lowerCAmelCase ( pl.Callback ): """simple docstring""" def _a ( self : Dict , _snake_case : Union[str, Any] , _snake_case : str ): """simple docstring""" A__ = {F'''lr_group_{i}''': param['lr'] for i, param in enumerate(pl_module.trainer.optimizers[0].param_groups )} pl_module.logger.log_metrics(_snake_case ) @rank_zero_only def _a ( self : Union[str, Any] , _snake_case : pl.Trainer , _snake_case : pl.LightningModule , _snake_case : str , _snake_case : Optional[Any]=True ): """simple docstring""" logger.info(F'''***** {type_path} results at step {trainer.global_step:05d} *****''' ) A__ = trainer.callback_metrics trainer.logger.log_metrics({k: v for k, v in metrics.items() if k not in ['log', 'progress_bar', 'preds']} ) # Log results A__ = Path(pl_module.hparams.output_dir ) if type_path == "test": A__ = od / 'test_results.txt' A__ = od / 'test_generations.txt' else: # this never gets hit. I prefer not to save intermediate generations, and results are in metrics.json # If people want this it will be easy enough to add back. A__ = od / F'''{type_path}_results/{trainer.global_step:05d}.txt''' A__ = od / F'''{type_path}_generations/{trainer.global_step:05d}.txt''' results_file.parent.mkdir(exist_ok=_snake_case ) generations_file.parent.mkdir(exist_ok=_snake_case ) with open(_snake_case , 'a+' ) as writer: for key in sorted(_snake_case ): if key in ["log", "progress_bar", "preds"]: continue A__ = metrics[key] if isinstance(_snake_case , torch.Tensor ): A__ = val.item() A__ = F'''{key}: {val:.6f}\n''' writer.write(_snake_case ) if not save_generations: return if "preds" in metrics: A__ = '\n'.join(metrics['preds'] ) generations_file.open('w+' ).write(_snake_case ) @rank_zero_only def _a ( self : Dict , _snake_case : List[str] , _snake_case : List[Any] ): """simple docstring""" try: A__ = pl_module.model.model.num_parameters() except AttributeError: A__ = pl_module.model.num_parameters() A__ = count_trainable_parameters(_snake_case ) # mp stands for million parameters trainer.logger.log_metrics({'n_params': npars, 'mp': npars / 1E6, 'grad_mp': n_trainable_pars / 1E6} ) @rank_zero_only def _a ( self : int , _snake_case : pl.Trainer , _snake_case : pl.LightningModule ): """simple docstring""" save_json(pl_module.metrics , pl_module.metrics_save_path ) return self._write_logs(_snake_case , _snake_case , 'test' ) @rank_zero_only def _a ( self : Optional[Any] , _snake_case : pl.Trainer , _snake_case : List[Any] ): """simple docstring""" save_json(pl_module.metrics , pl_module.metrics_save_path ) # Uncommenting this will save val generations # return self._write_logs(trainer, pl_module, "valid")
9
1
import gc import unittest import numpy as np import torch from diffusers import AutoencoderKL, DDIMScheduler, DiTPipeline, DPMSolverMultistepScheduler, TransformeraDModel from diffusers.utils import is_xformers_available, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS, CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class __lowerCAmelCase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : int = DiTPipeline A__ : Dict = CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS A__ : Optional[Any] = PipelineTesterMixin.required_optional_params - { "latents", "num_images_per_prompt", "callback", "callback_steps", } A__ : Any = CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS A__ : List[str] = False def _a ( self : Any ): """simple docstring""" torch.manual_seed(0 ) A__ = TransformeraDModel( sample_size=16 , num_layers=2 , patch_size=4 , attention_head_dim=8 , num_attention_heads=2 , in_channels=4 , out_channels=8 , attention_bias=_snake_case , activation_fn='gelu-approximate' , num_embeds_ada_norm=10_00 , norm_type='ada_norm_zero' , norm_elementwise_affine=_snake_case , ) A__ = AutoencoderKL() A__ = DDIMScheduler() A__ = {'transformer': transformer.eval(), 'vae': vae.eval(), 'scheduler': scheduler} return components def _a ( self : Any , _snake_case : Optional[Any] , _snake_case : Tuple=0 ): """simple docstring""" if str(_snake_case ).startswith('mps' ): A__ = torch.manual_seed(_snake_case ) else: A__ = torch.Generator(device=_snake_case ).manual_seed(_snake_case ) A__ = { 'class_labels': [1], 'generator': generator, 'num_inference_steps': 2, 'output_type': 'numpy', } return inputs def _a ( self : Union[str, Any] ): """simple docstring""" A__ = 'cpu' A__ = self.get_dummy_components() A__ = self.pipeline_class(**_snake_case ) pipe.to(_snake_case ) pipe.set_progress_bar_config(disable=_snake_case ) A__ = self.get_dummy_inputs(_snake_case ) A__ = pipe(**_snake_case ).images A__ = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 16, 16, 3) ) A__ = np.array([0.2946, 0.6601, 0.4329, 0.3296, 0.4144, 0.5319, 0.7273, 0.5013, 0.4457] ) A__ = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(_snake_case , 1E-3 ) def _a ( self : Optional[int] ): """simple docstring""" self._test_inference_batch_single_identical(relax_max_difference=_snake_case , expected_max_diff=1E-3 ) @unittest.skipIf( torch_device != 'cuda' or not is_xformers_available() , reason='XFormers attention is only available with CUDA and `xformers` installed' , ) def _a ( self : Tuple ): """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) @require_torch_gpu @slow class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def _a ( self : Optional[Any] ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def _a ( self : Optional[int] ): """simple docstring""" A__ = torch.manual_seed(0 ) A__ = DiTPipeline.from_pretrained('facebook/DiT-XL-2-256' ) pipe.to('cuda' ) A__ = ['vase', 'umbrella', 'white shark', 'white wolf'] A__ = pipe.get_label_ids(_snake_case ) A__ = pipe(_snake_case , generator=_snake_case , num_inference_steps=40 , output_type='np' ).images for word, image in zip(_snake_case , _snake_case ): A__ = load_numpy( F'''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/dit/{word}.npy''' ) assert np.abs((expected_image - image).max() ) < 1E-2 def _a ( self : Any ): """simple docstring""" A__ = DiTPipeline.from_pretrained('facebook/DiT-XL-2-512' ) A__ = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) pipe.to('cuda' ) A__ = ['vase', 'umbrella'] A__ = pipe.get_label_ids(_snake_case ) A__ = torch.manual_seed(0 ) A__ = pipe(_snake_case , generator=_snake_case , num_inference_steps=25 , output_type='np' ).images for word, image in zip(_snake_case , _snake_case ): A__ = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' F'''/dit/{word}_512.npy''' ) assert np.abs((expected_image - image).max() ) < 1E-1
9
import warnings from typing import Any, Dict, List, Optional, Union import numpy as np from ...audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Optional[Any] = ["input_values", "attention_mask"] def __init__( self : str , _snake_case : int = 1 , _snake_case : int = 1_60_00 , _snake_case : float = 0.0 , _snake_case : bool = False , _snake_case : int = 80 , _snake_case : int = 16 , _snake_case : int = 64 , _snake_case : str = "hann_window" , _snake_case : float = 1.0 , _snake_case : float = 80 , _snake_case : float = 76_00 , _snake_case : float = 1E-10 , _snake_case : int = 2 , _snake_case : bool = True , **_snake_case : Union[str, Any] , ): """simple docstring""" super().__init__(feature_size=_snake_case , sampling_rate=_snake_case , padding_value=_snake_case , **_snake_case ) A__ = do_normalize A__ = return_attention_mask A__ = num_mel_bins A__ = hop_length A__ = win_length A__ = win_function A__ = frame_signal_scale A__ = fmin A__ = fmax A__ = mel_floor A__ = reduction_factor A__ = win_length * sampling_rate // 10_00 A__ = hop_length * sampling_rate // 10_00 A__ = optimal_fft_length(self.sample_size ) A__ = (self.n_fft // 2) + 1 A__ = window_function(window_length=self.sample_size , name=self.win_function , periodic=_snake_case ) A__ = mel_filter_bank( num_frequency_bins=self.n_freqs , num_mel_filters=self.num_mel_bins , min_frequency=self.fmin , max_frequency=self.fmax , sampling_rate=self.sampling_rate , norm='slaney' , mel_scale='slaney' , ) if frame_signal_scale != 1.0: warnings.warn( 'The argument `frame_signal_scale` is deprecated and will be removed in version 4.30.0 of Transformers' , _snake_case , ) if reduction_factor != 2.0: warnings.warn( 'The argument `reduction_factor` is deprecated and will be removed in version 4.30.0 of Transformers' , _snake_case , ) @staticmethod # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm def _a ( _snake_case : List[np.ndarray] , _snake_case : List[np.ndarray] , _snake_case : float = 0.0 ): """simple docstring""" if attention_mask is not None: A__ = np.array(_snake_case , np.intaa ) A__ = [] for vector, length in zip(_snake_case , attention_mask.sum(-1 ) ): A__ = (vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1E-7 ) if length < normed_slice.shape[0]: A__ = padding_value normed_input_values.append(_snake_case ) else: A__ = [(x - x.mean()) / np.sqrt(x.var() + 1E-7 ) for x in input_values] return normed_input_values def _a ( self : Tuple , _snake_case : np.ndarray , ): """simple docstring""" A__ = spectrogram( _snake_case , window=self.window , frame_length=self.sample_size , hop_length=self.sample_stride , fft_length=self.n_fft , mel_filters=self.mel_filters , mel_floor=self.mel_floor , log_mel='log10' , ) return log_mel_spec.T def __call__( self : List[str] , _snake_case : Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None , _snake_case : Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None , _snake_case : Union[bool, str, PaddingStrategy] = False , _snake_case : Optional[int] = None , _snake_case : bool = False , _snake_case : Optional[int] = None , _snake_case : Optional[bool] = None , _snake_case : Optional[Union[str, TensorType]] = None , _snake_case : Optional[int] = None , **_snake_case : Tuple , ): """simple docstring""" if audio is None and audio_target is None: raise ValueError('You must provide either `audio` or `audio_target` values.' ) if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' F''' {self.sampling_rate}. Please make sure that the provided audio input was sampled with''' F''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( 'It is strongly recommended to pass the ``sampling_rate`` argument to this function. ' 'Failing to do so can result in silent errors that might be hard to debug.' ) if audio is not None: A__ = self._process_audio( _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , **_snake_case , ) else: A__ = None if audio_target is not None: A__ = self._process_audio( _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , **_snake_case , ) if inputs is None: return inputs_target else: A__ = inputs_target['input_values'] A__ = inputs_target.get('attention_mask' ) if decoder_attention_mask is not None: A__ = decoder_attention_mask return inputs def _a ( self : Tuple , _snake_case : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , _snake_case : bool = False , _snake_case : Union[bool, str, PaddingStrategy] = False , _snake_case : Optional[int] = None , _snake_case : bool = False , _snake_case : Optional[int] = None , _snake_case : Optional[bool] = None , _snake_case : Optional[Union[str, TensorType]] = None , **_snake_case : Tuple , ): """simple docstring""" A__ = isinstance(_snake_case , np.ndarray ) and len(speech.shape ) > 1 if is_batched_numpy and len(speech.shape ) > 2: raise ValueError(F'''Only mono-channel audio is supported for input to {self}''' ) A__ = is_batched_numpy or ( isinstance(_snake_case , (list, tuple) ) and (isinstance(speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: A__ = [np.asarray(_snake_case , dtype=np.floataa ) for speech in speech] elif not is_batched and not isinstance(_snake_case , np.ndarray ): A__ = np.asarray(_snake_case , dtype=np.floataa ) elif isinstance(_snake_case , np.ndarray ) and speech.dtype is np.dtype(np.floataa ): A__ = speech.astype(np.floataa ) # always return batch if not is_batched: A__ = [speech] # needed to make pad() work on spectrogram inputs A__ = self.feature_size # convert into correct format for padding if is_target: A__ = [self._extract_mel_features(_snake_case ) for waveform in speech] A__ = BatchFeature({'input_values': features} ) A__ = self.num_mel_bins else: A__ = BatchFeature({'input_values': speech} ) A__ = self.pad( _snake_case , padding=_snake_case , max_length=_snake_case , truncation=_snake_case , pad_to_multiple_of=_snake_case , return_attention_mask=_snake_case , **_snake_case , ) A__ = feature_size_hack # convert input values to correct format A__ = padded_inputs['input_values'] if not isinstance(input_values[0] , np.ndarray ): A__ = [np.asarray(_snake_case , dtype=np.floataa ) for array in input_values] elif ( not isinstance(_snake_case , np.ndarray ) and isinstance(input_values[0] , np.ndarray ) and input_values[0].dtype is np.dtype(np.floataa ) ): A__ = [array.astype(np.floataa ) for array in input_values] elif isinstance(_snake_case , np.ndarray ) and input_values.dtype is np.dtype(np.floataa ): A__ = input_values.astype(np.floataa ) # convert attention_mask to correct format A__ = padded_inputs.get('attention_mask' ) if attention_mask is not None: A__ = [np.asarray(_snake_case , dtype=np.intaa ) for array in attention_mask] # zero-mean and unit-variance normalization if not is_target and self.do_normalize: A__ = ( attention_mask if self._get_padding_strategies(_snake_case , max_length=_snake_case ) is not PaddingStrategy.DO_NOT_PAD else None ) A__ = self.zero_mean_unit_var_norm( padded_inputs['input_values'] , attention_mask=_snake_case , padding_value=self.padding_value ) if return_tensors is not None: A__ = padded_inputs.convert_to_tensors(_snake_case ) return padded_inputs def _a ( self : Optional[Any] ): """simple docstring""" A__ = super().to_dict() # Don't serialize these as they are derived from the other properties. A__ = ['window', 'mel_filters', 'sample_size', 'sample_stride', 'n_fft', 'n_freqs'] for name in names: if name in output: del output[name] return output
9
1
import json import os import subprocess import unittest from ast import literal_eval import pytest from parameterized import parameterized, parameterized_class from . import is_sagemaker_available if is_sagemaker_available(): from sagemaker import Session, TrainingJobAnalytics from sagemaker.huggingface import HuggingFace @pytest.mark.skipif( literal_eval(os.getenv("TEST_SAGEMAKER" , "False" ) ) is not True , reason="Skipping test because should only be run when releasing minor transformers version" , ) @pytest.mark.usefixtures("sm_env" ) @parameterized_class( [ { "framework": "pytorch", "script": "run_glue_model_parallelism.py", "model_name_or_path": "roberta-large", "instance_type": "ml.p3dn.24xlarge", "results": {"train_runtime": 16_00, "eval_accuracy": 0.3, "eval_loss": 1.2}, }, { "framework": "pytorch", "script": "run_glue.py", "model_name_or_path": "roberta-large", "instance_type": "ml.p3dn.24xlarge", "results": {"train_runtime": 16_00, "eval_accuracy": 0.3, "eval_loss": 1.2}, }, ] ) class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def _a ( self : Any ): """simple docstring""" if self.framework == "pytorch": subprocess.run( F'''cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py'''.split() , encoding='utf-8' , check=_snake_case , ) assert hasattr(self , 'env' ) def _a ( self : Optional[int] , _snake_case : int ): """simple docstring""" A__ = { 'enabled': True, 'processes_per_host': 8, } A__ = { 'enabled': True, 'parameters': { 'microbatches': 4, 'placement_strategy': 'spread', 'pipeline': 'interleaved', 'optimize': 'speed', 'partitions': 4, 'ddp': True, }, } A__ = {'smdistributed': {'modelparallel': smp_options}, 'mpi': mpi_options} A__ = 'trainer' if self.script == 'run_glue.py' else 'smtrainer' # creates estimator return HuggingFace( entry_point=self.script , source_dir=self.env.test_path , role=self.env.role , image_uri=self.env.image_uri , base_job_name=F'''{self.env.base_job_name}-{instance_count}-smp-{name_extension}''' , instance_count=_snake_case , instance_type=self.instance_type , debugger_hook_config=_snake_case , hyperparameters={ **self.env.hyperparameters, 'model_name_or_path': self.model_name_or_path, 'max_steps': 5_00, } , metric_definitions=self.env.metric_definitions , distribution=_snake_case , py_version='py36' , ) def _a ( self : List[str] , _snake_case : Optional[Any] ): """simple docstring""" TrainingJobAnalytics(_snake_case ).export_csv(F'''{self.env.test_path}/{job_name}_metrics.csv''' ) @parameterized.expand([(1,)] ) def _a ( self : Dict , _snake_case : Optional[int] ): """simple docstring""" A__ = self.create_estimator(_snake_case ) # run training estimator.fit() # result dataframe A__ = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis A__ = list(result_metrics_df[result_metrics_df.metric_name == 'eval_accuracy']['value'] ) A__ = list(result_metrics_df[result_metrics_df.metric_name == 'eval_loss']['value'] ) # get train time from SageMaker job, this includes starting, preprocessing, stopping A__ = ( Session().describe_training_job(estimator.latest_training_job.name ).get('TrainingTimeInSeconds' , 99_99_99 ) ) # assert kpis assert train_runtime <= self.results["train_runtime"] assert all(t >= self.results['eval_accuracy'] for t in eval_accuracy ) assert all(t <= self.results['eval_loss'] for t in eval_loss ) # dump tests result into json file to share in PR with open(F'''{estimator.latest_training_job.name}.json''' , 'w' ) as outfile: json.dump({'train_time': train_runtime, 'eval_accuracy': eval_accuracy, 'eval_loss': eval_loss} , _snake_case )
9
import argparse import OmegaConf import torch from diffusers import DDIMScheduler, LDMPipeline, UNetLDMModel, VQModel def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: A__ = OmegaConf.load(__UpperCamelCase ) A__ = torch.load(__UpperCamelCase , map_location='cpu' )['model'] A__ = list(state_dict.keys() ) # extract state_dict for VQVAE A__ = {} A__ = 'first_stage_model.' for key in keys: if key.startswith(__UpperCamelCase ): A__ = state_dict[key] # extract state_dict for UNetLDM A__ = {} A__ = 'model.diffusion_model.' for key in keys: if key.startswith(__UpperCamelCase ): A__ = state_dict[key] A__ = config.model.params.first_stage_config.params A__ = config.model.params.unet_config.params A__ = VQModel(**__UpperCamelCase ).eval() vqvae.load_state_dict(__UpperCamelCase ) A__ = UNetLDMModel(**__UpperCamelCase ).eval() unet.load_state_dict(__UpperCamelCase ) A__ = DDIMScheduler( timesteps=config.model.params.timesteps , beta_schedule='scaled_linear' , beta_start=config.model.params.linear_start , beta_end=config.model.params.linear_end , clip_sample=__UpperCamelCase , ) A__ = LDMPipeline(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) pipeline.save_pretrained(__UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument('''--checkpoint_path''', type=str, required=True) parser.add_argument('''--config_path''', type=str, required=True) parser.add_argument('''--output_path''', type=str, required=True) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_ldm_original(args.checkpoint_path, args.config_path, args.output_path)
9
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 LevitImageProcessor class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def __init__( self : Any , _snake_case : Optional[int] , _snake_case : Any=7 , _snake_case : int=3 , _snake_case : Tuple=18 , _snake_case : Tuple=30 , _snake_case : Tuple=4_00 , _snake_case : List[str]=True , _snake_case : str=None , _snake_case : int=True , _snake_case : Tuple=None , _snake_case : Tuple=True , _snake_case : Optional[int]=[0.5, 0.5, 0.5] , _snake_case : Any=[0.5, 0.5, 0.5] , ): """simple docstring""" A__ = size if size is not None else {'shortest_edge': 18} A__ = crop_size if crop_size is not None else {'height': 18, 'width': 18} A__ = parent A__ = batch_size A__ = num_channels A__ = image_size A__ = min_resolution A__ = max_resolution A__ = do_resize A__ = size A__ = do_center_crop A__ = crop_size A__ = do_normalize A__ = image_mean A__ = image_std def _a ( self : Any ): """simple docstring""" return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "do_center_crop": self.do_center_crop, "size": self.size, "crop_size": self.crop_size, } @require_torch @require_vision class __lowerCAmelCase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Dict = LevitImageProcessor if is_vision_available() else None def _a ( self : str ): """simple docstring""" A__ = LevitImageProcessingTester(self ) @property def _a ( self : int ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def _a ( self : Optional[int] ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_snake_case , 'image_mean' ) ) self.assertTrue(hasattr(_snake_case , 'image_std' ) ) self.assertTrue(hasattr(_snake_case , 'do_normalize' ) ) self.assertTrue(hasattr(_snake_case , 'do_resize' ) ) self.assertTrue(hasattr(_snake_case , 'do_center_crop' ) ) self.assertTrue(hasattr(_snake_case , 'size' ) ) def _a ( self : int ): """simple docstring""" A__ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'shortest_edge': 18} ) self.assertEqual(image_processor.crop_size , {'height': 18, 'width': 18} ) A__ = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {'shortest_edge': 42} ) self.assertEqual(image_processor.crop_size , {'height': 84, 'width': 84} ) def _a ( self : str ): """simple docstring""" pass def _a ( self : Tuple ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=_snake_case ) for image in image_inputs: self.assertIsInstance(_snake_case , Image.Image ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) # Test batched A__ = image_processing(_snake_case , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) def _a ( self : Dict ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=_snake_case , numpify=_snake_case ) for image in image_inputs: self.assertIsInstance(_snake_case , np.ndarray ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) # Test batched A__ = image_processing(_snake_case , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) def _a ( self : Any ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=_snake_case , torchify=_snake_case ) for image in image_inputs: self.assertIsInstance(_snake_case , torch.Tensor ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) # Test batched A__ = image_processing(_snake_case , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , )
9
import json import os import torch from diffusers import UNetaDModel os.makedirs('''hub/hopper-medium-v2/unet/hor32''', exist_ok=True) os.makedirs('''hub/hopper-medium-v2/unet/hor128''', exist_ok=True) os.makedirs('''hub/hopper-medium-v2/value_function''', exist_ok=True) def A ( __UpperCamelCase ) -> Union[str, Any]: if hor == 128: A__ = ('DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D') A__ = (32, 128, 256) A__ = ('UpResnetBlock1D', 'UpResnetBlock1D') elif hor == 32: A__ = ('DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D') A__ = (32, 64, 128, 256) A__ = ('UpResnetBlock1D', 'UpResnetBlock1D', 'UpResnetBlock1D') A__ = torch.load(f'''/Users/bglickenhaus/Documents/diffuser/temporal_unet-hopper-mediumv2-hor{hor}.torch''' ) A__ = model.state_dict() A__ = { 'down_block_types': down_block_types, 'block_out_channels': block_out_channels, 'up_block_types': up_block_types, 'layers_per_block': 1, 'use_timestep_embedding': True, 'out_block_type': 'OutConv1DBlock', 'norm_num_groups': 8, 'downsample_each_block': False, 'in_channels': 14, 'out_channels': 14, 'extra_in_channels': 0, 'time_embedding_type': 'positional', 'flip_sin_to_cos': False, 'freq_shift': 1, 'sample_size': 65_536, 'mid_block_type': 'MidResTemporalBlock1D', 'act_fn': 'mish', } A__ = UNetaDModel(**__UpperCamelCase ) print(f'''length of state dict: {len(state_dict.keys() )}''' ) print(f'''length of value function dict: {len(hf_value_function.state_dict().keys() )}''' ) A__ = dict(zip(model.state_dict().keys() , hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): A__ = state_dict.pop(__UpperCamelCase ) hf_value_function.load_state_dict(__UpperCamelCase ) torch.save(hf_value_function.state_dict() , f'''hub/hopper-medium-v2/unet/hor{hor}/diffusion_pytorch_model.bin''' ) with open(f'''hub/hopper-medium-v2/unet/hor{hor}/config.json''' , 'w' ) as f: json.dump(__UpperCamelCase , __UpperCamelCase ) def A ( ) -> List[str]: A__ = { 'in_channels': 14, 'down_block_types': ('DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D'), 'up_block_types': (), 'out_block_type': 'ValueFunction', 'mid_block_type': 'ValueFunctionMidBlock1D', 'block_out_channels': (32, 64, 128, 256), 'layers_per_block': 1, 'downsample_each_block': True, 'sample_size': 65_536, 'out_channels': 14, 'extra_in_channels': 0, 'time_embedding_type': 'positional', 'use_timestep_embedding': True, 'flip_sin_to_cos': False, 'freq_shift': 1, 'norm_num_groups': 8, 'act_fn': 'mish', } A__ = torch.load('/Users/bglickenhaus/Documents/diffuser/value_function-hopper-mediumv2-hor32.torch' ) A__ = model A__ = UNetaDModel(**__UpperCamelCase ) print(f'''length of state dict: {len(state_dict.keys() )}''' ) print(f'''length of value function dict: {len(hf_value_function.state_dict().keys() )}''' ) A__ = dict(zip(state_dict.keys() , hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): A__ = state_dict.pop(__UpperCamelCase ) hf_value_function.load_state_dict(__UpperCamelCase ) torch.save(hf_value_function.state_dict() , 'hub/hopper-medium-v2/value_function/diffusion_pytorch_model.bin' ) with open('hub/hopper-medium-v2/value_function/config.json' , 'w' ) as f: json.dump(__UpperCamelCase , __UpperCamelCase ) if __name__ == "__main__": unet(3_2) # unet(128) value_function()
9
1
import os import tempfile import unittest from pathlib import Path from transformers import AutoConfig, is_tf_available from transformers.testing_utils import require_tf if is_tf_available(): import tensorflow as tf from transformers import TensorFlowBenchmark, TensorFlowBenchmarkArguments @require_tf class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def _a ( self : Dict , _snake_case : Optional[int] ): """simple docstring""" for model_result in results.values(): for batch_size, sequence_length in zip(model_result['bs'] , model_result['ss'] ): A__ = model_result['result'][batch_size][sequence_length] self.assertIsNotNone(_snake_case ) def _a ( self : Optional[int] ): """simple docstring""" A__ = 'sshleifer/tiny-gpt2' A__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=_snake_case , inference=_snake_case , sequence_lengths=[8] , batch_sizes=[1] , eager_mode=_snake_case , multi_process=_snake_case , ) A__ = TensorFlowBenchmark(_snake_case ) A__ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def _a ( self : List[str] ): """simple docstring""" A__ = 'sgugger/tiny-distilbert-classification' A__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=_snake_case , inference=_snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_snake_case , only_pretrain_model=_snake_case , ) A__ = TensorFlowBenchmark(_snake_case ) A__ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def _a ( self : Any ): """simple docstring""" A__ = 'sshleifer/tiny-gpt2' A__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=_snake_case , inference=_snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_snake_case , ) A__ = TensorFlowBenchmark(_snake_case ) A__ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def _a ( self : Tuple ): """simple docstring""" A__ = 'sshleifer/tiny-gpt2' A__ = AutoConfig.from_pretrained(_snake_case ) A__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=_snake_case , inference=_snake_case , sequence_lengths=[8] , batch_sizes=[1] , eager_mode=_snake_case , multi_process=_snake_case , ) A__ = TensorFlowBenchmark(_snake_case , [config] ) A__ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def _a ( self : Union[str, Any] ): """simple docstring""" A__ = 'sshleifer/tiny-gpt2' A__ = AutoConfig.from_pretrained(_snake_case ) A__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=_snake_case , inference=_snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_snake_case , ) A__ = TensorFlowBenchmark(_snake_case , [config] ) A__ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def _a ( self : Tuple ): """simple docstring""" A__ = 'sshleifer/tiny-gpt2' A__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=_snake_case , inference=_snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_snake_case , ) A__ = TensorFlowBenchmark(_snake_case ) A__ = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) def _a ( self : Tuple ): """simple docstring""" A__ = 'sshleifer/tiny-gpt2' A__ = AutoConfig.from_pretrained(_snake_case ) A__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=_snake_case , inference=_snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_snake_case , ) A__ = TensorFlowBenchmark(_snake_case , [config] ) A__ = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) def _a ( self : Any ): """simple docstring""" A__ = 'patrickvonplaten/t5-tiny-random' A__ = AutoConfig.from_pretrained(_snake_case ) A__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=_snake_case , inference=_snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_snake_case , ) A__ = TensorFlowBenchmark(_snake_case , configs=[config] ) A__ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) @unittest.skipIf(is_tf_available() and len(tf.config.list_physical_devices('GPU' ) ) == 0 , 'Cannot do xla on CPU.' ) def _a ( self : str ): """simple docstring""" A__ = 'sshleifer/tiny-gpt2' A__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=_snake_case , inference=_snake_case , sequence_lengths=[8] , batch_sizes=[1] , use_xla=_snake_case , multi_process=_snake_case , ) A__ = TensorFlowBenchmark(_snake_case ) A__ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def _a ( self : Any ): """simple docstring""" A__ = 'sshleifer/tiny-gpt2' with tempfile.TemporaryDirectory() as tmp_dir: A__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , inference=_snake_case , save_to_csv=_snake_case , sequence_lengths=[8] , batch_sizes=[1] , inference_time_csv_file=os.path.join(_snake_case , 'inf_time.csv' ) , inference_memory_csv_file=os.path.join(_snake_case , 'inf_mem.csv' ) , env_info_csv_file=os.path.join(_snake_case , 'env.csv' ) , multi_process=_snake_case , ) A__ = TensorFlowBenchmark(_snake_case ) benchmark.run() self.assertTrue(Path(os.path.join(_snake_case , 'inf_time.csv' ) ).exists() ) self.assertTrue(Path(os.path.join(_snake_case , 'inf_mem.csv' ) ).exists() ) self.assertTrue(Path(os.path.join(_snake_case , 'env.csv' ) ).exists() ) def _a ( self : int ): """simple docstring""" A__ = 'sshleifer/tiny-gpt2' def _check_summary_is_not_empty(_snake_case : str ): self.assertTrue(hasattr(_snake_case , 'sequential' ) ) self.assertTrue(hasattr(_snake_case , 'cumulative' ) ) self.assertTrue(hasattr(_snake_case , 'current' ) ) self.assertTrue(hasattr(_snake_case , 'total' ) ) with tempfile.TemporaryDirectory() as tmp_dir: A__ = TensorFlowBenchmarkArguments( models=[MODEL_ID] , inference=_snake_case , sequence_lengths=[8] , batch_sizes=[1] , log_filename=os.path.join(_snake_case , 'log.txt' ) , log_print=_snake_case , trace_memory_line_by_line=_snake_case , eager_mode=_snake_case , multi_process=_snake_case , ) A__ = TensorFlowBenchmark(_snake_case ) A__ = benchmark.run() _check_summary_is_not_empty(result.inference_summary ) self.assertTrue(Path(os.path.join(_snake_case , 'log.txt' ) ).exists() )
9
from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class __lowerCAmelCase : """simple docstring""" def __init__( self : Dict , _snake_case : Union[str, Any] , _snake_case : Optional[Any]=12 , _snake_case : Any=7 , _snake_case : List[str]=True , _snake_case : int=True , _snake_case : int=True , _snake_case : Tuple=99 , _snake_case : List[Any]=32 , _snake_case : Optional[int]=32 , _snake_case : List[str]=2 , _snake_case : List[str]=4 , _snake_case : List[Any]=37 , _snake_case : Union[str, Any]=0.1 , _snake_case : Tuple=0.1 , _snake_case : Dict=5_12 , _snake_case : Union[str, Any]=0.02 , _snake_case : Any=0 , _snake_case : Optional[Any]=None , ): """simple docstring""" A__ = parent A__ = batch_size A__ = seq_length A__ = is_training A__ = use_input_mask A__ = use_labels A__ = vocab_size A__ = hidden_size A__ = projection_dim A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = dropout A__ = attention_dropout A__ = max_position_embeddings A__ = initializer_range A__ = scope A__ = bos_token_id def _a ( self : Optional[Any] ): """simple docstring""" A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = None if self.use_input_mask: A__ = random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: A__ = input_mask.numpy() A__ , A__ = input_mask.shape A__ = np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(_snake_case ): A__ = 1 A__ = 0 A__ = self.get_config() return config, input_ids, tf.convert_to_tensor(_snake_case ) def _a ( self : Tuple ): """simple docstring""" return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def _a ( self : int , _snake_case : Union[str, Any] , _snake_case : Any , _snake_case : List[str] ): """simple docstring""" A__ = TFBlipTextModel(config=_snake_case ) A__ = model(_snake_case , attention_mask=_snake_case , training=_snake_case ) A__ = model(_snake_case , training=_snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def _a ( self : str ): """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ = config_and_inputs A__ = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_tf class __lowerCAmelCase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Tuple = (TFBlipTextModel,) if is_tf_available() else () A__ : Optional[int] = False A__ : Union[str, Any] = False A__ : Union[str, Any] = False def _a ( self : Any ): """simple docstring""" A__ = BlipTextModelTester(self ) A__ = ConfigTester(self , config_class=_snake_case , hidden_size=37 ) def _a ( self : List[str] ): """simple docstring""" self.config_tester.run_common_tests() def _a ( self : Union[str, Any] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def _a ( self : Tuple ): """simple docstring""" pass def _a ( self : int ): """simple docstring""" pass @unittest.skip(reason='Blip does not use inputs_embeds' ) def _a ( self : Any ): """simple docstring""" pass @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING' ) def _a ( self : str ): """simple docstring""" pass @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING' ) def _a ( self : Optional[Any] ): """simple docstring""" pass @slow def _a ( self : Union[str, Any] ): """simple docstring""" for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = TFBlipTextModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def _a ( self : int , _snake_case : int=True ): """simple docstring""" super().test_pt_tf_model_equivalence(allow_missing_keys=_snake_case )
9
1
import time from contextlib import contextmanager from pathlib import Path import pytest import requests from huggingface_hub.hf_api import HfApi, HfFolder SCREAMING_SNAKE_CASE__ = '''__DUMMY_TRANSFORMERS_USER__''' SCREAMING_SNAKE_CASE__ = '''Dummy User''' SCREAMING_SNAKE_CASE__ = '''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''' SCREAMING_SNAKE_CASE__ = '''https://hub-ci.huggingface.co''' SCREAMING_SNAKE_CASE__ = CI_HUB_ENDPOINT + '''/datasets/{repo_id}/resolve/{revision}/{path}''' SCREAMING_SNAKE_CASE__ = CI_HUB_ENDPOINT + '''/{repo_id}/resolve/{revision}/{filename}''' SCREAMING_SNAKE_CASE__ = Path('''~/.huggingface/hub_ci_token''').expanduser() @pytest.fixture def A ( __UpperCamelCase ) -> str: monkeypatch.setattr( 'huggingface_hub.file_download.HUGGINGFACE_CO_URL_TEMPLATE' , __UpperCamelCase ) @pytest.fixture def A ( __UpperCamelCase ) -> Optional[int]: monkeypatch.setattr('datasets.config.HF_ENDPOINT' , __UpperCamelCase ) monkeypatch.setattr('datasets.config.HUB_DATASETS_URL' , __UpperCamelCase ) @pytest.fixture def A ( __UpperCamelCase ) -> Union[str, Any]: monkeypatch.setattr('huggingface_hub.hf_api.HfFolder.path_token' , __UpperCamelCase ) @pytest.fixture def A ( __UpperCamelCase , __UpperCamelCase ) -> str: HfFolder.save_token(__UpperCamelCase ) yield HfFolder.delete_token() @pytest.fixture(scope='session' ) def A ( ) -> Tuple: return HfApi(endpoint=__UpperCamelCase ) @pytest.fixture(scope='session' ) def A ( __UpperCamelCase ) -> List[str]: A__ = HfFolder.get_token() HfFolder.save_token(__UpperCamelCase ) yield CI_HUB_USER_TOKEN if previous_token is not None: HfFolder.save_token(__UpperCamelCase ) @pytest.fixture def A ( __UpperCamelCase ) -> int: def _cleanup_repo(__UpperCamelCase ): hf_api.delete_repo(__UpperCamelCase , token=__UpperCamelCase , repo_type='dataset' ) return _cleanup_repo @pytest.fixture def A ( __UpperCamelCase ) -> List[Any]: @contextmanager def _temporary_repo(__UpperCamelCase ): try: yield repo_id finally: cleanup_repo(__UpperCamelCase ) return _temporary_repo @pytest.fixture(scope='session' ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: A__ = f'''repo_txt_data-{int(time.time() * 10E3 )}''' A__ = f'''{CI_HUB_USER}/{repo_name}''' hf_api.create_repo(__UpperCamelCase , token=__UpperCamelCase , repo_type='dataset' , private=__UpperCamelCase ) hf_api.upload_file( token=__UpperCamelCase , path_or_fileobj=str(__UpperCamelCase ) , path_in_repo='data/text_data.txt' , repo_id=__UpperCamelCase , repo_type='dataset' , ) yield repo_id try: hf_api.delete_repo(__UpperCamelCase , token=__UpperCamelCase , repo_type='dataset' ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Optional[Any]: return hf_private_dataset_repo_txt_data_ @pytest.fixture(scope='session' ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: A__ = f'''repo_zipped_txt_data-{int(time.time() * 10E3 )}''' A__ = f'''{CI_HUB_USER}/{repo_name}''' hf_api.create_repo(__UpperCamelCase , token=__UpperCamelCase , repo_type='dataset' , private=__UpperCamelCase ) hf_api.upload_file( token=__UpperCamelCase , path_or_fileobj=str(__UpperCamelCase ) , path_in_repo='data.zip' , repo_id=__UpperCamelCase , repo_type='dataset' , ) yield repo_id try: hf_api.delete_repo(__UpperCamelCase , token=__UpperCamelCase , repo_type='dataset' ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Dict: return hf_private_dataset_repo_zipped_txt_data_ @pytest.fixture(scope='session' ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[Any]: A__ = f'''repo_zipped_img_data-{int(time.time() * 10E3 )}''' A__ = f'''{CI_HUB_USER}/{repo_name}''' hf_api.create_repo(__UpperCamelCase , token=__UpperCamelCase , repo_type='dataset' , private=__UpperCamelCase ) hf_api.upload_file( token=__UpperCamelCase , path_or_fileobj=str(__UpperCamelCase ) , path_in_repo='data.zip' , repo_id=__UpperCamelCase , repo_type='dataset' , ) yield repo_id try: hf_api.delete_repo(__UpperCamelCase , token=__UpperCamelCase , repo_type='dataset' ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Any: return hf_private_dataset_repo_zipped_img_data_
9
from __future__ import annotations from typing import Any def A ( __UpperCamelCase ) -> int: if not postfix_notation: return 0 A__ = {'+', '-', '*', '/'} A__ = [] for token in postfix_notation: if token in operations: A__ , A__ = stack.pop(), stack.pop() if token == "+": stack.append(a + b ) elif token == "-": stack.append(a - b ) elif token == "*": stack.append(a * b ) else: if a * b < 0 and a % b != 0: stack.append(a // b + 1 ) else: stack.append(a // b ) else: stack.append(int(__UpperCamelCase ) ) return stack.pop() if __name__ == "__main__": import doctest doctest.testmod()
9
1
from ..utils import DummyObject, requires_backends class __lowerCAmelCase ( metaclass=UpperCAmelCase_ ): """simple docstring""" A__ : Union[str, Any] = ["flax"] def __init__( self : Union[str, Any] , *_snake_case : Dict , **_snake_case : Optional[int] ): """simple docstring""" requires_backends(self , ['flax'] ) @classmethod def _a ( cls : Dict , *_snake_case : str , **_snake_case : List[str] ): """simple docstring""" requires_backends(cls , ['flax'] ) @classmethod def _a ( cls : Union[str, Any] , *_snake_case : List[str] , **_snake_case : int ): """simple docstring""" requires_backends(cls , ['flax'] ) class __lowerCAmelCase ( metaclass=UpperCAmelCase_ ): """simple docstring""" A__ : int = ["flax"] def __init__( self : str , *_snake_case : Tuple , **_snake_case : Dict ): """simple docstring""" requires_backends(self , ['flax'] ) @classmethod def _a ( cls : List[str] , *_snake_case : Optional[int] , **_snake_case : int ): """simple docstring""" requires_backends(cls , ['flax'] ) @classmethod def _a ( cls : List[Any] , *_snake_case : List[Any] , **_snake_case : str ): """simple docstring""" requires_backends(cls , ['flax'] ) class __lowerCAmelCase ( metaclass=UpperCAmelCase_ ): """simple docstring""" A__ : List[Any] = ["flax"] def __init__( self : Any , *_snake_case : List[str] , **_snake_case : Any ): """simple docstring""" requires_backends(self , ['flax'] ) @classmethod def _a ( cls : int , *_snake_case : Tuple , **_snake_case : Optional[Any] ): """simple docstring""" requires_backends(cls , ['flax'] ) @classmethod def _a ( cls : Optional[int] , *_snake_case : str , **_snake_case : int ): """simple docstring""" requires_backends(cls , ['flax'] ) class __lowerCAmelCase ( metaclass=UpperCAmelCase_ ): """simple docstring""" A__ : Tuple = ["flax"] def __init__( self : Optional[int] , *_snake_case : str , **_snake_case : List[str] ): """simple docstring""" requires_backends(self , ['flax'] ) @classmethod def _a ( cls : Dict , *_snake_case : List[Any] , **_snake_case : Optional[int] ): """simple docstring""" requires_backends(cls , ['flax'] ) @classmethod def _a ( cls : Optional[Any] , *_snake_case : int , **_snake_case : Tuple ): """simple docstring""" requires_backends(cls , ['flax'] ) class __lowerCAmelCase ( metaclass=UpperCAmelCase_ ): """simple docstring""" A__ : Optional[Any] = ["flax"] def __init__( self : List[str] , *_snake_case : int , **_snake_case : Any ): """simple docstring""" requires_backends(self , ['flax'] ) @classmethod def _a ( cls : Union[str, Any] , *_snake_case : List[str] , **_snake_case : List[Any] ): """simple docstring""" requires_backends(cls , ['flax'] ) @classmethod def _a ( cls : int , *_snake_case : Union[str, Any] , **_snake_case : Any ): """simple docstring""" requires_backends(cls , ['flax'] ) class __lowerCAmelCase ( metaclass=UpperCAmelCase_ ): """simple docstring""" A__ : Optional[int] = ["flax"] def __init__( self : Optional[int] , *_snake_case : List[str] , **_snake_case : Optional[Any] ): """simple docstring""" requires_backends(self , ['flax'] ) @classmethod def _a ( cls : str , *_snake_case : List[str] , **_snake_case : Union[str, Any] ): """simple docstring""" requires_backends(cls , ['flax'] ) @classmethod def _a ( cls : Optional[Any] , *_snake_case : List[str] , **_snake_case : Tuple ): """simple docstring""" requires_backends(cls , ['flax'] ) class __lowerCAmelCase ( metaclass=UpperCAmelCase_ ): """simple docstring""" A__ : List[str] = ["flax"] def __init__( self : List[str] , *_snake_case : int , **_snake_case : List[Any] ): """simple docstring""" requires_backends(self , ['flax'] ) @classmethod def _a ( cls : Dict , *_snake_case : Any , **_snake_case : int ): """simple docstring""" requires_backends(cls , ['flax'] ) @classmethod def _a ( cls : Optional[Any] , *_snake_case : Tuple , **_snake_case : Optional[Any] ): """simple docstring""" requires_backends(cls , ['flax'] ) class __lowerCAmelCase ( metaclass=UpperCAmelCase_ ): """simple docstring""" A__ : Any = ["flax"] def __init__( self : Dict , *_snake_case : int , **_snake_case : Dict ): """simple docstring""" requires_backends(self , ['flax'] ) @classmethod def _a ( cls : Dict , *_snake_case : Optional[Any] , **_snake_case : List[Any] ): """simple docstring""" requires_backends(cls , ['flax'] ) @classmethod def _a ( cls : int , *_snake_case : List[str] , **_snake_case : Union[str, Any] ): """simple docstring""" requires_backends(cls , ['flax'] ) class __lowerCAmelCase ( metaclass=UpperCAmelCase_ ): """simple docstring""" A__ : Optional[Any] = ["flax"] def __init__( self : Tuple , *_snake_case : str , **_snake_case : Union[str, Any] ): """simple docstring""" requires_backends(self , ['flax'] ) @classmethod def _a ( cls : List[Any] , *_snake_case : Dict , **_snake_case : Any ): """simple docstring""" requires_backends(cls , ['flax'] ) @classmethod def _a ( cls : Optional[int] , *_snake_case : List[str] , **_snake_case : List[Any] ): """simple docstring""" requires_backends(cls , ['flax'] ) class __lowerCAmelCase ( metaclass=UpperCAmelCase_ ): """simple docstring""" A__ : Optional[int] = ["flax"] def __init__( self : List[Any] , *_snake_case : List[Any] , **_snake_case : Optional[int] ): """simple docstring""" requires_backends(self , ['flax'] ) @classmethod def _a ( cls : Any , *_snake_case : Any , **_snake_case : List[str] ): """simple docstring""" requires_backends(cls , ['flax'] ) @classmethod def _a ( cls : Optional[int] , *_snake_case : Optional[Any] , **_snake_case : List[Any] ): """simple docstring""" requires_backends(cls , ['flax'] ) class __lowerCAmelCase ( metaclass=UpperCAmelCase_ ): """simple docstring""" A__ : Dict = ["flax"] def __init__( self : Dict , *_snake_case : Dict , **_snake_case : int ): """simple docstring""" requires_backends(self , ['flax'] ) @classmethod def _a ( cls : Tuple , *_snake_case : str , **_snake_case : Any ): """simple docstring""" requires_backends(cls , ['flax'] ) @classmethod def _a ( cls : str , *_snake_case : Union[str, Any] , **_snake_case : List[str] ): """simple docstring""" requires_backends(cls , ['flax'] ) class __lowerCAmelCase ( metaclass=UpperCAmelCase_ ): """simple docstring""" A__ : str = ["flax"] def __init__( self : Any , *_snake_case : Optional[int] , **_snake_case : List[Any] ): """simple docstring""" requires_backends(self , ['flax'] ) @classmethod def _a ( cls : int , *_snake_case : Any , **_snake_case : Tuple ): """simple docstring""" requires_backends(cls , ['flax'] ) @classmethod def _a ( cls : Optional[int] , *_snake_case : Optional[Any] , **_snake_case : Union[str, Any] ): """simple docstring""" requires_backends(cls , ['flax'] ) class __lowerCAmelCase ( metaclass=UpperCAmelCase_ ): """simple docstring""" A__ : Union[str, Any] = ["flax"] def __init__( self : Union[str, Any] , *_snake_case : Tuple , **_snake_case : Union[str, Any] ): """simple docstring""" requires_backends(self , ['flax'] ) @classmethod def _a ( cls : int , *_snake_case : str , **_snake_case : Dict ): """simple docstring""" requires_backends(cls , ['flax'] ) @classmethod def _a ( cls : Tuple , *_snake_case : int , **_snake_case : Optional[int] ): """simple docstring""" requires_backends(cls , ['flax'] )
9
from __future__ import annotations def A ( __UpperCamelCase = 4 ) -> list[list[int]]: A__ = abs(__UpperCamelCase ) or 4 return [[1 + x + y * row_size for x in range(__UpperCamelCase )] for y in range(__UpperCamelCase )] def A ( __UpperCamelCase ) -> list[list[int]]: return reverse_row(transpose(__UpperCamelCase ) ) # OR.. transpose(reverse_column(matrix)) def A ( __UpperCamelCase ) -> list[list[int]]: return reverse_row(reverse_column(__UpperCamelCase ) ) # OR.. reverse_column(reverse_row(matrix)) def A ( __UpperCamelCase ) -> list[list[int]]: return reverse_column(transpose(__UpperCamelCase ) ) # OR.. transpose(reverse_row(matrix)) def A ( __UpperCamelCase ) -> list[list[int]]: A__ = [list(__UpperCamelCase ) for x in zip(*__UpperCamelCase )] return matrix def A ( __UpperCamelCase ) -> list[list[int]]: A__ = matrix[::-1] return matrix def A ( __UpperCamelCase ) -> list[list[int]]: A__ = [x[::-1] for x in matrix] return matrix def A ( __UpperCamelCase ) -> None: for i in matrix: print(*__UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = make_matrix() print('''\norigin:\n''') print_matrix(matrix) print('''\nrotate 90 counterclockwise:\n''') print_matrix(rotate_aa(matrix)) SCREAMING_SNAKE_CASE__ = make_matrix() print('''\norigin:\n''') print_matrix(matrix) print('''\nrotate 180:\n''') print_matrix(rotate_aaa(matrix)) SCREAMING_SNAKE_CASE__ = make_matrix() print('''\norigin:\n''') print_matrix(matrix) print('''\nrotate 270 counterclockwise:\n''') print_matrix(rotate_aaa(matrix))
9
1
import json from typing import Iterator, List, Union from tokenizers import AddedToken, Regex, Tokenizer, decoders, normalizers, pre_tokenizers, trainers from tokenizers.implementations.base_tokenizer import BaseTokenizer from tokenizers.models import Unigram from tokenizers.processors import TemplateProcessing class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Tuple , _snake_case : str = "▁" , _snake_case : bool = True , _snake_case : Union[str, AddedToken] = "<unk>" , _snake_case : Union[str, AddedToken] = "</s>" , _snake_case : Union[str, AddedToken] = "<pad>" , ): """simple docstring""" A__ = { 'pad': {'id': 0, 'token': pad_token}, 'eos': {'id': 1, 'token': eos_token}, 'unk': {'id': 2, 'token': unk_token}, } A__ = [None] * len(self.special_tokens ) for token_dict in self.special_tokens.values(): A__ = token_dict['token'] A__ = Tokenizer(Unigram() ) A__ = normalizers.Sequence( [ normalizers.Nmt(), normalizers.NFKC(), normalizers.Replace(Regex(' {2,}' ) , ' ' ), normalizers.Lowercase(), ] ) A__ = pre_tokenizers.Sequence( [ pre_tokenizers.Metaspace(replacement=_snake_case , add_prefix_space=_snake_case ), pre_tokenizers.Digits(individual_digits=_snake_case ), pre_tokenizers.Punctuation(), ] ) A__ = decoders.Metaspace(replacement=_snake_case , add_prefix_space=_snake_case ) A__ = TemplateProcessing( single=F'''$A {self.special_tokens["eos"]["token"]}''' , special_tokens=[(self.special_tokens['eos']['token'], self.special_tokens['eos']['id'])] , ) A__ = { 'model': 'SentencePieceUnigram', 'replacement': replacement, 'add_prefix_space': add_prefix_space, } super().__init__(_snake_case , _snake_case ) def _a ( self : int , _snake_case : Union[str, List[str]] , _snake_case : int = 80_00 , _snake_case : bool = True , ): """simple docstring""" A__ = trainers.UnigramTrainer( vocab_size=_snake_case , special_tokens=self.special_tokens_list , show_progress=_snake_case , ) if isinstance(_snake_case , _snake_case ): A__ = [files] self._tokenizer.train(_snake_case , trainer=_snake_case ) self.add_unk_id() def _a ( self : Any , _snake_case : Union[Iterator[str], Iterator[Iterator[str]]] , _snake_case : int = 80_00 , _snake_case : bool = True , ): """simple docstring""" A__ = trainers.UnigramTrainer( vocab_size=_snake_case , special_tokens=self.special_tokens_list , show_progress=_snake_case , ) self._tokenizer.train_from_iterator(_snake_case , trainer=_snake_case ) self.add_unk_id() def _a ( self : Union[str, Any] ): """simple docstring""" A__ = json.loads(self._tokenizer.to_str() ) A__ = self.special_tokens['unk']['id'] A__ = Tokenizer.from_str(json.dumps(_snake_case ) )
9
from __future__ import annotations from fractions import Fraction def A ( __UpperCamelCase , __UpperCamelCase ) -> bool: return ( num != den and num % 10 == den // 10 and (num // 10) / (den % 10) == num / den ) def A ( __UpperCamelCase ) -> list[str]: A__ = [] A__ = 11 A__ = int('1' + '0' * digit_len ) for num in range(__UpperCamelCase , __UpperCamelCase ): while den <= 99: if (num != den) and (num % 10 == den // 10) and (den % 10 != 0): if is_digit_cancelling(__UpperCamelCase , __UpperCamelCase ): solutions.append(f'''{num}/{den}''' ) den += 1 num += 1 A__ = 10 return solutions def A ( __UpperCamelCase = 2 ) -> int: A__ = 1.0 for fraction in fraction_list(__UpperCamelCase ): A__ = Fraction(__UpperCamelCase ) result *= frac.denominator / frac.numerator return int(__UpperCamelCase ) if __name__ == "__main__": print(solution())
9
1
import jax.numpy as jnp from ...utils import logging from ..ta.modeling_flax_ta import FlaxTaEncoderModel, FlaxTaForConditionalGeneration, FlaxTaModel from .configuration_mta import MTaConfig SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = '''T5Config''' def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> jnp.ndarray: A__ = jnp.zeros_like(__UpperCamelCase ) A__ = shifted_input_ids.at[:, 1:].set(input_ids[:, :-1] ) A__ = shifted_input_ids.at[:, 0].set(__UpperCamelCase ) A__ = jnp.where(shifted_input_ids == -100 , __UpperCamelCase , __UpperCamelCase ) return shifted_input_ids class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Tuple = "mt5" A__ : int = MTaConfig class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : str = "mt5" A__ : List[str] = MTaConfig class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Optional[int] = "mt5" A__ : Union[str, Any] = MTaConfig
9
# 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_tokenizers_available, is_torch_available SCREAMING_SNAKE_CASE__ = {'''configuration_mra''': ['''MRA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MraConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''MRA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MraForMaskedLM''', '''MraForMultipleChoice''', '''MraForQuestionAnswering''', '''MraForSequenceClassification''', '''MraForTokenClassification''', '''MraLayer''', '''MraModel''', '''MraPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_mra import MRA_PRETRAINED_CONFIG_ARCHIVE_MAP, MraConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mra import ( MRA_PRETRAINED_MODEL_ARCHIVE_LIST, MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, MraLayer, MraModel, MraPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
9
1
import re from filelock import FileLock try: import nltk SCREAMING_SNAKE_CASE__ = True except (ImportError, ModuleNotFoundError): SCREAMING_SNAKE_CASE__ = False if NLTK_AVAILABLE: with FileLock('''.lock''') as lock: nltk.download('''punkt''', quiet=True) def A ( __UpperCamelCase ) -> str: re.sub('<n>' , '' , __UpperCamelCase ) # remove pegasus newline char assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)" return "\n".join(nltk.sent_tokenize(__UpperCamelCase ) )
9
SCREAMING_SNAKE_CASE__ = ''' # Transformers installation ! pip install transformers datasets # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git ''' SCREAMING_SNAKE_CASE__ = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] SCREAMING_SNAKE_CASE__ = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
9
1
import unittest from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __lowerCAmelCase : """simple docstring""" @staticmethod def _a ( *_snake_case : Any , **_snake_case : Optional[int] ): """simple docstring""" pass @is_pipeline_test @require_torch @require_vision class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" A__ : Union[str, Any] = MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING def _a ( self : List[Any] , _snake_case : Union[str, Any] , _snake_case : Tuple , _snake_case : Union[str, Any] ): """simple docstring""" A__ = pipeline('visual-question-answering' , model='hf-internal-testing/tiny-vilt-random-vqa' ) A__ = [ { 'image': Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ), 'question': 'How many cats are there?', }, { 'image': './tests/fixtures/tests_samples/COCO/000000039769.png', 'question': 'How many cats are there?', }, ] return vqa_pipeline, examples def _a ( self : Optional[Any] , _snake_case : Union[str, Any] , _snake_case : List[str] ): """simple docstring""" A__ = vqa_pipeline(_snake_case , top_k=1 ) self.assertEqual( _snake_case , [ [{'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}], [{'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}], ] , ) @require_torch def _a ( self : Any ): """simple docstring""" A__ = pipeline('visual-question-answering' , model='hf-internal-testing/tiny-vilt-random-vqa' ) A__ = './tests/fixtures/tests_samples/COCO/000000039769.png' A__ = 'How many cats are there?' A__ = vqa_pipeline(image=_snake_case , question='How many cats are there?' , top_k=2 ) self.assertEqual( _snake_case , [{'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}, {'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}] ) A__ = vqa_pipeline({'image': image, 'question': question} , top_k=2 ) self.assertEqual( _snake_case , [{'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}, {'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}] ) @slow @require_torch def _a ( self : Any ): """simple docstring""" A__ = pipeline('visual-question-answering' , model='dandelin/vilt-b32-finetuned-vqa' ) A__ = './tests/fixtures/tests_samples/COCO/000000039769.png' A__ = 'How many cats are there?' A__ = vqa_pipeline(image=_snake_case , question=_snake_case , top_k=2 ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [{'score': 0.8799, 'answer': '2'}, {'score': 0.296, 'answer': '1'}] ) A__ = vqa_pipeline({'image': image, 'question': question} , top_k=2 ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [{'score': 0.8799, 'answer': '2'}, {'score': 0.296, 'answer': '1'}] ) A__ = vqa_pipeline( [{'image': image, 'question': question}, {'image': image, 'question': question}] , top_k=2 ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [[{'score': 0.8799, 'answer': '2'}, {'score': 0.296, 'answer': '1'}]] * 2 , ) @require_tf @unittest.skip('Visual question answering not implemented in TF' ) def _a ( self : Dict ): """simple docstring""" pass
9
import unittest from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __lowerCAmelCase : """simple docstring""" @staticmethod def _a ( *_snake_case : Any , **_snake_case : Optional[int] ): """simple docstring""" pass @is_pipeline_test @require_torch @require_vision class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" A__ : Union[str, Any] = MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING def _a ( self : List[Any] , _snake_case : Union[str, Any] , _snake_case : Tuple , _snake_case : Union[str, Any] ): """simple docstring""" A__ = pipeline('visual-question-answering' , model='hf-internal-testing/tiny-vilt-random-vqa' ) A__ = [ { 'image': Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ), 'question': 'How many cats are there?', }, { 'image': './tests/fixtures/tests_samples/COCO/000000039769.png', 'question': 'How many cats are there?', }, ] return vqa_pipeline, examples def _a ( self : Optional[Any] , _snake_case : Union[str, Any] , _snake_case : List[str] ): """simple docstring""" A__ = vqa_pipeline(_snake_case , top_k=1 ) self.assertEqual( _snake_case , [ [{'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}], [{'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}], ] , ) @require_torch def _a ( self : Any ): """simple docstring""" A__ = pipeline('visual-question-answering' , model='hf-internal-testing/tiny-vilt-random-vqa' ) A__ = './tests/fixtures/tests_samples/COCO/000000039769.png' A__ = 'How many cats are there?' A__ = vqa_pipeline(image=_snake_case , question='How many cats are there?' , top_k=2 ) self.assertEqual( _snake_case , [{'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}, {'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}] ) A__ = vqa_pipeline({'image': image, 'question': question} , top_k=2 ) self.assertEqual( _snake_case , [{'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}, {'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}] ) @slow @require_torch def _a ( self : Any ): """simple docstring""" A__ = pipeline('visual-question-answering' , model='dandelin/vilt-b32-finetuned-vqa' ) A__ = './tests/fixtures/tests_samples/COCO/000000039769.png' A__ = 'How many cats are there?' A__ = vqa_pipeline(image=_snake_case , question=_snake_case , top_k=2 ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [{'score': 0.8799, 'answer': '2'}, {'score': 0.296, 'answer': '1'}] ) A__ = vqa_pipeline({'image': image, 'question': question} , top_k=2 ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [{'score': 0.8799, 'answer': '2'}, {'score': 0.296, 'answer': '1'}] ) A__ = vqa_pipeline( [{'image': image, 'question': question}, {'image': image, 'question': question}] , top_k=2 ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [[{'score': 0.8799, 'answer': '2'}, {'score': 0.296, 'answer': '1'}]] * 2 , ) @require_tf @unittest.skip('Visual question answering not implemented in TF' ) def _a ( self : Dict ): """simple docstring""" pass
9
1
import logging import os from dataclasses import dataclass from typing import List, Optional, Union import tqdm from filelock import FileLock from transformers import ( BartTokenizer, BartTokenizerFast, DataProcessor, PreTrainedTokenizer, RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, is_tf_available, is_torch_available, ) SCREAMING_SNAKE_CASE__ = logging.getLogger(__name__) @dataclass(frozen=UpperCAmelCase_ ) class __lowerCAmelCase : """simple docstring""" A__ : str A__ : str A__ : Optional[str] = None A__ : Optional[str] = None A__ : Optional[str] = None @dataclass(frozen=UpperCAmelCase_ ) class __lowerCAmelCase : """simple docstring""" A__ : List[int] A__ : Optional[List[int]] = None A__ : Optional[List[int]] = None A__ : Optional[Union[int, float]] = None A__ : Optional[int] = None if is_torch_available(): import torch from torch.utils.data import Dataset class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : List[InputFeatures] def __init__( self : Optional[Any] , _snake_case : str , _snake_case : PreTrainedTokenizer , _snake_case : str , _snake_case : Optional[int] = None , _snake_case : Tuple=False , _snake_case : bool = False , ): """simple docstring""" A__ = hans_processors[task]() A__ = os.path.join( _snake_case , 'cached_{}_{}_{}_{}'.format( 'dev' if evaluate else 'train' , tokenizer.__class__.__name__ , str(_snake_case ) , _snake_case , ) , ) A__ = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) A__ , A__ = label_list[2], label_list[1] A__ = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. A__ = cached_features_file + '.lock' with FileLock(_snake_case ): if os.path.exists(_snake_case ) and not overwrite_cache: logger.info(F'''Loading features from cached file {cached_features_file}''' ) A__ = torch.load(_snake_case ) else: logger.info(F'''Creating features from dataset file at {data_dir}''' ) A__ = ( processor.get_dev_examples(_snake_case ) if evaluate else processor.get_train_examples(_snake_case ) ) logger.info('Training examples: %s' , len(_snake_case ) ) A__ = hans_convert_examples_to_features(_snake_case , _snake_case , _snake_case , _snake_case ) logger.info('Saving features into cached file %s' , _snake_case ) torch.save(self.features , _snake_case ) def __len__( self : Dict ): """simple docstring""" return len(self.features ) def __getitem__( self : Any , _snake_case : Dict ): """simple docstring""" return self.features[i] def _a ( self : str ): """simple docstring""" return self.label_list if is_tf_available(): import tensorflow as tf class __lowerCAmelCase : """simple docstring""" A__ : List[InputFeatures] def __init__( self : Union[str, Any] , _snake_case : str , _snake_case : PreTrainedTokenizer , _snake_case : str , _snake_case : Optional[int] = 1_28 , _snake_case : List[Any]=False , _snake_case : bool = False , ): """simple docstring""" A__ = hans_processors[task]() A__ = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) A__ , A__ = label_list[2], label_list[1] A__ = label_list A__ = processor.get_dev_examples(_snake_case ) if evaluate else processor.get_train_examples(_snake_case ) A__ = hans_convert_examples_to_features(_snake_case , _snake_case , _snake_case , _snake_case ) def gen(): for ex_index, ex in tqdm.tqdm(enumerate(self.features ) , desc='convert examples to features' ): if ex_index % 1_00_00 == 0: logger.info('Writing example %d of %d' % (ex_index, len(_snake_case )) ) yield ( { "example_id": 0, "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label, ) A__ = tf.data.Dataset.from_generator( _snake_case , ( { 'example_id': tf.intaa, 'input_ids': tf.intaa, 'attention_mask': tf.intaa, 'token_type_ids': tf.intaa, }, tf.intaa, ) , ( { 'example_id': tf.TensorShape([] ), 'input_ids': tf.TensorShape([None, None] ), 'attention_mask': tf.TensorShape([None, None] ), 'token_type_ids': tf.TensorShape([None, None] ), }, tf.TensorShape([] ), ) , ) def _a ( self : Tuple ): """simple docstring""" return self.dataset def __len__( self : Optional[int] ): """simple docstring""" return len(self.features ) def __getitem__( self : str , _snake_case : Optional[int] ): """simple docstring""" return self.features[i] def _a ( self : Any ): """simple docstring""" return self.label_list class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def _a ( self : List[str] , _snake_case : int ): """simple docstring""" return self._create_examples(self._read_tsv(os.path.join(_snake_case , 'heuristics_train_set.txt' ) ) , 'train' ) def _a ( self : List[Any] , _snake_case : Union[str, Any] ): """simple docstring""" return self._create_examples(self._read_tsv(os.path.join(_snake_case , 'heuristics_evaluation_set.txt' ) ) , 'dev' ) def _a ( self : Optional[Any] ): """simple docstring""" return ["contradiction", "entailment", "neutral"] def _a ( self : str , _snake_case : Tuple , _snake_case : Any ): """simple docstring""" A__ = [] for i, line in enumerate(_snake_case ): if i == 0: continue A__ = '%s-%s' % (set_type, line[0]) A__ = line[5] A__ = line[6] A__ = line[7][2:] if line[7].startswith('ex' ) else line[7] A__ = line[0] examples.append(InputExample(guid=_snake_case , text_a=_snake_case , text_b=_snake_case , label=_snake_case , pairID=_snake_case ) ) return examples def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , ) -> str: A__ = {label: i for i, label in enumerate(__UpperCamelCase )} A__ = [] for ex_index, example in tqdm.tqdm(enumerate(__UpperCamelCase ) , desc='convert examples to features' ): if ex_index % 10_000 == 0: logger.info('Writing example %d' % (ex_index) ) A__ = tokenizer( example.text_a , example.text_b , add_special_tokens=__UpperCamelCase , max_length=__UpperCamelCase , padding='max_length' , truncation=__UpperCamelCase , return_overflowing_tokens=__UpperCamelCase , ) A__ = label_map[example.label] if example.label in label_map else 0 A__ = int(example.pairID ) features.append(InputFeatures(**__UpperCamelCase , label=__UpperCamelCase , pairID=__UpperCamelCase ) ) for i, example in enumerate(examples[:5] ): logger.info('*** Example ***' ) logger.info(f'''guid: {example}''' ) logger.info(f'''features: {features[i]}''' ) return features SCREAMING_SNAKE_CASE__ = { '''hans''': 3, } SCREAMING_SNAKE_CASE__ = { '''hans''': HansProcessor, }
9
def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: if exponent == 1: return base if exponent % 2 == 0: A__ = _modexpt(__UpperCamelCase , exponent // 2 , __UpperCamelCase ) % modulo_value return (x * x) % modulo_value else: return (base * _modexpt(__UpperCamelCase , exponent - 1 , __UpperCamelCase )) % modulo_value def A ( __UpperCamelCase = 1_777 , __UpperCamelCase = 1_855 , __UpperCamelCase = 8 ) -> int: A__ = base for _ in range(1 , __UpperCamelCase ): A__ = _modexpt(__UpperCamelCase , __UpperCamelCase , 10**digits ) return result if __name__ == "__main__": print(f'{solution() = }')
9
1
from .integrations import ( is_optuna_available, is_ray_available, is_sigopt_available, is_wandb_available, run_hp_search_optuna, run_hp_search_ray, run_hp_search_sigopt, run_hp_search_wandb, ) from .trainer_utils import ( HPSearchBackend, default_hp_space_optuna, default_hp_space_ray, default_hp_space_sigopt, default_hp_space_wandb, ) from .utils import logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) class __lowerCAmelCase : """simple docstring""" A__ : str A__ : str = None @staticmethod def _a ( ): """simple docstring""" raise NotImplementedError def _a ( self : List[str] , _snake_case : List[Any] , _snake_case : int , _snake_case : str , **_snake_case : Dict ): """simple docstring""" raise NotImplementedError def _a ( self : List[Any] , _snake_case : List[Any] ): """simple docstring""" raise NotImplementedError def _a ( self : Optional[Any] ): """simple docstring""" if not self.is_available(): raise RuntimeError( F'''You picked the {self.name} backend, but it is not installed. Run {self.pip_install()}.''' ) @classmethod def _a ( cls : Optional[int] ): """simple docstring""" return F'''`pip install {cls.pip_package or cls.name}`''' class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : str = "optuna" @staticmethod def _a ( ): """simple docstring""" return is_optuna_available() def _a ( self : Optional[int] , _snake_case : int , _snake_case : int , _snake_case : str , **_snake_case : Union[str, Any] ): """simple docstring""" return run_hp_search_optuna(_snake_case , _snake_case , _snake_case , **_snake_case ) def _a ( self : Optional[Any] , _snake_case : Tuple ): """simple docstring""" return default_hp_space_optuna(_snake_case ) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Any = "ray" A__ : List[str] = "'ray[tune]'" @staticmethod def _a ( ): """simple docstring""" return is_ray_available() def _a ( self : List[Any] , _snake_case : Optional[int] , _snake_case : int , _snake_case : str , **_snake_case : List[str] ): """simple docstring""" return run_hp_search_ray(_snake_case , _snake_case , _snake_case , **_snake_case ) def _a ( self : Optional[Any] , _snake_case : int ): """simple docstring""" return default_hp_space_ray(_snake_case ) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Optional[int] = "sigopt" @staticmethod def _a ( ): """simple docstring""" return is_sigopt_available() def _a ( self : Optional[Any] , _snake_case : Tuple , _snake_case : int , _snake_case : str , **_snake_case : List[str] ): """simple docstring""" return run_hp_search_sigopt(_snake_case , _snake_case , _snake_case , **_snake_case ) def _a ( self : Tuple , _snake_case : List[Any] ): """simple docstring""" return default_hp_space_sigopt(_snake_case ) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : List[str] = "wandb" @staticmethod def _a ( ): """simple docstring""" return is_wandb_available() def _a ( self : str , _snake_case : str , _snake_case : int , _snake_case : str , **_snake_case : Tuple ): """simple docstring""" return run_hp_search_wandb(_snake_case , _snake_case , _snake_case , **_snake_case ) def _a ( self : Any , _snake_case : Optional[int] ): """simple docstring""" return default_hp_space_wandb(_snake_case ) SCREAMING_SNAKE_CASE__ = { HPSearchBackend(backend.name): backend for backend in [OptunaBackend, RayTuneBackend, SigOptBackend, WandbBackend] } def A ( ) -> str: A__ = [backend for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() if backend.is_available()] if len(__UpperCamelCase ) > 0: A__ = available_backends[0].name if len(__UpperCamelCase ) > 1: logger.info( f'''{len(__UpperCamelCase )} hyperparameter search backends available. Using {name} as the default.''' ) return name raise RuntimeError( 'No hyperparameter search backend available.\n' + '\n'.join( f''' - To install {backend.name} run {backend.pip_install()}''' for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() ) )
9
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() SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) def A ( __UpperCamelCase , __UpperCamelCase=False , __UpperCamelCase=False ) -> Dict: A__ = 'backbone.' if is_semantic else '' A__ = [] 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 A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=False , __UpperCamelCase=False ) -> Optional[Any]: for i in range(config.num_hidden_layers ): A__ = 'backbone.' if is_semantic else '' # queries, keys and values A__ = state_dict.pop(f'''{prefix}blocks.{i}.attn.qkv.weight''' ) A__ = state_dict.pop(f'''{prefix}blocks.{i}.attn.q_bias''' ) A__ = state_dict.pop(f'''{prefix}blocks.{i}.attn.v_bias''' ) A__ = in_proj_weight[ : config.hidden_size, : ] A__ = q_bias A__ = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] A__ = in_proj_weight[ -config.hidden_size :, : ] A__ = v_bias # gamma_1 and gamma_2 # we call them lambda because otherwise they are renamed when using .from_pretrained A__ = state_dict.pop(f'''{prefix}blocks.{i}.gamma_1''' ) A__ = state_dict.pop(f'''{prefix}blocks.{i}.gamma_2''' ) A__ = gamma_a A__ = gamma_a def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Union[str, Any]: A__ = dct.pop(__UpperCamelCase ) A__ = val def A ( ) -> Dict: A__ = 'http://images.cocodataset.org/val2017/000000039769.jpg' A__ = Image.open(requests.get(__UpperCamelCase , stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=False ) -> str: A__ = False if 'rvlcdip' in checkpoint_url else True A__ = BeitConfig(use_absolute_position_embeddings=__UpperCamelCase , use_mask_token=__UpperCamelCase ) # size of the architecture if "large" in checkpoint_url or "dit-l" in checkpoint_url: A__ = 1_024 A__ = 4_096 A__ = 24 A__ = 16 # labels if "rvlcdip" in checkpoint_url: A__ = 16 A__ = 'huggingface/label-files' A__ = 'rvlcdip-id2label.json' A__ = json.load(open(hf_hub_download(__UpperCamelCase , __UpperCamelCase , repo_type='dataset' ) , 'r' ) ) A__ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} A__ = idalabel A__ = {v: k for k, v in idalabel.items()} # load state_dict of original model, remove and rename some keys A__ = torch.hub.load_state_dict_from_url(__UpperCamelCase , map_location='cpu' )['model'] A__ = create_rename_keys(__UpperCamelCase , has_lm_head=__UpperCamelCase ) for src, dest in rename_keys: rename_key(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) read_in_q_k_v(__UpperCamelCase , __UpperCamelCase , has_lm_head=__UpperCamelCase ) # load HuggingFace model A__ = BeitForMaskedImageModeling(__UpperCamelCase ) if has_lm_head else BeitForImageClassification(__UpperCamelCase ) model.eval() model.load_state_dict(__UpperCamelCase ) # Check outputs on an image A__ = BeitImageProcessor( size=config.image_size , resample=PILImageResampling.BILINEAR , do_center_crop=__UpperCamelCase ) A__ = prepare_img() A__ = image_processor(images=__UpperCamelCase , return_tensors='pt' ) A__ = encoding['pixel_values'] A__ = model(__UpperCamelCase ) A__ = outputs.logits # verify logits A__ = [1, 16] if 'rvlcdip' in checkpoint_url else [1, 196, 8_192] assert logits.shape == torch.Size(__UpperCamelCase ), "Shape of logits not as expected" Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) print(f'''Saving model to {pytorch_dump_folder_path}''' ) model.save_pretrained(__UpperCamelCase ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(__UpperCamelCase ) if push_to_hub: if has_lm_head: A__ = 'dit-base' if 'base' in checkpoint_url else 'dit-large' else: A__ = '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(__UpperCamelCase , __UpperCamelCase ) , organization='nielsr' , commit_message='Add image processor' , use_temp_dir=__UpperCamelCase , ) model.push_to_hub( repo_path_or_name=Path(__UpperCamelCase , __UpperCamelCase ) , organization='nielsr' , commit_message='Add model' , use_temp_dir=__UpperCamelCase , ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = 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''', ) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_dit_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub)
9
1
def A ( __UpperCamelCase ) -> str: return " ".join(input_str.split()[::-1] ) if __name__ == "__main__": import doctest doctest.testmod()
9
SCREAMING_SNAKE_CASE__ = { '''A''': ['''B''', '''C''', '''E'''], '''B''': ['''A''', '''D''', '''E'''], '''C''': ['''A''', '''F''', '''G'''], '''D''': ['''B'''], '''E''': ['''A''', '''B''', '''D'''], '''F''': ['''C'''], '''G''': ['''C'''], } def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> list[str]: A__ = set() # keep track of all the paths to be checked A__ = [[start]] # return path if start is goal if start == goal: return [start] # keeps looping until all possible paths have been checked while queue: # pop the first path from the queue A__ = queue.pop(0 ) # get the last node from the path A__ = path[-1] if node not in explored: A__ = graph[node] # go through all neighbour nodes, construct a new path and # push it into the queue for neighbour in neighbours: A__ = list(__UpperCamelCase ) new_path.append(__UpperCamelCase ) queue.append(__UpperCamelCase ) # return path if neighbour is goal if neighbour == goal: return new_path # mark node as explored explored.add(__UpperCamelCase ) # in case there's no path between the 2 nodes return [] def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: if not graph or start not in graph or target not in graph: return -1 if start == target: return 0 A__ = [start] A__ = set(__UpperCamelCase ) # Keep tab on distances from `start` node. A__ = {start: 0, target: -1} while queue: A__ = queue.pop(0 ) if node == target: A__ = ( dist[node] if dist[target] == -1 else min(dist[target] , dist[node] ) ) for adjacent in graph[node]: if adjacent not in visited: visited.add(__UpperCamelCase ) queue.append(__UpperCamelCase ) A__ = dist[node] + 1 return dist[target] if __name__ == "__main__": print(bfs_shortest_path(demo_graph, '''G''', '''D''')) # returns ['G', 'C', 'A', 'B', 'D'] print(bfs_shortest_path_distance(demo_graph, '''G''', '''D''')) # returns 4
9
1
import os import posixpath import uuid from dataclasses import dataclass from typing import TYPE_CHECKING, Iterable, List, Optional, Tuple, Union import numpy as np import pyarrow as pa import datasets from datasets.arrow_writer import ArrowWriter, ParquetWriter from datasets.config import MAX_SHARD_SIZE from datasets.filesystems import ( is_remote_filesystem, rename, ) from datasets.iterable_dataset import _BaseExamplesIterable from datasets.utils.py_utils import convert_file_size_to_int SCREAMING_SNAKE_CASE__ = datasets.utils.logging.get_logger(__name__) if TYPE_CHECKING: import pyspark @dataclass class __lowerCAmelCase ( datasets.BuilderConfig ): """simple docstring""" A__ : Optional[datasets.Features] = None def A ( __UpperCamelCase , __UpperCamelCase , ) -> Optional[int]: import pyspark def generate_fn(): A__ = df.select('*' , pyspark.sql.functions.spark_partition_id().alias('part_id' ) ) for partition_id in partition_order: A__ = df_with_partition_id.select('*' ).where(f'''part_id = {partition_id}''' ).drop('part_id' ) A__ = partition_df.collect() A__ = 0 for row in rows: yield f'''{partition_id}_{row_id}''', row.asDict() row_id += 1 return generate_fn class __lowerCAmelCase ( _BaseExamplesIterable ): """simple docstring""" def __init__( self : str , _snake_case : "pyspark.sql.DataFrame" , _snake_case : List[str]=None , ): """simple docstring""" A__ = df A__ = partition_order or range(self.df.rdd.getNumPartitions() ) A__ = _generate_iterable_examples(self.df , self.partition_order ) def __iter__( self : Optional[int] ): """simple docstring""" yield from self.generate_examples_fn() def _a ( self : int , _snake_case : np.random.Generator ): """simple docstring""" A__ = list(range(self.df.rdd.getNumPartitions() ) ) generator.shuffle(_snake_case ) return SparkExamplesIterable(self.df , partition_order=_snake_case ) def _a ( self : Optional[int] , _snake_case : int , _snake_case : int ): """simple docstring""" A__ = self.split_shard_indices_by_worker(_snake_case , _snake_case ) return SparkExamplesIterable(self.df , partition_order=_snake_case ) @property def _a ( self : Tuple ): """simple docstring""" return len(self.partition_order ) class __lowerCAmelCase ( datasets.DatasetBuilder ): """simple docstring""" A__ : Optional[int] = SparkConfig def __init__( self : Optional[Any] , _snake_case : "pyspark.sql.DataFrame" , _snake_case : str = None , _snake_case : str = None , **_snake_case : Optional[int] , ): """simple docstring""" import pyspark A__ = pyspark.sql.SparkSession.builder.getOrCreate() A__ = df A__ = working_dir super().__init__( cache_dir=_snake_case , config_name=str(self.df.semanticHash() ) , **_snake_case , ) def _a ( self : List[Any] ): """simple docstring""" def create_cache_and_write_probe(_snake_case : List[str] ): # makedirs with exist_ok will recursively create the directory. It will not throw an error if directories # already exist. os.makedirs(self._cache_dir , exist_ok=_snake_case ) A__ = os.path.join(self._cache_dir , 'fs_test' + uuid.uuida().hex ) # Opening the file in append mode will create a new file unless it already exists, in which case it will not # change the file contents. open(_snake_case , 'a' ) return [probe_file] if self._spark.conf.get('spark.master' , '' ).startswith('local' ): return # If the cluster is multi-node, make sure that the user provided a cache_dir and that it is on an NFS # accessible to the driver. # TODO: Stream batches to the driver using ArrowCollectSerializer instead of throwing an error. if self._cache_dir: A__ = ( self._spark.sparkContext.parallelize(range(1 ) , 1 ).mapPartitions(_snake_case ).collect() ) if os.path.isfile(probe[0] ): return raise ValueError( 'When using Dataset.from_spark on a multi-node cluster, the driver and all workers should be able to access cache_dir' ) def _a ( self : str ): """simple docstring""" return datasets.DatasetInfo(features=self.config.features ) def _a ( self : Union[str, Any] , _snake_case : datasets.download.download_manager.DownloadManager ): """simple docstring""" return [datasets.SplitGenerator(name=datasets.Split.TRAIN )] def _a ( self : Union[str, Any] , _snake_case : List[str] ): """simple docstring""" import pyspark def get_arrow_batch_size(_snake_case : str ): for batch in it: yield pa.RecordBatch.from_pydict({'batch_bytes': [batch.nbytes]} ) A__ = self.df.count() A__ = df_num_rows if df_num_rows <= 1_00 else 1_00 # Approximate the size of each row (in Arrow format) by averaging over a max-100-row sample. A__ = ( self.df.limit(_snake_case ) .repartition(1 ) .mapInArrow(_snake_case , 'batch_bytes: long' ) .agg(pyspark.sql.functions.sum('batch_bytes' ).alias('sample_bytes' ) ) .collect()[0] .sample_bytes / sample_num_rows ) A__ = approx_bytes_per_row * df_num_rows if approx_total_size > max_shard_size: # Make sure there is at least one row per partition. A__ = min(_snake_case , int(approx_total_size / max_shard_size ) ) A__ = self.df.repartition(_snake_case ) def _a ( self : List[Any] , _snake_case : str , _snake_case : str , _snake_case : int , ): """simple docstring""" import pyspark A__ = ParquetWriter if file_format == 'parquet' else ArrowWriter A__ = os.path.join(self._working_dir , os.path.basename(_snake_case ) ) if self._working_dir else fpath A__ = file_format == 'parquet' # Define these so that we don't reference self in write_arrow, which will result in a pickling error due to # pickling the SparkContext. A__ = self.config.features A__ = self._writer_batch_size A__ = self._fs.storage_options def write_arrow(_snake_case : Any ): # Within the same SparkContext, no two task attempts will share the same attempt ID. A__ = pyspark.TaskContext().taskAttemptId() A__ = next(_snake_case , _snake_case ) if first_batch is None: # Some partitions might not receive any data. return pa.RecordBatch.from_arrays( [[task_id], [0], [0]] , names=['task_id', 'num_examples', 'num_bytes'] , ) A__ = 0 A__ = writer_class( features=_snake_case , path=working_fpath.replace('SSSSS' , F'''{shard_id:05d}''' ).replace('TTTTT' , F'''{task_id:05d}''' ) , writer_batch_size=_snake_case , storage_options=_snake_case , embed_local_files=_snake_case , ) A__ = pa.Table.from_batches([first_batch] ) writer.write_table(_snake_case ) for batch in it: if max_shard_size is not None and writer._num_bytes >= max_shard_size: A__ , A__ = writer.finalize() writer.close() yield pa.RecordBatch.from_arrays( [[task_id], [num_examples], [num_bytes]] , names=['task_id', 'num_examples', 'num_bytes'] , ) shard_id += 1 A__ = writer_class( features=writer._features , path=working_fpath.replace('SSSSS' , F'''{shard_id:05d}''' ).replace('TTTTT' , F'''{task_id:05d}''' ) , writer_batch_size=_snake_case , storage_options=_snake_case , embed_local_files=_snake_case , ) A__ = pa.Table.from_batches([batch] ) writer.write_table(_snake_case ) if writer._num_bytes > 0: A__ , A__ = writer.finalize() writer.close() yield pa.RecordBatch.from_arrays( [[task_id], [num_examples], [num_bytes]] , names=['task_id', 'num_examples', 'num_bytes'] , ) if working_fpath != fpath: for file in os.listdir(os.path.dirname(_snake_case ) ): A__ = os.path.join(os.path.dirname(_snake_case ) , os.path.basename(_snake_case ) ) shutil.move(_snake_case , _snake_case ) A__ = ( self.df.mapInArrow(_snake_case , 'task_id: long, num_examples: long, num_bytes: long' ) .groupBy('task_id' ) .agg( pyspark.sql.functions.sum('num_examples' ).alias('total_num_examples' ) , pyspark.sql.functions.sum('num_bytes' ).alias('total_num_bytes' ) , pyspark.sql.functions.count('num_bytes' ).alias('num_shards' ) , pyspark.sql.functions.collect_list('num_examples' ).alias('shard_lengths' ) , ) .collect() ) for row in stats: yield row.task_id, (row.total_num_examples, row.total_num_bytes, row.num_shards, row.shard_lengths) def _a ( self : List[Any] , _snake_case : "datasets.SplitGenerator" , _snake_case : str = "arrow" , _snake_case : Optional[Union[str, int]] = None , _snake_case : Optional[int] = None , **_snake_case : Dict , ): """simple docstring""" self._validate_cache_dir() A__ = convert_file_size_to_int(max_shard_size or MAX_SHARD_SIZE ) self._repartition_df_if_needed(_snake_case ) A__ = not is_remote_filesystem(self._fs ) A__ = os.path.join if is_local else posixpath.join A__ = '-TTTTT-SSSSS-of-NNNNN' A__ = F'''{self.name}-{split_generator.name}{SUFFIX}.{file_format}''' A__ = path_join(self._output_dir , _snake_case ) A__ = 0 A__ = 0 A__ = 0 A__ = [] A__ = [] for task_id, content in self._prepare_split_single(_snake_case , _snake_case , _snake_case ): ( ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ) = content if num_bytes > 0: total_num_examples += num_examples total_num_bytes += num_bytes total_shards += num_shards task_id_and_num_shards.append((task_id, num_shards) ) all_shard_lengths.extend(_snake_case ) A__ = total_num_examples A__ = total_num_bytes # should rename everything at the end logger.debug(F'''Renaming {total_shards} shards.''' ) if total_shards > 1: A__ = all_shard_lengths # Define fs outside of _rename_shard so that we don't reference self in the function, which will result in a # pickling error due to pickling the SparkContext. A__ = self._fs # use the -SSSSS-of-NNNNN pattern def _rename_shard( _snake_case : int , _snake_case : int , _snake_case : int , ): rename( _snake_case , fpath.replace('SSSSS' , F'''{shard_id:05d}''' ).replace('TTTTT' , F'''{task_id:05d}''' ) , fpath.replace('TTTTT-SSSSS' , F'''{global_shard_id:05d}''' ).replace('NNNNN' , F'''{total_shards:05d}''' ) , ) A__ = [] A__ = 0 for i in range(len(_snake_case ) ): A__ , A__ = task_id_and_num_shards[i] for shard_id in range(_snake_case ): args.append([task_id, shard_id, global_shard_id] ) global_shard_id += 1 self._spark.sparkContext.parallelize(_snake_case , len(_snake_case ) ).map(lambda _snake_case : _rename_shard(*_snake_case ) ).collect() else: # don't use any pattern A__ = 0 A__ = task_id_and_num_shards[0][0] self._rename( fpath.replace('SSSSS' , F'''{shard_id:05d}''' ).replace('TTTTT' , F'''{task_id:05d}''' ) , fpath.replace(_snake_case , '' ) , ) def _a ( self : List[Any] , _snake_case : "datasets.SplitGenerator" , ): """simple docstring""" return SparkExamplesIterable(self.df )
9
def A ( __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: A__ = 0 A__ = len(__UpperCamelCase ) - 1 while left <= right: # avoid divided by 0 during interpolation if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: return left else: return None A__ = left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left] ) # out of range check if point < 0 or point >= len(__UpperCamelCase ): return None A__ = sorted_collection[point] if current_item == item: return point else: if point < left: A__ = left A__ = point elif point > right: A__ = right A__ = point else: if item < current_item: A__ = point - 1 else: A__ = point + 1 return None def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: # avoid divided by 0 during interpolation if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: return left else: return None A__ = left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left] ) # out of range check if point < 0 or point >= len(__UpperCamelCase ): return None if sorted_collection[point] == item: return point elif point < left: return interpolation_search_by_recursion(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) elif point > right: return interpolation_search_by_recursion(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) else: if sorted_collection[point] > item: return interpolation_search_by_recursion( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , point - 1 ) else: return interpolation_search_by_recursion( __UpperCamelCase , __UpperCamelCase , point + 1 , __UpperCamelCase ) def A ( __UpperCamelCase ) -> List[str]: if collection != sorted(__UpperCamelCase ): raise ValueError('Collection must be ascending sorted' ) return True if __name__ == "__main__": import sys SCREAMING_SNAKE_CASE__ = 0 if debug == 1: SCREAMING_SNAKE_CASE__ = [1_0, 3_0, 4_0, 4_5, 5_0, 6_6, 7_7, 9_3] try: __assert_sorted(collection) except ValueError: sys.exit('''Sequence must be ascending sorted to apply interpolation search''') SCREAMING_SNAKE_CASE__ = 6_7 SCREAMING_SNAKE_CASE__ = interpolation_search(collection, target) if result is not None: print(f'{target} found at positions: {result}') else: print('''Not found''')
9
1
# this script reports modified .py files under the desired list of top-level sub-dirs passed as a list of arguments, e.g.: # python ./utils/get_modified_files.py utils src tests examples # # it uses git to find the forking point and which files were modified - i.e. files not under git won't be considered # since the output of this script is fed into Makefile commands it doesn't print a newline after the results import re import subprocess import sys SCREAMING_SNAKE_CASE__ = subprocess.check_output('''git merge-base main HEAD'''.split()).decode('''utf-8''') SCREAMING_SNAKE_CASE__ = subprocess.check_output(f'git diff --name-only {fork_point_sha}'.split()).decode('''utf-8''').split() SCREAMING_SNAKE_CASE__ = '''|'''.join(sys.argv[1:]) SCREAMING_SNAKE_CASE__ = re.compile(rf'^({joined_dirs}).*?\.py$') SCREAMING_SNAKE_CASE__ = [x for x in modified_files if regex.match(x)] print(''' '''.join(relevant_modified_files), end='''''')
9
import warnings from ...utils import logging from .image_processing_clip import CLIPImageProcessor SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Dict , *_snake_case : int , **_snake_case : Optional[int] ): """simple docstring""" warnings.warn( 'The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please' ' use CLIPImageProcessor instead.' , _snake_case , ) super().__init__(*_snake_case , **_snake_case )
9
1
import argparse from collections import defaultdict import yaml SCREAMING_SNAKE_CASE__ = '''docs/source/en/_toctree.yml''' def A ( __UpperCamelCase ) -> Optional[Any]: A__ = defaultdict(__UpperCamelCase ) for doc in model_doc: counts[doc["local"]] += 1 A__ = [key for key, value in counts.items() if value > 1] A__ = [] for duplicate_key in duplicates: A__ = list({doc['title'] for doc in model_doc if doc['local'] == duplicate_key} ) if len(__UpperCamelCase ) > 1: raise ValueError( f'''{duplicate_key} is present several times in the documentation table of content at ''' '`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the ' 'others.' ) # Only add this once new_doc.append({'local': duplicate_key, 'title': titles[0]} ) # Add none duplicate-keys new_doc.extend([doc for doc in model_doc if counts[doc['local']] == 1] ) # Sort return sorted(__UpperCamelCase , key=lambda __UpperCamelCase : s["title"].lower() ) def A ( __UpperCamelCase=False ) -> str: with open(__UpperCamelCase , encoding='utf-8' ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]['sections'] # Then to the model doc A__ = 0 while api_doc[model_idx]["title"] != "Models": model_idx += 1 A__ = api_doc[model_idx]['sections'] A__ = [(idx, section) for idx, section in enumerate(__UpperCamelCase ) if 'sections' in section] A__ = False for idx, modality_doc in modalities_docs: A__ = modality_doc['sections'] A__ = clean_model_doc_toc(__UpperCamelCase ) if old_modality_doc != new_modality_doc: A__ = True if overwrite: A__ = new_modality_doc if diff: if overwrite: A__ = model_doc A__ = api_doc with open(__UpperCamelCase , 'w' , encoding='utf-8' ) as f: f.write(yaml.dump(__UpperCamelCase , allow_unicode=__UpperCamelCase ) ) else: raise ValueError( 'The model doc part of the table of content is not properly sorted, run `make style` to fix this.' ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') SCREAMING_SNAKE_CASE__ = parser.parse_args() check_model_doc(args.fix_and_overwrite)
9
import numpy as np import skfuzzy as fuzz if __name__ == "__main__": # Create universe of discourse in Python using linspace () SCREAMING_SNAKE_CASE__ = np.linspace(start=0, stop=7_5, num=7_5, endpoint=True, retstep=False) # Create two fuzzy sets by defining any membership function # (trapmf(), gbellmf(), gaussmf(), etc). SCREAMING_SNAKE_CASE__ = [0, 2_5, 5_0] SCREAMING_SNAKE_CASE__ = [2_5, 5_0, 7_5] SCREAMING_SNAKE_CASE__ = fuzz.membership.trimf(X, abca) SCREAMING_SNAKE_CASE__ = fuzz.membership.trimf(X, abca) # Compute the different operations using inbuilt functions. SCREAMING_SNAKE_CASE__ = np.ones(7_5) SCREAMING_SNAKE_CASE__ = np.zeros((7_5,)) # 1. Union = max(µA(x), µB(x)) SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_or(X, young, X, middle_aged)[1] # 2. Intersection = min(µA(x), µB(x)) SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_and(X, young, X, middle_aged)[1] # 3. Complement (A) = (1- min(µA(x)) SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_not(young) # 4. Difference (A/B) = min(µA(x),(1- µB(x))) SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_and(X, young, X, fuzz.fuzzy_not(middle_aged)[1])[1] # 5. Algebraic Sum = [µA(x) + µB(x) – (µA(x) * µB(x))] SCREAMING_SNAKE_CASE__ = young + middle_aged - (young * middle_aged) # 6. Algebraic Product = (µA(x) * µB(x)) SCREAMING_SNAKE_CASE__ = young * middle_aged # 7. Bounded Sum = min[1,(µA(x), µB(x))] SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_and(X, one, X, young + middle_aged)[1] # 8. Bounded difference = min[0,(µA(x), µB(x))] SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_or(X, zero, X, young - middle_aged)[1] # max-min composition # max-product composition # Plot each set A, set B and each operation result using plot() and subplot(). from matplotlib import pyplot as plt plt.figure() plt.subplot(4, 3, 1) plt.plot(X, young) plt.title('''Young''') plt.grid(True) plt.subplot(4, 3, 2) plt.plot(X, middle_aged) plt.title('''Middle aged''') plt.grid(True) plt.subplot(4, 3, 3) plt.plot(X, union) plt.title('''union''') plt.grid(True) plt.subplot(4, 3, 4) plt.plot(X, intersection) plt.title('''intersection''') plt.grid(True) plt.subplot(4, 3, 5) plt.plot(X, complement_a) plt.title('''complement_a''') plt.grid(True) plt.subplot(4, 3, 6) plt.plot(X, difference) plt.title('''difference a/b''') plt.grid(True) plt.subplot(4, 3, 7) plt.plot(X, alg_sum) plt.title('''alg_sum''') plt.grid(True) plt.subplot(4, 3, 8) plt.plot(X, alg_product) plt.title('''alg_product''') plt.grid(True) plt.subplot(4, 3, 9) plt.plot(X, bdd_sum) plt.title('''bdd_sum''') plt.grid(True) plt.subplot(4, 3, 1_0) plt.plot(X, bdd_difference) plt.title('''bdd_difference''') plt.grid(True) plt.subplots_adjust(hspace=0.5) plt.show()
9
1
# limitations under the License. # NOTE: This file is deprecated and will be removed in a future version. # It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works from .pipelines import DiffusionPipeline, ImagePipelineOutput # noqa: F401 from .utils import deprecate deprecate( '''pipelines_utils''', '''0.22.0''', '''Importing `DiffusionPipeline` or `ImagePipelineOutput` from diffusers.pipeline_utils is deprecated. Please import from diffusers.pipelines.pipeline_utils instead.''', standard_warn=False, stacklevel=3, )
9
import unittest from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __lowerCAmelCase : """simple docstring""" @staticmethod def _a ( *_snake_case : int , **_snake_case : List[str] ): """simple docstring""" pass @is_pipeline_test @require_vision @require_torch class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" A__ : List[str] = MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING def _a ( self : Any , _snake_case : Union[str, Any] , _snake_case : Tuple , _snake_case : Optional[Any] ): """simple docstring""" A__ = pipeline( 'zero-shot-object-detection' , model='hf-internal-testing/tiny-random-owlvit-object-detection' ) A__ = [ { 'image': './tests/fixtures/tests_samples/COCO/000000039769.png', 'candidate_labels': ['cat', 'remote', 'couch'], } ] return object_detector, examples def _a ( self : int , _snake_case : int , _snake_case : List[str] ): """simple docstring""" A__ = object_detector(examples[0] , threshold=0.0 ) A__ = len(_snake_case ) self.assertGreater(_snake_case , 0 ) self.assertEqual( _snake_case , [ { 'score': ANY(_snake_case ), 'label': ANY(_snake_case ), 'box': {'xmin': ANY(_snake_case ), 'ymin': ANY(_snake_case ), 'xmax': ANY(_snake_case ), 'ymax': ANY(_snake_case )}, } for i in range(_snake_case ) ] , ) @require_tf @unittest.skip('Zero Shot Object Detection not implemented in TF' ) def _a ( self : List[str] ): """simple docstring""" pass @require_torch def _a ( self : Optional[int] ): """simple docstring""" A__ = pipeline( 'zero-shot-object-detection' , model='hf-internal-testing/tiny-random-owlvit-object-detection' ) A__ = object_detector( './tests/fixtures/tests_samples/COCO/000000039769.png' , candidate_labels=['cat', 'remote', 'couch'] , threshold=0.64 , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}}, {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}}, {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}}, ] , ) A__ = object_detector( [ { 'image': './tests/fixtures/tests_samples/COCO/000000039769.png', 'candidate_labels': ['cat', 'remote', 'couch'], } ] , threshold=0.64 , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ [ {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}}, {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}}, {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}}, ] ] , ) @require_torch @slow def _a ( self : int ): """simple docstring""" A__ = pipeline('zero-shot-object-detection' ) A__ = object_detector( 'http://images.cocodataset.org/val2017/000000039769.jpg' , candidate_labels=['cat', 'remote', 'couch'] , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}}, {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}}, {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}}, ] , ) A__ = object_detector( [ { 'image': 'http://images.cocodataset.org/val2017/000000039769.jpg', 'candidate_labels': ['cat', 'remote', 'couch'], }, { 'image': 'http://images.cocodataset.org/val2017/000000039769.jpg', 'candidate_labels': ['cat', 'remote', 'couch'], }, ] , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}}, {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}}, {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}}, ], [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}}, {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}}, {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}}, ], ] , ) @require_tf @unittest.skip('Zero Shot Object Detection not implemented in TF' ) def _a ( self : int ): """simple docstring""" pass @require_torch @slow def _a ( self : str ): """simple docstring""" A__ = 0.2 A__ = pipeline('zero-shot-object-detection' ) A__ = object_detector( 'http://images.cocodataset.org/val2017/000000039769.jpg' , candidate_labels=['cat', 'remote', 'couch'] , threshold=_snake_case , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}}, ] , ) @require_torch @slow def _a ( self : Any ): """simple docstring""" A__ = 2 A__ = pipeline('zero-shot-object-detection' ) A__ = object_detector( 'http://images.cocodataset.org/val2017/000000039769.jpg' , candidate_labels=['cat', 'remote', 'couch'] , top_k=_snake_case , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, ] , )
9
1
def A ( __UpperCamelCase , __UpperCamelCase ) -> str: if not isinstance(__UpperCamelCase , __UpperCamelCase ): raise ValueError('iterations must be defined as integers' ) if not isinstance(__UpperCamelCase , __UpperCamelCase ) or not number >= 1: raise ValueError( 'starting number must be\n and integer and be more than 0' ) if not iterations >= 1: raise ValueError('Iterations must be done more than 0 times to play FizzBuzz' ) A__ = '' while number <= iterations: if number % 3 == 0: out += "Fizz" if number % 5 == 0: out += "Buzz" if 0 not in (number % 3, number % 5): out += str(__UpperCamelCase ) # print(out) number += 1 out += " " return out if __name__ == "__main__": import doctest doctest.testmod()
9
import dataclasses import json import sys import types from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, ArgumentTypeError from copy import copy from enum import Enum from inspect import isclass from pathlib import Path from typing import Any, Callable, Dict, Iterable, List, Literal, NewType, Optional, Tuple, Union, get_type_hints import yaml SCREAMING_SNAKE_CASE__ = NewType('''DataClass''', Any) SCREAMING_SNAKE_CASE__ = NewType('''DataClassType''', Any) def A ( __UpperCamelCase ) -> List[Any]: if isinstance(__UpperCamelCase , __UpperCamelCase ): return v if v.lower() in ("yes", "true", "t", "y", "1"): return True elif v.lower() in ("no", "false", "f", "n", "0"): return False else: raise ArgumentTypeError( f'''Truthy value expected: got {v} but expected one of yes/no, true/false, t/f, y/n, 1/0 (case insensitive).''' ) def A ( __UpperCamelCase ) -> Callable[[str], Any]: A__ = {str(__UpperCamelCase ): choice for choice in choices} return lambda __UpperCamelCase : str_to_choice.get(__UpperCamelCase , __UpperCamelCase ) def A ( *, __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = dataclasses.MISSING , __UpperCamelCase = dataclasses.MISSING , __UpperCamelCase = None , **__UpperCamelCase , ) -> dataclasses.Field: if metadata is None: # Important, don't use as default param in function signature because dict is mutable and shared across function calls A__ = {} if aliases is not None: A__ = aliases if help is not None: A__ = help return dataclasses.field(metadata=__UpperCamelCase , default=__UpperCamelCase , default_factory=__UpperCamelCase , **__UpperCamelCase ) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Iterable[DataClassType] def __init__( self : Optional[int] , _snake_case : Union[DataClassType, Iterable[DataClassType]] , **_snake_case : Tuple ): """simple docstring""" if "formatter_class" not in kwargs: A__ = ArgumentDefaultsHelpFormatter super().__init__(**_snake_case ) if dataclasses.is_dataclass(_snake_case ): A__ = [dataclass_types] A__ = list(_snake_case ) for dtype in self.dataclass_types: self._add_dataclass_arguments(_snake_case ) @staticmethod def _a ( _snake_case : ArgumentParser , _snake_case : dataclasses.Field ): """simple docstring""" A__ = F'''--{field.name}''' A__ = field.metadata.copy() # field.metadata is not used at all by Data Classes, # it is provided as a third-party extension mechanism. if isinstance(field.type , _snake_case ): raise RuntimeError( 'Unresolved type detected, which should have been done with the help of ' '`typing.get_type_hints` method by default' ) A__ = kwargs.pop('aliases' , [] ) if isinstance(_snake_case , _snake_case ): A__ = [aliases] A__ = getattr(field.type , '__origin__' , field.type ) if origin_type is Union or (hasattr(_snake_case , 'UnionType' ) and isinstance(_snake_case , types.UnionType )): if str not in field.type.__args__ and ( len(field.type.__args__ ) != 2 or type(_snake_case ) not in field.type.__args__ ): raise ValueError( 'Only `Union[X, NoneType]` (i.e., `Optional[X]`) is allowed for `Union` because' ' the argument parser only supports one type per argument.' F''' Problem encountered in field \'{field.name}\'.''' ) if type(_snake_case ) not in field.type.__args__: # filter `str` in Union A__ = field.type.__args__[0] if field.type.__args__[1] == str else field.type.__args__[1] A__ = getattr(field.type , '__origin__' , field.type ) elif bool not in field.type.__args__: # filter `NoneType` in Union (except for `Union[bool, NoneType]`) A__ = ( field.type.__args__[0] if isinstance(_snake_case , field.type.__args__[1] ) else field.type.__args__[1] ) A__ = getattr(field.type , '__origin__' , field.type ) # A variable to store kwargs for a boolean field, if needed # so that we can init a `no_*` complement argument (see below) A__ = {} if origin_type is Literal or (isinstance(field.type , _snake_case ) and issubclass(field.type , _snake_case )): if origin_type is Literal: A__ = field.type.__args__ else: A__ = [x.value for x in field.type] A__ = make_choice_type_function(kwargs['choices'] ) if field.default is not dataclasses.MISSING: A__ = field.default else: A__ = True elif field.type is bool or field.type == Optional[bool]: # Copy the currect kwargs to use to instantiate a `no_*` complement argument below. # We do not initialize it here because the `no_*` alternative must be instantiated after the real argument A__ = copy(_snake_case ) # Hack because type=bool in argparse does not behave as we want. A__ = string_to_bool if field.type is bool or (field.default is not None and field.default is not dataclasses.MISSING): # Default value is False if we have no default when of type bool. A__ = False if field.default is dataclasses.MISSING else field.default # This is the value that will get picked if we don't include --field_name in any way A__ = default # This tells argparse we accept 0 or 1 value after --field_name A__ = '?' # This is the value that will get picked if we do --field_name (without value) A__ = True elif isclass(_snake_case ) and issubclass(_snake_case , _snake_case ): A__ = field.type.__args__[0] A__ = '+' if field.default_factory is not dataclasses.MISSING: A__ = field.default_factory() elif field.default is dataclasses.MISSING: A__ = True else: A__ = field.type if field.default is not dataclasses.MISSING: A__ = field.default elif field.default_factory is not dataclasses.MISSING: A__ = field.default_factory() else: A__ = True parser.add_argument(_snake_case , *_snake_case , **_snake_case ) # Add a complement `no_*` argument for a boolean field AFTER the initial field has already been added. # Order is important for arguments with the same destination! # We use a copy of earlier kwargs because the original kwargs have changed a lot before reaching down # here and we do not need those changes/additional keys. if field.default is True and (field.type is bool or field.type == Optional[bool]): A__ = False parser.add_argument(F'''--no_{field.name}''' , action='store_false' , dest=field.name , **_snake_case ) def _a ( self : Any , _snake_case : DataClassType ): """simple docstring""" if hasattr(_snake_case , '_argument_group_name' ): A__ = self.add_argument_group(dtype._argument_group_name ) else: A__ = self try: A__ = get_type_hints(_snake_case ) except NameError: raise RuntimeError( F'''Type resolution failed for {dtype}. Try declaring the class in global scope or ''' 'removing line of `from __future__ import annotations` which opts in Postponed ' 'Evaluation of Annotations (PEP 563)' ) except TypeError as ex: # Remove this block when we drop Python 3.9 support if sys.version_info[:2] < (3, 10) and "unsupported operand type(s) for |" in str(_snake_case ): A__ = '.'.join(map(_snake_case , sys.version_info[:3] ) ) raise RuntimeError( F'''Type resolution failed for {dtype} on Python {python_version}. Try removing ''' 'line of `from __future__ import annotations` which opts in union types as ' '`X | Y` (PEP 604) via Postponed Evaluation of Annotations (PEP 563). To ' 'support Python versions that lower than 3.10, you need to use ' '`typing.Union[X, Y]` instead of `X | Y` and `typing.Optional[X]` instead of ' '`X | None`.' ) from ex raise for field in dataclasses.fields(_snake_case ): if not field.init: continue A__ = type_hints[field.name] self._parse_dataclass_field(_snake_case , _snake_case ) def _a ( self : Optional[int] , _snake_case : Optional[Any]=None , _snake_case : Any=False , _snake_case : int=True , _snake_case : List[Any]=None , _snake_case : int=None , ): """simple docstring""" if args_file_flag or args_filename or (look_for_args_file and len(sys.argv )): A__ = [] if args_filename: args_files.append(Path(_snake_case ) ) elif look_for_args_file and len(sys.argv ): args_files.append(Path(sys.argv[0] ).with_suffix('.args' ) ) # args files specified via command line flag should overwrite default args files so we add them last if args_file_flag: # Create special parser just to extract the args_file_flag values A__ = ArgumentParser() args_file_parser.add_argument(_snake_case , type=_snake_case , action='append' ) # Use only remaining args for further parsing (remove the args_file_flag) A__ , A__ = args_file_parser.parse_known_args(args=_snake_case ) A__ = vars(_snake_case ).get(args_file_flag.lstrip('-' ) , _snake_case ) if cmd_args_file_paths: args_files.extend([Path(_snake_case ) for p in cmd_args_file_paths] ) A__ = [] for args_file in args_files: if args_file.exists(): file_args += args_file.read_text().split() # in case of duplicate arguments the last one has precedence # args specified via the command line should overwrite args from files, so we add them last A__ = file_args + args if args is not None else file_args + sys.argv[1:] A__ , A__ = self.parse_known_args(args=_snake_case ) A__ = [] for dtype in self.dataclass_types: A__ = {f.name for f in dataclasses.fields(_snake_case ) if f.init} A__ = {k: v for k, v in vars(_snake_case ).items() if k in keys} for k in keys: delattr(_snake_case , _snake_case ) A__ = dtype(**_snake_case ) outputs.append(_snake_case ) if len(namespace.__dict__ ) > 0: # additional namespace. outputs.append(_snake_case ) if return_remaining_strings: return (*outputs, remaining_args) else: if remaining_args: raise ValueError(F'''Some specified arguments are not used by the HfArgumentParser: {remaining_args}''' ) return (*outputs,) def _a ( self : Dict , _snake_case : Dict[str, Any] , _snake_case : bool = False ): """simple docstring""" A__ = set(args.keys() ) A__ = [] for dtype in self.dataclass_types: A__ = {f.name for f in dataclasses.fields(_snake_case ) if f.init} A__ = {k: v for k, v in args.items() if k in keys} unused_keys.difference_update(inputs.keys() ) A__ = dtype(**_snake_case ) outputs.append(_snake_case ) if not allow_extra_keys and unused_keys: raise ValueError(F'''Some keys are not used by the HfArgumentParser: {sorted(_snake_case )}''' ) return tuple(_snake_case ) def _a ( self : Dict , _snake_case : str , _snake_case : bool = False ): """simple docstring""" with open(Path(_snake_case ) , encoding='utf-8' ) as open_json_file: A__ = json.loads(open_json_file.read() ) A__ = self.parse_dict(_snake_case , allow_extra_keys=_snake_case ) return tuple(_snake_case ) def _a ( self : Tuple , _snake_case : str , _snake_case : bool = False ): """simple docstring""" A__ = self.parse_dict(yaml.safe_load(Path(_snake_case ).read_text() ) , allow_extra_keys=_snake_case ) return tuple(_snake_case )
9
1
import numpy # List of input, output pairs SCREAMING_SNAKE_CASE__ = ( ((5, 2, 3), 1_5), ((6, 5, 9), 2_5), ((1_1, 1_2, 1_3), 4_1), ((1, 1, 1), 8), ((1_1, 1_2, 1_3), 4_1), ) SCREAMING_SNAKE_CASE__ = (((5_1_5, 2_2, 1_3), 5_5_5), ((6_1, 3_5, 4_9), 1_5_0)) SCREAMING_SNAKE_CASE__ = [2, 4, 1, 5] SCREAMING_SNAKE_CASE__ = len(train_data) SCREAMING_SNAKE_CASE__ = 0.009 def A ( __UpperCamelCase , __UpperCamelCase="train" ) -> str: return calculate_hypothesis_value(__UpperCamelCase , __UpperCamelCase ) - output( __UpperCamelCase , __UpperCamelCase ) def A ( __UpperCamelCase ) -> int: A__ = 0 for i in range(len(__UpperCamelCase ) - 1 ): hyp_val += data_input_tuple[i] * parameter_vector[i + 1] hyp_val += parameter_vector[0] return hyp_val def A ( __UpperCamelCase , __UpperCamelCase ) -> Any: if data_set == "train": return train_data[example_no][1] elif data_set == "test": return test_data[example_no][1] return None def A ( __UpperCamelCase , __UpperCamelCase ) -> int: 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 A ( __UpperCamelCase , __UpperCamelCase=m ) -> Optional[int]: A__ = 0 for i in range(__UpperCamelCase ): if index == -1: summation_value += _error(__UpperCamelCase ) else: summation_value += _error(__UpperCamelCase ) * train_data[i][0][index] return summation_value def A ( __UpperCamelCase ) -> Dict: A__ = summation_of_cost_derivative(__UpperCamelCase , __UpperCamelCase ) / m return cost_derivative_value def A ( ) -> Tuple: global parameter_vector # Tune these values to set a tolerance value for predicted output A__ = 0.00_0002 A__ = 0 A__ = 0 while True: j += 1 A__ = [0, 0, 0, 0] for i in range(0 , len(__UpperCamelCase ) ): A__ = get_cost_derivative(i - 1 ) A__ = ( parameter_vector[i] - LEARNING_RATE * cost_derivative ) if numpy.allclose( __UpperCamelCase , __UpperCamelCase , atol=__UpperCamelCase , rtol=__UpperCamelCase , ): break A__ = temp_parameter_vector print(('Number of iterations:', j) ) def A ( ) -> List[str]: for i in range(len(__UpperCamelCase ) ): print(('Actual output value:', output(__UpperCamelCase , 'test' )) ) print(('Hypothesis output:', calculate_hypothesis_value(__UpperCamelCase , 'test' )) ) if __name__ == "__main__": run_gradient_descent() print('''\nTesting gradient descent for a linear hypothesis function.\n''') test_gradient_descent()
9
import argparse import collections import json from pathlib import Path import requests import torch import yaml from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( MobileViTImageProcessor, MobileViTVaConfig, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation, ) from transformers.utils import logging logging.set_verbosity_info() SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) def A ( __UpperCamelCase ) -> List[Any]: print('Loading config file...' ) def flatten_yaml_as_dict(__UpperCamelCase , __UpperCamelCase="" , __UpperCamelCase="." ): A__ = [] for k, v in d.items(): A__ = parent_key + sep + k if parent_key else k if isinstance(__UpperCamelCase , collections.abc.MutableMapping ): items.extend(flatten_yaml_as_dict(__UpperCamelCase , __UpperCamelCase , sep=__UpperCamelCase ).items() ) else: items.append((new_key, v) ) return dict(__UpperCamelCase ) A__ = argparse.Namespace() with open(__UpperCamelCase , 'r' ) as yaml_file: try: A__ = yaml.load(__UpperCamelCase , Loader=yaml.FullLoader ) A__ = flatten_yaml_as_dict(__UpperCamelCase ) for k, v in flat_cfg.items(): setattr(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) except yaml.YAMLError as exc: logger.error('Error while loading config file: {}. Error message: {}'.format(__UpperCamelCase , str(__UpperCamelCase ) ) ) return config def A ( __UpperCamelCase , __UpperCamelCase ) -> Optional[Any]: A__ = MobileViTVaConfig() A__ = False # dataset if task_name.startswith('imagenet1k_' ): A__ = 1_000 if int(task_name.strip().split('_' )[-1] ) == 384: A__ = 384 else: A__ = 256 A__ = 'imagenet-1k-id2label.json' elif task_name.startswith('imagenet21k_to_1k_' ): A__ = 21_000 if int(task_name.strip().split('_' )[-1] ) == 384: A__ = 384 else: A__ = 256 A__ = 'imagenet-22k-id2label.json' elif task_name.startswith('ade20k_' ): A__ = 151 A__ = 512 A__ = 'ade20k-id2label.json' A__ = True elif task_name.startswith('voc_' ): A__ = 21 A__ = 512 A__ = 'pascal-voc-id2label.json' A__ = True # orig_config A__ = load_orig_config_file(__UpperCamelCase ) assert getattr(__UpperCamelCase , 'model.classification.name' , -1 ) == "mobilevit_v2", "Invalid model" A__ = getattr(__UpperCamelCase , 'model.classification.mitv2.width_multiplier' , 1.0 ) assert ( getattr(__UpperCamelCase , 'model.classification.mitv2.attn_norm_layer' , -1 ) == "layer_norm_2d" ), "Norm layers other than layer_norm_2d is not supported" A__ = getattr(__UpperCamelCase , 'model.classification.activation.name' , 'swish' ) # config.image_size == getattr(orig_config, 'sampler.bs.crop_size_width', 256) if is_segmentation_model: A__ = getattr(__UpperCamelCase , 'model.segmentation.output_stride' , 16 ) if "_deeplabv3" in task_name: A__ = getattr(__UpperCamelCase , 'model.segmentation.deeplabv3.aspp_rates' , [12, 24, 36] ) A__ = getattr(__UpperCamelCase , 'model.segmentation.deeplabv3.aspp_out_channels' , 512 ) A__ = getattr(__UpperCamelCase , 'model.segmentation.deeplabv3.aspp_dropout' , 0.1 ) # id2label A__ = 'huggingface/label-files' A__ = json.load(open(hf_hub_download(__UpperCamelCase , __UpperCamelCase , repo_type='dataset' ) , 'r' ) ) A__ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} A__ = idalabel A__ = {v: k for k, v in idalabel.items()} return config def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[str]: A__ = dct.pop(__UpperCamelCase ) A__ = val def A ( __UpperCamelCase , __UpperCamelCase=False ) -> Dict: if base_model: A__ = '' else: A__ = 'mobilevitv2.' A__ = [] for k in state_dict.keys(): if k[:8] == "encoder.": A__ = k[8:] else: A__ = k if ".block." in k: A__ = k_new.replace('.block.' , '.' ) if ".conv." in k: A__ = k_new.replace('.conv.' , '.convolution.' ) if ".norm." in k: A__ = k_new.replace('.norm.' , '.normalization.' ) if "conv_1." in k: A__ = k_new.replace('conv_1.' , f'''{model_prefix}conv_stem.''' ) for i in [1, 2]: if f'''layer_{i}.''' in k: A__ = k_new.replace(f'''layer_{i}.''' , f'''{model_prefix}encoder.layer.{i-1}.layer.''' ) if ".exp_1x1." in k: A__ = k_new.replace('.exp_1x1.' , '.expand_1x1.' ) if ".red_1x1." in k: A__ = k_new.replace('.red_1x1.' , '.reduce_1x1.' ) for i in [3, 4, 5]: if f'''layer_{i}.0.''' in k: A__ = k_new.replace(f'''layer_{i}.0.''' , f'''{model_prefix}encoder.layer.{i-1}.downsampling_layer.''' ) if f'''layer_{i}.1.local_rep.0.''' in k: A__ = k_new.replace(f'''layer_{i}.1.local_rep.0.''' , f'''{model_prefix}encoder.layer.{i-1}.conv_kxk.''' ) if f'''layer_{i}.1.local_rep.1.''' in k: A__ = k_new.replace(f'''layer_{i}.1.local_rep.1.''' , f'''{model_prefix}encoder.layer.{i-1}.conv_1x1.''' ) for i in [3, 4, 5]: if i == 3: A__ = [0, 1] elif i == 4: A__ = [0, 1, 2, 3] elif i == 5: A__ = [0, 1, 2] for j in j_in: if f'''layer_{i}.1.global_rep.{j}.''' in k: A__ = k_new.replace( f'''layer_{i}.1.global_rep.{j}.''' , f'''{model_prefix}encoder.layer.{i-1}.transformer.layer.{j}.''' ) if f'''layer_{i}.1.global_rep.{j+1}.''' in k: A__ = k_new.replace( f'''layer_{i}.1.global_rep.{j+1}.''' , f'''{model_prefix}encoder.layer.{i-1}.layernorm.''' ) if f'''layer_{i}.1.conv_proj.''' in k: A__ = k_new.replace(f'''layer_{i}.1.conv_proj.''' , f'''{model_prefix}encoder.layer.{i-1}.conv_projection.''' ) if "pre_norm_attn.0." in k: A__ = k_new.replace('pre_norm_attn.0.' , 'layernorm_before.' ) if "pre_norm_attn.1." in k: A__ = k_new.replace('pre_norm_attn.1.' , 'attention.' ) if "pre_norm_ffn.0." in k: A__ = k_new.replace('pre_norm_ffn.0.' , 'layernorm_after.' ) if "pre_norm_ffn.1." in k: A__ = k_new.replace('pre_norm_ffn.1.' , 'ffn.conv1.' ) if "pre_norm_ffn.3." in k: A__ = k_new.replace('pre_norm_ffn.3.' , 'ffn.conv2.' ) if "classifier.1." in k: A__ = k_new.replace('classifier.1.' , 'classifier.' ) if "seg_head." in k: A__ = k_new.replace('seg_head.' , 'segmentation_head.' ) if ".aspp_layer." in k: A__ = k_new.replace('.aspp_layer.' , '.' ) if ".aspp_pool." in k: A__ = k_new.replace('.aspp_pool.' , '.' ) rename_keys.append((k, k_new) ) return rename_keys def A ( __UpperCamelCase ) -> Tuple: A__ = [] for k in state_dict.keys(): if k.startswith('seg_head.aux_head.' ): keys_to_ignore.append(__UpperCamelCase ) for k in keys_to_ignore: state_dict.pop(__UpperCamelCase , __UpperCamelCase ) def A ( ) -> str: A__ = 'http://images.cocodataset.org/val2017/000000039769.jpg' # url = "https://cdn.britannica.com/86/141086-050-9D7C75EE/Gulfstream-G450-business-jet-passengers.jpg" A__ = Image.open(requests.get(__UpperCamelCase , stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Optional[Any]: A__ = get_mobilevitva_config(__UpperCamelCase , __UpperCamelCase ) # load original state_dict A__ = torch.load(__UpperCamelCase , map_location='cpu' ) # load huggingface model if task_name.startswith('ade20k_' ) or task_name.startswith('voc_' ): A__ = MobileViTVaForSemanticSegmentation(__UpperCamelCase ).eval() A__ = False else: A__ = MobileViTVaForImageClassification(__UpperCamelCase ).eval() A__ = False # remove and rename some keys of load the original model A__ = checkpoint remove_unused_keys(__UpperCamelCase ) A__ = create_rename_keys(__UpperCamelCase , base_model=__UpperCamelCase ) for rename_key_src, rename_key_dest in rename_keys: rename_key(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) # load modified state_dict model.load_state_dict(__UpperCamelCase ) # Check outputs on an image, prepared by MobileViTImageProcessor A__ = MobileViTImageProcessor(crop_size=config.image_size , size=config.image_size + 32 ) A__ = image_processor(images=prepare_img() , return_tensors='pt' ) A__ = model(**__UpperCamelCase ) # verify classification model if task_name.startswith('imagenet' ): A__ = outputs.logits A__ = logits.argmax(-1 ).item() print('Predicted class:' , model.config.idalabel[predicted_class_idx] ) if task_name.startswith('imagenet1k_256' ) and config.width_multiplier == 1.0: # expected_logits for base variant A__ = torch.tensor([-1.6336E00, -7.3204E-02, -5.1883E-01] ) assert torch.allclose(logits[0, :3] , __UpperCamelCase , atol=1E-4 ) Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) print(f'''Saving model {task_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(__UpperCamelCase ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(__UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--task''', default='''imagenet1k_256''', type=str, help=( '''Name of the task for which the MobileViTV2 model you\'d like to convert is trained on . ''' ''' Classification (ImageNet-1k) - MobileViTV2 (256x256) : imagenet1k_256 - MobileViTV2 (Trained on 256x256 and Finetuned on 384x384) : imagenet1k_384 - MobileViTV2 (Trained on ImageNet-21k and Finetuned on ImageNet-1k 256x256) : imagenet21k_to_1k_256 - MobileViTV2 (Trained on ImageNet-21k, Finetuned on ImageNet-1k 256x256, and Finetuned on ImageNet-1k 384x384) : imagenet21k_to_1k_384 Segmentation - ADE20K Dataset : ade20k_deeplabv3 - Pascal VOC 2012 Dataset: voc_deeplabv3 ''' ), choices=[ '''imagenet1k_256''', '''imagenet1k_384''', '''imagenet21k_to_1k_256''', '''imagenet21k_to_1k_384''', '''ade20k_deeplabv3''', '''voc_deeplabv3''', ], ) parser.add_argument( '''--orig_checkpoint_path''', required=True, type=str, help='''Path to the original state dict (.pt file).''' ) parser.add_argument('''--orig_config_path''', required=True, type=str, help='''Path to the original config file.''') parser.add_argument( '''--pytorch_dump_folder_path''', required=True, type=str, help='''Path to the output PyTorch model directory.''' ) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_mobilevitva_checkpoint( args.task, args.orig_checkpoint_path, args.orig_config_path, args.pytorch_dump_folder_path )
9
1
# Copyright 2022 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os import subprocess from packaging.version import Version, parse from accelerate.commands.config.config_args import default_config_file, load_config_from_file SCREAMING_SNAKE_CASE__ = '''Run commands across TPU VMs for initial setup before running `accelerate launch`.''' def A ( __UpperCamelCase=None ) -> Union[str, Any]: if subparsers is not None: A__ = subparsers.add_parser('tpu-config' , description=_description ) else: A__ = argparse.ArgumentParser('Accelerate tpu-config command' , description=_description ) # Core arguments A__ = parser.add_argument_group( 'Config Arguments' , 'Arguments that can be configured through `accelerate config`.' ) config_args.add_argument( '--config_file' , type=__UpperCamelCase , default=__UpperCamelCase , help='Path to the config file to use for accelerate.' , ) config_args.add_argument( '--tpu_name' , default=__UpperCamelCase , help='The name of the TPU to use. If not specified, will use the TPU specified in the config file.' , ) config_args.add_argument( '--tpu_zone' , default=__UpperCamelCase , help='The zone of the TPU to use. If not specified, will use the zone specified in the config file.' , ) A__ = parser.add_argument_group('TPU Arguments' , 'Arguments for options ran inside the TPU.' ) pod_args.add_argument( '--use_alpha' , action='store_true' , help='Whether to use `gcloud alpha` when running the TPU training script instead of `gcloud`.' , ) pod_args.add_argument( '--command_file' , default=__UpperCamelCase , help='The path to the file containing the commands to run on the pod on startup.' , ) pod_args.add_argument( '--command' , action='append' , nargs='+' , help='A command to run on the pod. Can be passed multiple times.' , ) pod_args.add_argument( '--install_accelerate' , action='store_true' , help='Whether to install accelerate on the pod. Defaults to False.' , ) pod_args.add_argument( '--accelerate_version' , default='latest' , help='The version of accelerate to install on the pod. If not specified, will use the latest pypi version. Specify \'dev\' to install from GitHub.' , ) pod_args.add_argument( '--debug' , action='store_true' , help='If set, will print the command that would be run instead of running it.' ) if subparsers is not None: parser.set_defaults(func=__UpperCamelCase ) return parser def A ( __UpperCamelCase ) -> Optional[Any]: A__ = None # Get the default from the config file if it exists. if args.config_file is not None or os.path.isfile(__UpperCamelCase ): A__ = load_config_from_file(args.config_file ) if not args.command_file and defaults.command_file is not None and not args.command: A__ = defaults.command_file if not args.command and defaults.commands is not None: A__ = defaults.commands if not args.tpu_name: A__ = defaults.tpu_name if not args.tpu_zone: A__ = defaults.tpu_zone if args.accelerate_version == "dev": A__ = 'git+https://github.com/huggingface/accelerate.git' elif args.accelerate_version == "latest": A__ = 'accelerate -U' elif isinstance(parse(args.accelerate_version ) , __UpperCamelCase ): A__ = f'''accelerate=={args.accelerate_version}''' if not args.command_file and not args.command: raise ValueError('You must specify either a command file or a command to run on the pod.' ) if args.command_file: with open(args.command_file , 'r' ) as f: A__ = [f.read().splitlines()] # To turn list of lists into list of strings if isinstance(args.command[0] , __UpperCamelCase ): A__ = [line for cmd in args.command for line in cmd] # Default to the shared folder and install accelerate A__ = ['cd /usr/share'] if args.install_accelerate: new_cmd += [f'''pip install {args.accelerate_version}'''] new_cmd += args.command A__ = '; '.join(__UpperCamelCase ) # Then send it to gcloud # Eventually try to use google-api-core to do this instead of subprocess A__ = ['gcloud'] if args.use_alpha: cmd += ["alpha"] cmd += [ "compute", "tpus", "tpu-vm", "ssh", args.tpu_name, "--zone", args.tpu_zone, "--command", args.command, "--worker", "all", ] if args.debug: print(f'''Running {" ".join(__UpperCamelCase )}''' ) return subprocess.run(__UpperCamelCase ) print('Successfully setup pod.' ) def A ( ) -> Optional[Any]: A__ = tpu_command_parser() A__ = parser.parse_args() tpu_command_launcher(__UpperCamelCase )
9
import argparse from collections import defaultdict import yaml SCREAMING_SNAKE_CASE__ = '''docs/source/en/_toctree.yml''' def A ( __UpperCamelCase ) -> Optional[Any]: A__ = defaultdict(__UpperCamelCase ) for doc in model_doc: counts[doc["local"]] += 1 A__ = [key for key, value in counts.items() if value > 1] A__ = [] for duplicate_key in duplicates: A__ = list({doc['title'] for doc in model_doc if doc['local'] == duplicate_key} ) if len(__UpperCamelCase ) > 1: raise ValueError( f'''{duplicate_key} is present several times in the documentation table of content at ''' '`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the ' 'others.' ) # Only add this once new_doc.append({'local': duplicate_key, 'title': titles[0]} ) # Add none duplicate-keys new_doc.extend([doc for doc in model_doc if counts[doc['local']] == 1] ) # Sort return sorted(__UpperCamelCase , key=lambda __UpperCamelCase : s["title"].lower() ) def A ( __UpperCamelCase=False ) -> str: with open(__UpperCamelCase , encoding='utf-8' ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]['sections'] # Then to the model doc A__ = 0 while api_doc[model_idx]["title"] != "Models": model_idx += 1 A__ = api_doc[model_idx]['sections'] A__ = [(idx, section) for idx, section in enumerate(__UpperCamelCase ) if 'sections' in section] A__ = False for idx, modality_doc in modalities_docs: A__ = modality_doc['sections'] A__ = clean_model_doc_toc(__UpperCamelCase ) if old_modality_doc != new_modality_doc: A__ = True if overwrite: A__ = new_modality_doc if diff: if overwrite: A__ = model_doc A__ = api_doc with open(__UpperCamelCase , 'w' , encoding='utf-8' ) as f: f.write(yaml.dump(__UpperCamelCase , allow_unicode=__UpperCamelCase ) ) else: raise ValueError( 'The model doc part of the table of content is not properly sorted, run `make style` to fix this.' ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') SCREAMING_SNAKE_CASE__ = parser.parse_args() check_model_doc(args.fix_and_overwrite)
9
1
from math import factorial SCREAMING_SNAKE_CASE__ = {str(digit): factorial(digit) for digit in range(1_0)} def A ( __UpperCamelCase ) -> int: if not isinstance(__UpperCamelCase , __UpperCamelCase ): raise TypeError('Parameter number must be int' ) if number < 0: raise ValueError('Parameter number must be greater than or equal to 0' ) # Converts number in string to iterate on its digits and adds its factorial. return sum(DIGIT_FACTORIAL[digit] for digit in str(__UpperCamelCase ) ) def A ( __UpperCamelCase = 60 , __UpperCamelCase = 1_000_000 ) -> int: if not isinstance(__UpperCamelCase , __UpperCamelCase ) or not isinstance(__UpperCamelCase , __UpperCamelCase ): raise TypeError('Parameters chain_length and number_limit must be int' ) if chain_length <= 0 or number_limit <= 0: raise ValueError( 'Parameters chain_length and number_limit must be greater than 0' ) # the counter for the chains with the exact desired length A__ = 0 # the cached sizes of the previous chains A__ = {} for start_chain_element in range(1 , __UpperCamelCase ): # The temporary set will contain the elements of the chain A__ = set() A__ = 0 # Stop computing the chain when you find a cached size, a repeating item or the # length is greater then the desired one. A__ = start_chain_element while ( chain_element not in chain_sets_lengths and chain_element not in chain_set and chain_set_length <= chain_length ): chain_set.add(__UpperCamelCase ) chain_set_length += 1 A__ = digit_factorial_sum(__UpperCamelCase ) if chain_element in chain_sets_lengths: chain_set_length += chain_sets_lengths[chain_element] A__ = chain_set_length # If chain contains the exact amount of elements increase the counter if chain_set_length == chain_length: chains_counter += 1 return chains_counter if __name__ == "__main__": import doctest doctest.testmod() print(f'{solution()}')
9
import inspect import unittest from transformers import SegformerConfig, is_torch_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_MAPPING, SegformerForImageClassification, SegformerForSemanticSegmentation, SegformerModel, ) from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import SegformerImageProcessor class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def _a ( self : List[str] ): """simple docstring""" A__ = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(_snake_case , 'hidden_sizes' ) ) self.parent.assertTrue(hasattr(_snake_case , 'num_attention_heads' ) ) self.parent.assertTrue(hasattr(_snake_case , 'num_encoder_blocks' ) ) class __lowerCAmelCase : """simple docstring""" def __init__( self : Any , _snake_case : str , _snake_case : Union[str, Any]=13 , _snake_case : Any=64 , _snake_case : Optional[Any]=3 , _snake_case : Dict=4 , _snake_case : Tuple=[2, 2, 2, 2] , _snake_case : str=[8, 4, 2, 1] , _snake_case : Union[str, Any]=[16, 32, 64, 1_28] , _snake_case : int=[1, 4, 8, 16] , _snake_case : List[str]=[1, 2, 4, 8] , _snake_case : int=True , _snake_case : int=True , _snake_case : Union[str, Any]="gelu" , _snake_case : Optional[int]=0.1 , _snake_case : Tuple=0.1 , _snake_case : Dict=0.02 , _snake_case : Tuple=3 , _snake_case : int=None , ): """simple docstring""" A__ = parent A__ = batch_size A__ = image_size A__ = num_channels A__ = num_encoder_blocks A__ = sr_ratios A__ = depths A__ = hidden_sizes A__ = downsampling_rates A__ = num_attention_heads A__ = is_training A__ = use_labels A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = initializer_range A__ = num_labels A__ = scope def _a ( self : int ): """simple docstring""" A__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) A__ = self.get_config() return config, pixel_values, labels def _a ( self : int ): """simple docstring""" return SegformerConfig( image_size=self.image_size , num_channels=self.num_channels , num_encoder_blocks=self.num_encoder_blocks , depths=self.depths , hidden_sizes=self.hidden_sizes , num_attention_heads=self.num_attention_heads , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , initializer_range=self.initializer_range , ) def _a ( self : int , _snake_case : Optional[Any] , _snake_case : int , _snake_case : Any ): """simple docstring""" A__ = SegformerModel(config=_snake_case ) model.to(_snake_case ) model.eval() A__ = model(_snake_case ) A__ = A__ = self.image_size // (self.downsampling_rates[-1] * 2) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width) ) def _a ( self : Union[str, Any] , _snake_case : Union[str, Any] , _snake_case : Tuple , _snake_case : Dict ): """simple docstring""" A__ = self.num_labels A__ = SegformerForSemanticSegmentation(_snake_case ) model.to(_snake_case ) model.eval() A__ = model(_snake_case ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4) ) A__ = model(_snake_case , labels=_snake_case ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4) ) self.parent.assertGreater(result.loss , 0.0 ) def _a ( self : List[str] , _snake_case : Optional[Any] , _snake_case : Union[str, Any] , _snake_case : List[str] ): """simple docstring""" A__ = 1 A__ = SegformerForSemanticSegmentation(config=_snake_case ) model.to(_snake_case ) model.eval() A__ = torch.randint(0 , 1 , (self.batch_size, self.image_size, self.image_size) ).to(_snake_case ) A__ = model(_snake_case , labels=_snake_case ) self.parent.assertGreater(result.loss , 0.0 ) def _a ( self : List[Any] ): """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ = config_and_inputs A__ = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class __lowerCAmelCase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Optional[int] = ( ( SegformerModel, SegformerForSemanticSegmentation, SegformerForImageClassification, ) if is_torch_available() else () ) A__ : Union[str, Any] = ( { "feature-extraction": SegformerModel, "image-classification": SegformerForImageClassification, "image-segmentation": SegformerForSemanticSegmentation, } if is_torch_available() else {} ) A__ : Optional[Any] = True A__ : str = False A__ : Tuple = False A__ : Dict = False def _a ( self : Union[str, Any] ): """simple docstring""" A__ = SegformerModelTester(self ) A__ = SegformerConfigTester(self , config_class=_snake_case ) def _a ( self : Optional[int] ): """simple docstring""" self.config_tester.run_common_tests() def _a ( self : Optional[Any] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def _a ( self : List[Any] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_binary_image_segmentation(*_snake_case ) def _a ( self : Tuple ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_segmentation(*_snake_case ) @unittest.skip('SegFormer does not use inputs_embeds' ) def _a ( self : List[Any] ): """simple docstring""" pass @unittest.skip('SegFormer does not have get_input_embeddings method and get_output_embeddings methods' ) def _a ( self : Dict ): """simple docstring""" pass def _a ( self : Dict ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(_snake_case ) A__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ = [*signature.parameters.keys()] A__ = ['pixel_values'] self.assertListEqual(arg_names[:1] , _snake_case ) def _a ( self : Dict ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = True for model_class in self.all_model_classes: A__ = True A__ = False A__ = True A__ = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) ) A__ = outputs.attentions A__ = sum(self.model_tester.depths ) self.assertEqual(len(_snake_case ) , _snake_case ) # check that output_attentions also work using config del inputs_dict["output_attentions"] A__ = True A__ = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) ) A__ = outputs.attentions self.assertEqual(len(_snake_case ) , _snake_case ) # verify the first attentions (first block, first layer) A__ = (self.model_tester.image_size // 4) ** 2 A__ = (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2 self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len] , ) # verify the last attentions (last block, last layer) A__ = (self.model_tester.image_size // 32) ** 2 A__ = (self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2 self.assertListEqual( list(attentions[-1].shape[-3:] ) , [self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len] , ) A__ = len(_snake_case ) # Check attention is always last and order is fine A__ = True A__ = True A__ = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) ) self.assertEqual(out_len + 1 , len(_snake_case ) ) A__ = outputs.attentions self.assertEqual(len(_snake_case ) , _snake_case ) # verify the first attentions (first block, first layer) A__ = (self.model_tester.image_size // 4) ** 2 A__ = (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2 self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len] , ) def _a ( self : Union[str, Any] ): """simple docstring""" def check_hidden_states_output(_snake_case : Dict , _snake_case : int , _snake_case : List[Any] ): A__ = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) ) A__ = outputs.hidden_states A__ = self.model_tester.num_encoder_blocks self.assertEqual(len(_snake_case ) , _snake_case ) # verify the first hidden states (first block) self.assertListEqual( list(hidden_states[0].shape[-3:] ) , [ self.model_tester.hidden_sizes[0], self.model_tester.image_size // 4, self.model_tester.image_size // 4, ] , ) A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = True check_hidden_states_output(_snake_case , _snake_case , _snake_case ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A__ = True check_hidden_states_output(_snake_case , _snake_case , _snake_case ) def _a ( self : Tuple ): """simple docstring""" if not self.model_tester.is_training: return A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = True for model_class in self.all_model_classes: if model_class in get_values(_snake_case ): continue A__ = model_class(_snake_case ) model.to(_snake_case ) model.train() A__ = self._prepare_for_class(_snake_case , _snake_case , return_labels=_snake_case ) A__ = model(**_snake_case ).loss loss.backward() @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.' ) def _a ( self : Optional[Any] ): """simple docstring""" pass @slow def _a ( self : Tuple ): """simple docstring""" for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = SegformerModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def A ( ) -> str: A__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @slow def _a ( self : Dict ): """simple docstring""" A__ = SegformerImageProcessor( image_scale=(5_12, 5_12) , keep_ratio=_snake_case , align=_snake_case , do_random_crop=_snake_case ) A__ = SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512' ).to( _snake_case ) A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ) A__ = encoded_inputs.pixel_values.to(_snake_case ) with torch.no_grad(): A__ = model(_snake_case ) A__ = torch.Size((1, model.config.num_labels, 1_28, 1_28) ) self.assertEqual(outputs.logits.shape , _snake_case ) A__ = torch.tensor( [ [[-4.6310, -5.5232, -6.2356], [-5.1921, -6.1444, -6.5996], [-5.4424, -6.2790, -6.7574]], [[-12.1391, -13.3122, -13.9554], [-12.8732, -13.9352, -14.3563], [-12.9438, -13.8226, -14.2513]], [[-12.5134, -13.4686, -14.4915], [-12.8669, -14.4343, -14.7758], [-13.2523, -14.5819, -15.0694]], ] ).to(_snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3] , _snake_case , atol=1E-4 ) ) @slow def _a ( self : Optional[Any] ): """simple docstring""" A__ = SegformerImageProcessor( image_scale=(5_12, 5_12) , keep_ratio=_snake_case , align=_snake_case , do_random_crop=_snake_case ) A__ = SegformerForSemanticSegmentation.from_pretrained( 'nvidia/segformer-b1-finetuned-cityscapes-1024-1024' ).to(_snake_case ) A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ) A__ = encoded_inputs.pixel_values.to(_snake_case ) with torch.no_grad(): A__ = model(_snake_case ) A__ = torch.Size((1, model.config.num_labels, 1_28, 1_28) ) self.assertEqual(outputs.logits.shape , _snake_case ) A__ = torch.tensor( [ [[-13.5748, -13.9111, -12.6500], [-14.3500, -15.3683, -14.2328], [-14.7532, -16.0424, -15.6087]], [[-17.1651, -15.8725, -12.9653], [-17.2580, -17.3718, -14.8223], [-16.6058, -16.8783, -16.7452]], [[-3.6456, -3.0209, -1.4203], [-3.0797, -3.1959, -2.0000], [-1.8757, -1.9217, -1.6997]], ] ).to(_snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3] , _snake_case , atol=1E-1 ) ) @slow def _a ( self : Any ): """simple docstring""" A__ = SegformerImageProcessor( image_scale=(5_12, 5_12) , keep_ratio=_snake_case , align=_snake_case , do_random_crop=_snake_case ) A__ = SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512' ).to( _snake_case ) A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ) A__ = encoded_inputs.pixel_values.to(_snake_case ) with torch.no_grad(): A__ = model(_snake_case ) A__ = outputs.logits.detach().cpu() A__ = image_processor.post_process_semantic_segmentation(outputs=_snake_case , target_sizes=[(5_00, 3_00)] ) A__ = torch.Size((5_00, 3_00) ) self.assertEqual(segmentation[0].shape , _snake_case ) A__ = image_processor.post_process_semantic_segmentation(outputs=_snake_case ) A__ = torch.Size((1_28, 1_28) ) self.assertEqual(segmentation[0].shape , _snake_case )
9
1
import functools import gc import inspect import torch from .imports import is_npu_available, is_xpu_available def A ( *__UpperCamelCase ) -> Dict: if not isinstance(__UpperCamelCase , __UpperCamelCase ): A__ = list(__UpperCamelCase ) for i in range(len(__UpperCamelCase ) ): A__ = None gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() return objects def A ( __UpperCamelCase ) -> bool: A__ = [ 'CUDA out of memory.', # CUDA OOM 'cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.', # CUDNN SNAFU 'DefaultCPUAllocator: can\'t allocate memory', # CPU OOM ] if isinstance(__UpperCamelCase , __UpperCamelCase ) and len(exception.args ) == 1: return any(err in exception.args[0] for err in _statements ) return False def A ( __UpperCamelCase = None , __UpperCamelCase = 128 ) -> List[str]: if function is None: return functools.partial(__UpperCamelCase , starting_batch_size=__UpperCamelCase ) A__ = starting_batch_size def decorator(*__UpperCamelCase , **__UpperCamelCase ): nonlocal batch_size gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() A__ = list(inspect.signature(__UpperCamelCase ).parameters.keys() ) # Guard against user error if len(__UpperCamelCase ) < (len(__UpperCamelCase ) + 1): A__ = ', '.join([f'''{arg}={value}''' for arg, value in zip(params[1:] , args[1:] )] ) raise TypeError( f'''Batch size was passed into `{function.__name__}` as the first argument when called.''' f'''Remove this as the decorator already does so: `{function.__name__}({arg_str})`''' ) while True: if batch_size == 0: raise RuntimeError('No executable batch size found, reached zero.' ) try: return function(__UpperCamelCase , *__UpperCamelCase , **__UpperCamelCase ) except Exception as e: if should_reduce_batch_size(__UpperCamelCase ): gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() batch_size //= 2 else: raise return decorator
9
import logging from pathlib import Path import numpy as np import pytorch_lightning as pl import torch from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint from pytorch_lightning.utilities import rank_zero_only from utils_rag import save_json def A ( __UpperCamelCase ) -> Optional[int]: A__ = filter(lambda __UpperCamelCase : p.requires_grad , model.parameters() ) A__ = sum([np.prod(p.size() ) for p in model_parameters] ) return params SCREAMING_SNAKE_CASE__ = logging.getLogger(__name__) def A ( __UpperCamelCase , __UpperCamelCase ) -> Dict: if metric == "rouge2": A__ = '{val_avg_rouge2:.4f}-{step_count}' elif metric == "bleu": A__ = '{val_avg_bleu:.4f}-{step_count}' elif metric == "em": A__ = '{val_avg_em:.4f}-{step_count}' elif metric == "loss": A__ = '{val_avg_loss:.4f}-{step_count}' else: raise NotImplementedError( f'''seq2seq callbacks only support rouge2 and bleu, got {metric}, You can make your own by adding to this''' ' function.' ) A__ = ModelCheckpoint( dirpath=__UpperCamelCase , filename=__UpperCamelCase , monitor=f'''val_{metric}''' , mode='max' , save_top_k=1 , every_n_epochs=1 , ) return checkpoint_callback def A ( __UpperCamelCase , __UpperCamelCase ) -> Any: return EarlyStopping( monitor=f'''val_{metric}''' , mode='min' if 'loss' in metric else 'max' , patience=__UpperCamelCase , verbose=__UpperCamelCase , ) class __lowerCAmelCase ( pl.Callback ): """simple docstring""" def _a ( self : Dict , _snake_case : Union[str, Any] , _snake_case : str ): """simple docstring""" A__ = {F'''lr_group_{i}''': param['lr'] for i, param in enumerate(pl_module.trainer.optimizers[0].param_groups )} pl_module.logger.log_metrics(_snake_case ) @rank_zero_only def _a ( self : Union[str, Any] , _snake_case : pl.Trainer , _snake_case : pl.LightningModule , _snake_case : str , _snake_case : Optional[Any]=True ): """simple docstring""" logger.info(F'''***** {type_path} results at step {trainer.global_step:05d} *****''' ) A__ = trainer.callback_metrics trainer.logger.log_metrics({k: v for k, v in metrics.items() if k not in ['log', 'progress_bar', 'preds']} ) # Log results A__ = Path(pl_module.hparams.output_dir ) if type_path == "test": A__ = od / 'test_results.txt' A__ = od / 'test_generations.txt' else: # this never gets hit. I prefer not to save intermediate generations, and results are in metrics.json # If people want this it will be easy enough to add back. A__ = od / F'''{type_path}_results/{trainer.global_step:05d}.txt''' A__ = od / F'''{type_path}_generations/{trainer.global_step:05d}.txt''' results_file.parent.mkdir(exist_ok=_snake_case ) generations_file.parent.mkdir(exist_ok=_snake_case ) with open(_snake_case , 'a+' ) as writer: for key in sorted(_snake_case ): if key in ["log", "progress_bar", "preds"]: continue A__ = metrics[key] if isinstance(_snake_case , torch.Tensor ): A__ = val.item() A__ = F'''{key}: {val:.6f}\n''' writer.write(_snake_case ) if not save_generations: return if "preds" in metrics: A__ = '\n'.join(metrics['preds'] ) generations_file.open('w+' ).write(_snake_case ) @rank_zero_only def _a ( self : Dict , _snake_case : List[str] , _snake_case : List[Any] ): """simple docstring""" try: A__ = pl_module.model.model.num_parameters() except AttributeError: A__ = pl_module.model.num_parameters() A__ = count_trainable_parameters(_snake_case ) # mp stands for million parameters trainer.logger.log_metrics({'n_params': npars, 'mp': npars / 1E6, 'grad_mp': n_trainable_pars / 1E6} ) @rank_zero_only def _a ( self : int , _snake_case : pl.Trainer , _snake_case : pl.LightningModule ): """simple docstring""" save_json(pl_module.metrics , pl_module.metrics_save_path ) return self._write_logs(_snake_case , _snake_case , 'test' ) @rank_zero_only def _a ( self : Optional[Any] , _snake_case : pl.Trainer , _snake_case : List[Any] ): """simple docstring""" save_json(pl_module.metrics , pl_module.metrics_save_path ) # Uncommenting this will save val generations # return self._write_logs(trainer, pl_module, "valid")
9
1
def A ( __UpperCamelCase , __UpperCamelCase ) -> int: if len(__UpperCamelCase ) != len(__UpperCamelCase ): raise ValueError('String lengths must match!' ) A__ = 0 for chara, chara in zip(__UpperCamelCase , __UpperCamelCase ): if chara != chara: count += 1 return count if __name__ == "__main__": import doctest doctest.testmod()
9
import warnings from typing import Any, Dict, List, Optional, Union import numpy as np from ...audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Optional[Any] = ["input_values", "attention_mask"] def __init__( self : str , _snake_case : int = 1 , _snake_case : int = 1_60_00 , _snake_case : float = 0.0 , _snake_case : bool = False , _snake_case : int = 80 , _snake_case : int = 16 , _snake_case : int = 64 , _snake_case : str = "hann_window" , _snake_case : float = 1.0 , _snake_case : float = 80 , _snake_case : float = 76_00 , _snake_case : float = 1E-10 , _snake_case : int = 2 , _snake_case : bool = True , **_snake_case : Union[str, Any] , ): """simple docstring""" super().__init__(feature_size=_snake_case , sampling_rate=_snake_case , padding_value=_snake_case , **_snake_case ) A__ = do_normalize A__ = return_attention_mask A__ = num_mel_bins A__ = hop_length A__ = win_length A__ = win_function A__ = frame_signal_scale A__ = fmin A__ = fmax A__ = mel_floor A__ = reduction_factor A__ = win_length * sampling_rate // 10_00 A__ = hop_length * sampling_rate // 10_00 A__ = optimal_fft_length(self.sample_size ) A__ = (self.n_fft // 2) + 1 A__ = window_function(window_length=self.sample_size , name=self.win_function , periodic=_snake_case ) A__ = mel_filter_bank( num_frequency_bins=self.n_freqs , num_mel_filters=self.num_mel_bins , min_frequency=self.fmin , max_frequency=self.fmax , sampling_rate=self.sampling_rate , norm='slaney' , mel_scale='slaney' , ) if frame_signal_scale != 1.0: warnings.warn( 'The argument `frame_signal_scale` is deprecated and will be removed in version 4.30.0 of Transformers' , _snake_case , ) if reduction_factor != 2.0: warnings.warn( 'The argument `reduction_factor` is deprecated and will be removed in version 4.30.0 of Transformers' , _snake_case , ) @staticmethod # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm def _a ( _snake_case : List[np.ndarray] , _snake_case : List[np.ndarray] , _snake_case : float = 0.0 ): """simple docstring""" if attention_mask is not None: A__ = np.array(_snake_case , np.intaa ) A__ = [] for vector, length in zip(_snake_case , attention_mask.sum(-1 ) ): A__ = (vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1E-7 ) if length < normed_slice.shape[0]: A__ = padding_value normed_input_values.append(_snake_case ) else: A__ = [(x - x.mean()) / np.sqrt(x.var() + 1E-7 ) for x in input_values] return normed_input_values def _a ( self : Tuple , _snake_case : np.ndarray , ): """simple docstring""" A__ = spectrogram( _snake_case , window=self.window , frame_length=self.sample_size , hop_length=self.sample_stride , fft_length=self.n_fft , mel_filters=self.mel_filters , mel_floor=self.mel_floor , log_mel='log10' , ) return log_mel_spec.T def __call__( self : List[str] , _snake_case : Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None , _snake_case : Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None , _snake_case : Union[bool, str, PaddingStrategy] = False , _snake_case : Optional[int] = None , _snake_case : bool = False , _snake_case : Optional[int] = None , _snake_case : Optional[bool] = None , _snake_case : Optional[Union[str, TensorType]] = None , _snake_case : Optional[int] = None , **_snake_case : Tuple , ): """simple docstring""" if audio is None and audio_target is None: raise ValueError('You must provide either `audio` or `audio_target` values.' ) if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' F''' {self.sampling_rate}. Please make sure that the provided audio input was sampled with''' F''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( 'It is strongly recommended to pass the ``sampling_rate`` argument to this function. ' 'Failing to do so can result in silent errors that might be hard to debug.' ) if audio is not None: A__ = self._process_audio( _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , **_snake_case , ) else: A__ = None if audio_target is not None: A__ = self._process_audio( _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , **_snake_case , ) if inputs is None: return inputs_target else: A__ = inputs_target['input_values'] A__ = inputs_target.get('attention_mask' ) if decoder_attention_mask is not None: A__ = decoder_attention_mask return inputs def _a ( self : Tuple , _snake_case : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , _snake_case : bool = False , _snake_case : Union[bool, str, PaddingStrategy] = False , _snake_case : Optional[int] = None , _snake_case : bool = False , _snake_case : Optional[int] = None , _snake_case : Optional[bool] = None , _snake_case : Optional[Union[str, TensorType]] = None , **_snake_case : Tuple , ): """simple docstring""" A__ = isinstance(_snake_case , np.ndarray ) and len(speech.shape ) > 1 if is_batched_numpy and len(speech.shape ) > 2: raise ValueError(F'''Only mono-channel audio is supported for input to {self}''' ) A__ = is_batched_numpy or ( isinstance(_snake_case , (list, tuple) ) and (isinstance(speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: A__ = [np.asarray(_snake_case , dtype=np.floataa ) for speech in speech] elif not is_batched and not isinstance(_snake_case , np.ndarray ): A__ = np.asarray(_snake_case , dtype=np.floataa ) elif isinstance(_snake_case , np.ndarray ) and speech.dtype is np.dtype(np.floataa ): A__ = speech.astype(np.floataa ) # always return batch if not is_batched: A__ = [speech] # needed to make pad() work on spectrogram inputs A__ = self.feature_size # convert into correct format for padding if is_target: A__ = [self._extract_mel_features(_snake_case ) for waveform in speech] A__ = BatchFeature({'input_values': features} ) A__ = self.num_mel_bins else: A__ = BatchFeature({'input_values': speech} ) A__ = self.pad( _snake_case , padding=_snake_case , max_length=_snake_case , truncation=_snake_case , pad_to_multiple_of=_snake_case , return_attention_mask=_snake_case , **_snake_case , ) A__ = feature_size_hack # convert input values to correct format A__ = padded_inputs['input_values'] if not isinstance(input_values[0] , np.ndarray ): A__ = [np.asarray(_snake_case , dtype=np.floataa ) for array in input_values] elif ( not isinstance(_snake_case , np.ndarray ) and isinstance(input_values[0] , np.ndarray ) and input_values[0].dtype is np.dtype(np.floataa ) ): A__ = [array.astype(np.floataa ) for array in input_values] elif isinstance(_snake_case , np.ndarray ) and input_values.dtype is np.dtype(np.floataa ): A__ = input_values.astype(np.floataa ) # convert attention_mask to correct format A__ = padded_inputs.get('attention_mask' ) if attention_mask is not None: A__ = [np.asarray(_snake_case , dtype=np.intaa ) for array in attention_mask] # zero-mean and unit-variance normalization if not is_target and self.do_normalize: A__ = ( attention_mask if self._get_padding_strategies(_snake_case , max_length=_snake_case ) is not PaddingStrategy.DO_NOT_PAD else None ) A__ = self.zero_mean_unit_var_norm( padded_inputs['input_values'] , attention_mask=_snake_case , padding_value=self.padding_value ) if return_tensors is not None: A__ = padded_inputs.convert_to_tensors(_snake_case ) return padded_inputs def _a ( self : Optional[Any] ): """simple docstring""" A__ = super().to_dict() # Don't serialize these as they are derived from the other properties. A__ = ['window', 'mel_filters', 'sample_size', 'sample_stride', 'n_fft', 'n_freqs'] for name in names: if name in output: del output[name] return output
9
1
from __future__ import annotations import random import unittest from transformers import TransfoXLConfig, 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 from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST, TFTransfoXLForSequenceClassification, TFTransfoXLLMHeadModel, TFTransfoXLModel, ) class __lowerCAmelCase : """simple docstring""" def __init__( self : List[str] , _snake_case : Any , ): """simple docstring""" A__ = parent A__ = 13 A__ = 7 A__ = 30 A__ = self.seq_length + self.mem_len A__ = 15 A__ = True A__ = True A__ = 99 A__ = [10, 50, 80] A__ = 32 A__ = 32 A__ = 4 A__ = 8 A__ = 1_28 A__ = 2 A__ = 2 A__ = None A__ = 1 A__ = 0 A__ = 3 A__ = self.vocab_size - 1 A__ = 0.01 def _a ( self : int ): """simple docstring""" A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = TransfoXLConfig( vocab_size=self.vocab_size , mem_len=self.mem_len , clamp_len=self.clamp_len , cutoffs=self.cutoffs , d_model=self.hidden_size , d_embed=self.d_embed , n_head=self.num_attention_heads , d_head=self.d_head , d_inner=self.d_inner , div_val=self.div_val , n_layer=self.num_hidden_layers , eos_token_id=self.eos_token_id , pad_token_id=self.vocab_size - 1 , init_range=self.init_range , num_labels=self.num_labels , ) return (config, input_ids_a, input_ids_a, lm_labels) def _a ( self : Tuple ): """simple docstring""" random.seed(self.seed ) tf.random.set_seed(self.seed ) def _a ( self : int , _snake_case : List[str] , _snake_case : Tuple , _snake_case : str , _snake_case : Union[str, Any] ): """simple docstring""" A__ = TFTransfoXLModel(_snake_case ) A__ , A__ = model(_snake_case ).to_tuple() A__ = {'input_ids': input_ids_a, 'mems': mems_a} A__ , A__ = model(_snake_case ).to_tuple() self.parent.assertEqual(hidden_states_a.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(hidden_states_a.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) def _a ( self : int , _snake_case : Dict , _snake_case : Dict , _snake_case : Dict , _snake_case : Tuple ): """simple docstring""" A__ = TFTransfoXLLMHeadModel(_snake_case ) A__ , A__ = model(_snake_case ).to_tuple() A__ = {'input_ids': input_ids_a, 'labels': lm_labels} A__ , A__ = model(_snake_case ).to_tuple() A__ , A__ = model([input_ids_a, mems_a] ).to_tuple() A__ = {'input_ids': input_ids_a, 'mems': mems_a, 'labels': lm_labels} A__ , A__ = model(_snake_case ).to_tuple() self.parent.assertEqual(lm_logits_a.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) self.parent.assertEqual(lm_logits_a.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) def _a ( self : Optional[Any] , _snake_case : Tuple , _snake_case : Dict , _snake_case : int , _snake_case : Tuple ): """simple docstring""" A__ = TFTransfoXLForSequenceClassification(_snake_case ) A__ = model(_snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _a ( self : Optional[Any] ): """simple docstring""" A__ = self.prepare_config_and_inputs() ((A__) , (A__) , (A__) , (A__)) = config_and_inputs A__ = {'input_ids': input_ids_a} return config, inputs_dict @require_tf class __lowerCAmelCase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Optional[Any] = ( (TFTransfoXLModel, TFTransfoXLLMHeadModel, TFTransfoXLForSequenceClassification) if is_tf_available() else () ) A__ : str = () if is_tf_available() else () A__ : Optional[int] = ( { "feature-extraction": TFTransfoXLModel, "text-classification": TFTransfoXLForSequenceClassification, "text-generation": TFTransfoXLLMHeadModel, "zero-shot": TFTransfoXLForSequenceClassification, } if is_tf_available() else {} ) # TODO: add this test when TFTransfoXLLMHead has a linear output layer implemented A__ : List[str] = False A__ : int = False A__ : Tuple = False A__ : Dict = False def _a ( self : Dict , _snake_case : List[Any] , _snake_case : int , _snake_case : Dict , _snake_case : List[str] , _snake_case : str ): """simple docstring""" if pipeline_test_casse_name == "TextGenerationPipelineTests": # Get `ValueError: AttributeError: 'NoneType' object has no attribute 'new_ones'` or `AssertionError`. # `TransfoXLConfig` was never used in pipeline tests: cannot create a simple # tokenizer. return True return False def _a ( self : Union[str, Any] ): """simple docstring""" A__ = TFTransfoXLModelTester(self ) A__ = ConfigTester(self , config_class=_snake_case , d_embed=37 ) def _a ( self : Tuple ): """simple docstring""" self.config_tester.run_common_tests() def _a ( self : Any ): """simple docstring""" self.model_tester.set_seed() A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_model(*_snake_case ) def _a ( self : Dict ): """simple docstring""" self.model_tester.set_seed() A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_lm_head(*_snake_case ) def _a ( self : int ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_for_sequence_classification(*_snake_case ) def _a ( self : Union[str, Any] ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = [TFTransfoXLForSequenceClassification] for model_class in self.all_model_classes: A__ = model_class(_snake_case ) assert isinstance(model.get_input_embeddings() , tf.keras.layers.Layer ) if model_class in list_other_models_with_output_ebd: A__ = model.get_output_embeddings() assert isinstance(_snake_case , tf.keras.layers.Layer ) A__ = model.get_bias() assert name is None else: A__ = model.get_output_embeddings() assert x is None A__ = model.get_bias() assert name is None def _a ( self : Union[str, Any] ): """simple docstring""" pass @slow def _a ( self : str ): """simple docstring""" for model_name in TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = TFTransfoXLModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) @unittest.skip(reason='This model doesn\'t play well with fit() due to not returning a single loss.' ) def _a ( self : Union[str, Any] ): """simple docstring""" pass @require_tf class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @unittest.skip('Skip test until #12651 is resolved.' ) @slow def _a ( self : int ): """simple docstring""" A__ = TFTransfoXLLMHeadModel.from_pretrained('transfo-xl-wt103' ) # fmt: off A__ = tf.convert_to_tensor([[33,12_97,2,1,10_09,4,11_09,1_17_39,47_62,3_58,5,25,2_45,22,17_06,17,2_00_98,5,32_15,21,37,11_10,3,13,10_41,4,24,6_03,4_90,2,7_14_77,2_00_98,10_44_47,2,2_09_61,1,26_04,4,1,3_29,3,62_24,8_31,1_60_02,2,8,6_03,7_89_67,2_95_46,23,8_03,20,25,4_16,5,8,2_32,4,2_77,6,18_55,46_01,3,2_95_46,54,8,36_09,5,5_72_11,49,4,1,2_77,18,8,17_55,1_56_91,3,3_41,25,4_16,6_93,4_25_73,71,17,4_01,94,31,1_79_19,2,2_95_46,78_73,18,1,4_35,23,1_10_11,7_55,5,51_67,3,79_83,98,84,2,2_95_46,32_67,8,36_09,4,1,48_65,10_75,2,60_87,71,6,3_46,8,58_54,3,2_95_46,8_24,14_00,18_68,2,19,1_60,2,3_11,8,54_96,2,2_09_20,17,25,1_50_97,3,24,24,0]] , dtype=tf.intaa ) # noqa: E231 # fmt: on # In 1991 , the remains of Russian Tsar Nicholas II and his family # ( except for Alexei and Maria ) are discovered . # The voice of Nicholas's young son , Tsarevich Alexei Nikolaevich , narrates the # remainder of the story . 1883 Western Siberia , # a young Grigori Rasputin is asked by his father and a group of men to perform magic . # Rasputin has a vision and denounces one of the men as a horse thief . Although his # father initially slaps him for making such an accusation , Rasputin watches as the # man is chased outside and beaten . Twenty years later , Rasputin sees a vision of # the Virgin Mary , prompting him to become a priest . Rasputin quickly becomes famous , # with people , even a bishop , begging for his blessing . <eod> </s> <eos> # fmt: off A__ = [33,12_97,2,1,10_09,4,11_09,1_17_39,47_62,3_58,5,25,2_45,22,17_06,17,2_00_98,5,32_15,21,37,11_10,3,13,10_41,4,24,6_03,4_90,2,7_14_77,2_00_98,10_44_47,2,2_09_61,1,26_04,4,1,3_29,3,62_24,8_31,1_60_02,2,8,6_03,7_89_67,2_95_46,23,8_03,20,25,4_16,5,8,2_32,4,2_77,6,18_55,46_01,3,2_95_46,54,8,36_09,5,5_72_11,49,4,1,2_77,18,8,17_55,1_56_91,3,3_41,25,4_16,6_93,4_25_73,71,17,4_01,94,31,1_79_19,2,2_95_46,78_73,18,1,4_35,23,1_10_11,7_55,5,51_67,3,79_83,98,84,2,2_95_46,32_67,8,36_09,4,1,48_65,10_75,2,60_87,71,6,3_46,8,58_54,3,2_95_46,8_24,14_00,18_68,2,19,1_60,2,3_11,8,54_96,2,2_09_20,17,25,1_50_97,3,24,24,0,33,1,18_57,2,1,10_09,4,11_09,1_17_39,47_62,3_58,5,25,2_45,28,11_10,3,13,10_41,4,24,6_03,4_90,2,7_14_77,2_00_98,10_44_47,2,2_09_61,1,26_04,4,1,3_29,3,0] # noqa: E231 # fmt: on # In 1991, the remains of Russian Tsar Nicholas II and his family ( # except for Alexei and Maria ) are discovered. The voice of young son, # Tsarevich Alexei Nikolaevich, narrates the remainder of the story. # 1883 Western Siberia, a young Grigori Rasputin is asked by his father # and a group of men to perform magic. Rasputin has a vision and # denounces one of the men as a horse thief. Although his father initially # slaps him for making such an accusation, Rasputin watches as the man # is chased outside and beaten. Twenty years later, Rasputin sees a vision # of the Virgin Mary, prompting him to become a priest. # Rasputin quickly becomes famous, with people, even a bishop, begging for # his blessing. <unk> <unk> <eos> In the 1990s, the remains of Russian Tsar # Nicholas II and his family were discovered. The voice of <unk> young son, # Tsarevich Alexei Nikolaevich, narrates the remainder of the story.<eos> A__ = model.generate(_snake_case , max_length=2_00 , do_sample=_snake_case ) self.assertListEqual(output_ids[0].numpy().tolist() , _snake_case )
9
import argparse import OmegaConf import torch from diffusers import DDIMScheduler, LDMPipeline, UNetLDMModel, VQModel def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: A__ = OmegaConf.load(__UpperCamelCase ) A__ = torch.load(__UpperCamelCase , map_location='cpu' )['model'] A__ = list(state_dict.keys() ) # extract state_dict for VQVAE A__ = {} A__ = 'first_stage_model.' for key in keys: if key.startswith(__UpperCamelCase ): A__ = state_dict[key] # extract state_dict for UNetLDM A__ = {} A__ = 'model.diffusion_model.' for key in keys: if key.startswith(__UpperCamelCase ): A__ = state_dict[key] A__ = config.model.params.first_stage_config.params A__ = config.model.params.unet_config.params A__ = VQModel(**__UpperCamelCase ).eval() vqvae.load_state_dict(__UpperCamelCase ) A__ = UNetLDMModel(**__UpperCamelCase ).eval() unet.load_state_dict(__UpperCamelCase ) A__ = DDIMScheduler( timesteps=config.model.params.timesteps , beta_schedule='scaled_linear' , beta_start=config.model.params.linear_start , beta_end=config.model.params.linear_end , clip_sample=__UpperCamelCase , ) A__ = LDMPipeline(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) pipeline.save_pretrained(__UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument('''--checkpoint_path''', type=str, required=True) parser.add_argument('''--config_path''', type=str, required=True) parser.add_argument('''--output_path''', type=str, required=True) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_ldm_original(args.checkpoint_path, args.config_path, args.output_path)
9
1
import inspect import unittest from transformers import ViTHybridConfig from transformers.testing_utils import require_accelerate, require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class __lowerCAmelCase : """simple docstring""" def __init__( self : List[Any] , _snake_case : Dict , _snake_case : Union[str, Any]=13 , _snake_case : Tuple=64 , _snake_case : Any=2 , _snake_case : Optional[Any]=3 , _snake_case : Any=True , _snake_case : Dict=True , _snake_case : Optional[int]=32 , _snake_case : List[str]=5 , _snake_case : Union[str, Any]=4 , _snake_case : str=37 , _snake_case : str="gelu" , _snake_case : Dict=0.1 , _snake_case : Union[str, Any]=0.1 , _snake_case : Union[str, Any]=10 , _snake_case : Optional[int]=0.02 , _snake_case : Optional[Any]=[1, 16, 4, 4] , _snake_case : Optional[Any]=None , ): """simple docstring""" A__ = parent A__ = batch_size A__ = image_size A__ = patch_size A__ = num_channels A__ = is_training A__ = use_labels A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = type_sequence_label_size A__ = initializer_range A__ = scope A__ = backbone_featmap_shape # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) # the number of patches is based on the feature map of the backbone, which by default uses an output stride # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size A__ = (self.image_size // 32) ** 2 A__ = num_patches + 1 def _a ( self : Optional[Any] ): """simple docstring""" A__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A__ = self.get_config() return config, pixel_values, labels def _a ( self : Any ): """simple docstring""" A__ = { 'global_padding': 'same', 'layer_type': 'bottleneck', 'depths': [3, 4, 9], 'out_features': ['stage1', 'stage2', 'stage3'], 'embedding_dynamic_padding': True, 'hidden_sizes': [4, 8, 16, 32], 'num_groups': 2, } return ViTHybridConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_snake_case , initializer_range=self.initializer_range , backbone_featmap_shape=self.backbone_featmap_shape , backbone_config=_snake_case , ) def _a ( self : Union[str, Any] , _snake_case : int , _snake_case : Optional[int] , _snake_case : int ): """simple docstring""" A__ = ViTHybridModel(config=_snake_case ) model.to(_snake_case ) model.eval() A__ = model(_snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _a ( self : int , _snake_case : Dict , _snake_case : Dict , _snake_case : Optional[Any] ): """simple docstring""" A__ = self.type_sequence_label_size A__ = ViTHybridForImageClassification(_snake_case ) model.to(_snake_case ) model.eval() A__ = model(_snake_case , labels=_snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _a ( self : Any ): """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ = config_and_inputs A__ = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class __lowerCAmelCase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Any = (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else () A__ : str = ( {"feature-extraction": ViTHybridModel, "image-classification": ViTHybridForImageClassification} if is_torch_available() else {} ) A__ : int = False A__ : Dict = False A__ : Tuple = False def _a ( self : List[str] ): """simple docstring""" A__ = ViTHybridModelTester(self ) A__ = ConfigTester(self , config_class=_snake_case , has_text_modality=_snake_case , hidden_size=37 ) def _a ( self : int ): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='ViT does not use inputs_embeds' ) def _a ( self : Optional[int] ): """simple docstring""" pass def _a ( self : int ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(_snake_case ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A__ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_snake_case , nn.Linear ) ) def _a ( self : Dict ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(_snake_case ) A__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ = [*signature.parameters.keys()] A__ = ['pixel_values'] self.assertListEqual(arg_names[:1] , _snake_case ) def _a ( self : List[str] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def _a ( self : List[Any] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_snake_case ) def _a ( self : Optional[Any] ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = _config_zero_init(_snake_case ) for model_class in self.all_model_classes: A__ = model_class(config=_snake_case ) # Skip the check for the backbone for name, module in model.named_modules(): if module.__class__.__name__ == "ViTHybridPatchEmbeddings": A__ = [F'''{name}.{key}''' for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() , [0.0, 1.0] , msg=F'''Parameter {name} of model {model_class} seems not properly initialized''' , ) @slow def _a ( self : Any ): """simple docstring""" for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = ViTHybridModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def A ( ) -> Optional[Any]: A__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @cached_property def _a ( self : int ): """simple docstring""" return ( ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def _a ( self : Any ): """simple docstring""" A__ = ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to( _snake_case ) A__ = self.default_image_processor A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ).to(_snake_case ) # forward pass with torch.no_grad(): A__ = model(**_snake_case ) # verify the logits A__ = torch.Size((1, 10_00) ) self.assertEqual(outputs.logits.shape , _snake_case ) A__ = torch.tensor([-1.9090, -0.4993, -0.2389] ).to(_snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _snake_case , atol=1E-4 ) ) @slow @require_accelerate def _a ( self : str ): """simple docstring""" A__ = ViTHybridImageProcessor.from_pretrained('google/vit-hybrid-base-bit-384' ) A__ = ViTHybridForImageClassification.from_pretrained('google/vit-hybrid-base-bit-384' , device_map='auto' ) A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ) A__ = model(**_snake_case ) A__ = outputs.logits # model predicts one of the 1000 ImageNet classes A__ = logits.argmax(-1 ).item() self.assertTrue(model.config.idalabel[predicted_class_idx] , 'tabby, tabby cat' )
9
import json import os import torch from diffusers import UNetaDModel os.makedirs('''hub/hopper-medium-v2/unet/hor32''', exist_ok=True) os.makedirs('''hub/hopper-medium-v2/unet/hor128''', exist_ok=True) os.makedirs('''hub/hopper-medium-v2/value_function''', exist_ok=True) def A ( __UpperCamelCase ) -> Union[str, Any]: if hor == 128: A__ = ('DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D') A__ = (32, 128, 256) A__ = ('UpResnetBlock1D', 'UpResnetBlock1D') elif hor == 32: A__ = ('DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D') A__ = (32, 64, 128, 256) A__ = ('UpResnetBlock1D', 'UpResnetBlock1D', 'UpResnetBlock1D') A__ = torch.load(f'''/Users/bglickenhaus/Documents/diffuser/temporal_unet-hopper-mediumv2-hor{hor}.torch''' ) A__ = model.state_dict() A__ = { 'down_block_types': down_block_types, 'block_out_channels': block_out_channels, 'up_block_types': up_block_types, 'layers_per_block': 1, 'use_timestep_embedding': True, 'out_block_type': 'OutConv1DBlock', 'norm_num_groups': 8, 'downsample_each_block': False, 'in_channels': 14, 'out_channels': 14, 'extra_in_channels': 0, 'time_embedding_type': 'positional', 'flip_sin_to_cos': False, 'freq_shift': 1, 'sample_size': 65_536, 'mid_block_type': 'MidResTemporalBlock1D', 'act_fn': 'mish', } A__ = UNetaDModel(**__UpperCamelCase ) print(f'''length of state dict: {len(state_dict.keys() )}''' ) print(f'''length of value function dict: {len(hf_value_function.state_dict().keys() )}''' ) A__ = dict(zip(model.state_dict().keys() , hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): A__ = state_dict.pop(__UpperCamelCase ) hf_value_function.load_state_dict(__UpperCamelCase ) torch.save(hf_value_function.state_dict() , f'''hub/hopper-medium-v2/unet/hor{hor}/diffusion_pytorch_model.bin''' ) with open(f'''hub/hopper-medium-v2/unet/hor{hor}/config.json''' , 'w' ) as f: json.dump(__UpperCamelCase , __UpperCamelCase ) def A ( ) -> List[str]: A__ = { 'in_channels': 14, 'down_block_types': ('DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D'), 'up_block_types': (), 'out_block_type': 'ValueFunction', 'mid_block_type': 'ValueFunctionMidBlock1D', 'block_out_channels': (32, 64, 128, 256), 'layers_per_block': 1, 'downsample_each_block': True, 'sample_size': 65_536, 'out_channels': 14, 'extra_in_channels': 0, 'time_embedding_type': 'positional', 'use_timestep_embedding': True, 'flip_sin_to_cos': False, 'freq_shift': 1, 'norm_num_groups': 8, 'act_fn': 'mish', } A__ = torch.load('/Users/bglickenhaus/Documents/diffuser/value_function-hopper-mediumv2-hor32.torch' ) A__ = model A__ = UNetaDModel(**__UpperCamelCase ) print(f'''length of state dict: {len(state_dict.keys() )}''' ) print(f'''length of value function dict: {len(hf_value_function.state_dict().keys() )}''' ) A__ = dict(zip(state_dict.keys() , hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): A__ = state_dict.pop(__UpperCamelCase ) hf_value_function.load_state_dict(__UpperCamelCase ) torch.save(hf_value_function.state_dict() , 'hub/hopper-medium-v2/value_function/diffusion_pytorch_model.bin' ) with open('hub/hopper-medium-v2/value_function/config.json' , 'w' ) as f: json.dump(__UpperCamelCase , __UpperCamelCase ) if __name__ == "__main__": unet(3_2) # unet(128) value_function()
9
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 if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ChineseCLIPImageProcessor class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def __init__( self : Tuple , _snake_case : List[str] , _snake_case : Union[str, Any]=7 , _snake_case : Any=3 , _snake_case : List[Any]=18 , _snake_case : Union[str, Any]=30 , _snake_case : Optional[int]=4_00 , _snake_case : Union[str, Any]=True , _snake_case : str=None , _snake_case : List[str]=True , _snake_case : str=None , _snake_case : Union[str, Any]=True , _snake_case : List[str]=[0.4814_5466, 0.457_8275, 0.4082_1073] , _snake_case : List[Any]=[0.2686_2954, 0.2613_0258, 0.2757_7711] , _snake_case : Tuple=True , ): """simple docstring""" A__ = size if size is not None else {'height': 2_24, 'width': 2_24} A__ = crop_size if crop_size is not None else {'height': 18, 'width': 18} A__ = parent A__ = batch_size A__ = num_channels A__ = image_size A__ = min_resolution A__ = max_resolution A__ = do_resize A__ = size A__ = do_center_crop A__ = crop_size A__ = do_normalize A__ = image_mean A__ = image_std A__ = do_convert_rgb def _a ( self : int ): """simple docstring""" return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_convert_rgb": self.do_convert_rgb, } def _a ( self : Any , _snake_case : Optional[int]=False , _snake_case : str=False , _snake_case : Optional[Any]=False ): """simple docstring""" assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time" if equal_resolution: A__ = [] for i in range(self.batch_size ): image_inputs.append( np.random.randint( 2_55 , size=(self.num_channels, self.max_resolution, self.max_resolution) , dtype=np.uinta ) ) else: A__ = [] for i in range(self.batch_size ): A__ , A__ = np.random.choice(np.arange(self.min_resolution , self.max_resolution ) , 2 ) image_inputs.append(np.random.randint(2_55 , size=(self.num_channels, width, height) , dtype=np.uinta ) ) if not numpify and not torchify: # PIL expects the channel dimension as last dimension A__ = [Image.fromarray(np.moveaxis(_snake_case , 0 , -1 ) ) for x in image_inputs] if torchify: A__ = [torch.from_numpy(_snake_case ) for x in image_inputs] return image_inputs @require_torch @require_vision class __lowerCAmelCase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Dict = ChineseCLIPImageProcessor if is_vision_available() else None def _a ( self : Optional[int] ): """simple docstring""" A__ = ChineseCLIPImageProcessingTester(self , do_center_crop=_snake_case ) @property def _a ( self : str ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def _a ( self : Optional[int] ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_snake_case , 'do_resize' ) ) self.assertTrue(hasattr(_snake_case , 'size' ) ) self.assertTrue(hasattr(_snake_case , 'do_center_crop' ) ) self.assertTrue(hasattr(_snake_case , 'center_crop' ) ) self.assertTrue(hasattr(_snake_case , 'do_normalize' ) ) self.assertTrue(hasattr(_snake_case , 'image_mean' ) ) self.assertTrue(hasattr(_snake_case , 'image_std' ) ) self.assertTrue(hasattr(_snake_case , 'do_convert_rgb' ) ) def _a ( self : Any ): """simple docstring""" A__ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'height': 2_24, 'width': 2_24} ) self.assertEqual(image_processor.crop_size , {'height': 18, 'width': 18} ) A__ = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {'shortest_edge': 42} ) self.assertEqual(image_processor.crop_size , {'height': 84, 'width': 84} ) def _a ( self : Tuple ): """simple docstring""" pass def _a ( self : int ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A__ = self.image_processor_tester.prepare_inputs(equal_resolution=_snake_case ) for image in image_inputs: self.assertIsInstance(_snake_case , Image.Image ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) # Test batched A__ = image_processing(_snake_case , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) def _a ( self : Tuple ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors A__ = self.image_processor_tester.prepare_inputs(equal_resolution=_snake_case , numpify=_snake_case ) for image in image_inputs: self.assertIsInstance(_snake_case , np.ndarray ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) # Test batched A__ = image_processing(_snake_case , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) def _a ( self : Optional[int] ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A__ = self.image_processor_tester.prepare_inputs(equal_resolution=_snake_case , torchify=_snake_case ) for image in image_inputs: self.assertIsInstance(_snake_case , torch.Tensor ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) # Test batched A__ = image_processing(_snake_case , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) @require_torch @require_vision class __lowerCAmelCase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Dict = ChineseCLIPImageProcessor if is_vision_available() else None def _a ( self : Optional[int] ): """simple docstring""" A__ = ChineseCLIPImageProcessingTester(self , num_channels=4 , do_center_crop=_snake_case ) A__ = 3 @property def _a ( self : Optional[int] ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def _a ( self : List[str] ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_snake_case , 'do_resize' ) ) self.assertTrue(hasattr(_snake_case , 'size' ) ) self.assertTrue(hasattr(_snake_case , 'do_center_crop' ) ) self.assertTrue(hasattr(_snake_case , 'center_crop' ) ) self.assertTrue(hasattr(_snake_case , 'do_normalize' ) ) self.assertTrue(hasattr(_snake_case , 'image_mean' ) ) self.assertTrue(hasattr(_snake_case , 'image_std' ) ) self.assertTrue(hasattr(_snake_case , 'do_convert_rgb' ) ) def _a ( self : str ): """simple docstring""" pass def _a ( self : str ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A__ = self.image_processor_tester.prepare_inputs(equal_resolution=_snake_case ) for image in image_inputs: self.assertIsInstance(_snake_case , Image.Image ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.expected_encoded_image_num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) # Test batched A__ = image_processing(_snake_case , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.expected_encoded_image_num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , )
9
from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class __lowerCAmelCase : """simple docstring""" def __init__( self : Dict , _snake_case : Union[str, Any] , _snake_case : Optional[Any]=12 , _snake_case : Any=7 , _snake_case : List[str]=True , _snake_case : int=True , _snake_case : int=True , _snake_case : Tuple=99 , _snake_case : List[Any]=32 , _snake_case : Optional[int]=32 , _snake_case : List[str]=2 , _snake_case : List[str]=4 , _snake_case : List[Any]=37 , _snake_case : Union[str, Any]=0.1 , _snake_case : Tuple=0.1 , _snake_case : Dict=5_12 , _snake_case : Union[str, Any]=0.02 , _snake_case : Any=0 , _snake_case : Optional[Any]=None , ): """simple docstring""" A__ = parent A__ = batch_size A__ = seq_length A__ = is_training A__ = use_input_mask A__ = use_labels A__ = vocab_size A__ = hidden_size A__ = projection_dim A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = dropout A__ = attention_dropout A__ = max_position_embeddings A__ = initializer_range A__ = scope A__ = bos_token_id def _a ( self : Optional[Any] ): """simple docstring""" A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = None if self.use_input_mask: A__ = random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: A__ = input_mask.numpy() A__ , A__ = input_mask.shape A__ = np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(_snake_case ): A__ = 1 A__ = 0 A__ = self.get_config() return config, input_ids, tf.convert_to_tensor(_snake_case ) def _a ( self : Tuple ): """simple docstring""" return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def _a ( self : int , _snake_case : Union[str, Any] , _snake_case : Any , _snake_case : List[str] ): """simple docstring""" A__ = TFBlipTextModel(config=_snake_case ) A__ = model(_snake_case , attention_mask=_snake_case , training=_snake_case ) A__ = model(_snake_case , training=_snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def _a ( self : str ): """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ = config_and_inputs A__ = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_tf class __lowerCAmelCase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Tuple = (TFBlipTextModel,) if is_tf_available() else () A__ : Optional[int] = False A__ : Union[str, Any] = False A__ : Union[str, Any] = False def _a ( self : Any ): """simple docstring""" A__ = BlipTextModelTester(self ) A__ = ConfigTester(self , config_class=_snake_case , hidden_size=37 ) def _a ( self : List[str] ): """simple docstring""" self.config_tester.run_common_tests() def _a ( self : Union[str, Any] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def _a ( self : Tuple ): """simple docstring""" pass def _a ( self : int ): """simple docstring""" pass @unittest.skip(reason='Blip does not use inputs_embeds' ) def _a ( self : Any ): """simple docstring""" pass @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING' ) def _a ( self : str ): """simple docstring""" pass @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING' ) def _a ( self : Optional[Any] ): """simple docstring""" pass @slow def _a ( self : Union[str, Any] ): """simple docstring""" for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = TFBlipTextModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def _a ( self : int , _snake_case : int=True ): """simple docstring""" super().test_pt_tf_model_equivalence(allow_missing_keys=_snake_case )
9
1
import cva import numpy as np class __lowerCAmelCase : """simple docstring""" def __init__( self : Union[str, Any] , _snake_case : float , _snake_case : int ): """simple docstring""" if k in (0.04, 0.06): A__ = k A__ = window_size else: raise ValueError('invalid k value' ) def __str__( self : Any ): """simple docstring""" return str(self.k ) def _a ( self : Union[str, Any] , _snake_case : str ): """simple docstring""" A__ = cva.imread(_snake_case , 0 ) A__ , A__ = img.shape A__ = [] A__ = img.copy() A__ = cva.cvtColor(_snake_case , cva.COLOR_GRAY2RGB ) A__ , A__ = np.gradient(_snake_case ) A__ = dx**2 A__ = dy**2 A__ = dx * dy A__ = 0.04 A__ = self.window_size // 2 for y in range(_snake_case , h - offset ): for x in range(_snake_case , w - offset ): A__ = ixx[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() A__ = iyy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() A__ = ixy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() A__ = (wxx * wyy) - (wxy**2) A__ = wxx + wyy A__ = det - k * (trace**2) # Can change the value if r > 0.5: corner_list.append([x, y, r] ) color_img.itemset((y, x, 0) , 0 ) color_img.itemset((y, x, 1) , 0 ) color_img.itemset((y, x, 2) , 2_55 ) return color_img, corner_list if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = HarrisCorner(0.04, 3) SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = edge_detect.detect('''path_to_image''') cva.imwrite('''detect.png''', color_img)
9
from __future__ import annotations from typing import Any def A ( __UpperCamelCase ) -> int: if not postfix_notation: return 0 A__ = {'+', '-', '*', '/'} A__ = [] for token in postfix_notation: if token in operations: A__ , A__ = stack.pop(), stack.pop() if token == "+": stack.append(a + b ) elif token == "-": stack.append(a - b ) elif token == "*": stack.append(a * b ) else: if a * b < 0 and a % b != 0: stack.append(a // b + 1 ) else: stack.append(a // b ) else: stack.append(int(__UpperCamelCase ) ) return stack.pop() if __name__ == "__main__": import doctest doctest.testmod()
9
1
from __future__ import annotations class lowerCamelCase_ : def __init__( self , __lowerCAmelCase = 0 ): """simple docstring""" __magic_name__ :str = key def A ( self , __lowerCAmelCase , __lowerCAmelCase ): """simple docstring""" assert isinstance(__lowerCAmelCase , __lowerCAmelCase ) and isinstance(__lowerCAmelCase , __lowerCAmelCase ) __magic_name__ :Optional[Any] = key or self.__key or 1 # make sure key is an appropriate size key %= 2_5_5 return [chr(ord(__lowerCAmelCase ) ^ key ) for ch in content] def A ( self , __lowerCAmelCase , __lowerCAmelCase ): """simple docstring""" assert isinstance(__lowerCAmelCase , __lowerCAmelCase ) and isinstance(__lowerCAmelCase , __lowerCAmelCase ) __magic_name__ :Any = key or self.__key or 1 # make sure key is an appropriate size key %= 2_5_5 return [chr(ord(__lowerCAmelCase ) ^ key ) for ch in content] def A ( self , __lowerCAmelCase , __lowerCAmelCase = 0 ): """simple docstring""" assert isinstance(__lowerCAmelCase , __lowerCAmelCase ) and isinstance(__lowerCAmelCase , __lowerCAmelCase ) __magic_name__ :int = key or self.__key or 1 # make sure key can be any size while key > 2_5_5: key -= 2_5_5 # This will be returned __magic_name__ :List[Any] = '''''' for ch in content: ans += chr(ord(__lowerCAmelCase ) ^ key ) return ans def A ( self , __lowerCAmelCase , __lowerCAmelCase = 0 ): """simple docstring""" assert isinstance(__lowerCAmelCase , __lowerCAmelCase ) and isinstance(__lowerCAmelCase , __lowerCAmelCase ) __magic_name__ :Tuple = key or self.__key or 1 # make sure key can be any size while key > 2_5_5: key -= 2_5_5 # This will be returned __magic_name__ :List[str] = '''''' for ch in content: ans += chr(ord(__lowerCAmelCase ) ^ key ) return ans def A ( self , __lowerCAmelCase , __lowerCAmelCase = 0 ): """simple docstring""" assert isinstance(__lowerCAmelCase , __lowerCAmelCase ) and isinstance(__lowerCAmelCase , __lowerCAmelCase ) try: with open(__lowerCAmelCase ) as fin, open('''encrypt.out''' , '''w+''' ) as fout: # actual encrypt-process for line in fin: fout.write(self.encrypt_string(__lowerCAmelCase , __lowerCAmelCase ) ) except OSError: return False return True def A ( self , __lowerCAmelCase , __lowerCAmelCase ): """simple docstring""" assert isinstance(__lowerCAmelCase , __lowerCAmelCase ) and isinstance(__lowerCAmelCase , __lowerCAmelCase ) try: with open(__lowerCAmelCase ) as fin, open('''decrypt.out''' , '''w+''' ) as fout: # actual encrypt-process for line in fin: fout.write(self.decrypt_string(__lowerCAmelCase , __lowerCAmelCase ) ) except OSError: return False return True # Tests # crypt = XORCipher() # key = 67 # # test encrypt # print(crypt.encrypt("hallo welt",key)) # # test decrypt # print(crypt.decrypt(crypt.encrypt("hallo welt",key), key)) # # test encrypt_string # print(crypt.encrypt_string("hallo welt",key)) # # test decrypt_string # print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key)) # if (crypt.encrypt_file("test.txt",key)): # print("encrypt successful") # else: # print("encrypt unsuccessful") # if (crypt.decrypt_file("encrypt.out",key)): # print("decrypt successful") # else: # print("decrypt unsuccessful")
0
from __future__ import annotations def A ( __UpperCamelCase = 4 ) -> list[list[int]]: A__ = abs(__UpperCamelCase ) or 4 return [[1 + x + y * row_size for x in range(__UpperCamelCase )] for y in range(__UpperCamelCase )] def A ( __UpperCamelCase ) -> list[list[int]]: return reverse_row(transpose(__UpperCamelCase ) ) # OR.. transpose(reverse_column(matrix)) def A ( __UpperCamelCase ) -> list[list[int]]: return reverse_row(reverse_column(__UpperCamelCase ) ) # OR.. reverse_column(reverse_row(matrix)) def A ( __UpperCamelCase ) -> list[list[int]]: return reverse_column(transpose(__UpperCamelCase ) ) # OR.. transpose(reverse_row(matrix)) def A ( __UpperCamelCase ) -> list[list[int]]: A__ = [list(__UpperCamelCase ) for x in zip(*__UpperCamelCase )] return matrix def A ( __UpperCamelCase ) -> list[list[int]]: A__ = matrix[::-1] return matrix def A ( __UpperCamelCase ) -> list[list[int]]: A__ = [x[::-1] for x in matrix] return matrix def A ( __UpperCamelCase ) -> None: for i in matrix: print(*__UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = make_matrix() print('''\norigin:\n''') print_matrix(matrix) print('''\nrotate 90 counterclockwise:\n''') print_matrix(rotate_aa(matrix)) SCREAMING_SNAKE_CASE__ = make_matrix() print('''\norigin:\n''') print_matrix(matrix) print('''\nrotate 180:\n''') print_matrix(rotate_aaa(matrix)) SCREAMING_SNAKE_CASE__ = make_matrix() print('''\norigin:\n''') print_matrix(matrix) print('''\nrotate 270 counterclockwise:\n''') print_matrix(rotate_aaa(matrix))
9
0
import argparse import torch from transformers import YosoConfig, YosoForMaskedLM def _A ( _lowercase ) -> str: """simple docstring""" if "model" in orig_key: __UpperCamelCase = orig_key.replace('model.' , '' ) if "norm1" in orig_key: __UpperCamelCase = orig_key.replace('norm1' , 'attention.output.LayerNorm' ) if "norm2" in orig_key: __UpperCamelCase = orig_key.replace('norm2' , 'output.LayerNorm' ) if "norm" in orig_key: __UpperCamelCase = orig_key.replace('norm' , 'LayerNorm' ) if "transformer" in orig_key: __UpperCamelCase = orig_key.split('.' )[0].split('_' )[-1] __UpperCamelCase = orig_key.replace(f'''transformer_{layer_num}''' , f'''encoder.layer.{layer_num}''' ) if "mha.attn" in orig_key: __UpperCamelCase = orig_key.replace('mha.attn' , 'attention.self' ) if "mha" in orig_key: __UpperCamelCase = orig_key.replace('mha' , 'attention' ) if "W_q" in orig_key: __UpperCamelCase = orig_key.replace('W_q' , 'self.query' ) if "W_k" in orig_key: __UpperCamelCase = orig_key.replace('W_k' , 'self.key' ) if "W_v" in orig_key: __UpperCamelCase = orig_key.replace('W_v' , 'self.value' ) if "ff1" in orig_key: __UpperCamelCase = orig_key.replace('ff1' , 'intermediate.dense' ) if "ff2" in orig_key: __UpperCamelCase = orig_key.replace('ff2' , 'output.dense' ) if "ff" in orig_key: __UpperCamelCase = orig_key.replace('ff' , 'output.dense' ) if "mlm_class" in orig_key: __UpperCamelCase = orig_key.replace('mlm.mlm_class' , 'cls.predictions.decoder' ) if "mlm" in orig_key: __UpperCamelCase = orig_key.replace('mlm' , 'cls.predictions.transform' ) if "cls" not in orig_key: __UpperCamelCase = 'yoso.' + orig_key return orig_key def _A ( _lowercase , _lowercase ) -> List[Any]: """simple docstring""" for key in orig_state_dict.copy().keys(): __UpperCamelCase = orig_state_dict.pop(_lowercase ) if ("pooler" in key) or ("sen_class" in key): continue else: __UpperCamelCase = val __UpperCamelCase = orig_state_dict['cls.predictions.decoder.bias'] __UpperCamelCase = torch.arange(_lowercase ).expand((1, -1) ) + 2 return orig_state_dict def _A ( _lowercase , _lowercase , _lowercase ) -> Optional[Any]: """simple docstring""" __UpperCamelCase = torch.load(_lowercase , map_location='cpu' )['model_state_dict'] __UpperCamelCase = YosoConfig.from_json_file(_lowercase ) __UpperCamelCase = YosoForMaskedLM(_lowercase ) __UpperCamelCase = convert_checkpoint_helper(config.max_position_embeddings , _lowercase ) print(model.load_state_dict(_lowercase ) ) model.eval() model.save_pretrained(_lowercase ) print(f'''Checkpoint successfuly converted. Model saved at {pytorch_dump_path}''' ) if __name__ == "__main__": __snake_case = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--pytorch_model_path''', default=None, type=str, required=True, help='''Path to YOSO pytorch checkpoint.''' ) parser.add_argument( '''--config_file''', default=None, type=str, required=True, help='''The json file for YOSO model config.''', ) parser.add_argument( '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) __snake_case = parser.parse_args() convert_yoso_checkpoint(args.pytorch_model_path, args.config_file, args.pytorch_dump_path)
1
from __future__ import annotations from fractions import Fraction def A ( __UpperCamelCase , __UpperCamelCase ) -> bool: return ( num != den and num % 10 == den // 10 and (num // 10) / (den % 10) == num / den ) def A ( __UpperCamelCase ) -> list[str]: A__ = [] A__ = 11 A__ = int('1' + '0' * digit_len ) for num in range(__UpperCamelCase , __UpperCamelCase ): while den <= 99: if (num != den) and (num % 10 == den // 10) and (den % 10 != 0): if is_digit_cancelling(__UpperCamelCase , __UpperCamelCase ): solutions.append(f'''{num}/{den}''' ) den += 1 num += 1 A__ = 10 return solutions def A ( __UpperCamelCase = 2 ) -> int: A__ = 1.0 for fraction in fraction_list(__UpperCamelCase ): A__ = Fraction(__UpperCamelCase ) result *= frac.denominator / frac.numerator return int(__UpperCamelCase ) if __name__ == "__main__": print(solution())
9
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCAmelCase_ = {"""configuration_wavlm""": ["""WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """WavLMConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ = [ """WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST""", """WavLMForAudioFrameClassification""", """WavLMForCTC""", """WavLMForSequenceClassification""", """WavLMForXVector""", """WavLMModel""", """WavLMPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_wavlm import WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP, WavLMConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_wavlm import ( WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST, WavLMForAudioFrameClassification, WavLMForCTC, WavLMForSequenceClassification, WavLMForXVector, WavLMModel, WavLMPreTrainedModel, ) else: import sys UpperCAmelCase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
2
# 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_tokenizers_available, is_torch_available SCREAMING_SNAKE_CASE__ = {'''configuration_mra''': ['''MRA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MraConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''MRA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MraForMaskedLM''', '''MraForMultipleChoice''', '''MraForQuestionAnswering''', '''MraForSequenceClassification''', '''MraForTokenClassification''', '''MraLayer''', '''MraModel''', '''MraPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_mra import MRA_PRETRAINED_CONFIG_ARCHIVE_MAP, MraConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mra import ( MRA_PRETRAINED_MODEL_ARCHIVE_LIST, MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, MraLayer, MraModel, MraPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
9
0
'''simple docstring''' import json import logging import os import sys from time import time from unittest.mock import patch from transformers.testing_utils import TestCasePlus, require_torch_tpu logging.basicConfig(level=logging.DEBUG) lowerCAmelCase : Optional[Any] = logging.getLogger() def A_( A : Dict): UpperCamelCase = {} UpperCamelCase = os.path.join(A , 'all_results.json') if os.path.exists(A): with open(A , 'r') as f: UpperCamelCase = json.load(A) else: raise ValueError(f'''can\'t find {path}''') return results lowerCAmelCase : Optional[Any] = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) @require_torch_tpu class SCREAMING_SNAKE_CASE__ ( snake_case_): def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' import xla_spawn UpperCamelCase = self.get_auto_remove_tmp_dir() UpperCamelCase = F''' ./examples/pytorch/text-classification/run_glue.py --num_cores=8 ./examples/pytorch/text-classification/run_glue.py --model_name_or_path distilbert-base-uncased --output_dir {tmp_dir} --overwrite_output_dir --train_file ./tests/fixtures/tests_samples/MRPC/train.csv --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv --do_train --do_eval --debug tpu_metrics_debug --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --learning_rate=1e-4 --max_steps=10 --warmup_steps=2 --seed=42 --max_seq_length=128 '''.split() with patch.object(A_ , 'argv' , A_ ): UpperCamelCase = time() xla_spawn.main() UpperCamelCase = time() UpperCamelCase = get_results(A_ ) self.assertGreaterEqual(result['eval_accuracy'] , 0.75 ) # Assert that the script takes less than 500 seconds to make sure it doesn't hang. self.assertLess(end - start , 500 ) def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' import xla_spawn UpperCamelCase = '\n ./tests/test_trainer_tpu.py\n --num_cores=8\n ./tests/test_trainer_tpu.py\n '.split() with patch.object(A_ , 'argv' , A_ ): xla_spawn.main()
3
SCREAMING_SNAKE_CASE__ = ''' # Transformers installation ! pip install transformers datasets # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git ''' SCREAMING_SNAKE_CASE__ = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] SCREAMING_SNAKE_CASE__ = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
9
0
"""simple docstring""" import unittest from transformers import TrOCRConfig from transformers.testing_utils import is_torch_available, require_torch, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers.models.trocr.modeling_trocr import TrOCRDecoder, TrOCRForCausalLM @require_torch class a : def __init__( self , _snake_case , _snake_case=99 , _snake_case=13 , _snake_case=16 , _snake_case=7 , _snake_case=True , _snake_case=True , _snake_case=True , _snake_case=False , _snake_case=True , _snake_case=2 , _snake_case=32 , _snake_case=4 , _snake_case=4 , _snake_case=30 , _snake_case=0 , _snake_case=1 , _snake_case=2 , _snake_case=None , ): """simple docstring""" lowerCAmelCase = parent lowerCAmelCase = batch_size lowerCAmelCase = decoder_seq_length # For common tests lowerCAmelCase = self.decoder_seq_length lowerCAmelCase = is_training lowerCAmelCase = use_attention_mask lowerCAmelCase = use_labels lowerCAmelCase = vocab_size lowerCAmelCase = d_model lowerCAmelCase = d_model lowerCAmelCase = decoder_layers lowerCAmelCase = decoder_layers lowerCAmelCase = decoder_ffn_dim lowerCAmelCase = decoder_attention_heads lowerCAmelCase = decoder_attention_heads lowerCAmelCase = eos_token_id lowerCAmelCase = bos_token_id lowerCAmelCase = pad_token_id lowerCAmelCase = decoder_start_token_id lowerCAmelCase = use_cache lowerCAmelCase = max_position_embeddings lowerCAmelCase = None lowerCAmelCase = decoder_seq_length lowerCAmelCase = 2 lowerCAmelCase = 1 def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase = ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) lowerCAmelCase = None if self.use_attention_mask: lowerCAmelCase = ids_tensor([self.batch_size, self.decoder_seq_length] , vocab_size=2 ) lowerCAmelCase = None if self.use_labels: lowerCAmelCase = ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) lowerCAmelCase = TrOCRConfig( vocab_size=self.vocab_size , d_model=self.d_model , decoder_layers=self.decoder_layers , decoder_ffn_dim=self.decoder_ffn_dim , decoder_attention_heads=self.decoder_attention_heads , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , use_cache=self.use_cache , pad_token_id=self.pad_token_id , decoder_start_token_id=self.decoder_start_token_id , max_position_embeddings=self.max_position_embeddings , ) return (config, input_ids, attention_mask, lm_labels) def UpperCamelCase__ ( self , _snake_case , _snake_case , _snake_case , _snake_case , ): """simple docstring""" lowerCAmelCase = True lowerCAmelCase = TrOCRDecoder(config=_snake_case ).to(_snake_case ).eval() lowerCAmelCase = input_ids[:2] input_ids[input_ids == 0] += 1 # first forward pass lowerCAmelCase = model(_snake_case , use_cache=_snake_case ) lowerCAmelCase = model(_snake_case ) lowerCAmelCase = model(_snake_case , use_cache=_snake_case ) self.parent.assertTrue(len(_snake_case ) == len(_snake_case ) ) self.parent.assertTrue(len(_snake_case ) == len(_snake_case ) + 1 ) lowerCAmelCase = outputs['past_key_values'] # create hypothetical next token and extent to next_input_ids lowerCAmelCase = ids_tensor((2, 1) , config.vocab_size - 1 ) + 1 # append to next input_ids and lowerCAmelCase = torch.cat([input_ids, next_tokens] , dim=-1 ) lowerCAmelCase = model(_snake_case )['last_hidden_state'] lowerCAmelCase = model(_snake_case , past_key_values=_snake_case )['last_hidden_state'] # select random slice lowerCAmelCase = ids_tensor((1,) , output_from_past.shape[-1] ).item() lowerCAmelCase = output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach() lowerCAmelCase = output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice assert torch.allclose(_snake_case , _snake_case , atol=1E-3 ) def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase = self.prepare_config_and_inputs() lowerCAmelCase ,lowerCAmelCase ,lowerCAmelCase ,lowerCAmelCase = config_and_inputs lowerCAmelCase = {'input_ids': input_ids, 'attention_mask': attention_mask} return config, inputs_dict @require_torch class a ( a__ , a__ , a__ , unittest.TestCase ): snake_case__ = (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else () snake_case__ = (TrOCRForCausalLM,) if is_torch_available() else () snake_case__ = {'''text-generation''': TrOCRForCausalLM} if is_torch_available() else {} snake_case__ = True snake_case__ = False def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase = TrOCRStandaloneDecoderModelTester(self , is_training=_snake_case ) lowerCAmelCase = ConfigTester(self , config_class=_snake_case ) def UpperCamelCase__ ( self ): """simple docstring""" pass def UpperCamelCase__ ( self ): """simple docstring""" pass def UpperCamelCase__ ( self ): """simple docstring""" pass def UpperCamelCase__ ( self ): """simple docstring""" self.config_tester.run_common_tests() def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past(*_snake_case ) def UpperCamelCase__ ( self ): """simple docstring""" return @unittest.skip('The model doesn\'t support left padding' ) # and it's not used enough to be worth fixing :) def UpperCamelCase__ ( self ): """simple docstring""" pass
4
import unittest from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __lowerCAmelCase : """simple docstring""" @staticmethod def _a ( *_snake_case : Any , **_snake_case : Optional[int] ): """simple docstring""" pass @is_pipeline_test @require_torch @require_vision class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" A__ : Union[str, Any] = MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING def _a ( self : List[Any] , _snake_case : Union[str, Any] , _snake_case : Tuple , _snake_case : Union[str, Any] ): """simple docstring""" A__ = pipeline('visual-question-answering' , model='hf-internal-testing/tiny-vilt-random-vqa' ) A__ = [ { 'image': Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ), 'question': 'How many cats are there?', }, { 'image': './tests/fixtures/tests_samples/COCO/000000039769.png', 'question': 'How many cats are there?', }, ] return vqa_pipeline, examples def _a ( self : Optional[Any] , _snake_case : Union[str, Any] , _snake_case : List[str] ): """simple docstring""" A__ = vqa_pipeline(_snake_case , top_k=1 ) self.assertEqual( _snake_case , [ [{'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}], [{'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}], ] , ) @require_torch def _a ( self : Any ): """simple docstring""" A__ = pipeline('visual-question-answering' , model='hf-internal-testing/tiny-vilt-random-vqa' ) A__ = './tests/fixtures/tests_samples/COCO/000000039769.png' A__ = 'How many cats are there?' A__ = vqa_pipeline(image=_snake_case , question='How many cats are there?' , top_k=2 ) self.assertEqual( _snake_case , [{'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}, {'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}] ) A__ = vqa_pipeline({'image': image, 'question': question} , top_k=2 ) self.assertEqual( _snake_case , [{'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}, {'score': ANY(_snake_case ), 'answer': ANY(_snake_case )}] ) @slow @require_torch def _a ( self : Any ): """simple docstring""" A__ = pipeline('visual-question-answering' , model='dandelin/vilt-b32-finetuned-vqa' ) A__ = './tests/fixtures/tests_samples/COCO/000000039769.png' A__ = 'How many cats are there?' A__ = vqa_pipeline(image=_snake_case , question=_snake_case , top_k=2 ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [{'score': 0.8799, 'answer': '2'}, {'score': 0.296, 'answer': '1'}] ) A__ = vqa_pipeline({'image': image, 'question': question} , top_k=2 ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [{'score': 0.8799, 'answer': '2'}, {'score': 0.296, 'answer': '1'}] ) A__ = vqa_pipeline( [{'image': image, 'question': question}, {'image': image, 'question': question}] , top_k=2 ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [[{'score': 0.8799, 'answer': '2'}, {'score': 0.296, 'answer': '1'}]] * 2 , ) @require_tf @unittest.skip('Visual question answering not implemented in TF' ) def _a ( self : Dict ): """simple docstring""" pass
9
0
'''simple docstring''' from __future__ import annotations def A (__lowerCamelCase :list[int | float] , __lowerCamelCase :int , __lowerCamelCase :int ): if len(__lowerCamelCase ) == 0: raise ValueError("""find_max() arg is an empty sequence""" ) if ( left >= len(__lowerCamelCase ) or left < -len(__lowerCamelCase ) or right >= len(__lowerCamelCase ) or right < -len(__lowerCamelCase ) ): raise IndexError("""list index out of range""" ) if left == right: return nums[left] _lowerCAmelCase = (left + right) >> 1 # the middle _lowerCAmelCase = find_max(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) # find max in range[left, mid] _lowerCAmelCase = find_max(__lowerCamelCase , mid + 1 , __lowerCamelCase ) # 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)
5
def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: if exponent == 1: return base if exponent % 2 == 0: A__ = _modexpt(__UpperCamelCase , exponent // 2 , __UpperCamelCase ) % modulo_value return (x * x) % modulo_value else: return (base * _modexpt(__UpperCamelCase , exponent - 1 , __UpperCamelCase )) % modulo_value def A ( __UpperCamelCase = 1_777 , __UpperCamelCase = 1_855 , __UpperCamelCase = 8 ) -> int: A__ = base for _ in range(1 , __UpperCamelCase ): A__ = _modexpt(__UpperCamelCase , __UpperCamelCase , 10**digits ) return result if __name__ == "__main__": print(f'{solution() = }')
9
0
import os from argparse import ArgumentParser from typing import List import torch.utils.data from datasets import Dataset, IterableDataset from datasets.distributed import split_dataset_by_node _lowerCamelCase = 4 _lowerCamelCase = 3 class UpperCamelCase_ ( UpperCamelCase__ ): pass def SCREAMING_SNAKE_CASE__ ( UpperCamelCase__: List[str] ): for shard in shards: for i in range(UpperCamelCase__ ): yield {"i": i, "shard": shard} def SCREAMING_SNAKE_CASE__ ( ): SCREAMING_SNAKE_CASE__ = int(os.environ["""RANK"""] ) SCREAMING_SNAKE_CASE__ = int(os.environ["""WORLD_SIZE"""] ) SCREAMING_SNAKE_CASE__ = ArgumentParser() parser.add_argument("""--streaming""" , type=UpperCamelCase__ ) parser.add_argument("""--local_rank""" , type=UpperCamelCase__ ) parser.add_argument("""--num_workers""" , type=UpperCamelCase__ , default=0 ) SCREAMING_SNAKE_CASE__ = parser.parse_args() SCREAMING_SNAKE_CASE__ = args.streaming SCREAMING_SNAKE_CASE__ = args.num_workers SCREAMING_SNAKE_CASE__ = {"""shards""": [f'''shard_{shard_idx}''' for shard_idx in range(UpperCamelCase__ )]} SCREAMING_SNAKE_CASE__ = IterableDataset.from_generator(UpperCamelCase__ , gen_kwargs=UpperCamelCase__ ) if not streaming: SCREAMING_SNAKE_CASE__ = Dataset.from_list(list(UpperCamelCase__ ) ) SCREAMING_SNAKE_CASE__ = split_dataset_by_node(UpperCamelCase__ , rank=UpperCamelCase__ , world_size=UpperCamelCase__ ) SCREAMING_SNAKE_CASE__ = torch.utils.data.DataLoader(UpperCamelCase__ , num_workers=UpperCamelCase__ ) SCREAMING_SNAKE_CASE__ = NUM_SHARDS * NUM_ITEMS_PER_SHARD SCREAMING_SNAKE_CASE__ = full_size // world_size expected_local_size += int(rank < (full_size % world_size) ) SCREAMING_SNAKE_CASE__ = sum(1 for _ in dataloader ) if local_size != expected_local_size: raise FailedTestError(f'''local_size {local_size} != expected_local_size {expected_local_size}''' ) if __name__ == "__main__": main()
6
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() SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) def A ( __UpperCamelCase , __UpperCamelCase=False , __UpperCamelCase=False ) -> Dict: A__ = 'backbone.' if is_semantic else '' A__ = [] 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 A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=False , __UpperCamelCase=False ) -> Optional[Any]: for i in range(config.num_hidden_layers ): A__ = 'backbone.' if is_semantic else '' # queries, keys and values A__ = state_dict.pop(f'''{prefix}blocks.{i}.attn.qkv.weight''' ) A__ = state_dict.pop(f'''{prefix}blocks.{i}.attn.q_bias''' ) A__ = state_dict.pop(f'''{prefix}blocks.{i}.attn.v_bias''' ) A__ = in_proj_weight[ : config.hidden_size, : ] A__ = q_bias A__ = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] A__ = in_proj_weight[ -config.hidden_size :, : ] A__ = v_bias # gamma_1 and gamma_2 # we call them lambda because otherwise they are renamed when using .from_pretrained A__ = state_dict.pop(f'''{prefix}blocks.{i}.gamma_1''' ) A__ = state_dict.pop(f'''{prefix}blocks.{i}.gamma_2''' ) A__ = gamma_a A__ = gamma_a def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Union[str, Any]: A__ = dct.pop(__UpperCamelCase ) A__ = val def A ( ) -> Dict: A__ = 'http://images.cocodataset.org/val2017/000000039769.jpg' A__ = Image.open(requests.get(__UpperCamelCase , stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=False ) -> str: A__ = False if 'rvlcdip' in checkpoint_url else True A__ = BeitConfig(use_absolute_position_embeddings=__UpperCamelCase , use_mask_token=__UpperCamelCase ) # size of the architecture if "large" in checkpoint_url or "dit-l" in checkpoint_url: A__ = 1_024 A__ = 4_096 A__ = 24 A__ = 16 # labels if "rvlcdip" in checkpoint_url: A__ = 16 A__ = 'huggingface/label-files' A__ = 'rvlcdip-id2label.json' A__ = json.load(open(hf_hub_download(__UpperCamelCase , __UpperCamelCase , repo_type='dataset' ) , 'r' ) ) A__ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} A__ = idalabel A__ = {v: k for k, v in idalabel.items()} # load state_dict of original model, remove and rename some keys A__ = torch.hub.load_state_dict_from_url(__UpperCamelCase , map_location='cpu' )['model'] A__ = create_rename_keys(__UpperCamelCase , has_lm_head=__UpperCamelCase ) for src, dest in rename_keys: rename_key(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) read_in_q_k_v(__UpperCamelCase , __UpperCamelCase , has_lm_head=__UpperCamelCase ) # load HuggingFace model A__ = BeitForMaskedImageModeling(__UpperCamelCase ) if has_lm_head else BeitForImageClassification(__UpperCamelCase ) model.eval() model.load_state_dict(__UpperCamelCase ) # Check outputs on an image A__ = BeitImageProcessor( size=config.image_size , resample=PILImageResampling.BILINEAR , do_center_crop=__UpperCamelCase ) A__ = prepare_img() A__ = image_processor(images=__UpperCamelCase , return_tensors='pt' ) A__ = encoding['pixel_values'] A__ = model(__UpperCamelCase ) A__ = outputs.logits # verify logits A__ = [1, 16] if 'rvlcdip' in checkpoint_url else [1, 196, 8_192] assert logits.shape == torch.Size(__UpperCamelCase ), "Shape of logits not as expected" Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) print(f'''Saving model to {pytorch_dump_folder_path}''' ) model.save_pretrained(__UpperCamelCase ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(__UpperCamelCase ) if push_to_hub: if has_lm_head: A__ = 'dit-base' if 'base' in checkpoint_url else 'dit-large' else: A__ = '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(__UpperCamelCase , __UpperCamelCase ) , organization='nielsr' , commit_message='Add image processor' , use_temp_dir=__UpperCamelCase , ) model.push_to_hub( repo_path_or_name=Path(__UpperCamelCase , __UpperCamelCase ) , organization='nielsr' , commit_message='Add model' , use_temp_dir=__UpperCamelCase , ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = 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''', ) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_dit_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub)
9
0
"""simple docstring""" import itertools import os from collections import Counter, defaultdict from concurrent.futures import ThreadPoolExecutor, as_completed import numpy as np import datasets from .execute import check_correctness a = '''\ @misc{chen2021evaluating, title={Evaluating Large Language Models Trained on Code}, author={Mark Chen and Jerry Tworek and Heewoo Jun and Qiming Yuan \ and Henrique Ponde de Oliveira Pinto and Jared Kaplan and Harri Edwards \ and Yuri Burda and Nicholas Joseph and Greg Brockman and Alex Ray \ and Raul Puri and Gretchen Krueger and Michael Petrov and Heidy Khlaaf \ and Girish Sastry and Pamela Mishkin and Brooke Chan and Scott Gray \ and Nick Ryder and Mikhail Pavlov and Alethea Power and Lukasz Kaiser \ and Mohammad Bavarian and Clemens Winter and Philippe Tillet \ and Felipe Petroski Such and Dave Cummings and Matthias Plappert \ and Fotios Chantzis and Elizabeth Barnes and Ariel Herbert-Voss \ and William Hebgen Guss and Alex Nichol and Alex Paino and Nikolas Tezak \ and Jie Tang and Igor Babuschkin and Suchir Balaji and Shantanu Jain \ and William Saunders and Christopher Hesse and Andrew N. Carr \ and Jan Leike and Josh Achiam and Vedant Misra and Evan Morikawa \ and Alec Radford and Matthew Knight and Miles Brundage and Mira Murati \ and Katie Mayer and Peter Welinder and Bob McGrew and Dario Amodei \ and Sam McCandlish and Ilya Sutskever and Wojciech Zaremba}, year={2021}, eprint={2107.03374}, archivePrefix={arXiv}, primaryClass={cs.LG} } ''' a = '''\ This metric implements the evaluation harness for the HumanEval problem solving dataset described in the paper "Evaluating Large Language Models Trained on Code" (https://arxiv.org/abs/2107.03374). ''' a = ''' Calculates how good are predictions given some references, using certain scores Args: predictions: list of candidates to evaluate. Each candidates should be a list of strings with several code candidates to solve the problem. references: a list with a test for each prediction. Each test should evaluate the correctness of a code candidate. k: number of code candidates to consider in the evaluation (Default: [1, 10, 100]) num_workers: number of workers used to evaluate the canidate programs (Default: 4). timeout: Returns: pass_at_k: dict with pass rates for each k results: dict with granular results of each unittest Examples: >>> code_eval = datasets.load_metric("code_eval") >>> test_cases = ["assert add(2,3)==5"] >>> candidates = [["def add(a,b): return a*b", "def add(a, b): return a+b"]] >>> pass_at_k, results = code_eval.compute(references=test_cases, predictions=candidates, k=[1, 2]) >>> print(pass_at_k) {\'pass@1\': 0.5, \'pass@2\': 1.0} ''' a = ''' ################################################################################ !!!WARNING!!! ################################################################################ The "code_eval" metric executes untrusted model-generated code in Python. Although it is highly unlikely that model-generated code will do something overtly malicious in response to this test suite, model-generated code may act destructively due to a lack of model capability or alignment. Users are strongly encouraged to sandbox this evaluation suite so that it does not perform destructive actions on their host or network. For more information on how OpenAI sandboxes its code, see the paper "Evaluating Large Language Models Trained on Code" (https://arxiv.org/abs/2107.03374). Once you have read this disclaimer and taken appropriate precautions, set the environment variable HF_ALLOW_CODE_EVAL="1". Within Python you can to this with: >>> import os >>> os.environ["HF_ALLOW_CODE_EVAL"] = "1" ################################################################################\ ''' a = '''The MIT License Copyright (c) OpenAI (https://openai.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowercase_ ( datasets.Metric ): '''simple docstring''' def lowerCAmelCase_ ( self : Optional[Any] ): return datasets.MetricInfo( # This is the description that will appear on the metrics page. description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Sequence(datasets.Value('string' ) ), 'references': datasets.Value('string' ), } ) , homepage='https://github.com/openai/human-eval' , codebase_urls=['https://github.com/openai/human-eval'] , reference_urls=['https://github.com/openai/human-eval'] , license=_LICENSE , ) def lowerCAmelCase_ ( self : Union[str, Any] , _UpperCAmelCase : Union[str, Any] , _UpperCAmelCase : Optional[int] , _UpperCAmelCase : List[str]=[1, 10, 100] , _UpperCAmelCase : str=4 , _UpperCAmelCase : Union[str, Any]=3.0 ): if os.getenv('HF_ALLOW_CODE_EVAL' , 0 ) != "1": raise ValueError(_WARNING ) if os.name == "nt": raise NotImplementedError('This metric is currently not supported on Windows.' ) with ThreadPoolExecutor(max_workers=_UpperCAmelCase ) as executor: _A = [] _A = Counter() _A = 0 _A = defaultdict(_UpperCAmelCase ) for task_id, (candidates, test_case) in enumerate(zip(_UpperCAmelCase , _UpperCAmelCase ) ): for candidate in candidates: _A = candidate + '\n' + test_case _A = (test_program, timeout, task_id, completion_id[task_id]) _A = executor.submit(_UpperCAmelCase , *_UpperCAmelCase ) futures.append(_UpperCAmelCase ) completion_id[task_id] += 1 n_samples += 1 for future in as_completed(_UpperCAmelCase ): _A = future.result() results[result["task_id"]].append((result['completion_id'], result) ) _A , _A = [], [] for result in results.values(): result.sort() _A = [r[1]['passed'] for r in result] total.append(len(_UpperCAmelCase ) ) correct.append(sum(_UpperCAmelCase ) ) _A = np.array(_UpperCAmelCase ) _A = np.array(_UpperCAmelCase ) _A = k _A = {F'''pass@{k}''': estimate_pass_at_k(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ).mean() for k in ks if (total >= k).all()} return pass_at_k, results def _snake_case ( _snake_case : Tuple , _snake_case : int , _snake_case : int ) -> int: '''simple docstring''' def estimator(_snake_case : int , _snake_case : int , _snake_case : int ) -> float: if n - c < k: return 1.0 return 1.0 - np.prod(1.0 - k / np.arange(n - c + 1 , n + 1 ) ) if isinstance(_snake_case , _snake_case ): _A = itertools.repeat(_snake_case , len(_snake_case ) ) else: assert len(_snake_case ) == len(_snake_case ) _A = iter(_snake_case ) return np.array([estimator(int(_snake_case ) , int(_snake_case ) , _snake_case ) for n, c in zip(_snake_case , _snake_case )] )
7
SCREAMING_SNAKE_CASE__ = { '''A''': ['''B''', '''C''', '''E'''], '''B''': ['''A''', '''D''', '''E'''], '''C''': ['''A''', '''F''', '''G'''], '''D''': ['''B'''], '''E''': ['''A''', '''B''', '''D'''], '''F''': ['''C'''], '''G''': ['''C'''], } def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> list[str]: A__ = set() # keep track of all the paths to be checked A__ = [[start]] # return path if start is goal if start == goal: return [start] # keeps looping until all possible paths have been checked while queue: # pop the first path from the queue A__ = queue.pop(0 ) # get the last node from the path A__ = path[-1] if node not in explored: A__ = graph[node] # go through all neighbour nodes, construct a new path and # push it into the queue for neighbour in neighbours: A__ = list(__UpperCamelCase ) new_path.append(__UpperCamelCase ) queue.append(__UpperCamelCase ) # return path if neighbour is goal if neighbour == goal: return new_path # mark node as explored explored.add(__UpperCamelCase ) # in case there's no path between the 2 nodes return [] def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: if not graph or start not in graph or target not in graph: return -1 if start == target: return 0 A__ = [start] A__ = set(__UpperCamelCase ) # Keep tab on distances from `start` node. A__ = {start: 0, target: -1} while queue: A__ = queue.pop(0 ) if node == target: A__ = ( dist[node] if dist[target] == -1 else min(dist[target] , dist[node] ) ) for adjacent in graph[node]: if adjacent not in visited: visited.add(__UpperCamelCase ) queue.append(__UpperCamelCase ) A__ = dist[node] + 1 return dist[target] if __name__ == "__main__": print(bfs_shortest_path(demo_graph, '''G''', '''D''')) # returns ['G', 'C', 'A', 'B', 'D'] print(bfs_shortest_path_distance(demo_graph, '''G''', '''D''')) # returns 4
9
0
'''simple docstring''' import logging import os import sys from pathlib import Path from unittest.mock import patch from parameterized import parameterized from run_eval import run_generate from run_eval_search import run_search from transformers.testing_utils import CaptureStdout, TestCasePlus, slow from utils import ROUGE_KEYS logging.basicConfig(level=logging.DEBUG) lowercase__ : List[Any] = logging.getLogger() def _lowerCAmelCase ( __snake_case : Path , __snake_case : list ) -> List[str]: __A : Tuple = '\n'.join(__snake_case ) Path(__snake_case ).open('w' ).writelines(__snake_case ) lowercase__ : Optional[Any] = '''patrickvonplaten/t5-tiny-random''' lowercase__ : List[Any] = '''sshleifer/bart-tiny-random''' lowercase__ : Optional[Any] = '''sshleifer/tiny-mbart''' lowercase__ : Any = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) logging.disable(logging.CRITICAL) # remove noisy download output from tracebacks class SCREAMING_SNAKE_CASE (a__ ): def SCREAMING_SNAKE_CASE ( self , _UpperCAmelCase): '''simple docstring''' __A : Dict = Path(self.get_auto_remove_tmp_dir()) / 'utest_input.source' __A : Optional[Any] = input_file_name.parent / 'utest_output.txt' assert not output_file_name.exists() __A : Optional[int] = [' New York (CNN)When Liana Barrientos was 23 years old, she got married in Westchester County.'] _dump_articles(_UpperCAmelCase , _UpperCAmelCase) __A : Optional[int] = str(Path(self.get_auto_remove_tmp_dir()) / 'scores.json') __A : Dict = 'translation_en_to_de' if model == T5_TINY else 'summarization' __A : Any = F'\n run_eval_search.py\n {model}\n {input_file_name}\n {output_file_name}\n --score_path {score_path}\n --task {task}\n --num_beams 2\n --length_penalty 2.0\n '.split() with patch.object(_UpperCAmelCase , 'argv' , _UpperCAmelCase): run_generate() assert Path(_UpperCAmelCase).exists() # os.remove(Path(output_file_name)) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' self.run_eval_tester(_UpperCAmelCase) @parameterized.expand([BART_TINY, MBART_TINY]) @slow def SCREAMING_SNAKE_CASE ( self , _UpperCAmelCase): '''simple docstring''' self.run_eval_tester(_UpperCAmelCase) @parameterized.expand([T5_TINY, MBART_TINY]) @slow def SCREAMING_SNAKE_CASE ( self , _UpperCAmelCase): '''simple docstring''' __A : Tuple = Path(self.get_auto_remove_tmp_dir()) / 'utest_input.source' __A : str = input_file_name.parent / 'utest_output.txt' assert not output_file_name.exists() __A : int = { 'en': ['Machine learning is great, isn\'t it?', 'I like to eat bananas', 'Tomorrow is another great day!'], 'de': [ 'Maschinelles Lernen ist großartig, oder?', 'Ich esse gerne Bananen', 'Morgen ist wieder ein toller Tag!', ], } __A : Dict = Path(self.get_auto_remove_tmp_dir()) __A : str = str(tmp_dir / 'scores.json') __A : int = str(tmp_dir / 'val.target') _dump_articles(_UpperCAmelCase , text['en']) _dump_articles(_UpperCAmelCase , text['de']) __A : Optional[int] = 'translation_en_to_de' if model == T5_TINY else 'summarization' __A : int = F'\n run_eval_search.py\n {model}\n {str(_UpperCAmelCase)}\n {str(_UpperCAmelCase)}\n --score_path {score_path}\n --reference_path {reference_path}\n --task {task}\n '.split() testargs.extend(['--search', 'num_beams=1:2 length_penalty=0.9:1.0']) with patch.object(_UpperCAmelCase , 'argv' , _UpperCAmelCase): with CaptureStdout() as cs: run_search() __A : str = [' num_beams | length_penalty', model, 'Best score args'] __A : List[Any] = ['Info'] if "translation" in task: expected_strings.append('bleu') else: expected_strings.extend(_UpperCAmelCase) for w in expected_strings: assert w in cs.out for w in un_expected_strings: assert w not in cs.out assert Path(_UpperCAmelCase).exists() os.remove(Path(_UpperCAmelCase))
8
def A ( __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: A__ = 0 A__ = len(__UpperCamelCase ) - 1 while left <= right: # avoid divided by 0 during interpolation if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: return left else: return None A__ = left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left] ) # out of range check if point < 0 or point >= len(__UpperCamelCase ): return None A__ = sorted_collection[point] if current_item == item: return point else: if point < left: A__ = left A__ = point elif point > right: A__ = right A__ = point else: if item < current_item: A__ = point - 1 else: A__ = point + 1 return None def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: # avoid divided by 0 during interpolation if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: return left else: return None A__ = left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left] ) # out of range check if point < 0 or point >= len(__UpperCamelCase ): return None if sorted_collection[point] == item: return point elif point < left: return interpolation_search_by_recursion(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) elif point > right: return interpolation_search_by_recursion(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) else: if sorted_collection[point] > item: return interpolation_search_by_recursion( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , point - 1 ) else: return interpolation_search_by_recursion( __UpperCamelCase , __UpperCamelCase , point + 1 , __UpperCamelCase ) def A ( __UpperCamelCase ) -> List[str]: if collection != sorted(__UpperCamelCase ): raise ValueError('Collection must be ascending sorted' ) return True if __name__ == "__main__": import sys SCREAMING_SNAKE_CASE__ = 0 if debug == 1: SCREAMING_SNAKE_CASE__ = [1_0, 3_0, 4_0, 4_5, 5_0, 6_6, 7_7, 9_3] try: __assert_sorted(collection) except ValueError: sys.exit('''Sequence must be ascending sorted to apply interpolation search''') SCREAMING_SNAKE_CASE__ = 6_7 SCREAMING_SNAKE_CASE__ = interpolation_search(collection, target) if result is not None: print(f'{target} found at positions: {result}') else: print('''Not found''')
9
0
def _snake_case ( __snake_case , __snake_case ): return "\n".join( f"""{number} * {i} = {number * i}""" for i in range(1 , number_of_terms + 1 ) ) if __name__ == "__main__": print(multiplication_table(number=5, number_of_terms=10))
10
import warnings from ...utils import logging from .image_processing_clip import CLIPImageProcessor SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Dict , *_snake_case : int , **_snake_case : Optional[int] ): """simple docstring""" warnings.warn( 'The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please' ' use CLIPImageProcessor instead.' , _snake_case , ) super().__init__(*_snake_case , **_snake_case )
9
0
'''simple docstring''' import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DPTImageProcessor class __A ( unittest.TestCase ): '''simple docstring''' def __init__(self , A , A=7 , A=3 , A=18 , A=30 , A=400 , A=True , A=None , A=True , A=[0.5, 0.5, 0.5] , A=[0.5, 0.5, 0.5] , ) -> str: """simple docstring""" _a = size if size is not None else {'''height''': 18, '''width''': 18} _a = parent _a = batch_size _a = num_channels _a = image_size _a = min_resolution _a = max_resolution _a = do_resize _a = size _a = do_normalize _a = image_mean _a = image_std def a__ (self ) -> Union[str, Any]: """simple docstring""" return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = DPTImageProcessor if is_vision_available() else None def a__ (self ) -> Optional[Any]: """simple docstring""" _a = DPTImageProcessingTester(self ) @property def a__ (self ) -> int: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def a__ (self ) -> Dict: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A , '''image_mean''' ) ) self.assertTrue(hasattr(A , '''image_std''' ) ) self.assertTrue(hasattr(A , '''do_normalize''' ) ) self.assertTrue(hasattr(A , '''do_resize''' ) ) self.assertTrue(hasattr(A , '''size''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 18, '''width''': 18} ) _a = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'''height''': 42, '''width''': 42} ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _a = prepare_image_inputs(self.image_processor_tester , equal_resolution=A ) for image in image_inputs: self.assertIsInstance(A , Image.Image ) # Test not batched input _a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched _a = image_processing(A , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def a__ (self ) -> str: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _a = prepare_image_inputs(self.image_processor_tester , equal_resolution=A , numpify=A ) for image in image_inputs: self.assertIsInstance(A , np.ndarray ) # Test not batched input _a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched _a = image_processing(A , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def a__ (self ) -> Optional[int]: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _a = prepare_image_inputs(self.image_processor_tester , equal_resolution=A , torchify=A ) for image in image_inputs: self.assertIsInstance(A , torch.Tensor ) # Test not batched input _a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched _a = image_processing(A , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , )
11
import numpy as np import skfuzzy as fuzz if __name__ == "__main__": # Create universe of discourse in Python using linspace () SCREAMING_SNAKE_CASE__ = np.linspace(start=0, stop=7_5, num=7_5, endpoint=True, retstep=False) # Create two fuzzy sets by defining any membership function # (trapmf(), gbellmf(), gaussmf(), etc). SCREAMING_SNAKE_CASE__ = [0, 2_5, 5_0] SCREAMING_SNAKE_CASE__ = [2_5, 5_0, 7_5] SCREAMING_SNAKE_CASE__ = fuzz.membership.trimf(X, abca) SCREAMING_SNAKE_CASE__ = fuzz.membership.trimf(X, abca) # Compute the different operations using inbuilt functions. SCREAMING_SNAKE_CASE__ = np.ones(7_5) SCREAMING_SNAKE_CASE__ = np.zeros((7_5,)) # 1. Union = max(µA(x), µB(x)) SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_or(X, young, X, middle_aged)[1] # 2. Intersection = min(µA(x), µB(x)) SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_and(X, young, X, middle_aged)[1] # 3. Complement (A) = (1- min(µA(x)) SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_not(young) # 4. Difference (A/B) = min(µA(x),(1- µB(x))) SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_and(X, young, X, fuzz.fuzzy_not(middle_aged)[1])[1] # 5. Algebraic Sum = [µA(x) + µB(x) – (µA(x) * µB(x))] SCREAMING_SNAKE_CASE__ = young + middle_aged - (young * middle_aged) # 6. Algebraic Product = (µA(x) * µB(x)) SCREAMING_SNAKE_CASE__ = young * middle_aged # 7. Bounded Sum = min[1,(µA(x), µB(x))] SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_and(X, one, X, young + middle_aged)[1] # 8. Bounded difference = min[0,(µA(x), µB(x))] SCREAMING_SNAKE_CASE__ = fuzz.fuzzy_or(X, zero, X, young - middle_aged)[1] # max-min composition # max-product composition # Plot each set A, set B and each operation result using plot() and subplot(). from matplotlib import pyplot as plt plt.figure() plt.subplot(4, 3, 1) plt.plot(X, young) plt.title('''Young''') plt.grid(True) plt.subplot(4, 3, 2) plt.plot(X, middle_aged) plt.title('''Middle aged''') plt.grid(True) plt.subplot(4, 3, 3) plt.plot(X, union) plt.title('''union''') plt.grid(True) plt.subplot(4, 3, 4) plt.plot(X, intersection) plt.title('''intersection''') plt.grid(True) plt.subplot(4, 3, 5) plt.plot(X, complement_a) plt.title('''complement_a''') plt.grid(True) plt.subplot(4, 3, 6) plt.plot(X, difference) plt.title('''difference a/b''') plt.grid(True) plt.subplot(4, 3, 7) plt.plot(X, alg_sum) plt.title('''alg_sum''') plt.grid(True) plt.subplot(4, 3, 8) plt.plot(X, alg_product) plt.title('''alg_product''') plt.grid(True) plt.subplot(4, 3, 9) plt.plot(X, bdd_sum) plt.title('''bdd_sum''') plt.grid(True) plt.subplot(4, 3, 1_0) plt.plot(X, bdd_difference) plt.title('''bdd_difference''') plt.grid(True) plt.subplots_adjust(hspace=0.5) plt.show()
9
0
import argparse import hashlib import os import urllib import warnings import torch from torch import nn from tqdm import tqdm from transformers import WhisperConfig, WhisperForConditionalGeneration lowerCamelCase__ : int = { """tiny.en""": """https://openaipublic.azureedge.net/main/whisper/models/d3dd57d32accea0b295c96e26691aa14d8822fac7d9d27d5dc00b4ca2826dd03/tiny.en.pt""", """tiny""": """https://openaipublic.azureedge.net/main/whisper/models/65147644a518d12f04e32d6f3b26facc3f8dd46e5390956a9424a650c0ce22b9/tiny.pt""", """base.en""": """https://openaipublic.azureedge.net/main/whisper/models/25a8566e1d0c1e2231d1c762132cd20e0f96a85d16145c3a00adf5d1ac670ead/base.en.pt""", """base""": """https://openaipublic.azureedge.net/main/whisper/models/ed3a0b6b1c0edf879ad9b11b1af5a0e6ab5db9205f891f668f8b0e6c6326e34e/base.pt""", """small.en""": """https://openaipublic.azureedge.net/main/whisper/models/f953ad0fd29cacd07d5a9eda5624af0f6bcf2258be67c92b79389873d91e0872/small.en.pt""", """small""": """https://openaipublic.azureedge.net/main/whisper/models/9ecf779972d90ba49c06d968637d720dd632c55bbf19d441fb42bf17a411e794/small.pt""", """medium.en""": """https://openaipublic.azureedge.net/main/whisper/models/d7440d1dc186f76616474e0ff0b3b6b879abc9d1a4926b7adfa41db2d497ab4f/medium.en.pt""", """medium""": """https://openaipublic.azureedge.net/main/whisper/models/345ae4da62f9b3d59415adc60127b97c714f32e89e936602e85993674d08dcb1/medium.pt""", """large""": """https://openaipublic.azureedge.net/main/whisper/models/e4b87e7e0bf463eb8e6956e646f1e277e901512310def2c24bf0e11bd3c28e9a/large.pt""", """large-v2""": """https://openaipublic.azureedge.net/main/whisper/models/81f7c96c852ee8fc832187b0132e569d6c3065a3252ed18e56effd0b6a73e524/large-v2.pt""", } def UpperCamelCase ( lowercase_ ) -> Union[str, Any]: '''simple docstring''' lowercase__ : int = ["""layers""", """blocks"""] for k in ignore_keys: state_dict.pop(lowercase_ , lowercase_ ) lowerCamelCase__ : Optional[Any] = { """blocks""": """layers""", """mlp.0""": """fc1""", """mlp.2""": """fc2""", """mlp_ln""": """final_layer_norm""", """.attn.query""": """.self_attn.q_proj""", """.attn.key""": """.self_attn.k_proj""", """.attn.value""": """.self_attn.v_proj""", """.attn_ln""": """.self_attn_layer_norm""", """.attn.out""": """.self_attn.out_proj""", """.cross_attn.query""": """.encoder_attn.q_proj""", """.cross_attn.key""": """.encoder_attn.k_proj""", """.cross_attn.value""": """.encoder_attn.v_proj""", """.cross_attn_ln""": """.encoder_attn_layer_norm""", """.cross_attn.out""": """.encoder_attn.out_proj""", """decoder.ln.""": """decoder.layer_norm.""", """encoder.ln.""": """encoder.layer_norm.""", """token_embedding""": """embed_tokens""", """encoder.positional_embedding""": """encoder.embed_positions.weight""", """decoder.positional_embedding""": """decoder.embed_positions.weight""", """ln_post""": """layer_norm""", } def UpperCamelCase ( lowercase_ ) -> int: '''simple docstring''' lowercase__ : str = list(s_dict.keys() ) for key in keys: lowercase__ : int = key for k, v in WHISPER_MAPPING.items(): if k in key: lowercase__ : Union[str, Any] = new_key.replace(lowercase_ , lowercase_ ) print(F'{key} -> {new_key}' ) lowercase__ : List[str] = s_dict.pop(lowercase_ ) return s_dict def UpperCamelCase ( lowercase_ ) -> List[str]: '''simple docstring''' lowercase__ , lowercase__ : int = emb.weight.shape lowercase__ : List[Any] = nn.Linear(lowercase_ , lowercase_ , bias=lowercase_ ) lowercase__ : Any = emb.weight.data return lin_layer def UpperCamelCase ( lowercase_ , lowercase_ ) -> bytes: '''simple docstring''' os.makedirs(lowercase_ , exist_ok=lowercase_ ) lowercase__ : List[str] = os.path.basename(lowercase_ ) lowercase__ : Union[str, Any] = url.split("""/""" )[-2] lowercase__ : int = os.path.join(lowercase_ , lowercase_ ) if os.path.exists(lowercase_ ) and not os.path.isfile(lowercase_ ): raise RuntimeError(F'{download_target} exists and is not a regular file' ) if os.path.isfile(lowercase_ ): lowercase__ : Tuple = open(lowercase_ , """rb""" ).read() if hashlib.shaaaa(lowercase_ ).hexdigest() == expected_shaaaa: return model_bytes else: warnings.warn(F'{download_target} exists, but the SHA256 checksum does not match; re-downloading the file' ) with urllib.request.urlopen(lowercase_ ) as source, open(lowercase_ , """wb""" ) as output: with tqdm( total=int(source.info().get("""Content-Length""" ) ) , ncols=80 , unit="""iB""" , unit_scale=lowercase_ , unit_divisor=10_24 ) as loop: while True: lowercase__ : str = source.read(81_92 ) if not buffer: break output.write(lowercase_ ) loop.update(len(lowercase_ ) ) lowercase__ : Tuple = open(lowercase_ , """rb""" ).read() if hashlib.shaaaa(lowercase_ ).hexdigest() != expected_shaaaa: raise RuntimeError( """Model has been downloaded but the SHA256 checksum does not not match. Please retry loading the model.""" ) return model_bytes def UpperCamelCase ( lowercase_ , lowercase_ ) -> List[Any]: '''simple docstring''' if ".pt" not in checkpoint_path: lowercase__ : Dict = _download(_MODELS[checkpoint_path] ) else: lowercase__ : str = torch.load(lowercase_ , map_location="""cpu""" ) lowercase__ : List[Any] = original_checkpoint["""dims"""] lowercase__ : Optional[Any] = original_checkpoint["""model_state_dict"""] lowercase__ : str = state_dict["""decoder.token_embedding.weight"""] remove_ignore_keys_(lowercase_ ) rename_keys(lowercase_ ) lowercase__ : Any = True lowercase__ : Any = state_dict["""decoder.layers.0.fc1.weight"""].shape[0] lowercase__ : int = WhisperConfig( vocab_size=dimensions["""n_vocab"""] , encoder_ffn_dim=lowercase_ , decoder_ffn_dim=lowercase_ , num_mel_bins=dimensions["""n_mels"""] , d_model=dimensions["""n_audio_state"""] , max_target_positions=dimensions["""n_text_ctx"""] , encoder_layers=dimensions["""n_audio_layer"""] , encoder_attention_heads=dimensions["""n_audio_head"""] , decoder_layers=dimensions["""n_text_layer"""] , decoder_attention_heads=dimensions["""n_text_state"""] , max_source_positions=dimensions["""n_audio_ctx"""] , ) lowercase__ : Optional[Any] = WhisperForConditionalGeneration(lowercase_ ) lowercase__ , lowercase__ : int = model.model.load_state_dict(lowercase_ , strict=lowercase_ ) if len(lowercase_ ) > 0 and not set(lowercase_ ) <= { "encoder.embed_positions.weights", "decoder.embed_positions.weights", }: raise ValueError( """Only `encoder.embed_positions.weights` and `decoder.embed_positions.weights` are allowed to be missing,""" F' but all the following weights are missing {missing}' ) if tie_embeds: lowercase__ : List[Any] = make_linear_from_emb(model.model.decoder.embed_tokens ) else: lowercase__ : Tuple = proj_out_weights model.save_pretrained(lowercase_ ) if __name__ == "__main__": lowerCamelCase__ : Tuple = argparse.ArgumentParser() # # Required parameters parser.add_argument("""--checkpoint_path""", type=str, help="""Patht to the downloaded checkpoints""") parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") lowerCamelCase__ : Any = parser.parse_args() convert_openai_whisper_to_tfms(args.checkpoint_path, args.pytorch_dump_folder_path)
12
import unittest from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __lowerCAmelCase : """simple docstring""" @staticmethod def _a ( *_snake_case : int , **_snake_case : List[str] ): """simple docstring""" pass @is_pipeline_test @require_vision @require_torch class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" A__ : List[str] = MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING def _a ( self : Any , _snake_case : Union[str, Any] , _snake_case : Tuple , _snake_case : Optional[Any] ): """simple docstring""" A__ = pipeline( 'zero-shot-object-detection' , model='hf-internal-testing/tiny-random-owlvit-object-detection' ) A__ = [ { 'image': './tests/fixtures/tests_samples/COCO/000000039769.png', 'candidate_labels': ['cat', 'remote', 'couch'], } ] return object_detector, examples def _a ( self : int , _snake_case : int , _snake_case : List[str] ): """simple docstring""" A__ = object_detector(examples[0] , threshold=0.0 ) A__ = len(_snake_case ) self.assertGreater(_snake_case , 0 ) self.assertEqual( _snake_case , [ { 'score': ANY(_snake_case ), 'label': ANY(_snake_case ), 'box': {'xmin': ANY(_snake_case ), 'ymin': ANY(_snake_case ), 'xmax': ANY(_snake_case ), 'ymax': ANY(_snake_case )}, } for i in range(_snake_case ) ] , ) @require_tf @unittest.skip('Zero Shot Object Detection not implemented in TF' ) def _a ( self : List[str] ): """simple docstring""" pass @require_torch def _a ( self : Optional[int] ): """simple docstring""" A__ = pipeline( 'zero-shot-object-detection' , model='hf-internal-testing/tiny-random-owlvit-object-detection' ) A__ = object_detector( './tests/fixtures/tests_samples/COCO/000000039769.png' , candidate_labels=['cat', 'remote', 'couch'] , threshold=0.64 , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}}, {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}}, {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}}, ] , ) A__ = object_detector( [ { 'image': './tests/fixtures/tests_samples/COCO/000000039769.png', 'candidate_labels': ['cat', 'remote', 'couch'], } ] , threshold=0.64 , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ [ {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}}, {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}}, {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}}, {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}}, {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}}, ] ] , ) @require_torch @slow def _a ( self : int ): """simple docstring""" A__ = pipeline('zero-shot-object-detection' ) A__ = object_detector( 'http://images.cocodataset.org/val2017/000000039769.jpg' , candidate_labels=['cat', 'remote', 'couch'] , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}}, {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}}, {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}}, ] , ) A__ = object_detector( [ { 'image': 'http://images.cocodataset.org/val2017/000000039769.jpg', 'candidate_labels': ['cat', 'remote', 'couch'], }, { 'image': 'http://images.cocodataset.org/val2017/000000039769.jpg', 'candidate_labels': ['cat', 'remote', 'couch'], }, ] , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}}, {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}}, {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}}, ], [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}}, {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}}, {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}}, ], ] , ) @require_tf @unittest.skip('Zero Shot Object Detection not implemented in TF' ) def _a ( self : int ): """simple docstring""" pass @require_torch @slow def _a ( self : str ): """simple docstring""" A__ = 0.2 A__ = pipeline('zero-shot-object-detection' ) A__ = object_detector( 'http://images.cocodataset.org/val2017/000000039769.jpg' , candidate_labels=['cat', 'remote', 'couch'] , threshold=_snake_case , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}}, ] , ) @require_torch @slow def _a ( self : Any ): """simple docstring""" A__ = 2 A__ = pipeline('zero-shot-object-detection' ) A__ = object_detector( 'http://images.cocodataset.org/val2017/000000039769.jpg' , candidate_labels=['cat', 'remote', 'couch'] , top_k=_snake_case , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}}, {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}}, ] , )
9
0
'''simple docstring''' import gc import random import unittest import numpy as np import torch from PIL import Image from diffusers import ( DDIMScheduler, KandinskyVaaInpaintPipeline, KandinskyVaaPriorPipeline, UNetaDConditionModel, VQModel, ) 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 UpperCAmelCase_ (_UpperCAmelCase , unittest.TestCase ): """simple docstring""" lowerCamelCase : Any = KandinskyVaaInpaintPipeline lowerCamelCase : Dict = ['image_embeds', 'negative_image_embeds', 'image', 'mask_image'] lowerCamelCase : List[Any] = [ 'image_embeds', 'negative_image_embeds', 'image', 'mask_image', ] lowerCamelCase : Union[str, Any] = [ 'generator', 'height', 'width', 'latents', 'guidance_scale', 'num_inference_steps', 'return_dict', 'guidance_scale', 'num_images_per_prompt', 'output_type', 'return_dict', ] lowerCamelCase : str = False @property def lowercase_ ( self ) -> Union[str, Any]: return 32 @property def lowercase_ ( self ) -> str: return 32 @property def lowercase_ ( self ) -> Tuple: return self.time_input_dim @property def lowercase_ ( self ) -> Optional[Any]: return self.time_input_dim * 4 @property def lowercase_ ( self ) -> Optional[Any]: return 1_00 @property def lowercase_ ( self ) -> Any: torch.manual_seed(0 ) __lowerCamelCase : str = { 'in_channels': 9, # 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, } __lowerCamelCase : Union[str, Any] = UNetaDConditionModel(**SCREAMING_SNAKE_CASE_ ) return model @property def lowercase_ ( 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 lowercase_ ( self ) -> Optional[int]: torch.manual_seed(0 ) __lowerCamelCase : List[Any] = VQModel(**self.dummy_movq_kwargs ) return model def lowercase_ ( self ) -> Any: __lowerCamelCase : List[str] = self.dummy_unet __lowerCamelCase : str = self.dummy_movq __lowerCamelCase : List[str] = DDIMScheduler( num_train_timesteps=10_00 , beta_schedule='linear' , beta_start=0.0_0_0_8_5 , beta_end=0.0_1_2 , clip_sample=SCREAMING_SNAKE_CASE_ , set_alpha_to_one=SCREAMING_SNAKE_CASE_ , steps_offset=1 , prediction_type='epsilon' , thresholding=SCREAMING_SNAKE_CASE_ , ) __lowerCamelCase : Optional[Any] = { 'unet': unet, 'scheduler': scheduler, 'movq': movq, } return components def lowercase_ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=0 ) -> Tuple: __lowerCamelCase : Union[str, Any] = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(SCREAMING_SNAKE_CASE_ ) ).to(SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : str = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(seed + 1 ) ).to( SCREAMING_SNAKE_CASE_ ) # create init_image __lowerCamelCase : Optional[Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(SCREAMING_SNAKE_CASE_ ) ).to(SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : Union[str, Any] = image.cpu().permute(0 , 2 , 3 , 1 )[0] __lowerCamelCase : Optional[int] = Image.fromarray(np.uinta(SCREAMING_SNAKE_CASE_ ) ).convert('RGB' ).resize((2_56, 2_56) ) # create mask __lowerCamelCase : Optional[Any] = np.ones((64, 64) , dtype=np.floataa ) __lowerCamelCase : Dict = 0 if str(SCREAMING_SNAKE_CASE_ ).startswith('mps' ): __lowerCamelCase : Any = torch.manual_seed(SCREAMING_SNAKE_CASE_ ) else: __lowerCamelCase : Optional[int] = torch.Generator(device=SCREAMING_SNAKE_CASE_ ).manual_seed(SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : Optional[int] = { 'image': init_image, 'mask_image': mask, 'image_embeds': image_embeds, 'negative_image_embeds': negative_image_embeds, 'generator': generator, 'height': 64, 'width': 64, 'num_inference_steps': 2, 'guidance_scale': 4.0, 'output_type': 'np', } return inputs def lowercase_ ( self ) -> str: __lowerCamelCase : List[str] = 'cpu' __lowerCamelCase : int = self.get_dummy_components() __lowerCamelCase : Optional[int] = self.pipeline_class(**SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : Union[str, Any] = pipe.to(SCREAMING_SNAKE_CASE_ ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : str = pipe(**self.get_dummy_inputs(SCREAMING_SNAKE_CASE_ ) ) __lowerCamelCase : List[str] = output.images __lowerCamelCase : Dict = pipe( **self.get_dummy_inputs(SCREAMING_SNAKE_CASE_ ) , return_dict=SCREAMING_SNAKE_CASE_ , )[0] __lowerCamelCase : int = image[0, -3:, -3:, -1] __lowerCamelCase : Optional[Any] = image_from_tuple[0, -3:, -3:, -1] print(f'image.shape {image.shape}' ) assert image.shape == (1, 64, 64, 3) __lowerCamelCase : Dict = np.array( [0.5_0_7_7_5_9_0_3, 0.4_9_5_2_7_1_9_5, 0.4_8_8_2_4_5_4_3, 0.5_0_1_9_2_2_3_7, 0.4_8_6_4_4_9_0_6, 0.4_9_3_7_3_8_1_4, 0.4_7_8_0_5_9_8, 0.4_7_2_3_4_8_2_7, 0.4_8_3_2_7_8_4_8] ) 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 lowercase_ ( self ) -> List[Any]: super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class UpperCAmelCase_ (unittest.TestCase ): """simple docstring""" def lowercase_ ( self ) -> Optional[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def lowercase_ ( self ) -> Optional[Any]: __lowerCamelCase : str = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinskyv22/kandinskyv22_inpaint_cat_with_hat_fp16.npy' ) __lowerCamelCase : Optional[Any] = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinsky/cat.png' ) __lowerCamelCase : str = np.ones((7_68, 7_68) , dtype=np.floataa ) __lowerCamelCase : str = 0 __lowerCamelCase : Tuple = 'a hat' __lowerCamelCase : Union[str, Any] = KandinskyVaaPriorPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-2-prior' , torch_dtype=torch.floataa ) pipe_prior.to(SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : Optional[Any] = KandinskyVaaInpaintPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-2-decoder-inpaint' , torch_dtype=torch.floataa ) __lowerCamelCase : Optional[Any] = pipeline.to(SCREAMING_SNAKE_CASE_ ) pipeline.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : Dict = torch.Generator(device='cpu' ).manual_seed(0 ) __lowerCamelCase , __lowerCamelCase : str = pipe_prior( SCREAMING_SNAKE_CASE_ , generator=SCREAMING_SNAKE_CASE_ , num_inference_steps=5 , negative_prompt='' , ).to_tuple() __lowerCamelCase : Optional[Any] = pipeline( image=SCREAMING_SNAKE_CASE_ , mask_image=SCREAMING_SNAKE_CASE_ , image_embeds=SCREAMING_SNAKE_CASE_ , negative_image_embeds=SCREAMING_SNAKE_CASE_ , generator=SCREAMING_SNAKE_CASE_ , num_inference_steps=1_00 , height=7_68 , width=7_68 , output_type='np' , ) __lowerCamelCase : Optional[int] = output.images[0] assert image.shape == (7_68, 7_68, 3) assert_mean_pixel_difference(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )
13
import dataclasses import json import sys import types from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, ArgumentTypeError from copy import copy from enum import Enum from inspect import isclass from pathlib import Path from typing import Any, Callable, Dict, Iterable, List, Literal, NewType, Optional, Tuple, Union, get_type_hints import yaml SCREAMING_SNAKE_CASE__ = NewType('''DataClass''', Any) SCREAMING_SNAKE_CASE__ = NewType('''DataClassType''', Any) def A ( __UpperCamelCase ) -> List[Any]: if isinstance(__UpperCamelCase , __UpperCamelCase ): return v if v.lower() in ("yes", "true", "t", "y", "1"): return True elif v.lower() in ("no", "false", "f", "n", "0"): return False else: raise ArgumentTypeError( f'''Truthy value expected: got {v} but expected one of yes/no, true/false, t/f, y/n, 1/0 (case insensitive).''' ) def A ( __UpperCamelCase ) -> Callable[[str], Any]: A__ = {str(__UpperCamelCase ): choice for choice in choices} return lambda __UpperCamelCase : str_to_choice.get(__UpperCamelCase , __UpperCamelCase ) def A ( *, __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = dataclasses.MISSING , __UpperCamelCase = dataclasses.MISSING , __UpperCamelCase = None , **__UpperCamelCase , ) -> dataclasses.Field: if metadata is None: # Important, don't use as default param in function signature because dict is mutable and shared across function calls A__ = {} if aliases is not None: A__ = aliases if help is not None: A__ = help return dataclasses.field(metadata=__UpperCamelCase , default=__UpperCamelCase , default_factory=__UpperCamelCase , **__UpperCamelCase ) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Iterable[DataClassType] def __init__( self : Optional[int] , _snake_case : Union[DataClassType, Iterable[DataClassType]] , **_snake_case : Tuple ): """simple docstring""" if "formatter_class" not in kwargs: A__ = ArgumentDefaultsHelpFormatter super().__init__(**_snake_case ) if dataclasses.is_dataclass(_snake_case ): A__ = [dataclass_types] A__ = list(_snake_case ) for dtype in self.dataclass_types: self._add_dataclass_arguments(_snake_case ) @staticmethod def _a ( _snake_case : ArgumentParser , _snake_case : dataclasses.Field ): """simple docstring""" A__ = F'''--{field.name}''' A__ = field.metadata.copy() # field.metadata is not used at all by Data Classes, # it is provided as a third-party extension mechanism. if isinstance(field.type , _snake_case ): raise RuntimeError( 'Unresolved type detected, which should have been done with the help of ' '`typing.get_type_hints` method by default' ) A__ = kwargs.pop('aliases' , [] ) if isinstance(_snake_case , _snake_case ): A__ = [aliases] A__ = getattr(field.type , '__origin__' , field.type ) if origin_type is Union or (hasattr(_snake_case , 'UnionType' ) and isinstance(_snake_case , types.UnionType )): if str not in field.type.__args__ and ( len(field.type.__args__ ) != 2 or type(_snake_case ) not in field.type.__args__ ): raise ValueError( 'Only `Union[X, NoneType]` (i.e., `Optional[X]`) is allowed for `Union` because' ' the argument parser only supports one type per argument.' F''' Problem encountered in field \'{field.name}\'.''' ) if type(_snake_case ) not in field.type.__args__: # filter `str` in Union A__ = field.type.__args__[0] if field.type.__args__[1] == str else field.type.__args__[1] A__ = getattr(field.type , '__origin__' , field.type ) elif bool not in field.type.__args__: # filter `NoneType` in Union (except for `Union[bool, NoneType]`) A__ = ( field.type.__args__[0] if isinstance(_snake_case , field.type.__args__[1] ) else field.type.__args__[1] ) A__ = getattr(field.type , '__origin__' , field.type ) # A variable to store kwargs for a boolean field, if needed # so that we can init a `no_*` complement argument (see below) A__ = {} if origin_type is Literal or (isinstance(field.type , _snake_case ) and issubclass(field.type , _snake_case )): if origin_type is Literal: A__ = field.type.__args__ else: A__ = [x.value for x in field.type] A__ = make_choice_type_function(kwargs['choices'] ) if field.default is not dataclasses.MISSING: A__ = field.default else: A__ = True elif field.type is bool or field.type == Optional[bool]: # Copy the currect kwargs to use to instantiate a `no_*` complement argument below. # We do not initialize it here because the `no_*` alternative must be instantiated after the real argument A__ = copy(_snake_case ) # Hack because type=bool in argparse does not behave as we want. A__ = string_to_bool if field.type is bool or (field.default is not None and field.default is not dataclasses.MISSING): # Default value is False if we have no default when of type bool. A__ = False if field.default is dataclasses.MISSING else field.default # This is the value that will get picked if we don't include --field_name in any way A__ = default # This tells argparse we accept 0 or 1 value after --field_name A__ = '?' # This is the value that will get picked if we do --field_name (without value) A__ = True elif isclass(_snake_case ) and issubclass(_snake_case , _snake_case ): A__ = field.type.__args__[0] A__ = '+' if field.default_factory is not dataclasses.MISSING: A__ = field.default_factory() elif field.default is dataclasses.MISSING: A__ = True else: A__ = field.type if field.default is not dataclasses.MISSING: A__ = field.default elif field.default_factory is not dataclasses.MISSING: A__ = field.default_factory() else: A__ = True parser.add_argument(_snake_case , *_snake_case , **_snake_case ) # Add a complement `no_*` argument for a boolean field AFTER the initial field has already been added. # Order is important for arguments with the same destination! # We use a copy of earlier kwargs because the original kwargs have changed a lot before reaching down # here and we do not need those changes/additional keys. if field.default is True and (field.type is bool or field.type == Optional[bool]): A__ = False parser.add_argument(F'''--no_{field.name}''' , action='store_false' , dest=field.name , **_snake_case ) def _a ( self : Any , _snake_case : DataClassType ): """simple docstring""" if hasattr(_snake_case , '_argument_group_name' ): A__ = self.add_argument_group(dtype._argument_group_name ) else: A__ = self try: A__ = get_type_hints(_snake_case ) except NameError: raise RuntimeError( F'''Type resolution failed for {dtype}. Try declaring the class in global scope or ''' 'removing line of `from __future__ import annotations` which opts in Postponed ' 'Evaluation of Annotations (PEP 563)' ) except TypeError as ex: # Remove this block when we drop Python 3.9 support if sys.version_info[:2] < (3, 10) and "unsupported operand type(s) for |" in str(_snake_case ): A__ = '.'.join(map(_snake_case , sys.version_info[:3] ) ) raise RuntimeError( F'''Type resolution failed for {dtype} on Python {python_version}. Try removing ''' 'line of `from __future__ import annotations` which opts in union types as ' '`X | Y` (PEP 604) via Postponed Evaluation of Annotations (PEP 563). To ' 'support Python versions that lower than 3.10, you need to use ' '`typing.Union[X, Y]` instead of `X | Y` and `typing.Optional[X]` instead of ' '`X | None`.' ) from ex raise for field in dataclasses.fields(_snake_case ): if not field.init: continue A__ = type_hints[field.name] self._parse_dataclass_field(_snake_case , _snake_case ) def _a ( self : Optional[int] , _snake_case : Optional[Any]=None , _snake_case : Any=False , _snake_case : int=True , _snake_case : List[Any]=None , _snake_case : int=None , ): """simple docstring""" if args_file_flag or args_filename or (look_for_args_file and len(sys.argv )): A__ = [] if args_filename: args_files.append(Path(_snake_case ) ) elif look_for_args_file and len(sys.argv ): args_files.append(Path(sys.argv[0] ).with_suffix('.args' ) ) # args files specified via command line flag should overwrite default args files so we add them last if args_file_flag: # Create special parser just to extract the args_file_flag values A__ = ArgumentParser() args_file_parser.add_argument(_snake_case , type=_snake_case , action='append' ) # Use only remaining args for further parsing (remove the args_file_flag) A__ , A__ = args_file_parser.parse_known_args(args=_snake_case ) A__ = vars(_snake_case ).get(args_file_flag.lstrip('-' ) , _snake_case ) if cmd_args_file_paths: args_files.extend([Path(_snake_case ) for p in cmd_args_file_paths] ) A__ = [] for args_file in args_files: if args_file.exists(): file_args += args_file.read_text().split() # in case of duplicate arguments the last one has precedence # args specified via the command line should overwrite args from files, so we add them last A__ = file_args + args if args is not None else file_args + sys.argv[1:] A__ , A__ = self.parse_known_args(args=_snake_case ) A__ = [] for dtype in self.dataclass_types: A__ = {f.name for f in dataclasses.fields(_snake_case ) if f.init} A__ = {k: v for k, v in vars(_snake_case ).items() if k in keys} for k in keys: delattr(_snake_case , _snake_case ) A__ = dtype(**_snake_case ) outputs.append(_snake_case ) if len(namespace.__dict__ ) > 0: # additional namespace. outputs.append(_snake_case ) if return_remaining_strings: return (*outputs, remaining_args) else: if remaining_args: raise ValueError(F'''Some specified arguments are not used by the HfArgumentParser: {remaining_args}''' ) return (*outputs,) def _a ( self : Dict , _snake_case : Dict[str, Any] , _snake_case : bool = False ): """simple docstring""" A__ = set(args.keys() ) A__ = [] for dtype in self.dataclass_types: A__ = {f.name for f in dataclasses.fields(_snake_case ) if f.init} A__ = {k: v for k, v in args.items() if k in keys} unused_keys.difference_update(inputs.keys() ) A__ = dtype(**_snake_case ) outputs.append(_snake_case ) if not allow_extra_keys and unused_keys: raise ValueError(F'''Some keys are not used by the HfArgumentParser: {sorted(_snake_case )}''' ) return tuple(_snake_case ) def _a ( self : Dict , _snake_case : str , _snake_case : bool = False ): """simple docstring""" with open(Path(_snake_case ) , encoding='utf-8' ) as open_json_file: A__ = json.loads(open_json_file.read() ) A__ = self.parse_dict(_snake_case , allow_extra_keys=_snake_case ) return tuple(_snake_case ) def _a ( self : Tuple , _snake_case : str , _snake_case : bool = False ): """simple docstring""" A__ = self.parse_dict(yaml.safe_load(Path(_snake_case ).read_text() ) , allow_extra_keys=_snake_case ) return tuple(_snake_case )
9
0
import argparse from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_controlnet_from_original_ckpt if __name__ == "__main__": a__ = argparse.ArgumentParser() parser.add_argument( '''--checkpoint_path''', default=None, type=str, required=True, help='''Path to the checkpoint to convert.''' ) parser.add_argument( '''--original_config_file''', type=str, required=True, help='''The YAML config file corresponding to the original architecture.''', ) parser.add_argument( '''--num_in_channels''', default=None, type=int, help='''The number of input channels. If `None` number of input channels will be automatically inferred.''', ) parser.add_argument( '''--image_size''', default=512, type=int, help=( '''The image size that the model was trained on. Use 512 for Stable Diffusion v1.X and Stable Siffusion v2''' ''' Base. Use 768 for Stable Diffusion v2.''' ), ) parser.add_argument( '''--extract_ema''', action='''store_true''', help=( '''Only relevant for checkpoints that have both EMA and non-EMA weights. Whether to extract the EMA weights''' ''' or not. Defaults to `False`. Add `--extract_ema` to extract the EMA weights. EMA weights usually yield''' ''' higher quality images for inference. Non-EMA weights are usually better to continue fine-tuning.''' ), ) parser.add_argument( '''--upcast_attention''', action='''store_true''', help=( '''Whether the attention computation should always be upcasted. This is necessary when running stable''' ''' diffusion 2.1.''' ), ) parser.add_argument( '''--from_safetensors''', action='''store_true''', help='''If `--checkpoint_path` is in `safetensors` format, load checkpoint with safetensors instead of PyTorch.''', ) parser.add_argument( '''--to_safetensors''', action='''store_true''', help='''Whether to store pipeline in safetensors format or not.''', ) parser.add_argument('''--dump_path''', default=None, type=str, required=True, help='''Path to the output model.''') parser.add_argument('''--device''', type=str, help='''Device to use (e.g. cpu, cuda:0, cuda:1, etc.)''') def __UpperCAmelCase ( __a : Any ) -> List[Any]: """simple docstring""" if string == "True": return True elif string == "False": return False else: raise ValueError(F"""could not parse string as bool {string}""" ) parser.add_argument( '''--use_linear_projection''', help='''Override for use linear projection''', required=False, type=parse_bool ) parser.add_argument('''--cross_attention_dim''', help='''Override for cross attention_dim''', required=False, type=int) a__ = parser.parse_args() a__ = download_controlnet_from_original_ckpt( checkpoint_path=args.checkpoint_path, original_config_file=args.original_config_file, image_size=args.image_size, extract_ema=args.extract_ema, num_in_channels=args.num_in_channels, upcast_attention=args.upcast_attention, from_safetensors=args.from_safetensors, device=args.device, use_linear_projection=args.use_linear_projection, cross_attention_dim=args.cross_attention_dim, ) controlnet.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)
14
import argparse import collections import json from pathlib import Path import requests import torch import yaml from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( MobileViTImageProcessor, MobileViTVaConfig, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation, ) from transformers.utils import logging logging.set_verbosity_info() SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) def A ( __UpperCamelCase ) -> List[Any]: print('Loading config file...' ) def flatten_yaml_as_dict(__UpperCamelCase , __UpperCamelCase="" , __UpperCamelCase="." ): A__ = [] for k, v in d.items(): A__ = parent_key + sep + k if parent_key else k if isinstance(__UpperCamelCase , collections.abc.MutableMapping ): items.extend(flatten_yaml_as_dict(__UpperCamelCase , __UpperCamelCase , sep=__UpperCamelCase ).items() ) else: items.append((new_key, v) ) return dict(__UpperCamelCase ) A__ = argparse.Namespace() with open(__UpperCamelCase , 'r' ) as yaml_file: try: A__ = yaml.load(__UpperCamelCase , Loader=yaml.FullLoader ) A__ = flatten_yaml_as_dict(__UpperCamelCase ) for k, v in flat_cfg.items(): setattr(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) except yaml.YAMLError as exc: logger.error('Error while loading config file: {}. Error message: {}'.format(__UpperCamelCase , str(__UpperCamelCase ) ) ) return config def A ( __UpperCamelCase , __UpperCamelCase ) -> Optional[Any]: A__ = MobileViTVaConfig() A__ = False # dataset if task_name.startswith('imagenet1k_' ): A__ = 1_000 if int(task_name.strip().split('_' )[-1] ) == 384: A__ = 384 else: A__ = 256 A__ = 'imagenet-1k-id2label.json' elif task_name.startswith('imagenet21k_to_1k_' ): A__ = 21_000 if int(task_name.strip().split('_' )[-1] ) == 384: A__ = 384 else: A__ = 256 A__ = 'imagenet-22k-id2label.json' elif task_name.startswith('ade20k_' ): A__ = 151 A__ = 512 A__ = 'ade20k-id2label.json' A__ = True elif task_name.startswith('voc_' ): A__ = 21 A__ = 512 A__ = 'pascal-voc-id2label.json' A__ = True # orig_config A__ = load_orig_config_file(__UpperCamelCase ) assert getattr(__UpperCamelCase , 'model.classification.name' , -1 ) == "mobilevit_v2", "Invalid model" A__ = getattr(__UpperCamelCase , 'model.classification.mitv2.width_multiplier' , 1.0 ) assert ( getattr(__UpperCamelCase , 'model.classification.mitv2.attn_norm_layer' , -1 ) == "layer_norm_2d" ), "Norm layers other than layer_norm_2d is not supported" A__ = getattr(__UpperCamelCase , 'model.classification.activation.name' , 'swish' ) # config.image_size == getattr(orig_config, 'sampler.bs.crop_size_width', 256) if is_segmentation_model: A__ = getattr(__UpperCamelCase , 'model.segmentation.output_stride' , 16 ) if "_deeplabv3" in task_name: A__ = getattr(__UpperCamelCase , 'model.segmentation.deeplabv3.aspp_rates' , [12, 24, 36] ) A__ = getattr(__UpperCamelCase , 'model.segmentation.deeplabv3.aspp_out_channels' , 512 ) A__ = getattr(__UpperCamelCase , 'model.segmentation.deeplabv3.aspp_dropout' , 0.1 ) # id2label A__ = 'huggingface/label-files' A__ = json.load(open(hf_hub_download(__UpperCamelCase , __UpperCamelCase , repo_type='dataset' ) , 'r' ) ) A__ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} A__ = idalabel A__ = {v: k for k, v in idalabel.items()} return config def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[str]: A__ = dct.pop(__UpperCamelCase ) A__ = val def A ( __UpperCamelCase , __UpperCamelCase=False ) -> Dict: if base_model: A__ = '' else: A__ = 'mobilevitv2.' A__ = [] for k in state_dict.keys(): if k[:8] == "encoder.": A__ = k[8:] else: A__ = k if ".block." in k: A__ = k_new.replace('.block.' , '.' ) if ".conv." in k: A__ = k_new.replace('.conv.' , '.convolution.' ) if ".norm." in k: A__ = k_new.replace('.norm.' , '.normalization.' ) if "conv_1." in k: A__ = k_new.replace('conv_1.' , f'''{model_prefix}conv_stem.''' ) for i in [1, 2]: if f'''layer_{i}.''' in k: A__ = k_new.replace(f'''layer_{i}.''' , f'''{model_prefix}encoder.layer.{i-1}.layer.''' ) if ".exp_1x1." in k: A__ = k_new.replace('.exp_1x1.' , '.expand_1x1.' ) if ".red_1x1." in k: A__ = k_new.replace('.red_1x1.' , '.reduce_1x1.' ) for i in [3, 4, 5]: if f'''layer_{i}.0.''' in k: A__ = k_new.replace(f'''layer_{i}.0.''' , f'''{model_prefix}encoder.layer.{i-1}.downsampling_layer.''' ) if f'''layer_{i}.1.local_rep.0.''' in k: A__ = k_new.replace(f'''layer_{i}.1.local_rep.0.''' , f'''{model_prefix}encoder.layer.{i-1}.conv_kxk.''' ) if f'''layer_{i}.1.local_rep.1.''' in k: A__ = k_new.replace(f'''layer_{i}.1.local_rep.1.''' , f'''{model_prefix}encoder.layer.{i-1}.conv_1x1.''' ) for i in [3, 4, 5]: if i == 3: A__ = [0, 1] elif i == 4: A__ = [0, 1, 2, 3] elif i == 5: A__ = [0, 1, 2] for j in j_in: if f'''layer_{i}.1.global_rep.{j}.''' in k: A__ = k_new.replace( f'''layer_{i}.1.global_rep.{j}.''' , f'''{model_prefix}encoder.layer.{i-1}.transformer.layer.{j}.''' ) if f'''layer_{i}.1.global_rep.{j+1}.''' in k: A__ = k_new.replace( f'''layer_{i}.1.global_rep.{j+1}.''' , f'''{model_prefix}encoder.layer.{i-1}.layernorm.''' ) if f'''layer_{i}.1.conv_proj.''' in k: A__ = k_new.replace(f'''layer_{i}.1.conv_proj.''' , f'''{model_prefix}encoder.layer.{i-1}.conv_projection.''' ) if "pre_norm_attn.0." in k: A__ = k_new.replace('pre_norm_attn.0.' , 'layernorm_before.' ) if "pre_norm_attn.1." in k: A__ = k_new.replace('pre_norm_attn.1.' , 'attention.' ) if "pre_norm_ffn.0." in k: A__ = k_new.replace('pre_norm_ffn.0.' , 'layernorm_after.' ) if "pre_norm_ffn.1." in k: A__ = k_new.replace('pre_norm_ffn.1.' , 'ffn.conv1.' ) if "pre_norm_ffn.3." in k: A__ = k_new.replace('pre_norm_ffn.3.' , 'ffn.conv2.' ) if "classifier.1." in k: A__ = k_new.replace('classifier.1.' , 'classifier.' ) if "seg_head." in k: A__ = k_new.replace('seg_head.' , 'segmentation_head.' ) if ".aspp_layer." in k: A__ = k_new.replace('.aspp_layer.' , '.' ) if ".aspp_pool." in k: A__ = k_new.replace('.aspp_pool.' , '.' ) rename_keys.append((k, k_new) ) return rename_keys def A ( __UpperCamelCase ) -> Tuple: A__ = [] for k in state_dict.keys(): if k.startswith('seg_head.aux_head.' ): keys_to_ignore.append(__UpperCamelCase ) for k in keys_to_ignore: state_dict.pop(__UpperCamelCase , __UpperCamelCase ) def A ( ) -> str: A__ = 'http://images.cocodataset.org/val2017/000000039769.jpg' # url = "https://cdn.britannica.com/86/141086-050-9D7C75EE/Gulfstream-G450-business-jet-passengers.jpg" A__ = Image.open(requests.get(__UpperCamelCase , stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Optional[Any]: A__ = get_mobilevitva_config(__UpperCamelCase , __UpperCamelCase ) # load original state_dict A__ = torch.load(__UpperCamelCase , map_location='cpu' ) # load huggingface model if task_name.startswith('ade20k_' ) or task_name.startswith('voc_' ): A__ = MobileViTVaForSemanticSegmentation(__UpperCamelCase ).eval() A__ = False else: A__ = MobileViTVaForImageClassification(__UpperCamelCase ).eval() A__ = False # remove and rename some keys of load the original model A__ = checkpoint remove_unused_keys(__UpperCamelCase ) A__ = create_rename_keys(__UpperCamelCase , base_model=__UpperCamelCase ) for rename_key_src, rename_key_dest in rename_keys: rename_key(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) # load modified state_dict model.load_state_dict(__UpperCamelCase ) # Check outputs on an image, prepared by MobileViTImageProcessor A__ = MobileViTImageProcessor(crop_size=config.image_size , size=config.image_size + 32 ) A__ = image_processor(images=prepare_img() , return_tensors='pt' ) A__ = model(**__UpperCamelCase ) # verify classification model if task_name.startswith('imagenet' ): A__ = outputs.logits A__ = logits.argmax(-1 ).item() print('Predicted class:' , model.config.idalabel[predicted_class_idx] ) if task_name.startswith('imagenet1k_256' ) and config.width_multiplier == 1.0: # expected_logits for base variant A__ = torch.tensor([-1.6336E00, -7.3204E-02, -5.1883E-01] ) assert torch.allclose(logits[0, :3] , __UpperCamelCase , atol=1E-4 ) Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) print(f'''Saving model {task_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(__UpperCamelCase ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(__UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--task''', default='''imagenet1k_256''', type=str, help=( '''Name of the task for which the MobileViTV2 model you\'d like to convert is trained on . ''' ''' Classification (ImageNet-1k) - MobileViTV2 (256x256) : imagenet1k_256 - MobileViTV2 (Trained on 256x256 and Finetuned on 384x384) : imagenet1k_384 - MobileViTV2 (Trained on ImageNet-21k and Finetuned on ImageNet-1k 256x256) : imagenet21k_to_1k_256 - MobileViTV2 (Trained on ImageNet-21k, Finetuned on ImageNet-1k 256x256, and Finetuned on ImageNet-1k 384x384) : imagenet21k_to_1k_384 Segmentation - ADE20K Dataset : ade20k_deeplabv3 - Pascal VOC 2012 Dataset: voc_deeplabv3 ''' ), choices=[ '''imagenet1k_256''', '''imagenet1k_384''', '''imagenet21k_to_1k_256''', '''imagenet21k_to_1k_384''', '''ade20k_deeplabv3''', '''voc_deeplabv3''', ], ) parser.add_argument( '''--orig_checkpoint_path''', required=True, type=str, help='''Path to the original state dict (.pt file).''' ) parser.add_argument('''--orig_config_path''', required=True, type=str, help='''Path to the original config file.''') parser.add_argument( '''--pytorch_dump_folder_path''', required=True, type=str, help='''Path to the output PyTorch model directory.''' ) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_mobilevitva_checkpoint( args.task, args.orig_checkpoint_path, args.orig_config_path, args.pytorch_dump_folder_path )
9
0
from __future__ import annotations from collections import deque from collections.abc import Sequence from dataclasses import dataclass from typing import Any @dataclass class A : '''simple docstring''' A__ = 42 A__ = None A__ = None def UpperCamelCase ( ) -> Node | None: """simple docstring""" lowercase__ = Node(1 ) lowercase__ = Node(2 ) lowercase__ = Node(3 ) lowercase__ = Node(4 ) lowercase__ = Node(5 ) return tree def UpperCamelCase ( __magic_name__ : Node | None ) -> list[int]: """simple docstring""" return [root.data, *preorder(root.left ), *preorder(root.right )] if root else [] def UpperCamelCase ( __magic_name__ : Node | None ) -> list[int]: """simple docstring""" return postorder(root.left ) + postorder(root.right ) + [root.data] if root else [] def UpperCamelCase ( __magic_name__ : Node | None ) -> list[int]: """simple docstring""" return [*inorder(root.left ), root.data, *inorder(root.right )] if root else [] def UpperCamelCase ( __magic_name__ : Node | None ) -> int: """simple docstring""" return (max(height(root.left ) , height(root.right ) ) + 1) if root else 0 def UpperCamelCase ( __magic_name__ : Node | None ) -> Sequence[Node | None]: """simple docstring""" lowercase__ = [] if root is None: return output lowercase__ = deque([root] ) while process_queue: lowercase__ = process_queue.popleft() output.append(node.data ) if node.left: process_queue.append(node.left ) if node.right: process_queue.append(node.right ) return output def UpperCamelCase ( __magic_name__ : Node | None , __magic_name__ : int ) -> Sequence[Node | None]: """simple docstring""" lowercase__ = [] def populate_output(__magic_name__ : Node | None , __magic_name__ : int ) -> None: if not root: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.left , level - 1 ) populate_output(root.right , level - 1 ) populate_output(__magic_name__ , __magic_name__ ) return output def UpperCamelCase ( __magic_name__ : Node | None , __magic_name__ : int ) -> Sequence[Node | None]: """simple docstring""" lowercase__ = [] def populate_output(__magic_name__ : Node | None , __magic_name__ : int ) -> None: if root is None: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.right , level - 1 ) populate_output(root.left , level - 1 ) populate_output(__magic_name__ , __magic_name__ ) return output def UpperCamelCase ( __magic_name__ : Node | None ) -> Sequence[Node | None] | list[Any]: """simple docstring""" if root is None: return [] lowercase__ = [] lowercase__ = 0 lowercase__ = height(__magic_name__ ) for h in range(1 , height_tree + 1 ): if not flag: output.append(get_nodes_from_left_to_right(__magic_name__ , __magic_name__ ) ) lowercase__ = 1 else: output.append(get_nodes_from_right_to_left(__magic_name__ , __magic_name__ ) ) lowercase__ = 0 return output def UpperCamelCase ( ) -> None: # Main function for testing. """simple docstring""" lowercase__ = make_tree() print(f'''In-order Traversal: {inorder(__magic_name__ )}''' ) print(f'''Pre-order Traversal: {preorder(__magic_name__ )}''' ) print(f'''Post-order Traversal: {postorder(__magic_name__ )}''' , """\n""" ) print(f'''Height of Tree: {height(__magic_name__ )}''' , """\n""" ) print("""Complete Level Order Traversal: """ ) print(level_order(__magic_name__ ) , """\n""" ) print("""Level-wise order Traversal: """ ) for level in range(1 , height(__magic_name__ ) + 1 ): print(f'''Level {level}:''' , get_nodes_from_left_to_right(__magic_name__ , level=__magic_name__ ) ) print("""\nZigZag order Traversal: """ ) print(zigzag(__magic_name__ ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
15
import argparse from collections import defaultdict import yaml SCREAMING_SNAKE_CASE__ = '''docs/source/en/_toctree.yml''' def A ( __UpperCamelCase ) -> Optional[Any]: A__ = defaultdict(__UpperCamelCase ) for doc in model_doc: counts[doc["local"]] += 1 A__ = [key for key, value in counts.items() if value > 1] A__ = [] for duplicate_key in duplicates: A__ = list({doc['title'] for doc in model_doc if doc['local'] == duplicate_key} ) if len(__UpperCamelCase ) > 1: raise ValueError( f'''{duplicate_key} is present several times in the documentation table of content at ''' '`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the ' 'others.' ) # Only add this once new_doc.append({'local': duplicate_key, 'title': titles[0]} ) # Add none duplicate-keys new_doc.extend([doc for doc in model_doc if counts[doc['local']] == 1] ) # Sort return sorted(__UpperCamelCase , key=lambda __UpperCamelCase : s["title"].lower() ) def A ( __UpperCamelCase=False ) -> str: with open(__UpperCamelCase , encoding='utf-8' ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]['sections'] # Then to the model doc A__ = 0 while api_doc[model_idx]["title"] != "Models": model_idx += 1 A__ = api_doc[model_idx]['sections'] A__ = [(idx, section) for idx, section in enumerate(__UpperCamelCase ) if 'sections' in section] A__ = False for idx, modality_doc in modalities_docs: A__ = modality_doc['sections'] A__ = clean_model_doc_toc(__UpperCamelCase ) if old_modality_doc != new_modality_doc: A__ = True if overwrite: A__ = new_modality_doc if diff: if overwrite: A__ = model_doc A__ = api_doc with open(__UpperCamelCase , 'w' , encoding='utf-8' ) as f: f.write(yaml.dump(__UpperCamelCase , allow_unicode=__UpperCamelCase ) ) else: raise ValueError( 'The model doc part of the table of content is not properly sorted, run `make style` to fix this.' ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') SCREAMING_SNAKE_CASE__ = parser.parse_args() check_model_doc(args.fix_and_overwrite)
9
0
from numpy import exp, pi, sqrt def __a ( A__ : int , A__ : float = 0.0 , A__ : float = 1.0 ): return 1 / sqrt(2 * pi * sigma**2 ) * exp(-((x - mu) ** 2) / (2 * sigma**2) ) if __name__ == "__main__": import doctest doctest.testmod()
16
import inspect import unittest from transformers import SegformerConfig, is_torch_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_MAPPING, SegformerForImageClassification, SegformerForSemanticSegmentation, SegformerModel, ) from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import SegformerImageProcessor class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def _a ( self : List[str] ): """simple docstring""" A__ = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(_snake_case , 'hidden_sizes' ) ) self.parent.assertTrue(hasattr(_snake_case , 'num_attention_heads' ) ) self.parent.assertTrue(hasattr(_snake_case , 'num_encoder_blocks' ) ) class __lowerCAmelCase : """simple docstring""" def __init__( self : Any , _snake_case : str , _snake_case : Union[str, Any]=13 , _snake_case : Any=64 , _snake_case : Optional[Any]=3 , _snake_case : Dict=4 , _snake_case : Tuple=[2, 2, 2, 2] , _snake_case : str=[8, 4, 2, 1] , _snake_case : Union[str, Any]=[16, 32, 64, 1_28] , _snake_case : int=[1, 4, 8, 16] , _snake_case : List[str]=[1, 2, 4, 8] , _snake_case : int=True , _snake_case : int=True , _snake_case : Union[str, Any]="gelu" , _snake_case : Optional[int]=0.1 , _snake_case : Tuple=0.1 , _snake_case : Dict=0.02 , _snake_case : Tuple=3 , _snake_case : int=None , ): """simple docstring""" A__ = parent A__ = batch_size A__ = image_size A__ = num_channels A__ = num_encoder_blocks A__ = sr_ratios A__ = depths A__ = hidden_sizes A__ = downsampling_rates A__ = num_attention_heads A__ = is_training A__ = use_labels A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = initializer_range A__ = num_labels A__ = scope def _a ( self : int ): """simple docstring""" A__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) A__ = self.get_config() return config, pixel_values, labels def _a ( self : int ): """simple docstring""" return SegformerConfig( image_size=self.image_size , num_channels=self.num_channels , num_encoder_blocks=self.num_encoder_blocks , depths=self.depths , hidden_sizes=self.hidden_sizes , num_attention_heads=self.num_attention_heads , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , initializer_range=self.initializer_range , ) def _a ( self : int , _snake_case : Optional[Any] , _snake_case : int , _snake_case : Any ): """simple docstring""" A__ = SegformerModel(config=_snake_case ) model.to(_snake_case ) model.eval() A__ = model(_snake_case ) A__ = A__ = self.image_size // (self.downsampling_rates[-1] * 2) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width) ) def _a ( self : Union[str, Any] , _snake_case : Union[str, Any] , _snake_case : Tuple , _snake_case : Dict ): """simple docstring""" A__ = self.num_labels A__ = SegformerForSemanticSegmentation(_snake_case ) model.to(_snake_case ) model.eval() A__ = model(_snake_case ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4) ) A__ = model(_snake_case , labels=_snake_case ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4) ) self.parent.assertGreater(result.loss , 0.0 ) def _a ( self : List[str] , _snake_case : Optional[Any] , _snake_case : Union[str, Any] , _snake_case : List[str] ): """simple docstring""" A__ = 1 A__ = SegformerForSemanticSegmentation(config=_snake_case ) model.to(_snake_case ) model.eval() A__ = torch.randint(0 , 1 , (self.batch_size, self.image_size, self.image_size) ).to(_snake_case ) A__ = model(_snake_case , labels=_snake_case ) self.parent.assertGreater(result.loss , 0.0 ) def _a ( self : List[Any] ): """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ = config_and_inputs A__ = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class __lowerCAmelCase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Optional[int] = ( ( SegformerModel, SegformerForSemanticSegmentation, SegformerForImageClassification, ) if is_torch_available() else () ) A__ : Union[str, Any] = ( { "feature-extraction": SegformerModel, "image-classification": SegformerForImageClassification, "image-segmentation": SegformerForSemanticSegmentation, } if is_torch_available() else {} ) A__ : Optional[Any] = True A__ : str = False A__ : Tuple = False A__ : Dict = False def _a ( self : Union[str, Any] ): """simple docstring""" A__ = SegformerModelTester(self ) A__ = SegformerConfigTester(self , config_class=_snake_case ) def _a ( self : Optional[int] ): """simple docstring""" self.config_tester.run_common_tests() def _a ( self : Optional[Any] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def _a ( self : List[Any] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_binary_image_segmentation(*_snake_case ) def _a ( self : Tuple ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_segmentation(*_snake_case ) @unittest.skip('SegFormer does not use inputs_embeds' ) def _a ( self : List[Any] ): """simple docstring""" pass @unittest.skip('SegFormer does not have get_input_embeddings method and get_output_embeddings methods' ) def _a ( self : Dict ): """simple docstring""" pass def _a ( self : Dict ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(_snake_case ) A__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ = [*signature.parameters.keys()] A__ = ['pixel_values'] self.assertListEqual(arg_names[:1] , _snake_case ) def _a ( self : Dict ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = True for model_class in self.all_model_classes: A__ = True A__ = False A__ = True A__ = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) ) A__ = outputs.attentions A__ = sum(self.model_tester.depths ) self.assertEqual(len(_snake_case ) , _snake_case ) # check that output_attentions also work using config del inputs_dict["output_attentions"] A__ = True A__ = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) ) A__ = outputs.attentions self.assertEqual(len(_snake_case ) , _snake_case ) # verify the first attentions (first block, first layer) A__ = (self.model_tester.image_size // 4) ** 2 A__ = (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2 self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len] , ) # verify the last attentions (last block, last layer) A__ = (self.model_tester.image_size // 32) ** 2 A__ = (self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2 self.assertListEqual( list(attentions[-1].shape[-3:] ) , [self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len] , ) A__ = len(_snake_case ) # Check attention is always last and order is fine A__ = True A__ = True A__ = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) ) self.assertEqual(out_len + 1 , len(_snake_case ) ) A__ = outputs.attentions self.assertEqual(len(_snake_case ) , _snake_case ) # verify the first attentions (first block, first layer) A__ = (self.model_tester.image_size // 4) ** 2 A__ = (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2 self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len] , ) def _a ( self : Union[str, Any] ): """simple docstring""" def check_hidden_states_output(_snake_case : Dict , _snake_case : int , _snake_case : List[Any] ): A__ = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(_snake_case , _snake_case ) ) A__ = outputs.hidden_states A__ = self.model_tester.num_encoder_blocks self.assertEqual(len(_snake_case ) , _snake_case ) # verify the first hidden states (first block) self.assertListEqual( list(hidden_states[0].shape[-3:] ) , [ self.model_tester.hidden_sizes[0], self.model_tester.image_size // 4, self.model_tester.image_size // 4, ] , ) A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = True check_hidden_states_output(_snake_case , _snake_case , _snake_case ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A__ = True check_hidden_states_output(_snake_case , _snake_case , _snake_case ) def _a ( self : Tuple ): """simple docstring""" if not self.model_tester.is_training: return A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = True for model_class in self.all_model_classes: if model_class in get_values(_snake_case ): continue A__ = model_class(_snake_case ) model.to(_snake_case ) model.train() A__ = self._prepare_for_class(_snake_case , _snake_case , return_labels=_snake_case ) A__ = model(**_snake_case ).loss loss.backward() @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.' ) def _a ( self : Optional[Any] ): """simple docstring""" pass @slow def _a ( self : Tuple ): """simple docstring""" for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = SegformerModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def A ( ) -> str: A__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @slow def _a ( self : Dict ): """simple docstring""" A__ = SegformerImageProcessor( image_scale=(5_12, 5_12) , keep_ratio=_snake_case , align=_snake_case , do_random_crop=_snake_case ) A__ = SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512' ).to( _snake_case ) A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ) A__ = encoded_inputs.pixel_values.to(_snake_case ) with torch.no_grad(): A__ = model(_snake_case ) A__ = torch.Size((1, model.config.num_labels, 1_28, 1_28) ) self.assertEqual(outputs.logits.shape , _snake_case ) A__ = torch.tensor( [ [[-4.6310, -5.5232, -6.2356], [-5.1921, -6.1444, -6.5996], [-5.4424, -6.2790, -6.7574]], [[-12.1391, -13.3122, -13.9554], [-12.8732, -13.9352, -14.3563], [-12.9438, -13.8226, -14.2513]], [[-12.5134, -13.4686, -14.4915], [-12.8669, -14.4343, -14.7758], [-13.2523, -14.5819, -15.0694]], ] ).to(_snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3] , _snake_case , atol=1E-4 ) ) @slow def _a ( self : Optional[Any] ): """simple docstring""" A__ = SegformerImageProcessor( image_scale=(5_12, 5_12) , keep_ratio=_snake_case , align=_snake_case , do_random_crop=_snake_case ) A__ = SegformerForSemanticSegmentation.from_pretrained( 'nvidia/segformer-b1-finetuned-cityscapes-1024-1024' ).to(_snake_case ) A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ) A__ = encoded_inputs.pixel_values.to(_snake_case ) with torch.no_grad(): A__ = model(_snake_case ) A__ = torch.Size((1, model.config.num_labels, 1_28, 1_28) ) self.assertEqual(outputs.logits.shape , _snake_case ) A__ = torch.tensor( [ [[-13.5748, -13.9111, -12.6500], [-14.3500, -15.3683, -14.2328], [-14.7532, -16.0424, -15.6087]], [[-17.1651, -15.8725, -12.9653], [-17.2580, -17.3718, -14.8223], [-16.6058, -16.8783, -16.7452]], [[-3.6456, -3.0209, -1.4203], [-3.0797, -3.1959, -2.0000], [-1.8757, -1.9217, -1.6997]], ] ).to(_snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3] , _snake_case , atol=1E-1 ) ) @slow def _a ( self : Any ): """simple docstring""" A__ = SegformerImageProcessor( image_scale=(5_12, 5_12) , keep_ratio=_snake_case , align=_snake_case , do_random_crop=_snake_case ) A__ = SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512' ).to( _snake_case ) A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ) A__ = encoded_inputs.pixel_values.to(_snake_case ) with torch.no_grad(): A__ = model(_snake_case ) A__ = outputs.logits.detach().cpu() A__ = image_processor.post_process_semantic_segmentation(outputs=_snake_case , target_sizes=[(5_00, 3_00)] ) A__ = torch.Size((5_00, 3_00) ) self.assertEqual(segmentation[0].shape , _snake_case ) A__ = image_processor.post_process_semantic_segmentation(outputs=_snake_case ) A__ = torch.Size((1_28, 1_28) ) self.assertEqual(segmentation[0].shape , _snake_case )
9
0
import operator as op UpperCAmelCase_ : Union[str, Any] = '''scaler.pt''' UpperCAmelCase_ : int = '''pytorch_model''' UpperCAmelCase_ : Optional[Any] = '''random_states''' UpperCAmelCase_ : Dict = '''optimizer''' UpperCAmelCase_ : Dict = '''scheduler''' UpperCAmelCase_ : int = '''pytorch_model.bin''' UpperCAmelCase_ : List[Any] = '''pytorch_model.bin.index.json''' UpperCAmelCase_ : Optional[Any] = '''model.safetensors''' UpperCAmelCase_ : str = '''model.safetensors.index.json''' UpperCAmelCase_ : Tuple = '''1.10.2''' UpperCAmelCase_ : str = '''py38''' UpperCAmelCase_ : List[str] = '''4.17.0''' UpperCAmelCase_ : Any = ['''ml.p3.16xlarge''', '''ml.p3dn.24xlarge''', '''ml.p4dn.24xlarge'''] UpperCAmelCase_ : List[Any] = ['''FULL_SHARD''', '''SHARD_GRAD_OP''', '''NO_SHARD''', '''HYBRID_SHARD''', '''HYBRID_SHARD_ZERO2'''] UpperCAmelCase_ : Optional[int] = ['''TRANSFORMER_BASED_WRAP''', '''SIZE_BASED_WRAP''', '''NO_WRAP'''] UpperCAmelCase_ : Tuple = ['''BACKWARD_PRE''', '''BACKWARD_POST''', '''NO_PREFETCH'''] UpperCAmelCase_ : Union[str, Any] = ['''FULL_STATE_DICT''', '''LOCAL_STATE_DICT''', '''SHARDED_STATE_DICT'''] UpperCAmelCase_ : int = '''2.0.1''' UpperCAmelCase_ : List[Any] = ['''pdsh''', '''standard''', '''openmpi''', '''mvapich'''] UpperCAmelCase_ : List[Any] = ['''default''', '''reduce-overhead''', '''max-autotune'''] UpperCAmelCase_ : Dict = {'''>''': op.gt, '''>=''': op.ge, '''==''': op.eq, '''!=''': op.ne, '''<=''': op.le, '''<''': op.lt} # These are the args for `torch.distributed.launch` for pytorch < 1.9 UpperCAmelCase_ : Tuple = [ '''nnodes''', '''nproc_per_node''', '''rdzv_backend''', '''rdzv_endpoint''', '''rdzv_id''', '''rdzv_conf''', '''standalone''', '''max_restarts''', '''monitor_interval''', '''start_method''', '''role''', '''module''', '''m''', '''no_python''', '''run_path''', '''log_dir''', '''r''', '''redirects''', '''t''', '''tee''', '''node_rank''', '''master_addr''', '''master_port''', ] UpperCAmelCase_ : Tuple = ['''DEEPSPEED''', '''MULTI_GPU''', '''FSDP''', '''MEGATRON_LM'''] UpperCAmelCase_ : Any = ['''DEEPSPEED''', '''MULTI_XPU''', '''FSDP''']
17
import logging from pathlib import Path import numpy as np import pytorch_lightning as pl import torch from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint from pytorch_lightning.utilities import rank_zero_only from utils_rag import save_json def A ( __UpperCamelCase ) -> Optional[int]: A__ = filter(lambda __UpperCamelCase : p.requires_grad , model.parameters() ) A__ = sum([np.prod(p.size() ) for p in model_parameters] ) return params SCREAMING_SNAKE_CASE__ = logging.getLogger(__name__) def A ( __UpperCamelCase , __UpperCamelCase ) -> Dict: if metric == "rouge2": A__ = '{val_avg_rouge2:.4f}-{step_count}' elif metric == "bleu": A__ = '{val_avg_bleu:.4f}-{step_count}' elif metric == "em": A__ = '{val_avg_em:.4f}-{step_count}' elif metric == "loss": A__ = '{val_avg_loss:.4f}-{step_count}' else: raise NotImplementedError( f'''seq2seq callbacks only support rouge2 and bleu, got {metric}, You can make your own by adding to this''' ' function.' ) A__ = ModelCheckpoint( dirpath=__UpperCamelCase , filename=__UpperCamelCase , monitor=f'''val_{metric}''' , mode='max' , save_top_k=1 , every_n_epochs=1 , ) return checkpoint_callback def A ( __UpperCamelCase , __UpperCamelCase ) -> Any: return EarlyStopping( monitor=f'''val_{metric}''' , mode='min' if 'loss' in metric else 'max' , patience=__UpperCamelCase , verbose=__UpperCamelCase , ) class __lowerCAmelCase ( pl.Callback ): """simple docstring""" def _a ( self : Dict , _snake_case : Union[str, Any] , _snake_case : str ): """simple docstring""" A__ = {F'''lr_group_{i}''': param['lr'] for i, param in enumerate(pl_module.trainer.optimizers[0].param_groups )} pl_module.logger.log_metrics(_snake_case ) @rank_zero_only def _a ( self : Union[str, Any] , _snake_case : pl.Trainer , _snake_case : pl.LightningModule , _snake_case : str , _snake_case : Optional[Any]=True ): """simple docstring""" logger.info(F'''***** {type_path} results at step {trainer.global_step:05d} *****''' ) A__ = trainer.callback_metrics trainer.logger.log_metrics({k: v for k, v in metrics.items() if k not in ['log', 'progress_bar', 'preds']} ) # Log results A__ = Path(pl_module.hparams.output_dir ) if type_path == "test": A__ = od / 'test_results.txt' A__ = od / 'test_generations.txt' else: # this never gets hit. I prefer not to save intermediate generations, and results are in metrics.json # If people want this it will be easy enough to add back. A__ = od / F'''{type_path}_results/{trainer.global_step:05d}.txt''' A__ = od / F'''{type_path}_generations/{trainer.global_step:05d}.txt''' results_file.parent.mkdir(exist_ok=_snake_case ) generations_file.parent.mkdir(exist_ok=_snake_case ) with open(_snake_case , 'a+' ) as writer: for key in sorted(_snake_case ): if key in ["log", "progress_bar", "preds"]: continue A__ = metrics[key] if isinstance(_snake_case , torch.Tensor ): A__ = val.item() A__ = F'''{key}: {val:.6f}\n''' writer.write(_snake_case ) if not save_generations: return if "preds" in metrics: A__ = '\n'.join(metrics['preds'] ) generations_file.open('w+' ).write(_snake_case ) @rank_zero_only def _a ( self : Dict , _snake_case : List[str] , _snake_case : List[Any] ): """simple docstring""" try: A__ = pl_module.model.model.num_parameters() except AttributeError: A__ = pl_module.model.num_parameters() A__ = count_trainable_parameters(_snake_case ) # mp stands for million parameters trainer.logger.log_metrics({'n_params': npars, 'mp': npars / 1E6, 'grad_mp': n_trainable_pars / 1E6} ) @rank_zero_only def _a ( self : int , _snake_case : pl.Trainer , _snake_case : pl.LightningModule ): """simple docstring""" save_json(pl_module.metrics , pl_module.metrics_save_path ) return self._write_logs(_snake_case , _snake_case , 'test' ) @rank_zero_only def _a ( self : Optional[Any] , _snake_case : pl.Trainer , _snake_case : List[Any] ): """simple docstring""" save_json(pl_module.metrics , pl_module.metrics_save_path ) # Uncommenting this will save val generations # return self._write_logs(trainer, pl_module, "valid")
9
0
'''simple docstring''' def __a(SCREAMING_SNAKE_CASE_ : list , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int = 0 , SCREAMING_SNAKE_CASE_ : int = 0 ): '''simple docstring''' _lowerCAmelCase = right or len(SCREAMING_SNAKE_CASE_ ) - 1 if left > right: return -1 elif list_data[left] == key: return left elif list_data[right] == key: return right else: return search(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , left + 1 , right - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
18
import warnings from typing import Any, Dict, List, Optional, Union import numpy as np from ...audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Optional[Any] = ["input_values", "attention_mask"] def __init__( self : str , _snake_case : int = 1 , _snake_case : int = 1_60_00 , _snake_case : float = 0.0 , _snake_case : bool = False , _snake_case : int = 80 , _snake_case : int = 16 , _snake_case : int = 64 , _snake_case : str = "hann_window" , _snake_case : float = 1.0 , _snake_case : float = 80 , _snake_case : float = 76_00 , _snake_case : float = 1E-10 , _snake_case : int = 2 , _snake_case : bool = True , **_snake_case : Union[str, Any] , ): """simple docstring""" super().__init__(feature_size=_snake_case , sampling_rate=_snake_case , padding_value=_snake_case , **_snake_case ) A__ = do_normalize A__ = return_attention_mask A__ = num_mel_bins A__ = hop_length A__ = win_length A__ = win_function A__ = frame_signal_scale A__ = fmin A__ = fmax A__ = mel_floor A__ = reduction_factor A__ = win_length * sampling_rate // 10_00 A__ = hop_length * sampling_rate // 10_00 A__ = optimal_fft_length(self.sample_size ) A__ = (self.n_fft // 2) + 1 A__ = window_function(window_length=self.sample_size , name=self.win_function , periodic=_snake_case ) A__ = mel_filter_bank( num_frequency_bins=self.n_freqs , num_mel_filters=self.num_mel_bins , min_frequency=self.fmin , max_frequency=self.fmax , sampling_rate=self.sampling_rate , norm='slaney' , mel_scale='slaney' , ) if frame_signal_scale != 1.0: warnings.warn( 'The argument `frame_signal_scale` is deprecated and will be removed in version 4.30.0 of Transformers' , _snake_case , ) if reduction_factor != 2.0: warnings.warn( 'The argument `reduction_factor` is deprecated and will be removed in version 4.30.0 of Transformers' , _snake_case , ) @staticmethod # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm def _a ( _snake_case : List[np.ndarray] , _snake_case : List[np.ndarray] , _snake_case : float = 0.0 ): """simple docstring""" if attention_mask is not None: A__ = np.array(_snake_case , np.intaa ) A__ = [] for vector, length in zip(_snake_case , attention_mask.sum(-1 ) ): A__ = (vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1E-7 ) if length < normed_slice.shape[0]: A__ = padding_value normed_input_values.append(_snake_case ) else: A__ = [(x - x.mean()) / np.sqrt(x.var() + 1E-7 ) for x in input_values] return normed_input_values def _a ( self : Tuple , _snake_case : np.ndarray , ): """simple docstring""" A__ = spectrogram( _snake_case , window=self.window , frame_length=self.sample_size , hop_length=self.sample_stride , fft_length=self.n_fft , mel_filters=self.mel_filters , mel_floor=self.mel_floor , log_mel='log10' , ) return log_mel_spec.T def __call__( self : List[str] , _snake_case : Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None , _snake_case : Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None , _snake_case : Union[bool, str, PaddingStrategy] = False , _snake_case : Optional[int] = None , _snake_case : bool = False , _snake_case : Optional[int] = None , _snake_case : Optional[bool] = None , _snake_case : Optional[Union[str, TensorType]] = None , _snake_case : Optional[int] = None , **_snake_case : Tuple , ): """simple docstring""" if audio is None and audio_target is None: raise ValueError('You must provide either `audio` or `audio_target` values.' ) if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' F''' {self.sampling_rate}. Please make sure that the provided audio input was sampled with''' F''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( 'It is strongly recommended to pass the ``sampling_rate`` argument to this function. ' 'Failing to do so can result in silent errors that might be hard to debug.' ) if audio is not None: A__ = self._process_audio( _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , **_snake_case , ) else: A__ = None if audio_target is not None: A__ = self._process_audio( _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , **_snake_case , ) if inputs is None: return inputs_target else: A__ = inputs_target['input_values'] A__ = inputs_target.get('attention_mask' ) if decoder_attention_mask is not None: A__ = decoder_attention_mask return inputs def _a ( self : Tuple , _snake_case : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , _snake_case : bool = False , _snake_case : Union[bool, str, PaddingStrategy] = False , _snake_case : Optional[int] = None , _snake_case : bool = False , _snake_case : Optional[int] = None , _snake_case : Optional[bool] = None , _snake_case : Optional[Union[str, TensorType]] = None , **_snake_case : Tuple , ): """simple docstring""" A__ = isinstance(_snake_case , np.ndarray ) and len(speech.shape ) > 1 if is_batched_numpy and len(speech.shape ) > 2: raise ValueError(F'''Only mono-channel audio is supported for input to {self}''' ) A__ = is_batched_numpy or ( isinstance(_snake_case , (list, tuple) ) and (isinstance(speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: A__ = [np.asarray(_snake_case , dtype=np.floataa ) for speech in speech] elif not is_batched and not isinstance(_snake_case , np.ndarray ): A__ = np.asarray(_snake_case , dtype=np.floataa ) elif isinstance(_snake_case , np.ndarray ) and speech.dtype is np.dtype(np.floataa ): A__ = speech.astype(np.floataa ) # always return batch if not is_batched: A__ = [speech] # needed to make pad() work on spectrogram inputs A__ = self.feature_size # convert into correct format for padding if is_target: A__ = [self._extract_mel_features(_snake_case ) for waveform in speech] A__ = BatchFeature({'input_values': features} ) A__ = self.num_mel_bins else: A__ = BatchFeature({'input_values': speech} ) A__ = self.pad( _snake_case , padding=_snake_case , max_length=_snake_case , truncation=_snake_case , pad_to_multiple_of=_snake_case , return_attention_mask=_snake_case , **_snake_case , ) A__ = feature_size_hack # convert input values to correct format A__ = padded_inputs['input_values'] if not isinstance(input_values[0] , np.ndarray ): A__ = [np.asarray(_snake_case , dtype=np.floataa ) for array in input_values] elif ( not isinstance(_snake_case , np.ndarray ) and isinstance(input_values[0] , np.ndarray ) and input_values[0].dtype is np.dtype(np.floataa ) ): A__ = [array.astype(np.floataa ) for array in input_values] elif isinstance(_snake_case , np.ndarray ) and input_values.dtype is np.dtype(np.floataa ): A__ = input_values.astype(np.floataa ) # convert attention_mask to correct format A__ = padded_inputs.get('attention_mask' ) if attention_mask is not None: A__ = [np.asarray(_snake_case , dtype=np.intaa ) for array in attention_mask] # zero-mean and unit-variance normalization if not is_target and self.do_normalize: A__ = ( attention_mask if self._get_padding_strategies(_snake_case , max_length=_snake_case ) is not PaddingStrategy.DO_NOT_PAD else None ) A__ = self.zero_mean_unit_var_norm( padded_inputs['input_values'] , attention_mask=_snake_case , padding_value=self.padding_value ) if return_tensors is not None: A__ = padded_inputs.convert_to_tensors(_snake_case ) return padded_inputs def _a ( self : Optional[Any] ): """simple docstring""" A__ = super().to_dict() # Don't serialize these as they are derived from the other properties. A__ = ['window', 'mel_filters', 'sample_size', 'sample_stride', 'n_fft', 'n_freqs'] for name in names: if name in output: del output[name] return output
9
0
"""simple docstring""" from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, ChannelDimension, ImageInput, PILImageResampling, is_batched, to_numpy_array, valid_images, ) from ...utils import TensorType, logging _a = logging.get_logger(__name__) class _UpperCAmelCase( lowerCamelCase ): lowercase__ = ['pixel_values'] def __init__( self , __a = True , __a = None , __a = PILImageResampling.BICUBIC , __a = True , __a = True , __a = 1 / 2_55 , __a = None , __a = True , __a = None , __a = None , **__a , ) -> None: '''simple docstring''' super().__init__(**__a) _UpperCamelCase = size if size is not None else {'''height''': 2_24, '''width''': 2_24} _UpperCamelCase = get_size_dict(__a) _UpperCamelCase = crop_size if crop_size is not None else {'''height''': 2_24, '''width''': 2_24} _UpperCamelCase = get_size_dict(__a , default_to_square=__a , param_name='''crop_size''') _UpperCamelCase = do_resize _UpperCamelCase = do_rescale _UpperCamelCase = do_normalize _UpperCamelCase = do_center_crop _UpperCamelCase = crop_size _UpperCamelCase = size _UpperCamelCase = resample _UpperCamelCase = rescale_factor _UpperCamelCase = image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN _UpperCamelCase = image_std if image_std is not None else IMAGENET_DEFAULT_STD def UpperCAmelCase ( self , __a , __a , __a = PILImageResampling.BILINEAR , __a = None , **__a , ) -> np.ndarray: '''simple docstring''' _UpperCamelCase = get_size_dict(__a) if "shortest_edge" in size: _UpperCamelCase = get_resize_output_image_size(__a , size=size['''shortest_edge'''] , default_to_square=__a) # size = get_resize_output_image_size(image, size["shortest_edge"], size["longest_edge"]) elif "height" in size and "width" in size: _UpperCamelCase = (size['''height'''], size['''width''']) else: raise ValueError(F'''Size must contain \'height\' and \'width\' keys or \'shortest_edge\' key. Got {size.keys()}''') return resize(__a , size=__a , resample=__a , data_format=__a , **__a) def UpperCAmelCase ( self , __a , __a , __a = None , **__a , ) -> np.ndarray: '''simple docstring''' _UpperCamelCase = get_size_dict(__a) if "height" not in size or "width" not in size: raise ValueError(F'''The `size` parameter must contain the keys (height, width). Got {size.keys()}''') return center_crop(__a , size=(size['''height'''], size['''width''']) , data_format=__a , **__a) def UpperCAmelCase ( self , __a , __a , __a = None , **__a) -> np.ndarray: '''simple docstring''' return rescale(__a , scale=__a , data_format=__a , **__a) def UpperCAmelCase ( self , __a , __a , __a , __a = None , **__a , ) -> np.ndarray: '''simple docstring''' return normalize(__a , mean=__a , std=__a , data_format=__a , **__a) def UpperCAmelCase ( self , __a , __a = None , __a = None , __a = None , __a = None , __a = None , __a = None , __a = None , __a = None , __a = None , __a = None , __a = None , __a = ChannelDimension.FIRST , **__a , ) -> BatchFeature: '''simple docstring''' _UpperCamelCase = do_resize if do_resize is not None else self.do_resize _UpperCamelCase = do_rescale if do_rescale is not None else self.do_rescale _UpperCamelCase = do_normalize if do_normalize is not None else self.do_normalize _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(__a , param_name='''crop_size''' , default_to_square=__a) _UpperCamelCase = resample if resample is not None else self.resample _UpperCamelCase = rescale_factor if rescale_factor is not None else self.rescale_factor _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(__a) if not is_batched(__a): _UpperCamelCase = [images] if not valid_images(__a): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''') if do_resize and size is None: 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.''') # All transformations expect numpy arrays. _UpperCamelCase = [to_numpy_array(__a) for image in images] if do_resize: _UpperCamelCase = [self.resize(image=__a , size=__a , resample=__a) for image in images] if do_center_crop: _UpperCamelCase = [self.center_crop(image=__a , size=__a) for image in images] if do_rescale: _UpperCamelCase = [self.rescale(image=__a , scale=__a) for image in images] if do_normalize: _UpperCamelCase = [self.normalize(image=__a , mean=__a , std=__a) for image in images] _UpperCamelCase = [to_channel_dimension_format(__a , __a) for image in images] _UpperCamelCase = {'''pixel_values''': images} return BatchFeature(data=__a , tensor_type=__a)
19
import argparse import OmegaConf import torch from diffusers import DDIMScheduler, LDMPipeline, UNetLDMModel, VQModel def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: A__ = OmegaConf.load(__UpperCamelCase ) A__ = torch.load(__UpperCamelCase , map_location='cpu' )['model'] A__ = list(state_dict.keys() ) # extract state_dict for VQVAE A__ = {} A__ = 'first_stage_model.' for key in keys: if key.startswith(__UpperCamelCase ): A__ = state_dict[key] # extract state_dict for UNetLDM A__ = {} A__ = 'model.diffusion_model.' for key in keys: if key.startswith(__UpperCamelCase ): A__ = state_dict[key] A__ = config.model.params.first_stage_config.params A__ = config.model.params.unet_config.params A__ = VQModel(**__UpperCamelCase ).eval() vqvae.load_state_dict(__UpperCamelCase ) A__ = UNetLDMModel(**__UpperCamelCase ).eval() unet.load_state_dict(__UpperCamelCase ) A__ = DDIMScheduler( timesteps=config.model.params.timesteps , beta_schedule='scaled_linear' , beta_start=config.model.params.linear_start , beta_end=config.model.params.linear_end , clip_sample=__UpperCamelCase , ) A__ = LDMPipeline(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) pipeline.save_pretrained(__UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument('''--checkpoint_path''', type=str, required=True) parser.add_argument('''--config_path''', type=str, required=True) parser.add_argument('''--output_path''', type=str, required=True) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_ldm_original(args.checkpoint_path, args.config_path, args.output_path)
9
0
_lowerCAmelCase: Union[str, Any] = [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] _lowerCAmelCase: int = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] _lowerCAmelCase: Any = { 0: 'Sunday', 1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday', } def _lowercase( __a : int , __a : int , __a : int ): assert len(str(__a ) ) > 2, "year should be in YYYY format" assert 1 <= month <= 12, "month should be between 1 to 12" assert 1 <= day <= 31, "day should be between 1 to 31" # Doomsday algorithm: a__ =year // 100 a__ =(5 * (century % 4) + 2) % 7 a__ =year % 100 a__ =centurian % 12 a__ =( (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor ) % 7 a__ =( DOOMSDAY_NOT_LEAP[month - 1] if (year % 4 != 0) or (centurian == 0 and (year % 400) == 0) else DOOMSDAY_LEAP[month - 1] ) a__ =(dooms_day + day - day_anchor) % 7 return WEEK_DAY_NAMES[week_day] if __name__ == "__main__": import doctest doctest.testmod()
20
import json import os import torch from diffusers import UNetaDModel os.makedirs('''hub/hopper-medium-v2/unet/hor32''', exist_ok=True) os.makedirs('''hub/hopper-medium-v2/unet/hor128''', exist_ok=True) os.makedirs('''hub/hopper-medium-v2/value_function''', exist_ok=True) def A ( __UpperCamelCase ) -> Union[str, Any]: if hor == 128: A__ = ('DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D') A__ = (32, 128, 256) A__ = ('UpResnetBlock1D', 'UpResnetBlock1D') elif hor == 32: A__ = ('DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D') A__ = (32, 64, 128, 256) A__ = ('UpResnetBlock1D', 'UpResnetBlock1D', 'UpResnetBlock1D') A__ = torch.load(f'''/Users/bglickenhaus/Documents/diffuser/temporal_unet-hopper-mediumv2-hor{hor}.torch''' ) A__ = model.state_dict() A__ = { 'down_block_types': down_block_types, 'block_out_channels': block_out_channels, 'up_block_types': up_block_types, 'layers_per_block': 1, 'use_timestep_embedding': True, 'out_block_type': 'OutConv1DBlock', 'norm_num_groups': 8, 'downsample_each_block': False, 'in_channels': 14, 'out_channels': 14, 'extra_in_channels': 0, 'time_embedding_type': 'positional', 'flip_sin_to_cos': False, 'freq_shift': 1, 'sample_size': 65_536, 'mid_block_type': 'MidResTemporalBlock1D', 'act_fn': 'mish', } A__ = UNetaDModel(**__UpperCamelCase ) print(f'''length of state dict: {len(state_dict.keys() )}''' ) print(f'''length of value function dict: {len(hf_value_function.state_dict().keys() )}''' ) A__ = dict(zip(model.state_dict().keys() , hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): A__ = state_dict.pop(__UpperCamelCase ) hf_value_function.load_state_dict(__UpperCamelCase ) torch.save(hf_value_function.state_dict() , f'''hub/hopper-medium-v2/unet/hor{hor}/diffusion_pytorch_model.bin''' ) with open(f'''hub/hopper-medium-v2/unet/hor{hor}/config.json''' , 'w' ) as f: json.dump(__UpperCamelCase , __UpperCamelCase ) def A ( ) -> List[str]: A__ = { 'in_channels': 14, 'down_block_types': ('DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D'), 'up_block_types': (), 'out_block_type': 'ValueFunction', 'mid_block_type': 'ValueFunctionMidBlock1D', 'block_out_channels': (32, 64, 128, 256), 'layers_per_block': 1, 'downsample_each_block': True, 'sample_size': 65_536, 'out_channels': 14, 'extra_in_channels': 0, 'time_embedding_type': 'positional', 'use_timestep_embedding': True, 'flip_sin_to_cos': False, 'freq_shift': 1, 'norm_num_groups': 8, 'act_fn': 'mish', } A__ = torch.load('/Users/bglickenhaus/Documents/diffuser/value_function-hopper-mediumv2-hor32.torch' ) A__ = model A__ = UNetaDModel(**__UpperCamelCase ) print(f'''length of state dict: {len(state_dict.keys() )}''' ) print(f'''length of value function dict: {len(hf_value_function.state_dict().keys() )}''' ) A__ = dict(zip(state_dict.keys() , hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): A__ = state_dict.pop(__UpperCamelCase ) hf_value_function.load_state_dict(__UpperCamelCase ) torch.save(hf_value_function.state_dict() , 'hub/hopper-medium-v2/value_function/diffusion_pytorch_model.bin' ) with open('hub/hopper-medium-v2/value_function/config.json' , 'w' ) as f: json.dump(__UpperCamelCase , __UpperCamelCase ) if __name__ == "__main__": unet(3_2) # unet(128) value_function()
9
0