code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available UpperCamelCase : Tuple = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Any = ["""GPTSw3Tokenizer"""] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_gpt_swa import GPTSwaTokenizer else: import sys UpperCamelCase : Optional[int] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
345
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, is_vision_available, ) UpperCamelCase : List[str] = {"""processing_layoutxlm""": ["""LayoutXLMProcessor"""]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Any = ["""LayoutXLMTokenizer"""] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Optional[int] = ["""LayoutXLMTokenizerFast"""] if TYPE_CHECKING: from .processing_layoutxlm import LayoutXLMProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutxlm import LayoutXLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutxlm_fast import LayoutXLMTokenizerFast else: import sys UpperCamelCase : Dict = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
345
1
'''simple docstring''' import warnings from ...utils import logging from .image_processing_segformer import SegformerImageProcessor UpperCamelCase : List[Any] = logging.get_logger(__name__) class UpperCamelCase ( a_ ): """simple docstring""" def __init__( self : Any , *UpperCAmelCase_ : List[Any] , **UpperCAmelCase_ : List[Any]): """simple docstring""" warnings.warn( 'The class SegformerFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use SegformerImageProcessor instead.' , UpperCAmelCase_ , ) super().__init__(*UpperCAmelCase_ , **UpperCAmelCase_)
345
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCamelCase : Tuple = { """configuration_pegasus_x""": ["""PEGASUS_X_PRETRAINED_CONFIG_ARCHIVE_MAP""", """PegasusXConfig"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Union[str, Any] = [ """PEGASUS_X_PRETRAINED_MODEL_ARCHIVE_LIST""", """PegasusXForConditionalGeneration""", """PegasusXModel""", """PegasusXPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_pegasus_x import PEGASUS_X_PRETRAINED_CONFIG_ARCHIVE_MAP, PegasusXConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_pegasus_x import ( PEGASUS_X_PRETRAINED_MODEL_ARCHIVE_LIST, PegasusXForConditionalGeneration, PegasusXModel, PegasusXPreTrainedModel, ) else: import sys UpperCamelCase : List[str] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
345
1
'''simple docstring''' def SCREAMING_SNAKE_CASE__ ( snake_case : list , snake_case : list , snake_case : int ) -> list: """simple docstring""" a : Optional[int] = len(snake_case ) a : str = [[0] * n for i in range(snake_case )] for i in range(snake_case ): a : Dict = y_points[i] for i in range(2 , snake_case ): for j in range(snake_case , snake_case ): a : Any = ( (xa - x_points[j - i + 1]) * q[j][i - 1] - (xa - x_points[j]) * q[j - 1][i - 1] ) / (x_points[j] - x_points[j - i + 1]) return [q[n - 1][n - 1], q] if __name__ == "__main__": import doctest doctest.testmod()
345
'''simple docstring''' import math from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase : str = logging.get_logger(__name__) UpperCamelCase : List[str] = { """facebook/data2vec-base-960h""": """https://huggingface.co/facebook/data2vec-audio-base-960h/resolve/main/config.json""", # See all Data2VecAudio models at https://huggingface.co/models?filter=data2vec-audio } class UpperCamelCase ( a_ ): """simple docstring""" A : Optional[int] = "data2vec-audio" def __init__( self : Dict , UpperCAmelCase_ : Optional[int]=3_2 , UpperCAmelCase_ : Union[str, Any]=7_6_8 , UpperCAmelCase_ : Dict=1_2 , UpperCAmelCase_ : str=1_2 , UpperCAmelCase_ : Optional[Any]=3_0_7_2 , UpperCAmelCase_ : Dict="gelu" , UpperCAmelCase_ : Union[str, Any]=0.1 , UpperCAmelCase_ : Optional[int]=0.1 , UpperCAmelCase_ : int=0.1 , UpperCAmelCase_ : Union[str, Any]=0.0 , UpperCAmelCase_ : Union[str, Any]=0.1 , UpperCAmelCase_ : Dict=0.1 , UpperCAmelCase_ : Optional[Any]=0.02 , UpperCAmelCase_ : str=1e-5 , UpperCAmelCase_ : List[Any]="gelu" , UpperCAmelCase_ : Optional[Any]=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2) , UpperCAmelCase_ : Dict=(5, 2, 2, 2, 2, 2, 2) , UpperCAmelCase_ : int=(1_0, 3, 3, 3, 3, 2, 2) , UpperCAmelCase_ : List[Any]=False , UpperCAmelCase_ : List[Any]=1_6 , UpperCAmelCase_ : Optional[Any]=1_9 , UpperCAmelCase_ : int=5 , UpperCAmelCase_ : Any=0.05 , UpperCAmelCase_ : Dict=1_0 , UpperCAmelCase_ : Optional[Any]=2 , UpperCAmelCase_ : Optional[int]=0.0 , UpperCAmelCase_ : Tuple=1_0 , UpperCAmelCase_ : int=0 , UpperCAmelCase_ : Any="sum" , UpperCAmelCase_ : Optional[int]=False , UpperCAmelCase_ : Dict=False , UpperCAmelCase_ : Optional[int]=2_5_6 , UpperCAmelCase_ : Any=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0) , UpperCAmelCase_ : Optional[Any]=(5, 3, 3, 1, 1) , UpperCAmelCase_ : Optional[int]=(1, 2, 3, 1, 1) , UpperCAmelCase_ : int=5_1_2 , UpperCAmelCase_ : str=0 , UpperCAmelCase_ : Tuple=1 , UpperCAmelCase_ : Union[str, Any]=2 , UpperCAmelCase_ : List[str]=False , UpperCAmelCase_ : Any=3 , UpperCAmelCase_ : Dict=2 , UpperCAmelCase_ : int=3 , UpperCAmelCase_ : List[Any]=None , **UpperCAmelCase_ : int , ): """simple docstring""" super().__init__(**UpperCAmelCase_ , pad_token_id=UpperCAmelCase_ , bos_token_id=UpperCAmelCase_ , eos_token_id=UpperCAmelCase_) a : List[Any] = hidden_size a : Any = feat_extract_activation a : Any = list(UpperCAmelCase_) a : Optional[int] = list(UpperCAmelCase_) a : Dict = list(UpperCAmelCase_) a : Tuple = conv_bias a : str = num_conv_pos_embeddings a : Dict = num_conv_pos_embedding_groups a : Optional[Any] = conv_pos_kernel_size a : Any = len(self.conv_dim) a : Tuple = num_hidden_layers a : Any = intermediate_size a : Any = hidden_act a : Dict = num_attention_heads a : Dict = hidden_dropout a : Union[str, Any] = attention_dropout a : Dict = activation_dropout a : Optional[int] = feat_proj_dropout a : Tuple = final_dropout a : Union[str, Any] = layerdrop a : Tuple = layer_norm_eps a : Dict = initializer_range a : Tuple = vocab_size a : int = use_weighted_layer_sum if ( (len(self.conv_stride) != self.num_feat_extract_layers) or (len(self.conv_kernel) != self.num_feat_extract_layers) or (len(self.conv_dim) != self.num_feat_extract_layers) ): raise ValueError( 'Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==' ' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =' f""" {len(self.conv_dim)}`, `len(config.conv_stride) = {len(self.conv_stride)}`,""" f""" `len(config.conv_kernel) = {len(self.conv_kernel)}`.""") # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 a : List[str] = mask_time_prob a : int = mask_time_length a : Optional[int] = mask_time_min_masks a : Dict = mask_feature_prob a : List[str] = mask_feature_length a : str = mask_feature_min_masks # ctc loss a : str = ctc_loss_reduction a : Optional[Any] = ctc_zero_infinity # adapter a : List[str] = add_adapter a : Optional[Any] = adapter_kernel_size a : int = adapter_stride a : str = num_adapter_layers a : Optional[Any] = output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. a : str = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. a : List[Any] = list(UpperCAmelCase_) a : List[str] = list(UpperCAmelCase_) a : str = list(UpperCAmelCase_) a : Optional[Any] = xvector_output_dim @property def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" return math.prod(self.conv_stride)
345
1
'''simple docstring''' import os import unittest from transformers import MobileBertTokenizer, MobileBertTokenizerFast from transformers.models.bert.tokenization_bert import ( VOCAB_FILES_NAMES, BasicTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin, filter_non_english @require_tokenizers class UpperCamelCase ( a_ , unittest.TestCase ): """simple docstring""" A : Dict = MobileBertTokenizer A : List[Any] = MobileBertTokenizerFast A : List[Any] = True A : str = True A : Dict = filter_non_english A : Union[str, Any] = "google/mobilebert-uncased" def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" super().setUp() a : int = [ '[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] a : Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file']) with open(self.vocab_file , 'w' , encoding='utf-8') as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens])) a : Union[str, Any] = [ (tokenizer_def[0], self.pre_trained_model_path, tokenizer_def[2]) # else the 'google/' prefix is stripped for tokenizer_def in self.tokenizers_list ] def SCREAMING_SNAKE_CASE_ ( self : List[Any] , UpperCAmelCase_ : Dict): """simple docstring""" a : Optional[Any] = 'UNwant\u00E9d,running' a : Optional[Any] = 'unwanted, running' return input_text, output_text def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : Optional[int] = self.tokenizer_class(self.vocab_file) a : int = tokenizer.tokenize('UNwant\u00E9d,running') self.assertListEqual(UpperCAmelCase_ , ['un', '##want', '##ed', ',', 'runn', '##ing']) self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCAmelCase_) , [9, 6, 7, 1_2, 1_0, 1_1]) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" if not self.test_rust_tokenizer: return a : List[str] = self.get_tokenizer() a : Optional[Any] = self.get_rust_tokenizer() a : int = 'UNwant\u00E9d,running' a : str = tokenizer.tokenize(UpperCAmelCase_) a : Union[str, Any] = rust_tokenizer.tokenize(UpperCAmelCase_) self.assertListEqual(UpperCAmelCase_ , UpperCAmelCase_) a : List[str] = tokenizer.encode(UpperCAmelCase_ , add_special_tokens=UpperCAmelCase_) a : Dict = rust_tokenizer.encode(UpperCAmelCase_ , add_special_tokens=UpperCAmelCase_) self.assertListEqual(UpperCAmelCase_ , UpperCAmelCase_) a : List[Any] = self.get_rust_tokenizer() a : int = tokenizer.encode(UpperCAmelCase_) a : str = rust_tokenizer.encode(UpperCAmelCase_) self.assertListEqual(UpperCAmelCase_ , UpperCAmelCase_) # With lower casing a : List[Any] = self.get_tokenizer(do_lower_case=UpperCAmelCase_) a : Optional[Any] = self.get_rust_tokenizer(do_lower_case=UpperCAmelCase_) a : List[str] = 'UNwant\u00E9d,running' a : Any = tokenizer.tokenize(UpperCAmelCase_) a : Union[str, Any] = rust_tokenizer.tokenize(UpperCAmelCase_) self.assertListEqual(UpperCAmelCase_ , UpperCAmelCase_) a : Optional[int] = tokenizer.encode(UpperCAmelCase_ , add_special_tokens=UpperCAmelCase_) a : Any = rust_tokenizer.encode(UpperCAmelCase_ , add_special_tokens=UpperCAmelCase_) self.assertListEqual(UpperCAmelCase_ , UpperCAmelCase_) a : Union[str, Any] = self.get_rust_tokenizer() a : Union[str, Any] = tokenizer.encode(UpperCAmelCase_) a : int = rust_tokenizer.encode(UpperCAmelCase_) self.assertListEqual(UpperCAmelCase_ , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : int = BasicTokenizer() self.assertListEqual(tokenizer.tokenize('ah\u535A\u63A8zz') , ['ah', '\u535A', '\u63A8', 'zz']) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : Optional[int] = BasicTokenizer(do_lower_case=UpperCAmelCase_) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? ') , ['hello', '!', 'how', 'are', 'you', '?']) self.assertListEqual(tokenizer.tokenize('H\u00E9llo') , ['hello']) def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" a : Union[str, Any] = BasicTokenizer(do_lower_case=UpperCAmelCase_ , strip_accents=UpperCAmelCase_) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ') , ['hällo', '!', 'how', 'are', 'you', '?']) self.assertListEqual(tokenizer.tokenize('H\u00E9llo') , ['h\u00E9llo']) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Any = BasicTokenizer(do_lower_case=UpperCAmelCase_ , strip_accents=UpperCAmelCase_) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ') , ['hallo', '!', 'how', 'are', 'you', '?']) self.assertListEqual(tokenizer.tokenize('H\u00E9llo') , ['hello']) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : Optional[int] = BasicTokenizer(do_lower_case=UpperCAmelCase_) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ') , ['hallo', '!', 'how', 'are', 'you', '?']) self.assertListEqual(tokenizer.tokenize('H\u00E9llo') , ['hello']) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" a : Dict = BasicTokenizer(do_lower_case=UpperCAmelCase_) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? ') , ['HeLLo', '!', 'how', 'Are', 'yoU', '?']) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : List[Any] = BasicTokenizer(do_lower_case=UpperCAmelCase_ , strip_accents=UpperCAmelCase_) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ') , ['HäLLo', '!', 'how', 'Are', 'yoU', '?']) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" a : Optional[Any] = BasicTokenizer(do_lower_case=UpperCAmelCase_ , strip_accents=UpperCAmelCase_) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ') , ['HaLLo', '!', 'how', 'Are', 'yoU', '?']) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" a : int = BasicTokenizer(do_lower_case=UpperCAmelCase_ , never_split=['[UNK]']) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? [UNK]') , ['HeLLo', '!', 'how', 'Are', 'yoU', '?', '[UNK]']) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Optional[Any] = ['[UNK]', '[CLS]', '[SEP]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing'] a : Dict = {} for i, token in enumerate(UpperCAmelCase_): a : Optional[Any] = i a : int = WordpieceTokenizer(vocab=UpperCAmelCase_ , unk_token='[UNK]') self.assertListEqual(tokenizer.tokenize('') , []) self.assertListEqual(tokenizer.tokenize('unwanted running') , ['un', '##want', '##ed', 'runn', '##ing']) self.assertListEqual(tokenizer.tokenize('unwantedX running') , ['[UNK]', 'runn', '##ing']) def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" self.assertTrue(_is_whitespace(' ')) self.assertTrue(_is_whitespace('\t')) self.assertTrue(_is_whitespace('\r')) self.assertTrue(_is_whitespace('\n')) self.assertTrue(_is_whitespace('\u00A0')) self.assertFalse(_is_whitespace('A')) self.assertFalse(_is_whitespace('-')) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" self.assertTrue(_is_control('\u0005')) self.assertFalse(_is_control('A')) self.assertFalse(_is_control(' ')) self.assertFalse(_is_control('\t')) self.assertFalse(_is_control('\r')) def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" self.assertTrue(_is_punctuation('-')) self.assertTrue(_is_punctuation('$')) self.assertTrue(_is_punctuation('`')) self.assertTrue(_is_punctuation('.')) self.assertFalse(_is_punctuation('A')) self.assertFalse(_is_punctuation(' ')) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" a : List[str] = self.get_tokenizer() a : List[str] = self.get_rust_tokenizer() # Example taken from the issue https://github.com/huggingface/tokenizers/issues/340 self.assertListEqual([tokenizer.tokenize(UpperCAmelCase_) for t in ['Test', '\xad', 'test']] , [['[UNK]'], [], ['[UNK]']]) self.assertListEqual( [rust_tokenizer.tokenize(UpperCAmelCase_) for t in ['Test', '\xad', 'test']] , [['[UNK]'], [], ['[UNK]']]) @slow def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" a : Union[str, Any] = self.tokenizer_class.from_pretrained('google/mobilebert-uncased') a : Dict = tokenizer.encode('sequence builders' , add_special_tokens=UpperCAmelCase_) a : Optional[Any] = tokenizer.encode('multi-sequence build' , add_special_tokens=UpperCAmelCase_) a : int = tokenizer.build_inputs_with_special_tokens(UpperCAmelCase_) a : Tuple = tokenizer.build_inputs_with_special_tokens(UpperCAmelCase_ , UpperCAmelCase_) assert encoded_sentence == [1_0_1] + text + [1_0_2] assert encoded_pair == [1_0_1] + text + [1_0_2] + text_a + [1_0_2] def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})"""): a : Optional[Any] = self.rust_tokenizer_class.from_pretrained(UpperCAmelCase_ , **UpperCAmelCase_) a : str = f"""A, naïve {tokenizer_r.mask_token} AllenNLP sentence.""" a : List[Any] = tokenizer_r.encode_plus( UpperCAmelCase_ , return_attention_mask=UpperCAmelCase_ , return_token_type_ids=UpperCAmelCase_ , return_offsets_mapping=UpperCAmelCase_ , add_special_tokens=UpperCAmelCase_ , ) a : List[Any] = tokenizer_r.do_lower_case if hasattr(UpperCAmelCase_ , 'do_lower_case') else False a : Tuple = ( [ ((0, 0), tokenizer_r.cls_token), ((0, 1), 'A'), ((1, 2), ','), ((3, 5), 'na'), ((5, 6), '##ï'), ((6, 8), '##ve'), ((9, 1_5), tokenizer_r.mask_token), ((1_6, 2_1), 'Allen'), ((2_1, 2_3), '##NL'), ((2_3, 2_4), '##P'), ((2_5, 3_3), 'sentence'), ((3_3, 3_4), '.'), ((0, 0), tokenizer_r.sep_token), ] if not do_lower_case else [ ((0, 0), tokenizer_r.cls_token), ((0, 1), 'a'), ((1, 2), ','), ((3, 8), 'naive'), ((9, 1_5), tokenizer_r.mask_token), ((1_6, 2_1), 'allen'), ((2_1, 2_3), '##nl'), ((2_3, 2_4), '##p'), ((2_5, 3_3), 'sentence'), ((3_3, 3_4), '.'), ((0, 0), tokenizer_r.sep_token), ] ) self.assertEqual( [e[1] for e in expected_results] , tokenizer_r.convert_ids_to_tokens(tokens['input_ids'])) self.assertEqual([e[0] for e in expected_results] , tokens['offset_mapping']) def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : Union[str, Any] = ['的', '人', '有'] a : int = ''.join(UpperCAmelCase_) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})"""): a : Optional[int] = True a : Tuple = self.tokenizer_class.from_pretrained(UpperCAmelCase_ , **UpperCAmelCase_) a : List[Any] = self.rust_tokenizer_class.from_pretrained(UpperCAmelCase_ , **UpperCAmelCase_) a : Optional[int] = tokenizer_p.encode(UpperCAmelCase_ , add_special_tokens=UpperCAmelCase_) a : Union[str, Any] = tokenizer_r.encode(UpperCAmelCase_ , add_special_tokens=UpperCAmelCase_) a : Tuple = tokenizer_r.convert_ids_to_tokens(UpperCAmelCase_) a : Optional[Any] = tokenizer_p.convert_ids_to_tokens(UpperCAmelCase_) # it is expected that each Chinese character is not preceded by "##" self.assertListEqual(UpperCAmelCase_ , UpperCAmelCase_) self.assertListEqual(UpperCAmelCase_ , UpperCAmelCase_) a : List[Any] = False a : Any = self.rust_tokenizer_class.from_pretrained(UpperCAmelCase_ , **UpperCAmelCase_) a : Tuple = self.tokenizer_class.from_pretrained(UpperCAmelCase_ , **UpperCAmelCase_) a : Optional[int] = tokenizer_r.encode(UpperCAmelCase_ , add_special_tokens=UpperCAmelCase_) a : Dict = tokenizer_p.encode(UpperCAmelCase_ , add_special_tokens=UpperCAmelCase_) a : List[str] = tokenizer_r.convert_ids_to_tokens(UpperCAmelCase_) a : str = tokenizer_p.convert_ids_to_tokens(UpperCAmelCase_) # it is expected that only the first Chinese character is not preceded by "##". a : Dict = [ f"""##{token}""" if idx != 0 else token for idx, token in enumerate(UpperCAmelCase_) ] self.assertListEqual(UpperCAmelCase_ , UpperCAmelCase_) self.assertListEqual(UpperCAmelCase_ , UpperCAmelCase_)
345
'''simple docstring''' import logging from transformers.configuration_utils import PretrainedConfig UpperCamelCase : Optional[Any] = logging.getLogger(__name__) class UpperCamelCase ( a_ ): """simple docstring""" A : Tuple = "masked_bert" def __init__( self : Tuple , UpperCAmelCase_ : List[Any]=3_0_5_2_2 , UpperCAmelCase_ : str=7_6_8 , UpperCAmelCase_ : Optional[Any]=1_2 , UpperCAmelCase_ : Optional[int]=1_2 , UpperCAmelCase_ : Union[str, Any]=3_0_7_2 , UpperCAmelCase_ : Union[str, Any]="gelu" , UpperCAmelCase_ : Optional[Any]=0.1 , UpperCAmelCase_ : List[Any]=0.1 , UpperCAmelCase_ : Optional[int]=5_1_2 , UpperCAmelCase_ : List[str]=2 , UpperCAmelCase_ : str=0.02 , UpperCAmelCase_ : Optional[Any]=1e-12 , UpperCAmelCase_ : Dict=0 , UpperCAmelCase_ : Dict="topK" , UpperCAmelCase_ : str="constant" , UpperCAmelCase_ : Optional[Any]=0.0 , **UpperCAmelCase_ : Optional[Any] , ): """simple docstring""" super().__init__(pad_token_id=UpperCAmelCase_ , **UpperCAmelCase_) a : Union[str, Any] = vocab_size a : List[Any] = hidden_size a : List[str] = num_hidden_layers a : Any = num_attention_heads a : Optional[Any] = hidden_act a : str = intermediate_size a : Dict = hidden_dropout_prob a : Any = attention_probs_dropout_prob a : Any = max_position_embeddings a : Dict = type_vocab_size a : List[str] = initializer_range a : int = layer_norm_eps a : Dict = pruning_method a : List[str] = mask_init a : Union[str, Any] = mask_scale
345
1
'''simple docstring''' from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel from ...schedulers import ScoreSdeVeScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class UpperCamelCase ( a_ ): """simple docstring""" A : UNetaDModel A : ScoreSdeVeScheduler def __init__( self : List[str] , UpperCAmelCase_ : UNetaDModel , UpperCAmelCase_ : ScoreSdeVeScheduler): """simple docstring""" super().__init__() self.register_modules(unet=UpperCAmelCase_ , scheduler=UpperCAmelCase_) @torch.no_grad() def __call__( self : Any , UpperCAmelCase_ : int = 1 , UpperCAmelCase_ : int = 2_0_0_0 , UpperCAmelCase_ : Optional[Union[torch.Generator, List[torch.Generator]]] = None , UpperCAmelCase_ : Optional[str] = "pil" , UpperCAmelCase_ : bool = True , **UpperCAmelCase_ : Dict , ): """simple docstring""" a : str = self.unet.config.sample_size a : str = (batch_size, 3, img_size, img_size) a : List[Any] = self.unet a : Optional[int] = randn_tensor(UpperCAmelCase_ , generator=UpperCAmelCase_) * self.scheduler.init_noise_sigma a : Union[str, Any] = sample.to(self.device) self.scheduler.set_timesteps(UpperCAmelCase_) self.scheduler.set_sigmas(UpperCAmelCase_) for i, t in enumerate(self.progress_bar(self.scheduler.timesteps)): a : str = self.scheduler.sigmas[i] * torch.ones(shape[0] , device=self.device) # correction step for _ in range(self.scheduler.config.correct_steps): a : Tuple = self.unet(UpperCAmelCase_ , UpperCAmelCase_).sample a : int = self.scheduler.step_correct(UpperCAmelCase_ , UpperCAmelCase_ , generator=UpperCAmelCase_).prev_sample # prediction step a : Dict = model(UpperCAmelCase_ , UpperCAmelCase_).sample a : Optional[Any] = self.scheduler.step_pred(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , generator=UpperCAmelCase_) a , a : int = output.prev_sample, output.prev_sample_mean a : str = sample_mean.clamp(0 , 1) a : Optional[int] = sample.cpu().permute(0 , 2 , 3 , 1).numpy() if output_type == "pil": a : Any = self.numpy_to_pil(UpperCAmelCase_) if not return_dict: return (sample,) return ImagePipelineOutput(images=UpperCAmelCase_)
345
'''simple docstring''' import copy import random from transformers import CLIPTokenizer class UpperCamelCase ( a_ ): """simple docstring""" def __init__( self : Union[str, Any] , *UpperCAmelCase_ : Dict , **UpperCAmelCase_ : Any): """simple docstring""" super().__init__(*UpperCAmelCase_ , **UpperCAmelCase_) a : str = {} def SCREAMING_SNAKE_CASE_ ( self : int , UpperCAmelCase_ : Tuple , *UpperCAmelCase_ : Optional[int] , **UpperCAmelCase_ : int): """simple docstring""" a : Dict = super().add_tokens(UpperCAmelCase_ , *UpperCAmelCase_ , **UpperCAmelCase_) if num_added_tokens == 0: raise ValueError( f"""The tokenizer already contains the token {placeholder_token}. Please pass a different""" ' `placeholder_token` that is not already in the tokenizer.') def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : Optional[int] , *UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : List[str]=1 , **UpperCAmelCase_ : Optional[int]): """simple docstring""" a : Any = [] if num_vec_per_token == 1: self.try_adding_tokens(UpperCAmelCase_ , *UpperCAmelCase_ , **UpperCAmelCase_) output.append(UpperCAmelCase_) else: a : int = [] for i in range(UpperCAmelCase_): a : Union[str, Any] = placeholder_token + f"""_{i}""" self.try_adding_tokens(UpperCAmelCase_ , *UpperCAmelCase_ , **UpperCAmelCase_) output.append(UpperCAmelCase_) # handle cases where there is a new placeholder token that contains the current placeholder token but is larger for token in self.token_map: if token in placeholder_token: raise ValueError( f"""The tokenizer already has placeholder token {token} that can get confused with""" f""" {placeholder_token}keep placeholder tokens independent""") a : Any = output def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Optional[int]=False , UpperCAmelCase_ : str=1.0): """simple docstring""" if isinstance(UpperCAmelCase_ , UpperCAmelCase_): a : Any = [] for i in range(len(UpperCAmelCase_)): output.append(self.replace_placeholder_tokens_in_text(text[i] , vector_shuffle=UpperCAmelCase_)) return output for placeholder_token in self.token_map: if placeholder_token in text: a : List[Any] = self.token_map[placeholder_token] a : int = tokens[: 1 + int(len(UpperCAmelCase_) * prop_tokens_to_load)] if vector_shuffle: a : List[Any] = copy.copy(UpperCAmelCase_) random.shuffle(UpperCAmelCase_) a : List[str] = text.replace(UpperCAmelCase_ , ' '.join(UpperCAmelCase_)) return text def __call__( self : Optional[int] , UpperCAmelCase_ : Any , *UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Union[str, Any]=False , UpperCAmelCase_ : Optional[int]=1.0 , **UpperCAmelCase_ : str): """simple docstring""" return super().__call__( self.replace_placeholder_tokens_in_text( UpperCAmelCase_ , vector_shuffle=UpperCAmelCase_ , prop_tokens_to_load=UpperCAmelCase_) , *UpperCAmelCase_ , **UpperCAmelCase_ , ) def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : Optional[int] , *UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Any=False , UpperCAmelCase_ : Optional[Any]=1.0 , **UpperCAmelCase_ : Dict): """simple docstring""" return super().encode( self.replace_placeholder_tokens_in_text( UpperCAmelCase_ , vector_shuffle=UpperCAmelCase_ , prop_tokens_to_load=UpperCAmelCase_) , *UpperCAmelCase_ , **UpperCAmelCase_ , )
345
1
'''simple docstring''' import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class UpperCamelCase ( a_ ): """simple docstring""" A : Tuple = ["image_processor", "tokenizer"] A : Any = "ViTImageProcessor" A : str = ("CLIPTokenizer", "CLIPTokenizerFast") def __init__( self : int , UpperCAmelCase_ : Any=None , UpperCAmelCase_ : Dict=None , **UpperCAmelCase_ : str): """simple docstring""" a : Union[str, Any] = None if "feature_extractor" in kwargs: warnings.warn( 'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`' ' instead.' , UpperCAmelCase_ , ) a : Tuple = kwargs.pop('feature_extractor') a : Dict = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('You need to specify an `image_processor`.') if tokenizer is None: raise ValueError('You need to specify a `tokenizer`.') super().__init__(UpperCAmelCase_ , UpperCAmelCase_) def __call__( self : List[Any] , UpperCAmelCase_ : List[str]=None , UpperCAmelCase_ : Tuple=None , UpperCAmelCase_ : Dict=None , UpperCAmelCase_ : Optional[Any]=None , **UpperCAmelCase_ : Tuple): """simple docstring""" if text is None and visual_prompt is None and images is None: raise ValueError('You have to specify either text, visual prompt or images.') if text is not None and visual_prompt is not None: raise ValueError('You have to specify exactly one type of prompt. Either text or visual prompt.') if text is not None: a : Optional[int] = self.tokenizer(UpperCAmelCase_ , return_tensors=UpperCAmelCase_ , **UpperCAmelCase_) if visual_prompt is not None: a : List[str] = self.image_processor(UpperCAmelCase_ , return_tensors=UpperCAmelCase_ , **UpperCAmelCase_) if images is not None: a : str = self.image_processor(UpperCAmelCase_ , return_tensors=UpperCAmelCase_ , **UpperCAmelCase_) if visual_prompt is not None and images is not None: a : Optional[int] = { 'pixel_values': image_features.pixel_values, 'conditional_pixel_values': prompt_features.pixel_values, } return encoding elif text is not None and images is not None: a : Any = image_features.pixel_values return encoding elif text is not None: return encoding elif visual_prompt is not None: a : Any = { 'conditional_pixel_values': prompt_features.pixel_values, } return encoding else: return BatchEncoding(data=dict(**UpperCAmelCase_) , tensor_type=UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[Any] , *UpperCAmelCase_ : int , **UpperCAmelCase_ : Optional[int]): """simple docstring""" return self.tokenizer.batch_decode(*UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , *UpperCAmelCase_ : List[Any] , **UpperCAmelCase_ : Tuple): """simple docstring""" return self.tokenizer.decode(*UpperCAmelCase_ , **UpperCAmelCase_) @property def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" warnings.warn( '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.' , UpperCAmelCase_ , ) return self.image_processor_class @property def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" warnings.warn( '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.' , UpperCAmelCase_ , ) return self.image_processor
345
'''simple docstring''' import argparse import json import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import AutoImageProcessor, SwinConfig, SwinForImageClassification def SCREAMING_SNAKE_CASE__ ( snake_case : str ) -> Optional[Any]: """simple docstring""" a : Union[str, Any] = SwinConfig() a : Optional[int] = swin_name.split('_' ) a : Union[str, Any] = name_split[1] a : Dict = int(name_split[4] ) a : Union[str, Any] = int(name_split[3][-1] ) if model_size == "tiny": a : Optional[Any] = 96 a : Any = (2, 2, 6, 2) a : List[str] = (3, 6, 12, 24) elif model_size == "small": a : int = 96 a : List[str] = (2, 2, 18, 2) a : int = (3, 6, 12, 24) elif model_size == "base": a : Tuple = 128 a : Optional[int] = (2, 2, 18, 2) a : List[Any] = (4, 8, 16, 32) else: a : Dict = 192 a : str = (2, 2, 18, 2) a : List[Any] = (6, 12, 24, 48) if "in22k" in swin_name: a : Any = 21_841 else: a : str = 1_000 a : str = 'huggingface/label-files' a : Optional[Any] = 'imagenet-1k-id2label.json' a : Dict = json.load(open(hf_hub_download(snake_case , snake_case , repo_type='dataset' ) , 'r' ) ) a : Tuple = {int(snake_case ): v for k, v in idalabel.items()} a : int = idalabel a : str = {v: k for k, v in idalabel.items()} a : Dict = img_size a : List[Any] = num_classes a : str = embed_dim a : Dict = depths a : Union[str, Any] = num_heads a : int = window_size return config def SCREAMING_SNAKE_CASE__ ( snake_case : List[Any] ) -> Optional[int]: """simple docstring""" if "patch_embed.proj" in name: a : int = name.replace('patch_embed.proj' , 'embeddings.patch_embeddings.projection' ) if "patch_embed.norm" in name: a : Tuple = name.replace('patch_embed.norm' , 'embeddings.norm' ) if "layers" in name: a : Optional[int] = 'encoder.' + name if "attn.proj" in name: a : List[Any] = name.replace('attn.proj' , 'attention.output.dense' ) if "attn" in name: a : Tuple = name.replace('attn' , 'attention.self' ) if "norm1" in name: a : Optional[int] = name.replace('norm1' , 'layernorm_before' ) if "norm2" in name: a : Dict = name.replace('norm2' , 'layernorm_after' ) if "mlp.fc1" in name: a : Union[str, Any] = name.replace('mlp.fc1' , 'intermediate.dense' ) if "mlp.fc2" in name: a : Any = name.replace('mlp.fc2' , 'output.dense' ) if name == "norm.weight": a : Union[str, Any] = 'layernorm.weight' if name == "norm.bias": a : List[str] = 'layernorm.bias' if "head" in name: a : Union[str, Any] = name.replace('head' , 'classifier' ) else: a : List[Any] = 'swin.' + name return name def SCREAMING_SNAKE_CASE__ ( snake_case : List[Any] , snake_case : Tuple ) -> List[str]: """simple docstring""" for key in orig_state_dict.copy().keys(): a : Any = orig_state_dict.pop(snake_case ) if "mask" in key: continue elif "qkv" in key: a : Optional[Any] = key.split('.' ) a : Dict = int(key_split[1] ) a : Optional[int] = int(key_split[3] ) a : Tuple = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: a : Optional[Any] = val[:dim, :] a : List[Any] = val[ dim : dim * 2, : ] a : List[Any] = val[-dim:, :] else: a : Dict = val[ :dim ] a : Union[str, Any] = val[ dim : dim * 2 ] a : Union[str, Any] = val[ -dim: ] else: a : Dict = val return orig_state_dict def SCREAMING_SNAKE_CASE__ ( snake_case : Optional[int] , snake_case : Dict ) -> List[str]: """simple docstring""" a : Any = timm.create_model(snake_case , pretrained=snake_case ) timm_model.eval() a : str = get_swin_config(snake_case ) a : Optional[int] = SwinForImageClassification(snake_case ) model.eval() a : Union[str, Any] = convert_state_dict(timm_model.state_dict() , snake_case ) model.load_state_dict(snake_case ) a : List[str] = 'http://images.cocodataset.org/val2017/000000039769.jpg' a : Optional[Any] = AutoImageProcessor.from_pretrained('microsoft/{}'.format(swin_name.replace('_' , '-' ) ) ) a : str = Image.open(requests.get(snake_case , stream=snake_case ).raw ) a : Union[str, Any] = image_processor(images=snake_case , return_tensors='pt' ) a : int = timm_model(inputs['pixel_values'] ) a : Optional[int] = model(**snake_case ).logits assert torch.allclose(snake_case , snake_case , atol=1E-3 ) print(F"""Saving model {swin_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(snake_case ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(snake_case ) if __name__ == "__main__": UpperCamelCase : int = argparse.ArgumentParser() # Required parameters parser.add_argument( """--swin_name""", default="""swin_tiny_patch4_window7_224""", type=str, help="""Name of the Swin timm model you'd like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) UpperCamelCase : Optional[Any] = parser.parse_args() convert_swin_checkpoint(args.swin_name, args.pytorch_dump_folder_path)
345
1
'''simple docstring''' import copy import inspect import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers import TimesformerConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, TimesformerForVideoClassification, TimesformerModel, ) from transformers.models.timesformer.modeling_timesformer import TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from transformers import VideoMAEImageProcessor class UpperCamelCase : """simple docstring""" def __init__( self : Any , UpperCAmelCase_ : Any , UpperCAmelCase_ : List[Any]=1_3 , UpperCAmelCase_ : Dict=1_0 , UpperCAmelCase_ : Optional[int]=3 , UpperCAmelCase_ : Tuple=2 , UpperCAmelCase_ : Union[str, Any]=2 , UpperCAmelCase_ : Any=True , UpperCAmelCase_ : Union[str, Any]=True , UpperCAmelCase_ : Optional[int]=3_2 , UpperCAmelCase_ : Optional[int]=5 , UpperCAmelCase_ : List[str]=4 , UpperCAmelCase_ : Tuple=3_7 , UpperCAmelCase_ : str="gelu" , UpperCAmelCase_ : Optional[int]=0.1 , UpperCAmelCase_ : Union[str, Any]=0.1 , UpperCAmelCase_ : int=1_0 , UpperCAmelCase_ : Any=0.02 , UpperCAmelCase_ : int="divided_space_time" , UpperCAmelCase_ : Optional[int]=None , ): """simple docstring""" a : str = parent a : Optional[Any] = batch_size a : Dict = image_size a : Optional[int] = num_channels a : List[Any] = patch_size a : List[Any] = num_frames a : Optional[int] = is_training a : Any = use_labels a : Tuple = hidden_size a : Tuple = num_hidden_layers a : str = num_attention_heads a : List[Any] = intermediate_size a : Tuple = hidden_act a : Optional[Any] = hidden_dropout_prob a : Any = attention_probs_dropout_prob a : Union[str, Any] = attention_type a : Optional[Any] = initializer_range a : str = scope a : Dict = num_labels # in TimeSformer, the number of spatial tokens equals num_frames * num_patches per frame + 1 CLS token a : Optional[Any] = (image_size // patch_size) ** 2 a : List[Any] = (num_frames) * self.num_patches_per_frame + 1 def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : Dict = floats_tensor( [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]) a : int = None if self.use_labels: a : Tuple = ids_tensor([self.batch_size] , self.num_labels) a : Optional[int] = self.get_config() return config, pixel_values, labels def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : Union[str, Any] = TimesformerConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , num_frames=self.num_frames , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , initializer_range=self.initializer_range , attention_type=self.attention_type , ) a : List[Any] = self.num_labels return config def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[Any]): """simple docstring""" a : List[Any] = TimesformerModel(config=UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Optional[Any] = model(UpperCAmelCase_) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def SCREAMING_SNAKE_CASE_ ( self : List[Any] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : Any): """simple docstring""" a : Union[str, Any] = TimesformerForVideoClassification(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : List[str] = model(UpperCAmelCase_) # verify the logits shape a : Optional[int] = torch.Size((self.batch_size, self.num_labels)) self.parent.assertEqual(result.logits.shape , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : Optional[Any] = self.prepare_config_and_inputs() a , a , a : str = config_and_inputs a : Tuple = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class UpperCamelCase ( a_ , a_ , unittest.TestCase ): """simple docstring""" A : Dict = (TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else () A : Optional[Any] = ( {"feature-extraction": TimesformerModel, "video-classification": TimesformerForVideoClassification} if is_torch_available() else {} ) A : Optional[int] = False A : Union[str, Any] = False A : Tuple = False A : Optional[int] = False def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" a : Optional[Any] = TimesformerModelTester(self) a : List[Any] = ConfigTester( self , config_class=UpperCAmelCase_ , has_text_modality=UpperCAmelCase_ , hidden_size=3_7) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Any , UpperCAmelCase_ : List[Any]=False): """simple docstring""" a : List[str] = copy.deepcopy(UpperCAmelCase_) if return_labels: if model_class in get_values(UpperCAmelCase_): a : Dict = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=UpperCAmelCase_) return inputs_dict def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='TimeSformer does not use inputs_embeds') def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" pass def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" a , a : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a : Tuple = model_class(UpperCAmelCase_) self.assertIsInstance(model.get_input_embeddings() , (nn.Module)) a : List[str] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(UpperCAmelCase_ , nn.Linear)) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a , a : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a : List[Any] = model_class(UpperCAmelCase_) a : str = inspect.signature(model.forward) # signature.parameters is an OrderedDict => so arg_names order is deterministic a : Any = [*signature.parameters.keys()] a : Optional[int] = ['pixel_values'] self.assertListEqual(arg_names[:1] , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" a : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_video_classification(*UpperCAmelCase_) @slow def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a : Dict = TimesformerModel.from_pretrained(UpperCAmelCase_) self.assertIsNotNone(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" if not self.has_attentions: pass else: a , a : int = self.model_tester.prepare_config_and_inputs_for_common() a : Any = True for model_class in self.all_model_classes: a : Optional[Any] = self.model_tester.seq_length a : List[str] = self.model_tester.num_frames a : Dict = True a : Optional[Any] = False a : List[str] = True a : Optional[int] = model_class(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() with torch.no_grad(): a : Dict = model(**self._prepare_for_class(UpperCAmelCase_ , UpperCAmelCase_)) a : Union[str, Any] = outputs.attentions self.assertEqual(len(UpperCAmelCase_) , self.model_tester.num_hidden_layers) # check that output_attentions also work using config del inputs_dict["output_attentions"] a : Dict = True a : List[Any] = model_class(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() with torch.no_grad(): a : List[str] = model(**self._prepare_for_class(UpperCAmelCase_ , UpperCAmelCase_)) a : List[str] = outputs.attentions self.assertEqual(len(UpperCAmelCase_) , self.model_tester.num_hidden_layers) # attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1) self.assertListEqual( list(attentions[0].shape[-3:]) , [self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1] , ) a : List[Any] = len(UpperCAmelCase_) # Check attention is always last and order is fine a : Union[str, Any] = True a : Any = True a : Optional[int] = model_class(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() with torch.no_grad(): a : Union[str, Any] = model(**self._prepare_for_class(UpperCAmelCase_ , UpperCAmelCase_)) self.assertEqual(out_len + 1 , len(UpperCAmelCase_)) a : Any = outputs.attentions self.assertEqual(len(UpperCAmelCase_) , self.model_tester.num_hidden_layers) # attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1) self.assertListEqual( list(self_attentions[0].shape[-3:]) , [self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1] , ) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" def check_hidden_states_output(UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Optional[int]): a : Tuple = model_class(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() with torch.no_grad(): a : List[str] = model(**self._prepare_for_class(UpperCAmelCase_ , UpperCAmelCase_)) a : List[Any] = outputs.hidden_states a : str = self.model_tester.num_hidden_layers + 1 self.assertEqual(len(UpperCAmelCase_) , UpperCAmelCase_) a : Any = self.model_tester.seq_length self.assertListEqual( list(hidden_states[0].shape[-2:]) , [seq_length, self.model_tester.hidden_size] , ) a , a : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a : str = True check_hidden_states_output(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] a : Optional[int] = True check_hidden_states_output(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_) def SCREAMING_SNAKE_CASE__ ( ) -> Tuple: """simple docstring""" a : Dict = hf_hub_download( repo_id='hf-internal-testing/spaghetti-video' , filename='eating_spaghetti.npy' , repo_type='dataset' ) a : Tuple = np.load(snake_case ) return list(snake_case ) @require_torch @require_vision class UpperCamelCase ( unittest.TestCase ): """simple docstring""" @cached_property def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" return ( VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5] , image_std=[0.5, 0.5, 0.5]) if is_vision_available() else None ) @slow def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : int = TimesformerForVideoClassification.from_pretrained('facebook/timesformer-base-finetuned-k400').to( UpperCAmelCase_) a : Any = self.default_image_processor a : Optional[Any] = prepare_video() a : Any = image_processor(video[:8] , return_tensors='pt').to(UpperCAmelCase_) # forward pass with torch.no_grad(): a : List[str] = model(**UpperCAmelCase_) # verify the logits a : str = torch.Size((1, 4_0_0)) self.assertEqual(outputs.logits.shape , UpperCAmelCase_) a : Tuple = torch.tensor([-0.30_16, -0.77_13, -0.42_05]).to(UpperCAmelCase_) self.assertTrue(torch.allclose(outputs.logits[0, :3] , UpperCAmelCase_ , atol=1e-4))
345
'''simple docstring''' import baseaa import io import json import os from copy import deepcopy from ..optimizer import AcceleratedOptimizer from ..scheduler import AcceleratedScheduler class UpperCamelCase : """simple docstring""" def __init__( self : List[str] , UpperCAmelCase_ : Tuple): """simple docstring""" if isinstance(UpperCAmelCase_ , UpperCAmelCase_): # Don't modify user's data should they want to reuse it (e.g. in tests), because once we # modified it, it will not be accepted here again, since `auto` values would have been overridden a : Dict = deepcopy(UpperCAmelCase_) elif os.path.exists(UpperCAmelCase_): with io.open(UpperCAmelCase_ , 'r' , encoding='utf-8') as f: a : Union[str, Any] = json.load(UpperCAmelCase_) else: try: a : Union[str, Any] = baseaa.urlsafe_baadecode(UpperCAmelCase_).decode('utf-8') a : List[str] = json.loads(UpperCAmelCase_) except (UnicodeDecodeError, AttributeError, ValueError): raise ValueError( f"""Expected a string path to an existing deepspeed config, or a dictionary, or a base64 encoded string. Received: {config_file_or_dict}""") a : Optional[int] = config self.set_stage_and_offload() def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : str = self.get_value('zero_optimization.stage' , -1) # offload a : Any = False if self.is_zeroa() or self.is_zeroa(): a : Tuple = set(['cpu', 'nvme']) a : int = set( [ self.get_value('zero_optimization.offload_optimizer.device'), self.get_value('zero_optimization.offload_param.device'), ]) if len(offload_devices & offload_devices_valid) > 0: a : List[str] = True def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : Dict): """simple docstring""" a : List[str] = self.config # find the config node of interest if it exists a : int = ds_key_long.split('.') a : Union[str, Any] = nodes.pop() for node in nodes: a : Union[str, Any] = config.get(UpperCAmelCase_) if config is None: return None, ds_key return config, ds_key def SCREAMING_SNAKE_CASE_ ( self : Any , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : int=None): """simple docstring""" a , a : int = self.find_config_node(UpperCAmelCase_) if config is None: return default return config.get(UpperCAmelCase_ , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : int , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Any=False): """simple docstring""" a : Any = self.config # find the config node of interest if it exists a : Optional[Any] = ds_key_long.split('.') for node in nodes: a : List[str] = config a : int = config.get(UpperCAmelCase_) if config is None: if must_exist: raise ValueError(f"""Can't find {ds_key_long} entry in the config: {self.config}""") else: return # if found remove it if parent_config is not None: parent_config.pop(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : str): """simple docstring""" a : List[str] = self.get_value(UpperCAmelCase_) return False if value is None else bool(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : Union[str, Any]): """simple docstring""" a : List[Any] = self.get_value(UpperCAmelCase_) return False if value is None else not bool(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" return self._stage == 2 def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" return self._stage == 3 def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" return self._offload class UpperCamelCase : """simple docstring""" def __init__( self : str , UpperCAmelCase_ : int): """simple docstring""" a : Union[str, Any] = engine def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : Any , **UpperCAmelCase_ : List[Any]): """simple docstring""" self.engine.backward(UpperCAmelCase_ , **UpperCAmelCase_) # Deepspeed's `engine.step` performs the following operations: # - gradient accumulation check # - gradient clipping # - optimizer step # - zero grad # - checking overflow # - lr_scheduler step (only if engine.lr_scheduler is not None) self.engine.step() # and this plugin overrides the above calls with no-ops when Accelerate runs under # Deepspeed, but allows normal functionality for non-Deepspeed cases thus enabling a simple # training loop that works transparently under many training regimes. class UpperCamelCase ( a_ ): """simple docstring""" def __init__( self : Union[str, Any] , UpperCAmelCase_ : Any): """simple docstring""" super().__init__(UpperCAmelCase_ , device_placement=UpperCAmelCase_ , scaler=UpperCAmelCase_) a : List[str] = hasattr(self.optimizer , 'overflow') def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : Dict=None): """simple docstring""" pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed @property def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" if self.__has_overflow__: return self.optimizer.overflow return False class UpperCamelCase ( a_ ): """simple docstring""" def __init__( self : str , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Optional[Any]): """simple docstring""" super().__init__(UpperCAmelCase_ , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed class UpperCamelCase : """simple docstring""" def __init__( self : Union[str, Any] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Optional[int]=0.0_01 , UpperCAmelCase_ : List[Any]=0 , **UpperCAmelCase_ : Union[str, Any]): """simple docstring""" a : int = params a : str = lr a : Tuple = weight_decay a : Dict = kwargs class UpperCamelCase : """simple docstring""" def __init__( self : int , UpperCAmelCase_ : int , UpperCAmelCase_ : Any=None , UpperCAmelCase_ : Union[str, Any]=0 , **UpperCAmelCase_ : List[Any]): """simple docstring""" a : str = optimizer a : Tuple = total_num_steps a : Optional[Any] = warmup_num_steps a : List[str] = kwargs
345
1
'''simple docstring''' import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, Features, Value from .base import TaskTemplate @dataclass(frozen=a_ ) class UpperCamelCase ( a_ ): """simple docstring""" A : str = field(default="automatic-speech-recognition" , metadata={"include_in_asdict_even_if_is_default": True} ) A : ClassVar[Features] = Features({"audio": Audio()} ) A : ClassVar[Features] = Features({"transcription": Value("string" )} ) A : str = "audio" A : str = "transcription" def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : int): """simple docstring""" if self.audio_column not in features: raise ValueError(f"""Column {self.audio_column} is not present in features.""") if not isinstance(features[self.audio_column] , UpperCAmelCase_): raise ValueError(f"""Column {self.audio_column} is not an Audio type.""") a : Dict = copy.deepcopy(self) a : str = self.input_schema.copy() a : str = features[self.audio_column] a : int = input_schema return task_template @property def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" return {self.audio_column: "audio", self.transcription_column: "transcription"}
345
'''simple docstring''' import os import time import warnings from dataclasses import dataclass, field from enum import Enum from typing import List, Optional, Union import torch from filelock import FileLock from torch.utils.data import Dataset from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import logging from ..processors.glue import glue_convert_examples_to_features, glue_output_modes, glue_processors from ..processors.utils import InputFeatures UpperCamelCase : List[str] = logging.get_logger(__name__) @dataclass class UpperCamelCase : """simple docstring""" A : str = field(metadata={"help": "The name of the task to train on: " + ", ".join(glue_processors.keys() )} ) A : str = field( metadata={"help": "The input data dir. Should contain the .tsv files (or other data files) for the task."} ) A : int = field( default=128 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) A : bool = field( default=a_ , metadata={"help": "Overwrite the cached training and evaluation sets"} ) def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" a : Union[str, Any] = self.task_name.lower() class UpperCamelCase ( a_ ): """simple docstring""" A : int = "train" A : Tuple = "dev" A : List[Any] = "test" class UpperCamelCase ( a_ ): """simple docstring""" A : GlueDataTrainingArguments A : str A : List[InputFeatures] def __init__( self : Tuple , UpperCAmelCase_ : GlueDataTrainingArguments , UpperCAmelCase_ : PreTrainedTokenizerBase , UpperCAmelCase_ : Optional[int] = None , UpperCAmelCase_ : Union[str, Split] = Split.train , UpperCAmelCase_ : Optional[str] = None , ): """simple docstring""" warnings.warn( 'This dataset will be removed from the library soon, preprocessing should be handled with the 🤗 Datasets ' 'library. You can have a look at this example script for pointers: ' 'https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py' , UpperCAmelCase_ , ) a : Dict = args a : int = glue_processors[args.task_name]() a : int = glue_output_modes[args.task_name] if isinstance(UpperCAmelCase_ , UpperCAmelCase_): try: a : str = Split[mode] except KeyError: raise KeyError('mode is not a valid split name') # Load data features from cache or dataset file a : List[str] = os.path.join( cache_dir if cache_dir is not None else args.data_dir , f"""cached_{mode.value}_{tokenizer.__class__.__name__}_{args.max_seq_length}_{args.task_name}""" , ) a : Tuple = self.processor.get_labels() if args.task_name in ["mnli", "mnli-mm"] and tokenizer.__class__.__name__ in ( "RobertaTokenizer", "RobertaTokenizerFast", "XLMRobertaTokenizer", "BartTokenizer", "BartTokenizerFast", ): # HACK(label indices are swapped in RoBERTa pretrained model) a , a : str = label_list[2], label_list[1] a : int = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. a : Union[str, Any] = cached_features_file + '.lock' with FileLock(UpperCAmelCase_): if os.path.exists(UpperCAmelCase_) and not args.overwrite_cache: a : Optional[Any] = time.time() a : Optional[Any] = torch.load(UpperCAmelCase_) logger.info( f"""Loading features from cached file {cached_features_file} [took %.3f s]""" , time.time() - start) else: logger.info(f"""Creating features from dataset file at {args.data_dir}""") if mode == Split.dev: a : List[Any] = self.processor.get_dev_examples(args.data_dir) elif mode == Split.test: a : Optional[Any] = self.processor.get_test_examples(args.data_dir) else: a : List[str] = self.processor.get_train_examples(args.data_dir) if limit_length is not None: a : Dict = examples[:limit_length] a : List[Any] = glue_convert_examples_to_features( UpperCAmelCase_ , UpperCAmelCase_ , max_length=args.max_seq_length , label_list=UpperCAmelCase_ , output_mode=self.output_mode , ) a : Dict = time.time() torch.save(self.features , UpperCAmelCase_) # ^ This seems to take a lot of time so I want to investigate why and how we can improve. logger.info( f"""Saving features into cached file {cached_features_file} [took {time.time() - start:.3f} s]""") def __len__( self : Tuple): """simple docstring""" return len(self.features) def __getitem__( self : Optional[int] , UpperCAmelCase_ : List[str]): """simple docstring""" return self.features[i] def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" return self.label_list
345
1
'''simple docstring''' import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase : Any = logging.get_logger(__name__) UpperCamelCase : int = { """asapp/sew-d-tiny-100k""": """https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json""", # See all SEW-D models at https://huggingface.co/models?filter=sew-d } class UpperCamelCase ( a_ ): """simple docstring""" A : str = "sew-d" def __init__( self : Any , UpperCAmelCase_ : str=3_2 , UpperCAmelCase_ : List[Any]=7_6_8 , UpperCAmelCase_ : int=1_2 , UpperCAmelCase_ : List[str]=1_2 , UpperCAmelCase_ : Optional[Any]=3_0_7_2 , UpperCAmelCase_ : Union[str, Any]=2 , UpperCAmelCase_ : List[str]=5_1_2 , UpperCAmelCase_ : Optional[int]=2_5_6 , UpperCAmelCase_ : Optional[int]=True , UpperCAmelCase_ : Tuple=True , UpperCAmelCase_ : Dict=("p2c", "c2p") , UpperCAmelCase_ : List[Any]="layer_norm" , UpperCAmelCase_ : List[str]="gelu_python" , UpperCAmelCase_ : int=0.1 , UpperCAmelCase_ : Dict=0.1 , UpperCAmelCase_ : Tuple=0.1 , UpperCAmelCase_ : Optional[Any]=0.0 , UpperCAmelCase_ : List[str]=0.1 , UpperCAmelCase_ : Tuple=0.02 , UpperCAmelCase_ : Dict=1e-7 , UpperCAmelCase_ : List[Any]=1e-5 , UpperCAmelCase_ : Union[str, Any]="group" , UpperCAmelCase_ : List[str]="gelu" , UpperCAmelCase_ : Optional[int]=(6_4, 1_2_8, 1_2_8, 1_2_8, 1_2_8, 2_5_6, 2_5_6, 2_5_6, 2_5_6, 5_1_2, 5_1_2, 5_1_2, 5_1_2) , UpperCAmelCase_ : Union[str, Any]=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , UpperCAmelCase_ : Union[str, Any]=(1_0, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , UpperCAmelCase_ : List[Any]=False , UpperCAmelCase_ : Optional[int]=1_2_8 , UpperCAmelCase_ : Tuple=1_6 , UpperCAmelCase_ : str=True , UpperCAmelCase_ : Optional[int]=0.05 , UpperCAmelCase_ : Optional[int]=1_0 , UpperCAmelCase_ : int=2 , UpperCAmelCase_ : Union[str, Any]=0.0 , UpperCAmelCase_ : Dict=1_0 , UpperCAmelCase_ : Tuple=0 , UpperCAmelCase_ : Any="mean" , UpperCAmelCase_ : Union[str, Any]=False , UpperCAmelCase_ : str=False , UpperCAmelCase_ : Any=2_5_6 , UpperCAmelCase_ : Union[str, Any]=0 , UpperCAmelCase_ : List[str]=1 , UpperCAmelCase_ : Dict=2 , **UpperCAmelCase_ : str , ): """simple docstring""" super().__init__(**UpperCAmelCase_ , pad_token_id=UpperCAmelCase_ , bos_token_id=UpperCAmelCase_ , eos_token_id=UpperCAmelCase_) a : int = hidden_size a : List[str] = feat_extract_norm a : str = feat_extract_activation a : Optional[int] = list(UpperCAmelCase_) a : List[Any] = list(UpperCAmelCase_) a : List[str] = list(UpperCAmelCase_) a : Dict = conv_bias a : Optional[Any] = num_conv_pos_embeddings a : Any = num_conv_pos_embedding_groups a : int = len(self.conv_dim) a : int = num_hidden_layers a : Dict = intermediate_size a : int = squeeze_factor a : Optional[Any] = max_position_embeddings a : Optional[int] = position_buckets a : Optional[Any] = share_att_key a : List[str] = relative_attention a : Tuple = norm_rel_ebd a : Union[str, Any] = list(UpperCAmelCase_) a : int = hidden_act a : List[Any] = num_attention_heads a : str = hidden_dropout a : List[str] = attention_dropout a : Union[str, Any] = activation_dropout a : List[str] = feat_proj_dropout a : List[str] = final_dropout a : Any = layer_norm_eps a : Tuple = feature_layer_norm_eps a : str = initializer_range a : Optional[int] = vocab_size if ( (len(self.conv_stride) != self.num_feat_extract_layers) or (len(self.conv_kernel) != self.num_feat_extract_layers) or (len(self.conv_dim) != self.num_feat_extract_layers) ): raise ValueError( 'Configuration for convolutional layers is incorrect.' 'It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,' f"""but is `len(config.conv_dim) = {len(self.conv_dim)}`, `len(config.conv_stride)""" f"""= {len(self.conv_stride)}`, `len(config.conv_kernel) = {len(self.conv_kernel)}`.""") # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 a : Optional[Any] = apply_spec_augment a : List[str] = mask_time_prob a : Tuple = mask_time_length a : Union[str, Any] = mask_time_min_masks a : Dict = mask_feature_prob a : Union[str, Any] = mask_feature_length a : Optional[Any] = mask_feature_min_masks # ctc loss a : str = ctc_loss_reduction a : str = ctc_zero_infinity # sequence classification a : str = use_weighted_layer_sum a : Optional[int] = classifier_proj_size @property def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" return functools.reduce(operator.mul , self.conv_stride , 1)
345
'''simple docstring''' from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, logging if is_torch_available(): import torch UpperCamelCase : Dict = logging.get_logger(__name__) class UpperCamelCase ( a_ ): """simple docstring""" A : Any = ["pixel_values"] def __init__( self : str , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Optional[Dict[str, int]] = None , UpperCAmelCase_ : PILImageResampling = PILImageResampling.BILINEAR , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Dict[str, int] = None , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Union[int, float] = 1 / 2_5_5 , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Optional[Union[float, List[float]]] = None , UpperCAmelCase_ : Optional[Union[float, List[float]]] = None , **UpperCAmelCase_ : List[str] , ): """simple docstring""" super().__init__(**UpperCAmelCase_) a : str = size if size is not None else {'shortest_edge': 2_5_6} a : Dict = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) a : str = crop_size if crop_size is not None else {'height': 2_2_4, 'width': 2_2_4} a : int = get_size_dict(UpperCAmelCase_ , param_name='crop_size') a : Any = do_resize a : List[str] = size a : Union[str, Any] = resample a : int = do_center_crop a : Optional[int] = crop_size a : Tuple = do_rescale a : int = rescale_factor a : Optional[Any] = do_normalize a : Dict = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN a : Any = image_std if image_std is not None else IMAGENET_STANDARD_STD def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Dict[str, int] , UpperCAmelCase_ : PILImageResampling = PILImageResampling.BICUBIC , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Optional[int] , ): """simple docstring""" a : Optional[int] = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) if "shortest_edge" not in size: raise ValueError(f"""The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}""") a : Union[str, Any] = get_resize_output_image_size(UpperCAmelCase_ , size=size['shortest_edge'] , default_to_square=UpperCAmelCase_) return resize(UpperCAmelCase_ , size=UpperCAmelCase_ , resample=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Dict[str, int] , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Optional[int] , ): """simple docstring""" a : List[str] = get_size_dict(UpperCAmelCase_) if "height" not in size or "width" not in size: raise ValueError(f"""The `size` parameter must contain the keys `height` and `width`. Got {size.keys()}""") return center_crop(UpperCAmelCase_ , size=(size['height'], size['width']) , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : float , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Optional[Any]): """simple docstring""" return rescale(UpperCAmelCase_ , scale=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Union[float, List[float]] , UpperCAmelCase_ : Union[float, List[float]] , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Optional[int] , ): """simple docstring""" return normalize(UpperCAmelCase_ , mean=UpperCAmelCase_ , std=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : ImageInput , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Dict[str, int] = None , UpperCAmelCase_ : PILImageResampling = None , UpperCAmelCase_ : bool = None , UpperCAmelCase_ : Dict[str, int] = None , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[float] = None , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[Union[float, List[float]]] = None , UpperCAmelCase_ : Optional[Union[float, List[float]]] = None , UpperCAmelCase_ : Optional[Union[str, TensorType]] = None , UpperCAmelCase_ : Union[str, ChannelDimension] = ChannelDimension.FIRST , **UpperCAmelCase_ : List[str] , ): """simple docstring""" a : int = do_resize if do_resize is not None else self.do_resize a : int = size if size is not None else self.size a : Union[str, Any] = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) a : str = resample if resample is not None else self.resample a : Optional[Any] = do_center_crop if do_center_crop is not None else self.do_center_crop a : Union[str, Any] = crop_size if crop_size is not None else self.crop_size a : Dict = get_size_dict(UpperCAmelCase_ , param_name='crop_size') a : str = do_rescale if do_rescale is not None else self.do_rescale a : int = rescale_factor if rescale_factor is not None else self.rescale_factor a : str = do_normalize if do_normalize is not None else self.do_normalize a : List[str] = image_mean if image_mean is not None else self.image_mean a : Optional[int] = image_std if image_std is not None else self.image_std a : Dict = make_list_of_images(UpperCAmelCase_) if not valid_images(UpperCAmelCase_): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.') if do_resize and size is None: raise ValueError('Size must be specified if do_resize is True.') if do_center_crop and crop_size is None: raise ValueError('Crop size must be specified if do_center_crop is True.') if do_rescale and rescale_factor is None: raise ValueError('Rescale factor must be specified if do_rescale is True.') if do_normalize and (image_mean is None or image_std is None): raise ValueError('Image mean and std must be specified if do_normalize is True.') # All transformations expect numpy arrays. a : List[Any] = [to_numpy_array(UpperCAmelCase_) for image in images] if do_resize: a : Dict = [self.resize(image=UpperCAmelCase_ , size=UpperCAmelCase_ , resample=UpperCAmelCase_) for image in images] if do_center_crop: a : Any = [self.center_crop(image=UpperCAmelCase_ , size=UpperCAmelCase_) for image in images] if do_rescale: a : Optional[int] = [self.rescale(image=UpperCAmelCase_ , scale=UpperCAmelCase_) for image in images] if do_normalize: a : Dict = [self.normalize(image=UpperCAmelCase_ , mean=UpperCAmelCase_ , std=UpperCAmelCase_) for image in images] a : List[Any] = [to_channel_dimension_format(UpperCAmelCase_ , UpperCAmelCase_) for image in images] a : List[str] = {'pixel_values': images} return BatchFeature(data=UpperCAmelCase_ , tensor_type=UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : List[Tuple] = None): """simple docstring""" a : Dict = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(UpperCAmelCase_) != len(UpperCAmelCase_): raise ValueError( 'Make sure that you pass in as many target sizes as the batch dimension of the logits') if is_torch_tensor(UpperCAmelCase_): a : Optional[Any] = target_sizes.numpy() a : List[str] = [] for idx in range(len(UpperCAmelCase_)): a : Optional[Any] = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0) , size=target_sizes[idx] , mode='bilinear' , align_corners=UpperCAmelCase_) a : Union[str, Any] = resized_logits[0].argmax(dim=0) semantic_segmentation.append(UpperCAmelCase_) else: a : Optional[int] = logits.argmax(dim=1) a : List[str] = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0])] return semantic_segmentation
345
1
'''simple docstring''' from collections import defaultdict from math import gcd def SCREAMING_SNAKE_CASE__ ( snake_case : int = 1_500_000 ) -> int: """simple docstring""" a : defaultdict = defaultdict(snake_case ) a : Tuple = 2 while 2 * euclid_m * (euclid_m + 1) <= limit: for euclid_n in range((euclid_m % 2) + 1 , snake_case , 2 ): if gcd(snake_case , snake_case ) > 1: continue a : Optional[Any] = 2 * euclid_m * (euclid_m + euclid_n) for perimeter in range(snake_case , limit + 1 , snake_case ): frequencies[perimeter] += 1 euclid_m += 1 return sum(1 for frequency in frequencies.values() if frequency == 1 ) if __name__ == "__main__": print(f'''{solution() = }''')
345
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE__ ( snake_case : int | float | str , snake_case : int | float | str ) -> list[str]: """simple docstring""" if nth_term == "": return [""] a : Dict = int(snake_case ) a : Optional[int] = int(snake_case ) a : list[str] = [] for temp in range(int(snake_case ) ): series.append(F"""1 / {pow(temp + 1 , int(snake_case ) )}""" if series else '1' ) return series if __name__ == "__main__": import doctest doctest.testmod() UpperCamelCase : Optional[int] = int(input("""Enter the last number (nth term) of the P-Series""")) UpperCamelCase : List[Any] = int(input("""Enter the power for P-Series""")) print("""Formula of P-Series => 1+1/2^p+1/3^p ..... 1/n^p""") print(p_series(nth_term, power))
345
1
'''simple docstring''' from collections.abc import Callable def SCREAMING_SNAKE_CASE__ ( snake_case : Callable[[float], float] , snake_case : float , snake_case : float ) -> float: """simple docstring""" a : float = a a : float = b if function(snake_case ) == 0: # one of the a or b is a root for the function return a elif function(snake_case ) == 0: return b elif ( function(snake_case ) * function(snake_case ) > 0 ): # if none of these are root and they are both positive or negative, # then this algorithm can't find the root raise ValueError('could not find root in given interval.' ) else: a : float = start + (end - start) / 2.0 while abs(start - mid ) > 10**-7: # until precisely equals to 10^-7 if function(snake_case ) == 0: return mid elif function(snake_case ) * function(snake_case ) < 0: a : Tuple = mid else: a : Tuple = mid a : Tuple = start + (end - start) / 2.0 return mid def SCREAMING_SNAKE_CASE__ ( snake_case : float ) -> float: """simple docstring""" return x**3 - 2 * x - 5 if __name__ == "__main__": print(bisection(f, 1, 1_000)) import doctest doctest.testmod()
345
'''simple docstring''' import torch def SCREAMING_SNAKE_CASE__ ( ) -> str: """simple docstring""" if torch.cuda.is_available(): a : int = torch.cuda.device_count() else: a : Any = 0 print(F"""Successfully ran on {num_gpus} GPUs""" ) if __name__ == "__main__": main()
345
1
'''simple docstring''' from typing import Any, Callable, Dict, List, Optional, Union import torch from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionPipeline, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker UpperCamelCase : Optional[Any] = '''CompVis/stable-diffusion-v1-1''' UpperCamelCase : int = '''CompVis/stable-diffusion-v1-2''' UpperCamelCase : List[str] = '''CompVis/stable-diffusion-v1-3''' UpperCamelCase : List[Any] = '''CompVis/stable-diffusion-v1-4''' class UpperCamelCase ( SCREAMING_SNAKE_CASE_ ): """simple docstring""" def __init__( self : Optional[Any] , UpperCAmelCase_ : AutoencoderKL , UpperCAmelCase_ : CLIPTextModel , UpperCAmelCase_ : CLIPTokenizer , UpperCAmelCase_ : UNetaDConditionModel , UpperCAmelCase_ : Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , UpperCAmelCase_ : StableDiffusionSafetyChecker , UpperCAmelCase_ : CLIPImageProcessor , UpperCAmelCase_ : bool = True , ): """simple docstring""" super()._init_() a : List[str] = StableDiffusionPipeline.from_pretrained(__a) a : Union[str, Any] = StableDiffusionPipeline.from_pretrained(__a) a : Dict = StableDiffusionPipeline.from_pretrained(__a) a : Optional[Any] = StableDiffusionPipeline( vae=__a , text_encoder=__a , tokenizer=__a , unet=__a , scheduler=__a , safety_checker=__a , feature_extractor=__a , requires_safety_checker=__a , ) self.register_modules(pipelinea=self.pipea , pipelinea=self.pipea , pipelinea=self.pipea , pipelinea=self.pipea) @property def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" return {k: getattr(self , __a) for k in self.config.keys() if not k.startswith('_')} def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : Optional[Union[str, int]] = "auto"): """simple docstring""" if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory a : Any = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(__a) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" self.enable_attention_slicing(__a) @torch.no_grad() def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : Union[str, List[str]] , UpperCAmelCase_ : int = 5_1_2 , UpperCAmelCase_ : int = 5_1_2 , UpperCAmelCase_ : int = 5_0 , UpperCAmelCase_ : float = 7.5 , UpperCAmelCase_ : Optional[Union[str, List[str]]] = None , UpperCAmelCase_ : Optional[int] = 1 , UpperCAmelCase_ : float = 0.0 , UpperCAmelCase_ : Optional[torch.Generator] = None , UpperCAmelCase_ : Optional[torch.FloatTensor] = None , UpperCAmelCase_ : Optional[str] = "pil" , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , UpperCAmelCase_ : int = 1 , **UpperCAmelCase_ : Dict , ): """simple docstring""" return self.pipea( prompt=__a , height=__a , width=__a , num_inference_steps=__a , guidance_scale=__a , negative_prompt=__a , num_images_per_prompt=__a , eta=__a , generator=__a , latents=__a , output_type=__a , return_dict=__a , callback=__a , callback_steps=__a , **__a , ) @torch.no_grad() def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : Union[str, List[str]] , UpperCAmelCase_ : int = 5_1_2 , UpperCAmelCase_ : int = 5_1_2 , UpperCAmelCase_ : int = 5_0 , UpperCAmelCase_ : float = 7.5 , UpperCAmelCase_ : Optional[Union[str, List[str]]] = None , UpperCAmelCase_ : Optional[int] = 1 , UpperCAmelCase_ : float = 0.0 , UpperCAmelCase_ : Optional[torch.Generator] = None , UpperCAmelCase_ : Optional[torch.FloatTensor] = None , UpperCAmelCase_ : Optional[str] = "pil" , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , UpperCAmelCase_ : int = 1 , **UpperCAmelCase_ : Any , ): """simple docstring""" return self.pipea( prompt=__a , height=__a , width=__a , num_inference_steps=__a , guidance_scale=__a , negative_prompt=__a , num_images_per_prompt=__a , eta=__a , generator=__a , latents=__a , output_type=__a , return_dict=__a , callback=__a , callback_steps=__a , **__a , ) @torch.no_grad() def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : Union[str, List[str]] , UpperCAmelCase_ : int = 5_1_2 , UpperCAmelCase_ : int = 5_1_2 , UpperCAmelCase_ : int = 5_0 , UpperCAmelCase_ : float = 7.5 , UpperCAmelCase_ : Optional[Union[str, List[str]]] = None , UpperCAmelCase_ : Optional[int] = 1 , UpperCAmelCase_ : float = 0.0 , UpperCAmelCase_ : Optional[torch.Generator] = None , UpperCAmelCase_ : Optional[torch.FloatTensor] = None , UpperCAmelCase_ : Optional[str] = "pil" , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , UpperCAmelCase_ : int = 1 , **UpperCAmelCase_ : int , ): """simple docstring""" return self.pipea( prompt=__a , height=__a , width=__a , num_inference_steps=__a , guidance_scale=__a , negative_prompt=__a , num_images_per_prompt=__a , eta=__a , generator=__a , latents=__a , output_type=__a , return_dict=__a , callback=__a , callback_steps=__a , **__a , ) @torch.no_grad() def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : Union[str, List[str]] , UpperCAmelCase_ : int = 5_1_2 , UpperCAmelCase_ : int = 5_1_2 , UpperCAmelCase_ : int = 5_0 , UpperCAmelCase_ : float = 7.5 , UpperCAmelCase_ : Optional[Union[str, List[str]]] = None , UpperCAmelCase_ : Optional[int] = 1 , UpperCAmelCase_ : float = 0.0 , UpperCAmelCase_ : Optional[torch.Generator] = None , UpperCAmelCase_ : Optional[torch.FloatTensor] = None , UpperCAmelCase_ : Optional[str] = "pil" , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , UpperCAmelCase_ : int = 1 , **UpperCAmelCase_ : Optional[int] , ): """simple docstring""" return self.pipea( prompt=__a , height=__a , width=__a , num_inference_steps=__a , guidance_scale=__a , negative_prompt=__a , num_images_per_prompt=__a , eta=__a , generator=__a , latents=__a , output_type=__a , return_dict=__a , callback=__a , callback_steps=__a , **__a , ) @torch.no_grad() def SCREAMING_SNAKE_CASE_ ( self : List[Any] , UpperCAmelCase_ : Union[str, List[str]] , UpperCAmelCase_ : int = 5_1_2 , UpperCAmelCase_ : int = 5_1_2 , UpperCAmelCase_ : int = 5_0 , UpperCAmelCase_ : float = 7.5 , UpperCAmelCase_ : Optional[Union[str, List[str]]] = None , UpperCAmelCase_ : Optional[int] = 1 , UpperCAmelCase_ : float = 0.0 , UpperCAmelCase_ : Optional[torch.Generator] = None , UpperCAmelCase_ : Optional[torch.FloatTensor] = None , UpperCAmelCase_ : Optional[str] = "pil" , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , UpperCAmelCase_ : int = 1 , **UpperCAmelCase_ : int , ): """simple docstring""" a : Tuple = 'cuda' if torch.cuda.is_available() else 'cpu' self.to(__a) # Checks if the height and width are divisible by 8 or not if height % 8 != 0 or width % 8 != 0: raise ValueError(f"""`height` and `width` must be divisible by 8 but are {height} and {width}.""") # Get first result from Stable Diffusion Checkpoint v1.1 a : str = self.textaimg_sda_a( prompt=__a , height=__a , width=__a , num_inference_steps=__a , guidance_scale=__a , negative_prompt=__a , num_images_per_prompt=__a , eta=__a , generator=__a , latents=__a , output_type=__a , return_dict=__a , callback=__a , callback_steps=__a , **__a , ) # Get first result from Stable Diffusion Checkpoint v1.2 a : Optional[Any] = self.textaimg_sda_a( prompt=__a , height=__a , width=__a , num_inference_steps=__a , guidance_scale=__a , negative_prompt=__a , num_images_per_prompt=__a , eta=__a , generator=__a , latents=__a , output_type=__a , return_dict=__a , callback=__a , callback_steps=__a , **__a , ) # Get first result from Stable Diffusion Checkpoint v1.3 a : List[str] = self.textaimg_sda_a( prompt=__a , height=__a , width=__a , num_inference_steps=__a , guidance_scale=__a , negative_prompt=__a , num_images_per_prompt=__a , eta=__a , generator=__a , latents=__a , output_type=__a , return_dict=__a , callback=__a , callback_steps=__a , **__a , ) # Get first result from Stable Diffusion Checkpoint v1.4 a : Union[str, Any] = self.textaimg_sda_a( prompt=__a , height=__a , width=__a , num_inference_steps=__a , guidance_scale=__a , negative_prompt=__a , num_images_per_prompt=__a , eta=__a , generator=__a , latents=__a , output_type=__a , return_dict=__a , callback=__a , callback_steps=__a , **__a , ) # Get all result images into a single list and pass it via StableDiffusionPipelineOutput for final result return StableDiffusionPipelineOutput([resa[0], resa[0], resa[0], resa[0]])
350
'''simple docstring''' import faiss # noqa: F401 # Here to have a nice missing dependency error message early on import numpy # noqa: F401 # Here to have a nice missing dependency error message early on import requests # noqa: F401 # Here to have a nice missing dependency error message early on import sklearn # noqa: F401 # Here to have a nice missing dependency error message early on import tqdm # noqa: F401 # Here to have a nice missing dependency error message early on from mauve import compute_mauve # From: mauve-text import datasets UpperCamelCase : Optional[int] = """\ @inproceedings{pillutla-etal:mauve:neurips2021, title={MAUVE: Measuring the Gap Between Neural Text and Human Text using Divergence Frontiers}, author={Pillutla, Krishna and Swayamdipta, Swabha and Zellers, Rowan and Thickstun, John and Welleck, Sean and Choi, Yejin and Harchaoui, Zaid}, booktitle = {NeurIPS}, year = {2021} } """ UpperCamelCase : Optional[Any] = """\ MAUVE is a library built on PyTorch and HuggingFace Transformers to measure the gap between neural text and human text with the eponymous MAUVE measure. MAUVE summarizes both Type I and Type II errors measured softly using Kullback–Leibler (KL) divergences. For details, see the MAUVE paper: https://arxiv.org/abs/2102.01454 (Neurips, 2021). This metrics is a wrapper around the official implementation of MAUVE: https://github.com/krishnap25/mauve """ UpperCamelCase : str = """ Calculates MAUVE scores between two lists of generated text and reference text. Args: predictions: list of generated text to score. Each predictions should be a string with tokens separated by spaces. references: list of reference for each prediction. Each reference should be a string with tokens separated by spaces. Optional Args: num_buckets: the size of the histogram to quantize P and Q. Options: 'auto' (default) or an integer pca_max_data: the number data points to use for PCA dimensionality reduction prior to clustering. If -1, use all the data. Default -1 kmeans_explained_var: amount of variance of the data to keep in dimensionality reduction by PCA. Default 0.9 kmeans_num_redo: number of times to redo k-means clustering (the best objective is kept). Default 5 kmeans_max_iter: maximum number of k-means iterations. Default 500 featurize_model_name: name of the model from which features are obtained. Default 'gpt2-large' Use one of ['gpt2', 'gpt2-medium', 'gpt2-large', 'gpt2-xl']. device_id: Device for featurization. Supply a GPU id (e.g. 0 or 3) to use GPU. If no GPU with this id is found, use CPU max_text_length: maximum number of tokens to consider. Default 1024 divergence_curve_discretization_size: Number of points to consider on the divergence curve. Default 25 mauve_scaling_factor: \"c\" from the paper. Default 5. verbose: If True (default), print running time updates seed: random seed to initialize k-means cluster assignments. Returns: mauve: MAUVE score, a number between 0 and 1. Larger values indicate that P and Q are closer, frontier_integral: Frontier Integral, a number between 0 and 1. Smaller values indicate that P and Q are closer, divergence_curve: a numpy.ndarray of shape (m, 2); plot it with matplotlib to view the divergence curve, p_hist: a discrete distribution, which is a quantized version of the text distribution p_text, q_hist: same as above, but with q_text. Examples: >>> # faiss segfaults in doctest for some reason, so the .compute call is not tested with doctest >>> import datasets >>> mauve = datasets.load_metric('mauve') >>> predictions = [\"hello there\", \"general kenobi\"] >>> references = [\"hello there\", \"general kenobi\"] >>> out = mauve.compute(predictions=predictions, references=references) # doctest: +SKIP >>> print(out.mauve) # doctest: +SKIP 1.0 """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCamelCase ( datasets.Metric ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage='https://github.com/krishnap25/mauve' , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('string' , id='sequence'), 'references': datasets.Value('string' , id='sequence'), }) , codebase_urls=['https://github.com/krishnap25/mauve'] , reference_urls=[ 'https://arxiv.org/abs/2102.01454', 'https://github.com/krishnap25/mauve', ] , ) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : Optional[Any]=None , UpperCAmelCase_ : int=None , UpperCAmelCase_ : Any=None , UpperCAmelCase_ : Optional[int]=None , UpperCAmelCase_ : Tuple="auto" , UpperCAmelCase_ : Any=-1 , UpperCAmelCase_ : Optional[int]=0.9 , UpperCAmelCase_ : Union[str, Any]=5 , UpperCAmelCase_ : int=5_0_0 , UpperCAmelCase_ : int="gpt2-large" , UpperCAmelCase_ : Tuple=-1 , UpperCAmelCase_ : Dict=1_0_2_4 , UpperCAmelCase_ : List[str]=2_5 , UpperCAmelCase_ : int=5 , UpperCAmelCase_ : Any=True , UpperCAmelCase_ : str=2_5 , ): """simple docstring""" a : List[str] = compute_mauve( p_text=UpperCAmelCase_ , q_text=UpperCAmelCase_ , p_features=UpperCAmelCase_ , q_features=UpperCAmelCase_ , p_tokens=UpperCAmelCase_ , q_tokens=UpperCAmelCase_ , num_buckets=UpperCAmelCase_ , pca_max_data=UpperCAmelCase_ , kmeans_explained_var=UpperCAmelCase_ , kmeans_num_redo=UpperCAmelCase_ , kmeans_max_iter=UpperCAmelCase_ , featurize_model_name=UpperCAmelCase_ , device_id=UpperCAmelCase_ , max_text_length=UpperCAmelCase_ , divergence_curve_discretization_size=UpperCAmelCase_ , mauve_scaling_factor=UpperCAmelCase_ , verbose=UpperCAmelCase_ , seed=UpperCAmelCase_ , ) return out
345
0
'''simple docstring''' import argparse import io import requests import torch from omegaconf import OmegaConf from diffusers import AutoencoderKL from diffusers.pipelines.stable_diffusion.convert_from_ckpt import ( assign_to_checkpoint, conv_attn_to_linear, create_vae_diffusers_config, renew_vae_attention_paths, renew_vae_resnet_paths, ) def SCREAMING_SNAKE_CASE__ ( snake_case : Optional[Any] , snake_case : Any ) -> Dict: """simple docstring""" a : int = checkpoint a : Any = {} a : int = vae_state_dict["""encoder.conv_in.weight"""] a : Tuple = vae_state_dict["""encoder.conv_in.bias"""] a : Optional[int] = vae_state_dict["""encoder.conv_out.weight"""] a : str = vae_state_dict["""encoder.conv_out.bias"""] a : Any = vae_state_dict["""encoder.norm_out.weight"""] a : Optional[int] = vae_state_dict["""encoder.norm_out.bias"""] a : Union[str, Any] = vae_state_dict["""decoder.conv_in.weight"""] a : List[Any] = vae_state_dict["""decoder.conv_in.bias"""] a : str = vae_state_dict["""decoder.conv_out.weight"""] a : List[Any] = vae_state_dict["""decoder.conv_out.bias"""] a : Optional[Any] = vae_state_dict["""decoder.norm_out.weight"""] a : str = vae_state_dict["""decoder.norm_out.bias"""] a : List[Any] = vae_state_dict["""quant_conv.weight"""] a : str = vae_state_dict["""quant_conv.bias"""] a : Optional[int] = vae_state_dict["""post_quant_conv.weight"""] a : List[str] = vae_state_dict["""post_quant_conv.bias"""] # Retrieves the keys for the encoder down blocks only a : int = len({'.'.join(layer.split('.' )[:3] ) for layer in vae_state_dict if 'encoder.down' in layer} ) a : str = { layer_id: [key for key in vae_state_dict if F"""down.{layer_id}""" in key] for layer_id in range(lowercase__ ) } # Retrieves the keys for the decoder up blocks only a : Union[str, Any] = len({'.'.join(layer.split('.' )[:3] ) for layer in vae_state_dict if 'decoder.up' in layer} ) a : str = { layer_id: [key for key in vae_state_dict if F"""up.{layer_id}""" in key] for layer_id in range(lowercase__ ) } for i in range(lowercase__ ): a : Optional[Any] = [key for key in down_blocks[i] if F"""down.{i}""" in key and F"""down.{i}.downsample""" not in key] if F"""encoder.down.{i}.downsample.conv.weight""" in vae_state_dict: a : Optional[int] = vae_state_dict.pop( F"""encoder.down.{i}.downsample.conv.weight""" ) a : List[Any] = vae_state_dict.pop( F"""encoder.down.{i}.downsample.conv.bias""" ) a : Tuple = renew_vae_resnet_paths(lowercase__ ) a : Tuple = {"""old""": F"""down.{i}.block""", """new""": F"""down_blocks.{i}.resnets"""} assign_to_checkpoint(lowercase__ , lowercase__ , lowercase__ , additional_replacements=[meta_path] , config=lowercase__ ) a : Optional[int] = [key for key in vae_state_dict if """encoder.mid.block""" in key] a : int = 2 for i in range(1 , num_mid_res_blocks + 1 ): a : List[Any] = [key for key in mid_resnets if F"""encoder.mid.block_{i}""" in key] a : Any = renew_vae_resnet_paths(lowercase__ ) a : Optional[int] = {"""old""": F"""mid.block_{i}""", """new""": F"""mid_block.resnets.{i - 1}"""} assign_to_checkpoint(lowercase__ , lowercase__ , lowercase__ , additional_replacements=[meta_path] , config=lowercase__ ) a : List[Any] = [key for key in vae_state_dict if """encoder.mid.attn""" in key] a : List[Any] = renew_vae_attention_paths(lowercase__ ) a : Optional[Any] = {"""old""": """mid.attn_1""", """new""": """mid_block.attentions.0"""} assign_to_checkpoint(lowercase__ , lowercase__ , lowercase__ , additional_replacements=[meta_path] , config=lowercase__ ) conv_attn_to_linear(lowercase__ ) for i in range(lowercase__ ): a : Optional[int] = num_up_blocks - 1 - i a : Any = [ key for key in up_blocks[block_id] if F"""up.{block_id}""" in key and F"""up.{block_id}.upsample""" not in key ] if F"""decoder.up.{block_id}.upsample.conv.weight""" in vae_state_dict: a : Dict = vae_state_dict[ F"""decoder.up.{block_id}.upsample.conv.weight""" ] a : Any = vae_state_dict[ F"""decoder.up.{block_id}.upsample.conv.bias""" ] a : Optional[Any] = renew_vae_resnet_paths(lowercase__ ) a : int = {"""old""": F"""up.{block_id}.block""", """new""": F"""up_blocks.{i}.resnets"""} assign_to_checkpoint(lowercase__ , lowercase__ , lowercase__ , additional_replacements=[meta_path] , config=lowercase__ ) a : List[Any] = [key for key in vae_state_dict if """decoder.mid.block""" in key] a : Optional[Any] = 2 for i in range(1 , num_mid_res_blocks + 1 ): a : Union[str, Any] = [key for key in mid_resnets if F"""decoder.mid.block_{i}""" in key] a : Any = renew_vae_resnet_paths(lowercase__ ) a : Dict = {"""old""": F"""mid.block_{i}""", """new""": F"""mid_block.resnets.{i - 1}"""} assign_to_checkpoint(lowercase__ , lowercase__ , lowercase__ , additional_replacements=[meta_path] , config=lowercase__ ) a : Union[str, Any] = [key for key in vae_state_dict if """decoder.mid.attn""" in key] a : List[str] = renew_vae_attention_paths(lowercase__ ) a : Optional[Any] = {"""old""": """mid.attn_1""", """new""": """mid_block.attentions.0"""} assign_to_checkpoint(lowercase__ , lowercase__ , lowercase__ , additional_replacements=[meta_path] , config=lowercase__ ) conv_attn_to_linear(lowercase__ ) return new_checkpoint def SCREAMING_SNAKE_CASE__ ( snake_case : str , snake_case : str , ) -> Tuple: """simple docstring""" a : str = requests.get( ' https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml' ) a : List[str] = io.BytesIO(r.content ) a : str = OmegaConf.load(lowercase__ ) a : Tuple = 512 a : Optional[Any] = """cuda""" if torch.cuda.is_available() else """cpu""" if checkpoint_path.endswith('safetensors' ): from safetensors import safe_open a : str = {} with safe_open(lowercase__ , framework='pt' , device='cpu' ) as f: for key in f.keys(): a : Tuple = f.get_tensor(lowercase__ ) else: a : Union[str, Any] = torch.load(lowercase__ , map_location=lowercase__ )["""state_dict"""] # Convert the VAE model. a : int = create_vae_diffusers_config(lowercase__ , image_size=lowercase__ ) a : List[str] = custom_convert_ldm_vae_checkpoint(lowercase__ , lowercase__ ) a : Dict = AutoencoderKL(**lowercase__ ) vae.load_state_dict(lowercase__ ) vae.save_pretrained(lowercase__ ) if __name__ == "__main__": UpperCamelCase : List[Any] = argparse.ArgumentParser() parser.add_argument("""--vae_pt_path""", default=None, type=str, required=True, help="""Path to the VAE.pt to convert.""") parser.add_argument("""--dump_path""", default=None, type=str, required=True, help="""Path to the VAE.pt to convert.""") UpperCamelCase : Union[str, Any] = parser.parse_args() vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)
351
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE__ ( snake_case : list[int | float] , snake_case : int , snake_case : int ) -> int | float: """simple docstring""" if len(snake_case ) == 0: raise ValueError('find_max() arg is an empty sequence' ) if ( left >= len(snake_case ) or left < -len(snake_case ) or right >= len(snake_case ) or right < -len(snake_case ) ): raise IndexError('list index out of range' ) if left == right: return nums[left] a : Union[str, Any] = (left + right) >> 1 # the middle a : List[str] = find_max(snake_case , snake_case , snake_case ) # find max in range[left, mid] a : Dict = find_max(snake_case , mid + 1 , snake_case ) # 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)
345
0
'''simple docstring''' import argparse import torch from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_from_original_stable_diffusion_ckpt if __name__ == "__main__": UpperCamelCase : str = argparse.ArgumentParser() parser.add_argument( """--checkpoint_path""", default=None, type=str, required=True, help="""Path to the checkpoint to convert.""" ) # !wget https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml parser.add_argument( """--original_config_file""", default=None, type=str, 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( """--scheduler_type""", default="""pndm""", type=str, help="""Type of scheduler to use. Should be one of ['pndm', 'lms', 'ddim', 'euler', 'euler-ancestral', 'dpm']""", ) parser.add_argument( """--pipeline_type""", default=None, type=str, help=( """The pipeline type. One of 'FrozenOpenCLIPEmbedder', 'FrozenCLIPEmbedder', 'PaintByExample'""" """. If `None` pipeline will be automatically inferred.""" ), ) parser.add_argument( """--image_size""", default=None, 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( """--prediction_type""", default=None, type=str, help=( """The prediction type that the model was trained on. Use 'epsilon' for Stable Diffusion v1.X and Stable""" """ Diffusion v2 Base. Use 'v_prediction' 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.)""") parser.add_argument( """--stable_unclip""", type=str, default=None, required=False, help="""Set if this is a stable unCLIP model. One of 'txt2img' or 'img2img'.""", ) parser.add_argument( """--stable_unclip_prior""", type=str, default=None, required=False, help="""Set if this is a stable unCLIP txt2img model. Selects which prior to use. If `--stable_unclip` is set to `txt2img`, the karlo prior (https://huggingface.co/kakaobrain/karlo-v1-alpha/tree/main/prior) is selected by default.""", ) parser.add_argument( """--clip_stats_path""", type=str, help="""Path to the clip stats file. Only required if the stable unclip model's config specifies `model.params.noise_aug_config.params.clip_stats_path`.""", required=False, ) parser.add_argument( """--controlnet""", action="""store_true""", default=None, help="""Set flag if this is a controlnet checkpoint.""" ) parser.add_argument("""--half""", action="""store_true""", help="""Save weights in half precision.""") parser.add_argument( """--vae_path""", type=str, default=None, required=False, help="""Set to a path, hub id to an already converted vae to not convert it again.""", ) UpperCamelCase : str = parser.parse_args() UpperCamelCase : List[str] = download_from_original_stable_diffusion_ckpt( checkpoint_path=args.checkpoint_path, original_config_file=args.original_config_file, image_size=args.image_size, prediction_type=args.prediction_type, model_type=args.pipeline_type, extract_ema=args.extract_ema, scheduler_type=args.scheduler_type, num_in_channels=args.num_in_channels, upcast_attention=args.upcast_attention, from_safetensors=args.from_safetensors, device=args.device, stable_unclip=args.stable_unclip, stable_unclip_prior=args.stable_unclip_prior, clip_stats_path=args.clip_stats_path, controlnet=args.controlnet, vae_path=args.vae_path, ) if args.half: pipe.to(torch_dtype=torch.floataa) if args.controlnet: # only save the controlnet model pipe.controlnet.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors) else: pipe.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)
352
'''simple docstring''' import math import os from copy import deepcopy import datasets import evaluate import torch import transformers from datasets import load_dataset from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer from accelerate import Accelerator from accelerate.test_utils import RegressionDataset, RegressionModel from accelerate.utils import is_tpu_available, set_seed UpperCamelCase : int = """true""" def SCREAMING_SNAKE_CASE__ ( snake_case : List[str] , snake_case : int=82 , snake_case : Tuple=16 ) -> Union[str, Any]: """simple docstring""" set_seed(42 ) a : List[str] = RegressionModel() a : Union[str, Any] = deepcopy(snake_case ) a : Dict = RegressionDataset(length=snake_case ) a : Dict = DataLoader(snake_case , batch_size=snake_case ) model.to(accelerator.device ) a , a : Optional[int] = accelerator.prepare(snake_case , snake_case ) return model, ddp_model, dataloader def SCREAMING_SNAKE_CASE__ ( snake_case : Accelerator , snake_case : Union[str, Any]=False ) -> Optional[int]: """simple docstring""" a : List[str] = AutoTokenizer.from_pretrained('hf-internal-testing/mrpc-bert-base-cased' ) a : Any = load_dataset('glue' , 'mrpc' , split='validation' ) def tokenize_function(snake_case : int ): a : Any = tokenizer(examples['sentence1'] , examples['sentence2'] , truncation=snake_case , max_length=snake_case ) return outputs with accelerator.main_process_first(): a : Dict = dataset.map( snake_case , batched=snake_case , remove_columns=['idx', 'sentence1', 'sentence2'] , ) a : List[str] = tokenized_datasets.rename_column('label' , 'labels' ) def collate_fn(snake_case : Optional[Any] ): if use_longest: return tokenizer.pad(snake_case , padding='longest' , return_tensors='pt' ) return tokenizer.pad(snake_case , padding='max_length' , max_length=128 , return_tensors='pt' ) return DataLoader(snake_case , shuffle=snake_case , collate_fn=snake_case , batch_size=16 ) def SCREAMING_SNAKE_CASE__ ( snake_case : Optional[Any] , snake_case : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" a : int = Accelerator(dispatch_batches=snake_case , split_batches=snake_case ) a : List[str] = get_dataloader(snake_case , not dispatch_batches ) a : Optional[Any] = AutoModelForSequenceClassification.from_pretrained( 'hf-internal-testing/mrpc-bert-base-cased' , return_dict=snake_case ) a , a : Optional[Any] = accelerator.prepare(snake_case , snake_case ) return {"ddp": [ddp_model, ddp_dataloader, "cuda:0"], "no": [model, dataloader, accelerator.device]}, accelerator def SCREAMING_SNAKE_CASE__ ( snake_case : Optional[Any] , snake_case : Optional[Any] , snake_case : Union[str, Any] ) -> Optional[int]: """simple docstring""" a : Dict = [] for batch in dataloader: a , a : Any = batch.values() with torch.no_grad(): a : Tuple = model(snake_case ) a , a : Dict = accelerator.gather_for_metrics((logit, target) ) logits_and_targets.append((logit, target) ) a , a : List[str] = [], [] for logit, targ in logits_and_targets: logits.append(snake_case ) targs.append(snake_case ) a , a : Any = torch.cat(snake_case ), torch.cat(snake_case ) return logits, targs def SCREAMING_SNAKE_CASE__ ( snake_case : Accelerator , snake_case : Dict=82 , snake_case : str=False , snake_case : List[str]=False , snake_case : List[Any]=16 ) -> Optional[int]: """simple docstring""" a , a , a : int = get_basic_setup(snake_case , snake_case , snake_case ) a , a : int = generate_predictions(snake_case , snake_case , snake_case ) assert ( len(snake_case ) == num_samples ), F"""Unexpected number of inputs:\n Expected: {num_samples}\n Actual: {len(snake_case )}""" def SCREAMING_SNAKE_CASE__ ( snake_case : bool = False , snake_case : bool = False ) -> List[str]: """simple docstring""" a : int = evaluate.load('glue' , 'mrpc' ) a , a : Tuple = get_mrpc_setup(snake_case , snake_case ) # First do baseline a , a , a : Tuple = setup['no'] model.to(snake_case ) model.eval() for batch in dataloader: batch.to(snake_case ) with torch.inference_mode(): a : List[Any] = model(**snake_case ) a : Optional[Any] = outputs.logits.argmax(dim=-1 ) metric.add_batch(predictions=snake_case , references=batch['labels'] ) a : Tuple = metric.compute() # Then do distributed a , a , a : Tuple = setup['ddp'] model.eval() for batch in dataloader: with torch.inference_mode(): a : List[str] = model(**snake_case ) a : Optional[Any] = outputs.logits.argmax(dim=-1 ) a : Optional[int] = batch['labels'] a , a : Optional[int] = accelerator.gather_for_metrics((preds, references) ) metric.add_batch(predictions=snake_case , references=snake_case ) a : str = metric.compute() for key in "accuracy f1".split(): assert math.isclose( baseline[key] , distributed[key] ), F"""Baseline and Distributed are not the same for key {key}:\n\tBaseline: {baseline[key]}\n\tDistributed: {distributed[key]}\n""" def SCREAMING_SNAKE_CASE__ ( ) -> str: """simple docstring""" a : Dict = Accelerator(split_batches=snake_case , dispatch_batches=snake_case ) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_warning() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() # These are a bit slower so they should only be ran on the GPU or TPU if torch.cuda.is_available() or is_tpu_available(): if accelerator.is_local_main_process: print('**Testing gather_for_metrics**' ) for split_batches in [True, False]: for dispatch_batches in [True, False]: if accelerator.is_local_main_process: print(F"""With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`""" ) test_mrpc(snake_case , snake_case ) accelerator.state._reset_state() if accelerator.is_local_main_process: print('**Test torch metrics**' ) for split_batches in [True, False]: for dispatch_batches in [True, False]: a : List[Any] = Accelerator(split_batches=snake_case , dispatch_batches=snake_case ) if accelerator.is_local_main_process: print(F"""With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`, length=99""" ) test_torch_metrics(snake_case , 99 ) accelerator.state._reset_state() if accelerator.is_local_main_process: print('**Test last batch is not dropped when perfectly divisible**' ) a : Optional[Any] = Accelerator() test_torch_metrics(snake_case , 512 ) accelerator.state._reset_state() def SCREAMING_SNAKE_CASE__ ( snake_case : Union[str, Any] ) -> int: """simple docstring""" # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
345
0
import argparse import re import torch from CLAP import create_model from transformers import AutoFeatureExtractor, ClapConfig, ClapModel UpperCamelCase : Any = { """text_branch""": """text_model""", """audio_branch""": """audio_model.audio_encoder""", """attn""": """attention.self""", """self.proj""": """output.dense""", """attention.self_mask""": """attn_mask""", """mlp.fc1""": """intermediate.dense""", """mlp.fc2""": """output.dense""", """norm1""": """layernorm_before""", """norm2""": """layernorm_after""", """bn0""": """batch_norm""", } UpperCamelCase : Optional[int] = AutoFeatureExtractor.from_pretrained("""laion/clap-htsat-unfused""", truncation="""rand_trunc""") def SCREAMING_SNAKE_CASE__ ( snake_case : Dict , snake_case : str=False ) -> int: """simple docstring""" a , a : Any = create_model( 'HTSAT-tiny' , 'roberta' , __lowerCAmelCase , precision='fp32' , device='cuda:0' if torch.cuda.is_available() else 'cpu' , enable_fusion=__lowerCAmelCase , fusion_type='aff_2d' if enable_fusion else None , ) return model, model_cfg def SCREAMING_SNAKE_CASE__ ( snake_case : int ) -> Tuple: """simple docstring""" a : Any = {} a : Optional[Any] = R'.*sequential.(\d+).*' a : Tuple = R'.*_projection.(\d+).*' for key, value in state_dict.items(): # check if any key needs to be modified for key_to_modify, new_key in KEYS_TO_MODIFY_MAPPING.items(): if key_to_modify in key: a : str = key.replace(__lowerCAmelCase , __lowerCAmelCase ) if re.match(__lowerCAmelCase , __lowerCAmelCase ): # replace sequential layers with list a : Optional[int] = re.match(__lowerCAmelCase , __lowerCAmelCase ).group(1 ) a : int = key.replace(F"""sequential.{sequential_layer}.""" , F"""layers.{int(__lowerCAmelCase )//3}.linear.""" ) elif re.match(__lowerCAmelCase , __lowerCAmelCase ): a : Union[str, Any] = int(re.match(__lowerCAmelCase , __lowerCAmelCase ).group(1 ) ) # Because in CLAP they use `nn.Sequential`... a : Optional[int] = 1 if projecton_layer == 0 else 2 a : str = key.replace(F"""_projection.{projecton_layer}.""" , F"""_projection.linear{transformers_projection_layer}.""" ) if "audio" and "qkv" in key: # split qkv into query key and value a : Tuple = value a : Optional[int] = mixed_qkv.size(0 ) // 3 a : Optional[Any] = mixed_qkv[:qkv_dim] a : List[Any] = mixed_qkv[qkv_dim : qkv_dim * 2] a : Optional[Any] = mixed_qkv[qkv_dim * 2 :] a : Tuple = query_layer a : List[Any] = key_layer a : Optional[int] = value_layer else: a : Tuple = value return model_state_dict def SCREAMING_SNAKE_CASE__ ( snake_case : Dict , snake_case : Tuple , snake_case : Tuple , snake_case : int=False ) -> List[Any]: """simple docstring""" a , a : List[Any] = init_clap(__lowerCAmelCase , enable_fusion=__lowerCAmelCase ) clap_model.eval() a : Optional[int] = clap_model.state_dict() a : Dict = rename_state_dict(__lowerCAmelCase ) a : List[str] = ClapConfig() a : Dict = enable_fusion a : Dict = ClapModel(__lowerCAmelCase ) # ignore the spectrogram embedding layer model.load_state_dict(__lowerCAmelCase , strict=__lowerCAmelCase ) model.save_pretrained(__lowerCAmelCase ) transformers_config.save_pretrained(__lowerCAmelCase ) if __name__ == "__main__": UpperCamelCase : Dict = 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("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""") parser.add_argument("""--enable_fusion""", action="""store_true""", help="""Whether to enable fusion or not""") UpperCamelCase : Any = parser.parse_args() convert_clap_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.enable_fusion)
353
'''simple docstring''' from math import acos, sin from typing import List, Tuple, Union import numpy as np import torch from PIL import Image from ...models import AutoencoderKL, UNetaDConditionModel from ...schedulers import DDIMScheduler, DDPMScheduler from ...utils import randn_tensor from ..pipeline_utils import AudioPipelineOutput, BaseOutput, DiffusionPipeline, ImagePipelineOutput from .mel import Mel class UpperCamelCase ( a_ ): """simple docstring""" A : Optional[int] = ["vqvae"] def __init__( self : List[str] , UpperCAmelCase_ : AutoencoderKL , UpperCAmelCase_ : UNetaDConditionModel , UpperCAmelCase_ : Mel , UpperCAmelCase_ : Union[DDIMScheduler, DDPMScheduler] , ): """simple docstring""" super().__init__() self.register_modules(unet=UpperCAmelCase_ , scheduler=UpperCAmelCase_ , mel=UpperCAmelCase_ , vqvae=UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" return 5_0 if isinstance(self.scheduler , UpperCAmelCase_) else 1_0_0_0 @torch.no_grad() def __call__( self : Dict , UpperCAmelCase_ : int = 1 , UpperCAmelCase_ : str = None , UpperCAmelCase_ : np.ndarray = None , UpperCAmelCase_ : int = 0 , UpperCAmelCase_ : int = 0 , UpperCAmelCase_ : int = None , UpperCAmelCase_ : torch.Generator = None , UpperCAmelCase_ : float = 0 , UpperCAmelCase_ : float = 0 , UpperCAmelCase_ : torch.Generator = None , UpperCAmelCase_ : float = 0 , UpperCAmelCase_ : torch.Tensor = None , UpperCAmelCase_ : torch.Tensor = None , UpperCAmelCase_ : Optional[Any]=True , ): """simple docstring""" a : Optional[Any] = steps or self.get_default_steps() self.scheduler.set_timesteps(UpperCAmelCase_) a : Optional[Any] = step_generator or generator # For backwards compatibility if type(self.unet.config.sample_size) == int: a : Dict = (self.unet.config.sample_size, self.unet.config.sample_size) if noise is None: a : Dict = randn_tensor( ( batch_size, self.unet.config.in_channels, self.unet.config.sample_size[0], self.unet.config.sample_size[1], ) , generator=UpperCAmelCase_ , device=self.device , ) a : Tuple = noise a : Optional[int] = None if audio_file is not None or raw_audio is not None: self.mel.load_audio(UpperCAmelCase_ , UpperCAmelCase_) a : List[Any] = self.mel.audio_slice_to_image(UpperCAmelCase_) a : str = np.frombuffer(input_image.tobytes() , dtype='uint8').reshape( (input_image.height, input_image.width)) a : List[str] = (input_image / 2_5_5) * 2 - 1 a : Any = torch.tensor(input_image[np.newaxis, :, :] , dtype=torch.float).to(self.device) if self.vqvae is not None: a : List[Any] = self.vqvae.encode(torch.unsqueeze(UpperCAmelCase_ , 0)).latent_dist.sample( generator=UpperCAmelCase_)[0] a : str = self.vqvae.config.scaling_factor * input_images if start_step > 0: a : Union[str, Any] = self.scheduler.add_noise(UpperCAmelCase_ , UpperCAmelCase_ , self.scheduler.timesteps[start_step - 1]) a : Dict = ( self.unet.config.sample_size[1] * self.mel.get_sample_rate() / self.mel.x_res / self.mel.hop_length ) a : List[Any] = int(mask_start_secs * pixels_per_second) a : Optional[Any] = int(mask_end_secs * pixels_per_second) a : Optional[int] = self.scheduler.add_noise(UpperCAmelCase_ , UpperCAmelCase_ , torch.tensor(self.scheduler.timesteps[start_step:])) for step, t in enumerate(self.progress_bar(self.scheduler.timesteps[start_step:])): if isinstance(self.unet , UpperCAmelCase_): a : Dict = self.unet(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_)['sample'] else: a : str = self.unet(UpperCAmelCase_ , UpperCAmelCase_)['sample'] if isinstance(self.scheduler , UpperCAmelCase_): a : List[Any] = self.scheduler.step( model_output=UpperCAmelCase_ , timestep=UpperCAmelCase_ , sample=UpperCAmelCase_ , eta=UpperCAmelCase_ , generator=UpperCAmelCase_ , )['prev_sample'] else: a : Any = self.scheduler.step( model_output=UpperCAmelCase_ , timestep=UpperCAmelCase_ , sample=UpperCAmelCase_ , generator=UpperCAmelCase_ , )['prev_sample'] if mask is not None: if mask_start > 0: a : str = mask[:, step, :, :mask_start] if mask_end > 0: a : Dict = mask[:, step, :, -mask_end:] if self.vqvae is not None: # 0.18215 was scaling factor used in training to ensure unit variance a : List[str] = 1 / self.vqvae.config.scaling_factor * images a : str = self.vqvae.decode(UpperCAmelCase_)['sample'] a : Tuple = (images / 2 + 0.5).clamp(0 , 1) a : Any = images.cpu().permute(0 , 2 , 3 , 1).numpy() a : List[str] = (images * 2_5_5).round().astype('uint8') a : Tuple = list( (Image.fromarray(_[:, :, 0]) for _ in images) if images.shape[3] == 1 else (Image.fromarray(UpperCAmelCase_ , mode='RGB').convert('L') for _ in images)) a : List[str] = [self.mel.image_to_audio(UpperCAmelCase_) for _ in images] if not return_dict: return images, (self.mel.get_sample_rate(), audios) return BaseOutput(**AudioPipelineOutput(np.array(UpperCAmelCase_)[:, np.newaxis, :]) , **ImagePipelineOutput(UpperCAmelCase_)) @torch.no_grad() def SCREAMING_SNAKE_CASE_ ( self : List[Any] , UpperCAmelCase_ : List[Image.Image] , UpperCAmelCase_ : int = 5_0): """simple docstring""" assert isinstance(self.scheduler , UpperCAmelCase_) self.scheduler.set_timesteps(UpperCAmelCase_) a : Dict = np.array( [np.frombuffer(image.tobytes() , dtype='uint8').reshape((1, image.height, image.width)) for image in images]) a : Tuple = (sample / 2_5_5) * 2 - 1 a : int = torch.Tensor(UpperCAmelCase_).to(self.device) for t in self.progress_bar(torch.flip(self.scheduler.timesteps , (0,))): a : Optional[Any] = t - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps a : Optional[Any] = self.scheduler.alphas_cumprod[t] a : List[Any] = ( self.scheduler.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.scheduler.final_alpha_cumprod ) a : List[str] = 1 - alpha_prod_t a : Optional[Any] = self.unet(UpperCAmelCase_ , UpperCAmelCase_)['sample'] a : Union[str, Any] = (1 - alpha_prod_t_prev) ** 0.5 * model_output a : Dict = (sample - pred_sample_direction) * alpha_prod_t_prev ** (-0.5) a : Union[str, Any] = sample * alpha_prod_t ** 0.5 + beta_prod_t ** 0.5 * model_output return sample @staticmethod def SCREAMING_SNAKE_CASE_ ( UpperCAmelCase_ : torch.Tensor , UpperCAmelCase_ : torch.Tensor , UpperCAmelCase_ : float): """simple docstring""" a : List[Any] = acos(torch.dot(torch.flatten(UpperCAmelCase_) , torch.flatten(UpperCAmelCase_)) / torch.norm(UpperCAmelCase_) / torch.norm(UpperCAmelCase_)) return sin((1 - alpha) * theta) * xa / sin(UpperCAmelCase_) + sin(alpha * theta) * xa / sin(UpperCAmelCase_)
345
0
'''simple docstring''' import unittest from queue import Empty from threading import Thread from transformers import AutoTokenizer, TextIteratorStreamer, TextStreamer, is_torch_available from transformers.testing_utils import CaptureStdout, require_torch, torch_device from ..test_modeling_common import ids_tensor if is_torch_available(): import torch from transformers import AutoModelForCausalLM @require_torch class UpperCamelCase ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : Optional[int] = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-gpt2') a : Tuple = AutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2').to(_lowerCamelCase) a : Dict = -1 a : List[str] = ids_tensor((1, 5) , vocab_size=model.config.vocab_size).to(_lowerCamelCase) a : Any = model.generate(_lowerCamelCase , max_new_tokens=1_0 , do_sample=_lowerCamelCase) a : List[str] = tokenizer.decode(greedy_ids[0]) with CaptureStdout() as cs: a : List[str] = TextStreamer(_lowerCamelCase) model.generate(_lowerCamelCase , max_new_tokens=1_0 , do_sample=_lowerCamelCase , streamer=_lowerCamelCase) # The greedy text should be printed to stdout, except for the final "\n" in the streamer a : Dict = cs.out[:-1] self.assertEqual(_lowerCamelCase , _lowerCamelCase) def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" a : Optional[int] = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-gpt2') a : List[str] = AutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2').to(_lowerCamelCase) a : Dict = -1 a : List[str] = ids_tensor((1, 5) , vocab_size=model.config.vocab_size).to(_lowerCamelCase) a : Optional[int] = model.generate(_lowerCamelCase , max_new_tokens=1_0 , do_sample=_lowerCamelCase) a : str = tokenizer.decode(greedy_ids[0]) a : int = TextIteratorStreamer(_lowerCamelCase) a : List[Any] = {'''input_ids''': input_ids, '''max_new_tokens''': 1_0, '''do_sample''': False, '''streamer''': streamer} a : List[Any] = Thread(target=model.generate , kwargs=_lowerCamelCase) thread.start() a : List[Any] = '''''' for new_text in streamer: streamer_text += new_text self.assertEqual(_lowerCamelCase , _lowerCamelCase) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : List[str] = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-gpt2') a : List[Any] = AutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2').to(_lowerCamelCase) a : List[str] = -1 a : Any = ids_tensor((1, 5) , vocab_size=model.config.vocab_size).to(_lowerCamelCase) a : Tuple = model.generate(_lowerCamelCase , max_new_tokens=1_0 , do_sample=_lowerCamelCase) a : Tuple = greedy_ids[:, input_ids.shape[1] :] a : Tuple = tokenizer.decode(new_greedy_ids[0]) with CaptureStdout() as cs: a : Any = TextStreamer(_lowerCamelCase , skip_prompt=_lowerCamelCase) model.generate(_lowerCamelCase , max_new_tokens=1_0 , do_sample=_lowerCamelCase , streamer=_lowerCamelCase) # The greedy text should be printed to stdout, except for the final "\n" in the streamer a : Any = cs.out[:-1] self.assertEqual(_lowerCamelCase , _lowerCamelCase) def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : List[Any] = AutoTokenizer.from_pretrained('distilgpt2') a : Tuple = AutoModelForCausalLM.from_pretrained('distilgpt2').to(_lowerCamelCase) a : List[Any] = -1 a : Union[str, Any] = torch.ones((1, 5) , device=_lowerCamelCase).long() * model.config.bos_token_id with CaptureStdout() as cs: a : List[Any] = TextStreamer(_lowerCamelCase , skip_special_tokens=_lowerCamelCase) model.generate(_lowerCamelCase , max_new_tokens=1 , do_sample=_lowerCamelCase , streamer=_lowerCamelCase) # The prompt contains a special token, so the streamer should not print it. As such, the output text, when # re-tokenized, must only contain one token a : List[str] = cs.out[:-1] # Remove the final "\n" a : List[Any] = tokenizer(_lowerCamelCase , return_tensors='pt') self.assertEqual(streamer_text_tokenized.input_ids.shape , (1, 1)) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : str = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-gpt2') a : str = AutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2').to(_lowerCamelCase) a : Union[str, Any] = -1 a : Union[str, Any] = ids_tensor((1, 5) , vocab_size=model.config.vocab_size).to(_lowerCamelCase) a : List[str] = TextIteratorStreamer(_lowerCamelCase , timeout=0.0_01) a : str = {'''input_ids''': input_ids, '''max_new_tokens''': 1_0, '''do_sample''': False, '''streamer''': streamer} a : List[str] = Thread(target=model.generate , kwargs=_lowerCamelCase) thread.start() # The streamer will timeout after 0.001 seconds, so an exception will be raised with self.assertRaises(_lowerCamelCase): a : str = '''''' for new_text in streamer: streamer_text += new_text
354
'''simple docstring''' import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_gpu, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class UpperCamelCase : """simple docstring""" def __init__( self : str , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : List[Any]=1_3 , UpperCAmelCase_ : List[str]=3_0 , UpperCAmelCase_ : str=2 , UpperCAmelCase_ : Union[str, Any]=3 , UpperCAmelCase_ : str=True , UpperCAmelCase_ : Union[str, Any]=True , UpperCAmelCase_ : Union[str, Any]=3_2 , UpperCAmelCase_ : Union[str, Any]=5 , UpperCAmelCase_ : Tuple=4 , UpperCAmelCase_ : Dict=3_7 , UpperCAmelCase_ : Optional[int]="gelu" , UpperCAmelCase_ : Optional[int]=0.1 , UpperCAmelCase_ : Union[str, Any]=0.1 , UpperCAmelCase_ : Dict=1_0 , UpperCAmelCase_ : Optional[Any]=0.02 , UpperCAmelCase_ : List[str]=None , UpperCAmelCase_ : Tuple=2 , ): """simple docstring""" a : Any = parent a : Optional[int] = batch_size a : str = image_size a : str = patch_size a : List[Any] = num_channels a : Optional[int] = is_training a : Dict = use_labels a : Any = hidden_size a : Optional[int] = num_hidden_layers a : int = num_attention_heads a : int = intermediate_size a : Any = hidden_act a : Optional[int] = hidden_dropout_prob a : Optional[int] = attention_probs_dropout_prob a : Dict = type_sequence_label_size a : Tuple = initializer_range a : List[str] = scope a : str = encoder_stride # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) a : Optional[Any] = (image_size // patch_size) ** 2 a : str = num_patches + 1 def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : Union[str, Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]) a : List[Any] = None if self.use_labels: a : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size) a : List[str] = self.get_config() return config, pixel_values, labels def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" return ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=UpperCAmelCase_ , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Dict): """simple docstring""" a : int = ViTModel(config=UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Optional[Any] = model(UpperCAmelCase_) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : List[Any]): """simple docstring""" a : str = ViTForMaskedImageModeling(config=UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : List[Any] = model(UpperCAmelCase_) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size)) # test greyscale images a : int = 1 a : Union[str, Any] = ViTForMaskedImageModeling(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Optional[Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]) a : Optional[Any] = model(UpperCAmelCase_) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size)) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : int , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Any): """simple docstring""" a : str = self.type_sequence_label_size a : Tuple = ViTForImageClassification(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Union[str, Any] = model(UpperCAmelCase_ , labels=UpperCAmelCase_) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) # test greyscale images a : List[Any] = 1 a : Union[str, Any] = ViTForImageClassification(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : int = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]) a : Optional[int] = model(UpperCAmelCase_) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Optional[int] = self.prepare_config_and_inputs() ( ( a ) , ( a ) , ( a ) , ) : Tuple = config_and_inputs a : str = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class UpperCamelCase ( a_ , a_ , unittest.TestCase ): """simple docstring""" A : str = ( ( ViTModel, ViTForImageClassification, ViTForMaskedImageModeling, ) if is_torch_available() else () ) A : Optional[Any] = ( {"feature-extraction": ViTModel, "image-classification": ViTForImageClassification} if is_torch_available() else {} ) A : List[str] = True A : Optional[int] = False A : Dict = False A : Optional[int] = False def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" a : str = ViTModelTester(self) a : Optional[Any] = ConfigTester(self , config_class=UpperCAmelCase_ , has_text_modality=UpperCAmelCase_ , hidden_size=3_7) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='ViT does not use inputs_embeds') def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" pass def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a , a : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a : Union[str, Any] = model_class(UpperCAmelCase_) self.assertIsInstance(model.get_input_embeddings() , (nn.Module)) a : Tuple = model.get_output_embeddings() self.assertTrue(x is None or isinstance(UpperCAmelCase_ , nn.Linear)) def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a , a : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a : Union[str, Any] = model_class(UpperCAmelCase_) a : Tuple = inspect.signature(model.forward) # signature.parameters is an OrderedDict => so arg_names order is deterministic a : Dict = [*signature.parameters.keys()] a : Dict = ['pixel_values'] self.assertListEqual(arg_names[:1] , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" a : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" a : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase_) @slow def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a : Dict = ViTModel.from_pretrained(UpperCAmelCase_) self.assertIsNotNone(UpperCAmelCase_) def SCREAMING_SNAKE_CASE__ ( ) -> Optional[Any]: """simple docstring""" a : List[Any] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class UpperCamelCase ( unittest.TestCase ): """simple docstring""" @cached_property def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224') if is_vision_available() else None @slow def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" a : Optional[Any] = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224').to(UpperCAmelCase_) a : List[Any] = self.default_image_processor a : List[str] = prepare_img() a : Tuple = image_processor(images=UpperCAmelCase_ , return_tensors='pt').to(UpperCAmelCase_) # forward pass with torch.no_grad(): a : List[Any] = model(**UpperCAmelCase_) # verify the logits a : List[str] = torch.Size((1, 1_0_0_0)) self.assertEqual(outputs.logits.shape , UpperCAmelCase_) a : Union[str, Any] = torch.tensor([-0.27_44, 0.82_15, -0.08_36]).to(UpperCAmelCase_) self.assertTrue(torch.allclose(outputs.logits[0, :3] , UpperCAmelCase_ , atol=1e-4)) @slow def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : List[str] = ViTModel.from_pretrained('facebook/dino-vits8').to(UpperCAmelCase_) a : Any = ViTImageProcessor.from_pretrained('facebook/dino-vits8' , size=4_8_0) a : int = prepare_img() a : List[str] = image_processor(images=UpperCAmelCase_ , return_tensors='pt') a : List[str] = inputs.pixel_values.to(UpperCAmelCase_) # forward pass with torch.no_grad(): a : List[Any] = model(UpperCAmelCase_ , interpolate_pos_encoding=UpperCAmelCase_) # verify the logits a : Dict = torch.Size((1, 3_6_0_1, 3_8_4)) self.assertEqual(outputs.last_hidden_state.shape , UpperCAmelCase_) a : str = torch.tensor( [[4.23_40, 4.39_06, -6.66_92], [4.54_63, 1.89_28, -6.72_57], [4.44_29, 0.84_96, -5.85_85]]).to(UpperCAmelCase_) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , UpperCAmelCase_ , atol=1e-4)) @slow @require_accelerate @require_torch_gpu def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : str = ViTModel.from_pretrained('facebook/dino-vits8' , torch_dtype=torch.floataa , device_map='auto') a : List[Any] = self.default_image_processor a : List[str] = prepare_img() a : Tuple = image_processor(images=UpperCAmelCase_ , return_tensors='pt') a : Tuple = inputs.pixel_values.to(UpperCAmelCase_) # forward pass to make sure inference works in fp16 with torch.no_grad(): a : Tuple = model(UpperCAmelCase_)
345
0
'''simple docstring''' import os # Precomputes a list of the 100 first triangular numbers UpperCamelCase : Optional[int] = [int(0.5 * n * (n + 1)) for n in range(1, 101)] def SCREAMING_SNAKE_CASE__ ( ) -> List[str]: """simple docstring""" a : Tuple = os.path.dirname(os.path.realpath(__a ) ) a : int = os.path.join(__a , 'words.txt' ) a : Union[str, Any] = '' with open(__a ) as f: a : Dict = f.readline() a : Tuple = [word.strip('"' ) for word in words.strip('\r\n' ).split(',' )] a : str = [ word for word in [sum(ord(__a ) - 64 for x in word ) for word in words] if word in TRIANGULAR_NUMBERS ] return len(__a ) if __name__ == "__main__": print(solution())
355
'''simple docstring''' # Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import numpy as np import torch from ..models.clipseg import CLIPSegForImageSegmentation from ..utils import is_vision_available, requires_backends from .base import PipelineTool if is_vision_available(): from PIL import Image class UpperCamelCase ( a_ ): """simple docstring""" A : List[str] = ( "This is a tool that creates a segmentation mask of an image according to a label. It cannot create an image." "It takes two arguments named `image` which should be the original image, and `label` which should be a text " "describing the elements what should be identified in the segmentation mask. The tool returns the mask." ) A : List[Any] = "CIDAS/clipseg-rd64-refined" A : Optional[Any] = "image_segmenter" A : List[Any] = CLIPSegForImageSegmentation A : Tuple = ["image", "text"] A : Optional[int] = ["image"] def __init__( self : str , *UpperCAmelCase_ : str , **UpperCAmelCase_ : str): """simple docstring""" requires_backends(self , ['vision']) super().__init__(*UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : "Image" , UpperCAmelCase_ : str): """simple docstring""" return self.pre_processor(text=[label] , images=[image] , padding=UpperCAmelCase_ , return_tensors='pt') def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : str): """simple docstring""" with torch.no_grad(): a : Union[str, Any] = self.model(**UpperCAmelCase_).logits return logits def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , UpperCAmelCase_ : int): """simple docstring""" a : int = outputs.cpu().detach().numpy() a : str = 0 a : str = 1 return Image.fromarray((array * 2_5_5).astype(np.uinta))
345
0
'''simple docstring''' def SCREAMING_SNAKE_CASE__ ( snake_case : bytes ) -> str: """simple docstring""" return "".join([hex(__snake_case )[2:].zfill(2 ).upper() for byte in list(__snake_case )] ) def SCREAMING_SNAKE_CASE__ ( snake_case : str ) -> bytes: """simple docstring""" # Check data validity, following RFC3548 # https://www.ietf.org/rfc/rfc3548.txt if (len(__snake_case ) % 2) != 0: raise ValueError( 'Base16 encoded data is invalid:\nData does not have an even number of hex digits.' ) # Check the character set - the standard base16 alphabet # is uppercase according to RFC3548 section 6 if not set(__snake_case ) <= set('0123456789ABCDEF' ): raise ValueError( 'Base16 encoded data is invalid:\nData is not uppercase hex or it contains invalid characters.' ) # For every two hexadecimal digits (= a byte), turn it into an integer. # Then, string the result together into bytes, and return it. return bytes(int(data[i] + data[i + 1] , 16 ) for i in range(0 , len(__snake_case ) , 2 ) ) if __name__ == "__main__": import doctest doctest.testmod()
356
'''simple docstring''' import json import os import unittest from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer from ...test_tokenization_common import TokenizerTesterMixin class UpperCamelCase ( a_ , unittest.TestCase ): """simple docstring""" A : Union[str, Any] = CTRLTokenizer A : List[Any] = False A : Optional[Any] = False def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt a : Dict = ['adapt', 're@@', 'a@@', 'apt', 'c@@', 't', '<unk>'] a : Tuple = dict(zip(UpperCAmelCase_ , range(len(UpperCAmelCase_)))) a : Any = ['#version: 0.2', 'a p', 'ap t</w>', 'r e', 'a d', 'ad apt</w>', ''] a : List[Any] = {'unk_token': '<unk>'} a : Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file']) a : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['merges_file']) with open(self.vocab_file , 'w' , encoding='utf-8') as fp: fp.write(json.dumps(UpperCAmelCase_) + '\n') with open(self.merges_file , 'w' , encoding='utf-8') as fp: fp.write('\n'.join(UpperCAmelCase_)) def SCREAMING_SNAKE_CASE_ ( self : Tuple , **UpperCAmelCase_ : Dict): """simple docstring""" kwargs.update(self.special_tokens_map) return CTRLTokenizer.from_pretrained(self.tmpdirname , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : Any): """simple docstring""" a : List[str] = 'adapt react readapt apt' a : int = 'adapt react readapt apt' return input_text, output_text def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : int = CTRLTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map) a : str = 'adapt react readapt apt' a : Optional[Any] = 'adapt re@@ a@@ c@@ t re@@ adapt apt'.split() a : List[Any] = tokenizer.tokenize(UpperCAmelCase_) self.assertListEqual(UpperCAmelCase_ , UpperCAmelCase_) a : Union[str, Any] = tokens + [tokenizer.unk_token] a : Any = [0, 1, 2, 4, 5, 1, 0, 3, 6] self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCAmelCase_) , UpperCAmelCase_)
345
0
import multiprocessing import os from typing import BinaryIO, Optional, Union import fsspec from .. import Dataset, Features, NamedSplit, config from ..formatting import query_table from ..packaged_modules.json.json import Json from ..utils import logging from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader class UpperCamelCase ( a_ ): """simple docstring""" def __init__( self : List[Any] , UpperCAmelCase_ : NestedDataStructureLike[PathLike] , UpperCAmelCase_ : Optional[NamedSplit] = None , UpperCAmelCase_ : Optional[Features] = None , UpperCAmelCase_ : str = None , UpperCAmelCase_ : bool = False , UpperCAmelCase_ : bool = False , UpperCAmelCase_ : Optional[str] = None , UpperCAmelCase_ : Optional[int] = None , **UpperCAmelCase_ : Union[str, Any] , ): """simple docstring""" super().__init__( _snake_case , split=_snake_case , features=_snake_case , cache_dir=_snake_case , keep_in_memory=_snake_case , streaming=_snake_case , num_proc=_snake_case , **_snake_case , ) a : Dict = field a : List[str] = path_or_paths if isinstance(_snake_case , _snake_case) else {self.split: path_or_paths} a : int = Json( cache_dir=_snake_case , data_files=_snake_case , features=_snake_case , field=_snake_case , **_snake_case , ) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" if self.streaming: a : str = self.builder.as_streaming_dataset(split=self.split) # Build regular (map-style) dataset else: a : List[str] = None a : List[str] = None a : Optional[int] = None a : Union[str, Any] = None self.builder.download_and_prepare( download_config=_snake_case , download_mode=_snake_case , verification_mode=_snake_case , base_path=_snake_case , num_proc=self.num_proc , ) a : Union[str, Any] = self.builder.as_dataset( split=self.split , verification_mode=_snake_case , in_memory=self.keep_in_memory) return dataset class UpperCamelCase : """simple docstring""" def __init__( self : Dict , UpperCAmelCase_ : Dataset , UpperCAmelCase_ : Union[PathLike, BinaryIO] , UpperCAmelCase_ : Optional[int] = None , UpperCAmelCase_ : Optional[int] = None , **UpperCAmelCase_ : List[Any] , ): """simple docstring""" if num_proc is not None and num_proc <= 0: raise ValueError(f"""num_proc {num_proc} must be an integer > 0.""") a : Dict = dataset a : Any = path_or_buf a : str = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE a : List[Any] = num_proc a : Any = 'utf-8' a : Optional[Any] = to_json_kwargs def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : Any = self.to_json_kwargs.pop('path_or_buf' , _snake_case) a : str = self.to_json_kwargs.pop('orient' , 'records') a : Tuple = self.to_json_kwargs.pop('lines' , True if orient == 'records' else False) a : Any = self.to_json_kwargs.pop('index' , False if orient in ['split', 'table'] else True) a : int = self.to_json_kwargs.pop('compression' , _snake_case) if compression not in [None, "infer", "gzip", "bz2", "xz"]: raise NotImplementedError(f"""`datasets` currently does not support {compression} compression""") if isinstance(self.path_or_buf , (str, bytes, os.PathLike)): with fsspec.open(self.path_or_buf , 'wb' , compression=_snake_case) as buffer: a : Dict = self._write(file_obj=_snake_case , orient=_snake_case , lines=_snake_case , index=_snake_case , **self.to_json_kwargs) else: if compression: raise NotImplementedError( f"""The compression parameter is not supported when writing to a buffer, but compression={compression}""" ' was passed. Please provide a local path instead.') a : Tuple = self._write( file_obj=self.path_or_buf , orient=_snake_case , lines=_snake_case , index=_snake_case , **self.to_json_kwargs) return written def SCREAMING_SNAKE_CASE_ ( self : Any , UpperCAmelCase_ : List[str]): """simple docstring""" a , a , a , a , a : Dict = args a : Tuple = query_table( table=self.dataset.data , key=slice(_snake_case , offset + self.batch_size) , indices=self.dataset._indices , ) a : List[str] = batch.to_pandas().to_json( path_or_buf=_snake_case , orient=_snake_case , lines=_snake_case , index=_snake_case , **_snake_case) if not json_str.endswith('\n'): json_str += "\n" return json_str.encode(self.encoding) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : BinaryIO , UpperCAmelCase_ : Any , UpperCAmelCase_ : str , UpperCAmelCase_ : Any , **UpperCAmelCase_ : str , ): """simple docstring""" a : Union[str, Any] = 0 if self.num_proc is None or self.num_proc == 1: for offset in logging.tqdm( range(0 , len(self.dataset) , self.batch_size) , unit='ba' , disable=not logging.is_progress_bar_enabled() , desc='Creating json from Arrow format' , ): a : Optional[int] = self._batch_json((offset, orient, lines, index, to_json_kwargs)) written += file_obj.write(_snake_case) else: a , a : Tuple = len(self.dataset), self.batch_size with multiprocessing.Pool(self.num_proc) as pool: for json_str in logging.tqdm( pool.imap( self._batch_json , [(offset, orient, lines, index, to_json_kwargs) for offset in range(0 , _snake_case , _snake_case)] , ) , total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size , unit='ba' , disable=not logging.is_progress_bar_enabled() , desc='Creating json from Arrow format' , ): written += file_obj.write(_snake_case) return written
357
'''simple docstring''' def SCREAMING_SNAKE_CASE__ ( snake_case : int , snake_case : int ) -> bool: """simple docstring""" return numa ^ numa < 0 if __name__ == "__main__": import doctest doctest.testmod()
345
0
'''simple docstring''' 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 UpperCamelCase : """simple docstring""" def __init__( self : Any , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Optional[int]=1_2 , UpperCAmelCase_ : Optional[Any]=7 , UpperCAmelCase_ : Optional[int]=True , UpperCAmelCase_ : Union[str, Any]=True , UpperCAmelCase_ : Dict=True , UpperCAmelCase_ : Optional[int]=9_9 , UpperCAmelCase_ : Dict=3_2 , UpperCAmelCase_ : Union[str, Any]=3_2 , UpperCAmelCase_ : Union[str, Any]=2 , UpperCAmelCase_ : Optional[Any]=4 , UpperCAmelCase_ : List[Any]=3_7 , UpperCAmelCase_ : Tuple=0.1 , UpperCAmelCase_ : Optional[int]=0.1 , UpperCAmelCase_ : int=5_1_2 , UpperCAmelCase_ : Tuple=0.02 , UpperCAmelCase_ : Any=0 , UpperCAmelCase_ : str=None , ): """simple docstring""" a : str = parent a : int = batch_size a : Union[str, Any] = seq_length a : List[Any] = is_training a : Union[str, Any] = use_input_mask a : List[str] = use_labels a : int = vocab_size a : Any = hidden_size a : List[Any] = projection_dim a : Dict = num_hidden_layers a : Dict = num_attention_heads a : str = intermediate_size a : int = dropout a : int = attention_dropout a : Dict = max_position_embeddings a : Union[str, Any] = initializer_range a : Dict = scope a : Union[str, Any] = bos_token_id def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Any = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size) a : Union[str, Any] = None if self.use_input_mask: a : Union[str, Any] = random_attention_mask([self.batch_size, self.seq_length]) if input_mask is not None: a : int = input_mask.numpy() a : Tuple = input_mask.shape a : Any = np.random.randint(1 , seq_length - 1 , size=(batch_size,)) for batch_idx, start_index in enumerate(_UpperCamelCase): a : Optional[int] = 1 a : List[str] = 0 a : Tuple = self.get_config() return config, input_ids, tf.convert_to_tensor(_UpperCamelCase) def SCREAMING_SNAKE_CASE_ ( self : str): """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 SCREAMING_SNAKE_CASE_ ( self : List[Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Optional[int]): """simple docstring""" a : List[str] = TFBlipTextModel(config=_UpperCamelCase) a : List[Any] = model(_UpperCamelCase , attention_mask=_UpperCamelCase , training=_UpperCamelCase) a : Any = model(_UpperCamelCase , training=_UpperCamelCase) 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 SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" a : Union[str, Any] = self.prepare_config_and_inputs() a : str = config_and_inputs a : Dict = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_tf class UpperCamelCase ( lowercase__ , unittest.TestCase ): """simple docstring""" A : Optional[Any] = (TFBlipTextModel,) if is_tf_available() else () A : int = False A : List[Any] = False A : Dict = False def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : List[str] = BlipTextModelTester(self) a : Tuple = ConfigTester(self , config_class=_UpperCamelCase , hidden_size=3_7) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase) def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" pass def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" pass @unittest.skip(reason='Blip does not use inputs_embeds') def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" pass @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING') def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" pass @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING') def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" pass @slow def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a : Optional[Any] = TFBlipTextModel.from_pretrained(_UpperCamelCase) self.assertIsNotNone(_UpperCamelCase) def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : Tuple=True): """simple docstring""" super().test_pt_tf_model_equivalence(allow_missing_keys=_UpperCamelCase)
358
'''simple docstring''' from typing import Dict, Iterable, 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, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, logging UpperCamelCase : int = logging.get_logger(__name__) class UpperCamelCase ( a_ ): """simple docstring""" A : Dict = ["pixel_values"] def __init__( self : Optional[Any] , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Dict[str, int] = None , UpperCAmelCase_ : PILImageResampling = PILImageResampling.BICUBIC , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Dict[str, int] = None , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Union[int, float] = 1 / 2_5_5 , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Optional[Union[float, Iterable[float]]] = IMAGENET_DEFAULT_MEAN , UpperCAmelCase_ : Optional[Union[float, Iterable[float]]] = IMAGENET_DEFAULT_STD , **UpperCAmelCase_ : List[Any] , ): """simple docstring""" super().__init__(**UpperCAmelCase_) a : List[str] = size if size is not None else {'shortest_edge': 2_2_4} a : str = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) a : str = crop_size if crop_size is not None else {'height': 2_2_4, 'width': 2_2_4} a : int = get_size_dict(UpperCAmelCase_ , param_name='crop_size') a : Any = do_resize a : Dict = size a : Optional[Any] = resample a : List[Any] = do_center_crop a : List[Any] = crop_size a : Optional[Any] = do_rescale a : Dict = rescale_factor a : Tuple = do_normalize a : int = image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN a : Optional[Any] = image_std if image_std is not None else IMAGENET_DEFAULT_STD def SCREAMING_SNAKE_CASE_ ( self : List[Any] , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Dict[str, int] , UpperCAmelCase_ : PILImageResampling = PILImageResampling.BICUBIC , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Any , ): """simple docstring""" a : Optional[Any] = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) # size_dict is a dict with either keys "height" and "width" or "shortest_edge" if "shortest_edge" in size: a : int = int((2_5_6 / 2_2_4) * size['shortest_edge']) a : Optional[int] = get_resize_output_image_size(UpperCAmelCase_ , size=UpperCAmelCase_ , default_to_square=UpperCAmelCase_) a : Optional[Any] = {'height': output_size[0], 'width': output_size[1]} if "height" not in size_dict or "width" not in size_dict: raise ValueError( f"""Size dict must have keys 'height' and 'width' or 'shortest_edge'. Got {size_dict.keys()}""") return resize( UpperCAmelCase_ , size=(size_dict['height'], size_dict['width']) , resample=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Dict[str, int] , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Union[str, Any] , ): """simple docstring""" a : str = get_size_dict(UpperCAmelCase_) if "height" not in size or "width" not in size: raise ValueError(f"""Size dict must have keys 'height' and 'width'. Got {size.keys()}""") return center_crop(UpperCAmelCase_ , size=(size['height'], size['width']) , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Union[int, float] , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Optional[Any] , ): """simple docstring""" return rescale(UpperCAmelCase_ , scale=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Union[float, List[float]] , UpperCAmelCase_ : Union[float, List[float]] , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : List[str] , ): """simple docstring""" return normalize(UpperCAmelCase_ , mean=UpperCAmelCase_ , std=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , UpperCAmelCase_ : ImageInput , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[Dict[str, int]] = None , UpperCAmelCase_ : PILImageResampling = None , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[Dict[str, int]] = None , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[float] = None , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[Union[float, Iterable[float]]] = None , UpperCAmelCase_ : Optional[Union[float, Iterable[float]]] = None , UpperCAmelCase_ : Optional[TensorType] = None , UpperCAmelCase_ : ChannelDimension = ChannelDimension.FIRST , **UpperCAmelCase_ : Optional[Any] , ): """simple docstring""" a : int = do_resize if do_resize is not None else self.do_resize a : Optional[int] = resample if resample is not None else self.resample a : Tuple = do_center_crop if do_center_crop is not None else self.do_center_crop a : Tuple = do_rescale if do_rescale is not None else self.do_rescale a : List[Any] = rescale_factor if rescale_factor is not None else self.rescale_factor a : Dict = do_normalize if do_normalize is not None else self.do_normalize a : Tuple = image_mean if image_mean is not None else self.image_mean a : int = image_std if image_std is not None else self.image_std a : Optional[int] = size if size is not None else self.size a : Optional[Any] = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) a : List[Any] = crop_size if crop_size is not None else self.crop_size a : str = get_size_dict(UpperCAmelCase_ , param_name='crop_size') a : Dict = make_list_of_images(UpperCAmelCase_) if not valid_images(UpperCAmelCase_): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.') if do_resize and size is None: raise ValueError('Size must be specified if do_resize is True.') if do_center_crop and crop_size is None: raise ValueError('Crop size must be specified if do_center_crop is True.') if do_rescale and rescale_factor is None: raise ValueError('Rescale factor must be specified if do_rescale is True.') if do_normalize and (image_mean is None or image_std is None): raise ValueError('Image mean and std must be specified if do_normalize is True.') # All transformations expect numpy arrays. a : Any = [to_numpy_array(UpperCAmelCase_) for image in images] if do_resize: a : Optional[int] = [self.resize(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_) for image in images] if do_center_crop: a : int = [self.center_crop(UpperCAmelCase_ , UpperCAmelCase_) for image in images] if do_rescale: a : Any = [self.rescale(UpperCAmelCase_ , UpperCAmelCase_) for image in images] if do_normalize: a : str = [self.normalize(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_) for image in images] a : Optional[int] = [to_channel_dimension_format(UpperCAmelCase_ , UpperCAmelCase_) for image in images] a : Optional[int] = {'pixel_values': images} return BatchFeature(data=UpperCAmelCase_ , tensor_type=UpperCAmelCase_)
345
0
'''simple docstring''' UpperCamelCase : List[Any] = """Input must be a string of 8 numbers plus letter""" UpperCamelCase : Any = """TRWAGMYFPDXBNJZSQVHLCKE""" def SCREAMING_SNAKE_CASE__ ( snake_case : str ) -> Tuple: if not isinstance(snake_case , snake_case ): a : List[Any] = F"""Expected string as input, found {type(snake_case ).__name__}""" raise TypeError(snake_case ) a : List[str] = spanish_id.replace('-' , '' ).upper() if len(snake_case ) != 9: raise ValueError(snake_case ) try: a : Tuple = int(spanish_id_clean[0:8] ) a : List[str] = spanish_id_clean[8] except ValueError as ex: raise ValueError(snake_case ) from ex if letter.isdigit(): raise ValueError(snake_case ) return letter == LOOKUP_LETTERS[number % 23] if __name__ == "__main__": import doctest doctest.testmod()
359
'''simple docstring''' from __future__ import annotations from decimal import Decimal from math import * # noqa: F403 from sympy import diff def SCREAMING_SNAKE_CASE__ ( snake_case : str , snake_case : float | Decimal , snake_case : float = 10**-10 ) -> float: """simple docstring""" a : Dict = a while True: a : Any = Decimal(snake_case ) - ( Decimal(eval(snake_case ) ) / Decimal(eval(str(diff(snake_case ) ) ) ) # noqa: S307 ) # This number dictates the accuracy of the answer if abs(eval(snake_case ) ) < precision: # noqa: S307 return float(snake_case ) # Let's Execute if __name__ == "__main__": # Find root of trigonometric function # Find value of pi print(f'''The root of sin(x) = 0 is {newton_raphson("sin(x)", 2)}''') # Find root of polynomial print(f'''The root of x**2 - 5*x + 2 = 0 is {newton_raphson("x**2 - 5*x + 2", 0.4)}''') # Find Square Root of 5 print(f'''The root of log(x) - 1 = 0 is {newton_raphson("log(x) - 1", 2)}''') # Exponential Roots print(f'''The root of exp(x) - 1 = 0 is {newton_raphson("exp(x) - 1", 0)}''')
345
0
'''simple docstring''' import inspect import re from transformers.utils import direct_transformers_import # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_config_docstrings.py UpperCamelCase : Union[str, Any] = """src/transformers""" # This is to make sure the transformers module imported is the one in the repo. UpperCamelCase : int = direct_transformers_import(PATH_TO_TRANSFORMERS) UpperCamelCase : List[str] = transformers.models.auto.configuration_auto.CONFIG_MAPPING # Regex pattern used to find the checkpoint mentioned in the docstring of `config_class`. # For example, `[bert-base-uncased](https://huggingface.co/bert-base-uncased)` UpperCamelCase : str = re.compile(R"""\[(.+?)\]\((https://huggingface\.co/.+?)\)""") UpperCamelCase : List[str] = { """DecisionTransformerConfig""", """EncoderDecoderConfig""", """MusicgenConfig""", """RagConfig""", """SpeechEncoderDecoderConfig""", """TimmBackboneConfig""", """VisionEncoderDecoderConfig""", """VisionTextDualEncoderConfig""", """LlamaConfig""", } def SCREAMING_SNAKE_CASE__ ( snake_case : Optional[Any] ) -> Tuple: """simple docstring""" a : Union[str, Any] = None # source code of `config_class` a : List[str] = inspect.getsource(__lowerCamelCase ) a : Optional[Any] = _re_checkpoint.findall(__lowerCamelCase ) # Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link. # For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')` for ckpt_name, ckpt_link in checkpoints: # allow the link to end with `/` if ckpt_link.endswith('/' ): a : Tuple = ckpt_link[:-1] # verify the checkpoint name corresponds to the checkpoint link a : Union[str, Any] = F"""https://huggingface.co/{ckpt_name}""" if ckpt_link == ckpt_link_from_name: a : Union[str, Any] = ckpt_name break return checkpoint def SCREAMING_SNAKE_CASE__ ( ) -> Dict: """simple docstring""" a : str = [] for config_class in list(CONFIG_MAPPING.values() ): # Skip deprecated models if "models.deprecated" in config_class.__module__: continue a : Dict = get_checkpoint_from_config_class(__lowerCamelCase ) a : str = config_class.__name__ if checkpoint is None and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK: configs_without_checkpoint.append(__lowerCamelCase ) if len(__lowerCamelCase ) > 0: a : List[str] = "\n".join(sorted(__lowerCamelCase ) ) raise ValueError(F"""The following configurations don't contain any valid checkpoint:\n{message}""" ) if __name__ == "__main__": check_config_docstrings_have_checkpoints()
360
'''simple docstring''' import unittest from transformers import XLMConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( XLMForMultipleChoice, XLMForQuestionAnswering, XLMForQuestionAnsweringSimple, XLMForSequenceClassification, XLMForTokenClassification, XLMModel, XLMWithLMHeadModel, ) from transformers.models.xlm.modeling_xlm import XLM_PRETRAINED_MODEL_ARCHIVE_LIST class UpperCamelCase : """simple docstring""" def __init__( self : Union[str, Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Optional[int]=1_3 , UpperCAmelCase_ : Dict=7 , UpperCAmelCase_ : int=True , UpperCAmelCase_ : Tuple=True , UpperCAmelCase_ : Any=True , UpperCAmelCase_ : List[Any]=True , UpperCAmelCase_ : List[Any]=True , UpperCAmelCase_ : List[Any]=False , UpperCAmelCase_ : List[Any]=False , UpperCAmelCase_ : Union[str, Any]=False , UpperCAmelCase_ : Union[str, Any]=2 , UpperCAmelCase_ : Optional[Any]=9_9 , UpperCAmelCase_ : str=0 , UpperCAmelCase_ : int=3_2 , UpperCAmelCase_ : Tuple=5 , UpperCAmelCase_ : Any=4 , UpperCAmelCase_ : Tuple=0.1 , UpperCAmelCase_ : int=0.1 , UpperCAmelCase_ : Optional[int]=5_1_2 , UpperCAmelCase_ : str=2 , UpperCAmelCase_ : Any=0.02 , UpperCAmelCase_ : str=2 , UpperCAmelCase_ : Optional[int]=4 , UpperCAmelCase_ : int="last" , UpperCAmelCase_ : Union[str, Any]=True , UpperCAmelCase_ : Tuple=None , UpperCAmelCase_ : List[str]=0 , ): """simple docstring""" a : Tuple = parent a : Optional[Any] = batch_size a : Tuple = seq_length a : Union[str, Any] = is_training a : List[str] = use_input_lengths a : Union[str, Any] = use_token_type_ids a : Optional[int] = use_labels a : int = gelu_activation a : Dict = sinusoidal_embeddings a : Any = causal a : Optional[int] = asm a : int = n_langs a : List[str] = vocab_size a : List[str] = n_special a : List[str] = hidden_size a : Any = num_hidden_layers a : Union[str, Any] = num_attention_heads a : Optional[Any] = hidden_dropout_prob a : str = attention_probs_dropout_prob a : Dict = max_position_embeddings a : Union[str, Any] = type_sequence_label_size a : str = initializer_range a : List[Any] = num_labels a : Union[str, Any] = num_choices a : Optional[Any] = summary_type a : Optional[Any] = use_proj a : Optional[Any] = scope a : Dict = bos_token_id def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size) a : List[Any] = random_attention_mask([self.batch_size, self.seq_length]) a : Optional[int] = None if self.use_input_lengths: a : Optional[int] = ( ids_tensor([self.batch_size] , vocab_size=2) + self.seq_length - 2 ) # small variation of seq_length a : int = None if self.use_token_type_ids: a : str = ids_tensor([self.batch_size, self.seq_length] , self.n_langs) a : Optional[Any] = None a : Tuple = None a : Optional[Any] = None if self.use_labels: a : Optional[int] = ids_tensor([self.batch_size] , self.type_sequence_label_size) a : int = ids_tensor([self.batch_size, self.seq_length] , self.num_labels) a : Optional[Any] = ids_tensor([self.batch_size] , 2).float() a : Dict = ids_tensor([self.batch_size] , self.num_choices) a : Any = self.get_config() return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" return XLMConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , num_labels=self.num_labels , bos_token_id=self.bos_token_id , ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : str , UpperCAmelCase_ : Any , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : str , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[Any] , ): """simple docstring""" a : Any = XLMModel(config=UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : int = model(UpperCAmelCase_ , lengths=UpperCAmelCase_ , langs=UpperCAmelCase_) a : str = model(UpperCAmelCase_ , langs=UpperCAmelCase_) a : int = model(UpperCAmelCase_) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : str , UpperCAmelCase_ : int , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : Any , UpperCAmelCase_ : List[Any] , ): """simple docstring""" a : Optional[Any] = XLMWithLMHeadModel(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : List[str] = model(UpperCAmelCase_ , token_type_ids=UpperCAmelCase_ , labels=UpperCAmelCase_) self.parent.assertEqual(result.loss.shape , ()) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size)) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : Dict , UpperCAmelCase_ : int , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : str , UpperCAmelCase_ : Optional[Any] , ): """simple docstring""" a : Union[str, Any] = XLMForQuestionAnsweringSimple(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : List[str] = model(UpperCAmelCase_) a : Tuple = model(UpperCAmelCase_ , start_positions=UpperCAmelCase_ , end_positions=UpperCAmelCase_) a : Any = outputs self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length)) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length)) def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Any , UpperCAmelCase_ : Any , ): """simple docstring""" a : Any = XLMForQuestionAnswering(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : int = model(UpperCAmelCase_) a : Dict = model( UpperCAmelCase_ , start_positions=UpperCAmelCase_ , end_positions=UpperCAmelCase_ , cls_index=UpperCAmelCase_ , is_impossible=UpperCAmelCase_ , p_mask=UpperCAmelCase_ , ) a : int = model( UpperCAmelCase_ , start_positions=UpperCAmelCase_ , end_positions=UpperCAmelCase_ , cls_index=UpperCAmelCase_ , is_impossible=UpperCAmelCase_ , ) ((a) , ) : Union[str, Any] = result_with_labels.to_tuple() a : int = model(UpperCAmelCase_ , start_positions=UpperCAmelCase_ , end_positions=UpperCAmelCase_) ((a) , ) : Union[str, Any] = result_with_labels.to_tuple() self.parent.assertEqual(result_with_labels.loss.shape , ()) self.parent.assertEqual(result.start_top_log_probs.shape , (self.batch_size, model.config.start_n_top)) self.parent.assertEqual(result.start_top_index.shape , (self.batch_size, model.config.start_n_top)) self.parent.assertEqual( result.end_top_log_probs.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top)) self.parent.assertEqual( result.end_top_index.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top)) self.parent.assertEqual(result.cls_logits.shape , (self.batch_size,)) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : int , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : str , ): """simple docstring""" a : Dict = XLMForSequenceClassification(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Union[str, Any] = model(UpperCAmelCase_) a : Union[str, Any] = model(UpperCAmelCase_ , labels=UpperCAmelCase_) self.parent.assertEqual(result.loss.shape , ()) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , UpperCAmelCase_ : str , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : str , UpperCAmelCase_ : Any , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Any , ): """simple docstring""" a : Dict = self.num_labels a : int = XLMForTokenClassification(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Optional[Any] = model(UpperCAmelCase_ , attention_mask=UpperCAmelCase_ , labels=UpperCAmelCase_) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels)) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : str , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Dict , UpperCAmelCase_ : str , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Dict , ): """simple docstring""" a : str = self.num_choices a : Dict = XLMForMultipleChoice(config=UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Union[str, Any] = input_ids.unsqueeze(1).expand(-1 , self.num_choices , -1).contiguous() a : str = token_type_ids.unsqueeze(1).expand(-1 , self.num_choices , -1).contiguous() a : Optional[int] = input_mask.unsqueeze(1).expand(-1 , self.num_choices , -1).contiguous() a : Any = model( UpperCAmelCase_ , attention_mask=UpperCAmelCase_ , token_type_ids=UpperCAmelCase_ , labels=UpperCAmelCase_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices)) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" a : int = self.prepare_config_and_inputs() ( ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ) : Union[str, Any] = config_and_inputs a : Optional[int] = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'lengths': input_lengths} return config, inputs_dict @require_torch class UpperCamelCase ( a_ , a_ , a_ , unittest.TestCase ): """simple docstring""" A : int = ( ( XLMModel, XLMWithLMHeadModel, XLMForQuestionAnswering, XLMForSequenceClassification, XLMForQuestionAnsweringSimple, XLMForTokenClassification, XLMForMultipleChoice, ) if is_torch_available() else () ) A : List[str] = ( (XLMWithLMHeadModel,) if is_torch_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable A : Optional[Any] = ( { "feature-extraction": XLMModel, "fill-mask": XLMWithLMHeadModel, "question-answering": XLMForQuestionAnsweringSimple, "text-classification": XLMForSequenceClassification, "text-generation": XLMWithLMHeadModel, "token-classification": XLMForTokenClassification, "zero-shot": XLMForSequenceClassification, } if is_torch_available() else {} ) def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Any , UpperCAmelCase_ : Any): """simple docstring""" if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith('Fast') ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : int , UpperCAmelCase_ : Dict=False): """simple docstring""" a : List[Any] = super()._prepare_for_class(UpperCAmelCase_ , UpperCAmelCase_ , return_labels=UpperCAmelCase_) if return_labels: if model_class.__name__ == "XLMForQuestionAnswering": a : Optional[int] = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=UpperCAmelCase_) a : str = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=UpperCAmelCase_) return inputs_dict def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : List[Any] = XLMModelTester(self) a : Tuple = ConfigTester(self , config_class=UpperCAmelCase_ , emb_dim=3_7) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_model(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_lm_head(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_simple_qa(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_qa(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_sequence_classif(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_token_classif(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" a : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_for_multiple_choice(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : int , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Any=False , UpperCAmelCase_ : Optional[Any]=1): """simple docstring""" self.assertIsInstance(UpperCAmelCase_ , UpperCAmelCase_) self.assertListEqual( [isinstance(UpperCAmelCase_ , UpperCAmelCase_) for iter_attentions in attentions] , [True] * len(UpperCAmelCase_)) self.assertEqual(len(UpperCAmelCase_) , (max_length - min_length) * num_beam_groups) for idx, iter_attentions in enumerate(UpperCAmelCase_): # adds PAD dummy token a : List[str] = min_length + idx + 1 a : Optional[Any] = min_length + idx + 1 a : Optional[int] = ( batch_size * num_beam_groups, config.num_attention_heads, tgt_len, src_len, ) # check attn size self.assertListEqual( [layer_attention.shape for layer_attention in iter_attentions] , [expected_shape] * len(UpperCAmelCase_)) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : str=False , UpperCAmelCase_ : Union[str, Any]=1): """simple docstring""" self.assertIsInstance(UpperCAmelCase_ , UpperCAmelCase_) self.assertListEqual( [isinstance(UpperCAmelCase_ , UpperCAmelCase_) for iter_hidden_states in hidden_states] , [True] * len(UpperCAmelCase_) , ) self.assertEqual(len(UpperCAmelCase_) , (max_length - min_length) * num_beam_groups) for idx, iter_hidden_states in enumerate(UpperCAmelCase_): # adds PAD dummy token a : int = min_length + idx + 1 a : Any = (batch_size * num_beam_groups, seq_len, config.hidden_size) # check hidden size self.assertListEqual( [layer_hidden_states.shape for layer_hidden_states in iter_hidden_states] , [expected_shape] * len(UpperCAmelCase_) , ) pass @slow def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a : Union[str, Any] = XLMModel.from_pretrained(UpperCAmelCase_) self.assertIsNotNone(UpperCAmelCase_) @require_torch class UpperCamelCase ( unittest.TestCase ): """simple docstring""" @slow def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" a : Dict = XLMWithLMHeadModel.from_pretrained('xlm-mlm-en-2048') model.to(UpperCAmelCase_) a : List[Any] = torch.tensor([[1_4, 4_4_7]] , dtype=torch.long , device=UpperCAmelCase_) # the president a : Dict = [ 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, ] # the president the president the president the president the president the president the president the president the president the president # TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference a : Optional[Any] = model.generate(UpperCAmelCase_ , do_sample=UpperCAmelCase_) self.assertListEqual(output_ids[0].cpu().numpy().tolist() , UpperCAmelCase_)
345
0
'''simple docstring''' from __future__ import annotations UpperCamelCase : int = """#""" class UpperCamelCase : """simple docstring""" def __init__( self : Optional[int]): """simple docstring""" a : Dict = {} def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : str): """simple docstring""" a : List[Any] = self._trie for char in text: if char not in trie: a : Any = {} a : Union[str, Any] = trie[char] a : Tuple = True def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : List[str]): """simple docstring""" a : str = self._trie for char in prefix: if char in trie: a : Tuple = trie[char] else: return [] return self._elements(_a) def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : List[str]): """simple docstring""" a : Any = [] for c, v in d.items(): a : List[Any] = [' '] if c == END else [(c + s) for s in self._elements(_a)] result.extend(_a) return tuple(_a) UpperCamelCase : Optional[int] = Trie() UpperCamelCase : Optional[int] = ("""depart""", """detergent""", """daring""", """dog""", """deer""", """deal""") for word in words: trie.insert_word(word) def SCREAMING_SNAKE_CASE__ ( snake_case : str ) -> tuple: """simple docstring""" a : Union[str, Any] = trie.find_word(lowerCAmelCase__ ) return tuple(string + word for word in suffixes ) def SCREAMING_SNAKE_CASE__ ( ) -> None: """simple docstring""" print(autocomplete_using_trie('de' ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
361
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, is_vision_available, ) UpperCamelCase : List[str] = {"""processing_layoutxlm""": ["""LayoutXLMProcessor"""]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Any = ["""LayoutXLMTokenizer"""] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Optional[int] = ["""LayoutXLMTokenizerFast"""] if TYPE_CHECKING: from .processing_layoutxlm import LayoutXLMProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutxlm import LayoutXLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutxlm_fast import LayoutXLMTokenizerFast else: import sys UpperCamelCase : Dict = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
345
0
'''simple docstring''' import unittest from transformers import PegasusConfig, PegasusTokenizer, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_configuration_common import ConfigTester from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor if is_flax_available(): import os # The slow tests are often failing with OOM error on GPU # This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed # but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html UpperCamelCase : int = """platform""" import jax import jax.numpy as jnp import numpy as np from transformers import FlaxPegasusForConditionalGeneration, FlaxPegasusModel @require_flax class UpperCamelCase : """simple docstring""" A : Tuple = PegasusConfig A : str = {} A : Dict = "gelu" def __init__( self : List[Any] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : List[str]=1_3 , UpperCAmelCase_ : List[Any]=7 , UpperCAmelCase_ : int=True , UpperCAmelCase_ : Union[str, Any]=False , UpperCAmelCase_ : List[Any]=9_9 , UpperCAmelCase_ : str=3_2 , UpperCAmelCase_ : Optional[Any]=5 , UpperCAmelCase_ : Dict=4 , UpperCAmelCase_ : List[str]=3_7 , UpperCAmelCase_ : List[str]=0.1 , UpperCAmelCase_ : Any=0.1 , UpperCAmelCase_ : List[str]=2_0 , UpperCAmelCase_ : List[Any]=2 , UpperCAmelCase_ : int=1 , UpperCAmelCase_ : int=0 , ): """simple docstring""" a : Dict = parent a : List[str] = batch_size a : Dict = seq_length a : List[str] = is_training a : Optional[int] = use_labels a : List[str] = vocab_size a : int = hidden_size a : Dict = num_hidden_layers a : Optional[int] = num_attention_heads a : Dict = intermediate_size a : Optional[Any] = hidden_dropout_prob a : Optional[int] = attention_probs_dropout_prob a : str = max_position_embeddings a : Optional[Any] = eos_token_id a : List[Any] = pad_token_id a : List[str] = bos_token_id def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : Tuple = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size).clip(3 , self.vocab_size) a : Union[str, Any] = np.expand_dims(np.array([self.eos_token_id] * self.batch_size) , 1) a : List[Any] = np.concatenate([input_ids, eos_tensor] , axis=1) a : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size) a : Union[str, Any] = self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) a : Optional[int] = prepare_pegasus_inputs_dict(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_) return config, inputs_dict def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : str , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Tuple): """simple docstring""" a : List[str] = 2_0 a : Any = model_class_name(UpperCAmelCase_) a : Tuple = model.encode(inputs_dict['input_ids']) a : Optional[Any] = ( inputs_dict['decoder_input_ids'], inputs_dict['decoder_attention_mask'], ) a : List[Any] = model.init_cache(decoder_input_ids.shape[0] , UpperCAmelCase_ , UpperCAmelCase_) a : Dict = jnp.ones((decoder_input_ids.shape[0], max_decoder_length) , dtype='i4') a : Dict = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1)[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) a : List[Any] = model.decode( decoder_input_ids[:, :-1] , UpperCAmelCase_ , decoder_attention_mask=UpperCAmelCase_ , past_key_values=UpperCAmelCase_ , decoder_position_ids=UpperCAmelCase_ , ) a : Union[str, Any] = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype='i4') a : Union[str, Any] = model.decode( decoder_input_ids[:, -1:] , UpperCAmelCase_ , decoder_attention_mask=UpperCAmelCase_ , past_key_values=outputs_cache.past_key_values , decoder_position_ids=UpperCAmelCase_ , ) a : Tuple = model.decode(UpperCAmelCase_ , UpperCAmelCase_) a : Optional[Any] = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]))) self.parent.assertTrue(diff < 1e-3 , msg=f"""Max diff is {diff}""") def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : str , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Union[str, Any]): """simple docstring""" a : Dict = 2_0 a : Optional[Any] = model_class_name(UpperCAmelCase_) a : Any = model.encode(inputs_dict['input_ids']) a : int = ( inputs_dict['decoder_input_ids'], inputs_dict['decoder_attention_mask'], ) a : List[Any] = jnp.concatenate( [ decoder_attention_mask, jnp.zeros((decoder_attention_mask.shape[0], max_decoder_length - decoder_attention_mask.shape[1])), ] , axis=-1 , ) a : List[str] = model.init_cache(decoder_input_ids.shape[0] , UpperCAmelCase_ , UpperCAmelCase_) a : str = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1)[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) a : str = model.decode( decoder_input_ids[:, :-1] , UpperCAmelCase_ , decoder_attention_mask=UpperCAmelCase_ , past_key_values=UpperCAmelCase_ , decoder_position_ids=UpperCAmelCase_ , ) a : List[Any] = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype='i4') a : int = model.decode( decoder_input_ids[:, -1:] , UpperCAmelCase_ , past_key_values=outputs_cache.past_key_values , decoder_attention_mask=UpperCAmelCase_ , decoder_position_ids=UpperCAmelCase_ , ) a : Optional[Any] = model.decode(UpperCAmelCase_ , UpperCAmelCase_ , decoder_attention_mask=UpperCAmelCase_) a : Union[str, Any] = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]))) self.parent.assertTrue(diff < 1e-3 , msg=f"""Max diff is {diff}""") def SCREAMING_SNAKE_CASE__ ( snake_case : int , snake_case : str , snake_case : Dict , snake_case : Optional[Any]=None , snake_case : List[Any]=None , ) -> Dict: """simple docstring""" if attention_mask is None: a : Any = np.not_equal(lowercase__ , config.pad_token_id ).astype(np.inta ) if decoder_attention_mask is None: a : Tuple = np.concatenate( [ np.ones(decoder_input_ids[:, :1].shape , dtype=np.inta ), np.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ).astype(np.inta ), ] , axis=-1 , ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, } @require_flax class UpperCamelCase ( a_ , unittest.TestCase ): """simple docstring""" A : List[str] = ( ( FlaxPegasusForConditionalGeneration, FlaxPegasusModel, ) if is_flax_available() else () ) A : Optional[int] = (FlaxPegasusForConditionalGeneration,) if is_flax_available() else () A : List[str] = True A : Optional[Any] = False A : str = False A : Dict = False def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" a : str = FlaxPegasusModelTester(self) a : str = ConfigTester(self , config_class=UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" a : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" a : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward_with_attn_mask(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__): a : Tuple = self._prepare_for_class(UpperCAmelCase_ , UpperCAmelCase_) a : List[Any] = model_class(UpperCAmelCase_) @jax.jit def encode_jitted(UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : str=None , **UpperCAmelCase_ : Optional[int]): return model.encode(input_ids=UpperCAmelCase_ , attention_mask=UpperCAmelCase_) with self.subTest('JIT Enabled'): a : Any = encode_jitted(**UpperCAmelCase_).to_tuple() with self.subTest('JIT Disabled'): with jax.disable_jit(): a : Tuple = encode_jitted(**UpperCAmelCase_).to_tuple() self.assertEqual(len(UpperCAmelCase_) , len(UpperCAmelCase_)) for jitted_output, output in zip(UpperCAmelCase_ , UpperCAmelCase_): self.assertEqual(jitted_output.shape , output.shape) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" a : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__): a : Any = model_class(UpperCAmelCase_) a : Optional[Any] = model.encode(inputs_dict['input_ids'] , inputs_dict['attention_mask']) a : int = { 'decoder_input_ids': inputs_dict['decoder_input_ids'], 'decoder_attention_mask': inputs_dict['decoder_attention_mask'], 'encoder_outputs': encoder_outputs, } @jax.jit def decode_jitted(UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : str): return model.decode( decoder_input_ids=UpperCAmelCase_ , decoder_attention_mask=UpperCAmelCase_ , encoder_outputs=UpperCAmelCase_ , ) with self.subTest('JIT Enabled'): a : Any = decode_jitted(**UpperCAmelCase_).to_tuple() with self.subTest('JIT Disabled'): with jax.disable_jit(): a : Tuple = decode_jitted(**UpperCAmelCase_).to_tuple() self.assertEqual(len(UpperCAmelCase_) , len(UpperCAmelCase_)) for jitted_output, output in zip(UpperCAmelCase_ , UpperCAmelCase_): self.assertEqual(jitted_output.shape , output.shape) @slow def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" for model_class_name in self.all_model_classes: a : Optional[int] = model_class_name.from_pretrained('google/pegasus-large' , from_pt=UpperCAmelCase_) a : Optional[Any] = np.ones((1, 1)) a : Optional[int] = model(UpperCAmelCase_) self.assertIsNotNone(UpperCAmelCase_) @slow def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : int = FlaxPegasusForConditionalGeneration.from_pretrained('google/pegasus-xsum') a : Optional[Any] = PegasusTokenizer.from_pretrained('google/pegasus-xsum') a : Optional[int] = [ ' PG&E stated it scheduled the blackouts in response to forecasts for high winds amid dry conditions. The aim is to reduce the risk of wildfires. Nearly 800 thousand customers were scheduled to be affected by the shutoffs which were expected to last through at least midday tomorrow.', ' The London trio are up for best UK act and best album, as well as getting two nominations in the best song category."We got told like this morning \'Oh I think you\'re nominated\'", said Dappy."And I was like \'Oh yeah, which one?\' And now we\'ve got nominated for four awards. I mean, wow!"Bandmate Fazer added: "We thought it\'s best of us to come down and mingle with everyone and say hello to the cameras. And now we find we\'ve got four nominations."The band have two shots at the best song prize, getting the nod for their Tynchy Stryder collaboration Number One, and single Strong Again.Their album Uncle B will also go up against records by the likes of Beyonce and Kanye West.N-Dubz picked up the best newcomer Mobo in 2007, but female member Tulisa said they wouldn\'t be too disappointed if they didn\'t win this time around."At the end of the day we\'re grateful to be where we are in our careers."If it don\'t happen then it don\'t happen - live to fight another day and keep on making albums and hits for the fans."Dappy also revealed they could be performing live several times on the night.The group will be doing Number One and also a possible rendition of the War Child single, I Got Soul.The charity song is a re-working of The Killers\' All These Things That I\'ve Done and is set to feature artists like Chipmunk, Ironik and Pixie Lott.This year\'s Mobos will be held outside of London for the first time, in Glasgow on 30 September.N-Dubz said they were looking forward to performing for their Scottish fans and boasted about their recent shows north of the border."We just done Edinburgh the other day," said Dappy."We smashed up an N-Dubz show over there. We done Aberdeen about three or four months ago - we smashed up that show over there! Everywhere we go we smash it up!" ', ] a : Dict = [ 'California\'s largest electricity provider has turned off power to hundreds of thousands of customers.', 'Pop group N-Dubz have revealed they were surprised to get four nominations for this year\'s Mobo Awards.', ] a : int = tokenizer(UpperCAmelCase_ , return_tensors='np' , truncation=UpperCAmelCase_ , max_length=5_1_2 , padding=UpperCAmelCase_) a : Tuple = model.generate(**UpperCAmelCase_ , num_beams=2).sequences a : Dict = tokenizer.batch_decode(UpperCAmelCase_ , skip_special_tokens=UpperCAmelCase_) assert tgt_text == decoded
362
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCamelCase : Tuple = { """configuration_pegasus_x""": ["""PEGASUS_X_PRETRAINED_CONFIG_ARCHIVE_MAP""", """PegasusXConfig"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Union[str, Any] = [ """PEGASUS_X_PRETRAINED_MODEL_ARCHIVE_LIST""", """PegasusXForConditionalGeneration""", """PegasusXModel""", """PegasusXPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_pegasus_x import PEGASUS_X_PRETRAINED_CONFIG_ARCHIVE_MAP, PegasusXConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_pegasus_x import ( PEGASUS_X_PRETRAINED_MODEL_ARCHIVE_LIST, PegasusXForConditionalGeneration, PegasusXModel, PegasusXPreTrainedModel, ) else: import sys UpperCamelCase : List[str] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
345
0
'''simple docstring''' import unittest from transformers import 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 UpperCamelCase : """simple docstring""" @staticmethod def SCREAMING_SNAKE_CASE_ ( *UpperCAmelCase_ : str , **UpperCAmelCase_ : List[str]): """simple docstring""" pass @is_pipeline_test @require_vision class UpperCamelCase ( unittest.TestCase ): """simple docstring""" @require_torch def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" a : Dict = pipeline( model='hf-internal-testing/tiny-random-clip-zero-shot-image-classification' , ) a : Optional[Any] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png') a : Optional[Any] = image_classifier(UpperCAmelCase_ , candidate_labels=['a', 'b', 'c']) # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across # python and torch versions. self.assertIn( nested_simplify(UpperCAmelCase_) , [ [{'score': 0.3_33, 'label': 'a'}, {'score': 0.3_33, 'label': 'b'}, {'score': 0.3_33, 'label': 'c'}], [{'score': 0.3_33, 'label': 'a'}, {'score': 0.3_33, 'label': 'c'}, {'score': 0.3_33, 'label': 'b'}], ] , ) a : Optional[int] = image_classifier([image] * 5 , candidate_labels=['A', 'B', 'C'] , batch_size=2) self.assertEqual( nested_simplify(UpperCAmelCase_) , [ [ {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, ], [ {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, ], [ {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, ], [ {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, ], [ {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, ], ] , ) @require_tf def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : Union[str, Any] = pipeline( model='hf-internal-testing/tiny-random-clip-zero-shot-image-classification' , framework='tf') a : Tuple = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png') a : Tuple = image_classifier(UpperCAmelCase_ , candidate_labels=['a', 'b', 'c']) self.assertEqual( nested_simplify(UpperCAmelCase_) , [{'score': 0.3_33, 'label': 'a'}, {'score': 0.3_33, 'label': 'b'}, {'score': 0.3_33, 'label': 'c'}] , ) a : Union[str, Any] = image_classifier([image] * 5 , candidate_labels=['A', 'B', 'C'] , batch_size=2) self.assertEqual( nested_simplify(UpperCAmelCase_) , [ [ {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, ], [ {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, ], [ {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, ], [ {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, ], [ {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, {'score': 0.3_33, 'label': ANY(UpperCAmelCase_)}, ], ] , ) @slow @require_torch def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" a : Optional[Any] = pipeline( task='zero-shot-image-classification' , model='openai/clip-vit-base-patch32' , ) # This is an image of 2 cats with remotes and no planes a : Union[str, Any] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png') a : Optional[Any] = image_classifier(UpperCAmelCase_ , candidate_labels=['cat', 'plane', 'remote']) self.assertEqual( nested_simplify(UpperCAmelCase_) , [ {'score': 0.5_11, 'label': 'remote'}, {'score': 0.4_85, 'label': 'cat'}, {'score': 0.0_04, 'label': 'plane'}, ] , ) a : Optional[Any] = image_classifier([image] * 5 , candidate_labels=['cat', 'plane', 'remote'] , batch_size=2) self.assertEqual( nested_simplify(UpperCAmelCase_) , [ [ {'score': 0.5_11, 'label': 'remote'}, {'score': 0.4_85, 'label': 'cat'}, {'score': 0.0_04, 'label': 'plane'}, ], ] * 5 , ) @slow @require_tf def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : Tuple = pipeline( task='zero-shot-image-classification' , model='openai/clip-vit-base-patch32' , framework='tf') # This is an image of 2 cats with remotes and no planes a : int = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png') a : Dict = image_classifier(UpperCAmelCase_ , candidate_labels=['cat', 'plane', 'remote']) self.assertEqual( nested_simplify(UpperCAmelCase_) , [ {'score': 0.5_11, 'label': 'remote'}, {'score': 0.4_85, 'label': 'cat'}, {'score': 0.0_04, 'label': 'plane'}, ] , ) a : int = image_classifier([image] * 5 , candidate_labels=['cat', 'plane', 'remote'] , batch_size=2) self.assertEqual( nested_simplify(UpperCAmelCase_) , [ [ {'score': 0.5_11, 'label': 'remote'}, {'score': 0.4_85, 'label': 'cat'}, {'score': 0.0_04, 'label': 'plane'}, ], ] * 5 , )
363
'''simple docstring''' import math from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase : str = logging.get_logger(__name__) UpperCamelCase : List[str] = { """facebook/data2vec-base-960h""": """https://huggingface.co/facebook/data2vec-audio-base-960h/resolve/main/config.json""", # See all Data2VecAudio models at https://huggingface.co/models?filter=data2vec-audio } class UpperCamelCase ( a_ ): """simple docstring""" A : Optional[int] = "data2vec-audio" def __init__( self : Dict , UpperCAmelCase_ : Optional[int]=3_2 , UpperCAmelCase_ : Union[str, Any]=7_6_8 , UpperCAmelCase_ : Dict=1_2 , UpperCAmelCase_ : str=1_2 , UpperCAmelCase_ : Optional[Any]=3_0_7_2 , UpperCAmelCase_ : Dict="gelu" , UpperCAmelCase_ : Union[str, Any]=0.1 , UpperCAmelCase_ : Optional[int]=0.1 , UpperCAmelCase_ : int=0.1 , UpperCAmelCase_ : Union[str, Any]=0.0 , UpperCAmelCase_ : Union[str, Any]=0.1 , UpperCAmelCase_ : Dict=0.1 , UpperCAmelCase_ : Optional[Any]=0.02 , UpperCAmelCase_ : str=1e-5 , UpperCAmelCase_ : List[Any]="gelu" , UpperCAmelCase_ : Optional[Any]=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2) , UpperCAmelCase_ : Dict=(5, 2, 2, 2, 2, 2, 2) , UpperCAmelCase_ : int=(1_0, 3, 3, 3, 3, 2, 2) , UpperCAmelCase_ : List[Any]=False , UpperCAmelCase_ : List[Any]=1_6 , UpperCAmelCase_ : Optional[Any]=1_9 , UpperCAmelCase_ : int=5 , UpperCAmelCase_ : Any=0.05 , UpperCAmelCase_ : Dict=1_0 , UpperCAmelCase_ : Optional[Any]=2 , UpperCAmelCase_ : Optional[int]=0.0 , UpperCAmelCase_ : Tuple=1_0 , UpperCAmelCase_ : int=0 , UpperCAmelCase_ : Any="sum" , UpperCAmelCase_ : Optional[int]=False , UpperCAmelCase_ : Dict=False , UpperCAmelCase_ : Optional[int]=2_5_6 , UpperCAmelCase_ : Any=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0) , UpperCAmelCase_ : Optional[Any]=(5, 3, 3, 1, 1) , UpperCAmelCase_ : Optional[int]=(1, 2, 3, 1, 1) , UpperCAmelCase_ : int=5_1_2 , UpperCAmelCase_ : str=0 , UpperCAmelCase_ : Tuple=1 , UpperCAmelCase_ : Union[str, Any]=2 , UpperCAmelCase_ : List[str]=False , UpperCAmelCase_ : Any=3 , UpperCAmelCase_ : Dict=2 , UpperCAmelCase_ : int=3 , UpperCAmelCase_ : List[Any]=None , **UpperCAmelCase_ : int , ): """simple docstring""" super().__init__(**UpperCAmelCase_ , pad_token_id=UpperCAmelCase_ , bos_token_id=UpperCAmelCase_ , eos_token_id=UpperCAmelCase_) a : List[Any] = hidden_size a : Any = feat_extract_activation a : Any = list(UpperCAmelCase_) a : Optional[int] = list(UpperCAmelCase_) a : Dict = list(UpperCAmelCase_) a : Tuple = conv_bias a : str = num_conv_pos_embeddings a : Dict = num_conv_pos_embedding_groups a : Optional[Any] = conv_pos_kernel_size a : Any = len(self.conv_dim) a : Tuple = num_hidden_layers a : Any = intermediate_size a : Any = hidden_act a : Dict = num_attention_heads a : Dict = hidden_dropout a : Union[str, Any] = attention_dropout a : Dict = activation_dropout a : Optional[int] = feat_proj_dropout a : Tuple = final_dropout a : Union[str, Any] = layerdrop a : Tuple = layer_norm_eps a : Dict = initializer_range a : Tuple = vocab_size a : int = use_weighted_layer_sum if ( (len(self.conv_stride) != self.num_feat_extract_layers) or (len(self.conv_kernel) != self.num_feat_extract_layers) or (len(self.conv_dim) != self.num_feat_extract_layers) ): raise ValueError( 'Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==' ' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =' f""" {len(self.conv_dim)}`, `len(config.conv_stride) = {len(self.conv_stride)}`,""" f""" `len(config.conv_kernel) = {len(self.conv_kernel)}`.""") # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 a : List[str] = mask_time_prob a : int = mask_time_length a : Optional[int] = mask_time_min_masks a : Dict = mask_feature_prob a : List[str] = mask_feature_length a : str = mask_feature_min_masks # ctc loss a : str = ctc_loss_reduction a : Optional[Any] = ctc_zero_infinity # adapter a : List[str] = add_adapter a : Optional[Any] = adapter_kernel_size a : int = adapter_stride a : str = num_adapter_layers a : Optional[Any] = output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. a : str = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. a : List[Any] = list(UpperCAmelCase_) a : List[str] = list(UpperCAmelCase_) a : str = list(UpperCAmelCase_) a : Optional[Any] = xvector_output_dim @property def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" return math.prod(self.conv_stride)
345
0
'''simple docstring''' def SCREAMING_SNAKE_CASE__ ( snake_case : Optional[int] ) -> Optional[int]: """simple docstring""" a : Optional[int] = [0] * len(a_ ) a : Optional[int] = [] a : List[Any] = [1] * len(a_ ) for values in graph.values(): for i in values: indegree[i] += 1 for i in range(len(a_ ) ): if indegree[i] == 0: queue.append(a_ ) while queue: a : str = queue.pop(0 ) for x in graph[vertex]: indegree[x] -= 1 if long_dist[vertex] + 1 > long_dist[x]: a : Optional[int] = long_dist[vertex] + 1 if indegree[x] == 0: queue.append(a_ ) print(max(a_ ) ) # Adjacency list of Graph UpperCamelCase : Any = {0: [2, 3, 4], 1: [2, 7], 2: [5], 3: [5, 7], 4: [7], 5: [6], 6: [7], 7: []} longest_distance(graph)
364
'''simple docstring''' import logging from transformers.configuration_utils import PretrainedConfig UpperCamelCase : Optional[Any] = logging.getLogger(__name__) class UpperCamelCase ( a_ ): """simple docstring""" A : Tuple = "masked_bert" def __init__( self : Tuple , UpperCAmelCase_ : List[Any]=3_0_5_2_2 , UpperCAmelCase_ : str=7_6_8 , UpperCAmelCase_ : Optional[Any]=1_2 , UpperCAmelCase_ : Optional[int]=1_2 , UpperCAmelCase_ : Union[str, Any]=3_0_7_2 , UpperCAmelCase_ : Union[str, Any]="gelu" , UpperCAmelCase_ : Optional[Any]=0.1 , UpperCAmelCase_ : List[Any]=0.1 , UpperCAmelCase_ : Optional[int]=5_1_2 , UpperCAmelCase_ : List[str]=2 , UpperCAmelCase_ : str=0.02 , UpperCAmelCase_ : Optional[Any]=1e-12 , UpperCAmelCase_ : Dict=0 , UpperCAmelCase_ : Dict="topK" , UpperCAmelCase_ : str="constant" , UpperCAmelCase_ : Optional[Any]=0.0 , **UpperCAmelCase_ : Optional[Any] , ): """simple docstring""" super().__init__(pad_token_id=UpperCAmelCase_ , **UpperCAmelCase_) a : Union[str, Any] = vocab_size a : List[Any] = hidden_size a : List[str] = num_hidden_layers a : Any = num_attention_heads a : Optional[Any] = hidden_act a : str = intermediate_size a : Dict = hidden_dropout_prob a : Any = attention_probs_dropout_prob a : Any = max_position_embeddings a : Dict = type_vocab_size a : List[str] = initializer_range a : int = layer_norm_eps a : Dict = pruning_method a : List[str] = mask_init a : Union[str, Any] = mask_scale
345
0
'''simple docstring''' import unittest import numpy as np import requests 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 from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11 else: UpperCamelCase : Union[str, Any] = False if is_vision_available(): from PIL import Image from transformers import PixaStructImageProcessor class UpperCamelCase ( unittest.TestCase ): """simple docstring""" def __init__( self : Dict , UpperCAmelCase_ : Any , UpperCAmelCase_ : str=7 , UpperCAmelCase_ : List[str]=3 , UpperCAmelCase_ : Optional[Any]=1_8 , UpperCAmelCase_ : int=3_0 , UpperCAmelCase_ : int=4_0_0 , UpperCAmelCase_ : Optional[Any]=None , UpperCAmelCase_ : Optional[Any]=True , UpperCAmelCase_ : Any=True , UpperCAmelCase_ : Union[str, Any]=None , ): """simple docstring""" a : Tuple = size if size is not None else {'''height''': 2_0, '''width''': 2_0} a : Optional[Any] = parent a : Optional[int] = batch_size a : List[Any] = num_channels a : List[Any] = image_size a : int = min_resolution a : Tuple = max_resolution a : Tuple = size a : Tuple = do_normalize a : Dict = do_convert_rgb a : List[str] = [5_1_2, 1_0_2_4, 2_0_4_8, 4_0_9_6] a : Any = patch_size if patch_size is not None else {'''height''': 1_6, '''width''': 1_6} def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb} def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : List[Any] = '''https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg''' a : int = Image.open(requests.get(_a , stream=_a).raw).convert('RGB') return raw_image @unittest.skipIf( not is_torch_greater_or_equal_than_1_11 , reason="`Pix2StructImageProcessor` requires `torch>=1.11.0`." , ) @require_torch @require_vision class UpperCamelCase ( __lowercase , unittest.TestCase ): """simple docstring""" A : List[Any] = PixaStructImageProcessor if is_vision_available() else None def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : Optional[int] = PixaStructImageProcessingTester(self) @property def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" a : Union[str, Any] = self.image_processing_class(**self.image_processor_dict) self.assertTrue(hasattr(_a , 'do_normalize')) self.assertTrue(hasattr(_a , 'do_convert_rgb')) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" a : List[str] = self.image_processor_tester.prepare_dummy_image() a : Optional[int] = self.image_processing_class(**self.image_processor_dict) a : Any = 2_0_4_8 a : Dict = image_processor(_a , return_tensors='pt' , max_patches=_a) self.assertTrue(torch.allclose(inputs.flattened_patches.mean() , torch.tensor(0.06_06) , atol=1e-3 , rtol=1e-3)) def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : List[str] = self.image_processing_class(**self.image_processor_dict) # create random PIL images a : List[str] = 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 : Optional[Any] = ( (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width''']) * self.image_processor_tester.num_channels ) + 2 for max_patch in self.image_processor_tester.max_patches: # Test not batched input a : str = image_processor( image_inputs[0] , return_tensors='pt' , max_patches=_a).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched a : Union[str, Any] = image_processor( _a , return_tensors='pt' , max_patches=_a).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" a : Optional[int] = self.image_processing_class(**self.image_processor_dict) # create random PIL images a : Union[str, Any] = 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 : str = ( (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width''']) * self.image_processor_tester.num_channels ) + 2 a : str = True for max_patch in self.image_processor_tester.max_patches: # Test not batched input with self.assertRaises(_a): a : Dict = image_processor( image_inputs[0] , return_tensors='pt' , max_patches=_a).flattened_patches a : Any = '''Hello''' a : Optional[Any] = image_processor( image_inputs[0] , return_tensors='pt' , max_patches=_a , header_text=_a).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched a : int = image_processor( _a , return_tensors='pt' , max_patches=_a , header_text=_a).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : List[str] = self.image_processing_class(**self.image_processor_dict) # create random numpy tensors a : Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , numpify=_a) for image in image_inputs: self.assertIsInstance(_a , np.ndarray) a : str = ( (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width''']) * self.image_processor_tester.num_channels ) + 2 for max_patch in self.image_processor_tester.max_patches: # Test not batched input a : Optional[Any] = image_processor( image_inputs[0] , return_tensors='pt' , max_patches=_a).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched a : List[Any] = image_processor( _a , return_tensors='pt' , max_patches=_a).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , ) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : int = self.image_processing_class(**self.image_processor_dict) # create random PyTorch tensors a : Dict = 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 : List[str] = ( (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width''']) * self.image_processor_tester.num_channels ) + 2 for max_patch in self.image_processor_tester.max_patches: # Test not batched input a : Any = image_processor( image_inputs[0] , return_tensors='pt' , max_patches=_a).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched a : Any = image_processor( _a , return_tensors='pt' , max_patches=_a).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , ) @unittest.skipIf( not is_torch_greater_or_equal_than_1_11 , reason="`Pix2StructImageProcessor` requires `torch>=1.11.0`." , ) @require_torch @require_vision class UpperCamelCase ( __lowercase , unittest.TestCase ): """simple docstring""" A : List[str] = PixaStructImageProcessor if is_vision_available() else None def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : Any = PixaStructImageProcessingTester(self , num_channels=4) a : Tuple = 3 @property def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : List[Any] = self.image_processing_class(**self.image_processor_dict) self.assertTrue(hasattr(_a , 'do_normalize')) self.assertTrue(hasattr(_a , 'do_convert_rgb')) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" a : List[Any] = self.image_processing_class(**self.image_processor_dict) # create random PIL images a : Tuple = 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 : List[str] = ( (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width''']) * (self.image_processor_tester.num_channels - 1) ) + 2 for max_patch in self.image_processor_tester.max_patches: # Test not batched input a : Dict = image_processor( image_inputs[0] , return_tensors='pt' , max_patches=_a).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched a : str = image_processor( _a , return_tensors='pt' , max_patches=_a).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , )
365
'''simple docstring''' import copy import random from transformers import CLIPTokenizer class UpperCamelCase ( a_ ): """simple docstring""" def __init__( self : Union[str, Any] , *UpperCAmelCase_ : Dict , **UpperCAmelCase_ : Any): """simple docstring""" super().__init__(*UpperCAmelCase_ , **UpperCAmelCase_) a : str = {} def SCREAMING_SNAKE_CASE_ ( self : int , UpperCAmelCase_ : Tuple , *UpperCAmelCase_ : Optional[int] , **UpperCAmelCase_ : int): """simple docstring""" a : Dict = super().add_tokens(UpperCAmelCase_ , *UpperCAmelCase_ , **UpperCAmelCase_) if num_added_tokens == 0: raise ValueError( f"""The tokenizer already contains the token {placeholder_token}. Please pass a different""" ' `placeholder_token` that is not already in the tokenizer.') def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : Optional[int] , *UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : List[str]=1 , **UpperCAmelCase_ : Optional[int]): """simple docstring""" a : Any = [] if num_vec_per_token == 1: self.try_adding_tokens(UpperCAmelCase_ , *UpperCAmelCase_ , **UpperCAmelCase_) output.append(UpperCAmelCase_) else: a : int = [] for i in range(UpperCAmelCase_): a : Union[str, Any] = placeholder_token + f"""_{i}""" self.try_adding_tokens(UpperCAmelCase_ , *UpperCAmelCase_ , **UpperCAmelCase_) output.append(UpperCAmelCase_) # handle cases where there is a new placeholder token that contains the current placeholder token but is larger for token in self.token_map: if token in placeholder_token: raise ValueError( f"""The tokenizer already has placeholder token {token} that can get confused with""" f""" {placeholder_token}keep placeholder tokens independent""") a : Any = output def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Optional[int]=False , UpperCAmelCase_ : str=1.0): """simple docstring""" if isinstance(UpperCAmelCase_ , UpperCAmelCase_): a : Any = [] for i in range(len(UpperCAmelCase_)): output.append(self.replace_placeholder_tokens_in_text(text[i] , vector_shuffle=UpperCAmelCase_)) return output for placeholder_token in self.token_map: if placeholder_token in text: a : List[Any] = self.token_map[placeholder_token] a : int = tokens[: 1 + int(len(UpperCAmelCase_) * prop_tokens_to_load)] if vector_shuffle: a : List[Any] = copy.copy(UpperCAmelCase_) random.shuffle(UpperCAmelCase_) a : List[str] = text.replace(UpperCAmelCase_ , ' '.join(UpperCAmelCase_)) return text def __call__( self : Optional[int] , UpperCAmelCase_ : Any , *UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Union[str, Any]=False , UpperCAmelCase_ : Optional[int]=1.0 , **UpperCAmelCase_ : str): """simple docstring""" return super().__call__( self.replace_placeholder_tokens_in_text( UpperCAmelCase_ , vector_shuffle=UpperCAmelCase_ , prop_tokens_to_load=UpperCAmelCase_) , *UpperCAmelCase_ , **UpperCAmelCase_ , ) def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : Optional[int] , *UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Any=False , UpperCAmelCase_ : Optional[Any]=1.0 , **UpperCAmelCase_ : Dict): """simple docstring""" return super().encode( self.replace_placeholder_tokens_in_text( UpperCAmelCase_ , vector_shuffle=UpperCAmelCase_ , prop_tokens_to_load=UpperCAmelCase_) , *UpperCAmelCase_ , **UpperCAmelCase_ , )
345
0
'''simple docstring''' import unittest from parameterized import parameterized from transformers import LlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import LlamaForCausalLM, LlamaForSequenceClassification, LlamaModel, LlamaTokenizer class UpperCamelCase : """simple docstring""" def __init__( self : int , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Optional[Any]=1_3 , UpperCAmelCase_ : Tuple=7 , UpperCAmelCase_ : int=True , UpperCAmelCase_ : Optional[Any]=True , UpperCAmelCase_ : Optional[Any]=False , UpperCAmelCase_ : List[Any]=True , UpperCAmelCase_ : Union[str, Any]=9_9 , UpperCAmelCase_ : Dict=3_2 , UpperCAmelCase_ : Optional[int]=5 , UpperCAmelCase_ : str=4 , UpperCAmelCase_ : List[Any]=3_7 , UpperCAmelCase_ : Tuple="gelu" , UpperCAmelCase_ : Optional[int]=0.1 , UpperCAmelCase_ : Dict=0.1 , UpperCAmelCase_ : int=5_1_2 , UpperCAmelCase_ : List[Any]=1_6 , UpperCAmelCase_ : str=2 , UpperCAmelCase_ : Optional[Any]=0.02 , UpperCAmelCase_ : Tuple=3 , UpperCAmelCase_ : int=4 , UpperCAmelCase_ : Union[str, Any]=None , ): """simple docstring""" a : List[str] = parent a : Union[str, Any] = batch_size a : Tuple = seq_length a : str = is_training a : Union[str, Any] = use_input_mask a : List[Any] = use_token_type_ids a : Optional[Any] = use_labels a : str = vocab_size a : Union[str, Any] = hidden_size a : Optional[int] = num_hidden_layers a : str = num_attention_heads a : Optional[Any] = intermediate_size a : Optional[int] = hidden_act a : List[str] = hidden_dropout_prob a : List[str] = attention_probs_dropout_prob a : Tuple = max_position_embeddings a : Dict = type_vocab_size a : List[Any] = type_sequence_label_size a : List[Any] = initializer_range a : List[str] = num_labels a : str = num_choices a : List[Any] = scope def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" a : Any = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size) a : Dict = None if self.use_input_mask: a : str = random_attention_mask([self.batch_size, self.seq_length]) a : int = None if self.use_token_type_ids: a : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size) a : Optional[int] = None a : List[Any] = None a : Union[str, Any] = None if self.use_labels: a : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size) a : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels) a : Any = ids_tensor([self.batch_size] , self.num_choices) a : Dict = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" return LlamaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=__UpperCAmelCase , initializer_range=self.initializer_range , ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : str , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Optional[Any]): """simple docstring""" a : Optional[int] = LlamaModel(config=__UpperCAmelCase) model.to(__UpperCAmelCase) model.eval() a : Dict = model(__UpperCAmelCase , attention_mask=__UpperCAmelCase) a : Union[str, Any] = model(__UpperCAmelCase) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : int , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Optional[Any] , ): """simple docstring""" a : List[str] = True a : List[str] = LlamaModel(__UpperCAmelCase) model.to(__UpperCAmelCase) model.eval() a : List[Any] = model( __UpperCAmelCase , attention_mask=__UpperCAmelCase , encoder_hidden_states=__UpperCAmelCase , encoder_attention_mask=__UpperCAmelCase , ) a : Tuple = model( __UpperCAmelCase , attention_mask=__UpperCAmelCase , encoder_hidden_states=__UpperCAmelCase , ) a : Union[str, Any] = model(__UpperCAmelCase , attention_mask=__UpperCAmelCase) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Any , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : str , UpperCAmelCase_ : Dict , UpperCAmelCase_ : str , ): """simple docstring""" a : List[Any] = LlamaForCausalLM(config=__UpperCAmelCase) model.to(__UpperCAmelCase) model.eval() a : int = model(__UpperCAmelCase , attention_mask=__UpperCAmelCase , labels=__UpperCAmelCase) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size)) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : str , UpperCAmelCase_ : str , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : int , ): """simple docstring""" a : Optional[int] = True a : Any = True a : Tuple = LlamaForCausalLM(config=__UpperCAmelCase) model.to(__UpperCAmelCase) model.eval() # first forward pass a : Optional[int] = model( __UpperCAmelCase , attention_mask=__UpperCAmelCase , encoder_hidden_states=__UpperCAmelCase , encoder_attention_mask=__UpperCAmelCase , use_cache=__UpperCAmelCase , ) a : Union[str, Any] = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids a : List[Any] = ids_tensor((self.batch_size, 3) , config.vocab_size) a : List[Any] = ids_tensor((self.batch_size, 3) , vocab_size=2) # append to next input_ids and a : str = torch.cat([input_ids, next_tokens] , dim=-1) a : Union[str, Any] = torch.cat([input_mask, next_mask] , dim=-1) a : int = model( __UpperCAmelCase , attention_mask=__UpperCAmelCase , encoder_hidden_states=__UpperCAmelCase , encoder_attention_mask=__UpperCAmelCase , output_hidden_states=__UpperCAmelCase , )["""hidden_states"""][0] a : Dict = model( __UpperCAmelCase , attention_mask=__UpperCAmelCase , encoder_hidden_states=__UpperCAmelCase , encoder_attention_mask=__UpperCAmelCase , past_key_values=__UpperCAmelCase , output_hidden_states=__UpperCAmelCase , )["""hidden_states"""][0] # select random slice a : List[str] = ids_tensor((1,) , output_from_past.shape[-1]).item() a : Dict = output_from_no_past[:, -3:, random_slice_idx].detach() a : Tuple = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(__UpperCAmelCase , __UpperCAmelCase , atol=1e-3)) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Any = self.prepare_config_and_inputs() ( a ) : Any = config_and_inputs a : Optional[Any] = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_torch class UpperCamelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): """simple docstring""" A : Optional[int] = (LlamaModel, LlamaForCausalLM, LlamaForSequenceClassification) if is_torch_available() else () A : Any = (LlamaForCausalLM,) if is_torch_available() else () A : List[str] = ( { "feature-extraction": LlamaModel, "text-classification": LlamaForSequenceClassification, "text-generation": LlamaForCausalLM, "zero-shot": LlamaForSequenceClassification, } if is_torch_available() else {} ) A : Optional[int] = False A : List[str] = False def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : Tuple = LlamaModelTester(self) a : Tuple = ConfigTester(self , config_class=__UpperCAmelCase , hidden_size=3_7) def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" a : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__UpperCAmelCase) def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : Dict = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: a : str = type self.model_tester.create_and_check_model(*__UpperCAmelCase) def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" a : Tuple = self.model_tester.prepare_config_and_inputs_for_common() a : Any = 3 a : Optional[Any] = input_dict["""input_ids"""] a : int = input_ids.ne(1).to(__UpperCAmelCase) a : Union[str, Any] = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size) a : Dict = LlamaForSequenceClassification(__UpperCAmelCase) model.to(__UpperCAmelCase) model.eval() a : List[Any] = model(__UpperCAmelCase , attention_mask=__UpperCAmelCase , labels=__UpperCAmelCase) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels)) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : Dict = self.model_tester.prepare_config_and_inputs_for_common() a : Optional[int] = 3 a : Optional[Any] = """single_label_classification""" a : int = input_dict["""input_ids"""] a : List[Any] = input_ids.ne(1).to(__UpperCAmelCase) a : str = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size) a : Tuple = LlamaForSequenceClassification(__UpperCAmelCase) model.to(__UpperCAmelCase) model.eval() a : Tuple = model(__UpperCAmelCase , attention_mask=__UpperCAmelCase , labels=__UpperCAmelCase) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels)) def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" a : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() a : Optional[Any] = 3 a : str = """multi_label_classification""" a : Union[str, Any] = input_dict["""input_ids"""] a : int = input_ids.ne(1).to(__UpperCAmelCase) a : str = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size).to(torch.float) a : Dict = LlamaForSequenceClassification(__UpperCAmelCase) model.to(__UpperCAmelCase) model.eval() a : Tuple = model(__UpperCAmelCase , attention_mask=__UpperCAmelCase , labels=__UpperCAmelCase) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels)) @unittest.skip('LLaMA buffers include complex numbers, which breaks this test') def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" pass @parameterized.expand([('linear',), ('dynamic',)]) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : List[str]): """simple docstring""" a : Tuple = self.model_tester.prepare_config_and_inputs_for_common() a : List[Any] = ids_tensor([1, 1_0] , config.vocab_size) a : str = ids_tensor([1, int(config.max_position_embeddings * 1.5)] , config.vocab_size) set_seed(4_2) # Fixed seed at init time so the two models get the same random weights a : Optional[Any] = LlamaModel(__UpperCAmelCase) original_model.to(__UpperCAmelCase) original_model.eval() a : int = original_model(__UpperCAmelCase).last_hidden_state a : List[str] = original_model(__UpperCAmelCase).last_hidden_state set_seed(4_2) # Fixed seed at init time so the two models get the same random weights a : Dict = {"""type""": scaling_type, """factor""": 10.0} a : Optional[Any] = LlamaModel(__UpperCAmelCase) scaled_model.to(__UpperCAmelCase) scaled_model.eval() a : Optional[Any] = scaled_model(__UpperCAmelCase).last_hidden_state a : List[str] = scaled_model(__UpperCAmelCase).last_hidden_state # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original # maximum sequence length, so the outputs for the short input should match. if scaling_type == "dynamic": self.assertTrue(torch.allclose(__UpperCAmelCase , __UpperCAmelCase , atol=1e-5)) else: self.assertFalse(torch.allclose(__UpperCAmelCase , __UpperCAmelCase , atol=1e-5)) # The output should be different for long inputs self.assertFalse(torch.allclose(__UpperCAmelCase , __UpperCAmelCase , atol=1e-5)) @require_torch class UpperCamelCase ( unittest.TestCase ): """simple docstring""" @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!') @slow def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : Optional[int] = [1, 3_0_6, 4_6_5_8, 2_7_8, 6_5_9_3, 3_1_0, 2_8_3_4, 3_3_8] a : Optional[int] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-7b-hf' , device_map='auto') a : int = model(torch.tensor([input_ids])) # Expected mean on dim = -1 a : str = torch.tensor([[-6.65_50, -4.12_27, -4.98_59, -3.24_06, 0.82_62, -3.00_33, 1.29_64, -3.36_99]]) torch.testing.assert_close(out.mean(-1) , __UpperCAmelCase , atol=1e-2 , rtol=1e-2) # slicing logits[0, 0, 0:30] # fmt: off a : List[Any] = torch.tensor([-12.82_81, -7.44_53, -0.46_39, -8.06_25, -7.25_00, -8.00_00, -6.48_83, -7.76_95, -7.84_38, -7.03_12, -6.21_88, -7.13_28, -1.84_96, 1.99_61, -8.62_50, -6.72_27, -12.82_81, -6.94_92, -7.07_42, -7.78_52, -7.58_20, -7.90_62, -6.93_75, -7.98_05, -8.34_38, -8.15_62, -8.04_69, -7.62_50, -7.74_22, -7.33_98,]) # fmt: on torch.testing.assert_close(out[0, 0, :3_0] , __UpperCAmelCase , atol=1e-5 , rtol=1e-5) @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!') @slow def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : Any = [1, 3_0_6, 4_6_5_8, 2_7_8, 6_5_9_3, 3_1_0, 2_8_3_4, 3_3_8] a : int = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-hf' , device_map='auto') a : str = model(torch.tensor(__UpperCAmelCase)) # Expected mean on dim = -1 a : str = torch.tensor([[-2.06_22, -1.27_94, -1.16_38, -0.97_88, -1.46_03, -1.02_38, -1.78_93, -1.44_11]]) torch.testing.assert_close(out.mean(-1) , __UpperCAmelCase , atol=1e-2 , rtol=1e-2) # slicing logits[0, 0, 0:30] # fmt: off a : List[str] = torch.tensor([-8.14_06, -8.05_47, 2.74_61, -1.23_44, -0.14_48, -1.82_62, -1.00_20, -1.81_54, -1.68_95, -1.85_16, -2.35_74, -0.92_77, 3.75_98, 6.57_42, -1.29_98, -0.11_77, -8.14_06, -2.96_88, -2.91_99, -3.16_99, -3.52_54, -2.35_55, -2.79_88, -3.41_41, -2.82_62, -4.51_95, -3.33_79, -3.31_64, -2.78_32, -3.02_73]) # fmt: on torch.testing.assert_close(out[0, 0, :3_0] , __UpperCAmelCase , atol=1e-5 , rtol=1e-5) @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!') @slow def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" a : Union[str, Any] = [1, 3_0_6, 4_6_5_8, 2_7_8, 6_5_9_3, 3_1_0, 2_8_3_4, 3_3_8] a : Union[str, Any] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-chat-hf' , device_map='auto') a : Union[str, Any] = model(torch.tensor(__UpperCAmelCase)) # Expected mean on dim = -1 a : Dict = torch.tensor([[-0.85_62, -1.85_20, -0.75_51, -0.41_62, -1.51_61, -1.20_38, -2.48_23, -2.32_54]]) torch.testing.assert_close(out.mean(-1) , __UpperCAmelCase , atol=1e-2 , rtol=1e-2) # slicing logits[0, 0, 0:30] # fmt: off a : Any = torch.tensor([-2.22_27, 4.88_28, 0.90_23, -0.45_78, -0.78_71, -0.10_33, -0.62_21, -0.57_86, -0.78_03, -1.06_74, -1.29_20, -0.15_70, 0.80_08, 2.07_23, -0.94_97, 0.27_71, -2.22_27, -0.76_12, -1.43_46, -1.20_61, -1.64_26, -0.30_00, -0.71_39, -1.19_34, -1.86_91, -1.69_73, -1.59_47, -1.27_05, -0.35_23, -0.55_13]) # fmt: on torch.testing.assert_close(out.mean(-1) , __UpperCAmelCase , atol=1e-2 , rtol=1e-2) @unittest.skip( 'Logits are not exactly the same, once we fix the instabalities somehow, will update! Also it is gonna be a `too_slow` test') @slow def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : Any = [1, 3_0_6, 4_6_5_8, 2_7_8, 6_5_9_3, 3_1_0, 2_8_3_4, 3_3_8] a : str = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-70b-hf' , device_map='auto') a : List[Any] = model(torch.tensor(__UpperCAmelCase)) a : Dict = torch.tensor( [[-4.23_27, -3.33_60, -4.66_65, -4.76_31, -1.81_80, -3.41_70, -1.42_11, -3.18_10]] , dtype=torch.floataa) torch.testing.assert_close(out.mean(-1) , __UpperCAmelCase , atol=1e-2 , rtol=1e-2) # fmt: off a : List[str] = torch.tensor([-9.49_22, -3.95_51, 1.79_98, -5.67_58, -5.10_55, -5.89_84, -4.83_20, -6.80_86, -6.53_91, -5.61_72, -5.58_20, -5.53_52, 1.78_81, 3.62_89, -6.51_17, -3.47_85, -9.50_00, -6.03_52, -6.81_25, -6.01_95, -6.68_36, -5.47_27, -6.28_12, -6.03_91, -7.33_98, -7.42_97, -7.48_44, -6.58_20, -5.87_89, -5.53_12]) # fmt: on torch.testing.assert_close(out[0, 0, :3_0] , __UpperCAmelCase , atol=1e-5 , rtol=1e-5) @unittest.skip('Model is curently gated') @slow def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : Optional[int] = """Simply put, the theory of relativity states that 1) the laws of physics are the same everywhere in the universe and 2) the passage of time and the length of objects can vary depending on the observer\'s frame of reference.\n\nThe first part of the theory, that the laws of physics are the same everywhere, is known as the \"princi""" a : Dict = """Simply put, the theory of relativity states that """ a : int = LlamaTokenizer.from_pretrained('meta-llama/Llama-2-13b-chat-hf') a : int = tokenizer.encode(__UpperCAmelCase , return_tensors='pt') a : int = LlamaForCausalLM.from_pretrained( 'meta-llama/Llama-2-13b-chat-hf' , device_map='sequential' , use_safetensors=__UpperCAmelCase) # greedy generation outputs a : Tuple = model.generate(__UpperCAmelCase , max_new_tokens=6_4 , top_p=__UpperCAmelCase , temperature=1 , do_sample=__UpperCAmelCase) a : Optional[int] = tokenizer.decode(generated_ids[0] , skip_special_tokens=__UpperCAmelCase) self.assertEqual(__UpperCAmelCase , __UpperCAmelCase)
366
'''simple docstring''' import argparse import json import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import AutoImageProcessor, SwinConfig, SwinForImageClassification def SCREAMING_SNAKE_CASE__ ( snake_case : str ) -> Optional[Any]: """simple docstring""" a : Union[str, Any] = SwinConfig() a : Optional[int] = swin_name.split('_' ) a : Union[str, Any] = name_split[1] a : Dict = int(name_split[4] ) a : Union[str, Any] = int(name_split[3][-1] ) if model_size == "tiny": a : Optional[Any] = 96 a : Any = (2, 2, 6, 2) a : List[str] = (3, 6, 12, 24) elif model_size == "small": a : int = 96 a : List[str] = (2, 2, 18, 2) a : int = (3, 6, 12, 24) elif model_size == "base": a : Tuple = 128 a : Optional[int] = (2, 2, 18, 2) a : List[Any] = (4, 8, 16, 32) else: a : Dict = 192 a : str = (2, 2, 18, 2) a : List[Any] = (6, 12, 24, 48) if "in22k" in swin_name: a : Any = 21_841 else: a : str = 1_000 a : str = 'huggingface/label-files' a : Optional[Any] = 'imagenet-1k-id2label.json' a : Dict = json.load(open(hf_hub_download(snake_case , snake_case , repo_type='dataset' ) , 'r' ) ) a : Tuple = {int(snake_case ): v for k, v in idalabel.items()} a : int = idalabel a : str = {v: k for k, v in idalabel.items()} a : Dict = img_size a : List[Any] = num_classes a : str = embed_dim a : Dict = depths a : Union[str, Any] = num_heads a : int = window_size return config def SCREAMING_SNAKE_CASE__ ( snake_case : List[Any] ) -> Optional[int]: """simple docstring""" if "patch_embed.proj" in name: a : int = name.replace('patch_embed.proj' , 'embeddings.patch_embeddings.projection' ) if "patch_embed.norm" in name: a : Tuple = name.replace('patch_embed.norm' , 'embeddings.norm' ) if "layers" in name: a : Optional[int] = 'encoder.' + name if "attn.proj" in name: a : List[Any] = name.replace('attn.proj' , 'attention.output.dense' ) if "attn" in name: a : Tuple = name.replace('attn' , 'attention.self' ) if "norm1" in name: a : Optional[int] = name.replace('norm1' , 'layernorm_before' ) if "norm2" in name: a : Dict = name.replace('norm2' , 'layernorm_after' ) if "mlp.fc1" in name: a : Union[str, Any] = name.replace('mlp.fc1' , 'intermediate.dense' ) if "mlp.fc2" in name: a : Any = name.replace('mlp.fc2' , 'output.dense' ) if name == "norm.weight": a : Union[str, Any] = 'layernorm.weight' if name == "norm.bias": a : List[str] = 'layernorm.bias' if "head" in name: a : Union[str, Any] = name.replace('head' , 'classifier' ) else: a : List[Any] = 'swin.' + name return name def SCREAMING_SNAKE_CASE__ ( snake_case : List[Any] , snake_case : Tuple ) -> List[str]: """simple docstring""" for key in orig_state_dict.copy().keys(): a : Any = orig_state_dict.pop(snake_case ) if "mask" in key: continue elif "qkv" in key: a : Optional[Any] = key.split('.' ) a : Dict = int(key_split[1] ) a : Optional[int] = int(key_split[3] ) a : Tuple = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: a : Optional[Any] = val[:dim, :] a : List[Any] = val[ dim : dim * 2, : ] a : List[Any] = val[-dim:, :] else: a : Dict = val[ :dim ] a : Union[str, Any] = val[ dim : dim * 2 ] a : Union[str, Any] = val[ -dim: ] else: a : Dict = val return orig_state_dict def SCREAMING_SNAKE_CASE__ ( snake_case : Optional[int] , snake_case : Dict ) -> List[str]: """simple docstring""" a : Any = timm.create_model(snake_case , pretrained=snake_case ) timm_model.eval() a : str = get_swin_config(snake_case ) a : Optional[int] = SwinForImageClassification(snake_case ) model.eval() a : Union[str, Any] = convert_state_dict(timm_model.state_dict() , snake_case ) model.load_state_dict(snake_case ) a : List[str] = 'http://images.cocodataset.org/val2017/000000039769.jpg' a : Optional[Any] = AutoImageProcessor.from_pretrained('microsoft/{}'.format(swin_name.replace('_' , '-' ) ) ) a : str = Image.open(requests.get(snake_case , stream=snake_case ).raw ) a : Union[str, Any] = image_processor(images=snake_case , return_tensors='pt' ) a : int = timm_model(inputs['pixel_values'] ) a : Optional[int] = model(**snake_case ).logits assert torch.allclose(snake_case , snake_case , atol=1E-3 ) print(F"""Saving model {swin_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(snake_case ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(snake_case ) if __name__ == "__main__": UpperCamelCase : int = argparse.ArgumentParser() # Required parameters parser.add_argument( """--swin_name""", default="""swin_tiny_patch4_window7_224""", type=str, help="""Name of the Swin timm model you'd like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) UpperCamelCase : Optional[Any] = parser.parse_args() convert_swin_checkpoint(args.swin_name, args.pytorch_dump_folder_path)
345
0
'''simple docstring''' import inspect import unittest import numpy as np from transformers import BeitConfig from transformers.testing_utils import require_flax, require_vision, slow from transformers.utils import cached_property, is_flax_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor if is_flax_available(): import jax from transformers import FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling, FlaxBeitModel if is_vision_available(): from PIL import Image from transformers import BeitImageProcessor class UpperCamelCase ( unittest.TestCase ): """simple docstring""" def __init__( self : List[str] , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : int=1_0_0 , UpperCAmelCase_ : Any=1_3 , UpperCAmelCase_ : Union[str, Any]=3_0 , UpperCAmelCase_ : Optional[Any]=2 , UpperCAmelCase_ : List[Any]=3 , UpperCAmelCase_ : Dict=True , UpperCAmelCase_ : Optional[Any]=True , UpperCAmelCase_ : int=3_2 , UpperCAmelCase_ : Optional[int]=5 , UpperCAmelCase_ : Optional[Any]=4 , UpperCAmelCase_ : Tuple=3_7 , UpperCAmelCase_ : int="gelu" , UpperCAmelCase_ : Any=0.1 , UpperCAmelCase_ : Optional[int]=0.1 , UpperCAmelCase_ : Optional[int]=1_0 , UpperCAmelCase_ : Optional[int]=0.02 , UpperCAmelCase_ : List[Any]=3 , ): """simple docstring""" a : str = parent a : List[Any] = vocab_size a : Optional[int] = batch_size a : List[Any] = image_size a : int = patch_size a : str = num_channels a : str = is_training a : Union[str, Any] = use_labels a : Optional[Any] = hidden_size a : List[Any] = num_hidden_layers a : List[Any] = num_attention_heads a : List[str] = intermediate_size a : Tuple = hidden_act a : Union[str, Any] = hidden_dropout_prob a : str = attention_probs_dropout_prob a : Any = type_sequence_label_size a : str = initializer_range # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) a : int = (image_size // patch_size) ** 2 a : Dict = num_patches + 1 def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]) a : List[str] = None if self.use_labels: a : Any = ids_tensor([self.batch_size] , self.type_sequence_label_size) a : str = BeitConfig( vocab_size=self.vocab_size , image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=lowerCAmelCase__ , initializer_range=self.initializer_range , ) return config, pixel_values, labels def SCREAMING_SNAKE_CASE_ ( self : List[Any] , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : List[Any]): """simple docstring""" a : Optional[int] = FlaxBeitModel(config=lowerCAmelCase__) a : List[Any] = model(lowerCAmelCase__) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : str): """simple docstring""" a : List[Any] = FlaxBeitForMaskedImageModeling(config=lowerCAmelCase__) a : Optional[int] = model(lowerCAmelCase__) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length - 1, self.vocab_size)) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : Optional[Any]): """simple docstring""" a : Optional[Any] = self.type_sequence_label_size a : Union[str, Any] = FlaxBeitForImageClassification(config=lowerCAmelCase__) a : Optional[Any] = model(lowerCAmelCase__) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) # test greyscale images a : str = 1 a : str = FlaxBeitForImageClassification(lowerCAmelCase__) a : Optional[int] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]) a : Optional[Any] = model(lowerCAmelCase__) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : List[str] = self.prepare_config_and_inputs() ( a ) : Union[str, Any] = config_and_inputs a : str = {"pixel_values": pixel_values} return config, inputs_dict @require_flax class UpperCamelCase ( a_ , unittest.TestCase ): """simple docstring""" A : Any = ( (FlaxBeitModel, FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling) if is_flax_available() else () ) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Union[str, Any] = FlaxBeitModelTester(self) a : str = ConfigTester(self , config_class=lowerCAmelCase__ , has_text_modality=lowerCAmelCase__ , hidden_size=3_7) def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a : List[str] = model_class(lowerCAmelCase__) a : Optional[int] = inspect.signature(model.__call__) # signature.parameters is an OrderedDict => so arg_names order is deterministic a : Optional[int] = [*signature.parameters.keys()] a : List[str] = ["pixel_values"] self.assertListEqual(arg_names[:1] , lowerCAmelCase__) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__): a : Optional[Any] = self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__) a : List[Any] = model_class(lowerCAmelCase__) @jax.jit def model_jitted(UpperCAmelCase_ : List[Any] , **UpperCAmelCase_ : Optional[int]): return model(pixel_values=lowerCAmelCase__ , **lowerCAmelCase__) with self.subTest('JIT Enabled'): a : Any = model_jitted(**lowerCAmelCase__).to_tuple() with self.subTest('JIT Disabled'): with jax.disable_jit(): a : int = model_jitted(**lowerCAmelCase__).to_tuple() self.assertEqual(len(lowerCAmelCase__) , len(lowerCAmelCase__)) for jitted_output, output in zip(lowerCAmelCase__ , lowerCAmelCase__): self.assertEqual(jitted_output.shape , output.shape) def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCAmelCase__) def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase__) def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__) @slow def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" for model_class_name in self.all_model_classes: a : Any = model_class_name.from_pretrained('microsoft/beit-base-patch16-224') a : List[Any] = model(np.ones((1, 3, 2_2_4, 2_2_4))) self.assertIsNotNone(lowerCAmelCase__) def SCREAMING_SNAKE_CASE__ ( ) -> Optional[Any]: """simple docstring""" a : Tuple = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_vision @require_flax class UpperCamelCase ( unittest.TestCase ): """simple docstring""" @cached_property def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" return BeitImageProcessor.from_pretrained('microsoft/beit-base-patch16-224') if is_vision_available() else None @slow def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : str = FlaxBeitForMaskedImageModeling.from_pretrained('microsoft/beit-base-patch16-224-pt22k') a : Dict = self.default_image_processor a : str = prepare_img() a : Tuple = image_processor(images=lowerCAmelCase__ , return_tensors='np').pixel_values # prepare bool_masked_pos a : Optional[int] = np.ones((1, 1_9_6) , dtype=lowerCAmelCase__) # forward pass a : Optional[int] = model(pixel_values=lowerCAmelCase__ , bool_masked_pos=lowerCAmelCase__) a : List[str] = outputs.logits # verify the logits a : Dict = (1, 1_9_6, 8_1_9_2) self.assertEqual(logits.shape , lowerCAmelCase__) a : Dict = np.array( [[-3.24_37, 0.50_72, -13.91_74], [-3.24_56, 0.49_48, -13.94_01], [-3.20_33, 0.51_21, -13.85_50]]) self.assertTrue(np.allclose(logits[bool_masked_pos][:3, :3] , lowerCAmelCase__ , atol=1e-2)) @slow def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : int = FlaxBeitForImageClassification.from_pretrained('microsoft/beit-base-patch16-224') a : str = self.default_image_processor a : str = prepare_img() a : List[Any] = image_processor(images=lowerCAmelCase__ , return_tensors='np') # forward pass a : Union[str, Any] = model(**lowerCAmelCase__) a : Tuple = outputs.logits # verify the logits a : str = (1, 1_0_0_0) self.assertEqual(logits.shape , lowerCAmelCase__) a : Dict = np.array([-1.23_85, -1.09_87, -1.01_08]) self.assertTrue(np.allclose(logits[0, :3] , lowerCAmelCase__ , atol=1e-4)) a : List[Any] = 2_8_1 self.assertEqual(logits.argmax(-1).item() , lowerCAmelCase__) @slow def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : Dict = FlaxBeitForImageClassification.from_pretrained('microsoft/beit-large-patch16-224-pt22k-ft22k') a : List[str] = self.default_image_processor a : Optional[int] = prepare_img() a : Tuple = image_processor(images=lowerCAmelCase__ , return_tensors='np') # forward pass a : Optional[int] = model(**lowerCAmelCase__) a : List[str] = outputs.logits # verify the logits a : str = (1, 2_1_8_4_1) self.assertEqual(logits.shape , lowerCAmelCase__) a : int = np.array([1.68_81, -0.27_87, 0.59_01]) self.assertTrue(np.allclose(logits[0, :3] , lowerCAmelCase__ , atol=1e-4)) a : int = 2_3_9_6 self.assertEqual(logits.argmax(-1).item() , lowerCAmelCase__)
367
'''simple docstring''' import baseaa import io import json import os from copy import deepcopy from ..optimizer import AcceleratedOptimizer from ..scheduler import AcceleratedScheduler class UpperCamelCase : """simple docstring""" def __init__( self : List[str] , UpperCAmelCase_ : Tuple): """simple docstring""" if isinstance(UpperCAmelCase_ , UpperCAmelCase_): # Don't modify user's data should they want to reuse it (e.g. in tests), because once we # modified it, it will not be accepted here again, since `auto` values would have been overridden a : Dict = deepcopy(UpperCAmelCase_) elif os.path.exists(UpperCAmelCase_): with io.open(UpperCAmelCase_ , 'r' , encoding='utf-8') as f: a : Union[str, Any] = json.load(UpperCAmelCase_) else: try: a : Union[str, Any] = baseaa.urlsafe_baadecode(UpperCAmelCase_).decode('utf-8') a : List[str] = json.loads(UpperCAmelCase_) except (UnicodeDecodeError, AttributeError, ValueError): raise ValueError( f"""Expected a string path to an existing deepspeed config, or a dictionary, or a base64 encoded string. Received: {config_file_or_dict}""") a : Optional[int] = config self.set_stage_and_offload() def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : str = self.get_value('zero_optimization.stage' , -1) # offload a : Any = False if self.is_zeroa() or self.is_zeroa(): a : Tuple = set(['cpu', 'nvme']) a : int = set( [ self.get_value('zero_optimization.offload_optimizer.device'), self.get_value('zero_optimization.offload_param.device'), ]) if len(offload_devices & offload_devices_valid) > 0: a : List[str] = True def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : Dict): """simple docstring""" a : List[str] = self.config # find the config node of interest if it exists a : int = ds_key_long.split('.') a : Union[str, Any] = nodes.pop() for node in nodes: a : Union[str, Any] = config.get(UpperCAmelCase_) if config is None: return None, ds_key return config, ds_key def SCREAMING_SNAKE_CASE_ ( self : Any , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : int=None): """simple docstring""" a , a : int = self.find_config_node(UpperCAmelCase_) if config is None: return default return config.get(UpperCAmelCase_ , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : int , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Any=False): """simple docstring""" a : Any = self.config # find the config node of interest if it exists a : Optional[Any] = ds_key_long.split('.') for node in nodes: a : List[str] = config a : int = config.get(UpperCAmelCase_) if config is None: if must_exist: raise ValueError(f"""Can't find {ds_key_long} entry in the config: {self.config}""") else: return # if found remove it if parent_config is not None: parent_config.pop(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : str): """simple docstring""" a : List[str] = self.get_value(UpperCAmelCase_) return False if value is None else bool(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : Union[str, Any]): """simple docstring""" a : List[Any] = self.get_value(UpperCAmelCase_) return False if value is None else not bool(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" return self._stage == 2 def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" return self._stage == 3 def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" return self._offload class UpperCamelCase : """simple docstring""" def __init__( self : str , UpperCAmelCase_ : int): """simple docstring""" a : Union[str, Any] = engine def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : Any , **UpperCAmelCase_ : List[Any]): """simple docstring""" self.engine.backward(UpperCAmelCase_ , **UpperCAmelCase_) # Deepspeed's `engine.step` performs the following operations: # - gradient accumulation check # - gradient clipping # - optimizer step # - zero grad # - checking overflow # - lr_scheduler step (only if engine.lr_scheduler is not None) self.engine.step() # and this plugin overrides the above calls with no-ops when Accelerate runs under # Deepspeed, but allows normal functionality for non-Deepspeed cases thus enabling a simple # training loop that works transparently under many training regimes. class UpperCamelCase ( a_ ): """simple docstring""" def __init__( self : Union[str, Any] , UpperCAmelCase_ : Any): """simple docstring""" super().__init__(UpperCAmelCase_ , device_placement=UpperCAmelCase_ , scaler=UpperCAmelCase_) a : List[str] = hasattr(self.optimizer , 'overflow') def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : Dict=None): """simple docstring""" pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed @property def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" if self.__has_overflow__: return self.optimizer.overflow return False class UpperCamelCase ( a_ ): """simple docstring""" def __init__( self : str , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Optional[Any]): """simple docstring""" super().__init__(UpperCAmelCase_ , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed class UpperCamelCase : """simple docstring""" def __init__( self : Union[str, Any] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Optional[int]=0.0_01 , UpperCAmelCase_ : List[Any]=0 , **UpperCAmelCase_ : Union[str, Any]): """simple docstring""" a : int = params a : str = lr a : Tuple = weight_decay a : Dict = kwargs class UpperCamelCase : """simple docstring""" def __init__( self : int , UpperCAmelCase_ : int , UpperCAmelCase_ : Any=None , UpperCAmelCase_ : Union[str, Any]=0 , **UpperCAmelCase_ : List[Any]): """simple docstring""" a : str = optimizer a : Tuple = total_num_steps a : Optional[Any] = warmup_num_steps a : List[str] = kwargs
345
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available, is_vision_available, ) UpperCamelCase : Union[str, Any] = {'configuration_beit': ['BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BeitConfig', 'BeitOnnxConfig']} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : List[str] = ['BeitFeatureExtractor'] UpperCamelCase : Optional[int] = ['BeitImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Optional[Any] = [ 'BEIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'BeitForImageClassification', 'BeitForMaskedImageModeling', 'BeitForSemanticSegmentation', 'BeitModel', 'BeitPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Optional[Any] = [ 'FlaxBeitForImageClassification', 'FlaxBeitForMaskedImageModeling', 'FlaxBeitModel', 'FlaxBeitPreTrainedModel', ] if TYPE_CHECKING: from .configuration_beit import BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP, BeitConfig, BeitOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_beit import BeitFeatureExtractor from .image_processing_beit import BeitImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_beit import ( BEIT_PRETRAINED_MODEL_ARCHIVE_LIST, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, BeitPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_beit import ( FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling, FlaxBeitModel, FlaxBeitPreTrainedModel, ) else: import sys UpperCamelCase : Optional[int] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
368
'''simple docstring''' import os import time import warnings from dataclasses import dataclass, field from enum import Enum from typing import List, Optional, Union import torch from filelock import FileLock from torch.utils.data import Dataset from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import logging from ..processors.glue import glue_convert_examples_to_features, glue_output_modes, glue_processors from ..processors.utils import InputFeatures UpperCamelCase : List[str] = logging.get_logger(__name__) @dataclass class UpperCamelCase : """simple docstring""" A : str = field(metadata={"help": "The name of the task to train on: " + ", ".join(glue_processors.keys() )} ) A : str = field( metadata={"help": "The input data dir. Should contain the .tsv files (or other data files) for the task."} ) A : int = field( default=128 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) A : bool = field( default=a_ , metadata={"help": "Overwrite the cached training and evaluation sets"} ) def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" a : Union[str, Any] = self.task_name.lower() class UpperCamelCase ( a_ ): """simple docstring""" A : int = "train" A : Tuple = "dev" A : List[Any] = "test" class UpperCamelCase ( a_ ): """simple docstring""" A : GlueDataTrainingArguments A : str A : List[InputFeatures] def __init__( self : Tuple , UpperCAmelCase_ : GlueDataTrainingArguments , UpperCAmelCase_ : PreTrainedTokenizerBase , UpperCAmelCase_ : Optional[int] = None , UpperCAmelCase_ : Union[str, Split] = Split.train , UpperCAmelCase_ : Optional[str] = None , ): """simple docstring""" warnings.warn( 'This dataset will be removed from the library soon, preprocessing should be handled with the 🤗 Datasets ' 'library. You can have a look at this example script for pointers: ' 'https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py' , UpperCAmelCase_ , ) a : Dict = args a : int = glue_processors[args.task_name]() a : int = glue_output_modes[args.task_name] if isinstance(UpperCAmelCase_ , UpperCAmelCase_): try: a : str = Split[mode] except KeyError: raise KeyError('mode is not a valid split name') # Load data features from cache or dataset file a : List[str] = os.path.join( cache_dir if cache_dir is not None else args.data_dir , f"""cached_{mode.value}_{tokenizer.__class__.__name__}_{args.max_seq_length}_{args.task_name}""" , ) a : Tuple = self.processor.get_labels() if args.task_name in ["mnli", "mnli-mm"] and tokenizer.__class__.__name__ in ( "RobertaTokenizer", "RobertaTokenizerFast", "XLMRobertaTokenizer", "BartTokenizer", "BartTokenizerFast", ): # HACK(label indices are swapped in RoBERTa pretrained model) a , a : str = label_list[2], label_list[1] a : int = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. a : Union[str, Any] = cached_features_file + '.lock' with FileLock(UpperCAmelCase_): if os.path.exists(UpperCAmelCase_) and not args.overwrite_cache: a : Optional[Any] = time.time() a : Optional[Any] = torch.load(UpperCAmelCase_) logger.info( f"""Loading features from cached file {cached_features_file} [took %.3f s]""" , time.time() - start) else: logger.info(f"""Creating features from dataset file at {args.data_dir}""") if mode == Split.dev: a : List[Any] = self.processor.get_dev_examples(args.data_dir) elif mode == Split.test: a : Optional[Any] = self.processor.get_test_examples(args.data_dir) else: a : List[str] = self.processor.get_train_examples(args.data_dir) if limit_length is not None: a : Dict = examples[:limit_length] a : List[Any] = glue_convert_examples_to_features( UpperCAmelCase_ , UpperCAmelCase_ , max_length=args.max_seq_length , label_list=UpperCAmelCase_ , output_mode=self.output_mode , ) a : Dict = time.time() torch.save(self.features , UpperCAmelCase_) # ^ This seems to take a lot of time so I want to investigate why and how we can improve. logger.info( f"""Saving features into cached file {cached_features_file} [took {time.time() - start:.3f} s]""") def __len__( self : Tuple): """simple docstring""" return len(self.features) def __getitem__( self : Optional[int] , UpperCAmelCase_ : List[str]): """simple docstring""" return self.features[i] def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" return self.label_list
345
0
'''simple docstring''' def SCREAMING_SNAKE_CASE__ ( snake_case : List[str] ) -> List[str]: """simple docstring""" if not all(char in '01' for char in bin_string ): raise ValueError('Non-binary value was passed to the function' ) if not bin_string: raise ValueError('Empty string was passed to the function' ) a : List[Any] = '''''' while len(a__ ) % 3 != 0: a : Optional[int] = '''0''' + bin_string a : Dict = [ bin_string[index : index + 3] for index in range(len(a__ ) ) if index % 3 == 0 ] for bin_group in bin_string_in_3_list: a : Optional[Any] = 0 for index, val in enumerate(a__ ): oct_val += int(2 ** (2 - index) * int(a__ ) ) oct_string += str(a__ ) return oct_string if __name__ == "__main__": from doctest import testmod testmod()
369
'''simple docstring''' from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, logging if is_torch_available(): import torch UpperCamelCase : Dict = logging.get_logger(__name__) class UpperCamelCase ( a_ ): """simple docstring""" A : Any = ["pixel_values"] def __init__( self : str , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Optional[Dict[str, int]] = None , UpperCAmelCase_ : PILImageResampling = PILImageResampling.BILINEAR , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Dict[str, int] = None , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Union[int, float] = 1 / 2_5_5 , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Optional[Union[float, List[float]]] = None , UpperCAmelCase_ : Optional[Union[float, List[float]]] = None , **UpperCAmelCase_ : List[str] , ): """simple docstring""" super().__init__(**UpperCAmelCase_) a : str = size if size is not None else {'shortest_edge': 2_5_6} a : Dict = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) a : str = crop_size if crop_size is not None else {'height': 2_2_4, 'width': 2_2_4} a : int = get_size_dict(UpperCAmelCase_ , param_name='crop_size') a : Any = do_resize a : List[str] = size a : Union[str, Any] = resample a : int = do_center_crop a : Optional[int] = crop_size a : Tuple = do_rescale a : int = rescale_factor a : Optional[Any] = do_normalize a : Dict = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN a : Any = image_std if image_std is not None else IMAGENET_STANDARD_STD def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Dict[str, int] , UpperCAmelCase_ : PILImageResampling = PILImageResampling.BICUBIC , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Optional[int] , ): """simple docstring""" a : Optional[int] = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) if "shortest_edge" not in size: raise ValueError(f"""The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}""") a : Union[str, Any] = get_resize_output_image_size(UpperCAmelCase_ , size=size['shortest_edge'] , default_to_square=UpperCAmelCase_) return resize(UpperCAmelCase_ , size=UpperCAmelCase_ , resample=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Dict[str, int] , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Optional[int] , ): """simple docstring""" a : List[str] = get_size_dict(UpperCAmelCase_) if "height" not in size or "width" not in size: raise ValueError(f"""The `size` parameter must contain the keys `height` and `width`. Got {size.keys()}""") return center_crop(UpperCAmelCase_ , size=(size['height'], size['width']) , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : float , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Optional[Any]): """simple docstring""" return rescale(UpperCAmelCase_ , scale=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Union[float, List[float]] , UpperCAmelCase_ : Union[float, List[float]] , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Optional[int] , ): """simple docstring""" return normalize(UpperCAmelCase_ , mean=UpperCAmelCase_ , std=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : ImageInput , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Dict[str, int] = None , UpperCAmelCase_ : PILImageResampling = None , UpperCAmelCase_ : bool = None , UpperCAmelCase_ : Dict[str, int] = None , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[float] = None , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[Union[float, List[float]]] = None , UpperCAmelCase_ : Optional[Union[float, List[float]]] = None , UpperCAmelCase_ : Optional[Union[str, TensorType]] = None , UpperCAmelCase_ : Union[str, ChannelDimension] = ChannelDimension.FIRST , **UpperCAmelCase_ : List[str] , ): """simple docstring""" a : int = do_resize if do_resize is not None else self.do_resize a : int = size if size is not None else self.size a : Union[str, Any] = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) a : str = resample if resample is not None else self.resample a : Optional[Any] = do_center_crop if do_center_crop is not None else self.do_center_crop a : Union[str, Any] = crop_size if crop_size is not None else self.crop_size a : Dict = get_size_dict(UpperCAmelCase_ , param_name='crop_size') a : str = do_rescale if do_rescale is not None else self.do_rescale a : int = rescale_factor if rescale_factor is not None else self.rescale_factor a : str = do_normalize if do_normalize is not None else self.do_normalize a : List[str] = image_mean if image_mean is not None else self.image_mean a : Optional[int] = image_std if image_std is not None else self.image_std a : Dict = make_list_of_images(UpperCAmelCase_) if not valid_images(UpperCAmelCase_): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.') if do_resize and size is None: raise ValueError('Size must be specified if do_resize is True.') if do_center_crop and crop_size is None: raise ValueError('Crop size must be specified if do_center_crop is True.') if do_rescale and rescale_factor is None: raise ValueError('Rescale factor must be specified if do_rescale is True.') if do_normalize and (image_mean is None or image_std is None): raise ValueError('Image mean and std must be specified if do_normalize is True.') # All transformations expect numpy arrays. a : List[Any] = [to_numpy_array(UpperCAmelCase_) for image in images] if do_resize: a : Dict = [self.resize(image=UpperCAmelCase_ , size=UpperCAmelCase_ , resample=UpperCAmelCase_) for image in images] if do_center_crop: a : Any = [self.center_crop(image=UpperCAmelCase_ , size=UpperCAmelCase_) for image in images] if do_rescale: a : Optional[int] = [self.rescale(image=UpperCAmelCase_ , scale=UpperCAmelCase_) for image in images] if do_normalize: a : Dict = [self.normalize(image=UpperCAmelCase_ , mean=UpperCAmelCase_ , std=UpperCAmelCase_) for image in images] a : List[Any] = [to_channel_dimension_format(UpperCAmelCase_ , UpperCAmelCase_) for image in images] a : List[str] = {'pixel_values': images} return BatchFeature(data=UpperCAmelCase_ , tensor_type=UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : List[Tuple] = None): """simple docstring""" a : Dict = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(UpperCAmelCase_) != len(UpperCAmelCase_): raise ValueError( 'Make sure that you pass in as many target sizes as the batch dimension of the logits') if is_torch_tensor(UpperCAmelCase_): a : Optional[Any] = target_sizes.numpy() a : List[str] = [] for idx in range(len(UpperCAmelCase_)): a : Optional[Any] = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0) , size=target_sizes[idx] , mode='bilinear' , align_corners=UpperCAmelCase_) a : Union[str, Any] = resized_logits[0].argmax(dim=0) semantic_segmentation.append(UpperCAmelCase_) else: a : Optional[int] = logits.argmax(dim=1) a : List[str] = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0])] return semantic_segmentation
345
0
'''simple docstring''' import contextlib import os import sqlitea import pytest from datasets import Dataset, Features, Value from datasets.io.sql import SqlDatasetReader, SqlDatasetWriter from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases, require_sqlalchemy def SCREAMING_SNAKE_CASE__ ( snake_case : Any , snake_case : str ) -> str: """simple docstring""" assert isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @require_sqlalchemy @pytest.mark.parametrize('keep_in_memory' , [False, True] ) def SCREAMING_SNAKE_CASE__ ( snake_case : List[Any] , snake_case : int , snake_case : List[str] , snake_case : Dict ) -> Union[str, Any]: """simple docstring""" a : Optional[Any] = tmp_path / """cache""" a : Tuple = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): a : List[Any] = SqlDatasetReader( 'dataset' , 'sqlite:///' + sqlite_path , cache_dir=__SCREAMING_SNAKE_CASE , keep_in_memory=__SCREAMING_SNAKE_CASE ).read() _check_sql_dataset(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @require_sqlalchemy @pytest.mark.parametrize( 'features' , [ None, {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}, {'col_1': 'string', 'col_2': 'string', 'col_3': 'string'}, {'col_1': 'int32', 'col_2': 'int32', 'col_3': 'int32'}, {'col_1': 'float32', 'col_2': 'float32', 'col_3': 'float32'}, ] , ) def SCREAMING_SNAKE_CASE__ ( snake_case : List[Any] , snake_case : str , snake_case : Any , snake_case : Optional[Any] ) -> List[Any]: """simple docstring""" a : Dict = tmp_path / """cache""" a : Union[str, Any] = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} a : Tuple = features.copy() if features else default_expected_features a : Optional[int] = ( Features({feature: Value(__SCREAMING_SNAKE_CASE ) for feature, dtype in features.items()} ) if features is not None else None ) a : Any = SqlDatasetReader('dataset' , 'sqlite:///' + sqlite_path , features=__SCREAMING_SNAKE_CASE , cache_dir=__SCREAMING_SNAKE_CASE ).read() _check_sql_dataset(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE__ ( snake_case : List[Any] ) -> Optional[Any]: """simple docstring""" with contextlib.closing(sqlitea.connect(__SCREAMING_SNAKE_CASE ) ) as con: a : List[str] = con.cursor() cur.execute('SELECT * FROM dataset' ) for row in cur: yield row @require_sqlalchemy def SCREAMING_SNAKE_CASE__ ( snake_case : List[str] , snake_case : Optional[Any] , snake_case : Union[str, Any] ) -> str: """simple docstring""" a : str = tmp_path / """cache""" a : int = os.path.join(__SCREAMING_SNAKE_CASE , 'tmp.sql' ) a : Tuple = SqlDatasetReader('dataset' , 'sqlite:///' + sqlite_path , cache_dir=__SCREAMING_SNAKE_CASE ).read() SqlDatasetWriter(__SCREAMING_SNAKE_CASE , 'dataset' , 'sqlite:///' + output_sqlite_path , num_proc=1 ).write() a : int = iter_sql_file(__SCREAMING_SNAKE_CASE ) a : int = iter_sql_file(__SCREAMING_SNAKE_CASE ) for rowa, rowa in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): assert rowa == rowa @require_sqlalchemy def SCREAMING_SNAKE_CASE__ ( snake_case : int , snake_case : Union[str, Any] , snake_case : Optional[Any] ) -> List[Any]: """simple docstring""" a : int = tmp_path / """cache""" a : Optional[Any] = os.path.join(__SCREAMING_SNAKE_CASE , 'tmp.sql' ) a : Optional[int] = SqlDatasetReader('dataset' , 'sqlite:///' + sqlite_path , cache_dir=__SCREAMING_SNAKE_CASE ).read() SqlDatasetWriter(__SCREAMING_SNAKE_CASE , 'dataset' , 'sqlite:///' + output_sqlite_path , num_proc=2 ).write() a : List[str] = iter_sql_file(__SCREAMING_SNAKE_CASE ) a : Optional[Any] = iter_sql_file(__SCREAMING_SNAKE_CASE ) for rowa, rowa in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): assert rowa == rowa @require_sqlalchemy def SCREAMING_SNAKE_CASE__ ( snake_case : List[str] , snake_case : Any , snake_case : str ) -> Union[str, Any]: """simple docstring""" a : List[str] = tmp_path / """cache""" a : Union[str, Any] = os.path.join(__SCREAMING_SNAKE_CASE , 'tmp.sql' ) a : Any = SqlDatasetReader('dataset' , 'sqlite:///' + sqlite_path , cache_dir=__SCREAMING_SNAKE_CASE ).read() with pytest.raises(__SCREAMING_SNAKE_CASE ): SqlDatasetWriter(__SCREAMING_SNAKE_CASE , 'dataset' , 'sqlite:///' + output_sqlite_path , num_proc=0 ).write()
370
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE__ ( snake_case : int | float | str , snake_case : int | float | str ) -> list[str]: """simple docstring""" if nth_term == "": return [""] a : Dict = int(snake_case ) a : Optional[int] = int(snake_case ) a : list[str] = [] for temp in range(int(snake_case ) ): series.append(F"""1 / {pow(temp + 1 , int(snake_case ) )}""" if series else '1' ) return series if __name__ == "__main__": import doctest doctest.testmod() UpperCamelCase : Optional[int] = int(input("""Enter the last number (nth term) of the P-Series""")) UpperCamelCase : List[Any] = int(input("""Enter the power for P-Series""")) print("""Formula of P-Series => 1+1/2^p+1/3^p ..... 1/n^p""") print(p_series(nth_term, power))
345
0
'''simple docstring''' from typing import List, Optional, Union import torch from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) UpperCamelCase : Dict = logging.get_logger(__name__) # pylint: disable=invalid-name UpperCamelCase : Tuple = "\n Examples:\n ```py\n >>> from diffusers import KandinskyV22Pipeline, KandinskyV22PriorPipeline\n >>> import torch\n\n >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(\"kandinsky-community/kandinsky-2-2-prior\")\n >>> pipe_prior.to(\"cuda\")\n >>> prompt = \"red cat, 4k photo\"\n >>> out = pipe_prior(prompt)\n >>> image_emb = out.image_embeds\n >>> zero_image_emb = out.negative_image_embeds\n >>> pipe = KandinskyV22Pipeline.from_pretrained(\"kandinsky-community/kandinsky-2-2-decoder\")\n >>> pipe.to(\"cuda\")\n >>> image = pipe(\n ... image_embeds=image_emb,\n ... negative_image_embeds=zero_image_emb,\n ... height=768,\n ... width=768,\n ... num_inference_steps=50,\n ... ).images\n >>> image[0].save(\"cat.png\")\n ```\n" def SCREAMING_SNAKE_CASE__ ( snake_case : Dict , snake_case : Optional[Any] , snake_case : str=8 ) -> Dict: """simple docstring""" a : Tuple = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 a : Optional[int] = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor class UpperCamelCase ( _UpperCAmelCase ): """simple docstring""" def __init__( self : Tuple , UpperCAmelCase_ : UNetaDConditionModel , UpperCAmelCase_ : DDPMScheduler , UpperCAmelCase_ : VQModel , ): """simple docstring""" super().__init__() self.register_modules( unet=lowercase_ , scheduler=lowercase_ , movq=lowercase_ , ) a : Dict = 2 ** (len(self.movq.config.block_out_channels) - 1) def SCREAMING_SNAKE_CASE_ ( self : int , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : str , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Tuple): """simple docstring""" if latents is None: a : Optional[int] = randn_tensor(lowercase_ , generator=lowercase_ , device=lowercase_ , dtype=lowercase_) else: if latents.shape != shape: raise ValueError(f"""Unexpected latents shape, got {latents.shape}, expected {shape}""") a : int = latents.to(lowercase_) a : List[Any] = latents * scheduler.init_noise_sigma return latents def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : List[str]=0): """simple docstring""" if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('Please install accelerate via `pip install accelerate`') a : Tuple = torch.device(f"""cuda:{gpu_id}""") a : int = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(lowercase_ , lowercase_) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : Dict=0): """simple docstring""" if is_accelerate_available() and is_accelerate_version('>=' , '0.17.0.dev0'): from accelerate import cpu_offload_with_hook else: raise ImportError('`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.') a : Union[str, Any] = torch.device(f"""cuda:{gpu_id}""") if self.device.type != "cpu": self.to('cpu' , silence_dtype_warnings=lowercase_) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) a : Optional[int] = None for cpu_offloaded_model in [self.unet, self.movq]: a : Any = cpu_offload_with_hook(lowercase_ , lowercase_ , prev_module_hook=lowercase_) # We'll offload the last model manually. a : Union[str, Any] = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" if not hasattr(self.unet , '_hf_hook'): return self.device for module in self.unet.modules(): if ( hasattr(lowercase_ , '_hf_hook') and hasattr(module._hf_hook , 'execution_device') and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device) return self.device @torch.no_grad() @replace_example_docstring(lowercase_) def __call__( self : int , UpperCAmelCase_ : Union[torch.FloatTensor, List[torch.FloatTensor]] , UpperCAmelCase_ : Union[torch.FloatTensor, List[torch.FloatTensor]] , UpperCAmelCase_ : int = 5_1_2 , UpperCAmelCase_ : int = 5_1_2 , UpperCAmelCase_ : int = 1_0_0 , UpperCAmelCase_ : float = 4.0 , UpperCAmelCase_ : int = 1 , UpperCAmelCase_ : Optional[Union[torch.Generator, List[torch.Generator]]] = None , UpperCAmelCase_ : Optional[torch.FloatTensor] = None , UpperCAmelCase_ : Optional[str] = "pil" , UpperCAmelCase_ : bool = True , ): """simple docstring""" a : List[str] = self._execution_device a : Union[str, Any] = guidance_scale > 1.0 if isinstance(lowercase_ , lowercase_): a : Any = torch.cat(lowercase_ , dim=0) a : Dict = image_embeds.shape[0] * num_images_per_prompt if isinstance(lowercase_ , lowercase_): a : List[str] = torch.cat(lowercase_ , dim=0) if do_classifier_free_guidance: a : Union[str, Any] = image_embeds.repeat_interleave(lowercase_ , dim=0) a : Union[str, Any] = negative_image_embeds.repeat_interleave(lowercase_ , dim=0) a : List[Any] = torch.cat([negative_image_embeds, image_embeds] , dim=0).to(dtype=self.unet.dtype , device=lowercase_) self.scheduler.set_timesteps(lowercase_ , device=lowercase_) a : Optional[int] = self.scheduler.timesteps a : Union[str, Any] = self.unet.config.in_channels a : Optional[int] = downscale_height_and_width(lowercase_ , lowercase_ , self.movq_scale_factor) # create initial latent a : List[str] = self.prepare_latents( (batch_size, num_channels_latents, height, width) , image_embeds.dtype , lowercase_ , lowercase_ , lowercase_ , self.scheduler , ) for i, t in enumerate(self.progress_bar(lowercase_)): # expand the latents if we are doing classifier free guidance a : Dict = torch.cat([latents] * 2) if do_classifier_free_guidance else latents a : Any = {"""image_embeds""": image_embeds} a : Any = self.unet( sample=lowercase_ , timestep=lowercase_ , encoder_hidden_states=lowercase_ , added_cond_kwargs=lowercase_ , return_dict=lowercase_ , )[0] if do_classifier_free_guidance: a : List[str] = noise_pred.split(latents.shape[1] , dim=1) a : Dict = noise_pred.chunk(2) a : Any = variance_pred.chunk(2) a : str = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) a : Optional[Any] = torch.cat([noise_pred, variance_pred_text] , dim=1) if not ( hasattr(self.scheduler.config , 'variance_type') and self.scheduler.config.variance_type in ["learned", "learned_range"] ): a : List[str] = noise_pred.split(latents.shape[1] , dim=1) # compute the previous noisy sample x_t -> x_t-1 a : int = self.scheduler.step( lowercase_ , lowercase_ , lowercase_ , generator=lowercase_ , )[0] # post-processing a : Optional[Any] = self.movq.decode(lowercase_ , force_not_quantize=lowercase_)["""sample"""] if output_type not in ["pt", "np", "pil"]: raise ValueError(f"""Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}""") if output_type in ["np", "pil"]: a : str = image * 0.5 + 0.5 a : Any = image.clamp(0 , 1) a : int = image.cpu().permute(0 , 2 , 3 , 1).float().numpy() if output_type == "pil": a : int = self.numpy_to_pil(lowercase_) if not return_dict: return (image,) return ImagePipelineOutput(images=lowercase_)
371
'''simple docstring''' import torch def SCREAMING_SNAKE_CASE__ ( ) -> str: """simple docstring""" if torch.cuda.is_available(): a : int = torch.cuda.device_count() else: a : Any = 0 print(F"""Successfully ran on {num_gpus} GPUs""" ) if __name__ == "__main__": main()
345
0
'''simple docstring''' import argparse import io import requests import torch from omegaconf import OmegaConf from diffusers import AutoencoderKL from diffusers.pipelines.stable_diffusion.convert_from_ckpt import ( assign_to_checkpoint, conv_attn_to_linear, create_vae_diffusers_config, renew_vae_attention_paths, renew_vae_resnet_paths, ) def SCREAMING_SNAKE_CASE__ ( snake_case : Tuple , snake_case : Union[str, Any] ) -> str: """simple docstring""" a : Optional[Any] = checkpoint a : Optional[int] = {} a : List[Any] = vae_state_dict['encoder.conv_in.weight'] a : Any = vae_state_dict['encoder.conv_in.bias'] a : List[str] = vae_state_dict['encoder.conv_out.weight'] a : List[Any] = vae_state_dict['encoder.conv_out.bias'] a : Dict = vae_state_dict['encoder.norm_out.weight'] a : Union[str, Any] = vae_state_dict['encoder.norm_out.bias'] a : str = vae_state_dict['decoder.conv_in.weight'] a : int = vae_state_dict['decoder.conv_in.bias'] a : List[Any] = vae_state_dict['decoder.conv_out.weight'] a : Optional[Any] = vae_state_dict['decoder.conv_out.bias'] a : Union[str, Any] = vae_state_dict['decoder.norm_out.weight'] a : List[str] = vae_state_dict['decoder.norm_out.bias'] a : Optional[Any] = vae_state_dict['quant_conv.weight'] a : Optional[int] = vae_state_dict['quant_conv.bias'] a : List[str] = vae_state_dict['post_quant_conv.weight'] a : Dict = vae_state_dict['post_quant_conv.bias'] # Retrieves the keys for the encoder down blocks only a : Optional[Any] = len({'.'.join(layer.split('.' )[:3] ) for layer in vae_state_dict if 'encoder.down' in layer} ) a : int = { layer_id: [key for key in vae_state_dict if F"""down.{layer_id}""" in key] for layer_id in range(__a ) } # Retrieves the keys for the decoder up blocks only a : Any = len({'.'.join(layer.split('.' )[:3] ) for layer in vae_state_dict if 'decoder.up' in layer} ) a : str = { layer_id: [key for key in vae_state_dict if F"""up.{layer_id}""" in key] for layer_id in range(__a ) } for i in range(__a ): a : Optional[Any] = [key for key in down_blocks[i] if F"""down.{i}""" in key and F"""down.{i}.downsample""" not in key] if F"""encoder.down.{i}.downsample.conv.weight""" in vae_state_dict: a : Dict = vae_state_dict.pop( F"""encoder.down.{i}.downsample.conv.weight""" ) a : Union[str, Any] = vae_state_dict.pop( F"""encoder.down.{i}.downsample.conv.bias""" ) a : Optional[Any] = renew_vae_resnet_paths(__a ) a : List[Any] = {'old': F"""down.{i}.block""", 'new': F"""down_blocks.{i}.resnets"""} assign_to_checkpoint(__a , __a , __a , additional_replacements=[meta_path] , config=__a ) a : Union[str, Any] = [key for key in vae_state_dict if 'encoder.mid.block' in key] a : List[str] = 2 for i in range(1 , num_mid_res_blocks + 1 ): a : int = [key for key in mid_resnets if F"""encoder.mid.block_{i}""" in key] a : Tuple = renew_vae_resnet_paths(__a ) a : Optional[Any] = {'old': F"""mid.block_{i}""", 'new': F"""mid_block.resnets.{i - 1}"""} assign_to_checkpoint(__a , __a , __a , additional_replacements=[meta_path] , config=__a ) a : Dict = [key for key in vae_state_dict if 'encoder.mid.attn' in key] a : Dict = renew_vae_attention_paths(__a ) a : Union[str, Any] = {'old': 'mid.attn_1', 'new': 'mid_block.attentions.0'} assign_to_checkpoint(__a , __a , __a , additional_replacements=[meta_path] , config=__a ) conv_attn_to_linear(__a ) for i in range(__a ): a : List[str] = num_up_blocks - 1 - i a : Dict = [ key for key in up_blocks[block_id] if F"""up.{block_id}""" in key and F"""up.{block_id}.upsample""" not in key ] if F"""decoder.up.{block_id}.upsample.conv.weight""" in vae_state_dict: a : List[str] = vae_state_dict[ F"""decoder.up.{block_id}.upsample.conv.weight""" ] a : int = vae_state_dict[ F"""decoder.up.{block_id}.upsample.conv.bias""" ] a : List[Any] = renew_vae_resnet_paths(__a ) a : Dict = {'old': F"""up.{block_id}.block""", 'new': F"""up_blocks.{i}.resnets"""} assign_to_checkpoint(__a , __a , __a , additional_replacements=[meta_path] , config=__a ) a : str = [key for key in vae_state_dict if 'decoder.mid.block' in key] a : str = 2 for i in range(1 , num_mid_res_blocks + 1 ): a : Optional[Any] = [key for key in mid_resnets if F"""decoder.mid.block_{i}""" in key] a : Dict = renew_vae_resnet_paths(__a ) a : List[str] = {'old': F"""mid.block_{i}""", 'new': F"""mid_block.resnets.{i - 1}"""} assign_to_checkpoint(__a , __a , __a , additional_replacements=[meta_path] , config=__a ) a : List[str] = [key for key in vae_state_dict if 'decoder.mid.attn' in key] a : str = renew_vae_attention_paths(__a ) a : Tuple = {'old': 'mid.attn_1', 'new': 'mid_block.attentions.0'} assign_to_checkpoint(__a , __a , __a , additional_replacements=[meta_path] , config=__a ) conv_attn_to_linear(__a ) return new_checkpoint def SCREAMING_SNAKE_CASE__ ( snake_case : str , snake_case : str , ) -> Any: """simple docstring""" # Only support V1 a : Optional[Any] = requests.get( ' https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml' ) a : Dict = io.BytesIO(r.content ) a : Tuple = OmegaConf.load(__a ) a : int = 512 a : str = 'cuda' if torch.cuda.is_available() else 'cpu' if checkpoint_path.endswith('safetensors' ): from safetensors import safe_open a : Tuple = {} with safe_open(__a , framework='pt' , device='cpu' ) as f: for key in f.keys(): a : Optional[int] = f.get_tensor(__a ) else: a : Any = torch.load(__a , map_location=__a )['state_dict'] # Convert the VAE model. a : int = create_vae_diffusers_config(__a , image_size=__a ) a : str = custom_convert_ldm_vae_checkpoint(__a , __a ) a : List[str] = AutoencoderKL(**__a ) vae.load_state_dict(__a ) vae.save_pretrained(__a ) if __name__ == "__main__": UpperCamelCase : Dict = argparse.ArgumentParser() parser.add_argument("""--vae_pt_path""", default=None, type=str, required=True, help="""Path to the VAE.pt to convert.""") parser.add_argument("""--dump_path""", default=None, type=str, required=True, help="""Path to the VAE.pt to convert.""") UpperCamelCase : Tuple = parser.parse_args() vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)
350
'''simple docstring''' import faiss # noqa: F401 # Here to have a nice missing dependency error message early on import numpy # noqa: F401 # Here to have a nice missing dependency error message early on import requests # noqa: F401 # Here to have a nice missing dependency error message early on import sklearn # noqa: F401 # Here to have a nice missing dependency error message early on import tqdm # noqa: F401 # Here to have a nice missing dependency error message early on from mauve import compute_mauve # From: mauve-text import datasets UpperCamelCase : Optional[int] = """\ @inproceedings{pillutla-etal:mauve:neurips2021, title={MAUVE: Measuring the Gap Between Neural Text and Human Text using Divergence Frontiers}, author={Pillutla, Krishna and Swayamdipta, Swabha and Zellers, Rowan and Thickstun, John and Welleck, Sean and Choi, Yejin and Harchaoui, Zaid}, booktitle = {NeurIPS}, year = {2021} } """ UpperCamelCase : Optional[Any] = """\ MAUVE is a library built on PyTorch and HuggingFace Transformers to measure the gap between neural text and human text with the eponymous MAUVE measure. MAUVE summarizes both Type I and Type II errors measured softly using Kullback–Leibler (KL) divergences. For details, see the MAUVE paper: https://arxiv.org/abs/2102.01454 (Neurips, 2021). This metrics is a wrapper around the official implementation of MAUVE: https://github.com/krishnap25/mauve """ UpperCamelCase : str = """ Calculates MAUVE scores between two lists of generated text and reference text. Args: predictions: list of generated text to score. Each predictions should be a string with tokens separated by spaces. references: list of reference for each prediction. Each reference should be a string with tokens separated by spaces. Optional Args: num_buckets: the size of the histogram to quantize P and Q. Options: 'auto' (default) or an integer pca_max_data: the number data points to use for PCA dimensionality reduction prior to clustering. If -1, use all the data. Default -1 kmeans_explained_var: amount of variance of the data to keep in dimensionality reduction by PCA. Default 0.9 kmeans_num_redo: number of times to redo k-means clustering (the best objective is kept). Default 5 kmeans_max_iter: maximum number of k-means iterations. Default 500 featurize_model_name: name of the model from which features are obtained. Default 'gpt2-large' Use one of ['gpt2', 'gpt2-medium', 'gpt2-large', 'gpt2-xl']. device_id: Device for featurization. Supply a GPU id (e.g. 0 or 3) to use GPU. If no GPU with this id is found, use CPU max_text_length: maximum number of tokens to consider. Default 1024 divergence_curve_discretization_size: Number of points to consider on the divergence curve. Default 25 mauve_scaling_factor: \"c\" from the paper. Default 5. verbose: If True (default), print running time updates seed: random seed to initialize k-means cluster assignments. Returns: mauve: MAUVE score, a number between 0 and 1. Larger values indicate that P and Q are closer, frontier_integral: Frontier Integral, a number between 0 and 1. Smaller values indicate that P and Q are closer, divergence_curve: a numpy.ndarray of shape (m, 2); plot it with matplotlib to view the divergence curve, p_hist: a discrete distribution, which is a quantized version of the text distribution p_text, q_hist: same as above, but with q_text. Examples: >>> # faiss segfaults in doctest for some reason, so the .compute call is not tested with doctest >>> import datasets >>> mauve = datasets.load_metric('mauve') >>> predictions = [\"hello there\", \"general kenobi\"] >>> references = [\"hello there\", \"general kenobi\"] >>> out = mauve.compute(predictions=predictions, references=references) # doctest: +SKIP >>> print(out.mauve) # doctest: +SKIP 1.0 """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCamelCase ( datasets.Metric ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage='https://github.com/krishnap25/mauve' , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('string' , id='sequence'), 'references': datasets.Value('string' , id='sequence'), }) , codebase_urls=['https://github.com/krishnap25/mauve'] , reference_urls=[ 'https://arxiv.org/abs/2102.01454', 'https://github.com/krishnap25/mauve', ] , ) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : Optional[Any]=None , UpperCAmelCase_ : int=None , UpperCAmelCase_ : Any=None , UpperCAmelCase_ : Optional[int]=None , UpperCAmelCase_ : Tuple="auto" , UpperCAmelCase_ : Any=-1 , UpperCAmelCase_ : Optional[int]=0.9 , UpperCAmelCase_ : Union[str, Any]=5 , UpperCAmelCase_ : int=5_0_0 , UpperCAmelCase_ : int="gpt2-large" , UpperCAmelCase_ : Tuple=-1 , UpperCAmelCase_ : Dict=1_0_2_4 , UpperCAmelCase_ : List[str]=2_5 , UpperCAmelCase_ : int=5 , UpperCAmelCase_ : Any=True , UpperCAmelCase_ : str=2_5 , ): """simple docstring""" a : List[str] = compute_mauve( p_text=UpperCAmelCase_ , q_text=UpperCAmelCase_ , p_features=UpperCAmelCase_ , q_features=UpperCAmelCase_ , p_tokens=UpperCAmelCase_ , q_tokens=UpperCAmelCase_ , num_buckets=UpperCAmelCase_ , pca_max_data=UpperCAmelCase_ , kmeans_explained_var=UpperCAmelCase_ , kmeans_num_redo=UpperCAmelCase_ , kmeans_max_iter=UpperCAmelCase_ , featurize_model_name=UpperCAmelCase_ , device_id=UpperCAmelCase_ , max_text_length=UpperCAmelCase_ , divergence_curve_discretization_size=UpperCAmelCase_ , mauve_scaling_factor=UpperCAmelCase_ , verbose=UpperCAmelCase_ , seed=UpperCAmelCase_ , ) return out
345
0
'''simple docstring''' import unittest import numpy as np import torch from diffusers import ScoreSdeVePipeline, ScoreSdeVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class UpperCamelCase ( unittest.TestCase ): """simple docstring""" @property def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" torch.manual_seed(0) a : Dict = UNetaDModel( block_out_channels=(3_2, 6_4) , layers_per_block=2 , sample_size=3_2 , in_channels=3 , out_channels=3 , down_block_types=('DownBlock2D', 'AttnDownBlock2D') , up_block_types=('AttnUpBlock2D', 'UpBlock2D') , ) return model def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : str = self.dummy_uncond_unet a : List[Any] = ScoreSdeVeScheduler() a : Any = ScoreSdeVePipeline(unet=lowercase_ , scheduler=lowercase_) sde_ve.to(lowercase_) sde_ve.set_progress_bar_config(disable=lowercase_) a : Dict = torch.manual_seed(0) a : Optional[int] = sde_ve(num_inference_steps=2 , output_type='numpy' , generator=lowercase_).images a : int = torch.manual_seed(0) a : Any = sde_ve(num_inference_steps=2 , output_type='numpy' , generator=lowercase_ , return_dict=lowercase_)[ 0 ] a : str = image[0, -3:, -3:, -1] a : List[Any] = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 3_2, 3_2, 3) a : Optional[Any] = np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice).max() < 1e-2 @slow @require_torch class UpperCamelCase ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : Optional[Any] = "google/ncsnpp-church-256" a : List[str] = UNetaDModel.from_pretrained(lowercase_) a : int = ScoreSdeVeScheduler.from_pretrained(lowercase_) a : List[str] = ScoreSdeVePipeline(unet=lowercase_ , scheduler=lowercase_) sde_ve.to(lowercase_) sde_ve.set_progress_bar_config(disable=lowercase_) a : Any = torch.manual_seed(0) a : List[Any] = sde_ve(num_inference_steps=1_0 , output_type='numpy' , generator=lowercase_).images a : Union[str, Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 2_5_6, 2_5_6, 3) a : str = np.array([0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-2
351
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE__ ( snake_case : list[int | float] , snake_case : int , snake_case : int ) -> int | float: """simple docstring""" if len(snake_case ) == 0: raise ValueError('find_max() arg is an empty sequence' ) if ( left >= len(snake_case ) or left < -len(snake_case ) or right >= len(snake_case ) or right < -len(snake_case ) ): raise IndexError('list index out of range' ) if left == right: return nums[left] a : Union[str, Any] = (left + right) >> 1 # the middle a : List[str] = find_max(snake_case , snake_case , snake_case ) # find max in range[left, mid] a : Dict = find_max(snake_case , mid + 1 , snake_case ) # 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)
345
0
'''simple docstring''' import argparse import os import re UpperCamelCase : Union[str, Any] = """src/transformers/models/auto""" # re pattern that matches mapping introductions: # SUPER_MODEL_MAPPING_NAMES = OrderedDict or SUPER_MODEL_MAPPING = OrderedDict UpperCamelCase : List[Any] = re.compile(R"""[A-Z_]+_MAPPING(\s+|_[A-Z_]+\s+)=\s+OrderedDict""") # re pattern that matches identifiers in mappings UpperCamelCase : Union[str, Any] = re.compile(R"""\s*\(\s*\"(\S[^\"]+)\"""") def SCREAMING_SNAKE_CASE__ ( snake_case : Any , snake_case : bool = False ) -> Tuple: """simple docstring""" with open(_snake_case , 'r' , encoding='utf-8' ) as f: a : str = f.read() a : List[Any] = content.split('\n' ) a : str = [] a : List[Any] = 0 while line_idx < len(_snake_case ): if _re_intro_mapping.search(lines[line_idx] ) is not None: a : List[str] = len(re.search(R'^(\s*)\S' , lines[line_idx] ).groups()[0] ) + 8 # Start of a new mapping! while not lines[line_idx].startswith(' ' * indent + '(' ): new_lines.append(lines[line_idx] ) line_idx += 1 a : str = [] while lines[line_idx].strip() != "]": # Blocks either fit in one line or not if lines[line_idx].strip() == "(": a : Optional[Any] = line_idx while not lines[line_idx].startswith(' ' * indent + ')' ): line_idx += 1 blocks.append('\n'.join(lines[start_idx : line_idx + 1] ) ) else: blocks.append(lines[line_idx] ) line_idx += 1 # Sort blocks by their identifiers a : Optional[int] = sorted(_snake_case , key=lambda snake_case : _re_identifier.search(_snake_case ).groups()[0] ) new_lines += blocks else: new_lines.append(lines[line_idx] ) line_idx += 1 if overwrite: with open(_snake_case , 'w' , encoding='utf-8' ) as f: f.write('\n'.join(_snake_case ) ) elif "\n".join(_snake_case ) != content: return True def SCREAMING_SNAKE_CASE__ ( snake_case : bool = False ) -> int: """simple docstring""" a : Union[str, Any] = [os.path.join(_snake_case , _snake_case ) for f in os.listdir(_snake_case ) if f.endswith('.py' )] a : Optional[Any] = [sort_auto_mapping(_snake_case , overwrite=_snake_case ) for fname in fnames] if not overwrite and any(_snake_case ): a : List[str] = [f for f, d in zip(_snake_case , _snake_case ) if d] raise ValueError( F"""The following files have auto mappings that need sorting: {', '.join(_snake_case )}. Run `make style` to fix""" ' this.' ) if __name__ == "__main__": UpperCamelCase : Union[str, Any] = argparse.ArgumentParser() parser.add_argument("""--check_only""", action="""store_true""", help="""Whether to only check or fix style.""") UpperCamelCase : Optional[int] = parser.parse_args() sort_all_auto_mappings(not args.check_only)
352
'''simple docstring''' import math import os from copy import deepcopy import datasets import evaluate import torch import transformers from datasets import load_dataset from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer from accelerate import Accelerator from accelerate.test_utils import RegressionDataset, RegressionModel from accelerate.utils import is_tpu_available, set_seed UpperCamelCase : int = """true""" def SCREAMING_SNAKE_CASE__ ( snake_case : List[str] , snake_case : int=82 , snake_case : Tuple=16 ) -> Union[str, Any]: """simple docstring""" set_seed(42 ) a : List[str] = RegressionModel() a : Union[str, Any] = deepcopy(snake_case ) a : Dict = RegressionDataset(length=snake_case ) a : Dict = DataLoader(snake_case , batch_size=snake_case ) model.to(accelerator.device ) a , a : Optional[int] = accelerator.prepare(snake_case , snake_case ) return model, ddp_model, dataloader def SCREAMING_SNAKE_CASE__ ( snake_case : Accelerator , snake_case : Union[str, Any]=False ) -> Optional[int]: """simple docstring""" a : List[str] = AutoTokenizer.from_pretrained('hf-internal-testing/mrpc-bert-base-cased' ) a : Any = load_dataset('glue' , 'mrpc' , split='validation' ) def tokenize_function(snake_case : int ): a : Any = tokenizer(examples['sentence1'] , examples['sentence2'] , truncation=snake_case , max_length=snake_case ) return outputs with accelerator.main_process_first(): a : Dict = dataset.map( snake_case , batched=snake_case , remove_columns=['idx', 'sentence1', 'sentence2'] , ) a : List[str] = tokenized_datasets.rename_column('label' , 'labels' ) def collate_fn(snake_case : Optional[Any] ): if use_longest: return tokenizer.pad(snake_case , padding='longest' , return_tensors='pt' ) return tokenizer.pad(snake_case , padding='max_length' , max_length=128 , return_tensors='pt' ) return DataLoader(snake_case , shuffle=snake_case , collate_fn=snake_case , batch_size=16 ) def SCREAMING_SNAKE_CASE__ ( snake_case : Optional[Any] , snake_case : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" a : int = Accelerator(dispatch_batches=snake_case , split_batches=snake_case ) a : List[str] = get_dataloader(snake_case , not dispatch_batches ) a : Optional[Any] = AutoModelForSequenceClassification.from_pretrained( 'hf-internal-testing/mrpc-bert-base-cased' , return_dict=snake_case ) a , a : Optional[Any] = accelerator.prepare(snake_case , snake_case ) return {"ddp": [ddp_model, ddp_dataloader, "cuda:0"], "no": [model, dataloader, accelerator.device]}, accelerator def SCREAMING_SNAKE_CASE__ ( snake_case : Optional[Any] , snake_case : Optional[Any] , snake_case : Union[str, Any] ) -> Optional[int]: """simple docstring""" a : Dict = [] for batch in dataloader: a , a : Any = batch.values() with torch.no_grad(): a : Tuple = model(snake_case ) a , a : Dict = accelerator.gather_for_metrics((logit, target) ) logits_and_targets.append((logit, target) ) a , a : List[str] = [], [] for logit, targ in logits_and_targets: logits.append(snake_case ) targs.append(snake_case ) a , a : Any = torch.cat(snake_case ), torch.cat(snake_case ) return logits, targs def SCREAMING_SNAKE_CASE__ ( snake_case : Accelerator , snake_case : Dict=82 , snake_case : str=False , snake_case : List[str]=False , snake_case : List[Any]=16 ) -> Optional[int]: """simple docstring""" a , a , a : int = get_basic_setup(snake_case , snake_case , snake_case ) a , a : int = generate_predictions(snake_case , snake_case , snake_case ) assert ( len(snake_case ) == num_samples ), F"""Unexpected number of inputs:\n Expected: {num_samples}\n Actual: {len(snake_case )}""" def SCREAMING_SNAKE_CASE__ ( snake_case : bool = False , snake_case : bool = False ) -> List[str]: """simple docstring""" a : int = evaluate.load('glue' , 'mrpc' ) a , a : Tuple = get_mrpc_setup(snake_case , snake_case ) # First do baseline a , a , a : Tuple = setup['no'] model.to(snake_case ) model.eval() for batch in dataloader: batch.to(snake_case ) with torch.inference_mode(): a : List[Any] = model(**snake_case ) a : Optional[Any] = outputs.logits.argmax(dim=-1 ) metric.add_batch(predictions=snake_case , references=batch['labels'] ) a : Tuple = metric.compute() # Then do distributed a , a , a : Tuple = setup['ddp'] model.eval() for batch in dataloader: with torch.inference_mode(): a : List[str] = model(**snake_case ) a : Optional[Any] = outputs.logits.argmax(dim=-1 ) a : Optional[int] = batch['labels'] a , a : Optional[int] = accelerator.gather_for_metrics((preds, references) ) metric.add_batch(predictions=snake_case , references=snake_case ) a : str = metric.compute() for key in "accuracy f1".split(): assert math.isclose( baseline[key] , distributed[key] ), F"""Baseline and Distributed are not the same for key {key}:\n\tBaseline: {baseline[key]}\n\tDistributed: {distributed[key]}\n""" def SCREAMING_SNAKE_CASE__ ( ) -> str: """simple docstring""" a : Dict = Accelerator(split_batches=snake_case , dispatch_batches=snake_case ) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_warning() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() # These are a bit slower so they should only be ran on the GPU or TPU if torch.cuda.is_available() or is_tpu_available(): if accelerator.is_local_main_process: print('**Testing gather_for_metrics**' ) for split_batches in [True, False]: for dispatch_batches in [True, False]: if accelerator.is_local_main_process: print(F"""With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`""" ) test_mrpc(snake_case , snake_case ) accelerator.state._reset_state() if accelerator.is_local_main_process: print('**Test torch metrics**' ) for split_batches in [True, False]: for dispatch_batches in [True, False]: a : List[Any] = Accelerator(split_batches=snake_case , dispatch_batches=snake_case ) if accelerator.is_local_main_process: print(F"""With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`, length=99""" ) test_torch_metrics(snake_case , 99 ) accelerator.state._reset_state() if accelerator.is_local_main_process: print('**Test last batch is not dropped when perfectly divisible**' ) a : Optional[Any] = Accelerator() test_torch_metrics(snake_case , 512 ) accelerator.state._reset_state() def SCREAMING_SNAKE_CASE__ ( snake_case : Union[str, Any] ) -> int: """simple docstring""" # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
345
0
UpperCamelCase : int = frozenset( [ """prompt""", """height""", """width""", """guidance_scale""", """negative_prompt""", """prompt_embeds""", """negative_prompt_embeds""", """cross_attention_kwargs""", ] ) UpperCamelCase : Optional[int] = frozenset(["""prompt""", """negative_prompt"""]) UpperCamelCase : Optional[int] = frozenset([]) UpperCamelCase : int = frozenset(["""image"""]) UpperCamelCase : Dict = frozenset( [ """image""", """height""", """width""", """guidance_scale""", ] ) UpperCamelCase : Optional[int] = frozenset(["""image"""]) UpperCamelCase : List[Any] = frozenset( [ """prompt""", """image""", """height""", """width""", """guidance_scale""", """negative_prompt""", """prompt_embeds""", """negative_prompt_embeds""", ] ) UpperCamelCase : List[Any] = frozenset(["""prompt""", """image""", """negative_prompt"""]) UpperCamelCase : List[str] = frozenset( [ # Text guided image variation with an image mask """prompt""", """image""", """mask_image""", """height""", """width""", """guidance_scale""", """negative_prompt""", """prompt_embeds""", """negative_prompt_embeds""", ] ) UpperCamelCase : Dict = frozenset(["""prompt""", """image""", """mask_image""", """negative_prompt"""]) UpperCamelCase : List[str] = frozenset( [ # image variation with an image mask """image""", """mask_image""", """height""", """width""", """guidance_scale""", ] ) UpperCamelCase : Any = frozenset(["""image""", """mask_image"""]) UpperCamelCase : Any = frozenset( [ """example_image""", """image""", """mask_image""", """height""", """width""", """guidance_scale""", ] ) UpperCamelCase : str = frozenset(["""example_image""", """image""", """mask_image"""]) UpperCamelCase : Dict = frozenset(["""class_labels"""]) UpperCamelCase : Any = frozenset(["""class_labels"""]) UpperCamelCase : str = frozenset(["""batch_size"""]) UpperCamelCase : Dict = frozenset([]) UpperCamelCase : str = frozenset(["""batch_size"""]) UpperCamelCase : Any = frozenset([]) UpperCamelCase : int = frozenset( [ """prompt""", """audio_length_in_s""", """guidance_scale""", """negative_prompt""", """prompt_embeds""", """negative_prompt_embeds""", """cross_attention_kwargs""", ] ) UpperCamelCase : List[Any] = frozenset(["""prompt""", """negative_prompt"""]) UpperCamelCase : Dict = frozenset(["""input_tokens"""]) UpperCamelCase : str = frozenset(["""input_tokens"""])
353
'''simple docstring''' from math import acos, sin from typing import List, Tuple, Union import numpy as np import torch from PIL import Image from ...models import AutoencoderKL, UNetaDConditionModel from ...schedulers import DDIMScheduler, DDPMScheduler from ...utils import randn_tensor from ..pipeline_utils import AudioPipelineOutput, BaseOutput, DiffusionPipeline, ImagePipelineOutput from .mel import Mel class UpperCamelCase ( a_ ): """simple docstring""" A : Optional[int] = ["vqvae"] def __init__( self : List[str] , UpperCAmelCase_ : AutoencoderKL , UpperCAmelCase_ : UNetaDConditionModel , UpperCAmelCase_ : Mel , UpperCAmelCase_ : Union[DDIMScheduler, DDPMScheduler] , ): """simple docstring""" super().__init__() self.register_modules(unet=UpperCAmelCase_ , scheduler=UpperCAmelCase_ , mel=UpperCAmelCase_ , vqvae=UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" return 5_0 if isinstance(self.scheduler , UpperCAmelCase_) else 1_0_0_0 @torch.no_grad() def __call__( self : Dict , UpperCAmelCase_ : int = 1 , UpperCAmelCase_ : str = None , UpperCAmelCase_ : np.ndarray = None , UpperCAmelCase_ : int = 0 , UpperCAmelCase_ : int = 0 , UpperCAmelCase_ : int = None , UpperCAmelCase_ : torch.Generator = None , UpperCAmelCase_ : float = 0 , UpperCAmelCase_ : float = 0 , UpperCAmelCase_ : torch.Generator = None , UpperCAmelCase_ : float = 0 , UpperCAmelCase_ : torch.Tensor = None , UpperCAmelCase_ : torch.Tensor = None , UpperCAmelCase_ : Optional[Any]=True , ): """simple docstring""" a : Optional[Any] = steps or self.get_default_steps() self.scheduler.set_timesteps(UpperCAmelCase_) a : Optional[Any] = step_generator or generator # For backwards compatibility if type(self.unet.config.sample_size) == int: a : Dict = (self.unet.config.sample_size, self.unet.config.sample_size) if noise is None: a : Dict = randn_tensor( ( batch_size, self.unet.config.in_channels, self.unet.config.sample_size[0], self.unet.config.sample_size[1], ) , generator=UpperCAmelCase_ , device=self.device , ) a : Tuple = noise a : Optional[int] = None if audio_file is not None or raw_audio is not None: self.mel.load_audio(UpperCAmelCase_ , UpperCAmelCase_) a : List[Any] = self.mel.audio_slice_to_image(UpperCAmelCase_) a : str = np.frombuffer(input_image.tobytes() , dtype='uint8').reshape( (input_image.height, input_image.width)) a : List[str] = (input_image / 2_5_5) * 2 - 1 a : Any = torch.tensor(input_image[np.newaxis, :, :] , dtype=torch.float).to(self.device) if self.vqvae is not None: a : List[Any] = self.vqvae.encode(torch.unsqueeze(UpperCAmelCase_ , 0)).latent_dist.sample( generator=UpperCAmelCase_)[0] a : str = self.vqvae.config.scaling_factor * input_images if start_step > 0: a : Union[str, Any] = self.scheduler.add_noise(UpperCAmelCase_ , UpperCAmelCase_ , self.scheduler.timesteps[start_step - 1]) a : Dict = ( self.unet.config.sample_size[1] * self.mel.get_sample_rate() / self.mel.x_res / self.mel.hop_length ) a : List[Any] = int(mask_start_secs * pixels_per_second) a : Optional[Any] = int(mask_end_secs * pixels_per_second) a : Optional[int] = self.scheduler.add_noise(UpperCAmelCase_ , UpperCAmelCase_ , torch.tensor(self.scheduler.timesteps[start_step:])) for step, t in enumerate(self.progress_bar(self.scheduler.timesteps[start_step:])): if isinstance(self.unet , UpperCAmelCase_): a : Dict = self.unet(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_)['sample'] else: a : str = self.unet(UpperCAmelCase_ , UpperCAmelCase_)['sample'] if isinstance(self.scheduler , UpperCAmelCase_): a : List[Any] = self.scheduler.step( model_output=UpperCAmelCase_ , timestep=UpperCAmelCase_ , sample=UpperCAmelCase_ , eta=UpperCAmelCase_ , generator=UpperCAmelCase_ , )['prev_sample'] else: a : Any = self.scheduler.step( model_output=UpperCAmelCase_ , timestep=UpperCAmelCase_ , sample=UpperCAmelCase_ , generator=UpperCAmelCase_ , )['prev_sample'] if mask is not None: if mask_start > 0: a : str = mask[:, step, :, :mask_start] if mask_end > 0: a : Dict = mask[:, step, :, -mask_end:] if self.vqvae is not None: # 0.18215 was scaling factor used in training to ensure unit variance a : List[str] = 1 / self.vqvae.config.scaling_factor * images a : str = self.vqvae.decode(UpperCAmelCase_)['sample'] a : Tuple = (images / 2 + 0.5).clamp(0 , 1) a : Any = images.cpu().permute(0 , 2 , 3 , 1).numpy() a : List[str] = (images * 2_5_5).round().astype('uint8') a : Tuple = list( (Image.fromarray(_[:, :, 0]) for _ in images) if images.shape[3] == 1 else (Image.fromarray(UpperCAmelCase_ , mode='RGB').convert('L') for _ in images)) a : List[str] = [self.mel.image_to_audio(UpperCAmelCase_) for _ in images] if not return_dict: return images, (self.mel.get_sample_rate(), audios) return BaseOutput(**AudioPipelineOutput(np.array(UpperCAmelCase_)[:, np.newaxis, :]) , **ImagePipelineOutput(UpperCAmelCase_)) @torch.no_grad() def SCREAMING_SNAKE_CASE_ ( self : List[Any] , UpperCAmelCase_ : List[Image.Image] , UpperCAmelCase_ : int = 5_0): """simple docstring""" assert isinstance(self.scheduler , UpperCAmelCase_) self.scheduler.set_timesteps(UpperCAmelCase_) a : Dict = np.array( [np.frombuffer(image.tobytes() , dtype='uint8').reshape((1, image.height, image.width)) for image in images]) a : Tuple = (sample / 2_5_5) * 2 - 1 a : int = torch.Tensor(UpperCAmelCase_).to(self.device) for t in self.progress_bar(torch.flip(self.scheduler.timesteps , (0,))): a : Optional[Any] = t - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps a : Optional[Any] = self.scheduler.alphas_cumprod[t] a : List[Any] = ( self.scheduler.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.scheduler.final_alpha_cumprod ) a : List[str] = 1 - alpha_prod_t a : Optional[Any] = self.unet(UpperCAmelCase_ , UpperCAmelCase_)['sample'] a : Union[str, Any] = (1 - alpha_prod_t_prev) ** 0.5 * model_output a : Dict = (sample - pred_sample_direction) * alpha_prod_t_prev ** (-0.5) a : Union[str, Any] = sample * alpha_prod_t ** 0.5 + beta_prod_t ** 0.5 * model_output return sample @staticmethod def SCREAMING_SNAKE_CASE_ ( UpperCAmelCase_ : torch.Tensor , UpperCAmelCase_ : torch.Tensor , UpperCAmelCase_ : float): """simple docstring""" a : List[Any] = acos(torch.dot(torch.flatten(UpperCAmelCase_) , torch.flatten(UpperCAmelCase_)) / torch.norm(UpperCAmelCase_) / torch.norm(UpperCAmelCase_)) return sin((1 - alpha) * theta) * xa / sin(UpperCAmelCase_) + sin(alpha * theta) * xa / sin(UpperCAmelCase_)
345
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available UpperCamelCase : Dict = { """configuration_ctrl""": ["""CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP""", """CTRLConfig"""], """tokenization_ctrl""": ["""CTRLTokenizer"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Tuple = [ """CTRL_PRETRAINED_MODEL_ARCHIVE_LIST""", """CTRLForSequenceClassification""", """CTRLLMHeadModel""", """CTRLModel""", """CTRLPreTrainedModel""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : List[Any] = [ """TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFCTRLForSequenceClassification""", """TFCTRLLMHeadModel""", """TFCTRLModel""", """TFCTRLPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_ctrl import CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, CTRLConfig from .tokenization_ctrl import CTRLTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_ctrl import ( CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, CTRLForSequenceClassification, CTRLLMHeadModel, CTRLModel, CTRLPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_ctrl import ( TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, TFCTRLForSequenceClassification, TFCTRLLMHeadModel, TFCTRLModel, TFCTRLPreTrainedModel, ) else: import sys UpperCamelCase : Tuple = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
354
'''simple docstring''' import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_gpu, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class UpperCamelCase : """simple docstring""" def __init__( self : str , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : List[Any]=1_3 , UpperCAmelCase_ : List[str]=3_0 , UpperCAmelCase_ : str=2 , UpperCAmelCase_ : Union[str, Any]=3 , UpperCAmelCase_ : str=True , UpperCAmelCase_ : Union[str, Any]=True , UpperCAmelCase_ : Union[str, Any]=3_2 , UpperCAmelCase_ : Union[str, Any]=5 , UpperCAmelCase_ : Tuple=4 , UpperCAmelCase_ : Dict=3_7 , UpperCAmelCase_ : Optional[int]="gelu" , UpperCAmelCase_ : Optional[int]=0.1 , UpperCAmelCase_ : Union[str, Any]=0.1 , UpperCAmelCase_ : Dict=1_0 , UpperCAmelCase_ : Optional[Any]=0.02 , UpperCAmelCase_ : List[str]=None , UpperCAmelCase_ : Tuple=2 , ): """simple docstring""" a : Any = parent a : Optional[int] = batch_size a : str = image_size a : str = patch_size a : List[Any] = num_channels a : Optional[int] = is_training a : Dict = use_labels a : Any = hidden_size a : Optional[int] = num_hidden_layers a : int = num_attention_heads a : int = intermediate_size a : Any = hidden_act a : Optional[int] = hidden_dropout_prob a : Optional[int] = attention_probs_dropout_prob a : Dict = type_sequence_label_size a : Tuple = initializer_range a : List[str] = scope a : str = encoder_stride # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) a : Optional[Any] = (image_size // patch_size) ** 2 a : str = num_patches + 1 def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : Union[str, Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]) a : List[Any] = None if self.use_labels: a : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size) a : List[str] = self.get_config() return config, pixel_values, labels def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" return ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=UpperCAmelCase_ , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Dict): """simple docstring""" a : int = ViTModel(config=UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Optional[Any] = model(UpperCAmelCase_) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : List[Any]): """simple docstring""" a : str = ViTForMaskedImageModeling(config=UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : List[Any] = model(UpperCAmelCase_) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size)) # test greyscale images a : int = 1 a : Union[str, Any] = ViTForMaskedImageModeling(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Optional[Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]) a : Optional[Any] = model(UpperCAmelCase_) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size)) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : int , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Any): """simple docstring""" a : str = self.type_sequence_label_size a : Tuple = ViTForImageClassification(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Union[str, Any] = model(UpperCAmelCase_ , labels=UpperCAmelCase_) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) # test greyscale images a : List[Any] = 1 a : Union[str, Any] = ViTForImageClassification(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : int = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]) a : Optional[int] = model(UpperCAmelCase_) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Optional[int] = self.prepare_config_and_inputs() ( ( a ) , ( a ) , ( a ) , ) : Tuple = config_and_inputs a : str = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class UpperCamelCase ( a_ , a_ , unittest.TestCase ): """simple docstring""" A : str = ( ( ViTModel, ViTForImageClassification, ViTForMaskedImageModeling, ) if is_torch_available() else () ) A : Optional[Any] = ( {"feature-extraction": ViTModel, "image-classification": ViTForImageClassification} if is_torch_available() else {} ) A : List[str] = True A : Optional[int] = False A : Dict = False A : Optional[int] = False def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" a : str = ViTModelTester(self) a : Optional[Any] = ConfigTester(self , config_class=UpperCAmelCase_ , has_text_modality=UpperCAmelCase_ , hidden_size=3_7) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='ViT does not use inputs_embeds') def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" pass def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a , a : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a : Union[str, Any] = model_class(UpperCAmelCase_) self.assertIsInstance(model.get_input_embeddings() , (nn.Module)) a : Tuple = model.get_output_embeddings() self.assertTrue(x is None or isinstance(UpperCAmelCase_ , nn.Linear)) def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a , a : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a : Union[str, Any] = model_class(UpperCAmelCase_) a : Tuple = inspect.signature(model.forward) # signature.parameters is an OrderedDict => so arg_names order is deterministic a : Dict = [*signature.parameters.keys()] a : Dict = ['pixel_values'] self.assertListEqual(arg_names[:1] , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" a : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" a : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase_) @slow def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a : Dict = ViTModel.from_pretrained(UpperCAmelCase_) self.assertIsNotNone(UpperCAmelCase_) def SCREAMING_SNAKE_CASE__ ( ) -> Optional[Any]: """simple docstring""" a : List[Any] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class UpperCamelCase ( unittest.TestCase ): """simple docstring""" @cached_property def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224') if is_vision_available() else None @slow def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" a : Optional[Any] = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224').to(UpperCAmelCase_) a : List[Any] = self.default_image_processor a : List[str] = prepare_img() a : Tuple = image_processor(images=UpperCAmelCase_ , return_tensors='pt').to(UpperCAmelCase_) # forward pass with torch.no_grad(): a : List[Any] = model(**UpperCAmelCase_) # verify the logits a : List[str] = torch.Size((1, 1_0_0_0)) self.assertEqual(outputs.logits.shape , UpperCAmelCase_) a : Union[str, Any] = torch.tensor([-0.27_44, 0.82_15, -0.08_36]).to(UpperCAmelCase_) self.assertTrue(torch.allclose(outputs.logits[0, :3] , UpperCAmelCase_ , atol=1e-4)) @slow def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : List[str] = ViTModel.from_pretrained('facebook/dino-vits8').to(UpperCAmelCase_) a : Any = ViTImageProcessor.from_pretrained('facebook/dino-vits8' , size=4_8_0) a : int = prepare_img() a : List[str] = image_processor(images=UpperCAmelCase_ , return_tensors='pt') a : List[str] = inputs.pixel_values.to(UpperCAmelCase_) # forward pass with torch.no_grad(): a : List[Any] = model(UpperCAmelCase_ , interpolate_pos_encoding=UpperCAmelCase_) # verify the logits a : Dict = torch.Size((1, 3_6_0_1, 3_8_4)) self.assertEqual(outputs.last_hidden_state.shape , UpperCAmelCase_) a : str = torch.tensor( [[4.23_40, 4.39_06, -6.66_92], [4.54_63, 1.89_28, -6.72_57], [4.44_29, 0.84_96, -5.85_85]]).to(UpperCAmelCase_) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , UpperCAmelCase_ , atol=1e-4)) @slow @require_accelerate @require_torch_gpu def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : str = ViTModel.from_pretrained('facebook/dino-vits8' , torch_dtype=torch.floataa , device_map='auto') a : List[Any] = self.default_image_processor a : List[str] = prepare_img() a : Tuple = image_processor(images=UpperCAmelCase_ , return_tensors='pt') a : Tuple = inputs.pixel_values.to(UpperCAmelCase_) # forward pass to make sure inference works in fp16 with torch.no_grad(): a : Tuple = model(UpperCAmelCase_)
345
0
'''simple docstring''' import os import sys import unittest UpperCamelCase : str = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, """utils""")) import get_test_info # noqa: E402 from get_test_info import ( # noqa: E402 get_model_to_test_mapping, get_model_to_tester_mapping, get_test_to_tester_mapping, ) UpperCamelCase : Optional[int] = os.path.join("""tests""", """models""", """bert""", """test_modeling_bert.py""") UpperCamelCase : Dict = os.path.join("""tests""", """models""", """blip""", """test_modeling_blip.py""") class UpperCamelCase ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" a : str = get_test_to_tester_mapping(SCREAMING_SNAKE_CASE_) a : Union[str, Any] = get_test_to_tester_mapping(SCREAMING_SNAKE_CASE_) a : Union[str, Any] = {"""BertModelTest""": """BertModelTester"""} a : str = { """BlipModelTest""": """BlipModelTester""", """BlipTextImageModelTest""": """BlipTextImageModelsModelTester""", """BlipTextModelTest""": """BlipTextModelTester""", """BlipTextRetrievalModelTest""": """BlipTextRetrievalModelTester""", """BlipVQAModelTest""": """BlipVQAModelTester""", """BlipVisionModelTest""": """BlipVisionModelTester""", } self.assertEqual(get_test_info.to_json(SCREAMING_SNAKE_CASE_) , SCREAMING_SNAKE_CASE_) self.assertEqual(get_test_info.to_json(SCREAMING_SNAKE_CASE_) , SCREAMING_SNAKE_CASE_) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" a : List[str] = get_model_to_test_mapping(SCREAMING_SNAKE_CASE_) a : List[str] = get_model_to_test_mapping(SCREAMING_SNAKE_CASE_) a : Union[str, Any] = { """BertForMaskedLM""": ["""BertModelTest"""], """BertForMultipleChoice""": ["""BertModelTest"""], """BertForNextSentencePrediction""": ["""BertModelTest"""], """BertForPreTraining""": ["""BertModelTest"""], """BertForQuestionAnswering""": ["""BertModelTest"""], """BertForSequenceClassification""": ["""BertModelTest"""], """BertForTokenClassification""": ["""BertModelTest"""], """BertLMHeadModel""": ["""BertModelTest"""], """BertModel""": ["""BertModelTest"""], } a : int = { """BlipForConditionalGeneration""": ["""BlipTextImageModelTest"""], """BlipForImageTextRetrieval""": ["""BlipTextRetrievalModelTest"""], """BlipForQuestionAnswering""": ["""BlipVQAModelTest"""], """BlipModel""": ["""BlipModelTest"""], """BlipTextModel""": ["""BlipTextModelTest"""], """BlipVisionModel""": ["""BlipVisionModelTest"""], } self.assertEqual(get_test_info.to_json(SCREAMING_SNAKE_CASE_) , SCREAMING_SNAKE_CASE_) self.assertEqual(get_test_info.to_json(SCREAMING_SNAKE_CASE_) , SCREAMING_SNAKE_CASE_) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : Any = get_model_to_tester_mapping(SCREAMING_SNAKE_CASE_) a : Optional[int] = get_model_to_tester_mapping(SCREAMING_SNAKE_CASE_) a : Optional[int] = { """BertForMaskedLM""": ["""BertModelTester"""], """BertForMultipleChoice""": ["""BertModelTester"""], """BertForNextSentencePrediction""": ["""BertModelTester"""], """BertForPreTraining""": ["""BertModelTester"""], """BertForQuestionAnswering""": ["""BertModelTester"""], """BertForSequenceClassification""": ["""BertModelTester"""], """BertForTokenClassification""": ["""BertModelTester"""], """BertLMHeadModel""": ["""BertModelTester"""], """BertModel""": ["""BertModelTester"""], } a : Union[str, Any] = { """BlipForConditionalGeneration""": ["""BlipTextImageModelsModelTester"""], """BlipForImageTextRetrieval""": ["""BlipTextRetrievalModelTester"""], """BlipForQuestionAnswering""": ["""BlipVQAModelTester"""], """BlipModel""": ["""BlipModelTester"""], """BlipTextModel""": ["""BlipTextModelTester"""], """BlipVisionModel""": ["""BlipVisionModelTester"""], } self.assertEqual(get_test_info.to_json(SCREAMING_SNAKE_CASE_) , SCREAMING_SNAKE_CASE_) self.assertEqual(get_test_info.to_json(SCREAMING_SNAKE_CASE_) , SCREAMING_SNAKE_CASE_)
355
'''simple docstring''' # Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import numpy as np import torch from ..models.clipseg import CLIPSegForImageSegmentation from ..utils import is_vision_available, requires_backends from .base import PipelineTool if is_vision_available(): from PIL import Image class UpperCamelCase ( a_ ): """simple docstring""" A : List[str] = ( "This is a tool that creates a segmentation mask of an image according to a label. It cannot create an image." "It takes two arguments named `image` which should be the original image, and `label` which should be a text " "describing the elements what should be identified in the segmentation mask. The tool returns the mask." ) A : List[Any] = "CIDAS/clipseg-rd64-refined" A : Optional[Any] = "image_segmenter" A : List[Any] = CLIPSegForImageSegmentation A : Tuple = ["image", "text"] A : Optional[int] = ["image"] def __init__( self : str , *UpperCAmelCase_ : str , **UpperCAmelCase_ : str): """simple docstring""" requires_backends(self , ['vision']) super().__init__(*UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : "Image" , UpperCAmelCase_ : str): """simple docstring""" return self.pre_processor(text=[label] , images=[image] , padding=UpperCAmelCase_ , return_tensors='pt') def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : str): """simple docstring""" with torch.no_grad(): a : Union[str, Any] = self.model(**UpperCAmelCase_).logits return logits def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , UpperCAmelCase_ : int): """simple docstring""" a : int = outputs.cpu().detach().numpy() a : str = 0 a : str = 1 return Image.fromarray((array * 2_5_5).astype(np.uinta))
345
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase : Optional[Any] = logging.get_logger(__name__) UpperCamelCase : Optional[Any] = { """uw-madison/mra-base-512-4""": """https://huggingface.co/uw-madison/mra-base-512-4/resolve/main/config.json""", } class UpperCamelCase ( __UpperCamelCase ): """simple docstring""" A : Optional[Any] = """mra""" def __init__( self : List[str] , UpperCAmelCase_ : Tuple=5_0_2_6_5 , UpperCAmelCase_ : str=7_6_8 , UpperCAmelCase_ : int=1_2 , UpperCAmelCase_ : Dict=1_2 , UpperCAmelCase_ : Tuple=3_0_7_2 , UpperCAmelCase_ : str="gelu" , UpperCAmelCase_ : Optional[Any]=0.1 , UpperCAmelCase_ : Optional[int]=0.1 , UpperCAmelCase_ : List[str]=5_1_2 , UpperCAmelCase_ : Optional[Any]=1 , UpperCAmelCase_ : Tuple=0.02 , UpperCAmelCase_ : int=1e-5 , UpperCAmelCase_ : Optional[int]="absolute" , UpperCAmelCase_ : Optional[Any]=4 , UpperCAmelCase_ : Any="full" , UpperCAmelCase_ : Optional[Any]=0 , UpperCAmelCase_ : List[str]=0 , UpperCAmelCase_ : Any=1 , UpperCAmelCase_ : int=0 , UpperCAmelCase_ : int=2 , **UpperCAmelCase_ : Tuple , ): """simple docstring""" super().__init__(pad_token_id=UpperCAmelCase_ , bos_token_id=UpperCAmelCase_ , eos_token_id=UpperCAmelCase_ , **UpperCAmelCase_) a : Union[str, Any] = vocab_size a : Optional[int] = max_position_embeddings a : Any = hidden_size a : List[Any] = num_hidden_layers a : Tuple = num_attention_heads a : List[Any] = intermediate_size a : Dict = hidden_act a : Optional[Any] = hidden_dropout_prob a : Any = attention_probs_dropout_prob a : str = initializer_range a : str = type_vocab_size a : str = layer_norm_eps a : Optional[int] = position_embedding_type a : Any = block_per_row a : int = approx_mode a : Union[str, Any] = initial_prior_first_n_blocks a : Dict = initial_prior_diagonal_n_blocks
356
'''simple docstring''' import json import os import unittest from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer from ...test_tokenization_common import TokenizerTesterMixin class UpperCamelCase ( a_ , unittest.TestCase ): """simple docstring""" A : Union[str, Any] = CTRLTokenizer A : List[Any] = False A : Optional[Any] = False def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt a : Dict = ['adapt', 're@@', 'a@@', 'apt', 'c@@', 't', '<unk>'] a : Tuple = dict(zip(UpperCAmelCase_ , range(len(UpperCAmelCase_)))) a : Any = ['#version: 0.2', 'a p', 'ap t</w>', 'r e', 'a d', 'ad apt</w>', ''] a : List[Any] = {'unk_token': '<unk>'} a : Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file']) a : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['merges_file']) with open(self.vocab_file , 'w' , encoding='utf-8') as fp: fp.write(json.dumps(UpperCAmelCase_) + '\n') with open(self.merges_file , 'w' , encoding='utf-8') as fp: fp.write('\n'.join(UpperCAmelCase_)) def SCREAMING_SNAKE_CASE_ ( self : Tuple , **UpperCAmelCase_ : Dict): """simple docstring""" kwargs.update(self.special_tokens_map) return CTRLTokenizer.from_pretrained(self.tmpdirname , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : Any): """simple docstring""" a : List[str] = 'adapt react readapt apt' a : int = 'adapt react readapt apt' return input_text, output_text def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : int = CTRLTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map) a : str = 'adapt react readapt apt' a : Optional[Any] = 'adapt re@@ a@@ c@@ t re@@ adapt apt'.split() a : List[Any] = tokenizer.tokenize(UpperCAmelCase_) self.assertListEqual(UpperCAmelCase_ , UpperCAmelCase_) a : Union[str, Any] = tokens + [tokenizer.unk_token] a : Any = [0, 1, 2, 4, 5, 1, 0, 3, 6] self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCAmelCase_) , UpperCAmelCase_)
345
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCamelCase : Optional[Any] = {"""configuration_vit_msn""": ["""VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP""", """ViTMSNConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : List[str] = [ """VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST""", """ViTMSNModel""", """ViTMSNForImageClassification""", """ViTMSNPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit_msn import ( VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST, ViTMSNForImageClassification, ViTMSNModel, ViTMSNPreTrainedModel, ) else: import sys UpperCamelCase : Dict = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
357
'''simple docstring''' def SCREAMING_SNAKE_CASE__ ( snake_case : int , snake_case : int ) -> bool: """simple docstring""" return numa ^ numa < 0 if __name__ == "__main__": import doctest doctest.testmod()
345
0
'''simple docstring''' import multiprocessing import time from arguments import PretokenizationArguments from datasets import load_dataset from transformers import AutoTokenizer, HfArgumentParser def SCREAMING_SNAKE_CASE__ ( snake_case : Union[str, Any] ): """simple docstring""" a : Tuple = {} a : Dict = tokenizer(example['content'] , truncation=snake_case )['input_ids'] a : Optional[Any] = len(example['content'] ) / len(output['input_ids'] ) return output UpperCamelCase : List[str] = HfArgumentParser(PretokenizationArguments) UpperCamelCase : Tuple = parser.parse_args() if args.num_workers is None: UpperCamelCase : Dict = multiprocessing.cpu_count() UpperCamelCase : int = AutoTokenizer.from_pretrained(args.tokenizer_dir) UpperCamelCase : int = time.time() UpperCamelCase : Optional[int] = load_dataset(args.dataset_name, split="""train""") print(f'''Dataset loaded in {time.time()-t_start:.2f}s''') UpperCamelCase : str = time.time() UpperCamelCase : Any = ds.map( tokenize, num_proc=args.num_workers, remove_columns=[ """repo_name""", """path""", """copies""", """size""", """content""", """license""", """hash""", """line_mean""", """line_max""", """alpha_frac""", """autogenerated""", ], ) print(f'''Dataset tokenized in {time.time()-t_start:.2f}s''') UpperCamelCase : Optional[Any] = time.time() ds.push_to_hub(args.tokenized_data_repo) print(f'''Data pushed to the hub in {time.time()-t_start:.2f}s''')
358
'''simple docstring''' from typing import Dict, Iterable, 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, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, logging UpperCamelCase : int = logging.get_logger(__name__) class UpperCamelCase ( a_ ): """simple docstring""" A : Dict = ["pixel_values"] def __init__( self : Optional[Any] , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Dict[str, int] = None , UpperCAmelCase_ : PILImageResampling = PILImageResampling.BICUBIC , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Dict[str, int] = None , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Union[int, float] = 1 / 2_5_5 , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Optional[Union[float, Iterable[float]]] = IMAGENET_DEFAULT_MEAN , UpperCAmelCase_ : Optional[Union[float, Iterable[float]]] = IMAGENET_DEFAULT_STD , **UpperCAmelCase_ : List[Any] , ): """simple docstring""" super().__init__(**UpperCAmelCase_) a : List[str] = size if size is not None else {'shortest_edge': 2_2_4} a : str = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) a : str = crop_size if crop_size is not None else {'height': 2_2_4, 'width': 2_2_4} a : int = get_size_dict(UpperCAmelCase_ , param_name='crop_size') a : Any = do_resize a : Dict = size a : Optional[Any] = resample a : List[Any] = do_center_crop a : List[Any] = crop_size a : Optional[Any] = do_rescale a : Dict = rescale_factor a : Tuple = do_normalize a : int = image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN a : Optional[Any] = image_std if image_std is not None else IMAGENET_DEFAULT_STD def SCREAMING_SNAKE_CASE_ ( self : List[Any] , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Dict[str, int] , UpperCAmelCase_ : PILImageResampling = PILImageResampling.BICUBIC , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Any , ): """simple docstring""" a : Optional[Any] = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) # size_dict is a dict with either keys "height" and "width" or "shortest_edge" if "shortest_edge" in size: a : int = int((2_5_6 / 2_2_4) * size['shortest_edge']) a : Optional[int] = get_resize_output_image_size(UpperCAmelCase_ , size=UpperCAmelCase_ , default_to_square=UpperCAmelCase_) a : Optional[Any] = {'height': output_size[0], 'width': output_size[1]} if "height" not in size_dict or "width" not in size_dict: raise ValueError( f"""Size dict must have keys 'height' and 'width' or 'shortest_edge'. Got {size_dict.keys()}""") return resize( UpperCAmelCase_ , size=(size_dict['height'], size_dict['width']) , resample=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Dict[str, int] , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Union[str, Any] , ): """simple docstring""" a : str = get_size_dict(UpperCAmelCase_) if "height" not in size or "width" not in size: raise ValueError(f"""Size dict must have keys 'height' and 'width'. Got {size.keys()}""") return center_crop(UpperCAmelCase_ , size=(size['height'], size['width']) , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Union[int, float] , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Optional[Any] , ): """simple docstring""" return rescale(UpperCAmelCase_ , scale=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Union[float, List[float]] , UpperCAmelCase_ : Union[float, List[float]] , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : List[str] , ): """simple docstring""" return normalize(UpperCAmelCase_ , mean=UpperCAmelCase_ , std=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , UpperCAmelCase_ : ImageInput , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[Dict[str, int]] = None , UpperCAmelCase_ : PILImageResampling = None , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[Dict[str, int]] = None , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[float] = None , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[Union[float, Iterable[float]]] = None , UpperCAmelCase_ : Optional[Union[float, Iterable[float]]] = None , UpperCAmelCase_ : Optional[TensorType] = None , UpperCAmelCase_ : ChannelDimension = ChannelDimension.FIRST , **UpperCAmelCase_ : Optional[Any] , ): """simple docstring""" a : int = do_resize if do_resize is not None else self.do_resize a : Optional[int] = resample if resample is not None else self.resample a : Tuple = do_center_crop if do_center_crop is not None else self.do_center_crop a : Tuple = do_rescale if do_rescale is not None else self.do_rescale a : List[Any] = rescale_factor if rescale_factor is not None else self.rescale_factor a : Dict = do_normalize if do_normalize is not None else self.do_normalize a : Tuple = image_mean if image_mean is not None else self.image_mean a : int = image_std if image_std is not None else self.image_std a : Optional[int] = size if size is not None else self.size a : Optional[Any] = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) a : List[Any] = crop_size if crop_size is not None else self.crop_size a : str = get_size_dict(UpperCAmelCase_ , param_name='crop_size') a : Dict = make_list_of_images(UpperCAmelCase_) if not valid_images(UpperCAmelCase_): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.') if do_resize and size is None: raise ValueError('Size must be specified if do_resize is True.') if do_center_crop and crop_size is None: raise ValueError('Crop size must be specified if do_center_crop is True.') if do_rescale and rescale_factor is None: raise ValueError('Rescale factor must be specified if do_rescale is True.') if do_normalize and (image_mean is None or image_std is None): raise ValueError('Image mean and std must be specified if do_normalize is True.') # All transformations expect numpy arrays. a : Any = [to_numpy_array(UpperCAmelCase_) for image in images] if do_resize: a : Optional[int] = [self.resize(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_) for image in images] if do_center_crop: a : int = [self.center_crop(UpperCAmelCase_ , UpperCAmelCase_) for image in images] if do_rescale: a : Any = [self.rescale(UpperCAmelCase_ , UpperCAmelCase_) for image in images] if do_normalize: a : str = [self.normalize(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_) for image in images] a : Optional[int] = [to_channel_dimension_format(UpperCAmelCase_ , UpperCAmelCase_) for image in images] a : Optional[int] = {'pixel_values': images} return BatchFeature(data=UpperCAmelCase_ , tensor_type=UpperCAmelCase_)
345
0
'''simple docstring''' def SCREAMING_SNAKE_CASE__ ( snake_case : str , snake_case : str ) -> float: def get_matched_characters(snake_case : str , snake_case : str ) -> str: a : Optional[int] = [] a : Dict = min(len(_stra ) , len(_stra ) ) // 2 for i, l in enumerate(_stra ): a : Tuple = int(max(0 , i - limit ) ) a : List[str] = int(min(i + limit + 1 , len(_stra ) ) ) if l in _stra[left:right]: matched.append(_UpperCamelCase ) a : List[Any] = F"""{_stra[0:_stra.index(_UpperCamelCase )]} {_stra[_stra.index(_UpperCamelCase ) + 1:]}""" return "".join(_UpperCamelCase ) # matching characters a : Tuple = get_matched_characters(_UpperCamelCase , _UpperCamelCase ) a : Dict = get_matched_characters(_UpperCamelCase , _UpperCamelCase ) a : List[str] = len(_UpperCamelCase ) # transposition a : Optional[int] = ( len([(ca, ca) for ca, ca in zip(_UpperCamelCase , _UpperCamelCase ) if ca != ca] ) // 2 ) if not match_count: a : Optional[int] = 0.0 else: a : Optional[int] = ( 1 / 3 * ( match_count / len(_UpperCamelCase ) + match_count / len(_UpperCamelCase ) + (match_count - transpositions) / match_count ) ) # common prefix up to 4 characters a : Tuple = 0 for ca, ca in zip(stra[:4] , stra[:4] ): if ca == ca: prefix_len += 1 else: break return jaro + 0.1 * prefix_len * (1 - jaro) if __name__ == "__main__": import doctest doctest.testmod() print(jaro_winkler("""hello""", """world"""))
359
'''simple docstring''' from __future__ import annotations from decimal import Decimal from math import * # noqa: F403 from sympy import diff def SCREAMING_SNAKE_CASE__ ( snake_case : str , snake_case : float | Decimal , snake_case : float = 10**-10 ) -> float: """simple docstring""" a : Dict = a while True: a : Any = Decimal(snake_case ) - ( Decimal(eval(snake_case ) ) / Decimal(eval(str(diff(snake_case ) ) ) ) # noqa: S307 ) # This number dictates the accuracy of the answer if abs(eval(snake_case ) ) < precision: # noqa: S307 return float(snake_case ) # Let's Execute if __name__ == "__main__": # Find root of trigonometric function # Find value of pi print(f'''The root of sin(x) = 0 is {newton_raphson("sin(x)", 2)}''') # Find root of polynomial print(f'''The root of x**2 - 5*x + 2 = 0 is {newton_raphson("x**2 - 5*x + 2", 0.4)}''') # Find Square Root of 5 print(f'''The root of log(x) - 1 = 0 is {newton_raphson("log(x) - 1", 2)}''') # Exponential Roots print(f'''The root of exp(x) - 1 = 0 is {newton_raphson("exp(x) - 1", 0)}''')
345
0
'''simple docstring''' import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class UpperCamelCase ( a_ ): """simple docstring""" A : Optional[int] = ['image_processor', 'tokenizer'] A : Optional[Any] = 'CLIPImageProcessor' A : int = ('CLIPTokenizer', 'CLIPTokenizerFast') def __init__( self : str , UpperCAmelCase_ : Optional[int]=None , UpperCAmelCase_ : int=None , **UpperCAmelCase_ : List[str]): """simple docstring""" a : Tuple = None if "feature_extractor" in kwargs: warnings.warn( 'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`' ' instead.' , __lowerCAmelCase , ) a : Optional[int] = kwargs.pop('feature_extractor') a : Optional[Any] = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('You need to specify an `image_processor`.') if tokenizer is None: raise ValueError('You need to specify a `tokenizer`.') super().__init__(__lowerCAmelCase , __lowerCAmelCase) def __call__( self : List[Any] , UpperCAmelCase_ : Any=None , UpperCAmelCase_ : str=None , UpperCAmelCase_ : Optional[Any]=None , **UpperCAmelCase_ : Tuple): """simple docstring""" if text is None and images is None: raise ValueError('You have to specify either text or images. Both cannot be none.') if text is not None: a : Any = self.tokenizer(__lowerCAmelCase , return_tensors=__lowerCAmelCase , **__lowerCAmelCase) if images is not None: a : int = self.image_processor(__lowerCAmelCase , return_tensors=__lowerCAmelCase , **__lowerCAmelCase) if text is not None and images is not None: a : List[Any] = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**__lowerCAmelCase) , tensor_type=__lowerCAmelCase) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , *UpperCAmelCase_ : str , **UpperCAmelCase_ : str): """simple docstring""" return self.tokenizer.batch_decode(*__lowerCAmelCase , **__lowerCAmelCase) def SCREAMING_SNAKE_CASE_ ( self : Tuple , *UpperCAmelCase_ : str , **UpperCAmelCase_ : Dict): """simple docstring""" return self.tokenizer.decode(*__lowerCAmelCase , **__lowerCAmelCase) @property def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" a : Dict = self.tokenizer.model_input_names a : Optional[Any] = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names)) @property def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" warnings.warn( '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.' , __lowerCAmelCase , ) return self.image_processor_class @property def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" warnings.warn( '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.' , __lowerCAmelCase , ) return self.image_processor
360
'''simple docstring''' import unittest from transformers import XLMConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( XLMForMultipleChoice, XLMForQuestionAnswering, XLMForQuestionAnsweringSimple, XLMForSequenceClassification, XLMForTokenClassification, XLMModel, XLMWithLMHeadModel, ) from transformers.models.xlm.modeling_xlm import XLM_PRETRAINED_MODEL_ARCHIVE_LIST class UpperCamelCase : """simple docstring""" def __init__( self : Union[str, Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Optional[int]=1_3 , UpperCAmelCase_ : Dict=7 , UpperCAmelCase_ : int=True , UpperCAmelCase_ : Tuple=True , UpperCAmelCase_ : Any=True , UpperCAmelCase_ : List[Any]=True , UpperCAmelCase_ : List[Any]=True , UpperCAmelCase_ : List[Any]=False , UpperCAmelCase_ : List[Any]=False , UpperCAmelCase_ : Union[str, Any]=False , UpperCAmelCase_ : Union[str, Any]=2 , UpperCAmelCase_ : Optional[Any]=9_9 , UpperCAmelCase_ : str=0 , UpperCAmelCase_ : int=3_2 , UpperCAmelCase_ : Tuple=5 , UpperCAmelCase_ : Any=4 , UpperCAmelCase_ : Tuple=0.1 , UpperCAmelCase_ : int=0.1 , UpperCAmelCase_ : Optional[int]=5_1_2 , UpperCAmelCase_ : str=2 , UpperCAmelCase_ : Any=0.02 , UpperCAmelCase_ : str=2 , UpperCAmelCase_ : Optional[int]=4 , UpperCAmelCase_ : int="last" , UpperCAmelCase_ : Union[str, Any]=True , UpperCAmelCase_ : Tuple=None , UpperCAmelCase_ : List[str]=0 , ): """simple docstring""" a : Tuple = parent a : Optional[Any] = batch_size a : Tuple = seq_length a : Union[str, Any] = is_training a : List[str] = use_input_lengths a : Union[str, Any] = use_token_type_ids a : Optional[int] = use_labels a : int = gelu_activation a : Dict = sinusoidal_embeddings a : Any = causal a : Optional[int] = asm a : int = n_langs a : List[str] = vocab_size a : List[str] = n_special a : List[str] = hidden_size a : Any = num_hidden_layers a : Union[str, Any] = num_attention_heads a : Optional[Any] = hidden_dropout_prob a : str = attention_probs_dropout_prob a : Dict = max_position_embeddings a : Union[str, Any] = type_sequence_label_size a : str = initializer_range a : List[Any] = num_labels a : Union[str, Any] = num_choices a : Optional[Any] = summary_type a : Optional[Any] = use_proj a : Optional[Any] = scope a : Dict = bos_token_id def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size) a : List[Any] = random_attention_mask([self.batch_size, self.seq_length]) a : Optional[int] = None if self.use_input_lengths: a : Optional[int] = ( ids_tensor([self.batch_size] , vocab_size=2) + self.seq_length - 2 ) # small variation of seq_length a : int = None if self.use_token_type_ids: a : str = ids_tensor([self.batch_size, self.seq_length] , self.n_langs) a : Optional[Any] = None a : Tuple = None a : Optional[Any] = None if self.use_labels: a : Optional[int] = ids_tensor([self.batch_size] , self.type_sequence_label_size) a : int = ids_tensor([self.batch_size, self.seq_length] , self.num_labels) a : Optional[Any] = ids_tensor([self.batch_size] , 2).float() a : Dict = ids_tensor([self.batch_size] , self.num_choices) a : Any = self.get_config() return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" return XLMConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , num_labels=self.num_labels , bos_token_id=self.bos_token_id , ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : str , UpperCAmelCase_ : Any , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : str , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[Any] , ): """simple docstring""" a : Any = XLMModel(config=UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : int = model(UpperCAmelCase_ , lengths=UpperCAmelCase_ , langs=UpperCAmelCase_) a : str = model(UpperCAmelCase_ , langs=UpperCAmelCase_) a : int = model(UpperCAmelCase_) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : str , UpperCAmelCase_ : int , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : Any , UpperCAmelCase_ : List[Any] , ): """simple docstring""" a : Optional[Any] = XLMWithLMHeadModel(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : List[str] = model(UpperCAmelCase_ , token_type_ids=UpperCAmelCase_ , labels=UpperCAmelCase_) self.parent.assertEqual(result.loss.shape , ()) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size)) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : Dict , UpperCAmelCase_ : int , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : str , UpperCAmelCase_ : Optional[Any] , ): """simple docstring""" a : Union[str, Any] = XLMForQuestionAnsweringSimple(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : List[str] = model(UpperCAmelCase_) a : Tuple = model(UpperCAmelCase_ , start_positions=UpperCAmelCase_ , end_positions=UpperCAmelCase_) a : Any = outputs self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length)) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length)) def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Any , UpperCAmelCase_ : Any , ): """simple docstring""" a : Any = XLMForQuestionAnswering(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : int = model(UpperCAmelCase_) a : Dict = model( UpperCAmelCase_ , start_positions=UpperCAmelCase_ , end_positions=UpperCAmelCase_ , cls_index=UpperCAmelCase_ , is_impossible=UpperCAmelCase_ , p_mask=UpperCAmelCase_ , ) a : int = model( UpperCAmelCase_ , start_positions=UpperCAmelCase_ , end_positions=UpperCAmelCase_ , cls_index=UpperCAmelCase_ , is_impossible=UpperCAmelCase_ , ) ((a) , ) : Union[str, Any] = result_with_labels.to_tuple() a : int = model(UpperCAmelCase_ , start_positions=UpperCAmelCase_ , end_positions=UpperCAmelCase_) ((a) , ) : Union[str, Any] = result_with_labels.to_tuple() self.parent.assertEqual(result_with_labels.loss.shape , ()) self.parent.assertEqual(result.start_top_log_probs.shape , (self.batch_size, model.config.start_n_top)) self.parent.assertEqual(result.start_top_index.shape , (self.batch_size, model.config.start_n_top)) self.parent.assertEqual( result.end_top_log_probs.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top)) self.parent.assertEqual( result.end_top_index.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top)) self.parent.assertEqual(result.cls_logits.shape , (self.batch_size,)) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : int , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : str , ): """simple docstring""" a : Dict = XLMForSequenceClassification(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Union[str, Any] = model(UpperCAmelCase_) a : Union[str, Any] = model(UpperCAmelCase_ , labels=UpperCAmelCase_) self.parent.assertEqual(result.loss.shape , ()) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , UpperCAmelCase_ : str , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : str , UpperCAmelCase_ : Any , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Any , ): """simple docstring""" a : Dict = self.num_labels a : int = XLMForTokenClassification(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Optional[Any] = model(UpperCAmelCase_ , attention_mask=UpperCAmelCase_ , labels=UpperCAmelCase_) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels)) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : str , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Dict , UpperCAmelCase_ : str , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Dict , ): """simple docstring""" a : str = self.num_choices a : Dict = XLMForMultipleChoice(config=UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Union[str, Any] = input_ids.unsqueeze(1).expand(-1 , self.num_choices , -1).contiguous() a : str = token_type_ids.unsqueeze(1).expand(-1 , self.num_choices , -1).contiguous() a : Optional[int] = input_mask.unsqueeze(1).expand(-1 , self.num_choices , -1).contiguous() a : Any = model( UpperCAmelCase_ , attention_mask=UpperCAmelCase_ , token_type_ids=UpperCAmelCase_ , labels=UpperCAmelCase_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices)) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" a : int = self.prepare_config_and_inputs() ( ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ) : Union[str, Any] = config_and_inputs a : Optional[int] = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'lengths': input_lengths} return config, inputs_dict @require_torch class UpperCamelCase ( a_ , a_ , a_ , unittest.TestCase ): """simple docstring""" A : int = ( ( XLMModel, XLMWithLMHeadModel, XLMForQuestionAnswering, XLMForSequenceClassification, XLMForQuestionAnsweringSimple, XLMForTokenClassification, XLMForMultipleChoice, ) if is_torch_available() else () ) A : List[str] = ( (XLMWithLMHeadModel,) if is_torch_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable A : Optional[Any] = ( { "feature-extraction": XLMModel, "fill-mask": XLMWithLMHeadModel, "question-answering": XLMForQuestionAnsweringSimple, "text-classification": XLMForSequenceClassification, "text-generation": XLMWithLMHeadModel, "token-classification": XLMForTokenClassification, "zero-shot": XLMForSequenceClassification, } if is_torch_available() else {} ) def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Any , UpperCAmelCase_ : Any): """simple docstring""" if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith('Fast') ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : int , UpperCAmelCase_ : Dict=False): """simple docstring""" a : List[Any] = super()._prepare_for_class(UpperCAmelCase_ , UpperCAmelCase_ , return_labels=UpperCAmelCase_) if return_labels: if model_class.__name__ == "XLMForQuestionAnswering": a : Optional[int] = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=UpperCAmelCase_) a : str = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=UpperCAmelCase_) return inputs_dict def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : List[Any] = XLMModelTester(self) a : Tuple = ConfigTester(self , config_class=UpperCAmelCase_ , emb_dim=3_7) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_model(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_lm_head(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_simple_qa(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_qa(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_sequence_classif(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_token_classif(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" a : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_for_multiple_choice(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : int , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Any=False , UpperCAmelCase_ : Optional[Any]=1): """simple docstring""" self.assertIsInstance(UpperCAmelCase_ , UpperCAmelCase_) self.assertListEqual( [isinstance(UpperCAmelCase_ , UpperCAmelCase_) for iter_attentions in attentions] , [True] * len(UpperCAmelCase_)) self.assertEqual(len(UpperCAmelCase_) , (max_length - min_length) * num_beam_groups) for idx, iter_attentions in enumerate(UpperCAmelCase_): # adds PAD dummy token a : List[str] = min_length + idx + 1 a : Optional[Any] = min_length + idx + 1 a : Optional[int] = ( batch_size * num_beam_groups, config.num_attention_heads, tgt_len, src_len, ) # check attn size self.assertListEqual( [layer_attention.shape for layer_attention in iter_attentions] , [expected_shape] * len(UpperCAmelCase_)) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : str=False , UpperCAmelCase_ : Union[str, Any]=1): """simple docstring""" self.assertIsInstance(UpperCAmelCase_ , UpperCAmelCase_) self.assertListEqual( [isinstance(UpperCAmelCase_ , UpperCAmelCase_) for iter_hidden_states in hidden_states] , [True] * len(UpperCAmelCase_) , ) self.assertEqual(len(UpperCAmelCase_) , (max_length - min_length) * num_beam_groups) for idx, iter_hidden_states in enumerate(UpperCAmelCase_): # adds PAD dummy token a : int = min_length + idx + 1 a : Any = (batch_size * num_beam_groups, seq_len, config.hidden_size) # check hidden size self.assertListEqual( [layer_hidden_states.shape for layer_hidden_states in iter_hidden_states] , [expected_shape] * len(UpperCAmelCase_) , ) pass @slow def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a : Union[str, Any] = XLMModel.from_pretrained(UpperCAmelCase_) self.assertIsNotNone(UpperCAmelCase_) @require_torch class UpperCamelCase ( unittest.TestCase ): """simple docstring""" @slow def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" a : Dict = XLMWithLMHeadModel.from_pretrained('xlm-mlm-en-2048') model.to(UpperCAmelCase_) a : List[Any] = torch.tensor([[1_4, 4_4_7]] , dtype=torch.long , device=UpperCAmelCase_) # the president a : Dict = [ 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, ] # the president the president the president the president the president the president the president the president the president the president # TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference a : Optional[Any] = model.generate(UpperCAmelCase_ , do_sample=UpperCAmelCase_) self.assertListEqual(output_ids[0].cpu().numpy().tolist() , UpperCAmelCase_)
345
0
'''simple docstring''' # DISCLAIMER: This code is strongly influenced by https://github.com/pesser/pytorch_diffusion # and https://github.com/hojonathanho/diffusion import math from dataclasses import dataclass from typing import List, Optional, Tuple, Union import numpy as np import torch from diffusers.configuration_utils import ConfigMixin, register_to_config from diffusers.schedulers.scheduling_utils import SchedulerMixin from diffusers.utils import BaseOutput, deprecate @dataclass # Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->DDIM class UpperCamelCase ( a_ ): """simple docstring""" A : Any = 42 A : List[str] = None def SCREAMING_SNAKE_CASE__ ( snake_case : Tuple , snake_case : List[Any]=0.9_99 , snake_case : Optional[Any]="cosine" , ) -> List[str]: """simple docstring""" if alpha_transform_type == "cosine": def alpha_bar_fn(snake_case : Dict ): return math.cos((t + 0.0_08) / 1.0_08 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(snake_case : Any ): return math.exp(t * -12.0 ) else: raise ValueError(F"""Unsupported alpha_tranform_type: {alpha_transform_type}""" ) a : List[str] = [] for i in range(a__ ): a : Optional[Any] = i / num_diffusion_timesteps a : Dict = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(a__ ) / alpha_bar_fn(a__ ) , a__ ) ) return torch.tensor(a__ , dtype=torch.floataa ) class UpperCamelCase ( a_ , a_ ): """simple docstring""" A : List[str] = 1 @register_to_config def __init__( self : int , UpperCAmelCase_ : List[Any] = 1_0_0_0 , UpperCAmelCase_ : List[str] = 0.00_01 , UpperCAmelCase_ : Union[str, Any] = 0.02 , UpperCAmelCase_ : Optional[int] = "linear" , UpperCAmelCase_ : Union[str, Any] = None , UpperCAmelCase_ : Union[str, Any] = True , UpperCAmelCase_ : List[str] = True , UpperCAmelCase_ : Optional[Any] = 0 , UpperCAmelCase_ : Tuple = "epsilon" , UpperCAmelCase_ : Union[str, Any] = 1.0 , **UpperCAmelCase_ : Tuple , ): """simple docstring""" if kwargs.get('set_alpha_to_one' , _snake_case) is not None: a : List[str] = ( 'The `set_alpha_to_one` argument is deprecated. Please use `set_alpha_to_zero` instead.' ) deprecate('set_alpha_to_one' , '1.0.0' , _snake_case , standard_warn=_snake_case) a : Optional[Any] = kwargs['set_alpha_to_one'] if trained_betas is not None: a : Union[str, Any] = torch.tensor(_snake_case , dtype=torch.floataa) elif beta_schedule == "linear": a : Optional[int] = torch.linspace(_snake_case , _snake_case , _snake_case , dtype=torch.floataa) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. a : str = ( torch.linspace(beta_start**0.5 , beta_end**0.5 , _snake_case , dtype=torch.floataa) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule a : List[str] = betas_for_alpha_bar(_snake_case) else: raise NotImplementedError(f"""{beta_schedule} does is not implemented for {self.__class__}""") a : Dict = 1.0 - self.betas a : List[str] = torch.cumprod(self.alphas , dim=0) # At every step in inverted ddim, we are looking into the next alphas_cumprod # For the final step, there is no next alphas_cumprod, and the index is out of bounds # `set_alpha_to_zero` decides whether we set this parameter simply to zero # in this case, self.step() just output the predicted noise # or whether we use the final alpha of the "non-previous" one. a : Optional[int] = torch.tensor(0.0) if set_alpha_to_zero else self.alphas_cumprod[-1] # standard deviation of the initial noise distribution a : Dict = 1.0 # setable values a : int = None a : str = torch.from_numpy(np.arange(0 , _snake_case).copy().astype(np.intaa)) def SCREAMING_SNAKE_CASE_ ( self : Any , UpperCAmelCase_ : Any , UpperCAmelCase_ : List[Any] = None): """simple docstring""" return sample def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Union[str, Any] = None): """simple docstring""" if num_inference_steps > self.config.num_train_timesteps: raise ValueError( f"""`num_inference_steps`: {num_inference_steps} cannot be larger than `self.config.train_timesteps`:""" f""" {self.config.num_train_timesteps} as the unet model trained with this scheduler can only handle""" f""" maximal {self.config.num_train_timesteps} timesteps.""") a : List[str] = num_inference_steps a : Tuple = self.config.num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 a : Optional[Any] = (np.arange(0 , _snake_case) * step_ratio).round().copy().astype(np.intaa) a : Dict = torch.from_numpy(_snake_case).to(_snake_case) self.timesteps += self.config.steps_offset def SCREAMING_SNAKE_CASE_ ( self : int , UpperCAmelCase_ : int , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Any , UpperCAmelCase_ : int = 0.0 , UpperCAmelCase_ : List[str] = False , UpperCAmelCase_ : Dict = None , UpperCAmelCase_ : List[Any] = True , ): """simple docstring""" a : Optional[Any] = timestep + self.config.num_train_timesteps // self.num_inference_steps # 2. compute alphas, betas # change original implementation to exactly match noise levels for analogous forward process a : Optional[Any] = self.alphas_cumprod[timestep] a : List[Any] = ( self.alphas_cumprod[prev_timestep] if prev_timestep < self.config.num_train_timesteps else self.final_alpha_cumprod ) a : List[Any] = 1 - alpha_prod_t # 3. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf if self.config.prediction_type == "epsilon": a : Any = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 a : Optional[Any] = model_output elif self.config.prediction_type == "sample": a : Any = model_output a : str = (sample - alpha_prod_t ** 0.5 * pred_original_sample) / beta_prod_t ** 0.5 elif self.config.prediction_type == "v_prediction": a : Union[str, Any] = (alpha_prod_t**0.5) * sample - (beta_prod_t**0.5) * model_output a : Union[str, Any] = (alpha_prod_t**0.5) * model_output + (beta_prod_t**0.5) * sample else: raise ValueError( f"""prediction_type given as {self.config.prediction_type} must be one of `epsilon`, `sample`, or""" ' `v_prediction`') # 4. Clip or threshold "predicted x_0" if self.config.clip_sample: a : List[Any] = pred_original_sample.clamp( -self.config.clip_sample_range , self.config.clip_sample_range) # 5. compute "direction pointing to x_t" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf a : Optional[Any] = (1 - alpha_prod_t_prev) ** 0.5 * pred_epsilon # 6. compute x_t without "random noise" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf a : Optional[int] = alpha_prod_t_prev ** 0.5 * pred_original_sample + pred_sample_direction if not return_dict: return (prev_sample, pred_original_sample) return DDIMSchedulerOutput(prev_sample=_snake_case , pred_original_sample=_snake_case) def __len__( self : List[Any]): """simple docstring""" return self.config.num_train_timesteps
361
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, is_vision_available, ) UpperCamelCase : List[str] = {"""processing_layoutxlm""": ["""LayoutXLMProcessor"""]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Any = ["""LayoutXLMTokenizer"""] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Optional[int] = ["""LayoutXLMTokenizerFast"""] if TYPE_CHECKING: from .processing_layoutxlm import LayoutXLMProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutxlm import LayoutXLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutxlm_fast import LayoutXLMTokenizerFast else: import sys UpperCamelCase : Dict = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
345
0
'''simple docstring''' import argparse import torch from transformers import LxmertConfig, LxmertForPreTraining, load_tf_weights_in_lxmert from transformers.utils import logging logging.set_verbosity_info() def SCREAMING_SNAKE_CASE__ ( snake_case : List[str] , snake_case : Union[str, Any] , snake_case : Any ) -> str: """simple docstring""" a : str = LxmertConfig.from_json_file(lowercase_ ) print(F"""Building PyTorch model from configuration: {config}""" ) a : int = LxmertForPreTraining(lowercase_ ) # Load weights from tf checkpoint load_tf_weights_in_lxmert(lowercase_ , lowercase_ , lowercase_ ) # Save pytorch-model print(F"""Save PyTorch model to {pytorch_dump_path}""" ) torch.save(model.state_dict() , lowercase_ ) if __name__ == "__main__": UpperCamelCase : str = argparse.ArgumentParser() # Required parameters parser.add_argument( """--tf_checkpoint_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path.""" ) parser.add_argument( """--config_file""", default=None, type=str, required=True, help="""The config json file corresponding to the pre-trained model. \nThis specifies the model architecture.""", ) parser.add_argument( """--pytorch_dump_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model.""" ) UpperCamelCase : Dict = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path)
362
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCamelCase : Tuple = { """configuration_pegasus_x""": ["""PEGASUS_X_PRETRAINED_CONFIG_ARCHIVE_MAP""", """PegasusXConfig"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Union[str, Any] = [ """PEGASUS_X_PRETRAINED_MODEL_ARCHIVE_LIST""", """PegasusXForConditionalGeneration""", """PegasusXModel""", """PegasusXPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_pegasus_x import PEGASUS_X_PRETRAINED_CONFIG_ARCHIVE_MAP, PegasusXConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_pegasus_x import ( PEGASUS_X_PRETRAINED_MODEL_ARCHIVE_LIST, PegasusXForConditionalGeneration, PegasusXModel, PegasusXPreTrainedModel, ) else: import sys UpperCamelCase : List[str] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
345
0
'''simple docstring''' import json import os import unittest from transformers.models.gptsan_japanese.tokenization_gptsan_japanese import ( VOCAB_FILES_NAMES, GPTSanJapaneseTokenizer, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class UpperCamelCase ( a_ , unittest.TestCase ): """simple docstring""" A : str = GPTSanJapaneseTokenizer A : Optional[int] = False A : Dict = {'''do_clean_text''': False, '''add_prefix_space''': False} def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" super().setUp() # fmt: off a : Any = ['こん', 'こんに', 'にちは', 'ばんは', '世界,㔺界', '、', '。', '<BR>', '<SP>', '<TAB>', '<URL>', '<EMAIL>', '<TEL>', '<DATE>', '<PRICE>', '<BLOCK>', '<KIGOU>', '<U2000U2BFF>', '<|emoji1|>', '<unk>', '<|bagoftoken|>', '<|endoftext|>'] # fmt: on a : List[str] = {'emoji': {'\ud83d\ude00': '<|emoji1|>'}, 'emoji_inv': {'<|emoji1|>': '\ud83d\ude00'}} # 😀 a : List[Any] = {'unk_token': '<unk>'} a : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file']) a : str = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['emoji_file']) with open(self.vocab_file , 'w' , encoding='utf-8') as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens])) with open(self.emoji_file , 'w') as emoji_writer: emoji_writer.write(json.dumps(_a)) def SCREAMING_SNAKE_CASE_ ( self : Dict , **UpperCAmelCase_ : Optional[Any]): """simple docstring""" kwargs.update(self.special_tokens_map) return GPTSanJapaneseTokenizer.from_pretrained(self.tmpdirname , **_a) def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : int): """simple docstring""" a : Union[str, Any] = 'こんにちは、世界。 \nこんばんは、㔺界。😀' a : List[str] = 'こんにちは、世界。 \nこんばんは、世界。😀' return input_text, output_text def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : Dict): """simple docstring""" a , a : List[Any] = self.get_input_output_texts(_a) a : int = tokenizer.encode(_a , add_special_tokens=_a) a : Any = tokenizer.decode(_a , clean_up_tokenization_spaces=_a) return text, ids def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" pass # TODO add if relevant def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" pass # TODO add if relevant def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" pass # TODO add if relevant def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : str = self.get_tokenizer() # Testing tokenization a : Dict = 'こんにちは、世界。 こんばんは、㔺界。' a : int = ['こん', 'にちは', '、', '世界', '。', '<SP>', 'こん', 'ばんは', '、', '㔺界', '。'] a : str = tokenizer.tokenize(_a) self.assertListEqual(_a , _a) # Testing conversion to ids without special tokens a : int = [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6] a : List[str] = tokenizer.convert_tokens_to_ids(_a) self.assertListEqual(_a , _a) # Testing conversion to ids with special tokens a : Optional[int] = tokens + [tokenizer.unk_token] a : List[Any] = [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6, 1_9] a : Dict = tokenizer.convert_tokens_to_ids(_a) self.assertListEqual(_a , _a) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Optional[Any] = self.get_tokenizer() # Testing tokenization a : Optional[int] = 'こんにちは、<|bagoftoken|>世界。こんばんは、<|bagoftoken|>㔺界。' a : List[str] = 'こんにちは、、、、世界。こんばんは、、、、世界。' a : Any = tokenizer.encode(_a) a : List[str] = tokenizer.decode(_a) self.assertEqual(_a , _a) @slow def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : Any = self.tokenizer_class.from_pretrained('Tanrei/GPTSAN-japanese') # Testing tokenization a : Dict = 'こんにちは、世界。' a : Union[str, Any] = 'こんばんは、㔺界。😀' a : Optional[int] = 'こんにちは、世界。こんばんは、世界。😀' a : int = tokenizer.encode(prefix_text + input_text) a : Dict = tokenizer.encode('' , prefix_text=prefix_text + input_text) a : List[str] = tokenizer.encode(_a , prefix_text=_a) a : Any = tokenizer.decode(_a) a : Optional[int] = tokenizer.decode(_a) a : Union[str, Any] = tokenizer.decode(_a) self.assertEqual(_a , _a) self.assertEqual(_a , _a) self.assertEqual(_a , _a) @slow def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : int = self.tokenizer_class.from_pretrained('Tanrei/GPTSAN-japanese') # Testing tokenization a : Union[str, Any] = 'こんにちは、世界。' a : Optional[int] = 'こんばんは、㔺界。😀' a : Union[str, Any] = len(tokenizer.encode(_a)) - 2 a : List[str] = len(tokenizer.encode(_a)) - 2 a : Any = [1] + [0] * (len_prefix + len_text + 1) a : Any = [1] * (len_prefix + len_text + 1) + [0] a : Tuple = [1] + [1] * (len_prefix) + [0] * (len_text + 1) a : List[Any] = tokenizer(prefix_text + input_text).token_type_ids a : Optional[Any] = tokenizer('' , prefix_text=prefix_text + input_text).token_type_ids a : List[Any] = tokenizer(_a , prefix_text=_a).token_type_ids self.assertListEqual(_a , _a) self.assertListEqual(_a , _a) self.assertListEqual(_a , _a) @slow def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" a : Optional[int] = self.tokenizer_class.from_pretrained('Tanrei/GPTSAN-japanese') a : str = tokenizer.encode('あンいワ') a : List[str] = tokenizer.encode('' , prefix_text='あンいワ') a : Optional[Any] = tokenizer.encode('いワ' , prefix_text='あン') self.assertEqual(tokenizer.decode(_a) , tokenizer.decode(_a)) self.assertEqual(tokenizer.decode(_a) , tokenizer.decode(_a)) self.assertNotEqual(_a , _a) self.assertNotEqual(_a , _a) self.assertEqual(x_token_a[1] , x_token_a[-1]) # SEG token self.assertEqual(x_token_a[1] , x_token_a[3]) # SEG token @slow def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : Optional[Any] = self.tokenizer_class.from_pretrained('Tanrei/GPTSAN-japanese') a : Dict = [['武田信玄', 'は、'], ['織田信長', 'の配下の、']] a : Any = tokenizer(_a , padding=_a) a : Union[str, Any] = tokenizer.batch_encode_plus(_a , padding=_a) # fmt: off a : List[Any] = [[3_5_9_9_3, 8_6_4_0, 2_5_9_4_8, 3_5_9_9_8, 3_0_6_4_7, 3_5_6_7_5, 3_5_9_9_9, 3_5_9_9_9], [3_5_9_9_3, 1_0_3_8_2, 9_8_6_8, 3_5_9_9_8, 3_0_6_4_6, 9_4_5_9, 3_0_6_4_6, 3_5_6_7_5]] a : Any = [[1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0]] a : int = [[1, 1, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1]] # fmt: on self.assertListEqual(x_token.input_ids , _a) self.assertListEqual(x_token.token_type_ids , _a) self.assertListEqual(x_token.attention_mask , _a) self.assertListEqual(x_token_a.input_ids , _a) self.assertListEqual(x_token_a.token_type_ids , _a) self.assertListEqual(x_token_a.attention_mask , _a) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" pass def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" pass
363
'''simple docstring''' import math from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase : str = logging.get_logger(__name__) UpperCamelCase : List[str] = { """facebook/data2vec-base-960h""": """https://huggingface.co/facebook/data2vec-audio-base-960h/resolve/main/config.json""", # See all Data2VecAudio models at https://huggingface.co/models?filter=data2vec-audio } class UpperCamelCase ( a_ ): """simple docstring""" A : Optional[int] = "data2vec-audio" def __init__( self : Dict , UpperCAmelCase_ : Optional[int]=3_2 , UpperCAmelCase_ : Union[str, Any]=7_6_8 , UpperCAmelCase_ : Dict=1_2 , UpperCAmelCase_ : str=1_2 , UpperCAmelCase_ : Optional[Any]=3_0_7_2 , UpperCAmelCase_ : Dict="gelu" , UpperCAmelCase_ : Union[str, Any]=0.1 , UpperCAmelCase_ : Optional[int]=0.1 , UpperCAmelCase_ : int=0.1 , UpperCAmelCase_ : Union[str, Any]=0.0 , UpperCAmelCase_ : Union[str, Any]=0.1 , UpperCAmelCase_ : Dict=0.1 , UpperCAmelCase_ : Optional[Any]=0.02 , UpperCAmelCase_ : str=1e-5 , UpperCAmelCase_ : List[Any]="gelu" , UpperCAmelCase_ : Optional[Any]=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2) , UpperCAmelCase_ : Dict=(5, 2, 2, 2, 2, 2, 2) , UpperCAmelCase_ : int=(1_0, 3, 3, 3, 3, 2, 2) , UpperCAmelCase_ : List[Any]=False , UpperCAmelCase_ : List[Any]=1_6 , UpperCAmelCase_ : Optional[Any]=1_9 , UpperCAmelCase_ : int=5 , UpperCAmelCase_ : Any=0.05 , UpperCAmelCase_ : Dict=1_0 , UpperCAmelCase_ : Optional[Any]=2 , UpperCAmelCase_ : Optional[int]=0.0 , UpperCAmelCase_ : Tuple=1_0 , UpperCAmelCase_ : int=0 , UpperCAmelCase_ : Any="sum" , UpperCAmelCase_ : Optional[int]=False , UpperCAmelCase_ : Dict=False , UpperCAmelCase_ : Optional[int]=2_5_6 , UpperCAmelCase_ : Any=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0) , UpperCAmelCase_ : Optional[Any]=(5, 3, 3, 1, 1) , UpperCAmelCase_ : Optional[int]=(1, 2, 3, 1, 1) , UpperCAmelCase_ : int=5_1_2 , UpperCAmelCase_ : str=0 , UpperCAmelCase_ : Tuple=1 , UpperCAmelCase_ : Union[str, Any]=2 , UpperCAmelCase_ : List[str]=False , UpperCAmelCase_ : Any=3 , UpperCAmelCase_ : Dict=2 , UpperCAmelCase_ : int=3 , UpperCAmelCase_ : List[Any]=None , **UpperCAmelCase_ : int , ): """simple docstring""" super().__init__(**UpperCAmelCase_ , pad_token_id=UpperCAmelCase_ , bos_token_id=UpperCAmelCase_ , eos_token_id=UpperCAmelCase_) a : List[Any] = hidden_size a : Any = feat_extract_activation a : Any = list(UpperCAmelCase_) a : Optional[int] = list(UpperCAmelCase_) a : Dict = list(UpperCAmelCase_) a : Tuple = conv_bias a : str = num_conv_pos_embeddings a : Dict = num_conv_pos_embedding_groups a : Optional[Any] = conv_pos_kernel_size a : Any = len(self.conv_dim) a : Tuple = num_hidden_layers a : Any = intermediate_size a : Any = hidden_act a : Dict = num_attention_heads a : Dict = hidden_dropout a : Union[str, Any] = attention_dropout a : Dict = activation_dropout a : Optional[int] = feat_proj_dropout a : Tuple = final_dropout a : Union[str, Any] = layerdrop a : Tuple = layer_norm_eps a : Dict = initializer_range a : Tuple = vocab_size a : int = use_weighted_layer_sum if ( (len(self.conv_stride) != self.num_feat_extract_layers) or (len(self.conv_kernel) != self.num_feat_extract_layers) or (len(self.conv_dim) != self.num_feat_extract_layers) ): raise ValueError( 'Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==' ' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =' f""" {len(self.conv_dim)}`, `len(config.conv_stride) = {len(self.conv_stride)}`,""" f""" `len(config.conv_kernel) = {len(self.conv_kernel)}`.""") # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 a : List[str] = mask_time_prob a : int = mask_time_length a : Optional[int] = mask_time_min_masks a : Dict = mask_feature_prob a : List[str] = mask_feature_length a : str = mask_feature_min_masks # ctc loss a : str = ctc_loss_reduction a : Optional[Any] = ctc_zero_infinity # adapter a : List[str] = add_adapter a : Optional[Any] = adapter_kernel_size a : int = adapter_stride a : str = num_adapter_layers a : Optional[Any] = output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. a : str = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. a : List[Any] = list(UpperCAmelCase_) a : List[str] = list(UpperCAmelCase_) a : str = list(UpperCAmelCase_) a : Optional[Any] = xvector_output_dim @property def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" return math.prod(self.conv_stride)
345
0
'''simple docstring''' from pathlib import Path from typing import List from transformers import is_torch_available, is_vision_available from transformers.testing_utils import get_tests_dir, is_tool_test from transformers.tools.agent_types import AGENT_TYPE_MAPPING, AgentAudio, AgentImage, AgentText if is_torch_available(): import torch if is_vision_available(): from PIL import Image UpperCamelCase : List[Any] = ['''text''', '''image''', '''audio'''] def SCREAMING_SNAKE_CASE__ ( snake_case : List[str] ) -> Optional[Any]: """simple docstring""" a : Optional[Any] = [] for input_type in input_types: if input_type == "text": inputs.append('Text input' ) elif input_type == "image": inputs.append( Image.open(Path(get_tests_dir('fixtures/tests_samples/COCO' ) ) / '000000039769.png' ).resize((512, 512) ) ) elif input_type == "audio": inputs.append(torch.ones(3_000 ) ) elif isinstance(_a , _a ): inputs.append(create_inputs(_a ) ) else: raise ValueError(F"""Invalid type requested: {input_type}""" ) return inputs def SCREAMING_SNAKE_CASE__ ( snake_case : List ) -> List[str]: """simple docstring""" a : str = [] for output in outputs: if isinstance(_a , (str, AgentText) ): output_types.append('text' ) elif isinstance(_a , (Image.Image, AgentImage) ): output_types.append('image' ) elif isinstance(_a , (torch.Tensor, AgentAudio) ): output_types.append('audio' ) else: raise ValueError(F"""Invalid output: {output}""" ) return output_types @is_tool_test class UpperCamelCase : """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" self.assertTrue(hasattr(self.tool , 'inputs')) self.assertTrue(hasattr(self.tool , 'outputs')) a : int = self.tool.inputs for _input in inputs: if isinstance(_input , _SCREAMING_SNAKE_CASE): for __input in _input: self.assertTrue(__input in authorized_types) else: self.assertTrue(_input in authorized_types) a : Dict = self.tool.outputs for _output in outputs: self.assertTrue(_output in authorized_types) def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" a : Optional[Any] = create_inputs(self.tool.inputs) a : Optional[int] = self.tool(*_SCREAMING_SNAKE_CASE) # There is a single output if len(self.tool.outputs) == 1: a : Optional[int] = [outputs] self.assertListEqual(output_types(_SCREAMING_SNAKE_CASE) , self.tool.outputs) def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" self.assertTrue(hasattr(self.tool , 'description')) self.assertTrue(hasattr(self.tool , 'default_checkpoint')) self.assertTrue(self.tool.description.startswith('This is a tool that')) def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : Dict = create_inputs(self.tool.inputs) a : Optional[int] = self.tool(*_SCREAMING_SNAKE_CASE) if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE): a : List[Any] = [outputs] self.assertEqual(len(_SCREAMING_SNAKE_CASE) , len(self.tool.outputs)) for output, output_type in zip(_SCREAMING_SNAKE_CASE , self.tool.outputs): a : str = AGENT_TYPE_MAPPING[output_type] self.assertTrue(isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE)) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" a : Optional[int] = create_inputs(self.tool.inputs) a : List[Any] = [] for _input, input_type in zip(_SCREAMING_SNAKE_CASE , self.tool.inputs): if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE): _inputs.append([AGENT_TYPE_MAPPING[_input_type](_input) for _input_type in input_type]) else: _inputs.append(AGENT_TYPE_MAPPING[input_type](_input)) # Should not raise an error a : Optional[Any] = self.tool(*_SCREAMING_SNAKE_CASE) if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE): a : Any = [outputs] self.assertEqual(len(_SCREAMING_SNAKE_CASE) , len(self.tool.outputs))
364
'''simple docstring''' import logging from transformers.configuration_utils import PretrainedConfig UpperCamelCase : Optional[Any] = logging.getLogger(__name__) class UpperCamelCase ( a_ ): """simple docstring""" A : Tuple = "masked_bert" def __init__( self : Tuple , UpperCAmelCase_ : List[Any]=3_0_5_2_2 , UpperCAmelCase_ : str=7_6_8 , UpperCAmelCase_ : Optional[Any]=1_2 , UpperCAmelCase_ : Optional[int]=1_2 , UpperCAmelCase_ : Union[str, Any]=3_0_7_2 , UpperCAmelCase_ : Union[str, Any]="gelu" , UpperCAmelCase_ : Optional[Any]=0.1 , UpperCAmelCase_ : List[Any]=0.1 , UpperCAmelCase_ : Optional[int]=5_1_2 , UpperCAmelCase_ : List[str]=2 , UpperCAmelCase_ : str=0.02 , UpperCAmelCase_ : Optional[Any]=1e-12 , UpperCAmelCase_ : Dict=0 , UpperCAmelCase_ : Dict="topK" , UpperCAmelCase_ : str="constant" , UpperCAmelCase_ : Optional[Any]=0.0 , **UpperCAmelCase_ : Optional[Any] , ): """simple docstring""" super().__init__(pad_token_id=UpperCAmelCase_ , **UpperCAmelCase_) a : Union[str, Any] = vocab_size a : List[Any] = hidden_size a : List[str] = num_hidden_layers a : Any = num_attention_heads a : Optional[Any] = hidden_act a : str = intermediate_size a : Dict = hidden_dropout_prob a : Any = attention_probs_dropout_prob a : Any = max_position_embeddings a : Dict = type_vocab_size a : List[str] = initializer_range a : int = layer_norm_eps a : Dict = pruning_method a : List[str] = mask_init a : Union[str, Any] = mask_scale
345
0
'''simple docstring''' import math class UpperCamelCase : """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : list[list[float]] , UpperCAmelCase_ : list[int]): """simple docstring""" a : List[str] = 0.0 a : str = 0.0 for i in range(len(UpperCAmelCase_)): da += math.pow((sample[i] - weights[0][i]) , 2) da += math.pow((sample[i] - weights[1][i]) , 2) return 0 if da > da else 1 return 0 def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : list[list[int | float]] , UpperCAmelCase_ : list[int] , UpperCAmelCase_ : int , UpperCAmelCase_ : float): """simple docstring""" for i in range(len(UpperCAmelCase_)): weights[j][i] += alpha * (sample[i] - weights[j][i]) return weights def SCREAMING_SNAKE_CASE__ ( ) -> Tuple: """simple docstring""" # Training Examples ( m, n ) a : List[Any] = [[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]] # weight initialization ( n, C ) a : Tuple = [[0.2, 0.6, 0.5, 0.9], [0.8, 0.4, 0.7, 0.3]] # training a : str = SelfOrganizingMap() a : Optional[int] = 3 a : str = 0.5 for _ in range(_UpperCAmelCase ): for j in range(len(_UpperCAmelCase ) ): # training sample a : int = training_samples[j] # Compute the winning vector a : Union[str, Any] = self_organizing_map.get_winner(_UpperCAmelCase , _UpperCAmelCase ) # Update the winning vector a : List[str] = self_organizing_map.update(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) # classify test sample a : int = [0, 0, 0, 1] a : Tuple = self_organizing_map.get_winner(_UpperCAmelCase , _UpperCAmelCase ) # results print(F"""Clusters that the test sample belongs to : {winner}""" ) print(F"""Weights that have been trained : {weights}""" ) # running the main() function if __name__ == "__main__": main()
365
'''simple docstring''' import copy import random from transformers import CLIPTokenizer class UpperCamelCase ( a_ ): """simple docstring""" def __init__( self : Union[str, Any] , *UpperCAmelCase_ : Dict , **UpperCAmelCase_ : Any): """simple docstring""" super().__init__(*UpperCAmelCase_ , **UpperCAmelCase_) a : str = {} def SCREAMING_SNAKE_CASE_ ( self : int , UpperCAmelCase_ : Tuple , *UpperCAmelCase_ : Optional[int] , **UpperCAmelCase_ : int): """simple docstring""" a : Dict = super().add_tokens(UpperCAmelCase_ , *UpperCAmelCase_ , **UpperCAmelCase_) if num_added_tokens == 0: raise ValueError( f"""The tokenizer already contains the token {placeholder_token}. Please pass a different""" ' `placeholder_token` that is not already in the tokenizer.') def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : Optional[int] , *UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : List[str]=1 , **UpperCAmelCase_ : Optional[int]): """simple docstring""" a : Any = [] if num_vec_per_token == 1: self.try_adding_tokens(UpperCAmelCase_ , *UpperCAmelCase_ , **UpperCAmelCase_) output.append(UpperCAmelCase_) else: a : int = [] for i in range(UpperCAmelCase_): a : Union[str, Any] = placeholder_token + f"""_{i}""" self.try_adding_tokens(UpperCAmelCase_ , *UpperCAmelCase_ , **UpperCAmelCase_) output.append(UpperCAmelCase_) # handle cases where there is a new placeholder token that contains the current placeholder token but is larger for token in self.token_map: if token in placeholder_token: raise ValueError( f"""The tokenizer already has placeholder token {token} that can get confused with""" f""" {placeholder_token}keep placeholder tokens independent""") a : Any = output def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Optional[int]=False , UpperCAmelCase_ : str=1.0): """simple docstring""" if isinstance(UpperCAmelCase_ , UpperCAmelCase_): a : Any = [] for i in range(len(UpperCAmelCase_)): output.append(self.replace_placeholder_tokens_in_text(text[i] , vector_shuffle=UpperCAmelCase_)) return output for placeholder_token in self.token_map: if placeholder_token in text: a : List[Any] = self.token_map[placeholder_token] a : int = tokens[: 1 + int(len(UpperCAmelCase_) * prop_tokens_to_load)] if vector_shuffle: a : List[Any] = copy.copy(UpperCAmelCase_) random.shuffle(UpperCAmelCase_) a : List[str] = text.replace(UpperCAmelCase_ , ' '.join(UpperCAmelCase_)) return text def __call__( self : Optional[int] , UpperCAmelCase_ : Any , *UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Union[str, Any]=False , UpperCAmelCase_ : Optional[int]=1.0 , **UpperCAmelCase_ : str): """simple docstring""" return super().__call__( self.replace_placeholder_tokens_in_text( UpperCAmelCase_ , vector_shuffle=UpperCAmelCase_ , prop_tokens_to_load=UpperCAmelCase_) , *UpperCAmelCase_ , **UpperCAmelCase_ , ) def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : Optional[int] , *UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Any=False , UpperCAmelCase_ : Optional[Any]=1.0 , **UpperCAmelCase_ : Dict): """simple docstring""" return super().encode( self.replace_placeholder_tokens_in_text( UpperCAmelCase_ , vector_shuffle=UpperCAmelCase_ , prop_tokens_to_load=UpperCAmelCase_) , *UpperCAmelCase_ , **UpperCAmelCase_ , )
345
0
'''simple docstring''' from collections.abc import Callable from math import pi, sqrt from random import uniform from statistics import mean def SCREAMING_SNAKE_CASE__ ( snake_case : int ) -> List[str]: """simple docstring""" # A local function to see if a dot lands in the circle. def is_in_circle(snake_case : float , snake_case : float ) -> bool: a : Any = sqrt((x**2) + (y**2) ) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle a : List[Any] = mean( int(is_in_circle(uniform(-1.0 , 1.0 ) , uniform(-1.0 , 1.0 ) ) ) for _ in range(SCREAMING_SNAKE_CASE_ ) ) # The ratio of the area for circle to square is pi/4. a : Optional[Any] = proportion * 4 print(F"""The estimated value of pi is {pi_estimate}""" ) print(F"""The numpy value of pi is {pi}""" ) print(F"""The total error is {abs(pi - pi_estimate )}""" ) def SCREAMING_SNAKE_CASE__ ( snake_case : int , snake_case : Callable[[float], float] , snake_case : float = 0.0 , snake_case : float = 1.0 , ) -> float: """simple docstring""" return mean( function_to_integrate(uniform(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) for _ in range(SCREAMING_SNAKE_CASE_ ) ) * (max_value - min_value) def SCREAMING_SNAKE_CASE__ ( snake_case : int , snake_case : float = 0.0 , snake_case : float = 1.0 ) -> None: """simple docstring""" def identity_function(snake_case : float ) -> float: return x a : str = area_under_curve_estimator( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) a : List[Any] = (max_value * max_value - min_value * min_value) / 2 print('******************' ) print(F"""Estimating area under y=x where x varies from {min_value} to {max_value}""" ) print(F"""Estimated value is {estimated_value}""" ) print(F"""Expected value is {expected_value}""" ) print(F"""Total error is {abs(estimated_value - expected_value )}""" ) print('******************' ) def SCREAMING_SNAKE_CASE__ ( snake_case : int ) -> None: """simple docstring""" def function_to_integrate(snake_case : float ) -> float: return sqrt(4.0 - x * x ) a : Dict = area_under_curve_estimator( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , 0.0 , 2.0 ) print('******************' ) print('Estimating pi using area_under_curve_estimator' ) print(F"""Estimated value is {estimated_value}""" ) print(F"""Expected value is {pi}""" ) print(F"""Total error is {abs(estimated_value - pi )}""" ) print('******************' ) if __name__ == "__main__": import doctest doctest.testmod()
366
'''simple docstring''' import argparse import json import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import AutoImageProcessor, SwinConfig, SwinForImageClassification def SCREAMING_SNAKE_CASE__ ( snake_case : str ) -> Optional[Any]: """simple docstring""" a : Union[str, Any] = SwinConfig() a : Optional[int] = swin_name.split('_' ) a : Union[str, Any] = name_split[1] a : Dict = int(name_split[4] ) a : Union[str, Any] = int(name_split[3][-1] ) if model_size == "tiny": a : Optional[Any] = 96 a : Any = (2, 2, 6, 2) a : List[str] = (3, 6, 12, 24) elif model_size == "small": a : int = 96 a : List[str] = (2, 2, 18, 2) a : int = (3, 6, 12, 24) elif model_size == "base": a : Tuple = 128 a : Optional[int] = (2, 2, 18, 2) a : List[Any] = (4, 8, 16, 32) else: a : Dict = 192 a : str = (2, 2, 18, 2) a : List[Any] = (6, 12, 24, 48) if "in22k" in swin_name: a : Any = 21_841 else: a : str = 1_000 a : str = 'huggingface/label-files' a : Optional[Any] = 'imagenet-1k-id2label.json' a : Dict = json.load(open(hf_hub_download(snake_case , snake_case , repo_type='dataset' ) , 'r' ) ) a : Tuple = {int(snake_case ): v for k, v in idalabel.items()} a : int = idalabel a : str = {v: k for k, v in idalabel.items()} a : Dict = img_size a : List[Any] = num_classes a : str = embed_dim a : Dict = depths a : Union[str, Any] = num_heads a : int = window_size return config def SCREAMING_SNAKE_CASE__ ( snake_case : List[Any] ) -> Optional[int]: """simple docstring""" if "patch_embed.proj" in name: a : int = name.replace('patch_embed.proj' , 'embeddings.patch_embeddings.projection' ) if "patch_embed.norm" in name: a : Tuple = name.replace('patch_embed.norm' , 'embeddings.norm' ) if "layers" in name: a : Optional[int] = 'encoder.' + name if "attn.proj" in name: a : List[Any] = name.replace('attn.proj' , 'attention.output.dense' ) if "attn" in name: a : Tuple = name.replace('attn' , 'attention.self' ) if "norm1" in name: a : Optional[int] = name.replace('norm1' , 'layernorm_before' ) if "norm2" in name: a : Dict = name.replace('norm2' , 'layernorm_after' ) if "mlp.fc1" in name: a : Union[str, Any] = name.replace('mlp.fc1' , 'intermediate.dense' ) if "mlp.fc2" in name: a : Any = name.replace('mlp.fc2' , 'output.dense' ) if name == "norm.weight": a : Union[str, Any] = 'layernorm.weight' if name == "norm.bias": a : List[str] = 'layernorm.bias' if "head" in name: a : Union[str, Any] = name.replace('head' , 'classifier' ) else: a : List[Any] = 'swin.' + name return name def SCREAMING_SNAKE_CASE__ ( snake_case : List[Any] , snake_case : Tuple ) -> List[str]: """simple docstring""" for key in orig_state_dict.copy().keys(): a : Any = orig_state_dict.pop(snake_case ) if "mask" in key: continue elif "qkv" in key: a : Optional[Any] = key.split('.' ) a : Dict = int(key_split[1] ) a : Optional[int] = int(key_split[3] ) a : Tuple = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: a : Optional[Any] = val[:dim, :] a : List[Any] = val[ dim : dim * 2, : ] a : List[Any] = val[-dim:, :] else: a : Dict = val[ :dim ] a : Union[str, Any] = val[ dim : dim * 2 ] a : Union[str, Any] = val[ -dim: ] else: a : Dict = val return orig_state_dict def SCREAMING_SNAKE_CASE__ ( snake_case : Optional[int] , snake_case : Dict ) -> List[str]: """simple docstring""" a : Any = timm.create_model(snake_case , pretrained=snake_case ) timm_model.eval() a : str = get_swin_config(snake_case ) a : Optional[int] = SwinForImageClassification(snake_case ) model.eval() a : Union[str, Any] = convert_state_dict(timm_model.state_dict() , snake_case ) model.load_state_dict(snake_case ) a : List[str] = 'http://images.cocodataset.org/val2017/000000039769.jpg' a : Optional[Any] = AutoImageProcessor.from_pretrained('microsoft/{}'.format(swin_name.replace('_' , '-' ) ) ) a : str = Image.open(requests.get(snake_case , stream=snake_case ).raw ) a : Union[str, Any] = image_processor(images=snake_case , return_tensors='pt' ) a : int = timm_model(inputs['pixel_values'] ) a : Optional[int] = model(**snake_case ).logits assert torch.allclose(snake_case , snake_case , atol=1E-3 ) print(F"""Saving model {swin_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(snake_case ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(snake_case ) if __name__ == "__main__": UpperCamelCase : int = argparse.ArgumentParser() # Required parameters parser.add_argument( """--swin_name""", default="""swin_tiny_patch4_window7_224""", type=str, help="""Name of the Swin timm model you'd like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) UpperCamelCase : Optional[Any] = parser.parse_args() convert_swin_checkpoint(args.swin_name, args.pytorch_dump_folder_path)
345
0
'''simple docstring''' import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging UpperCamelCase : int = logging.get_logger(__name__) UpperCamelCase : Dict = '▁' UpperCamelCase : int = {'vocab_file': 'sentencepiece.bpe.model', 'monolingual_vocab_file': 'dict.txt'} UpperCamelCase : List[Any] = { 'vocab_file': { 'vinai/bartpho-syllable': 'https://huggingface.co/vinai/bartpho-syllable/resolve/main/sentencepiece.bpe.model', }, 'monolingual_vocab_file': { 'vinai/bartpho-syllable': 'https://huggingface.co/vinai/bartpho-syllable/resolve/main/dict.txt', }, } UpperCamelCase : Any = {'vinai/bartpho-syllable': 1_024} class UpperCamelCase ( UpperCamelCase__ ): """simple docstring""" A : Any = VOCAB_FILES_NAMES A : List[Any] = PRETRAINED_VOCAB_FILES_MAP A : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A : Tuple = ["input_ids", "attention_mask"] def __init__( self : Union[str, Any] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Any="<s>" , UpperCAmelCase_ : Union[str, Any]="</s>" , UpperCAmelCase_ : Dict="</s>" , UpperCAmelCase_ : Tuple="<s>" , UpperCAmelCase_ : Dict="<unk>" , UpperCAmelCase_ : Dict="<pad>" , UpperCAmelCase_ : Union[str, Any]="<mask>" , UpperCAmelCase_ : str = None , **UpperCAmelCase_ : Union[str, Any] , ): """simple docstring""" a : List[Any] = AddedToken(__lowerCamelCase , lstrip=__lowerCamelCase , rstrip=__lowerCamelCase) if isinstance(__lowerCamelCase , __lowerCamelCase) else mask_token a : List[Any] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=__lowerCamelCase , eos_token=__lowerCamelCase , unk_token=__lowerCamelCase , sep_token=__lowerCamelCase , cls_token=__lowerCamelCase , pad_token=__lowerCamelCase , mask_token=__lowerCamelCase , sp_model_kwargs=self.sp_model_kwargs , **__lowerCamelCase , ) a : int = vocab_file a : Dict = monolingual_vocab_file a : Dict = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(str(__lowerCamelCase)) # Load the reduced vocab # Keep order of special tokens for backward compatibility a : int = {} a : Optional[Any] = 0 for token in [bos_token, pad_token, eos_token, unk_token, sep_token, cls_token]: if str(__lowerCamelCase) not in self.fairseq_tokens_to_ids: a : Any = cnt cnt += 1 with open(__lowerCamelCase , 'r' , encoding='utf-8') as f: for line in f.readlines(): a : str = line.strip().split()[0] a : Any = len(self.fairseq_tokens_to_ids) if str(__lowerCamelCase) not in self.fairseq_tokens_to_ids: a : Optional[Any] = len(self.fairseq_tokens_to_ids) a : Any = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def __getstate__( self : Optional[Any]): """simple docstring""" a : Tuple = self.__dict__.copy() a : int = None a : str = self.sp_model.serialized_model_proto() return state def __setstate__( self : int , UpperCAmelCase_ : Dict): """simple docstring""" a : List[Any] = d # for backward compatibility if not hasattr(self , 'sp_model_kwargs'): a : str = {} a : Any = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.LoadFromSerializedProto(self.sp_model_proto) def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Union[str, Any] = None): """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] a : Optional[Any] = [self.cls_token_id] a : List[Any] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE_ ( self : List[Any] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Any = None , UpperCAmelCase_ : List[str] = False): """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__lowerCamelCase , token_ids_a=__lowerCamelCase , already_has_special_tokens=__lowerCamelCase) if token_ids_a is None: return [1] + ([0] * len(__lowerCamelCase)) + [1] return [1] + ([0] * len(__lowerCamelCase)) + [1, 1] + ([0] * len(__lowerCamelCase)) + [1] def SCREAMING_SNAKE_CASE_ ( self : Any , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Tuple = None): """simple docstring""" a : Dict = [self.sep_token_id] a : Dict = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep) * [0] @property def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" return len(self.fairseq_ids_to_tokens) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Dict = {self.convert_ids_to_tokens(__lowerCamelCase): i for i in range(self.vocab_size)} vocab.update(self.added_tokens_encoder) return vocab def SCREAMING_SNAKE_CASE_ ( self : Any , UpperCAmelCase_ : Union[str, Any]): """simple docstring""" return self.sp_model.encode(__lowerCamelCase , out_type=__lowerCamelCase) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : int): """simple docstring""" if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] else: return self.unk_token_id def SCREAMING_SNAKE_CASE_ ( self : List[Any] , UpperCAmelCase_ : Optional[Any]): """simple docstring""" return self.fairseq_ids_to_tokens[index] def SCREAMING_SNAKE_CASE_ ( self : Any , UpperCAmelCase_ : Tuple): """simple docstring""" a : Union[str, Any] = ''.join(__lowerCamelCase).replace(__lowerCamelCase , ' ').strip() return out_string def SCREAMING_SNAKE_CASE_ ( self : Any , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : int = None): """simple docstring""" if not os.path.isdir(__lowerCamelCase): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""") return a : Union[str, Any] = os.path.join( __lowerCamelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']) a : Dict = os.path.join( __lowerCamelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['monolingual_vocab_file'] , ) if os.path.abspath(self.vocab_file) != os.path.abspath(__lowerCamelCase) and os.path.isfile(self.vocab_file): copyfile(self.vocab_file , __lowerCamelCase) elif not os.path.isfile(self.vocab_file): with open(__lowerCamelCase , 'wb') as fi: a : List[str] = self.sp_model.serialized_model_proto() fi.write(__lowerCamelCase) if os.path.abspath(self.monolingual_vocab_file) != os.path.abspath( __lowerCamelCase) and os.path.isfile(self.monolingual_vocab_file): copyfile(self.monolingual_vocab_file , __lowerCamelCase) elif not os.path.isfile(self.monolingual_vocab_file): with open(__lowerCamelCase , 'w' , encoding='utf-8') as fp: for token in self.fairseq_tokens_to_ids: if token not in self.all_special_tokens: fp.write(f"""{str(__lowerCamelCase)} \n""") return out_vocab_file, out_monolingual_vocab_file
367
'''simple docstring''' import baseaa import io import json import os from copy import deepcopy from ..optimizer import AcceleratedOptimizer from ..scheduler import AcceleratedScheduler class UpperCamelCase : """simple docstring""" def __init__( self : List[str] , UpperCAmelCase_ : Tuple): """simple docstring""" if isinstance(UpperCAmelCase_ , UpperCAmelCase_): # Don't modify user's data should they want to reuse it (e.g. in tests), because once we # modified it, it will not be accepted here again, since `auto` values would have been overridden a : Dict = deepcopy(UpperCAmelCase_) elif os.path.exists(UpperCAmelCase_): with io.open(UpperCAmelCase_ , 'r' , encoding='utf-8') as f: a : Union[str, Any] = json.load(UpperCAmelCase_) else: try: a : Union[str, Any] = baseaa.urlsafe_baadecode(UpperCAmelCase_).decode('utf-8') a : List[str] = json.loads(UpperCAmelCase_) except (UnicodeDecodeError, AttributeError, ValueError): raise ValueError( f"""Expected a string path to an existing deepspeed config, or a dictionary, or a base64 encoded string. Received: {config_file_or_dict}""") a : Optional[int] = config self.set_stage_and_offload() def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : str = self.get_value('zero_optimization.stage' , -1) # offload a : Any = False if self.is_zeroa() or self.is_zeroa(): a : Tuple = set(['cpu', 'nvme']) a : int = set( [ self.get_value('zero_optimization.offload_optimizer.device'), self.get_value('zero_optimization.offload_param.device'), ]) if len(offload_devices & offload_devices_valid) > 0: a : List[str] = True def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : Dict): """simple docstring""" a : List[str] = self.config # find the config node of interest if it exists a : int = ds_key_long.split('.') a : Union[str, Any] = nodes.pop() for node in nodes: a : Union[str, Any] = config.get(UpperCAmelCase_) if config is None: return None, ds_key return config, ds_key def SCREAMING_SNAKE_CASE_ ( self : Any , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : int=None): """simple docstring""" a , a : int = self.find_config_node(UpperCAmelCase_) if config is None: return default return config.get(UpperCAmelCase_ , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : int , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Any=False): """simple docstring""" a : Any = self.config # find the config node of interest if it exists a : Optional[Any] = ds_key_long.split('.') for node in nodes: a : List[str] = config a : int = config.get(UpperCAmelCase_) if config is None: if must_exist: raise ValueError(f"""Can't find {ds_key_long} entry in the config: {self.config}""") else: return # if found remove it if parent_config is not None: parent_config.pop(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : str): """simple docstring""" a : List[str] = self.get_value(UpperCAmelCase_) return False if value is None else bool(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : Union[str, Any]): """simple docstring""" a : List[Any] = self.get_value(UpperCAmelCase_) return False if value is None else not bool(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" return self._stage == 2 def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" return self._stage == 3 def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" return self._offload class UpperCamelCase : """simple docstring""" def __init__( self : str , UpperCAmelCase_ : int): """simple docstring""" a : Union[str, Any] = engine def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : Any , **UpperCAmelCase_ : List[Any]): """simple docstring""" self.engine.backward(UpperCAmelCase_ , **UpperCAmelCase_) # Deepspeed's `engine.step` performs the following operations: # - gradient accumulation check # - gradient clipping # - optimizer step # - zero grad # - checking overflow # - lr_scheduler step (only if engine.lr_scheduler is not None) self.engine.step() # and this plugin overrides the above calls with no-ops when Accelerate runs under # Deepspeed, but allows normal functionality for non-Deepspeed cases thus enabling a simple # training loop that works transparently under many training regimes. class UpperCamelCase ( a_ ): """simple docstring""" def __init__( self : Union[str, Any] , UpperCAmelCase_ : Any): """simple docstring""" super().__init__(UpperCAmelCase_ , device_placement=UpperCAmelCase_ , scaler=UpperCAmelCase_) a : List[str] = hasattr(self.optimizer , 'overflow') def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : Dict=None): """simple docstring""" pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed @property def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" if self.__has_overflow__: return self.optimizer.overflow return False class UpperCamelCase ( a_ ): """simple docstring""" def __init__( self : str , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Optional[Any]): """simple docstring""" super().__init__(UpperCAmelCase_ , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed class UpperCamelCase : """simple docstring""" def __init__( self : Union[str, Any] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Optional[int]=0.0_01 , UpperCAmelCase_ : List[Any]=0 , **UpperCAmelCase_ : Union[str, Any]): """simple docstring""" a : int = params a : str = lr a : Tuple = weight_decay a : Dict = kwargs class UpperCamelCase : """simple docstring""" def __init__( self : int , UpperCAmelCase_ : int , UpperCAmelCase_ : Any=None , UpperCAmelCase_ : Union[str, Any]=0 , **UpperCAmelCase_ : List[Any]): """simple docstring""" a : str = optimizer a : Tuple = total_num_steps a : Optional[Any] = warmup_num_steps a : List[str] = kwargs
345
0
'''simple docstring''' import json from typing import List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_roberta import RobertaTokenizer UpperCamelCase : List[Any] = logging.get_logger(__name__) UpperCamelCase : Union[str, Any] = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""} UpperCamelCase : int = { """vocab_file""": { """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/vocab.json""", """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/vocab.json""", """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/vocab.json""", """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/vocab.json""", """roberta-base-openai-detector""": """https://huggingface.co/roberta-base-openai-detector/resolve/main/vocab.json""", """roberta-large-openai-detector""": ( """https://huggingface.co/roberta-large-openai-detector/resolve/main/vocab.json""" ), }, """merges_file""": { """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/merges.txt""", """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/merges.txt""", """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/merges.txt""", """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/merges.txt""", """roberta-base-openai-detector""": """https://huggingface.co/roberta-base-openai-detector/resolve/main/merges.txt""", """roberta-large-openai-detector""": ( """https://huggingface.co/roberta-large-openai-detector/resolve/main/merges.txt""" ), }, """tokenizer_file""": { """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/tokenizer.json""", """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/tokenizer.json""", """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/tokenizer.json""", """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/tokenizer.json""", """roberta-base-openai-detector""": ( """https://huggingface.co/roberta-base-openai-detector/resolve/main/tokenizer.json""" ), """roberta-large-openai-detector""": ( """https://huggingface.co/roberta-large-openai-detector/resolve/main/tokenizer.json""" ), }, } UpperCamelCase : Union[str, Any] = { """roberta-base""": 512, """roberta-large""": 512, """roberta-large-mnli""": 512, """distilroberta-base""": 512, """roberta-base-openai-detector""": 512, """roberta-large-openai-detector""": 512, } class UpperCamelCase ( A_ ): """simple docstring""" A : Union[str, Any] = VOCAB_FILES_NAMES A : List[Any] = PRETRAINED_VOCAB_FILES_MAP A : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A : Optional[int] = ["input_ids", "attention_mask"] A : Optional[Any] = RobertaTokenizer def __init__( self : Dict , UpperCAmelCase_ : Dict=None , UpperCAmelCase_ : int=None , UpperCAmelCase_ : Optional[int]=None , UpperCAmelCase_ : int="replace" , UpperCAmelCase_ : Optional[Any]="<s>" , UpperCAmelCase_ : str="</s>" , UpperCAmelCase_ : List[str]="</s>" , UpperCAmelCase_ : str="<s>" , UpperCAmelCase_ : Union[str, Any]="<unk>" , UpperCAmelCase_ : List[str]="<pad>" , UpperCAmelCase_ : List[str]="<mask>" , UpperCAmelCase_ : int=False , UpperCAmelCase_ : int=True , **UpperCAmelCase_ : int , ): """simple docstring""" super().__init__( _lowerCamelCase , _lowerCamelCase , tokenizer_file=_lowerCamelCase , errors=_lowerCamelCase , bos_token=_lowerCamelCase , eos_token=_lowerCamelCase , sep_token=_lowerCamelCase , cls_token=_lowerCamelCase , unk_token=_lowerCamelCase , pad_token=_lowerCamelCase , mask_token=_lowerCamelCase , add_prefix_space=_lowerCamelCase , trim_offsets=_lowerCamelCase , **_lowerCamelCase , ) a : Optional[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()) if pre_tok_state.get('add_prefix_space' , _lowerCamelCase) != add_prefix_space: a : Dict = getattr(_lowerCamelCase , pre_tok_state.pop('type')) a : Dict = add_prefix_space a : Optional[int] = pre_tok_class(**_lowerCamelCase) a : List[str] = add_prefix_space a : Any = 'post_processor' a : Tuple = getattr(self.backend_tokenizer , _lowerCamelCase , _lowerCamelCase) if tokenizer_component_instance: a : Optional[int] = json.loads(tokenizer_component_instance.__getstate__()) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: a : Any = tuple(state['sep']) if "cls" in state: a : List[str] = tuple(state['cls']) a : Optional[int] = False if state.get('add_prefix_space' , _lowerCamelCase) != add_prefix_space: a : List[Any] = add_prefix_space a : Tuple = True if state.get('trim_offsets' , _lowerCamelCase) != trim_offsets: a : List[str] = trim_offsets a : List[str] = True if changes_to_apply: a : Union[str, Any] = getattr(_lowerCamelCase , state.pop('type')) a : str = component_class(**_lowerCamelCase) setattr(self.backend_tokenizer , _lowerCamelCase , _lowerCamelCase) @property def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" if self._mask_token is None: if self.verbose: logger.error('Using mask_token, but it is not set yet.') return None return str(self._mask_token) @mask_token.setter def SCREAMING_SNAKE_CASE_ ( self : List[Any] , UpperCAmelCase_ : Tuple): """simple docstring""" a : Optional[int] = AddedToken(_lowerCamelCase , lstrip=_lowerCamelCase , rstrip=_lowerCamelCase) if isinstance(_lowerCamelCase , _lowerCamelCase) else value a : Optional[Any] = value def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , *UpperCAmelCase_ : Dict , **UpperCAmelCase_ : int): """simple docstring""" a : Union[str, Any] = kwargs.get('is_split_into_words' , _lowerCamelCase) assert self.add_prefix_space or not is_split_into_words, ( f"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """ "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*_lowerCamelCase , **_lowerCamelCase) def SCREAMING_SNAKE_CASE_ ( self : int , *UpperCAmelCase_ : List[Any] , **UpperCAmelCase_ : List[Any]): """simple docstring""" a : str = kwargs.get('is_split_into_words' , _lowerCamelCase) assert self.add_prefix_space or not is_split_into_words, ( f"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """ "to use it with pretokenized inputs." ) return super()._encode_plus(*_lowerCamelCase , **_lowerCamelCase) def SCREAMING_SNAKE_CASE_ ( self : int , UpperCAmelCase_ : str , UpperCAmelCase_ : Optional[str] = None): """simple docstring""" a : Any = self._tokenizer.model.save(_lowerCamelCase , name=_lowerCamelCase) return tuple(_lowerCamelCase) def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Any=None): """simple docstring""" a : Tuple = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def SCREAMING_SNAKE_CASE_ ( self : List[Any] , UpperCAmelCase_ : List[int] , UpperCAmelCase_ : Optional[List[int]] = None): """simple docstring""" a : Tuple = [self.sep_token_id] a : int = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep) * [0]
368
'''simple docstring''' import os import time import warnings from dataclasses import dataclass, field from enum import Enum from typing import List, Optional, Union import torch from filelock import FileLock from torch.utils.data import Dataset from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import logging from ..processors.glue import glue_convert_examples_to_features, glue_output_modes, glue_processors from ..processors.utils import InputFeatures UpperCamelCase : List[str] = logging.get_logger(__name__) @dataclass class UpperCamelCase : """simple docstring""" A : str = field(metadata={"help": "The name of the task to train on: " + ", ".join(glue_processors.keys() )} ) A : str = field( metadata={"help": "The input data dir. Should contain the .tsv files (or other data files) for the task."} ) A : int = field( default=128 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) A : bool = field( default=a_ , metadata={"help": "Overwrite the cached training and evaluation sets"} ) def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" a : Union[str, Any] = self.task_name.lower() class UpperCamelCase ( a_ ): """simple docstring""" A : int = "train" A : Tuple = "dev" A : List[Any] = "test" class UpperCamelCase ( a_ ): """simple docstring""" A : GlueDataTrainingArguments A : str A : List[InputFeatures] def __init__( self : Tuple , UpperCAmelCase_ : GlueDataTrainingArguments , UpperCAmelCase_ : PreTrainedTokenizerBase , UpperCAmelCase_ : Optional[int] = None , UpperCAmelCase_ : Union[str, Split] = Split.train , UpperCAmelCase_ : Optional[str] = None , ): """simple docstring""" warnings.warn( 'This dataset will be removed from the library soon, preprocessing should be handled with the 🤗 Datasets ' 'library. You can have a look at this example script for pointers: ' 'https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py' , UpperCAmelCase_ , ) a : Dict = args a : int = glue_processors[args.task_name]() a : int = glue_output_modes[args.task_name] if isinstance(UpperCAmelCase_ , UpperCAmelCase_): try: a : str = Split[mode] except KeyError: raise KeyError('mode is not a valid split name') # Load data features from cache or dataset file a : List[str] = os.path.join( cache_dir if cache_dir is not None else args.data_dir , f"""cached_{mode.value}_{tokenizer.__class__.__name__}_{args.max_seq_length}_{args.task_name}""" , ) a : Tuple = self.processor.get_labels() if args.task_name in ["mnli", "mnli-mm"] and tokenizer.__class__.__name__ in ( "RobertaTokenizer", "RobertaTokenizerFast", "XLMRobertaTokenizer", "BartTokenizer", "BartTokenizerFast", ): # HACK(label indices are swapped in RoBERTa pretrained model) a , a : str = label_list[2], label_list[1] a : int = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. a : Union[str, Any] = cached_features_file + '.lock' with FileLock(UpperCAmelCase_): if os.path.exists(UpperCAmelCase_) and not args.overwrite_cache: a : Optional[Any] = time.time() a : Optional[Any] = torch.load(UpperCAmelCase_) logger.info( f"""Loading features from cached file {cached_features_file} [took %.3f s]""" , time.time() - start) else: logger.info(f"""Creating features from dataset file at {args.data_dir}""") if mode == Split.dev: a : List[Any] = self.processor.get_dev_examples(args.data_dir) elif mode == Split.test: a : Optional[Any] = self.processor.get_test_examples(args.data_dir) else: a : List[str] = self.processor.get_train_examples(args.data_dir) if limit_length is not None: a : Dict = examples[:limit_length] a : List[Any] = glue_convert_examples_to_features( UpperCAmelCase_ , UpperCAmelCase_ , max_length=args.max_seq_length , label_list=UpperCAmelCase_ , output_mode=self.output_mode , ) a : Dict = time.time() torch.save(self.features , UpperCAmelCase_) # ^ This seems to take a lot of time so I want to investigate why and how we can improve. logger.info( f"""Saving features into cached file {cached_features_file} [took {time.time() - start:.3f} s]""") def __len__( self : Tuple): """simple docstring""" return len(self.features) def __getitem__( self : Optional[int] , UpperCAmelCase_ : List[str]): """simple docstring""" return self.features[i] def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" return self.label_list
345
0
'''simple docstring''' def SCREAMING_SNAKE_CASE__ ( snake_case : int ) -> int: """simple docstring""" if not isinstance(_snake_case , _snake_case ): a : List[str] = F"""Input value of [number={number}] must be an integer""" raise TypeError(_snake_case ) if number < 1: a : Tuple = F"""Input value of [number={number}] must be > 0""" raise ValueError(_snake_case ) a : str = 1 for i in range(1 , _snake_case ): current_number *= 4 * i - 2 current_number //= i + 1 return current_number if __name__ == "__main__": import doctest doctest.testmod()
369
'''simple docstring''' from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, logging if is_torch_available(): import torch UpperCamelCase : Dict = logging.get_logger(__name__) class UpperCamelCase ( a_ ): """simple docstring""" A : Any = ["pixel_values"] def __init__( self : str , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Optional[Dict[str, int]] = None , UpperCAmelCase_ : PILImageResampling = PILImageResampling.BILINEAR , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Dict[str, int] = None , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Union[int, float] = 1 / 2_5_5 , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Optional[Union[float, List[float]]] = None , UpperCAmelCase_ : Optional[Union[float, List[float]]] = None , **UpperCAmelCase_ : List[str] , ): """simple docstring""" super().__init__(**UpperCAmelCase_) a : str = size if size is not None else {'shortest_edge': 2_5_6} a : Dict = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) a : str = crop_size if crop_size is not None else {'height': 2_2_4, 'width': 2_2_4} a : int = get_size_dict(UpperCAmelCase_ , param_name='crop_size') a : Any = do_resize a : List[str] = size a : Union[str, Any] = resample a : int = do_center_crop a : Optional[int] = crop_size a : Tuple = do_rescale a : int = rescale_factor a : Optional[Any] = do_normalize a : Dict = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN a : Any = image_std if image_std is not None else IMAGENET_STANDARD_STD def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Dict[str, int] , UpperCAmelCase_ : PILImageResampling = PILImageResampling.BICUBIC , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Optional[int] , ): """simple docstring""" a : Optional[int] = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) if "shortest_edge" not in size: raise ValueError(f"""The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}""") a : Union[str, Any] = get_resize_output_image_size(UpperCAmelCase_ , size=size['shortest_edge'] , default_to_square=UpperCAmelCase_) return resize(UpperCAmelCase_ , size=UpperCAmelCase_ , resample=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Dict[str, int] , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Optional[int] , ): """simple docstring""" a : List[str] = get_size_dict(UpperCAmelCase_) if "height" not in size or "width" not in size: raise ValueError(f"""The `size` parameter must contain the keys `height` and `width`. Got {size.keys()}""") return center_crop(UpperCAmelCase_ , size=(size['height'], size['width']) , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : float , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Optional[Any]): """simple docstring""" return rescale(UpperCAmelCase_ , scale=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Union[float, List[float]] , UpperCAmelCase_ : Union[float, List[float]] , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Optional[int] , ): """simple docstring""" return normalize(UpperCAmelCase_ , mean=UpperCAmelCase_ , std=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : ImageInput , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Dict[str, int] = None , UpperCAmelCase_ : PILImageResampling = None , UpperCAmelCase_ : bool = None , UpperCAmelCase_ : Dict[str, int] = None , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[float] = None , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[Union[float, List[float]]] = None , UpperCAmelCase_ : Optional[Union[float, List[float]]] = None , UpperCAmelCase_ : Optional[Union[str, TensorType]] = None , UpperCAmelCase_ : Union[str, ChannelDimension] = ChannelDimension.FIRST , **UpperCAmelCase_ : List[str] , ): """simple docstring""" a : int = do_resize if do_resize is not None else self.do_resize a : int = size if size is not None else self.size a : Union[str, Any] = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) a : str = resample if resample is not None else self.resample a : Optional[Any] = do_center_crop if do_center_crop is not None else self.do_center_crop a : Union[str, Any] = crop_size if crop_size is not None else self.crop_size a : Dict = get_size_dict(UpperCAmelCase_ , param_name='crop_size') a : str = do_rescale if do_rescale is not None else self.do_rescale a : int = rescale_factor if rescale_factor is not None else self.rescale_factor a : str = do_normalize if do_normalize is not None else self.do_normalize a : List[str] = image_mean if image_mean is not None else self.image_mean a : Optional[int] = image_std if image_std is not None else self.image_std a : Dict = make_list_of_images(UpperCAmelCase_) if not valid_images(UpperCAmelCase_): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.') if do_resize and size is None: raise ValueError('Size must be specified if do_resize is True.') if do_center_crop and crop_size is None: raise ValueError('Crop size must be specified if do_center_crop is True.') if do_rescale and rescale_factor is None: raise ValueError('Rescale factor must be specified if do_rescale is True.') if do_normalize and (image_mean is None or image_std is None): raise ValueError('Image mean and std must be specified if do_normalize is True.') # All transformations expect numpy arrays. a : List[Any] = [to_numpy_array(UpperCAmelCase_) for image in images] if do_resize: a : Dict = [self.resize(image=UpperCAmelCase_ , size=UpperCAmelCase_ , resample=UpperCAmelCase_) for image in images] if do_center_crop: a : Any = [self.center_crop(image=UpperCAmelCase_ , size=UpperCAmelCase_) for image in images] if do_rescale: a : Optional[int] = [self.rescale(image=UpperCAmelCase_ , scale=UpperCAmelCase_) for image in images] if do_normalize: a : Dict = [self.normalize(image=UpperCAmelCase_ , mean=UpperCAmelCase_ , std=UpperCAmelCase_) for image in images] a : List[Any] = [to_channel_dimension_format(UpperCAmelCase_ , UpperCAmelCase_) for image in images] a : List[str] = {'pixel_values': images} return BatchFeature(data=UpperCAmelCase_ , tensor_type=UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : List[Tuple] = None): """simple docstring""" a : Dict = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(UpperCAmelCase_) != len(UpperCAmelCase_): raise ValueError( 'Make sure that you pass in as many target sizes as the batch dimension of the logits') if is_torch_tensor(UpperCAmelCase_): a : Optional[Any] = target_sizes.numpy() a : List[str] = [] for idx in range(len(UpperCAmelCase_)): a : Optional[Any] = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0) , size=target_sizes[idx] , mode='bilinear' , align_corners=UpperCAmelCase_) a : Union[str, Any] = resized_logits[0].argmax(dim=0) semantic_segmentation.append(UpperCAmelCase_) else: a : Optional[int] = logits.argmax(dim=1) a : List[str] = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0])] return semantic_segmentation
345
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available, ) UpperCamelCase : Optional[int] = { """configuration_layoutlmv2""": ["""LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP""", """LayoutLMv2Config"""], """processing_layoutlmv2""": ["""LayoutLMv2Processor"""], """tokenization_layoutlmv2""": ["""LayoutLMv2Tokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Any = ["""LayoutLMv2TokenizerFast"""] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Optional[Any] = ["""LayoutLMv2FeatureExtractor"""] UpperCamelCase : int = ["""LayoutLMv2ImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : str = [ """LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST""", """LayoutLMv2ForQuestionAnswering""", """LayoutLMv2ForSequenceClassification""", """LayoutLMv2ForTokenClassification""", """LayoutLMv2Layer""", """LayoutLMv2Model""", """LayoutLMv2PreTrainedModel""", ] if TYPE_CHECKING: from .configuration_layoutlmva import LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMvaConfig from .processing_layoutlmva import LayoutLMvaProcessor from .tokenization_layoutlmva import LayoutLMvaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor, LayoutLMvaImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_layoutlmva import ( LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST, LayoutLMvaForQuestionAnswering, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaLayer, LayoutLMvaModel, LayoutLMvaPreTrainedModel, ) else: import sys UpperCamelCase : Union[str, Any] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
370
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE__ ( snake_case : int | float | str , snake_case : int | float | str ) -> list[str]: """simple docstring""" if nth_term == "": return [""] a : Dict = int(snake_case ) a : Optional[int] = int(snake_case ) a : list[str] = [] for temp in range(int(snake_case ) ): series.append(F"""1 / {pow(temp + 1 , int(snake_case ) )}""" if series else '1' ) return series if __name__ == "__main__": import doctest doctest.testmod() UpperCamelCase : Optional[int] = int(input("""Enter the last number (nth term) of the P-Series""")) UpperCamelCase : List[Any] = int(input("""Enter the power for P-Series""")) print("""Formula of P-Series => 1+1/2^p+1/3^p ..... 1/n^p""") print(p_series(nth_term, power))
345
0
'''simple docstring''' import argparse import os import shutil from pathlib import Path import onnx import torch from packaging import version from torch.onnx import export from diffusers import OnnxRuntimeModel, OnnxStableDiffusionPipeline, StableDiffusionPipeline UpperCamelCase : Tuple = version.parse(version.parse(torch.__version__).base_version) < version.parse("""1.11""") def SCREAMING_SNAKE_CASE__ ( snake_case : Tuple , snake_case : str , snake_case : Any , snake_case : Optional[Any] , snake_case : str , snake_case : Union[str, Any] , snake_case : List[str] , snake_case : List[Any]=False , ) -> List[str]: """simple docstring""" output_path.parent.mkdir(parents=_lowerCAmelCase , exist_ok=_lowerCAmelCase ) # PyTorch deprecated the `enable_onnx_checker` and `use_external_data_format` arguments in v1.11, # so we check the torch version for backwards compatibility if is_torch_less_than_1_11: export( _lowerCAmelCase , _lowerCAmelCase , f=output_path.as_posix() , input_names=_lowerCAmelCase , output_names=_lowerCAmelCase , dynamic_axes=_lowerCAmelCase , do_constant_folding=_lowerCAmelCase , use_external_data_format=_lowerCAmelCase , enable_onnx_checker=_lowerCAmelCase , opset_version=_lowerCAmelCase , ) else: export( _lowerCAmelCase , _lowerCAmelCase , f=output_path.as_posix() , input_names=_lowerCAmelCase , output_names=_lowerCAmelCase , dynamic_axes=_lowerCAmelCase , do_constant_folding=_lowerCAmelCase , opset_version=_lowerCAmelCase , ) @torch.no_grad() def SCREAMING_SNAKE_CASE__ ( snake_case : List[Any] , snake_case : str , snake_case : Any , snake_case : List[str] = False ) -> int: """simple docstring""" a : List[Any] = torch.floataa if fpaa else torch.floataa if fpaa and torch.cuda.is_available(): a : Union[str, Any] = """cuda""" elif fpaa and not torch.cuda.is_available(): raise ValueError('`float16` model export is only supported on GPUs with CUDA' ) else: a : str = """cpu""" a : str = StableDiffusionPipeline.from_pretrained(_lowerCAmelCase , torch_dtype=_lowerCAmelCase ).to(_lowerCAmelCase ) a : Union[str, Any] = Path(_lowerCAmelCase ) # TEXT ENCODER a : Dict = pipeline.text_encoder.config.max_position_embeddings a : Tuple = pipeline.text_encoder.config.hidden_size a : Union[str, Any] = pipeline.tokenizer( 'A sample prompt' , padding='max_length' , max_length=pipeline.tokenizer.model_max_length , truncation=_lowerCAmelCase , return_tensors='pt' , ) onnx_export( pipeline.text_encoder , model_args=(text_input.input_ids.to(device=_lowerCAmelCase , dtype=torch.intaa )) , output_path=output_path / 'text_encoder' / 'model.onnx' , ordered_input_names=['input_ids'] , output_names=['last_hidden_state', 'pooler_output'] , dynamic_axes={ 'input_ids': {0: 'batch', 1: 'sequence'}, } , opset=_lowerCAmelCase , ) del pipeline.text_encoder # UNET a : str = pipeline.unet.config.in_channels a : Optional[int] = pipeline.unet.config.sample_size a : Tuple = output_path / """unet""" / """model.onnx""" onnx_export( pipeline.unet , model_args=( torch.randn(2 , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ).to(device=_lowerCAmelCase , dtype=_lowerCAmelCase ), torch.randn(2 ).to(device=_lowerCAmelCase , dtype=_lowerCAmelCase ), torch.randn(2 , _lowerCAmelCase , _lowerCAmelCase ).to(device=_lowerCAmelCase , dtype=_lowerCAmelCase ), False, ) , output_path=_lowerCAmelCase , ordered_input_names=['sample', 'timestep', 'encoder_hidden_states', 'return_dict'] , output_names=['out_sample'] , dynamic_axes={ 'sample': {0: 'batch', 1: 'channels', 2: 'height', 3: 'width'}, 'timestep': {0: 'batch'}, 'encoder_hidden_states': {0: 'batch', 1: 'sequence'}, } , opset=_lowerCAmelCase , use_external_data_format=_lowerCAmelCase , ) a : str = str(unet_path.absolute().as_posix() ) a : Any = os.path.dirname(_lowerCAmelCase ) a : List[Any] = onnx.load(_lowerCAmelCase ) # clean up existing tensor files shutil.rmtree(_lowerCAmelCase ) os.mkdir(_lowerCAmelCase ) # collate external tensor files into one onnx.save_model( _lowerCAmelCase , _lowerCAmelCase , save_as_external_data=_lowerCAmelCase , all_tensors_to_one_file=_lowerCAmelCase , location='weights.pb' , convert_attribute=_lowerCAmelCase , ) del pipeline.unet # VAE ENCODER a : Any = pipeline.vae a : Union[str, Any] = vae_encoder.config.in_channels a : Tuple = vae_encoder.config.sample_size # need to get the raw tensor output (sample) from the encoder a : Optional[int] = lambda snake_case , snake_case : vae_encoder.encode(_lowerCAmelCase , _lowerCAmelCase )[0].sample() onnx_export( _lowerCAmelCase , model_args=( torch.randn(1 , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ).to(device=_lowerCAmelCase , dtype=_lowerCAmelCase ), False, ) , output_path=output_path / 'vae_encoder' / 'model.onnx' , ordered_input_names=['sample', 'return_dict'] , output_names=['latent_sample'] , dynamic_axes={ 'sample': {0: 'batch', 1: 'channels', 2: 'height', 3: 'width'}, } , opset=_lowerCAmelCase , ) # VAE DECODER a : Optional[Any] = pipeline.vae a : Dict = vae_decoder.config.latent_channels a : str = vae_decoder.config.out_channels # forward only through the decoder part a : Union[str, Any] = vae_encoder.decode onnx_export( _lowerCAmelCase , model_args=( torch.randn(1 , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ).to(device=_lowerCAmelCase , dtype=_lowerCAmelCase ), False, ) , output_path=output_path / 'vae_decoder' / 'model.onnx' , ordered_input_names=['latent_sample', 'return_dict'] , output_names=['sample'] , dynamic_axes={ 'latent_sample': {0: 'batch', 1: 'channels', 2: 'height', 3: 'width'}, } , opset=_lowerCAmelCase , ) del pipeline.vae # SAFETY CHECKER if pipeline.safety_checker is not None: a : Optional[Any] = pipeline.safety_checker a : List[Any] = safety_checker.config.vision_config.num_channels a : Tuple = safety_checker.config.vision_config.image_size a : Dict = safety_checker.forward_onnx onnx_export( pipeline.safety_checker , model_args=( torch.randn( 1 , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , ).to(device=_lowerCAmelCase , dtype=_lowerCAmelCase ), torch.randn(1 , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ).to(device=_lowerCAmelCase , dtype=_lowerCAmelCase ), ) , output_path=output_path / 'safety_checker' / 'model.onnx' , ordered_input_names=['clip_input', 'images'] , output_names=['out_images', 'has_nsfw_concepts'] , dynamic_axes={ 'clip_input': {0: 'batch', 1: 'channels', 2: 'height', 3: 'width'}, 'images': {0: 'batch', 1: 'height', 2: 'width', 3: 'channels'}, } , opset=_lowerCAmelCase , ) del pipeline.safety_checker a : Dict = OnnxRuntimeModel.from_pretrained(output_path / 'safety_checker' ) a : Any = pipeline.feature_extractor else: a : Any = None a : Dict = None a : Tuple = OnnxStableDiffusionPipeline( vae_encoder=OnnxRuntimeModel.from_pretrained(output_path / 'vae_encoder' ) , vae_decoder=OnnxRuntimeModel.from_pretrained(output_path / 'vae_decoder' ) , text_encoder=OnnxRuntimeModel.from_pretrained(output_path / 'text_encoder' ) , tokenizer=pipeline.tokenizer , unet=OnnxRuntimeModel.from_pretrained(output_path / 'unet' ) , scheduler=pipeline.scheduler , safety_checker=_lowerCAmelCase , feature_extractor=_lowerCAmelCase , requires_safety_checker=safety_checker is not None , ) onnx_pipeline.save_pretrained(_lowerCAmelCase ) print('ONNX pipeline saved to' , _lowerCAmelCase ) del pipeline del onnx_pipeline a : List[str] = OnnxStableDiffusionPipeline.from_pretrained(_lowerCAmelCase , provider='CPUExecutionProvider' ) print('ONNX pipeline is loadable' ) if __name__ == "__main__": UpperCamelCase : List[Any] = argparse.ArgumentParser() parser.add_argument( """--model_path""", type=str, required=True, help="""Path to the `diffusers` checkpoint to convert (either a local directory or on the Hub).""", ) parser.add_argument("""--output_path""", type=str, required=True, help="""Path to the output model.""") parser.add_argument( """--opset""", default=14, type=int, help="""The version of the ONNX operator set to use.""", ) parser.add_argument("""--fp16""", action="""store_true""", default=False, help="""Export the models in `float16` mode""") UpperCamelCase : Any = parser.parse_args() convert_models(args.model_path, args.output_path, args.opset, args.fpaa)
371
'''simple docstring''' import torch def SCREAMING_SNAKE_CASE__ ( ) -> str: """simple docstring""" if torch.cuda.is_available(): a : int = torch.cuda.device_count() else: a : Any = 0 print(F"""Successfully ran on {num_gpus} GPUs""" ) if __name__ == "__main__": main()
345
0
'''simple docstring''' import math def SCREAMING_SNAKE_CASE__ ( snake_case : Any , snake_case : Any ) -> int: """simple docstring""" if 0 not in (x, y): # We use the relation x^y = y*log10(x), where 10 is the base. return y * math.logaa(__lowerCAmelCase ) else: if x == 0: # 0 raised to any number is 0 return 0 elif y == 0: return 1 # any number raised to 0 is 1 raise AssertionError('This should never happen' ) if __name__ == "__main__": # Main function # Read two numbers from input and typecast them to int using map function. # Here x is the base and y is the power. UpperCamelCase : Any = """Enter the base and the power separated by a comma: """ UpperCamelCase , UpperCamelCase : Optional[Any] = map(int, input(prompt).split(""",""")) UpperCamelCase , UpperCamelCase : Any = map(int, input(prompt).split(""",""")) # We find the log of each number, using the function res(), which takes two # arguments. UpperCamelCase : Any = res(xa, ya) UpperCamelCase : Dict = res(xa, ya) # We check for the largest number if resa > resa: print("""Largest number is""", xa, """^""", ya) elif resa > resa: print("""Largest number is""", xa, """^""", ya) else: print("""Both are equal""")
350
'''simple docstring''' import faiss # noqa: F401 # Here to have a nice missing dependency error message early on import numpy # noqa: F401 # Here to have a nice missing dependency error message early on import requests # noqa: F401 # Here to have a nice missing dependency error message early on import sklearn # noqa: F401 # Here to have a nice missing dependency error message early on import tqdm # noqa: F401 # Here to have a nice missing dependency error message early on from mauve import compute_mauve # From: mauve-text import datasets UpperCamelCase : Optional[int] = """\ @inproceedings{pillutla-etal:mauve:neurips2021, title={MAUVE: Measuring the Gap Between Neural Text and Human Text using Divergence Frontiers}, author={Pillutla, Krishna and Swayamdipta, Swabha and Zellers, Rowan and Thickstun, John and Welleck, Sean and Choi, Yejin and Harchaoui, Zaid}, booktitle = {NeurIPS}, year = {2021} } """ UpperCamelCase : Optional[Any] = """\ MAUVE is a library built on PyTorch and HuggingFace Transformers to measure the gap between neural text and human text with the eponymous MAUVE measure. MAUVE summarizes both Type I and Type II errors measured softly using Kullback–Leibler (KL) divergences. For details, see the MAUVE paper: https://arxiv.org/abs/2102.01454 (Neurips, 2021). This metrics is a wrapper around the official implementation of MAUVE: https://github.com/krishnap25/mauve """ UpperCamelCase : str = """ Calculates MAUVE scores between two lists of generated text and reference text. Args: predictions: list of generated text to score. Each predictions should be a string with tokens separated by spaces. references: list of reference for each prediction. Each reference should be a string with tokens separated by spaces. Optional Args: num_buckets: the size of the histogram to quantize P and Q. Options: 'auto' (default) or an integer pca_max_data: the number data points to use for PCA dimensionality reduction prior to clustering. If -1, use all the data. Default -1 kmeans_explained_var: amount of variance of the data to keep in dimensionality reduction by PCA. Default 0.9 kmeans_num_redo: number of times to redo k-means clustering (the best objective is kept). Default 5 kmeans_max_iter: maximum number of k-means iterations. Default 500 featurize_model_name: name of the model from which features are obtained. Default 'gpt2-large' Use one of ['gpt2', 'gpt2-medium', 'gpt2-large', 'gpt2-xl']. device_id: Device for featurization. Supply a GPU id (e.g. 0 or 3) to use GPU. If no GPU with this id is found, use CPU max_text_length: maximum number of tokens to consider. Default 1024 divergence_curve_discretization_size: Number of points to consider on the divergence curve. Default 25 mauve_scaling_factor: \"c\" from the paper. Default 5. verbose: If True (default), print running time updates seed: random seed to initialize k-means cluster assignments. Returns: mauve: MAUVE score, a number between 0 and 1. Larger values indicate that P and Q are closer, frontier_integral: Frontier Integral, a number between 0 and 1. Smaller values indicate that P and Q are closer, divergence_curve: a numpy.ndarray of shape (m, 2); plot it with matplotlib to view the divergence curve, p_hist: a discrete distribution, which is a quantized version of the text distribution p_text, q_hist: same as above, but with q_text. Examples: >>> # faiss segfaults in doctest for some reason, so the .compute call is not tested with doctest >>> import datasets >>> mauve = datasets.load_metric('mauve') >>> predictions = [\"hello there\", \"general kenobi\"] >>> references = [\"hello there\", \"general kenobi\"] >>> out = mauve.compute(predictions=predictions, references=references) # doctest: +SKIP >>> print(out.mauve) # doctest: +SKIP 1.0 """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCamelCase ( datasets.Metric ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage='https://github.com/krishnap25/mauve' , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('string' , id='sequence'), 'references': datasets.Value('string' , id='sequence'), }) , codebase_urls=['https://github.com/krishnap25/mauve'] , reference_urls=[ 'https://arxiv.org/abs/2102.01454', 'https://github.com/krishnap25/mauve', ] , ) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : Optional[Any]=None , UpperCAmelCase_ : int=None , UpperCAmelCase_ : Any=None , UpperCAmelCase_ : Optional[int]=None , UpperCAmelCase_ : Tuple="auto" , UpperCAmelCase_ : Any=-1 , UpperCAmelCase_ : Optional[int]=0.9 , UpperCAmelCase_ : Union[str, Any]=5 , UpperCAmelCase_ : int=5_0_0 , UpperCAmelCase_ : int="gpt2-large" , UpperCAmelCase_ : Tuple=-1 , UpperCAmelCase_ : Dict=1_0_2_4 , UpperCAmelCase_ : List[str]=2_5 , UpperCAmelCase_ : int=5 , UpperCAmelCase_ : Any=True , UpperCAmelCase_ : str=2_5 , ): """simple docstring""" a : List[str] = compute_mauve( p_text=UpperCAmelCase_ , q_text=UpperCAmelCase_ , p_features=UpperCAmelCase_ , q_features=UpperCAmelCase_ , p_tokens=UpperCAmelCase_ , q_tokens=UpperCAmelCase_ , num_buckets=UpperCAmelCase_ , pca_max_data=UpperCAmelCase_ , kmeans_explained_var=UpperCAmelCase_ , kmeans_num_redo=UpperCAmelCase_ , kmeans_max_iter=UpperCAmelCase_ , featurize_model_name=UpperCAmelCase_ , device_id=UpperCAmelCase_ , max_text_length=UpperCAmelCase_ , divergence_curve_discretization_size=UpperCAmelCase_ , mauve_scaling_factor=UpperCAmelCase_ , verbose=UpperCAmelCase_ , seed=UpperCAmelCase_ , ) return out
345
0
'''simple docstring''' import json import os import tempfile import unittest import numpy as np from datasets import load_dataset from transformers.testing_utils import require_torch, require_vision, slow 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 ImageGPTImageProcessor class UpperCamelCase ( unittest.TestCase ): """simple docstring""" def __init__( self : str , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Tuple=7 , UpperCAmelCase_ : Any=3 , UpperCAmelCase_ : Optional[int]=1_8 , UpperCAmelCase_ : Optional[int]=3_0 , UpperCAmelCase_ : Dict=4_0_0 , UpperCAmelCase_ : str=True , UpperCAmelCase_ : Optional[int]=None , UpperCAmelCase_ : str=True , ): """simple docstring""" a : Tuple = size if size is not None else {'height': 1_8, 'width': 1_8} a : Tuple = parent a : Dict = batch_size a : Tuple = num_channels a : Union[str, Any] = image_size a : Dict = min_resolution a : Dict = max_resolution a : Optional[int] = do_resize a : Dict = size a : Any = do_normalize def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" return { # here we create 2 clusters for the sake of simplicity "clusters": np.asarray( [ [0.88_66_44_36_34_03_32_03, 0.66_18_82_93_69_54_49_83, 0.38_91_74_64_01_78_68_04], [-0.60_42_55_91_46_88_11_04, -0.0_22_95_00_88_60_52_84_69, 0.54_23_79_73_69_00_32_96], ]), "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, } @require_torch @require_vision class UpperCamelCase ( __UpperCamelCase , unittest.TestCase ): """simple docstring""" A : Optional[int] = ImageGPTImageProcessor if is_vision_available() else None def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : Optional[Any] = ImageGPTImageProcessingTester(self) @property def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : int = self.image_processing_class(**self.image_processor_dict) self.assertTrue(hasattr(UpperCAmelCase_ , 'clusters')) self.assertTrue(hasattr(UpperCAmelCase_ , 'do_resize')) self.assertTrue(hasattr(UpperCAmelCase_ , 'size')) self.assertTrue(hasattr(UpperCAmelCase_ , 'do_normalize')) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Optional[int] = self.image_processing_class.from_dict(self.image_processor_dict) self.assertEqual(image_processor.size , {'height': 1_8, 'width': 1_8}) a : Dict = self.image_processing_class.from_dict(self.image_processor_dict , size=4_2) self.assertEqual(image_processor.size , {'height': 4_2, 'width': 4_2}) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : Tuple = self.image_processing_class(**self.image_processor_dict) a : Dict = json.loads(image_processor.to_json_string()) for key, value in self.image_processor_dict.items(): if key == "clusters": self.assertTrue(np.array_equal(UpperCAmelCase_ , obj[key])) else: self.assertEqual(obj[key] , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" a : Union[str, Any] = self.image_processing_class(**self.image_processor_dict) with tempfile.TemporaryDirectory() as tmpdirname: a : int = os.path.join(UpperCAmelCase_ , 'image_processor.json') image_processor_first.to_json_file(UpperCAmelCase_) a : List[Any] = self.image_processing_class.from_json_file(UpperCAmelCase_).to_dict() a : Union[str, Any] = image_processor_first.to_dict() for key, value in image_processor_first.items(): if key == "clusters": self.assertTrue(np.array_equal(UpperCAmelCase_ , image_processor_second[key])) else: self.assertEqual(image_processor_first[key] , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : List[str] = self.image_processing_class(**self.image_processor_dict) with tempfile.TemporaryDirectory() as tmpdirname: image_processor_first.save_pretrained(UpperCAmelCase_) a : Dict = self.image_processing_class.from_pretrained(UpperCAmelCase_).to_dict() a : Dict = image_processor_first.to_dict() for key, value in image_processor_first.items(): if key == "clusters": self.assertTrue(np.array_equal(UpperCAmelCase_ , image_processor_second[key])) else: self.assertEqual(image_processor_first[key] , UpperCAmelCase_) @unittest.skip('ImageGPT requires clusters at initialization') def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" pass def SCREAMING_SNAKE_CASE__ ( ) -> Optional[int]: """simple docstring""" a : str = load_dataset('hf-internal-testing/fixtures_image_utils' , split='test' ) a : int = Image.open(dataset[4]['file'] ) a : Tuple = Image.open(dataset[5]['file'] ) a : Tuple = [imagea, imagea] return images @require_vision @require_torch class UpperCamelCase ( unittest.TestCase ): """simple docstring""" @slow def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : Any = ImageGPTImageProcessor.from_pretrained('openai/imagegpt-small') a : List[Any] = prepare_images() # test non-batched a : Optional[int] = image_processing(images[0] , return_tensors='pt') self.assertIsInstance(encoding.input_ids , torch.LongTensor) self.assertEqual(encoding.input_ids.shape , (1, 1_0_2_4)) a : Optional[int] = [3_0_6, 1_9_1, 1_9_1] self.assertEqual(encoding.input_ids[0, :3].tolist() , UpperCAmelCase_) # test batched a : int = image_processing(UpperCAmelCase_ , return_tensors='pt') self.assertIsInstance(encoding.input_ids , torch.LongTensor) self.assertEqual(encoding.input_ids.shape , (2, 1_0_2_4)) a : Dict = [3_0_3, 1_3, 1_3] self.assertEqual(encoding.input_ids[1, -3:].tolist() , UpperCAmelCase_)
351
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE__ ( snake_case : list[int | float] , snake_case : int , snake_case : int ) -> int | float: """simple docstring""" if len(snake_case ) == 0: raise ValueError('find_max() arg is an empty sequence' ) if ( left >= len(snake_case ) or left < -len(snake_case ) or right >= len(snake_case ) or right < -len(snake_case ) ): raise IndexError('list index out of range' ) if left == right: return nums[left] a : Union[str, Any] = (left + right) >> 1 # the middle a : List[str] = find_max(snake_case , snake_case , snake_case ) # find max in range[left, mid] a : Dict = find_max(snake_case , mid + 1 , snake_case ) # 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)
345
0
'''simple docstring''' import unittest from accelerate import debug_launcher from accelerate.test_utils import require_cpu, test_ops, test_script @require_cpu class UpperCamelCase ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" debug_launcher(test_script.main) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" debug_launcher(test_ops.main)
352
'''simple docstring''' import math import os from copy import deepcopy import datasets import evaluate import torch import transformers from datasets import load_dataset from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer from accelerate import Accelerator from accelerate.test_utils import RegressionDataset, RegressionModel from accelerate.utils import is_tpu_available, set_seed UpperCamelCase : int = """true""" def SCREAMING_SNAKE_CASE__ ( snake_case : List[str] , snake_case : int=82 , snake_case : Tuple=16 ) -> Union[str, Any]: """simple docstring""" set_seed(42 ) a : List[str] = RegressionModel() a : Union[str, Any] = deepcopy(snake_case ) a : Dict = RegressionDataset(length=snake_case ) a : Dict = DataLoader(snake_case , batch_size=snake_case ) model.to(accelerator.device ) a , a : Optional[int] = accelerator.prepare(snake_case , snake_case ) return model, ddp_model, dataloader def SCREAMING_SNAKE_CASE__ ( snake_case : Accelerator , snake_case : Union[str, Any]=False ) -> Optional[int]: """simple docstring""" a : List[str] = AutoTokenizer.from_pretrained('hf-internal-testing/mrpc-bert-base-cased' ) a : Any = load_dataset('glue' , 'mrpc' , split='validation' ) def tokenize_function(snake_case : int ): a : Any = tokenizer(examples['sentence1'] , examples['sentence2'] , truncation=snake_case , max_length=snake_case ) return outputs with accelerator.main_process_first(): a : Dict = dataset.map( snake_case , batched=snake_case , remove_columns=['idx', 'sentence1', 'sentence2'] , ) a : List[str] = tokenized_datasets.rename_column('label' , 'labels' ) def collate_fn(snake_case : Optional[Any] ): if use_longest: return tokenizer.pad(snake_case , padding='longest' , return_tensors='pt' ) return tokenizer.pad(snake_case , padding='max_length' , max_length=128 , return_tensors='pt' ) return DataLoader(snake_case , shuffle=snake_case , collate_fn=snake_case , batch_size=16 ) def SCREAMING_SNAKE_CASE__ ( snake_case : Optional[Any] , snake_case : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" a : int = Accelerator(dispatch_batches=snake_case , split_batches=snake_case ) a : List[str] = get_dataloader(snake_case , not dispatch_batches ) a : Optional[Any] = AutoModelForSequenceClassification.from_pretrained( 'hf-internal-testing/mrpc-bert-base-cased' , return_dict=snake_case ) a , a : Optional[Any] = accelerator.prepare(snake_case , snake_case ) return {"ddp": [ddp_model, ddp_dataloader, "cuda:0"], "no": [model, dataloader, accelerator.device]}, accelerator def SCREAMING_SNAKE_CASE__ ( snake_case : Optional[Any] , snake_case : Optional[Any] , snake_case : Union[str, Any] ) -> Optional[int]: """simple docstring""" a : Dict = [] for batch in dataloader: a , a : Any = batch.values() with torch.no_grad(): a : Tuple = model(snake_case ) a , a : Dict = accelerator.gather_for_metrics((logit, target) ) logits_and_targets.append((logit, target) ) a , a : List[str] = [], [] for logit, targ in logits_and_targets: logits.append(snake_case ) targs.append(snake_case ) a , a : Any = torch.cat(snake_case ), torch.cat(snake_case ) return logits, targs def SCREAMING_SNAKE_CASE__ ( snake_case : Accelerator , snake_case : Dict=82 , snake_case : str=False , snake_case : List[str]=False , snake_case : List[Any]=16 ) -> Optional[int]: """simple docstring""" a , a , a : int = get_basic_setup(snake_case , snake_case , snake_case ) a , a : int = generate_predictions(snake_case , snake_case , snake_case ) assert ( len(snake_case ) == num_samples ), F"""Unexpected number of inputs:\n Expected: {num_samples}\n Actual: {len(snake_case )}""" def SCREAMING_SNAKE_CASE__ ( snake_case : bool = False , snake_case : bool = False ) -> List[str]: """simple docstring""" a : int = evaluate.load('glue' , 'mrpc' ) a , a : Tuple = get_mrpc_setup(snake_case , snake_case ) # First do baseline a , a , a : Tuple = setup['no'] model.to(snake_case ) model.eval() for batch in dataloader: batch.to(snake_case ) with torch.inference_mode(): a : List[Any] = model(**snake_case ) a : Optional[Any] = outputs.logits.argmax(dim=-1 ) metric.add_batch(predictions=snake_case , references=batch['labels'] ) a : Tuple = metric.compute() # Then do distributed a , a , a : Tuple = setup['ddp'] model.eval() for batch in dataloader: with torch.inference_mode(): a : List[str] = model(**snake_case ) a : Optional[Any] = outputs.logits.argmax(dim=-1 ) a : Optional[int] = batch['labels'] a , a : Optional[int] = accelerator.gather_for_metrics((preds, references) ) metric.add_batch(predictions=snake_case , references=snake_case ) a : str = metric.compute() for key in "accuracy f1".split(): assert math.isclose( baseline[key] , distributed[key] ), F"""Baseline and Distributed are not the same for key {key}:\n\tBaseline: {baseline[key]}\n\tDistributed: {distributed[key]}\n""" def SCREAMING_SNAKE_CASE__ ( ) -> str: """simple docstring""" a : Dict = Accelerator(split_batches=snake_case , dispatch_batches=snake_case ) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_warning() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() # These are a bit slower so they should only be ran on the GPU or TPU if torch.cuda.is_available() or is_tpu_available(): if accelerator.is_local_main_process: print('**Testing gather_for_metrics**' ) for split_batches in [True, False]: for dispatch_batches in [True, False]: if accelerator.is_local_main_process: print(F"""With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`""" ) test_mrpc(snake_case , snake_case ) accelerator.state._reset_state() if accelerator.is_local_main_process: print('**Test torch metrics**' ) for split_batches in [True, False]: for dispatch_batches in [True, False]: a : List[Any] = Accelerator(split_batches=snake_case , dispatch_batches=snake_case ) if accelerator.is_local_main_process: print(F"""With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`, length=99""" ) test_torch_metrics(snake_case , 99 ) accelerator.state._reset_state() if accelerator.is_local_main_process: print('**Test last batch is not dropped when perfectly divisible**' ) a : Optional[Any] = Accelerator() test_torch_metrics(snake_case , 512 ) accelerator.state._reset_state() def SCREAMING_SNAKE_CASE__ ( snake_case : Union[str, Any] ) -> int: """simple docstring""" # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
345
0
from __future__ import annotations import math import random from typing import Any class UpperCamelCase : """simple docstring""" def __init__( self : str): """simple docstring""" a : Optional[int] = [] a : str = 0 a : List[Any] = 0 def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" return self.head == self.tail def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : str): """simple docstring""" self.data.append(UpperCAmelCase_) a : Any = self.tail + 1 def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : Union[str, Any] = self.data[self.head] a : List[Any] = self.head + 1 return ret def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" return self.tail - self.head def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" print(self.data) print('**************') print(self.data[self.head : self.tail]) class UpperCamelCase : """simple docstring""" def __init__( self : Any , UpperCAmelCase_ : List[Any]): """simple docstring""" a : Tuple = data a : Dict = None a : int = None a : Tuple = 1 def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" return self.data def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" return self.left def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" return self.right def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" return self.height def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : Union[str, Any]): """simple docstring""" a : Union[str, Any] = data def SCREAMING_SNAKE_CASE_ ( self : Any , UpperCAmelCase_ : Dict): """simple docstring""" a : Dict = node def SCREAMING_SNAKE_CASE_ ( self : List[Any] , UpperCAmelCase_ : List[str]): """simple docstring""" a : Dict = node def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : Union[str, Any]): """simple docstring""" a : List[str] = height def SCREAMING_SNAKE_CASE__ ( snake_case : str ) -> Union[str, Any]: """simple docstring""" if node is None: return 0 return node.get_height() def SCREAMING_SNAKE_CASE__ ( snake_case : Optional[int] , snake_case : List[str] ) -> Dict: """simple docstring""" if a > b: return a return b def SCREAMING_SNAKE_CASE__ ( snake_case : List[Any] ) -> Any: """simple docstring""" print('left rotation node:' , node.get_data() ) a : List[str] = node.get_left() assert ret is not None node.set_left(ret.get_right() ) ret.set_right(lowerCamelCase__ ) a : List[Any] = my_max(get_height(node.get_right() ) , get_height(node.get_left() ) ) + 1 node.set_height(lowerCamelCase__ ) a : List[Any] = my_max(get_height(ret.get_right() ) , get_height(ret.get_left() ) ) + 1 ret.set_height(lowerCamelCase__ ) return ret def SCREAMING_SNAKE_CASE__ ( snake_case : Any ) -> Any: """simple docstring""" print('right rotation node:' , node.get_data() ) a : int = node.get_right() assert ret is not None node.set_right(ret.get_left() ) ret.set_left(lowerCamelCase__ ) a : Union[str, Any] = my_max(get_height(node.get_right() ) , get_height(node.get_left() ) ) + 1 node.set_height(lowerCamelCase__ ) a : int = my_max(get_height(ret.get_right() ) , get_height(ret.get_left() ) ) + 1 ret.set_height(lowerCamelCase__ ) return ret def SCREAMING_SNAKE_CASE__ ( snake_case : List[Any] ) -> List[str]: """simple docstring""" a : List[str] = node.get_left() assert left_child is not None node.set_left(left_rotation(lowerCamelCase__ ) ) return right_rotation(lowerCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( snake_case : Dict ) -> Optional[Any]: """simple docstring""" a : Optional[Any] = node.get_right() assert right_child is not None node.set_right(right_rotation(lowerCamelCase__ ) ) return left_rotation(lowerCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( snake_case : List[Any] , snake_case : str ) -> int: """simple docstring""" if node is None: return MyNode(lowerCamelCase__ ) if data < node.get_data(): node.set_left(insert_node(node.get_left() , lowerCamelCase__ ) ) if ( get_height(node.get_left() ) - get_height(node.get_right() ) == 2 ): # an unbalance detected a : Tuple = node.get_left() assert left_child is not None if ( data < left_child.get_data() ): # new node is the left child of the left child a : Any = right_rotation(lowerCamelCase__ ) else: a : Union[str, Any] = lr_rotation(lowerCamelCase__ ) else: node.set_right(insert_node(node.get_right() , lowerCamelCase__ ) ) if get_height(node.get_right() ) - get_height(node.get_left() ) == 2: a : Any = node.get_right() assert right_child is not None if data < right_child.get_data(): a : Dict = rl_rotation(lowerCamelCase__ ) else: a : Union[str, Any] = left_rotation(lowerCamelCase__ ) a : Optional[int] = my_max(get_height(node.get_right() ) , get_height(node.get_left() ) ) + 1 node.set_height(lowerCamelCase__ ) return node def SCREAMING_SNAKE_CASE__ ( snake_case : Dict ) -> Union[str, Any]: """simple docstring""" while True: a : Tuple = root.get_right() if right_child is None: break a : Any = right_child return root.get_data() def SCREAMING_SNAKE_CASE__ ( snake_case : List[Any] ) -> Optional[Any]: """simple docstring""" while True: a : Tuple = root.get_left() if left_child is None: break a : Union[str, Any] = left_child return root.get_data() def SCREAMING_SNAKE_CASE__ ( snake_case : Any , snake_case : Union[str, Any] ) -> List[Any]: """simple docstring""" a : Any = root.get_left() a : Any = root.get_right() if root.get_data() == data: if left_child is not None and right_child is not None: a : Tuple = get_left_most(lowerCamelCase__ ) root.set_data(lowerCamelCase__ ) root.set_right(del_node(lowerCamelCase__ , lowerCamelCase__ ) ) elif left_child is not None: a : Any = left_child elif right_child is not None: a : int = right_child else: return None elif root.get_data() > data: if left_child is None: print('No such data' ) return root else: root.set_left(del_node(lowerCamelCase__ , lowerCamelCase__ ) ) else: # root.get_data() < data if right_child is None: return root else: root.set_right(del_node(lowerCamelCase__ , lowerCamelCase__ ) ) if get_height(lowerCamelCase__ ) - get_height(lowerCamelCase__ ) == 2: assert right_child is not None if get_height(right_child.get_right() ) > get_height(right_child.get_left() ): a : List[Any] = left_rotation(lowerCamelCase__ ) else: a : List[Any] = rl_rotation(lowerCamelCase__ ) elif get_height(lowerCamelCase__ ) - get_height(lowerCamelCase__ ) == -2: assert left_child is not None if get_height(left_child.get_left() ) > get_height(left_child.get_right() ): a : List[Any] = right_rotation(lowerCamelCase__ ) else: a : Any = lr_rotation(lowerCamelCase__ ) a : Tuple = my_max(get_height(root.get_right() ) , get_height(root.get_left() ) ) + 1 root.set_height(lowerCamelCase__ ) return root class UpperCamelCase : """simple docstring""" def __init__( self : Optional[int]): """simple docstring""" a : Tuple = None def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" return get_height(self.root) def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : List[Any]): """simple docstring""" print('insert:' + str(UpperCAmelCase_)) a : Optional[Any] = insert_node(self.root , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : Tuple): """simple docstring""" print('delete:' + str(UpperCAmelCase_)) if self.root is None: print('Tree is empty!') return a : str = del_node(self.root , UpperCAmelCase_) def __str__( self : Optional[Any] , ): # a level traversale, gives a more intuitive look on the tree """simple docstring""" a : Any = '' a : Tuple = MyQueue() q.push(self.root) a : Tuple = self.get_height() if layer == 0: return output a : Any = 0 while not q.is_empty(): a : List[str] = q.pop() a : str = ' ' * int(math.pow(2 , layer - 1)) output += space if node is None: output += "*" q.push(UpperCAmelCase_) q.push(UpperCAmelCase_) else: output += str(node.get_data()) q.push(node.get_left()) q.push(node.get_right()) output += space a : Union[str, Any] = cnt + 1 for i in range(1_0_0): if cnt == math.pow(2 , UpperCAmelCase_) - 1: a : Optional[int] = layer - 1 if layer == 0: output += "\n*************************************" return output output += "\n" break output += "\n*************************************" return output def SCREAMING_SNAKE_CASE__ ( ) -> Any: """simple docstring""" import doctest doctest.testmod() if __name__ == "__main__": _test() UpperCamelCase : List[Any] = AVLtree() UpperCamelCase : Optional[int] = list(range(10)) random.shuffle(lst) for i in lst: t.insert(i) print(str(t)) random.shuffle(lst) for i in lst: t.del_node(i) print(str(t))
353
'''simple docstring''' from math import acos, sin from typing import List, Tuple, Union import numpy as np import torch from PIL import Image from ...models import AutoencoderKL, UNetaDConditionModel from ...schedulers import DDIMScheduler, DDPMScheduler from ...utils import randn_tensor from ..pipeline_utils import AudioPipelineOutput, BaseOutput, DiffusionPipeline, ImagePipelineOutput from .mel import Mel class UpperCamelCase ( a_ ): """simple docstring""" A : Optional[int] = ["vqvae"] def __init__( self : List[str] , UpperCAmelCase_ : AutoencoderKL , UpperCAmelCase_ : UNetaDConditionModel , UpperCAmelCase_ : Mel , UpperCAmelCase_ : Union[DDIMScheduler, DDPMScheduler] , ): """simple docstring""" super().__init__() self.register_modules(unet=UpperCAmelCase_ , scheduler=UpperCAmelCase_ , mel=UpperCAmelCase_ , vqvae=UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" return 5_0 if isinstance(self.scheduler , UpperCAmelCase_) else 1_0_0_0 @torch.no_grad() def __call__( self : Dict , UpperCAmelCase_ : int = 1 , UpperCAmelCase_ : str = None , UpperCAmelCase_ : np.ndarray = None , UpperCAmelCase_ : int = 0 , UpperCAmelCase_ : int = 0 , UpperCAmelCase_ : int = None , UpperCAmelCase_ : torch.Generator = None , UpperCAmelCase_ : float = 0 , UpperCAmelCase_ : float = 0 , UpperCAmelCase_ : torch.Generator = None , UpperCAmelCase_ : float = 0 , UpperCAmelCase_ : torch.Tensor = None , UpperCAmelCase_ : torch.Tensor = None , UpperCAmelCase_ : Optional[Any]=True , ): """simple docstring""" a : Optional[Any] = steps or self.get_default_steps() self.scheduler.set_timesteps(UpperCAmelCase_) a : Optional[Any] = step_generator or generator # For backwards compatibility if type(self.unet.config.sample_size) == int: a : Dict = (self.unet.config.sample_size, self.unet.config.sample_size) if noise is None: a : Dict = randn_tensor( ( batch_size, self.unet.config.in_channels, self.unet.config.sample_size[0], self.unet.config.sample_size[1], ) , generator=UpperCAmelCase_ , device=self.device , ) a : Tuple = noise a : Optional[int] = None if audio_file is not None or raw_audio is not None: self.mel.load_audio(UpperCAmelCase_ , UpperCAmelCase_) a : List[Any] = self.mel.audio_slice_to_image(UpperCAmelCase_) a : str = np.frombuffer(input_image.tobytes() , dtype='uint8').reshape( (input_image.height, input_image.width)) a : List[str] = (input_image / 2_5_5) * 2 - 1 a : Any = torch.tensor(input_image[np.newaxis, :, :] , dtype=torch.float).to(self.device) if self.vqvae is not None: a : List[Any] = self.vqvae.encode(torch.unsqueeze(UpperCAmelCase_ , 0)).latent_dist.sample( generator=UpperCAmelCase_)[0] a : str = self.vqvae.config.scaling_factor * input_images if start_step > 0: a : Union[str, Any] = self.scheduler.add_noise(UpperCAmelCase_ , UpperCAmelCase_ , self.scheduler.timesteps[start_step - 1]) a : Dict = ( self.unet.config.sample_size[1] * self.mel.get_sample_rate() / self.mel.x_res / self.mel.hop_length ) a : List[Any] = int(mask_start_secs * pixels_per_second) a : Optional[Any] = int(mask_end_secs * pixels_per_second) a : Optional[int] = self.scheduler.add_noise(UpperCAmelCase_ , UpperCAmelCase_ , torch.tensor(self.scheduler.timesteps[start_step:])) for step, t in enumerate(self.progress_bar(self.scheduler.timesteps[start_step:])): if isinstance(self.unet , UpperCAmelCase_): a : Dict = self.unet(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_)['sample'] else: a : str = self.unet(UpperCAmelCase_ , UpperCAmelCase_)['sample'] if isinstance(self.scheduler , UpperCAmelCase_): a : List[Any] = self.scheduler.step( model_output=UpperCAmelCase_ , timestep=UpperCAmelCase_ , sample=UpperCAmelCase_ , eta=UpperCAmelCase_ , generator=UpperCAmelCase_ , )['prev_sample'] else: a : Any = self.scheduler.step( model_output=UpperCAmelCase_ , timestep=UpperCAmelCase_ , sample=UpperCAmelCase_ , generator=UpperCAmelCase_ , )['prev_sample'] if mask is not None: if mask_start > 0: a : str = mask[:, step, :, :mask_start] if mask_end > 0: a : Dict = mask[:, step, :, -mask_end:] if self.vqvae is not None: # 0.18215 was scaling factor used in training to ensure unit variance a : List[str] = 1 / self.vqvae.config.scaling_factor * images a : str = self.vqvae.decode(UpperCAmelCase_)['sample'] a : Tuple = (images / 2 + 0.5).clamp(0 , 1) a : Any = images.cpu().permute(0 , 2 , 3 , 1).numpy() a : List[str] = (images * 2_5_5).round().astype('uint8') a : Tuple = list( (Image.fromarray(_[:, :, 0]) for _ in images) if images.shape[3] == 1 else (Image.fromarray(UpperCAmelCase_ , mode='RGB').convert('L') for _ in images)) a : List[str] = [self.mel.image_to_audio(UpperCAmelCase_) for _ in images] if not return_dict: return images, (self.mel.get_sample_rate(), audios) return BaseOutput(**AudioPipelineOutput(np.array(UpperCAmelCase_)[:, np.newaxis, :]) , **ImagePipelineOutput(UpperCAmelCase_)) @torch.no_grad() def SCREAMING_SNAKE_CASE_ ( self : List[Any] , UpperCAmelCase_ : List[Image.Image] , UpperCAmelCase_ : int = 5_0): """simple docstring""" assert isinstance(self.scheduler , UpperCAmelCase_) self.scheduler.set_timesteps(UpperCAmelCase_) a : Dict = np.array( [np.frombuffer(image.tobytes() , dtype='uint8').reshape((1, image.height, image.width)) for image in images]) a : Tuple = (sample / 2_5_5) * 2 - 1 a : int = torch.Tensor(UpperCAmelCase_).to(self.device) for t in self.progress_bar(torch.flip(self.scheduler.timesteps , (0,))): a : Optional[Any] = t - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps a : Optional[Any] = self.scheduler.alphas_cumprod[t] a : List[Any] = ( self.scheduler.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.scheduler.final_alpha_cumprod ) a : List[str] = 1 - alpha_prod_t a : Optional[Any] = self.unet(UpperCAmelCase_ , UpperCAmelCase_)['sample'] a : Union[str, Any] = (1 - alpha_prod_t_prev) ** 0.5 * model_output a : Dict = (sample - pred_sample_direction) * alpha_prod_t_prev ** (-0.5) a : Union[str, Any] = sample * alpha_prod_t ** 0.5 + beta_prod_t ** 0.5 * model_output return sample @staticmethod def SCREAMING_SNAKE_CASE_ ( UpperCAmelCase_ : torch.Tensor , UpperCAmelCase_ : torch.Tensor , UpperCAmelCase_ : float): """simple docstring""" a : List[Any] = acos(torch.dot(torch.flatten(UpperCAmelCase_) , torch.flatten(UpperCAmelCase_)) / torch.norm(UpperCAmelCase_) / torch.norm(UpperCAmelCase_)) return sin((1 - alpha) * theta) * xa / sin(UpperCAmelCase_) + sin(alpha * theta) * xa / sin(UpperCAmelCase_)
345
0
'''simple docstring''' from ..utils import DummyObject, requires_backends class UpperCamelCase ( metaclass=a_ ): """simple docstring""" A : Dict = ["onnx"] def __init__( self : List[str] , *UpperCAmelCase_ : Tuple , **UpperCAmelCase_ : Optional[Any]): """simple docstring""" requires_backends(self , ['onnx']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : str , *UpperCAmelCase_ : Tuple , **UpperCAmelCase_ : Union[str, Any]): """simple docstring""" requires_backends(cls , ['onnx']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : Optional[Any] , *UpperCAmelCase_ : Optional[int] , **UpperCAmelCase_ : int): """simple docstring""" requires_backends(cls , ['onnx'])
354
'''simple docstring''' import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_gpu, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class UpperCamelCase : """simple docstring""" def __init__( self : str , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : List[Any]=1_3 , UpperCAmelCase_ : List[str]=3_0 , UpperCAmelCase_ : str=2 , UpperCAmelCase_ : Union[str, Any]=3 , UpperCAmelCase_ : str=True , UpperCAmelCase_ : Union[str, Any]=True , UpperCAmelCase_ : Union[str, Any]=3_2 , UpperCAmelCase_ : Union[str, Any]=5 , UpperCAmelCase_ : Tuple=4 , UpperCAmelCase_ : Dict=3_7 , UpperCAmelCase_ : Optional[int]="gelu" , UpperCAmelCase_ : Optional[int]=0.1 , UpperCAmelCase_ : Union[str, Any]=0.1 , UpperCAmelCase_ : Dict=1_0 , UpperCAmelCase_ : Optional[Any]=0.02 , UpperCAmelCase_ : List[str]=None , UpperCAmelCase_ : Tuple=2 , ): """simple docstring""" a : Any = parent a : Optional[int] = batch_size a : str = image_size a : str = patch_size a : List[Any] = num_channels a : Optional[int] = is_training a : Dict = use_labels a : Any = hidden_size a : Optional[int] = num_hidden_layers a : int = num_attention_heads a : int = intermediate_size a : Any = hidden_act a : Optional[int] = hidden_dropout_prob a : Optional[int] = attention_probs_dropout_prob a : Dict = type_sequence_label_size a : Tuple = initializer_range a : List[str] = scope a : str = encoder_stride # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) a : Optional[Any] = (image_size // patch_size) ** 2 a : str = num_patches + 1 def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : Union[str, Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]) a : List[Any] = None if self.use_labels: a : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size) a : List[str] = self.get_config() return config, pixel_values, labels def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" return ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=UpperCAmelCase_ , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Dict): """simple docstring""" a : int = ViTModel(config=UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Optional[Any] = model(UpperCAmelCase_) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : List[Any]): """simple docstring""" a : str = ViTForMaskedImageModeling(config=UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : List[Any] = model(UpperCAmelCase_) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size)) # test greyscale images a : int = 1 a : Union[str, Any] = ViTForMaskedImageModeling(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Optional[Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]) a : Optional[Any] = model(UpperCAmelCase_) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size)) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : int , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Any): """simple docstring""" a : str = self.type_sequence_label_size a : Tuple = ViTForImageClassification(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Union[str, Any] = model(UpperCAmelCase_ , labels=UpperCAmelCase_) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) # test greyscale images a : List[Any] = 1 a : Union[str, Any] = ViTForImageClassification(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : int = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]) a : Optional[int] = model(UpperCAmelCase_) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Optional[int] = self.prepare_config_and_inputs() ( ( a ) , ( a ) , ( a ) , ) : Tuple = config_and_inputs a : str = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class UpperCamelCase ( a_ , a_ , unittest.TestCase ): """simple docstring""" A : str = ( ( ViTModel, ViTForImageClassification, ViTForMaskedImageModeling, ) if is_torch_available() else () ) A : Optional[Any] = ( {"feature-extraction": ViTModel, "image-classification": ViTForImageClassification} if is_torch_available() else {} ) A : List[str] = True A : Optional[int] = False A : Dict = False A : Optional[int] = False def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" a : str = ViTModelTester(self) a : Optional[Any] = ConfigTester(self , config_class=UpperCAmelCase_ , has_text_modality=UpperCAmelCase_ , hidden_size=3_7) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='ViT does not use inputs_embeds') def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" pass def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a , a : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a : Union[str, Any] = model_class(UpperCAmelCase_) self.assertIsInstance(model.get_input_embeddings() , (nn.Module)) a : Tuple = model.get_output_embeddings() self.assertTrue(x is None or isinstance(UpperCAmelCase_ , nn.Linear)) def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a , a : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a : Union[str, Any] = model_class(UpperCAmelCase_) a : Tuple = inspect.signature(model.forward) # signature.parameters is an OrderedDict => so arg_names order is deterministic a : Dict = [*signature.parameters.keys()] a : Dict = ['pixel_values'] self.assertListEqual(arg_names[:1] , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" a : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" a : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase_) @slow def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a : Dict = ViTModel.from_pretrained(UpperCAmelCase_) self.assertIsNotNone(UpperCAmelCase_) def SCREAMING_SNAKE_CASE__ ( ) -> Optional[Any]: """simple docstring""" a : List[Any] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class UpperCamelCase ( unittest.TestCase ): """simple docstring""" @cached_property def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224') if is_vision_available() else None @slow def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" a : Optional[Any] = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224').to(UpperCAmelCase_) a : List[Any] = self.default_image_processor a : List[str] = prepare_img() a : Tuple = image_processor(images=UpperCAmelCase_ , return_tensors='pt').to(UpperCAmelCase_) # forward pass with torch.no_grad(): a : List[Any] = model(**UpperCAmelCase_) # verify the logits a : List[str] = torch.Size((1, 1_0_0_0)) self.assertEqual(outputs.logits.shape , UpperCAmelCase_) a : Union[str, Any] = torch.tensor([-0.27_44, 0.82_15, -0.08_36]).to(UpperCAmelCase_) self.assertTrue(torch.allclose(outputs.logits[0, :3] , UpperCAmelCase_ , atol=1e-4)) @slow def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : List[str] = ViTModel.from_pretrained('facebook/dino-vits8').to(UpperCAmelCase_) a : Any = ViTImageProcessor.from_pretrained('facebook/dino-vits8' , size=4_8_0) a : int = prepare_img() a : List[str] = image_processor(images=UpperCAmelCase_ , return_tensors='pt') a : List[str] = inputs.pixel_values.to(UpperCAmelCase_) # forward pass with torch.no_grad(): a : List[Any] = model(UpperCAmelCase_ , interpolate_pos_encoding=UpperCAmelCase_) # verify the logits a : Dict = torch.Size((1, 3_6_0_1, 3_8_4)) self.assertEqual(outputs.last_hidden_state.shape , UpperCAmelCase_) a : str = torch.tensor( [[4.23_40, 4.39_06, -6.66_92], [4.54_63, 1.89_28, -6.72_57], [4.44_29, 0.84_96, -5.85_85]]).to(UpperCAmelCase_) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , UpperCAmelCase_ , atol=1e-4)) @slow @require_accelerate @require_torch_gpu def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : str = ViTModel.from_pretrained('facebook/dino-vits8' , torch_dtype=torch.floataa , device_map='auto') a : List[Any] = self.default_image_processor a : List[str] = prepare_img() a : Tuple = image_processor(images=UpperCAmelCase_ , return_tensors='pt') a : Tuple = inputs.pixel_values.to(UpperCAmelCase_) # forward pass to make sure inference works in fp16 with torch.no_grad(): a : Tuple = model(UpperCAmelCase_)
345
0
'''simple docstring''' import unittest from transformers import LiltConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( LiltForQuestionAnswering, LiltForSequenceClassification, LiltForTokenClassification, LiltModel, ) from transformers.models.lilt.modeling_lilt import LILT_PRETRAINED_MODEL_ARCHIVE_LIST class UpperCamelCase : """simple docstring""" def __init__( self : Optional[Any] , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Union[str, Any]=1_3 , UpperCAmelCase_ : Union[str, Any]=7 , UpperCAmelCase_ : List[Any]=True , UpperCAmelCase_ : Optional[int]=True , UpperCAmelCase_ : Union[str, Any]=True , UpperCAmelCase_ : Optional[Any]=True , UpperCAmelCase_ : Tuple=9_9 , UpperCAmelCase_ : List[Any]=2_4 , UpperCAmelCase_ : Union[str, Any]=2 , UpperCAmelCase_ : str=6 , UpperCAmelCase_ : Dict=3_7 , UpperCAmelCase_ : Tuple="gelu" , UpperCAmelCase_ : List[Any]=0.1 , UpperCAmelCase_ : str=0.1 , UpperCAmelCase_ : Dict=5_1_2 , UpperCAmelCase_ : Optional[Any]=1_6 , UpperCAmelCase_ : Dict=2 , UpperCAmelCase_ : Any=0.02 , UpperCAmelCase_ : Any=3 , UpperCAmelCase_ : Any=None , UpperCAmelCase_ : List[Any]=1_0_0_0 , ): """simple docstring""" a : Optional[Any] = parent a : List[Any] = batch_size a : List[str] = seq_length a : List[Any] = is_training a : Dict = use_input_mask a : Tuple = use_token_type_ids a : Optional[int] = use_labels a : List[str] = vocab_size a : Optional[Any] = hidden_size a : List[str] = num_hidden_layers a : str = num_attention_heads a : Optional[int] = intermediate_size a : Union[str, Any] = hidden_act a : List[str] = hidden_dropout_prob a : List[str] = attention_probs_dropout_prob a : Tuple = max_position_embeddings a : Optional[Any] = type_vocab_size a : List[str] = type_sequence_label_size a : Any = initializer_range a : Optional[Any] = num_labels a : List[Any] = scope a : List[str] = range_bbox def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size) a : str = ids_tensor([self.batch_size, self.seq_length, 4] , self.range_bbox) # Ensure that bbox is legal for i in range(bbox.shape[0]): for j in range(bbox.shape[1]): if bbox[i, j, 3] < bbox[i, j, 1]: a : Dict = bbox[i, j, 3] a : Optional[Any] = bbox[i, j, 1] a : Union[str, Any] = t if bbox[i, j, 2] < bbox[i, j, 0]: a : List[str] = bbox[i, j, 2] a : List[Any] = bbox[i, j, 0] a : int = t a : int = None if self.use_input_mask: a : int = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2) a : List[str] = None if self.use_token_type_ids: a : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size) a : str = None a : Union[str, Any] = None if self.use_labels: a : List[str] = ids_tensor([self.batch_size] , self.type_sequence_label_size) a : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels) a : str = self.get_config() return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" return LiltConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , ) def SCREAMING_SNAKE_CASE_ ( self : Any , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : str , ): """simple docstring""" a : Optional[Any] = LiltModel(config=_SCREAMING_SNAKE_CASE) model.to(_SCREAMING_SNAKE_CASE) model.eval() a : List[Any] = model(_SCREAMING_SNAKE_CASE , bbox=_SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , token_type_ids=_SCREAMING_SNAKE_CASE) a : str = model(_SCREAMING_SNAKE_CASE , bbox=_SCREAMING_SNAKE_CASE , token_type_ids=_SCREAMING_SNAKE_CASE) a : List[str] = model(_SCREAMING_SNAKE_CASE , bbox=_SCREAMING_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 SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Optional[int] , ): """simple docstring""" a : Optional[Any] = self.num_labels a : Union[str, Any] = LiltForTokenClassification(config=_SCREAMING_SNAKE_CASE) model.to(_SCREAMING_SNAKE_CASE) model.eval() a : List[Any] = model( _SCREAMING_SNAKE_CASE , bbox=_SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , token_type_ids=_SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels)) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : str , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Tuple , ): """simple docstring""" a : Optional[Any] = LiltForQuestionAnswering(config=_SCREAMING_SNAKE_CASE) model.to(_SCREAMING_SNAKE_CASE) model.eval() a : Any = model( _SCREAMING_SNAKE_CASE , bbox=_SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , token_type_ids=_SCREAMING_SNAKE_CASE , start_positions=_SCREAMING_SNAKE_CASE , end_positions=_SCREAMING_SNAKE_CASE , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length)) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length)) def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : List[str] = self.prepare_config_and_inputs() ( ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ) : Optional[int] = config_and_inputs a : List[str] = { 'input_ids': input_ids, 'bbox': bbox, 'token_type_ids': token_type_ids, 'attention_mask': input_mask, } return config, inputs_dict @require_torch class UpperCamelCase ( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , unittest.TestCase ): """simple docstring""" A : Dict = ( ( LiltModel, LiltForSequenceClassification, LiltForTokenClassification, LiltForQuestionAnswering, ) if is_torch_available() else () ) A : str = ( { "feature-extraction": LiltModel, "question-answering": LiltForQuestionAnswering, "text-classification": LiltForSequenceClassification, "token-classification": LiltForTokenClassification, "zero-shot": LiltForSequenceClassification, } if is_torch_available() else {} ) A : str = False A : Tuple = False def SCREAMING_SNAKE_CASE_ ( self : Any , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : Any , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Optional[Any]): """simple docstring""" return True def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" a : Union[str, Any] = LiltModelTester(self) a : int = ConfigTester(self , config_class=_SCREAMING_SNAKE_CASE , hidden_size=3_7) def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" a : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE) def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : str = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: a : List[Any] = type self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*_SCREAMING_SNAKE_CASE) def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" a : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*_SCREAMING_SNAKE_CASE) @slow def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a : Optional[Any] = LiltModel.from_pretrained(_SCREAMING_SNAKE_CASE) self.assertIsNotNone(_SCREAMING_SNAKE_CASE) @require_torch @slow class UpperCamelCase ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : Any = LiltModel.from_pretrained('SCUT-DLVCLab/lilt-roberta-en-base').to(_SCREAMING_SNAKE_CASE) a : Optional[Any] = torch.tensor([[1, 2]] , device=_SCREAMING_SNAKE_CASE) a : Optional[Any] = torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]] , device=_SCREAMING_SNAKE_CASE) # forward pass with torch.no_grad(): a : Tuple = model(input_ids=_SCREAMING_SNAKE_CASE , bbox=_SCREAMING_SNAKE_CASE) a : Optional[int] = torch.Size([1, 2, 7_6_8]) a : Optional[Any] = torch.tensor( [[-0.06_53, 0.09_50, -0.00_61], [-0.05_45, 0.09_26, -0.03_24]] , device=_SCREAMING_SNAKE_CASE , ) self.assertTrue(outputs.last_hidden_state.shape , _SCREAMING_SNAKE_CASE) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :, :3] , _SCREAMING_SNAKE_CASE , atol=1e-3))
355
'''simple docstring''' # Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import numpy as np import torch from ..models.clipseg import CLIPSegForImageSegmentation from ..utils import is_vision_available, requires_backends from .base import PipelineTool if is_vision_available(): from PIL import Image class UpperCamelCase ( a_ ): """simple docstring""" A : List[str] = ( "This is a tool that creates a segmentation mask of an image according to a label. It cannot create an image." "It takes two arguments named `image` which should be the original image, and `label` which should be a text " "describing the elements what should be identified in the segmentation mask. The tool returns the mask." ) A : List[Any] = "CIDAS/clipseg-rd64-refined" A : Optional[Any] = "image_segmenter" A : List[Any] = CLIPSegForImageSegmentation A : Tuple = ["image", "text"] A : Optional[int] = ["image"] def __init__( self : str , *UpperCAmelCase_ : str , **UpperCAmelCase_ : str): """simple docstring""" requires_backends(self , ['vision']) super().__init__(*UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : "Image" , UpperCAmelCase_ : str): """simple docstring""" return self.pre_processor(text=[label] , images=[image] , padding=UpperCAmelCase_ , return_tensors='pt') def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : str): """simple docstring""" with torch.no_grad(): a : Union[str, Any] = self.model(**UpperCAmelCase_).logits return logits def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , UpperCAmelCase_ : int): """simple docstring""" a : int = outputs.cpu().detach().numpy() a : str = 0 a : str = 1 return Image.fromarray((array * 2_5_5).astype(np.uinta))
345
0
'''simple docstring''' import os import time import warnings from dataclasses import dataclass, field from enum import Enum from typing import List, Optional, Union import torch from filelock import FileLock from torch.utils.data import Dataset from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import logging from ..processors.glue import glue_convert_examples_to_features, glue_output_modes, glue_processors from ..processors.utils import InputFeatures UpperCamelCase : Dict = logging.get_logger(__name__) @dataclass class UpperCamelCase : """simple docstring""" A : str = field(metadata={"help": "The name of the task to train on: " + ", ".join(glue_processors.keys() )} ) A : Optional[Any] = field( metadata={"help": "The input data dir. Should contain the .tsv files (or other data files) for the task."} ) A : List[str] = field( default=128 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) A : str = field( default=snake_case_ , metadata={"help": "Overwrite the cached training and evaluation sets"} ) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : List[str] = self.task_name.lower() class UpperCamelCase ( snake_case_ ): """simple docstring""" A : int = "train" A : Any = "dev" A : Optional[int] = "test" class UpperCamelCase ( snake_case_ ): """simple docstring""" A : List[str] = 42 A : int = 42 A : Tuple = 42 def __init__( self : Any , UpperCAmelCase_ : GlueDataTrainingArguments , UpperCAmelCase_ : PreTrainedTokenizerBase , UpperCAmelCase_ : Optional[int] = None , UpperCAmelCase_ : Union[str, Split] = Split.train , UpperCAmelCase_ : Optional[str] = None , ): """simple docstring""" warnings.warn( 'This dataset will be removed from the library soon, preprocessing should be handled with the 🤗 Datasets ' 'library. You can have a look at this example script for pointers: ' 'https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py' , UpperCAmelCase_ , ) a : Any = args a : List[str] = glue_processors[args.task_name]() a : str = glue_output_modes[args.task_name] if isinstance(UpperCAmelCase_ , UpperCAmelCase_): try: a : str = Split[mode] except KeyError: raise KeyError('mode is not a valid split name') # Load data features from cache or dataset file a : Optional[Any] = os.path.join( cache_dir if cache_dir is not None else args.data_dir , f"""cached_{mode.value}_{tokenizer.__class__.__name__}_{args.max_seq_length}_{args.task_name}""" , ) a : int = self.processor.get_labels() if args.task_name in ["mnli", "mnli-mm"] and tokenizer.__class__.__name__ in ( "RobertaTokenizer", "RobertaTokenizerFast", "XLMRobertaTokenizer", "BartTokenizer", "BartTokenizerFast", ): # HACK(label indices are swapped in RoBERTa pretrained model) a : Dict = label_list[2], label_list[1] a : Optional[Any] = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. a : int = cached_features_file + '.lock' with FileLock(UpperCAmelCase_): if os.path.exists(UpperCAmelCase_) and not args.overwrite_cache: a : Optional[int] = time.time() a : Union[str, Any] = torch.load(UpperCAmelCase_) logger.info( f"""Loading features from cached file {cached_features_file} [took %.3f s]""" , time.time() - start) else: logger.info(f"""Creating features from dataset file at {args.data_dir}""") if mode == Split.dev: a : Any = self.processor.get_dev_examples(args.data_dir) elif mode == Split.test: a : Union[str, Any] = self.processor.get_test_examples(args.data_dir) else: a : List[Any] = self.processor.get_train_examples(args.data_dir) if limit_length is not None: a : Optional[int] = examples[:limit_length] a : int = glue_convert_examples_to_features( UpperCAmelCase_ , UpperCAmelCase_ , max_length=args.max_seq_length , label_list=UpperCAmelCase_ , output_mode=self.output_mode , ) a : Any = time.time() torch.save(self.features , UpperCAmelCase_) # ^ This seems to take a lot of time so I want to investigate why and how we can improve. logger.info( f"""Saving features into cached file {cached_features_file} [took {time.time() - start:.3f} s]""") def __len__( self : Any): """simple docstring""" return len(self.features) def __getitem__( self : int , UpperCAmelCase_ : str): """simple docstring""" return self.features[i] def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" return self.label_list
356
'''simple docstring''' import json import os import unittest from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer from ...test_tokenization_common import TokenizerTesterMixin class UpperCamelCase ( a_ , unittest.TestCase ): """simple docstring""" A : Union[str, Any] = CTRLTokenizer A : List[Any] = False A : Optional[Any] = False def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt a : Dict = ['adapt', 're@@', 'a@@', 'apt', 'c@@', 't', '<unk>'] a : Tuple = dict(zip(UpperCAmelCase_ , range(len(UpperCAmelCase_)))) a : Any = ['#version: 0.2', 'a p', 'ap t</w>', 'r e', 'a d', 'ad apt</w>', ''] a : List[Any] = {'unk_token': '<unk>'} a : Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file']) a : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['merges_file']) with open(self.vocab_file , 'w' , encoding='utf-8') as fp: fp.write(json.dumps(UpperCAmelCase_) + '\n') with open(self.merges_file , 'w' , encoding='utf-8') as fp: fp.write('\n'.join(UpperCAmelCase_)) def SCREAMING_SNAKE_CASE_ ( self : Tuple , **UpperCAmelCase_ : Dict): """simple docstring""" kwargs.update(self.special_tokens_map) return CTRLTokenizer.from_pretrained(self.tmpdirname , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : Any): """simple docstring""" a : List[str] = 'adapt react readapt apt' a : int = 'adapt react readapt apt' return input_text, output_text def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : int = CTRLTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map) a : str = 'adapt react readapt apt' a : Optional[Any] = 'adapt re@@ a@@ c@@ t re@@ adapt apt'.split() a : List[Any] = tokenizer.tokenize(UpperCAmelCase_) self.assertListEqual(UpperCAmelCase_ , UpperCAmelCase_) a : Union[str, Any] = tokens + [tokenizer.unk_token] a : Any = [0, 1, 2, 4, 5, 1, 0, 3, 6] self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCAmelCase_) , UpperCAmelCase_)
345
0
from math import ceil, sqrt def SCREAMING_SNAKE_CASE__ ( snake_case : int = 1_000_000 ) -> Union[str, Any]: """simple docstring""" a : List[str] = 0 for outer_width in range(3 , (limit // 4) + 2 ): if outer_width**2 > limit: a : str = max(ceil(sqrt(outer_width**2 - limit ) ) , 1 ) else: a : Dict = 1 if (outer_width - hole_width_lower_bound) % 2: hole_width_lower_bound += 1 answer += (outer_width - hole_width_lower_bound - 2) // 2 + 1 return answer if __name__ == "__main__": print(f'''{solution() = }''')
357
'''simple docstring''' def SCREAMING_SNAKE_CASE__ ( snake_case : int , snake_case : int ) -> bool: """simple docstring""" return numa ^ numa < 0 if __name__ == "__main__": import doctest doctest.testmod()
345
0
'''simple docstring''' import numpy as np def SCREAMING_SNAKE_CASE__ ( snake_case : List[str] , snake_case : List[Any] , snake_case : Any = 1E-1_2 , snake_case : int = 100 , ): """simple docstring""" assert np.shape(A__ )[0] == np.shape(A__ )[1] # Ensure proper dimensionality. assert np.shape(A__ )[0] == np.shape(A__ )[0] # Ensure inputs are either both complex or both real assert np.iscomplexobj(A__ ) == np.iscomplexobj(A__ ) a : List[Any] = np.iscomplexobj(A__ ) if is_complex: # Ensure complex input_matrix is Hermitian assert np.array_equal(A__ , input_matrix.conj().T ) # Set convergence to False. Will define convergence when we exceed max_iterations # or when we have small changes from one iteration to next. a : List[str] = False a : Dict = 0 a : Optional[Any] = 0 a : Any = 1E1_2 while not convergence: # Multiple matrix by the vector. a : Optional[int] = np.dot(A__ , A__ ) # Normalize the resulting output vector. a : Optional[Any] = w / np.linalg.norm(A__ ) # Find rayleigh quotient # (faster than usual b/c we know vector is normalized already) a : Union[str, Any] = vector.conj().T if is_complex else vector.T a : List[Any] = np.dot(A__ , np.dot(A__ , A__ ) ) # Check convergence. a : Union[str, Any] = np.abs(lambda_ - lambda_previous ) / lambda_ iterations += 1 if error <= error_tol or iterations >= max_iterations: a : Any = True a : List[str] = lambda_ if is_complex: a : Optional[int] = np.real(lambda_ ) return lambda_, vector def SCREAMING_SNAKE_CASE__ ( ): """simple docstring""" a : List[Any] = np.array([[41, 4, 20], [4, 26, 30], [20, 30, 50]] ) a : Union[str, Any] = np.array([41, 4, 20] ) a : Tuple = real_input_matrix.astype(np.complexaaa ) a : Optional[Any] = np.triu(1J * complex_input_matrix , 1 ) complex_input_matrix += imag_matrix complex_input_matrix += -1 * imag_matrix.T a : Tuple = np.array([41, 4, 20] ).astype(np.complexaaa ) for problem_type in ["real", "complex"]: if problem_type == "real": a : List[Any] = real_input_matrix a : Optional[Any] = real_vector elif problem_type == "complex": a : List[str] = complex_input_matrix a : str = complex_vector # Our implementation. a , a : Dict = power_iteration(A__ , A__ ) # Numpy implementation. # Get eigenvalues and eigenvectors using built-in numpy # eigh (eigh used for symmetric or hermetian matrices). a , a : Dict = np.linalg.eigh(A__ ) # Last eigenvalue is the maximum one. a : Tuple = eigen_values[-1] # Last column in this matrix is eigenvector corresponding to largest eigenvalue. a : int = eigen_vectors[:, -1] # Check our implementation and numpy gives close answers. assert np.abs(eigen_value - eigen_value_max ) <= 1E-6 # Take absolute values element wise of each eigenvector. # as they are only unique to a minus sign. assert np.linalg.norm(np.abs(A__ ) - np.abs(A__ ) ) <= 1E-6 if __name__ == "__main__": import doctest doctest.testmod() test_power_iteration()
358
'''simple docstring''' from typing import Dict, Iterable, 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, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, logging UpperCamelCase : int = logging.get_logger(__name__) class UpperCamelCase ( a_ ): """simple docstring""" A : Dict = ["pixel_values"] def __init__( self : Optional[Any] , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Dict[str, int] = None , UpperCAmelCase_ : PILImageResampling = PILImageResampling.BICUBIC , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Dict[str, int] = None , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Union[int, float] = 1 / 2_5_5 , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Optional[Union[float, Iterable[float]]] = IMAGENET_DEFAULT_MEAN , UpperCAmelCase_ : Optional[Union[float, Iterable[float]]] = IMAGENET_DEFAULT_STD , **UpperCAmelCase_ : List[Any] , ): """simple docstring""" super().__init__(**UpperCAmelCase_) a : List[str] = size if size is not None else {'shortest_edge': 2_2_4} a : str = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) a : str = crop_size if crop_size is not None else {'height': 2_2_4, 'width': 2_2_4} a : int = get_size_dict(UpperCAmelCase_ , param_name='crop_size') a : Any = do_resize a : Dict = size a : Optional[Any] = resample a : List[Any] = do_center_crop a : List[Any] = crop_size a : Optional[Any] = do_rescale a : Dict = rescale_factor a : Tuple = do_normalize a : int = image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN a : Optional[Any] = image_std if image_std is not None else IMAGENET_DEFAULT_STD def SCREAMING_SNAKE_CASE_ ( self : List[Any] , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Dict[str, int] , UpperCAmelCase_ : PILImageResampling = PILImageResampling.BICUBIC , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Any , ): """simple docstring""" a : Optional[Any] = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) # size_dict is a dict with either keys "height" and "width" or "shortest_edge" if "shortest_edge" in size: a : int = int((2_5_6 / 2_2_4) * size['shortest_edge']) a : Optional[int] = get_resize_output_image_size(UpperCAmelCase_ , size=UpperCAmelCase_ , default_to_square=UpperCAmelCase_) a : Optional[Any] = {'height': output_size[0], 'width': output_size[1]} if "height" not in size_dict or "width" not in size_dict: raise ValueError( f"""Size dict must have keys 'height' and 'width' or 'shortest_edge'. Got {size_dict.keys()}""") return resize( UpperCAmelCase_ , size=(size_dict['height'], size_dict['width']) , resample=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Dict[str, int] , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Union[str, Any] , ): """simple docstring""" a : str = get_size_dict(UpperCAmelCase_) if "height" not in size or "width" not in size: raise ValueError(f"""Size dict must have keys 'height' and 'width'. Got {size.keys()}""") return center_crop(UpperCAmelCase_ , size=(size['height'], size['width']) , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Union[int, float] , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Optional[Any] , ): """simple docstring""" return rescale(UpperCAmelCase_ , scale=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Union[float, List[float]] , UpperCAmelCase_ : Union[float, List[float]] , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : List[str] , ): """simple docstring""" return normalize(UpperCAmelCase_ , mean=UpperCAmelCase_ , std=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , UpperCAmelCase_ : ImageInput , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[Dict[str, int]] = None , UpperCAmelCase_ : PILImageResampling = None , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[Dict[str, int]] = None , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[float] = None , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[Union[float, Iterable[float]]] = None , UpperCAmelCase_ : Optional[Union[float, Iterable[float]]] = None , UpperCAmelCase_ : Optional[TensorType] = None , UpperCAmelCase_ : ChannelDimension = ChannelDimension.FIRST , **UpperCAmelCase_ : Optional[Any] , ): """simple docstring""" a : int = do_resize if do_resize is not None else self.do_resize a : Optional[int] = resample if resample is not None else self.resample a : Tuple = do_center_crop if do_center_crop is not None else self.do_center_crop a : Tuple = do_rescale if do_rescale is not None else self.do_rescale a : List[Any] = rescale_factor if rescale_factor is not None else self.rescale_factor a : Dict = do_normalize if do_normalize is not None else self.do_normalize a : Tuple = image_mean if image_mean is not None else self.image_mean a : int = image_std if image_std is not None else self.image_std a : Optional[int] = size if size is not None else self.size a : Optional[Any] = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) a : List[Any] = crop_size if crop_size is not None else self.crop_size a : str = get_size_dict(UpperCAmelCase_ , param_name='crop_size') a : Dict = make_list_of_images(UpperCAmelCase_) if not valid_images(UpperCAmelCase_): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.') if do_resize and size is None: raise ValueError('Size must be specified if do_resize is True.') if do_center_crop and crop_size is None: raise ValueError('Crop size must be specified if do_center_crop is True.') if do_rescale and rescale_factor is None: raise ValueError('Rescale factor must be specified if do_rescale is True.') if do_normalize and (image_mean is None or image_std is None): raise ValueError('Image mean and std must be specified if do_normalize is True.') # All transformations expect numpy arrays. a : Any = [to_numpy_array(UpperCAmelCase_) for image in images] if do_resize: a : Optional[int] = [self.resize(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_) for image in images] if do_center_crop: a : int = [self.center_crop(UpperCAmelCase_ , UpperCAmelCase_) for image in images] if do_rescale: a : Any = [self.rescale(UpperCAmelCase_ , UpperCAmelCase_) for image in images] if do_normalize: a : str = [self.normalize(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_) for image in images] a : Optional[int] = [to_channel_dimension_format(UpperCAmelCase_ , UpperCAmelCase_) for image in images] a : Optional[int] = {'pixel_values': images} return BatchFeature(data=UpperCAmelCase_ , tensor_type=UpperCAmelCase_)
345
0
'''simple docstring''' def SCREAMING_SNAKE_CASE__ ( snake_case : str ) -> Dict: # noqa: E741 a : Dict = len(_UpperCAmelCase ) a : Optional[int] = 0 a : Tuple = [0] * n a : Dict = [False] * n a : List[str] = [False] * n def dfs(snake_case : Tuple , snake_case : Optional[Any] , snake_case : Any , snake_case : Tuple ): if parent == root: out_edge_count += 1 a : Dict = True a : Tuple = at for to in l[at]: if to == parent: pass elif not visited[to]: a : Union[str, Any] = dfs(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) a : int = min(low[at] , low[to] ) # AP found via bridge if at < low[to]: a : List[Any] = True # AP found via cycle if at == low[to]: a : int = True else: a : Optional[int] = min(low[at] , _UpperCAmelCase ) return out_edge_count for i in range(_UpperCAmelCase ): if not visited[i]: a : Optional[Any] = 0 a : str = dfs(_UpperCAmelCase , _UpperCAmelCase , -1 , _UpperCAmelCase ) a : Optional[int] = out_edge_count > 1 for x in range(len(_UpperCAmelCase ) ): if is_art[x] is True: print(_UpperCAmelCase ) # Adjacency list of graph UpperCamelCase : Tuple = { 0: [1, 2], 1: [0, 2], 2: [0, 1, 3, 5], 3: [2, 4], 4: [3], 5: [2, 6, 8], 6: [5, 7], 7: [6, 8], 8: [5, 7], } compute_ap(data)
359
'''simple docstring''' from __future__ import annotations from decimal import Decimal from math import * # noqa: F403 from sympy import diff def SCREAMING_SNAKE_CASE__ ( snake_case : str , snake_case : float | Decimal , snake_case : float = 10**-10 ) -> float: """simple docstring""" a : Dict = a while True: a : Any = Decimal(snake_case ) - ( Decimal(eval(snake_case ) ) / Decimal(eval(str(diff(snake_case ) ) ) ) # noqa: S307 ) # This number dictates the accuracy of the answer if abs(eval(snake_case ) ) < precision: # noqa: S307 return float(snake_case ) # Let's Execute if __name__ == "__main__": # Find root of trigonometric function # Find value of pi print(f'''The root of sin(x) = 0 is {newton_raphson("sin(x)", 2)}''') # Find root of polynomial print(f'''The root of x**2 - 5*x + 2 = 0 is {newton_raphson("x**2 - 5*x + 2", 0.4)}''') # Find Square Root of 5 print(f'''The root of log(x) - 1 = 0 is {newton_raphson("log(x) - 1", 2)}''') # Exponential Roots print(f'''The root of exp(x) - 1 = 0 is {newton_raphson("exp(x) - 1", 0)}''')
345
0
'''simple docstring''' import torch def SCREAMING_SNAKE_CASE__ ( ) -> List[Any]: """simple docstring""" if torch.cuda.is_available(): a : Union[str, Any] = torch.cuda.device_count() else: a : Dict = 0 print(F"""Successfully ran on {num_gpus} GPUs""" ) if __name__ == "__main__": main()
360
'''simple docstring''' import unittest from transformers import XLMConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( XLMForMultipleChoice, XLMForQuestionAnswering, XLMForQuestionAnsweringSimple, XLMForSequenceClassification, XLMForTokenClassification, XLMModel, XLMWithLMHeadModel, ) from transformers.models.xlm.modeling_xlm import XLM_PRETRAINED_MODEL_ARCHIVE_LIST class UpperCamelCase : """simple docstring""" def __init__( self : Union[str, Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Optional[int]=1_3 , UpperCAmelCase_ : Dict=7 , UpperCAmelCase_ : int=True , UpperCAmelCase_ : Tuple=True , UpperCAmelCase_ : Any=True , UpperCAmelCase_ : List[Any]=True , UpperCAmelCase_ : List[Any]=True , UpperCAmelCase_ : List[Any]=False , UpperCAmelCase_ : List[Any]=False , UpperCAmelCase_ : Union[str, Any]=False , UpperCAmelCase_ : Union[str, Any]=2 , UpperCAmelCase_ : Optional[Any]=9_9 , UpperCAmelCase_ : str=0 , UpperCAmelCase_ : int=3_2 , UpperCAmelCase_ : Tuple=5 , UpperCAmelCase_ : Any=4 , UpperCAmelCase_ : Tuple=0.1 , UpperCAmelCase_ : int=0.1 , UpperCAmelCase_ : Optional[int]=5_1_2 , UpperCAmelCase_ : str=2 , UpperCAmelCase_ : Any=0.02 , UpperCAmelCase_ : str=2 , UpperCAmelCase_ : Optional[int]=4 , UpperCAmelCase_ : int="last" , UpperCAmelCase_ : Union[str, Any]=True , UpperCAmelCase_ : Tuple=None , UpperCAmelCase_ : List[str]=0 , ): """simple docstring""" a : Tuple = parent a : Optional[Any] = batch_size a : Tuple = seq_length a : Union[str, Any] = is_training a : List[str] = use_input_lengths a : Union[str, Any] = use_token_type_ids a : Optional[int] = use_labels a : int = gelu_activation a : Dict = sinusoidal_embeddings a : Any = causal a : Optional[int] = asm a : int = n_langs a : List[str] = vocab_size a : List[str] = n_special a : List[str] = hidden_size a : Any = num_hidden_layers a : Union[str, Any] = num_attention_heads a : Optional[Any] = hidden_dropout_prob a : str = attention_probs_dropout_prob a : Dict = max_position_embeddings a : Union[str, Any] = type_sequence_label_size a : str = initializer_range a : List[Any] = num_labels a : Union[str, Any] = num_choices a : Optional[Any] = summary_type a : Optional[Any] = use_proj a : Optional[Any] = scope a : Dict = bos_token_id def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size) a : List[Any] = random_attention_mask([self.batch_size, self.seq_length]) a : Optional[int] = None if self.use_input_lengths: a : Optional[int] = ( ids_tensor([self.batch_size] , vocab_size=2) + self.seq_length - 2 ) # small variation of seq_length a : int = None if self.use_token_type_ids: a : str = ids_tensor([self.batch_size, self.seq_length] , self.n_langs) a : Optional[Any] = None a : Tuple = None a : Optional[Any] = None if self.use_labels: a : Optional[int] = ids_tensor([self.batch_size] , self.type_sequence_label_size) a : int = ids_tensor([self.batch_size, self.seq_length] , self.num_labels) a : Optional[Any] = ids_tensor([self.batch_size] , 2).float() a : Dict = ids_tensor([self.batch_size] , self.num_choices) a : Any = self.get_config() return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" return XLMConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , num_labels=self.num_labels , bos_token_id=self.bos_token_id , ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : str , UpperCAmelCase_ : Any , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : str , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[Any] , ): """simple docstring""" a : Any = XLMModel(config=UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : int = model(UpperCAmelCase_ , lengths=UpperCAmelCase_ , langs=UpperCAmelCase_) a : str = model(UpperCAmelCase_ , langs=UpperCAmelCase_) a : int = model(UpperCAmelCase_) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : str , UpperCAmelCase_ : int , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : Any , UpperCAmelCase_ : List[Any] , ): """simple docstring""" a : Optional[Any] = XLMWithLMHeadModel(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : List[str] = model(UpperCAmelCase_ , token_type_ids=UpperCAmelCase_ , labels=UpperCAmelCase_) self.parent.assertEqual(result.loss.shape , ()) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size)) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : Dict , UpperCAmelCase_ : int , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : str , UpperCAmelCase_ : Optional[Any] , ): """simple docstring""" a : Union[str, Any] = XLMForQuestionAnsweringSimple(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : List[str] = model(UpperCAmelCase_) a : Tuple = model(UpperCAmelCase_ , start_positions=UpperCAmelCase_ , end_positions=UpperCAmelCase_) a : Any = outputs self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length)) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length)) def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Any , UpperCAmelCase_ : Any , ): """simple docstring""" a : Any = XLMForQuestionAnswering(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : int = model(UpperCAmelCase_) a : Dict = model( UpperCAmelCase_ , start_positions=UpperCAmelCase_ , end_positions=UpperCAmelCase_ , cls_index=UpperCAmelCase_ , is_impossible=UpperCAmelCase_ , p_mask=UpperCAmelCase_ , ) a : int = model( UpperCAmelCase_ , start_positions=UpperCAmelCase_ , end_positions=UpperCAmelCase_ , cls_index=UpperCAmelCase_ , is_impossible=UpperCAmelCase_ , ) ((a) , ) : Union[str, Any] = result_with_labels.to_tuple() a : int = model(UpperCAmelCase_ , start_positions=UpperCAmelCase_ , end_positions=UpperCAmelCase_) ((a) , ) : Union[str, Any] = result_with_labels.to_tuple() self.parent.assertEqual(result_with_labels.loss.shape , ()) self.parent.assertEqual(result.start_top_log_probs.shape , (self.batch_size, model.config.start_n_top)) self.parent.assertEqual(result.start_top_index.shape , (self.batch_size, model.config.start_n_top)) self.parent.assertEqual( result.end_top_log_probs.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top)) self.parent.assertEqual( result.end_top_index.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top)) self.parent.assertEqual(result.cls_logits.shape , (self.batch_size,)) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : int , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : str , ): """simple docstring""" a : Dict = XLMForSequenceClassification(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Union[str, Any] = model(UpperCAmelCase_) a : Union[str, Any] = model(UpperCAmelCase_ , labels=UpperCAmelCase_) self.parent.assertEqual(result.loss.shape , ()) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , UpperCAmelCase_ : str , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : str , UpperCAmelCase_ : Any , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Any , ): """simple docstring""" a : Dict = self.num_labels a : int = XLMForTokenClassification(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Optional[Any] = model(UpperCAmelCase_ , attention_mask=UpperCAmelCase_ , labels=UpperCAmelCase_) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels)) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : str , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Dict , UpperCAmelCase_ : str , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Dict , ): """simple docstring""" a : str = self.num_choices a : Dict = XLMForMultipleChoice(config=UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Union[str, Any] = input_ids.unsqueeze(1).expand(-1 , self.num_choices , -1).contiguous() a : str = token_type_ids.unsqueeze(1).expand(-1 , self.num_choices , -1).contiguous() a : Optional[int] = input_mask.unsqueeze(1).expand(-1 , self.num_choices , -1).contiguous() a : Any = model( UpperCAmelCase_ , attention_mask=UpperCAmelCase_ , token_type_ids=UpperCAmelCase_ , labels=UpperCAmelCase_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices)) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" a : int = self.prepare_config_and_inputs() ( ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ) : Union[str, Any] = config_and_inputs a : Optional[int] = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'lengths': input_lengths} return config, inputs_dict @require_torch class UpperCamelCase ( a_ , a_ , a_ , unittest.TestCase ): """simple docstring""" A : int = ( ( XLMModel, XLMWithLMHeadModel, XLMForQuestionAnswering, XLMForSequenceClassification, XLMForQuestionAnsweringSimple, XLMForTokenClassification, XLMForMultipleChoice, ) if is_torch_available() else () ) A : List[str] = ( (XLMWithLMHeadModel,) if is_torch_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable A : Optional[Any] = ( { "feature-extraction": XLMModel, "fill-mask": XLMWithLMHeadModel, "question-answering": XLMForQuestionAnsweringSimple, "text-classification": XLMForSequenceClassification, "text-generation": XLMWithLMHeadModel, "token-classification": XLMForTokenClassification, "zero-shot": XLMForSequenceClassification, } if is_torch_available() else {} ) def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Any , UpperCAmelCase_ : Any): """simple docstring""" if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith('Fast') ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : int , UpperCAmelCase_ : Dict=False): """simple docstring""" a : List[Any] = super()._prepare_for_class(UpperCAmelCase_ , UpperCAmelCase_ , return_labels=UpperCAmelCase_) if return_labels: if model_class.__name__ == "XLMForQuestionAnswering": a : Optional[int] = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=UpperCAmelCase_) a : str = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=UpperCAmelCase_) return inputs_dict def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : List[Any] = XLMModelTester(self) a : Tuple = ConfigTester(self , config_class=UpperCAmelCase_ , emb_dim=3_7) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_model(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_lm_head(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_simple_qa(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_qa(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_sequence_classif(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_token_classif(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" a : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_for_multiple_choice(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : int , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Any=False , UpperCAmelCase_ : Optional[Any]=1): """simple docstring""" self.assertIsInstance(UpperCAmelCase_ , UpperCAmelCase_) self.assertListEqual( [isinstance(UpperCAmelCase_ , UpperCAmelCase_) for iter_attentions in attentions] , [True] * len(UpperCAmelCase_)) self.assertEqual(len(UpperCAmelCase_) , (max_length - min_length) * num_beam_groups) for idx, iter_attentions in enumerate(UpperCAmelCase_): # adds PAD dummy token a : List[str] = min_length + idx + 1 a : Optional[Any] = min_length + idx + 1 a : Optional[int] = ( batch_size * num_beam_groups, config.num_attention_heads, tgt_len, src_len, ) # check attn size self.assertListEqual( [layer_attention.shape for layer_attention in iter_attentions] , [expected_shape] * len(UpperCAmelCase_)) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : str=False , UpperCAmelCase_ : Union[str, Any]=1): """simple docstring""" self.assertIsInstance(UpperCAmelCase_ , UpperCAmelCase_) self.assertListEqual( [isinstance(UpperCAmelCase_ , UpperCAmelCase_) for iter_hidden_states in hidden_states] , [True] * len(UpperCAmelCase_) , ) self.assertEqual(len(UpperCAmelCase_) , (max_length - min_length) * num_beam_groups) for idx, iter_hidden_states in enumerate(UpperCAmelCase_): # adds PAD dummy token a : int = min_length + idx + 1 a : Any = (batch_size * num_beam_groups, seq_len, config.hidden_size) # check hidden size self.assertListEqual( [layer_hidden_states.shape for layer_hidden_states in iter_hidden_states] , [expected_shape] * len(UpperCAmelCase_) , ) pass @slow def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a : Union[str, Any] = XLMModel.from_pretrained(UpperCAmelCase_) self.assertIsNotNone(UpperCAmelCase_) @require_torch class UpperCamelCase ( unittest.TestCase ): """simple docstring""" @slow def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" a : Dict = XLMWithLMHeadModel.from_pretrained('xlm-mlm-en-2048') model.to(UpperCAmelCase_) a : List[Any] = torch.tensor([[1_4, 4_4_7]] , dtype=torch.long , device=UpperCAmelCase_) # the president a : Dict = [ 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, ] # the president the president the president the president the president the president the president the president the president the president # TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference a : Optional[Any] = model.generate(UpperCAmelCase_ , do_sample=UpperCAmelCase_) self.assertListEqual(output_ids[0].cpu().numpy().tolist() , UpperCAmelCase_)
345
0
'''simple docstring''' import os import tempfile from functools import partial from unittest import TestCase from unittest.mock import patch import numpy as np import pytest from datasets.arrow_dataset import Dataset from datasets.search import ElasticSearchIndex, FaissIndex, MissingIndex from .utils import require_elasticsearch, require_faiss UpperCamelCase : List[str] = pytest.mark.integration @require_faiss class UpperCamelCase ( __lowercase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : Any = Dataset.from_dict({'filename': ['my_name-train' + '_' + str(UpperCAmelCase__) for x in np.arange(3_0).tolist()]}) return dset def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" import faiss a : Dict = self._create_dummy_dataset() a : Optional[Any] = dset.map( lambda UpperCAmelCase_ , UpperCAmelCase_: {"vecs": i * np.ones(5 , dtype=np.floataa)} , with_indices=UpperCAmelCase__ , keep_in_memory=UpperCAmelCase__) a : Optional[Any] = dset.add_faiss_index('vecs' , batch_size=1_0_0 , metric_type=faiss.METRIC_INNER_PRODUCT) a , a : Union[str, Any] = dset.get_nearest_examples('vecs' , np.ones(5 , dtype=np.floataa)) self.assertEqual(examples['filename'][0] , 'my_name-train_29') dset.drop_index('vecs') def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" import faiss a : int = self._create_dummy_dataset() dset.add_faiss_index_from_external_arrays( external_arrays=np.ones((3_0, 5)) * np.arange(3_0).reshape(-1 , 1) , index_name='vecs' , batch_size=1_0_0 , metric_type=faiss.METRIC_INNER_PRODUCT , ) a , a : str = dset.get_nearest_examples('vecs' , np.ones(5 , dtype=np.floataa)) self.assertEqual(examples['filename'][0] , 'my_name-train_29') def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" import faiss a : Optional[Any] = self._create_dummy_dataset() dset.add_faiss_index_from_external_arrays( external_arrays=np.ones((3_0, 5)) * np.arange(3_0).reshape(-1 , 1) , index_name='vecs' , metric_type=faiss.METRIC_INNER_PRODUCT , ) # Setting delete=False and unlinking manually is not pretty... but it is required on Windows to # ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue. # see https://bugs.python.org/issue14243 and # https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515 with tempfile.NamedTemporaryFile(delete=UpperCAmelCase__) as tmp_file: dset.save_faiss_index('vecs' , tmp_file.name) dset.load_faiss_index('vecs2' , tmp_file.name) os.unlink(tmp_file.name) a , a : Optional[int] = dset.get_nearest_examples('vecs2' , np.ones(5 , dtype=np.floataa)) self.assertEqual(examples['filename'][0] , 'my_name-train_29') def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : List[str] = self._create_dummy_dataset() dset.add_faiss_index_from_external_arrays( external_arrays=np.ones((3_0, 5)) * np.arange(3_0).reshape(-1 , 1) , index_name='vecs') dset.drop_index('vecs') self.assertRaises(UpperCAmelCase__ , partial(dset.get_nearest_examples , 'vecs2' , np.ones(5 , dtype=np.floataa))) def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" from elasticsearch import Elasticsearch a : Any = self._create_dummy_dataset() with patch('elasticsearch.Elasticsearch.search') as mocked_search, patch( 'elasticsearch.client.IndicesClient.create') as mocked_index_create, patch('elasticsearch.helpers.streaming_bulk') as mocked_bulk: a : str = {'acknowledged': True} mocked_bulk.return_value([(True, None)] * 3_0) a : Dict = {'hits': {'hits': [{'_score': 1, '_id': 2_9}]}} a : Dict = Elasticsearch() dset.add_elasticsearch_index('filename' , es_client=UpperCAmelCase__) a , a : List[str] = dset.get_nearest_examples('filename' , 'my_name-train_29') self.assertEqual(examples['filename'][0] , 'my_name-train_29') @require_faiss class UpperCamelCase ( __lowercase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" import faiss a : int = FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT) # add vectors index.add_vectors(np.eye(5 , dtype=np.floataa)) self.assertIsNotNone(index.faiss_index) self.assertEqual(index.faiss_index.ntotal , 5) index.add_vectors(np.zeros((5, 5) , dtype=np.floataa)) self.assertEqual(index.faiss_index.ntotal , 1_0) # single query a : Dict = np.zeros(5 , dtype=np.floataa) a : int = 1 a , a : Tuple = index.search(UpperCAmelCase__) self.assertRaises(UpperCAmelCase__ , index.search , query.reshape(-1 , 1)) self.assertGreater(scores[0] , 0) self.assertEqual(indices[0] , 1) # batched queries a : Optional[Any] = np.eye(5 , dtype=np.floataa)[::-1] a , a : Union[str, Any] = index.search_batch(UpperCAmelCase__) self.assertRaises(UpperCAmelCase__ , index.search_batch , queries[0]) a : Tuple = [scores[0] for scores in total_scores] a : str = [indices[0] for indices in total_indices] self.assertGreater(np.min(UpperCAmelCase__) , 0) self.assertListEqual([4, 3, 2, 1, 0] , UpperCAmelCase__) def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" import faiss a : List[Any] = FaissIndex(string_factory='Flat') index.add_vectors(np.eye(5 , dtype=np.floataa)) self.assertIsInstance(index.faiss_index , faiss.IndexFlat) a : str = FaissIndex(string_factory='LSH') index.add_vectors(np.eye(5 , dtype=np.floataa)) self.assertIsInstance(index.faiss_index , faiss.IndexLSH) with self.assertRaises(UpperCAmelCase__): a : int = FaissIndex(string_factory='Flat' , custom_index=faiss.IndexFlat(5)) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" import faiss a : Union[str, Any] = faiss.IndexFlat(5) a : List[Any] = FaissIndex(custom_index=UpperCAmelCase__) index.add_vectors(np.eye(5 , dtype=np.floataa)) self.assertIsInstance(index.faiss_index , faiss.IndexFlat) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" import faiss a : List[str] = FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT) index.add_vectors(np.eye(5 , dtype=np.floataa)) # Setting delete=False and unlinking manually is not pretty... but it is required on Windows to # ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue. # see https://bugs.python.org/issue14243 and # https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515 with tempfile.NamedTemporaryFile(delete=UpperCAmelCase__) as tmp_file: index.save(tmp_file.name) a : Dict = FaissIndex.load(tmp_file.name) os.unlink(tmp_file.name) a : Union[str, Any] = np.zeros(5 , dtype=np.floataa) a : Optional[Any] = 1 a , a : Any = index.search(UpperCAmelCase__) self.assertGreater(scores[0] , 0) self.assertEqual(indices[0] , 1) @require_faiss def SCREAMING_SNAKE_CASE__ ( snake_case : Union[str, Any] ) -> List[str]: """simple docstring""" import faiss a : str = FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT ) index.add_vectors(np.eye(5 , dtype=np.floataa ) ) a : Optional[int] = 'index.faiss' a : str = F"""mock://{index_name}""" index.save(snake_case , storage_options=mockfs.storage_options ) a : List[Any] = FaissIndex.load(snake_case , storage_options=mockfs.storage_options ) a : Optional[int] = np.zeros(5 , dtype=np.floataa ) a : Tuple = 1 a , a : str = index.search(snake_case ) assert scores[0] > 0 assert indices[0] == 1 @require_elasticsearch class UpperCamelCase ( __lowercase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" from elasticsearch import Elasticsearch with patch('elasticsearch.Elasticsearch.search') as mocked_search, patch( 'elasticsearch.client.IndicesClient.create') as mocked_index_create, patch('elasticsearch.helpers.streaming_bulk') as mocked_bulk: a : Dict = Elasticsearch() a : Tuple = {'acknowledged': True} a : Optional[int] = ElasticSearchIndex(es_client=UpperCAmelCase__) mocked_bulk.return_value([(True, None)] * 3) index.add_documents(['foo', 'bar', 'foobar']) # single query a : Optional[Any] = 'foo' a : Union[str, Any] = {'hits': {'hits': [{'_score': 1, '_id': 0}]}} a , a : int = index.search(UpperCAmelCase__) self.assertEqual(scores[0] , 1) self.assertEqual(indices[0] , 0) # single query with timeout a : int = 'foo' a : Union[str, Any] = {'hits': {'hits': [{'_score': 1, '_id': 0}]}} a , a : List[Any] = index.search(UpperCAmelCase__ , request_timeout=3_0) self.assertEqual(scores[0] , 1) self.assertEqual(indices[0] , 0) # batched queries a : Any = ['foo', 'bar', 'foobar'] a : Optional[int] = {'hits': {'hits': [{'_score': 1, '_id': 1}]}} a , a : List[str] = index.search_batch(UpperCAmelCase__) a : str = [scores[0] for scores in total_scores] a : Union[str, Any] = [indices[0] for indices in total_indices] self.assertGreater(np.min(UpperCAmelCase__) , 0) self.assertListEqual([1, 1, 1] , UpperCAmelCase__) # batched queries with timeout a : Dict = ['foo', 'bar', 'foobar'] a : int = {'hits': {'hits': [{'_score': 1, '_id': 1}]}} a , a : Any = index.search_batch(UpperCAmelCase__ , request_timeout=3_0) a : Tuple = [scores[0] for scores in total_scores] a : List[Any] = [indices[0] for indices in total_indices] self.assertGreater(np.min(UpperCAmelCase__) , 0) self.assertListEqual([1, 1, 1] , UpperCAmelCase__)
361
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, is_vision_available, ) UpperCamelCase : List[str] = {"""processing_layoutxlm""": ["""LayoutXLMProcessor"""]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Any = ["""LayoutXLMTokenizer"""] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Optional[int] = ["""LayoutXLMTokenizerFast"""] if TYPE_CHECKING: from .processing_layoutxlm import LayoutXLMProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutxlm import LayoutXLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutxlm_fast import LayoutXLMTokenizerFast else: import sys UpperCamelCase : Dict = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
345
0
'''simple docstring''' from __future__ import annotations import unittest from transformers import DebertaVaConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFDebertaVaForMaskedLM, TFDebertaVaForQuestionAnswering, TFDebertaVaForSequenceClassification, TFDebertaVaForTokenClassification, TFDebertaVaModel, ) class UpperCamelCase : """simple docstring""" def __init__( self : Union[str, Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Any=1_3 , UpperCAmelCase_ : Optional[int]=7 , UpperCAmelCase_ : Optional[Any]=True , UpperCAmelCase_ : Optional[Any]=True , UpperCAmelCase_ : Dict=True , UpperCAmelCase_ : List[Any]=True , UpperCAmelCase_ : Dict=9_9 , UpperCAmelCase_ : List[Any]=3_2 , UpperCAmelCase_ : str=2 , UpperCAmelCase_ : List[str]=4 , UpperCAmelCase_ : Optional[int]=3_7 , UpperCAmelCase_ : Optional[Any]="gelu" , UpperCAmelCase_ : Dict=0.1 , UpperCAmelCase_ : str=0.1 , UpperCAmelCase_ : str=5_1_2 , UpperCAmelCase_ : List[Any]=1_6 , UpperCAmelCase_ : List[str]=2 , UpperCAmelCase_ : Union[str, Any]=0.02 , UpperCAmelCase_ : List[str]=False , UpperCAmelCase_ : Union[str, Any]=True , UpperCAmelCase_ : Optional[int]="None" , UpperCAmelCase_ : Any=3 , UpperCAmelCase_ : List[Any]=4 , UpperCAmelCase_ : List[Any]=None , ): """simple docstring""" a : Optional[Any] = parent a : List[Any] = batch_size a : Tuple = seq_length a : Optional[Any] = is_training a : str = use_input_mask a : List[Any] = use_token_type_ids a : Any = use_labels a : Any = vocab_size a : Dict = hidden_size a : List[str] = num_hidden_layers a : Tuple = num_attention_heads a : List[str] = intermediate_size a : Union[str, Any] = hidden_act a : Any = hidden_dropout_prob a : Union[str, Any] = attention_probs_dropout_prob a : Optional[Any] = max_position_embeddings a : Any = type_vocab_size a : List[str] = type_sequence_label_size a : Dict = initializer_range a : List[Any] = num_labels a : Dict = num_choices a : Union[str, Any] = relative_attention a : List[str] = position_biased_input a : int = pos_att_type a : Optional[int] = scope def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size) a : int = None if self.use_input_mask: a : str = random_attention_mask([self.batch_size, self.seq_length]) a : Optional[int] = None if self.use_token_type_ids: a : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size) a : Union[str, Any] = None a : Optional[int] = None a : Any = None if self.use_labels: a : Optional[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size) a : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels) a : List[Any] = DebertaVaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , relative_attention=self.relative_attention , position_biased_input=self.position_biased_input , initializer_range=self.initializer_range , return_dict=_a , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : int , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Dict): """simple docstring""" a : List[Any] = TFDebertaVaModel(config=_a) a : Any = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} a : int = [input_ids, input_mask] a : List[str] = model(_a) a : Dict = model(_a) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Tuple): """simple docstring""" a : Union[str, Any] = TFDebertaVaForMaskedLM(config=_a) a : Optional[int] = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } a : Any = model(_a) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size)) def SCREAMING_SNAKE_CASE_ ( self : List[Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : int , UpperCAmelCase_ : Any , UpperCAmelCase_ : List[Any]): """simple docstring""" a : Union[str, Any] = self.num_labels a : List[Any] = TFDebertaVaForSequenceClassification(config=_a) a : int = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } a : Union[str, Any] = model(_a) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels)) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : int , UpperCAmelCase_ : int , UpperCAmelCase_ : Any , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Union[str, Any]): """simple docstring""" a : Union[str, Any] = self.num_labels a : str = TFDebertaVaForTokenClassification(config=_a) a : List[Any] = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } a : List[str] = model(_a) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels)) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Dict): """simple docstring""" a : List[Any] = TFDebertaVaForQuestionAnswering(config=_a) a : List[Any] = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } a : Dict = model(_a) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length)) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length)) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : str = self.prepare_config_and_inputs() ( a ) : str = config_and_inputs a : Union[str, Any] = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask} return config, inputs_dict @require_tf class UpperCamelCase ( UpperCamelCase_ , UpperCamelCase_ , unittest.TestCase ): """simple docstring""" A : int = ( ( TFDebertaVaModel, TFDebertaVaForMaskedLM, TFDebertaVaForQuestionAnswering, TFDebertaVaForSequenceClassification, TFDebertaVaForTokenClassification, ) if is_tf_available() else () ) A : int = ( { """feature-extraction""": TFDebertaVaModel, """fill-mask""": TFDebertaVaForMaskedLM, """question-answering""": TFDebertaVaForQuestionAnswering, """text-classification""": TFDebertaVaForSequenceClassification, """token-classification""": TFDebertaVaForTokenClassification, """zero-shot""": TFDebertaVaForSequenceClassification, } if is_tf_available() else {} ) A : Union[str, Any] = False A : List[str] = False def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : Dict = TFDebertaVaModelTester(self) a : Any = ConfigTester(self , config_class=_a , hidden_size=3_7) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" a : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" a : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*_a) def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*_a) def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" a : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*_a) def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*_a) @slow def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : Optional[Any] = TFDebertaVaModel.from_pretrained('kamalkraj/deberta-v2-xlarge') self.assertIsNotNone(_a) @require_tf class UpperCamelCase ( unittest.TestCase ): """simple docstring""" @unittest.skip(reason='Model not available yet') def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" pass @slow def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" a : Optional[Any] = TFDebertaVaModel.from_pretrained('kamalkraj/deberta-v2-xlarge') a : Optional[Any] = tf.constant([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2]]) a : Optional[Any] = tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]) a : Tuple = model(_a , attention_mask=_a)[0] a : List[str] = tf.constant( [[[0.23_56, 0.19_48, 0.03_69], [-0.10_63, 0.35_86, -0.51_52], [-0.63_99, -0.02_59, -0.25_25]]]) tf.debugging.assert_near(output[:, 1:4, 1:4] , _a , atol=1e-4)
362
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCamelCase : Tuple = { """configuration_pegasus_x""": ["""PEGASUS_X_PRETRAINED_CONFIG_ARCHIVE_MAP""", """PegasusXConfig"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Union[str, Any] = [ """PEGASUS_X_PRETRAINED_MODEL_ARCHIVE_LIST""", """PegasusXForConditionalGeneration""", """PegasusXModel""", """PegasusXPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_pegasus_x import PEGASUS_X_PRETRAINED_CONFIG_ARCHIVE_MAP, PegasusXConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_pegasus_x import ( PEGASUS_X_PRETRAINED_MODEL_ARCHIVE_LIST, PegasusXForConditionalGeneration, PegasusXModel, PegasusXPreTrainedModel, ) else: import sys UpperCamelCase : List[str] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
345
0
'''simple docstring''' import argparse import os import torch from diffusers import ( CMStochasticIterativeScheduler, ConsistencyModelPipeline, UNetaDModel, ) UpperCamelCase : int = { 'sample_size': 32, 'in_channels': 3, 'out_channels': 3, 'layers_per_block': 2, 'num_class_embeds': 1_000, 'block_out_channels': [32, 64], 'attention_head_dim': 8, 'down_block_types': [ 'ResnetDownsampleBlock2D', 'AttnDownBlock2D', ], 'up_block_types': [ 'AttnUpBlock2D', 'ResnetUpsampleBlock2D', ], 'resnet_time_scale_shift': 'scale_shift', 'upsample_type': 'resnet', 'downsample_type': 'resnet', } UpperCamelCase : List[str] = { 'sample_size': 64, 'in_channels': 3, 'out_channels': 3, 'layers_per_block': 3, 'num_class_embeds': 1_000, 'block_out_channels': [192, 192 * 2, 192 * 3, 192 * 4], 'attention_head_dim': 64, 'down_block_types': [ 'ResnetDownsampleBlock2D', 'AttnDownBlock2D', 'AttnDownBlock2D', 'AttnDownBlock2D', ], 'up_block_types': [ 'AttnUpBlock2D', 'AttnUpBlock2D', 'AttnUpBlock2D', 'ResnetUpsampleBlock2D', ], 'resnet_time_scale_shift': 'scale_shift', 'upsample_type': 'resnet', 'downsample_type': 'resnet', } UpperCamelCase : List[Any] = { 'sample_size': 256, 'in_channels': 3, 'out_channels': 3, 'layers_per_block': 2, 'num_class_embeds': None, 'block_out_channels': [256, 256, 256 * 2, 256 * 2, 256 * 4, 256 * 4], 'attention_head_dim': 64, 'down_block_types': [ 'ResnetDownsampleBlock2D', 'ResnetDownsampleBlock2D', 'ResnetDownsampleBlock2D', 'AttnDownBlock2D', 'AttnDownBlock2D', 'AttnDownBlock2D', ], 'up_block_types': [ 'AttnUpBlock2D', 'AttnUpBlock2D', 'AttnUpBlock2D', 'ResnetUpsampleBlock2D', 'ResnetUpsampleBlock2D', 'ResnetUpsampleBlock2D', ], 'resnet_time_scale_shift': 'default', 'upsample_type': 'resnet', 'downsample_type': 'resnet', } UpperCamelCase : str = { 'num_train_timesteps': 40, 'sigma_min': 0.0_02, 'sigma_max': 80.0, } UpperCamelCase : List[str] = { 'num_train_timesteps': 201, 'sigma_min': 0.0_02, 'sigma_max': 80.0, } UpperCamelCase : int = { 'num_train_timesteps': 151, 'sigma_min': 0.0_02, 'sigma_max': 80.0, } def SCREAMING_SNAKE_CASE__ ( snake_case : Tuple ) -> Any: """simple docstring""" if isinstance(lowercase__ , lowercase__ ): 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 argparse.ArgumentTypeError('boolean value expected' ) def SCREAMING_SNAKE_CASE__ ( snake_case : Optional[int] , snake_case : List[Any] , snake_case : int , snake_case : Optional[Any] , snake_case : int=False ) -> Union[str, Any]: """simple docstring""" a : Optional[Any] = checkpoint[F"""{old_prefix}.in_layers.0.weight"""] a : Optional[Any] = checkpoint[F"""{old_prefix}.in_layers.0.bias"""] a : Optional[int] = checkpoint[F"""{old_prefix}.in_layers.2.weight"""] a : Optional[int] = checkpoint[F"""{old_prefix}.in_layers.2.bias"""] a : List[str] = checkpoint[F"""{old_prefix}.emb_layers.1.weight"""] a : Tuple = checkpoint[F"""{old_prefix}.emb_layers.1.bias"""] a : Union[str, Any] = checkpoint[F"""{old_prefix}.out_layers.0.weight"""] a : Dict = checkpoint[F"""{old_prefix}.out_layers.0.bias"""] a : Dict = checkpoint[F"""{old_prefix}.out_layers.3.weight"""] a : Tuple = checkpoint[F"""{old_prefix}.out_layers.3.bias"""] if has_skip: a : Tuple = checkpoint[F"""{old_prefix}.skip_connection.weight"""] a : str = checkpoint[F"""{old_prefix}.skip_connection.bias"""] return new_checkpoint def SCREAMING_SNAKE_CASE__ ( snake_case : List[Any] , snake_case : Union[str, Any] , snake_case : Optional[int] , snake_case : Optional[int] , snake_case : Optional[Any]=None ) -> int: """simple docstring""" a , a , a : int = checkpoint[F"""{old_prefix}.qkv.weight"""].chunk(3 , dim=0 ) a , a , a : Any = checkpoint[F"""{old_prefix}.qkv.bias"""].chunk(3 , dim=0 ) a : Union[str, Any] = checkpoint[F"""{old_prefix}.norm.weight"""] a : str = checkpoint[F"""{old_prefix}.norm.bias"""] a : Any = weight_q.squeeze(-1 ).squeeze(-1 ) a : Dict = bias_q.squeeze(-1 ).squeeze(-1 ) a : List[Any] = weight_k.squeeze(-1 ).squeeze(-1 ) a : int = bias_k.squeeze(-1 ).squeeze(-1 ) a : int = weight_v.squeeze(-1 ).squeeze(-1 ) a : Tuple = bias_v.squeeze(-1 ).squeeze(-1 ) a : Optional[int] = ( checkpoint[F"""{old_prefix}.proj_out.weight"""].squeeze(-1 ).squeeze(-1 ) ) a : int = checkpoint[F"""{old_prefix}.proj_out.bias"""].squeeze(-1 ).squeeze(-1 ) return new_checkpoint def SCREAMING_SNAKE_CASE__ ( snake_case : Tuple , snake_case : Dict ) -> List[Any]: """simple docstring""" a : int = torch.load(lowercase__ , map_location='cpu' ) a : List[str] = {} a : Dict = checkpoint['time_embed.0.weight'] a : Dict = checkpoint['time_embed.0.bias'] a : Union[str, Any] = checkpoint['time_embed.2.weight'] a : Any = checkpoint['time_embed.2.bias'] if unet_config["num_class_embeds"] is not None: a : Tuple = checkpoint['label_emb.weight'] a : Optional[int] = checkpoint['input_blocks.0.0.weight'] a : List[str] = checkpoint['input_blocks.0.0.bias'] a : int = unet_config['down_block_types'] a : Optional[int] = unet_config['layers_per_block'] a : Optional[int] = unet_config['attention_head_dim'] a : Union[str, Any] = unet_config['block_out_channels'] a : Union[str, Any] = 1 a : List[Any] = channels_list[0] for i, layer_type in enumerate(lowercase__ ): a : Optional[int] = channels_list[i] a : Dict = current_channels != prev_channels if layer_type == "ResnetDownsampleBlock2D": for j in range(lowercase__ ): a : Optional[Any] = F"""down_blocks.{i}.resnets.{j}""" a : Tuple = F"""input_blocks.{current_layer}.0""" a : Optional[Any] = True if j == 0 and downsample_block_has_skip else False a : Tuple = convert_resnet(lowercase__ , lowercase__ , lowercase__ , lowercase__ , has_skip=lowercase__ ) current_layer += 1 elif layer_type == "AttnDownBlock2D": for j in range(lowercase__ ): a : Dict = F"""down_blocks.{i}.resnets.{j}""" a : Union[str, Any] = F"""input_blocks.{current_layer}.0""" a : List[Any] = True if j == 0 and downsample_block_has_skip else False a : Optional[int] = convert_resnet(lowercase__ , lowercase__ , lowercase__ , lowercase__ , has_skip=lowercase__ ) a : List[Any] = F"""down_blocks.{i}.attentions.{j}""" a : int = F"""input_blocks.{current_layer}.1""" a : List[str] = convert_attention( lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ ) current_layer += 1 if i != len(lowercase__ ) - 1: a : Tuple = F"""down_blocks.{i}.downsamplers.0""" a : Tuple = F"""input_blocks.{current_layer}.0""" a : Dict = convert_resnet(lowercase__ , lowercase__ , lowercase__ , lowercase__ ) current_layer += 1 a : Any = current_channels # hardcoded the mid-block for now a : Union[str, Any] = 'mid_block.resnets.0' a : Optional[Any] = 'middle_block.0' a : str = convert_resnet(lowercase__ , lowercase__ , lowercase__ , lowercase__ ) a : Optional[int] = 'mid_block.attentions.0' a : str = 'middle_block.1' a : Tuple = convert_attention(lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ ) a : Union[str, Any] = 'mid_block.resnets.1' a : List[Any] = 'middle_block.2' a : Optional[Any] = convert_resnet(lowercase__ , lowercase__ , lowercase__ , lowercase__ ) a : str = 0 a : Optional[int] = unet_config['up_block_types'] for i, layer_type in enumerate(lowercase__ ): if layer_type == "ResnetUpsampleBlock2D": for j in range(layers_per_block + 1 ): a : Optional[Any] = F"""up_blocks.{i}.resnets.{j}""" a : Dict = F"""output_blocks.{current_layer}.0""" a : Optional[int] = convert_resnet(lowercase__ , lowercase__ , lowercase__ , lowercase__ , has_skip=lowercase__ ) current_layer += 1 if i != len(lowercase__ ) - 1: a : Tuple = F"""up_blocks.{i}.upsamplers.0""" a : Optional[Any] = F"""output_blocks.{current_layer-1}.1""" a : Any = convert_resnet(lowercase__ , lowercase__ , lowercase__ , lowercase__ ) elif layer_type == "AttnUpBlock2D": for j in range(layers_per_block + 1 ): a : Union[str, Any] = F"""up_blocks.{i}.resnets.{j}""" a : Optional[int] = F"""output_blocks.{current_layer}.0""" a : str = convert_resnet(lowercase__ , lowercase__ , lowercase__ , lowercase__ , has_skip=lowercase__ ) a : Dict = F"""up_blocks.{i}.attentions.{j}""" a : Optional[Any] = F"""output_blocks.{current_layer}.1""" a : Any = convert_attention( lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ ) current_layer += 1 if i != len(lowercase__ ) - 1: a : str = F"""up_blocks.{i}.upsamplers.0""" a : List[str] = F"""output_blocks.{current_layer-1}.2""" a : Dict = convert_resnet(lowercase__ , lowercase__ , lowercase__ , lowercase__ ) a : Dict = checkpoint['out.0.weight'] a : Dict = checkpoint['out.0.bias'] a : str = checkpoint['out.2.weight'] a : Tuple = checkpoint['out.2.bias'] return new_checkpoint if __name__ == "__main__": UpperCamelCase : List[str] = argparse.ArgumentParser() parser.add_argument("""--unet_path""", default=None, type=str, required=True, help="""Path to the unet.pt to convert.""") parser.add_argument( """--dump_path""", default=None, type=str, required=True, help="""Path to output the converted UNet model.""" ) parser.add_argument("""--class_cond""", default=True, type=str, help="""Whether the model is class-conditional.""") UpperCamelCase : List[Any] = parser.parse_args() UpperCamelCase : Tuple = strabool(args.class_cond) UpperCamelCase : Any = os.path.basename(args.unet_path) print(f'''Checkpoint: {ckpt_name}''') # Get U-Net config if "imagenet64" in ckpt_name: UpperCamelCase : Optional[Any] = IMAGENET_64_UNET_CONFIG elif "256" in ckpt_name and (("bedroom" in ckpt_name) or ("cat" in ckpt_name)): UpperCamelCase : List[Any] = LSUN_256_UNET_CONFIG elif "test" in ckpt_name: UpperCamelCase : List[Any] = TEST_UNET_CONFIG else: raise ValueError(f'''Checkpoint type {ckpt_name} is not currently supported.''') if not args.class_cond: UpperCamelCase : Union[str, Any] = None UpperCamelCase : Dict = con_pt_to_diffuser(args.unet_path, unet_config) UpperCamelCase : List[Any] = UNetaDModel(**unet_config) image_unet.load_state_dict(converted_unet_ckpt) # Get scheduler config if "cd" in ckpt_name or "test" in ckpt_name: UpperCamelCase : Dict = CD_SCHEDULER_CONFIG elif "ct" in ckpt_name and "imagenet64" in ckpt_name: UpperCamelCase : Optional[int] = CT_IMAGENET_64_SCHEDULER_CONFIG elif "ct" in ckpt_name and "256" in ckpt_name and (("bedroom" in ckpt_name) or ("cat" in ckpt_name)): UpperCamelCase : List[Any] = CT_LSUN_256_SCHEDULER_CONFIG else: raise ValueError(f'''Checkpoint type {ckpt_name} is not currently supported.''') UpperCamelCase : Optional[int] = CMStochasticIterativeScheduler(**scheduler_config) UpperCamelCase : List[str] = ConsistencyModelPipeline(unet=image_unet, scheduler=cm_scheduler) consistency_model.save_pretrained(args.dump_path)
363
'''simple docstring''' import math from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase : str = logging.get_logger(__name__) UpperCamelCase : List[str] = { """facebook/data2vec-base-960h""": """https://huggingface.co/facebook/data2vec-audio-base-960h/resolve/main/config.json""", # See all Data2VecAudio models at https://huggingface.co/models?filter=data2vec-audio } class UpperCamelCase ( a_ ): """simple docstring""" A : Optional[int] = "data2vec-audio" def __init__( self : Dict , UpperCAmelCase_ : Optional[int]=3_2 , UpperCAmelCase_ : Union[str, Any]=7_6_8 , UpperCAmelCase_ : Dict=1_2 , UpperCAmelCase_ : str=1_2 , UpperCAmelCase_ : Optional[Any]=3_0_7_2 , UpperCAmelCase_ : Dict="gelu" , UpperCAmelCase_ : Union[str, Any]=0.1 , UpperCAmelCase_ : Optional[int]=0.1 , UpperCAmelCase_ : int=0.1 , UpperCAmelCase_ : Union[str, Any]=0.0 , UpperCAmelCase_ : Union[str, Any]=0.1 , UpperCAmelCase_ : Dict=0.1 , UpperCAmelCase_ : Optional[Any]=0.02 , UpperCAmelCase_ : str=1e-5 , UpperCAmelCase_ : List[Any]="gelu" , UpperCAmelCase_ : Optional[Any]=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2) , UpperCAmelCase_ : Dict=(5, 2, 2, 2, 2, 2, 2) , UpperCAmelCase_ : int=(1_0, 3, 3, 3, 3, 2, 2) , UpperCAmelCase_ : List[Any]=False , UpperCAmelCase_ : List[Any]=1_6 , UpperCAmelCase_ : Optional[Any]=1_9 , UpperCAmelCase_ : int=5 , UpperCAmelCase_ : Any=0.05 , UpperCAmelCase_ : Dict=1_0 , UpperCAmelCase_ : Optional[Any]=2 , UpperCAmelCase_ : Optional[int]=0.0 , UpperCAmelCase_ : Tuple=1_0 , UpperCAmelCase_ : int=0 , UpperCAmelCase_ : Any="sum" , UpperCAmelCase_ : Optional[int]=False , UpperCAmelCase_ : Dict=False , UpperCAmelCase_ : Optional[int]=2_5_6 , UpperCAmelCase_ : Any=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0) , UpperCAmelCase_ : Optional[Any]=(5, 3, 3, 1, 1) , UpperCAmelCase_ : Optional[int]=(1, 2, 3, 1, 1) , UpperCAmelCase_ : int=5_1_2 , UpperCAmelCase_ : str=0 , UpperCAmelCase_ : Tuple=1 , UpperCAmelCase_ : Union[str, Any]=2 , UpperCAmelCase_ : List[str]=False , UpperCAmelCase_ : Any=3 , UpperCAmelCase_ : Dict=2 , UpperCAmelCase_ : int=3 , UpperCAmelCase_ : List[Any]=None , **UpperCAmelCase_ : int , ): """simple docstring""" super().__init__(**UpperCAmelCase_ , pad_token_id=UpperCAmelCase_ , bos_token_id=UpperCAmelCase_ , eos_token_id=UpperCAmelCase_) a : List[Any] = hidden_size a : Any = feat_extract_activation a : Any = list(UpperCAmelCase_) a : Optional[int] = list(UpperCAmelCase_) a : Dict = list(UpperCAmelCase_) a : Tuple = conv_bias a : str = num_conv_pos_embeddings a : Dict = num_conv_pos_embedding_groups a : Optional[Any] = conv_pos_kernel_size a : Any = len(self.conv_dim) a : Tuple = num_hidden_layers a : Any = intermediate_size a : Any = hidden_act a : Dict = num_attention_heads a : Dict = hidden_dropout a : Union[str, Any] = attention_dropout a : Dict = activation_dropout a : Optional[int] = feat_proj_dropout a : Tuple = final_dropout a : Union[str, Any] = layerdrop a : Tuple = layer_norm_eps a : Dict = initializer_range a : Tuple = vocab_size a : int = use_weighted_layer_sum if ( (len(self.conv_stride) != self.num_feat_extract_layers) or (len(self.conv_kernel) != self.num_feat_extract_layers) or (len(self.conv_dim) != self.num_feat_extract_layers) ): raise ValueError( 'Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==' ' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =' f""" {len(self.conv_dim)}`, `len(config.conv_stride) = {len(self.conv_stride)}`,""" f""" `len(config.conv_kernel) = {len(self.conv_kernel)}`.""") # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 a : List[str] = mask_time_prob a : int = mask_time_length a : Optional[int] = mask_time_min_masks a : Dict = mask_feature_prob a : List[str] = mask_feature_length a : str = mask_feature_min_masks # ctc loss a : str = ctc_loss_reduction a : Optional[Any] = ctc_zero_infinity # adapter a : List[str] = add_adapter a : Optional[Any] = adapter_kernel_size a : int = adapter_stride a : str = num_adapter_layers a : Optional[Any] = output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. a : str = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. a : List[Any] = list(UpperCAmelCase_) a : List[str] = list(UpperCAmelCase_) a : str = list(UpperCAmelCase_) a : Optional[Any] = xvector_output_dim @property def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" return math.prod(self.conv_stride)
345
0
'''simple docstring''' from unittest import TestCase from datasets import Sequence, Value from datasets.arrow_dataset import Dataset class UpperCamelCase ( __SCREAMING_SNAKE_CASE ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" return [ {"col_1": 3, "col_2": "a"}, {"col_1": 2, "col_2": "b"}, {"col_1": 1, "col_2": "c"}, {"col_1": 0, "col_2": "d"}, ] def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : Dict = {"col_1": [3, 2, 1, 0], "col_2": ["a", "b", "c", "d"]} return Dataset.from_dict(_SCREAMING_SNAKE_CASE) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : str = self._create_example_records() a : Optional[Any] = Dataset.from_list(_SCREAMING_SNAKE_CASE) self.assertListEqual(dset.column_names , ['col_1', 'col_2']) for i, r in enumerate(_SCREAMING_SNAKE_CASE): self.assertDictEqual(_SCREAMING_SNAKE_CASE , example_records[i]) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" a : Tuple = self._create_example_records() a : int = Dataset.from_list(_SCREAMING_SNAKE_CASE) a : List[Any] = Dataset.from_dict({k: [r[k] for r in example_records] for k in example_records[0]}) self.assertEqual(dset.info , dset_from_dict.info) def SCREAMING_SNAKE_CASE_ ( self : str): # checks what happens with missing columns """simple docstring""" a : Optional[int] = [{"col_1": 1}, {"col_2": "x"}] a : List[str] = Dataset.from_list(_SCREAMING_SNAKE_CASE) self.assertDictEqual(dset[0] , {'col_1': 1}) self.assertDictEqual(dset[1] , {'col_1': None}) # NB: first record is used for columns def SCREAMING_SNAKE_CASE_ ( self : int): # checks if the type can be inferred from the second record """simple docstring""" a : Optional[Any] = [{"col_1": []}, {"col_1": [1, 2]}] a : Any = Dataset.from_list(_SCREAMING_SNAKE_CASE) self.assertEqual(dset.info.features['col_1'] , Sequence(Value('int64'))) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" a : Optional[int] = Dataset.from_list([]) self.assertEqual(len(_SCREAMING_SNAKE_CASE) , 0) self.assertListEqual(dset.column_names , [])
364
'''simple docstring''' import logging from transformers.configuration_utils import PretrainedConfig UpperCamelCase : Optional[Any] = logging.getLogger(__name__) class UpperCamelCase ( a_ ): """simple docstring""" A : Tuple = "masked_bert" def __init__( self : Tuple , UpperCAmelCase_ : List[Any]=3_0_5_2_2 , UpperCAmelCase_ : str=7_6_8 , UpperCAmelCase_ : Optional[Any]=1_2 , UpperCAmelCase_ : Optional[int]=1_2 , UpperCAmelCase_ : Union[str, Any]=3_0_7_2 , UpperCAmelCase_ : Union[str, Any]="gelu" , UpperCAmelCase_ : Optional[Any]=0.1 , UpperCAmelCase_ : List[Any]=0.1 , UpperCAmelCase_ : Optional[int]=5_1_2 , UpperCAmelCase_ : List[str]=2 , UpperCAmelCase_ : str=0.02 , UpperCAmelCase_ : Optional[Any]=1e-12 , UpperCAmelCase_ : Dict=0 , UpperCAmelCase_ : Dict="topK" , UpperCAmelCase_ : str="constant" , UpperCAmelCase_ : Optional[Any]=0.0 , **UpperCAmelCase_ : Optional[Any] , ): """simple docstring""" super().__init__(pad_token_id=UpperCAmelCase_ , **UpperCAmelCase_) a : Union[str, Any] = vocab_size a : List[Any] = hidden_size a : List[str] = num_hidden_layers a : Any = num_attention_heads a : Optional[Any] = hidden_act a : str = intermediate_size a : Dict = hidden_dropout_prob a : Any = attention_probs_dropout_prob a : Any = max_position_embeddings a : Dict = type_vocab_size a : List[str] = initializer_range a : int = layer_norm_eps a : Dict = pruning_method a : List[str] = mask_init a : Union[str, Any] = mask_scale
345
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCamelCase : List[Any] = { """configuration_instructblip""": [ """INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP""", """InstructBlipConfig""", """InstructBlipQFormerConfig""", """InstructBlipVisionConfig""", ], """processing_instructblip""": ["""InstructBlipProcessor"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : str = [ """INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST""", """InstructBlipQFormerModel""", """InstructBlipPreTrainedModel""", """InstructBlipForConditionalGeneration""", """InstructBlipVisionModel""", ] if TYPE_CHECKING: from .configuration_instructblip import ( INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, InstructBlipConfig, InstructBlipQFormerConfig, InstructBlipVisionConfig, ) from .processing_instructblip import InstructBlipProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_instructblip import ( INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST, InstructBlipForConditionalGeneration, InstructBlipPreTrainedModel, InstructBlipQFormerModel, InstructBlipVisionModel, ) else: import sys UpperCamelCase : List[str] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
365
'''simple docstring''' import copy import random from transformers import CLIPTokenizer class UpperCamelCase ( a_ ): """simple docstring""" def __init__( self : Union[str, Any] , *UpperCAmelCase_ : Dict , **UpperCAmelCase_ : Any): """simple docstring""" super().__init__(*UpperCAmelCase_ , **UpperCAmelCase_) a : str = {} def SCREAMING_SNAKE_CASE_ ( self : int , UpperCAmelCase_ : Tuple , *UpperCAmelCase_ : Optional[int] , **UpperCAmelCase_ : int): """simple docstring""" a : Dict = super().add_tokens(UpperCAmelCase_ , *UpperCAmelCase_ , **UpperCAmelCase_) if num_added_tokens == 0: raise ValueError( f"""The tokenizer already contains the token {placeholder_token}. Please pass a different""" ' `placeholder_token` that is not already in the tokenizer.') def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : Optional[int] , *UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : List[str]=1 , **UpperCAmelCase_ : Optional[int]): """simple docstring""" a : Any = [] if num_vec_per_token == 1: self.try_adding_tokens(UpperCAmelCase_ , *UpperCAmelCase_ , **UpperCAmelCase_) output.append(UpperCAmelCase_) else: a : int = [] for i in range(UpperCAmelCase_): a : Union[str, Any] = placeholder_token + f"""_{i}""" self.try_adding_tokens(UpperCAmelCase_ , *UpperCAmelCase_ , **UpperCAmelCase_) output.append(UpperCAmelCase_) # handle cases where there is a new placeholder token that contains the current placeholder token but is larger for token in self.token_map: if token in placeholder_token: raise ValueError( f"""The tokenizer already has placeholder token {token} that can get confused with""" f""" {placeholder_token}keep placeholder tokens independent""") a : Any = output def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Optional[int]=False , UpperCAmelCase_ : str=1.0): """simple docstring""" if isinstance(UpperCAmelCase_ , UpperCAmelCase_): a : Any = [] for i in range(len(UpperCAmelCase_)): output.append(self.replace_placeholder_tokens_in_text(text[i] , vector_shuffle=UpperCAmelCase_)) return output for placeholder_token in self.token_map: if placeholder_token in text: a : List[Any] = self.token_map[placeholder_token] a : int = tokens[: 1 + int(len(UpperCAmelCase_) * prop_tokens_to_load)] if vector_shuffle: a : List[Any] = copy.copy(UpperCAmelCase_) random.shuffle(UpperCAmelCase_) a : List[str] = text.replace(UpperCAmelCase_ , ' '.join(UpperCAmelCase_)) return text def __call__( self : Optional[int] , UpperCAmelCase_ : Any , *UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Union[str, Any]=False , UpperCAmelCase_ : Optional[int]=1.0 , **UpperCAmelCase_ : str): """simple docstring""" return super().__call__( self.replace_placeholder_tokens_in_text( UpperCAmelCase_ , vector_shuffle=UpperCAmelCase_ , prop_tokens_to_load=UpperCAmelCase_) , *UpperCAmelCase_ , **UpperCAmelCase_ , ) def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : Optional[int] , *UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Any=False , UpperCAmelCase_ : Optional[Any]=1.0 , **UpperCAmelCase_ : Dict): """simple docstring""" return super().encode( self.replace_placeholder_tokens_in_text( UpperCAmelCase_ , vector_shuffle=UpperCAmelCase_ , prop_tokens_to_load=UpperCAmelCase_) , *UpperCAmelCase_ , **UpperCAmelCase_ , )
345
0
'''simple docstring''' def SCREAMING_SNAKE_CASE__ ( snake_case : Union[str, Any] , snake_case : Dict ) -> int: """simple docstring""" return int((input_a, input_a).count(1 ) != 0 ) def SCREAMING_SNAKE_CASE__ ( ) -> None: """simple docstring""" assert or_gate(0 , 0 ) == 0 assert or_gate(0 , 1 ) == 1 assert or_gate(1 , 0 ) == 1 assert or_gate(1 , 1 ) == 1 if __name__ == "__main__": print(or_gate(0, 1)) print(or_gate(1, 0)) print(or_gate(0, 0)) print(or_gate(1, 1))
366
'''simple docstring''' import argparse import json import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import AutoImageProcessor, SwinConfig, SwinForImageClassification def SCREAMING_SNAKE_CASE__ ( snake_case : str ) -> Optional[Any]: """simple docstring""" a : Union[str, Any] = SwinConfig() a : Optional[int] = swin_name.split('_' ) a : Union[str, Any] = name_split[1] a : Dict = int(name_split[4] ) a : Union[str, Any] = int(name_split[3][-1] ) if model_size == "tiny": a : Optional[Any] = 96 a : Any = (2, 2, 6, 2) a : List[str] = (3, 6, 12, 24) elif model_size == "small": a : int = 96 a : List[str] = (2, 2, 18, 2) a : int = (3, 6, 12, 24) elif model_size == "base": a : Tuple = 128 a : Optional[int] = (2, 2, 18, 2) a : List[Any] = (4, 8, 16, 32) else: a : Dict = 192 a : str = (2, 2, 18, 2) a : List[Any] = (6, 12, 24, 48) if "in22k" in swin_name: a : Any = 21_841 else: a : str = 1_000 a : str = 'huggingface/label-files' a : Optional[Any] = 'imagenet-1k-id2label.json' a : Dict = json.load(open(hf_hub_download(snake_case , snake_case , repo_type='dataset' ) , 'r' ) ) a : Tuple = {int(snake_case ): v for k, v in idalabel.items()} a : int = idalabel a : str = {v: k for k, v in idalabel.items()} a : Dict = img_size a : List[Any] = num_classes a : str = embed_dim a : Dict = depths a : Union[str, Any] = num_heads a : int = window_size return config def SCREAMING_SNAKE_CASE__ ( snake_case : List[Any] ) -> Optional[int]: """simple docstring""" if "patch_embed.proj" in name: a : int = name.replace('patch_embed.proj' , 'embeddings.patch_embeddings.projection' ) if "patch_embed.norm" in name: a : Tuple = name.replace('patch_embed.norm' , 'embeddings.norm' ) if "layers" in name: a : Optional[int] = 'encoder.' + name if "attn.proj" in name: a : List[Any] = name.replace('attn.proj' , 'attention.output.dense' ) if "attn" in name: a : Tuple = name.replace('attn' , 'attention.self' ) if "norm1" in name: a : Optional[int] = name.replace('norm1' , 'layernorm_before' ) if "norm2" in name: a : Dict = name.replace('norm2' , 'layernorm_after' ) if "mlp.fc1" in name: a : Union[str, Any] = name.replace('mlp.fc1' , 'intermediate.dense' ) if "mlp.fc2" in name: a : Any = name.replace('mlp.fc2' , 'output.dense' ) if name == "norm.weight": a : Union[str, Any] = 'layernorm.weight' if name == "norm.bias": a : List[str] = 'layernorm.bias' if "head" in name: a : Union[str, Any] = name.replace('head' , 'classifier' ) else: a : List[Any] = 'swin.' + name return name def SCREAMING_SNAKE_CASE__ ( snake_case : List[Any] , snake_case : Tuple ) -> List[str]: """simple docstring""" for key in orig_state_dict.copy().keys(): a : Any = orig_state_dict.pop(snake_case ) if "mask" in key: continue elif "qkv" in key: a : Optional[Any] = key.split('.' ) a : Dict = int(key_split[1] ) a : Optional[int] = int(key_split[3] ) a : Tuple = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: a : Optional[Any] = val[:dim, :] a : List[Any] = val[ dim : dim * 2, : ] a : List[Any] = val[-dim:, :] else: a : Dict = val[ :dim ] a : Union[str, Any] = val[ dim : dim * 2 ] a : Union[str, Any] = val[ -dim: ] else: a : Dict = val return orig_state_dict def SCREAMING_SNAKE_CASE__ ( snake_case : Optional[int] , snake_case : Dict ) -> List[str]: """simple docstring""" a : Any = timm.create_model(snake_case , pretrained=snake_case ) timm_model.eval() a : str = get_swin_config(snake_case ) a : Optional[int] = SwinForImageClassification(snake_case ) model.eval() a : Union[str, Any] = convert_state_dict(timm_model.state_dict() , snake_case ) model.load_state_dict(snake_case ) a : List[str] = 'http://images.cocodataset.org/val2017/000000039769.jpg' a : Optional[Any] = AutoImageProcessor.from_pretrained('microsoft/{}'.format(swin_name.replace('_' , '-' ) ) ) a : str = Image.open(requests.get(snake_case , stream=snake_case ).raw ) a : Union[str, Any] = image_processor(images=snake_case , return_tensors='pt' ) a : int = timm_model(inputs['pixel_values'] ) a : Optional[int] = model(**snake_case ).logits assert torch.allclose(snake_case , snake_case , atol=1E-3 ) print(F"""Saving model {swin_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(snake_case ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(snake_case ) if __name__ == "__main__": UpperCamelCase : int = argparse.ArgumentParser() # Required parameters parser.add_argument( """--swin_name""", default="""swin_tiny_patch4_window7_224""", type=str, help="""Name of the Swin timm model you'd like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) UpperCamelCase : Optional[Any] = parser.parse_args() convert_swin_checkpoint(args.swin_name, args.pytorch_dump_folder_path)
345
0
'''simple docstring''' import numpy as np import torch from torch.utils.data import Dataset from utils import logger class UpperCamelCase ( lowerCamelCase_ ): """simple docstring""" def __init__( self : Tuple , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[str]): """simple docstring""" a : Union[str, Any] = params a : int = np.array(_UpperCAmelCase) a : Optional[Any] = np.array([len(_UpperCAmelCase) for t in data]) self.check() self.remove_long_sequences() self.remove_empty_sequences() self.remove_unknown_sequences() self.check() self.print_statistics() def __getitem__( self : Optional[int] , UpperCAmelCase_ : List[str]): """simple docstring""" return (self.token_ids[index], self.lengths[index]) def __len__( self : Optional[int]): """simple docstring""" return len(self.lengths) def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" assert len(self.token_ids) == len(self.lengths) assert all(self.lengths[i] == len(self.token_ids[i]) for i in range(len(self.lengths))) def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : int = self.params.max_model_input_size a : Optional[int] = self.lengths > max_len logger.info(f"""Splitting {sum(_UpperCAmelCase)} too long sequences.""") def divide_chunks(UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Tuple): return [l[i : i + n] for i in range(0 , len(_UpperCAmelCase) , _UpperCAmelCase)] a : List[str] = [] a : Optional[int] = [] if self.params.mlm: a , a : Union[str, Any] = self.params.special_tok_ids['cls_token'], self.params.special_tok_ids['sep_token'] else: a , a : Dict = self.params.special_tok_ids['bos_token'], self.params.special_tok_ids['eos_token'] for seq_, len_ in zip(self.token_ids , self.lengths): assert (seq_[0] == cls_id) and (seq_[-1] == sep_id), seq_ if len_ <= max_len: new_tok_ids.append(seq_) new_lengths.append(len_) else: a : List[str] = [] for sub_s in divide_chunks(seq_ , max_len - 2): if sub_s[0] != cls_id: a : int = np.insert(_UpperCAmelCase , 0 , _UpperCAmelCase) if sub_s[-1] != sep_id: a : Optional[int] = np.insert(_UpperCAmelCase , len(_UpperCAmelCase) , _UpperCAmelCase) assert len(_UpperCAmelCase) <= max_len assert (sub_s[0] == cls_id) and (sub_s[-1] == sep_id), sub_s sub_seqs.append(_UpperCAmelCase) new_tok_ids.extend(_UpperCAmelCase) new_lengths.extend([len(_UpperCAmelCase) for l in sub_seqs]) a : List[str] = np.array(_UpperCAmelCase) a : Optional[int] = np.array(_UpperCAmelCase) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : Optional[int] = len(self) a : Dict = self.lengths > 1_1 a : Union[str, Any] = self.token_ids[indices] a : List[str] = self.lengths[indices] a : int = len(self) logger.info(f"""Remove {init_size - new_size} too short (<=11 tokens) sequences.""") def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" if "unk_token" not in self.params.special_tok_ids: return else: a : Union[str, Any] = self.params.special_tok_ids['unk_token'] a : Optional[Any] = len(self) a : str = np.array([np.count_nonzero(a == unk_token_id) for a in self.token_ids]) a : List[str] = (unk_occs / self.lengths) < 0.5 a : str = self.token_ids[indices] a : Union[str, Any] = self.lengths[indices] a : Dict = len(self) logger.info(f"""Remove {init_size - new_size} sequences with a high level of unknown tokens (50%).""") def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" if not self.params.is_master: return logger.info(f"""{len(self)} sequences""") # data_len = sum(self.lengths) # nb_unique_tokens = len(Counter(list(chain(*self.token_ids)))) # logger.info(f'{data_len} tokens ({nb_unique_tokens} unique)') # unk_idx = self.params.special_tok_ids['unk_token'] # nb_unknown = sum([(t==unk_idx).sum() for t in self.token_ids]) # logger.info(f'{nb_unknown} unknown tokens (covering {100*nb_unknown/data_len:.2f}% of the data)') def SCREAMING_SNAKE_CASE_ ( self : int , UpperCAmelCase_ : Optional[Any]): """simple docstring""" a : int = [t[0] for t in batch] a : List[str] = [t[1] for t in batch] assert len(_UpperCAmelCase) == len(_UpperCAmelCase) # Max for paddings a : Union[str, Any] = max(_UpperCAmelCase) # Pad token ids if self.params.mlm: a : Optional[Any] = self.params.special_tok_ids['pad_token'] else: a : Optional[int] = self.params.special_tok_ids['unk_token'] a : str = [list(t.astype(_UpperCAmelCase)) + [pad_idx] * (max_seq_len_ - len(_UpperCAmelCase)) for t in token_ids] assert len(tk_) == len(_UpperCAmelCase) assert all(len(_UpperCAmelCase) == max_seq_len_ for t in tk_) a : Optional[int] = torch.tensor(tk_) # (bs, max_seq_len_) a : Optional[Any] = torch.tensor(_UpperCAmelCase) # (bs) return tk_t, lg_t
367
'''simple docstring''' import baseaa import io import json import os from copy import deepcopy from ..optimizer import AcceleratedOptimizer from ..scheduler import AcceleratedScheduler class UpperCamelCase : """simple docstring""" def __init__( self : List[str] , UpperCAmelCase_ : Tuple): """simple docstring""" if isinstance(UpperCAmelCase_ , UpperCAmelCase_): # Don't modify user's data should they want to reuse it (e.g. in tests), because once we # modified it, it will not be accepted here again, since `auto` values would have been overridden a : Dict = deepcopy(UpperCAmelCase_) elif os.path.exists(UpperCAmelCase_): with io.open(UpperCAmelCase_ , 'r' , encoding='utf-8') as f: a : Union[str, Any] = json.load(UpperCAmelCase_) else: try: a : Union[str, Any] = baseaa.urlsafe_baadecode(UpperCAmelCase_).decode('utf-8') a : List[str] = json.loads(UpperCAmelCase_) except (UnicodeDecodeError, AttributeError, ValueError): raise ValueError( f"""Expected a string path to an existing deepspeed config, or a dictionary, or a base64 encoded string. Received: {config_file_or_dict}""") a : Optional[int] = config self.set_stage_and_offload() def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : str = self.get_value('zero_optimization.stage' , -1) # offload a : Any = False if self.is_zeroa() or self.is_zeroa(): a : Tuple = set(['cpu', 'nvme']) a : int = set( [ self.get_value('zero_optimization.offload_optimizer.device'), self.get_value('zero_optimization.offload_param.device'), ]) if len(offload_devices & offload_devices_valid) > 0: a : List[str] = True def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : Dict): """simple docstring""" a : List[str] = self.config # find the config node of interest if it exists a : int = ds_key_long.split('.') a : Union[str, Any] = nodes.pop() for node in nodes: a : Union[str, Any] = config.get(UpperCAmelCase_) if config is None: return None, ds_key return config, ds_key def SCREAMING_SNAKE_CASE_ ( self : Any , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : int=None): """simple docstring""" a , a : int = self.find_config_node(UpperCAmelCase_) if config is None: return default return config.get(UpperCAmelCase_ , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : int , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Any=False): """simple docstring""" a : Any = self.config # find the config node of interest if it exists a : Optional[Any] = ds_key_long.split('.') for node in nodes: a : List[str] = config a : int = config.get(UpperCAmelCase_) if config is None: if must_exist: raise ValueError(f"""Can't find {ds_key_long} entry in the config: {self.config}""") else: return # if found remove it if parent_config is not None: parent_config.pop(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : str): """simple docstring""" a : List[str] = self.get_value(UpperCAmelCase_) return False if value is None else bool(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : Union[str, Any]): """simple docstring""" a : List[Any] = self.get_value(UpperCAmelCase_) return False if value is None else not bool(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" return self._stage == 2 def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" return self._stage == 3 def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" return self._offload class UpperCamelCase : """simple docstring""" def __init__( self : str , UpperCAmelCase_ : int): """simple docstring""" a : Union[str, Any] = engine def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : Any , **UpperCAmelCase_ : List[Any]): """simple docstring""" self.engine.backward(UpperCAmelCase_ , **UpperCAmelCase_) # Deepspeed's `engine.step` performs the following operations: # - gradient accumulation check # - gradient clipping # - optimizer step # - zero grad # - checking overflow # - lr_scheduler step (only if engine.lr_scheduler is not None) self.engine.step() # and this plugin overrides the above calls with no-ops when Accelerate runs under # Deepspeed, but allows normal functionality for non-Deepspeed cases thus enabling a simple # training loop that works transparently under many training regimes. class UpperCamelCase ( a_ ): """simple docstring""" def __init__( self : Union[str, Any] , UpperCAmelCase_ : Any): """simple docstring""" super().__init__(UpperCAmelCase_ , device_placement=UpperCAmelCase_ , scaler=UpperCAmelCase_) a : List[str] = hasattr(self.optimizer , 'overflow') def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : Dict=None): """simple docstring""" pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed @property def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" if self.__has_overflow__: return self.optimizer.overflow return False class UpperCamelCase ( a_ ): """simple docstring""" def __init__( self : str , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Optional[Any]): """simple docstring""" super().__init__(UpperCAmelCase_ , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed class UpperCamelCase : """simple docstring""" def __init__( self : Union[str, Any] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Optional[int]=0.0_01 , UpperCAmelCase_ : List[Any]=0 , **UpperCAmelCase_ : Union[str, Any]): """simple docstring""" a : int = params a : str = lr a : Tuple = weight_decay a : Dict = kwargs class UpperCamelCase : """simple docstring""" def __init__( self : int , UpperCAmelCase_ : int , UpperCAmelCase_ : Any=None , UpperCAmelCase_ : Union[str, Any]=0 , **UpperCAmelCase_ : List[Any]): """simple docstring""" a : str = optimizer a : Tuple = total_num_steps a : Optional[Any] = warmup_num_steps a : List[str] = kwargs
345
0
'''simple docstring''' from __future__ import annotations from PIL import Image # Define glider example UpperCamelCase : Optional[Any] = [ [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], ] # Define blinker example UpperCamelCase : List[Any] = [[0, 1, 0], [0, 1, 0], [0, 1, 0]] def SCREAMING_SNAKE_CASE__ ( snake_case : list[list[int]] ) -> list[list[int]]: """simple docstring""" a : List[str] = [] for i in range(len(snake_case ) ): a : Optional[int] = [] for j in range(len(cells[i] ) ): # Get the number of live neighbours a : Tuple = 0 if i > 0 and j > 0: neighbour_count += cells[i - 1][j - 1] if i > 0: neighbour_count += cells[i - 1][j] if i > 0 and j < len(cells[i] ) - 1: neighbour_count += cells[i - 1][j + 1] if j > 0: neighbour_count += cells[i][j - 1] if j < len(cells[i] ) - 1: neighbour_count += cells[i][j + 1] if i < len(snake_case ) - 1 and j > 0: neighbour_count += cells[i + 1][j - 1] if i < len(snake_case ) - 1: neighbour_count += cells[i + 1][j] if i < len(snake_case ) - 1 and j < len(cells[i] ) - 1: neighbour_count += cells[i + 1][j + 1] # Rules of the game of life (excerpt from Wikipedia): # 1. Any live cell with two or three live neighbours survives. # 2. Any dead cell with three live neighbours becomes a live cell. # 3. All other live cells die in the next generation. # Similarly, all other dead cells stay dead. a : str = cells[i][j] == 1 if ( (alive and 2 <= neighbour_count <= 3) or not alive and neighbour_count == 3 ): next_generation_row.append(1 ) else: next_generation_row.append(0 ) next_generation.append(snake_case ) return next_generation def SCREAMING_SNAKE_CASE__ ( snake_case : list[list[int]] , snake_case : int ) -> list[Image.Image]: """simple docstring""" a : int = [] for _ in range(snake_case ): # Create output image a : int = Image.new('RGB' , (len(cells[0] ), len(snake_case )) ) a : Tuple = img.load() # Save cells to image for x in range(len(snake_case ) ): for y in range(len(cells[0] ) ): a : Union[str, Any] = 255 - cells[y][x] * 255 a : int = (colour, colour, colour) # Save image images.append(snake_case ) a : Any = new_generation(snake_case ) return images if __name__ == "__main__": UpperCamelCase : Any = generate_images(GLIDER, 16) images[0].save("""out.gif""", save_all=True, append_images=images[1:])
368
'''simple docstring''' import os import time import warnings from dataclasses import dataclass, field from enum import Enum from typing import List, Optional, Union import torch from filelock import FileLock from torch.utils.data import Dataset from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import logging from ..processors.glue import glue_convert_examples_to_features, glue_output_modes, glue_processors from ..processors.utils import InputFeatures UpperCamelCase : List[str] = logging.get_logger(__name__) @dataclass class UpperCamelCase : """simple docstring""" A : str = field(metadata={"help": "The name of the task to train on: " + ", ".join(glue_processors.keys() )} ) A : str = field( metadata={"help": "The input data dir. Should contain the .tsv files (or other data files) for the task."} ) A : int = field( default=128 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) A : bool = field( default=a_ , metadata={"help": "Overwrite the cached training and evaluation sets"} ) def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" a : Union[str, Any] = self.task_name.lower() class UpperCamelCase ( a_ ): """simple docstring""" A : int = "train" A : Tuple = "dev" A : List[Any] = "test" class UpperCamelCase ( a_ ): """simple docstring""" A : GlueDataTrainingArguments A : str A : List[InputFeatures] def __init__( self : Tuple , UpperCAmelCase_ : GlueDataTrainingArguments , UpperCAmelCase_ : PreTrainedTokenizerBase , UpperCAmelCase_ : Optional[int] = None , UpperCAmelCase_ : Union[str, Split] = Split.train , UpperCAmelCase_ : Optional[str] = None , ): """simple docstring""" warnings.warn( 'This dataset will be removed from the library soon, preprocessing should be handled with the 🤗 Datasets ' 'library. You can have a look at this example script for pointers: ' 'https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py' , UpperCAmelCase_ , ) a : Dict = args a : int = glue_processors[args.task_name]() a : int = glue_output_modes[args.task_name] if isinstance(UpperCAmelCase_ , UpperCAmelCase_): try: a : str = Split[mode] except KeyError: raise KeyError('mode is not a valid split name') # Load data features from cache or dataset file a : List[str] = os.path.join( cache_dir if cache_dir is not None else args.data_dir , f"""cached_{mode.value}_{tokenizer.__class__.__name__}_{args.max_seq_length}_{args.task_name}""" , ) a : Tuple = self.processor.get_labels() if args.task_name in ["mnli", "mnli-mm"] and tokenizer.__class__.__name__ in ( "RobertaTokenizer", "RobertaTokenizerFast", "XLMRobertaTokenizer", "BartTokenizer", "BartTokenizerFast", ): # HACK(label indices are swapped in RoBERTa pretrained model) a , a : str = label_list[2], label_list[1] a : int = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. a : Union[str, Any] = cached_features_file + '.lock' with FileLock(UpperCAmelCase_): if os.path.exists(UpperCAmelCase_) and not args.overwrite_cache: a : Optional[Any] = time.time() a : Optional[Any] = torch.load(UpperCAmelCase_) logger.info( f"""Loading features from cached file {cached_features_file} [took %.3f s]""" , time.time() - start) else: logger.info(f"""Creating features from dataset file at {args.data_dir}""") if mode == Split.dev: a : List[Any] = self.processor.get_dev_examples(args.data_dir) elif mode == Split.test: a : Optional[Any] = self.processor.get_test_examples(args.data_dir) else: a : List[str] = self.processor.get_train_examples(args.data_dir) if limit_length is not None: a : Dict = examples[:limit_length] a : List[Any] = glue_convert_examples_to_features( UpperCAmelCase_ , UpperCAmelCase_ , max_length=args.max_seq_length , label_list=UpperCAmelCase_ , output_mode=self.output_mode , ) a : Dict = time.time() torch.save(self.features , UpperCAmelCase_) # ^ This seems to take a lot of time so I want to investigate why and how we can improve. logger.info( f"""Saving features into cached file {cached_features_file} [took {time.time() - start:.3f} s]""") def __len__( self : Tuple): """simple docstring""" return len(self.features) def __getitem__( self : Optional[int] , UpperCAmelCase_ : List[str]): """simple docstring""" return self.features[i] def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" return self.label_list
345
0
'''simple docstring''' import argparse import json from collections import OrderedDict from functools import partial from pathlib import Path import timm import torch from huggingface_hub import hf_hub_download from transformers import LevitConfig, LevitForImageClassificationWithTeacher, LevitImageProcessor from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase : Dict = logging.get_logger() def SCREAMING_SNAKE_CASE__ ( snake_case : Dict , snake_case : int , snake_case : Tuple , snake_case : Any , snake_case : Any = True ) -> Dict: """simple docstring""" print(F"""Converting {name}...""" ) with torch.no_grad(): if hidden_sizes == 128: if name[-1] == "S": a : Union[str, Any] = timm.create_model('levit_128s' , pretrained=a__ ) else: a : List[str] = timm.create_model('levit_128' , pretrained=a__ ) if hidden_sizes == 192: a : int = timm.create_model('levit_192' , pretrained=a__ ) if hidden_sizes == 256: a : List[Any] = timm.create_model('levit_256' , pretrained=a__ ) if hidden_sizes == 384: a : Optional[Any] = timm.create_model('levit_384' , pretrained=a__ ) from_model.eval() a : Union[str, Any] = LevitForImageClassificationWithTeacher(a__ ).eval() a : Optional[Any] = OrderedDict() a : str = from_model.state_dict() a : List[str] = list(from_model.state_dict().keys() ) a : Tuple = list(our_model.state_dict().keys() ) print(len(a__ ) , len(a__ ) ) for i in range(len(a__ ) ): a : List[Any] = weights[og_keys[i]] our_model.load_state_dict(a__ ) a : Union[str, Any] = torch.randn((2, 3, 224, 224) ) a : Any = from_model(a__ ) a : Optional[int] = our_model(a__ ).logits assert torch.allclose(a__ , a__ ), "The model logits don't match the original one." a : Optional[Any] = name print(a__ ) if push_to_hub: our_model.save_pretrained(save_directory / checkpoint_name ) a : Optional[int] = LevitImageProcessor() image_processor.save_pretrained(save_directory / checkpoint_name ) print(F"""Pushed {checkpoint_name}""" ) def SCREAMING_SNAKE_CASE__ ( snake_case : Optional[Any] , snake_case : Optional[Any] = None , snake_case : int = True ) -> Optional[int]: """simple docstring""" a : int = 'imagenet-1k-id2label.json' a : Any = 1_000 a : int = (1, num_labels) a : str = 'huggingface/label-files' a : Any = num_labels a : List[str] = json.load(open(hf_hub_download(a__ , a__ , repo_type='dataset' ) , 'r' ) ) a : Optional[int] = {int(a__ ): v for k, v in idalabel.items()} a : Any = idalabel a : Union[str, Any] = {v: k for k, v in idalabel.items()} a : Tuple = partial(a__ , num_labels=a__ , idalabel=a__ , labelaid=a__ ) a : Union[str, Any] = { 'levit-128S': 128, 'levit-128': 128, 'levit-192': 192, 'levit-256': 256, 'levit-384': 384, } a : int = { 'levit-128S': ImageNetPreTrainedConfig( hidden_sizes=[128, 256, 384] , num_attention_heads=[4, 6, 8] , depths=[2, 3, 4] , key_dim=[16, 16, 16] , drop_path_rate=0 , ), 'levit-128': ImageNetPreTrainedConfig( hidden_sizes=[128, 256, 384] , num_attention_heads=[4, 8, 12] , depths=[4, 4, 4] , key_dim=[16, 16, 16] , drop_path_rate=0 , ), 'levit-192': ImageNetPreTrainedConfig( hidden_sizes=[192, 288, 384] , num_attention_heads=[3, 5, 6] , depths=[4, 4, 4] , key_dim=[32, 32, 32] , drop_path_rate=0 , ), 'levit-256': ImageNetPreTrainedConfig( hidden_sizes=[256, 384, 512] , num_attention_heads=[4, 6, 8] , depths=[4, 4, 4] , key_dim=[32, 32, 32] , drop_path_rate=0 , ), 'levit-384': ImageNetPreTrainedConfig( hidden_sizes=[384, 512, 768] , num_attention_heads=[6, 9, 12] , depths=[4, 4, 4] , key_dim=[32, 32, 32] , drop_path_rate=0.1 , ), } if model_name: convert_weight_and_push( names_to_hidden_sizes[model_name] , a__ , names_to_config[model_name] , a__ , a__ ) else: for model_name, config in names_to_config.items(): convert_weight_and_push(names_to_hidden_sizes[model_name] , a__ , a__ , a__ , a__ ) return config, expected_shape if __name__ == "__main__": UpperCamelCase : int = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default=None, type=str, help="""The name of the model you wish to convert, it must be one of the supported Levit* architecture,""", ) parser.add_argument( """--pytorch_dump_folder_path""", default="""levit-dump-folder/""", type=Path, required=False, help="""Path to the output PyTorch model directory.""", ) parser.add_argument("""--push_to_hub""", action="""store_true""", help="""Push model and image processor to the hub""") parser.add_argument( """--no-push_to_hub""", dest="""push_to_hub""", action="""store_false""", help="""Do not push model and image processor to the hub""", ) UpperCamelCase : Optional[Any] = parser.parse_args() UpperCamelCase : Tuple = args.pytorch_dump_folder_path pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True) convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)
369
'''simple docstring''' from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, logging if is_torch_available(): import torch UpperCamelCase : Dict = logging.get_logger(__name__) class UpperCamelCase ( a_ ): """simple docstring""" A : Any = ["pixel_values"] def __init__( self : str , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Optional[Dict[str, int]] = None , UpperCAmelCase_ : PILImageResampling = PILImageResampling.BILINEAR , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Dict[str, int] = None , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Union[int, float] = 1 / 2_5_5 , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Optional[Union[float, List[float]]] = None , UpperCAmelCase_ : Optional[Union[float, List[float]]] = None , **UpperCAmelCase_ : List[str] , ): """simple docstring""" super().__init__(**UpperCAmelCase_) a : str = size if size is not None else {'shortest_edge': 2_5_6} a : Dict = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) a : str = crop_size if crop_size is not None else {'height': 2_2_4, 'width': 2_2_4} a : int = get_size_dict(UpperCAmelCase_ , param_name='crop_size') a : Any = do_resize a : List[str] = size a : Union[str, Any] = resample a : int = do_center_crop a : Optional[int] = crop_size a : Tuple = do_rescale a : int = rescale_factor a : Optional[Any] = do_normalize a : Dict = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN a : Any = image_std if image_std is not None else IMAGENET_STANDARD_STD def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Dict[str, int] , UpperCAmelCase_ : PILImageResampling = PILImageResampling.BICUBIC , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Optional[int] , ): """simple docstring""" a : Optional[int] = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) if "shortest_edge" not in size: raise ValueError(f"""The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}""") a : Union[str, Any] = get_resize_output_image_size(UpperCAmelCase_ , size=size['shortest_edge'] , default_to_square=UpperCAmelCase_) return resize(UpperCAmelCase_ , size=UpperCAmelCase_ , resample=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Dict[str, int] , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Optional[int] , ): """simple docstring""" a : List[str] = get_size_dict(UpperCAmelCase_) if "height" not in size or "width" not in size: raise ValueError(f"""The `size` parameter must contain the keys `height` and `width`. Got {size.keys()}""") return center_crop(UpperCAmelCase_ , size=(size['height'], size['width']) , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : float , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Optional[Any]): """simple docstring""" return rescale(UpperCAmelCase_ , scale=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Union[float, List[float]] , UpperCAmelCase_ : Union[float, List[float]] , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Optional[int] , ): """simple docstring""" return normalize(UpperCAmelCase_ , mean=UpperCAmelCase_ , std=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : ImageInput , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Dict[str, int] = None , UpperCAmelCase_ : PILImageResampling = None , UpperCAmelCase_ : bool = None , UpperCAmelCase_ : Dict[str, int] = None , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[float] = None , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[Union[float, List[float]]] = None , UpperCAmelCase_ : Optional[Union[float, List[float]]] = None , UpperCAmelCase_ : Optional[Union[str, TensorType]] = None , UpperCAmelCase_ : Union[str, ChannelDimension] = ChannelDimension.FIRST , **UpperCAmelCase_ : List[str] , ): """simple docstring""" a : int = do_resize if do_resize is not None else self.do_resize a : int = size if size is not None else self.size a : Union[str, Any] = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) a : str = resample if resample is not None else self.resample a : Optional[Any] = do_center_crop if do_center_crop is not None else self.do_center_crop a : Union[str, Any] = crop_size if crop_size is not None else self.crop_size a : Dict = get_size_dict(UpperCAmelCase_ , param_name='crop_size') a : str = do_rescale if do_rescale is not None else self.do_rescale a : int = rescale_factor if rescale_factor is not None else self.rescale_factor a : str = do_normalize if do_normalize is not None else self.do_normalize a : List[str] = image_mean if image_mean is not None else self.image_mean a : Optional[int] = image_std if image_std is not None else self.image_std a : Dict = make_list_of_images(UpperCAmelCase_) if not valid_images(UpperCAmelCase_): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.') if do_resize and size is None: raise ValueError('Size must be specified if do_resize is True.') if do_center_crop and crop_size is None: raise ValueError('Crop size must be specified if do_center_crop is True.') if do_rescale and rescale_factor is None: raise ValueError('Rescale factor must be specified if do_rescale is True.') if do_normalize and (image_mean is None or image_std is None): raise ValueError('Image mean and std must be specified if do_normalize is True.') # All transformations expect numpy arrays. a : List[Any] = [to_numpy_array(UpperCAmelCase_) for image in images] if do_resize: a : Dict = [self.resize(image=UpperCAmelCase_ , size=UpperCAmelCase_ , resample=UpperCAmelCase_) for image in images] if do_center_crop: a : Any = [self.center_crop(image=UpperCAmelCase_ , size=UpperCAmelCase_) for image in images] if do_rescale: a : Optional[int] = [self.rescale(image=UpperCAmelCase_ , scale=UpperCAmelCase_) for image in images] if do_normalize: a : Dict = [self.normalize(image=UpperCAmelCase_ , mean=UpperCAmelCase_ , std=UpperCAmelCase_) for image in images] a : List[Any] = [to_channel_dimension_format(UpperCAmelCase_ , UpperCAmelCase_) for image in images] a : List[str] = {'pixel_values': images} return BatchFeature(data=UpperCAmelCase_ , tensor_type=UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : List[Tuple] = None): """simple docstring""" a : Dict = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(UpperCAmelCase_) != len(UpperCAmelCase_): raise ValueError( 'Make sure that you pass in as many target sizes as the batch dimension of the logits') if is_torch_tensor(UpperCAmelCase_): a : Optional[Any] = target_sizes.numpy() a : List[str] = [] for idx in range(len(UpperCAmelCase_)): a : Optional[Any] = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0) , size=target_sizes[idx] , mode='bilinear' , align_corners=UpperCAmelCase_) a : Union[str, Any] = resized_logits[0].argmax(dim=0) semantic_segmentation.append(UpperCAmelCase_) else: a : Optional[int] = logits.argmax(dim=1) a : List[str] = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0])] return semantic_segmentation
345
0
'''simple docstring''' import argparse from collections import OrderedDict from pathlib import Path import torch from huggingface_hub import hf_hub_download from PIL import Image from torchvision.transforms import functional as F from transformers import DetrImageProcessor, TableTransformerConfig, TableTransformerForObjectDetection from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase : List[str] = logging.get_logger(__name__) # here we list all keys to be renamed (original name on the left, our name on the right) UpperCamelCase : Tuple = [] for i in range(6): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append( (f'''transformer.encoder.layers.{i}.self_attn.out_proj.weight''', f'''encoder.layers.{i}.self_attn.out_proj.weight''') ) rename_keys.append( (f'''transformer.encoder.layers.{i}.self_attn.out_proj.bias''', f'''encoder.layers.{i}.self_attn.out_proj.bias''') ) rename_keys.append((f'''transformer.encoder.layers.{i}.linear1.weight''', f'''encoder.layers.{i}.fc1.weight''')) rename_keys.append((f'''transformer.encoder.layers.{i}.linear1.bias''', f'''encoder.layers.{i}.fc1.bias''')) rename_keys.append((f'''transformer.encoder.layers.{i}.linear2.weight''', f'''encoder.layers.{i}.fc2.weight''')) rename_keys.append((f'''transformer.encoder.layers.{i}.linear2.bias''', f'''encoder.layers.{i}.fc2.bias''')) rename_keys.append( (f'''transformer.encoder.layers.{i}.norm1.weight''', f'''encoder.layers.{i}.self_attn_layer_norm.weight''') ) rename_keys.append((f'''transformer.encoder.layers.{i}.norm1.bias''', f'''encoder.layers.{i}.self_attn_layer_norm.bias''')) rename_keys.append((f'''transformer.encoder.layers.{i}.norm2.weight''', f'''encoder.layers.{i}.final_layer_norm.weight''')) rename_keys.append((f'''transformer.encoder.layers.{i}.norm2.bias''', f'''encoder.layers.{i}.final_layer_norm.bias''')) # decoder layers: 2 times output projection, 2 feedforward neural networks and 3 layernorms rename_keys.append( (f'''transformer.decoder.layers.{i}.self_attn.out_proj.weight''', f'''decoder.layers.{i}.self_attn.out_proj.weight''') ) rename_keys.append( (f'''transformer.decoder.layers.{i}.self_attn.out_proj.bias''', f'''decoder.layers.{i}.self_attn.out_proj.bias''') ) rename_keys.append( ( f'''transformer.decoder.layers.{i}.multihead_attn.out_proj.weight''', f'''decoder.layers.{i}.encoder_attn.out_proj.weight''', ) ) rename_keys.append( ( f'''transformer.decoder.layers.{i}.multihead_attn.out_proj.bias''', f'''decoder.layers.{i}.encoder_attn.out_proj.bias''', ) ) rename_keys.append((f'''transformer.decoder.layers.{i}.linear1.weight''', f'''decoder.layers.{i}.fc1.weight''')) rename_keys.append((f'''transformer.decoder.layers.{i}.linear1.bias''', f'''decoder.layers.{i}.fc1.bias''')) rename_keys.append((f'''transformer.decoder.layers.{i}.linear2.weight''', f'''decoder.layers.{i}.fc2.weight''')) rename_keys.append((f'''transformer.decoder.layers.{i}.linear2.bias''', f'''decoder.layers.{i}.fc2.bias''')) rename_keys.append( (f'''transformer.decoder.layers.{i}.norm1.weight''', f'''decoder.layers.{i}.self_attn_layer_norm.weight''') ) rename_keys.append((f'''transformer.decoder.layers.{i}.norm1.bias''', f'''decoder.layers.{i}.self_attn_layer_norm.bias''')) rename_keys.append( (f'''transformer.decoder.layers.{i}.norm2.weight''', f'''decoder.layers.{i}.encoder_attn_layer_norm.weight''') ) rename_keys.append( (f'''transformer.decoder.layers.{i}.norm2.bias''', f'''decoder.layers.{i}.encoder_attn_layer_norm.bias''') ) rename_keys.append((f'''transformer.decoder.layers.{i}.norm3.weight''', f'''decoder.layers.{i}.final_layer_norm.weight''')) rename_keys.append((f'''transformer.decoder.layers.{i}.norm3.bias''', f'''decoder.layers.{i}.final_layer_norm.bias''')) # convolutional projection + query embeddings + layernorm of encoder + layernorm of decoder + class and bounding box heads rename_keys.extend( [ ("""input_proj.weight""", """input_projection.weight"""), ("""input_proj.bias""", """input_projection.bias"""), ("""query_embed.weight""", """query_position_embeddings.weight"""), ("""transformer.encoder.norm.weight""", """encoder.layernorm.weight"""), ("""transformer.encoder.norm.bias""", """encoder.layernorm.bias"""), ("""transformer.decoder.norm.weight""", """decoder.layernorm.weight"""), ("""transformer.decoder.norm.bias""", """decoder.layernorm.bias"""), ("""class_embed.weight""", """class_labels_classifier.weight"""), ("""class_embed.bias""", """class_labels_classifier.bias"""), ("""bbox_embed.layers.0.weight""", """bbox_predictor.layers.0.weight"""), ("""bbox_embed.layers.0.bias""", """bbox_predictor.layers.0.bias"""), ("""bbox_embed.layers.1.weight""", """bbox_predictor.layers.1.weight"""), ("""bbox_embed.layers.1.bias""", """bbox_predictor.layers.1.bias"""), ("""bbox_embed.layers.2.weight""", """bbox_predictor.layers.2.weight"""), ("""bbox_embed.layers.2.bias""", """bbox_predictor.layers.2.bias"""), ] ) def SCREAMING_SNAKE_CASE__ ( snake_case : int , snake_case : Any , snake_case : Optional[Any] ) -> Optional[Any]: """simple docstring""" a : Tuple = state_dict.pop(_lowerCamelCase ) a : str = val def SCREAMING_SNAKE_CASE__ ( snake_case : Tuple ) -> List[Any]: """simple docstring""" a : str = OrderedDict() for key, value in state_dict.items(): if "backbone.0.body" in key: a : List[str] = key.replace('backbone.0.body' , 'backbone.conv_encoder.model' ) a : Optional[Any] = value else: a : Optional[int] = value return new_state_dict def SCREAMING_SNAKE_CASE__ ( snake_case : List[Any] ) -> Dict: """simple docstring""" a : Optional[int] = "" # first: transformer encoder for i in range(6 ): # read in weights + bias of input projection layer (in PyTorch's MultiHeadAttention, this is a single matrix + bias) a : Any = state_dict.pop(F"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_weight""" ) a : List[str] = state_dict.pop(F"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict a : Dict = in_proj_weight[:256, :] a : str = in_proj_bias[:256] a : str = in_proj_weight[256:512, :] a : List[Any] = in_proj_bias[256:512] a : List[str] = in_proj_weight[-256:, :] a : Dict = in_proj_bias[-256:] # next: transformer decoder (which is a bit more complex because it also includes cross-attention) for i in range(6 ): # read in weights + bias of input projection layer of self-attention a : Union[str, Any] = state_dict.pop(F"""{prefix}transformer.decoder.layers.{i}.self_attn.in_proj_weight""" ) a : Optional[int] = state_dict.pop(F"""{prefix}transformer.decoder.layers.{i}.self_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict a : Any = in_proj_weight[:256, :] a : int = in_proj_bias[:256] a : Union[str, Any] = in_proj_weight[256:512, :] a : Union[str, Any] = in_proj_bias[256:512] a : Optional[int] = in_proj_weight[-256:, :] a : str = in_proj_bias[-256:] # read in weights + bias of input projection layer of cross-attention a : Optional[Any] = state_dict.pop( F"""{prefix}transformer.decoder.layers.{i}.multihead_attn.in_proj_weight""" ) a : Tuple = state_dict.pop(F"""{prefix}transformer.decoder.layers.{i}.multihead_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) of cross-attention to the state dict a : Tuple = in_proj_weight_cross_attn[:256, :] a : Union[str, Any] = in_proj_bias_cross_attn[:256] a : List[str] = in_proj_weight_cross_attn[256:512, :] a : Union[str, Any] = in_proj_bias_cross_attn[256:512] a : int = in_proj_weight_cross_attn[-256:, :] a : Dict = in_proj_bias_cross_attn[-256:] def SCREAMING_SNAKE_CASE__ ( snake_case : str , snake_case : List[Any] ) -> Optional[int]: """simple docstring""" a : Union[str, Any] = image.size a : Tuple = max(_lowerCamelCase , _lowerCamelCase ) a : List[str] = 800 if "detection" in checkpoint_url else 1_000 a : Tuple = target_max_size / current_max_size a : List[str] = image.resize((int(round(scale * width ) ), int(round(scale * height ) )) ) return resized_image def SCREAMING_SNAKE_CASE__ ( snake_case : Tuple ) -> Optional[int]: """simple docstring""" a : Optional[int] = F.to_tensor(_lowerCamelCase ) a : Optional[int] = F.normalize(_lowerCamelCase , mean=[0.4_85, 0.4_56, 0.4_06] , std=[0.2_29, 0.2_24, 0.2_25] ) return image @torch.no_grad() def SCREAMING_SNAKE_CASE__ ( snake_case : Tuple , snake_case : Tuple , snake_case : Dict ) -> str: """simple docstring""" logger.info('Converting model...' ) # load original state dict a : Any = torch.hub.load_state_dict_from_url(_lowerCamelCase , map_location='cpu' ) # rename keys for src, dest in rename_keys: rename_key(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) a : Optional[Any] = rename_backbone_keys(_lowerCamelCase ) # query, key and value matrices need special treatment read_in_q_k_v(_lowerCamelCase ) # important: we need to prepend a prefix to each of the base model keys as the head models use different attributes for them a : Optional[Any] = "model." for key in state_dict.copy().keys(): if not key.startswith('class_labels_classifier' ) and not key.startswith('bbox_predictor' ): a : List[Any] = state_dict.pop(_lowerCamelCase ) a : int = val # create HuggingFace model and load state dict a : int = TableTransformerConfig( backbone='resnet18' , mask_loss_coefficient=1 , dice_loss_coefficient=1 , ce_loss_coefficient=1 , bbox_loss_coefficient=5 , giou_loss_coefficient=2 , eos_coefficient=0.4 , class_cost=1 , bbox_cost=5 , giou_cost=2 , ) if "detection" in checkpoint_url: a : Dict = 15 a : Optional[int] = 2 a : Union[str, Any] = {0: "table", 1: "table rotated"} a : Union[str, Any] = idalabel a : str = {v: k for k, v in idalabel.items()} else: a : Optional[int] = 125 a : Dict = 6 a : str = { 0: "table", 1: "table column", 2: "table row", 3: "table column header", 4: "table projected row header", 5: "table spanning cell", } a : Dict = idalabel a : Optional[Any] = {v: k for k, v in idalabel.items()} a : List[str] = DetrImageProcessor( format='coco_detection' , max_size=800 if 'detection' in checkpoint_url else 1_000 ) a : Optional[Any] = TableTransformerForObjectDetection(_lowerCamelCase ) model.load_state_dict(_lowerCamelCase ) model.eval() # verify our conversion a : List[str] = "example_pdf.png" if "detection" in checkpoint_url else "example_table.png" a : Union[str, Any] = hf_hub_download(repo_id='nielsr/example-pdf' , repo_type='dataset' , filename=_lowerCamelCase ) a : Tuple = Image.open(_lowerCamelCase ).convert('RGB' ) a : List[Any] = normalize(resize(_lowerCamelCase , _lowerCamelCase ) ).unsqueeze(0 ) a : List[Any] = model(_lowerCamelCase ) if "detection" in checkpoint_url: a : str = (1, 15, 3) a : List[Any] = torch.tensor( [[-6.78_97, -16.99_85, 6.79_37], [-8.01_86, -22.21_92, 6.96_77], [-7.31_17, -21.07_08, 7.40_55]] ) a : Optional[Any] = torch.tensor([[0.48_67, 0.17_67, 0.67_32], [0.67_18, 0.44_79, 0.38_30], [0.47_16, 0.17_60, 0.63_64]] ) else: a : Optional[Any] = (1, 125, 7) a : Optional[Any] = torch.tensor( [[-18.14_30, -8.32_14, 4.82_74], [-18.46_85, -7.13_61, -4.26_67], [-26.36_93, -9.34_29, -4.99_62]] ) a : Optional[Any] = torch.tensor([[0.49_83, 0.55_95, 0.94_40], [0.49_16, 0.63_15, 0.59_54], [0.61_08, 0.86_37, 0.11_35]] ) assert outputs.logits.shape == expected_shape assert torch.allclose(outputs.logits[0, :3, :3] , _lowerCamelCase , atol=1E-4 ) assert torch.allclose(outputs.pred_boxes[0, :3, :3] , _lowerCamelCase , atol=1E-4 ) print('Looks ok!' ) if pytorch_dump_folder_path is not None: # Save model and image processor logger.info(F"""Saving PyTorch model and image processor to {pytorch_dump_folder_path}...""" ) Path(_lowerCamelCase ).mkdir(exist_ok=_lowerCamelCase ) model.save_pretrained(_lowerCamelCase ) image_processor.save_pretrained(_lowerCamelCase ) if push_to_hub: # Push model to HF hub logger.info('Pushing model to the hub...' ) a : Union[str, Any] = ( "microsoft/table-transformer-detection" if "detection" in checkpoint_url else "microsoft/table-transformer-structure-recognition" ) model.push_to_hub(_lowerCamelCase ) image_processor.push_to_hub(_lowerCamelCase ) if __name__ == "__main__": UpperCamelCase : Any = argparse.ArgumentParser() parser.add_argument( """--checkpoint_url""", default="""https://pubtables1m.blob.core.windows.net/model/pubtables1m_detection_detr_r18.pth""", type=str, choices=[ """https://pubtables1m.blob.core.windows.net/model/pubtables1m_detection_detr_r18.pth""", """https://pubtables1m.blob.core.windows.net/model/pubtables1m_structure_detr_r18.pth""", ], help="""URL of the Table Transformer checkpoint you\'d like to convert.""", ) 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""", help="""Whether or not to push the converted model to the 🤗 hub.""" ) UpperCamelCase : List[str] = parser.parse_args() convert_table_transformer_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub)
370
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE__ ( snake_case : int | float | str , snake_case : int | float | str ) -> list[str]: """simple docstring""" if nth_term == "": return [""] a : Dict = int(snake_case ) a : Optional[int] = int(snake_case ) a : list[str] = [] for temp in range(int(snake_case ) ): series.append(F"""1 / {pow(temp + 1 , int(snake_case ) )}""" if series else '1' ) return series if __name__ == "__main__": import doctest doctest.testmod() UpperCamelCase : Optional[int] = int(input("""Enter the last number (nth term) of the P-Series""")) UpperCamelCase : List[Any] = int(input("""Enter the power for P-Series""")) print("""Formula of P-Series => 1+1/2^p+1/3^p ..... 1/n^p""") print(p_series(nth_term, power))
345
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCamelCase : Dict = { """configuration_jukebox""": [ """JUKEBOX_PRETRAINED_CONFIG_ARCHIVE_MAP""", """JukeboxConfig""", """JukeboxPriorConfig""", """JukeboxVQVAEConfig""", ], """tokenization_jukebox""": ["""JukeboxTokenizer"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Optional[Any] = [ """JUKEBOX_PRETRAINED_MODEL_ARCHIVE_LIST""", """JukeboxModel""", """JukeboxPreTrainedModel""", """JukeboxVQVAE""", """JukeboxPrior""", ] if TYPE_CHECKING: from .configuration_jukebox import ( JUKEBOX_PRETRAINED_CONFIG_ARCHIVE_MAP, JukeboxConfig, JukeboxPriorConfig, JukeboxVQVAEConfig, ) from .tokenization_jukebox import JukeboxTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_jukebox import ( JUKEBOX_PRETRAINED_MODEL_ARCHIVE_LIST, JukeboxModel, JukeboxPreTrainedModel, JukeboxPrior, JukeboxVQVAE, ) else: import sys UpperCamelCase : str = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
371
'''simple docstring''' import torch def SCREAMING_SNAKE_CASE__ ( ) -> str: """simple docstring""" if torch.cuda.is_available(): a : int = torch.cuda.device_count() else: a : Any = 0 print(F"""Successfully ran on {num_gpus} GPUs""" ) if __name__ == "__main__": main()
345
0
'''simple docstring''' import re from filelock import FileLock try: import nltk UpperCamelCase : Tuple = True except (ImportError, ModuleNotFoundError): UpperCamelCase : int = False if NLTK_AVAILABLE: with FileLock(""".lock""") as lock: nltk.download("""punkt""", quiet=True) def SCREAMING_SNAKE_CASE__ ( snake_case : str ) -> Optional[int]: """simple docstring""" re.sub('<n>' , '' , lowerCamelCase__ ) # 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(lowerCamelCase__ ) )
350
'''simple docstring''' import faiss # noqa: F401 # Here to have a nice missing dependency error message early on import numpy # noqa: F401 # Here to have a nice missing dependency error message early on import requests # noqa: F401 # Here to have a nice missing dependency error message early on import sklearn # noqa: F401 # Here to have a nice missing dependency error message early on import tqdm # noqa: F401 # Here to have a nice missing dependency error message early on from mauve import compute_mauve # From: mauve-text import datasets UpperCamelCase : Optional[int] = """\ @inproceedings{pillutla-etal:mauve:neurips2021, title={MAUVE: Measuring the Gap Between Neural Text and Human Text using Divergence Frontiers}, author={Pillutla, Krishna and Swayamdipta, Swabha and Zellers, Rowan and Thickstun, John and Welleck, Sean and Choi, Yejin and Harchaoui, Zaid}, booktitle = {NeurIPS}, year = {2021} } """ UpperCamelCase : Optional[Any] = """\ MAUVE is a library built on PyTorch and HuggingFace Transformers to measure the gap between neural text and human text with the eponymous MAUVE measure. MAUVE summarizes both Type I and Type II errors measured softly using Kullback–Leibler (KL) divergences. For details, see the MAUVE paper: https://arxiv.org/abs/2102.01454 (Neurips, 2021). This metrics is a wrapper around the official implementation of MAUVE: https://github.com/krishnap25/mauve """ UpperCamelCase : str = """ Calculates MAUVE scores between two lists of generated text and reference text. Args: predictions: list of generated text to score. Each predictions should be a string with tokens separated by spaces. references: list of reference for each prediction. Each reference should be a string with tokens separated by spaces. Optional Args: num_buckets: the size of the histogram to quantize P and Q. Options: 'auto' (default) or an integer pca_max_data: the number data points to use for PCA dimensionality reduction prior to clustering. If -1, use all the data. Default -1 kmeans_explained_var: amount of variance of the data to keep in dimensionality reduction by PCA. Default 0.9 kmeans_num_redo: number of times to redo k-means clustering (the best objective is kept). Default 5 kmeans_max_iter: maximum number of k-means iterations. Default 500 featurize_model_name: name of the model from which features are obtained. Default 'gpt2-large' Use one of ['gpt2', 'gpt2-medium', 'gpt2-large', 'gpt2-xl']. device_id: Device for featurization. Supply a GPU id (e.g. 0 or 3) to use GPU. If no GPU with this id is found, use CPU max_text_length: maximum number of tokens to consider. Default 1024 divergence_curve_discretization_size: Number of points to consider on the divergence curve. Default 25 mauve_scaling_factor: \"c\" from the paper. Default 5. verbose: If True (default), print running time updates seed: random seed to initialize k-means cluster assignments. Returns: mauve: MAUVE score, a number between 0 and 1. Larger values indicate that P and Q are closer, frontier_integral: Frontier Integral, a number between 0 and 1. Smaller values indicate that P and Q are closer, divergence_curve: a numpy.ndarray of shape (m, 2); plot it with matplotlib to view the divergence curve, p_hist: a discrete distribution, which is a quantized version of the text distribution p_text, q_hist: same as above, but with q_text. Examples: >>> # faiss segfaults in doctest for some reason, so the .compute call is not tested with doctest >>> import datasets >>> mauve = datasets.load_metric('mauve') >>> predictions = [\"hello there\", \"general kenobi\"] >>> references = [\"hello there\", \"general kenobi\"] >>> out = mauve.compute(predictions=predictions, references=references) # doctest: +SKIP >>> print(out.mauve) # doctest: +SKIP 1.0 """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCamelCase ( datasets.Metric ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage='https://github.com/krishnap25/mauve' , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('string' , id='sequence'), 'references': datasets.Value('string' , id='sequence'), }) , codebase_urls=['https://github.com/krishnap25/mauve'] , reference_urls=[ 'https://arxiv.org/abs/2102.01454', 'https://github.com/krishnap25/mauve', ] , ) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : Optional[Any]=None , UpperCAmelCase_ : int=None , UpperCAmelCase_ : Any=None , UpperCAmelCase_ : Optional[int]=None , UpperCAmelCase_ : Tuple="auto" , UpperCAmelCase_ : Any=-1 , UpperCAmelCase_ : Optional[int]=0.9 , UpperCAmelCase_ : Union[str, Any]=5 , UpperCAmelCase_ : int=5_0_0 , UpperCAmelCase_ : int="gpt2-large" , UpperCAmelCase_ : Tuple=-1 , UpperCAmelCase_ : Dict=1_0_2_4 , UpperCAmelCase_ : List[str]=2_5 , UpperCAmelCase_ : int=5 , UpperCAmelCase_ : Any=True , UpperCAmelCase_ : str=2_5 , ): """simple docstring""" a : List[str] = compute_mauve( p_text=UpperCAmelCase_ , q_text=UpperCAmelCase_ , p_features=UpperCAmelCase_ , q_features=UpperCAmelCase_ , p_tokens=UpperCAmelCase_ , q_tokens=UpperCAmelCase_ , num_buckets=UpperCAmelCase_ , pca_max_data=UpperCAmelCase_ , kmeans_explained_var=UpperCAmelCase_ , kmeans_num_redo=UpperCAmelCase_ , kmeans_max_iter=UpperCAmelCase_ , featurize_model_name=UpperCAmelCase_ , device_id=UpperCAmelCase_ , max_text_length=UpperCAmelCase_ , divergence_curve_discretization_size=UpperCAmelCase_ , mauve_scaling_factor=UpperCAmelCase_ , verbose=UpperCAmelCase_ , seed=UpperCAmelCase_ , ) return out
345
0
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, StableDiffusionSAGPipeline, UNetaDConditionModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class UpperCamelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): """simple docstring""" A : Union[str, Any] = StableDiffusionSAGPipeline A : Optional[int] = TEXT_TO_IMAGE_PARAMS A : str = TEXT_TO_IMAGE_BATCH_PARAMS A : int = TEXT_TO_IMAGE_IMAGE_PARAMS A : Union[str, Any] = TEXT_TO_IMAGE_IMAGE_PARAMS A : Union[str, Any] = False def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" torch.manual_seed(0) a : Union[str, Any] = UNetaDConditionModel( block_out_channels=(3_2, 6_4) , layers_per_block=2 , sample_size=3_2 , in_channels=4 , out_channels=4 , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D') , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D') , cross_attention_dim=3_2 , ) a : Tuple = DDIMScheduler( beta_start=0.0_00_85 , beta_end=0.0_12 , beta_schedule='scaled_linear' , clip_sample=_snake_case , set_alpha_to_one=_snake_case , ) torch.manual_seed(0) a : Any = AutoencoderKL( block_out_channels=[3_2, 6_4] , in_channels=3 , out_channels=3 , down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] , up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] , latent_channels=4 , ) torch.manual_seed(0) a : Union[str, Any] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=3_2 , intermediate_size=3_7 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_0_0_0 , ) a : Union[str, Any] = CLIPTextModel(_snake_case) a : Dict = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip') a : Optional[int] = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, "safety_checker": None, "feature_extractor": None, } return components def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : Any , UpperCAmelCase_ : Union[str, Any]=0): """simple docstring""" if str(_snake_case).startswith('mps'): a : Dict = torch.manual_seed(_snake_case) else: a : List[Any] = torch.Generator(device=_snake_case).manual_seed(_snake_case) a : Dict = { "prompt": ".", "generator": generator, "num_inference_steps": 2, "guidance_scale": 1.0, "sag_scale": 1.0, "output_type": "numpy", } return inputs def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=3e-3) @slow @require_torch_gpu class UpperCamelCase ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : Dict = StableDiffusionSAGPipeline.from_pretrained('CompVis/stable-diffusion-v1-4') a : Tuple = sag_pipe.to(_snake_case) sag_pipe.set_progress_bar_config(disable=_snake_case) a : Optional[int] = "." a : Optional[int] = torch.manual_seed(0) a : List[str] = sag_pipe( [prompt] , generator=_snake_case , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=2_0 , output_type='np') a : Tuple = output.images a : int = image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) a : List[Any] = np.array([0.15_68, 0.17_38, 0.16_95, 0.16_93, 0.15_07, 0.17_05, 0.15_47, 0.17_51, 0.19_49]) assert np.abs(image_slice.flatten() - expected_slice).max() < 5e-2 def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" a : Optional[int] = StableDiffusionSAGPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base') a : Optional[Any] = sag_pipe.to(_snake_case) sag_pipe.set_progress_bar_config(disable=_snake_case) a : Union[str, Any] = "." a : Dict = torch.manual_seed(0) a : Union[str, Any] = sag_pipe( [prompt] , generator=_snake_case , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=2_0 , output_type='np') a : Optional[Any] = output.images a : Optional[int] = image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) a : Union[str, Any] = np.array([0.34_59, 0.28_76, 0.25_37, 0.30_02, 0.26_71, 0.21_60, 0.30_26, 0.22_62, 0.23_71]) assert np.abs(image_slice.flatten() - expected_slice).max() < 5e-2 def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Dict = StableDiffusionSAGPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base') a : str = sag_pipe.to(_snake_case) sag_pipe.set_progress_bar_config(disable=_snake_case) a : str = "." a : Union[str, Any] = torch.manual_seed(0) a : int = sag_pipe( [prompt] , width=7_6_8 , height=5_1_2 , generator=_snake_case , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=2_0 , output_type='np' , ) a : Union[str, Any] = output.images assert image.shape == (1, 5_1_2, 7_6_8, 3)
351
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE__ ( snake_case : list[int | float] , snake_case : int , snake_case : int ) -> int | float: """simple docstring""" if len(snake_case ) == 0: raise ValueError('find_max() arg is an empty sequence' ) if ( left >= len(snake_case ) or left < -len(snake_case ) or right >= len(snake_case ) or right < -len(snake_case ) ): raise IndexError('list index out of range' ) if left == right: return nums[left] a : Union[str, Any] = (left + right) >> 1 # the middle a : List[str] = find_max(snake_case , snake_case , snake_case ) # find max in range[left, mid] a : Dict = find_max(snake_case , mid + 1 , snake_case ) # 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)
345
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase : Union[str, Any] = logging.get_logger(__name__) UpperCamelCase : Any = { 'weiweishi/roc-bert-base-zh': 'https://huggingface.co/weiweishi/roc-bert-base-zh/resolve/main/config.json', } class UpperCamelCase ( a_ ): """simple docstring""" A : Tuple = "roc_bert" def __init__( self : Tuple , UpperCAmelCase_ : Dict=3_0_5_2_2 , UpperCAmelCase_ : List[str]=7_6_8 , UpperCAmelCase_ : Any=1_2 , UpperCAmelCase_ : str=1_2 , UpperCAmelCase_ : List[str]=3_0_7_2 , UpperCAmelCase_ : str="gelu" , UpperCAmelCase_ : List[str]=0.1 , UpperCAmelCase_ : Dict=0.1 , UpperCAmelCase_ : str=5_1_2 , UpperCAmelCase_ : Any=2 , UpperCAmelCase_ : Optional[int]=0.02 , UpperCAmelCase_ : Union[str, Any]=1e-12 , UpperCAmelCase_ : Optional[int]=True , UpperCAmelCase_ : Optional[Any]=0 , UpperCAmelCase_ : List[Any]="absolute" , UpperCAmelCase_ : List[Any]=None , UpperCAmelCase_ : Dict=True , UpperCAmelCase_ : Tuple=True , UpperCAmelCase_ : Optional[int]=7_6_8 , UpperCAmelCase_ : Tuple=9_1_0 , UpperCAmelCase_ : Dict=5_1_2 , UpperCAmelCase_ : Any=2_4_8_5_8 , UpperCAmelCase_ : List[str]=True , **UpperCAmelCase_ : List[str] , ): """simple docstring""" a : Optional[int] = vocab_size a : Optional[int] = max_position_embeddings a : Union[str, Any] = hidden_size a : Dict = num_hidden_layers a : List[str] = num_attention_heads a : int = intermediate_size a : Tuple = hidden_act a : Any = hidden_dropout_prob a : Optional[int] = attention_probs_dropout_prob a : int = initializer_range a : int = type_vocab_size a : Dict = layer_norm_eps a : List[str] = use_cache a : Optional[Any] = enable_pronunciation a : Union[str, Any] = enable_shape a : Union[str, Any] = pronunciation_embed_dim a : Optional[int] = pronunciation_vocab_size a : int = shape_embed_dim a : int = shape_vocab_size a : Tuple = concat_input a : str = position_embedding_type a : Union[str, Any] = classifier_dropout super().__init__(pad_token_id=UpperCAmelCase_ , **UpperCAmelCase_)
352
'''simple docstring''' import math import os from copy import deepcopy import datasets import evaluate import torch import transformers from datasets import load_dataset from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer from accelerate import Accelerator from accelerate.test_utils import RegressionDataset, RegressionModel from accelerate.utils import is_tpu_available, set_seed UpperCamelCase : int = """true""" def SCREAMING_SNAKE_CASE__ ( snake_case : List[str] , snake_case : int=82 , snake_case : Tuple=16 ) -> Union[str, Any]: """simple docstring""" set_seed(42 ) a : List[str] = RegressionModel() a : Union[str, Any] = deepcopy(snake_case ) a : Dict = RegressionDataset(length=snake_case ) a : Dict = DataLoader(snake_case , batch_size=snake_case ) model.to(accelerator.device ) a , a : Optional[int] = accelerator.prepare(snake_case , snake_case ) return model, ddp_model, dataloader def SCREAMING_SNAKE_CASE__ ( snake_case : Accelerator , snake_case : Union[str, Any]=False ) -> Optional[int]: """simple docstring""" a : List[str] = AutoTokenizer.from_pretrained('hf-internal-testing/mrpc-bert-base-cased' ) a : Any = load_dataset('glue' , 'mrpc' , split='validation' ) def tokenize_function(snake_case : int ): a : Any = tokenizer(examples['sentence1'] , examples['sentence2'] , truncation=snake_case , max_length=snake_case ) return outputs with accelerator.main_process_first(): a : Dict = dataset.map( snake_case , batched=snake_case , remove_columns=['idx', 'sentence1', 'sentence2'] , ) a : List[str] = tokenized_datasets.rename_column('label' , 'labels' ) def collate_fn(snake_case : Optional[Any] ): if use_longest: return tokenizer.pad(snake_case , padding='longest' , return_tensors='pt' ) return tokenizer.pad(snake_case , padding='max_length' , max_length=128 , return_tensors='pt' ) return DataLoader(snake_case , shuffle=snake_case , collate_fn=snake_case , batch_size=16 ) def SCREAMING_SNAKE_CASE__ ( snake_case : Optional[Any] , snake_case : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" a : int = Accelerator(dispatch_batches=snake_case , split_batches=snake_case ) a : List[str] = get_dataloader(snake_case , not dispatch_batches ) a : Optional[Any] = AutoModelForSequenceClassification.from_pretrained( 'hf-internal-testing/mrpc-bert-base-cased' , return_dict=snake_case ) a , a : Optional[Any] = accelerator.prepare(snake_case , snake_case ) return {"ddp": [ddp_model, ddp_dataloader, "cuda:0"], "no": [model, dataloader, accelerator.device]}, accelerator def SCREAMING_SNAKE_CASE__ ( snake_case : Optional[Any] , snake_case : Optional[Any] , snake_case : Union[str, Any] ) -> Optional[int]: """simple docstring""" a : Dict = [] for batch in dataloader: a , a : Any = batch.values() with torch.no_grad(): a : Tuple = model(snake_case ) a , a : Dict = accelerator.gather_for_metrics((logit, target) ) logits_and_targets.append((logit, target) ) a , a : List[str] = [], [] for logit, targ in logits_and_targets: logits.append(snake_case ) targs.append(snake_case ) a , a : Any = torch.cat(snake_case ), torch.cat(snake_case ) return logits, targs def SCREAMING_SNAKE_CASE__ ( snake_case : Accelerator , snake_case : Dict=82 , snake_case : str=False , snake_case : List[str]=False , snake_case : List[Any]=16 ) -> Optional[int]: """simple docstring""" a , a , a : int = get_basic_setup(snake_case , snake_case , snake_case ) a , a : int = generate_predictions(snake_case , snake_case , snake_case ) assert ( len(snake_case ) == num_samples ), F"""Unexpected number of inputs:\n Expected: {num_samples}\n Actual: {len(snake_case )}""" def SCREAMING_SNAKE_CASE__ ( snake_case : bool = False , snake_case : bool = False ) -> List[str]: """simple docstring""" a : int = evaluate.load('glue' , 'mrpc' ) a , a : Tuple = get_mrpc_setup(snake_case , snake_case ) # First do baseline a , a , a : Tuple = setup['no'] model.to(snake_case ) model.eval() for batch in dataloader: batch.to(snake_case ) with torch.inference_mode(): a : List[Any] = model(**snake_case ) a : Optional[Any] = outputs.logits.argmax(dim=-1 ) metric.add_batch(predictions=snake_case , references=batch['labels'] ) a : Tuple = metric.compute() # Then do distributed a , a , a : Tuple = setup['ddp'] model.eval() for batch in dataloader: with torch.inference_mode(): a : List[str] = model(**snake_case ) a : Optional[Any] = outputs.logits.argmax(dim=-1 ) a : Optional[int] = batch['labels'] a , a : Optional[int] = accelerator.gather_for_metrics((preds, references) ) metric.add_batch(predictions=snake_case , references=snake_case ) a : str = metric.compute() for key in "accuracy f1".split(): assert math.isclose( baseline[key] , distributed[key] ), F"""Baseline and Distributed are not the same for key {key}:\n\tBaseline: {baseline[key]}\n\tDistributed: {distributed[key]}\n""" def SCREAMING_SNAKE_CASE__ ( ) -> str: """simple docstring""" a : Dict = Accelerator(split_batches=snake_case , dispatch_batches=snake_case ) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_warning() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() # These are a bit slower so they should only be ran on the GPU or TPU if torch.cuda.is_available() or is_tpu_available(): if accelerator.is_local_main_process: print('**Testing gather_for_metrics**' ) for split_batches in [True, False]: for dispatch_batches in [True, False]: if accelerator.is_local_main_process: print(F"""With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`""" ) test_mrpc(snake_case , snake_case ) accelerator.state._reset_state() if accelerator.is_local_main_process: print('**Test torch metrics**' ) for split_batches in [True, False]: for dispatch_batches in [True, False]: a : List[Any] = Accelerator(split_batches=snake_case , dispatch_batches=snake_case ) if accelerator.is_local_main_process: print(F"""With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`, length=99""" ) test_torch_metrics(snake_case , 99 ) accelerator.state._reset_state() if accelerator.is_local_main_process: print('**Test last batch is not dropped when perfectly divisible**' ) a : Optional[Any] = Accelerator() test_torch_metrics(snake_case , 512 ) accelerator.state._reset_state() def SCREAMING_SNAKE_CASE__ ( snake_case : Union[str, Any] ) -> int: """simple docstring""" # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
345
0
from __future__ import annotations import time import numpy as np UpperCamelCase : Union[str, Any] = [8, 5, 9, 7] UpperCamelCase : int = [ [2, 0, 1, 1], [0, 1, 2, 1], [4, 0, 0, 3], [0, 2, 1, 0], [1, 0, 3, 0], ] UpperCamelCase : List[Any] = [ [3, 2, 1, 4], [0, 2, 5, 2], [5, 1, 0, 5], [1, 5, 3, 0], [3, 0, 3, 3], ] class UpperCamelCase : """simple docstring""" def __init__( self : str , UpperCAmelCase_ : list[int] , UpperCAmelCase_ : list[list[int]] , UpperCAmelCase_ : list[list[int]] , ): """simple docstring""" a : Optional[Any] = claim_vector a : List[Any] = allocated_resources_table a : Union[str, Any] = maximum_claim_table def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" return [ sum(p_item[i] for p_item in self.__allocated_resources_table) for i in range(len(self.__allocated_resources_table[0])) ] def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" return np.array(self.__claim_vector) - np.array( self.__processes_resource_summation()) def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" return [ list(np.array(self.__maximum_claim_table[i]) - np.array(UpperCAmelCase_)) for i, allocated_resource in enumerate(self.__allocated_resources_table) ] def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" return {self.__need().index(UpperCAmelCase_): i for i in self.__need()} def SCREAMING_SNAKE_CASE_ ( self : List[Any] , **UpperCAmelCase_ : Optional[int]): """simple docstring""" a : Tuple = self.__need() a : Union[str, Any] = self.__allocated_resources_table a : Optional[int] = self.__available_resources() a : Optional[Any] = self.__need_index_manager() for kw, val in kwargs.items(): if kw and val is True: self.__pretty_data() print('_' * 5_0 + '\n') while need_list: a : Tuple = False for each_need in need_list: a : List[Any] = True for index, need in enumerate(UpperCAmelCase_): if need > available_resources[index]: a : Union[str, Any] = False break if execution: a : Optional[Any] = True # get the original index of the process from ind_ctrl db for original_need_index, need_clone in need_index_manager.items(): if each_need == need_clone: a : str = original_need_index print(f"""Process {process_number + 1} is executing.""") # remove the process run from stack need_list.remove(UpperCAmelCase_) # update available/freed resources stack a : str = np.array(UpperCAmelCase_) + np.array( alloc_resources_table[process_number]) print( 'Updated available resource stack for processes: ' + ' '.join([str(UpperCAmelCase_) for x in available_resources])) break if safe: print('The process is in a safe state.\n') else: print('System in unsafe state. Aborting...\n') break def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" print(' ' * 9 + 'Allocated Resource Table') for item in self.__allocated_resources_table: print( f"""P{self.__allocated_resources_table.index(UpperCAmelCase_) + 1}""" + ' '.join(f"""{it:>8}""" for it in item) + '\n') print(' ' * 9 + 'System Resource Table') for item in self.__maximum_claim_table: print( f"""P{self.__maximum_claim_table.index(UpperCAmelCase_) + 1}""" + ' '.join(f"""{it:>8}""" for it in item) + '\n') print( 'Current Usage by Active Processes: ' + ' '.join(str(UpperCAmelCase_) for x in self.__claim_vector)) print( 'Initial Available Resources: ' + ' '.join(str(UpperCAmelCase_) for x in self.__available_resources())) time.sleep(1) if __name__ == "__main__": import doctest doctest.testmod()
353
'''simple docstring''' from math import acos, sin from typing import List, Tuple, Union import numpy as np import torch from PIL import Image from ...models import AutoencoderKL, UNetaDConditionModel from ...schedulers import DDIMScheduler, DDPMScheduler from ...utils import randn_tensor from ..pipeline_utils import AudioPipelineOutput, BaseOutput, DiffusionPipeline, ImagePipelineOutput from .mel import Mel class UpperCamelCase ( a_ ): """simple docstring""" A : Optional[int] = ["vqvae"] def __init__( self : List[str] , UpperCAmelCase_ : AutoencoderKL , UpperCAmelCase_ : UNetaDConditionModel , UpperCAmelCase_ : Mel , UpperCAmelCase_ : Union[DDIMScheduler, DDPMScheduler] , ): """simple docstring""" super().__init__() self.register_modules(unet=UpperCAmelCase_ , scheduler=UpperCAmelCase_ , mel=UpperCAmelCase_ , vqvae=UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" return 5_0 if isinstance(self.scheduler , UpperCAmelCase_) else 1_0_0_0 @torch.no_grad() def __call__( self : Dict , UpperCAmelCase_ : int = 1 , UpperCAmelCase_ : str = None , UpperCAmelCase_ : np.ndarray = None , UpperCAmelCase_ : int = 0 , UpperCAmelCase_ : int = 0 , UpperCAmelCase_ : int = None , UpperCAmelCase_ : torch.Generator = None , UpperCAmelCase_ : float = 0 , UpperCAmelCase_ : float = 0 , UpperCAmelCase_ : torch.Generator = None , UpperCAmelCase_ : float = 0 , UpperCAmelCase_ : torch.Tensor = None , UpperCAmelCase_ : torch.Tensor = None , UpperCAmelCase_ : Optional[Any]=True , ): """simple docstring""" a : Optional[Any] = steps or self.get_default_steps() self.scheduler.set_timesteps(UpperCAmelCase_) a : Optional[Any] = step_generator or generator # For backwards compatibility if type(self.unet.config.sample_size) == int: a : Dict = (self.unet.config.sample_size, self.unet.config.sample_size) if noise is None: a : Dict = randn_tensor( ( batch_size, self.unet.config.in_channels, self.unet.config.sample_size[0], self.unet.config.sample_size[1], ) , generator=UpperCAmelCase_ , device=self.device , ) a : Tuple = noise a : Optional[int] = None if audio_file is not None or raw_audio is not None: self.mel.load_audio(UpperCAmelCase_ , UpperCAmelCase_) a : List[Any] = self.mel.audio_slice_to_image(UpperCAmelCase_) a : str = np.frombuffer(input_image.tobytes() , dtype='uint8').reshape( (input_image.height, input_image.width)) a : List[str] = (input_image / 2_5_5) * 2 - 1 a : Any = torch.tensor(input_image[np.newaxis, :, :] , dtype=torch.float).to(self.device) if self.vqvae is not None: a : List[Any] = self.vqvae.encode(torch.unsqueeze(UpperCAmelCase_ , 0)).latent_dist.sample( generator=UpperCAmelCase_)[0] a : str = self.vqvae.config.scaling_factor * input_images if start_step > 0: a : Union[str, Any] = self.scheduler.add_noise(UpperCAmelCase_ , UpperCAmelCase_ , self.scheduler.timesteps[start_step - 1]) a : Dict = ( self.unet.config.sample_size[1] * self.mel.get_sample_rate() / self.mel.x_res / self.mel.hop_length ) a : List[Any] = int(mask_start_secs * pixels_per_second) a : Optional[Any] = int(mask_end_secs * pixels_per_second) a : Optional[int] = self.scheduler.add_noise(UpperCAmelCase_ , UpperCAmelCase_ , torch.tensor(self.scheduler.timesteps[start_step:])) for step, t in enumerate(self.progress_bar(self.scheduler.timesteps[start_step:])): if isinstance(self.unet , UpperCAmelCase_): a : Dict = self.unet(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_)['sample'] else: a : str = self.unet(UpperCAmelCase_ , UpperCAmelCase_)['sample'] if isinstance(self.scheduler , UpperCAmelCase_): a : List[Any] = self.scheduler.step( model_output=UpperCAmelCase_ , timestep=UpperCAmelCase_ , sample=UpperCAmelCase_ , eta=UpperCAmelCase_ , generator=UpperCAmelCase_ , )['prev_sample'] else: a : Any = self.scheduler.step( model_output=UpperCAmelCase_ , timestep=UpperCAmelCase_ , sample=UpperCAmelCase_ , generator=UpperCAmelCase_ , )['prev_sample'] if mask is not None: if mask_start > 0: a : str = mask[:, step, :, :mask_start] if mask_end > 0: a : Dict = mask[:, step, :, -mask_end:] if self.vqvae is not None: # 0.18215 was scaling factor used in training to ensure unit variance a : List[str] = 1 / self.vqvae.config.scaling_factor * images a : str = self.vqvae.decode(UpperCAmelCase_)['sample'] a : Tuple = (images / 2 + 0.5).clamp(0 , 1) a : Any = images.cpu().permute(0 , 2 , 3 , 1).numpy() a : List[str] = (images * 2_5_5).round().astype('uint8') a : Tuple = list( (Image.fromarray(_[:, :, 0]) for _ in images) if images.shape[3] == 1 else (Image.fromarray(UpperCAmelCase_ , mode='RGB').convert('L') for _ in images)) a : List[str] = [self.mel.image_to_audio(UpperCAmelCase_) for _ in images] if not return_dict: return images, (self.mel.get_sample_rate(), audios) return BaseOutput(**AudioPipelineOutput(np.array(UpperCAmelCase_)[:, np.newaxis, :]) , **ImagePipelineOutput(UpperCAmelCase_)) @torch.no_grad() def SCREAMING_SNAKE_CASE_ ( self : List[Any] , UpperCAmelCase_ : List[Image.Image] , UpperCAmelCase_ : int = 5_0): """simple docstring""" assert isinstance(self.scheduler , UpperCAmelCase_) self.scheduler.set_timesteps(UpperCAmelCase_) a : Dict = np.array( [np.frombuffer(image.tobytes() , dtype='uint8').reshape((1, image.height, image.width)) for image in images]) a : Tuple = (sample / 2_5_5) * 2 - 1 a : int = torch.Tensor(UpperCAmelCase_).to(self.device) for t in self.progress_bar(torch.flip(self.scheduler.timesteps , (0,))): a : Optional[Any] = t - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps a : Optional[Any] = self.scheduler.alphas_cumprod[t] a : List[Any] = ( self.scheduler.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.scheduler.final_alpha_cumprod ) a : List[str] = 1 - alpha_prod_t a : Optional[Any] = self.unet(UpperCAmelCase_ , UpperCAmelCase_)['sample'] a : Union[str, Any] = (1 - alpha_prod_t_prev) ** 0.5 * model_output a : Dict = (sample - pred_sample_direction) * alpha_prod_t_prev ** (-0.5) a : Union[str, Any] = sample * alpha_prod_t ** 0.5 + beta_prod_t ** 0.5 * model_output return sample @staticmethod def SCREAMING_SNAKE_CASE_ ( UpperCAmelCase_ : torch.Tensor , UpperCAmelCase_ : torch.Tensor , UpperCAmelCase_ : float): """simple docstring""" a : List[Any] = acos(torch.dot(torch.flatten(UpperCAmelCase_) , torch.flatten(UpperCAmelCase_)) / torch.norm(UpperCAmelCase_) / torch.norm(UpperCAmelCase_)) return sin((1 - alpha) * theta) * xa / sin(UpperCAmelCase_) + sin(alpha * theta) * xa / sin(UpperCAmelCase_)
345
0
'''simple docstring''' UpperCamelCase : Union[str, Any] = ''' # Transformers 설치 방법 ! pip install transformers datasets # 마지막 릴리스 대신 소스에서 설치하려면, 위 명령을 주석으로 바꾸고 아래 명령을 해제하세요. # ! pip install git+https://github.com/huggingface/transformers.git ''' UpperCamelCase : Union[str, Any] = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] UpperCamelCase : List[Any] = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
354
'''simple docstring''' import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_gpu, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class UpperCamelCase : """simple docstring""" def __init__( self : str , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : List[Any]=1_3 , UpperCAmelCase_ : List[str]=3_0 , UpperCAmelCase_ : str=2 , UpperCAmelCase_ : Union[str, Any]=3 , UpperCAmelCase_ : str=True , UpperCAmelCase_ : Union[str, Any]=True , UpperCAmelCase_ : Union[str, Any]=3_2 , UpperCAmelCase_ : Union[str, Any]=5 , UpperCAmelCase_ : Tuple=4 , UpperCAmelCase_ : Dict=3_7 , UpperCAmelCase_ : Optional[int]="gelu" , UpperCAmelCase_ : Optional[int]=0.1 , UpperCAmelCase_ : Union[str, Any]=0.1 , UpperCAmelCase_ : Dict=1_0 , UpperCAmelCase_ : Optional[Any]=0.02 , UpperCAmelCase_ : List[str]=None , UpperCAmelCase_ : Tuple=2 , ): """simple docstring""" a : Any = parent a : Optional[int] = batch_size a : str = image_size a : str = patch_size a : List[Any] = num_channels a : Optional[int] = is_training a : Dict = use_labels a : Any = hidden_size a : Optional[int] = num_hidden_layers a : int = num_attention_heads a : int = intermediate_size a : Any = hidden_act a : Optional[int] = hidden_dropout_prob a : Optional[int] = attention_probs_dropout_prob a : Dict = type_sequence_label_size a : Tuple = initializer_range a : List[str] = scope a : str = encoder_stride # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) a : Optional[Any] = (image_size // patch_size) ** 2 a : str = num_patches + 1 def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : Union[str, Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]) a : List[Any] = None if self.use_labels: a : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size) a : List[str] = self.get_config() return config, pixel_values, labels def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" return ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=UpperCAmelCase_ , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Dict): """simple docstring""" a : int = ViTModel(config=UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Optional[Any] = model(UpperCAmelCase_) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : List[Any]): """simple docstring""" a : str = ViTForMaskedImageModeling(config=UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : List[Any] = model(UpperCAmelCase_) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size)) # test greyscale images a : int = 1 a : Union[str, Any] = ViTForMaskedImageModeling(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Optional[Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]) a : Optional[Any] = model(UpperCAmelCase_) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size)) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : int , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Any): """simple docstring""" a : str = self.type_sequence_label_size a : Tuple = ViTForImageClassification(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Union[str, Any] = model(UpperCAmelCase_ , labels=UpperCAmelCase_) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) # test greyscale images a : List[Any] = 1 a : Union[str, Any] = ViTForImageClassification(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : int = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]) a : Optional[int] = model(UpperCAmelCase_) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Optional[int] = self.prepare_config_and_inputs() ( ( a ) , ( a ) , ( a ) , ) : Tuple = config_and_inputs a : str = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class UpperCamelCase ( a_ , a_ , unittest.TestCase ): """simple docstring""" A : str = ( ( ViTModel, ViTForImageClassification, ViTForMaskedImageModeling, ) if is_torch_available() else () ) A : Optional[Any] = ( {"feature-extraction": ViTModel, "image-classification": ViTForImageClassification} if is_torch_available() else {} ) A : List[str] = True A : Optional[int] = False A : Dict = False A : Optional[int] = False def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" a : str = ViTModelTester(self) a : Optional[Any] = ConfigTester(self , config_class=UpperCAmelCase_ , has_text_modality=UpperCAmelCase_ , hidden_size=3_7) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='ViT does not use inputs_embeds') def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" pass def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a , a : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a : Union[str, Any] = model_class(UpperCAmelCase_) self.assertIsInstance(model.get_input_embeddings() , (nn.Module)) a : Tuple = model.get_output_embeddings() self.assertTrue(x is None or isinstance(UpperCAmelCase_ , nn.Linear)) def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a , a : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a : Union[str, Any] = model_class(UpperCAmelCase_) a : Tuple = inspect.signature(model.forward) # signature.parameters is an OrderedDict => so arg_names order is deterministic a : Dict = [*signature.parameters.keys()] a : Dict = ['pixel_values'] self.assertListEqual(arg_names[:1] , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" a : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" a : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase_) @slow def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a : Dict = ViTModel.from_pretrained(UpperCAmelCase_) self.assertIsNotNone(UpperCAmelCase_) def SCREAMING_SNAKE_CASE__ ( ) -> Optional[Any]: """simple docstring""" a : List[Any] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class UpperCamelCase ( unittest.TestCase ): """simple docstring""" @cached_property def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224') if is_vision_available() else None @slow def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" a : Optional[Any] = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224').to(UpperCAmelCase_) a : List[Any] = self.default_image_processor a : List[str] = prepare_img() a : Tuple = image_processor(images=UpperCAmelCase_ , return_tensors='pt').to(UpperCAmelCase_) # forward pass with torch.no_grad(): a : List[Any] = model(**UpperCAmelCase_) # verify the logits a : List[str] = torch.Size((1, 1_0_0_0)) self.assertEqual(outputs.logits.shape , UpperCAmelCase_) a : Union[str, Any] = torch.tensor([-0.27_44, 0.82_15, -0.08_36]).to(UpperCAmelCase_) self.assertTrue(torch.allclose(outputs.logits[0, :3] , UpperCAmelCase_ , atol=1e-4)) @slow def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : List[str] = ViTModel.from_pretrained('facebook/dino-vits8').to(UpperCAmelCase_) a : Any = ViTImageProcessor.from_pretrained('facebook/dino-vits8' , size=4_8_0) a : int = prepare_img() a : List[str] = image_processor(images=UpperCAmelCase_ , return_tensors='pt') a : List[str] = inputs.pixel_values.to(UpperCAmelCase_) # forward pass with torch.no_grad(): a : List[Any] = model(UpperCAmelCase_ , interpolate_pos_encoding=UpperCAmelCase_) # verify the logits a : Dict = torch.Size((1, 3_6_0_1, 3_8_4)) self.assertEqual(outputs.last_hidden_state.shape , UpperCAmelCase_) a : str = torch.tensor( [[4.23_40, 4.39_06, -6.66_92], [4.54_63, 1.89_28, -6.72_57], [4.44_29, 0.84_96, -5.85_85]]).to(UpperCAmelCase_) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , UpperCAmelCase_ , atol=1e-4)) @slow @require_accelerate @require_torch_gpu def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : str = ViTModel.from_pretrained('facebook/dino-vits8' , torch_dtype=torch.floataa , device_map='auto') a : List[Any] = self.default_image_processor a : List[str] = prepare_img() a : Tuple = image_processor(images=UpperCAmelCase_ , return_tensors='pt') a : Tuple = inputs.pixel_values.to(UpperCAmelCase_) # forward pass to make sure inference works in fp16 with torch.no_grad(): a : Tuple = model(UpperCAmelCase_)
345
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices UpperCamelCase : Union[str, Any] = logging.get_logger(__name__) UpperCamelCase : Tuple = { "facebook/convnextv2-tiny-1k-224": "https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json", } class UpperCamelCase ( a_ , a_ ): """simple docstring""" A : List[Any] = "convnextv2" def __init__( self : List[Any] , UpperCAmelCase_ : Union[str, Any]=3 , UpperCAmelCase_ : Tuple=4 , UpperCAmelCase_ : Optional[int]=4 , UpperCAmelCase_ : Union[str, Any]=None , UpperCAmelCase_ : List[Any]=None , UpperCAmelCase_ : Optional[Any]="gelu" , UpperCAmelCase_ : Tuple=0.02 , UpperCAmelCase_ : Union[str, Any]=1e-12 , UpperCAmelCase_ : str=0.0 , UpperCAmelCase_ : Tuple=2_2_4 , UpperCAmelCase_ : Any=None , UpperCAmelCase_ : Optional[int]=None , **UpperCAmelCase_ : List[str] , ): """simple docstring""" super().__init__(**lowerCamelCase_) a : int = num_channels a : Tuple = patch_size a : Optional[Any] = num_stages a : List[Any] = [9_6, 1_9_2, 3_8_4, 7_6_8] if hidden_sizes is None else hidden_sizes a : str = [3, 3, 9, 3] if depths is None else depths a : Optional[Any] = hidden_act a : int = initializer_range a : List[Any] = layer_norm_eps a : Union[str, Any] = drop_path_rate a : Union[str, Any] = image_size a : List[str] = ['stem'] + [f"""stage{idx}""" for idx in range(1 , len(self.depths) + 1)] a , a : str = get_aligned_output_features_output_indices( out_features=lowerCamelCase_ , out_indices=lowerCamelCase_ , stage_names=self.stage_names)
355
'''simple docstring''' # Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import numpy as np import torch from ..models.clipseg import CLIPSegForImageSegmentation from ..utils import is_vision_available, requires_backends from .base import PipelineTool if is_vision_available(): from PIL import Image class UpperCamelCase ( a_ ): """simple docstring""" A : List[str] = ( "This is a tool that creates a segmentation mask of an image according to a label. It cannot create an image." "It takes two arguments named `image` which should be the original image, and `label` which should be a text " "describing the elements what should be identified in the segmentation mask. The tool returns the mask." ) A : List[Any] = "CIDAS/clipseg-rd64-refined" A : Optional[Any] = "image_segmenter" A : List[Any] = CLIPSegForImageSegmentation A : Tuple = ["image", "text"] A : Optional[int] = ["image"] def __init__( self : str , *UpperCAmelCase_ : str , **UpperCAmelCase_ : str): """simple docstring""" requires_backends(self , ['vision']) super().__init__(*UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : "Image" , UpperCAmelCase_ : str): """simple docstring""" return self.pre_processor(text=[label] , images=[image] , padding=UpperCAmelCase_ , return_tensors='pt') def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : str): """simple docstring""" with torch.no_grad(): a : Union[str, Any] = self.model(**UpperCAmelCase_).logits return logits def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , UpperCAmelCase_ : int): """simple docstring""" a : int = outputs.cpu().detach().numpy() a : str = 0 a : str = 1 return Image.fromarray((array * 2_5_5).astype(np.uinta))
345
0
'''simple docstring''' from typing import List, Optional from tokenizers import ByteLevelBPETokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot_small import BlenderbotSmallTokenizer UpperCamelCase : Any = logging.get_logger(__name__) UpperCamelCase : Union[str, Any] = { 'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_config_file': 'tokenizer_config.json', } UpperCamelCase : List[str] = { 'vocab_file': { 'facebook/blenderbot_small-90M': 'https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/vocab.json' }, 'merges_file': { 'facebook/blenderbot_small-90M': 'https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/merges.txt' }, 'tokenizer_config_file': { 'facebook/blenderbot_small-90M': ( 'https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/tokenizer_config.json' ) }, } UpperCamelCase : Optional[int] = { 'facebook/blenderbot_small-90M': 512, } class UpperCamelCase ( _lowerCamelCase ): """simple docstring""" A : Optional[int] = VOCAB_FILES_NAMES A : Dict = PRETRAINED_VOCAB_FILES_MAP A : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A : Union[str, Any] = BlenderbotSmallTokenizer def __init__( self : str , UpperCAmelCase_ : List[Any]=None , UpperCAmelCase_ : List[str]=None , UpperCAmelCase_ : List[Any]="<|endoftext|>" , UpperCAmelCase_ : Optional[int]="<|endoftext|>" , UpperCAmelCase_ : Any="<|endoftext|>" , UpperCAmelCase_ : int=False , UpperCAmelCase_ : str=True , **UpperCAmelCase_ : List[str] , ): """simple docstring""" super().__init__( ByteLevelBPETokenizer( vocab=lowercase_ , merges=lowercase_ , add_prefix_space=lowercase_ , trim_offsets=lowercase_ , ) , bos_token=lowercase_ , eos_token=lowercase_ , unk_token=lowercase_ , **lowercase_ , ) a : Optional[Any] = add_prefix_space def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Dict=None): """simple docstring""" a : Union[str, Any] = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , UpperCAmelCase_ : List[int] , UpperCAmelCase_ : Optional[List[int]] = None): """simple docstring""" a : Dict = [self.sep_token_id] a : Union[str, Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep) * [0]
356
'''simple docstring''' import json import os import unittest from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer from ...test_tokenization_common import TokenizerTesterMixin class UpperCamelCase ( a_ , unittest.TestCase ): """simple docstring""" A : Union[str, Any] = CTRLTokenizer A : List[Any] = False A : Optional[Any] = False def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt a : Dict = ['adapt', 're@@', 'a@@', 'apt', 'c@@', 't', '<unk>'] a : Tuple = dict(zip(UpperCAmelCase_ , range(len(UpperCAmelCase_)))) a : Any = ['#version: 0.2', 'a p', 'ap t</w>', 'r e', 'a d', 'ad apt</w>', ''] a : List[Any] = {'unk_token': '<unk>'} a : Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file']) a : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['merges_file']) with open(self.vocab_file , 'w' , encoding='utf-8') as fp: fp.write(json.dumps(UpperCAmelCase_) + '\n') with open(self.merges_file , 'w' , encoding='utf-8') as fp: fp.write('\n'.join(UpperCAmelCase_)) def SCREAMING_SNAKE_CASE_ ( self : Tuple , **UpperCAmelCase_ : Dict): """simple docstring""" kwargs.update(self.special_tokens_map) return CTRLTokenizer.from_pretrained(self.tmpdirname , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : Any): """simple docstring""" a : List[str] = 'adapt react readapt apt' a : int = 'adapt react readapt apt' return input_text, output_text def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : int = CTRLTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map) a : str = 'adapt react readapt apt' a : Optional[Any] = 'adapt re@@ a@@ c@@ t re@@ adapt apt'.split() a : List[Any] = tokenizer.tokenize(UpperCAmelCase_) self.assertListEqual(UpperCAmelCase_ , UpperCAmelCase_) a : Union[str, Any] = tokens + [tokenizer.unk_token] a : Any = [0, 1, 2, 4, 5, 1, 0, 3, 6] self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCAmelCase_) , UpperCAmelCase_)
345
0
import argparse import requests import torch from PIL import Image from torchvision.transforms import Compose, Normalize, Resize, ToTensor from transformers import SwinaSRConfig, SwinaSRForImageSuperResolution, SwinaSRImageProcessor def SCREAMING_SNAKE_CASE__ ( snake_case : Any ) -> Dict: """simple docstring""" a : Dict = SwinaSRConfig() if "Swin2SR_ClassicalSR_X4_64" in checkpoint_url: a : str = 4 elif "Swin2SR_CompressedSR_X4_48" in checkpoint_url: a : str = 4 a : Optional[Any] = 48 a : int = 'pixelshuffle_aux' elif "Swin2SR_Lightweight_X2_64" in checkpoint_url: a : Optional[int] = [6, 6, 6, 6] a : List[str] = 60 a : Any = [6, 6, 6, 6] a : Any = 'pixelshuffledirect' elif "Swin2SR_RealworldSR_X4_64_BSRGAN_PSNR" in checkpoint_url: a : List[str] = 4 a : Any = 'nearest+conv' elif "Swin2SR_Jpeg_dynamic" in checkpoint_url: a : Any = 1 a : Optional[int] = 1 a : Optional[int] = 126 a : str = 7 a : Union[str, Any] = 255.0 a : Any = '' return config def SCREAMING_SNAKE_CASE__ ( snake_case : Union[str, Any] , snake_case : int ) -> List[str]: """simple docstring""" if "patch_embed.proj" in name and "layers" not in name: a : Any = name.replace('patch_embed.proj' , 'embeddings.patch_embeddings.projection' ) if "patch_embed.norm" in name: a : Any = name.replace('patch_embed.norm' , 'embeddings.patch_embeddings.layernorm' ) if "layers" in name: a : Optional[Any] = name.replace('layers' , 'encoder.stages' ) if "residual_group.blocks" in name: a : int = name.replace('residual_group.blocks' , 'layers' ) if "attn.proj" in name: a : int = name.replace('attn.proj' , 'attention.output.dense' ) if "attn" in name: a : str = name.replace('attn' , 'attention.self' ) if "norm1" in name: a : str = name.replace('norm1' , 'layernorm_before' ) if "norm2" in name: a : List[Any] = name.replace('norm2' , 'layernorm_after' ) if "mlp.fc1" in name: a : Optional[Any] = name.replace('mlp.fc1' , 'intermediate.dense' ) if "mlp.fc2" in name: a : List[str] = name.replace('mlp.fc2' , 'output.dense' ) if "q_bias" in name: a : Any = name.replace('q_bias' , 'query.bias' ) if "k_bias" in name: a : Optional[int] = name.replace('k_bias' , 'key.bias' ) if "v_bias" in name: a : Dict = name.replace('v_bias' , 'value.bias' ) if "cpb_mlp" in name: a : int = name.replace('cpb_mlp' , 'continuous_position_bias_mlp' ) if "patch_embed.proj" in name: a : Optional[int] = name.replace('patch_embed.proj' , 'patch_embed.projection' ) if name == "norm.weight": a : Optional[Any] = 'layernorm.weight' if name == "norm.bias": a : int = 'layernorm.bias' if "conv_first" in name: a : Optional[int] = name.replace('conv_first' , 'first_convolution' ) if ( "upsample" in name or "conv_before_upsample" in name or "conv_bicubic" in name or "conv_up" in name or "conv_hr" in name or "conv_last" in name or "aux" in name ): # heads if "conv_last" in name: a : Optional[Any] = name.replace('conv_last' , 'final_convolution' ) if config.upsampler in ["pixelshuffle", "pixelshuffle_aux", "nearest+conv"]: if "conv_before_upsample.0" in name: a : List[Any] = name.replace('conv_before_upsample.0' , 'conv_before_upsample' ) if "upsample.0" in name: a : Any = name.replace('upsample.0' , 'upsample.convolution_0' ) if "upsample.2" in name: a : List[Any] = name.replace('upsample.2' , 'upsample.convolution_1' ) a : Tuple = 'upsample.' + name elif config.upsampler == "pixelshuffledirect": a : Optional[int] = name.replace('upsample.0.weight' , 'upsample.conv.weight' ) a : Dict = name.replace('upsample.0.bias' , 'upsample.conv.bias' ) else: pass else: a : int = 'swin2sr.' + name return name def SCREAMING_SNAKE_CASE__ ( snake_case : List[Any] , snake_case : List[str] ) -> Dict: """simple docstring""" for key in orig_state_dict.copy().keys(): a : List[Any] = orig_state_dict.pop(lowercase_ ) if "qkv" in key: a : str = key.split('.' ) a : Tuple = int(key_split[1] ) a : Optional[int] = int(key_split[4] ) a : int = config.embed_dim if "weight" in key: a : Dict = val[:dim, :] a : Any = val[dim : dim * 2, :] a : List[Any] = val[-dim:, :] else: a : List[Any] = val[:dim] a : Union[str, Any] = val[dim : dim * 2] a : Tuple = val[-dim:] pass else: a : Any = val return orig_state_dict def SCREAMING_SNAKE_CASE__ ( snake_case : Dict , snake_case : List[Any] , snake_case : Any ) -> Tuple: """simple docstring""" a : Dict = get_config(lowercase_ ) a : str = SwinaSRForImageSuperResolution(lowercase_ ) model.eval() a : Dict = torch.hub.load_state_dict_from_url(lowercase_ , map_location='cpu' ) a : Union[str, Any] = convert_state_dict(lowercase_ , lowercase_ ) a , a : str = model.load_state_dict(lowercase_ , strict=lowercase_ ) if len(lowercase_ ) > 0: raise ValueError('Missing keys when converting: {}'.format(lowercase_ ) ) for key in unexpected_keys: if not ("relative_position_index" in key or "relative_coords_table" in key or "self_mask" in key): raise ValueError(F"""Unexpected key {key} in state_dict""" ) # verify values a : Optional[int] = 'https://github.com/mv-lab/swin2sr/blob/main/testsets/real-inputs/shanghai.jpg?raw=true' a : List[str] = Image.open(requests.get(lowercase_ , stream=lowercase_ ).raw ).convert('RGB' ) a : Dict = SwinaSRImageProcessor() # pixel_values = processor(image, return_tensors="pt").pixel_values a : int = 126 if 'Jpeg' in checkpoint_url else 256 a : int = Compose( [ Resize((image_size, image_size) ), ToTensor(), Normalize(mean=[0.4_85, 0.4_56, 0.4_06] , std=[0.2_29, 0.2_24, 0.2_25] ), ] ) a : int = transforms(lowercase_ ).unsqueeze(0 ) if config.num_channels == 1: a : Tuple = pixel_values[:, 0, :, :].unsqueeze(1 ) a : Optional[Any] = model(lowercase_ ) # assert values if "Swin2SR_ClassicalSR_X2_64" in checkpoint_url: a : str = torch.Size([1, 3, 512, 512] ) a : Union[str, Any] = torch.tensor( [[-0.70_87, -0.71_38, -0.67_21], [-0.83_40, -0.80_95, -0.72_98], [-0.91_49, -0.84_14, -0.79_40]] ) elif "Swin2SR_ClassicalSR_X4_64" in checkpoint_url: a : List[str] = torch.Size([1, 3, 1_024, 1_024] ) a : Union[str, Any] = torch.tensor( [[-0.77_75, -0.81_05, -0.89_33], [-0.77_64, -0.83_56, -0.92_25], [-0.79_76, -0.86_86, -0.95_79]] ) elif "Swin2SR_CompressedSR_X4_48" in checkpoint_url: # TODO values didn't match exactly here a : str = torch.Size([1, 3, 1_024, 1_024] ) a : Optional[Any] = torch.tensor( [[-0.80_35, -0.75_04, -0.74_91], [-0.85_38, -0.81_24, -0.77_82], [-0.88_04, -0.86_51, -0.84_93]] ) elif "Swin2SR_Lightweight_X2_64" in checkpoint_url: a : List[Any] = torch.Size([1, 3, 512, 512] ) a : int = torch.tensor( [[-0.76_69, -0.86_62, -0.87_67], [-0.88_10, -0.99_62, -0.98_20], [-0.93_40, -1.03_22, -1.11_49]] ) elif "Swin2SR_RealworldSR_X4_64_BSRGAN_PSNR" in checkpoint_url: a : str = torch.Size([1, 3, 1_024, 1_024] ) a : List[Any] = torch.tensor( [[-0.52_38, -0.55_57, -0.63_21], [-0.60_16, -0.59_03, -0.63_91], [-0.62_44, -0.63_34, -0.68_89]] ) assert ( outputs.reconstruction.shape == expected_shape ), F"""Shape of reconstruction should be {expected_shape}, but is {outputs.reconstruction.shape}""" assert torch.allclose(outputs.reconstruction[0, 0, :3, :3] , lowercase_ , atol=1E-3 ) print('Looks ok!' ) a : List[Any] = { 'https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_ClassicalSR_X2_64.pth': ( 'swin2SR-classical-sr-x2-64' ), 'https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_ClassicalSR_X4_64.pth': ( 'swin2SR-classical-sr-x4-64' ), 'https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_CompressedSR_X4_48.pth': ( 'swin2SR-compressed-sr-x4-48' ), 'https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_Lightweight_X2_64.pth': ( 'swin2SR-lightweight-x2-64' ), 'https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_RealworldSR_X4_64_BSRGAN_PSNR.pth': ( 'swin2SR-realworld-sr-x4-64-bsrgan-psnr' ), } a : int = url_to_name[checkpoint_url] if pytorch_dump_folder_path is not None: print(F"""Saving model {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(lowercase_ ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) processor.save_pretrained(lowercase_ ) if push_to_hub: model.push_to_hub(F"""caidas/{model_name}""" ) processor.push_to_hub(F"""caidas/{model_name}""" ) if __name__ == "__main__": UpperCamelCase : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--checkpoint_url""", default="""https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_ClassicalSR_X2_64.pth""", type=str, help="""URL of the original Swin2SR checkpoint you\'d like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) parser.add_argument("""--push_to_hub""", action="""store_true""", help="""Whether to push the converted model to the hub.""") UpperCamelCase : List[str] = parser.parse_args() convert_swinasr_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub)
357
'''simple docstring''' def SCREAMING_SNAKE_CASE__ ( snake_case : int , snake_case : int ) -> bool: """simple docstring""" return numa ^ numa < 0 if __name__ == "__main__": import doctest doctest.testmod()
345
0
'''simple docstring''' import gc import random import unittest import numpy as np import torch from PIL import Image from diffusers import ( DDIMScheduler, KandinskyVaaImgaImgPipeline, 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 ( a_ , unittest.TestCase ): """simple docstring""" A : str = KandinskyVaaImgaImgPipeline A : List[str] = ["image_embeds", "negative_image_embeds", "image"] A : Optional[int] = [ "image_embeds", "negative_image_embeds", "image", ] A : Tuple = [ "generator", "height", "width", "strength", "guidance_scale", "num_inference_steps", "return_dict", "guidance_scale", "num_images_per_prompt", "output_type", "return_dict", ] A : Union[str, Any] = False @property def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" return 3_2 @property def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" return 3_2 @property def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" return self.time_input_dim @property def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" return self.time_input_dim * 4 @property def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" return 1_0_0 @property def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" torch.manual_seed(0) a : Tuple = { '''in_channels''': 4, # Out channels is double in channels because predicts mean and variance '''out_channels''': 8, '''addition_embed_type''': '''image''', '''down_block_types''': ('''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D'''), '''up_block_types''': ('''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''), '''mid_block_type''': '''UNetMidBlock2DSimpleCrossAttn''', '''block_out_channels''': (self.block_out_channels_a, self.block_out_channels_a * 2), '''layers_per_block''': 1, '''encoder_hid_dim''': self.text_embedder_hidden_size, '''encoder_hid_dim_type''': '''image_proj''', '''cross_attention_dim''': self.cross_attention_dim, '''attention_head_dim''': 4, '''resnet_time_scale_shift''': '''scale_shift''', '''class_embed_type''': None, } a : Optional[Any] = UNetaDConditionModel(**lowercase_) return model @property def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" return { "block_out_channels": [3_2, 6_4], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 1_2, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" torch.manual_seed(0) a : int = VQModel(**self.dummy_movq_kwargs) return model def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : Union[str, Any] = self.dummy_unet a : str = self.dummy_movq a : Any = { '''num_train_timesteps''': 1_0_0_0, '''beta_schedule''': '''linear''', '''beta_start''': 0.0_00_85, '''beta_end''': 0.0_12, '''clip_sample''': False, '''set_alpha_to_one''': False, '''steps_offset''': 0, '''prediction_type''': '''epsilon''', '''thresholding''': False, } a : List[str] = DDIMScheduler(**lowercase_) a : Optional[int] = { '''unet''': unet, '''scheduler''': scheduler, '''movq''': movq, } return components def SCREAMING_SNAKE_CASE_ ( self : Any , UpperCAmelCase_ : str , UpperCAmelCase_ : Union[str, Any]=0): """simple docstring""" a : Tuple = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(lowercase_)).to(lowercase_) a : List[str] = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(seed + 1)).to( lowercase_) # create init_image a : List[str] = floats_tensor((1, 3, 6_4, 6_4) , rng=random.Random(lowercase_)).to(lowercase_) a : str = image.cpu().permute(0 , 2 , 3 , 1)[0] a : str = Image.fromarray(np.uinta(lowercase_)).convert('RGB').resize((2_5_6, 2_5_6)) if str(lowercase_).startswith('mps'): a : int = torch.manual_seed(lowercase_) else: a : Any = torch.Generator(device=lowercase_).manual_seed(lowercase_) a : str = { '''image''': init_image, '''image_embeds''': image_embeds, '''negative_image_embeds''': negative_image_embeds, '''generator''': generator, '''height''': 6_4, '''width''': 6_4, '''num_inference_steps''': 1_0, '''guidance_scale''': 7.0, '''strength''': 0.2, '''output_type''': '''np''', } return inputs def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Optional[Any] = '''cpu''' a : Any = self.get_dummy_components() a : Any = self.pipeline_class(**lowercase_) a : Union[str, Any] = pipe.to(lowercase_) pipe.set_progress_bar_config(disable=lowercase_) a : Union[str, Any] = pipe(**self.get_dummy_inputs(lowercase_)) a : Dict = output.images a : Tuple = pipe( **self.get_dummy_inputs(lowercase_) , return_dict=lowercase_ , )[0] a : Dict = image[0, -3:, -3:, -1] a : List[Any] = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 6_4, 6_4, 3) a : str = np.array( [0.6_19_97_78, 0.63_98_44_06, 0.46_14_57_85, 0.62_94_49_84, 0.5_62_22_15, 0.47_30_61_32, 0.47_44_14_56, 0.4_60_76_06, 0.48_71_92_63]) assert ( np.abs(image_slice.flatten() - expected_slice).max() < 1e-2 ), f""" expected_slice {expected_slice}, but got {image_slice.flatten()}""" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice).max() < 1e-2 ), f""" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}""" @slow @require_torch_gpu class UpperCamelCase ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" a : Union[str, Any] = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinskyv22/kandinskyv22_img2img_frog.npy') a : Union[str, Any] = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinsky/cat.png') a : Optional[Any] = '''A red cartoon frog, 4k''' a : Union[str, Any] = KandinskyVaaPriorPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-2-prior' , torch_dtype=torch.floataa) pipe_prior.to(lowercase_) a : List[Any] = KandinskyVaaImgaImgPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-2-decoder' , torch_dtype=torch.floataa) a : List[Any] = pipeline.to(lowercase_) pipeline.set_progress_bar_config(disable=lowercase_) a : List[str] = torch.Generator(device='cpu').manual_seed(0) a : Tuple = pipe_prior( lowercase_ , generator=lowercase_ , num_inference_steps=5 , negative_prompt='' , ).to_tuple() a : Optional[Any] = pipeline( image=lowercase_ , image_embeds=lowercase_ , negative_image_embeds=lowercase_ , generator=lowercase_ , num_inference_steps=1_0_0 , height=7_6_8 , width=7_6_8 , strength=0.2 , output_type='np' , ) a : int = output.images[0] assert image.shape == (7_6_8, 7_6_8, 3) assert_mean_pixel_difference(lowercase_ , lowercase_)
358
'''simple docstring''' from typing import Dict, Iterable, 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, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, logging UpperCamelCase : int = logging.get_logger(__name__) class UpperCamelCase ( a_ ): """simple docstring""" A : Dict = ["pixel_values"] def __init__( self : Optional[Any] , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Dict[str, int] = None , UpperCAmelCase_ : PILImageResampling = PILImageResampling.BICUBIC , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Dict[str, int] = None , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Union[int, float] = 1 / 2_5_5 , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Optional[Union[float, Iterable[float]]] = IMAGENET_DEFAULT_MEAN , UpperCAmelCase_ : Optional[Union[float, Iterable[float]]] = IMAGENET_DEFAULT_STD , **UpperCAmelCase_ : List[Any] , ): """simple docstring""" super().__init__(**UpperCAmelCase_) a : List[str] = size if size is not None else {'shortest_edge': 2_2_4} a : str = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) a : str = crop_size if crop_size is not None else {'height': 2_2_4, 'width': 2_2_4} a : int = get_size_dict(UpperCAmelCase_ , param_name='crop_size') a : Any = do_resize a : Dict = size a : Optional[Any] = resample a : List[Any] = do_center_crop a : List[Any] = crop_size a : Optional[Any] = do_rescale a : Dict = rescale_factor a : Tuple = do_normalize a : int = image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN a : Optional[Any] = image_std if image_std is not None else IMAGENET_DEFAULT_STD def SCREAMING_SNAKE_CASE_ ( self : List[Any] , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Dict[str, int] , UpperCAmelCase_ : PILImageResampling = PILImageResampling.BICUBIC , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Any , ): """simple docstring""" a : Optional[Any] = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) # size_dict is a dict with either keys "height" and "width" or "shortest_edge" if "shortest_edge" in size: a : int = int((2_5_6 / 2_2_4) * size['shortest_edge']) a : Optional[int] = get_resize_output_image_size(UpperCAmelCase_ , size=UpperCAmelCase_ , default_to_square=UpperCAmelCase_) a : Optional[Any] = {'height': output_size[0], 'width': output_size[1]} if "height" not in size_dict or "width" not in size_dict: raise ValueError( f"""Size dict must have keys 'height' and 'width' or 'shortest_edge'. Got {size_dict.keys()}""") return resize( UpperCAmelCase_ , size=(size_dict['height'], size_dict['width']) , resample=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Dict[str, int] , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Union[str, Any] , ): """simple docstring""" a : str = get_size_dict(UpperCAmelCase_) if "height" not in size or "width" not in size: raise ValueError(f"""Size dict must have keys 'height' and 'width'. Got {size.keys()}""") return center_crop(UpperCAmelCase_ , size=(size['height'], size['width']) , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Union[int, float] , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Optional[Any] , ): """simple docstring""" return rescale(UpperCAmelCase_ , scale=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Union[float, List[float]] , UpperCAmelCase_ : Union[float, List[float]] , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : List[str] , ): """simple docstring""" return normalize(UpperCAmelCase_ , mean=UpperCAmelCase_ , std=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , UpperCAmelCase_ : ImageInput , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[Dict[str, int]] = None , UpperCAmelCase_ : PILImageResampling = None , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[Dict[str, int]] = None , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[float] = None , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[Union[float, Iterable[float]]] = None , UpperCAmelCase_ : Optional[Union[float, Iterable[float]]] = None , UpperCAmelCase_ : Optional[TensorType] = None , UpperCAmelCase_ : ChannelDimension = ChannelDimension.FIRST , **UpperCAmelCase_ : Optional[Any] , ): """simple docstring""" a : int = do_resize if do_resize is not None else self.do_resize a : Optional[int] = resample if resample is not None else self.resample a : Tuple = do_center_crop if do_center_crop is not None else self.do_center_crop a : Tuple = do_rescale if do_rescale is not None else self.do_rescale a : List[Any] = rescale_factor if rescale_factor is not None else self.rescale_factor a : Dict = do_normalize if do_normalize is not None else self.do_normalize a : Tuple = image_mean if image_mean is not None else self.image_mean a : int = image_std if image_std is not None else self.image_std a : Optional[int] = size if size is not None else self.size a : Optional[Any] = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) a : List[Any] = crop_size if crop_size is not None else self.crop_size a : str = get_size_dict(UpperCAmelCase_ , param_name='crop_size') a : Dict = make_list_of_images(UpperCAmelCase_) if not valid_images(UpperCAmelCase_): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.') if do_resize and size is None: raise ValueError('Size must be specified if do_resize is True.') if do_center_crop and crop_size is None: raise ValueError('Crop size must be specified if do_center_crop is True.') if do_rescale and rescale_factor is None: raise ValueError('Rescale factor must be specified if do_rescale is True.') if do_normalize and (image_mean is None or image_std is None): raise ValueError('Image mean and std must be specified if do_normalize is True.') # All transformations expect numpy arrays. a : Any = [to_numpy_array(UpperCAmelCase_) for image in images] if do_resize: a : Optional[int] = [self.resize(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_) for image in images] if do_center_crop: a : int = [self.center_crop(UpperCAmelCase_ , UpperCAmelCase_) for image in images] if do_rescale: a : Any = [self.rescale(UpperCAmelCase_ , UpperCAmelCase_) for image in images] if do_normalize: a : str = [self.normalize(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_) for image in images] a : Optional[int] = [to_channel_dimension_format(UpperCAmelCase_ , UpperCAmelCase_) for image in images] a : Optional[int] = {'pixel_values': images} return BatchFeature(data=UpperCAmelCase_ , tensor_type=UpperCAmelCase_)
345
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase : Union[str, Any] = logging.get_logger(__name__) UpperCamelCase : Union[str, Any] = { """weiweishi/roc-bert-base-zh""": """https://huggingface.co/weiweishi/roc-bert-base-zh/resolve/main/config.json""", } class UpperCamelCase ( a_ ): """simple docstring""" A : List[Any] = "roc_bert" def __init__( self : Dict , UpperCAmelCase_ : Optional[Any]=3_0_5_2_2 , UpperCAmelCase_ : Dict=7_6_8 , UpperCAmelCase_ : Optional[Any]=1_2 , UpperCAmelCase_ : int=1_2 , UpperCAmelCase_ : List[Any]=3_0_7_2 , UpperCAmelCase_ : int="gelu" , UpperCAmelCase_ : Optional[Any]=0.1 , UpperCAmelCase_ : Tuple=0.1 , UpperCAmelCase_ : int=5_1_2 , UpperCAmelCase_ : Optional[Any]=2 , UpperCAmelCase_ : Optional[Any]=0.02 , UpperCAmelCase_ : Any=1e-12 , UpperCAmelCase_ : Optional[Any]=True , UpperCAmelCase_ : int=0 , UpperCAmelCase_ : Union[str, Any]="absolute" , UpperCAmelCase_ : Tuple=None , UpperCAmelCase_ : Dict=True , UpperCAmelCase_ : List[str]=True , UpperCAmelCase_ : Union[str, Any]=7_6_8 , UpperCAmelCase_ : List[str]=9_1_0 , UpperCAmelCase_ : Any=5_1_2 , UpperCAmelCase_ : List[Any]=2_4_8_5_8 , UpperCAmelCase_ : int=True , **UpperCAmelCase_ : str , ): """simple docstring""" a : Dict = vocab_size a : Union[str, Any] = max_position_embeddings a : List[str] = hidden_size a : List[Any] = num_hidden_layers a : List[Any] = num_attention_heads a : Union[str, Any] = intermediate_size a : List[Any] = hidden_act a : Union[str, Any] = hidden_dropout_prob a : List[str] = attention_probs_dropout_prob a : int = initializer_range a : Optional[int] = type_vocab_size a : List[str] = layer_norm_eps a : Union[str, Any] = use_cache a : Optional[int] = enable_pronunciation a : Tuple = enable_shape a : Optional[Any] = pronunciation_embed_dim a : List[Any] = pronunciation_vocab_size a : int = shape_embed_dim a : Optional[Any] = shape_vocab_size a : Optional[int] = concat_input a : Union[str, Any] = position_embedding_type a : Optional[int] = classifier_dropout super().__init__(pad_token_id=__A , **__A)
359
'''simple docstring''' from __future__ import annotations from decimal import Decimal from math import * # noqa: F403 from sympy import diff def SCREAMING_SNAKE_CASE__ ( snake_case : str , snake_case : float | Decimal , snake_case : float = 10**-10 ) -> float: """simple docstring""" a : Dict = a while True: a : Any = Decimal(snake_case ) - ( Decimal(eval(snake_case ) ) / Decimal(eval(str(diff(snake_case ) ) ) ) # noqa: S307 ) # This number dictates the accuracy of the answer if abs(eval(snake_case ) ) < precision: # noqa: S307 return float(snake_case ) # Let's Execute if __name__ == "__main__": # Find root of trigonometric function # Find value of pi print(f'''The root of sin(x) = 0 is {newton_raphson("sin(x)", 2)}''') # Find root of polynomial print(f'''The root of x**2 - 5*x + 2 = 0 is {newton_raphson("x**2 - 5*x + 2", 0.4)}''') # Find Square Root of 5 print(f'''The root of log(x) - 1 = 0 is {newton_raphson("log(x) - 1", 2)}''') # Exponential Roots print(f'''The root of exp(x) - 1 = 0 is {newton_raphson("exp(x) - 1", 0)}''')
345
0
'''simple docstring''' import shutil import tempfile import unittest import numpy as np import pytest from transformers import is_speech_available, is_vision_available from transformers.testing_utils import require_torch if is_vision_available(): from transformers import TvltImageProcessor if is_speech_available(): from transformers import TvltFeatureExtractor from transformers import TvltProcessor @require_torch class UpperCamelCase ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" a : Tuple = """ZinengTang/tvlt-base""" a : Any = tempfile.mkdtemp() def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , **UpperCAmelCase_ : Optional[Any]): """simple docstring""" return TvltImageProcessor.from_pretrained(self.checkpoint , **_SCREAMING_SNAKE_CASE) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , **UpperCAmelCase_ : Union[str, Any]): """simple docstring""" return TvltFeatureExtractor.from_pretrained(self.checkpoint , **_SCREAMING_SNAKE_CASE) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" shutil.rmtree(self.tmpdirname) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : Dict = self.get_image_processor() a : Optional[Any] = self.get_feature_extractor() a : Optional[Any] = TvltProcessor(image_processor=_SCREAMING_SNAKE_CASE , feature_extractor=_SCREAMING_SNAKE_CASE) processor.save_pretrained(self.tmpdirname) a : List[str] = TvltProcessor.from_pretrained(self.tmpdirname) self.assertIsInstance(processor.feature_extractor , _SCREAMING_SNAKE_CASE) self.assertIsInstance(processor.image_processor , _SCREAMING_SNAKE_CASE) def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : Dict = self.get_image_processor() a : List[str] = self.get_feature_extractor() a : Union[str, Any] = TvltProcessor(image_processor=_SCREAMING_SNAKE_CASE , feature_extractor=_SCREAMING_SNAKE_CASE) a : int = np.ones([1_2_0_0_0]) a : Any = feature_extractor(_SCREAMING_SNAKE_CASE , return_tensors='np') a : Union[str, Any] = processor(audio=_SCREAMING_SNAKE_CASE , return_tensors='np') for key in audio_dict.keys(): self.assertAlmostEqual(audio_dict[key].sum() , input_processor[key].sum() , delta=1e-2) def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : List[str] = self.get_image_processor() a : str = self.get_feature_extractor() a : Optional[int] = TvltProcessor(image_processor=_SCREAMING_SNAKE_CASE , feature_extractor=_SCREAMING_SNAKE_CASE) a : Dict = np.ones([3, 2_2_4, 2_2_4]) a : Optional[int] = image_processor(_SCREAMING_SNAKE_CASE , return_tensors='np') a : List[str] = processor(images=_SCREAMING_SNAKE_CASE , return_tensors='np') for key in image_dict.keys(): self.assertAlmostEqual(image_dict[key].sum() , input_processor[key].sum() , delta=1e-2) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : List[str] = self.get_image_processor() a : Optional[Any] = self.get_feature_extractor() a : str = TvltProcessor(image_processor=_SCREAMING_SNAKE_CASE , feature_extractor=_SCREAMING_SNAKE_CASE) a : Tuple = np.ones([1_2_0_0_0]) a : Optional[Any] = np.ones([3, 2_2_4, 2_2_4]) a : Optional[int] = processor(audio=_SCREAMING_SNAKE_CASE , images=_SCREAMING_SNAKE_CASE) self.assertListEqual(list(inputs.keys()) , ['audio_values', 'audio_mask', 'pixel_values', 'pixel_mask']) # test if it raises when no input is passed with pytest.raises(_SCREAMING_SNAKE_CASE): processor() def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : str = self.get_image_processor() a : int = self.get_feature_extractor() a : Optional[Any] = TvltProcessor(image_processor=_SCREAMING_SNAKE_CASE , feature_extractor=_SCREAMING_SNAKE_CASE) self.assertListEqual( processor.model_input_names , image_processor.model_input_names + feature_extractor.model_input_names , msg='`processor` and `image_processor`+`feature_extractor` model input names do not match' , )
360
'''simple docstring''' import unittest from transformers import XLMConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( XLMForMultipleChoice, XLMForQuestionAnswering, XLMForQuestionAnsweringSimple, XLMForSequenceClassification, XLMForTokenClassification, XLMModel, XLMWithLMHeadModel, ) from transformers.models.xlm.modeling_xlm import XLM_PRETRAINED_MODEL_ARCHIVE_LIST class UpperCamelCase : """simple docstring""" def __init__( self : Union[str, Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Optional[int]=1_3 , UpperCAmelCase_ : Dict=7 , UpperCAmelCase_ : int=True , UpperCAmelCase_ : Tuple=True , UpperCAmelCase_ : Any=True , UpperCAmelCase_ : List[Any]=True , UpperCAmelCase_ : List[Any]=True , UpperCAmelCase_ : List[Any]=False , UpperCAmelCase_ : List[Any]=False , UpperCAmelCase_ : Union[str, Any]=False , UpperCAmelCase_ : Union[str, Any]=2 , UpperCAmelCase_ : Optional[Any]=9_9 , UpperCAmelCase_ : str=0 , UpperCAmelCase_ : int=3_2 , UpperCAmelCase_ : Tuple=5 , UpperCAmelCase_ : Any=4 , UpperCAmelCase_ : Tuple=0.1 , UpperCAmelCase_ : int=0.1 , UpperCAmelCase_ : Optional[int]=5_1_2 , UpperCAmelCase_ : str=2 , UpperCAmelCase_ : Any=0.02 , UpperCAmelCase_ : str=2 , UpperCAmelCase_ : Optional[int]=4 , UpperCAmelCase_ : int="last" , UpperCAmelCase_ : Union[str, Any]=True , UpperCAmelCase_ : Tuple=None , UpperCAmelCase_ : List[str]=0 , ): """simple docstring""" a : Tuple = parent a : Optional[Any] = batch_size a : Tuple = seq_length a : Union[str, Any] = is_training a : List[str] = use_input_lengths a : Union[str, Any] = use_token_type_ids a : Optional[int] = use_labels a : int = gelu_activation a : Dict = sinusoidal_embeddings a : Any = causal a : Optional[int] = asm a : int = n_langs a : List[str] = vocab_size a : List[str] = n_special a : List[str] = hidden_size a : Any = num_hidden_layers a : Union[str, Any] = num_attention_heads a : Optional[Any] = hidden_dropout_prob a : str = attention_probs_dropout_prob a : Dict = max_position_embeddings a : Union[str, Any] = type_sequence_label_size a : str = initializer_range a : List[Any] = num_labels a : Union[str, Any] = num_choices a : Optional[Any] = summary_type a : Optional[Any] = use_proj a : Optional[Any] = scope a : Dict = bos_token_id def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size) a : List[Any] = random_attention_mask([self.batch_size, self.seq_length]) a : Optional[int] = None if self.use_input_lengths: a : Optional[int] = ( ids_tensor([self.batch_size] , vocab_size=2) + self.seq_length - 2 ) # small variation of seq_length a : int = None if self.use_token_type_ids: a : str = ids_tensor([self.batch_size, self.seq_length] , self.n_langs) a : Optional[Any] = None a : Tuple = None a : Optional[Any] = None if self.use_labels: a : Optional[int] = ids_tensor([self.batch_size] , self.type_sequence_label_size) a : int = ids_tensor([self.batch_size, self.seq_length] , self.num_labels) a : Optional[Any] = ids_tensor([self.batch_size] , 2).float() a : Dict = ids_tensor([self.batch_size] , self.num_choices) a : Any = self.get_config() return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" return XLMConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , num_labels=self.num_labels , bos_token_id=self.bos_token_id , ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : str , UpperCAmelCase_ : Any , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : str , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[Any] , ): """simple docstring""" a : Any = XLMModel(config=UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : int = model(UpperCAmelCase_ , lengths=UpperCAmelCase_ , langs=UpperCAmelCase_) a : str = model(UpperCAmelCase_ , langs=UpperCAmelCase_) a : int = model(UpperCAmelCase_) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : str , UpperCAmelCase_ : int , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : Any , UpperCAmelCase_ : List[Any] , ): """simple docstring""" a : Optional[Any] = XLMWithLMHeadModel(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : List[str] = model(UpperCAmelCase_ , token_type_ids=UpperCAmelCase_ , labels=UpperCAmelCase_) self.parent.assertEqual(result.loss.shape , ()) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size)) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : Dict , UpperCAmelCase_ : int , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : str , UpperCAmelCase_ : Optional[Any] , ): """simple docstring""" a : Union[str, Any] = XLMForQuestionAnsweringSimple(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : List[str] = model(UpperCAmelCase_) a : Tuple = model(UpperCAmelCase_ , start_positions=UpperCAmelCase_ , end_positions=UpperCAmelCase_) a : Any = outputs self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length)) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length)) def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Any , UpperCAmelCase_ : Any , ): """simple docstring""" a : Any = XLMForQuestionAnswering(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : int = model(UpperCAmelCase_) a : Dict = model( UpperCAmelCase_ , start_positions=UpperCAmelCase_ , end_positions=UpperCAmelCase_ , cls_index=UpperCAmelCase_ , is_impossible=UpperCAmelCase_ , p_mask=UpperCAmelCase_ , ) a : int = model( UpperCAmelCase_ , start_positions=UpperCAmelCase_ , end_positions=UpperCAmelCase_ , cls_index=UpperCAmelCase_ , is_impossible=UpperCAmelCase_ , ) ((a) , ) : Union[str, Any] = result_with_labels.to_tuple() a : int = model(UpperCAmelCase_ , start_positions=UpperCAmelCase_ , end_positions=UpperCAmelCase_) ((a) , ) : Union[str, Any] = result_with_labels.to_tuple() self.parent.assertEqual(result_with_labels.loss.shape , ()) self.parent.assertEqual(result.start_top_log_probs.shape , (self.batch_size, model.config.start_n_top)) self.parent.assertEqual(result.start_top_index.shape , (self.batch_size, model.config.start_n_top)) self.parent.assertEqual( result.end_top_log_probs.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top)) self.parent.assertEqual( result.end_top_index.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top)) self.parent.assertEqual(result.cls_logits.shape , (self.batch_size,)) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : int , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : str , ): """simple docstring""" a : Dict = XLMForSequenceClassification(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Union[str, Any] = model(UpperCAmelCase_) a : Union[str, Any] = model(UpperCAmelCase_ , labels=UpperCAmelCase_) self.parent.assertEqual(result.loss.shape , ()) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , UpperCAmelCase_ : str , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : str , UpperCAmelCase_ : Any , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Any , ): """simple docstring""" a : Dict = self.num_labels a : int = XLMForTokenClassification(UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Optional[Any] = model(UpperCAmelCase_ , attention_mask=UpperCAmelCase_ , labels=UpperCAmelCase_) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels)) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : str , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Dict , UpperCAmelCase_ : str , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Dict , ): """simple docstring""" a : str = self.num_choices a : Dict = XLMForMultipleChoice(config=UpperCAmelCase_) model.to(UpperCAmelCase_) model.eval() a : Union[str, Any] = input_ids.unsqueeze(1).expand(-1 , self.num_choices , -1).contiguous() a : str = token_type_ids.unsqueeze(1).expand(-1 , self.num_choices , -1).contiguous() a : Optional[int] = input_mask.unsqueeze(1).expand(-1 , self.num_choices , -1).contiguous() a : Any = model( UpperCAmelCase_ , attention_mask=UpperCAmelCase_ , token_type_ids=UpperCAmelCase_ , labels=UpperCAmelCase_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices)) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any]): """simple docstring""" a : int = self.prepare_config_and_inputs() ( ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ) : Union[str, Any] = config_and_inputs a : Optional[int] = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'lengths': input_lengths} return config, inputs_dict @require_torch class UpperCamelCase ( a_ , a_ , a_ , unittest.TestCase ): """simple docstring""" A : int = ( ( XLMModel, XLMWithLMHeadModel, XLMForQuestionAnswering, XLMForSequenceClassification, XLMForQuestionAnsweringSimple, XLMForTokenClassification, XLMForMultipleChoice, ) if is_torch_available() else () ) A : List[str] = ( (XLMWithLMHeadModel,) if is_torch_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable A : Optional[Any] = ( { "feature-extraction": XLMModel, "fill-mask": XLMWithLMHeadModel, "question-answering": XLMForQuestionAnsweringSimple, "text-classification": XLMForSequenceClassification, "text-generation": XLMWithLMHeadModel, "token-classification": XLMForTokenClassification, "zero-shot": XLMForSequenceClassification, } if is_torch_available() else {} ) def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Any , UpperCAmelCase_ : Any): """simple docstring""" if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith('Fast') ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : int , UpperCAmelCase_ : Dict=False): """simple docstring""" a : List[Any] = super()._prepare_for_class(UpperCAmelCase_ , UpperCAmelCase_ , return_labels=UpperCAmelCase_) if return_labels: if model_class.__name__ == "XLMForQuestionAnswering": a : Optional[int] = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=UpperCAmelCase_) a : str = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=UpperCAmelCase_) return inputs_dict def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : List[Any] = XLMModelTester(self) a : Tuple = ConfigTester(self , config_class=UpperCAmelCase_ , emb_dim=3_7) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_model(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_lm_head(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_simple_qa(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_qa(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_sequence_classif(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_token_classif(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[str]): """simple docstring""" a : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_for_multiple_choice(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : int , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Any=False , UpperCAmelCase_ : Optional[Any]=1): """simple docstring""" self.assertIsInstance(UpperCAmelCase_ , UpperCAmelCase_) self.assertListEqual( [isinstance(UpperCAmelCase_ , UpperCAmelCase_) for iter_attentions in attentions] , [True] * len(UpperCAmelCase_)) self.assertEqual(len(UpperCAmelCase_) , (max_length - min_length) * num_beam_groups) for idx, iter_attentions in enumerate(UpperCAmelCase_): # adds PAD dummy token a : List[str] = min_length + idx + 1 a : Optional[Any] = min_length + idx + 1 a : Optional[int] = ( batch_size * num_beam_groups, config.num_attention_heads, tgt_len, src_len, ) # check attn size self.assertListEqual( [layer_attention.shape for layer_attention in iter_attentions] , [expected_shape] * len(UpperCAmelCase_)) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : str=False , UpperCAmelCase_ : Union[str, Any]=1): """simple docstring""" self.assertIsInstance(UpperCAmelCase_ , UpperCAmelCase_) self.assertListEqual( [isinstance(UpperCAmelCase_ , UpperCAmelCase_) for iter_hidden_states in hidden_states] , [True] * len(UpperCAmelCase_) , ) self.assertEqual(len(UpperCAmelCase_) , (max_length - min_length) * num_beam_groups) for idx, iter_hidden_states in enumerate(UpperCAmelCase_): # adds PAD dummy token a : int = min_length + idx + 1 a : Any = (batch_size * num_beam_groups, seq_len, config.hidden_size) # check hidden size self.assertListEqual( [layer_hidden_states.shape for layer_hidden_states in iter_hidden_states] , [expected_shape] * len(UpperCAmelCase_) , ) pass @slow def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a : Union[str, Any] = XLMModel.from_pretrained(UpperCAmelCase_) self.assertIsNotNone(UpperCAmelCase_) @require_torch class UpperCamelCase ( unittest.TestCase ): """simple docstring""" @slow def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" a : Dict = XLMWithLMHeadModel.from_pretrained('xlm-mlm-en-2048') model.to(UpperCAmelCase_) a : List[Any] = torch.tensor([[1_4, 4_4_7]] , dtype=torch.long , device=UpperCAmelCase_) # the president a : Dict = [ 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, ] # the president the president the president the president the president the president the president the president the president the president # TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference a : Optional[Any] = model.generate(UpperCAmelCase_ , do_sample=UpperCAmelCase_) self.assertListEqual(output_ids[0].cpu().numpy().tolist() , UpperCAmelCase_)
345
0
'''simple docstring''' class UpperCamelCase : """simple docstring""" def __init__( self : List[Any]): """simple docstring""" a : Optional[int] = 0 a : str = 0 a : Optional[int] = {} def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : Optional[Any]): """simple docstring""" if vertex not in self.adjacency: a : List[str] = {} self.num_vertices += 1 def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Any): """simple docstring""" self.add_vertex(lowerCamelCase_) self.add_vertex(lowerCamelCase_) if head == tail: return a : int = weight a : Optional[Any] = weight def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : List[str] = self.get_edges() for edge in edges: a , a , a : Union[str, Any] = edge edges.remove((tail, head, weight)) for i in range(len(lowerCamelCase_)): a : Union[str, Any] = list(edges[i]) edges.sort(key=lambda UpperCAmelCase_: e[2]) for i in range(len(lowerCamelCase_) - 1): if edges[i][2] >= edges[i + 1][2]: a : Dict = edges[i][2] + 1 for edge in edges: a , a , a : List[str] = edge a : Union[str, Any] = weight a : Any = weight def __str__( self : str): """simple docstring""" a : List[str] = '' for tail in self.adjacency: for head in self.adjacency[tail]: a : List[str] = self.adjacency[head][tail] string += f"""{head} -> {tail} == {weight}\n""" return string.rstrip('\n') def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : Union[str, Any] = [] for tail in self.adjacency: for head in self.adjacency[tail]: output.append((tail, head, self.adjacency[head][tail])) return output def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" return self.adjacency.keys() @staticmethod def SCREAMING_SNAKE_CASE_ ( UpperCAmelCase_ : str=None , UpperCAmelCase_ : Any=None): """simple docstring""" a : int = Graph() if vertices is None: a : str = [] if edges is None: a : int = [] for vertex in vertices: g.add_vertex(lowerCamelCase_) for edge in edges: g.add_edge(*lowerCamelCase_) return g class UpperCamelCase : """simple docstring""" def __init__( self : Optional[int]): """simple docstring""" a : List[str] = {} a : Tuple = {} def __len__( self : Union[str, Any]): """simple docstring""" return len(self.parent) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : int): """simple docstring""" if item in self.parent: return self.find(lowerCamelCase_) a : Any = item a : int = 0 return item def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : List[str]): """simple docstring""" if item not in self.parent: return self.make_set(lowerCamelCase_) if item != self.parent[item]: a : Tuple = self.find(self.parent[item]) return self.parent[item] def SCREAMING_SNAKE_CASE_ ( self : Any , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Dict): """simple docstring""" a : Optional[int] = self.find(lowerCamelCase_) a : List[Any] = self.find(lowerCamelCase_) if roota == roota: return roota if self.rank[roota] > self.rank[roota]: a : List[str] = roota return roota if self.rank[roota] < self.rank[roota]: a : List[str] = roota return roota if self.rank[roota] == self.rank[roota]: self.rank[roota] += 1 a : str = roota return roota return None @staticmethod def SCREAMING_SNAKE_CASE_ ( UpperCAmelCase_ : Dict): """simple docstring""" a : int = graph.num_vertices a : Tuple = Graph.UnionFind() a : int = [] while num_components > 1: a : Union[str, Any] = {} for vertex in graph.get_vertices(): a : int = -1 a : Union[str, Any] = graph.get_edges() for edge in edges: a , a , a : List[Any] = edge edges.remove((tail, head, weight)) for edge in edges: a , a , a : Optional[int] = edge a : Union[str, Any] = union_find.find(lowerCamelCase_) a : int = union_find.find(lowerCamelCase_) if seta != seta: if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: a : int = [head, tail, weight] if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: a : str = [head, tail, weight] for vertex in cheap_edge: if cheap_edge[vertex] != -1: a , a , a : List[Any] = cheap_edge[vertex] if union_find.find(lowerCamelCase_) != union_find.find(lowerCamelCase_): union_find.union(lowerCamelCase_ , lowerCamelCase_) mst_edges.append(cheap_edge[vertex]) a : Any = num_components - 1 a : str = Graph.build(edges=lowerCamelCase_) return mst
361
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, is_vision_available, ) UpperCamelCase : List[str] = {"""processing_layoutxlm""": ["""LayoutXLMProcessor"""]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Any = ["""LayoutXLMTokenizer"""] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Optional[int] = ["""LayoutXLMTokenizerFast"""] if TYPE_CHECKING: from .processing_layoutxlm import LayoutXLMProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutxlm import LayoutXLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutxlm_fast import LayoutXLMTokenizerFast else: import sys UpperCamelCase : Dict = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
345
0
'''simple docstring''' import importlib.metadata from typing import Union from packaging.version import Version, parse from .constants import STR_OPERATION_TO_FUNC UpperCamelCase : Dict = parse(importlib.metadata.version("""torch""")) def SCREAMING_SNAKE_CASE__ ( snake_case : List[str] , snake_case : Any , snake_case : Union[str, Any] ) -> str: """simple docstring""" if operation not in STR_OPERATION_TO_FUNC.keys(): raise ValueError(F"""`operation` must be one of {list(STR_OPERATION_TO_FUNC.keys() )}, received {operation}""" ) a : int = STR_OPERATION_TO_FUNC[operation] if isinstance(__lowerCAmelCase , __lowerCAmelCase ): a : int = parse(importlib.metadata.version(__lowerCAmelCase ) ) return operation(__lowerCAmelCase , parse(__lowerCAmelCase ) ) def SCREAMING_SNAKE_CASE__ ( snake_case : Any , snake_case : List[Any] ) -> Tuple: """simple docstring""" return compare_versions(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase )
362
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCamelCase : Tuple = { """configuration_pegasus_x""": ["""PEGASUS_X_PRETRAINED_CONFIG_ARCHIVE_MAP""", """PegasusXConfig"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Union[str, Any] = [ """PEGASUS_X_PRETRAINED_MODEL_ARCHIVE_LIST""", """PegasusXForConditionalGeneration""", """PegasusXModel""", """PegasusXPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_pegasus_x import PEGASUS_X_PRETRAINED_CONFIG_ARCHIVE_MAP, PegasusXConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_pegasus_x import ( PEGASUS_X_PRETRAINED_MODEL_ARCHIVE_LIST, PegasusXForConditionalGeneration, PegasusXModel, PegasusXPreTrainedModel, ) else: import sys UpperCamelCase : List[str] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
345
0
'''simple docstring''' from ..utils import DummyObject, requires_backends class UpperCamelCase ( metaclass=a__ ): """simple docstring""" A : Dict = ["""flax"""] def __init__( self : Tuple , *UpperCAmelCase_ : int , **UpperCAmelCase_ : Optional[Any]): """simple docstring""" requires_backends(self , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : Any , *UpperCAmelCase_ : Dict , **UpperCAmelCase_ : str): """simple docstring""" requires_backends(cls , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : Any , *UpperCAmelCase_ : Any , **UpperCAmelCase_ : Any): """simple docstring""" requires_backends(cls , ['flax']) class UpperCamelCase ( metaclass=a__ ): """simple docstring""" A : Tuple = ["""flax"""] def __init__( self : List[Any] , *UpperCAmelCase_ : Optional[int] , **UpperCAmelCase_ : Optional[Any]): """simple docstring""" requires_backends(self , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : Optional[int] , *UpperCAmelCase_ : str , **UpperCAmelCase_ : int): """simple docstring""" requires_backends(cls , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : Optional[Any] , *UpperCAmelCase_ : Optional[int] , **UpperCAmelCase_ : Union[str, Any]): """simple docstring""" requires_backends(cls , ['flax']) class UpperCamelCase ( metaclass=a__ ): """simple docstring""" A : List[str] = ["""flax"""] def __init__( self : List[Any] , *UpperCAmelCase_ : Optional[Any] , **UpperCAmelCase_ : Optional[int]): """simple docstring""" requires_backends(self , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : Optional[Any] , *UpperCAmelCase_ : List[Any] , **UpperCAmelCase_ : Union[str, Any]): """simple docstring""" requires_backends(cls , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : List[Any] , *UpperCAmelCase_ : Union[str, Any] , **UpperCAmelCase_ : Optional[Any]): """simple docstring""" requires_backends(cls , ['flax']) class UpperCamelCase ( metaclass=a__ ): """simple docstring""" A : Dict = ["""flax"""] def __init__( self : Dict , *UpperCAmelCase_ : Optional[Any] , **UpperCAmelCase_ : Tuple): """simple docstring""" requires_backends(self , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : List[Any] , *UpperCAmelCase_ : Optional[Any] , **UpperCAmelCase_ : Tuple): """simple docstring""" requires_backends(cls , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : Optional[int] , *UpperCAmelCase_ : int , **UpperCAmelCase_ : List[str]): """simple docstring""" requires_backends(cls , ['flax']) class UpperCamelCase ( metaclass=a__ ): """simple docstring""" A : Optional[Any] = ["""flax"""] def __init__( self : Dict , *UpperCAmelCase_ : str , **UpperCAmelCase_ : Optional[int]): """simple docstring""" requires_backends(self , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : Optional[Any] , *UpperCAmelCase_ : Union[str, Any] , **UpperCAmelCase_ : List[str]): """simple docstring""" requires_backends(cls , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : Tuple , *UpperCAmelCase_ : Tuple , **UpperCAmelCase_ : List[str]): """simple docstring""" requires_backends(cls , ['flax']) class UpperCamelCase ( metaclass=a__ ): """simple docstring""" A : int = ["""flax"""] def __init__( self : str , *UpperCAmelCase_ : int , **UpperCAmelCase_ : Union[str, Any]): """simple docstring""" requires_backends(self , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : Tuple , *UpperCAmelCase_ : Tuple , **UpperCAmelCase_ : Tuple): """simple docstring""" requires_backends(cls , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : str , *UpperCAmelCase_ : Optional[int] , **UpperCAmelCase_ : Dict): """simple docstring""" requires_backends(cls , ['flax']) class UpperCamelCase ( metaclass=a__ ): """simple docstring""" A : Optional[Any] = ["""flax"""] def __init__( self : Optional[Any] , *UpperCAmelCase_ : str , **UpperCAmelCase_ : Optional[Any]): """simple docstring""" requires_backends(self , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : Optional[int] , *UpperCAmelCase_ : Tuple , **UpperCAmelCase_ : List[Any]): """simple docstring""" requires_backends(cls , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : List[Any] , *UpperCAmelCase_ : Any , **UpperCAmelCase_ : Optional[Any]): """simple docstring""" requires_backends(cls , ['flax']) class UpperCamelCase ( metaclass=a__ ): """simple docstring""" A : Union[str, Any] = ["""flax"""] def __init__( self : Tuple , *UpperCAmelCase_ : Union[str, Any] , **UpperCAmelCase_ : Any): """simple docstring""" requires_backends(self , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : int , *UpperCAmelCase_ : Tuple , **UpperCAmelCase_ : Optional[Any]): """simple docstring""" requires_backends(cls , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : Union[str, Any] , *UpperCAmelCase_ : Optional[Any] , **UpperCAmelCase_ : List[Any]): """simple docstring""" requires_backends(cls , ['flax']) class UpperCamelCase ( metaclass=a__ ): """simple docstring""" A : Optional[Any] = ["""flax"""] def __init__( self : List[Any] , *UpperCAmelCase_ : str , **UpperCAmelCase_ : str): """simple docstring""" requires_backends(self , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : str , *UpperCAmelCase_ : Tuple , **UpperCAmelCase_ : int): """simple docstring""" requires_backends(cls , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : List[Any] , *UpperCAmelCase_ : str , **UpperCAmelCase_ : Optional[Any]): """simple docstring""" requires_backends(cls , ['flax']) class UpperCamelCase ( metaclass=a__ ): """simple docstring""" A : Any = ["""flax"""] def __init__( self : Tuple , *UpperCAmelCase_ : Optional[Any] , **UpperCAmelCase_ : str): """simple docstring""" requires_backends(self , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : str , *UpperCAmelCase_ : int , **UpperCAmelCase_ : Optional[int]): """simple docstring""" requires_backends(cls , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : List[Any] , *UpperCAmelCase_ : str , **UpperCAmelCase_ : List[Any]): """simple docstring""" requires_backends(cls , ['flax']) class UpperCamelCase ( metaclass=a__ ): """simple docstring""" A : int = ["""flax"""] def __init__( self : Dict , *UpperCAmelCase_ : Any , **UpperCAmelCase_ : Union[str, Any]): """simple docstring""" requires_backends(self , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : List[str] , *UpperCAmelCase_ : Tuple , **UpperCAmelCase_ : str): """simple docstring""" requires_backends(cls , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : Dict , *UpperCAmelCase_ : Dict , **UpperCAmelCase_ : Union[str, Any]): """simple docstring""" requires_backends(cls , ['flax']) class UpperCamelCase ( metaclass=a__ ): """simple docstring""" A : Optional[int] = ["""flax"""] def __init__( self : int , *UpperCAmelCase_ : int , **UpperCAmelCase_ : Union[str, Any]): """simple docstring""" requires_backends(self , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : List[str] , *UpperCAmelCase_ : int , **UpperCAmelCase_ : str): """simple docstring""" requires_backends(cls , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : Union[str, Any] , *UpperCAmelCase_ : str , **UpperCAmelCase_ : int): """simple docstring""" requires_backends(cls , ['flax']) class UpperCamelCase ( metaclass=a__ ): """simple docstring""" A : Dict = ["""flax"""] def __init__( self : Tuple , *UpperCAmelCase_ : List[str] , **UpperCAmelCase_ : Union[str, Any]): """simple docstring""" requires_backends(self , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : Optional[Any] , *UpperCAmelCase_ : Any , **UpperCAmelCase_ : Any): """simple docstring""" requires_backends(cls , ['flax']) @classmethod def SCREAMING_SNAKE_CASE_ ( cls : Union[str, Any] , *UpperCAmelCase_ : int , **UpperCAmelCase_ : List[Any]): """simple docstring""" requires_backends(cls , ['flax'])
363
'''simple docstring''' import math from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase : str = logging.get_logger(__name__) UpperCamelCase : List[str] = { """facebook/data2vec-base-960h""": """https://huggingface.co/facebook/data2vec-audio-base-960h/resolve/main/config.json""", # See all Data2VecAudio models at https://huggingface.co/models?filter=data2vec-audio } class UpperCamelCase ( a_ ): """simple docstring""" A : Optional[int] = "data2vec-audio" def __init__( self : Dict , UpperCAmelCase_ : Optional[int]=3_2 , UpperCAmelCase_ : Union[str, Any]=7_6_8 , UpperCAmelCase_ : Dict=1_2 , UpperCAmelCase_ : str=1_2 , UpperCAmelCase_ : Optional[Any]=3_0_7_2 , UpperCAmelCase_ : Dict="gelu" , UpperCAmelCase_ : Union[str, Any]=0.1 , UpperCAmelCase_ : Optional[int]=0.1 , UpperCAmelCase_ : int=0.1 , UpperCAmelCase_ : Union[str, Any]=0.0 , UpperCAmelCase_ : Union[str, Any]=0.1 , UpperCAmelCase_ : Dict=0.1 , UpperCAmelCase_ : Optional[Any]=0.02 , UpperCAmelCase_ : str=1e-5 , UpperCAmelCase_ : List[Any]="gelu" , UpperCAmelCase_ : Optional[Any]=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2) , UpperCAmelCase_ : Dict=(5, 2, 2, 2, 2, 2, 2) , UpperCAmelCase_ : int=(1_0, 3, 3, 3, 3, 2, 2) , UpperCAmelCase_ : List[Any]=False , UpperCAmelCase_ : List[Any]=1_6 , UpperCAmelCase_ : Optional[Any]=1_9 , UpperCAmelCase_ : int=5 , UpperCAmelCase_ : Any=0.05 , UpperCAmelCase_ : Dict=1_0 , UpperCAmelCase_ : Optional[Any]=2 , UpperCAmelCase_ : Optional[int]=0.0 , UpperCAmelCase_ : Tuple=1_0 , UpperCAmelCase_ : int=0 , UpperCAmelCase_ : Any="sum" , UpperCAmelCase_ : Optional[int]=False , UpperCAmelCase_ : Dict=False , UpperCAmelCase_ : Optional[int]=2_5_6 , UpperCAmelCase_ : Any=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0) , UpperCAmelCase_ : Optional[Any]=(5, 3, 3, 1, 1) , UpperCAmelCase_ : Optional[int]=(1, 2, 3, 1, 1) , UpperCAmelCase_ : int=5_1_2 , UpperCAmelCase_ : str=0 , UpperCAmelCase_ : Tuple=1 , UpperCAmelCase_ : Union[str, Any]=2 , UpperCAmelCase_ : List[str]=False , UpperCAmelCase_ : Any=3 , UpperCAmelCase_ : Dict=2 , UpperCAmelCase_ : int=3 , UpperCAmelCase_ : List[Any]=None , **UpperCAmelCase_ : int , ): """simple docstring""" super().__init__(**UpperCAmelCase_ , pad_token_id=UpperCAmelCase_ , bos_token_id=UpperCAmelCase_ , eos_token_id=UpperCAmelCase_) a : List[Any] = hidden_size a : Any = feat_extract_activation a : Any = list(UpperCAmelCase_) a : Optional[int] = list(UpperCAmelCase_) a : Dict = list(UpperCAmelCase_) a : Tuple = conv_bias a : str = num_conv_pos_embeddings a : Dict = num_conv_pos_embedding_groups a : Optional[Any] = conv_pos_kernel_size a : Any = len(self.conv_dim) a : Tuple = num_hidden_layers a : Any = intermediate_size a : Any = hidden_act a : Dict = num_attention_heads a : Dict = hidden_dropout a : Union[str, Any] = attention_dropout a : Dict = activation_dropout a : Optional[int] = feat_proj_dropout a : Tuple = final_dropout a : Union[str, Any] = layerdrop a : Tuple = layer_norm_eps a : Dict = initializer_range a : Tuple = vocab_size a : int = use_weighted_layer_sum if ( (len(self.conv_stride) != self.num_feat_extract_layers) or (len(self.conv_kernel) != self.num_feat_extract_layers) or (len(self.conv_dim) != self.num_feat_extract_layers) ): raise ValueError( 'Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==' ' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =' f""" {len(self.conv_dim)}`, `len(config.conv_stride) = {len(self.conv_stride)}`,""" f""" `len(config.conv_kernel) = {len(self.conv_kernel)}`.""") # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 a : List[str] = mask_time_prob a : int = mask_time_length a : Optional[int] = mask_time_min_masks a : Dict = mask_feature_prob a : List[str] = mask_feature_length a : str = mask_feature_min_masks # ctc loss a : str = ctc_loss_reduction a : Optional[Any] = ctc_zero_infinity # adapter a : List[str] = add_adapter a : Optional[Any] = adapter_kernel_size a : int = adapter_stride a : str = num_adapter_layers a : Optional[Any] = output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. a : str = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. a : List[Any] = list(UpperCAmelCase_) a : List[str] = list(UpperCAmelCase_) a : str = list(UpperCAmelCase_) a : Optional[Any] = xvector_output_dim @property def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" return math.prod(self.conv_stride)
345
0
'''simple docstring''' def SCREAMING_SNAKE_CASE__ ( snake_case : list ) -> list: """simple docstring""" def merge(snake_case : list , snake_case : list ) -> list: def _merge(): while left and right: yield (left if left[0] <= right[0] else right).pop(0 ) yield from left yield from right return list(_merge() ) if len(snake_case ) <= 1: return collection a : Dict = len(snake_case ) // 2 return merge(merge_sort(collection[:mid] ) , merge_sort(collection[mid:] ) ) if __name__ == "__main__": import doctest doctest.testmod() UpperCamelCase : int = input("""Enter numbers separated by a comma:\n""").strip() UpperCamelCase : Dict = [int(item) for item in user_input.split(""",""")] print(*merge_sort(unsorted), sep=""",""")
364
'''simple docstring''' import logging from transformers.configuration_utils import PretrainedConfig UpperCamelCase : Optional[Any] = logging.getLogger(__name__) class UpperCamelCase ( a_ ): """simple docstring""" A : Tuple = "masked_bert" def __init__( self : Tuple , UpperCAmelCase_ : List[Any]=3_0_5_2_2 , UpperCAmelCase_ : str=7_6_8 , UpperCAmelCase_ : Optional[Any]=1_2 , UpperCAmelCase_ : Optional[int]=1_2 , UpperCAmelCase_ : Union[str, Any]=3_0_7_2 , UpperCAmelCase_ : Union[str, Any]="gelu" , UpperCAmelCase_ : Optional[Any]=0.1 , UpperCAmelCase_ : List[Any]=0.1 , UpperCAmelCase_ : Optional[int]=5_1_2 , UpperCAmelCase_ : List[str]=2 , UpperCAmelCase_ : str=0.02 , UpperCAmelCase_ : Optional[Any]=1e-12 , UpperCAmelCase_ : Dict=0 , UpperCAmelCase_ : Dict="topK" , UpperCAmelCase_ : str="constant" , UpperCAmelCase_ : Optional[Any]=0.0 , **UpperCAmelCase_ : Optional[Any] , ): """simple docstring""" super().__init__(pad_token_id=UpperCAmelCase_ , **UpperCAmelCase_) a : Union[str, Any] = vocab_size a : List[Any] = hidden_size a : List[str] = num_hidden_layers a : Any = num_attention_heads a : Optional[Any] = hidden_act a : str = intermediate_size a : Dict = hidden_dropout_prob a : Any = attention_probs_dropout_prob a : Any = max_position_embeddings a : Dict = type_vocab_size a : List[str] = initializer_range a : int = layer_norm_eps a : Dict = pruning_method a : List[str] = mask_init a : Union[str, Any] = mask_scale
345
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase : int = logging.get_logger(__name__) UpperCamelCase : int = { """google/switch-base-8""": """https://huggingface.co/google/switch-base-8/blob/main/config.json""", } class UpperCamelCase ( __lowerCAmelCase ): """simple docstring""" A : Optional[Any] = '''switch_transformers''' A : str = ['''past_key_values'''] A : List[str] = {'''hidden_size''': '''d_model''', '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers'''} def __init__( self : Any , UpperCAmelCase_ : Optional[int]=3_2_1_2_8 , UpperCAmelCase_ : Dict=7_6_8 , UpperCAmelCase_ : List[Any]=6_4 , UpperCAmelCase_ : List[str]=2_0_4_8 , UpperCAmelCase_ : Any=6_4 , UpperCAmelCase_ : Dict=1_2 , UpperCAmelCase_ : Dict=3 , UpperCAmelCase_ : Dict=1_2 , UpperCAmelCase_ : List[Any]=3 , UpperCAmelCase_ : str=1_2 , UpperCAmelCase_ : Optional[Any]=8 , UpperCAmelCase_ : Union[str, Any]=False , UpperCAmelCase_ : Optional[Any]=0.01 , UpperCAmelCase_ : Optional[int]="float32" , UpperCAmelCase_ : Tuple=False , UpperCAmelCase_ : List[Any]=3_2 , UpperCAmelCase_ : Dict=1_2_8 , UpperCAmelCase_ : Tuple=0.1 , UpperCAmelCase_ : List[str]=1e-6 , UpperCAmelCase_ : Optional[int]=0.0_01 , UpperCAmelCase_ : int=0.0_01 , UpperCAmelCase_ : List[str]=1.0 , UpperCAmelCase_ : Tuple="relu" , UpperCAmelCase_ : Tuple=True , UpperCAmelCase_ : Any=False , UpperCAmelCase_ : List[Any]=True , UpperCAmelCase_ : int=0 , UpperCAmelCase_ : int=1 , **UpperCAmelCase_ : int , ): """simple docstring""" a : Any = vocab_size a : Any = d_model a : Dict = d_kv a : Optional[Any] = d_ff a : Dict = num_sparse_encoder_layers a : Optional[Any] = num_layers a : List[str] = ( num_decoder_layers if num_decoder_layers is not None else self.num_layers ) # default = symmetry a : Tuple = num_sparse_decoder_layers # This tells us, each how many encoder layer we'll have to set a sparse layer. if self.num_sparse_encoder_layers > 0: a : Optional[Any] = self.num_layers // self.num_sparse_encoder_layers else: a : Union[str, Any] = self.num_layers # HACK: this will create 0 sparse layers # This tells us, each how many encoder layer we'll have to set a sparse layer. if self.num_sparse_decoder_layers > 0: a : Optional[Any] = self.num_decoder_layers // self.num_sparse_decoder_layers else: a : List[Any] = self.num_decoder_layers # HACK: this will create 0 sparse layers a : Any = num_heads a : Tuple = num_experts a : Optional[Any] = expert_capacity a : int = router_bias a : Union[str, Any] = router_jitter_noise if router_dtype not in ["float32", "float16", "bfloat16"]: raise ValueError(f"""`router_dtype` must be one of \'float32\', \'float16\' or \'bfloat16\', got {router_dtype}""") a : Dict = router_dtype a : Optional[int] = router_ignore_padding_tokens a : Union[str, Any] = relative_attention_num_buckets a : List[str] = relative_attention_max_distance a : Tuple = dropout_rate a : Tuple = layer_norm_epsilon a : Any = initializer_factor a : Optional[int] = feed_forward_proj a : str = use_cache a : Optional[Any] = add_router_probs a : int = router_z_loss_coef a : Tuple = router_aux_loss_coef a : int = self.feed_forward_proj.split('-') a : int = act_info[-1] a : Any = act_info[0] == 'gated' if len(lowerCAmelCase_) > 1 and act_info[0] != "gated" or len(lowerCAmelCase_) > 2: raise ValueError( f"""`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer.""" 'Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. ' '\'gated-gelu\' or \'relu\'') # for backwards compatibility if feed_forward_proj == "gated-gelu": a : str = 'gelu_new' super().__init__( pad_token_id=lowerCAmelCase_ , eos_token_id=lowerCAmelCase_ , is_encoder_decoder=lowerCAmelCase_ , **lowerCAmelCase_ , )
365
'''simple docstring''' import copy import random from transformers import CLIPTokenizer class UpperCamelCase ( a_ ): """simple docstring""" def __init__( self : Union[str, Any] , *UpperCAmelCase_ : Dict , **UpperCAmelCase_ : Any): """simple docstring""" super().__init__(*UpperCAmelCase_ , **UpperCAmelCase_) a : str = {} def SCREAMING_SNAKE_CASE_ ( self : int , UpperCAmelCase_ : Tuple , *UpperCAmelCase_ : Optional[int] , **UpperCAmelCase_ : int): """simple docstring""" a : Dict = super().add_tokens(UpperCAmelCase_ , *UpperCAmelCase_ , **UpperCAmelCase_) if num_added_tokens == 0: raise ValueError( f"""The tokenizer already contains the token {placeholder_token}. Please pass a different""" ' `placeholder_token` that is not already in the tokenizer.') def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : Optional[int] , *UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : List[str]=1 , **UpperCAmelCase_ : Optional[int]): """simple docstring""" a : Any = [] if num_vec_per_token == 1: self.try_adding_tokens(UpperCAmelCase_ , *UpperCAmelCase_ , **UpperCAmelCase_) output.append(UpperCAmelCase_) else: a : int = [] for i in range(UpperCAmelCase_): a : Union[str, Any] = placeholder_token + f"""_{i}""" self.try_adding_tokens(UpperCAmelCase_ , *UpperCAmelCase_ , **UpperCAmelCase_) output.append(UpperCAmelCase_) # handle cases where there is a new placeholder token that contains the current placeholder token but is larger for token in self.token_map: if token in placeholder_token: raise ValueError( f"""The tokenizer already has placeholder token {token} that can get confused with""" f""" {placeholder_token}keep placeholder tokens independent""") a : Any = output def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Optional[int]=False , UpperCAmelCase_ : str=1.0): """simple docstring""" if isinstance(UpperCAmelCase_ , UpperCAmelCase_): a : Any = [] for i in range(len(UpperCAmelCase_)): output.append(self.replace_placeholder_tokens_in_text(text[i] , vector_shuffle=UpperCAmelCase_)) return output for placeholder_token in self.token_map: if placeholder_token in text: a : List[Any] = self.token_map[placeholder_token] a : int = tokens[: 1 + int(len(UpperCAmelCase_) * prop_tokens_to_load)] if vector_shuffle: a : List[Any] = copy.copy(UpperCAmelCase_) random.shuffle(UpperCAmelCase_) a : List[str] = text.replace(UpperCAmelCase_ , ' '.join(UpperCAmelCase_)) return text def __call__( self : Optional[int] , UpperCAmelCase_ : Any , *UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Union[str, Any]=False , UpperCAmelCase_ : Optional[int]=1.0 , **UpperCAmelCase_ : str): """simple docstring""" return super().__call__( self.replace_placeholder_tokens_in_text( UpperCAmelCase_ , vector_shuffle=UpperCAmelCase_ , prop_tokens_to_load=UpperCAmelCase_) , *UpperCAmelCase_ , **UpperCAmelCase_ , ) def SCREAMING_SNAKE_CASE_ ( self : Dict , UpperCAmelCase_ : Optional[int] , *UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Any=False , UpperCAmelCase_ : Optional[Any]=1.0 , **UpperCAmelCase_ : Dict): """simple docstring""" return super().encode( self.replace_placeholder_tokens_in_text( UpperCAmelCase_ , vector_shuffle=UpperCAmelCase_ , prop_tokens_to_load=UpperCAmelCase_) , *UpperCAmelCase_ , **UpperCAmelCase_ , )
345
0
'''simple docstring''' import random import unittest import numpy as np from diffusers import ( DPMSolverMultistepScheduler, EulerAncestralDiscreteScheduler, EulerDiscreteScheduler, LMSDiscreteScheduler, OnnxStableDiffusionImgaImgPipeline, PNDMScheduler, ) from diffusers.utils import floats_tensor from diffusers.utils.testing_utils import ( is_onnx_available, load_image, nightly, require_onnxruntime, require_torch_gpu, ) from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class UpperCamelCase ( _lowerCAmelCase , unittest.TestCase ): """simple docstring""" A : Union[str, Any] = "hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline" def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : List[str]=0): """simple docstring""" a : List[Any] = floats_tensor((1, 3, 1_2_8, 1_2_8) , rng=random.Random(SCREAMING_SNAKE_CASE_)) a : List[str] = np.random.RandomState(SCREAMING_SNAKE_CASE_) a : Optional[Any] = { """prompt""": """A painting of a squirrel eating a burger""", """image""": image, """generator""": generator, """num_inference_steps""": 3, """strength""": 0.75, """guidance_scale""": 7.5, """output_type""": """numpy""", } return inputs def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" a : List[str] = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider='CPUExecutionProvider') pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_) a : Dict = self.get_dummy_inputs() a : Dict = pipe(**SCREAMING_SNAKE_CASE_).images a : Union[str, Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 1_2_8, 1_2_8, 3) a : Tuple = np.array([0.6_96_43, 0.5_84_84, 0.5_03_14, 0.5_87_60, 0.5_53_68, 0.5_96_43, 0.5_15_29, 0.4_12_17, 0.4_90_87]) assert np.abs(image_slice - expected_slice).max() < 1e-1 def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" a : Optional[int] = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider='CPUExecutionProvider') a : str = PNDMScheduler.from_config(pipe.scheduler.config , skip_prk_steps=SCREAMING_SNAKE_CASE_) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_) a : int = self.get_dummy_inputs() a : Dict = pipe(**SCREAMING_SNAKE_CASE_).images a : int = image[0, -3:, -3:, -1] assert image.shape == (1, 1_2_8, 1_2_8, 3) a : List[Any] = np.array([0.6_17_37, 0.5_46_42, 0.5_31_83, 0.5_44_65, 0.5_27_42, 0.6_05_25, 0.4_99_69, 0.4_06_55, 0.4_81_54]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-1 def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" a : int = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider='CPUExecutionProvider') a : Dict = LMSDiscreteScheduler.from_config(pipe.scheduler.config) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_) # warmup pass to apply optimizations a : Tuple = pipe(**self.get_dummy_inputs()) a : Tuple = self.get_dummy_inputs() a : Optional[int] = pipe(**SCREAMING_SNAKE_CASE_).images a : int = image[0, -3:, -3:, -1] assert image.shape == (1, 1_2_8, 1_2_8, 3) a : List[Any] = np.array([0.5_27_61, 0.5_99_77, 0.4_90_33, 0.4_96_19, 0.5_42_82, 0.5_03_11, 0.4_76_00, 0.4_09_18, 0.4_52_03]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-1 def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : Optional[int] = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider='CPUExecutionProvider') a : str = EulerDiscreteScheduler.from_config(pipe.scheduler.config) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_) a : Any = self.get_dummy_inputs() a : str = pipe(**SCREAMING_SNAKE_CASE_).images a : str = image[0, -3:, -3:, -1] assert image.shape == (1, 1_2_8, 1_2_8, 3) a : List[Any] = np.array([0.5_29_11, 0.6_00_04, 0.4_92_29, 0.4_98_05, 0.5_45_02, 0.5_06_80, 0.4_77_77, 0.4_10_28, 0.4_53_04]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-1 def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" a : Optional[Any] = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider='CPUExecutionProvider') a : int = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_) a : int = self.get_dummy_inputs() a : Optional[Any] = pipe(**SCREAMING_SNAKE_CASE_).images a : Tuple = image[0, -3:, -3:, -1] assert image.shape == (1, 1_2_8, 1_2_8, 3) a : int = np.array([0.5_29_11, 0.6_00_04, 0.4_92_29, 0.4_98_05, 0.5_45_02, 0.5_06_80, 0.4_77_77, 0.4_10_28, 0.4_53_04]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-1 def SCREAMING_SNAKE_CASE_ ( self : Any): """simple docstring""" a : Dict = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider='CPUExecutionProvider') a : List[str] = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_) a : Union[str, Any] = self.get_dummy_inputs() a : Optional[int] = pipe(**SCREAMING_SNAKE_CASE_).images a : Dict = image[0, -3:, -3:, -1] assert image.shape == (1, 1_2_8, 1_2_8, 3) a : Optional[Any] = np.array([0.6_53_31, 0.5_82_77, 0.4_82_04, 0.5_60_59, 0.5_36_65, 0.5_62_35, 0.5_09_69, 0.4_00_09, 0.4_65_52]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-1 @nightly @require_onnxruntime @require_torch_gpu class UpperCamelCase ( unittest.TestCase ): """simple docstring""" @property def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" a : List[str] = ort.SessionOptions() a : Dict = False return options def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : int = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/img2img/sketch-mountains-input.jpg') a : Tuple = init_image.resize((7_6_8, 5_1_2)) # using the PNDM scheduler by default a : List[str] = OnnxStableDiffusionImgaImgPipeline.from_pretrained( 'CompVis/stable-diffusion-v1-4' , revision='onnx' , safety_checker=SCREAMING_SNAKE_CASE_ , feature_extractor=SCREAMING_SNAKE_CASE_ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_) a : str = """A fantasy landscape, trending on artstation""" a : Any = np.random.RandomState(0) a : List[Any] = pipe( prompt=SCREAMING_SNAKE_CASE_ , image=SCREAMING_SNAKE_CASE_ , strength=0.75 , guidance_scale=7.5 , num_inference_steps=1_0 , generator=SCREAMING_SNAKE_CASE_ , output_type='np' , ) a : int = output.images a : Optional[int] = images[0, 2_5_5:2_5_8, 3_8_3:3_8_6, -1] assert images.shape == (1, 5_1_2, 7_6_8, 3) a : Optional[int] = np.array([0.49_09, 0.50_59, 0.53_72, 0.46_23, 0.48_76, 0.50_49, 0.48_20, 0.49_56, 0.50_19]) # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues assert np.abs(image_slice.flatten() - expected_slice).max() < 2e-2 def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" a : str = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/img2img/sketch-mountains-input.jpg') a : Dict = init_image.resize((7_6_8, 5_1_2)) a : Optional[Any] = LMSDiscreteScheduler.from_pretrained( 'runwayml/stable-diffusion-v1-5' , subfolder='scheduler' , revision='onnx') a : List[Any] = OnnxStableDiffusionImgaImgPipeline.from_pretrained( 'runwayml/stable-diffusion-v1-5' , revision='onnx' , scheduler=SCREAMING_SNAKE_CASE_ , safety_checker=SCREAMING_SNAKE_CASE_ , feature_extractor=SCREAMING_SNAKE_CASE_ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_) a : Dict = """A fantasy landscape, trending on artstation""" a : Optional[Any] = np.random.RandomState(0) a : Optional[Any] = pipe( prompt=SCREAMING_SNAKE_CASE_ , image=SCREAMING_SNAKE_CASE_ , strength=0.75 , guidance_scale=7.5 , num_inference_steps=2_0 , generator=SCREAMING_SNAKE_CASE_ , output_type='np' , ) a : int = output.images a : List[str] = images[0, 2_5_5:2_5_8, 3_8_3:3_8_6, -1] assert images.shape == (1, 5_1_2, 7_6_8, 3) a : Optional[int] = np.array([0.80_43, 0.9_26, 0.95_81, 0.81_19, 0.89_54, 0.9_13, 0.72_09, 0.74_63, 0.74_31]) # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues assert np.abs(image_slice.flatten() - expected_slice).max() < 2e-2
366
'''simple docstring''' import argparse import json import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import AutoImageProcessor, SwinConfig, SwinForImageClassification def SCREAMING_SNAKE_CASE__ ( snake_case : str ) -> Optional[Any]: """simple docstring""" a : Union[str, Any] = SwinConfig() a : Optional[int] = swin_name.split('_' ) a : Union[str, Any] = name_split[1] a : Dict = int(name_split[4] ) a : Union[str, Any] = int(name_split[3][-1] ) if model_size == "tiny": a : Optional[Any] = 96 a : Any = (2, 2, 6, 2) a : List[str] = (3, 6, 12, 24) elif model_size == "small": a : int = 96 a : List[str] = (2, 2, 18, 2) a : int = (3, 6, 12, 24) elif model_size == "base": a : Tuple = 128 a : Optional[int] = (2, 2, 18, 2) a : List[Any] = (4, 8, 16, 32) else: a : Dict = 192 a : str = (2, 2, 18, 2) a : List[Any] = (6, 12, 24, 48) if "in22k" in swin_name: a : Any = 21_841 else: a : str = 1_000 a : str = 'huggingface/label-files' a : Optional[Any] = 'imagenet-1k-id2label.json' a : Dict = json.load(open(hf_hub_download(snake_case , snake_case , repo_type='dataset' ) , 'r' ) ) a : Tuple = {int(snake_case ): v for k, v in idalabel.items()} a : int = idalabel a : str = {v: k for k, v in idalabel.items()} a : Dict = img_size a : List[Any] = num_classes a : str = embed_dim a : Dict = depths a : Union[str, Any] = num_heads a : int = window_size return config def SCREAMING_SNAKE_CASE__ ( snake_case : List[Any] ) -> Optional[int]: """simple docstring""" if "patch_embed.proj" in name: a : int = name.replace('patch_embed.proj' , 'embeddings.patch_embeddings.projection' ) if "patch_embed.norm" in name: a : Tuple = name.replace('patch_embed.norm' , 'embeddings.norm' ) if "layers" in name: a : Optional[int] = 'encoder.' + name if "attn.proj" in name: a : List[Any] = name.replace('attn.proj' , 'attention.output.dense' ) if "attn" in name: a : Tuple = name.replace('attn' , 'attention.self' ) if "norm1" in name: a : Optional[int] = name.replace('norm1' , 'layernorm_before' ) if "norm2" in name: a : Dict = name.replace('norm2' , 'layernorm_after' ) if "mlp.fc1" in name: a : Union[str, Any] = name.replace('mlp.fc1' , 'intermediate.dense' ) if "mlp.fc2" in name: a : Any = name.replace('mlp.fc2' , 'output.dense' ) if name == "norm.weight": a : Union[str, Any] = 'layernorm.weight' if name == "norm.bias": a : List[str] = 'layernorm.bias' if "head" in name: a : Union[str, Any] = name.replace('head' , 'classifier' ) else: a : List[Any] = 'swin.' + name return name def SCREAMING_SNAKE_CASE__ ( snake_case : List[Any] , snake_case : Tuple ) -> List[str]: """simple docstring""" for key in orig_state_dict.copy().keys(): a : Any = orig_state_dict.pop(snake_case ) if "mask" in key: continue elif "qkv" in key: a : Optional[Any] = key.split('.' ) a : Dict = int(key_split[1] ) a : Optional[int] = int(key_split[3] ) a : Tuple = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: a : Optional[Any] = val[:dim, :] a : List[Any] = val[ dim : dim * 2, : ] a : List[Any] = val[-dim:, :] else: a : Dict = val[ :dim ] a : Union[str, Any] = val[ dim : dim * 2 ] a : Union[str, Any] = val[ -dim: ] else: a : Dict = val return orig_state_dict def SCREAMING_SNAKE_CASE__ ( snake_case : Optional[int] , snake_case : Dict ) -> List[str]: """simple docstring""" a : Any = timm.create_model(snake_case , pretrained=snake_case ) timm_model.eval() a : str = get_swin_config(snake_case ) a : Optional[int] = SwinForImageClassification(snake_case ) model.eval() a : Union[str, Any] = convert_state_dict(timm_model.state_dict() , snake_case ) model.load_state_dict(snake_case ) a : List[str] = 'http://images.cocodataset.org/val2017/000000039769.jpg' a : Optional[Any] = AutoImageProcessor.from_pretrained('microsoft/{}'.format(swin_name.replace('_' , '-' ) ) ) a : str = Image.open(requests.get(snake_case , stream=snake_case ).raw ) a : Union[str, Any] = image_processor(images=snake_case , return_tensors='pt' ) a : int = timm_model(inputs['pixel_values'] ) a : Optional[int] = model(**snake_case ).logits assert torch.allclose(snake_case , snake_case , atol=1E-3 ) print(F"""Saving model {swin_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(snake_case ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(snake_case ) if __name__ == "__main__": UpperCamelCase : int = argparse.ArgumentParser() # Required parameters parser.add_argument( """--swin_name""", default="""swin_tiny_patch4_window7_224""", type=str, help="""Name of the Swin timm model you'd like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) UpperCamelCase : Optional[Any] = parser.parse_args() convert_swin_checkpoint(args.swin_name, args.pytorch_dump_folder_path)
345
0
'''simple docstring''' import math import sys def SCREAMING_SNAKE_CASE__ ( snake_case : int ) -> str: """simple docstring""" a : List[str] = '''''' try: with open(SCREAMING_SNAKE_CASE__ , 'rb' ) as binary_file: a : List[Any] = binary_file.read() for dat in data: a : Optional[int] = F"""{dat:08b}""" result += curr_byte return result except OSError: print('File not accessible' ) sys.exit() def SCREAMING_SNAKE_CASE__ ( snake_case : Optional[int] ) -> str: """simple docstring""" a : List[Any] = {'''0''': '''0''', '''1''': '''1'''} a : int = '''''', '''''' a : Any = len(SCREAMING_SNAKE_CASE__ ) for i in range(len(SCREAMING_SNAKE_CASE__ ) ): curr_string += data_bits[i] if curr_string not in lexicon: continue a : Dict = lexicon[curr_string] result += last_match_id a : str = last_match_id + '''0''' if math.loga(SCREAMING_SNAKE_CASE__ ).is_integer(): a : Dict = {} for curr_key in list(SCREAMING_SNAKE_CASE__ ): a : Optional[int] = lexicon.pop(SCREAMING_SNAKE_CASE__ ) a : int = new_lex a : Union[str, Any] = last_match_id + '''1''' index += 1 a : Dict = '''''' return result def SCREAMING_SNAKE_CASE__ ( snake_case : Tuple , snake_case : str ) -> None: """simple docstring""" a : int = 8 try: with open(SCREAMING_SNAKE_CASE__ , 'wb' ) as opened_file: a : Optional[Any] = [ to_write[i : i + byte_length] for i in range(0 , len(SCREAMING_SNAKE_CASE__ ) , SCREAMING_SNAKE_CASE__ ) ] if len(result_byte_array[-1] ) % byte_length == 0: result_byte_array.append('10000000' ) else: result_byte_array[-1] += "1" + "0" * ( byte_length - len(result_byte_array[-1] ) - 1 ) for elem in result_byte_array[:-1]: opened_file.write(int(SCREAMING_SNAKE_CASE__ , 2 ).to_bytes(1 , byteorder='big' ) ) except OSError: print('File not accessible' ) sys.exit() def SCREAMING_SNAKE_CASE__ ( snake_case : int ) -> str: """simple docstring""" a : Optional[Any] = 0 for letter in data_bits: if letter == "1": break counter += 1 a : Dict = data_bits[counter:] a : Optional[Any] = data_bits[counter + 1 :] return data_bits def SCREAMING_SNAKE_CASE__ ( snake_case : Optional[Any] , snake_case : int ) -> None: """simple docstring""" a : Optional[Any] = read_file_binary(SCREAMING_SNAKE_CASE__ ) a : Optional[int] = remove_prefix(SCREAMING_SNAKE_CASE__ ) a : Tuple = decompress_data(SCREAMING_SNAKE_CASE__ ) write_file_binary(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": compress(sys.argv[1], sys.argv[2])
367
'''simple docstring''' import baseaa import io import json import os from copy import deepcopy from ..optimizer import AcceleratedOptimizer from ..scheduler import AcceleratedScheduler class UpperCamelCase : """simple docstring""" def __init__( self : List[str] , UpperCAmelCase_ : Tuple): """simple docstring""" if isinstance(UpperCAmelCase_ , UpperCAmelCase_): # Don't modify user's data should they want to reuse it (e.g. in tests), because once we # modified it, it will not be accepted here again, since `auto` values would have been overridden a : Dict = deepcopy(UpperCAmelCase_) elif os.path.exists(UpperCAmelCase_): with io.open(UpperCAmelCase_ , 'r' , encoding='utf-8') as f: a : Union[str, Any] = json.load(UpperCAmelCase_) else: try: a : Union[str, Any] = baseaa.urlsafe_baadecode(UpperCAmelCase_).decode('utf-8') a : List[str] = json.loads(UpperCAmelCase_) except (UnicodeDecodeError, AttributeError, ValueError): raise ValueError( f"""Expected a string path to an existing deepspeed config, or a dictionary, or a base64 encoded string. Received: {config_file_or_dict}""") a : Optional[int] = config self.set_stage_and_offload() def SCREAMING_SNAKE_CASE_ ( self : List[Any]): """simple docstring""" a : str = self.get_value('zero_optimization.stage' , -1) # offload a : Any = False if self.is_zeroa() or self.is_zeroa(): a : Tuple = set(['cpu', 'nvme']) a : int = set( [ self.get_value('zero_optimization.offload_optimizer.device'), self.get_value('zero_optimization.offload_param.device'), ]) if len(offload_devices & offload_devices_valid) > 0: a : List[str] = True def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , UpperCAmelCase_ : Dict): """simple docstring""" a : List[str] = self.config # find the config node of interest if it exists a : int = ds_key_long.split('.') a : Union[str, Any] = nodes.pop() for node in nodes: a : Union[str, Any] = config.get(UpperCAmelCase_) if config is None: return None, ds_key return config, ds_key def SCREAMING_SNAKE_CASE_ ( self : Any , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : int=None): """simple docstring""" a , a : int = self.find_config_node(UpperCAmelCase_) if config is None: return default return config.get(UpperCAmelCase_ , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : int , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Any=False): """simple docstring""" a : Any = self.config # find the config node of interest if it exists a : Optional[Any] = ds_key_long.split('.') for node in nodes: a : List[str] = config a : int = config.get(UpperCAmelCase_) if config is None: if must_exist: raise ValueError(f"""Can't find {ds_key_long} entry in the config: {self.config}""") else: return # if found remove it if parent_config is not None: parent_config.pop(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : str): """simple docstring""" a : List[str] = self.get_value(UpperCAmelCase_) return False if value is None else bool(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : Union[str, Any]): """simple docstring""" a : List[Any] = self.get_value(UpperCAmelCase_) return False if value is None else not bool(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" return self._stage == 2 def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any]): """simple docstring""" return self._stage == 3 def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" return self._offload class UpperCamelCase : """simple docstring""" def __init__( self : str , UpperCAmelCase_ : int): """simple docstring""" a : Union[str, Any] = engine def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : Any , **UpperCAmelCase_ : List[Any]): """simple docstring""" self.engine.backward(UpperCAmelCase_ , **UpperCAmelCase_) # Deepspeed's `engine.step` performs the following operations: # - gradient accumulation check # - gradient clipping # - optimizer step # - zero grad # - checking overflow # - lr_scheduler step (only if engine.lr_scheduler is not None) self.engine.step() # and this plugin overrides the above calls with no-ops when Accelerate runs under # Deepspeed, but allows normal functionality for non-Deepspeed cases thus enabling a simple # training loop that works transparently under many training regimes. class UpperCamelCase ( a_ ): """simple docstring""" def __init__( self : Union[str, Any] , UpperCAmelCase_ : Any): """simple docstring""" super().__init__(UpperCAmelCase_ , device_placement=UpperCAmelCase_ , scaler=UpperCAmelCase_) a : List[str] = hasattr(self.optimizer , 'overflow') def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : Dict=None): """simple docstring""" pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed def SCREAMING_SNAKE_CASE_ ( self : Tuple): """simple docstring""" pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed @property def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" if self.__has_overflow__: return self.optimizer.overflow return False class UpperCamelCase ( a_ ): """simple docstring""" def __init__( self : str , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Optional[Any]): """simple docstring""" super().__init__(UpperCAmelCase_ , UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : int): """simple docstring""" pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed class UpperCamelCase : """simple docstring""" def __init__( self : Union[str, Any] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Optional[int]=0.0_01 , UpperCAmelCase_ : List[Any]=0 , **UpperCAmelCase_ : Union[str, Any]): """simple docstring""" a : int = params a : str = lr a : Tuple = weight_decay a : Dict = kwargs class UpperCamelCase : """simple docstring""" def __init__( self : int , UpperCAmelCase_ : int , UpperCAmelCase_ : Any=None , UpperCAmelCase_ : Union[str, Any]=0 , **UpperCAmelCase_ : List[Any]): """simple docstring""" a : str = optimizer a : Tuple = total_num_steps a : Optional[Any] = warmup_num_steps a : List[str] = kwargs
345
0
'''simple docstring''' from statistics import mean import numpy as np def SCREAMING_SNAKE_CASE__ ( snake_case : int , snake_case : List[str] , snake_case : Optional[Any] , snake_case : Tuple ) -> list: """simple docstring""" a : Dict = 0 # Number of processes finished a : Optional[Any] = 0 # Displays the finished process. # If it is 0, the performance is completed if it is 1, before the performance. a : Optional[int] = [0] * no_of_process # List to include calculation results a : Tuple = [0] * no_of_process # Sort by arrival time. a : Union[str, Any] = [burst_time[i] for i in np.argsort(lowerCamelCase_ )] a : Any = [process_name[i] for i in np.argsort(lowerCamelCase_ )] arrival_time.sort() while no_of_process > finished_process_count: a : List[Any] = 0 while finished_process[i] == 1: i += 1 if current_time < arrival_time[i]: a : Union[str, Any] = arrival_time[i] a : List[str] = 0 # Index showing the location of the process being performed a : Dict = 0 # Saves the current response ratio. a : int = 0 for i in range(0 , lowerCamelCase_ ): if finished_process[i] == 0 and arrival_time[i] <= current_time: a : Tuple = (burst_time[i] + (current_time - arrival_time[i])) / burst_time[ i ] if response_ratio < temp: a : Any = temp a : List[str] = i # Calculate the turn around time a : Any = current_time + burst_time[loc] - arrival_time[loc] current_time += burst_time[loc] # Indicates that the process has been performed. a : str = 1 # Increase finished_process_count by 1 finished_process_count += 1 return turn_around_time def SCREAMING_SNAKE_CASE__ ( snake_case : List[str] , snake_case : Tuple , snake_case : Union[str, Any] , snake_case : int ) -> list: """simple docstring""" a : Any = [0] * no_of_process for i in range(0 , lowerCamelCase_ ): a : int = turn_around_time[i] - burst_time[i] return waiting_time if __name__ == "__main__": UpperCamelCase : int = 5 UpperCamelCase : List[str] = ["A", "B", "C", "D", "E"] UpperCamelCase : Union[str, Any] = [1, 2, 3, 4, 5] UpperCamelCase : str = [1, 2, 3, 4, 5] UpperCamelCase : List[str] = calculate_turn_around_time( process_name, arrival_time, burst_time, no_of_process ) UpperCamelCase : Union[str, Any] = calculate_waiting_time( process_name, turn_around_time, burst_time, no_of_process ) print("""Process name \tArrival time \tBurst time \tTurn around time \tWaiting time""") for i in range(0, no_of_process): print( f'''{process_name[i]}\t\t{arrival_time[i]}\t\t{burst_time[i]}\t\t''' f'''{turn_around_time[i]}\t\t\t{waiting_time[i]}''' ) print(f'''average waiting time : {mean(waiting_time):.5f}''') print(f'''average turn around time : {mean(turn_around_time):.5f}''')
368
'''simple docstring''' import os import time import warnings from dataclasses import dataclass, field from enum import Enum from typing import List, Optional, Union import torch from filelock import FileLock from torch.utils.data import Dataset from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import logging from ..processors.glue import glue_convert_examples_to_features, glue_output_modes, glue_processors from ..processors.utils import InputFeatures UpperCamelCase : List[str] = logging.get_logger(__name__) @dataclass class UpperCamelCase : """simple docstring""" A : str = field(metadata={"help": "The name of the task to train on: " + ", ".join(glue_processors.keys() )} ) A : str = field( metadata={"help": "The input data dir. Should contain the .tsv files (or other data files) for the task."} ) A : int = field( default=128 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) A : bool = field( default=a_ , metadata={"help": "Overwrite the cached training and evaluation sets"} ) def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" a : Union[str, Any] = self.task_name.lower() class UpperCamelCase ( a_ ): """simple docstring""" A : int = "train" A : Tuple = "dev" A : List[Any] = "test" class UpperCamelCase ( a_ ): """simple docstring""" A : GlueDataTrainingArguments A : str A : List[InputFeatures] def __init__( self : Tuple , UpperCAmelCase_ : GlueDataTrainingArguments , UpperCAmelCase_ : PreTrainedTokenizerBase , UpperCAmelCase_ : Optional[int] = None , UpperCAmelCase_ : Union[str, Split] = Split.train , UpperCAmelCase_ : Optional[str] = None , ): """simple docstring""" warnings.warn( 'This dataset will be removed from the library soon, preprocessing should be handled with the 🤗 Datasets ' 'library. You can have a look at this example script for pointers: ' 'https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py' , UpperCAmelCase_ , ) a : Dict = args a : int = glue_processors[args.task_name]() a : int = glue_output_modes[args.task_name] if isinstance(UpperCAmelCase_ , UpperCAmelCase_): try: a : str = Split[mode] except KeyError: raise KeyError('mode is not a valid split name') # Load data features from cache or dataset file a : List[str] = os.path.join( cache_dir if cache_dir is not None else args.data_dir , f"""cached_{mode.value}_{tokenizer.__class__.__name__}_{args.max_seq_length}_{args.task_name}""" , ) a : Tuple = self.processor.get_labels() if args.task_name in ["mnli", "mnli-mm"] and tokenizer.__class__.__name__ in ( "RobertaTokenizer", "RobertaTokenizerFast", "XLMRobertaTokenizer", "BartTokenizer", "BartTokenizerFast", ): # HACK(label indices are swapped in RoBERTa pretrained model) a , a : str = label_list[2], label_list[1] a : int = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. a : Union[str, Any] = cached_features_file + '.lock' with FileLock(UpperCAmelCase_): if os.path.exists(UpperCAmelCase_) and not args.overwrite_cache: a : Optional[Any] = time.time() a : Optional[Any] = torch.load(UpperCAmelCase_) logger.info( f"""Loading features from cached file {cached_features_file} [took %.3f s]""" , time.time() - start) else: logger.info(f"""Creating features from dataset file at {args.data_dir}""") if mode == Split.dev: a : List[Any] = self.processor.get_dev_examples(args.data_dir) elif mode == Split.test: a : Optional[Any] = self.processor.get_test_examples(args.data_dir) else: a : List[str] = self.processor.get_train_examples(args.data_dir) if limit_length is not None: a : Dict = examples[:limit_length] a : List[Any] = glue_convert_examples_to_features( UpperCAmelCase_ , UpperCAmelCase_ , max_length=args.max_seq_length , label_list=UpperCAmelCase_ , output_mode=self.output_mode , ) a : Dict = time.time() torch.save(self.features , UpperCAmelCase_) # ^ This seems to take a lot of time so I want to investigate why and how we can improve. logger.info( f"""Saving features into cached file {cached_features_file} [took {time.time() - start:.3f} s]""") def __len__( self : Tuple): """simple docstring""" return len(self.features) def __getitem__( self : Optional[int] , UpperCAmelCase_ : List[str]): """simple docstring""" return self.features[i] def SCREAMING_SNAKE_CASE_ ( self : str): """simple docstring""" return self.label_list
345
0
'''simple docstring''' from itertools import product from cva import COLOR_BGR2GRAY, cvtColor, imread, imshow, waitKey from numpy import dot, exp, mgrid, pi, ravel, square, uinta, zeros def SCREAMING_SNAKE_CASE__ ( snake_case : str , snake_case : str ) -> Union[str, Any]: """simple docstring""" a : Union[str, Any] = k_size // 2 a , a : Union[str, Any] = mgrid[0 - center : k_size - center, 0 - center : k_size - center] a : List[Any] = 1 / (2 * pi * sigma) * exp(-(square(__UpperCamelCase ) + square(__UpperCamelCase )) / (2 * square(__UpperCamelCase )) ) return g def SCREAMING_SNAKE_CASE__ ( snake_case : Any , snake_case : Any , snake_case : Optional[int] ) -> Tuple: """simple docstring""" a , a : Optional[int] = image.shape[0], image.shape[1] # dst image height and width a : Union[str, Any] = height - k_size + 1 a : str = width - k_size + 1 # im2col, turn the k_size*k_size pixels into a row and np.vstack all rows a : List[Any] = zeros((dst_height * dst_width, k_size * k_size) ) a : Tuple = 0 for i, j in product(range(__UpperCamelCase ) , range(__UpperCamelCase ) ): a : int = ravel(image[i : i + k_size, j : j + k_size] ) a : Union[str, Any] = window row += 1 # turn the kernel into shape(k*k, 1) a : Dict = gen_gaussian_kernel(__UpperCamelCase , __UpperCamelCase ) a : List[str] = ravel(__UpperCamelCase ) # reshape and get the dst image a : Union[str, Any] = dot(__UpperCamelCase , __UpperCamelCase ).reshape(__UpperCamelCase , __UpperCamelCase ).astype(__UpperCamelCase ) return dst if __name__ == "__main__": # read original image UpperCamelCase : Optional[Any] = imread(R"""../image_data/lena.jpg""") # turn image in gray scale value UpperCamelCase : Optional[int] = cvtColor(img, COLOR_BGR2GRAY) # get values with two different mask size UpperCamelCase : Union[str, Any] = gaussian_filter(gray, 3, sigma=1) UpperCamelCase : Tuple = gaussian_filter(gray, 5, sigma=0.8) # show result images imshow("""gaussian filter with 3x3 mask""", gaussianaxa) imshow("""gaussian filter with 5x5 mask""", gaussianaxa) waitKey()
369
'''simple docstring''' from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, logging if is_torch_available(): import torch UpperCamelCase : Dict = logging.get_logger(__name__) class UpperCamelCase ( a_ ): """simple docstring""" A : Any = ["pixel_values"] def __init__( self : str , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Optional[Dict[str, int]] = None , UpperCAmelCase_ : PILImageResampling = PILImageResampling.BILINEAR , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Dict[str, int] = None , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Union[int, float] = 1 / 2_5_5 , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Optional[Union[float, List[float]]] = None , UpperCAmelCase_ : Optional[Union[float, List[float]]] = None , **UpperCAmelCase_ : List[str] , ): """simple docstring""" super().__init__(**UpperCAmelCase_) a : str = size if size is not None else {'shortest_edge': 2_5_6} a : Dict = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) a : str = crop_size if crop_size is not None else {'height': 2_2_4, 'width': 2_2_4} a : int = get_size_dict(UpperCAmelCase_ , param_name='crop_size') a : Any = do_resize a : List[str] = size a : Union[str, Any] = resample a : int = do_center_crop a : Optional[int] = crop_size a : Tuple = do_rescale a : int = rescale_factor a : Optional[Any] = do_normalize a : Dict = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN a : Any = image_std if image_std is not None else IMAGENET_STANDARD_STD def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Dict[str, int] , UpperCAmelCase_ : PILImageResampling = PILImageResampling.BICUBIC , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Optional[int] , ): """simple docstring""" a : Optional[int] = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) if "shortest_edge" not in size: raise ValueError(f"""The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}""") a : Union[str, Any] = get_resize_output_image_size(UpperCAmelCase_ , size=size['shortest_edge'] , default_to_square=UpperCAmelCase_) return resize(UpperCAmelCase_ , size=UpperCAmelCase_ , resample=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Dict[str, int] , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Optional[int] , ): """simple docstring""" a : List[str] = get_size_dict(UpperCAmelCase_) if "height" not in size or "width" not in size: raise ValueError(f"""The `size` parameter must contain the keys `height` and `width`. Got {size.keys()}""") return center_crop(UpperCAmelCase_ , size=(size['height'], size['width']) , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : float , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Optional[Any]): """simple docstring""" return rescale(UpperCAmelCase_ , scale=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : Tuple , UpperCAmelCase_ : np.ndarray , UpperCAmelCase_ : Union[float, List[float]] , UpperCAmelCase_ : Union[float, List[float]] , UpperCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase_ : Optional[int] , ): """simple docstring""" return normalize(UpperCAmelCase_ , mean=UpperCAmelCase_ , std=UpperCAmelCase_ , data_format=UpperCAmelCase_ , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : ImageInput , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Dict[str, int] = None , UpperCAmelCase_ : PILImageResampling = None , UpperCAmelCase_ : bool = None , UpperCAmelCase_ : Dict[str, int] = None , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[float] = None , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[Union[float, List[float]]] = None , UpperCAmelCase_ : Optional[Union[float, List[float]]] = None , UpperCAmelCase_ : Optional[Union[str, TensorType]] = None , UpperCAmelCase_ : Union[str, ChannelDimension] = ChannelDimension.FIRST , **UpperCAmelCase_ : List[str] , ): """simple docstring""" a : int = do_resize if do_resize is not None else self.do_resize a : int = size if size is not None else self.size a : Union[str, Any] = get_size_dict(UpperCAmelCase_ , default_to_square=UpperCAmelCase_) a : str = resample if resample is not None else self.resample a : Optional[Any] = do_center_crop if do_center_crop is not None else self.do_center_crop a : Union[str, Any] = crop_size if crop_size is not None else self.crop_size a : Dict = get_size_dict(UpperCAmelCase_ , param_name='crop_size') a : str = do_rescale if do_rescale is not None else self.do_rescale a : int = rescale_factor if rescale_factor is not None else self.rescale_factor a : str = do_normalize if do_normalize is not None else self.do_normalize a : List[str] = image_mean if image_mean is not None else self.image_mean a : Optional[int] = image_std if image_std is not None else self.image_std a : Dict = make_list_of_images(UpperCAmelCase_) if not valid_images(UpperCAmelCase_): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.') if do_resize and size is None: raise ValueError('Size must be specified if do_resize is True.') if do_center_crop and crop_size is None: raise ValueError('Crop size must be specified if do_center_crop is True.') if do_rescale and rescale_factor is None: raise ValueError('Rescale factor must be specified if do_rescale is True.') if do_normalize and (image_mean is None or image_std is None): raise ValueError('Image mean and std must be specified if do_normalize is True.') # All transformations expect numpy arrays. a : List[Any] = [to_numpy_array(UpperCAmelCase_) for image in images] if do_resize: a : Dict = [self.resize(image=UpperCAmelCase_ , size=UpperCAmelCase_ , resample=UpperCAmelCase_) for image in images] if do_center_crop: a : Any = [self.center_crop(image=UpperCAmelCase_ , size=UpperCAmelCase_) for image in images] if do_rescale: a : Optional[int] = [self.rescale(image=UpperCAmelCase_ , scale=UpperCAmelCase_) for image in images] if do_normalize: a : Dict = [self.normalize(image=UpperCAmelCase_ , mean=UpperCAmelCase_ , std=UpperCAmelCase_) for image in images] a : List[Any] = [to_channel_dimension_format(UpperCAmelCase_ , UpperCAmelCase_) for image in images] a : List[str] = {'pixel_values': images} return BatchFeature(data=UpperCAmelCase_ , tensor_type=UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ ( self : List[str] , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : List[Tuple] = None): """simple docstring""" a : Dict = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(UpperCAmelCase_) != len(UpperCAmelCase_): raise ValueError( 'Make sure that you pass in as many target sizes as the batch dimension of the logits') if is_torch_tensor(UpperCAmelCase_): a : Optional[Any] = target_sizes.numpy() a : List[str] = [] for idx in range(len(UpperCAmelCase_)): a : Optional[Any] = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0) , size=target_sizes[idx] , mode='bilinear' , align_corners=UpperCAmelCase_) a : Union[str, Any] = resized_logits[0].argmax(dim=0) semantic_segmentation.append(UpperCAmelCase_) else: a : Optional[int] = logits.argmax(dim=1) a : List[str] = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0])] return semantic_segmentation
345
0
'''simple docstring''' from __future__ import annotations import os from typing import Any import requests UpperCamelCase : Dict = 'https://api.github.com' # https://docs.github.com/en/free-pro-team@latest/rest/reference/users#get-the-authenticated-user UpperCamelCase : Optional[int] = BASE_URL + '/user' # https://github.com/settings/tokens UpperCamelCase : Union[str, Any] = os.environ.get("""USER_TOKEN""", """""") def SCREAMING_SNAKE_CASE__ ( snake_case : str ) -> str: """simple docstring""" a : Any = { 'Authorization': F"""token {auth_token}""", 'Accept': 'application/vnd.github.v3+json', } return requests.get(snake_case_ , headers=snake_case_ ).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.""")
370
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE__ ( snake_case : int | float | str , snake_case : int | float | str ) -> list[str]: """simple docstring""" if nth_term == "": return [""] a : Dict = int(snake_case ) a : Optional[int] = int(snake_case ) a : list[str] = [] for temp in range(int(snake_case ) ): series.append(F"""1 / {pow(temp + 1 , int(snake_case ) )}""" if series else '1' ) return series if __name__ == "__main__": import doctest doctest.testmod() UpperCamelCase : Optional[int] = int(input("""Enter the last number (nth term) of the P-Series""")) UpperCamelCase : List[Any] = int(input("""Enter the power for P-Series""")) print("""Formula of P-Series => 1+1/2^p+1/3^p ..... 1/n^p""") print(p_series(nth_term, power))
345
0
'''simple docstring''' from __future__ import annotations import requests UpperCamelCase : str = set( """approved_at_utc approved_by author_flair_background_color\nauthor_flair_css_class author_flair_richtext author_flair_template_id author_fullname\nauthor_premium can_mod_post category clicked content_categories created_utc downs\nedited gilded gildings hidden hide_score is_created_from_ads_ui is_meta\nis_original_content is_reddit_media_domain is_video link_flair_css_class\nlink_flair_richtext link_flair_text link_flair_text_color media_embed mod_reason_title\nname permalink pwls quarantine saved score secure_media secure_media_embed selftext\nsubreddit subreddit_name_prefixed subreddit_type thumbnail title top_awarded_type\ntotal_awards_received ups upvote_ratio url user_reports""".split() ) def SCREAMING_SNAKE_CASE__ ( snake_case : str , snake_case : int = 1 , snake_case : str = "new" , snake_case : list | None = None ) -> List[str]: """simple docstring""" a : List[str] = wanted_data or [] if invalid_search_terms := ", ".join(sorted(set(a__ ) - valid_terms ) ): a : Optional[Any] = F"""Invalid search term: {invalid_search_terms}""" raise ValueError(a__ ) a : Any = requests.get( F"""https://reddit.com/r/{subreddit}/{age}.json?limit={limit}""" , headers={'User-agent': 'A random string'} , ) if response.status_code == 429: raise requests.HTTPError a : str = response.json() if not wanted_data: return {id_: data["data"]["children"][id_] for id_ in range(a__ )} a : List[Any] = {} for id_ in range(a__ ): a : Any = { item: data['data']['children'][id_]['data'][item] for item in wanted_data } return data_dict if __name__ == "__main__": # If you get Error 429, that means you are rate limited.Try after some time print(get_subreddit_data("""learnpython""", wanted_data=["""title""", """url""", """selftext"""]))
371
'''simple docstring''' import torch def SCREAMING_SNAKE_CASE__ ( ) -> str: """simple docstring""" if torch.cuda.is_available(): a : int = torch.cuda.device_count() else: a : Any = 0 print(F"""Successfully ran on {num_gpus} GPUs""" ) if __name__ == "__main__": main()
345
0
'''simple docstring''' from __future__ import annotations import math def SCREAMING_SNAKE_CASE__ ( snake_case : Optional[Any] , snake_case : List[str] ) -> List[Any]: """simple docstring""" a : Optional[int] = u for i in range(1 , __a ): a : Optional[Any] = temp * (u - i) return temp def SCREAMING_SNAKE_CASE__ ( ) -> Tuple: """simple docstring""" a : Dict = int(input('enter the numbers of values: ' ) ) a : list[list[float]] = [] for _ in range(__a ): y.append([] ) for i in range(__a ): for j in range(__a ): y[i].append(__a ) a : str = 0 print('enter the values of parameters in a list: ' ) a : int = list(map(__a , input().split() ) ) print('enter the values of corresponding parameters: ' ) for i in range(__a ): a : Union[str, Any] = float(input() ) a : int = int(input('enter the value to interpolate: ' ) ) a : List[Any] = (value - x[0]) / (x[1] - x[0]) # for calculating forward difference table for i in range(1 , __a ): for j in range(n - i ): a : int = y[j + 1][i - 1] - y[j][i - 1] a : str = y[0][0] for i in range(1 , __a ): summ += (ucal(__a , __a ) * y[0][i]) / math.factorial(__a ) print(F"""the value at {value} is {summ}""" ) if __name__ == "__main__": main()
350
'''simple docstring''' import faiss # noqa: F401 # Here to have a nice missing dependency error message early on import numpy # noqa: F401 # Here to have a nice missing dependency error message early on import requests # noqa: F401 # Here to have a nice missing dependency error message early on import sklearn # noqa: F401 # Here to have a nice missing dependency error message early on import tqdm # noqa: F401 # Here to have a nice missing dependency error message early on from mauve import compute_mauve # From: mauve-text import datasets UpperCamelCase : Optional[int] = """\ @inproceedings{pillutla-etal:mauve:neurips2021, title={MAUVE: Measuring the Gap Between Neural Text and Human Text using Divergence Frontiers}, author={Pillutla, Krishna and Swayamdipta, Swabha and Zellers, Rowan and Thickstun, John and Welleck, Sean and Choi, Yejin and Harchaoui, Zaid}, booktitle = {NeurIPS}, year = {2021} } """ UpperCamelCase : Optional[Any] = """\ MAUVE is a library built on PyTorch and HuggingFace Transformers to measure the gap between neural text and human text with the eponymous MAUVE measure. MAUVE summarizes both Type I and Type II errors measured softly using Kullback–Leibler (KL) divergences. For details, see the MAUVE paper: https://arxiv.org/abs/2102.01454 (Neurips, 2021). This metrics is a wrapper around the official implementation of MAUVE: https://github.com/krishnap25/mauve """ UpperCamelCase : str = """ Calculates MAUVE scores between two lists of generated text and reference text. Args: predictions: list of generated text to score. Each predictions should be a string with tokens separated by spaces. references: list of reference for each prediction. Each reference should be a string with tokens separated by spaces. Optional Args: num_buckets: the size of the histogram to quantize P and Q. Options: 'auto' (default) or an integer pca_max_data: the number data points to use for PCA dimensionality reduction prior to clustering. If -1, use all the data. Default -1 kmeans_explained_var: amount of variance of the data to keep in dimensionality reduction by PCA. Default 0.9 kmeans_num_redo: number of times to redo k-means clustering (the best objective is kept). Default 5 kmeans_max_iter: maximum number of k-means iterations. Default 500 featurize_model_name: name of the model from which features are obtained. Default 'gpt2-large' Use one of ['gpt2', 'gpt2-medium', 'gpt2-large', 'gpt2-xl']. device_id: Device for featurization. Supply a GPU id (e.g. 0 or 3) to use GPU. If no GPU with this id is found, use CPU max_text_length: maximum number of tokens to consider. Default 1024 divergence_curve_discretization_size: Number of points to consider on the divergence curve. Default 25 mauve_scaling_factor: \"c\" from the paper. Default 5. verbose: If True (default), print running time updates seed: random seed to initialize k-means cluster assignments. Returns: mauve: MAUVE score, a number between 0 and 1. Larger values indicate that P and Q are closer, frontier_integral: Frontier Integral, a number between 0 and 1. Smaller values indicate that P and Q are closer, divergence_curve: a numpy.ndarray of shape (m, 2); plot it with matplotlib to view the divergence curve, p_hist: a discrete distribution, which is a quantized version of the text distribution p_text, q_hist: same as above, but with q_text. Examples: >>> # faiss segfaults in doctest for some reason, so the .compute call is not tested with doctest >>> import datasets >>> mauve = datasets.load_metric('mauve') >>> predictions = [\"hello there\", \"general kenobi\"] >>> references = [\"hello there\", \"general kenobi\"] >>> out = mauve.compute(predictions=predictions, references=references) # doctest: +SKIP >>> print(out.mauve) # doctest: +SKIP 1.0 """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCamelCase ( datasets.Metric ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self : Optional[int]): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage='https://github.com/krishnap25/mauve' , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('string' , id='sequence'), 'references': datasets.Value('string' , id='sequence'), }) , codebase_urls=['https://github.com/krishnap25/mauve'] , reference_urls=[ 'https://arxiv.org/abs/2102.01454', 'https://github.com/krishnap25/mauve', ] , ) def SCREAMING_SNAKE_CASE_ ( self : str , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : Optional[Any]=None , UpperCAmelCase_ : int=None , UpperCAmelCase_ : Any=None , UpperCAmelCase_ : Optional[int]=None , UpperCAmelCase_ : Tuple="auto" , UpperCAmelCase_ : Any=-1 , UpperCAmelCase_ : Optional[int]=0.9 , UpperCAmelCase_ : Union[str, Any]=5 , UpperCAmelCase_ : int=5_0_0 , UpperCAmelCase_ : int="gpt2-large" , UpperCAmelCase_ : Tuple=-1 , UpperCAmelCase_ : Dict=1_0_2_4 , UpperCAmelCase_ : List[str]=2_5 , UpperCAmelCase_ : int=5 , UpperCAmelCase_ : Any=True , UpperCAmelCase_ : str=2_5 , ): """simple docstring""" a : List[str] = compute_mauve( p_text=UpperCAmelCase_ , q_text=UpperCAmelCase_ , p_features=UpperCAmelCase_ , q_features=UpperCAmelCase_ , p_tokens=UpperCAmelCase_ , q_tokens=UpperCAmelCase_ , num_buckets=UpperCAmelCase_ , pca_max_data=UpperCAmelCase_ , kmeans_explained_var=UpperCAmelCase_ , kmeans_num_redo=UpperCAmelCase_ , kmeans_max_iter=UpperCAmelCase_ , featurize_model_name=UpperCAmelCase_ , device_id=UpperCAmelCase_ , max_text_length=UpperCAmelCase_ , divergence_curve_discretization_size=UpperCAmelCase_ , mauve_scaling_factor=UpperCAmelCase_ , verbose=UpperCAmelCase_ , seed=UpperCAmelCase_ , ) return out
345
0