code
stringlengths
82
54.1k
code_codestyle
int64
0
699
style_context
stringlengths
111
35.6k
style_context_codestyle
int64
0
699
label
int64
0
1
'''simple docstring''' import argparse import torch from ...utils import logging from . import AlbertConfig, AlbertForPreTraining, load_tf_weights_in_albert logging.set_verbosity_info() def _lowerCamelCase ( lowercase : Union[str, Any] , lowercase : Union[str, Any] , lowercase : Optional[int] ) -> Optional[int]: _a = AlbertConfig.from_json_file(_UpperCAmelCase ) print(F'Building PyTorch model from configuration: {config}' ) _a = AlbertForPreTraining(_UpperCAmelCase ) # Load weights from tf checkpoint load_tf_weights_in_albert(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) # Save pytorch-model print(F'Save PyTorch model to {pytorch_dump_path}' ) torch.save(model.state_dict() , _UpperCAmelCase ) if __name__ == "__main__": lowerCAmelCase_ : Any = 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( '--albert_config_file', default=None, type=str, required=True, help=( 'The config json file corresponding to the pre-trained ALBERT model. \n' 'This specifies the model architecture.' ), ) parser.add_argument( '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) lowerCAmelCase_ : Tuple = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.albert_config_file, args.pytorch_dump_path)
692
'''simple docstring''' from typing import Callable, Optional from .. import Features from ..packaged_modules.generator.generator import Generator from .abc import AbstractDatasetInputStream class _snake_case ( snake_case ): """simple docstring""" def __init__( self , UpperCAmelCase__ , UpperCAmelCase__ = None , UpperCAmelCase__ = None , UpperCAmelCase__ = False , UpperCAmelCase__ = False , UpperCAmelCase__ = None , UpperCAmelCase__ = None , **UpperCAmelCase__ , ) -> str: super().__init__( features=UpperCAmelCase__ , cache_dir=UpperCAmelCase__ , keep_in_memory=UpperCAmelCase__ , streaming=UpperCAmelCase__ , num_proc=UpperCAmelCase__ , **UpperCAmelCase__ , ) a_ = Generator( cache_dir=UpperCAmelCase__ , features=UpperCAmelCase__ , generator=UpperCAmelCase__ , gen_kwargs=UpperCAmelCase__ , **UpperCAmelCase__ , ) def __SCREAMING_SNAKE_CASE ( self ) -> Optional[int]: # Build iterable dataset if self.streaming: a_ = self.builder.as_streaming_dataset(split='train' ) # Build regular (map-style) dataset else: a_ = None a_ = None a_ = None a_ = None self.builder.download_and_prepare( download_config=UpperCAmelCase__ , download_mode=UpperCAmelCase__ , verification_mode=UpperCAmelCase__ , base_path=UpperCAmelCase__ , num_proc=self.num_proc , ) a_ = self.builder.as_dataset( split='train' , verification_mode=UpperCAmelCase__ , in_memory=self.keep_in_memory ) return dataset
697
0
from __future__ import annotations from collections import deque from collections.abc import Sequence from dataclasses import dataclass from typing import Any @dataclass class _a : """simple docstring""" snake_case =4_2 snake_case =None snake_case =None def lowerCamelCase__ ( ) ->Node | None: _UpperCAmelCase =Node(1 ) _UpperCAmelCase =Node(2 ) _UpperCAmelCase =Node(3 ) _UpperCAmelCase =Node(4 ) _UpperCAmelCase =Node(5 ) return tree def lowerCamelCase__ ( _lowerCamelCase ) ->list[int]: return [root.data, *preorder(root.left ), *preorder(root.right )] if root else [] def lowerCamelCase__ ( _lowerCamelCase ) ->list[int]: return postorder(root.left ) + postorder(root.right ) + [root.data] if root else [] def lowerCamelCase__ ( _lowerCamelCase ) ->list[int]: return [*inorder(root.left ), root.data, *inorder(root.right )] if root else [] def lowerCamelCase__ ( _lowerCamelCase ) ->int: return (max(height(root.left ) , height(root.right ) ) + 1) if root else 0 def lowerCamelCase__ ( _lowerCamelCase ) ->Sequence[Node | None]: _UpperCAmelCase =[] if root is None: return output _UpperCAmelCase =deque([root] ) while process_queue: _UpperCAmelCase =process_queue.popleft() output.append(node.data ) if node.left: process_queue.append(node.left ) if node.right: process_queue.append(node.right ) return output def lowerCamelCase__ ( _lowerCamelCase , _lowerCamelCase ) ->Sequence[Node | None]: _UpperCAmelCase =[] def populate_output(_lowerCamelCase , _lowerCamelCase ) -> None: if not root: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.left , level - 1 ) populate_output(root.right , level - 1 ) populate_output(_UpperCAmelCase , _UpperCAmelCase ) return output def lowerCamelCase__ ( _lowerCamelCase , _lowerCamelCase ) ->Sequence[Node | None]: _UpperCAmelCase =[] def populate_output(_lowerCamelCase , _lowerCamelCase ) -> None: if root is None: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.right , level - 1 ) populate_output(root.left , level - 1 ) populate_output(_UpperCAmelCase , _UpperCAmelCase ) return output def lowerCamelCase__ ( _lowerCamelCase ) ->Sequence[Node | None] | list[Any]: if root is None: return [] _UpperCAmelCase =[] _UpperCAmelCase =0 _UpperCAmelCase =height(_UpperCAmelCase ) for h in range(1 , height_tree + 1 ): if not flag: output.append(get_nodes_from_left_to_right(_UpperCAmelCase , _UpperCAmelCase ) ) _UpperCAmelCase =1 else: output.append(get_nodes_from_right_to_left(_UpperCAmelCase , _UpperCAmelCase ) ) _UpperCAmelCase =0 return output def lowerCamelCase__ ( ) ->None: # Main function for testing. _UpperCAmelCase =make_tree() print(F"In-order Traversal: {inorder(_UpperCAmelCase )}" ) print(F"Pre-order Traversal: {preorder(_UpperCAmelCase )}" ) print(F"Post-order Traversal: {postorder(_UpperCAmelCase )}" , "\n" ) print(F"Height of Tree: {height(_UpperCAmelCase )}" , "\n" ) print("Complete Level Order Traversal: " ) print(level_order(_UpperCAmelCase ) , "\n" ) print("Level-wise order Traversal: " ) for level in range(1 , height(_UpperCAmelCase ) + 1 ): print(F"Level {level}:" , get_nodes_from_left_to_right(_UpperCAmelCase , level=_UpperCAmelCase ) ) print("\nZigZag order Traversal: " ) print(zigzag(_UpperCAmelCase ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
408
'''simple docstring''' from __future__ import annotations import matplotlib.pyplot as plt # type: ignore import numpy # initial triangle of Koch snowflake __lowerCAmelCase =numpy.array([0, 0]) __lowerCAmelCase =numpy.array([0.5, 0.866_0254]) __lowerCAmelCase =numpy.array([1, 0]) __lowerCAmelCase =[VECTOR_1, VECTOR_2, VECTOR_3, VECTOR_1] def a ( _UpperCAmelCase , _UpperCAmelCase ) -> list[numpy.ndarray]: """simple docstring""" a_ = initial_vectors for _ in range(_UpperCAmelCase ): a_ = iteration_step(_UpperCAmelCase ) return vectors def a ( _UpperCAmelCase ) -> list[numpy.ndarray]: """simple docstring""" a_ = [] for i, start_vector in enumerate(vectors[:-1] ): a_ = vectors[i + 1] new_vectors.append(_UpperCAmelCase ) a_ = end_vector - start_vector new_vectors.append(start_vector + difference_vector / 3 ) new_vectors.append( start_vector + difference_vector / 3 + rotate(difference_vector / 3 , 6_0 ) ) new_vectors.append(start_vector + difference_vector * 2 / 3 ) new_vectors.append(vectors[-1] ) return new_vectors def a ( _UpperCAmelCase , _UpperCAmelCase ) -> numpy.ndarray: """simple docstring""" a_ = numpy.radians(_UpperCAmelCase ) a_ , a_ = numpy.cos(_UpperCAmelCase ), numpy.sin(_UpperCAmelCase ) a_ = numpy.array(((c, -s), (s, c)) ) return numpy.dot(_UpperCAmelCase , _UpperCAmelCase ) def a ( _UpperCAmelCase ) -> None: """simple docstring""" a_ = plt.gca() axes.set_aspect('equal' ) # matplotlib.pyplot.plot takes a list of all x-coordinates and a list of all # y-coordinates as inputs, which are constructed from the vector-list using # zip() a_ , a_ = zip(*_UpperCAmelCase ) plt.plot(_UpperCAmelCase , _UpperCAmelCase ) plt.show() if __name__ == "__main__": import doctest doctest.testmod() __lowerCAmelCase =iterate(INITIAL_VECTORS, 5) plot(processed_vectors)
697
0
'''simple docstring''' import json import os import re import shutil import tempfile import unittest from typing import Tuple from transformers import AddedToken, BatchEncoding, PerceiverTokenizer from transformers.utils import cached_property, is_tf_available, is_torch_available from ...test_tokenization_common import TokenizerTesterMixin if is_torch_available(): _SCREAMING_SNAKE_CASE = '''pt''' elif is_tf_available(): _SCREAMING_SNAKE_CASE = '''tf''' else: _SCREAMING_SNAKE_CASE = '''jax''' class __lowercase ( lowerCAmelCase__ , unittest.TestCase ): '''simple docstring''' a : int = PerceiverTokenizer a : Tuple = False def _UpperCAmelCase (self ) -> Tuple: '''simple docstring''' super().setUp() __lowercase = PerceiverTokenizer() tokenizer.save_pretrained(self.tmpdirname ) @cached_property def _UpperCAmelCase (self ) -> Optional[int]: '''simple docstring''' return PerceiverTokenizer.from_pretrained('''deepmind/language-perceiver''' ) def _UpperCAmelCase (self ,**_lowerCamelCase ) -> PerceiverTokenizer: '''simple docstring''' return self.tokenizer_class.from_pretrained(self.tmpdirname ,**UpperCAmelCase__ ) def _UpperCAmelCase (self ,_lowerCamelCase ,_lowerCamelCase=False ,_lowerCamelCase=20 ,_lowerCamelCase=5 ) -> Tuple[str, list]: '''simple docstring''' __lowercase = [] for i in range(len(UpperCAmelCase__ ) ): try: __lowercase = tokenizer.decode([i] ,clean_up_tokenization_spaces=UpperCAmelCase__ ) except UnicodeDecodeError: pass toks.append((i, tok) ) __lowercase = list(filter(lambda _lowerCamelCase : re.match(R'''^[ a-zA-Z]+$''' ,t[1] ) ,UpperCAmelCase__ ) ) __lowercase = list(filter(lambda _lowerCamelCase : [t[0]] == tokenizer.encode(t[1] ,add_special_tokens=UpperCAmelCase__ ) ,UpperCAmelCase__ ) ) if max_length is not None and len(UpperCAmelCase__ ) > max_length: __lowercase = toks[:max_length] if min_length is not None and len(UpperCAmelCase__ ) < min_length and len(UpperCAmelCase__ ) > 0: while len(UpperCAmelCase__ ) < min_length: __lowercase = toks + toks # toks_str = [t[1] for t in toks] __lowercase = [t[0] for t in toks] # Ensure consistency __lowercase = tokenizer.decode(UpperCAmelCase__ ,clean_up_tokenization_spaces=UpperCAmelCase__ ) if " " not in output_txt and len(UpperCAmelCase__ ) > 1: __lowercase = ( tokenizer.decode([toks_ids[0]] ,clean_up_tokenization_spaces=UpperCAmelCase__ ) + ''' ''' + tokenizer.decode(toks_ids[1:] ,clean_up_tokenization_spaces=UpperCAmelCase__ ) ) if with_prefix_space: __lowercase = ''' ''' + output_txt __lowercase = tokenizer.encode(UpperCAmelCase__ ,add_special_tokens=UpperCAmelCase__ ) return output_txt, output_ids def _UpperCAmelCase (self ) -> Tuple: '''simple docstring''' __lowercase = self.perceiver_tokenizer __lowercase = '''Unicode €.''' __lowercase = tokenizer(UpperCAmelCase__ ) __lowercase = [4, 91, 116, 111, 105, 117, 106, 107, 38, 232, 136, 178, 52, 5] self.assertEqual(encoded['''input_ids'''] ,UpperCAmelCase__ ) # decoding __lowercase = tokenizer.decode(UpperCAmelCase__ ) self.assertEqual(UpperCAmelCase__ ,'''[CLS]Unicode €.[SEP]''' ) __lowercase = tokenizer('''e è é ê ë''' ) __lowercase = [4, 107, 38, 201, 174, 38, 201, 175, 38, 201, 176, 38, 201, 177, 5] self.assertEqual(encoded['''input_ids'''] ,UpperCAmelCase__ ) # decoding __lowercase = tokenizer.decode(UpperCAmelCase__ ) self.assertEqual(UpperCAmelCase__ ,'''[CLS]e è é ê ë[SEP]''' ) # encode/decode, but with `encode` instead of `__call__` self.assertEqual(tokenizer.decode(tokenizer.encode('''e è é ê ë''' ) ) ,'''[CLS]e è é ê ë[SEP]''' ) def _UpperCAmelCase (self ) -> Tuple: '''simple docstring''' __lowercase = self.perceiver_tokenizer __lowercase = ['''A long paragraph for summarization.''', '''Another paragraph for summarization.'''] # fmt: off __lowercase = [4, 71, 38, 114, 117, 116, 109, 38, 118, 103, 120, 103, 109, 120, 103, 118, 110, 38, 108, 117, 120, 38, 121, 123, 115, 115, 103, 120, 111, 128, 103, 122, 111, 117, 116, 52, 5, 0] # fmt: on __lowercase = tokenizer(UpperCAmelCase__ ,padding=UpperCAmelCase__ ,return_tensors=UpperCAmelCase__ ) self.assertIsInstance(UpperCAmelCase__ ,UpperCAmelCase__ ) if FRAMEWORK != "jax": __lowercase = list(batch.input_ids.numpy()[0] ) else: __lowercase = list(batch.input_ids.tolist()[0] ) self.assertListEqual(UpperCAmelCase__ ,UpperCAmelCase__ ) self.assertEqual((2, 38) ,batch.input_ids.shape ) self.assertEqual((2, 38) ,batch.attention_mask.shape ) def _UpperCAmelCase (self ) -> str: '''simple docstring''' __lowercase = self.perceiver_tokenizer __lowercase = ['''A long paragraph for summarization.''', '''Another paragraph for summarization.'''] __lowercase = tokenizer(UpperCAmelCase__ ,padding=UpperCAmelCase__ ,return_tensors=UpperCAmelCase__ ) # check if input_ids are returned and no decoder_input_ids self.assertIn('''input_ids''' ,UpperCAmelCase__ ) self.assertIn('''attention_mask''' ,UpperCAmelCase__ ) self.assertNotIn('''decoder_input_ids''' ,UpperCAmelCase__ ) self.assertNotIn('''decoder_attention_mask''' ,UpperCAmelCase__ ) def _UpperCAmelCase (self ) -> Optional[Any]: '''simple docstring''' __lowercase = self.perceiver_tokenizer __lowercase = [ '''Summary of the text.''', '''Another summary.''', ] __lowercase = tokenizer( text_target=UpperCAmelCase__ ,max_length=32 ,padding='''max_length''' ,truncation=UpperCAmelCase__ ,return_tensors=UpperCAmelCase__ ) self.assertEqual(32 ,targets['''input_ids'''].shape[1] ) def _UpperCAmelCase (self ) -> Any: '''simple docstring''' __lowercase = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f"{tokenizer.__class__.__name__}" ): self.assertNotEqual(tokenizer.model_max_length ,42 ) # Now let's start the test __lowercase = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f"{tokenizer.__class__.__name__}" ): # Isolate this from the other tests because we save additional tokens/etc __lowercase = tempfile.mkdtemp() __lowercase = ''' He is very happy, UNwant\u00E9d,running''' __lowercase = tokenizer.encode(UpperCAmelCase__ ,add_special_tokens=UpperCAmelCase__ ) tokenizer.save_pretrained(UpperCAmelCase__ ) __lowercase = tokenizer.__class__.from_pretrained(UpperCAmelCase__ ) __lowercase = after_tokenizer.encode(UpperCAmelCase__ ,add_special_tokens=UpperCAmelCase__ ) self.assertListEqual(UpperCAmelCase__ ,UpperCAmelCase__ ) shutil.rmtree(UpperCAmelCase__ ) __lowercase = self.get_tokenizers(model_max_length=42 ) for tokenizer in tokenizers: with self.subTest(f"{tokenizer.__class__.__name__}" ): # Isolate this from the other tests because we save additional tokens/etc __lowercase = tempfile.mkdtemp() __lowercase = ''' He is very happy, UNwant\u00E9d,running''' tokenizer.add_tokens(['''bim''', '''bambam'''] ) __lowercase = tokenizer.additional_special_tokens additional_special_tokens.append('''new_additional_special_token''' ) tokenizer.add_special_tokens({'''additional_special_tokens''': additional_special_tokens} ) __lowercase = tokenizer.encode(UpperCAmelCase__ ,add_special_tokens=UpperCAmelCase__ ) tokenizer.save_pretrained(UpperCAmelCase__ ) __lowercase = tokenizer.__class__.from_pretrained(UpperCAmelCase__ ) __lowercase = after_tokenizer.encode(UpperCAmelCase__ ,add_special_tokens=UpperCAmelCase__ ) self.assertListEqual(UpperCAmelCase__ ,UpperCAmelCase__ ) self.assertIn('''new_additional_special_token''' ,after_tokenizer.additional_special_tokens ) self.assertEqual(after_tokenizer.model_max_length ,42 ) __lowercase = tokenizer.__class__.from_pretrained(UpperCAmelCase__ ,model_max_length=43 ) self.assertEqual(tokenizer.model_max_length ,43 ) shutil.rmtree(UpperCAmelCase__ ) def _UpperCAmelCase (self ) -> Tuple: '''simple docstring''' __lowercase = [] if self.test_slow_tokenizer: tokenizer_list.append((self.tokenizer_class, self.get_tokenizer()) ) if self.test_rust_tokenizer: tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer()) ) for tokenizer_class, tokenizer_utils in tokenizer_list: with tempfile.TemporaryDirectory() as tmp_dir: tokenizer_utils.save_pretrained(UpperCAmelCase__ ) with open(os.path.join(UpperCAmelCase__ ,'''special_tokens_map.json''' ) ,encoding='''utf-8''' ) as json_file: __lowercase = json.load(UpperCAmelCase__ ) with open(os.path.join(UpperCAmelCase__ ,'''tokenizer_config.json''' ) ,encoding='''utf-8''' ) as json_file: __lowercase = json.load(UpperCAmelCase__ ) __lowercase = [f"<extra_id_{i}>" for i in range(125 )] __lowercase = added_tokens_extra_ids + [ '''an_additional_special_token''' ] __lowercase = added_tokens_extra_ids + [ '''an_additional_special_token''' ] with open(os.path.join(UpperCAmelCase__ ,'''special_tokens_map.json''' ) ,'''w''' ,encoding='''utf-8''' ) as outfile: json.dump(UpperCAmelCase__ ,UpperCAmelCase__ ) with open(os.path.join(UpperCAmelCase__ ,'''tokenizer_config.json''' ) ,'''w''' ,encoding='''utf-8''' ) as outfile: json.dump(UpperCAmelCase__ ,UpperCAmelCase__ ) # the following checks allow us to verify that our test works as expected, i.e. that the tokenizer takes # into account the new value of additional_special_tokens given in the "tokenizer_config.json" and # "special_tokens_map.json" files __lowercase = tokenizer_class.from_pretrained( UpperCAmelCase__ ,) self.assertIn( '''an_additional_special_token''' ,tokenizer_without_change_in_init.additional_special_tokens ) self.assertEqual( ['''an_additional_special_token'''] ,tokenizer_without_change_in_init.convert_ids_to_tokens( tokenizer_without_change_in_init.convert_tokens_to_ids(['''an_additional_special_token'''] ) ) ,) # Now we test that we can change the value of additional_special_tokens in the from_pretrained __lowercase = added_tokens_extra_ids + [AddedToken('''a_new_additional_special_token''' ,lstrip=UpperCAmelCase__ )] __lowercase = tokenizer_class.from_pretrained( UpperCAmelCase__ ,additional_special_tokens=UpperCAmelCase__ ,) self.assertIn('''a_new_additional_special_token''' ,tokenizer.additional_special_tokens ) self.assertEqual( ['''a_new_additional_special_token'''] ,tokenizer.convert_ids_to_tokens( tokenizer.convert_tokens_to_ids(['''a_new_additional_special_token'''] ) ) ,) def _UpperCAmelCase (self ) -> Tuple: '''simple docstring''' __lowercase = self.perceiver_tokenizer self.assertEqual(tokenizer.decode([178] ) ,'''�''' ) def _UpperCAmelCase (self ) -> List[str]: '''simple docstring''' pass def _UpperCAmelCase (self ) -> Any: '''simple docstring''' pass def _UpperCAmelCase (self ) -> Union[str, Any]: '''simple docstring''' pass def _UpperCAmelCase (self ) -> str: '''simple docstring''' pass def _UpperCAmelCase (self ) -> str: '''simple docstring''' __lowercase = self.get_tokenizers(fast=UpperCAmelCase__ ,do_lower_case=UpperCAmelCase__ ) for tokenizer in tokenizers: with self.subTest(f"{tokenizer.__class__.__name__}" ): __lowercase = ['''[CLS]''', '''t''', '''h''', '''i''', '''s''', ''' ''', '''i''', '''s''', ''' ''', '''a''', ''' ''', '''t''', '''e''', '''s''', '''t''', '''[SEP]'''] __lowercase = tokenizer.convert_tokens_to_string(UpperCAmelCase__ ) self.assertIsInstance(UpperCAmelCase__ ,UpperCAmelCase__ )
502
'''simple docstring''' import argparse import json from typing import List from ltp import LTP from transformers.models.bert.tokenization_bert import BertTokenizer def a ( _UpperCAmelCase ) -> int: """simple docstring""" if ( (cp >= 0X4_e00 and cp <= 0X9_fff) or (cp >= 0X3_400 and cp <= 0X4_dbf) # or (cp >= 0X20_000 and cp <= 0X2a_6df) # or (cp >= 0X2a_700 and cp <= 0X2b_73f) # or (cp >= 0X2b_740 and cp <= 0X2b_81f) # or (cp >= 0X2b_820 and cp <= 0X2c_eaf) # or (cp >= 0Xf_900 and cp <= 0Xf_aff) or (cp >= 0X2f_800 and cp <= 0X2f_a1f) # ): # return True return False def a ( _UpperCAmelCase ) -> Union[str, Any]: """simple docstring""" for char in word: a_ = ord(_UpperCAmelCase ) if not _is_chinese_char(_UpperCAmelCase ): return 0 return 1 def a ( _UpperCAmelCase ) -> Tuple: """simple docstring""" a_ = set() for token in tokens: a_ = len(_UpperCAmelCase ) > 1 and is_chinese(_UpperCAmelCase ) if chinese_word: word_set.add(_UpperCAmelCase ) a_ = list(_UpperCAmelCase ) return word_list def a ( _UpperCAmelCase , _UpperCAmelCase ) -> List[Any]: """simple docstring""" if not chinese_word_set: return bert_tokens a_ = max([len(_UpperCAmelCase ) for w in chinese_word_set] ) a_ = bert_tokens a_ , a_ = 0, len(_UpperCAmelCase ) while start < end: a_ = True if is_chinese(bert_word[start] ): a_ = min(end - start , _UpperCAmelCase ) for i in range(_UpperCAmelCase , 1 , -1 ): a_ = ''.join(bert_word[start : start + i] ) if whole_word in chinese_word_set: for j in range(start + 1 , start + i ): a_ = '##' + bert_word[j] a_ = start + i a_ = False break if single_word: start += 1 return bert_word def a ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) -> Any: """simple docstring""" a_ = [] for i in range(0 , len(_UpperCAmelCase ) , 1_0_0 ): a_ = ltp_tokenizer.pipeline(lines[i : i + 1_0_0] , tasks=['cws'] ).cws a_ = [get_chinese_word(_UpperCAmelCase ) for r in res] ltp_res.extend(_UpperCAmelCase ) assert len(_UpperCAmelCase ) == len(_UpperCAmelCase ) a_ = [] for i in range(0 , len(_UpperCAmelCase ) , 1_0_0 ): a_ = bert_tokenizer(lines[i : i + 1_0_0] , add_special_tokens=_UpperCAmelCase , truncation=_UpperCAmelCase , max_length=5_1_2 ) bert_res.extend(res['input_ids'] ) assert len(_UpperCAmelCase ) == len(_UpperCAmelCase ) a_ = [] for input_ids, chinese_word in zip(_UpperCAmelCase , _UpperCAmelCase ): a_ = [] for id in input_ids: a_ = bert_tokenizer._convert_id_to_token(_UpperCAmelCase ) input_tokens.append(_UpperCAmelCase ) a_ = add_sub_symbol(_UpperCAmelCase , _UpperCAmelCase ) a_ = [] # We only save pos of chinese subwords start with ##, which mean is part of a whole word. for i, token in enumerate(_UpperCAmelCase ): if token[:2] == "##": a_ = token[2:] # save chinese tokens' pos if len(_UpperCAmelCase ) == 1 and _is_chinese_char(ord(_UpperCAmelCase ) ): ref_id.append(_UpperCAmelCase ) ref_ids.append(_UpperCAmelCase ) assert len(_UpperCAmelCase ) == len(_UpperCAmelCase ) return ref_ids def a ( _UpperCAmelCase ) -> Optional[Any]: """simple docstring""" with open(args.file_name , 'r' , encoding='utf-8' ) as f: a_ = f.readlines() a_ = [line.strip() for line in data if len(_UpperCAmelCase ) > 0 and not line.isspace()] # avoid delimiter like '\u2029' a_ = LTP(args.ltp ) # faster in GPU device a_ = BertTokenizer.from_pretrained(args.bert ) a_ = prepare_ref(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) with open(args.save_path , 'w' , encoding='utf-8' ) as f: a_ = [json.dumps(_UpperCAmelCase ) + '\n' for ref in ref_ids] f.writelines(_UpperCAmelCase ) if __name__ == "__main__": __lowerCAmelCase =argparse.ArgumentParser(description="prepare_chinese_ref") parser.add_argument( "--file_name", required=False, type=str, default="./resources/chinese-demo.txt", help="file need process, same as training data in lm", ) parser.add_argument( "--ltp", required=False, type=str, default="./resources/ltp", help="resources for LTP tokenizer, usually a path", ) parser.add_argument( "--bert", required=False, type=str, default="./resources/robert", help="resources for Bert tokenizer", ) parser.add_argument( "--save_path", required=False, type=str, default="./resources/ref.txt", help="path to save res", ) __lowerCAmelCase =parser.parse_args() main(args)
697
0
'''simple docstring''' from math import factorial, radians def snake_case_ (UpperCamelCase : Optional[int] , UpperCamelCase : Optional[int] = 18 , UpperCamelCase : Optional[int] = 10 ): '''simple docstring''' _a = angle_in_degrees - ((angle_in_degrees // 360.0) * 360.0) # Converting from degrees to radians _a = radians(_UpperCAmelCase ) _a = angle_in_radians _a = 3 _a = -1 for _ in range(_UpperCAmelCase ): result += (b * (angle_in_radians**a)) / factorial(_UpperCAmelCase ) _a = -b # One positive term and the next will be negative and so on... a += 2 # Increased by 2 for every term. return round(_UpperCAmelCase , _UpperCAmelCase ) if __name__ == "__main__": __import__('doctest').testmod()
22
'''simple docstring''' import json from typing import List, Optional, Tuple from tokenizers import normalizers from ....tokenization_utils_fast import PreTrainedTokenizerFast from ....utils import logging from .tokenization_retribert import RetriBertTokenizer __lowerCAmelCase =logging.get_logger(__name__) __lowerCAmelCase ={"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} __lowerCAmelCase ={ "vocab_file": { "yjernite/retribert-base-uncased": ( "https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/vocab.txt" ), }, "tokenizer_file": { "yjernite/retribert-base-uncased": ( "https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/tokenizer.json" ), }, } __lowerCAmelCase ={ "yjernite/retribert-base-uncased": 512, } __lowerCAmelCase ={ "yjernite/retribert-base-uncased": {"do_lower_case": True}, } class _snake_case ( snake_case ): """simple docstring""" _UpperCamelCase = VOCAB_FILES_NAMES _UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP _UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _UpperCamelCase = PRETRAINED_INIT_CONFIGURATION _UpperCamelCase = RetriBertTokenizer _UpperCamelCase = ["input_ids", "attention_mask"] def __init__( self , UpperCAmelCase__=None , UpperCAmelCase__=None , UpperCAmelCase__=True , UpperCAmelCase__="[UNK]" , UpperCAmelCase__="[SEP]" , UpperCAmelCase__="[PAD]" , UpperCAmelCase__="[CLS]" , UpperCAmelCase__="[MASK]" , UpperCAmelCase__=True , UpperCAmelCase__=None , **UpperCAmelCase__ , ) -> int: super().__init__( UpperCAmelCase__ , tokenizer_file=UpperCAmelCase__ , do_lower_case=UpperCAmelCase__ , unk_token=UpperCAmelCase__ , sep_token=UpperCAmelCase__ , pad_token=UpperCAmelCase__ , cls_token=UpperCAmelCase__ , mask_token=UpperCAmelCase__ , tokenize_chinese_chars=UpperCAmelCase__ , strip_accents=UpperCAmelCase__ , **UpperCAmelCase__ , ) a_ = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('lowercase' , UpperCAmelCase__ ) != do_lower_case or normalizer_state.get('strip_accents' , UpperCAmelCase__ ) != strip_accents or normalizer_state.get('handle_chinese_chars' , UpperCAmelCase__ ) != tokenize_chinese_chars ): a_ = getattr(UpperCAmelCase__ , normalizer_state.pop('type' ) ) a_ = do_lower_case a_ = strip_accents a_ = tokenize_chinese_chars a_ = normalizer_class(**UpperCAmelCase__ ) a_ = do_lower_case def __SCREAMING_SNAKE_CASE ( self , UpperCAmelCase__ , UpperCAmelCase__=None ) -> str: a_ = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def __SCREAMING_SNAKE_CASE ( self , UpperCAmelCase__ , UpperCAmelCase__ = None ) -> List[int]: a_ = [self.sep_token_id] a_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __SCREAMING_SNAKE_CASE ( self , UpperCAmelCase__ , UpperCAmelCase__ = None ) -> Tuple[str]: a_ = self._tokenizer.model.save(UpperCAmelCase__ , name=UpperCAmelCase__ ) return tuple(UpperCAmelCase__ )
697
0
import pytest import requests from datasets.utils.file_utils import http_head from .utils import OfflineSimulationMode, RequestWouldHangIndefinitelyError, offline @pytest.mark.integration def _UpperCamelCase ( ) -> List[str]: with offline(OfflineSimulationMode.CONNECTION_TIMES_OUT ): with pytest.raises(_UpperCAmelCase ): requests.request("GET", "https://huggingface.co" ) with pytest.raises(requests.exceptions.ConnectTimeout ): requests.request("GET", "https://huggingface.co", timeout=1.0 ) @pytest.mark.integration def _UpperCamelCase ( ) -> int: with offline(OfflineSimulationMode.CONNECTION_FAILS ): with pytest.raises(requests.exceptions.ConnectionError ): requests.request("GET", "https://huggingface.co" ) def _UpperCamelCase ( ) -> Union[str, Any]: with offline(OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1 ): with pytest.raises(_UpperCAmelCase ): http_head("https://huggingface.co" )
382
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __lowerCAmelCase =logging.get_logger(__name__) __lowerCAmelCase ={ "SCUT-DLVCLab/lilt-roberta-en-base": ( "https://huggingface.co/SCUT-DLVCLab/lilt-roberta-en-base/resolve/main/config.json" ), } class _snake_case ( snake_case ): """simple docstring""" _UpperCamelCase = "lilt" def __init__( self , UpperCAmelCase__=3_0522 , UpperCAmelCase__=768 , UpperCAmelCase__=12 , UpperCAmelCase__=12 , UpperCAmelCase__=3072 , UpperCAmelCase__="gelu" , UpperCAmelCase__=0.1 , UpperCAmelCase__=0.1 , UpperCAmelCase__=512 , UpperCAmelCase__=2 , UpperCAmelCase__=0.0_2 , UpperCAmelCase__=1e-12 , UpperCAmelCase__=0 , UpperCAmelCase__="absolute" , UpperCAmelCase__=None , UpperCAmelCase__=4 , UpperCAmelCase__=1024 , **UpperCAmelCase__ , ) -> Optional[Any]: super().__init__(pad_token_id=UpperCAmelCase__ , **UpperCAmelCase__ ) a_ = vocab_size a_ = hidden_size a_ = num_hidden_layers a_ = num_attention_heads a_ = hidden_act a_ = intermediate_size a_ = hidden_dropout_prob a_ = attention_probs_dropout_prob a_ = max_position_embeddings a_ = type_vocab_size a_ = initializer_range a_ = layer_norm_eps a_ = position_embedding_type a_ = classifier_dropout a_ = channel_shrink_ratio a_ = max_ad_position_embeddings
697
0
from math import sqrt def UpperCamelCase ( __lowerCamelCase : Tuple ): assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) and ( number >= 0 ), "'number' must been an int and positive" snake_case : Dict = True # 0 and 1 are none primes. if number <= 1: snake_case : Tuple = False for divisor in range(2 , int(round(sqrt(_UpperCAmelCase ) ) ) + 1 ): # if 'number' divisible by 'divisor' then sets 'status' # of false and break up the loop. if number % divisor == 0: snake_case : Optional[int] = False break # precondition assert isinstance(_UpperCAmelCase , _UpperCAmelCase ), "'status' must been from type bool" return status def UpperCamelCase ( __lowerCamelCase : int ): assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) and (n > 2), "'N' must been an int and > 2" # beginList: contains all natural numbers from 2 up to N snake_case : Optional[int] = list(range(2 , n + 1 ) ) snake_case : Any = [] # this list will be returns. # actual sieve of erathostenes for i in range(len(_UpperCAmelCase ) ): for j in range(i + 1 , len(_UpperCAmelCase ) ): if (begin_list[i] != 0) and (begin_list[j] % begin_list[i] == 0): snake_case : Any = 0 # filters actual prime numbers. snake_case : Optional[Any] = [x for x in begin_list if x != 0] # precondition assert isinstance(_UpperCAmelCase , _UpperCAmelCase ), "'ans' must been from type list" return ans def UpperCamelCase ( __lowerCamelCase : Any ): assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) and (n > 2), "'N' must been an int and > 2" snake_case : Optional[Any] = [] # iterates over all numbers between 2 up to N+1 # if a number is prime then appends to list 'ans' for number in range(2 , n + 1 ): if is_prime(_UpperCAmelCase ): ans.append(_UpperCAmelCase ) # precondition assert isinstance(_UpperCAmelCase , _UpperCAmelCase ), "'ans' must been from type list" return ans def UpperCamelCase ( __lowerCamelCase : Union[str, Any] ): assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) and number >= 0, "'number' must been an int and >= 0" snake_case : List[str] = [] # this list will be returns of the function. # potential prime number factors. snake_case : List[str] = 2 snake_case : int = number if number == 0 or number == 1: ans.append(_UpperCAmelCase ) # if 'number' not prime then builds the prime factorization of 'number' elif not is_prime(_UpperCAmelCase ): while quotient != 1: if is_prime(_UpperCAmelCase ) and (quotient % factor == 0): ans.append(_UpperCAmelCase ) quotient /= factor else: factor += 1 else: ans.append(_UpperCAmelCase ) # precondition assert isinstance(_UpperCAmelCase , _UpperCAmelCase ), "'ans' must been from type list" return ans def UpperCamelCase ( __lowerCamelCase : Optional[int] ): assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) and ( number >= 0 ), "'number' bust been an int and >= 0" snake_case : Optional[Any] = 0 # prime factorization of 'number' snake_case : Dict = prime_factorization(_UpperCAmelCase ) snake_case : Dict = max(_UpperCAmelCase ) # precondition assert isinstance(_UpperCAmelCase , _UpperCAmelCase ), "'ans' must been from type int" return ans def UpperCamelCase ( __lowerCamelCase : Union[str, Any] ): assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) and ( number >= 0 ), "'number' bust been an int and >= 0" snake_case : Tuple = 0 # prime factorization of 'number' snake_case : int = prime_factorization(_UpperCAmelCase ) snake_case : Any = min(_UpperCAmelCase ) # precondition assert isinstance(_UpperCAmelCase , _UpperCAmelCase ), "'ans' must been from type int" return ans def UpperCamelCase ( __lowerCamelCase : List[Any] ): assert isinstance(_UpperCAmelCase , _UpperCAmelCase ), "'number' must been an int" assert isinstance(number % 2 == 0 , _UpperCAmelCase ), "compare bust been from type bool" return number % 2 == 0 def UpperCamelCase ( __lowerCamelCase : Optional[int] ): assert isinstance(_UpperCAmelCase , _UpperCAmelCase ), "'number' must been an int" assert isinstance(number % 2 != 0 , _UpperCAmelCase ), "compare bust been from type bool" return number % 2 != 0 def UpperCamelCase ( __lowerCamelCase : Optional[int] ): assert ( isinstance(_UpperCAmelCase , _UpperCAmelCase ) and (number > 2) and is_even(_UpperCAmelCase ) ), "'number' must been an int, even and > 2" snake_case : Union[str, Any] = [] # this list will returned # creates a list of prime numbers between 2 up to 'number' snake_case : List[str] = get_prime_numbers(_UpperCAmelCase ) snake_case : List[Any] = len(_UpperCAmelCase ) # run variable for while-loops. snake_case : str = 0 snake_case : str = None # exit variable. for break up the loops snake_case : Optional[Any] = True while i < len_pn and loop: snake_case : int = i + 1 while j < len_pn and loop: if prime_numbers[i] + prime_numbers[j] == number: snake_case : Tuple = False ans.append(prime_numbers[i] ) ans.append(prime_numbers[j] ) j += 1 i += 1 # precondition assert ( isinstance(_UpperCAmelCase , _UpperCAmelCase ) and (len(_UpperCAmelCase ) == 2) and (ans[0] + ans[1] == number) and is_prime(ans[0] ) and is_prime(ans[1] ) ), "'ans' must contains two primes. And sum of elements must been eq 'number'" return ans def UpperCamelCase ( __lowerCamelCase : int , __lowerCamelCase : List[Any] ): assert ( isinstance(_UpperCAmelCase , _UpperCAmelCase ) and isinstance(_UpperCAmelCase , _UpperCAmelCase ) and (numbera >= 0) and (numbera >= 0) ), "'number1' and 'number2' must been positive integer." snake_case : int = 0 while numbera != 0: snake_case : str = numbera % numbera snake_case : Any = numbera snake_case : List[Any] = rest # precondition assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) and ( numbera >= 0 ), "'number' must been from type int and positive" return numbera def UpperCamelCase ( __lowerCamelCase : Optional[Any] , __lowerCamelCase : List[Any] ): assert ( isinstance(_UpperCAmelCase , _UpperCAmelCase ) and isinstance(_UpperCAmelCase , _UpperCAmelCase ) and (numbera >= 1) and (numbera >= 1) ), "'number1' and 'number2' must been positive integer." snake_case : List[str] = 1 # actual answer that will be return. # for kgV (x,1) if numbera > 1 and numbera > 1: # builds the prime factorization of 'number1' and 'number2' snake_case : Union[str, Any] = prime_factorization(_UpperCAmelCase ) snake_case : List[str] = prime_factorization(_UpperCAmelCase ) elif numbera == 1 or numbera == 1: snake_case : str = [] snake_case : Optional[Any] = [] snake_case : Any = max(_UpperCAmelCase , _UpperCAmelCase ) snake_case : int = 0 snake_case : Optional[Any] = 0 snake_case : Tuple = [] # captured numbers int both 'primeFac1' and 'primeFac2' # iterates through primeFac1 for n in prime_fac_a: if n not in done: if n in prime_fac_a: snake_case : Dict = prime_fac_a.count(_UpperCAmelCase ) snake_case : Dict = prime_fac_a.count(_UpperCAmelCase ) for _ in range(max(_UpperCAmelCase , _UpperCAmelCase ) ): ans *= n else: snake_case : List[Any] = prime_fac_a.count(_UpperCAmelCase ) for _ in range(_UpperCAmelCase ): ans *= n done.append(_UpperCAmelCase ) # iterates through primeFac2 for n in prime_fac_a: if n not in done: snake_case : List[str] = prime_fac_a.count(_UpperCAmelCase ) for _ in range(_UpperCAmelCase ): ans *= n done.append(_UpperCAmelCase ) # precondition assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) and ( ans >= 0 ), "'ans' must been from type int and positive" return ans def UpperCamelCase ( __lowerCamelCase : Any ): assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) and (n >= 0), "'number' must been a positive int" snake_case : List[str] = 0 snake_case : str = 2 # this variable holds the answer while index < n: index += 1 ans += 1 # counts to the next number # if ans not prime then # runs to the next prime number. while not is_prime(_UpperCAmelCase ): ans += 1 # precondition assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) and is_prime( _UpperCAmelCase ), "'ans' must been a prime number and from type int" return ans def UpperCamelCase ( __lowerCamelCase : str , __lowerCamelCase : Optional[Any] ): assert ( is_prime(_UpperCAmelCase ) and is_prime(_UpperCAmelCase ) and (p_number_a < p_number_a) ), "The arguments must been prime numbers and 'pNumber1' < 'pNumber2'" snake_case : Dict = p_number_a + 1 # jump to the next number snake_case : Optional[int] = [] # this list will be returns. # if number is not prime then # fetch the next prime number. while not is_prime(_UpperCAmelCase ): number += 1 while number < p_number_a: ans.append(_UpperCAmelCase ) number += 1 # fetch the next prime number. while not is_prime(_UpperCAmelCase ): number += 1 # precondition assert ( isinstance(_UpperCAmelCase , _UpperCAmelCase ) and ans[0] != p_number_a and ans[len(_UpperCAmelCase ) - 1] != p_number_a ), "'ans' must been a list without the arguments" # 'ans' contains not 'pNumber1' and 'pNumber2' ! return ans def UpperCamelCase ( __lowerCamelCase : Dict ): assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) and (n >= 1), "'n' must been int and >= 1" snake_case : int = [] # will be returned. for divisor in range(1 , n + 1 ): if n % divisor == 0: ans.append(_UpperCAmelCase ) # precondition assert ans[0] == 1 and ans[len(_UpperCAmelCase ) - 1] == n, "Error in function getDivisiors(...)" return ans def UpperCamelCase ( __lowerCamelCase : List[Any] ): assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) and ( number > 1 ), "'number' must been an int and >= 1" snake_case : Optional[Any] = get_divisors(_UpperCAmelCase ) # precondition assert ( isinstance(_UpperCAmelCase , _UpperCAmelCase ) and (divisors[0] == 1) and (divisors[len(_UpperCAmelCase ) - 1] == number) ), "Error in help-function getDivisiors(...)" # summed all divisors up to 'number' (exclusive), hence [:-1] return sum(divisors[:-1] ) == number def UpperCamelCase ( __lowerCamelCase : Any , __lowerCamelCase : str ): assert ( isinstance(_UpperCAmelCase , _UpperCAmelCase ) and isinstance(_UpperCAmelCase , _UpperCAmelCase ) and (denominator != 0) ), "The arguments must been from type int and 'denominator' != 0" # build the greatest common divisor of numerator and denominator. snake_case : Tuple = gcd(abs(_UpperCAmelCase ) , abs(_UpperCAmelCase ) ) # precondition assert ( isinstance(_UpperCAmelCase , _UpperCAmelCase ) and (numerator % gcd_of_fraction == 0) and (denominator % gcd_of_fraction == 0) ), "Error in function gcd(...,...)" return (numerator // gcd_of_fraction, denominator // gcd_of_fraction) def UpperCamelCase ( __lowerCamelCase : Tuple ): assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) and (n >= 0), "'n' must been a int and >= 0" snake_case : Dict = 1 # this will be return. for factor in range(1 , n + 1 ): ans *= factor return ans def UpperCamelCase ( __lowerCamelCase : Optional[Any] ): assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) and (n >= 0), "'n' must been an int and >= 0" snake_case : Union[str, Any] = 0 snake_case : Optional[Any] = 1 snake_case : Dict = 1 # this will be return for _ in range(n - 1 ): snake_case : Tuple = ans ans += fiba snake_case : str = tmp return ans
204
'''simple docstring''' from __future__ import annotations def a ( _UpperCAmelCase ) -> bool: """simple docstring""" a_ = len(_UpperCAmelCase ) # We need to create solution object to save path. a_ = [[0 for _ in range(_UpperCAmelCase )] for _ in range(_UpperCAmelCase )] a_ = run_maze(_UpperCAmelCase , 0 , 0 , _UpperCAmelCase ) if solved: print('\n'.join(str(_UpperCAmelCase ) for row in solutions ) ) else: print('No solution exists!' ) return solved def a ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) -> bool: """simple docstring""" a_ = len(_UpperCAmelCase ) # Final check point. if i == j == (size - 1): a_ = 1 return True a_ = (not i < 0) and (not j < 0) # Check lower bounds a_ = (i < size) and (j < size) # Check upper bounds if lower_flag and upper_flag: # check for already visited and block points. a_ = (not solutions[i][j]) and (not maze[i][j]) if block_flag: # check visited a_ = 1 # check for directions if ( run_maze(_UpperCAmelCase , i + 1 , _UpperCAmelCase , _UpperCAmelCase ) or run_maze(_UpperCAmelCase , _UpperCAmelCase , j + 1 , _UpperCAmelCase ) or run_maze(_UpperCAmelCase , i - 1 , _UpperCAmelCase , _UpperCAmelCase ) or run_maze(_UpperCAmelCase , _UpperCAmelCase , j - 1 , _UpperCAmelCase ) ): return True a_ = 0 return False return False if __name__ == "__main__": import doctest doctest.testmod()
697
0
import re from filelock import FileLock try: import nltk A__ : Optional[int] = True except (ImportError, ModuleNotFoundError): A__ : str = False if NLTK_AVAILABLE: with FileLock('''.lock''') as lock: nltk.download('''punkt''', quiet=True) def UpperCamelCase( __UpperCamelCase : List[Any] ): re.sub('''<n>''' ,'''''' ,_UpperCAmelCase ) # remove pegasus newline char assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)" return "\n".join(nltk.sent_tokenize(_UpperCAmelCase ) )
171
'''simple docstring''' __lowerCAmelCase ={ "meter": "m", "kilometer": "km", "megametre": "Mm", "gigametre": "Gm", "terametre": "Tm", "petametre": "Pm", "exametre": "Em", "zettametre": "Zm", "yottametre": "Ym", } # Exponent of the factor(meter) __lowerCAmelCase ={ "m": 0, "km": 3, "Mm": 6, "Gm": 9, "Tm": 12, "Pm": 15, "Em": 18, "Zm": 21, "Ym": 24, } def a ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) -> float: """simple docstring""" a_ = from_type.lower().strip('s' ) a_ = to_type.lower().strip('s' ) a_ = UNIT_SYMBOL.get(_UpperCAmelCase , _UpperCAmelCase ) a_ = UNIT_SYMBOL.get(_UpperCAmelCase , _UpperCAmelCase ) if from_sanitized not in METRIC_CONVERSION: a_ = ( F'''Invalid \'from_type\' value: {from_type!r}.\n''' F'''Conversion abbreviations are: {', '.join(_UpperCAmelCase )}''' ) raise ValueError(_UpperCAmelCase ) if to_sanitized not in METRIC_CONVERSION: a_ = ( F'''Invalid \'to_type\' value: {to_type!r}.\n''' F'''Conversion abbreviations are: {', '.join(_UpperCAmelCase )}''' ) raise ValueError(_UpperCAmelCase ) a_ = METRIC_CONVERSION[from_sanitized] a_ = METRIC_CONVERSION[to_sanitized] a_ = 1 if from_exponent > to_exponent: a_ = from_exponent - to_exponent else: a_ = -(to_exponent - from_exponent) return value * pow(1_0 , _UpperCAmelCase ) if __name__ == "__main__": from doctest import testmod testmod()
697
0
"""simple docstring""" import math from typing import Optional import numpy as np from ...configuration_utils import PretrainedConfig from ...utils import logging _lowerCAmelCase :Any = logging.get_logger(__name__) _lowerCAmelCase :Tuple = { 'facebook/encodec_24khz': 'https://huggingface.co/facebook/encodec_24khz/resolve/main/config.json', 'facebook/encodec_48khz': 'https://huggingface.co/facebook/encodec_48khz/resolve/main/config.json', } class _UpperCAmelCase ( a ): '''simple docstring''' a__ ='''encodec''' def __init__( self , A=[1.5, 3.0, 6.0, 1_2.0, 2_4.0] , A=2_4_0_0_0 , A=1 , A=False , A=None , A=None , A=1_2_8 , A=3_2 , A=1 , A=[8, 5, 4, 2] , A="weight_norm" , A=7 , A=7 , A=3 , A=2 , A=True , A="reflect" , A=2 , A=2 , A=1.0 , A=1_0_2_4 , A=None , A=True , **A , ) -> Tuple: _UpperCAmelCase : Union[str, Any] = target_bandwidths _UpperCAmelCase : str = sampling_rate _UpperCAmelCase : Dict = audio_channels _UpperCAmelCase : List[Any] = normalize _UpperCAmelCase : Union[str, Any] = chunk_length_s _UpperCAmelCase : Tuple = overlap _UpperCAmelCase : str = hidden_size _UpperCAmelCase : Any = num_filters _UpperCAmelCase : int = num_residual_layers _UpperCAmelCase : Tuple = upsampling_ratios _UpperCAmelCase : Tuple = norm_type _UpperCAmelCase : Tuple = kernel_size _UpperCAmelCase : Dict = last_kernel_size _UpperCAmelCase : List[Any] = residual_kernel_size _UpperCAmelCase : Optional[int] = dilation_growth_rate _UpperCAmelCase : Union[str, Any] = use_causal_conv _UpperCAmelCase : List[str] = pad_mode _UpperCAmelCase : Any = compress _UpperCAmelCase : Any = num_lstm_layers _UpperCAmelCase : Tuple = trim_right_ratio _UpperCAmelCase : List[str] = codebook_size _UpperCAmelCase : List[Any] = codebook_dim if codebook_dim is not None else hidden_size _UpperCAmelCase : Optional[Any] = use_conv_shortcut if self.norm_type not in ["weight_norm", "time_group_norm"]: raise ValueError( f'self.norm_type must be one of `"weight_norm"`, `"time_group_norm"`), got {self.norm_type}' ) super().__init__(**UpperCAmelCase__ ) @property def __lowerCAmelCase ( self ) -> Optional[int]: if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def __lowerCAmelCase ( self ) -> Optional[int]: if self.chunk_length_s is None or self.overlap is None: return None else: return max(1 , int((1.0 - self.overlap) * self.chunk_length ) ) @property def __lowerCAmelCase ( self ) -> int: _UpperCAmelCase : Optional[Any] = np.prod(self.upsampling_ratios ) return math.ceil(self.sampling_rate / hop_length ) @property def __lowerCAmelCase ( self ) -> int: return int(1_0_0_0 * self.target_bandwidths[-1] // (self.frame_rate * 1_0) )
506
'''simple docstring''' import unittest from transformers import DonutProcessor __lowerCAmelCase ="naver-clova-ix/donut-base" class _snake_case ( unittest.TestCase ): """simple docstring""" def __SCREAMING_SNAKE_CASE ( self ) -> Optional[Any]: a_ = DonutProcessor.from_pretrained(UpperCAmelCase__ ) def __SCREAMING_SNAKE_CASE ( self ) -> str: a_ = { 'name': 'John Doe', 'age': '99', 'city': 'Atlanta', 'state': 'GA', 'zip': '30301', 'phone': '123-4567', 'nicknames': [{'nickname': 'Johnny'}, {'nickname': 'JD'}], } a_ = ( '<s_name>John Doe</s_name><s_age>99</s_age><s_city>Atlanta</s_city>' '<s_state>GA</s_state><s_zip>30301</s_zip><s_phone>123-4567</s_phone>' '<s_nicknames><s_nickname>Johnny</s_nickname>' '<sep/><s_nickname>JD</s_nickname></s_nicknames>' ) a_ = self.processor.tokenajson(UpperCAmelCase__ ) self.assertDictEqual(UpperCAmelCase__ , UpperCAmelCase__ )
697
0
from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCAmelCase = {'''configuration_focalnet''': ['''FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''FocalNetConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase = [ '''FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST''', '''FocalNetForImageClassification''', '''FocalNetForMaskedImageModeling''', '''FocalNetBackbone''', '''FocalNetModel''', '''FocalNetPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_focalnet import FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FocalNetConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_focalnet import ( FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST, FocalNetBackbone, FocalNetForImageClassification, FocalNetForMaskedImageModeling, FocalNetModel, FocalNetPreTrainedModel, ) else: import sys UpperCAmelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
84
'''simple docstring''' import copy import inspect import unittest from transformers import AutoBackbone from transformers.configuration_utils import PretrainedConfig from transformers.testing_utils import require_timm, require_torch, torch_device from transformers.utils.import_utils import is_torch_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor if is_torch_available(): import torch from transformers import TimmBackbone, TimmBackboneConfig from ...test_pipeline_mixin import PipelineTesterMixin class _snake_case : """simple docstring""" def __init__( self , UpperCAmelCase__ , UpperCAmelCase__=None , UpperCAmelCase__=None , UpperCAmelCase__=None , UpperCAmelCase__="resnet50" , UpperCAmelCase__=3 , UpperCAmelCase__=32 , UpperCAmelCase__=3 , UpperCAmelCase__=True , UpperCAmelCase__=True , ) -> Optional[Any]: a_ = parent a_ = out_indices if out_indices is not None else [4] a_ = stage_names a_ = out_features a_ = backbone a_ = batch_size a_ = image_size a_ = num_channels a_ = use_pretrained_backbone a_ = is_training def __SCREAMING_SNAKE_CASE ( self ) -> str: a_ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) a_ = self.get_config() return config, pixel_values def __SCREAMING_SNAKE_CASE ( self ) -> Dict: return TimmBackboneConfig( image_size=self.image_size , num_channels=self.num_channels , out_features=self.out_features , out_indices=self.out_indices , stage_names=self.stage_names , use_pretrained_backbone=self.use_pretrained_backbone , backbone=self.backbone , ) def __SCREAMING_SNAKE_CASE ( self , UpperCAmelCase__ , UpperCAmelCase__ ) -> List[str]: a_ = TimmBackbone(config=UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.eval() with torch.no_grad(): a_ = model(UpperCAmelCase__ ) self.parent.assertEqual( result.feature_map[-1].shape , (self.batch_size, model.channels[-1], 14, 14) , ) def __SCREAMING_SNAKE_CASE ( self ) -> List[Any]: a_ = self.prepare_config_and_inputs() a_ , a_ = config_and_inputs a_ = {'pixel_values': pixel_values} return config, inputs_dict @require_torch @require_timm class _snake_case ( snake_case , snake_case , snake_case , unittest.TestCase ): """simple docstring""" _UpperCamelCase = (TimmBackbone,) if is_torch_available() else () _UpperCamelCase = {"feature-extraction": TimmBackbone} if is_torch_available() else {} _UpperCamelCase = False _UpperCamelCase = False _UpperCamelCase = False _UpperCamelCase = False def __SCREAMING_SNAKE_CASE ( self ) -> Union[str, Any]: a_ = TimmBackboneModelTester(self ) a_ = ConfigTester(self , config_class=UpperCAmelCase__ , has_text_modality=UpperCAmelCase__ ) def __SCREAMING_SNAKE_CASE ( self ) -> List[Any]: self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def __SCREAMING_SNAKE_CASE ( self ) -> Any: a_ = 'resnet18' a_ = 'microsoft/resnet-18' a_ = AutoBackbone.from_pretrained(UpperCAmelCase__ , use_timm_backbone=UpperCAmelCase__ ) a_ = AutoBackbone.from_pretrained(UpperCAmelCase__ ) self.assertEqual(len(timm_model.out_features ) , len(transformers_model.out_features ) ) self.assertEqual(len(timm_model.stage_names ) , len(transformers_model.stage_names ) ) self.assertEqual(timm_model.channels , transformers_model.channels ) # Out indices are set to the last layer by default. For timm models, we don't know # the number of layers in advance, so we set it to (-1,), whereas for transformers # models, we set it to [len(stage_names) - 1] (kept for backward compatibility). self.assertEqual(timm_model.out_indices , (-1,) ) self.assertEqual(transformers_model.out_indices , [len(timm_model.stage_names ) - 1] ) a_ = AutoBackbone.from_pretrained(UpperCAmelCase__ , use_timm_backbone=UpperCAmelCase__ , out_indices=[1, 2, 3] ) a_ = AutoBackbone.from_pretrained(UpperCAmelCase__ , out_indices=[1, 2, 3] ) self.assertEqual(timm_model.out_indices , transformers_model.out_indices ) self.assertEqual(len(timm_model.out_features ) , len(transformers_model.out_features ) ) self.assertEqual(timm_model.channels , transformers_model.channels ) @unittest.skip('TimmBackbone doesn\'t support feed forward chunking' ) def __SCREAMING_SNAKE_CASE ( self ) -> str: pass @unittest.skip('TimmBackbone doesn\'t have num_hidden_layers attribute' ) def __SCREAMING_SNAKE_CASE ( self ) -> List[Any]: pass @unittest.skip('TimmBackbone initialization is managed on the timm side' ) def __SCREAMING_SNAKE_CASE ( self ) -> Optional[int]: pass @unittest.skip('TimmBackbone models doesn\'t have inputs_embeds' ) def __SCREAMING_SNAKE_CASE ( self ) -> Optional[Any]: pass @unittest.skip('TimmBackbone models doesn\'t have inputs_embeds' ) def __SCREAMING_SNAKE_CASE ( self ) -> Any: pass @unittest.skip('TimmBackbone model cannot be created without specifying a backbone checkpoint' ) def __SCREAMING_SNAKE_CASE ( self ) -> Optional[int]: pass @unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone' ) def __SCREAMING_SNAKE_CASE ( self ) -> Tuple: pass @unittest.skip('model weights aren\'t tied in TimmBackbone.' ) def __SCREAMING_SNAKE_CASE ( self ) -> Optional[int]: pass @unittest.skip('model weights aren\'t tied in TimmBackbone.' ) def __SCREAMING_SNAKE_CASE ( self ) -> int: pass @unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone' ) def __SCREAMING_SNAKE_CASE ( self ) -> int: pass @unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone' ) def __SCREAMING_SNAKE_CASE ( self ) -> Any: pass @unittest.skip('TimmBackbone doesn\'t have hidden size info in its configuration.' ) def __SCREAMING_SNAKE_CASE ( self ) -> List[str]: pass @unittest.skip('TimmBackbone doesn\'t support output_attentions.' ) def __SCREAMING_SNAKE_CASE ( self ) -> Optional[int]: pass @unittest.skip('Safetensors is not supported by timm.' ) def __SCREAMING_SNAKE_CASE ( self ) -> str: pass @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.' ) def __SCREAMING_SNAKE_CASE ( self ) -> int: pass def __SCREAMING_SNAKE_CASE ( self ) -> Any: a_ , a_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a_ = model_class(UpperCAmelCase__ ) a_ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic a_ = [*signature.parameters.keys()] a_ = ['pixel_values'] self.assertListEqual(arg_names[:1] , UpperCAmelCase__ ) def __SCREAMING_SNAKE_CASE ( self ) -> Dict: a_ , a_ = self.model_tester.prepare_config_and_inputs_for_common() a_ = True a_ = self.has_attentions # no need to test all models as different heads yield the same functionality a_ = self.all_model_classes[0] a_ = model_class(UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) a_ = self._prepare_for_class(UpperCAmelCase__ , UpperCAmelCase__ ) a_ = model(**UpperCAmelCase__ ) a_ = outputs[0][-1] # Encoder-/Decoder-only models a_ = outputs.hidden_states[0] hidden_states.retain_grad() if self.has_attentions: a_ = outputs.attentions[0] attentions.retain_grad() output.flatten()[0].backward(retain_graph=UpperCAmelCase__ ) self.assertIsNotNone(hidden_states.grad ) if self.has_attentions: self.assertIsNotNone(attentions.grad ) def __SCREAMING_SNAKE_CASE ( self ) -> Any: a_ , a_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a_ = model_class(UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.eval() a_ = model(**UpperCAmelCase__ ) self.assertEqual(len(result.feature_maps ) , len(config.out_indices ) ) self.assertEqual(len(model.channels ) , len(config.out_indices ) ) # Check output of last stage is taken if out_features=None, out_indices=None a_ = copy.deepcopy(UpperCAmelCase__ ) a_ = None a_ = model_class(UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.eval() a_ = model(**UpperCAmelCase__ ) self.assertEqual(len(result.feature_maps ) , 1 ) self.assertEqual(len(model.channels ) , 1 ) # Check backbone can be initialized with fresh weights a_ = copy.deepcopy(UpperCAmelCase__ ) a_ = False a_ = model_class(UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.eval() a_ = model(**UpperCAmelCase__ )
697
0
import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( HubertConfig, HubertForCTC, HubertModel, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaProcessor, logging, ) logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { '''post_extract_proj''': '''feature_projection.projection''', '''encoder.pos_conv.0''': '''encoder.pos_conv_embed.conv''', '''self_attn.k_proj''': '''encoder.layers.*.attention.k_proj''', '''self_attn.v_proj''': '''encoder.layers.*.attention.v_proj''', '''self_attn.q_proj''': '''encoder.layers.*.attention.q_proj''', '''self_attn.out_proj''': '''encoder.layers.*.attention.out_proj''', '''self_attn_layer_norm''': '''encoder.layers.*.layer_norm''', '''fc1''': '''encoder.layers.*.feed_forward.intermediate_dense''', '''fc2''': '''encoder.layers.*.feed_forward.output_dense''', '''final_layer_norm''': '''encoder.layers.*.final_layer_norm''', '''encoder.layer_norm''': '''encoder.layer_norm''', '''w2v_model.layer_norm''': '''feature_projection.layer_norm''', '''w2v_encoder.proj''': '''lm_head''', '''mask_emb''': '''masked_spec_embed''', } def a_ (__A , __A , __A , __A , __A ) -> int: """simple docstring""" for attribute in key.split("." ): __a : Any = getattr(_UpperCAmelCase , _UpperCAmelCase ) if weight_type is not None: __a : int = getattr(_UpperCAmelCase , _UpperCAmelCase ).shape else: __a : Union[str, Any] = hf_pointer.shape assert hf_shape == value.shape, ( f'Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be' f' {value.shape} for {full_name}' ) if weight_type == "weight": __a : Optional[Any] = value elif weight_type == "weight_g": __a : List[Any] = value elif weight_type == "weight_v": __a : Any = value elif weight_type == "bias": __a : Tuple = value else: __a : str = value logger.info(f'{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.' ) def a_ (__A , __A , __A ) -> Dict: """simple docstring""" __a : List[Any] = [] __a : List[str] = fairseq_model.state_dict() __a : Optional[Any] = hf_model.hubert.feature_extractor if is_finetuned else hf_model.feature_extractor for name, value in fairseq_dict.items(): __a : int = False if "conv_layers" in name: load_conv_layer( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , hf_model.config.feat_extract_norm == "group" , ) __a : Dict = True else: for key, mapped_key in MAPPING.items(): __a : List[str] = "hubert." + mapped_key if (is_finetuned and mapped_key != "lm_head") else mapped_key if key in name or (key.split("w2v_model." )[-1] == name.split("." )[0] and not is_finetuned): __a : List[str] = True if "*" in mapped_key: __a : Union[str, Any] = name.split(_UpperCAmelCase )[0].split("." )[-2] __a : List[str] = mapped_key.replace("*" , _UpperCAmelCase ) if "weight_g" in name: __a : Any = "weight_g" elif "weight_v" in name: __a : Tuple = "weight_v" elif "weight" in name: __a : Optional[Any] = "weight" elif "bias" in name: __a : Any = "bias" else: __a : Optional[Any] = None set_recursively(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) continue if not is_used: unused_weights.append(_UpperCAmelCase ) logger.warning(f'Unused weights: {unused_weights}' ) def a_ (__A , __A , __A , __A , __A ) -> str: """simple docstring""" __a : Optional[Any] = full_name.split("conv_layers." )[-1] __a : Union[str, Any] = name.split("." ) __a : Union[str, Any] = int(items[0] ) __a : str = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( f'{full_name} has size {value.shape}, but' f' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.' ) __a : str = value logger.info(f'Feat extract conv layer {layer_id} was initialized from {full_name}.' ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( f'{full_name} has size {value.shape}, but' f' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.' ) __a : str = value logger.info(f'Feat extract conv layer {layer_id} was initialized from {full_name}.' ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( f'{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was' " found." ) __a : Union[str, Any] = value logger.info(f'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.' ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( f'{full_name} has size {value.shape}, but' f' {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.' ) __a : Optional[int] = value logger.info(f'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.' ) else: unused_weights.append(_UpperCAmelCase ) @torch.no_grad() def a_ (__A , __A , __A=None , __A=None , __A=True ) -> List[Any]: """simple docstring""" if config_path is not None: __a : List[Any] = HubertConfig.from_pretrained(_UpperCAmelCase ) else: __a : Any = HubertConfig() if is_finetuned: if dict_path: __a : List[Any] = Dictionary.load(_UpperCAmelCase ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq __a : Optional[Any] = target_dict.pad_index __a : List[str] = target_dict.bos_index __a : List[Any] = target_dict.eos_index __a : int = len(target_dict.symbols ) __a : Optional[Any] = os.path.join(_UpperCAmelCase , "vocab.json" ) if not os.path.isdir(_UpperCAmelCase ): logger.error("--pytorch_dump_folder_path ({}) should be a directory".format(_UpperCAmelCase ) ) return os.makedirs(_UpperCAmelCase , exist_ok=_UpperCAmelCase ) with open(_UpperCAmelCase , "w" , encoding="utf-8" ) as vocab_handle: json.dump(target_dict.indices , _UpperCAmelCase ) __a : int = WavaVecaCTCTokenizer( _UpperCAmelCase , unk_token=target_dict.unk_word , pad_token=target_dict.pad_word , bos_token=target_dict.bos_word , eos_token=target_dict.eos_word , word_delimiter_token="|" , do_lower_case=_UpperCAmelCase , ) __a : List[str] = True if config.feat_extract_norm == "layer" else False __a : Dict = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16_000 , padding_value=0 , do_normalize=_UpperCAmelCase , return_attention_mask=_UpperCAmelCase , ) __a : str = WavaVecaProcessor(feature_extractor=_UpperCAmelCase , tokenizer=_UpperCAmelCase ) processor.save_pretrained(_UpperCAmelCase ) __a : List[Any] = HubertForCTC(_UpperCAmelCase ) else: __a : Dict = HubertModel(_UpperCAmelCase ) if is_finetuned: __a , __a , __a : Optional[int] = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"data": "/".join(dict_path.split("/" )[:-1] )} ) else: __a , __a , __a : List[Any] = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] ) __a : Tuple = model[0].eval() recursively_load_weights(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) hf_wavavec.save_pretrained(_UpperCAmelCase ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument('''--checkpoint_path''', default=None, type=str, help='''Path to fairseq checkpoint''') parser.add_argument('''--dict_path''', default=None, type=str, help='''Path to dict of fine-tuned model''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') parser.add_argument( '''--not_finetuned''', action='''store_true''', help='''Whether the model to convert is a fine-tuned model or not''' ) UpperCAmelCase__ = parser.parse_args() convert_hubert_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned )
351
'''simple docstring''' import unittest import numpy as np from transformers.testing_utils import require_pytesseract, require_torch from transformers.utils import is_pytesseract_available, is_torch_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_pytesseract_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class _snake_case ( unittest.TestCase ): """simple docstring""" def __init__( self , UpperCAmelCase__ , UpperCAmelCase__=7 , UpperCAmelCase__=3 , UpperCAmelCase__=18 , UpperCAmelCase__=30 , UpperCAmelCase__=400 , UpperCAmelCase__=True , UpperCAmelCase__=None , UpperCAmelCase__=True , ) -> List[Any]: a_ = size if size is not None else {'height': 18, 'width': 18} a_ = parent a_ = batch_size a_ = num_channels a_ = image_size a_ = min_resolution a_ = max_resolution a_ = do_resize a_ = size a_ = apply_ocr def __SCREAMING_SNAKE_CASE ( self ) -> Optional[int]: return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class _snake_case ( snake_case , unittest.TestCase ): """simple docstring""" _UpperCamelCase = LayoutLMvaImageProcessor if is_pytesseract_available() else None def __SCREAMING_SNAKE_CASE ( self ) -> Optional[Any]: a_ = LayoutLMvaImageProcessingTester(self ) @property def __SCREAMING_SNAKE_CASE ( self ) -> Union[str, Any]: return self.image_processor_tester.prepare_image_processor_dict() def __SCREAMING_SNAKE_CASE ( self ) -> Optional[Any]: a_ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(UpperCAmelCase__ , 'do_resize' ) ) self.assertTrue(hasattr(UpperCAmelCase__ , 'size' ) ) self.assertTrue(hasattr(UpperCAmelCase__ , 'apply_ocr' ) ) def __SCREAMING_SNAKE_CASE ( self ) -> Optional[int]: a_ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'height': 18, 'width': 18} ) a_ = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'height': 42, 'width': 42} ) def __SCREAMING_SNAKE_CASE ( self ) -> Any: pass def __SCREAMING_SNAKE_CASE ( self ) -> List[str]: # Initialize image_processing a_ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images a_ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCAmelCase__ ) for image in image_inputs: self.assertIsInstance(UpperCAmelCase__ , Image.Image ) # Test not batched input a_ = image_processing(image_inputs[0] , return_tensors='pt' ) self.assertEqual( encoding.pixel_values.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) self.assertIsInstance(encoding.words , UpperCAmelCase__ ) self.assertIsInstance(encoding.boxes , UpperCAmelCase__ ) # Test batched a_ = image_processing(UpperCAmelCase__ , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) def __SCREAMING_SNAKE_CASE ( self ) -> Dict: # Initialize image_processing a_ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors a_ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCAmelCase__ , numpify=UpperCAmelCase__ ) for image in image_inputs: self.assertIsInstance(UpperCAmelCase__ , np.ndarray ) # Test not batched input a_ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched a_ = image_processing(UpperCAmelCase__ , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) def __SCREAMING_SNAKE_CASE ( self ) -> List[Any]: # Initialize image_processing a_ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors a_ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCAmelCase__ , torchify=UpperCAmelCase__ ) for image in image_inputs: self.assertIsInstance(UpperCAmelCase__ , torch.Tensor ) # Test not batched input a_ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched a_ = image_processing(UpperCAmelCase__ , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) def __SCREAMING_SNAKE_CASE ( self ) -> int: # with apply_OCR = True a_ = LayoutLMvaImageProcessor() from datasets import load_dataset a_ = load_dataset('hf-internal-testing/fixtures_docvqa' , split='test' ) a_ = Image.open(ds[0]['file'] ).convert('RGB' ) a_ = image_processing(UpperCAmelCase__ , return_tensors='pt' ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 224, 224) ) self.assertEqual(len(encoding.words ) , len(encoding.boxes ) ) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 a_ = [['11:14', 'to', '11:39', 'a.m', '11:39', 'to', '11:44', 'a.m.', '11:44', 'a.m.', 'to', '12:25', 'p.m.', '12:25', 'to', '12:58', 'p.m.', '12:58', 'to', '4:00', 'p.m.', '2:00', 'to', '5:00', 'p.m.', 'Coffee', 'Break', 'Coffee', 'will', 'be', 'served', 'for', 'men', 'and', 'women', 'in', 'the', 'lobby', 'adjacent', 'to', 'exhibit', 'area.', 'Please', 'move', 'into', 'exhibit', 'area.', '(Exhibits', 'Open)', 'TRRF', 'GENERAL', 'SESSION', '(PART', '|)', 'Presiding:', 'Lee', 'A.', 'Waller', 'TRRF', 'Vice', 'President', '“Introductory', 'Remarks”', 'Lee', 'A.', 'Waller,', 'TRRF', 'Vice', 'Presi-', 'dent', 'Individual', 'Interviews', 'with', 'TRRF', 'Public', 'Board', 'Members', 'and', 'Sci-', 'entific', 'Advisory', 'Council', 'Mem-', 'bers', 'Conducted', 'by', 'TRRF', 'Treasurer', 'Philip', 'G.', 'Kuehn', 'to', 'get', 'answers', 'which', 'the', 'public', 'refrigerated', 'warehousing', 'industry', 'is', 'looking', 'for.', 'Plus', 'questions', 'from', 'the', 'floor.', 'Dr.', 'Emil', 'M.', 'Mrak,', 'University', 'of', 'Cal-', 'ifornia,', 'Chairman,', 'TRRF', 'Board;', 'Sam', 'R.', 'Cecil,', 'University', 'of', 'Georgia', 'College', 'of', 'Agriculture;', 'Dr.', 'Stanley', 'Charm,', 'Tufts', 'University', 'School', 'of', 'Medicine;', 'Dr.', 'Robert', 'H.', 'Cotton,', 'ITT', 'Continental', 'Baking', 'Company;', 'Dr.', 'Owen', 'Fennema,', 'University', 'of', 'Wis-', 'consin;', 'Dr.', 'Robert', 'E.', 'Hardenburg,', 'USDA.', 'Questions', 'and', 'Answers', 'Exhibits', 'Open', 'Capt.', 'Jack', 'Stoney', 'Room', 'TRRF', 'Scientific', 'Advisory', 'Council', 'Meeting', 'Ballroom', 'Foyer']] # noqa: E231 a_ = [[[141, 57, 214, 69], [228, 58, 252, 69], [141, 75, 216, 88], [230, 79, 280, 88], [142, 260, 218, 273], [230, 261, 255, 273], [143, 279, 218, 290], [231, 282, 290, 291], [143, 342, 218, 354], [231, 345, 289, 355], [202, 362, 227, 373], [143, 379, 220, 392], [231, 382, 291, 394], [144, 714, 220, 726], [231, 715, 256, 726], [144, 732, 220, 745], [232, 736, 291, 747], [144, 769, 218, 782], [231, 770, 256, 782], [141, 788, 202, 801], [215, 791, 274, 804], [143, 826, 204, 838], [215, 826, 240, 838], [142, 844, 202, 857], [215, 847, 274, 859], [334, 57, 427, 69], [440, 57, 522, 69], [369, 75, 461, 88], [469, 75, 516, 88], [528, 76, 562, 88], [570, 76, 667, 88], [675, 75, 711, 87], [721, 79, 778, 88], [789, 75, 840, 88], [369, 97, 470, 107], [484, 94, 507, 106], [518, 94, 562, 107], [576, 94, 655, 110], [668, 94, 792, 109], [804, 95, 829, 107], [369, 113, 465, 125], [477, 116, 547, 125], [562, 113, 658, 125], [671, 116, 748, 125], [761, 113, 811, 125], [369, 131, 465, 143], [477, 133, 548, 143], [563, 130, 698, 145], [710, 130, 802, 146], [336, 171, 412, 183], [423, 171, 572, 183], [582, 170, 716, 184], [728, 171, 817, 187], [829, 171, 844, 186], [338, 197, 482, 212], [507, 196, 557, 209], [569, 196, 595, 208], [610, 196, 702, 209], [505, 214, 583, 226], [595, 214, 656, 227], [670, 215, 807, 227], [335, 259, 543, 274], [556, 259, 708, 272], [372, 279, 422, 291], [435, 279, 460, 291], [474, 279, 574, 292], [587, 278, 664, 291], [676, 278, 738, 291], [751, 279, 834, 291], [372, 298, 434, 310], [335, 341, 483, 354], [497, 341, 655, 354], [667, 341, 728, 354], [740, 341, 825, 354], [335, 360, 430, 372], [442, 360, 534, 372], [545, 359, 687, 372], [697, 360, 754, 372], [765, 360, 823, 373], [334, 378, 428, 391], [440, 378, 577, 394], [590, 378, 705, 391], [720, 378, 801, 391], [334, 397, 400, 409], [370, 416, 529, 429], [544, 416, 576, 432], [587, 416, 665, 428], [677, 416, 814, 429], [372, 435, 452, 450], [465, 434, 495, 447], [511, 434, 600, 447], [611, 436, 637, 447], [649, 436, 694, 451], [705, 438, 824, 447], [369, 453, 452, 466], [464, 454, 509, 466], [522, 453, 611, 469], [625, 453, 792, 469], [370, 472, 556, 488], [570, 472, 684, 487], [697, 472, 718, 485], [732, 472, 835, 488], [369, 490, 411, 503], [425, 490, 484, 503], [496, 490, 635, 506], [645, 490, 707, 503], [718, 491, 761, 503], [771, 490, 840, 503], [336, 510, 374, 521], [388, 510, 447, 522], [460, 510, 489, 521], [503, 510, 580, 522], [592, 509, 736, 525], [745, 509, 770, 522], [781, 509, 840, 522], [338, 528, 434, 541], [448, 528, 596, 541], [609, 527, 687, 540], [700, 528, 792, 541], [336, 546, 397, 559], [407, 546, 431, 559], [443, 546, 525, 560], [537, 546, 680, 562], [688, 546, 714, 559], [722, 546, 837, 562], [336, 565, 449, 581], [461, 565, 485, 577], [497, 565, 665, 581], [681, 565, 718, 577], [732, 565, 837, 580], [337, 584, 438, 597], [452, 583, 521, 596], [535, 584, 677, 599], [690, 583, 787, 596], [801, 583, 825, 596], [338, 602, 478, 615], [492, 602, 530, 614], [543, 602, 638, 615], [650, 602, 676, 614], [688, 602, 788, 615], [802, 602, 843, 614], [337, 621, 502, 633], [516, 621, 615, 637], [629, 621, 774, 636], [789, 621, 827, 633], [337, 639, 418, 652], [432, 640, 571, 653], [587, 639, 731, 655], [743, 639, 769, 652], [780, 639, 841, 652], [338, 658, 440, 673], [455, 658, 491, 670], [508, 658, 602, 671], [616, 658, 638, 670], [654, 658, 835, 674], [337, 677, 429, 689], [337, 714, 482, 726], [495, 714, 548, 726], [561, 714, 683, 726], [338, 770, 461, 782], [474, 769, 554, 785], [489, 788, 562, 803], [576, 788, 643, 801], [656, 787, 751, 804], [764, 788, 844, 801], [334, 825, 421, 838], [430, 824, 574, 838], [584, 824, 723, 841], [335, 844, 450, 857], [464, 843, 583, 860], [628, 862, 755, 875], [769, 861, 848, 878]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words , UpperCAmelCase__ ) self.assertListEqual(encoding.boxes , UpperCAmelCase__ ) # with apply_OCR = False a_ = LayoutLMvaImageProcessor(apply_ocr=UpperCAmelCase__ ) a_ = image_processing(UpperCAmelCase__ , return_tensors='pt' ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 224, 224) )
697
0
'''simple docstring''' from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __magic_name__ = logging.get_logger(__name__) __magic_name__ = { 'facebook/levit-128S': 'https://huggingface.co/facebook/levit-128S/resolve/main/config.json', # See all LeViT models at https://huggingface.co/models?filter=levit } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' a_ = """levit""" def __init__( self : List[str] ,_a : Any=224 ,_a : int=3 ,_a : Dict=3 ,_a : Optional[Any]=2 ,_a : Dict=1 ,_a : Dict=16 ,_a : Dict=[128, 256, 384] ,_a : List[Any]=[4, 8, 12] ,_a : List[str]=[4, 4, 4] ,_a : int=[16, 16, 16] ,_a : Any=0 ,_a : List[Any]=[2, 2, 2] ,_a : Optional[int]=[2, 2, 2] ,_a : Union[str, Any]=0.02 ,**_a : int ,): '''simple docstring''' super().__init__(**UpperCAmelCase__ ) A_ : Union[str, Any] = image_size A_ : int = num_channels A_ : List[Any] = kernel_size A_ : Dict = stride A_ : Optional[Any] = padding A_ : Dict = hidden_sizes A_ : Any = num_attention_heads A_ : List[Any] = depths A_ : Tuple = key_dim A_ : Any = drop_path_rate A_ : int = patch_size A_ : str = attention_ratio A_ : str = mlp_ratio A_ : Optional[Any] = initializer_range A_ : List[Any] = [ ["""Subsample""", key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2], ["""Subsample""", key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2], ] class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' a_ = version.parse("""1.11""" ) @property def _a ( self : Optional[Any] ): '''simple docstring''' return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def _a ( self : Optional[Any] ): '''simple docstring''' return 1e-4
665
'''simple docstring''' import math def a ( _UpperCAmelCase ) -> bool: """simple docstring""" if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(_UpperCAmelCase ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def a ( _UpperCAmelCase = 1_0_0_0_1 ) -> int: """simple docstring""" try: a_ = int(_UpperCAmelCase ) except (TypeError, ValueError): raise TypeError('Parameter nth must be int or castable to int.' ) from None if nth <= 0: raise ValueError('Parameter nth must be greater than or equal to one.' ) a_ = [] a_ = 2 while len(_UpperCAmelCase ) < nth: if is_prime(_UpperCAmelCase ): primes.append(_UpperCAmelCase ) num += 1 else: num += 1 return primes[len(_UpperCAmelCase ) - 1] if __name__ == "__main__": print(f'''{solution() = }''')
697
0
'''simple docstring''' def _lowerCamelCase ( lowercase : Optional[Any] ) -> None: _a = generate_pascal_triangle(_UpperCAmelCase ) for row_idx in range(_UpperCAmelCase ): # Print left spaces for _ in range(num_rows - row_idx - 1 ): print(end=" " ) # Print row values for col_idx in range(row_idx + 1 ): if col_idx != row_idx: print(triangle[row_idx][col_idx] , end=" " ) else: print(triangle[row_idx][col_idx] , end="" ) print() def _lowerCamelCase ( lowercase : Dict ) -> list[list[int]]: if not isinstance(_UpperCAmelCase , _UpperCAmelCase ): raise TypeError("The input value of \'num_rows\' should be \'int\'" ) if num_rows == 0: return [] elif num_rows < 0: raise ValueError( "The input value of \'num_rows\' should be greater than or equal to 0" ) _a = [] for current_row_idx in range(_UpperCAmelCase ): _a = populate_current_row(_UpperCAmelCase , _UpperCAmelCase ) triangle.append(_UpperCAmelCase ) return triangle def _lowerCamelCase ( lowercase : str , lowercase : Any ) -> list[int]: _a = [-1] * (current_row_idx + 1) # first and last elements of current row are equal to 1 _a , _a = 1, 1 for current_col_idx in range(1 , _UpperCAmelCase ): calculate_current_element( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) return current_row def _lowerCamelCase ( lowercase : str , lowercase : int , lowercase : Dict , lowercase : Optional[Any] , ) -> None: _a = triangle[current_row_idx - 1][current_col_idx - 1] _a = triangle[current_row_idx - 1][current_col_idx] _a = above_to_left_elt + above_to_right_elt def _lowerCamelCase ( lowercase : Optional[Any] ) -> list[list[int]]: if not isinstance(_UpperCAmelCase , _UpperCAmelCase ): raise TypeError("The input value of \'num_rows\' should be \'int\'" ) if num_rows == 0: return [] elif num_rows < 0: raise ValueError( "The input value of \'num_rows\' should be greater than or equal to 0" ) _a = [[1]] for row_index in range(1 , _UpperCAmelCase ): _a = [0] + result[-1] + [0] _a = row_index + 1 # Calculate the number of distinct elements in a row _a = sum(divmod(_UpperCAmelCase , 2 ) ) _a = [ temp_row[i - 1] + temp_row[i] for i in range(1 , distinct_elements + 1 ) ] _a = row_first_half[: (row_index + 1) // 2] row_second_half.reverse() _a = row_first_half + row_second_half result.append(_UpperCAmelCase ) return result def _lowerCamelCase ( ) -> None: from collections.abc import Callable from timeit import timeit def benchmark_a_function(lowercase : Union[str, Any] , lowercase : Tuple ) -> None: _a = F'{func.__name__}({value})' _a = timeit(F'__main__.{call}' , setup="import __main__" ) # print(f"{call:38} = {func(value)} -- {timing:.4f} seconds") print(F'{call:38} -- {timing:.4f} seconds' ) for value in range(15 ): # (1, 7, 14): for func in (generate_pascal_triangle, generate_pascal_triangle_optimized): benchmark_a_function(_UpperCAmelCase , _UpperCAmelCase ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
692
'''simple docstring''' import unittest import numpy as np import torch from .utils_summarization import build_mask, compute_token_type_ids, process_story, truncate_or_pad class _snake_case ( unittest.TestCase ): """simple docstring""" def __SCREAMING_SNAKE_CASE ( self ) -> List[str]: a_ = 10 def __SCREAMING_SNAKE_CASE ( self ) -> Union[str, Any]: a_ = [1, 2, 3, 4] a_ = [1, 2, 3, 4, 0, 0, 0, 0, 0, 0] self.assertEqual(truncate_or_pad(UpperCAmelCase__ , self.block_size , 0 ) , UpperCAmelCase__ ) def __SCREAMING_SNAKE_CASE ( self ) -> Optional[int]: a_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] self.assertEqual(truncate_or_pad(UpperCAmelCase__ , self.block_size , 0 ) , UpperCAmelCase__ ) def __SCREAMING_SNAKE_CASE ( self ) -> Tuple: a_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] a_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] self.assertEqual(truncate_or_pad(UpperCAmelCase__ , self.block_size , 0 ) , UpperCAmelCase__ ) def __SCREAMING_SNAKE_CASE ( self ) -> Union[str, Any]: a_ = 'It was the year of Our Lord one thousand seven hundred and\n seventy-five.\n\nSpiritual revelations were conceded to England at that\n favoured period, as at this.' a_ , a_ = process_story(UpperCAmelCase__ ) self.assertEqual(UpperCAmelCase__ , [] ) def __SCREAMING_SNAKE_CASE ( self ) -> int: a_ = '' a_ , a_ = process_story(UpperCAmelCase__ ) self.assertEqual(UpperCAmelCase__ , [] ) self.assertEqual(UpperCAmelCase__ , [] ) def __SCREAMING_SNAKE_CASE ( self ) -> Union[str, Any]: a_ = ( 'It was the year of Our Lord one thousand seven hundred and ' 'seventy-five\n\nSpiritual revelations were conceded to England ' 'at that favoured period, as at this.\n@highlight\n\nIt was the best of times' ) a_ , a_ = process_story(UpperCAmelCase__ ) a_ = [ 'It was the year of Our Lord one thousand seven hundred and seventy-five.', 'Spiritual revelations were conceded to England at that favoured period, as at this.', ] self.assertEqual(UpperCAmelCase__ , UpperCAmelCase__ ) a_ = ['It was the best of times.'] self.assertEqual(UpperCAmelCase__ , UpperCAmelCase__ ) def __SCREAMING_SNAKE_CASE ( self ) -> Union[str, Any]: a_ = torch.tensor([1, 2, 3, 4] ) a_ = torch.tensor([1, 1, 1, 1] ) np.testing.assert_array_equal(build_mask(UpperCAmelCase__ , 0 ).numpy() , expected.numpy() ) def __SCREAMING_SNAKE_CASE ( self ) -> Optional[int]: a_ = torch.tensor([1, 2, 3, 4, 23, 23, 23] ) a_ = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(UpperCAmelCase__ , 23 ).numpy() , expected.numpy() ) def __SCREAMING_SNAKE_CASE ( self ) -> List[Any]: a_ = torch.tensor([8, 2, 3, 4, 1, 1, 1] ) a_ = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(UpperCAmelCase__ , 1 ).numpy() , expected.numpy() ) def __SCREAMING_SNAKE_CASE ( self ) -> int: a_ = 101 a_ = torch.tensor([[1, 2, 3, 4, 5, 6], [1, 2, 3, 101, 5, 6], [1, 101, 3, 4, 101, 6]] ) a_ = torch.tensor([[1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 0, 0], [1, 0, 0, 0, 1, 1]] ) a_ = compute_token_type_ids(UpperCAmelCase__ , UpperCAmelCase__ ) np.testing.assert_array_equal(UpperCAmelCase__ , UpperCAmelCase__ )
697
0
import argparse import glob import logging import os from argparse import Namespace from importlib import import_module import numpy as np import torch from lightning_base import BaseTransformer, add_generic_args, generic_train from seqeval.metrics import accuracy_score, fa_score, precision_score, recall_score from torch.nn import CrossEntropyLoss from torch.utils.data import DataLoader, TensorDataset from utils_ner import TokenClassificationTask snake_case__ : str = logging.getLogger(__name__) class _a ( A__ ): """simple docstring""" snake_case ="""token-classification""" def __init__( self , _snake_case ): if type(UpperCAmelCase__ ) == dict: _UpperCAmelCase =Namespace(**UpperCAmelCase__ ) _UpperCAmelCase =import_module("tasks" ) try: _UpperCAmelCase =getattr(UpperCAmelCase__ , hparams.task_type ) _UpperCAmelCase =token_classification_task_clazz() except AttributeError: raise ValueError( F"Task {hparams.task_type} needs to be defined as a TokenClassificationTask subclass in {module}. " F"Available tasks classes are: {TokenClassificationTask.__subclasses__()}" ) _UpperCAmelCase =self.token_classification_task.get_labels(hparams.labels ) _UpperCAmelCase =CrossEntropyLoss().ignore_index super().__init__(UpperCAmelCase__ , len(self.labels ) , self.mode ) def SCREAMING_SNAKE_CASE ( self , **_snake_case ): return self.model(**UpperCAmelCase__ ) def SCREAMING_SNAKE_CASE ( self , _snake_case , _snake_case ): _UpperCAmelCase ={"input_ids": batch[0], "attention_mask": batch[1], "labels": batch[3]} if self.config.model_type != "distilbert": _UpperCAmelCase =( batch[2] if self.config.model_type in ["bert", "xlnet"] else None ) # XLM and RoBERTa don"t use token_type_ids _UpperCAmelCase =self(**UpperCAmelCase__ ) _UpperCAmelCase =outputs[0] # tensorboard_logs = {"loss": loss, "rate": self.lr_scheduler.get_last_lr()[-1]} return {"loss": loss} def SCREAMING_SNAKE_CASE ( self ): _UpperCAmelCase =self.hparams for mode in ["train", "dev", "test"]: _UpperCAmelCase =self._feature_file(UpperCAmelCase__ ) if os.path.exists(UpperCAmelCase__ ) and not args.overwrite_cache: logger.info("Loading features from cached file %s" , UpperCAmelCase__ ) _UpperCAmelCase =torch.load(UpperCAmelCase__ ) else: logger.info("Creating features from dataset file at %s" , args.data_dir ) _UpperCAmelCase =self.token_classification_task.read_examples_from_file(args.data_dir , UpperCAmelCase__ ) _UpperCAmelCase =self.token_classification_task.convert_examples_to_features( UpperCAmelCase__ , self.labels , args.max_seq_length , self.tokenizer , cls_token_at_end=bool(self.config.model_type in ["xlnet"] ) , cls_token=self.tokenizer.cls_token , cls_token_segment_id=2 if self.config.model_type in ["xlnet"] else 0 , sep_token=self.tokenizer.sep_token , sep_token_extra=UpperCAmelCase__ , pad_on_left=bool(self.config.model_type in ["xlnet"] ) , pad_token=self.tokenizer.pad_token_id , pad_token_segment_id=self.tokenizer.pad_token_type_id , pad_token_label_id=self.pad_token_label_id , ) logger.info("Saving features into cached file %s" , UpperCAmelCase__ ) torch.save(UpperCAmelCase__ , UpperCAmelCase__ ) def SCREAMING_SNAKE_CASE ( self , _snake_case , _snake_case , _snake_case = False ): _UpperCAmelCase =self._feature_file(UpperCAmelCase__ ) logger.info("Loading features from cached file %s" , UpperCAmelCase__ ) _UpperCAmelCase =torch.load(UpperCAmelCase__ ) _UpperCAmelCase =torch.tensor([f.input_ids for f in features] , dtype=torch.long ) _UpperCAmelCase =torch.tensor([f.attention_mask for f in features] , dtype=torch.long ) if features[0].token_type_ids is not None: _UpperCAmelCase =torch.tensor([f.token_type_ids for f in features] , dtype=torch.long ) else: _UpperCAmelCase =torch.tensor([0 for f in features] , dtype=torch.long ) # HACK(we will not use this anymore soon) _UpperCAmelCase =torch.tensor([f.label_ids for f in features] , dtype=torch.long ) return DataLoader( TensorDataset(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) , batch_size=UpperCAmelCase__ ) def SCREAMING_SNAKE_CASE ( self , _snake_case , _snake_case ): """Compute validation""" "" _UpperCAmelCase ={"input_ids": batch[0], "attention_mask": batch[1], "labels": batch[3]} if self.config.model_type != "distilbert": _UpperCAmelCase =( batch[2] if self.config.model_type in ["bert", "xlnet"] else None ) # XLM and RoBERTa don"t use token_type_ids _UpperCAmelCase =self(**UpperCAmelCase__ ) _UpperCAmelCase , _UpperCAmelCase =outputs[:2] _UpperCAmelCase =logits.detach().cpu().numpy() _UpperCAmelCase =inputs["labels"].detach().cpu().numpy() return {"val_loss": tmp_eval_loss.detach().cpu(), "pred": preds, "target": out_label_ids} def SCREAMING_SNAKE_CASE ( self , _snake_case ): _UpperCAmelCase =torch.stack([x["val_loss"] for x in outputs] ).mean() _UpperCAmelCase =np.concatenate([x["pred"] for x in outputs] , axis=0 ) _UpperCAmelCase =np.argmax(UpperCAmelCase__ , axis=2 ) _UpperCAmelCase =np.concatenate([x["target"] for x in outputs] , axis=0 ) _UpperCAmelCase =dict(enumerate(self.labels ) ) _UpperCAmelCase =[[] for _ in range(out_label_ids.shape[0] )] _UpperCAmelCase =[[] for _ in range(out_label_ids.shape[0] )] for i in range(out_label_ids.shape[0] ): for j in range(out_label_ids.shape[1] ): if out_label_ids[i, j] != self.pad_token_label_id: out_label_list[i].append(label_map[out_label_ids[i][j]] ) preds_list[i].append(label_map[preds[i][j]] ) _UpperCAmelCase ={ "val_loss": val_loss_mean, "accuracy_score": accuracy_score(UpperCAmelCase__ , UpperCAmelCase__ ), "precision": precision_score(UpperCAmelCase__ , UpperCAmelCase__ ), "recall": recall_score(UpperCAmelCase__ , UpperCAmelCase__ ), "f1": fa_score(UpperCAmelCase__ , UpperCAmelCase__ ), } _UpperCAmelCase =dict(results.items() ) _UpperCAmelCase =results return ret, preds_list, out_label_list def SCREAMING_SNAKE_CASE ( self , _snake_case ): # when stable _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase =self._eval_end(UpperCAmelCase__ ) _UpperCAmelCase =ret["log"] return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs} def SCREAMING_SNAKE_CASE ( self , _snake_case ): # updating to test_epoch_end instead of deprecated test_end _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase =self._eval_end(UpperCAmelCase__ ) # Converting to the dict required by pl # https://github.com/PyTorchLightning/pytorch-lightning/blob/master/\ # pytorch_lightning/trainer/logging.py#L139 _UpperCAmelCase =ret["log"] # `val_loss` is the key returned by `self._eval_end()` but actually refers to `test_loss` return {"avg_test_loss": logs["val_loss"], "log": logs, "progress_bar": logs} @staticmethod def SCREAMING_SNAKE_CASE ( _snake_case , _snake_case ): # Add NER specific options BaseTransformer.add_model_specific_args(UpperCAmelCase__ , UpperCAmelCase__ ) parser.add_argument( "--task_type" , default="NER" , type=UpperCAmelCase__ , help="Task type to fine tune in training (e.g. NER, POS, etc)" ) parser.add_argument( "--max_seq_length" , default=128 , type=UpperCAmelCase__ , help=( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) , ) parser.add_argument( "--labels" , default="" , type=UpperCAmelCase__ , help="Path to a file containing all labels. If not specified, CoNLL-2003 labels are used." , ) parser.add_argument( "--gpus" , default=0 , type=UpperCAmelCase__ , help="The number of GPUs allocated for this, it is by default 0 meaning none" , ) parser.add_argument( "--overwrite_cache" , action="store_true" , help="Overwrite the cached training and evaluation sets" ) return parser if __name__ == "__main__": snake_case__ : int = argparse.ArgumentParser() add_generic_args(parser, os.getcwd()) snake_case__ : Dict = NERTransformer.add_model_specific_args(parser, os.getcwd()) snake_case__ : Optional[int] = parser.parse_args() snake_case__ : Dict = NERTransformer(args) snake_case__ : Union[str, Any] = generic_train(model, args) if args.do_predict: # See https://github.com/huggingface/transformers/issues/3159 # pl use this default format to create a checkpoint: # https://github.com/PyTorchLightning/pytorch-lightning/blob/master\ # /pytorch_lightning/callbacks/model_checkpoint.py#L322 snake_case__ : Dict = sorted(glob.glob(os.path.join(args.output_dir, 'checkpoint-epoch=*.ckpt'), recursive=True)) snake_case__ : List[str] = model.load_from_checkpoint(checkpoints[-1]) trainer.test(model)
408
'''simple docstring''' import unittest from transformers import load_tool from .test_tools_common import ToolTesterMixin __lowerCAmelCase ="\nHugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf originally as a company that developed a chatbot app targeted at teenagers.[2] After open-sourcing the model behind the chatbot, the company pivoted to focus on being a platform for machine learning.\n\nIn March 2021, Hugging Face raised $40 million in a Series B funding round.[3]\n\nOn April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model.[4] In 2022, the workshop concluded with the announcement of BLOOM, a multilingual large language model with 176 billion parameters.[5]\n" class _snake_case ( unittest.TestCase , snake_case ): """simple docstring""" def __SCREAMING_SNAKE_CASE ( self ) -> int: a_ = load_tool('text-question-answering' ) self.tool.setup() a_ = load_tool('text-question-answering' , remote=UpperCAmelCase__ ) def __SCREAMING_SNAKE_CASE ( self ) -> Any: a_ = self.tool(UpperCAmelCase__ , 'What did Hugging Face do in April 2021?' ) self.assertEqual(UpperCAmelCase__ , 'launched the BigScience Research Workshop' ) def __SCREAMING_SNAKE_CASE ( self ) -> Any: a_ = self.remote_tool(UpperCAmelCase__ , 'What did Hugging Face do in April 2021?' ) self.assertEqual(UpperCAmelCase__ , 'launched the BigScience Research Workshop' ) def __SCREAMING_SNAKE_CASE ( self ) -> Tuple: a_ = self.tool(text=UpperCAmelCase__ , question='What did Hugging Face do in April 2021?' ) self.assertEqual(UpperCAmelCase__ , 'launched the BigScience Research Workshop' ) def __SCREAMING_SNAKE_CASE ( self ) -> List[str]: a_ = self.remote_tool(text=UpperCAmelCase__ , question='What did Hugging Face do in April 2021?' ) self.assertEqual(UpperCAmelCase__ , 'launched the BigScience Research Workshop' )
697
0
'''simple docstring''' from __future__ import annotations import math def _lowerCAmelCase ( lowerCamelCase_ : str , lowerCamelCase_ : Any ): __lowercase = u for i in range(1 , _UpperCAmelCase ): __lowercase = temp * (u - i) return temp def _lowerCAmelCase ( ): __lowercase = int(input('''enter the numbers of values: ''' ) ) __lowercase = [] for _ in range(_UpperCAmelCase ): y.append([] ) for i in range(_UpperCAmelCase ): for j in range(_UpperCAmelCase ): y[i].append(_UpperCAmelCase ) __lowercase = 0 print('''enter the values of parameters in a list: ''' ) __lowercase = list(map(_UpperCAmelCase , input().split() ) ) print('''enter the values of corresponding parameters: ''' ) for i in range(_UpperCAmelCase ): __lowercase = float(input() ) __lowercase = int(input('''enter the value to interpolate: ''' ) ) __lowercase = (value - x[0]) / (x[1] - x[0]) # for calculating forward difference table for i in range(1 , _UpperCAmelCase ): for j in range(n - i ): __lowercase = y[j + 1][i - 1] - y[j][i - 1] __lowercase = y[0][0] for i in range(1 , _UpperCAmelCase ): summ += (ucal(_UpperCAmelCase , _UpperCAmelCase ) * y[0][i]) / math.factorial(_UpperCAmelCase ) print(f"the value at {value} is {summ}" ) if __name__ == "__main__": main()
502
'''simple docstring''' from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCAmelCase ={"configuration_focalnet": ["FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP", "FocalNetConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase =[ "FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST", "FocalNetForImageClassification", "FocalNetForMaskedImageModeling", "FocalNetBackbone", "FocalNetModel", "FocalNetPreTrainedModel", ] if TYPE_CHECKING: from .configuration_focalnet import FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FocalNetConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_focalnet import ( FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST, FocalNetBackbone, FocalNetForImageClassification, FocalNetForMaskedImageModeling, FocalNetModel, FocalNetPreTrainedModel, ) else: import sys __lowerCAmelCase =_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
697
0
'''simple docstring''' import warnings from ...utils import logging from .image_processing_segformer import SegformerImageProcessor _snake_case : Dict = logging.get_logger(__name__) class A ( _a ): def __init__( self : Dict , *lowerCAmelCase_ : Optional[Any] , **lowerCAmelCase_ : str ) -> None: """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__ )
22
'''simple docstring''' from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __lowerCAmelCase =logging.get_logger(__name__) __lowerCAmelCase ={ "google/vit-base-patch16-224": "https://huggingface.co/vit-base-patch16-224/resolve/main/config.json", # See all ViT models at https://huggingface.co/models?filter=vit } class _snake_case ( snake_case ): """simple docstring""" _UpperCamelCase = "vit" def __init__( self , UpperCAmelCase__=768 , UpperCAmelCase__=12 , UpperCAmelCase__=12 , UpperCAmelCase__=3072 , UpperCAmelCase__="gelu" , UpperCAmelCase__=0.0 , UpperCAmelCase__=0.0 , UpperCAmelCase__=0.0_2 , UpperCAmelCase__=1e-12 , UpperCAmelCase__=224 , UpperCAmelCase__=16 , UpperCAmelCase__=3 , UpperCAmelCase__=True , UpperCAmelCase__=16 , **UpperCAmelCase__ , ) -> Dict: super().__init__(**UpperCAmelCase__ ) a_ = hidden_size a_ = num_hidden_layers a_ = num_attention_heads a_ = intermediate_size a_ = hidden_act a_ = hidden_dropout_prob a_ = attention_probs_dropout_prob a_ = initializer_range a_ = layer_norm_eps a_ = image_size a_ = patch_size a_ = num_channels a_ = qkv_bias a_ = encoder_stride class _snake_case ( snake_case ): """simple docstring""" _UpperCamelCase = version.parse("1.11" ) @property def __SCREAMING_SNAKE_CASE ( self ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def __SCREAMING_SNAKE_CASE ( self ) -> float: return 1e-4
697
0
from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { '''SCUT-DLVCLab/lilt-roberta-en-base''': ( '''https://huggingface.co/SCUT-DLVCLab/lilt-roberta-en-base/resolve/main/config.json''' ), } class _snake_case ( _lowercase ): lowerCamelCase__: str = "lilt" def __init__( self: List[Any] , __lowerCamelCase: Optional[int]=3_05_22 , __lowerCamelCase: Any=7_68 , __lowerCamelCase: Any=12 , __lowerCamelCase: List[str]=12 , __lowerCamelCase: Dict=30_72 , __lowerCamelCase: List[str]="gelu" , __lowerCamelCase: Optional[Any]=0.1 , __lowerCamelCase: List[Any]=0.1 , __lowerCamelCase: Optional[Any]=5_12 , __lowerCamelCase: Union[str, Any]=2 , __lowerCamelCase: Dict=0.02 , __lowerCamelCase: Dict=1e-12 , __lowerCamelCase: Optional[int]=0 , __lowerCamelCase: List[Any]="absolute" , __lowerCamelCase: Tuple=None , __lowerCamelCase: Any=4 , __lowerCamelCase: Dict=10_24 , **__lowerCamelCase: Optional[Any] , ) -> Optional[Any]: super().__init__(pad_token_id=UpperCAmelCase__ , **UpperCAmelCase__ ) __UpperCAmelCase : Optional[Any] = vocab_size __UpperCAmelCase : Any = hidden_size __UpperCAmelCase : Optional[Any] = num_hidden_layers __UpperCAmelCase : List[Any] = num_attention_heads __UpperCAmelCase : Tuple = hidden_act __UpperCAmelCase : Optional[Any] = intermediate_size __UpperCAmelCase : Tuple = hidden_dropout_prob __UpperCAmelCase : int = attention_probs_dropout_prob __UpperCAmelCase : Union[str, Any] = max_position_embeddings __UpperCAmelCase : Dict = type_vocab_size __UpperCAmelCase : Optional[int] = initializer_range __UpperCAmelCase : Optional[int] = layer_norm_eps __UpperCAmelCase : Union[str, Any] = position_embedding_type __UpperCAmelCase : Any = classifier_dropout __UpperCAmelCase : Optional[int] = channel_shrink_ratio __UpperCAmelCase : Dict = max_ad_position_embeddings
382
'''simple docstring''' def a ( _UpperCAmelCase = 5_0 ) -> int: """simple docstring""" a_ = [1] * (length + 1) for row_length in range(3 , length + 1 ): for block_length in range(3 , row_length + 1 ): for block_start in range(row_length - block_length ): ways_number[row_length] += ways_number[ row_length - block_start - block_length - 1 ] ways_number[row_length] += 1 return ways_number[length] if __name__ == "__main__": print(f'''{solution() = }''')
697
0
import warnings from diffusers import StableDiffusionInpaintPipeline as StableDiffusionInpaintPipeline # noqa F401 warnings.warn( """The `inpainting.py` script is outdated. Please use directly `from diffusers import""" """ StableDiffusionInpaintPipeline` instead.""" )
204
'''simple docstring''' def a ( _UpperCAmelCase , _UpperCAmelCase ) -> int: """simple docstring""" return int(input_a == input_a == 0 ) def a ( ) -> None: """simple docstring""" print('Truth Table of NOR Gate:' ) print('| Input 1 | Input 2 | Output |' ) print(F'''| 0 | 0 | {nor_gate(0 , 0 )} |''' ) print(F'''| 0 | 1 | {nor_gate(0 , 1 )} |''' ) print(F'''| 1 | 0 | {nor_gate(1 , 0 )} |''' ) print(F'''| 1 | 1 | {nor_gate(1 , 1 )} |''' ) if __name__ == "__main__": import doctest doctest.testmod() main()
697
0
import argparse import torch from transformers import GPTaConfig, GPTaModel, load_tf_weights_in_gpta from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() def UpperCamelCase( __UpperCamelCase : Optional[int] ,__UpperCamelCase : Any ,__UpperCamelCase : Optional[int] ): if gpta_config_file == "": lowerCAmelCase_ : Optional[Any] = GPTaConfig() else: lowerCAmelCase_ : List[str] = GPTaConfig.from_json_file(_UpperCAmelCase ) lowerCAmelCase_ : Any = GPTaModel(_UpperCAmelCase ) # Load weights from numpy load_tf_weights_in_gpta(_UpperCAmelCase ,_UpperCAmelCase ,_UpperCAmelCase ) # Save pytorch-model lowerCAmelCase_ : List[Any] = pytorch_dump_folder_path + '''/''' + WEIGHTS_NAME lowerCAmelCase_ : Dict = pytorch_dump_folder_path + '''/''' + CONFIG_NAME print(f"""Save PyTorch model to {pytorch_weights_dump_path}""" ) torch.save(model.state_dict() ,_UpperCAmelCase ) print(f"""Save configuration file to {pytorch_config_dump_path}""" ) with open(_UpperCAmelCase ,'''w''' ,encoding='''utf-8''' ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": A__ : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--gpt2_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.''' ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) parser.add_argument( '''--gpt2_config_file''', default='''''', type=str, help=( '''An optional config json file corresponding to the pre-trained OpenAI model. \n''' '''This specifies the model architecture.''' ), ) A__ : str = parser.parse_args() convert_gpta_checkpoint_to_pytorch(args.gpta_checkpoint_path, args.gpta_config_file, args.pytorch_dump_folder_path)
171
'''simple docstring''' from argparse import ArgumentParser from . import BaseTransformersCLICommand def a ( _UpperCAmelCase ) -> int: """simple docstring""" return DownloadCommand(args.model , args.cache_dir , args.force , args.trust_remote_code ) class _snake_case ( snake_case ): """simple docstring""" @staticmethod def __SCREAMING_SNAKE_CASE ( UpperCAmelCase__ ) -> str: a_ = parser.add_parser('download' ) download_parser.add_argument( '--cache-dir' , type=UpperCAmelCase__ , default=UpperCAmelCase__ , help='Path to location to store the models' ) download_parser.add_argument( '--force' , action='store_true' , help='Force the model to be download even if already in cache-dir' ) download_parser.add_argument( '--trust-remote-code' , action='store_true' , help='Whether or not to allow for custom models defined on the Hub in their own modeling files. Use only if you\'ve reviewed the code as it will execute on your local machine' , ) download_parser.add_argument('model' , type=UpperCAmelCase__ , help='Name of the model to download' ) download_parser.set_defaults(func=UpperCAmelCase__ ) def __init__( self , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) -> List[str]: a_ = model a_ = cache a_ = force a_ = trust_remote_code def __SCREAMING_SNAKE_CASE ( self ) -> Union[str, Any]: from ..models.auto import AutoModel, AutoTokenizer AutoModel.from_pretrained( self._model , cache_dir=self._cache , force_download=self._force , trust_remote_code=self._trust_remote_code ) AutoTokenizer.from_pretrained( self._model , cache_dir=self._cache , force_download=self._force , trust_remote_code=self._trust_remote_code )
697
0
"""simple docstring""" import os from pathlib import Path def lowerCamelCase_ (): from torch.utils.cpp_extension import load _UpperCAmelCase : Tuple = Path(_UpperCAmelCase ).resolve().parent.parent.parent / '''kernels''' / '''deformable_detr''' _UpperCAmelCase : Optional[Any] = [ root / filename for filename in [ '''vision.cpp''', os.path.join('''cpu''' , '''ms_deform_attn_cpu.cpp''' ), os.path.join('''cuda''' , '''ms_deform_attn_cuda.cu''' ), ] ] load( '''MultiScaleDeformableAttention''' , _UpperCAmelCase , with_cuda=_UpperCAmelCase , extra_include_paths=[str(_UpperCAmelCase )] , extra_cflags=['''-DWITH_CUDA=1'''] , extra_cuda_cflags=[ '''-DCUDA_HAS_FP16=1''', '''-D__CUDA_NO_HALF_OPERATORS__''', '''-D__CUDA_NO_HALF_CONVERSIONS__''', '''-D__CUDA_NO_HALF2_OPERATORS__''', ] , ) import MultiScaleDeformableAttention as MSDA return MSDA
506
'''simple docstring''' def a ( _UpperCAmelCase ) -> int: """simple docstring""" assert column_title.isupper() a_ = 0 a_ = len(_UpperCAmelCase ) - 1 a_ = 0 while index >= 0: a_ = (ord(column_title[index] ) - 6_4) * pow(2_6 , _UpperCAmelCase ) answer += value power += 1 index -= 1 return answer if __name__ == "__main__": from doctest import testmod testmod()
697
0
import numpy as np from PIL import Image def UpperCAmelCase_ ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): lowercase = np.array(_UpperCAmelCase ) if arr.shape[0] != arr.shape[1]: raise ValueError('The input array is not a square matrix' ) lowercase = 0 lowercase = 0 lowercase = 0 lowercase = 0 # compute the shape of the output matrix lowercase = (arr.shape[0] - size) // stride + 1 # initialize the output matrix with zeros of shape maxpool_shape lowercase = np.zeros((maxpool_shape, maxpool_shape) ) while i < arr.shape[0]: if i + size > arr.shape[0]: # if the end of the matrix is reached, break break while j < arr.shape[1]: # if the end of the matrix is reached, break if j + size > arr.shape[1]: break # compute the maximum of the pooling matrix lowercase = np.max(arr[i : i + size, j : j + size] ) # shift the pooling matrix by stride of column pixels j += stride mat_j += 1 # shift the pooling matrix by stride of row pixels i += stride mat_i += 1 # reset the column index to 0 lowercase = 0 lowercase = 0 return updated_arr def UpperCAmelCase_ ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): lowercase = np.array(_UpperCAmelCase ) if arr.shape[0] != arr.shape[1]: raise ValueError('The input array is not a square matrix' ) lowercase = 0 lowercase = 0 lowercase = 0 lowercase = 0 # compute the shape of the output matrix lowercase = (arr.shape[0] - size) // stride + 1 # initialize the output matrix with zeros of shape avgpool_shape lowercase = np.zeros((avgpool_shape, avgpool_shape) ) while i < arr.shape[0]: # if the end of the matrix is reached, break if i + size > arr.shape[0]: break while j < arr.shape[1]: # if the end of the matrix is reached, break if j + size > arr.shape[1]: break # compute the average of the pooling matrix lowercase = int(np.average(arr[i : i + size, j : j + size] ) ) # shift the pooling matrix by stride of column pixels j += stride mat_j += 1 # shift the pooling matrix by stride of row pixels i += stride mat_i += 1 # reset the column index to 0 lowercase = 0 lowercase = 0 return updated_arr # Main Function if __name__ == "__main__": from doctest import testmod testmod(name='''avgpooling''', verbose=True) # Loading the image UpperCAmelCase = Image.open('''path_to_image''') # Converting the image to numpy array and maxpooling, displaying the result # Ensure that the image is a square matrix Image.fromarray(maxpooling(np.array(image), size=3, stride=2)).show() # Converting the image to numpy array and averagepooling, displaying the result # Ensure that the image is a square matrix Image.fromarray(avgpooling(np.array(image), size=3, stride=2)).show()
84
'''simple docstring''' from math import ceil from typing import List, Optional, Union import numpy as np from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import BatchFeature, SequenceFeatureExtractor from ...utils import TensorType, logging __lowerCAmelCase =logging.get_logger(__name__) class _snake_case ( snake_case ): """simple docstring""" _UpperCamelCase = ["audio_values", "audio_mask"] def __init__( self , UpperCAmelCase__=2048 , UpperCAmelCase__=1 , UpperCAmelCase__=[16, 16] , UpperCAmelCase__=128 , UpperCAmelCase__=4_4100 , UpperCAmelCase__=86 , UpperCAmelCase__=2048 , UpperCAmelCase__=0.0 , **UpperCAmelCase__ , ) -> Union[str, Any]: super().__init__( feature_size=UpperCAmelCase__ , sampling_rate=UpperCAmelCase__ , padding_value=UpperCAmelCase__ , **UpperCAmelCase__ , ) a_ = spectrogram_length a_ = num_channels a_ = patch_size a_ = feature_size // self.patch_size[1] a_ = n_fft a_ = sampling_rate // hop_length_to_sampling_rate a_ = sampling_rate a_ = padding_value a_ = mel_filter_bank( num_frequency_bins=1 + n_fft // 2 , num_mel_filters=UpperCAmelCase__ , min_frequency=0.0 , max_frequency=2_2_0_5_0.0 , sampling_rate=UpperCAmelCase__ , norm='slaney' , mel_scale='slaney' , ).T def __SCREAMING_SNAKE_CASE ( self , UpperCAmelCase__ ) -> np.ndarray: a_ = spectrogram( UpperCAmelCase__ , window_function(self.n_fft , 'hann' ) , frame_length=self.n_fft , hop_length=self.hop_length , power=2.0 , mel_filters=self.mel_filters.T , log_mel='dB' , db_range=8_0.0 , ) a_ = log_spec[:, :-1] a_ = log_spec - 2_0.0 a_ = np.clip(log_spec / 4_0.0 , -2.0 , 0.0 ) + 1.0 return log_spec def __call__( self , UpperCAmelCase__ , UpperCAmelCase__ = None , UpperCAmelCase__ = True , UpperCAmelCase__ = None , UpperCAmelCase__ = False , UpperCAmelCase__ = False , **UpperCAmelCase__ , ) -> BatchFeature: if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( 'This feature extractor is set to support sampling rate' F''' of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled''' F''' with {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( 'It is strongly recommended to pass the `sampling_rate` argument to this function. ' 'Failing to do so can result in silent errors that might be hard to debug.' ) a_ = isinstance(UpperCAmelCase__ , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(F'''Only mono-channel audio is supported for input to {self}''' ) a_ = is_batched_numpy or ( isinstance(UpperCAmelCase__ , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: a_ = [np.asarray([speech] , dtype=np.floataa ).T for speech in raw_speech] elif not is_batched and not isinstance(UpperCAmelCase__ , np.ndarray ): a_ = np.asarray(UpperCAmelCase__ , dtype=np.floataa ) elif isinstance(UpperCAmelCase__ , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): a_ = raw_speech.astype(np.floataa ) # always return batch if not is_batched: a_ = [np.asarray([raw_speech] ).T] # Convert audio signals to log mel spectrograms, truncate by time axis a_ = [ self._np_extract_fbank_features(waveform.squeeze() ).T[: self.spectrogram_length] for waveform in raw_speech ] if isinstance(audio_features[0] , UpperCAmelCase__ ): a_ = [np.asarray(UpperCAmelCase__ , dtype=np.floataa ) for feature in audio_features] # Create audio attention mask a_ = max( [ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len for feature in audio_features] ) # The maximum number of audio patches in a batch if return_attention_mask: a_ = [ (ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len) * [1] + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len) * [0] for feature in audio_features ] a_ = np.array(UpperCAmelCase__ ).astype(np.floataa ) # convert into correct format for padding a_ = max_patch_len // self.freq_len * self.patch_size[0] # The maximum audio size in a batch a_ = np.ones([len(UpperCAmelCase__ ), 1, max_time_len, self.feature_size] ).astype(np.floataa ) a_ = padded_audio_features * self.padding_value for i in range(len(UpperCAmelCase__ ) ): a_ = audio_features[i] a_ = feature # return as BatchFeature if return_attention_mask: a_ = {'audio_values': padded_audio_features, 'audio_mask': audio_mask} else: a_ = {'audio_values': padded_audio_features} a_ = BatchFeature(data=UpperCAmelCase__ , tensor_type=UpperCAmelCase__ ) return encoded_inputs
697
0
"""simple docstring""" import os import re import warnings from shutil import copyfile from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer if TYPE_CHECKING: from ...tokenization_utils_base import TextInput from ...utils import logging lowerCamelCase__ : List[Any] = logging.get_logger(__name__) lowerCamelCase__ : Tuple = {"vocab_file": "spiece.model"} lowerCamelCase__ : Any = { "vocab_file": { "t5-small": "https://huggingface.co/t5-small/resolve/main/spiece.model", "t5-base": "https://huggingface.co/t5-base/resolve/main/spiece.model", "t5-large": "https://huggingface.co/t5-large/resolve/main/spiece.model", "t5-3b": "https://huggingface.co/t5-3b/resolve/main/spiece.model", "t5-11b": "https://huggingface.co/t5-11b/resolve/main/spiece.model", } } # TODO(PVP) - this should be removed in Transformers v5 lowerCamelCase__ : Dict = { "t5-small": 512, "t5-base": 512, "t5-large": 512, "t5-3b": 512, "t5-11b": 512, } lowerCamelCase__ : Dict = "▁" class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = VOCAB_FILES_NAMES UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase = ["""input_ids""", """attention_mask"""] def __init__( self :Any , lowerCamelCase_ :List[str] , lowerCamelCase_ :List[Any]="</s>" , lowerCamelCase_ :List[str]="<unk>" , lowerCamelCase_ :str="<pad>" , lowerCamelCase_ :Tuple=1_00 , lowerCamelCase_ :Optional[int]=None , lowerCamelCase_ :Optional[Dict[str, Any]] = None , lowerCamelCase_ :Dict=True , **lowerCamelCase_ :List[str] , ) -> None: '''simple docstring''' if extra_ids > 0 and additional_special_tokens is None: SCREAMING_SNAKE_CASE : List[Any] = [f"<extra_id_{i}>" for i in range(lowerCamelCase_ )] elif extra_ids > 0 and additional_special_tokens is not None: # Check that we have the right number of extra_id special tokens SCREAMING_SNAKE_CASE : List[Any] = len(set(filter(lambda lowerCamelCase_ : bool('''extra_id''' in str(lowerCamelCase_ ) ) , lowerCamelCase_ ) ) ) if extra_tokens != extra_ids: raise ValueError( f"Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are" ''' provided to T5Tokenizer. In this case the additional_special_tokens must include the extra_ids''' ''' tokens''' ) if legacy: logger.warning_once( f"You are using the legacy behaviour of the {self.__class__}. This means that tokens that come after special tokens will not be properly handled. We recommend you to" ''' read the related pull request available at https://github.com/huggingface/transformers/pull/24565''' ) SCREAMING_SNAKE_CASE : List[str] = legacy SCREAMING_SNAKE_CASE : Optional[Any] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( eos_token=lowerCamelCase_ , unk_token=lowerCamelCase_ , pad_token=lowerCamelCase_ , extra_ids=lowerCamelCase_ , additional_special_tokens=lowerCamelCase_ , sp_model_kwargs=self.sp_model_kwargs , legacy=lowerCamelCase_ , **lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : int = vocab_file SCREAMING_SNAKE_CASE : Optional[int] = extra_ids SCREAMING_SNAKE_CASE : str = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(lowerCamelCase_ ) @staticmethod def __lowerCAmelCase ( lowerCamelCase_ :str , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :List[Any] ) -> Union[str, Any]: '''simple docstring''' if pretrained_model_name_or_path in TaTokenizer.max_model_input_sizes: SCREAMING_SNAKE_CASE : Dict = TaTokenizer.max_model_input_sizes[pretrained_model_name_or_path] if init_max_model_length is not None and init_max_model_length != max_model_length: return init_max_model_length elif init_max_model_length is None: warnings.warn( '''This tokenizer was incorrectly instantiated with a model max length of''' f" {deprecated_max_model_length} which will be corrected in Transformers v5.\nFor now, this" ''' behavior is kept to avoid breaking backwards compatibility when padding/encoding with''' ''' `truncation is True`.\n- Be aware that you SHOULD NOT rely on''' f" {pretrained_model_name_or_path} automatically truncating your input to" f" {deprecated_max_model_length} when padding/encoding.\n- If you want to encode/pad to sequences" f" longer than {deprecated_max_model_length} you can either instantiate this tokenizer with" ''' `model_max_length` or pass `max_length` when encoding/padding.\n- To avoid this warning, please''' ''' instantiate this tokenizer with `model_max_length` set to your preferred value.''' , lowerCamelCase_ , ) return max_model_length @property def __lowerCAmelCase ( self :Optional[Any] ) -> Any: '''simple docstring''' return self.sp_model.get_piece_size() + self._extra_ids def __lowerCAmelCase ( self :Any ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = {self.convert_ids_to_tokens(lowerCamelCase_ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :List[int] , lowerCamelCase_ :Optional[List[int]] = None , lowerCamelCase_ :bool = False ) -> List[int]: '''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_ ) # normal case: some special tokens if token_ids_a is None: return ([0] * len(lowerCamelCase_ )) + [1] return ([0] * len(lowerCamelCase_ )) + [1] + ([0] * len(lowerCamelCase_ )) + [1] def __lowerCAmelCase ( self :List[Any] ) -> Any: '''simple docstring''' return list( set(filter(lambda lowerCamelCase_ : bool(re.search(R'''<extra_id_\d+>''' , lowerCamelCase_ ) ) is not None , self.additional_special_tokens ) ) ) def __lowerCAmelCase ( self :Optional[Any] ) -> Tuple: '''simple docstring''' return [self._convert_token_to_id(lowerCamelCase_ ) for token in self.get_sentinel_tokens()] def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :List[int] ) -> List[int]: '''simple docstring''' if len(lowerCamelCase_ ) > 0 and token_ids[-1] == self.eos_token_id: warnings.warn( f"This sequence already has {self.eos_token}. In future versions this behavior may lead to duplicated" ''' eos tokens being added.''' ) return token_ids else: return token_ids + [self.eos_token_id] def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :List[int] , lowerCamelCase_ :Optional[List[int]] = None ) -> List[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = [self.eos_token_id] if token_ids_a is None: return len(token_ids_a + eos ) * [0] return len(token_ids_a + eos + token_ids_a + eos ) * [0] def __lowerCAmelCase ( self :Any , lowerCamelCase_ :List[int] , lowerCamelCase_ :Optional[List[int]] = None ) -> List[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = self._add_eos_if_not_present(lowerCamelCase_ ) if token_ids_a is None: return token_ids_a else: SCREAMING_SNAKE_CASE : Optional[Any] = self._add_eos_if_not_present(lowerCamelCase_ ) return token_ids_a + token_ids_a def __getstate__( self :Optional[int] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.__dict__.copy() SCREAMING_SNAKE_CASE : Tuple = None return state def __setstate__( self :Dict , lowerCamelCase_ :List[str] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): SCREAMING_SNAKE_CASE : int = {} SCREAMING_SNAKE_CASE : List[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __lowerCAmelCase ( self :str , lowerCamelCase_ :"TextInput" , **lowerCamelCase_ :Optional[int] ) -> List[str]: '''simple docstring''' if not self.legacy: SCREAMING_SNAKE_CASE : Optional[int] = SPIECE_UNDERLINE + text.replace(lowerCamelCase_ , ''' ''' ) return super().tokenize(lowerCamelCase_ , **lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :Dict , **lowerCamelCase_ :Union[str, Any] ) -> str: '''simple docstring''' if not self.legacy: SCREAMING_SNAKE_CASE : Union[str, Any] = text.startswith(lowerCamelCase_ ) if is_first: SCREAMING_SNAKE_CASE : Tuple = text[1:] SCREAMING_SNAKE_CASE : Optional[Any] = self.sp_model.encode(lowerCamelCase_ , out_type=lowerCamelCase_ ) if not self.legacy and not is_first and not text.startswith(''' ''' ) and tokens[0].startswith(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Optional[Any] = ([tokens[0][1:]] if len(tokens[0] ) > 1 else []) + tokens[1:] return tokens def __lowerCAmelCase ( self :str , lowerCamelCase_ :List[Any] ) -> Tuple: '''simple docstring''' if token.startswith('''<extra_id_''' ): SCREAMING_SNAKE_CASE : Tuple = re.match(R'''<extra_id_(\d+)>''' , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = int(match.group(1 ) ) return self.vocab_size - num - 1 return self.sp_model.piece_to_id(lowerCamelCase_ ) def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :Dict ) -> Union[str, Any]: '''simple docstring''' if index < self.sp_model.get_piece_size(): SCREAMING_SNAKE_CASE : str = self.sp_model.IdToPiece(lowerCamelCase_ ) else: SCREAMING_SNAKE_CASE : Optional[int] = f"<extra_id_{self.vocab_size - 1 - index}>" return token def __lowerCAmelCase ( self :int , lowerCamelCase_ :Any ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = [] SCREAMING_SNAKE_CASE : Optional[int] = '''''' SCREAMING_SNAKE_CASE : Optional[Any] = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(lowerCamelCase_ ) + token SCREAMING_SNAKE_CASE : Optional[int] = True SCREAMING_SNAKE_CASE : Any = [] else: current_sub_tokens.append(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = False out_string += self.sp_model.decode(lowerCamelCase_ ) return out_string.strip() def __lowerCAmelCase ( self :Tuple , lowerCamelCase_ :str , lowerCamelCase_ :Optional[str] = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(lowerCamelCase_ ): logger.error(f"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE : Any = os.path.join( lowerCamelCase_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(lowerCamelCase_ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , lowerCamelCase_ ) elif not os.path.isfile(self.vocab_file ): with open(lowerCamelCase_ , '''wb''' ) as fi: SCREAMING_SNAKE_CASE : Optional[int] = self.sp_model.serialized_model_proto() fi.write(lowerCamelCase_ ) return (out_vocab_file,)
698
"""simple docstring""" from unittest import TestCase from datasets import Sequence, Value from datasets.arrow_dataset import Dataset class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __lowerCAmelCase ( self :Union[str, Any] ) -> str: '''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 __lowerCAmelCase ( self :Dict ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = {'''col_1''': [3, 2, 1, 0], '''col_2''': ['''a''', '''b''', '''c''', '''d''']} return Dataset.from_dict(lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[int] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self._create_example_records() SCREAMING_SNAKE_CASE : List[Any] = Dataset.from_list(lowerCamelCase_ ) self.assertListEqual(dset.column_names , ['''col_1''', '''col_2'''] ) for i, r in enumerate(lowerCamelCase_ ): self.assertDictEqual(lowerCamelCase_ , example_records[i] ) def __lowerCAmelCase ( self :Dict ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : str = self._create_example_records() SCREAMING_SNAKE_CASE : Optional[int] = Dataset.from_list(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = 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 __lowerCAmelCase ( self :List[str] ) -> Dict: # checks what happens with missing columns '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = [{'''col_1''': 1}, {'''col_2''': '''x'''}] SCREAMING_SNAKE_CASE : Optional[int] = Dataset.from_list(lowerCamelCase_ ) self.assertDictEqual(dset[0] , {'''col_1''': 1} ) self.assertDictEqual(dset[1] , {'''col_1''': None} ) # NB: first record is used for columns def __lowerCAmelCase ( self :Tuple ) -> Optional[Any]: # checks if the type can be inferred from the second record '''simple docstring''' SCREAMING_SNAKE_CASE : int = [{'''col_1''': []}, {'''col_1''': [1, 2]}] SCREAMING_SNAKE_CASE : List[str] = Dataset.from_list(lowerCamelCase_ ) self.assertEqual(dset.info.features['''col_1'''] , Sequence(Value('''int64''' ) ) ) def __lowerCAmelCase ( self :Any ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = Dataset.from_list([] ) self.assertEqual(len(lowerCamelCase_ ) , 0 ) self.assertListEqual(dset.column_names , [] )
698
1
"""simple docstring""" lowerCamelCase__ : Dict = "Tobias Carryer" from time import time class lowercase__: '''simple docstring''' def __init__( self :str , lowerCamelCase_ :str , lowerCamelCase_ :List[str] , lowerCamelCase_ :Tuple , lowerCamelCase_ :str=int(time() ) ) -> List[str]: # noqa: B008 '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = multiplier SCREAMING_SNAKE_CASE : Dict = increment SCREAMING_SNAKE_CASE : Optional[int] = modulo SCREAMING_SNAKE_CASE : List[Any] = seed def __lowerCAmelCase ( self :Tuple ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = (self.multiplier * self.seed + self.increment) % self.modulo return self.seed if __name__ == "__main__": # Show the LCG in action. lowerCamelCase__ : Any = LinearCongruentialGenerator(1664525, 1013904223, 2 << 31) while True: print(lcg.next_number())
698
"""simple docstring""" from __future__ import annotations import math from collections.abc import Callable def __A ( a_ : Callable[[int | float], int | float] , a_ : int | float , a_ : int | float , a_ : int = 1_00 , )-> float: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = x_start SCREAMING_SNAKE_CASE : Union[str, Any] = fnc(a_ ) SCREAMING_SNAKE_CASE : Optional[int] = 0.0 for _ in range(a_ ): # Approximates curve as a sequence of linear lines and sums their length SCREAMING_SNAKE_CASE : int = (x_end - x_start) / steps + xa SCREAMING_SNAKE_CASE : Optional[int] = fnc(a_ ) length += math.hypot(xa - xa , fxa - fxa ) # Increment step SCREAMING_SNAKE_CASE : str = xa SCREAMING_SNAKE_CASE : Any = fxa return length if __name__ == "__main__": def __A ( a_ : Optional[Any] )-> List[Any]: '''simple docstring''' return math.sin(10 * x ) print("f(x) = sin(10 * x)") print("The length of the curve from x = -10 to x = 10 is:") lowerCamelCase__ : str = 10 while i <= 100000: print(f'''With {i} steps: {line_length(f, -10, 10, i)}''') i *= 10
698
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : int = logging.get_logger(__name__) lowerCamelCase__ : Dict = { "facebook/dpr-ctx_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/config.json" ), "facebook/dpr-question_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/config.json" ), "facebook/dpr-reader-single-nq-base": ( "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/config.json" ), "facebook/dpr-ctx_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/config.json" ), "facebook/dpr-question_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/config.json" ), "facebook/dpr-reader-multiset-base": ( "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/config.json" ), } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """dpr""" def __init__( self :Any , lowerCamelCase_ :str=3_05_22 , lowerCamelCase_ :Union[str, Any]=7_68 , lowerCamelCase_ :List[Any]=12 , lowerCamelCase_ :Optional[int]=12 , lowerCamelCase_ :int=30_72 , lowerCamelCase_ :Any="gelu" , lowerCamelCase_ :str=0.1 , lowerCamelCase_ :Tuple=0.1 , lowerCamelCase_ :int=5_12 , lowerCamelCase_ :Any=2 , lowerCamelCase_ :str=0.0_2 , lowerCamelCase_ :Union[str, Any]=1E-12 , lowerCamelCase_ :List[str]=0 , lowerCamelCase_ :List[Any]="absolute" , lowerCamelCase_ :int = 0 , **lowerCamelCase_ :Dict , ) -> Optional[Any]: '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = vocab_size SCREAMING_SNAKE_CASE : Dict = hidden_size SCREAMING_SNAKE_CASE : Dict = num_hidden_layers SCREAMING_SNAKE_CASE : Union[str, Any] = num_attention_heads SCREAMING_SNAKE_CASE : Tuple = hidden_act SCREAMING_SNAKE_CASE : Optional[int] = intermediate_size SCREAMING_SNAKE_CASE : Tuple = hidden_dropout_prob SCREAMING_SNAKE_CASE : Tuple = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Tuple = max_position_embeddings SCREAMING_SNAKE_CASE : Tuple = type_vocab_size SCREAMING_SNAKE_CASE : int = initializer_range SCREAMING_SNAKE_CASE : Tuple = layer_norm_eps SCREAMING_SNAKE_CASE : Tuple = projection_dim SCREAMING_SNAKE_CASE : Optional[int] = position_embedding_type
698
"""simple docstring""" # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os from accelerate.test_utils import execute_subprocess_async def __A ( a_ : int=None )-> Tuple: '''simple docstring''' if subparsers is not None: SCREAMING_SNAKE_CASE : List[str] = subparsers.add_parser('''test''' ) else: SCREAMING_SNAKE_CASE : int = argparse.ArgumentParser('''Accelerate test command''' ) parser.add_argument( '''--config_file''' , default=a_ , help=( '''The path to use to store the config file. Will default to a file named default_config.yaml in the cache ''' '''location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have ''' '''such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed ''' '''with \'huggingface\'.''' ) , ) if subparsers is not None: parser.set_defaults(func=a_ ) return parser def __A ( a_ : Any )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = os.path.sep.join(__file__.split(os.path.sep )[:-2] + ['''test_utils''', '''scripts''', '''test_script.py'''] ) if args.config_file is None: SCREAMING_SNAKE_CASE : Tuple = script_name else: SCREAMING_SNAKE_CASE : Optional[Any] = F"--config_file={args.config_file} {script_name}" SCREAMING_SNAKE_CASE : str = ['''accelerate-launch'''] + test_args.split() SCREAMING_SNAKE_CASE : List[str] = execute_subprocess_async(a_ , env=os.environ.copy() ) if result.returncode == 0: print('''Test is a success! You are ready for your distributed training!''' ) def __A ( )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : str = test_command_parser() SCREAMING_SNAKE_CASE : Optional[Any] = parser.parse_args() test_command(a_ ) if __name__ == "__main__": main()
698
1
"""simple docstring""" # tests directory-specific settings - this file is run automatically # by pytest before any tests are run import doctest import sys import warnings from os.path import abspath, dirname, join import _pytest from transformers.testing_utils import HfDoctestModule, HfDocTestParser # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. lowerCamelCase__ : Optional[int] = abspath(join(dirname(__file__), "src")) sys.path.insert(1, git_repo_path) # silence FutureWarning warnings in tests since often we can't act on them until # they become normal warnings - i.e. the tests still need to test the current functionality warnings.simplefilter(action="ignore", category=FutureWarning) def __A ( a_ : Dict )-> str: '''simple docstring''' config.addinivalue_line( '''markers''' , '''is_pt_tf_cross_test: mark test to run only when PT and TF interactions are tested''' ) config.addinivalue_line( '''markers''' , '''is_pt_flax_cross_test: mark test to run only when PT and FLAX interactions are tested''' ) config.addinivalue_line('''markers''' , '''is_pipeline_test: mark test to run only when pipelines are tested''' ) config.addinivalue_line('''markers''' , '''is_staging_test: mark test to run only in the staging environment''' ) config.addinivalue_line('''markers''' , '''accelerate_tests: mark test that require accelerate''' ) config.addinivalue_line('''markers''' , '''tool_tests: mark the tool tests that are run on their specific schedule''' ) def __A ( a_ : Dict )-> Tuple: '''simple docstring''' from transformers.testing_utils import pytest_addoption_shared pytest_addoption_shared(a_ ) def __A ( a_ : Union[str, Any] )-> List[Any]: '''simple docstring''' from transformers.testing_utils import pytest_terminal_summary_main SCREAMING_SNAKE_CASE : List[str] = terminalreporter.config.getoption('''--make-reports''' ) if make_reports: pytest_terminal_summary_main(a_ , id=a_ ) def __A ( a_ : Dict , a_ : List[str] )-> Dict: '''simple docstring''' if exitstatus == 5: SCREAMING_SNAKE_CASE : List[str] = 0 # Doctest custom flag to ignore output. lowerCamelCase__ : Tuple = doctest.register_optionflag("IGNORE_RESULT") lowerCamelCase__ : Optional[int] = doctest.OutputChecker class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :int , lowerCamelCase_ :int , lowerCamelCase_ :Optional[Any] ) -> Dict: '''simple docstring''' if IGNORE_RESULT & optionflags: return True return OutputChecker.check_output(self , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) lowerCamelCase__ : str = CustomOutputChecker lowerCamelCase__ : Any = HfDoctestModule lowerCamelCase__ : int = HfDocTestParser
698
"""simple docstring""" def __A ( a_ : int = 10 , a_ : int = 10_00 , a_ : bool = True )-> int: '''simple docstring''' assert ( isinstance(a_ , a_ ) and isinstance(a_ , a_ ) and isinstance(a_ , a_ ) ), "Invalid type of value(s) specified to function!" if min_val > max_val: raise ValueError('''Invalid value for min_val or max_val (min_value < max_value)''' ) return min_val if option else max_val def __A ( a_ : int , a_ : int )-> int: '''simple docstring''' return int((number_a + number_a) / 2 ) def __A ( a_ : int , a_ : int , a_ : int )-> None: '''simple docstring''' assert ( isinstance(a_ , a_ ) and isinstance(a_ , a_ ) and isinstance(a_ , a_ ) ), 'argument values must be type of "int"' if lower > higher: raise ValueError('''argument value for lower and higher must be(lower > higher)''' ) if not lower < to_guess < higher: raise ValueError( '''guess value must be within the range of lower and higher value''' ) def answer(a_ : int ) -> str: if number > to_guess: return "high" elif number < to_guess: return "low" else: return "same" print('''started...''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = lower SCREAMING_SNAKE_CASE : int = higher SCREAMING_SNAKE_CASE : List[str] = [] while True: SCREAMING_SNAKE_CASE : Any = get_avg(a_ , a_ ) last_numbers.append(a_ ) if answer(a_ ) == "low": SCREAMING_SNAKE_CASE : Dict = number elif answer(a_ ) == "high": SCREAMING_SNAKE_CASE : Tuple = number else: break print(F"guess the number : {last_numbers[-1]}" ) print(F"details : {last_numbers!s}" ) def __A ( )-> None: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = int(input('''Enter lower value : ''' ).strip() ) SCREAMING_SNAKE_CASE : Tuple = int(input('''Enter high value : ''' ).strip() ) SCREAMING_SNAKE_CASE : List[str] = int(input('''Enter value to guess : ''' ).strip() ) guess_the_number(a_ , a_ , a_ ) if __name__ == "__main__": main()
698
1
"""simple docstring""" from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, is_valid_image, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) def __A ( a_ : Tuple )-> List[List[ImageInput]]: '''simple docstring''' if isinstance(a_ , (list, tuple) ) and isinstance(videos[0] , (list, tuple) ) and is_valid_image(videos[0][0] ): return videos elif isinstance(a_ , (list, tuple) ) and is_valid_image(videos[0] ): return [videos] elif is_valid_image(a_ ): return [[videos]] raise ValueError(F"Could not make batched video from {videos}" ) class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = ["""pixel_values"""] def __init__( self :Dict , lowerCamelCase_ :bool = True , lowerCamelCase_ :Dict[str, int] = None , lowerCamelCase_ :PILImageResampling = PILImageResampling.BILINEAR , lowerCamelCase_ :bool = True , lowerCamelCase_ :Dict[str, int] = None , lowerCamelCase_ :bool = True , lowerCamelCase_ :Union[int, float] = 1 / 2_55 , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Union[float, List[float]]] = None , lowerCamelCase_ :Optional[Union[float, List[float]]] = None , **lowerCamelCase_ :List[str] , ) -> None: '''simple docstring''' super().__init__(**lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = size if size is not None else {'''shortest_edge''': 2_24} SCREAMING_SNAKE_CASE : Optional[int] = get_size_dict(lowerCamelCase_ , default_to_square=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = crop_size if crop_size is not None else {'''height''': 2_24, '''width''': 2_24} SCREAMING_SNAKE_CASE : List[str] = get_size_dict(lowerCamelCase_ , param_name='''crop_size''' ) SCREAMING_SNAKE_CASE : Dict = do_resize SCREAMING_SNAKE_CASE : Union[str, Any] = size SCREAMING_SNAKE_CASE : Dict = do_center_crop SCREAMING_SNAKE_CASE : Tuple = crop_size SCREAMING_SNAKE_CASE : List[Any] = resample SCREAMING_SNAKE_CASE : Dict = do_rescale SCREAMING_SNAKE_CASE : List[str] = rescale_factor SCREAMING_SNAKE_CASE : int = do_normalize SCREAMING_SNAKE_CASE : Union[str, Any] = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN SCREAMING_SNAKE_CASE : Optional[int] = image_std if image_std is not None else IMAGENET_STANDARD_STD def __lowerCAmelCase ( self :Tuple , lowerCamelCase_ :np.ndarray , lowerCamelCase_ :Dict[str, int] , lowerCamelCase_ :PILImageResampling = PILImageResampling.BILINEAR , lowerCamelCase_ :Optional[Union[str, ChannelDimension]] = None , **lowerCamelCase_ :List[Any] , ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = get_size_dict(lowerCamelCase_ , default_to_square=lowerCamelCase_ ) if "shortest_edge" in size: SCREAMING_SNAKE_CASE : Dict = get_resize_output_image_size(lowerCamelCase_ , size['''shortest_edge'''] , default_to_square=lowerCamelCase_ ) elif "height" in size and "width" in size: SCREAMING_SNAKE_CASE : Optional[int] = (size['''height'''], size['''width''']) else: raise ValueError(f"Size must have 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}" ) return resize(lowerCamelCase_ , size=lowerCamelCase_ , resample=lowerCamelCase_ , data_format=lowerCamelCase_ , **lowerCamelCase_ ) def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :np.ndarray , lowerCamelCase_ :Dict[str, int] , lowerCamelCase_ :Optional[Union[str, ChannelDimension]] = None , **lowerCamelCase_ :str , ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = get_size_dict(lowerCamelCase_ ) if "height" not in size or "width" not in size: raise ValueError(f"Size must have 'height' and 'width' as keys. Got {size.keys()}" ) return center_crop(lowerCamelCase_ , size=(size['''height'''], size['''width''']) , data_format=lowerCamelCase_ , **lowerCamelCase_ ) def __lowerCAmelCase ( self :Any , lowerCamelCase_ :np.ndarray , lowerCamelCase_ :Union[int, float] , lowerCamelCase_ :Optional[Union[str, ChannelDimension]] = None , **lowerCamelCase_ :Any , ) -> Any: '''simple docstring''' return rescale(lowerCamelCase_ , scale=lowerCamelCase_ , data_format=lowerCamelCase_ , **lowerCamelCase_ ) def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :np.ndarray , lowerCamelCase_ :Union[float, List[float]] , lowerCamelCase_ :Union[float, List[float]] , lowerCamelCase_ :Optional[Union[str, ChannelDimension]] = None , **lowerCamelCase_ :int , ) -> np.ndarray: '''simple docstring''' return normalize(lowerCamelCase_ , mean=lowerCamelCase_ , std=lowerCamelCase_ , data_format=lowerCamelCase_ , **lowerCamelCase_ ) def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :ImageInput , lowerCamelCase_ :bool = None , lowerCamelCase_ :Dict[str, int] = None , lowerCamelCase_ :PILImageResampling = None , lowerCamelCase_ :bool = None , lowerCamelCase_ :Dict[str, int] = None , lowerCamelCase_ :bool = None , lowerCamelCase_ :float = None , lowerCamelCase_ :bool = None , lowerCamelCase_ :Optional[Union[float, List[float]]] = None , lowerCamelCase_ :Optional[Union[float, List[float]]] = None , lowerCamelCase_ :Optional[ChannelDimension] = ChannelDimension.FIRST , ) -> np.ndarray: '''simple docstring''' if do_resize and size is None or resample is None: raise ValueError('''Size and resample must be specified if do_resize is True.''' ) if do_center_crop and crop_size is None: raise ValueError('''Crop size must be specified if do_center_crop is True.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE : Optional[Any] = to_numpy_array(lowerCamelCase_ ) if do_resize: SCREAMING_SNAKE_CASE : Any = self.resize(image=lowerCamelCase_ , size=lowerCamelCase_ , resample=lowerCamelCase_ ) if do_center_crop: SCREAMING_SNAKE_CASE : str = self.center_crop(lowerCamelCase_ , size=lowerCamelCase_ ) if do_rescale: SCREAMING_SNAKE_CASE : List[Any] = self.rescale(image=lowerCamelCase_ , scale=lowerCamelCase_ ) if do_normalize: SCREAMING_SNAKE_CASE : Tuple = self.normalize(image=lowerCamelCase_ , mean=lowerCamelCase_ , std=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = to_channel_dimension_format(lowerCamelCase_ , lowerCamelCase_ ) return image def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :ImageInput , lowerCamelCase_ :bool = None , lowerCamelCase_ :Dict[str, int] = None , lowerCamelCase_ :PILImageResampling = None , lowerCamelCase_ :bool = None , lowerCamelCase_ :Dict[str, int] = None , lowerCamelCase_ :bool = None , lowerCamelCase_ :float = None , lowerCamelCase_ :bool = None , lowerCamelCase_ :Optional[Union[float, List[float]]] = None , lowerCamelCase_ :Optional[Union[float, List[float]]] = None , lowerCamelCase_ :Optional[Union[str, TensorType]] = None , lowerCamelCase_ :ChannelDimension = ChannelDimension.FIRST , **lowerCamelCase_ :Union[str, Any] , ) -> PIL.Image.Image: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE : List[Any] = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE : Union[str, Any] = do_center_crop if do_center_crop is not None else self.do_center_crop SCREAMING_SNAKE_CASE : Any = do_rescale if do_rescale is not None else self.do_rescale SCREAMING_SNAKE_CASE : int = rescale_factor if rescale_factor is not None else self.rescale_factor SCREAMING_SNAKE_CASE : List[str] = do_normalize if do_normalize is not None else self.do_normalize SCREAMING_SNAKE_CASE : List[str] = image_mean if image_mean is not None else self.image_mean SCREAMING_SNAKE_CASE : Dict = image_std if image_std is not None else self.image_std SCREAMING_SNAKE_CASE : Tuple = size if size is not None else self.size SCREAMING_SNAKE_CASE : Optional[Any] = get_size_dict(lowerCamelCase_ , default_to_square=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = crop_size if crop_size is not None else self.crop_size SCREAMING_SNAKE_CASE : int = get_size_dict(lowerCamelCase_ , param_name='''crop_size''' ) if not valid_images(lowerCamelCase_ ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) SCREAMING_SNAKE_CASE : Dict = make_batched(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = [ [ self._preprocess_image( image=lowerCamelCase_ , do_resize=lowerCamelCase_ , size=lowerCamelCase_ , resample=lowerCamelCase_ , do_center_crop=lowerCamelCase_ , crop_size=lowerCamelCase_ , do_rescale=lowerCamelCase_ , rescale_factor=lowerCamelCase_ , do_normalize=lowerCamelCase_ , image_mean=lowerCamelCase_ , image_std=lowerCamelCase_ , data_format=lowerCamelCase_ , ) for img in video ] for video in videos ] SCREAMING_SNAKE_CASE : Union[str, Any] = {'''pixel_values''': videos} return BatchFeature(data=lowerCamelCase_ , tensor_type=lowerCamelCase_ )
698
"""simple docstring""" import argparse import fairseq import torch from torch import nn from transformers import ( MBartaaTokenizer, MBartConfig, MBartForCausalLM, SpeechEncoderDecoderConfig, SpeechEncoderDecoderModel, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaModel, logging, ) logging.set_verbosity_info() lowerCamelCase__ : List[Any] = logging.get_logger(__name__) lowerCamelCase__ : Tuple = { "post_extract_proj": "feature_projection.projection", "encoder.pos_conv.0": "encoder.pos_conv_embed.conv", "self_attn.k_proj": "encoder.layers.*.attention.k_proj", "self_attn.v_proj": "encoder.layers.*.attention.v_proj", "self_attn.q_proj": "encoder.layers.*.attention.q_proj", "self_attn.out_proj": "encoder.layers.*.attention.out_proj", "self_attn_layer_norm": "encoder.layers.*.layer_norm", "fc1": "encoder.layers.*.feed_forward.intermediate_dense", "fc2": "encoder.layers.*.feed_forward.output_dense", "final_layer_norm": "encoder.layers.*.final_layer_norm", "encoder.layer_norm": "encoder.layer_norm", "w2v_model.layer_norm": "feature_projection.layer_norm", "quantizer.weight_proj": "quantizer.weight_proj", "quantizer.vars": "quantizer.codevectors", "project_q": "project_q", "final_proj": "project_hid", "w2v_encoder.proj": "lm_head", "mask_emb": "masked_spec_embed", } lowerCamelCase__ : List[str] = [ "lm_head", "quantizer.weight_proj", "quantizer.codevectors", "project_q", "project_hid", ] def __A ( a_ : Optional[int] , a_ : str , a_ : str , a_ : str , a_ : List[str] )-> Tuple: '''simple docstring''' for attribute in key.split('''.''' ): SCREAMING_SNAKE_CASE : Any = getattr(a_ , a_ ) if weight_type is not None: SCREAMING_SNAKE_CASE : Optional[int] = getattr(a_ , a_ ).shape else: SCREAMING_SNAKE_CASE : Any = hf_pointer.shape assert hf_shape == value.shape, ( F"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be" F" {value.shape} for {full_name}" ) if weight_type == "weight": SCREAMING_SNAKE_CASE : List[Any] = value elif weight_type == "weight_g": SCREAMING_SNAKE_CASE : Optional[int] = value elif weight_type == "weight_v": SCREAMING_SNAKE_CASE : Any = value elif weight_type == "bias": SCREAMING_SNAKE_CASE : List[Any] = value else: SCREAMING_SNAKE_CASE : List[str] = value logger.info(F"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." ) def __A ( a_ : Optional[Any] , a_ : Dict )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = [] SCREAMING_SNAKE_CASE : Optional[Any] = fairseq_model.state_dict() SCREAMING_SNAKE_CASE : Tuple = hf_model.feature_extractor SCREAMING_SNAKE_CASE : Tuple = hf_model.adapter for name, value in fairseq_dict.items(): SCREAMING_SNAKE_CASE : int = False if "conv_layers" in name: load_conv_layer( a_ , a_ , a_ , a_ , hf_model.config.feat_extract_norm == '''group''' , ) SCREAMING_SNAKE_CASE : List[str] = True elif any(x in name for x in ['''adaptor''', '''w2v_encoder.proj.''', '''w2v_proj_ln.'''] ): load_adapter(a_ , a_ , a_ , a_ ) SCREAMING_SNAKE_CASE : List[Any] = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split('''w2v_model.''' )[-1] == name.split('''.''' )[0]: SCREAMING_SNAKE_CASE : Union[str, Any] = True if "*" in mapped_key: SCREAMING_SNAKE_CASE : Dict = name.split(a_ )[0].split('''.''' )[-2] SCREAMING_SNAKE_CASE : Optional[int] = mapped_key.replace('''*''' , a_ ) if "weight_g" in name: SCREAMING_SNAKE_CASE : List[str] = '''weight_g''' elif "weight_v" in name: SCREAMING_SNAKE_CASE : Union[str, Any] = '''weight_v''' elif "bias" in name: SCREAMING_SNAKE_CASE : str = '''bias''' elif "weight" in name: SCREAMING_SNAKE_CASE : Tuple = '''weight''' else: SCREAMING_SNAKE_CASE : str = None set_recursively(a_ , a_ , a_ , a_ , a_ ) continue if not is_used: unused_weights.append(a_ ) logger.warning(F"Unused weights: {unused_weights}" ) def __A ( a_ : Dict , a_ : int , a_ : Optional[int] , a_ : Optional[int] , a_ : Dict )-> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = full_name.split('''conv_layers.''' )[-1] SCREAMING_SNAKE_CASE : List[str] = name.split('''.''' ) SCREAMING_SNAKE_CASE : Dict = int(items[0] ) SCREAMING_SNAKE_CASE : Optional[Any] = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found." ) SCREAMING_SNAKE_CASE : List[Any] = value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found." ) SCREAMING_SNAKE_CASE : str = value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( F"{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was" " found." ) SCREAMING_SNAKE_CASE : str = value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found." ) SCREAMING_SNAKE_CASE : Union[str, Any] = value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) else: unused_weights.append(a_ ) def __A ( a_ : Optional[int] , a_ : Optional[int] , a_ : Any , a_ : Any )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = full_name.split('''adaptor.''' )[-1] SCREAMING_SNAKE_CASE : List[Any] = name.split('''.''' ) if items[1].isdigit(): SCREAMING_SNAKE_CASE : List[Any] = int(items[1] ) else: SCREAMING_SNAKE_CASE : str = None if "adaptor" not in full_name: if "proj_ln" in full_name: # has to be layer norm if "bias" in name: assert ( value.shape == adapter.proj_layer_norm.bias.data.shape ), F"{full_name} has size {value.shape}, but {adapter.proj_layer_norm.bias.data.shape} was found." SCREAMING_SNAKE_CASE : str = value logger.info(F"Adapter proj layer norm bias was initialized from {full_name}." ) if "weight" in name: assert ( value.shape == adapter.proj_layer_norm.weight.data.shape ), F"{full_name} has size {value.shape}, but {adapter.proj_layer_norm.weight.data.shape} was found." SCREAMING_SNAKE_CASE : Optional[Any] = value else: # has to be projection layer if "bias" in name: assert ( value.shape == adapter.proj.bias.data.shape ), F"{full_name} has size {value.shape}, but {adapter.proj.bias.data.shape} was found." SCREAMING_SNAKE_CASE : Union[str, Any] = value logger.info(F"Adapter proj layer bias was initialized from {full_name}." ) if "weight" in name: assert ( value.shape == adapter.proj.weight.data.shape ), F"{full_name} has size {value.shape}, but {adapter.proj.weight.data.shape} was found." SCREAMING_SNAKE_CASE : int = value logger.info(F"Adapter proj layer weight was initialized from {full_name}." ) elif isinstance(a_ , a_ ): if "bias" in name: assert ( value.shape == adapter.layers[layer_id].conv.bias.data.shape ), F"{full_name} has size {value.shape}, but {adapter.layers[layer_id].conv.bias.data.shape} was found." SCREAMING_SNAKE_CASE : str = value logger.info(F"Adapter layer {layer_id} bias was initialized from {full_name}." ) elif "weight" in name: assert ( value.shape == adapter.layers[layer_id].conv.weight.data.shape ), F"{full_name} has size {value.shape}, but {adapter.layers[layer_id].conv.weight.data.shape} was found." SCREAMING_SNAKE_CASE : List[str] = value logger.info(F"Adapter layer {layer_id} bias was initialized from {full_name}." ) else: unused_weights.append(a_ ) def __A ( a_ : Optional[Any] )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = emb.weight.shape SCREAMING_SNAKE_CASE : Any = nn.Linear(a_ , a_ , bias=a_ ) SCREAMING_SNAKE_CASE : Optional[int] = emb.weight.data return lin_layer @torch.no_grad() def __A ( a_ : Tuple , a_ : Optional[int] , a_ : List[Any] , a_ : Any , a_ : Tuple , a_ : int , a_ : Any , a_ : str , a_ : Tuple , a_ : Union[str, Any] , a_ : Union[str, Any] , )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = WavaVecaConfig.from_pretrained( a_ , add_adapter=a_ , adapter_stride=a_ , adapter_kernel_size=a_ , use_auth_token=a_ , output_hidden_size=a_ , ) SCREAMING_SNAKE_CASE : Union[str, Any] = MBartConfig.from_pretrained(a_ ) # load model SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={ '''config_yaml''': config_yaml_path, '''data''': '''/'''.join(dict_path.split('''/''' )[:-1] ), '''w2v_path''': checkpoint_path, '''load_pretrained_decoder_from''': None, } , ) SCREAMING_SNAKE_CASE : int = model[0].eval() # load feature extractor SCREAMING_SNAKE_CASE : Union[str, Any] = WavaVecaFeatureExtractor.from_pretrained(a_ , use_auth_token=a_ ) # set weights for wav2vec2 encoder SCREAMING_SNAKE_CASE : str = WavaVecaModel(a_ ) recursively_load_weights_wavaveca(model.encoder , a_ ) # load decoder weights SCREAMING_SNAKE_CASE : Dict = MBartForCausalLM(a_ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict() , strict=a_ ) logger.warning(F"The following keys are missing when loading the decoder weights: {missing_keys}" ) logger.warning(F"The following keys are unexpected when loading the decoder weights: {unexpected_keys}" ) SCREAMING_SNAKE_CASE : Union[str, Any] = SpeechEncoderDecoderModel(encoder=a_ , decoder=a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = False SCREAMING_SNAKE_CASE : Optional[Any] = MBartaaTokenizer(a_ ) tokenizer.save_pretrained(a_ ) SCREAMING_SNAKE_CASE : Tuple = hf_wavavec.config.to_dict() SCREAMING_SNAKE_CASE : Any = tokenizer.pad_token_id SCREAMING_SNAKE_CASE : List[str] = tokenizer.bos_token_id SCREAMING_SNAKE_CASE : Dict = tokenizer.eos_token_id SCREAMING_SNAKE_CASE : Optional[Any] = '''mbart50''' SCREAMING_SNAKE_CASE : Optional[int] = '''wav2vec2''' SCREAMING_SNAKE_CASE : Optional[Any] = tokenizer.eos_token_id SCREAMING_SNAKE_CASE : List[str] = 25_00_04 SCREAMING_SNAKE_CASE : Dict = tokenizer.eos_token_id SCREAMING_SNAKE_CASE : Any = SpeechEncoderDecoderConfig.from_dict(a_ ) hf_wavavec.save_pretrained(a_ ) feature_extractor.save_pretrained(a_ ) if __name__ == "__main__": lowerCamelCase__ : Any = argparse.ArgumentParser() parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to fairseq checkpoint") parser.add_argument("--dict_path", default=None, type=str, help="Path to dict of fine-tuned model") parser.add_argument("--config_yaml_path", default=None, type=str, help="Path to yaml file of fine-tuned model") parser.add_argument( "--encoder_config_path", default="facebook/wav2vec2-xls-r-1b", type=str, help="Path to hf encoder wav2vec2 checkpoint config", ) parser.add_argument( "--decoder_config_path", default="facebook/mbart-large-50-one-to-many-mmt", type=str, help="Path to hf decoder checkpoint config", ) parser.add_argument("--add_adapter", default=True, type=bool, help="whethere to add model adapter layers") parser.add_argument("--adapter_stride", default=2, type=int, help="stride of adapter layers") parser.add_argument("--adapter_kernel_size", default=3, type=int, help="kernel size of adapter layers") parser.add_argument("--encoder_output_dim", default=1024, type=int, help="encoder output dim") parser.add_argument("--start_token_id", default=250004, type=int, help="`decoder_start_token_id` of model config") lowerCamelCase__ : Dict = parser.parse_args() convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.dict_path, args.config_yaml_path, encoder_config_path=args.encoder_config_path, decoder_config_path=args.decoder_config_path, add_adapter=args.add_adapter, adapter_kernel_size=args.adapter_kernel_size, adapter_stride=args.adapter_stride, decoder_start_token_id=args.start_token_id, encoder_output_dim=args.encoder_output_dim, )
698
1
"""simple docstring""" import math def __A ( a_ : int )-> bool: '''simple docstring''' assert isinstance(a_ , a_ ) and ( number >= 0 ), "'number' must been an int and positive" if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or not number % 2: # Negatives, 0, 1 and all even numbers are not primes return False SCREAMING_SNAKE_CASE : List[str] = range(3 , int(math.sqrt(a_ ) + 1 ) , 2 ) return not any(not number % i for i in odd_numbers ) def __A ( a_ : Union[str, Any] , a_ : Union[str, Any]=1 , **a_ : Optional[Any] )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = factor * value SCREAMING_SNAKE_CASE : Tuple = value while not is_prime(a_ ): value += 1 if not ("desc" in kwargs and kwargs["desc"] is True) else -1 if value == first_value_val: return next_prime(value + 1 , **a_ ) return value
698
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available lowerCamelCase__ : Union[str, Any] = { "configuration_m2m_100": ["M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP", "M2M100Config", "M2M100OnnxConfig"], "tokenization_m2m_100": ["M2M100Tokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : str = [ "M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST", "M2M100ForConditionalGeneration", "M2M100Model", "M2M100PreTrainedModel", ] if TYPE_CHECKING: from .configuration_mam_aaa import M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP, MaMaaaConfig, MaMaaaOnnxConfig from .tokenization_mam_aaa import MaMaaaTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mam_aaa import ( M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST, MaMaaaForConditionalGeneration, MaMaaaModel, MaMaaaPreTrainedModel, ) else: import sys lowerCamelCase__ : Optional[int] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
698
1
"""simple docstring""" import random from typing import Any def __A ( a_ : list )-> list[Any]: '''simple docstring''' for _ in range(len(a_ ) ): SCREAMING_SNAKE_CASE : str = random.randint(0 , len(a_ ) - 1 ) SCREAMING_SNAKE_CASE : str = random.randint(0 , len(a_ ) - 1 ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Tuple = data[b], data[a] return data if __name__ == "__main__": lowerCamelCase__ : Union[str, Any] = [0, 1, 2, 3, 4, 5, 6, 7] lowerCamelCase__ : Dict = ["python", "says", "hello", "!"] print("Fisher-Yates Shuffle:") print("List", integers, strings) print("FY Shuffle", fisher_yates_shuffle(integers), fisher_yates_shuffle(strings))
698
"""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 lowerCamelCase__ : List[Any] = "\\n@inproceedings{pillutla-etal:mauve:neurips2021,\n title={MAUVE: Measuring the Gap Between Neural Text and Human Text using Divergence Frontiers},\n author={Pillutla, Krishna and Swayamdipta, Swabha and Zellers, Rowan and Thickstun, John and Welleck, Sean and Choi, Yejin and Harchaoui, Zaid},\n booktitle = {NeurIPS},\n year = {2021}\n}\n\n" lowerCamelCase__ : List[str] = "\\nMAUVE is a library built on PyTorch and HuggingFace Transformers to measure the gap between neural text and human text with the eponymous MAUVE measure.\n\nMAUVE summarizes both Type I and Type II errors measured softly using Kullback–Leibler (KL) divergences.\n\nFor details, see the MAUVE paper: https://arxiv.org/abs/2102.01454 (Neurips, 2021).\n\nThis metrics is a wrapper around the official implementation of MAUVE:\nhttps://github.com/krishnap25/mauve\n" lowerCamelCase__ : List[Any] = "\nCalculates MAUVE scores between two lists of generated text and reference text.\nArgs:\n predictions: list of generated text to score. Each predictions\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\nOptional Args:\n num_buckets: the size of the histogram to quantize P and Q. Options: 'auto' (default) or an integer\n pca_max_data: the number data points to use for PCA dimensionality reduction prior to clustering. If -1, use all the data. Default -1\n kmeans_explained_var: amount of variance of the data to keep in dimensionality reduction by PCA. Default 0.9\n kmeans_num_redo: number of times to redo k-means clustering (the best objective is kept). Default 5\n kmeans_max_iter: maximum number of k-means iterations. Default 500\n 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'].\n 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\n max_text_length: maximum number of tokens to consider. Default 1024\n divergence_curve_discretization_size: Number of points to consider on the divergence curve. Default 25\n mauve_scaling_factor: \"c\" from the paper. Default 5.\n verbose: If True (default), print running time updates\n seed: random seed to initialize k-means cluster assignments.\nReturns:\n mauve: MAUVE score, a number between 0 and 1. Larger values indicate that P and Q are closer,\n frontier_integral: Frontier Integral, a number between 0 and 1. Smaller values indicate that P and Q are closer,\n divergence_curve: a numpy.ndarray of shape (m, 2); plot it with matplotlib to view the divergence curve,\n p_hist: a discrete distribution, which is a quantized version of the text distribution p_text,\n q_hist: same as above, but with q_text.\nExamples:\n\n >>> # faiss segfaults in doctest for some reason, so the .compute call is not tested with doctest\n >>> import datasets\n >>> mauve = datasets.load_metric('mauve')\n >>> predictions = [\"hello there\", \"general kenobi\"]\n >>> references = [\"hello there\", \"general kenobi\"]\n >>> out = mauve.compute(predictions=predictions, references=references) # doctest: +SKIP\n >>> print(out.mauve) # doctest: +SKIP\n 1.0\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowercase__( datasets.Metric ): '''simple docstring''' def __lowerCAmelCase ( self :Optional[int] ) -> 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 __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :List[str] , lowerCamelCase_ :Optional[Any]=None , lowerCamelCase_ :str=None , lowerCamelCase_ :Tuple=None , lowerCamelCase_ :Optional[Any]=None , lowerCamelCase_ :Optional[int]="auto" , lowerCamelCase_ :Dict=-1 , lowerCamelCase_ :str=0.9 , lowerCamelCase_ :str=5 , lowerCamelCase_ :Tuple=5_00 , lowerCamelCase_ :str="gpt2-large" , lowerCamelCase_ :List[Any]=-1 , lowerCamelCase_ :Dict=10_24 , lowerCamelCase_ :Tuple=25 , lowerCamelCase_ :List[Any]=5 , lowerCamelCase_ :Dict=True , lowerCamelCase_ :List[Any]=25 , ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = compute_mauve( p_text=lowerCamelCase_ , q_text=lowerCamelCase_ , p_features=lowerCamelCase_ , q_features=lowerCamelCase_ , p_tokens=lowerCamelCase_ , q_tokens=lowerCamelCase_ , num_buckets=lowerCamelCase_ , pca_max_data=lowerCamelCase_ , kmeans_explained_var=lowerCamelCase_ , kmeans_num_redo=lowerCamelCase_ , kmeans_max_iter=lowerCamelCase_ , featurize_model_name=lowerCamelCase_ , device_id=lowerCamelCase_ , max_text_length=lowerCamelCase_ , divergence_curve_discretization_size=lowerCamelCase_ , mauve_scaling_factor=lowerCamelCase_ , verbose=lowerCamelCase_ , seed=lowerCamelCase_ , ) return out
698
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : int = logging.get_logger(__name__) class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """timm_backbone""" def __init__( self :str , lowerCamelCase_ :str=None , lowerCamelCase_ :Optional[int]=3 , lowerCamelCase_ :Optional[int]=True , lowerCamelCase_ :Dict=True , lowerCamelCase_ :Union[str, Any]=None , **lowerCamelCase_ :Dict , ) -> Optional[int]: '''simple docstring''' super().__init__(**lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = backbone SCREAMING_SNAKE_CASE : List[Any] = num_channels SCREAMING_SNAKE_CASE : List[Any] = features_only SCREAMING_SNAKE_CASE : List[str] = use_pretrained_backbone SCREAMING_SNAKE_CASE : Optional[Any] = True SCREAMING_SNAKE_CASE : Optional[int] = out_indices if out_indices is not None else (-1,)
698
"""simple docstring""" import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert_fast import BertTokenizerFast from .tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer, DPRReaderTokenizer lowerCamelCase__ : Union[str, Any] = logging.get_logger(__name__) lowerCamelCase__ : Union[str, Any] = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} lowerCamelCase__ : Any = { "vocab_file": { "facebook/dpr-ctx_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt" ), "facebook/dpr-ctx_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "facebook/dpr-ctx_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json" ), "facebook/dpr-ctx_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json" ), }, } lowerCamelCase__ : str = { "vocab_file": { "facebook/dpr-question_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt" ), "facebook/dpr-question_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "facebook/dpr-question_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json" ), "facebook/dpr-question_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json" ), }, } lowerCamelCase__ : Any = { "vocab_file": { "facebook/dpr-reader-single-nq-base": ( "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt" ), "facebook/dpr-reader-multiset-base": ( "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "facebook/dpr-reader-single-nq-base": ( "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json" ), "facebook/dpr-reader-multiset-base": ( "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json" ), }, } lowerCamelCase__ : Optional[Any] = { "facebook/dpr-ctx_encoder-single-nq-base": 512, "facebook/dpr-ctx_encoder-multiset-base": 512, } lowerCamelCase__ : Tuple = { "facebook/dpr-question_encoder-single-nq-base": 512, "facebook/dpr-question_encoder-multiset-base": 512, } lowerCamelCase__ : Dict = { "facebook/dpr-reader-single-nq-base": 512, "facebook/dpr-reader-multiset-base": 512, } lowerCamelCase__ : int = { "facebook/dpr-ctx_encoder-single-nq-base": {"do_lower_case": True}, "facebook/dpr-ctx_encoder-multiset-base": {"do_lower_case": True}, } lowerCamelCase__ : Tuple = { "facebook/dpr-question_encoder-single-nq-base": {"do_lower_case": True}, "facebook/dpr-question_encoder-multiset-base": {"do_lower_case": True}, } lowerCamelCase__ : Dict = { "facebook/dpr-reader-single-nq-base": {"do_lower_case": True}, "facebook/dpr-reader-multiset-base": {"do_lower_case": True}, } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = VOCAB_FILES_NAMES UpperCamelCase = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase = DPRContextEncoderTokenizer class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = VOCAB_FILES_NAMES UpperCamelCase = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase = DPRQuestionEncoderTokenizer lowerCamelCase__ : Union[str, Any] = collections.namedtuple( "DPRSpanPrediction", ["span_score", "relevance_score", "doc_id", "start_index", "end_index", "text"] ) lowerCamelCase__ : int = collections.namedtuple("DPRReaderOutput", ["start_logits", "end_logits", "relevance_logits"]) lowerCamelCase__ : str = r"\n Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`.\n It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers),\n using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)`\n with the format:\n\n [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids>\n\n Args:\n questions (`str` or `List[str]`):\n The questions to be encoded. You can specify one question for many passages. In this case, the question\n will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in\n `titles` or `texts`.\n titles (`str` or `List[str]`):\n The passages titles to be encoded. This can be a string or a list of strings if there are several passages.\n texts (`str` or `List[str]`):\n The passages texts to be encoded. This can be a string or a list of strings if there are several passages.\n padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`):\n Activates and controls padding. Accepts the following values:\n\n - `True` or `'longest'`: Pad to the longest sequence in the batch (or no padding if only a single sequence\n if provided).\n - `'max_length'`: Pad to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided.\n - `False` or `'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of different\n lengths).\n truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`):\n Activates and controls truncation. Accepts the following values:\n\n - `True` or `'longest_first'`: Truncate to a maximum length specified with the argument `max_length` or to\n the maximum acceptable input length for the model if that argument is not provided. This will truncate\n token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch\n of pairs) is provided.\n - `'only_first'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the first\n sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `'only_second'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the\n second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `False` or `'do_not_truncate'` (default): No truncation (i.e., can output batch with sequence lengths\n greater than the model maximum admissible input size).\n max_length (`int`, *optional*):\n Controls the maximum length to use by one of the truncation/padding parameters.\n\n If left unset or set to `None`, this will use the predefined model maximum length if a maximum length\n is required by one of the truncation/padding parameters. If the model has no specific maximum input\n length (like XLNet) truncation/padding to a maximum length will be deactivated.\n return_tensors (`str` or [`~utils.TensorType`], *optional*):\n If set, will return tensors instead of list of python integers. Acceptable values are:\n\n - `'tf'`: Return TensorFlow `tf.constant` objects.\n - `'pt'`: Return PyTorch `torch.Tensor` objects.\n - `'np'`: Return Numpy `np.ndarray` objects.\n return_attention_mask (`bool`, *optional*):\n Whether or not to return the attention mask. If not set, will return the attention mask according to the\n specific tokenizer's default, defined by the `return_outputs` attribute.\n\n [What are attention masks?](../glossary#attention-mask)\n\n Return:\n `Dict[str, List[List[int]]]`: A dictionary with the following keys:\n\n - `input_ids`: List of token ids to be fed to a model.\n - `attention_mask`: List of indices specifying which tokens should be attended to by the model.\n " @add_start_docstrings(_UpperCAmelCase ) class lowercase__: '''simple docstring''' def __call__( self :str , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Optional[str] = None , lowerCamelCase_ :Optional[str] = None , lowerCamelCase_ :Union[bool, str] = False , lowerCamelCase_ :Union[bool, str] = False , lowerCamelCase_ :Optional[int] = None , lowerCamelCase_ :Optional[Union[str, TensorType]] = None , lowerCamelCase_ :Optional[bool] = None , **lowerCamelCase_ :Tuple , ) -> BatchEncoding: '''simple docstring''' if titles is None and texts is None: return super().__call__( lowerCamelCase_ , padding=lowerCamelCase_ , truncation=lowerCamelCase_ , max_length=lowerCamelCase_ , return_tensors=lowerCamelCase_ , return_attention_mask=lowerCamelCase_ , **lowerCamelCase_ , ) elif titles is None or texts is None: SCREAMING_SNAKE_CASE : List[str] = titles if texts is None else texts return super().__call__( lowerCamelCase_ , lowerCamelCase_ , padding=lowerCamelCase_ , truncation=lowerCamelCase_ , max_length=lowerCamelCase_ , return_tensors=lowerCamelCase_ , return_attention_mask=lowerCamelCase_ , **lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Dict = titles if not isinstance(lowerCamelCase_ , lowerCamelCase_ ) else [titles] SCREAMING_SNAKE_CASE : Dict = texts if not isinstance(lowerCamelCase_ , lowerCamelCase_ ) else [texts] SCREAMING_SNAKE_CASE : Optional[int] = len(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = questions if not isinstance(lowerCamelCase_ , lowerCamelCase_ ) else [questions] * n_passages assert len(lowerCamelCase_ ) == len( lowerCamelCase_ ), f"There should be as many titles than texts but got {len(lowerCamelCase_ )} titles and {len(lowerCamelCase_ )} texts." SCREAMING_SNAKE_CASE : Any = super().__call__(lowerCamelCase_ , lowerCamelCase_ , padding=lowerCamelCase_ , truncation=lowerCamelCase_ )['''input_ids'''] SCREAMING_SNAKE_CASE : Dict = super().__call__(lowerCamelCase_ , add_special_tokens=lowerCamelCase_ , padding=lowerCamelCase_ , truncation=lowerCamelCase_ )['''input_ids'''] SCREAMING_SNAKE_CASE : int = { '''input_ids''': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(lowerCamelCase_ , lowerCamelCase_ ) ] } if return_attention_mask is not False: SCREAMING_SNAKE_CASE : List[str] = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) SCREAMING_SNAKE_CASE : int = attention_mask return self.pad(lowerCamelCase_ , padding=lowerCamelCase_ , max_length=lowerCamelCase_ , return_tensors=lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :BatchEncoding , lowerCamelCase_ :DPRReaderOutput , lowerCamelCase_ :int = 16 , lowerCamelCase_ :int = 64 , lowerCamelCase_ :int = 4 , ) -> List[DPRSpanPrediction]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = reader_input['''input_ids'''] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = reader_output[:3] SCREAMING_SNAKE_CASE : Dict = len(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = sorted(range(lowerCamelCase_ ) , reverse=lowerCamelCase_ , key=relevance_logits.__getitem__ ) SCREAMING_SNAKE_CASE : List[DPRReaderOutput] = [] for doc_id in sorted_docs: SCREAMING_SNAKE_CASE : Union[str, Any] = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence SCREAMING_SNAKE_CASE : int = sequence_ids.index(self.sep_token_id , 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: SCREAMING_SNAKE_CASE : Dict = sequence_ids.index(self.pad_token_id ) else: SCREAMING_SNAKE_CASE : Optional[int] = len(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=lowerCamelCase_ , top_spans=lowerCamelCase_ , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=lowerCamelCase_ , start_index=lowerCamelCase_ , end_index=lowerCamelCase_ , text=self.decode(sequence_ids[start_index : end_index + 1] ) , ) ) if len(lowerCamelCase_ ) >= num_spans: break return nbest_spans_predictions[:num_spans] def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :List[int] , lowerCamelCase_ :List[int] , lowerCamelCase_ :int , lowerCamelCase_ :int , ) -> List[DPRSpanPrediction]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = [] for start_index, start_score in enumerate(lowerCamelCase_ ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) SCREAMING_SNAKE_CASE : Dict = sorted(lowerCamelCase_ , key=lambda lowerCamelCase_ : x[1] , reverse=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = [] for (start_index, end_index), score in scores: assert start_index <= end_index, f"Wrong span indices: [{start_index}:{end_index}]" SCREAMING_SNAKE_CASE : Optional[int] = end_index - start_index + 1 assert length <= max_answer_length, f"Span is too long: {length} > {max_answer_length}" if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(lowerCamelCase_ ) == top_spans: break return chosen_span_intervals @add_end_docstrings(_UpperCAmelCase ) class lowercase__( _UpperCAmelCase , _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = VOCAB_FILES_NAMES UpperCamelCase = READER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase = READER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase = ["""input_ids""", """attention_mask"""] UpperCamelCase = DPRReaderTokenizer
698
1
"""simple docstring""" import os import random import sys from . import cryptomath_module as cryptomath from . import rabin_miller lowerCamelCase__ : List[Any] = 3 def __A ( a_ : int )-> int: '''simple docstring''' print('''Generating primitive root of p''' ) while True: SCREAMING_SNAKE_CASE : int = random.randrange(3 , a_ ) if pow(a_ , 2 , a_ ) == 1: continue if pow(a_ , a_ , a_ ) == 1: continue return g def __A ( a_ : int )-> tuple[tuple[int, int, int, int], tuple[int, int]]: '''simple docstring''' print('''Generating prime p...''' ) SCREAMING_SNAKE_CASE : str = rabin_miller.generate_large_prime(a_ ) # select large prime number. SCREAMING_SNAKE_CASE : Optional[Any] = primitive_root(a_ ) # one primitive root on modulo p. SCREAMING_SNAKE_CASE : Dict = random.randrange(3 , a_ ) # private_key -> have to be greater than 2 for safety. SCREAMING_SNAKE_CASE : List[Any] = cryptomath.find_mod_inverse(pow(a_ , a_ , a_ ) , a_ ) SCREAMING_SNAKE_CASE : List[Any] = (key_size, e_a, e_a, p) SCREAMING_SNAKE_CASE : Dict = (key_size, d) return public_key, private_key def __A ( a_ : str , a_ : int )-> None: '''simple docstring''' if os.path.exists(F"{name}_pubkey.txt" ) or os.path.exists(F"{name}_privkey.txt" ): print('''\nWARNING:''' ) print( F"\"{name}_pubkey.txt\" or \"{name}_privkey.txt\" already exists. \n" '''Use a different name or delete these files and re-run this program.''' ) sys.exit() SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = generate_key(a_ ) print(F"\nWriting public key to file {name}_pubkey.txt..." ) with open(F"{name}_pubkey.txt" , '''w''' ) as fo: fo.write(F"{public_key[0]},{public_key[1]},{public_key[2]},{public_key[3]}" ) print(F"Writing private key to file {name}_privkey.txt..." ) with open(F"{name}_privkey.txt" , '''w''' ) as fo: fo.write(F"{private_key[0]},{private_key[1]}" ) def __A ( )-> None: '''simple docstring''' print('''Making key files...''' ) make_key_files('''elgamal''' , 20_48 ) print('''Key files generation successful''' ) if __name__ == "__main__": main()
698
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : Optional[Any] = { "microsoft/markuplm-base": "https://huggingface.co/microsoft/markuplm-base/resolve/main/config.json", "microsoft/markuplm-large": "https://huggingface.co/microsoft/markuplm-large/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """markuplm""" def __init__( self :int , lowerCamelCase_ :List[str]=3_05_22 , lowerCamelCase_ :Union[str, Any]=7_68 , lowerCamelCase_ :str=12 , lowerCamelCase_ :Dict=12 , lowerCamelCase_ :str=30_72 , lowerCamelCase_ :Union[str, Any]="gelu" , lowerCamelCase_ :Union[str, Any]=0.1 , lowerCamelCase_ :Optional[Any]=0.1 , lowerCamelCase_ :Union[str, Any]=5_12 , lowerCamelCase_ :Any=2 , lowerCamelCase_ :Optional[Any]=0.0_2 , lowerCamelCase_ :Any=1E-12 , lowerCamelCase_ :Dict=0 , lowerCamelCase_ :Optional[Any]=0 , lowerCamelCase_ :Optional[int]=2 , lowerCamelCase_ :str=2_56 , lowerCamelCase_ :List[Any]=10_24 , lowerCamelCase_ :Union[str, Any]=2_16 , lowerCamelCase_ :Dict=10_01 , lowerCamelCase_ :Any=32 , lowerCamelCase_ :str=50 , lowerCamelCase_ :List[str]="absolute" , lowerCamelCase_ :List[str]=True , lowerCamelCase_ :int=None , **lowerCamelCase_ :Dict , ) -> List[Any]: '''simple docstring''' super().__init__( pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , **lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : str = vocab_size SCREAMING_SNAKE_CASE : Optional[int] = hidden_size SCREAMING_SNAKE_CASE : int = num_hidden_layers SCREAMING_SNAKE_CASE : List[str] = num_attention_heads SCREAMING_SNAKE_CASE : List[str] = hidden_act SCREAMING_SNAKE_CASE : int = intermediate_size SCREAMING_SNAKE_CASE : Optional[int] = hidden_dropout_prob SCREAMING_SNAKE_CASE : List[str] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = max_position_embeddings SCREAMING_SNAKE_CASE : Tuple = type_vocab_size SCREAMING_SNAKE_CASE : Any = initializer_range SCREAMING_SNAKE_CASE : int = layer_norm_eps SCREAMING_SNAKE_CASE : int = position_embedding_type SCREAMING_SNAKE_CASE : Tuple = use_cache SCREAMING_SNAKE_CASE : str = classifier_dropout # additional properties SCREAMING_SNAKE_CASE : Optional[Any] = max_depth SCREAMING_SNAKE_CASE : Dict = max_xpath_tag_unit_embeddings SCREAMING_SNAKE_CASE : Optional[int] = max_xpath_subs_unit_embeddings SCREAMING_SNAKE_CASE : Tuple = tag_pad_id SCREAMING_SNAKE_CASE : str = subs_pad_id SCREAMING_SNAKE_CASE : List[Any] = xpath_unit_hidden_size
698
1
"""simple docstring""" import argparse import os import re import torch from flax.traverse_util import flatten_dict from tax import checkpoints from transformers import ( AutoTokenizer, PixaStructConfig, PixaStructForConditionalGeneration, PixaStructImageProcessor, PixaStructProcessor, PixaStructTextConfig, PixaStructVisionConfig, ) def __A ( a_ : Optional[int] )-> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : str = checkpoints.load_tax_checkpoint(a_ ) SCREAMING_SNAKE_CASE : int = flatten_dict(a_ ) return flax_params def __A ( a_ : Dict )-> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = {} SCREAMING_SNAKE_CASE : List[str] = { '''token_embedder''': '''embeddings''', '''encoder_norm''': '''layernorm''', '''kernel''': '''weight''', '''.out''': '''.output''', '''scale''': '''weight''', '''embedders_0.pos_embedding''': '''row_embedder.weight''', '''embedders_1.pos_embedding''': '''column_embedder.weight''', } SCREAMING_SNAKE_CASE : Optional[Any] = { '''query''': '''attention.query''', '''key''': '''attention.key''', '''value''': '''attention.value''', '''output.dense''': '''output''', '''encoder_decoder_attention.o''': '''encoder_decoder_attention.attention.o''', '''pre_self_attention_layer_norm''': '''self_attention.layer_norm''', '''pre_cross_attention_layer_norm''': '''encoder_decoder_attention.layer_norm''', '''mlp.''': '''mlp.DenseReluDense.''', '''pre_mlp_layer_norm''': '''mlp.layer_norm''', '''self_attention.o''': '''self_attention.attention.o''', '''decoder.embeddings.embedding''': '''decoder.embed_tokens.weight''', '''decoder.relpos_bias.rel_embedding''': '''decoder.layer.0.self_attention.attention.relative_attention_bias.weight''', '''decoder.decoder_norm.weight''': '''decoder.final_layer_norm.weight''', '''decoder.logits_dense.weight''': '''decoder.lm_head.weight''', } for key in flax_dict.keys(): if "target" in key: # remove the first prefix from the key SCREAMING_SNAKE_CASE : Dict = '''.'''.join(key[1:] ) # rename the key for old, new in CONVERSION_MAPPING.items(): SCREAMING_SNAKE_CASE : Dict = new_key.replace(a_ , a_ ) if "decoder" in new_key: for old, new in DECODER_CONVERSION_MAPPING.items(): SCREAMING_SNAKE_CASE : Tuple = new_key.replace(a_ , a_ ) if "layers" in new_key and "decoder" not in new_key: # use regex to replace the layer number SCREAMING_SNAKE_CASE : Any = re.sub(r'''layers_(\d+)''' , r'''layer.\1''' , a_ ) SCREAMING_SNAKE_CASE : List[Any] = new_key.replace('''encoder''' , '''encoder.encoder''' ) elif "layers" in new_key and "decoder" in new_key: # use regex to replace the layer number SCREAMING_SNAKE_CASE : Dict = re.sub(r'''layers_(\d+)''' , r'''layer.\1''' , a_ ) SCREAMING_SNAKE_CASE : str = flax_dict[key] SCREAMING_SNAKE_CASE : Union[str, Any] = {} # convert converted_dict into torch format for key in converted_dict.keys(): if ("embed_tokens" not in key) and ("embedder" not in key): SCREAMING_SNAKE_CASE : Tuple = torch.from_numpy(converted_dict[key].T ) else: SCREAMING_SNAKE_CASE : Tuple = torch.from_numpy(converted_dict[key] ) return converted_torch_dict def __A ( a_ : int , a_ : List[str] , a_ : List[Any]=False , a_ : Optional[Any]=False )-> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = get_flax_param(a_ ) if not use_large: SCREAMING_SNAKE_CASE : int = PixaStructVisionConfig() SCREAMING_SNAKE_CASE : List[Any] = PixaStructTextConfig() else: SCREAMING_SNAKE_CASE : int = PixaStructVisionConfig( hidden_size=15_36 , d_ff=39_68 , num_attention_heads=24 , num_hidden_layers=18 ) SCREAMING_SNAKE_CASE : List[str] = PixaStructTextConfig(hidden_size=15_36 , d_ff=39_68 , num_heads=24 , num_layers=18 ) SCREAMING_SNAKE_CASE : int = PixaStructConfig( vision_config=encoder_config.to_dict() , text_config=decoder_config.to_dict() , is_vqa=a_ ) SCREAMING_SNAKE_CASE : Any = PixaStructForConditionalGeneration(a_ ) SCREAMING_SNAKE_CASE : List[Any] = rename_and_convert_flax_params(a_ ) model.load_state_dict(a_ ) SCREAMING_SNAKE_CASE : int = AutoTokenizer.from_pretrained('''ybelkada/test-pix2struct-tokenizer''' ) SCREAMING_SNAKE_CASE : Dict = PixaStructImageProcessor() SCREAMING_SNAKE_CASE : Tuple = PixaStructProcessor(image_processor=a_ , tokenizer=a_ ) if use_large: SCREAMING_SNAKE_CASE : List[str] = 40_96 SCREAMING_SNAKE_CASE : Optional[Any] = True # mkdir if needed os.makedirs(a_ , exist_ok=a_ ) model.save_pretrained(a_ ) processor.save_pretrained(a_ ) print('''Model saved in {}'''.format(a_ ) ) if __name__ == "__main__": lowerCamelCase__ : Optional[Any] = argparse.ArgumentParser() parser.add_argument("--t5x_checkpoint_path", default=None, type=str, help="Path to the original T5x checkpoint.") parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--use_large", action="store_true", help="Use large model.") parser.add_argument("--is_vqa", action="store_true", help="Use large model.") lowerCamelCase__ : List[Any] = parser.parse_args() convert_pixastruct_original_pytorch_checkpoint_to_hf( args.tax_checkpoint_path, args.pytorch_dump_folder_path, args.use_large )
698
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCamelCase__ : Optional[Any] = logging.get_logger(__name__) lowerCamelCase__ : Union[str, Any] = { "microsoft/resnet-50": "https://huggingface.co/microsoft/resnet-50/blob/main/config.json", } class lowercase__( _UpperCAmelCase , _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """resnet""" UpperCamelCase = ["""basic""", """bottleneck"""] def __init__( self :Optional[int] , lowerCamelCase_ :Tuple=3 , lowerCamelCase_ :Tuple=64 , lowerCamelCase_ :Union[str, Any]=[2_56, 5_12, 10_24, 20_48] , lowerCamelCase_ :int=[3, 4, 6, 3] , lowerCamelCase_ :Any="bottleneck" , lowerCamelCase_ :Optional[int]="relu" , lowerCamelCase_ :Optional[int]=False , lowerCamelCase_ :Any=None , lowerCamelCase_ :Optional[int]=None , **lowerCamelCase_ :Optional[int] , ) -> Tuple: '''simple docstring''' super().__init__(**lowerCamelCase_ ) if layer_type not in self.layer_types: raise ValueError(f"layer_type={layer_type} is not one of {','.join(self.layer_types )}" ) SCREAMING_SNAKE_CASE : Tuple = num_channels SCREAMING_SNAKE_CASE : Union[str, Any] = embedding_size SCREAMING_SNAKE_CASE : List[str] = hidden_sizes SCREAMING_SNAKE_CASE : Optional[Any] = depths SCREAMING_SNAKE_CASE : List[Any] = layer_type SCREAMING_SNAKE_CASE : str = hidden_act SCREAMING_SNAKE_CASE : Optional[Any] = downsample_in_first_stage SCREAMING_SNAKE_CASE : int = ['''stem'''] + [f"stage{idx}" for idx in range(1 , len(lowerCamelCase_ ) + 1 )] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = get_aligned_output_features_output_indices( out_features=lowerCamelCase_ , out_indices=lowerCamelCase_ , stage_names=self.stage_names ) class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = version.parse("""1.11""" ) @property def __lowerCAmelCase ( self :Optional[Any] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def __lowerCAmelCase ( self :str ) -> float: '''simple docstring''' return 1E-3
698
1
"""simple docstring""" import torch from diffusers import UnCLIPScheduler from .test_schedulers import SchedulerCommonTest class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = (UnCLIPScheduler,) def __lowerCAmelCase ( self :Union[str, Any] , **lowerCamelCase_ :List[Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : str = { '''num_train_timesteps''': 10_00, '''variance_type''': '''fixed_small_log''', '''clip_sample''': True, '''clip_sample_range''': 1.0, '''prediction_type''': '''epsilon''', } config.update(**lowerCamelCase_ ) return config def __lowerCAmelCase ( self :Any ) -> Dict: '''simple docstring''' for timesteps in [1, 5, 1_00, 10_00]: self.check_over_configs(num_train_timesteps=lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[int] ) -> Dict: '''simple docstring''' for variance in ["fixed_small_log", "learned_range"]: self.check_over_configs(variance_type=lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[int] ) -> Optional[int]: '''simple docstring''' for clip_sample in [True, False]: self.check_over_configs(clip_sample=lowerCamelCase_ ) def __lowerCAmelCase ( self :Dict ) -> Optional[Any]: '''simple docstring''' for clip_sample_range in [1, 5, 10, 20]: self.check_over_configs(clip_sample_range=lowerCamelCase_ ) def __lowerCAmelCase ( self :List[str] ) -> List[str]: '''simple docstring''' for prediction_type in ["epsilon", "sample"]: self.check_over_configs(prediction_type=lowerCamelCase_ ) def __lowerCAmelCase ( self :List[Any] ) -> Union[str, Any]: '''simple docstring''' for time_step in [0, 5_00, 9_99]: for prev_timestep in [None, 5, 1_00, 2_50, 5_00, 7_50]: if prev_timestep is not None and prev_timestep >= time_step: continue self.check_over_forward(time_step=lowerCamelCase_ , prev_timestep=lowerCamelCase_ ) def __lowerCAmelCase ( self :Tuple ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : str = self.scheduler_classes[0] SCREAMING_SNAKE_CASE : List[Any] = self.get_scheduler_config(variance_type='''fixed_small_log''' ) SCREAMING_SNAKE_CASE : List[str] = scheduler_class(**lowerCamelCase_ ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 1.0000E-10 ) ) < 1E-5 assert torch.sum(torch.abs(scheduler._get_variance(4_87 ) - 0.0_5_4_9_6_2_5 ) ) < 1E-5 assert torch.sum(torch.abs(scheduler._get_variance(9_99 ) - 0.9_9_9_4_9_8_7 ) ) < 1E-5 def __lowerCAmelCase ( self :List[str] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.scheduler_classes[0] SCREAMING_SNAKE_CASE : Tuple = self.get_scheduler_config(variance_type='''learned_range''' ) SCREAMING_SNAKE_CASE : str = scheduler_class(**lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = 0.5 assert scheduler._get_variance(1 , predicted_variance=lowerCamelCase_ ) - -1_0.1_7_1_2_7_9_0 < 1E-5 assert scheduler._get_variance(4_87 , predicted_variance=lowerCamelCase_ ) - -5.7_9_9_8_0_5_2 < 1E-5 assert scheduler._get_variance(9_99 , predicted_variance=lowerCamelCase_ ) - -0.0_0_1_0_0_1_1 < 1E-5 def __lowerCAmelCase ( self :Dict ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : str = self.scheduler_classes[0] SCREAMING_SNAKE_CASE : List[str] = self.get_scheduler_config() SCREAMING_SNAKE_CASE : List[str] = scheduler_class(**lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = scheduler.timesteps SCREAMING_SNAKE_CASE : List[str] = self.dummy_model() SCREAMING_SNAKE_CASE : Optional[Any] = self.dummy_sample_deter SCREAMING_SNAKE_CASE : Any = torch.manual_seed(0 ) for i, t in enumerate(lowerCamelCase_ ): # 1. predict noise residual SCREAMING_SNAKE_CASE : Union[str, Any] = model(lowerCamelCase_ , lowerCamelCase_ ) # 2. predict previous mean of sample x_t-1 SCREAMING_SNAKE_CASE : str = scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , generator=lowerCamelCase_ ).prev_sample SCREAMING_SNAKE_CASE : Union[str, Any] = pred_prev_sample SCREAMING_SNAKE_CASE : Dict = torch.sum(torch.abs(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : List[str] = torch.mean(torch.abs(lowerCamelCase_ ) ) assert abs(result_sum.item() - 2_5_2.2_6_8_2_4_9_5 ) < 1E-2 assert abs(result_mean.item() - 0.3_2_8_4_7_4_3 ) < 1E-3 def __lowerCAmelCase ( self :List[Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.scheduler_classes[0] SCREAMING_SNAKE_CASE : List[Any] = self.get_scheduler_config() SCREAMING_SNAKE_CASE : Tuple = scheduler_class(**lowerCamelCase_ ) scheduler.set_timesteps(25 ) SCREAMING_SNAKE_CASE : Tuple = scheduler.timesteps SCREAMING_SNAKE_CASE : Any = self.dummy_model() SCREAMING_SNAKE_CASE : str = self.dummy_sample_deter SCREAMING_SNAKE_CASE : int = torch.manual_seed(0 ) for i, t in enumerate(lowerCamelCase_ ): # 1. predict noise residual SCREAMING_SNAKE_CASE : List[str] = model(lowerCamelCase_ , lowerCamelCase_ ) if i + 1 == timesteps.shape[0]: SCREAMING_SNAKE_CASE : Union[str, Any] = None else: SCREAMING_SNAKE_CASE : List[Any] = timesteps[i + 1] # 2. predict previous mean of sample x_t-1 SCREAMING_SNAKE_CASE : Tuple = scheduler.step( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , prev_timestep=lowerCamelCase_ , generator=lowerCamelCase_ ).prev_sample SCREAMING_SNAKE_CASE : int = pred_prev_sample SCREAMING_SNAKE_CASE : List[str] = torch.sum(torch.abs(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : List[str] = torch.mean(torch.abs(lowerCamelCase_ ) ) assert abs(result_sum.item() - 2_5_8.2_0_4_4_9_8_3 ) < 1E-2 assert abs(result_mean.item() - 0.3_3_6_2_0_3_8 ) < 1E-3 def __lowerCAmelCase ( self :Tuple ) -> Tuple: '''simple docstring''' pass def __lowerCAmelCase ( self :List[str] ) -> Tuple: '''simple docstring''' pass
698
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : List[Any] = { "uw-madison/mra-base-512-4": "https://huggingface.co/uw-madison/mra-base-512-4/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """mra""" def __init__( self :int , lowerCamelCase_ :Optional[int]=5_02_65 , lowerCamelCase_ :List[str]=7_68 , lowerCamelCase_ :List[str]=12 , lowerCamelCase_ :Optional[Any]=12 , lowerCamelCase_ :int=30_72 , lowerCamelCase_ :Tuple="gelu" , lowerCamelCase_ :List[Any]=0.1 , lowerCamelCase_ :str=0.1 , lowerCamelCase_ :str=5_12 , lowerCamelCase_ :List[str]=1 , lowerCamelCase_ :int=0.0_2 , lowerCamelCase_ :int=1E-5 , lowerCamelCase_ :List[Any]="absolute" , lowerCamelCase_ :str=4 , lowerCamelCase_ :List[str]="full" , lowerCamelCase_ :List[Any]=0 , lowerCamelCase_ :Optional[Any]=0 , lowerCamelCase_ :Union[str, Any]=1 , lowerCamelCase_ :List[str]=0 , lowerCamelCase_ :List[Any]=2 , **lowerCamelCase_ :str , ) -> Dict: '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = vocab_size SCREAMING_SNAKE_CASE : Tuple = max_position_embeddings SCREAMING_SNAKE_CASE : List[Any] = hidden_size SCREAMING_SNAKE_CASE : Dict = num_hidden_layers SCREAMING_SNAKE_CASE : Tuple = num_attention_heads SCREAMING_SNAKE_CASE : Any = intermediate_size SCREAMING_SNAKE_CASE : Any = hidden_act SCREAMING_SNAKE_CASE : Tuple = hidden_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : str = initializer_range SCREAMING_SNAKE_CASE : Tuple = type_vocab_size SCREAMING_SNAKE_CASE : Union[str, Any] = layer_norm_eps SCREAMING_SNAKE_CASE : str = position_embedding_type SCREAMING_SNAKE_CASE : List[str] = block_per_row SCREAMING_SNAKE_CASE : Optional[int] = approx_mode SCREAMING_SNAKE_CASE : List[Any] = initial_prior_first_n_blocks SCREAMING_SNAKE_CASE : Union[str, Any] = initial_prior_diagonal_n_blocks
698
1
"""simple docstring""" import logging import torch from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.bert.modeling_bert import ( BERT_INPUTS_DOCSTRING, BERT_START_DOCSTRING, BertEncoder, BertModel, BertPreTrainedModel, ) lowerCamelCase__ : Optional[Any] = logging.getLogger(__name__) class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :Any , lowerCamelCase_ :List[str] , lowerCamelCase_ :List[str]=None , lowerCamelCase_ :Optional[Any]=None ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.layer[current_layer](lowerCamelCase_ , lowerCamelCase_ , head_mask[current_layer] ) SCREAMING_SNAKE_CASE : str = layer_outputs[0] return hidden_states @add_start_docstrings( """The bare Bert Model transformer with PABEE outputting raw hidden-states without any specific head on top.""" , _UpperCAmelCase , ) class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __init__( self :Optional[Any] , lowerCamelCase_ :List[str] ) -> List[Any]: '''simple docstring''' super().__init__(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = BertEncoderWithPabee(lowerCamelCase_ ) self.init_weights() SCREAMING_SNAKE_CASE : Dict = 0 SCREAMING_SNAKE_CASE : Union[str, Any] = 0 SCREAMING_SNAKE_CASE : Optional[int] = 0 SCREAMING_SNAKE_CASE : Union[str, Any] = 0 def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :Any ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = threshold def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :int ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = patience def __lowerCAmelCase ( self :Union[str, Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = 0 SCREAMING_SNAKE_CASE : int = 0 def __lowerCAmelCase ( self :Tuple ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.inference_layers_num / self.inference_instances_num SCREAMING_SNAKE_CASE : List[str] = ( f"*** Patience = {self.patience} Avg. Inference Layers = {avg_inf_layers:.2f} Speed Up =" f" {1 - avg_inf_layers / self.config.num_hidden_layers:.2f} ***" ) print(lowerCamelCase_ ) @add_start_docstrings_to_model_forward(lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :Union[str, Any]=None , lowerCamelCase_ :str=None , lowerCamelCase_ :Union[str, Any]=None , lowerCamelCase_ :List[str]=None , lowerCamelCase_ :Dict=None , lowerCamelCase_ :Optional[Any]=None , lowerCamelCase_ :Optional[int]=None , lowerCamelCase_ :Tuple=None , lowerCamelCase_ :Optional[Any]=None , lowerCamelCase_ :int=None , lowerCamelCase_ :Optional[int]=False , ) -> List[str]: '''simple docstring''' if input_ids is not None and inputs_embeds is not None: raise ValueError('''You cannot specify both input_ids and inputs_embeds at the same time''' ) elif input_ids is not None: SCREAMING_SNAKE_CASE : str = input_ids.size() elif inputs_embeds is not None: SCREAMING_SNAKE_CASE : List[str] = inputs_embeds.size()[:-1] else: raise ValueError('''You have to specify either input_ids or inputs_embeds''' ) SCREAMING_SNAKE_CASE : str = input_ids.device if input_ids is not None else inputs_embeds.device if attention_mask is None: SCREAMING_SNAKE_CASE : List[str] = torch.ones(lowerCamelCase_ , device=lowerCamelCase_ ) if token_type_ids is None: SCREAMING_SNAKE_CASE : Optional[int] = torch.zeros(lowerCamelCase_ , dtype=torch.long , device=lowerCamelCase_ ) # We can provide a self-attention mask of dimensions [batch_size, from_seq_length, to_seq_length] # ourselves in which case we just need to make it broadcastable to all heads. SCREAMING_SNAKE_CASE : torch.Tensor = self.get_extended_attention_mask(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) # If a 2D ou 3D attention mask is provided for the cross-attention # we need to make broadcastable to [batch_size, num_heads, seq_length, seq_length] if self.config.is_decoder and encoder_hidden_states is not None: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = encoder_hidden_states.size() SCREAMING_SNAKE_CASE : str = (encoder_batch_size, encoder_sequence_length) if encoder_attention_mask is None: SCREAMING_SNAKE_CASE : int = torch.ones(lowerCamelCase_ , device=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = self.invert_attention_mask(lowerCamelCase_ ) else: SCREAMING_SNAKE_CASE : List[str] = None # Prepare head mask if needed # 1.0 in head_mask indicate we keep the head # attention_probs has shape bsz x n_heads x N x N # input head_mask has shape [num_heads] or [num_hidden_layers x num_heads] # and head_mask is converted to shape [num_hidden_layers x batch x num_heads x seq_length x seq_length] SCREAMING_SNAKE_CASE : List[Any] = self.get_head_mask(lowerCamelCase_ , self.config.num_hidden_layers ) SCREAMING_SNAKE_CASE : Optional[int] = self.embeddings( input_ids=lowerCamelCase_ , position_ids=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , inputs_embeds=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = embedding_output if self.training: SCREAMING_SNAKE_CASE : Dict = [] for i in range(self.config.num_hidden_layers ): SCREAMING_SNAKE_CASE : Union[str, Any] = self.encoder.adaptive_forward( lowerCamelCase_ , current_layer=lowerCamelCase_ , attention_mask=lowerCamelCase_ , head_mask=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = self.pooler(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = output_layers[i](output_dropout(lowerCamelCase_ ) ) res.append(lowerCamelCase_ ) elif self.patience == 0: # Use all layers for inference SCREAMING_SNAKE_CASE : List[Any] = self.encoder( lowerCamelCase_ , attention_mask=lowerCamelCase_ , head_mask=lowerCamelCase_ , encoder_hidden_states=lowerCamelCase_ , encoder_attention_mask=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : str = self.pooler(encoder_outputs[0] ) SCREAMING_SNAKE_CASE : Dict = [output_layers[self.config.num_hidden_layers - 1](lowerCamelCase_ )] else: SCREAMING_SNAKE_CASE : str = 0 SCREAMING_SNAKE_CASE : List[str] = None SCREAMING_SNAKE_CASE : List[str] = 0 for i in range(self.config.num_hidden_layers ): calculated_layer_num += 1 SCREAMING_SNAKE_CASE : Union[str, Any] = self.encoder.adaptive_forward( lowerCamelCase_ , current_layer=lowerCamelCase_ , attention_mask=lowerCamelCase_ , head_mask=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = self.pooler(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = output_layers[i](lowerCamelCase_ ) if regression: SCREAMING_SNAKE_CASE : str = logits.detach() if patient_result is not None: SCREAMING_SNAKE_CASE : Optional[int] = patient_result.detach() if (patient_result is not None) and torch.abs(patient_result - labels ) < self.regression_threshold: patient_counter += 1 else: SCREAMING_SNAKE_CASE : List[Any] = 0 else: SCREAMING_SNAKE_CASE : List[str] = logits.detach().argmax(dim=1 ) if patient_result is not None: SCREAMING_SNAKE_CASE : List[str] = patient_result.detach().argmax(dim=1 ) if (patient_result is not None) and torch.all(labels.eq(lowerCamelCase_ ) ): patient_counter += 1 else: SCREAMING_SNAKE_CASE : Dict = 0 SCREAMING_SNAKE_CASE : List[Any] = logits if patient_counter == self.patience: break SCREAMING_SNAKE_CASE : int = [patient_result] self.inference_layers_num += calculated_layer_num self.inference_instances_num += 1 return res @add_start_docstrings( """Bert Model transformer with PABEE and a sequence classification/regression head on top (a linear layer on top of the pooled output) e.g. for GLUE tasks. """ , _UpperCAmelCase , ) class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __init__( self :Tuple , lowerCamelCase_ :str ) -> Dict: '''simple docstring''' super().__init__(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = config.num_labels SCREAMING_SNAKE_CASE : str = BertModelWithPabee(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = nn.Dropout(config.hidden_dropout_prob ) SCREAMING_SNAKE_CASE : Any = nn.ModuleList( [nn.Linear(config.hidden_size , self.config.num_labels ) for _ in range(config.num_hidden_layers )] ) self.init_weights() @add_start_docstrings_to_model_forward(lowerCamelCase_ ) def __lowerCAmelCase ( self :int , lowerCamelCase_ :Optional[Any]=None , lowerCamelCase_ :Optional[int]=None , lowerCamelCase_ :Dict=None , lowerCamelCase_ :Dict=None , lowerCamelCase_ :Union[str, Any]=None , lowerCamelCase_ :Optional[int]=None , lowerCamelCase_ :str=None , ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.bert( input_ids=lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , position_ids=lowerCamelCase_ , head_mask=lowerCamelCase_ , inputs_embeds=lowerCamelCase_ , output_dropout=self.dropout , output_layers=self.classifiers , regression=self.num_labels == 1 , ) SCREAMING_SNAKE_CASE : Optional[Any] = (logits[-1],) if labels is not None: SCREAMING_SNAKE_CASE : str = None SCREAMING_SNAKE_CASE : int = 0 for ix, logits_item in enumerate(lowerCamelCase_ ): if self.num_labels == 1: # We are doing regression SCREAMING_SNAKE_CASE : str = MSELoss() SCREAMING_SNAKE_CASE : Any = loss_fct(logits_item.view(-1 ) , labels.view(-1 ) ) else: SCREAMING_SNAKE_CASE : Optional[Any] = CrossEntropyLoss() SCREAMING_SNAKE_CASE : Optional[int] = loss_fct(logits_item.view(-1 , self.num_labels ) , labels.view(-1 ) ) if total_loss is None: SCREAMING_SNAKE_CASE : Dict = loss else: total_loss += loss * (ix + 1) total_weights += ix + 1 SCREAMING_SNAKE_CASE : Dict = (total_loss / total_weights,) + outputs return outputs
698
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : str = logging.get_logger(__name__) lowerCamelCase__ : List[str] = { "facebook/nllb-moe-54B": "https://huggingface.co/facebook/nllb-moe-54b/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """nllb-moe""" UpperCamelCase = ["""past_key_values"""] UpperCamelCase = {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""} def __init__( self :List[str] , lowerCamelCase_ :Optional[int]=12_81_12 , lowerCamelCase_ :str=10_24 , lowerCamelCase_ :Any=12 , lowerCamelCase_ :Optional[int]=40_96 , lowerCamelCase_ :int=16 , lowerCamelCase_ :List[str]=12 , lowerCamelCase_ :Optional[int]=40_96 , lowerCamelCase_ :int=16 , lowerCamelCase_ :Union[str, Any]=0.0_5 , lowerCamelCase_ :Optional[int]=0.0_5 , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :Optional[Any]=True , lowerCamelCase_ :Tuple="relu" , lowerCamelCase_ :str=10_24 , lowerCamelCase_ :str=0.1 , lowerCamelCase_ :Optional[int]=0.1 , lowerCamelCase_ :List[str]=0.0 , lowerCamelCase_ :Optional[Any]=0.0_2 , lowerCamelCase_ :Optional[int]=2 , lowerCamelCase_ :Dict=True , lowerCamelCase_ :Any=False , lowerCamelCase_ :Optional[Any]="float32" , lowerCamelCase_ :Optional[Any]=False , lowerCamelCase_ :List[Any]=1_28 , lowerCamelCase_ :Any=64 , lowerCamelCase_ :Optional[int]=4 , lowerCamelCase_ :List[str]=4 , lowerCamelCase_ :Union[str, Any]=0.0_0_1 , lowerCamelCase_ :Optional[int]=0.0_0_1 , lowerCamelCase_ :List[str]="all" , lowerCamelCase_ :Optional[int]=False , lowerCamelCase_ :Any=False , lowerCamelCase_ :Tuple=1.0 , lowerCamelCase_ :Union[str, Any]=0.2 , lowerCamelCase_ :List[str]=1 , lowerCamelCase_ :Optional[int]=0 , lowerCamelCase_ :int=2 , lowerCamelCase_ :List[str]=False , **lowerCamelCase_ :int , ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : int = vocab_size SCREAMING_SNAKE_CASE : str = max_position_embeddings SCREAMING_SNAKE_CASE : str = d_model SCREAMING_SNAKE_CASE : Optional[int] = encoder_ffn_dim SCREAMING_SNAKE_CASE : Any = encoder_layers SCREAMING_SNAKE_CASE : Any = encoder_attention_heads SCREAMING_SNAKE_CASE : List[Any] = decoder_ffn_dim SCREAMING_SNAKE_CASE : str = decoder_layers SCREAMING_SNAKE_CASE : List[Any] = decoder_attention_heads SCREAMING_SNAKE_CASE : List[Any] = dropout SCREAMING_SNAKE_CASE : List[str] = attention_dropout SCREAMING_SNAKE_CASE : str = activation_dropout SCREAMING_SNAKE_CASE : Any = activation_function SCREAMING_SNAKE_CASE : Tuple = init_std SCREAMING_SNAKE_CASE : str = encoder_layerdrop SCREAMING_SNAKE_CASE : Union[str, Any] = decoder_layerdrop SCREAMING_SNAKE_CASE : List[Any] = use_cache SCREAMING_SNAKE_CASE : Optional[int] = encoder_layers SCREAMING_SNAKE_CASE : List[str] = scale_embedding # scale factor will be sqrt(d_model) if True SCREAMING_SNAKE_CASE : int = router_z_loss_coef SCREAMING_SNAKE_CASE : Any = router_aux_loss_coef SCREAMING_SNAKE_CASE : str = decoder_sparse_step SCREAMING_SNAKE_CASE : str = encoder_sparse_step SCREAMING_SNAKE_CASE : List[str] = num_experts SCREAMING_SNAKE_CASE : Union[str, Any] = expert_capacity SCREAMING_SNAKE_CASE : Tuple = router_bias if router_dtype not in ["float32", "float16", "bfloat16"]: raise ValueError(f"`router_dtype` must be one of 'float32', 'float16' or 'bfloat16', got {router_dtype}" ) SCREAMING_SNAKE_CASE : Union[str, Any] = router_dtype SCREAMING_SNAKE_CASE : Union[str, Any] = router_ignore_padding_tokens SCREAMING_SNAKE_CASE : int = batch_prioritized_routing SCREAMING_SNAKE_CASE : Optional[int] = second_expert_policy SCREAMING_SNAKE_CASE : Union[str, Any] = normalize_router_prob_before_dropping SCREAMING_SNAKE_CASE : Any = moe_eval_capacity_token_fraction SCREAMING_SNAKE_CASE : Optional[Any] = moe_token_dropout SCREAMING_SNAKE_CASE : Tuple = output_router_logits super().__init__( pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , is_encoder_decoder=lowerCamelCase_ , decoder_start_token_id=lowerCamelCase_ , **lowerCamelCase_ , )
698
1
"""simple docstring""" from random import randint from tempfile import TemporaryFile import numpy as np def __A ( a_ : List[str] , a_ : int , a_ : Any )-> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = 0 if start < end: SCREAMING_SNAKE_CASE : List[str] = randint(a_ , a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = a[end] SCREAMING_SNAKE_CASE : Optional[Any] = a[pivot] SCREAMING_SNAKE_CASE : Dict = temp SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[Any] = _in_place_partition(a_ , a_ , a_ ) count += _in_place_quick_sort(a_ , a_ , p - 1 ) count += _in_place_quick_sort(a_ , p + 1 , a_ ) return count def __A ( a_ : Tuple , a_ : Tuple , a_ : List[str] )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = 0 SCREAMING_SNAKE_CASE : Union[str, Any] = randint(a_ , a_ ) SCREAMING_SNAKE_CASE : Optional[Any] = a[end] SCREAMING_SNAKE_CASE : Union[str, Any] = a[pivot] SCREAMING_SNAKE_CASE : Union[str, Any] = temp SCREAMING_SNAKE_CASE : Optional[Any] = start - 1 for index in range(a_ , a_ ): count += 1 if a[index] < a[end]: # check if current val is less than pivot value SCREAMING_SNAKE_CASE : List[Any] = new_pivot_index + 1 SCREAMING_SNAKE_CASE : int = a[new_pivot_index] SCREAMING_SNAKE_CASE : Tuple = a[index] SCREAMING_SNAKE_CASE : int = temp SCREAMING_SNAKE_CASE : Tuple = a[new_pivot_index + 1] SCREAMING_SNAKE_CASE : int = a[end] SCREAMING_SNAKE_CASE : Dict = temp return new_pivot_index + 1, count lowerCamelCase__ : Any = TemporaryFile() lowerCamelCase__ : Union[str, Any] = 100 # 1000 elements are to be sorted lowerCamelCase__ , lowerCamelCase__ : Optional[Any] = 0, 1 # mean and standard deviation lowerCamelCase__ : Union[str, Any] = np.random.normal(mu, sigma, p) np.save(outfile, X) print("The array is") print(X) outfile.seek(0) # using the same array lowerCamelCase__ : Dict = np.load(outfile) lowerCamelCase__ : Optional[int] = len(M) - 1 lowerCamelCase__ : List[str] = _in_place_quick_sort(M, 0, r) print( "No of Comparisons for 100 elements selected from a standard normal distribution" "is :" ) print(z)
698
"""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 lowerCamelCase__ : Union[str, Any] = "CompVis/stable-diffusion-v1-1" lowerCamelCase__ : Optional[Any] = "CompVis/stable-diffusion-v1-2" lowerCamelCase__ : Dict = "CompVis/stable-diffusion-v1-3" lowerCamelCase__ : List[str] = "CompVis/stable-diffusion-v1-4" class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __init__( self :Any , lowerCamelCase_ :AutoencoderKL , lowerCamelCase_ :CLIPTextModel , lowerCamelCase_ :CLIPTokenizer , lowerCamelCase_ :UNetaDConditionModel , lowerCamelCase_ :Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , lowerCamelCase_ :StableDiffusionSafetyChecker , lowerCamelCase_ :CLIPImageProcessor , lowerCamelCase_ :bool = True , ) -> List[str]: '''simple docstring''' super()._init_() SCREAMING_SNAKE_CASE : Tuple = StableDiffusionPipeline.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = StableDiffusionPipeline.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = StableDiffusionPipeline.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = StableDiffusionPipeline( vae=lowerCamelCase_ , text_encoder=lowerCamelCase_ , tokenizer=lowerCamelCase_ , unet=lowerCamelCase_ , scheduler=lowerCamelCase_ , safety_checker=lowerCamelCase_ , feature_extractor=lowerCamelCase_ , requires_safety_checker=lowerCamelCase_ , ) self.register_modules(pipelinea=self.pipea , pipelinea=self.pipea , pipelinea=self.pipea , pipelinea=self.pipea ) @property def __lowerCAmelCase ( self :Dict ) -> Dict[str, Any]: '''simple docstring''' return {k: getattr(self , lowerCamelCase_ ) for k in self.config.keys() if not k.startswith('''_''' )} def __lowerCAmelCase ( self :int , lowerCamelCase_ :Optional[Union[str, int]] = "auto" ) -> Tuple: '''simple docstring''' if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory SCREAMING_SNAKE_CASE : str = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(lowerCamelCase_ ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Dict: '''simple docstring''' self.enable_attention_slicing(lowerCamelCase_ ) @torch.no_grad() def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :List[str] , ) -> Tuple: '''simple docstring''' return self.pipea( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) @torch.no_grad() def __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :Tuple , ) -> Optional[Any]: '''simple docstring''' return self.pipea( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) @torch.no_grad() def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :Dict , ) -> List[str]: '''simple docstring''' return self.pipea( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) @torch.no_grad() def __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :List[Any] , ) -> Optional[Any]: '''simple docstring''' return self.pipea( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) @torch.no_grad() def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :Optional[Any] , ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = '''cuda''' if torch.cuda.is_available() else '''cpu''' self.to(lowerCamelCase_ ) # 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 SCREAMING_SNAKE_CASE : str = self.textaimg_sda_a( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) # Get first result from Stable Diffusion Checkpoint v1.2 SCREAMING_SNAKE_CASE : Optional[Any] = self.textaimg_sda_a( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) # Get first result from Stable Diffusion Checkpoint v1.3 SCREAMING_SNAKE_CASE : Tuple = self.textaimg_sda_a( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) # Get first result from Stable Diffusion Checkpoint v1.4 SCREAMING_SNAKE_CASE : Union[str, Any] = self.textaimg_sda_a( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) # 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]] )
698
1
"""simple docstring""" from transformers import BertTokenizer, EncoderDecoderModel, SeqaSeqTrainer, SeqaSeqTrainingArguments from transformers.testing_utils import TestCasePlus, require_torch, slow from transformers.utils import is_datasets_available if is_datasets_available(): import datasets class lowercase__( _UpperCAmelCase ): '''simple docstring''' @slow @require_torch def __lowerCAmelCase ( self :int ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = EncoderDecoderModel.from_encoder_decoder_pretrained('''prajjwal1/bert-tiny''' , '''prajjwal1/bert-tiny''' ) SCREAMING_SNAKE_CASE : Any = BertTokenizer.from_pretrained('''bert-base-uncased''' ) SCREAMING_SNAKE_CASE : List[Any] = bertabert.config.encoder.vocab_size SCREAMING_SNAKE_CASE : Any = tokenizer.sep_token_id SCREAMING_SNAKE_CASE : Optional[int] = tokenizer.cls_token_id SCREAMING_SNAKE_CASE : Any = 1_28 SCREAMING_SNAKE_CASE : Dict = datasets.load_dataset('''cnn_dailymail''' , '''3.0.0''' , split='''train[:1%]''' ) SCREAMING_SNAKE_CASE : str = datasets.load_dataset('''cnn_dailymail''' , '''3.0.0''' , split='''validation[:1%]''' ) SCREAMING_SNAKE_CASE : Optional[int] = train_dataset.select(range(32 ) ) SCREAMING_SNAKE_CASE : str = val_dataset.select(range(16 ) ) SCREAMING_SNAKE_CASE : List[str] = 4 def _map_to_encoder_decoder_inputs(lowerCamelCase_ :Any ): # Tokenizer will automatically set [BOS] <text> [EOS] SCREAMING_SNAKE_CASE : Optional[Any] = tokenizer(batch['''article'''] , padding='''max_length''' , truncation=lowerCamelCase_ , max_length=5_12 ) SCREAMING_SNAKE_CASE : Optional[int] = tokenizer(batch['''highlights'''] , padding='''max_length''' , truncation=lowerCamelCase_ , max_length=1_28 ) SCREAMING_SNAKE_CASE : List[str] = inputs.input_ids SCREAMING_SNAKE_CASE : str = inputs.attention_mask SCREAMING_SNAKE_CASE : List[str] = outputs.input_ids SCREAMING_SNAKE_CASE : Union[str, Any] = outputs.input_ids.copy() SCREAMING_SNAKE_CASE : Dict = [ [-1_00 if token == tokenizer.pad_token_id else token for token in labels] for labels in batch['''labels'''] ] SCREAMING_SNAKE_CASE : Dict = outputs.attention_mask assert all(len(lowerCamelCase_ ) == 5_12 for x in inputs.input_ids ) assert all(len(lowerCamelCase_ ) == 1_28 for x in outputs.input_ids ) return batch def _compute_metrics(lowerCamelCase_ :Dict ): SCREAMING_SNAKE_CASE : Optional[int] = pred.label_ids SCREAMING_SNAKE_CASE : Dict = pred.predictions # all unnecessary tokens are removed SCREAMING_SNAKE_CASE : str = tokenizer.batch_decode(lowerCamelCase_ , skip_special_tokens=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = tokenizer.batch_decode(lowerCamelCase_ , skip_special_tokens=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = sum([int(pred_str[i] == label_str[i] ) for i in range(len(lowerCamelCase_ ) )] ) / len(lowerCamelCase_ ) return {"accuracy": accuracy} # map train dataset SCREAMING_SNAKE_CASE : Union[str, Any] = train_dataset.map( _map_to_encoder_decoder_inputs , batched=lowerCamelCase_ , batch_size=lowerCamelCase_ , remove_columns=['''article''', '''highlights'''] , ) train_dataset.set_format( type='''torch''' , columns=['''input_ids''', '''attention_mask''', '''decoder_input_ids''', '''decoder_attention_mask''', '''labels'''] , ) # same for validation dataset SCREAMING_SNAKE_CASE : str = val_dataset.map( _map_to_encoder_decoder_inputs , batched=lowerCamelCase_ , batch_size=lowerCamelCase_ , remove_columns=['''article''', '''highlights'''] , ) val_dataset.set_format( type='''torch''' , columns=['''input_ids''', '''attention_mask''', '''decoder_input_ids''', '''decoder_attention_mask''', '''labels'''] , ) SCREAMING_SNAKE_CASE : Dict = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : int = SeqaSeqTrainingArguments( output_dir=lowerCamelCase_ , per_device_train_batch_size=lowerCamelCase_ , per_device_eval_batch_size=lowerCamelCase_ , predict_with_generate=lowerCamelCase_ , evaluation_strategy='''steps''' , do_train=lowerCamelCase_ , do_eval=lowerCamelCase_ , warmup_steps=0 , eval_steps=2 , logging_steps=2 , ) # instantiate trainer SCREAMING_SNAKE_CASE : int = SeqaSeqTrainer( model=lowerCamelCase_ , args=lowerCamelCase_ , compute_metrics=_compute_metrics , train_dataset=lowerCamelCase_ , eval_dataset=lowerCamelCase_ , tokenizer=lowerCamelCase_ , ) # start training trainer.train()
698
"""simple docstring""" def __A ( a_ : list , a_ : int = 0 )-> list: '''simple docstring''' SCREAMING_SNAKE_CASE : int = length or len(a_ ) SCREAMING_SNAKE_CASE : List[Any] = False for i in range(length - 1 ): if list_data[i] > list_data[i + 1]: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[Any] = list_data[i + 1], list_data[i] SCREAMING_SNAKE_CASE : Optional[Any] = True return list_data if not swapped else bubble_sort(a_ , length - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
698
1
"""simple docstring""" from math import pi def __A ( a_ : int , a_ : int )-> float: '''simple docstring''' return 2 * pi * radius * (angle / 3_60) if __name__ == "__main__": print(arc_length(90, 10))
698
"""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 lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = 42 UpperCamelCase = 42 def __init__( self :List[str] , lowerCamelCase_ :UNetaDModel , lowerCamelCase_ :ScoreSdeVeScheduler ) -> int: '''simple docstring''' super().__init__() self.register_modules(unet=lowerCamelCase_ , scheduler=lowerCamelCase_ ) @torch.no_grad() def __call__( self :int , lowerCamelCase_ :int = 1 , lowerCamelCase_ :int = 20_00 , lowerCamelCase_ :Optional[Union[torch.Generator, List[torch.Generator]]] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , **lowerCamelCase_ :Union[str, Any] , ) -> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.unet.config.sample_size SCREAMING_SNAKE_CASE : List[str] = (batch_size, 3, img_size, img_size) SCREAMING_SNAKE_CASE : Any = self.unet SCREAMING_SNAKE_CASE : Dict = randn_tensor(lowerCamelCase_ , generator=lowerCamelCase_ ) * self.scheduler.init_noise_sigma SCREAMING_SNAKE_CASE : Union[str, Any] = sample.to(self.device ) self.scheduler.set_timesteps(lowerCamelCase_ ) self.scheduler.set_sigmas(lowerCamelCase_ ) for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): SCREAMING_SNAKE_CASE : Tuple = self.scheduler.sigmas[i] * torch.ones(shape[0] , device=self.device ) # correction step for _ in range(self.scheduler.config.correct_steps ): SCREAMING_SNAKE_CASE : Optional[Any] = self.unet(lowerCamelCase_ , lowerCamelCase_ ).sample SCREAMING_SNAKE_CASE : List[Any] = self.scheduler.step_correct(lowerCamelCase_ , lowerCamelCase_ , generator=lowerCamelCase_ ).prev_sample # prediction step SCREAMING_SNAKE_CASE : Any = model(lowerCamelCase_ , lowerCamelCase_ ).sample SCREAMING_SNAKE_CASE : Optional[Any] = self.scheduler.step_pred(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , generator=lowerCamelCase_ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Union[str, Any] = output.prev_sample, output.prev_sample_mean SCREAMING_SNAKE_CASE : List[str] = sample_mean.clamp(0 , 1 ) SCREAMING_SNAKE_CASE : Any = sample.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": SCREAMING_SNAKE_CASE : Any = self.numpy_to_pil(lowerCamelCase_ ) if not return_dict: return (sample,) return ImagePipelineOutput(images=lowerCamelCase_ )
698
1
"""simple docstring""" import importlib import os from dataclasses import dataclass from enum import Enum from typing import Any, Dict, Optional, Union import torch from ..utils import BaseOutput lowerCamelCase__ : str = "scheduler_config.json" class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = 1 UpperCamelCase = 2 UpperCamelCase = 3 UpperCamelCase = 4 UpperCamelCase = 5 UpperCamelCase = 6 UpperCamelCase = 7 UpperCamelCase = 8 UpperCamelCase = 9 UpperCamelCase = 10 UpperCamelCase = 11 UpperCamelCase = 12 UpperCamelCase = 13 UpperCamelCase = 14 @dataclass class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = 42 class lowercase__: '''simple docstring''' UpperCamelCase = SCHEDULER_CONFIG_NAME UpperCamelCase = [] UpperCamelCase = True @classmethod def __lowerCAmelCase ( cls :Dict , lowerCamelCase_ :Dict[str, Any] = None , lowerCamelCase_ :Optional[str] = None , lowerCamelCase_ :Dict=False , **lowerCamelCase_ :Tuple , ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = cls.load_config( pretrained_model_name_or_path=lowerCamelCase_ , subfolder=lowerCamelCase_ , return_unused_kwargs=lowerCamelCase_ , return_commit_hash=lowerCamelCase_ , **lowerCamelCase_ , ) return cls.from_config(lowerCamelCase_ , return_unused_kwargs=lowerCamelCase_ , **lowerCamelCase_ ) def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :Union[str, os.PathLike] , lowerCamelCase_ :bool = False , **lowerCamelCase_ :Any ) -> Optional[int]: '''simple docstring''' self.save_config(save_directory=lowerCamelCase_ , push_to_hub=lowerCamelCase_ , **lowerCamelCase_ ) @property def __lowerCAmelCase ( self :Any ) -> str: '''simple docstring''' return self._get_compatibles() @classmethod def __lowerCAmelCase ( cls :Dict ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = list(set([cls.__name__] + cls._compatibles ) ) SCREAMING_SNAKE_CASE : Union[str, Any] = importlib.import_module(__name__.split('''.''' )[0] ) SCREAMING_SNAKE_CASE : List[Any] = [ getattr(lowerCamelCase_ , lowerCamelCase_ ) for c in compatible_classes_str if hasattr(lowerCamelCase_ , lowerCamelCase_ ) ] return compatible_classes
698
"""simple docstring""" import qiskit def __A ( a_ : int , a_ : int )-> qiskit.result.counts.Counts: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = qiskit.Aer.get_backend('''aer_simulator''' ) # Create a Quantum Circuit acting on the q register SCREAMING_SNAKE_CASE : str = qiskit.QuantumCircuit(a_ , a_ ) # Apply X (NOT) Gate to Qubits 0 & 1 circuit.x(0 ) circuit.x(1 ) # Map the quantum measurement to the classical bits circuit.measure([0, 1] , [0, 1] ) # Execute the circuit on the qasm simulator SCREAMING_SNAKE_CASE : int = qiskit.execute(a_ , a_ , shots=10_00 ) # Return the histogram data of the results of the experiment. return job.result().get_counts(a_ ) if __name__ == "__main__": lowerCamelCase__ : List[Any] = single_qubit_measure(2, 2) print(f'''Total count for various states are: {counts}''')
698
1
"""simple docstring""" from collections import OrderedDict from typing import Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...feature_extraction_utils import FeatureExtractionMixin from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import TensorType, logging lowerCamelCase__ : List[Any] = logging.get_logger(__name__) lowerCamelCase__ : Dict = { "deepmind/language-perceiver": "https://huggingface.co/deepmind/language-perceiver/resolve/main/config.json", # See all Perceiver models at https://huggingface.co/models?filter=perceiver } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """perceiver""" def __init__( self :List[Any] , lowerCamelCase_ :Union[str, Any]=2_56 , lowerCamelCase_ :Dict=12_80 , lowerCamelCase_ :Union[str, Any]=7_68 , lowerCamelCase_ :str=1 , lowerCamelCase_ :Optional[Any]=26 , lowerCamelCase_ :str=8 , lowerCamelCase_ :List[Any]=8 , lowerCamelCase_ :Optional[int]=None , lowerCamelCase_ :Tuple=None , lowerCamelCase_ :List[Any]="kv" , lowerCamelCase_ :Tuple=1 , lowerCamelCase_ :List[Any]=1 , lowerCamelCase_ :List[Any]="gelu" , lowerCamelCase_ :Tuple=0.1 , lowerCamelCase_ :List[str]=0.0_2 , lowerCamelCase_ :List[str]=1E-12 , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :Dict=2_62 , lowerCamelCase_ :Optional[Any]=20_48 , lowerCamelCase_ :Dict=56 , lowerCamelCase_ :Tuple=[3_68, 4_96] , lowerCamelCase_ :Dict=16 , lowerCamelCase_ :Union[str, Any]=19_20 , lowerCamelCase_ :Union[str, Any]=16 , lowerCamelCase_ :Optional[int]=[1, 16, 2_24, 2_24] , **lowerCamelCase_ :List[str] , ) -> int: '''simple docstring''' super().__init__(**lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = num_latents SCREAMING_SNAKE_CASE : Optional[int] = d_latents SCREAMING_SNAKE_CASE : Tuple = d_model SCREAMING_SNAKE_CASE : Tuple = num_blocks SCREAMING_SNAKE_CASE : Dict = num_self_attends_per_block SCREAMING_SNAKE_CASE : Optional[Any] = num_self_attention_heads SCREAMING_SNAKE_CASE : List[Any] = num_cross_attention_heads SCREAMING_SNAKE_CASE : Union[str, Any] = qk_channels SCREAMING_SNAKE_CASE : int = v_channels SCREAMING_SNAKE_CASE : List[Any] = cross_attention_shape_for_attention SCREAMING_SNAKE_CASE : List[Any] = self_attention_widening_factor SCREAMING_SNAKE_CASE : Optional[int] = cross_attention_widening_factor SCREAMING_SNAKE_CASE : Optional[Any] = hidden_act SCREAMING_SNAKE_CASE : str = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = initializer_range SCREAMING_SNAKE_CASE : Dict = layer_norm_eps SCREAMING_SNAKE_CASE : Union[str, Any] = use_query_residual # masked language modeling attributes SCREAMING_SNAKE_CASE : Any = vocab_size SCREAMING_SNAKE_CASE : str = max_position_embeddings # image classification attributes SCREAMING_SNAKE_CASE : Optional[Any] = image_size # flow attributes SCREAMING_SNAKE_CASE : List[Any] = train_size # multimodal autoencoding attributes SCREAMING_SNAKE_CASE : Union[str, Any] = num_frames SCREAMING_SNAKE_CASE : Tuple = audio_samples_per_frame SCREAMING_SNAKE_CASE : Any = samples_per_patch SCREAMING_SNAKE_CASE : List[str] = output_shape class lowercase__( _UpperCAmelCase ): '''simple docstring''' @property def __lowerCAmelCase ( self :Dict ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": SCREAMING_SNAKE_CASE : str = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: SCREAMING_SNAKE_CASE : List[str] = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''inputs''', dynamic_axis), ('''attention_mask''', dynamic_axis), ] ) @property def __lowerCAmelCase ( self :List[str] ) -> float: '''simple docstring''' return 1E-4 def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"] , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[TensorType] = None , lowerCamelCase_ :int = 3 , lowerCamelCase_ :int = 40 , lowerCamelCase_ :int = 40 , ) -> Mapping[str, Any]: '''simple docstring''' if isinstance(lowerCamelCase_ , lowerCamelCase_ ): # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : str = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : List[str] = preprocessor.num_special_tokens_to_add(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=lowerCamelCase_ ) # Generate dummy inputs according to compute batch and sequence SCREAMING_SNAKE_CASE : str = [''' '''.join(['''a'''] ) * seq_length] * batch_size SCREAMING_SNAKE_CASE : Any = dict(preprocessor(lowerCamelCase_ , return_tensors=lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : List[Any] = inputs.pop('''input_ids''' ) return inputs elif isinstance(lowerCamelCase_ , lowerCamelCase_ ) and preprocessor.model_input_names[0] == "pixel_values": # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : Optional[int] = compute_effective_axis_dimension(lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_batch ) SCREAMING_SNAKE_CASE : Any = self._generate_dummy_images(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = dict(preprocessor(images=lowerCamelCase_ , return_tensors=lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : str = inputs.pop('''pixel_values''' ) return inputs else: raise ValueError( '''Unable to generate dummy inputs for the model. Please provide a tokenizer or a preprocessor.''' )
698
"""simple docstring""" # tests directory-specific settings - this file is run automatically # by pytest before any tests are run import doctest import sys import warnings from os.path import abspath, dirname, join import _pytest from transformers.testing_utils import HfDoctestModule, HfDocTestParser # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. lowerCamelCase__ : Optional[int] = abspath(join(dirname(__file__), "src")) sys.path.insert(1, git_repo_path) # silence FutureWarning warnings in tests since often we can't act on them until # they become normal warnings - i.e. the tests still need to test the current functionality warnings.simplefilter(action="ignore", category=FutureWarning) def __A ( a_ : Dict )-> str: '''simple docstring''' config.addinivalue_line( '''markers''' , '''is_pt_tf_cross_test: mark test to run only when PT and TF interactions are tested''' ) config.addinivalue_line( '''markers''' , '''is_pt_flax_cross_test: mark test to run only when PT and FLAX interactions are tested''' ) config.addinivalue_line('''markers''' , '''is_pipeline_test: mark test to run only when pipelines are tested''' ) config.addinivalue_line('''markers''' , '''is_staging_test: mark test to run only in the staging environment''' ) config.addinivalue_line('''markers''' , '''accelerate_tests: mark test that require accelerate''' ) config.addinivalue_line('''markers''' , '''tool_tests: mark the tool tests that are run on their specific schedule''' ) def __A ( a_ : Dict )-> Tuple: '''simple docstring''' from transformers.testing_utils import pytest_addoption_shared pytest_addoption_shared(a_ ) def __A ( a_ : Union[str, Any] )-> List[Any]: '''simple docstring''' from transformers.testing_utils import pytest_terminal_summary_main SCREAMING_SNAKE_CASE : List[str] = terminalreporter.config.getoption('''--make-reports''' ) if make_reports: pytest_terminal_summary_main(a_ , id=a_ ) def __A ( a_ : Dict , a_ : List[str] )-> Dict: '''simple docstring''' if exitstatus == 5: SCREAMING_SNAKE_CASE : List[str] = 0 # Doctest custom flag to ignore output. lowerCamelCase__ : Tuple = doctest.register_optionflag("IGNORE_RESULT") lowerCamelCase__ : Optional[int] = doctest.OutputChecker class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :int , lowerCamelCase_ :int , lowerCamelCase_ :Optional[Any] ) -> Dict: '''simple docstring''' if IGNORE_RESULT & optionflags: return True return OutputChecker.check_output(self , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) lowerCamelCase__ : str = CustomOutputChecker lowerCamelCase__ : Any = HfDoctestModule lowerCamelCase__ : int = HfDocTestParser
698
1
"""simple docstring""" from math import factorial class lowercase__: '''simple docstring''' def __init__( self :Optional[int] , lowerCamelCase_ :Any , lowerCamelCase_ :Any ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = real if isinstance(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE : str = [1] * rank else: SCREAMING_SNAKE_CASE : Any = rank def __repr__( self :Optional[Any] ) -> str: '''simple docstring''' return ( f"{self.real}+" f"{'+'.join(str(lowerCamelCase_ )+'E'+str(n+1 )for n,dual in enumerate(self.duals ) )}" ) def __lowerCAmelCase ( self :List[Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.duals.copy() while cur[-1] == 0: cur.pop(-1 ) return Dual(self.real , lowerCamelCase_ ) def __add__( self :List[str] , lowerCamelCase_ :Any ) -> List[str]: '''simple docstring''' if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): return Dual(self.real + other , self.duals ) SCREAMING_SNAKE_CASE : Any = self.duals.copy() SCREAMING_SNAKE_CASE : List[str] = other.duals.copy() if len(lowerCamelCase_ ) > len(lowerCamelCase_ ): o_dual.extend([1] * (len(lowerCamelCase_ ) - len(lowerCamelCase_ )) ) elif len(lowerCamelCase_ ) < len(lowerCamelCase_ ): s_dual.extend([1] * (len(lowerCamelCase_ ) - len(lowerCamelCase_ )) ) SCREAMING_SNAKE_CASE : str = [] for i in range(len(lowerCamelCase_ ) ): new_duals.append(s_dual[i] + o_dual[i] ) return Dual(self.real + other.real , lowerCamelCase_ ) UpperCamelCase = __add__ def __sub__( self :Dict , lowerCamelCase_ :Any ) -> Tuple: '''simple docstring''' return self + other * -1 def __mul__( self :List[str] , lowerCamelCase_ :List[Any] ) -> List[Any]: '''simple docstring''' if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Tuple = [] for i in self.duals: new_duals.append(i * other ) return Dual(self.real * other , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = [0] * (len(self.duals ) + len(other.duals ) + 1) for i, item in enumerate(self.duals ): for j, jtem in enumerate(other.duals ): new_duals[i + j + 1] += item * jtem for k in range(len(self.duals ) ): new_duals[k] += self.duals[k] * other.real for index in range(len(other.duals ) ): new_duals[index] += other.duals[index] * self.real return Dual(self.real * other.real , lowerCamelCase_ ) UpperCamelCase = __mul__ def __truediv__( self :Dict , lowerCamelCase_ :Tuple ) -> Tuple: '''simple docstring''' if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE : int = [] for i in self.duals: new_duals.append(i / other ) return Dual(self.real / other , lowerCamelCase_ ) raise ValueError def __floordiv__( self :Dict , lowerCamelCase_ :str ) -> Tuple: '''simple docstring''' if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE : int = [] for i in self.duals: new_duals.append(i // other ) return Dual(self.real // other , lowerCamelCase_ ) raise ValueError def __pow__( self :str , lowerCamelCase_ :str ) -> Union[str, Any]: '''simple docstring''' if n < 0 or isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise ValueError('''power must be a positive integer''' ) if n == 0: return 1 if n == 1: return self SCREAMING_SNAKE_CASE : List[Any] = self for _ in range(n - 1 ): x *= self return x def __A ( a_ : str , a_ : List[str] , a_ : Optional[Any] )-> int: '''simple docstring''' if not callable(a_ ): raise ValueError('''differentiate() requires a function as input for func''' ) if not isinstance(a_ , (float, int) ): raise ValueError('''differentiate() requires a float as input for position''' ) if not isinstance(a_ , a_ ): raise ValueError('''differentiate() requires an int as input for order''' ) SCREAMING_SNAKE_CASE : str = Dual(a_ , 1 ) SCREAMING_SNAKE_CASE : List[str] = func(a_ ) if order == 0: return result.real return result.duals[order - 1] * factorial(a_ ) if __name__ == "__main__": import doctest doctest.testmod() def __A ( a_ : Optional[Any] )-> str: '''simple docstring''' return y**2 * y**4 print(differentiate(f, 9, 2))
698
"""simple docstring""" from __future__ import annotations import os import tempfile import unittest from transformers import ConvBertConfig, 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 ( TFConvBertForMaskedLM, TFConvBertForMultipleChoice, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertModel, ) class lowercase__: '''simple docstring''' def __init__( self :Tuple , lowerCamelCase_ :Tuple , lowerCamelCase_ :Tuple=13 , lowerCamelCase_ :List[str]=7 , lowerCamelCase_ :Dict=True , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :List[str]=True , lowerCamelCase_ :Dict=True , lowerCamelCase_ :str=99 , lowerCamelCase_ :Optional[Any]=32 , lowerCamelCase_ :Tuple=2 , lowerCamelCase_ :int=4 , lowerCamelCase_ :Optional[Any]=37 , lowerCamelCase_ :Any="gelu" , lowerCamelCase_ :Any=0.1 , lowerCamelCase_ :Any=0.1 , lowerCamelCase_ :Optional[int]=5_12 , lowerCamelCase_ :str=16 , lowerCamelCase_ :Optional[int]=2 , lowerCamelCase_ :List[str]=0.0_2 , lowerCamelCase_ :int=3 , lowerCamelCase_ :List[Any]=4 , lowerCamelCase_ :Optional[Any]=None , ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : int = parent SCREAMING_SNAKE_CASE : str = 13 SCREAMING_SNAKE_CASE : str = 7 SCREAMING_SNAKE_CASE : List[Any] = True SCREAMING_SNAKE_CASE : List[str] = True SCREAMING_SNAKE_CASE : Union[str, Any] = True SCREAMING_SNAKE_CASE : str = True SCREAMING_SNAKE_CASE : Any = 99 SCREAMING_SNAKE_CASE : Dict = 3_84 SCREAMING_SNAKE_CASE : List[str] = 2 SCREAMING_SNAKE_CASE : int = 4 SCREAMING_SNAKE_CASE : Any = 37 SCREAMING_SNAKE_CASE : List[str] = '''gelu''' SCREAMING_SNAKE_CASE : List[str] = 0.1 SCREAMING_SNAKE_CASE : int = 0.1 SCREAMING_SNAKE_CASE : Union[str, Any] = 5_12 SCREAMING_SNAKE_CASE : int = 16 SCREAMING_SNAKE_CASE : List[str] = 2 SCREAMING_SNAKE_CASE : Tuple = 0.0_2 SCREAMING_SNAKE_CASE : List[str] = 3 SCREAMING_SNAKE_CASE : Union[str, Any] = 4 SCREAMING_SNAKE_CASE : str = 1_28 SCREAMING_SNAKE_CASE : List[str] = 2 SCREAMING_SNAKE_CASE : Union[str, Any] = 9 SCREAMING_SNAKE_CASE : Dict = 1 SCREAMING_SNAKE_CASE : List[str] = None def __lowerCAmelCase ( self :Optional[Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE : int = None if self.use_input_mask: SCREAMING_SNAKE_CASE : Tuple = random_attention_mask([self.batch_size, self.seq_length] ) SCREAMING_SNAKE_CASE : List[Any] = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) SCREAMING_SNAKE_CASE : List[str] = None SCREAMING_SNAKE_CASE : str = None SCREAMING_SNAKE_CASE : str = None if self.use_labels: SCREAMING_SNAKE_CASE : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor([self.batch_size] , self.num_choices ) SCREAMING_SNAKE_CASE : List[str] = ConvBertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , return_dict=lowerCamelCase_ , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def __lowerCAmelCase ( self :str , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :int , lowerCamelCase_ :List[str] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int , lowerCamelCase_ :List[Any] , lowerCamelCase_ :Tuple ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = TFConvBertModel(config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids} SCREAMING_SNAKE_CASE : Dict = [input_ids, input_mask] SCREAMING_SNAKE_CASE : List[Any] = model(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = model(lowerCamelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __lowerCAmelCase ( self :str , lowerCamelCase_ :List[Any] , lowerCamelCase_ :List[str] , lowerCamelCase_ :int , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :str , lowerCamelCase_ :str , lowerCamelCase_ :Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = TFConvBertForMaskedLM(config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } SCREAMING_SNAKE_CASE : Tuple = model(lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :str , lowerCamelCase_ :Any , lowerCamelCase_ :str , lowerCamelCase_ :Dict , lowerCamelCase_ :int , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.num_labels SCREAMING_SNAKE_CASE : Dict = TFConvBertForSequenceClassification(config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } SCREAMING_SNAKE_CASE : List[Any] = model(lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __lowerCAmelCase ( self :int , lowerCamelCase_ :Dict , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Dict , lowerCamelCase_ :Any , lowerCamelCase_ :Dict , lowerCamelCase_ :List[Any] , lowerCamelCase_ :List[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.num_choices SCREAMING_SNAKE_CASE : Optional[Any] = TFConvBertForMultipleChoice(config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = tf.tile(tf.expand_dims(lowerCamelCase_ , 1 ) , (1, self.num_choices, 1) ) SCREAMING_SNAKE_CASE : Dict = tf.tile(tf.expand_dims(lowerCamelCase_ , 1 ) , (1, self.num_choices, 1) ) SCREAMING_SNAKE_CASE : List[Any] = tf.tile(tf.expand_dims(lowerCamelCase_ , 1 ) , (1, self.num_choices, 1) ) SCREAMING_SNAKE_CASE : Any = { '''input_ids''': multiple_choice_inputs_ids, '''attention_mask''': multiple_choice_input_mask, '''token_type_ids''': multiple_choice_token_type_ids, } SCREAMING_SNAKE_CASE : Optional[int] = model(lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :Tuple , lowerCamelCase_ :Any , lowerCamelCase_ :List[Any] , lowerCamelCase_ :Tuple , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Tuple , lowerCamelCase_ :List[str] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.num_labels SCREAMING_SNAKE_CASE : List[Any] = TFConvBertForTokenClassification(config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } SCREAMING_SNAKE_CASE : int = model(lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Tuple , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :List[str] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = TFConvBertForQuestionAnswering(config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } SCREAMING_SNAKE_CASE : Dict = model(lowerCamelCase_ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __lowerCAmelCase ( self :List[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ) : Optional[Any] = config_and_inputs SCREAMING_SNAKE_CASE : Dict = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class lowercase__( _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase = ( ( TFConvBertModel, TFConvBertForMaskedLM, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertForMultipleChoice, ) if is_tf_available() else () ) UpperCamelCase = ( { """feature-extraction""": TFConvBertModel, """fill-mask""": TFConvBertForMaskedLM, """question-answering""": TFConvBertForQuestionAnswering, """text-classification""": TFConvBertForSequenceClassification, """token-classification""": TFConvBertForTokenClassification, """zero-shot""": TFConvBertForSequenceClassification, } if is_tf_available() else {} ) UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False def __lowerCAmelCase ( self :Optional[int] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = TFConvBertModelTester(self ) SCREAMING_SNAKE_CASE : int = ConfigTester(self , config_class=lowerCamelCase_ , hidden_size=37 ) def __lowerCAmelCase ( self :List[str] ) -> Union[str, Any]: '''simple docstring''' self.config_tester.run_common_tests() def __lowerCAmelCase ( self :Dict ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[int] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase_ ) def __lowerCAmelCase ( self :List[Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase_ ) def __lowerCAmelCase ( self :int ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Any ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*lowerCamelCase_ ) @slow def __lowerCAmelCase ( self :int ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : List[Any] = True SCREAMING_SNAKE_CASE : Tuple = True if hasattr(lowerCamelCase_ , '''use_cache''' ): SCREAMING_SNAKE_CASE : Any = True SCREAMING_SNAKE_CASE : str = getattr(self.model_tester , '''encoder_seq_length''' , self.model_tester.seq_length ) SCREAMING_SNAKE_CASE : Optional[int] = getattr(self.model_tester , '''key_length''' , lowerCamelCase_ ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : str = self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = model_class(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = len(model(lowerCamelCase_ ) ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(lowerCamelCase_ , saved_model=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = os.path.join(lowerCamelCase_ , '''saved_model''' , '''1''' ) SCREAMING_SNAKE_CASE : Tuple = tf.keras.models.load_model(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = model(lowerCamelCase_ ) if self.is_encoder_decoder: SCREAMING_SNAKE_CASE : Optional[int] = outputs['''encoder_hidden_states'''] SCREAMING_SNAKE_CASE : str = outputs['''encoder_attentions'''] else: SCREAMING_SNAKE_CASE : List[str] = outputs['''hidden_states'''] SCREAMING_SNAKE_CASE : List[Any] = outputs['''attentions'''] self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = getattr( self.model_tester , '''expected_num_hidden_layers''' , self.model_tester.num_hidden_layers + 1 ) self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) self.assertListEqual( list(output_hidden_states[0].shape[-2:] ) , [self.model_tester.seq_length, self.model_tester.hidden_size] , ) self.assertEqual(len(lowerCamelCase_ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(output_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length] , ) @slow def __lowerCAmelCase ( self :Any ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = TFConvBertModel.from_pretrained('''YituTech/conv-bert-base''' ) self.assertIsNotNone(lowerCamelCase_ ) def __lowerCAmelCase ( self :Tuple ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : str = True SCREAMING_SNAKE_CASE : List[str] = getattr(self.model_tester , '''decoder_seq_length''' , self.model_tester.seq_length ) SCREAMING_SNAKE_CASE : List[str] = getattr(self.model_tester , '''encoder_seq_length''' , self.model_tester.seq_length ) SCREAMING_SNAKE_CASE : List[str] = getattr(self.model_tester , '''key_length''' , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = getattr(self.model_tester , '''key_length''' , lowerCamelCase_ ) def check_decoder_attentions_output(lowerCamelCase_ :Optional[Any] ): SCREAMING_SNAKE_CASE : Any = len(lowerCamelCase_ ) self.assertEqual(out_len % 2 , 0 ) SCREAMING_SNAKE_CASE : int = outputs.decoder_attentions self.assertEqual(len(lowerCamelCase_ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length] , ) def check_encoder_attentions_output(lowerCamelCase_ :Optional[int] ): SCREAMING_SNAKE_CASE : List[Any] = [ t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions) ] self.assertEqual(len(lowerCamelCase_ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length] , ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Optional[int] = True SCREAMING_SNAKE_CASE : List[str] = False SCREAMING_SNAKE_CASE : str = model_class(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = model(self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Any = len(lowerCamelCase_ ) self.assertEqual(config.output_hidden_states , lowerCamelCase_ ) check_encoder_attentions_output(lowerCamelCase_ ) if self.is_encoder_decoder: SCREAMING_SNAKE_CASE : Union[str, Any] = model_class(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = model(self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) self.assertEqual(config.output_hidden_states , lowerCamelCase_ ) check_decoder_attentions_output(lowerCamelCase_ ) # Check that output attentions can also be changed via the config del inputs_dict["output_attentions"] SCREAMING_SNAKE_CASE : List[Any] = True SCREAMING_SNAKE_CASE : List[str] = model_class(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = model(self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) self.assertEqual(config.output_hidden_states , lowerCamelCase_ ) check_encoder_attentions_output(lowerCamelCase_ ) # Check attention is always last and order is fine SCREAMING_SNAKE_CASE : Optional[int] = True SCREAMING_SNAKE_CASE : str = True SCREAMING_SNAKE_CASE : Optional[Any] = model_class(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = model(self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1) , len(lowerCamelCase_ ) ) self.assertEqual(model.config.output_hidden_states , lowerCamelCase_ ) check_encoder_attentions_output(lowerCamelCase_ ) @require_tf class lowercase__( unittest.TestCase ): '''simple docstring''' @slow def __lowerCAmelCase ( self :int ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = TFConvBertModel.from_pretrained('''YituTech/conv-bert-base''' ) SCREAMING_SNAKE_CASE : Any = tf.constant([[0, 1, 2, 3, 4, 5]] ) SCREAMING_SNAKE_CASE : List[Any] = model(lowerCamelCase_ )[0] SCREAMING_SNAKE_CASE : Optional[Any] = [1, 6, 7_68] self.assertEqual(output.shape , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = tf.constant( [ [ [-0.0_3_4_7_5_4_9_3, -0.4_6_8_6_0_3_4, -0.3_0_6_3_8_8_3_2], [0.2_2_6_3_7_2_4_8, -0.2_6_9_8_8_6_4_6, -0.7_4_2_3_4_2_4], [0.1_0_3_2_4_8_6_8, -0.4_5_0_1_3_5_0_8, -0.5_8_2_8_0_7_8_4], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , lowerCamelCase_ , atol=1E-4 )
698
1
"""simple docstring""" from collections import deque from .hash_table import HashTable class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __init__( self :Dict , *lowerCamelCase_ :Tuple , **lowerCamelCase_ :Tuple ) -> int: '''simple docstring''' super().__init__(*lowerCamelCase_ , **lowerCamelCase_ ) def __lowerCAmelCase ( self :Any , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[int] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = deque([] ) if self.values[key] is None else self.values[key] self.values[key].appendleft(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = self.values[key] def __lowerCAmelCase ( self :Any ) -> int: '''simple docstring''' return ( sum(self.charge_factor - len(lowerCamelCase_ ) for slot in self.values ) / self.size_table * self.charge_factor ) def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Dict=None ) -> Optional[int]: '''simple docstring''' if not ( len(self.values[key] ) == self.charge_factor and self.values.count(lowerCamelCase_ ) == 0 ): return key return super()._collision_resolution(lowerCamelCase_ , lowerCamelCase_ )
698
"""simple docstring""" from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : Any = { "bert-base-uncased": "https://huggingface.co/bert-base-uncased/resolve/main/config.json", "bert-large-uncased": "https://huggingface.co/bert-large-uncased/resolve/main/config.json", "bert-base-cased": "https://huggingface.co/bert-base-cased/resolve/main/config.json", "bert-large-cased": "https://huggingface.co/bert-large-cased/resolve/main/config.json", "bert-base-multilingual-uncased": "https://huggingface.co/bert-base-multilingual-uncased/resolve/main/config.json", "bert-base-multilingual-cased": "https://huggingface.co/bert-base-multilingual-cased/resolve/main/config.json", "bert-base-chinese": "https://huggingface.co/bert-base-chinese/resolve/main/config.json", "bert-base-german-cased": "https://huggingface.co/bert-base-german-cased/resolve/main/config.json", "bert-large-uncased-whole-word-masking": ( "https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/config.json" ), "bert-large-cased-whole-word-masking": ( "https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/config.json" ), "bert-large-uncased-whole-word-masking-finetuned-squad": ( "https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/config.json" ), "bert-large-cased-whole-word-masking-finetuned-squad": ( "https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/config.json" ), "bert-base-cased-finetuned-mrpc": "https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/config.json", "bert-base-german-dbmdz-cased": "https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/config.json", "bert-base-german-dbmdz-uncased": "https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/config.json", "cl-tohoku/bert-base-japanese": "https://huggingface.co/cl-tohoku/bert-base-japanese/resolve/main/config.json", "cl-tohoku/bert-base-japanese-whole-word-masking": ( "https://huggingface.co/cl-tohoku/bert-base-japanese-whole-word-masking/resolve/main/config.json" ), "cl-tohoku/bert-base-japanese-char": ( "https://huggingface.co/cl-tohoku/bert-base-japanese-char/resolve/main/config.json" ), "cl-tohoku/bert-base-japanese-char-whole-word-masking": ( "https://huggingface.co/cl-tohoku/bert-base-japanese-char-whole-word-masking/resolve/main/config.json" ), "TurkuNLP/bert-base-finnish-cased-v1": ( "https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/config.json" ), "TurkuNLP/bert-base-finnish-uncased-v1": ( "https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/config.json" ), "wietsedv/bert-base-dutch-cased": "https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/config.json", # See all BERT models at https://huggingface.co/models?filter=bert } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """bert""" def __init__( self :Any , lowerCamelCase_ :List[Any]=3_05_22 , lowerCamelCase_ :List[str]=7_68 , lowerCamelCase_ :Tuple=12 , lowerCamelCase_ :List[Any]=12 , lowerCamelCase_ :int=30_72 , lowerCamelCase_ :Dict="gelu" , lowerCamelCase_ :List[Any]=0.1 , lowerCamelCase_ :int=0.1 , lowerCamelCase_ :int=5_12 , lowerCamelCase_ :Optional[int]=2 , lowerCamelCase_ :int=0.0_2 , lowerCamelCase_ :Optional[int]=1E-12 , lowerCamelCase_ :Optional[Any]=0 , lowerCamelCase_ :int="absolute" , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :Optional[Any]=None , **lowerCamelCase_ :List[Any] , ) -> List[str]: '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = vocab_size SCREAMING_SNAKE_CASE : Union[str, Any] = hidden_size SCREAMING_SNAKE_CASE : int = num_hidden_layers SCREAMING_SNAKE_CASE : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE : Dict = hidden_act SCREAMING_SNAKE_CASE : Union[str, Any] = intermediate_size SCREAMING_SNAKE_CASE : Dict = hidden_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : List[Any] = max_position_embeddings SCREAMING_SNAKE_CASE : int = type_vocab_size SCREAMING_SNAKE_CASE : List[str] = initializer_range SCREAMING_SNAKE_CASE : Union[str, Any] = layer_norm_eps SCREAMING_SNAKE_CASE : Optional[Any] = position_embedding_type SCREAMING_SNAKE_CASE : str = use_cache SCREAMING_SNAKE_CASE : Union[str, Any] = classifier_dropout class lowercase__( _UpperCAmelCase ): '''simple docstring''' @property def __lowerCAmelCase ( self :List[str] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": SCREAMING_SNAKE_CASE : Optional[Any] = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: SCREAMING_SNAKE_CASE : Optional[Any] = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis), ] )
698
1
"""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 lowerCamelCase__ : Tuple = "true" def __A ( a_ : List[str] , a_ : List[str]=82 , a_ : List[str]=16 )-> int: '''simple docstring''' set_seed(42 ) SCREAMING_SNAKE_CASE : Union[str, Any] = RegressionModel() SCREAMING_SNAKE_CASE : int = deepcopy(a_ ) SCREAMING_SNAKE_CASE : List[Any] = RegressionDataset(length=a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = DataLoader(a_ , batch_size=a_ ) model.to(accelerator.device ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Union[str, Any] = accelerator.prepare(a_ , a_ ) return model, ddp_model, dataloader def __A ( a_ : Accelerator , a_ : str=False )-> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = AutoTokenizer.from_pretrained('''hf-internal-testing/mrpc-bert-base-cased''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = load_dataset('''glue''' , '''mrpc''' , split='''validation''' ) def tokenize_function(a_ : Optional[Any] ): SCREAMING_SNAKE_CASE : int = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=a_ , max_length=a_ ) return outputs with accelerator.main_process_first(): SCREAMING_SNAKE_CASE : Tuple = dataset.map( a_ , batched=a_ , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , ) SCREAMING_SNAKE_CASE : Optional[int] = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(a_ : Optional[Any] ): if use_longest: return tokenizer.pad(a_ , padding='''longest''' , return_tensors='''pt''' ) return tokenizer.pad(a_ , padding='''max_length''' , max_length=1_28 , return_tensors='''pt''' ) return DataLoader(a_ , shuffle=a_ , collate_fn=a_ , batch_size=16 ) def __A ( a_ : Union[str, Any] , a_ : Optional[int] )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = Accelerator(dispatch_batches=a_ , split_batches=a_ ) SCREAMING_SNAKE_CASE : str = get_dataloader(a_ , not dispatch_batches ) SCREAMING_SNAKE_CASE : Optional[Any] = AutoModelForSequenceClassification.from_pretrained( '''hf-internal-testing/mrpc-bert-base-cased''' , return_dict=a_ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = accelerator.prepare(a_ , a_ ) return {"ddp": [ddp_model, ddp_dataloader, "cuda:0"], "no": [model, dataloader, accelerator.device]}, accelerator def __A ( a_ : Union[str, Any] , a_ : Any , a_ : Dict )-> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = [] for batch in dataloader: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[Any] = batch.values() with torch.no_grad(): SCREAMING_SNAKE_CASE : Any = model(a_ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = accelerator.gather_for_metrics((logit, target) ) logits_and_targets.append((logit, target) ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = [], [] for logit, targ in logits_and_targets: logits.append(a_ ) targs.append(a_ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = torch.cat(a_ ), torch.cat(a_ ) return logits, targs def __A ( a_ : Accelerator , a_ : Tuple=82 , a_ : Optional[int]=False , a_ : Union[str, Any]=False , a_ : Tuple=16 )-> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[Any] = get_basic_setup(a_ , a_ , a_ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = generate_predictions(a_ , a_ , a_ ) assert ( len(a_ ) == num_samples ), F"Unexpected number of inputs:\n Expected: {num_samples}\n Actual: {len(a_ )}" def __A ( a_ : bool = False , a_ : bool = False )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = evaluate.load('''glue''' , '''mrpc''' ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Union[str, Any] = get_mrpc_setup(a_ , a_ ) # First do baseline SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = setup['''no'''] model.to(a_ ) model.eval() for batch in dataloader: batch.to(a_ ) with torch.inference_mode(): SCREAMING_SNAKE_CASE : Any = model(**a_ ) SCREAMING_SNAKE_CASE : Optional[int] = outputs.logits.argmax(dim=-1 ) metric.add_batch(predictions=a_ , references=batch['''labels'''] ) SCREAMING_SNAKE_CASE : Optional[Any] = metric.compute() # Then do distributed SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = setup['''ddp'''] model.eval() for batch in dataloader: with torch.inference_mode(): SCREAMING_SNAKE_CASE : Dict = model(**a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = outputs.logits.argmax(dim=-1 ) SCREAMING_SNAKE_CASE : Dict = batch['''labels'''] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = accelerator.gather_for_metrics((preds, references) ) metric.add_batch(predictions=a_ , references=a_ ) SCREAMING_SNAKE_CASE : 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 __A ( )-> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = Accelerator(split_batches=a_ , dispatch_batches=a_ ) 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(a_ , a_ ) 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]: SCREAMING_SNAKE_CASE : List[str] = Accelerator(split_batches=a_ , dispatch_batches=a_ ) if accelerator.is_local_main_process: print(F"With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`, length=99" ) test_torch_metrics(a_ , 99 ) accelerator.state._reset_state() if accelerator.is_local_main_process: print('''**Test last batch is not dropped when perfectly divisible**''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = Accelerator() test_torch_metrics(a_ , 5_12 ) accelerator.state._reset_state() def __A ( a_ : str )-> Optional[int]: '''simple docstring''' main() if __name__ == "__main__": main()
698
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : int = logging.get_logger(__name__) lowerCamelCase__ : str = { "studio-ousia/luke-base": "https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json", "studio-ousia/luke-large": "https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """luke""" def __init__( self :List[Any] , lowerCamelCase_ :Optional[int]=5_02_67 , lowerCamelCase_ :List[Any]=50_00_00 , lowerCamelCase_ :str=7_68 , lowerCamelCase_ :Optional[Any]=2_56 , lowerCamelCase_ :List[Any]=12 , lowerCamelCase_ :List[Any]=12 , lowerCamelCase_ :Any=30_72 , lowerCamelCase_ :Optional[int]="gelu" , lowerCamelCase_ :Dict=0.1 , lowerCamelCase_ :Any=0.1 , lowerCamelCase_ :str=5_12 , lowerCamelCase_ :Tuple=2 , lowerCamelCase_ :Optional[Any]=0.0_2 , lowerCamelCase_ :Optional[int]=1E-12 , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :int=None , lowerCamelCase_ :Dict=1 , lowerCamelCase_ :str=0 , lowerCamelCase_ :int=2 , **lowerCamelCase_ :List[str] , ) -> int: '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = vocab_size SCREAMING_SNAKE_CASE : Optional[Any] = entity_vocab_size SCREAMING_SNAKE_CASE : int = hidden_size SCREAMING_SNAKE_CASE : Dict = entity_emb_size SCREAMING_SNAKE_CASE : Optional[int] = num_hidden_layers SCREAMING_SNAKE_CASE : int = num_attention_heads SCREAMING_SNAKE_CASE : str = hidden_act SCREAMING_SNAKE_CASE : str = intermediate_size SCREAMING_SNAKE_CASE : Tuple = hidden_dropout_prob SCREAMING_SNAKE_CASE : str = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = max_position_embeddings SCREAMING_SNAKE_CASE : Dict = type_vocab_size SCREAMING_SNAKE_CASE : List[Any] = initializer_range SCREAMING_SNAKE_CASE : Optional[int] = layer_norm_eps SCREAMING_SNAKE_CASE : Optional[Any] = use_entity_aware_attention SCREAMING_SNAKE_CASE : str = classifier_dropout
698
1
"""simple docstring""" from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import KandinskyPipeline, KandinskyPriorPipeline else: from .pipeline_kandinsky import KandinskyPipeline from .pipeline_kandinsky_imgaimg import KandinskyImgaImgPipeline from .pipeline_kandinsky_inpaint import KandinskyInpaintPipeline from .pipeline_kandinsky_prior import KandinskyPriorPipeline, KandinskyPriorPipelineOutput from .text_encoder import MultilingualCLIP
698
"""simple docstring""" # using dfs for finding eulerian path traversal def __A ( a_ : Dict , a_ : int , a_ : str , a_ : Optional[Any]=None )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = (path or []) + [u] for v in graph[u]: if visited_edge[u][v] is False: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = True, True SCREAMING_SNAKE_CASE : List[str] = dfs(a_ , a_ , a_ , a_ ) return path def __A ( a_ : List[str] , a_ : Any )-> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = 0 SCREAMING_SNAKE_CASE : str = -1 for i in range(a_ ): if i not in graph.keys(): continue if len(graph[i] ) % 2 == 1: odd_degree_nodes += 1 SCREAMING_SNAKE_CASE : Tuple = i if odd_degree_nodes == 0: return 1, odd_node if odd_degree_nodes == 2: return 2, odd_node return 3, odd_node def __A ( a_ : Any , a_ : int )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = [[False for _ in range(max_node + 1 )] for _ in range(max_node + 1 )] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = check_circuit_or_path(a_ , a_ ) if check == 3: print('''graph is not Eulerian''' ) print('''no path''' ) return SCREAMING_SNAKE_CASE : Tuple = 1 if check == 2: SCREAMING_SNAKE_CASE : Optional[int] = odd_node print('''graph has a Euler path''' ) if check == 1: print('''graph has a Euler cycle''' ) SCREAMING_SNAKE_CASE : Optional[int] = dfs(a_ , a_ , a_ ) print(a_ ) def __A ( )-> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = {1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]} SCREAMING_SNAKE_CASE : str = {1: [2, 3, 4, 5], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [1, 4]} SCREAMING_SNAKE_CASE : str = {1: [2, 3, 4], 2: [1, 3, 4], 3: [1, 2], 4: [1, 2, 5], 5: [4]} SCREAMING_SNAKE_CASE : int = {1: [2, 3], 2: [1, 3], 3: [1, 2]} SCREAMING_SNAKE_CASE : int = { 1: [], 2: [] # all degree is zero } SCREAMING_SNAKE_CASE : List[str] = 10 check_euler(a_ , a_ ) check_euler(a_ , a_ ) check_euler(a_ , a_ ) check_euler(a_ , a_ ) check_euler(a_ , a_ ) if __name__ == "__main__": main()
698
1
"""simple docstring""" import inspect import os import unittest import torch import accelerate from accelerate import debug_launcher from accelerate.test_utils import ( execute_subprocess_async, require_cpu, require_huggingface_suite, require_multi_gpu, require_single_gpu, ) from accelerate.utils import patch_environment @require_huggingface_suite class lowercase__( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self :Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : int = inspect.getfile(accelerate.test_utils ) SCREAMING_SNAKE_CASE : Optional[int] = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''external_deps''', '''test_metrics.py'''] ) from accelerate.test_utils.scripts.external_deps import test_metrics # noqa: F401 SCREAMING_SNAKE_CASE : Dict = test_metrics @require_cpu def __lowerCAmelCase ( self :Optional[int] ) -> str: '''simple docstring''' debug_launcher(self.test_metrics.main , num_processes=1 ) @require_cpu def __lowerCAmelCase ( self :Tuple ) -> Dict: '''simple docstring''' debug_launcher(self.test_metrics.main ) @require_single_gpu def __lowerCAmelCase ( self :Union[str, Any] ) -> List[Any]: '''simple docstring''' self.test_metrics.main() @require_multi_gpu def __lowerCAmelCase ( self :Optional[Any] ) -> List[Any]: '''simple docstring''' print(f"Found {torch.cuda.device_count()} devices." ) SCREAMING_SNAKE_CASE : Any = ['''torchrun''', f"--nproc_per_node={torch.cuda.device_count()}", self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(lowerCamelCase_ , env=os.environ.copy() )
698
"""simple docstring""" import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, BatchEncoding, MBartaaTokenizer, MBartaaTokenizerFast, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, slow, ) from ...test_tokenization_common import TokenizerTesterMixin lowerCamelCase__ : str = get_tests_dir("fixtures/test_sentencepiece.model") if is_torch_available(): from transformers.models.mbart.modeling_mbart import shift_tokens_right lowerCamelCase__ : List[str] = 250004 lowerCamelCase__ : str = 250020 @require_sentencepiece @require_tokenizers class lowercase__( _UpperCAmelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase = MBartaaTokenizer UpperCamelCase = MBartaaTokenizerFast UpperCamelCase = True UpperCamelCase = True def __lowerCAmelCase ( self :Union[str, Any] ) -> str: '''simple docstring''' super().setUp() # We have a SentencePiece fixture for testing SCREAMING_SNAKE_CASE : Optional[int] = MBartaaTokenizer(lowerCamelCase_ , src_lang='''en_XX''' , tgt_lang='''ro_RO''' , keep_accents=lowerCamelCase_ ) tokenizer.save_pretrained(self.tmpdirname ) def __lowerCAmelCase ( self :Union[str, Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = '''<s>''' SCREAMING_SNAKE_CASE : Union[str, Any] = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCamelCase_ ) , lowerCamelCase_ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCamelCase_ ) , lowerCamelCase_ ) def __lowerCAmelCase ( self :str ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<s>''' ) self.assertEqual(vocab_keys[1] , '''<pad>''' ) self.assertEqual(vocab_keys[-1] , '''<mask>''' ) self.assertEqual(len(lowerCamelCase_ ) , 10_54 ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Tuple: '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 10_54 ) def __lowerCAmelCase ( self :Tuple ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = MBartaaTokenizer(lowerCamelCase_ , src_lang='''en_XX''' , tgt_lang='''ro_RO''' , keep_accents=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(lowerCamelCase_ , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(lowerCamelCase_ ) , [value + tokenizer.fairseq_offset for value in [2_85, 46, 10, 1_70, 3_82]] , ) SCREAMING_SNAKE_CASE : Tuple = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( lowerCamelCase_ , [SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.'''] , ) SCREAMING_SNAKE_CASE : int = tokenizer.convert_tokens_to_ids(lowerCamelCase_ ) self.assertListEqual( lowerCamelCase_ , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 6_02, 3_47, 3_47, 3_47, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) SCREAMING_SNAKE_CASE : Union[str, Any] = tokenizer.convert_ids_to_tokens(lowerCamelCase_ ) self.assertListEqual( lowerCamelCase_ , [SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''<unk>''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''<unk>''', '''.'''] , ) @slow def __lowerCAmelCase ( self :Optional[Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = {'''input_ids''': [[25_00_04, 1_10_62, 8_27_72, 7, 15, 8_27_72, 5_38, 5_15_29, 2_37, 1_71_98, 12_90, 2_06, 9, 21_51_75, 13_14, 1_36, 1_71_98, 12_90, 2_06, 9, 5_63_59, 42, 12_20_09, 9, 1_64_66, 16, 8_73_44, 45_37, 9, 47_17, 7_83_81, 6, 15_99_58, 7, 15, 2_44_80, 6_18, 4, 5_27, 2_26_93, 54_28, 4, 27_77, 2_44_80, 98_74, 4, 4_35_23, 5_94, 4, 8_03, 1_83_92, 3_31_89, 18, 4, 4_35_23, 2_44_47, 1_23_99, 1_00, 2_49_55, 8_36_58, 96_26, 14_40_57, 15, 8_39, 2_23_35, 16, 1_36, 2_49_55, 8_36_58, 8_34_79, 15, 3_91_02, 7_24, 16, 6_78, 6_45, 27_89, 13_28, 45_89, 42, 12_20_09, 11_57_74, 23, 8_05, 13_28, 4_68_76, 7, 1_36, 5_38_94, 19_40, 4_22_27, 4_11_59, 1_77_21, 8_23, 4_25, 4, 2_75_12, 9_87_22, 2_06, 1_36, 55_31, 49_70, 9_19, 1_73_36, 5, 2], [25_00_04, 2_00_80, 6_18, 83, 8_27_75, 47, 4_79, 9, 15_17, 73, 5_38_94, 3_33, 8_05_81, 11_01_17, 1_88_11, 52_56, 12_95, 51, 15_25_26, 2_97, 79_86, 3_90, 12_44_16, 5_38, 3_54_31, 2_14, 98, 1_50_44, 2_57_37, 1_36, 71_08, 4_37_01, 23, 7_56, 13_53_55, 7, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [25_00_04, 5_81, 6_37_73, 11_94_55, 6, 14_77_97, 8_82_03, 7, 6_45, 70, 21, 32_85, 1_02_69, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=lowerCamelCase_ , model_name='''facebook/mbart-large-50''' , revision='''d3913889c59cd5c9e456b269c376325eabad57e2''' , ) def __lowerCAmelCase ( self :Optional[int] ) -> List[Any]: '''simple docstring''' if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return SCREAMING_SNAKE_CASE : str = (self.rust_tokenizer_class, '''hf-internal-testing/tiny-random-mbart50''', {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})" ): SCREAMING_SNAKE_CASE : Tuple = self.rust_tokenizer_class.from_pretrained(lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = self.tokenizer_class.from_pretrained(lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = tempfile.mkdtemp() SCREAMING_SNAKE_CASE : Dict = tokenizer_r.save_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = tokenizer_p.save_pretrained(lowerCamelCase_ ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any('''tokenizer.json''' in f for f in tokenizer_r_files ) ) SCREAMING_SNAKE_CASE : Any = tuple(f for f in tokenizer_r_files if '''tokenizer.json''' not in f ) self.assertSequenceEqual(lowerCamelCase_ , lowerCamelCase_ ) # Checks everything loads correctly in the same way SCREAMING_SNAKE_CASE : int = tokenizer_r.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = tokenizer_p.from_pretrained(lowerCamelCase_ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(lowerCamelCase_ , lowerCamelCase_ ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(lowerCamelCase_ ) # Save tokenizer rust, legacy_format=True SCREAMING_SNAKE_CASE : Optional[int] = tempfile.mkdtemp() SCREAMING_SNAKE_CASE : List[Any] = tokenizer_r.save_pretrained(lowerCamelCase_ , legacy_format=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = tokenizer_p.save_pretrained(lowerCamelCase_ ) # Checks it save with the same files self.assertSequenceEqual(lowerCamelCase_ , lowerCamelCase_ ) # Checks everything loads correctly in the same way SCREAMING_SNAKE_CASE : List[Any] = tokenizer_r.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = tokenizer_p.from_pretrained(lowerCamelCase_ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(lowerCamelCase_ , lowerCamelCase_ ) ) shutil.rmtree(lowerCamelCase_ ) # Save tokenizer rust, legacy_format=False SCREAMING_SNAKE_CASE : Union[str, Any] = tempfile.mkdtemp() SCREAMING_SNAKE_CASE : Optional[int] = tokenizer_r.save_pretrained(lowerCamelCase_ , legacy_format=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = tokenizer_p.save_pretrained(lowerCamelCase_ ) # Checks it saved the tokenizer.json file self.assertTrue(any('''tokenizer.json''' in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way SCREAMING_SNAKE_CASE : Tuple = tokenizer_r.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = tokenizer_p.from_pretrained(lowerCamelCase_ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(lowerCamelCase_ , lowerCamelCase_ ) ) shutil.rmtree(lowerCamelCase_ ) @require_torch @require_sentencepiece @require_tokenizers class lowercase__( unittest.TestCase ): '''simple docstring''' UpperCamelCase = """facebook/mbart-large-50-one-to-many-mmt""" UpperCamelCase = [ """ UN Chief Says There Is No Military Solution in Syria""", """ Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for Syria is that \"there is no military solution\" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.""", ] UpperCamelCase = [ """Şeful ONU declară că nu există o soluţie militară în Siria""", """Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei""" """ pentru Siria este că \"nu există o soluţie militară\" la conflictul de aproape cinci ani şi că noi arme nu vor""" """ face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.""", ] UpperCamelCase = [EN_CODE, 82_74, 12_78_73, 2_59_16, 7, 86_22, 20_71, 4_38, 6_74_85, 53, 18_78_95, 23, 5_17_12, 2] @classmethod def __lowerCAmelCase ( cls :Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : MBartaaTokenizer = MBartaaTokenizer.from_pretrained( cls.checkpoint_name , src_lang='''en_XX''' , tgt_lang='''ro_RO''' ) SCREAMING_SNAKE_CASE : Dict = 1 return cls def __lowerCAmelCase ( self :Any ) -> int: '''simple docstring''' self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''ar_AR'''] , 25_00_01 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''en_EN'''] , 25_00_04 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''ro_RO'''] , 25_00_20 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''mr_IN'''] , 25_00_38 ) def __lowerCAmelCase ( self :List[str] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , lowerCamelCase_ ) def __lowerCAmelCase ( self :str ) -> Optional[Any]: '''simple docstring''' self.assertIn(lowerCamelCase_ , self.tokenizer.all_special_ids ) SCREAMING_SNAKE_CASE : int = [RO_CODE, 8_84, 90_19, 96, 9, 9_16, 8_67_92, 36, 1_87_43, 1_55_96, 5, 2] SCREAMING_SNAKE_CASE : Optional[int] = self.tokenizer.decode(lowerCamelCase_ , skip_special_tokens=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=lowerCamelCase_ ) self.assertEqual(lowerCamelCase_ , lowerCamelCase_ ) self.assertNotIn(self.tokenizer.eos_token , lowerCamelCase_ ) def __lowerCAmelCase ( self :Tuple ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = ['''this is gunna be a long sentence ''' * 20] assert isinstance(src_text[0] , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = 10 SCREAMING_SNAKE_CASE : Union[str, Any] = self.tokenizer(lowerCamelCase_ , max_length=lowerCamelCase_ , truncation=lowerCamelCase_ ).input_ids[0] self.assertEqual(ids[0] , lowerCamelCase_ ) self.assertEqual(ids[-1] , 2 ) self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) def __lowerCAmelCase ( self :str ) -> List[str]: '''simple docstring''' self.assertListEqual(self.tokenizer.convert_tokens_to_ids(['''<mask>''', '''ar_AR'''] ) , [25_00_53, 25_00_01] ) def __lowerCAmelCase ( self :List[str] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = tempfile.mkdtemp() SCREAMING_SNAKE_CASE : Dict = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = MBartaaTokenizer.from_pretrained(lowerCamelCase_ ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , lowerCamelCase_ ) @require_torch def __lowerCAmelCase ( self :str ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.tokenizer(self.src_text , text_target=self.tgt_text , padding=lowerCamelCase_ , return_tensors='''pt''' ) SCREAMING_SNAKE_CASE : Dict = shift_tokens_right(batch['''labels'''] , self.tokenizer.pad_token_id ) # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 assert batch.input_ids[1][0] == EN_CODE assert batch.input_ids[1][-1] == 2 assert batch.labels[1][0] == RO_CODE assert batch.labels[1][-1] == 2 assert batch.decoder_input_ids[1][:2].tolist() == [2, RO_CODE] @require_torch def __lowerCAmelCase ( self :Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=lowerCamelCase_ , truncation=lowerCamelCase_ , max_length=len(self.expected_src_tokens ) , return_tensors='''pt''' , ) SCREAMING_SNAKE_CASE : List[Any] = shift_tokens_right(batch['''labels'''] , self.tokenizer.pad_token_id ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) self.assertEqual((2, 14) , batch.input_ids.shape ) self.assertEqual((2, 14) , batch.attention_mask.shape ) SCREAMING_SNAKE_CASE : List[str] = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , lowerCamelCase_ ) self.assertEqual(2 , batch.decoder_input_ids[0, 0] ) # decoder_start_token_id # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [EN_CODE] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.tokenizer(self.src_text , padding=lowerCamelCase_ , truncation=lowerCamelCase_ , max_length=3 , return_tensors='''pt''' ) SCREAMING_SNAKE_CASE : Tuple = self.tokenizer( text_target=self.tgt_text , padding=lowerCamelCase_ , truncation=lowerCamelCase_ , max_length=10 , return_tensors='''pt''' ) SCREAMING_SNAKE_CASE : List[Any] = targets['''input_ids'''] SCREAMING_SNAKE_CASE : Optional[int] = shift_tokens_right(lowerCamelCase_ , self.tokenizer.pad_token_id ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.decoder_input_ids.shape[1] , 10 ) @require_torch def __lowerCAmelCase ( self :Any ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.tokenizer._build_translation_inputs( '''A test''' , return_tensors='''pt''' , src_lang='''en_XX''' , tgt_lang='''ar_AR''' ) self.assertEqual( nested_simplify(lowerCamelCase_ ) , { # en_XX, A, test, EOS '''input_ids''': [[25_00_04, 62, 30_34, 2]], '''attention_mask''': [[1, 1, 1, 1]], # ar_AR '''forced_bos_token_id''': 25_00_01, } , )
698
1
"""simple docstring""" lowerCamelCase__ : dict[str, float] = { "joule": 1.0, "kilojoule": 1000, "megajoule": 1000000, "gigajoule": 1000000000, "wattsecond": 1.0, "watthour": 3600, "kilowatthour": 3600000, "newtonmeter": 1.0, "calorie_nutr": 4186.8, "kilocalorie_nutr": 4186800.00, "electronvolt": 1.6_0217_6634e-19, "britishthermalunit_it": 1055.05585, "footpound": 1.3_5_5_8_1_8, } def __A ( a_ : str , a_ : str , a_ : float )-> float: '''simple docstring''' if to_type not in ENERGY_CONVERSION or from_type not in ENERGY_CONVERSION: SCREAMING_SNAKE_CASE : Optional[Any] = ( F"Incorrect 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\n" F"Valid values are: {', '.join(a_ )}" ) raise ValueError(a_ ) return value * ENERGY_CONVERSION[from_type] / ENERGY_CONVERSION[to_type] if __name__ == "__main__": import doctest doctest.testmod()
698
"""simple docstring""" from unittest import TestCase from datasets import Sequence, Value from datasets.arrow_dataset import Dataset class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __lowerCAmelCase ( self :Union[str, Any] ) -> str: '''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 __lowerCAmelCase ( self :Dict ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = {'''col_1''': [3, 2, 1, 0], '''col_2''': ['''a''', '''b''', '''c''', '''d''']} return Dataset.from_dict(lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[int] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self._create_example_records() SCREAMING_SNAKE_CASE : List[Any] = Dataset.from_list(lowerCamelCase_ ) self.assertListEqual(dset.column_names , ['''col_1''', '''col_2'''] ) for i, r in enumerate(lowerCamelCase_ ): self.assertDictEqual(lowerCamelCase_ , example_records[i] ) def __lowerCAmelCase ( self :Dict ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : str = self._create_example_records() SCREAMING_SNAKE_CASE : Optional[int] = Dataset.from_list(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = 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 __lowerCAmelCase ( self :List[str] ) -> Dict: # checks what happens with missing columns '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = [{'''col_1''': 1}, {'''col_2''': '''x'''}] SCREAMING_SNAKE_CASE : Optional[int] = Dataset.from_list(lowerCamelCase_ ) self.assertDictEqual(dset[0] , {'''col_1''': 1} ) self.assertDictEqual(dset[1] , {'''col_1''': None} ) # NB: first record is used for columns def __lowerCAmelCase ( self :Tuple ) -> Optional[Any]: # checks if the type can be inferred from the second record '''simple docstring''' SCREAMING_SNAKE_CASE : int = [{'''col_1''': []}, {'''col_1''': [1, 2]}] SCREAMING_SNAKE_CASE : List[str] = Dataset.from_list(lowerCamelCase_ ) self.assertEqual(dset.info.features['''col_1'''] , Sequence(Value('''int64''' ) ) ) def __lowerCAmelCase ( self :Any ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = Dataset.from_list([] ) self.assertEqual(len(lowerCamelCase_ ) , 0 ) self.assertListEqual(dset.column_names , [] )
698
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 lowerCamelCase__ : Union[str, Any] = "CompVis/stable-diffusion-v1-1" lowerCamelCase__ : Optional[Any] = "CompVis/stable-diffusion-v1-2" lowerCamelCase__ : Dict = "CompVis/stable-diffusion-v1-3" lowerCamelCase__ : List[str] = "CompVis/stable-diffusion-v1-4" class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __init__( self :Any , lowerCamelCase_ :AutoencoderKL , lowerCamelCase_ :CLIPTextModel , lowerCamelCase_ :CLIPTokenizer , lowerCamelCase_ :UNetaDConditionModel , lowerCamelCase_ :Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , lowerCamelCase_ :StableDiffusionSafetyChecker , lowerCamelCase_ :CLIPImageProcessor , lowerCamelCase_ :bool = True , ) -> List[str]: '''simple docstring''' super()._init_() SCREAMING_SNAKE_CASE : Tuple = StableDiffusionPipeline.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = StableDiffusionPipeline.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = StableDiffusionPipeline.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = StableDiffusionPipeline( vae=lowerCamelCase_ , text_encoder=lowerCamelCase_ , tokenizer=lowerCamelCase_ , unet=lowerCamelCase_ , scheduler=lowerCamelCase_ , safety_checker=lowerCamelCase_ , feature_extractor=lowerCamelCase_ , requires_safety_checker=lowerCamelCase_ , ) self.register_modules(pipelinea=self.pipea , pipelinea=self.pipea , pipelinea=self.pipea , pipelinea=self.pipea ) @property def __lowerCAmelCase ( self :Dict ) -> Dict[str, Any]: '''simple docstring''' return {k: getattr(self , lowerCamelCase_ ) for k in self.config.keys() if not k.startswith('''_''' )} def __lowerCAmelCase ( self :int , lowerCamelCase_ :Optional[Union[str, int]] = "auto" ) -> Tuple: '''simple docstring''' if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory SCREAMING_SNAKE_CASE : str = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(lowerCamelCase_ ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Dict: '''simple docstring''' self.enable_attention_slicing(lowerCamelCase_ ) @torch.no_grad() def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :List[str] , ) -> Tuple: '''simple docstring''' return self.pipea( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) @torch.no_grad() def __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :Tuple , ) -> Optional[Any]: '''simple docstring''' return self.pipea( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) @torch.no_grad() def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :Dict , ) -> List[str]: '''simple docstring''' return self.pipea( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) @torch.no_grad() def __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :List[Any] , ) -> Optional[Any]: '''simple docstring''' return self.pipea( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) @torch.no_grad() def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :Optional[Any] , ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = '''cuda''' if torch.cuda.is_available() else '''cpu''' self.to(lowerCamelCase_ ) # 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 SCREAMING_SNAKE_CASE : str = self.textaimg_sda_a( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) # Get first result from Stable Diffusion Checkpoint v1.2 SCREAMING_SNAKE_CASE : Optional[Any] = self.textaimg_sda_a( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) # Get first result from Stable Diffusion Checkpoint v1.3 SCREAMING_SNAKE_CASE : Tuple = self.textaimg_sda_a( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) # Get first result from Stable Diffusion Checkpoint v1.4 SCREAMING_SNAKE_CASE : Union[str, Any] = self.textaimg_sda_a( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) # 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]] )
698
"""simple docstring""" from __future__ import annotations import math from collections.abc import Callable def __A ( a_ : Callable[[int | float], int | float] , a_ : int | float , a_ : int | float , a_ : int = 1_00 , )-> float: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = x_start SCREAMING_SNAKE_CASE : Union[str, Any] = fnc(a_ ) SCREAMING_SNAKE_CASE : Optional[int] = 0.0 for _ in range(a_ ): # Approximates curve as a sequence of linear lines and sums their length SCREAMING_SNAKE_CASE : int = (x_end - x_start) / steps + xa SCREAMING_SNAKE_CASE : Optional[int] = fnc(a_ ) length += math.hypot(xa - xa , fxa - fxa ) # Increment step SCREAMING_SNAKE_CASE : str = xa SCREAMING_SNAKE_CASE : Any = fxa return length if __name__ == "__main__": def __A ( a_ : Optional[Any] )-> List[Any]: '''simple docstring''' return math.sin(10 * x ) print("f(x) = sin(10 * x)") print("The length of the curve from x = -10 to x = 10 is:") lowerCamelCase__ : str = 10 while i <= 100000: print(f'''With {i} steps: {line_length(f, -10, 10, i)}''') i *= 10
698
1
"""simple docstring""" import torch from torch import nn from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin class lowercase__( _UpperCAmelCase , _UpperCAmelCase ): '''simple docstring''' @register_to_config def __init__( self :Union[str, Any] , *, lowerCamelCase_ :int = 4 , lowerCamelCase_ :int = 7_68 , lowerCamelCase_ :int , lowerCamelCase_ :Union[str, Any] , ) -> Union[str, Any]: '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE : Any = nn.Parameter(torch.zeros(lowerCamelCase_ ) ) # parameters for additional clip time embeddings SCREAMING_SNAKE_CASE : Tuple = nn.Linear(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = nn.Linear(lowerCamelCase_ , lowerCamelCase_ ) # parameters for encoder hidden states SCREAMING_SNAKE_CASE : Any = clip_extra_context_tokens SCREAMING_SNAKE_CASE : str = nn.Linear( lowerCamelCase_ , self.clip_extra_context_tokens * cross_attention_dim ) SCREAMING_SNAKE_CASE : Any = nn.Linear(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = nn.LayerNorm(lowerCamelCase_ ) def __lowerCAmelCase ( self :List[Any] , *, lowerCamelCase_ :Tuple , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Tuple ) -> Optional[Any]: '''simple docstring''' if do_classifier_free_guidance: # Add the classifier free guidance embeddings to the image embeddings SCREAMING_SNAKE_CASE : Union[str, Any] = image_embeddings.shape[0] SCREAMING_SNAKE_CASE : Optional[int] = self.learned_classifier_free_guidance_embeddings.unsqueeze(0 ) SCREAMING_SNAKE_CASE : Any = classifier_free_guidance_embeddings.expand( lowerCamelCase_ , -1 ) SCREAMING_SNAKE_CASE : List[Any] = torch.cat([classifier_free_guidance_embeddings, image_embeddings] , dim=0 ) # The image embeddings batch size and the text embeddings batch size are equal assert image_embeddings.shape[0] == prompt_embeds.shape[0] SCREAMING_SNAKE_CASE : List[str] = prompt_embeds.shape[0] # "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and # adding CLIP embeddings to the existing timestep embedding, ... SCREAMING_SNAKE_CASE : str = self.embedding_proj(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = self.clip_image_embeddings_project_to_time_embeddings(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = time_projected_image_embeddings + time_projected_prompt_embeds # ... and by projecting CLIP embeddings into four # extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder" SCREAMING_SNAKE_CASE : Optional[Any] = self.clip_extra_context_tokens_proj(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = clip_extra_context_tokens.reshape(lowerCamelCase_ , -1 , self.clip_extra_context_tokens ) SCREAMING_SNAKE_CASE : int = clip_extra_context_tokens.permute(0 , 2 , 1 ) SCREAMING_SNAKE_CASE : Tuple = self.encoder_hidden_states_proj(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = self.text_encoder_hidden_states_norm(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = torch.cat([clip_extra_context_tokens, text_encoder_hidden_states] , dim=1 ) return text_encoder_hidden_states, additive_clip_time_embeddings
698
"""simple docstring""" # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os from accelerate.test_utils import execute_subprocess_async def __A ( a_ : int=None )-> Tuple: '''simple docstring''' if subparsers is not None: SCREAMING_SNAKE_CASE : List[str] = subparsers.add_parser('''test''' ) else: SCREAMING_SNAKE_CASE : int = argparse.ArgumentParser('''Accelerate test command''' ) parser.add_argument( '''--config_file''' , default=a_ , help=( '''The path to use to store the config file. Will default to a file named default_config.yaml in the cache ''' '''location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have ''' '''such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed ''' '''with \'huggingface\'.''' ) , ) if subparsers is not None: parser.set_defaults(func=a_ ) return parser def __A ( a_ : Any )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = os.path.sep.join(__file__.split(os.path.sep )[:-2] + ['''test_utils''', '''scripts''', '''test_script.py'''] ) if args.config_file is None: SCREAMING_SNAKE_CASE : Tuple = script_name else: SCREAMING_SNAKE_CASE : Optional[Any] = F"--config_file={args.config_file} {script_name}" SCREAMING_SNAKE_CASE : str = ['''accelerate-launch'''] + test_args.split() SCREAMING_SNAKE_CASE : List[str] = execute_subprocess_async(a_ , env=os.environ.copy() ) if result.returncode == 0: print('''Test is a success! You are ready for your distributed training!''' ) def __A ( )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : str = test_command_parser() SCREAMING_SNAKE_CASE : Optional[Any] = parser.parse_args() test_command(a_ ) if __name__ == "__main__": main()
698
1
"""simple docstring""" import argparse import json import os from pathlib import Path import requests import torch from transformers import JukeboxConfig, JukeboxModel from transformers.utils import logging logging.set_verbosity_info() lowerCamelCase__ : Optional[Any] = logging.get_logger(__name__) lowerCamelCase__ : List[str] = "https://openaipublic.azureedge.net/jukebox/models/" lowerCamelCase__ : Tuple = { "jukebox-1b-lyrics": [ "5b/vqvae.pth.tar", "5b/prior_level_0.pth.tar", "5b/prior_level_1.pth.tar", "1b_lyrics/prior_level_2.pth.tar", ], "jukebox-5b-lyrics": [ "5b/vqvae.pth.tar", "5b/prior_level_0.pth.tar", "5b/prior_level_1.pth.tar", "5b_lyrics/prior_level_2.pth.tar", ], } def __A ( a_ : List[str] )-> Union[str, Any]: '''simple docstring''' if key.endswith('''.model.1.bias''' ) and len(key.split('''.''' ) ) > 10: SCREAMING_SNAKE_CASE : Optional[Any] = key.replace('''.model.1.bias''' , '''.conv1d_1.bias''' ) elif key.endswith('''.model.1.weight''' ) and len(key.split('''.''' ) ) > 10: SCREAMING_SNAKE_CASE : Union[str, Any] = key.replace('''.model.1.weight''' , '''.conv1d_1.weight''' ) elif key.endswith('''.model.3.bias''' ) and len(key.split('''.''' ) ) > 10: SCREAMING_SNAKE_CASE : Dict = key.replace('''.model.3.bias''' , '''.conv1d_2.bias''' ) elif key.endswith('''.model.3.weight''' ) and len(key.split('''.''' ) ) > 10: SCREAMING_SNAKE_CASE : Any = key.replace('''.model.3.weight''' , '''.conv1d_2.weight''' ) if "conditioner_blocks.0." in key: SCREAMING_SNAKE_CASE : Optional[int] = key.replace('''conditioner_blocks.0''' , '''conditioner_blocks''' ) if "prime_prior" in key: SCREAMING_SNAKE_CASE : Dict = key.replace('''prime_prior''' , '''encoder''' ) if ".emb." in key and "total" not in key and "absolute" not in key and "relative" not in key: SCREAMING_SNAKE_CASE : Dict = key.replace('''.emb.''' , '''.''' ) if key.endswith('''k''' ): # replace vqvae.X.k with vqvae.X.codebook return key.replace('''.k''' , '''.codebook''' ) if "y_emb." in key: return key.replace('''y_emb.''' , '''metadata_embedding.''' ) if "x_emb.emb." in key: SCREAMING_SNAKE_CASE : str = key.replace('''0.x_emb.emb''' , '''embed_tokens''' ) if "prime_state_ln" in key: return key.replace('''prime_state_ln''' , '''encoder.final_layer_norm''' ) if ".ln" in key: return key.replace('''.ln''' , '''.layer_norm''' ) if "_ln" in key: return key.replace('''_ln''' , '''_layer_norm''' ) if "prime_state_proj" in key: return key.replace('''prime_state_proj''' , '''encoder.proj_in''' ) if "prime_x_out" in key: return key.replace('''prime_x_out''' , '''encoder.lm_head''' ) if "prior.x_out" in key: return key.replace('''x_out''' , '''fc_proj_out''' ) if "x_emb" in key: return key.replace('''x_emb''' , '''embed_tokens''' ) return key def __A ( a_ : Tuple , a_ : Any , a_ : List[str] , a_ : Optional[int] )-> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = {} import re SCREAMING_SNAKE_CASE : Optional[int] = re.compile(r'''encoders.(\d*).level_blocks.(\d*).model.(\d*).(\d).(bias|weight)''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = re.compile( r'''encoders.(\d*).level_blocks.(\d*).model.(\d*).(\d).model.(\d*).model.(\d*).(bias|weight)''' ) SCREAMING_SNAKE_CASE : List[str] = re.compile(r'''encoders.(\d*).level_blocks.(\d*).model.(\d*).(bias|weight)''' ) SCREAMING_SNAKE_CASE : int = re.compile(r'''decoders.(\d*).level_blocks.(\d*).model.(\d*).(\d).(bias|weight)''' ) SCREAMING_SNAKE_CASE : Optional[int] = re.compile( r'''decoders.(\d*).level_blocks.(\d*).model.(\d*).(\d).model.(\d*).model.(\d*).(bias|weight)''' ) SCREAMING_SNAKE_CASE : Optional[int] = re.compile(r'''decoders.(\d*).level_blocks.(\d*).model.(\d*).(bias|weight)''' ) SCREAMING_SNAKE_CASE : str = re.compile(r'''conditioner_blocks.(\d*).cond.model.(\d*).(\d).(bias|weight)''' ) SCREAMING_SNAKE_CASE : Optional[int] = re.compile( r'''conditioner_blocks.(\d*).cond.model.(\d*).(\d).model.(\d*).model.(\d*).(bias|weight)''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = re.compile(r'''conditioner_blocks.(\d*).cond.model.(\d*).(bias|weight)''' ) for original_key, value in state_dict.items(): # rename vqvae.encoder keys if re_encoder_block_conv_in.fullmatch(a_ ): SCREAMING_SNAKE_CASE : int = re_encoder_block_conv_in.match(a_ ) SCREAMING_SNAKE_CASE : int = regex_match.groups() SCREAMING_SNAKE_CASE : int = int(groups[2] ) * 2 + int(groups[3] ) SCREAMING_SNAKE_CASE : Optional[Any] = F"encoders.{groups[0]}.level_blocks.{groups[1]}.downsample_block.{block_index}.{groups[-1]}" SCREAMING_SNAKE_CASE : Optional[int] = re_encoder_block_conv_in.sub(a_ , a_ ) elif re_encoder_block_resnet.fullmatch(a_ ): SCREAMING_SNAKE_CASE : Tuple = re_encoder_block_resnet.match(a_ ) SCREAMING_SNAKE_CASE : Any = regex_match.groups() SCREAMING_SNAKE_CASE : Optional[Any] = int(groups[2] ) * 2 + int(groups[3] ) SCREAMING_SNAKE_CASE : Dict = {'''1''': 1, '''3''': 2}[groups[-2]] SCREAMING_SNAKE_CASE : List[str] = F"encoders.{groups[0]}.level_blocks.{groups[1]}.downsample_block.{block_index}." SCREAMING_SNAKE_CASE : Dict = F"resnet_block.{groups[-3]}.conv1d_{conv_index}.{groups[-1]}" SCREAMING_SNAKE_CASE : str = prefix + resnet_block SCREAMING_SNAKE_CASE : str = re_encoder_block_resnet.sub(a_ , a_ ) elif re_encoder_block_proj_out.fullmatch(a_ ): SCREAMING_SNAKE_CASE : List[str] = re_encoder_block_proj_out.match(a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = regex_match.groups() SCREAMING_SNAKE_CASE : Dict = F"encoders.{groups[0]}.level_blocks.{groups[1]}.proj_out.{groups[-1]}" SCREAMING_SNAKE_CASE : Union[str, Any] = re_encoder_block_proj_out.sub(a_ , a_ ) # rename vqvae.decoder keys elif re_decoder_block_conv_out.fullmatch(a_ ): SCREAMING_SNAKE_CASE : int = re_decoder_block_conv_out.match(a_ ) SCREAMING_SNAKE_CASE : Any = regex_match.groups() SCREAMING_SNAKE_CASE : List[Any] = int(groups[2] ) * 2 + int(groups[3] ) - 2 SCREAMING_SNAKE_CASE : Optional[int] = F"decoders.{groups[0]}.level_blocks.{groups[1]}.upsample_block.{block_index}.{groups[-1]}" SCREAMING_SNAKE_CASE : int = re_decoder_block_conv_out.sub(a_ , a_ ) elif re_decoder_block_resnet.fullmatch(a_ ): SCREAMING_SNAKE_CASE : Tuple = re_decoder_block_resnet.match(a_ ) SCREAMING_SNAKE_CASE : Dict = regex_match.groups() SCREAMING_SNAKE_CASE : Optional[Any] = int(groups[2] ) * 2 + int(groups[3] ) - 2 SCREAMING_SNAKE_CASE : Dict = {'''1''': 1, '''3''': 2}[groups[-2]] SCREAMING_SNAKE_CASE : Union[str, Any] = F"decoders.{groups[0]}.level_blocks.{groups[1]}.upsample_block.{block_index}." SCREAMING_SNAKE_CASE : List[Any] = F"resnet_block.{groups[-3]}.conv1d_{conv_index}.{groups[-1]}" SCREAMING_SNAKE_CASE : Optional[int] = prefix + resnet_block SCREAMING_SNAKE_CASE : List[str] = re_decoder_block_resnet.sub(a_ , a_ ) elif re_decoder_block_proj_in.fullmatch(a_ ): SCREAMING_SNAKE_CASE : Tuple = re_decoder_block_proj_in.match(a_ ) SCREAMING_SNAKE_CASE : Optional[int] = regex_match.groups() SCREAMING_SNAKE_CASE : int = F"decoders.{groups[0]}.level_blocks.{groups[1]}.proj_in.{groups[-1]}" SCREAMING_SNAKE_CASE : Optional[Any] = re_decoder_block_proj_in.sub(a_ , a_ ) # rename prior cond.model to upsampler.upsample_block and resnet elif re_prior_cond_conv_out.fullmatch(a_ ): SCREAMING_SNAKE_CASE : Optional[Any] = re_prior_cond_conv_out.match(a_ ) SCREAMING_SNAKE_CASE : Any = regex_match.groups() SCREAMING_SNAKE_CASE : List[str] = int(groups[1] ) * 2 + int(groups[2] ) - 2 SCREAMING_SNAKE_CASE : Optional[int] = F"conditioner_blocks.upsampler.upsample_block.{block_index}.{groups[-1]}" SCREAMING_SNAKE_CASE : str = re_prior_cond_conv_out.sub(a_ , a_ ) elif re_prior_cond_resnet.fullmatch(a_ ): SCREAMING_SNAKE_CASE : Tuple = re_prior_cond_resnet.match(a_ ) SCREAMING_SNAKE_CASE : Optional[int] = regex_match.groups() SCREAMING_SNAKE_CASE : List[Any] = int(groups[1] ) * 2 + int(groups[2] ) - 2 SCREAMING_SNAKE_CASE : List[Any] = {'''1''': 1, '''3''': 2}[groups[-2]] SCREAMING_SNAKE_CASE : List[Any] = F"conditioner_blocks.upsampler.upsample_block.{block_index}." SCREAMING_SNAKE_CASE : List[Any] = F"resnet_block.{groups[-3]}.conv1d_{conv_index}.{groups[-1]}" SCREAMING_SNAKE_CASE : Optional[Any] = prefix + resnet_block SCREAMING_SNAKE_CASE : List[str] = re_prior_cond_resnet.sub(a_ , a_ ) elif re_prior_cond_proj_in.fullmatch(a_ ): SCREAMING_SNAKE_CASE : Optional[int] = re_prior_cond_proj_in.match(a_ ) SCREAMING_SNAKE_CASE : int = regex_match.groups() SCREAMING_SNAKE_CASE : int = F"conditioner_blocks.upsampler.proj_in.{groups[-1]}" SCREAMING_SNAKE_CASE : List[Any] = re_prior_cond_proj_in.sub(a_ , a_ ) # keep original key else: SCREAMING_SNAKE_CASE : List[str] = original_key SCREAMING_SNAKE_CASE : Optional[int] = replace_key(a_ ) if F"{key_prefix}.{key}" not in model_state_dict or key is None: print(F"failed converting {original_key} to {key}, does not match" ) # handle missmatched shape elif value.shape != model_state_dict[F"{key_prefix}.{key}"].shape: SCREAMING_SNAKE_CASE : Tuple = model_state_dict[F"{key_prefix}.{key}"] print(F"{original_key}-> {key} : \nshape {val.shape} and { value.shape}, do not match" ) SCREAMING_SNAKE_CASE : Dict = original_key SCREAMING_SNAKE_CASE : Any = original_key SCREAMING_SNAKE_CASE : int = value return new_dict @torch.no_grad() def __A ( a_ : Optional[Any]=None , a_ : Union[str, Any]=None )-> str: '''simple docstring''' for file in MODEL_MAPPING[model_name]: if not os.path.isfile(F"{pytorch_dump_folder_path}/{file.split('/' )[-1]}" ): SCREAMING_SNAKE_CASE : Tuple = requests.get(F"{PREFIX}{file}" , allow_redirects=a_ ) os.makedirs(F"{pytorch_dump_folder_path}/" , exist_ok=a_ ) open(F"{pytorch_dump_folder_path}/{file.split('/' )[-1]}" , '''wb''' ).write(r.content ) SCREAMING_SNAKE_CASE : Union[str, Any] = MODEL_MAPPING[model_name.split('''/''' )[-1]] SCREAMING_SNAKE_CASE : str = JukeboxConfig.from_pretrained(a_ ) SCREAMING_SNAKE_CASE : int = JukeboxModel(a_ ) SCREAMING_SNAKE_CASE : Optional[Any] = [] SCREAMING_SNAKE_CASE : List[str] = {} for i, dict_name in enumerate(a_ ): SCREAMING_SNAKE_CASE : Optional[Any] = torch.load(F"{pytorch_dump_folder_path}/{dict_name.split('/' )[-1]}" )['''model'''] SCREAMING_SNAKE_CASE : List[Any] = {} for k in old_dic.keys(): if k.endswith('''.b''' ): SCREAMING_SNAKE_CASE : Optional[Any] = old_dic[k] elif k.endswith('''.w''' ): SCREAMING_SNAKE_CASE : List[Any] = old_dic[k] elif "level_2" not in dict_name and "cond.model." in k: SCREAMING_SNAKE_CASE : Union[str, Any] = old_dic[k] else: SCREAMING_SNAKE_CASE : Optional[Any] = old_dic[k] SCREAMING_SNAKE_CASE : Dict = '''vqvae''' if i == 0 else F"priors.{3 - i}" SCREAMING_SNAKE_CASE : Optional[Any] = fix_jukebox_keys(a_ , model.state_dict() , a_ , a_ ) weight_dict.append(a_ ) SCREAMING_SNAKE_CASE : List[str] = weight_dict.pop(0 ) model.vqvae.load_state_dict(a_ ) for i in range(len(a_ ) ): model.priors[i].load_state_dict(weight_dict[2 - i] ) Path(a_ ).mkdir(exist_ok=a_ ) with open(F"{pytorch_dump_folder_path}/mapping.json" , '''w''' ) as txtfile: json.dump(a_ , a_ ) print(F"Saving model {model_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(a_ ) return weight_dict if __name__ == "__main__": lowerCamelCase__ : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default="jukebox-5b-lyrics", type=str, help="Name of the model you'd like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default="jukebox-5b-lyrics-converted", type=str, help="Path to the output PyTorch model directory.", ) lowerCamelCase__ : Any = parser.parse_args() convert_openai_checkpoint(args.model_name, args.pytorch_dump_folder_path)
698
"""simple docstring""" def __A ( a_ : int = 10 , a_ : int = 10_00 , a_ : bool = True )-> int: '''simple docstring''' assert ( isinstance(a_ , a_ ) and isinstance(a_ , a_ ) and isinstance(a_ , a_ ) ), "Invalid type of value(s) specified to function!" if min_val > max_val: raise ValueError('''Invalid value for min_val or max_val (min_value < max_value)''' ) return min_val if option else max_val def __A ( a_ : int , a_ : int )-> int: '''simple docstring''' return int((number_a + number_a) / 2 ) def __A ( a_ : int , a_ : int , a_ : int )-> None: '''simple docstring''' assert ( isinstance(a_ , a_ ) and isinstance(a_ , a_ ) and isinstance(a_ , a_ ) ), 'argument values must be type of "int"' if lower > higher: raise ValueError('''argument value for lower and higher must be(lower > higher)''' ) if not lower < to_guess < higher: raise ValueError( '''guess value must be within the range of lower and higher value''' ) def answer(a_ : int ) -> str: if number > to_guess: return "high" elif number < to_guess: return "low" else: return "same" print('''started...''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = lower SCREAMING_SNAKE_CASE : int = higher SCREAMING_SNAKE_CASE : List[str] = [] while True: SCREAMING_SNAKE_CASE : Any = get_avg(a_ , a_ ) last_numbers.append(a_ ) if answer(a_ ) == "low": SCREAMING_SNAKE_CASE : Dict = number elif answer(a_ ) == "high": SCREAMING_SNAKE_CASE : Tuple = number else: break print(F"guess the number : {last_numbers[-1]}" ) print(F"details : {last_numbers!s}" ) def __A ( )-> None: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = int(input('''Enter lower value : ''' ).strip() ) SCREAMING_SNAKE_CASE : Tuple = int(input('''Enter high value : ''' ).strip() ) SCREAMING_SNAKE_CASE : List[str] = int(input('''Enter value to guess : ''' ).strip() ) guess_the_number(a_ , a_ , a_ ) if __name__ == "__main__": main()
698
1
"""simple docstring""" import functools import logging import os import sys import threading from logging import ( CRITICAL, # NOQA DEBUG, # NOQA ERROR, # NOQA FATAL, # NOQA INFO, # NOQA NOTSET, # NOQA WARN, # NOQA WARNING, # NOQA ) from typing import Optional import huggingface_hub.utils as hf_hub_utils from tqdm import auto as tqdm_lib lowerCamelCase__ : Any = threading.Lock() lowerCamelCase__ : Optional[logging.Handler] = None lowerCamelCase__ : Tuple = { "debug": logging.DEBUG, "info": logging.INFO, "warning": logging.WARNING, "error": logging.ERROR, "critical": logging.CRITICAL, } lowerCamelCase__ : int = logging.WARNING lowerCamelCase__ : Union[str, Any] = True def __A ( )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = os.getenv('''TRANSFORMERS_VERBOSITY''' , a_ ) if env_level_str: if env_level_str in log_levels: return log_levels[env_level_str] else: logging.getLogger().warning( F"Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, " F"has to be one of: { ', '.join(log_levels.keys() ) }" ) return _default_log_level def __A ( )-> str: '''simple docstring''' return __name__.split('''.''' )[0] def __A ( )-> logging.Logger: '''simple docstring''' return logging.getLogger(_get_library_name() ) def __A ( )-> None: '''simple docstring''' global _default_handler with _lock: if _default_handler: # This library has already configured the library root logger. return SCREAMING_SNAKE_CASE : int = logging.StreamHandler() # Set sys.stderr as stream. SCREAMING_SNAKE_CASE : Union[str, Any] = sys.stderr.flush # Apply our default configuration to the library root logger. SCREAMING_SNAKE_CASE : Any = _get_library_root_logger() library_root_logger.addHandler(_default_handler ) library_root_logger.setLevel(_get_default_logging_level() ) SCREAMING_SNAKE_CASE : Dict = False def __A ( )-> None: '''simple docstring''' global _default_handler with _lock: if not _default_handler: return SCREAMING_SNAKE_CASE : List[str] = _get_library_root_logger() library_root_logger.removeHandler(_default_handler ) library_root_logger.setLevel(logging.NOTSET ) SCREAMING_SNAKE_CASE : str = None def __A ( )-> Optional[int]: '''simple docstring''' return log_levels def __A ( a_ : Optional[str] = None )-> logging.Logger: '''simple docstring''' if name is None: SCREAMING_SNAKE_CASE : int = _get_library_name() _configure_library_root_logger() return logging.getLogger(a_ ) def __A ( )-> int: '''simple docstring''' _configure_library_root_logger() return _get_library_root_logger().getEffectiveLevel() def __A ( a_ : int )-> None: '''simple docstring''' _configure_library_root_logger() _get_library_root_logger().setLevel(a_ ) def __A ( )-> Any: '''simple docstring''' return set_verbosity(a_ ) def __A ( )-> Union[str, Any]: '''simple docstring''' return set_verbosity(a_ ) def __A ( )-> List[Any]: '''simple docstring''' return set_verbosity(a_ ) def __A ( )-> str: '''simple docstring''' return set_verbosity(a_ ) def __A ( )-> None: '''simple docstring''' _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().removeHandler(_default_handler ) def __A ( )-> None: '''simple docstring''' _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().addHandler(_default_handler ) def __A ( a_ : logging.Handler )-> None: '''simple docstring''' _configure_library_root_logger() assert handler is not None _get_library_root_logger().addHandler(a_ ) def __A ( a_ : logging.Handler )-> None: '''simple docstring''' _configure_library_root_logger() assert handler is not None and handler not in _get_library_root_logger().handlers _get_library_root_logger().removeHandler(a_ ) def __A ( )-> None: '''simple docstring''' _configure_library_root_logger() SCREAMING_SNAKE_CASE : Union[str, Any] = False def __A ( )-> None: '''simple docstring''' _configure_library_root_logger() SCREAMING_SNAKE_CASE : Dict = True def __A ( )-> None: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = _get_library_root_logger().handlers for handler in handlers: SCREAMING_SNAKE_CASE : Optional[int] = logging.Formatter('''[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s''' ) handler.setFormatter(a_ ) def __A ( )-> None: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = _get_library_root_logger().handlers for handler in handlers: handler.setFormatter(a_ ) def __A ( self : Union[str, Any] , *a_ : Dict , **a_ : List[Any] )-> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = os.getenv('''TRANSFORMERS_NO_ADVISORY_WARNINGS''' , a_ ) if no_advisory_warnings: return self.warning(*a_ , **a_ ) lowerCamelCase__ : int = warning_advice @functools.lru_cache(a_ ) def __A ( self : Any , *a_ : Any , **a_ : Any )-> int: '''simple docstring''' self.warning(*a_ , **a_ ) lowerCamelCase__ : int = warning_once class lowercase__: '''simple docstring''' def __init__( self :Dict , *lowerCamelCase_ :Any , **lowerCamelCase_ :Dict ) -> str: # pylint: disable=unused-argument '''simple docstring''' SCREAMING_SNAKE_CASE : Any = args[0] if args else None def __iter__( self :str ) -> Optional[int]: '''simple docstring''' return iter(self._iterator ) def __getattr__( self :Any , lowerCamelCase_ :str ) -> Optional[Any]: '''simple docstring''' def empty_fn(*lowerCamelCase_ :int , **lowerCamelCase_ :Dict ): # pylint: disable=unused-argument return return empty_fn def __enter__( self :Optional[Any] ) -> List[Any]: '''simple docstring''' return self def __exit__( self :Any , lowerCamelCase_ :Any , lowerCamelCase_ :Tuple , lowerCamelCase_ :Union[str, Any] ) -> Optional[int]: '''simple docstring''' return class lowercase__: '''simple docstring''' def __call__( self :Dict , *lowerCamelCase_ :Optional[int] , **lowerCamelCase_ :List[str] ) -> List[Any]: '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm(*lowerCamelCase_ , **lowerCamelCase_ ) else: return EmptyTqdm(*lowerCamelCase_ , **lowerCamelCase_ ) def __lowerCAmelCase ( self :List[str] , *lowerCamelCase_ :Optional[Any] , **lowerCamelCase_ :List[str] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = None if _tqdm_active: return tqdm_lib.tqdm.set_lock(*lowerCamelCase_ , **lowerCamelCase_ ) def __lowerCAmelCase ( self :Any ) -> int: '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm.get_lock() lowerCamelCase__ : List[str] = _tqdm_cls() def __A ( )-> bool: '''simple docstring''' global _tqdm_active return bool(_tqdm_active ) def __A ( )-> Optional[int]: '''simple docstring''' global _tqdm_active SCREAMING_SNAKE_CASE : Tuple = True hf_hub_utils.enable_progress_bars() def __A ( )-> str: '''simple docstring''' global _tqdm_active SCREAMING_SNAKE_CASE : int = False hf_hub_utils.disable_progress_bars()
698
"""simple docstring""" import argparse import fairseq import torch from torch import nn from transformers import ( MBartaaTokenizer, MBartConfig, MBartForCausalLM, SpeechEncoderDecoderConfig, SpeechEncoderDecoderModel, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaModel, logging, ) logging.set_verbosity_info() lowerCamelCase__ : List[Any] = logging.get_logger(__name__) lowerCamelCase__ : Tuple = { "post_extract_proj": "feature_projection.projection", "encoder.pos_conv.0": "encoder.pos_conv_embed.conv", "self_attn.k_proj": "encoder.layers.*.attention.k_proj", "self_attn.v_proj": "encoder.layers.*.attention.v_proj", "self_attn.q_proj": "encoder.layers.*.attention.q_proj", "self_attn.out_proj": "encoder.layers.*.attention.out_proj", "self_attn_layer_norm": "encoder.layers.*.layer_norm", "fc1": "encoder.layers.*.feed_forward.intermediate_dense", "fc2": "encoder.layers.*.feed_forward.output_dense", "final_layer_norm": "encoder.layers.*.final_layer_norm", "encoder.layer_norm": "encoder.layer_norm", "w2v_model.layer_norm": "feature_projection.layer_norm", "quantizer.weight_proj": "quantizer.weight_proj", "quantizer.vars": "quantizer.codevectors", "project_q": "project_q", "final_proj": "project_hid", "w2v_encoder.proj": "lm_head", "mask_emb": "masked_spec_embed", } lowerCamelCase__ : List[str] = [ "lm_head", "quantizer.weight_proj", "quantizer.codevectors", "project_q", "project_hid", ] def __A ( a_ : Optional[int] , a_ : str , a_ : str , a_ : str , a_ : List[str] )-> Tuple: '''simple docstring''' for attribute in key.split('''.''' ): SCREAMING_SNAKE_CASE : Any = getattr(a_ , a_ ) if weight_type is not None: SCREAMING_SNAKE_CASE : Optional[int] = getattr(a_ , a_ ).shape else: SCREAMING_SNAKE_CASE : Any = hf_pointer.shape assert hf_shape == value.shape, ( F"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be" F" {value.shape} for {full_name}" ) if weight_type == "weight": SCREAMING_SNAKE_CASE : List[Any] = value elif weight_type == "weight_g": SCREAMING_SNAKE_CASE : Optional[int] = value elif weight_type == "weight_v": SCREAMING_SNAKE_CASE : Any = value elif weight_type == "bias": SCREAMING_SNAKE_CASE : List[Any] = value else: SCREAMING_SNAKE_CASE : List[str] = value logger.info(F"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." ) def __A ( a_ : Optional[Any] , a_ : Dict )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = [] SCREAMING_SNAKE_CASE : Optional[Any] = fairseq_model.state_dict() SCREAMING_SNAKE_CASE : Tuple = hf_model.feature_extractor SCREAMING_SNAKE_CASE : Tuple = hf_model.adapter for name, value in fairseq_dict.items(): SCREAMING_SNAKE_CASE : int = False if "conv_layers" in name: load_conv_layer( a_ , a_ , a_ , a_ , hf_model.config.feat_extract_norm == '''group''' , ) SCREAMING_SNAKE_CASE : List[str] = True elif any(x in name for x in ['''adaptor''', '''w2v_encoder.proj.''', '''w2v_proj_ln.'''] ): load_adapter(a_ , a_ , a_ , a_ ) SCREAMING_SNAKE_CASE : List[Any] = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split('''w2v_model.''' )[-1] == name.split('''.''' )[0]: SCREAMING_SNAKE_CASE : Union[str, Any] = True if "*" in mapped_key: SCREAMING_SNAKE_CASE : Dict = name.split(a_ )[0].split('''.''' )[-2] SCREAMING_SNAKE_CASE : Optional[int] = mapped_key.replace('''*''' , a_ ) if "weight_g" in name: SCREAMING_SNAKE_CASE : List[str] = '''weight_g''' elif "weight_v" in name: SCREAMING_SNAKE_CASE : Union[str, Any] = '''weight_v''' elif "bias" in name: SCREAMING_SNAKE_CASE : str = '''bias''' elif "weight" in name: SCREAMING_SNAKE_CASE : Tuple = '''weight''' else: SCREAMING_SNAKE_CASE : str = None set_recursively(a_ , a_ , a_ , a_ , a_ ) continue if not is_used: unused_weights.append(a_ ) logger.warning(F"Unused weights: {unused_weights}" ) def __A ( a_ : Dict , a_ : int , a_ : Optional[int] , a_ : Optional[int] , a_ : Dict )-> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = full_name.split('''conv_layers.''' )[-1] SCREAMING_SNAKE_CASE : List[str] = name.split('''.''' ) SCREAMING_SNAKE_CASE : Dict = int(items[0] ) SCREAMING_SNAKE_CASE : Optional[Any] = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found." ) SCREAMING_SNAKE_CASE : List[Any] = value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found." ) SCREAMING_SNAKE_CASE : str = value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( F"{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was" " found." ) SCREAMING_SNAKE_CASE : str = value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found." ) SCREAMING_SNAKE_CASE : Union[str, Any] = value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) else: unused_weights.append(a_ ) def __A ( a_ : Optional[int] , a_ : Optional[int] , a_ : Any , a_ : Any )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = full_name.split('''adaptor.''' )[-1] SCREAMING_SNAKE_CASE : List[Any] = name.split('''.''' ) if items[1].isdigit(): SCREAMING_SNAKE_CASE : List[Any] = int(items[1] ) else: SCREAMING_SNAKE_CASE : str = None if "adaptor" not in full_name: if "proj_ln" in full_name: # has to be layer norm if "bias" in name: assert ( value.shape == adapter.proj_layer_norm.bias.data.shape ), F"{full_name} has size {value.shape}, but {adapter.proj_layer_norm.bias.data.shape} was found." SCREAMING_SNAKE_CASE : str = value logger.info(F"Adapter proj layer norm bias was initialized from {full_name}." ) if "weight" in name: assert ( value.shape == adapter.proj_layer_norm.weight.data.shape ), F"{full_name} has size {value.shape}, but {adapter.proj_layer_norm.weight.data.shape} was found." SCREAMING_SNAKE_CASE : Optional[Any] = value else: # has to be projection layer if "bias" in name: assert ( value.shape == adapter.proj.bias.data.shape ), F"{full_name} has size {value.shape}, but {adapter.proj.bias.data.shape} was found." SCREAMING_SNAKE_CASE : Union[str, Any] = value logger.info(F"Adapter proj layer bias was initialized from {full_name}." ) if "weight" in name: assert ( value.shape == adapter.proj.weight.data.shape ), F"{full_name} has size {value.shape}, but {adapter.proj.weight.data.shape} was found." SCREAMING_SNAKE_CASE : int = value logger.info(F"Adapter proj layer weight was initialized from {full_name}." ) elif isinstance(a_ , a_ ): if "bias" in name: assert ( value.shape == adapter.layers[layer_id].conv.bias.data.shape ), F"{full_name} has size {value.shape}, but {adapter.layers[layer_id].conv.bias.data.shape} was found." SCREAMING_SNAKE_CASE : str = value logger.info(F"Adapter layer {layer_id} bias was initialized from {full_name}." ) elif "weight" in name: assert ( value.shape == adapter.layers[layer_id].conv.weight.data.shape ), F"{full_name} has size {value.shape}, but {adapter.layers[layer_id].conv.weight.data.shape} was found." SCREAMING_SNAKE_CASE : List[str] = value logger.info(F"Adapter layer {layer_id} bias was initialized from {full_name}." ) else: unused_weights.append(a_ ) def __A ( a_ : Optional[Any] )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = emb.weight.shape SCREAMING_SNAKE_CASE : Any = nn.Linear(a_ , a_ , bias=a_ ) SCREAMING_SNAKE_CASE : Optional[int] = emb.weight.data return lin_layer @torch.no_grad() def __A ( a_ : Tuple , a_ : Optional[int] , a_ : List[Any] , a_ : Any , a_ : Tuple , a_ : int , a_ : Any , a_ : str , a_ : Tuple , a_ : Union[str, Any] , a_ : Union[str, Any] , )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = WavaVecaConfig.from_pretrained( a_ , add_adapter=a_ , adapter_stride=a_ , adapter_kernel_size=a_ , use_auth_token=a_ , output_hidden_size=a_ , ) SCREAMING_SNAKE_CASE : Union[str, Any] = MBartConfig.from_pretrained(a_ ) # load model SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={ '''config_yaml''': config_yaml_path, '''data''': '''/'''.join(dict_path.split('''/''' )[:-1] ), '''w2v_path''': checkpoint_path, '''load_pretrained_decoder_from''': None, } , ) SCREAMING_SNAKE_CASE : int = model[0].eval() # load feature extractor SCREAMING_SNAKE_CASE : Union[str, Any] = WavaVecaFeatureExtractor.from_pretrained(a_ , use_auth_token=a_ ) # set weights for wav2vec2 encoder SCREAMING_SNAKE_CASE : str = WavaVecaModel(a_ ) recursively_load_weights_wavaveca(model.encoder , a_ ) # load decoder weights SCREAMING_SNAKE_CASE : Dict = MBartForCausalLM(a_ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict() , strict=a_ ) logger.warning(F"The following keys are missing when loading the decoder weights: {missing_keys}" ) logger.warning(F"The following keys are unexpected when loading the decoder weights: {unexpected_keys}" ) SCREAMING_SNAKE_CASE : Union[str, Any] = SpeechEncoderDecoderModel(encoder=a_ , decoder=a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = False SCREAMING_SNAKE_CASE : Optional[Any] = MBartaaTokenizer(a_ ) tokenizer.save_pretrained(a_ ) SCREAMING_SNAKE_CASE : Tuple = hf_wavavec.config.to_dict() SCREAMING_SNAKE_CASE : Any = tokenizer.pad_token_id SCREAMING_SNAKE_CASE : List[str] = tokenizer.bos_token_id SCREAMING_SNAKE_CASE : Dict = tokenizer.eos_token_id SCREAMING_SNAKE_CASE : Optional[Any] = '''mbart50''' SCREAMING_SNAKE_CASE : Optional[int] = '''wav2vec2''' SCREAMING_SNAKE_CASE : Optional[Any] = tokenizer.eos_token_id SCREAMING_SNAKE_CASE : List[str] = 25_00_04 SCREAMING_SNAKE_CASE : Dict = tokenizer.eos_token_id SCREAMING_SNAKE_CASE : Any = SpeechEncoderDecoderConfig.from_dict(a_ ) hf_wavavec.save_pretrained(a_ ) feature_extractor.save_pretrained(a_ ) if __name__ == "__main__": lowerCamelCase__ : Any = argparse.ArgumentParser() parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to fairseq checkpoint") parser.add_argument("--dict_path", default=None, type=str, help="Path to dict of fine-tuned model") parser.add_argument("--config_yaml_path", default=None, type=str, help="Path to yaml file of fine-tuned model") parser.add_argument( "--encoder_config_path", default="facebook/wav2vec2-xls-r-1b", type=str, help="Path to hf encoder wav2vec2 checkpoint config", ) parser.add_argument( "--decoder_config_path", default="facebook/mbart-large-50-one-to-many-mmt", type=str, help="Path to hf decoder checkpoint config", ) parser.add_argument("--add_adapter", default=True, type=bool, help="whethere to add model adapter layers") parser.add_argument("--adapter_stride", default=2, type=int, help="stride of adapter layers") parser.add_argument("--adapter_kernel_size", default=3, type=int, help="kernel size of adapter layers") parser.add_argument("--encoder_output_dim", default=1024, type=int, help="encoder output dim") parser.add_argument("--start_token_id", default=250004, type=int, help="`decoder_start_token_id` of model config") lowerCamelCase__ : Dict = parser.parse_args() convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.dict_path, args.config_yaml_path, encoder_config_path=args.encoder_config_path, decoder_config_path=args.decoder_config_path, add_adapter=args.add_adapter, adapter_kernel_size=args.adapter_kernel_size, adapter_stride=args.adapter_stride, decoder_start_token_id=args.start_token_id, encoder_output_dim=args.encoder_output_dim, )
698
1
"""simple docstring""" # using dfs for finding eulerian path traversal def __A ( a_ : Dict , a_ : int , a_ : str , a_ : Optional[Any]=None )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = (path or []) + [u] for v in graph[u]: if visited_edge[u][v] is False: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = True, True SCREAMING_SNAKE_CASE : List[str] = dfs(a_ , a_ , a_ , a_ ) return path def __A ( a_ : List[str] , a_ : Any )-> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = 0 SCREAMING_SNAKE_CASE : str = -1 for i in range(a_ ): if i not in graph.keys(): continue if len(graph[i] ) % 2 == 1: odd_degree_nodes += 1 SCREAMING_SNAKE_CASE : Tuple = i if odd_degree_nodes == 0: return 1, odd_node if odd_degree_nodes == 2: return 2, odd_node return 3, odd_node def __A ( a_ : Any , a_ : int )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = [[False for _ in range(max_node + 1 )] for _ in range(max_node + 1 )] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = check_circuit_or_path(a_ , a_ ) if check == 3: print('''graph is not Eulerian''' ) print('''no path''' ) return SCREAMING_SNAKE_CASE : Tuple = 1 if check == 2: SCREAMING_SNAKE_CASE : Optional[int] = odd_node print('''graph has a Euler path''' ) if check == 1: print('''graph has a Euler cycle''' ) SCREAMING_SNAKE_CASE : Optional[int] = dfs(a_ , a_ , a_ ) print(a_ ) def __A ( )-> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = {1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]} SCREAMING_SNAKE_CASE : str = {1: [2, 3, 4, 5], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [1, 4]} SCREAMING_SNAKE_CASE : str = {1: [2, 3, 4], 2: [1, 3, 4], 3: [1, 2], 4: [1, 2, 5], 5: [4]} SCREAMING_SNAKE_CASE : int = {1: [2, 3], 2: [1, 3], 3: [1, 2]} SCREAMING_SNAKE_CASE : int = { 1: [], 2: [] # all degree is zero } SCREAMING_SNAKE_CASE : List[str] = 10 check_euler(a_ , a_ ) check_euler(a_ , a_ ) check_euler(a_ , a_ ) check_euler(a_ , a_ ) check_euler(a_ , a_ ) if __name__ == "__main__": main()
698
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available lowerCamelCase__ : Union[str, Any] = { "configuration_m2m_100": ["M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP", "M2M100Config", "M2M100OnnxConfig"], "tokenization_m2m_100": ["M2M100Tokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : str = [ "M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST", "M2M100ForConditionalGeneration", "M2M100Model", "M2M100PreTrainedModel", ] if TYPE_CHECKING: from .configuration_mam_aaa import M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP, MaMaaaConfig, MaMaaaOnnxConfig from .tokenization_mam_aaa import MaMaaaTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mam_aaa import ( M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST, MaMaaaForConditionalGeneration, MaMaaaModel, MaMaaaPreTrainedModel, ) else: import sys lowerCamelCase__ : Optional[int] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
698
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCamelCase__ : Tuple = { "configuration_roberta": ["ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP", "RobertaConfig", "RobertaOnnxConfig"], "tokenization_roberta": ["RobertaTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Dict = ["RobertaTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : str = [ "ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "RobertaForCausalLM", "RobertaForMaskedLM", "RobertaForMultipleChoice", "RobertaForQuestionAnswering", "RobertaForSequenceClassification", "RobertaForTokenClassification", "RobertaModel", "RobertaPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Any = [ "TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "TFRobertaForCausalLM", "TFRobertaForMaskedLM", "TFRobertaForMultipleChoice", "TFRobertaForQuestionAnswering", "TFRobertaForSequenceClassification", "TFRobertaForTokenClassification", "TFRobertaMainLayer", "TFRobertaModel", "TFRobertaPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Optional[int] = [ "FlaxRobertaForCausalLM", "FlaxRobertaForMaskedLM", "FlaxRobertaForMultipleChoice", "FlaxRobertaForQuestionAnswering", "FlaxRobertaForSequenceClassification", "FlaxRobertaForTokenClassification", "FlaxRobertaModel", "FlaxRobertaPreTrainedModel", ] if TYPE_CHECKING: from .configuration_roberta import ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, RobertaConfig, RobertaOnnxConfig from .tokenization_roberta import RobertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roberta_fast import RobertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roberta import ( ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, RobertaForCausalLM, RobertaForMaskedLM, RobertaForMultipleChoice, RobertaForQuestionAnswering, RobertaForSequenceClassification, RobertaForTokenClassification, RobertaModel, RobertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roberta import ( TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFRobertaForCausalLM, TFRobertaForMaskedLM, TFRobertaForMultipleChoice, TFRobertaForQuestionAnswering, TFRobertaForSequenceClassification, TFRobertaForTokenClassification, TFRobertaMainLayer, TFRobertaModel, TFRobertaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, FlaxRobertaPreTrainedModel, ) else: import sys lowerCamelCase__ : int = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
698
"""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 lowerCamelCase__ : List[Any] = "\\n@inproceedings{pillutla-etal:mauve:neurips2021,\n title={MAUVE: Measuring the Gap Between Neural Text and Human Text using Divergence Frontiers},\n author={Pillutla, Krishna and Swayamdipta, Swabha and Zellers, Rowan and Thickstun, John and Welleck, Sean and Choi, Yejin and Harchaoui, Zaid},\n booktitle = {NeurIPS},\n year = {2021}\n}\n\n" lowerCamelCase__ : List[str] = "\\nMAUVE is a library built on PyTorch and HuggingFace Transformers to measure the gap between neural text and human text with the eponymous MAUVE measure.\n\nMAUVE summarizes both Type I and Type II errors measured softly using Kullback–Leibler (KL) divergences.\n\nFor details, see the MAUVE paper: https://arxiv.org/abs/2102.01454 (Neurips, 2021).\n\nThis metrics is a wrapper around the official implementation of MAUVE:\nhttps://github.com/krishnap25/mauve\n" lowerCamelCase__ : List[Any] = "\nCalculates MAUVE scores between two lists of generated text and reference text.\nArgs:\n predictions: list of generated text to score. Each predictions\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\nOptional Args:\n num_buckets: the size of the histogram to quantize P and Q. Options: 'auto' (default) or an integer\n pca_max_data: the number data points to use for PCA dimensionality reduction prior to clustering. If -1, use all the data. Default -1\n kmeans_explained_var: amount of variance of the data to keep in dimensionality reduction by PCA. Default 0.9\n kmeans_num_redo: number of times to redo k-means clustering (the best objective is kept). Default 5\n kmeans_max_iter: maximum number of k-means iterations. Default 500\n 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'].\n 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\n max_text_length: maximum number of tokens to consider. Default 1024\n divergence_curve_discretization_size: Number of points to consider on the divergence curve. Default 25\n mauve_scaling_factor: \"c\" from the paper. Default 5.\n verbose: If True (default), print running time updates\n seed: random seed to initialize k-means cluster assignments.\nReturns:\n mauve: MAUVE score, a number between 0 and 1. Larger values indicate that P and Q are closer,\n frontier_integral: Frontier Integral, a number between 0 and 1. Smaller values indicate that P and Q are closer,\n divergence_curve: a numpy.ndarray of shape (m, 2); plot it with matplotlib to view the divergence curve,\n p_hist: a discrete distribution, which is a quantized version of the text distribution p_text,\n q_hist: same as above, but with q_text.\nExamples:\n\n >>> # faiss segfaults in doctest for some reason, so the .compute call is not tested with doctest\n >>> import datasets\n >>> mauve = datasets.load_metric('mauve')\n >>> predictions = [\"hello there\", \"general kenobi\"]\n >>> references = [\"hello there\", \"general kenobi\"]\n >>> out = mauve.compute(predictions=predictions, references=references) # doctest: +SKIP\n >>> print(out.mauve) # doctest: +SKIP\n 1.0\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowercase__( datasets.Metric ): '''simple docstring''' def __lowerCAmelCase ( self :Optional[int] ) -> 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 __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :List[str] , lowerCamelCase_ :Optional[Any]=None , lowerCamelCase_ :str=None , lowerCamelCase_ :Tuple=None , lowerCamelCase_ :Optional[Any]=None , lowerCamelCase_ :Optional[int]="auto" , lowerCamelCase_ :Dict=-1 , lowerCamelCase_ :str=0.9 , lowerCamelCase_ :str=5 , lowerCamelCase_ :Tuple=5_00 , lowerCamelCase_ :str="gpt2-large" , lowerCamelCase_ :List[Any]=-1 , lowerCamelCase_ :Dict=10_24 , lowerCamelCase_ :Tuple=25 , lowerCamelCase_ :List[Any]=5 , lowerCamelCase_ :Dict=True , lowerCamelCase_ :List[Any]=25 , ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = compute_mauve( p_text=lowerCamelCase_ , q_text=lowerCamelCase_ , p_features=lowerCamelCase_ , q_features=lowerCamelCase_ , p_tokens=lowerCamelCase_ , q_tokens=lowerCamelCase_ , num_buckets=lowerCamelCase_ , pca_max_data=lowerCamelCase_ , kmeans_explained_var=lowerCamelCase_ , kmeans_num_redo=lowerCamelCase_ , kmeans_max_iter=lowerCamelCase_ , featurize_model_name=lowerCamelCase_ , device_id=lowerCamelCase_ , max_text_length=lowerCamelCase_ , divergence_curve_discretization_size=lowerCamelCase_ , mauve_scaling_factor=lowerCamelCase_ , verbose=lowerCamelCase_ , seed=lowerCamelCase_ , ) return out
698
1
"""simple docstring""" from collections import UserDict from typing import Union import numpy as np import requests from ..utils import ( add_end_docstrings, logging, ) from .audio_classification import ffmpeg_read from .base import PIPELINE_INIT_ARGS, Pipeline lowerCamelCase__ : Any = logging.get_logger(__name__) @add_end_docstrings(_UpperCAmelCase ) class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __init__( self :str , **lowerCamelCase_ :Dict ) -> List[Any]: '''simple docstring''' super().__init__(**lowerCamelCase_ ) if self.framework != "pt": raise ValueError(f"The {self.__class__} is only available in PyTorch." ) # No specific FOR_XXX available yet def __call__( self :Any , lowerCamelCase_ :Union[np.ndarray, bytes, str] , **lowerCamelCase_ :Tuple ) -> List[str]: '''simple docstring''' return super().__call__(lowerCamelCase_ , **lowerCamelCase_ ) def __lowerCAmelCase ( self :int , **lowerCamelCase_ :str ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = {} if "candidate_labels" in kwargs: SCREAMING_SNAKE_CASE : List[str] = kwargs['''candidate_labels'''] if "hypothesis_template" in kwargs: SCREAMING_SNAKE_CASE : Optional[Any] = kwargs['''hypothesis_template'''] return preprocess_params, {}, {} def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :int , lowerCamelCase_ :Union[str, Any]=None , lowerCamelCase_ :Optional[Any]="This is a sound of {}." ) -> str: '''simple docstring''' if isinstance(lowerCamelCase_ , lowerCamelCase_ ): if audio.startswith('''http://''' ) or audio.startswith('''https://''' ): # We need to actually check for a real protocol, otherwise it's impossible to use a local file # like http_huggingface_co.png SCREAMING_SNAKE_CASE : List[str] = requests.get(lowerCamelCase_ ).content else: with open(lowerCamelCase_ , '''rb''' ) as f: SCREAMING_SNAKE_CASE : Any = f.read() if isinstance(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Optional[Any] = ffmpeg_read(lowerCamelCase_ , self.feature_extractor.sampling_rate ) if not isinstance(lowerCamelCase_ , np.ndarray ): raise ValueError('''We expect a numpy ndarray as input''' ) if len(audio.shape ) != 1: raise ValueError('''We expect a single channel audio input for ZeroShotAudioClassificationPipeline''' ) SCREAMING_SNAKE_CASE : List[str] = self.feature_extractor( [audio] , sampling_rate=self.feature_extractor.sampling_rate , return_tensors='''pt''' ) SCREAMING_SNAKE_CASE : str = candidate_labels SCREAMING_SNAKE_CASE : Optional[Any] = [hypothesis_template.format(lowerCamelCase_ ) for x in candidate_labels] SCREAMING_SNAKE_CASE : Any = self.tokenizer(lowerCamelCase_ , return_tensors=self.framework , padding=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = [text_inputs] return inputs def __lowerCAmelCase ( self :Any , lowerCamelCase_ :Optional[int] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = model_inputs.pop('''candidate_labels''' ) SCREAMING_SNAKE_CASE : Any = model_inputs.pop('''text_inputs''' ) if isinstance(text_inputs[0] , lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Any = text_inputs[0] else: # Batching case. SCREAMING_SNAKE_CASE : Tuple = text_inputs[0][0] SCREAMING_SNAKE_CASE : Any = self.model(**lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = { '''candidate_labels''': candidate_labels, '''logits''': outputs.logits_per_audio, } return model_outputs def __lowerCAmelCase ( self :str , lowerCamelCase_ :Any ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = model_outputs.pop('''candidate_labels''' ) SCREAMING_SNAKE_CASE : List[str] = model_outputs['''logits'''][0] if self.framework == "pt": SCREAMING_SNAKE_CASE : Dict = logits.softmax(dim=0 ) SCREAMING_SNAKE_CASE : Optional[int] = probs.tolist() else: raise ValueError('''`tf` framework not supported.''' ) SCREAMING_SNAKE_CASE : str = [ {'''score''': score, '''label''': candidate_label} for score, candidate_label in sorted(zip(lowerCamelCase_ , lowerCamelCase_ ) , key=lambda lowerCamelCase_ : -x[0] ) ] return result
698
"""simple docstring""" import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert_fast import BertTokenizerFast from .tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer, DPRReaderTokenizer lowerCamelCase__ : Union[str, Any] = logging.get_logger(__name__) lowerCamelCase__ : Union[str, Any] = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} lowerCamelCase__ : Any = { "vocab_file": { "facebook/dpr-ctx_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt" ), "facebook/dpr-ctx_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "facebook/dpr-ctx_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json" ), "facebook/dpr-ctx_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json" ), }, } lowerCamelCase__ : str = { "vocab_file": { "facebook/dpr-question_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt" ), "facebook/dpr-question_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "facebook/dpr-question_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json" ), "facebook/dpr-question_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json" ), }, } lowerCamelCase__ : Any = { "vocab_file": { "facebook/dpr-reader-single-nq-base": ( "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt" ), "facebook/dpr-reader-multiset-base": ( "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "facebook/dpr-reader-single-nq-base": ( "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json" ), "facebook/dpr-reader-multiset-base": ( "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json" ), }, } lowerCamelCase__ : Optional[Any] = { "facebook/dpr-ctx_encoder-single-nq-base": 512, "facebook/dpr-ctx_encoder-multiset-base": 512, } lowerCamelCase__ : Tuple = { "facebook/dpr-question_encoder-single-nq-base": 512, "facebook/dpr-question_encoder-multiset-base": 512, } lowerCamelCase__ : Dict = { "facebook/dpr-reader-single-nq-base": 512, "facebook/dpr-reader-multiset-base": 512, } lowerCamelCase__ : int = { "facebook/dpr-ctx_encoder-single-nq-base": {"do_lower_case": True}, "facebook/dpr-ctx_encoder-multiset-base": {"do_lower_case": True}, } lowerCamelCase__ : Tuple = { "facebook/dpr-question_encoder-single-nq-base": {"do_lower_case": True}, "facebook/dpr-question_encoder-multiset-base": {"do_lower_case": True}, } lowerCamelCase__ : Dict = { "facebook/dpr-reader-single-nq-base": {"do_lower_case": True}, "facebook/dpr-reader-multiset-base": {"do_lower_case": True}, } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = VOCAB_FILES_NAMES UpperCamelCase = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase = DPRContextEncoderTokenizer class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = VOCAB_FILES_NAMES UpperCamelCase = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase = DPRQuestionEncoderTokenizer lowerCamelCase__ : Union[str, Any] = collections.namedtuple( "DPRSpanPrediction", ["span_score", "relevance_score", "doc_id", "start_index", "end_index", "text"] ) lowerCamelCase__ : int = collections.namedtuple("DPRReaderOutput", ["start_logits", "end_logits", "relevance_logits"]) lowerCamelCase__ : str = r"\n Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`.\n It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers),\n using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)`\n with the format:\n\n [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids>\n\n Args:\n questions (`str` or `List[str]`):\n The questions to be encoded. You can specify one question for many passages. In this case, the question\n will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in\n `titles` or `texts`.\n titles (`str` or `List[str]`):\n The passages titles to be encoded. This can be a string or a list of strings if there are several passages.\n texts (`str` or `List[str]`):\n The passages texts to be encoded. This can be a string or a list of strings if there are several passages.\n padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`):\n Activates and controls padding. Accepts the following values:\n\n - `True` or `'longest'`: Pad to the longest sequence in the batch (or no padding if only a single sequence\n if provided).\n - `'max_length'`: Pad to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided.\n - `False` or `'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of different\n lengths).\n truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`):\n Activates and controls truncation. Accepts the following values:\n\n - `True` or `'longest_first'`: Truncate to a maximum length specified with the argument `max_length` or to\n the maximum acceptable input length for the model if that argument is not provided. This will truncate\n token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch\n of pairs) is provided.\n - `'only_first'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the first\n sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `'only_second'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the\n second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `False` or `'do_not_truncate'` (default): No truncation (i.e., can output batch with sequence lengths\n greater than the model maximum admissible input size).\n max_length (`int`, *optional*):\n Controls the maximum length to use by one of the truncation/padding parameters.\n\n If left unset or set to `None`, this will use the predefined model maximum length if a maximum length\n is required by one of the truncation/padding parameters. If the model has no specific maximum input\n length (like XLNet) truncation/padding to a maximum length will be deactivated.\n return_tensors (`str` or [`~utils.TensorType`], *optional*):\n If set, will return tensors instead of list of python integers. Acceptable values are:\n\n - `'tf'`: Return TensorFlow `tf.constant` objects.\n - `'pt'`: Return PyTorch `torch.Tensor` objects.\n - `'np'`: Return Numpy `np.ndarray` objects.\n return_attention_mask (`bool`, *optional*):\n Whether or not to return the attention mask. If not set, will return the attention mask according to the\n specific tokenizer's default, defined by the `return_outputs` attribute.\n\n [What are attention masks?](../glossary#attention-mask)\n\n Return:\n `Dict[str, List[List[int]]]`: A dictionary with the following keys:\n\n - `input_ids`: List of token ids to be fed to a model.\n - `attention_mask`: List of indices specifying which tokens should be attended to by the model.\n " @add_start_docstrings(_UpperCAmelCase ) class lowercase__: '''simple docstring''' def __call__( self :str , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Optional[str] = None , lowerCamelCase_ :Optional[str] = None , lowerCamelCase_ :Union[bool, str] = False , lowerCamelCase_ :Union[bool, str] = False , lowerCamelCase_ :Optional[int] = None , lowerCamelCase_ :Optional[Union[str, TensorType]] = None , lowerCamelCase_ :Optional[bool] = None , **lowerCamelCase_ :Tuple , ) -> BatchEncoding: '''simple docstring''' if titles is None and texts is None: return super().__call__( lowerCamelCase_ , padding=lowerCamelCase_ , truncation=lowerCamelCase_ , max_length=lowerCamelCase_ , return_tensors=lowerCamelCase_ , return_attention_mask=lowerCamelCase_ , **lowerCamelCase_ , ) elif titles is None or texts is None: SCREAMING_SNAKE_CASE : List[str] = titles if texts is None else texts return super().__call__( lowerCamelCase_ , lowerCamelCase_ , padding=lowerCamelCase_ , truncation=lowerCamelCase_ , max_length=lowerCamelCase_ , return_tensors=lowerCamelCase_ , return_attention_mask=lowerCamelCase_ , **lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Dict = titles if not isinstance(lowerCamelCase_ , lowerCamelCase_ ) else [titles] SCREAMING_SNAKE_CASE : Dict = texts if not isinstance(lowerCamelCase_ , lowerCamelCase_ ) else [texts] SCREAMING_SNAKE_CASE : Optional[int] = len(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = questions if not isinstance(lowerCamelCase_ , lowerCamelCase_ ) else [questions] * n_passages assert len(lowerCamelCase_ ) == len( lowerCamelCase_ ), f"There should be as many titles than texts but got {len(lowerCamelCase_ )} titles and {len(lowerCamelCase_ )} texts." SCREAMING_SNAKE_CASE : Any = super().__call__(lowerCamelCase_ , lowerCamelCase_ , padding=lowerCamelCase_ , truncation=lowerCamelCase_ )['''input_ids'''] SCREAMING_SNAKE_CASE : Dict = super().__call__(lowerCamelCase_ , add_special_tokens=lowerCamelCase_ , padding=lowerCamelCase_ , truncation=lowerCamelCase_ )['''input_ids'''] SCREAMING_SNAKE_CASE : int = { '''input_ids''': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(lowerCamelCase_ , lowerCamelCase_ ) ] } if return_attention_mask is not False: SCREAMING_SNAKE_CASE : List[str] = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) SCREAMING_SNAKE_CASE : int = attention_mask return self.pad(lowerCamelCase_ , padding=lowerCamelCase_ , max_length=lowerCamelCase_ , return_tensors=lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :BatchEncoding , lowerCamelCase_ :DPRReaderOutput , lowerCamelCase_ :int = 16 , lowerCamelCase_ :int = 64 , lowerCamelCase_ :int = 4 , ) -> List[DPRSpanPrediction]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = reader_input['''input_ids'''] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = reader_output[:3] SCREAMING_SNAKE_CASE : Dict = len(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = sorted(range(lowerCamelCase_ ) , reverse=lowerCamelCase_ , key=relevance_logits.__getitem__ ) SCREAMING_SNAKE_CASE : List[DPRReaderOutput] = [] for doc_id in sorted_docs: SCREAMING_SNAKE_CASE : Union[str, Any] = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence SCREAMING_SNAKE_CASE : int = sequence_ids.index(self.sep_token_id , 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: SCREAMING_SNAKE_CASE : Dict = sequence_ids.index(self.pad_token_id ) else: SCREAMING_SNAKE_CASE : Optional[int] = len(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=lowerCamelCase_ , top_spans=lowerCamelCase_ , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=lowerCamelCase_ , start_index=lowerCamelCase_ , end_index=lowerCamelCase_ , text=self.decode(sequence_ids[start_index : end_index + 1] ) , ) ) if len(lowerCamelCase_ ) >= num_spans: break return nbest_spans_predictions[:num_spans] def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :List[int] , lowerCamelCase_ :List[int] , lowerCamelCase_ :int , lowerCamelCase_ :int , ) -> List[DPRSpanPrediction]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = [] for start_index, start_score in enumerate(lowerCamelCase_ ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) SCREAMING_SNAKE_CASE : Dict = sorted(lowerCamelCase_ , key=lambda lowerCamelCase_ : x[1] , reverse=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = [] for (start_index, end_index), score in scores: assert start_index <= end_index, f"Wrong span indices: [{start_index}:{end_index}]" SCREAMING_SNAKE_CASE : Optional[int] = end_index - start_index + 1 assert length <= max_answer_length, f"Span is too long: {length} > {max_answer_length}" if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(lowerCamelCase_ ) == top_spans: break return chosen_span_intervals @add_end_docstrings(_UpperCAmelCase ) class lowercase__( _UpperCAmelCase , _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = VOCAB_FILES_NAMES UpperCamelCase = READER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase = READER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase = ["""input_ids""", """attention_mask"""] UpperCamelCase = DPRReaderTokenizer
698
1
"""simple docstring""" def __A ( a_ : list )-> list: '''simple docstring''' for i in range(len(a_ ) - 1 , 0 , -1 ): SCREAMING_SNAKE_CASE : List[Any] = False for j in range(a_ , 0 , -1 ): if unsorted[j] < unsorted[j - 1]: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[Any] = unsorted[j - 1], unsorted[j] SCREAMING_SNAKE_CASE : Dict = True for j in range(a_ ): if unsorted[j] > unsorted[j + 1]: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Tuple = unsorted[j + 1], unsorted[j] SCREAMING_SNAKE_CASE : Tuple = True if not swapped: break return unsorted if __name__ == "__main__": import doctest doctest.testmod() lowerCamelCase__ : List[str] = input("Enter numbers separated by a comma:\n").strip() lowerCamelCase__ : Optional[Any] = [int(item) for item in user_input.split(",")] print(f'''{cocktail_shaker_sort(unsorted) = }''')
698
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : Optional[Any] = { "microsoft/markuplm-base": "https://huggingface.co/microsoft/markuplm-base/resolve/main/config.json", "microsoft/markuplm-large": "https://huggingface.co/microsoft/markuplm-large/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """markuplm""" def __init__( self :int , lowerCamelCase_ :List[str]=3_05_22 , lowerCamelCase_ :Union[str, Any]=7_68 , lowerCamelCase_ :str=12 , lowerCamelCase_ :Dict=12 , lowerCamelCase_ :str=30_72 , lowerCamelCase_ :Union[str, Any]="gelu" , lowerCamelCase_ :Union[str, Any]=0.1 , lowerCamelCase_ :Optional[Any]=0.1 , lowerCamelCase_ :Union[str, Any]=5_12 , lowerCamelCase_ :Any=2 , lowerCamelCase_ :Optional[Any]=0.0_2 , lowerCamelCase_ :Any=1E-12 , lowerCamelCase_ :Dict=0 , lowerCamelCase_ :Optional[Any]=0 , lowerCamelCase_ :Optional[int]=2 , lowerCamelCase_ :str=2_56 , lowerCamelCase_ :List[Any]=10_24 , lowerCamelCase_ :Union[str, Any]=2_16 , lowerCamelCase_ :Dict=10_01 , lowerCamelCase_ :Any=32 , lowerCamelCase_ :str=50 , lowerCamelCase_ :List[str]="absolute" , lowerCamelCase_ :List[str]=True , lowerCamelCase_ :int=None , **lowerCamelCase_ :Dict , ) -> List[Any]: '''simple docstring''' super().__init__( pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , **lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : str = vocab_size SCREAMING_SNAKE_CASE : Optional[int] = hidden_size SCREAMING_SNAKE_CASE : int = num_hidden_layers SCREAMING_SNAKE_CASE : List[str] = num_attention_heads SCREAMING_SNAKE_CASE : List[str] = hidden_act SCREAMING_SNAKE_CASE : int = intermediate_size SCREAMING_SNAKE_CASE : Optional[int] = hidden_dropout_prob SCREAMING_SNAKE_CASE : List[str] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = max_position_embeddings SCREAMING_SNAKE_CASE : Tuple = type_vocab_size SCREAMING_SNAKE_CASE : Any = initializer_range SCREAMING_SNAKE_CASE : int = layer_norm_eps SCREAMING_SNAKE_CASE : int = position_embedding_type SCREAMING_SNAKE_CASE : Tuple = use_cache SCREAMING_SNAKE_CASE : str = classifier_dropout # additional properties SCREAMING_SNAKE_CASE : Optional[Any] = max_depth SCREAMING_SNAKE_CASE : Dict = max_xpath_tag_unit_embeddings SCREAMING_SNAKE_CASE : Optional[int] = max_xpath_subs_unit_embeddings SCREAMING_SNAKE_CASE : Tuple = tag_pad_id SCREAMING_SNAKE_CASE : str = subs_pad_id SCREAMING_SNAKE_CASE : List[Any] = xpath_unit_hidden_size
698
1
"""simple docstring""" import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = ["""image_processor""", """tokenizer"""] UpperCamelCase = """ViTImageProcessor""" UpperCamelCase = ("""CLIPTokenizer""", """CLIPTokenizerFast""") def __init__( self :str , lowerCamelCase_ :int=None , lowerCamelCase_ :str=None , **lowerCamelCase_ :Optional[int] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : 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.''' , lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Optional[int] = kwargs.pop('''feature_extractor''' ) SCREAMING_SNAKE_CASE : 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 :str , lowerCamelCase_ :Union[str, Any]=None , lowerCamelCase_ :List[Any]=None , lowerCamelCase_ :Any=None , lowerCamelCase_ :Optional[Any]=None , **lowerCamelCase_ :Optional[int] ) -> int: '''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: SCREAMING_SNAKE_CASE : Optional[int] = self.tokenizer(lowerCamelCase_ , return_tensors=lowerCamelCase_ , **lowerCamelCase_ ) if visual_prompt is not None: SCREAMING_SNAKE_CASE : Tuple = self.image_processor(lowerCamelCase_ , return_tensors=lowerCamelCase_ , **lowerCamelCase_ ) if images is not None: SCREAMING_SNAKE_CASE : int = self.image_processor(lowerCamelCase_ , return_tensors=lowerCamelCase_ , **lowerCamelCase_ ) if visual_prompt is not None and images is not None: SCREAMING_SNAKE_CASE : Union[str, Any] = { '''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: SCREAMING_SNAKE_CASE : List[str] = image_features.pixel_values return encoding elif text is not None: return encoding elif visual_prompt is not None: SCREAMING_SNAKE_CASE : Tuple = { '''conditional_pixel_values''': prompt_features.pixel_values, } return encoding else: return BatchEncoding(data=dict(**lowerCamelCase_ ) , tensor_type=lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[int] , *lowerCamelCase_ :int , **lowerCamelCase_ :Any ) -> Dict: '''simple docstring''' return self.tokenizer.batch_decode(*lowerCamelCase_ , **lowerCamelCase_ ) def __lowerCAmelCase ( self :List[str] , *lowerCamelCase_ :List[Any] , **lowerCamelCase_ :Union[str, Any] ) -> List[Any]: '''simple docstring''' return self.tokenizer.decode(*lowerCamelCase_ , **lowerCamelCase_ ) @property def __lowerCAmelCase ( self :int ) -> str: '''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 __lowerCAmelCase ( self :str ) -> Tuple: '''simple docstring''' warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , lowerCamelCase_ , ) return self.image_processor
698
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCamelCase__ : Optional[Any] = logging.get_logger(__name__) lowerCamelCase__ : Union[str, Any] = { "microsoft/resnet-50": "https://huggingface.co/microsoft/resnet-50/blob/main/config.json", } class lowercase__( _UpperCAmelCase , _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """resnet""" UpperCamelCase = ["""basic""", """bottleneck"""] def __init__( self :Optional[int] , lowerCamelCase_ :Tuple=3 , lowerCamelCase_ :Tuple=64 , lowerCamelCase_ :Union[str, Any]=[2_56, 5_12, 10_24, 20_48] , lowerCamelCase_ :int=[3, 4, 6, 3] , lowerCamelCase_ :Any="bottleneck" , lowerCamelCase_ :Optional[int]="relu" , lowerCamelCase_ :Optional[int]=False , lowerCamelCase_ :Any=None , lowerCamelCase_ :Optional[int]=None , **lowerCamelCase_ :Optional[int] , ) -> Tuple: '''simple docstring''' super().__init__(**lowerCamelCase_ ) if layer_type not in self.layer_types: raise ValueError(f"layer_type={layer_type} is not one of {','.join(self.layer_types )}" ) SCREAMING_SNAKE_CASE : Tuple = num_channels SCREAMING_SNAKE_CASE : Union[str, Any] = embedding_size SCREAMING_SNAKE_CASE : List[str] = hidden_sizes SCREAMING_SNAKE_CASE : Optional[Any] = depths SCREAMING_SNAKE_CASE : List[Any] = layer_type SCREAMING_SNAKE_CASE : str = hidden_act SCREAMING_SNAKE_CASE : Optional[Any] = downsample_in_first_stage SCREAMING_SNAKE_CASE : int = ['''stem'''] + [f"stage{idx}" for idx in range(1 , len(lowerCamelCase_ ) + 1 )] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = get_aligned_output_features_output_indices( out_features=lowerCamelCase_ , out_indices=lowerCamelCase_ , stage_names=self.stage_names ) class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = version.parse("""1.11""" ) @property def __lowerCAmelCase ( self :Optional[Any] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def __lowerCAmelCase ( self :str ) -> float: '''simple docstring''' return 1E-3
698
1
"""simple docstring""" from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast from ...utils import logging lowerCamelCase__ : Any = logging.get_logger(__name__) lowerCamelCase__ : Dict = { "EleutherAI/gpt-neo-1.3B": "https://huggingface.co/EleutherAI/gpt-neo-1.3B/resolve/main/config.json", # See all GPTNeo models at https://huggingface.co/models?filter=gpt_neo } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """gpt_neo""" UpperCamelCase = ["""past_key_values"""] UpperCamelCase = {"""num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers"""} def __init__( self :Any , lowerCamelCase_ :List[str]=5_02_57 , lowerCamelCase_ :Optional[Any]=20_48 , lowerCamelCase_ :Any=20_48 , lowerCamelCase_ :Union[str, Any]=24 , lowerCamelCase_ :str=[[["global", "local"], 12]] , lowerCamelCase_ :Optional[Any]=16 , lowerCamelCase_ :Union[str, Any]=None , lowerCamelCase_ :List[Any]=2_56 , lowerCamelCase_ :str="gelu_new" , lowerCamelCase_ :Optional[int]=0.0 , lowerCamelCase_ :List[Any]=0.0 , lowerCamelCase_ :List[str]=0.0 , lowerCamelCase_ :Tuple=0.1 , lowerCamelCase_ :int=1E-5 , lowerCamelCase_ :Any=0.0_2 , lowerCamelCase_ :int=True , lowerCamelCase_ :Any=5_02_56 , lowerCamelCase_ :Dict=5_02_56 , **lowerCamelCase_ :Any , ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = vocab_size SCREAMING_SNAKE_CASE : Any = max_position_embeddings SCREAMING_SNAKE_CASE : Dict = hidden_size SCREAMING_SNAKE_CASE : str = num_layers SCREAMING_SNAKE_CASE : Union[str, Any] = num_heads SCREAMING_SNAKE_CASE : Dict = intermediate_size SCREAMING_SNAKE_CASE : Optional[int] = window_size SCREAMING_SNAKE_CASE : Any = activation_function SCREAMING_SNAKE_CASE : Dict = resid_dropout SCREAMING_SNAKE_CASE : Union[str, Any] = embed_dropout SCREAMING_SNAKE_CASE : List[Any] = attention_dropout SCREAMING_SNAKE_CASE : Dict = classifier_dropout SCREAMING_SNAKE_CASE : List[str] = layer_norm_epsilon SCREAMING_SNAKE_CASE : str = initializer_range SCREAMING_SNAKE_CASE : List[Any] = use_cache SCREAMING_SNAKE_CASE : Optional[int] = bos_token_id SCREAMING_SNAKE_CASE : List[str] = eos_token_id SCREAMING_SNAKE_CASE : Dict = attention_types SCREAMING_SNAKE_CASE : List[Any] = self.expand_attention_types_params(lowerCamelCase_ ) if len(self.attention_layers ) != self.num_layers: raise ValueError( '''Configuration for convolutional module is incorrect. ''' '''It is required that `len(config.attention_layers)` == `config.num_layers` ''' f"but is `len(config.attention_layers) = {len(self.attention_layers )}`, " f"`config.num_layers = {self.num_layers}`. " '''`config.attention_layers` is prepared using `config.attention_types`. ''' '''Please verify the value of `config.attention_types` argument.''' ) super().__init__(bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , **lowerCamelCase_ ) @staticmethod def __lowerCAmelCase ( lowerCamelCase_ :Any ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = [] for item in attention_types: for _ in range(item[1] ): attentions.extend(item[0] ) return attentions def __A ( a_ : List[Any] , a_ : Any , a_ : List[str] , a_ : Optional[int] )-> Union[str, Any]: '''simple docstring''' import torch SCREAMING_SNAKE_CASE : List[str] = input.size() SCREAMING_SNAKE_CASE : Any = len(a_ ) SCREAMING_SNAKE_CASE : int = shape[dimension] SCREAMING_SNAKE_CASE : Union[str, Any] = torch.arange(0 , a_ , a_ ) SCREAMING_SNAKE_CASE : List[Any] = torch.div(sizedim - size , a_ , rounding_mode='''floor''' ) + 1 SCREAMING_SNAKE_CASE : List[str] = torch.arange(a_ ) + low_indices[:min_length][:, None] SCREAMING_SNAKE_CASE : Union[str, Any] = [slice(a_ )] * rank SCREAMING_SNAKE_CASE : List[str] = indices SCREAMING_SNAKE_CASE : List[str] = input[s] SCREAMING_SNAKE_CASE : Any = list(range(0 , rank + 1 ) ) perm.append(perm.pop(dimension + 1 ) ) return sliced.permute(a_ ) def __A ( a_ : Optional[int] , a_ : List[Any] )-> Optional[Any]: '''simple docstring''' import torch SCREAMING_SNAKE_CASE : Optional[int] = torch.arange(1 , a_ ) SCREAMING_SNAKE_CASE : Dict = torch.remainder(a_ , a_ ) SCREAMING_SNAKE_CASE : Any = remainders == 0 SCREAMING_SNAKE_CASE : int = candidates[divisor_indices] SCREAMING_SNAKE_CASE : int = torch.max(a_ ) return largest_divisor, torch.div(a_ , a_ , rounding_mode='''floor''' ) class lowercase__( _UpperCAmelCase ): '''simple docstring''' @property def __lowerCAmelCase ( self :Tuple ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}} ) if self.use_past: self.fill_with_past_key_values_(lowerCamelCase_ , direction='''inputs''' ) SCREAMING_SNAKE_CASE : str = {0: '''batch''', 1: '''past_sequence + sequence'''} else: SCREAMING_SNAKE_CASE : Optional[Any] = {0: '''batch''', 1: '''sequence'''} return common_inputs @property def __lowerCAmelCase ( self :List[Any] ) -> int: '''simple docstring''' return self._config.num_heads def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :PreTrainedTokenizer , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[TensorType] = None , ) -> Mapping[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = super(lowerCamelCase_ , self ).generate_dummy_inputs( lowerCamelCase_ , batch_size=lowerCamelCase_ , seq_length=lowerCamelCase_ , is_pair=lowerCamelCase_ , framework=lowerCamelCase_ ) # We need to order the input in the way they appears in the forward() SCREAMING_SNAKE_CASE : int = OrderedDict({'''input_ids''': common_inputs['''input_ids''']} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = common_inputs['''input_ids'''].shape # Not using the same length for past_key_values SCREAMING_SNAKE_CASE : int = seqlen + 2 SCREAMING_SNAKE_CASE : List[str] = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) SCREAMING_SNAKE_CASE : Optional[Any] = [ (torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ )) for _ in range(self.num_layers ) ] SCREAMING_SNAKE_CASE : Union[str, Any] = common_inputs['''attention_mask'''] if self.use_past: SCREAMING_SNAKE_CASE : Optional[int] = ordered_inputs['''attention_mask'''].dtype SCREAMING_SNAKE_CASE : Any = torch.cat( [ordered_inputs['''attention_mask'''], torch.ones(lowerCamelCase_ , lowerCamelCase_ , dtype=lowerCamelCase_ )] , dim=1 ) return ordered_inputs @property def __lowerCAmelCase ( self :Optional[int] ) -> int: '''simple docstring''' return 13
698
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : List[Any] = { "uw-madison/mra-base-512-4": "https://huggingface.co/uw-madison/mra-base-512-4/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """mra""" def __init__( self :int , lowerCamelCase_ :Optional[int]=5_02_65 , lowerCamelCase_ :List[str]=7_68 , lowerCamelCase_ :List[str]=12 , lowerCamelCase_ :Optional[Any]=12 , lowerCamelCase_ :int=30_72 , lowerCamelCase_ :Tuple="gelu" , lowerCamelCase_ :List[Any]=0.1 , lowerCamelCase_ :str=0.1 , lowerCamelCase_ :str=5_12 , lowerCamelCase_ :List[str]=1 , lowerCamelCase_ :int=0.0_2 , lowerCamelCase_ :int=1E-5 , lowerCamelCase_ :List[Any]="absolute" , lowerCamelCase_ :str=4 , lowerCamelCase_ :List[str]="full" , lowerCamelCase_ :List[Any]=0 , lowerCamelCase_ :Optional[Any]=0 , lowerCamelCase_ :Union[str, Any]=1 , lowerCamelCase_ :List[str]=0 , lowerCamelCase_ :List[Any]=2 , **lowerCamelCase_ :str , ) -> Dict: '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = vocab_size SCREAMING_SNAKE_CASE : Tuple = max_position_embeddings SCREAMING_SNAKE_CASE : List[Any] = hidden_size SCREAMING_SNAKE_CASE : Dict = num_hidden_layers SCREAMING_SNAKE_CASE : Tuple = num_attention_heads SCREAMING_SNAKE_CASE : Any = intermediate_size SCREAMING_SNAKE_CASE : Any = hidden_act SCREAMING_SNAKE_CASE : Tuple = hidden_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : str = initializer_range SCREAMING_SNAKE_CASE : Tuple = type_vocab_size SCREAMING_SNAKE_CASE : Union[str, Any] = layer_norm_eps SCREAMING_SNAKE_CASE : str = position_embedding_type SCREAMING_SNAKE_CASE : List[str] = block_per_row SCREAMING_SNAKE_CASE : Optional[int] = approx_mode SCREAMING_SNAKE_CASE : List[Any] = initial_prior_first_n_blocks SCREAMING_SNAKE_CASE : Union[str, Any] = initial_prior_diagonal_n_blocks
698
1
"""simple docstring""" from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxSeqaSeqConfigWithPast from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : Optional[int] = { "t5-small": "https://huggingface.co/t5-small/resolve/main/config.json", "t5-base": "https://huggingface.co/t5-base/resolve/main/config.json", "t5-large": "https://huggingface.co/t5-large/resolve/main/config.json", "t5-3b": "https://huggingface.co/t5-3b/resolve/main/config.json", "t5-11b": "https://huggingface.co/t5-11b/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """t5""" UpperCamelCase = ["""past_key_values"""] UpperCamelCase = {"""hidden_size""": """d_model""", """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers"""} def __init__( self :Any , lowerCamelCase_ :str=3_21_28 , lowerCamelCase_ :Optional[int]=5_12 , lowerCamelCase_ :int=64 , lowerCamelCase_ :str=20_48 , lowerCamelCase_ :List[str]=6 , lowerCamelCase_ :Any=None , lowerCamelCase_ :List[Any]=8 , lowerCamelCase_ :Optional[int]=32 , lowerCamelCase_ :Tuple=1_28 , lowerCamelCase_ :Optional[Any]=0.1 , lowerCamelCase_ :Optional[int]=1E-6 , lowerCamelCase_ :List[str]=1.0 , lowerCamelCase_ :str="relu" , lowerCamelCase_ :Optional[int]=True , lowerCamelCase_ :Dict=True , lowerCamelCase_ :List[Any]=0 , lowerCamelCase_ :Optional[int]=1 , **lowerCamelCase_ :Any , ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = vocab_size SCREAMING_SNAKE_CASE : Optional[Any] = d_model SCREAMING_SNAKE_CASE : Any = d_kv SCREAMING_SNAKE_CASE : Optional[Any] = d_ff SCREAMING_SNAKE_CASE : Any = num_layers SCREAMING_SNAKE_CASE : str = ( num_decoder_layers if num_decoder_layers is not None else self.num_layers ) # default = symmetry SCREAMING_SNAKE_CASE : int = num_heads SCREAMING_SNAKE_CASE : Dict = relative_attention_num_buckets SCREAMING_SNAKE_CASE : int = relative_attention_max_distance SCREAMING_SNAKE_CASE : Tuple = dropout_rate SCREAMING_SNAKE_CASE : Dict = layer_norm_epsilon SCREAMING_SNAKE_CASE : Dict = initializer_factor SCREAMING_SNAKE_CASE : List[Any] = feed_forward_proj SCREAMING_SNAKE_CASE : Optional[int] = use_cache SCREAMING_SNAKE_CASE : Optional[int] = self.feed_forward_proj.split('''-''' ) SCREAMING_SNAKE_CASE : int = act_info[-1] SCREAMING_SNAKE_CASE : Optional[int] = 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": SCREAMING_SNAKE_CASE : Dict = '''gelu_new''' super().__init__( pad_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , is_encoder_decoder=lowerCamelCase_ , **lowerCamelCase_ , ) class lowercase__( _UpperCAmelCase ): '''simple docstring''' @property def __lowerCAmelCase ( self :Optional[int] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = { '''input_ids''': {0: '''batch''', 1: '''encoder_sequence'''}, '''attention_mask''': {0: '''batch''', 1: '''encoder_sequence'''}, } if self.use_past: SCREAMING_SNAKE_CASE : List[Any] = '''past_encoder_sequence + sequence''' SCREAMING_SNAKE_CASE : Optional[int] = {0: '''batch'''} SCREAMING_SNAKE_CASE : Optional[Any] = {0: '''batch''', 1: '''past_decoder_sequence + sequence'''} else: SCREAMING_SNAKE_CASE : int = {0: '''batch''', 1: '''decoder_sequence'''} SCREAMING_SNAKE_CASE : Dict = {0: '''batch''', 1: '''decoder_sequence'''} if self.use_past: self.fill_with_past_key_values_(lowerCamelCase_ , direction='''inputs''' ) return common_inputs @property def __lowerCAmelCase ( self :Dict ) -> int: '''simple docstring''' return 13
698
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : str = logging.get_logger(__name__) lowerCamelCase__ : List[str] = { "facebook/nllb-moe-54B": "https://huggingface.co/facebook/nllb-moe-54b/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """nllb-moe""" UpperCamelCase = ["""past_key_values"""] UpperCamelCase = {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""} def __init__( self :List[str] , lowerCamelCase_ :Optional[int]=12_81_12 , lowerCamelCase_ :str=10_24 , lowerCamelCase_ :Any=12 , lowerCamelCase_ :Optional[int]=40_96 , lowerCamelCase_ :int=16 , lowerCamelCase_ :List[str]=12 , lowerCamelCase_ :Optional[int]=40_96 , lowerCamelCase_ :int=16 , lowerCamelCase_ :Union[str, Any]=0.0_5 , lowerCamelCase_ :Optional[int]=0.0_5 , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :Optional[Any]=True , lowerCamelCase_ :Tuple="relu" , lowerCamelCase_ :str=10_24 , lowerCamelCase_ :str=0.1 , lowerCamelCase_ :Optional[int]=0.1 , lowerCamelCase_ :List[str]=0.0 , lowerCamelCase_ :Optional[Any]=0.0_2 , lowerCamelCase_ :Optional[int]=2 , lowerCamelCase_ :Dict=True , lowerCamelCase_ :Any=False , lowerCamelCase_ :Optional[Any]="float32" , lowerCamelCase_ :Optional[Any]=False , lowerCamelCase_ :List[Any]=1_28 , lowerCamelCase_ :Any=64 , lowerCamelCase_ :Optional[int]=4 , lowerCamelCase_ :List[str]=4 , lowerCamelCase_ :Union[str, Any]=0.0_0_1 , lowerCamelCase_ :Optional[int]=0.0_0_1 , lowerCamelCase_ :List[str]="all" , lowerCamelCase_ :Optional[int]=False , lowerCamelCase_ :Any=False , lowerCamelCase_ :Tuple=1.0 , lowerCamelCase_ :Union[str, Any]=0.2 , lowerCamelCase_ :List[str]=1 , lowerCamelCase_ :Optional[int]=0 , lowerCamelCase_ :int=2 , lowerCamelCase_ :List[str]=False , **lowerCamelCase_ :int , ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : int = vocab_size SCREAMING_SNAKE_CASE : str = max_position_embeddings SCREAMING_SNAKE_CASE : str = d_model SCREAMING_SNAKE_CASE : Optional[int] = encoder_ffn_dim SCREAMING_SNAKE_CASE : Any = encoder_layers SCREAMING_SNAKE_CASE : Any = encoder_attention_heads SCREAMING_SNAKE_CASE : List[Any] = decoder_ffn_dim SCREAMING_SNAKE_CASE : str = decoder_layers SCREAMING_SNAKE_CASE : List[Any] = decoder_attention_heads SCREAMING_SNAKE_CASE : List[Any] = dropout SCREAMING_SNAKE_CASE : List[str] = attention_dropout SCREAMING_SNAKE_CASE : str = activation_dropout SCREAMING_SNAKE_CASE : Any = activation_function SCREAMING_SNAKE_CASE : Tuple = init_std SCREAMING_SNAKE_CASE : str = encoder_layerdrop SCREAMING_SNAKE_CASE : Union[str, Any] = decoder_layerdrop SCREAMING_SNAKE_CASE : List[Any] = use_cache SCREAMING_SNAKE_CASE : Optional[int] = encoder_layers SCREAMING_SNAKE_CASE : List[str] = scale_embedding # scale factor will be sqrt(d_model) if True SCREAMING_SNAKE_CASE : int = router_z_loss_coef SCREAMING_SNAKE_CASE : Any = router_aux_loss_coef SCREAMING_SNAKE_CASE : str = decoder_sparse_step SCREAMING_SNAKE_CASE : str = encoder_sparse_step SCREAMING_SNAKE_CASE : List[str] = num_experts SCREAMING_SNAKE_CASE : Union[str, Any] = expert_capacity SCREAMING_SNAKE_CASE : Tuple = router_bias if router_dtype not in ["float32", "float16", "bfloat16"]: raise ValueError(f"`router_dtype` must be one of 'float32', 'float16' or 'bfloat16', got {router_dtype}" ) SCREAMING_SNAKE_CASE : Union[str, Any] = router_dtype SCREAMING_SNAKE_CASE : Union[str, Any] = router_ignore_padding_tokens SCREAMING_SNAKE_CASE : int = batch_prioritized_routing SCREAMING_SNAKE_CASE : Optional[int] = second_expert_policy SCREAMING_SNAKE_CASE : Union[str, Any] = normalize_router_prob_before_dropping SCREAMING_SNAKE_CASE : Any = moe_eval_capacity_token_fraction SCREAMING_SNAKE_CASE : Optional[Any] = moe_token_dropout SCREAMING_SNAKE_CASE : Tuple = output_router_logits super().__init__( pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , is_encoder_decoder=lowerCamelCase_ , decoder_start_token_id=lowerCamelCase_ , **lowerCamelCase_ , )
698
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCamelCase__ : List[Any] = { "configuration_bigbird_pegasus": [ "BIGBIRD_PEGASUS_PRETRAINED_CONFIG_ARCHIVE_MAP", "BigBirdPegasusConfig", "BigBirdPegasusOnnxConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Union[str, Any] = [ "BIGBIRD_PEGASUS_PRETRAINED_MODEL_ARCHIVE_LIST", "BigBirdPegasusForCausalLM", "BigBirdPegasusForConditionalGeneration", "BigBirdPegasusForQuestionAnswering", "BigBirdPegasusForSequenceClassification", "BigBirdPegasusModel", "BigBirdPegasusPreTrainedModel", ] if TYPE_CHECKING: from .configuration_bigbird_pegasus import ( BIGBIRD_PEGASUS_PRETRAINED_CONFIG_ARCHIVE_MAP, BigBirdPegasusConfig, BigBirdPegasusOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bigbird_pegasus import ( BIGBIRD_PEGASUS_PRETRAINED_MODEL_ARCHIVE_LIST, BigBirdPegasusForCausalLM, BigBirdPegasusForConditionalGeneration, BigBirdPegasusForQuestionAnswering, BigBirdPegasusForSequenceClassification, BigBirdPegasusModel, BigBirdPegasusPreTrainedModel, ) else: import sys lowerCamelCase__ : Dict = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
698
"""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 lowerCamelCase__ : Union[str, Any] = "CompVis/stable-diffusion-v1-1" lowerCamelCase__ : Optional[Any] = "CompVis/stable-diffusion-v1-2" lowerCamelCase__ : Dict = "CompVis/stable-diffusion-v1-3" lowerCamelCase__ : List[str] = "CompVis/stable-diffusion-v1-4" class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __init__( self :Any , lowerCamelCase_ :AutoencoderKL , lowerCamelCase_ :CLIPTextModel , lowerCamelCase_ :CLIPTokenizer , lowerCamelCase_ :UNetaDConditionModel , lowerCamelCase_ :Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , lowerCamelCase_ :StableDiffusionSafetyChecker , lowerCamelCase_ :CLIPImageProcessor , lowerCamelCase_ :bool = True , ) -> List[str]: '''simple docstring''' super()._init_() SCREAMING_SNAKE_CASE : Tuple = StableDiffusionPipeline.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = StableDiffusionPipeline.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = StableDiffusionPipeline.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = StableDiffusionPipeline( vae=lowerCamelCase_ , text_encoder=lowerCamelCase_ , tokenizer=lowerCamelCase_ , unet=lowerCamelCase_ , scheduler=lowerCamelCase_ , safety_checker=lowerCamelCase_ , feature_extractor=lowerCamelCase_ , requires_safety_checker=lowerCamelCase_ , ) self.register_modules(pipelinea=self.pipea , pipelinea=self.pipea , pipelinea=self.pipea , pipelinea=self.pipea ) @property def __lowerCAmelCase ( self :Dict ) -> Dict[str, Any]: '''simple docstring''' return {k: getattr(self , lowerCamelCase_ ) for k in self.config.keys() if not k.startswith('''_''' )} def __lowerCAmelCase ( self :int , lowerCamelCase_ :Optional[Union[str, int]] = "auto" ) -> Tuple: '''simple docstring''' if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory SCREAMING_SNAKE_CASE : str = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(lowerCamelCase_ ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Dict: '''simple docstring''' self.enable_attention_slicing(lowerCamelCase_ ) @torch.no_grad() def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :List[str] , ) -> Tuple: '''simple docstring''' return self.pipea( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) @torch.no_grad() def __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :Tuple , ) -> Optional[Any]: '''simple docstring''' return self.pipea( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) @torch.no_grad() def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :Dict , ) -> List[str]: '''simple docstring''' return self.pipea( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) @torch.no_grad() def __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :List[Any] , ) -> Optional[Any]: '''simple docstring''' return self.pipea( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) @torch.no_grad() def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :Optional[Any] , ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = '''cuda''' if torch.cuda.is_available() else '''cpu''' self.to(lowerCamelCase_ ) # 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 SCREAMING_SNAKE_CASE : str = self.textaimg_sda_a( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) # Get first result from Stable Diffusion Checkpoint v1.2 SCREAMING_SNAKE_CASE : Optional[Any] = self.textaimg_sda_a( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) # Get first result from Stable Diffusion Checkpoint v1.3 SCREAMING_SNAKE_CASE : Tuple = self.textaimg_sda_a( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) # Get first result from Stable Diffusion Checkpoint v1.4 SCREAMING_SNAKE_CASE : Union[str, Any] = self.textaimg_sda_a( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) # 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]] )
698
1
"""simple docstring""" import os import pytest from datasets import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, ) lowerCamelCase__ : Tuple = pytest.mark.integration @pytest.mark.parametrize('''path''' , ['''paws''', '''csv'''] ) def __A ( a_ : Tuple , a_ : Optional[int] )-> List[Any]: '''simple docstring''' inspect_dataset(a_ , a_ ) SCREAMING_SNAKE_CASE : Optional[Any] = path + '''.py''' assert script_name in os.listdir(a_ ) assert "__pycache__" not in os.listdir(a_ ) @pytest.mark.filterwarnings('''ignore:inspect_metric is deprecated:FutureWarning''' ) @pytest.mark.filterwarnings('''ignore:metric_module_factory is deprecated:FutureWarning''' ) @pytest.mark.parametrize('''path''' , ['''accuracy'''] ) def __A ( a_ : Tuple , a_ : Optional[int] )-> str: '''simple docstring''' inspect_metric(a_ , a_ ) SCREAMING_SNAKE_CASE : Optional[Any] = path + '''.py''' assert script_name in os.listdir(a_ ) assert "__pycache__" not in os.listdir(a_ ) @pytest.mark.parametrize( '''path, config_name, expected_splits''' , [ ('''squad''', '''plain_text''', ['''train''', '''validation''']), ('''dalle-mini/wit''', '''dalle-mini--wit''', ['''train''']), ('''paws''', '''labeled_final''', ['''train''', '''test''', '''validation''']), ] , ) def __A ( a_ : Dict , a_ : Union[str, Any] , a_ : Tuple )-> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = get_dataset_config_info(a_ , config_name=a_ ) assert info.config_name == config_name assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( '''path, config_name, expected_exception''' , [ ('''paws''', None, ValueError), ] , ) def __A ( a_ : Any , a_ : str , a_ : List[str] )-> Tuple: '''simple docstring''' with pytest.raises(a_ ): get_dataset_config_info(a_ , config_name=a_ ) @pytest.mark.parametrize( '''path, expected''' , [ ('''squad''', '''plain_text'''), ('''acronym_identification''', '''default'''), ('''lhoestq/squad''', '''plain_text'''), ('''lhoestq/test''', '''default'''), ('''lhoestq/demo1''', '''lhoestq--demo1'''), ('''dalle-mini/wit''', '''dalle-mini--wit'''), ] , ) def __A ( a_ : List[str] , a_ : Optional[Any] )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = get_dataset_config_names(a_ ) assert expected in config_names @pytest.mark.parametrize( '''path, expected_configs, expected_splits_in_first_config''' , [ ('''squad''', ['''plain_text'''], ['''train''', '''validation''']), ('''dalle-mini/wit''', ['''dalle-mini--wit'''], ['''train''']), ('''paws''', ['''labeled_final''', '''labeled_swap''', '''unlabeled_final'''], ['''train''', '''test''', '''validation''']), ] , ) def __A ( a_ : Any , a_ : str , a_ : Optional[Any] )-> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = get_dataset_infos(a_ ) assert list(infos.keys() ) == expected_configs SCREAMING_SNAKE_CASE : List[Any] = expected_configs[0] assert expected_config in infos SCREAMING_SNAKE_CASE : List[str] = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits_in_first_config @pytest.mark.parametrize( '''path, expected_config, expected_splits''' , [ ('''squad''', '''plain_text''', ['''train''', '''validation''']), ('''dalle-mini/wit''', '''dalle-mini--wit''', ['''train''']), ('''paws''', '''labeled_final''', ['''train''', '''test''', '''validation''']), ] , ) def __A ( a_ : int , a_ : Dict , a_ : str )-> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = get_dataset_infos(a_ ) assert expected_config in infos SCREAMING_SNAKE_CASE : Union[str, Any] = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( '''path, config_name, expected_exception''' , [ ('''paws''', None, ValueError), ] , ) def __A ( a_ : List[str] , a_ : List[Any] , a_ : Union[str, Any] )-> Dict: '''simple docstring''' with pytest.raises(a_ ): get_dataset_split_names(a_ , config_name=a_ )
698
"""simple docstring""" def __A ( a_ : list , a_ : int = 0 )-> list: '''simple docstring''' SCREAMING_SNAKE_CASE : int = length or len(a_ ) SCREAMING_SNAKE_CASE : List[Any] = False for i in range(length - 1 ): if list_data[i] > list_data[i + 1]: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[Any] = list_data[i + 1], list_data[i] SCREAMING_SNAKE_CASE : Optional[Any] = True return list_data if not swapped else bubble_sort(a_ , length - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
698
1
"""simple docstring""" import logging import os import random import sys from dataclasses import dataclass, field from typing import Optional import datasets import numpy as np import pandas as pd from datasets import load_dataset import transformers from transformers import ( AutoConfig, BartForSequenceClassification, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, TapexTokenizer, Trainer, TrainingArguments, default_data_collator, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version from transformers.utils.versions import require_version # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.17.0.dev0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/text-classification/requirements.txt") lowerCamelCase__ : Any = logging.getLogger(__name__) @dataclass class lowercase__: '''simple docstring''' UpperCamelCase = field( default="""tab_fact""" , metadata={"""help""": """The name of the dataset to use (via the datasets library)."""} ) UpperCamelCase = field( default="""tab_fact""" , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} , ) UpperCamelCase = field( default=10_24 , metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Overwrite the cached preprocessed datasets or not."""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """Whether to pad all samples to `max_seq_length`. """ """If False, will pad the samples dynamically when batching to the maximum length in the batch.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of evaluation examples to this """ """value if set.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of prediction examples to this """ """value if set.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """A csv or a json file containing the training data."""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """A csv or a json file containing the validation data."""} ) UpperCamelCase = field(default=_UpperCAmelCase , metadata={"""help""": """A csv or a json file containing the test data."""} ) def __lowerCAmelCase ( self :List[Any] ) -> Any: '''simple docstring''' if self.dataset_name is not None: pass elif self.train_file is None or self.validation_file is None: raise ValueError('''Need either a GLUE task, a training/validation file or a dataset name.''' ) else: SCREAMING_SNAKE_CASE : Union[str, Any] = self.train_file.split('''.''' )[-1] assert train_extension in ["csv", "json"], "`train_file` should be a csv or a json file." SCREAMING_SNAKE_CASE : Any = self.validation_file.split('''.''' )[-1] assert ( validation_extension == train_extension ), "`validation_file` should have the same extension (csv or json) as `train_file`." @dataclass class lowercase__: '''simple docstring''' UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."""} , ) UpperCamelCase = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) def __A ( )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('''.json''' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = parser.parse_args_into_dataclasses() # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' , datefmt='''%m/%d/%Y %H:%M:%S''' , handlers=[logging.StreamHandler(sys.stdout )] , ) SCREAMING_SNAKE_CASE : List[str] = training_args.get_process_log_level() logger.setLevel(a_ ) datasets.utils.logging.set_verbosity(a_ ) transformers.utils.logging.set_verbosity(a_ ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}" + F"distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}" ) logger.info(F"Training/evaluation parameters {training_args}" ) # Detecting last checkpoint. SCREAMING_SNAKE_CASE : Dict = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: SCREAMING_SNAKE_CASE : str = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F"Output directory ({training_args.output_dir}) already exists and is not empty. " '''Use --overwrite_output_dir to overcome.''' ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change " '''the `--output_dir` or add `--overwrite_output_dir` to train from scratch.''' ) # Set seed before initializing model. set_seed(training_args.seed ) # Get the datasets: you can either provide your own CSV/JSON training and evaluation files (see below) # or specify a GLUE benchmark task (the dataset will be downloaded automatically from the datasets Hub). # # For JSON files, this script will use the `question` column for the input question and `table` column for the corresponding table. # # If the CSVs/JSONs contain only one non-label column, the script does single sentence classification on this # single column. You can easily tweak this behavior (see below) # # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.dataset_name is not None: # Downloading and loading a dataset from the hub. SCREAMING_SNAKE_CASE : List[Any] = load_dataset( data_args.dataset_name , data_args.dataset_config_name , cache_dir=model_args.cache_dir ) else: # Loading a dataset from your local files. # CSV/JSON training and evaluation files are needed. SCREAMING_SNAKE_CASE : str = {'''train''': data_args.train_file, '''validation''': data_args.validation_file} # Get the test dataset: you can provide your own CSV/JSON test file (see below) # when you use `do_predict` without specifying a GLUE benchmark task. if training_args.do_predict: if data_args.test_file is not None: SCREAMING_SNAKE_CASE : List[str] = data_args.train_file.split('''.''' )[-1] SCREAMING_SNAKE_CASE : Union[str, Any] = data_args.test_file.split('''.''' )[-1] assert ( test_extension == train_extension ), "`test_file` should have the same extension (csv or json) as `train_file`." SCREAMING_SNAKE_CASE : List[str] = data_args.test_file else: raise ValueError('''Need either a GLUE task or a test file for `do_predict`.''' ) for key in data_files.keys(): logger.info(F"load a local file for {key}: {data_files[key]}" ) if data_args.train_file.endswith('''.csv''' ): # Loading a dataset from local csv files SCREAMING_SNAKE_CASE : Any = load_dataset('''csv''' , data_files=a_ , cache_dir=model_args.cache_dir ) else: # Loading a dataset from local json files SCREAMING_SNAKE_CASE : Dict = load_dataset('''json''' , data_files=a_ , cache_dir=model_args.cache_dir ) # See more about loading any type of standard or custom dataset at # https://huggingface.co/docs/datasets/loading_datasets.html. # Labels SCREAMING_SNAKE_CASE : List[Any] = raw_datasets['''train'''].features['''label'''].names SCREAMING_SNAKE_CASE : Optional[Any] = len(a_ ) # Load pretrained model and tokenizer # # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. SCREAMING_SNAKE_CASE : Optional[int] = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=a_ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # load tapex tokenizer SCREAMING_SNAKE_CASE : Tuple = TapexTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , use_fast=model_args.use_fast_tokenizer , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , add_prefix_space=a_ , ) SCREAMING_SNAKE_CASE : str = BartForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=a_ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # Padding strategy if data_args.pad_to_max_length: SCREAMING_SNAKE_CASE : List[Any] = '''max_length''' else: # We will pad later, dynamically at batch creation, to the max sequence length in each batch SCREAMING_SNAKE_CASE : Tuple = False # Some models have set the order of the labels to use, so let's make sure we do use it. SCREAMING_SNAKE_CASE : str = {'''Refused''': 0, '''Entailed''': 1} SCREAMING_SNAKE_CASE : str = {0: '''Refused''', 1: '''Entailed'''} if data_args.max_seq_length > tokenizer.model_max_length: logger.warning( F"The max_seq_length passed ({data_args.max_seq_length}) is larger than the maximum length for the" F"model ({tokenizer.model_max_length}). Using max_seq_length={tokenizer.model_max_length}." ) SCREAMING_SNAKE_CASE : Any = min(data_args.max_seq_length , tokenizer.model_max_length ) def preprocess_tabfact_function(a_ : List[str] ): # Tokenize the texts def _convert_table_text_to_pandas(a_ : List[str] ): SCREAMING_SNAKE_CASE : List[Any] = [_table_row.split('''#''' ) for _table_row in _table_text.strip('''\n''' ).split('''\n''' )] SCREAMING_SNAKE_CASE : Optional[int] = pd.DataFrame.from_records(_table_content[1:] , columns=_table_content[0] ) return _table_pd SCREAMING_SNAKE_CASE : Tuple = examples['''statement'''] SCREAMING_SNAKE_CASE : Any = list(map(_convert_table_text_to_pandas , examples['''table_text'''] ) ) SCREAMING_SNAKE_CASE : int = tokenizer(a_ , a_ , padding=a_ , max_length=a_ , truncation=a_ ) SCREAMING_SNAKE_CASE : Optional[Any] = examples['''label'''] return result with training_args.main_process_first(desc='''dataset map pre-processing''' ): SCREAMING_SNAKE_CASE : Optional[int] = raw_datasets.map( a_ , batched=a_ , load_from_cache_file=not data_args.overwrite_cache , desc='''Running tokenizer on dataset''' , ) if training_args.do_train: if "train" not in raw_datasets: raise ValueError('''--do_train requires a train dataset''' ) SCREAMING_SNAKE_CASE : int = raw_datasets['''train'''] if data_args.max_train_samples is not None: SCREAMING_SNAKE_CASE : Optional[int] = train_dataset.select(range(data_args.max_train_samples ) ) if training_args.do_eval: if "validation" not in raw_datasets and "validation_matched" not in raw_datasets: raise ValueError('''--do_eval requires a validation dataset''' ) SCREAMING_SNAKE_CASE : List[str] = raw_datasets['''validation'''] if data_args.max_eval_samples is not None: SCREAMING_SNAKE_CASE : str = eval_dataset.select(range(data_args.max_eval_samples ) ) if training_args.do_predict or data_args.test_file is not None: if "test" not in raw_datasets and "test_matched" not in raw_datasets: raise ValueError('''--do_predict requires a test dataset''' ) SCREAMING_SNAKE_CASE : str = raw_datasets['''test'''] if data_args.max_predict_samples is not None: SCREAMING_SNAKE_CASE : int = predict_dataset.select(range(data_args.max_predict_samples ) ) # Log a few random samples from the training set: if training_args.do_train: for index in random.sample(range(len(a_ ) ) , 3 ): logger.info(F"Sample {index} of the training set: {train_dataset[index]}." ) # You can define your custom compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a # predictions and label_ids field) and has to return a dictionary string to float. def compute_metrics(a_ : EvalPrediction ): SCREAMING_SNAKE_CASE : List[str] = p.predictions[0] if isinstance(p.predictions , a_ ) else p.predictions SCREAMING_SNAKE_CASE : str = np.argmax(a_ , axis=1 ) return {"accuracy": (preds == p.label_ids).astype(np.floataa ).mean().item()} # Data collator will default to DataCollatorWithPadding, so we change it if we already did the padding. if data_args.pad_to_max_length: SCREAMING_SNAKE_CASE : Dict = default_data_collator elif training_args.fpaa: SCREAMING_SNAKE_CASE : Union[str, Any] = DataCollatorWithPadding(a_ , pad_to_multiple_of=8 ) else: SCREAMING_SNAKE_CASE : Any = None # Initialize our Trainer SCREAMING_SNAKE_CASE : str = Trainer( model=a_ , args=a_ , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , compute_metrics=a_ , tokenizer=a_ , data_collator=a_ , ) # Training if training_args.do_train: SCREAMING_SNAKE_CASE : Union[str, Any] = None if training_args.resume_from_checkpoint is not None: SCREAMING_SNAKE_CASE : Any = training_args.resume_from_checkpoint elif last_checkpoint is not None: SCREAMING_SNAKE_CASE : Tuple = last_checkpoint SCREAMING_SNAKE_CASE : Optional[int] = trainer.train(resume_from_checkpoint=a_ ) SCREAMING_SNAKE_CASE : Dict = train_result.metrics SCREAMING_SNAKE_CASE : List[Any] = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(a_ ) ) SCREAMING_SNAKE_CASE : Optional[int] = min(a_ , len(a_ ) ) trainer.save_model() # Saves the tokenizer too for easy upload trainer.log_metrics('''train''' , a_ ) trainer.save_metrics('''train''' , a_ ) trainer.save_state() # Evaluation if training_args.do_eval: logger.info('''*** Evaluate ***''' ) SCREAMING_SNAKE_CASE : Optional[Any] = trainer.evaluate(eval_dataset=a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(a_ ) SCREAMING_SNAKE_CASE : Any = min(a_ , len(a_ ) ) trainer.log_metrics('''eval''' , a_ ) trainer.save_metrics('''eval''' , a_ ) if training_args.do_predict: logger.info('''*** Predict ***''' ) # Removing the `label` columns because it contains -1 and Trainer won't like that. SCREAMING_SNAKE_CASE : Any = predict_dataset.remove_columns('''label''' ) SCREAMING_SNAKE_CASE : Optional[Any] = trainer.predict(a_ , metric_key_prefix='''predict''' ).predictions SCREAMING_SNAKE_CASE : List[str] = np.argmax(a_ , axis=1 ) SCREAMING_SNAKE_CASE : List[str] = os.path.join(training_args.output_dir , '''predict_results_tabfact.txt''' ) if trainer.is_world_process_zero(): with open(a_ , '''w''' ) as writer: logger.info('''***** Predict Results *****''' ) writer.write('''index\tprediction\n''' ) for index, item in enumerate(a_ ): SCREAMING_SNAKE_CASE : Optional[Any] = label_list[item] writer.write(F"{index}\t{item}\n" ) SCREAMING_SNAKE_CASE : int = {'''finetuned_from''': model_args.model_name_or_path, '''tasks''': '''text-classification'''} if training_args.push_to_hub: trainer.push_to_hub(**a_ ) else: trainer.create_model_card(**a_ ) def __A ( a_ : str )-> Union[str, Any]: '''simple docstring''' main() if __name__ == "__main__": main()
698
"""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 lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = 42 UpperCamelCase = 42 def __init__( self :List[str] , lowerCamelCase_ :UNetaDModel , lowerCamelCase_ :ScoreSdeVeScheduler ) -> int: '''simple docstring''' super().__init__() self.register_modules(unet=lowerCamelCase_ , scheduler=lowerCamelCase_ ) @torch.no_grad() def __call__( self :int , lowerCamelCase_ :int = 1 , lowerCamelCase_ :int = 20_00 , lowerCamelCase_ :Optional[Union[torch.Generator, List[torch.Generator]]] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , **lowerCamelCase_ :Union[str, Any] , ) -> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.unet.config.sample_size SCREAMING_SNAKE_CASE : List[str] = (batch_size, 3, img_size, img_size) SCREAMING_SNAKE_CASE : Any = self.unet SCREAMING_SNAKE_CASE : Dict = randn_tensor(lowerCamelCase_ , generator=lowerCamelCase_ ) * self.scheduler.init_noise_sigma SCREAMING_SNAKE_CASE : Union[str, Any] = sample.to(self.device ) self.scheduler.set_timesteps(lowerCamelCase_ ) self.scheduler.set_sigmas(lowerCamelCase_ ) for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): SCREAMING_SNAKE_CASE : Tuple = self.scheduler.sigmas[i] * torch.ones(shape[0] , device=self.device ) # correction step for _ in range(self.scheduler.config.correct_steps ): SCREAMING_SNAKE_CASE : Optional[Any] = self.unet(lowerCamelCase_ , lowerCamelCase_ ).sample SCREAMING_SNAKE_CASE : List[Any] = self.scheduler.step_correct(lowerCamelCase_ , lowerCamelCase_ , generator=lowerCamelCase_ ).prev_sample # prediction step SCREAMING_SNAKE_CASE : Any = model(lowerCamelCase_ , lowerCamelCase_ ).sample SCREAMING_SNAKE_CASE : Optional[Any] = self.scheduler.step_pred(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , generator=lowerCamelCase_ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Union[str, Any] = output.prev_sample, output.prev_sample_mean SCREAMING_SNAKE_CASE : List[str] = sample_mean.clamp(0 , 1 ) SCREAMING_SNAKE_CASE : Any = sample.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": SCREAMING_SNAKE_CASE : Any = self.numpy_to_pil(lowerCamelCase_ ) if not return_dict: return (sample,) return ImagePipelineOutput(images=lowerCamelCase_ )
698
1
"""simple docstring""" import argparse import fairseq import torch from torch import nn from transformers import ( MBartaaTokenizer, MBartConfig, MBartForCausalLM, SpeechEncoderDecoderConfig, SpeechEncoderDecoderModel, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaModel, logging, ) logging.set_verbosity_info() lowerCamelCase__ : List[Any] = logging.get_logger(__name__) lowerCamelCase__ : Tuple = { "post_extract_proj": "feature_projection.projection", "encoder.pos_conv.0": "encoder.pos_conv_embed.conv", "self_attn.k_proj": "encoder.layers.*.attention.k_proj", "self_attn.v_proj": "encoder.layers.*.attention.v_proj", "self_attn.q_proj": "encoder.layers.*.attention.q_proj", "self_attn.out_proj": "encoder.layers.*.attention.out_proj", "self_attn_layer_norm": "encoder.layers.*.layer_norm", "fc1": "encoder.layers.*.feed_forward.intermediate_dense", "fc2": "encoder.layers.*.feed_forward.output_dense", "final_layer_norm": "encoder.layers.*.final_layer_norm", "encoder.layer_norm": "encoder.layer_norm", "w2v_model.layer_norm": "feature_projection.layer_norm", "quantizer.weight_proj": "quantizer.weight_proj", "quantizer.vars": "quantizer.codevectors", "project_q": "project_q", "final_proj": "project_hid", "w2v_encoder.proj": "lm_head", "mask_emb": "masked_spec_embed", } lowerCamelCase__ : List[str] = [ "lm_head", "quantizer.weight_proj", "quantizer.codevectors", "project_q", "project_hid", ] def __A ( a_ : Optional[int] , a_ : str , a_ : str , a_ : str , a_ : List[str] )-> Tuple: '''simple docstring''' for attribute in key.split('''.''' ): SCREAMING_SNAKE_CASE : Any = getattr(a_ , a_ ) if weight_type is not None: SCREAMING_SNAKE_CASE : Optional[int] = getattr(a_ , a_ ).shape else: SCREAMING_SNAKE_CASE : Any = hf_pointer.shape assert hf_shape == value.shape, ( F"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be" F" {value.shape} for {full_name}" ) if weight_type == "weight": SCREAMING_SNAKE_CASE : List[Any] = value elif weight_type == "weight_g": SCREAMING_SNAKE_CASE : Optional[int] = value elif weight_type == "weight_v": SCREAMING_SNAKE_CASE : Any = value elif weight_type == "bias": SCREAMING_SNAKE_CASE : List[Any] = value else: SCREAMING_SNAKE_CASE : List[str] = value logger.info(F"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." ) def __A ( a_ : Optional[Any] , a_ : Dict )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = [] SCREAMING_SNAKE_CASE : Optional[Any] = fairseq_model.state_dict() SCREAMING_SNAKE_CASE : Tuple = hf_model.feature_extractor SCREAMING_SNAKE_CASE : Tuple = hf_model.adapter for name, value in fairseq_dict.items(): SCREAMING_SNAKE_CASE : int = False if "conv_layers" in name: load_conv_layer( a_ , a_ , a_ , a_ , hf_model.config.feat_extract_norm == '''group''' , ) SCREAMING_SNAKE_CASE : List[str] = True elif any(x in name for x in ['''adaptor''', '''w2v_encoder.proj.''', '''w2v_proj_ln.'''] ): load_adapter(a_ , a_ , a_ , a_ ) SCREAMING_SNAKE_CASE : List[Any] = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split('''w2v_model.''' )[-1] == name.split('''.''' )[0]: SCREAMING_SNAKE_CASE : Union[str, Any] = True if "*" in mapped_key: SCREAMING_SNAKE_CASE : Dict = name.split(a_ )[0].split('''.''' )[-2] SCREAMING_SNAKE_CASE : Optional[int] = mapped_key.replace('''*''' , a_ ) if "weight_g" in name: SCREAMING_SNAKE_CASE : List[str] = '''weight_g''' elif "weight_v" in name: SCREAMING_SNAKE_CASE : Union[str, Any] = '''weight_v''' elif "bias" in name: SCREAMING_SNAKE_CASE : str = '''bias''' elif "weight" in name: SCREAMING_SNAKE_CASE : Tuple = '''weight''' else: SCREAMING_SNAKE_CASE : str = None set_recursively(a_ , a_ , a_ , a_ , a_ ) continue if not is_used: unused_weights.append(a_ ) logger.warning(F"Unused weights: {unused_weights}" ) def __A ( a_ : Dict , a_ : int , a_ : Optional[int] , a_ : Optional[int] , a_ : Dict )-> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = full_name.split('''conv_layers.''' )[-1] SCREAMING_SNAKE_CASE : List[str] = name.split('''.''' ) SCREAMING_SNAKE_CASE : Dict = int(items[0] ) SCREAMING_SNAKE_CASE : Optional[Any] = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found." ) SCREAMING_SNAKE_CASE : List[Any] = value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found." ) SCREAMING_SNAKE_CASE : str = value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( F"{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was" " found." ) SCREAMING_SNAKE_CASE : str = value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found." ) SCREAMING_SNAKE_CASE : Union[str, Any] = value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) else: unused_weights.append(a_ ) def __A ( a_ : Optional[int] , a_ : Optional[int] , a_ : Any , a_ : Any )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = full_name.split('''adaptor.''' )[-1] SCREAMING_SNAKE_CASE : List[Any] = name.split('''.''' ) if items[1].isdigit(): SCREAMING_SNAKE_CASE : List[Any] = int(items[1] ) else: SCREAMING_SNAKE_CASE : str = None if "adaptor" not in full_name: if "proj_ln" in full_name: # has to be layer norm if "bias" in name: assert ( value.shape == adapter.proj_layer_norm.bias.data.shape ), F"{full_name} has size {value.shape}, but {adapter.proj_layer_norm.bias.data.shape} was found." SCREAMING_SNAKE_CASE : str = value logger.info(F"Adapter proj layer norm bias was initialized from {full_name}." ) if "weight" in name: assert ( value.shape == adapter.proj_layer_norm.weight.data.shape ), F"{full_name} has size {value.shape}, but {adapter.proj_layer_norm.weight.data.shape} was found." SCREAMING_SNAKE_CASE : Optional[Any] = value else: # has to be projection layer if "bias" in name: assert ( value.shape == adapter.proj.bias.data.shape ), F"{full_name} has size {value.shape}, but {adapter.proj.bias.data.shape} was found." SCREAMING_SNAKE_CASE : Union[str, Any] = value logger.info(F"Adapter proj layer bias was initialized from {full_name}." ) if "weight" in name: assert ( value.shape == adapter.proj.weight.data.shape ), F"{full_name} has size {value.shape}, but {adapter.proj.weight.data.shape} was found." SCREAMING_SNAKE_CASE : int = value logger.info(F"Adapter proj layer weight was initialized from {full_name}." ) elif isinstance(a_ , a_ ): if "bias" in name: assert ( value.shape == adapter.layers[layer_id].conv.bias.data.shape ), F"{full_name} has size {value.shape}, but {adapter.layers[layer_id].conv.bias.data.shape} was found." SCREAMING_SNAKE_CASE : str = value logger.info(F"Adapter layer {layer_id} bias was initialized from {full_name}." ) elif "weight" in name: assert ( value.shape == adapter.layers[layer_id].conv.weight.data.shape ), F"{full_name} has size {value.shape}, but {adapter.layers[layer_id].conv.weight.data.shape} was found." SCREAMING_SNAKE_CASE : List[str] = value logger.info(F"Adapter layer {layer_id} bias was initialized from {full_name}." ) else: unused_weights.append(a_ ) def __A ( a_ : Optional[Any] )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = emb.weight.shape SCREAMING_SNAKE_CASE : Any = nn.Linear(a_ , a_ , bias=a_ ) SCREAMING_SNAKE_CASE : Optional[int] = emb.weight.data return lin_layer @torch.no_grad() def __A ( a_ : Tuple , a_ : Optional[int] , a_ : List[Any] , a_ : Any , a_ : Tuple , a_ : int , a_ : Any , a_ : str , a_ : Tuple , a_ : Union[str, Any] , a_ : Union[str, Any] , )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = WavaVecaConfig.from_pretrained( a_ , add_adapter=a_ , adapter_stride=a_ , adapter_kernel_size=a_ , use_auth_token=a_ , output_hidden_size=a_ , ) SCREAMING_SNAKE_CASE : Union[str, Any] = MBartConfig.from_pretrained(a_ ) # load model SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={ '''config_yaml''': config_yaml_path, '''data''': '''/'''.join(dict_path.split('''/''' )[:-1] ), '''w2v_path''': checkpoint_path, '''load_pretrained_decoder_from''': None, } , ) SCREAMING_SNAKE_CASE : int = model[0].eval() # load feature extractor SCREAMING_SNAKE_CASE : Union[str, Any] = WavaVecaFeatureExtractor.from_pretrained(a_ , use_auth_token=a_ ) # set weights for wav2vec2 encoder SCREAMING_SNAKE_CASE : str = WavaVecaModel(a_ ) recursively_load_weights_wavaveca(model.encoder , a_ ) # load decoder weights SCREAMING_SNAKE_CASE : Dict = MBartForCausalLM(a_ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict() , strict=a_ ) logger.warning(F"The following keys are missing when loading the decoder weights: {missing_keys}" ) logger.warning(F"The following keys are unexpected when loading the decoder weights: {unexpected_keys}" ) SCREAMING_SNAKE_CASE : Union[str, Any] = SpeechEncoderDecoderModel(encoder=a_ , decoder=a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = False SCREAMING_SNAKE_CASE : Optional[Any] = MBartaaTokenizer(a_ ) tokenizer.save_pretrained(a_ ) SCREAMING_SNAKE_CASE : Tuple = hf_wavavec.config.to_dict() SCREAMING_SNAKE_CASE : Any = tokenizer.pad_token_id SCREAMING_SNAKE_CASE : List[str] = tokenizer.bos_token_id SCREAMING_SNAKE_CASE : Dict = tokenizer.eos_token_id SCREAMING_SNAKE_CASE : Optional[Any] = '''mbart50''' SCREAMING_SNAKE_CASE : Optional[int] = '''wav2vec2''' SCREAMING_SNAKE_CASE : Optional[Any] = tokenizer.eos_token_id SCREAMING_SNAKE_CASE : List[str] = 25_00_04 SCREAMING_SNAKE_CASE : Dict = tokenizer.eos_token_id SCREAMING_SNAKE_CASE : Any = SpeechEncoderDecoderConfig.from_dict(a_ ) hf_wavavec.save_pretrained(a_ ) feature_extractor.save_pretrained(a_ ) if __name__ == "__main__": lowerCamelCase__ : Any = argparse.ArgumentParser() parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to fairseq checkpoint") parser.add_argument("--dict_path", default=None, type=str, help="Path to dict of fine-tuned model") parser.add_argument("--config_yaml_path", default=None, type=str, help="Path to yaml file of fine-tuned model") parser.add_argument( "--encoder_config_path", default="facebook/wav2vec2-xls-r-1b", type=str, help="Path to hf encoder wav2vec2 checkpoint config", ) parser.add_argument( "--decoder_config_path", default="facebook/mbart-large-50-one-to-many-mmt", type=str, help="Path to hf decoder checkpoint config", ) parser.add_argument("--add_adapter", default=True, type=bool, help="whethere to add model adapter layers") parser.add_argument("--adapter_stride", default=2, type=int, help="stride of adapter layers") parser.add_argument("--adapter_kernel_size", default=3, type=int, help="kernel size of adapter layers") parser.add_argument("--encoder_output_dim", default=1024, type=int, help="encoder output dim") parser.add_argument("--start_token_id", default=250004, type=int, help="`decoder_start_token_id` of model config") lowerCamelCase__ : Dict = parser.parse_args() convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.dict_path, args.config_yaml_path, encoder_config_path=args.encoder_config_path, decoder_config_path=args.decoder_config_path, add_adapter=args.add_adapter, adapter_kernel_size=args.adapter_kernel_size, adapter_stride=args.adapter_stride, decoder_start_token_id=args.start_token_id, encoder_output_dim=args.encoder_output_dim, )
698
"""simple docstring""" import qiskit def __A ( a_ : int , a_ : int )-> qiskit.result.counts.Counts: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = qiskit.Aer.get_backend('''aer_simulator''' ) # Create a Quantum Circuit acting on the q register SCREAMING_SNAKE_CASE : str = qiskit.QuantumCircuit(a_ , a_ ) # Apply X (NOT) Gate to Qubits 0 & 1 circuit.x(0 ) circuit.x(1 ) # Map the quantum measurement to the classical bits circuit.measure([0, 1] , [0, 1] ) # Execute the circuit on the qasm simulator SCREAMING_SNAKE_CASE : int = qiskit.execute(a_ , a_ , shots=10_00 ) # Return the histogram data of the results of the experiment. return job.result().get_counts(a_ ) if __name__ == "__main__": lowerCamelCase__ : List[Any] = single_qubit_measure(2, 2) print(f'''Total count for various states are: {counts}''')
698
1
"""simple docstring""" import argparse import json import os import torch from torch import nn from transformers import NllbMoeConfig, NllbMoeModel from transformers.modeling_utils import dtype_byte_size from transformers.utils import WEIGHTS_INDEX_NAME, WEIGHTS_NAME def __A ( a_ : str )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = [ '''encoder.version''', '''decoder.version''', '''model.encoder.version''', '''model.decoder.version''', '''decoder.output_projection.weight''', '''_float_tensor''', '''encoder.embed_positions._float_tensor''', '''decoder.embed_positions._float_tensor''', ] for k in ignore_keys: state_dict.pop(a_ , a_ ) def __A ( a_ : str )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[Any] = emb.weight.shape SCREAMING_SNAKE_CASE : Optional[Any] = nn.Linear(a_ , a_ , bias=a_ ) SCREAMING_SNAKE_CASE : Optional[int] = emb.weight.data return lin_layer def __A ( a_ : int , a_ : Dict=None )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = {} for old_key in state_dict.keys(): SCREAMING_SNAKE_CASE : Optional[Any] = old_key if "moe_layer.experts." in key: if expert_idx is not None: SCREAMING_SNAKE_CASE : Dict = key.replace('''moe_layer.experts.0''' , F"ffn.experts.expert_{expert_idx}" ) else: SCREAMING_SNAKE_CASE : Dict = key.replace('''moe_layer.experts.''' , '''ffn.experts.expert_''' ) if "gate" in key: SCREAMING_SNAKE_CASE : List[str] = key.replace('''.moe_layer.gate.wg''' , '''.ffn.router.classifier''' ) if "fc2" and "experts" not in key: SCREAMING_SNAKE_CASE : Optional[int] = key.replace('''.fc2.''' , '''.ffn.fc2.''' ) if "fc1" and "experts" not in key: SCREAMING_SNAKE_CASE : List[str] = key.replace('''.fc1.''' , '''.ffn.fc1.''' ) if ".encoder_attn." in key: SCREAMING_SNAKE_CASE : int = key.replace('''.encoder_attn.''' , '''.cross_attention.''' ) if "encoder_attn_layer_norm" in key: SCREAMING_SNAKE_CASE : int = key.replace('''encoder_attn_layer_norm''' , '''cross_attention_layer_norm''' ) if "final_layer_norm" in key: SCREAMING_SNAKE_CASE : List[Any] = key.replace('''final_layer_norm''' , '''ff_layer_norm''' ) SCREAMING_SNAKE_CASE : Dict = state_dict[old_key] return new_dict def __A ( a_ : int , a_ : List[Any] , a_ : Any , a_ : str , a_ : str = WEIGHTS_NAME )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = [] SCREAMING_SNAKE_CASE : Union[str, Any] = 0 os.makedirs(a_ , exist_ok=a_ ) for expert in range(a_ ): SCREAMING_SNAKE_CASE : Union[str, Any] = switch_checkpoint_path + F"-rank-{expert}.pt" if os.path.isfile(a_ ): SCREAMING_SNAKE_CASE : List[str] = torch.load(a_ )['''model'''] remove_ignore_keys_(a_ ) SCREAMING_SNAKE_CASE : str = rename_fairseq_keys(a_ , a_ ) SCREAMING_SNAKE_CASE : List[Any] = os.path.join( a_ , weights_name.replace('''.bin''' , F"-{len(a_ )+1:05d}-of-???.bin" ) ) torch.save(a_ , a_ ) sharded_state_dicts.append(expert_state.keys() ) total_size += sum([value.numel() for key, value in expert_state.items()] ) * dtype_byte_size( expert_state[list(a_ )[0]].dtype ) # Add the last block SCREAMING_SNAKE_CASE : Optional[int] = os.path.join(a_ , weights_name.replace('''.bin''' , F"-{len(a_ )+1:05d}-of-???.bin" ) ) SCREAMING_SNAKE_CASE : Dict = torch.load(switch_checkpoint_path + '''-shared.pt''' )['''model'''] remove_ignore_keys_(a_ ) SCREAMING_SNAKE_CASE : Any = rename_fairseq_keys(a_ , a_ ) SCREAMING_SNAKE_CASE : List[str] = shared_weights['''decoder.embed_tokens.weight'''] sharded_state_dicts.append(shared_weights.keys() ) # If we only have the shared weights (dummy model/experts saved on the same file) if len(a_ ) == 1: SCREAMING_SNAKE_CASE : Union[str, Any] = os.path.join(a_ , a_ ) torch.save(a_ , a_ ) return {weights_name: sharded_state_dicts[0]}, None else: torch.save(a_ , a_ ) # Otherwise, let's build the index SCREAMING_SNAKE_CASE : Any = {} for idx, shard in enumerate(a_ ): SCREAMING_SNAKE_CASE : Any = weights_name.replace('''.bin''' , F"-{idx+1:05d}-of-{len(a_ ):05d}.bin" ) SCREAMING_SNAKE_CASE : Union[str, Any] = os.path.join(a_ , weights_name.replace('''.bin''' , F"-{idx+1:05d}-of-???.bin" ) ) os.rename(a_ , os.path.join(a_ , a_ ) ) for key in shard: SCREAMING_SNAKE_CASE : str = shard_file # Add the metadata SCREAMING_SNAKE_CASE : List[Any] = {'''total_size''': total_size} SCREAMING_SNAKE_CASE : Tuple = {'''metadata''': metadata, '''weight_map''': weight_map} with open(os.path.join(a_ , a_ ) , '''w''' , encoding='''utf-8''' ) as f: SCREAMING_SNAKE_CASE : Union[str, Any] = json.dumps(a_ , indent=2 , sort_keys=a_ ) + '''\n''' f.write(a_ ) return metadata, index if __name__ == "__main__": lowerCamelCase__ : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument( "--nllb_moe_checkpoint_path", default="/home/arthur_huggingface_co/fairseq/weights/checkpoints/model_moe_54b/checkpoint_2_300000", type=str, required=False, help="Path to a directory containing a folder per layer. Follows the original Google format.", ) parser.add_argument("--dtype", default="float32", type=str, required=False, help="dtype of the saved model") parser.add_argument( "--pytorch_dump_folder_path", default="/home/arthur_huggingface_co/fairseq/weights/checkpoints/hf-converted-moe-54b", type=str, required=False, help="Path to the output pytorch model.", ) lowerCamelCase__ : List[Any] = parser.parse_args() lowerCamelCase__ , lowerCamelCase__ : Optional[int] = shard_on_the_fly( args.nllb_moe_checkpoint_path, args.pytorch_dump_folder_path, 128, args.dtype, ) lowerCamelCase__ : str = NllbMoeConfig.from_pretrained( "facebook/nllb-200-3.3B", encoder_sparse_step=4, decoder_sparse_step=4, num_experts=128 ) config.save_pretrained(args.pytorch_dump_folder_path) lowerCamelCase__ : List[Any] = NllbMoeModel.from_pretrained(args.pytorch_dump_folder_path) print("Done") model.save_pretrained(args.pytorch_dump_folder_path)
698
"""simple docstring""" # tests directory-specific settings - this file is run automatically # by pytest before any tests are run import doctest import sys import warnings from os.path import abspath, dirname, join import _pytest from transformers.testing_utils import HfDoctestModule, HfDocTestParser # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. lowerCamelCase__ : Optional[int] = abspath(join(dirname(__file__), "src")) sys.path.insert(1, git_repo_path) # silence FutureWarning warnings in tests since often we can't act on them until # they become normal warnings - i.e. the tests still need to test the current functionality warnings.simplefilter(action="ignore", category=FutureWarning) def __A ( a_ : Dict )-> str: '''simple docstring''' config.addinivalue_line( '''markers''' , '''is_pt_tf_cross_test: mark test to run only when PT and TF interactions are tested''' ) config.addinivalue_line( '''markers''' , '''is_pt_flax_cross_test: mark test to run only when PT and FLAX interactions are tested''' ) config.addinivalue_line('''markers''' , '''is_pipeline_test: mark test to run only when pipelines are tested''' ) config.addinivalue_line('''markers''' , '''is_staging_test: mark test to run only in the staging environment''' ) config.addinivalue_line('''markers''' , '''accelerate_tests: mark test that require accelerate''' ) config.addinivalue_line('''markers''' , '''tool_tests: mark the tool tests that are run on their specific schedule''' ) def __A ( a_ : Dict )-> Tuple: '''simple docstring''' from transformers.testing_utils import pytest_addoption_shared pytest_addoption_shared(a_ ) def __A ( a_ : Union[str, Any] )-> List[Any]: '''simple docstring''' from transformers.testing_utils import pytest_terminal_summary_main SCREAMING_SNAKE_CASE : List[str] = terminalreporter.config.getoption('''--make-reports''' ) if make_reports: pytest_terminal_summary_main(a_ , id=a_ ) def __A ( a_ : Dict , a_ : List[str] )-> Dict: '''simple docstring''' if exitstatus == 5: SCREAMING_SNAKE_CASE : List[str] = 0 # Doctest custom flag to ignore output. lowerCamelCase__ : Tuple = doctest.register_optionflag("IGNORE_RESULT") lowerCamelCase__ : Optional[int] = doctest.OutputChecker class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :int , lowerCamelCase_ :int , lowerCamelCase_ :Optional[Any] ) -> Dict: '''simple docstring''' if IGNORE_RESULT & optionflags: return True return OutputChecker.check_output(self , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) lowerCamelCase__ : str = CustomOutputChecker lowerCamelCase__ : Any = HfDoctestModule lowerCamelCase__ : int = HfDocTestParser
698
1
"""simple docstring""" from __future__ import annotations from collections.abc import Generator def __A ( )-> Generator[int, None, None]: '''simple docstring''' SCREAMING_SNAKE_CASE : dict[int, int] = {} SCREAMING_SNAKE_CASE : Dict = 2 while True: SCREAMING_SNAKE_CASE : Dict = factor_map.pop(a_ , a_ ) if factor: SCREAMING_SNAKE_CASE : Tuple = factor + prime while x in factor_map: x += factor SCREAMING_SNAKE_CASE : Tuple = factor else: SCREAMING_SNAKE_CASE : List[str] = prime yield prime prime += 1 def __A ( a_ : float = 1E10 )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = sieve() SCREAMING_SNAKE_CASE : Tuple = 1 while True: SCREAMING_SNAKE_CASE : List[str] = next(a_ ) if (2 * prime * n) > limit: return n # Ignore the next prime as the reminder will be 2. next(a_ ) n += 2 if __name__ == "__main__": print(solution())
698
"""simple docstring""" from __future__ import annotations import os import tempfile import unittest from transformers import ConvBertConfig, 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 ( TFConvBertForMaskedLM, TFConvBertForMultipleChoice, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertModel, ) class lowercase__: '''simple docstring''' def __init__( self :Tuple , lowerCamelCase_ :Tuple , lowerCamelCase_ :Tuple=13 , lowerCamelCase_ :List[str]=7 , lowerCamelCase_ :Dict=True , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :List[str]=True , lowerCamelCase_ :Dict=True , lowerCamelCase_ :str=99 , lowerCamelCase_ :Optional[Any]=32 , lowerCamelCase_ :Tuple=2 , lowerCamelCase_ :int=4 , lowerCamelCase_ :Optional[Any]=37 , lowerCamelCase_ :Any="gelu" , lowerCamelCase_ :Any=0.1 , lowerCamelCase_ :Any=0.1 , lowerCamelCase_ :Optional[int]=5_12 , lowerCamelCase_ :str=16 , lowerCamelCase_ :Optional[int]=2 , lowerCamelCase_ :List[str]=0.0_2 , lowerCamelCase_ :int=3 , lowerCamelCase_ :List[Any]=4 , lowerCamelCase_ :Optional[Any]=None , ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : int = parent SCREAMING_SNAKE_CASE : str = 13 SCREAMING_SNAKE_CASE : str = 7 SCREAMING_SNAKE_CASE : List[Any] = True SCREAMING_SNAKE_CASE : List[str] = True SCREAMING_SNAKE_CASE : Union[str, Any] = True SCREAMING_SNAKE_CASE : str = True SCREAMING_SNAKE_CASE : Any = 99 SCREAMING_SNAKE_CASE : Dict = 3_84 SCREAMING_SNAKE_CASE : List[str] = 2 SCREAMING_SNAKE_CASE : int = 4 SCREAMING_SNAKE_CASE : Any = 37 SCREAMING_SNAKE_CASE : List[str] = '''gelu''' SCREAMING_SNAKE_CASE : List[str] = 0.1 SCREAMING_SNAKE_CASE : int = 0.1 SCREAMING_SNAKE_CASE : Union[str, Any] = 5_12 SCREAMING_SNAKE_CASE : int = 16 SCREAMING_SNAKE_CASE : List[str] = 2 SCREAMING_SNAKE_CASE : Tuple = 0.0_2 SCREAMING_SNAKE_CASE : List[str] = 3 SCREAMING_SNAKE_CASE : Union[str, Any] = 4 SCREAMING_SNAKE_CASE : str = 1_28 SCREAMING_SNAKE_CASE : List[str] = 2 SCREAMING_SNAKE_CASE : Union[str, Any] = 9 SCREAMING_SNAKE_CASE : Dict = 1 SCREAMING_SNAKE_CASE : List[str] = None def __lowerCAmelCase ( self :Optional[Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE : int = None if self.use_input_mask: SCREAMING_SNAKE_CASE : Tuple = random_attention_mask([self.batch_size, self.seq_length] ) SCREAMING_SNAKE_CASE : List[Any] = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) SCREAMING_SNAKE_CASE : List[str] = None SCREAMING_SNAKE_CASE : str = None SCREAMING_SNAKE_CASE : str = None if self.use_labels: SCREAMING_SNAKE_CASE : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor([self.batch_size] , self.num_choices ) SCREAMING_SNAKE_CASE : List[str] = ConvBertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , return_dict=lowerCamelCase_ , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def __lowerCAmelCase ( self :str , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :int , lowerCamelCase_ :List[str] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int , lowerCamelCase_ :List[Any] , lowerCamelCase_ :Tuple ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = TFConvBertModel(config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids} SCREAMING_SNAKE_CASE : Dict = [input_ids, input_mask] SCREAMING_SNAKE_CASE : List[Any] = model(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = model(lowerCamelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __lowerCAmelCase ( self :str , lowerCamelCase_ :List[Any] , lowerCamelCase_ :List[str] , lowerCamelCase_ :int , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :str , lowerCamelCase_ :str , lowerCamelCase_ :Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = TFConvBertForMaskedLM(config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } SCREAMING_SNAKE_CASE : Tuple = model(lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :str , lowerCamelCase_ :Any , lowerCamelCase_ :str , lowerCamelCase_ :Dict , lowerCamelCase_ :int , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.num_labels SCREAMING_SNAKE_CASE : Dict = TFConvBertForSequenceClassification(config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } SCREAMING_SNAKE_CASE : List[Any] = model(lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __lowerCAmelCase ( self :int , lowerCamelCase_ :Dict , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Dict , lowerCamelCase_ :Any , lowerCamelCase_ :Dict , lowerCamelCase_ :List[Any] , lowerCamelCase_ :List[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.num_choices SCREAMING_SNAKE_CASE : Optional[Any] = TFConvBertForMultipleChoice(config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = tf.tile(tf.expand_dims(lowerCamelCase_ , 1 ) , (1, self.num_choices, 1) ) SCREAMING_SNAKE_CASE : Dict = tf.tile(tf.expand_dims(lowerCamelCase_ , 1 ) , (1, self.num_choices, 1) ) SCREAMING_SNAKE_CASE : List[Any] = tf.tile(tf.expand_dims(lowerCamelCase_ , 1 ) , (1, self.num_choices, 1) ) SCREAMING_SNAKE_CASE : Any = { '''input_ids''': multiple_choice_inputs_ids, '''attention_mask''': multiple_choice_input_mask, '''token_type_ids''': multiple_choice_token_type_ids, } SCREAMING_SNAKE_CASE : Optional[int] = model(lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :Tuple , lowerCamelCase_ :Any , lowerCamelCase_ :List[Any] , lowerCamelCase_ :Tuple , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Tuple , lowerCamelCase_ :List[str] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.num_labels SCREAMING_SNAKE_CASE : List[Any] = TFConvBertForTokenClassification(config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } SCREAMING_SNAKE_CASE : int = model(lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Tuple , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :List[str] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = TFConvBertForQuestionAnswering(config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } SCREAMING_SNAKE_CASE : Dict = model(lowerCamelCase_ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __lowerCAmelCase ( self :List[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ) : Optional[Any] = config_and_inputs SCREAMING_SNAKE_CASE : Dict = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class lowercase__( _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase = ( ( TFConvBertModel, TFConvBertForMaskedLM, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertForMultipleChoice, ) if is_tf_available() else () ) UpperCamelCase = ( { """feature-extraction""": TFConvBertModel, """fill-mask""": TFConvBertForMaskedLM, """question-answering""": TFConvBertForQuestionAnswering, """text-classification""": TFConvBertForSequenceClassification, """token-classification""": TFConvBertForTokenClassification, """zero-shot""": TFConvBertForSequenceClassification, } if is_tf_available() else {} ) UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False def __lowerCAmelCase ( self :Optional[int] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = TFConvBertModelTester(self ) SCREAMING_SNAKE_CASE : int = ConfigTester(self , config_class=lowerCamelCase_ , hidden_size=37 ) def __lowerCAmelCase ( self :List[str] ) -> Union[str, Any]: '''simple docstring''' self.config_tester.run_common_tests() def __lowerCAmelCase ( self :Dict ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[int] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase_ ) def __lowerCAmelCase ( self :List[Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase_ ) def __lowerCAmelCase ( self :int ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Any ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*lowerCamelCase_ ) @slow def __lowerCAmelCase ( self :int ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : List[Any] = True SCREAMING_SNAKE_CASE : Tuple = True if hasattr(lowerCamelCase_ , '''use_cache''' ): SCREAMING_SNAKE_CASE : Any = True SCREAMING_SNAKE_CASE : str = getattr(self.model_tester , '''encoder_seq_length''' , self.model_tester.seq_length ) SCREAMING_SNAKE_CASE : Optional[int] = getattr(self.model_tester , '''key_length''' , lowerCamelCase_ ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : str = self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = model_class(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = len(model(lowerCamelCase_ ) ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(lowerCamelCase_ , saved_model=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = os.path.join(lowerCamelCase_ , '''saved_model''' , '''1''' ) SCREAMING_SNAKE_CASE : Tuple = tf.keras.models.load_model(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = model(lowerCamelCase_ ) if self.is_encoder_decoder: SCREAMING_SNAKE_CASE : Optional[int] = outputs['''encoder_hidden_states'''] SCREAMING_SNAKE_CASE : str = outputs['''encoder_attentions'''] else: SCREAMING_SNAKE_CASE : List[str] = outputs['''hidden_states'''] SCREAMING_SNAKE_CASE : List[Any] = outputs['''attentions'''] self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = getattr( self.model_tester , '''expected_num_hidden_layers''' , self.model_tester.num_hidden_layers + 1 ) self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) self.assertListEqual( list(output_hidden_states[0].shape[-2:] ) , [self.model_tester.seq_length, self.model_tester.hidden_size] , ) self.assertEqual(len(lowerCamelCase_ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(output_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length] , ) @slow def __lowerCAmelCase ( self :Any ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = TFConvBertModel.from_pretrained('''YituTech/conv-bert-base''' ) self.assertIsNotNone(lowerCamelCase_ ) def __lowerCAmelCase ( self :Tuple ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : str = True SCREAMING_SNAKE_CASE : List[str] = getattr(self.model_tester , '''decoder_seq_length''' , self.model_tester.seq_length ) SCREAMING_SNAKE_CASE : List[str] = getattr(self.model_tester , '''encoder_seq_length''' , self.model_tester.seq_length ) SCREAMING_SNAKE_CASE : List[str] = getattr(self.model_tester , '''key_length''' , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = getattr(self.model_tester , '''key_length''' , lowerCamelCase_ ) def check_decoder_attentions_output(lowerCamelCase_ :Optional[Any] ): SCREAMING_SNAKE_CASE : Any = len(lowerCamelCase_ ) self.assertEqual(out_len % 2 , 0 ) SCREAMING_SNAKE_CASE : int = outputs.decoder_attentions self.assertEqual(len(lowerCamelCase_ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length] , ) def check_encoder_attentions_output(lowerCamelCase_ :Optional[int] ): SCREAMING_SNAKE_CASE : List[Any] = [ t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions) ] self.assertEqual(len(lowerCamelCase_ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length] , ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Optional[int] = True SCREAMING_SNAKE_CASE : List[str] = False SCREAMING_SNAKE_CASE : str = model_class(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = model(self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Any = len(lowerCamelCase_ ) self.assertEqual(config.output_hidden_states , lowerCamelCase_ ) check_encoder_attentions_output(lowerCamelCase_ ) if self.is_encoder_decoder: SCREAMING_SNAKE_CASE : Union[str, Any] = model_class(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = model(self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) self.assertEqual(config.output_hidden_states , lowerCamelCase_ ) check_decoder_attentions_output(lowerCamelCase_ ) # Check that output attentions can also be changed via the config del inputs_dict["output_attentions"] SCREAMING_SNAKE_CASE : List[Any] = True SCREAMING_SNAKE_CASE : List[str] = model_class(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = model(self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) self.assertEqual(config.output_hidden_states , lowerCamelCase_ ) check_encoder_attentions_output(lowerCamelCase_ ) # Check attention is always last and order is fine SCREAMING_SNAKE_CASE : Optional[int] = True SCREAMING_SNAKE_CASE : str = True SCREAMING_SNAKE_CASE : Optional[Any] = model_class(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = model(self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1) , len(lowerCamelCase_ ) ) self.assertEqual(model.config.output_hidden_states , lowerCamelCase_ ) check_encoder_attentions_output(lowerCamelCase_ ) @require_tf class lowercase__( unittest.TestCase ): '''simple docstring''' @slow def __lowerCAmelCase ( self :int ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = TFConvBertModel.from_pretrained('''YituTech/conv-bert-base''' ) SCREAMING_SNAKE_CASE : Any = tf.constant([[0, 1, 2, 3, 4, 5]] ) SCREAMING_SNAKE_CASE : List[Any] = model(lowerCamelCase_ )[0] SCREAMING_SNAKE_CASE : Optional[Any] = [1, 6, 7_68] self.assertEqual(output.shape , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = tf.constant( [ [ [-0.0_3_4_7_5_4_9_3, -0.4_6_8_6_0_3_4, -0.3_0_6_3_8_8_3_2], [0.2_2_6_3_7_2_4_8, -0.2_6_9_8_8_6_4_6, -0.7_4_2_3_4_2_4], [0.1_0_3_2_4_8_6_8, -0.4_5_0_1_3_5_0_8, -0.5_8_2_8_0_7_8_4], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , lowerCamelCase_ , atol=1E-4 )
698
1
"""simple docstring""" import unittest from transformers import SPIECE_UNDERLINE from transformers.models.speechta import SpeechTaTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.tokenization_utils import AddedToken from ...test_tokenization_common import TokenizerTesterMixin lowerCamelCase__ : Any = get_tests_dir("fixtures/test_sentencepiece_bpe_char.model") @require_sentencepiece @require_tokenizers class lowercase__( _UpperCAmelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase = SpeechTaTokenizer UpperCamelCase = False UpperCamelCase = True def __lowerCAmelCase ( self :Tuple ) -> Dict: '''simple docstring''' super().setUp() # We have a SentencePiece fixture for testing SCREAMING_SNAKE_CASE : str = SpeechTaTokenizer(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = AddedToken('''<mask>''' , lstrip=lowerCamelCase_ , rstrip=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = mask_token tokenizer.add_special_tokens({'''mask_token''': mask_token} ) tokenizer.add_tokens(['''<ctc_blank>'''] ) tokenizer.save_pretrained(self.tmpdirname ) def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :Optional[int] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = '''this is a test''' SCREAMING_SNAKE_CASE : str = '''this is a test''' return input_text, output_text def __lowerCAmelCase ( self :str , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Any=False , lowerCamelCase_ :Optional[int]=20 , lowerCamelCase_ :List[Any]=5 ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = self.get_input_output_texts(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = tokenizer.encode(lowerCamelCase_ , add_special_tokens=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = tokenizer.decode(lowerCamelCase_ , clean_up_tokenization_spaces=lowerCamelCase_ ) return text, ids def __lowerCAmelCase ( self :Union[str, Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = '''<pad>''' SCREAMING_SNAKE_CASE : Optional[int] = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCamelCase_ ) , lowerCamelCase_ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCamelCase_ ) , lowerCamelCase_ ) def __lowerCAmelCase ( self :List[str] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<s>''' ) self.assertEqual(vocab_keys[1] , '''<pad>''' ) self.assertEqual(vocab_keys[-4] , '''œ''' ) self.assertEqual(vocab_keys[-2] , '''<mask>''' ) self.assertEqual(vocab_keys[-1] , '''<ctc_blank>''' ) self.assertEqual(len(lowerCamelCase_ ) , 81 ) def __lowerCAmelCase ( self :Tuple ) -> int: '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 79 ) def __lowerCAmelCase ( self :int ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.get_tokenizers(do_lower_case=lowerCamelCase_ ) for tokenizer in tokenizers: with self.subTest(f"{tokenizer.__class__.__name__}" ): SCREAMING_SNAKE_CASE : int = tokenizer.vocab_size SCREAMING_SNAKE_CASE : Optional[int] = len(lowerCamelCase_ ) self.assertNotEqual(lowerCamelCase_ , 0 ) # We usually have added tokens from the start in tests because our vocab fixtures are # smaller than the original vocabs - let's not assert this # self.assertEqual(vocab_size, all_size) SCREAMING_SNAKE_CASE : Dict = ['''aaaaa bbbbbb''', '''cccccccccdddddddd'''] SCREAMING_SNAKE_CASE : Any = tokenizer.add_tokens(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = tokenizer.vocab_size SCREAMING_SNAKE_CASE : List[str] = len(lowerCamelCase_ ) self.assertNotEqual(lowerCamelCase_ , 0 ) self.assertEqual(lowerCamelCase_ , lowerCamelCase_ ) self.assertEqual(lowerCamelCase_ , len(lowerCamelCase_ ) ) self.assertEqual(lowerCamelCase_ , all_size + len(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : List[str] = tokenizer.encode('''aaaaa bbbbbb low cccccccccdddddddd l''' , add_special_tokens=lowerCamelCase_ ) self.assertGreaterEqual(len(lowerCamelCase_ ) , 4 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) SCREAMING_SNAKE_CASE : List[Any] = {'''eos_token''': '''>>>>|||<||<<|<<''', '''pad_token''': '''<<<<<|||>|>>>>|>'''} SCREAMING_SNAKE_CASE : List[Any] = tokenizer.add_special_tokens(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = tokenizer.vocab_size SCREAMING_SNAKE_CASE : Optional[Any] = len(lowerCamelCase_ ) self.assertNotEqual(lowerCamelCase_ , 0 ) self.assertEqual(lowerCamelCase_ , lowerCamelCase_ ) self.assertEqual(lowerCamelCase_ , len(lowerCamelCase_ ) ) self.assertEqual(lowerCamelCase_ , all_size_a + len(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Union[str, Any] = tokenizer.encode( '''>>>>|||<||<<|<< aaaaabbbbbb low cccccccccdddddddd <<<<<|||>|>>>>|> l''' , add_special_tokens=lowerCamelCase_ ) self.assertGreaterEqual(len(lowerCamelCase_ ) , 6 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[0] , tokens[1] ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokens[-4] ) self.assertEqual(tokens[0] , tokenizer.eos_token_id ) self.assertEqual(tokens[-3] , tokenizer.pad_token_id ) def __lowerCAmelCase ( self :Dict ) -> List[str]: '''simple docstring''' pass def __lowerCAmelCase ( self :List[str] ) -> Optional[int]: '''simple docstring''' pass def __lowerCAmelCase ( self :str ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.get_tokenizer() SCREAMING_SNAKE_CASE : Tuple = tokenizer.tokenize('''This is a test''' ) # fmt: off self.assertListEqual(lowerCamelCase_ , [SPIECE_UNDERLINE, '''T''', '''h''', '''i''', '''s''', SPIECE_UNDERLINE, '''i''', '''s''', SPIECE_UNDERLINE, '''a''', SPIECE_UNDERLINE, '''t''', '''e''', '''s''', '''t'''] ) # fmt: on self.assertListEqual( tokenizer.convert_tokens_to_ids(lowerCamelCase_ ) , [4, 32, 11, 10, 12, 4, 10, 12, 4, 7, 4, 6, 5, 12, 6] , ) SCREAMING_SNAKE_CASE : int = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( lowerCamelCase_ , [SPIECE_UNDERLINE, '''I''', SPIECE_UNDERLINE, '''w''', '''a''', '''s''', SPIECE_UNDERLINE, '''b''', '''o''', '''r''', '''n''', SPIECE_UNDERLINE, '''i''', '''n''', SPIECE_UNDERLINE, '''92000''', ''',''', SPIECE_UNDERLINE, '''a''', '''n''', '''d''', SPIECE_UNDERLINE, '''t''', '''h''', '''i''', '''s''', SPIECE_UNDERLINE, '''i''', '''s''', SPIECE_UNDERLINE, '''f''', '''a''', '''l''', '''s''', '''é''', '''.'''] ) SCREAMING_SNAKE_CASE : Optional[Any] = tokenizer.convert_tokens_to_ids(lowerCamelCase_ ) # fmt: off self.assertListEqual(lowerCamelCase_ , [4, 30, 4, 20, 7, 12, 4, 25, 8, 13, 9, 4, 10, 9, 4, 3, 23, 4, 7, 9, 14, 4, 6, 11, 10, 12, 4, 10, 12, 4, 19, 7, 15, 12, 73, 26] ) # fmt: on SCREAMING_SNAKE_CASE : Any = tokenizer.convert_ids_to_tokens(lowerCamelCase_ ) self.assertListEqual( lowerCamelCase_ , [SPIECE_UNDERLINE, '''I''', SPIECE_UNDERLINE, '''w''', '''a''', '''s''', SPIECE_UNDERLINE, '''b''', '''o''', '''r''', '''n''', SPIECE_UNDERLINE, '''i''', '''n''', SPIECE_UNDERLINE, '''<unk>''', ''',''', SPIECE_UNDERLINE, '''a''', '''n''', '''d''', SPIECE_UNDERLINE, '''t''', '''h''', '''i''', '''s''', SPIECE_UNDERLINE, '''i''', '''s''', SPIECE_UNDERLINE, '''f''', '''a''', '''l''', '''s''', '''é''', '''.'''] ) @slow def __lowerCAmelCase ( self :Any ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = [ '''Transformers (formerly known as pytorch-transformers and pytorch-pretrained-bert) provides ''' '''general-purpose architectures (BERT, GPT, RoBERTa, XLM, DistilBert, XLNet...) for Natural ''' '''Language Understanding (NLU) and Natural Language Generation (NLG) with over thirty-two pretrained ''' '''models in one hundred plus languages and deep interoperability between Jax, PyTorch and TensorFlow.''', '''BERT is designed to pre-train deep bidirectional representations from unlabeled text by jointly ''' '''conditioning on both left and right context in all layers.''', '''The quick brown fox jumps over the lazy dog.''', ] # fmt: off SCREAMING_SNAKE_CASE : Dict = { '''input_ids''': [ [4, 32, 13, 7, 9, 12, 19, 8, 13, 18, 5, 13, 12, 4, 64, 19, 8, 13, 18, 5, 13, 15, 22, 4, 28, 9, 8, 20, 9, 4, 7, 12, 4, 24, 22, 6, 8, 13, 17, 11, 39, 6, 13, 7, 9, 12, 19, 8, 13, 18, 5, 13, 12, 4, 7, 9, 14, 4, 24, 22, 6, 8, 13, 17, 11, 39, 24, 13, 5, 6, 13, 7, 10, 9, 5, 14, 39, 25, 5, 13, 6, 63, 4, 24, 13, 8, 27, 10, 14, 5, 12, 4, 21, 5, 9, 5, 13, 7, 15, 39, 24, 16, 13, 24, 8, 12, 5, 4, 7, 13, 17, 11, 10, 6, 5, 17, 6, 16, 13, 5, 12, 4, 64, 40, 47, 54, 32, 23, 4, 53, 49, 32, 23, 4, 54, 8, 40, 47, 54, 32, 7, 23, 4, 69, 52, 43, 23, 4, 51, 10, 12, 6, 10, 15, 40, 5, 13, 6, 23, 4, 69, 52, 48, 5, 6, 26, 26, 26, 63, 4, 19, 8, 13, 4, 48, 7, 6, 16, 13, 7, 15, 4, 52, 7, 9, 21, 16, 7, 21, 5, 4, 61, 9, 14, 5, 13, 12, 6, 7, 9, 14, 10, 9, 21, 4, 64, 48, 52, 61, 63, 4, 7, 9, 14, 4, 48, 7, 6, 16, 13, 7, 15, 4, 52, 7, 9, 21, 16, 7, 21, 5, 4, 53, 5, 9, 5, 13, 7, 6, 10, 8, 9, 4, 64, 48, 52, 53, 63, 4, 20, 10, 6, 11, 4, 8, 27, 5, 13, 4, 6, 11, 10, 13, 6, 22, 39, 6, 20, 8, 4, 24, 13, 5, 6, 13, 7, 10, 9, 5, 14, 4, 18, 8, 14, 5, 15, 12, 4, 10, 9, 4, 8, 9, 5, 4, 11, 16, 9, 14, 13, 5, 14, 4, 24, 15, 16, 12, 4, 15, 7, 9, 21, 16, 7, 21, 5, 12, 4, 7, 9, 14, 4, 14, 5, 5, 24, 4, 10, 9, 6, 5, 13, 8, 24, 5, 13, 7, 25, 10, 15, 10, 6, 22, 4, 25, 5, 6, 20, 5, 5, 9, 4, 58, 7, 37, 23, 4, 49, 22, 32, 8, 13, 17, 11, 4, 7, 9, 14, 4, 32, 5, 9, 12, 8, 13, 55, 15, 8, 20, 26, 2], [4, 40, 47, 54, 32, 4, 10, 12, 4, 14, 5, 12, 10, 21, 9, 5, 14, 4, 6, 8, 4, 24, 13, 5, 39, 6, 13, 7, 10, 9, 4, 14, 5, 5, 24, 4, 25, 10, 14, 10, 13, 5, 17, 6, 10, 8, 9, 7, 15, 4, 13, 5, 24, 13, 5, 12, 5, 9, 6, 7, 6, 10, 8, 9, 12, 4, 19, 13, 8, 18, 4, 16, 9, 15, 7, 25, 5, 15, 5, 14, 4, 6, 5, 37, 6, 4, 25, 22, 4, 46, 8, 10, 9, 6, 15, 22, 4, 17, 8, 9, 14, 10, 6, 10, 8, 9, 10, 9, 21, 4, 8, 9, 4, 25, 8, 6, 11, 4, 15, 5, 19, 6, 4, 7, 9, 14, 4, 13, 10, 21, 11, 6, 4, 17, 8, 9, 6, 5, 37, 6, 4, 10, 9, 4, 7, 15, 15, 4, 15, 7, 22, 5, 13, 12, 26, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [4, 32, 11, 5, 4, 45, 16, 10, 17, 28, 4, 25, 13, 8, 20, 9, 4, 19, 8, 37, 4, 46, 16, 18, 24, 12, 4, 8, 27, 5, 13, 4, 6, 11, 5, 4, 15, 7, 57, 22, 4, 14, 8, 21, 26, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], ], '''attention_mask''': [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ] } # fmt: on self.tokenizer_integration_test_util( expected_encoding=lowerCamelCase_ , model_name='''microsoft/speecht5_asr''' , revision='''c5ef64c71905caeccde0e4462ef3f9077224c524''' , sequences=lowerCamelCase_ , )
698
"""simple docstring""" from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : Any = { "bert-base-uncased": "https://huggingface.co/bert-base-uncased/resolve/main/config.json", "bert-large-uncased": "https://huggingface.co/bert-large-uncased/resolve/main/config.json", "bert-base-cased": "https://huggingface.co/bert-base-cased/resolve/main/config.json", "bert-large-cased": "https://huggingface.co/bert-large-cased/resolve/main/config.json", "bert-base-multilingual-uncased": "https://huggingface.co/bert-base-multilingual-uncased/resolve/main/config.json", "bert-base-multilingual-cased": "https://huggingface.co/bert-base-multilingual-cased/resolve/main/config.json", "bert-base-chinese": "https://huggingface.co/bert-base-chinese/resolve/main/config.json", "bert-base-german-cased": "https://huggingface.co/bert-base-german-cased/resolve/main/config.json", "bert-large-uncased-whole-word-masking": ( "https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/config.json" ), "bert-large-cased-whole-word-masking": ( "https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/config.json" ), "bert-large-uncased-whole-word-masking-finetuned-squad": ( "https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/config.json" ), "bert-large-cased-whole-word-masking-finetuned-squad": ( "https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/config.json" ), "bert-base-cased-finetuned-mrpc": "https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/config.json", "bert-base-german-dbmdz-cased": "https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/config.json", "bert-base-german-dbmdz-uncased": "https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/config.json", "cl-tohoku/bert-base-japanese": "https://huggingface.co/cl-tohoku/bert-base-japanese/resolve/main/config.json", "cl-tohoku/bert-base-japanese-whole-word-masking": ( "https://huggingface.co/cl-tohoku/bert-base-japanese-whole-word-masking/resolve/main/config.json" ), "cl-tohoku/bert-base-japanese-char": ( "https://huggingface.co/cl-tohoku/bert-base-japanese-char/resolve/main/config.json" ), "cl-tohoku/bert-base-japanese-char-whole-word-masking": ( "https://huggingface.co/cl-tohoku/bert-base-japanese-char-whole-word-masking/resolve/main/config.json" ), "TurkuNLP/bert-base-finnish-cased-v1": ( "https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/config.json" ), "TurkuNLP/bert-base-finnish-uncased-v1": ( "https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/config.json" ), "wietsedv/bert-base-dutch-cased": "https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/config.json", # See all BERT models at https://huggingface.co/models?filter=bert } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """bert""" def __init__( self :Any , lowerCamelCase_ :List[Any]=3_05_22 , lowerCamelCase_ :List[str]=7_68 , lowerCamelCase_ :Tuple=12 , lowerCamelCase_ :List[Any]=12 , lowerCamelCase_ :int=30_72 , lowerCamelCase_ :Dict="gelu" , lowerCamelCase_ :List[Any]=0.1 , lowerCamelCase_ :int=0.1 , lowerCamelCase_ :int=5_12 , lowerCamelCase_ :Optional[int]=2 , lowerCamelCase_ :int=0.0_2 , lowerCamelCase_ :Optional[int]=1E-12 , lowerCamelCase_ :Optional[Any]=0 , lowerCamelCase_ :int="absolute" , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :Optional[Any]=None , **lowerCamelCase_ :List[Any] , ) -> List[str]: '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = vocab_size SCREAMING_SNAKE_CASE : Union[str, Any] = hidden_size SCREAMING_SNAKE_CASE : int = num_hidden_layers SCREAMING_SNAKE_CASE : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE : Dict = hidden_act SCREAMING_SNAKE_CASE : Union[str, Any] = intermediate_size SCREAMING_SNAKE_CASE : Dict = hidden_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : List[Any] = max_position_embeddings SCREAMING_SNAKE_CASE : int = type_vocab_size SCREAMING_SNAKE_CASE : List[str] = initializer_range SCREAMING_SNAKE_CASE : Union[str, Any] = layer_norm_eps SCREAMING_SNAKE_CASE : Optional[Any] = position_embedding_type SCREAMING_SNAKE_CASE : str = use_cache SCREAMING_SNAKE_CASE : Union[str, Any] = classifier_dropout class lowercase__( _UpperCAmelCase ): '''simple docstring''' @property def __lowerCAmelCase ( self :List[str] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": SCREAMING_SNAKE_CASE : Optional[Any] = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: SCREAMING_SNAKE_CASE : Optional[Any] = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis), ] )
698
1
"""simple docstring""" import math from dataclasses import dataclass from typing import Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, randn_tensor from .scheduling_utils import SchedulerMixin @dataclass # Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->UnCLIP class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = 42 UpperCamelCase = None def __A ( a_ : Tuple , a_ : Tuple=0.999 , a_ : Tuple="cosine" , )-> Optional[int]: '''simple docstring''' if alpha_transform_type == "cosine": def alpha_bar_fn(a_ : Dict ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(a_ : Optional[Any] ): return math.exp(t * -12.0 ) else: raise ValueError(F"Unsupported alpha_tranform_type: {alpha_transform_type}" ) SCREAMING_SNAKE_CASE : List[Any] = [] for i in range(a_ ): SCREAMING_SNAKE_CASE : Dict = i / num_diffusion_timesteps SCREAMING_SNAKE_CASE : 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 lowercase__( _UpperCAmelCase , _UpperCAmelCase ): '''simple docstring''' @register_to_config def __init__( self :int , lowerCamelCase_ :int = 10_00 , lowerCamelCase_ :str = "fixed_small_log" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[float] = 1.0 , lowerCamelCase_ :str = "epsilon" , lowerCamelCase_ :str = "squaredcos_cap_v2" , ) -> Union[str, Any]: '''simple docstring''' if beta_schedule != "squaredcos_cap_v2": raise ValueError('''UnCLIPScheduler only supports `beta_schedule`: \'squaredcos_cap_v2\'''' ) SCREAMING_SNAKE_CASE : int = betas_for_alpha_bar(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = 1.0 - self.betas SCREAMING_SNAKE_CASE : Dict = torch.cumprod(self.alphas , dim=0 ) SCREAMING_SNAKE_CASE : List[Any] = torch.tensor(1.0 ) # standard deviation of the initial noise distribution SCREAMING_SNAKE_CASE : Tuple = 1.0 # setable values SCREAMING_SNAKE_CASE : str = None SCREAMING_SNAKE_CASE : List[Any] = torch.from_numpy(np.arange(0 , lowerCamelCase_ )[::-1].copy() ) SCREAMING_SNAKE_CASE : Union[str, Any] = variance_type def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :torch.FloatTensor , lowerCamelCase_ :Optional[int] = None ) -> torch.FloatTensor: '''simple docstring''' return sample def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :int , lowerCamelCase_ :Union[str, torch.device] = None ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = num_inference_steps SCREAMING_SNAKE_CASE : int = (self.config.num_train_timesteps - 1) / (self.num_inference_steps - 1) SCREAMING_SNAKE_CASE : List[Any] = (np.arange(0 , lowerCamelCase_ ) * step_ratio).round()[::-1].copy().astype(np.intaa ) SCREAMING_SNAKE_CASE : Tuple = torch.from_numpy(lowerCamelCase_ ).to(lowerCamelCase_ ) def __lowerCAmelCase ( self :Any , lowerCamelCase_ :List[str] , lowerCamelCase_ :Tuple=None , lowerCamelCase_ :int=None , lowerCamelCase_ :Optional[int]=None ) -> Dict: '''simple docstring''' if prev_timestep is None: SCREAMING_SNAKE_CASE : Any = t - 1 SCREAMING_SNAKE_CASE : Dict = self.alphas_cumprod[t] SCREAMING_SNAKE_CASE : Union[str, Any] = self.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.one SCREAMING_SNAKE_CASE : Union[str, Any] = 1 - alpha_prod_t SCREAMING_SNAKE_CASE : str = 1 - alpha_prod_t_prev if prev_timestep == t - 1: SCREAMING_SNAKE_CASE : Optional[Any] = self.betas[t] else: SCREAMING_SNAKE_CASE : int = 1 - alpha_prod_t / alpha_prod_t_prev # For t > 0, compute predicted variance βt (see formula (6) and (7) from https://arxiv.org/pdf/2006.11239.pdf) # and sample from it to get previous sample # x_{t-1} ~ N(pred_prev_sample, variance) == add variance to pred_sample SCREAMING_SNAKE_CASE : Dict = beta_prod_t_prev / beta_prod_t * beta if variance_type is None: SCREAMING_SNAKE_CASE : int = self.config.variance_type # hacks - were probably added for training stability if variance_type == "fixed_small_log": SCREAMING_SNAKE_CASE : List[Any] = torch.log(torch.clamp(lowerCamelCase_ , min=1E-20 ) ) SCREAMING_SNAKE_CASE : Tuple = torch.exp(0.5 * variance ) elif variance_type == "learned_range": # NOTE difference with DDPM scheduler SCREAMING_SNAKE_CASE : Optional[int] = variance.log() SCREAMING_SNAKE_CASE : int = beta.log() SCREAMING_SNAKE_CASE : Dict = (predicted_variance + 1) / 2 SCREAMING_SNAKE_CASE : Union[str, Any] = frac * max_log + (1 - frac) * min_log return variance def __lowerCAmelCase ( self :Any , lowerCamelCase_ :torch.FloatTensor , lowerCamelCase_ :int , lowerCamelCase_ :torch.FloatTensor , lowerCamelCase_ :Optional[int] = None , lowerCamelCase_ :Tuple=None , lowerCamelCase_ :bool = True , ) -> Union[UnCLIPSchedulerOutput, Tuple]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = timestep if model_output.shape[1] == sample.shape[1] * 2 and self.variance_type == "learned_range": SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[Any] = torch.split(lowerCamelCase_ , sample.shape[1] , dim=1 ) else: SCREAMING_SNAKE_CASE : Optional[int] = None # 1. compute alphas, betas if prev_timestep is None: SCREAMING_SNAKE_CASE : Any = t - 1 SCREAMING_SNAKE_CASE : Optional[Any] = self.alphas_cumprod[t] SCREAMING_SNAKE_CASE : Tuple = self.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.one SCREAMING_SNAKE_CASE : Union[str, Any] = 1 - alpha_prod_t SCREAMING_SNAKE_CASE : Optional[int] = 1 - alpha_prod_t_prev if prev_timestep == t - 1: SCREAMING_SNAKE_CASE : Union[str, Any] = self.betas[t] SCREAMING_SNAKE_CASE : Optional[int] = self.alphas[t] else: SCREAMING_SNAKE_CASE : Dict = 1 - alpha_prod_t / alpha_prod_t_prev SCREAMING_SNAKE_CASE : Union[str, Any] = 1 - beta # 2. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (15) from https://arxiv.org/pdf/2006.11239.pdf if self.config.prediction_type == "epsilon": SCREAMING_SNAKE_CASE : Any = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 elif self.config.prediction_type == "sample": SCREAMING_SNAKE_CASE : List[str] = model_output else: raise ValueError( f"prediction_type given as {self.config.prediction_type} must be one of `epsilon` or `sample`" ''' for the UnCLIPScheduler.''' ) # 3. Clip "predicted x_0" if self.config.clip_sample: SCREAMING_SNAKE_CASE : Optional[Any] = torch.clamp( lowerCamelCase_ , -self.config.clip_sample_range , self.config.clip_sample_range ) # 4. Compute coefficients for pred_original_sample x_0 and current sample x_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf SCREAMING_SNAKE_CASE : List[Any] = (alpha_prod_t_prev ** 0.5 * beta) / beta_prod_t SCREAMING_SNAKE_CASE : Any = alpha ** 0.5 * beta_prod_t_prev / beta_prod_t # 5. Compute predicted previous sample µ_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf SCREAMING_SNAKE_CASE : Dict = pred_original_sample_coeff * pred_original_sample + current_sample_coeff * sample # 6. Add noise SCREAMING_SNAKE_CASE : Union[str, Any] = 0 if t > 0: SCREAMING_SNAKE_CASE : List[Any] = randn_tensor( model_output.shape , dtype=model_output.dtype , generator=lowerCamelCase_ , device=model_output.device ) SCREAMING_SNAKE_CASE : Optional[int] = self._get_variance( lowerCamelCase_ , predicted_variance=lowerCamelCase_ , prev_timestep=lowerCamelCase_ , ) if self.variance_type == "fixed_small_log": SCREAMING_SNAKE_CASE : Tuple = variance elif self.variance_type == "learned_range": SCREAMING_SNAKE_CASE : Union[str, Any] = (0.5 * variance).exp() else: raise ValueError( f"variance_type given as {self.variance_type} must be one of `fixed_small_log` or `learned_range`" ''' for the UnCLIPScheduler.''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = variance * variance_noise SCREAMING_SNAKE_CASE : str = pred_prev_sample + variance if not return_dict: return (pred_prev_sample,) return UnCLIPSchedulerOutput(prev_sample=lowerCamelCase_ , pred_original_sample=lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :torch.FloatTensor , lowerCamelCase_ :torch.FloatTensor , lowerCamelCase_ :torch.IntTensor , ) -> torch.FloatTensor: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.alphas_cumprod.to(device=original_samples.device , dtype=original_samples.dtype ) SCREAMING_SNAKE_CASE : int = timesteps.to(original_samples.device ) SCREAMING_SNAKE_CASE : Optional[int] = alphas_cumprod[timesteps] ** 0.5 SCREAMING_SNAKE_CASE : int = sqrt_alpha_prod.flatten() while len(sqrt_alpha_prod.shape ) < len(original_samples.shape ): SCREAMING_SNAKE_CASE : Optional[Any] = sqrt_alpha_prod.unsqueeze(-1 ) SCREAMING_SNAKE_CASE : List[Any] = (1 - alphas_cumprod[timesteps]) ** 0.5 SCREAMING_SNAKE_CASE : List[Any] = sqrt_one_minus_alpha_prod.flatten() while len(sqrt_one_minus_alpha_prod.shape ) < len(original_samples.shape ): SCREAMING_SNAKE_CASE : List[str] = sqrt_one_minus_alpha_prod.unsqueeze(-1 ) SCREAMING_SNAKE_CASE : Dict = sqrt_alpha_prod * original_samples + sqrt_one_minus_alpha_prod * noise return noisy_samples
698
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : int = logging.get_logger(__name__) lowerCamelCase__ : str = { "studio-ousia/luke-base": "https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json", "studio-ousia/luke-large": "https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """luke""" def __init__( self :List[Any] , lowerCamelCase_ :Optional[int]=5_02_67 , lowerCamelCase_ :List[Any]=50_00_00 , lowerCamelCase_ :str=7_68 , lowerCamelCase_ :Optional[Any]=2_56 , lowerCamelCase_ :List[Any]=12 , lowerCamelCase_ :List[Any]=12 , lowerCamelCase_ :Any=30_72 , lowerCamelCase_ :Optional[int]="gelu" , lowerCamelCase_ :Dict=0.1 , lowerCamelCase_ :Any=0.1 , lowerCamelCase_ :str=5_12 , lowerCamelCase_ :Tuple=2 , lowerCamelCase_ :Optional[Any]=0.0_2 , lowerCamelCase_ :Optional[int]=1E-12 , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :int=None , lowerCamelCase_ :Dict=1 , lowerCamelCase_ :str=0 , lowerCamelCase_ :int=2 , **lowerCamelCase_ :List[str] , ) -> int: '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = vocab_size SCREAMING_SNAKE_CASE : Optional[Any] = entity_vocab_size SCREAMING_SNAKE_CASE : int = hidden_size SCREAMING_SNAKE_CASE : Dict = entity_emb_size SCREAMING_SNAKE_CASE : Optional[int] = num_hidden_layers SCREAMING_SNAKE_CASE : int = num_attention_heads SCREAMING_SNAKE_CASE : str = hidden_act SCREAMING_SNAKE_CASE : str = intermediate_size SCREAMING_SNAKE_CASE : Tuple = hidden_dropout_prob SCREAMING_SNAKE_CASE : str = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = max_position_embeddings SCREAMING_SNAKE_CASE : Dict = type_vocab_size SCREAMING_SNAKE_CASE : List[Any] = initializer_range SCREAMING_SNAKE_CASE : Optional[int] = layer_norm_eps SCREAMING_SNAKE_CASE : Optional[Any] = use_entity_aware_attention SCREAMING_SNAKE_CASE : str = classifier_dropout
698
1
"""simple docstring""" from __future__ import annotations def __A ( a_ : int = 4 )-> list[list[int]]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = abs(a_ ) or 4 return [[1 + x + y * row_size for x in range(a_ )] for y in range(a_ )] def __A ( a_ : list[list[int]] )-> list[list[int]]: '''simple docstring''' return reverse_row(transpose(a_ ) ) # OR.. transpose(reverse_column(matrix)) def __A ( a_ : list[list[int]] )-> list[list[int]]: '''simple docstring''' return reverse_row(reverse_column(a_ ) ) # OR.. reverse_column(reverse_row(matrix)) def __A ( a_ : list[list[int]] )-> list[list[int]]: '''simple docstring''' return reverse_column(transpose(a_ ) ) # OR.. transpose(reverse_row(matrix)) def __A ( a_ : list[list[int]] )-> list[list[int]]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = [list(a_ ) for x in zip(*a_ )] return matrix def __A ( a_ : list[list[int]] )-> list[list[int]]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = matrix[::-1] return matrix def __A ( a_ : list[list[int]] )-> list[list[int]]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = [x[::-1] for x in matrix] return matrix def __A ( a_ : list[list[int]] )-> None: '''simple docstring''' for i in matrix: print(*a_ ) if __name__ == "__main__": lowerCamelCase__ : Union[str, Any] = make_matrix() print("\norigin:\n") print_matrix(matrix) print("\nrotate 90 counterclockwise:\n") print_matrix(rotate_aa(matrix)) lowerCamelCase__ : Union[str, Any] = make_matrix() print("\norigin:\n") print_matrix(matrix) print("\nrotate 180:\n") print_matrix(rotate_aaa(matrix)) lowerCamelCase__ : Optional[int] = make_matrix() print("\norigin:\n") print_matrix(matrix) print("\nrotate 270 counterclockwise:\n") print_matrix(rotate_aaa(matrix))
698
"""simple docstring""" # using dfs for finding eulerian path traversal def __A ( a_ : Dict , a_ : int , a_ : str , a_ : Optional[Any]=None )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = (path or []) + [u] for v in graph[u]: if visited_edge[u][v] is False: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = True, True SCREAMING_SNAKE_CASE : List[str] = dfs(a_ , a_ , a_ , a_ ) return path def __A ( a_ : List[str] , a_ : Any )-> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = 0 SCREAMING_SNAKE_CASE : str = -1 for i in range(a_ ): if i not in graph.keys(): continue if len(graph[i] ) % 2 == 1: odd_degree_nodes += 1 SCREAMING_SNAKE_CASE : Tuple = i if odd_degree_nodes == 0: return 1, odd_node if odd_degree_nodes == 2: return 2, odd_node return 3, odd_node def __A ( a_ : Any , a_ : int )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = [[False for _ in range(max_node + 1 )] for _ in range(max_node + 1 )] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = check_circuit_or_path(a_ , a_ ) if check == 3: print('''graph is not Eulerian''' ) print('''no path''' ) return SCREAMING_SNAKE_CASE : Tuple = 1 if check == 2: SCREAMING_SNAKE_CASE : Optional[int] = odd_node print('''graph has a Euler path''' ) if check == 1: print('''graph has a Euler cycle''' ) SCREAMING_SNAKE_CASE : Optional[int] = dfs(a_ , a_ , a_ ) print(a_ ) def __A ( )-> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = {1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]} SCREAMING_SNAKE_CASE : str = {1: [2, 3, 4, 5], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [1, 4]} SCREAMING_SNAKE_CASE : str = {1: [2, 3, 4], 2: [1, 3, 4], 3: [1, 2], 4: [1, 2, 5], 5: [4]} SCREAMING_SNAKE_CASE : int = {1: [2, 3], 2: [1, 3], 3: [1, 2]} SCREAMING_SNAKE_CASE : int = { 1: [], 2: [] # all degree is zero } SCREAMING_SNAKE_CASE : List[str] = 10 check_euler(a_ , a_ ) check_euler(a_ , a_ ) check_euler(a_ , a_ ) check_euler(a_ , a_ ) check_euler(a_ , a_ ) if __name__ == "__main__": main()
698
1
"""simple docstring""" import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test sys.path.append(str(Path(__file__).parent.parent / "utils")) from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 lowerCamelCase__ : Union[str, Any] = get_tests_dir("fixtures") class lowercase__( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self :int ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = mock.Mock() SCREAMING_SNAKE_CASE : Any = 5_00 SCREAMING_SNAKE_CASE : Dict = {} SCREAMING_SNAKE_CASE : Optional[Any] = HTTPError SCREAMING_SNAKE_CASE : Union[str, Any] = {} # Download this model to make sure it's in the cache. SCREAMING_SNAKE_CASE : str = WavaVecaFeatureExtractor.from_pretrained('''hf-internal-testing/tiny-random-wav2vec2''' ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch('''requests.Session.request''' , return_value=lowerCamelCase_ ) as mock_head: SCREAMING_SNAKE_CASE : Any = WavaVecaFeatureExtractor.from_pretrained('''hf-internal-testing/tiny-random-wav2vec2''' ) # This check we did call the fake head request mock_head.assert_called() def __lowerCAmelCase ( self :List[str] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = WavaVecaFeatureExtractor.from_pretrained( '''https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json''' ) @is_staging_test class lowercase__( unittest.TestCase ): '''simple docstring''' @classmethod def __lowerCAmelCase ( cls :List[str] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : str = TOKEN HfFolder.save_token(lowerCamelCase_ ) @classmethod def __lowerCAmelCase ( cls :Optional[int] ) -> int: '''simple docstring''' try: delete_repo(token=cls._token , repo_id='''test-feature-extractor''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-feature-extractor-org''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''test-dynamic-feature-extractor''' ) except HTTPError: pass def __lowerCAmelCase ( self :Union[str, Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = WavaVecaFeatureExtractor.from_pretrained(lowerCamelCase_ ) feature_extractor.push_to_hub('''test-feature-extractor''' , use_auth_token=self._token ) SCREAMING_SNAKE_CASE : Optional[int] = WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCamelCase_ , getattr(lowerCamelCase_ , lowerCamelCase_ ) ) # Reset repo delete_repo(token=self._token , repo_id='''test-feature-extractor''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( lowerCamelCase_ , repo_id='''test-feature-extractor''' , push_to_hub=lowerCamelCase_ , use_auth_token=self._token ) SCREAMING_SNAKE_CASE : Union[str, Any] = WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCamelCase_ , getattr(lowerCamelCase_ , lowerCamelCase_ ) ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = WavaVecaFeatureExtractor.from_pretrained(lowerCamelCase_ ) feature_extractor.push_to_hub('''valid_org/test-feature-extractor''' , use_auth_token=self._token ) SCREAMING_SNAKE_CASE : Dict = WavaVecaFeatureExtractor.from_pretrained('''valid_org/test-feature-extractor''' ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCamelCase_ , getattr(lowerCamelCase_ , lowerCamelCase_ ) ) # Reset repo delete_repo(token=self._token , repo_id='''valid_org/test-feature-extractor''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( lowerCamelCase_ , repo_id='''valid_org/test-feature-extractor-org''' , push_to_hub=lowerCamelCase_ , use_auth_token=self._token ) SCREAMING_SNAKE_CASE : str = WavaVecaFeatureExtractor.from_pretrained('''valid_org/test-feature-extractor-org''' ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCamelCase_ , getattr(lowerCamelCase_ , lowerCamelCase_ ) ) def __lowerCAmelCase ( self :str ) -> Dict: '''simple docstring''' CustomFeatureExtractor.register_for_auto_class() SCREAMING_SNAKE_CASE : str = CustomFeatureExtractor.from_pretrained(lowerCamelCase_ ) feature_extractor.push_to_hub('''test-dynamic-feature-extractor''' , use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual( feature_extractor.auto_map , {'''AutoFeatureExtractor''': '''custom_feature_extraction.CustomFeatureExtractor'''} , ) SCREAMING_SNAKE_CASE : int = AutoFeatureExtractor.from_pretrained( f"{USER}/test-dynamic-feature-extractor" , trust_remote_code=lowerCamelCase_ ) # Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module self.assertEqual(new_feature_extractor.__class__.__name__ , '''CustomFeatureExtractor''' )
698
"""simple docstring""" import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, BatchEncoding, MBartaaTokenizer, MBartaaTokenizerFast, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, slow, ) from ...test_tokenization_common import TokenizerTesterMixin lowerCamelCase__ : str = get_tests_dir("fixtures/test_sentencepiece.model") if is_torch_available(): from transformers.models.mbart.modeling_mbart import shift_tokens_right lowerCamelCase__ : List[str] = 250004 lowerCamelCase__ : str = 250020 @require_sentencepiece @require_tokenizers class lowercase__( _UpperCAmelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase = MBartaaTokenizer UpperCamelCase = MBartaaTokenizerFast UpperCamelCase = True UpperCamelCase = True def __lowerCAmelCase ( self :Union[str, Any] ) -> str: '''simple docstring''' super().setUp() # We have a SentencePiece fixture for testing SCREAMING_SNAKE_CASE : Optional[int] = MBartaaTokenizer(lowerCamelCase_ , src_lang='''en_XX''' , tgt_lang='''ro_RO''' , keep_accents=lowerCamelCase_ ) tokenizer.save_pretrained(self.tmpdirname ) def __lowerCAmelCase ( self :Union[str, Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = '''<s>''' SCREAMING_SNAKE_CASE : Union[str, Any] = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCamelCase_ ) , lowerCamelCase_ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCamelCase_ ) , lowerCamelCase_ ) def __lowerCAmelCase ( self :str ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<s>''' ) self.assertEqual(vocab_keys[1] , '''<pad>''' ) self.assertEqual(vocab_keys[-1] , '''<mask>''' ) self.assertEqual(len(lowerCamelCase_ ) , 10_54 ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Tuple: '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 10_54 ) def __lowerCAmelCase ( self :Tuple ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = MBartaaTokenizer(lowerCamelCase_ , src_lang='''en_XX''' , tgt_lang='''ro_RO''' , keep_accents=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(lowerCamelCase_ , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(lowerCamelCase_ ) , [value + tokenizer.fairseq_offset for value in [2_85, 46, 10, 1_70, 3_82]] , ) SCREAMING_SNAKE_CASE : Tuple = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( lowerCamelCase_ , [SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.'''] , ) SCREAMING_SNAKE_CASE : int = tokenizer.convert_tokens_to_ids(lowerCamelCase_ ) self.assertListEqual( lowerCamelCase_ , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 6_02, 3_47, 3_47, 3_47, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) SCREAMING_SNAKE_CASE : Union[str, Any] = tokenizer.convert_ids_to_tokens(lowerCamelCase_ ) self.assertListEqual( lowerCamelCase_ , [SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''<unk>''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''<unk>''', '''.'''] , ) @slow def __lowerCAmelCase ( self :Optional[Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = {'''input_ids''': [[25_00_04, 1_10_62, 8_27_72, 7, 15, 8_27_72, 5_38, 5_15_29, 2_37, 1_71_98, 12_90, 2_06, 9, 21_51_75, 13_14, 1_36, 1_71_98, 12_90, 2_06, 9, 5_63_59, 42, 12_20_09, 9, 1_64_66, 16, 8_73_44, 45_37, 9, 47_17, 7_83_81, 6, 15_99_58, 7, 15, 2_44_80, 6_18, 4, 5_27, 2_26_93, 54_28, 4, 27_77, 2_44_80, 98_74, 4, 4_35_23, 5_94, 4, 8_03, 1_83_92, 3_31_89, 18, 4, 4_35_23, 2_44_47, 1_23_99, 1_00, 2_49_55, 8_36_58, 96_26, 14_40_57, 15, 8_39, 2_23_35, 16, 1_36, 2_49_55, 8_36_58, 8_34_79, 15, 3_91_02, 7_24, 16, 6_78, 6_45, 27_89, 13_28, 45_89, 42, 12_20_09, 11_57_74, 23, 8_05, 13_28, 4_68_76, 7, 1_36, 5_38_94, 19_40, 4_22_27, 4_11_59, 1_77_21, 8_23, 4_25, 4, 2_75_12, 9_87_22, 2_06, 1_36, 55_31, 49_70, 9_19, 1_73_36, 5, 2], [25_00_04, 2_00_80, 6_18, 83, 8_27_75, 47, 4_79, 9, 15_17, 73, 5_38_94, 3_33, 8_05_81, 11_01_17, 1_88_11, 52_56, 12_95, 51, 15_25_26, 2_97, 79_86, 3_90, 12_44_16, 5_38, 3_54_31, 2_14, 98, 1_50_44, 2_57_37, 1_36, 71_08, 4_37_01, 23, 7_56, 13_53_55, 7, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [25_00_04, 5_81, 6_37_73, 11_94_55, 6, 14_77_97, 8_82_03, 7, 6_45, 70, 21, 32_85, 1_02_69, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=lowerCamelCase_ , model_name='''facebook/mbart-large-50''' , revision='''d3913889c59cd5c9e456b269c376325eabad57e2''' , ) def __lowerCAmelCase ( self :Optional[int] ) -> List[Any]: '''simple docstring''' if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return SCREAMING_SNAKE_CASE : str = (self.rust_tokenizer_class, '''hf-internal-testing/tiny-random-mbart50''', {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})" ): SCREAMING_SNAKE_CASE : Tuple = self.rust_tokenizer_class.from_pretrained(lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = self.tokenizer_class.from_pretrained(lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = tempfile.mkdtemp() SCREAMING_SNAKE_CASE : Dict = tokenizer_r.save_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = tokenizer_p.save_pretrained(lowerCamelCase_ ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any('''tokenizer.json''' in f for f in tokenizer_r_files ) ) SCREAMING_SNAKE_CASE : Any = tuple(f for f in tokenizer_r_files if '''tokenizer.json''' not in f ) self.assertSequenceEqual(lowerCamelCase_ , lowerCamelCase_ ) # Checks everything loads correctly in the same way SCREAMING_SNAKE_CASE : int = tokenizer_r.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = tokenizer_p.from_pretrained(lowerCamelCase_ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(lowerCamelCase_ , lowerCamelCase_ ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(lowerCamelCase_ ) # Save tokenizer rust, legacy_format=True SCREAMING_SNAKE_CASE : Optional[int] = tempfile.mkdtemp() SCREAMING_SNAKE_CASE : List[Any] = tokenizer_r.save_pretrained(lowerCamelCase_ , legacy_format=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = tokenizer_p.save_pretrained(lowerCamelCase_ ) # Checks it save with the same files self.assertSequenceEqual(lowerCamelCase_ , lowerCamelCase_ ) # Checks everything loads correctly in the same way SCREAMING_SNAKE_CASE : List[Any] = tokenizer_r.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = tokenizer_p.from_pretrained(lowerCamelCase_ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(lowerCamelCase_ , lowerCamelCase_ ) ) shutil.rmtree(lowerCamelCase_ ) # Save tokenizer rust, legacy_format=False SCREAMING_SNAKE_CASE : Union[str, Any] = tempfile.mkdtemp() SCREAMING_SNAKE_CASE : Optional[int] = tokenizer_r.save_pretrained(lowerCamelCase_ , legacy_format=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = tokenizer_p.save_pretrained(lowerCamelCase_ ) # Checks it saved the tokenizer.json file self.assertTrue(any('''tokenizer.json''' in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way SCREAMING_SNAKE_CASE : Tuple = tokenizer_r.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = tokenizer_p.from_pretrained(lowerCamelCase_ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(lowerCamelCase_ , lowerCamelCase_ ) ) shutil.rmtree(lowerCamelCase_ ) @require_torch @require_sentencepiece @require_tokenizers class lowercase__( unittest.TestCase ): '''simple docstring''' UpperCamelCase = """facebook/mbart-large-50-one-to-many-mmt""" UpperCamelCase = [ """ UN Chief Says There Is No Military Solution in Syria""", """ Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for Syria is that \"there is no military solution\" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.""", ] UpperCamelCase = [ """Şeful ONU declară că nu există o soluţie militară în Siria""", """Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei""" """ pentru Siria este că \"nu există o soluţie militară\" la conflictul de aproape cinci ani şi că noi arme nu vor""" """ face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.""", ] UpperCamelCase = [EN_CODE, 82_74, 12_78_73, 2_59_16, 7, 86_22, 20_71, 4_38, 6_74_85, 53, 18_78_95, 23, 5_17_12, 2] @classmethod def __lowerCAmelCase ( cls :Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : MBartaaTokenizer = MBartaaTokenizer.from_pretrained( cls.checkpoint_name , src_lang='''en_XX''' , tgt_lang='''ro_RO''' ) SCREAMING_SNAKE_CASE : Dict = 1 return cls def __lowerCAmelCase ( self :Any ) -> int: '''simple docstring''' self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''ar_AR'''] , 25_00_01 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''en_EN'''] , 25_00_04 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''ro_RO'''] , 25_00_20 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''mr_IN'''] , 25_00_38 ) def __lowerCAmelCase ( self :List[str] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , lowerCamelCase_ ) def __lowerCAmelCase ( self :str ) -> Optional[Any]: '''simple docstring''' self.assertIn(lowerCamelCase_ , self.tokenizer.all_special_ids ) SCREAMING_SNAKE_CASE : int = [RO_CODE, 8_84, 90_19, 96, 9, 9_16, 8_67_92, 36, 1_87_43, 1_55_96, 5, 2] SCREAMING_SNAKE_CASE : Optional[int] = self.tokenizer.decode(lowerCamelCase_ , skip_special_tokens=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=lowerCamelCase_ ) self.assertEqual(lowerCamelCase_ , lowerCamelCase_ ) self.assertNotIn(self.tokenizer.eos_token , lowerCamelCase_ ) def __lowerCAmelCase ( self :Tuple ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = ['''this is gunna be a long sentence ''' * 20] assert isinstance(src_text[0] , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = 10 SCREAMING_SNAKE_CASE : Union[str, Any] = self.tokenizer(lowerCamelCase_ , max_length=lowerCamelCase_ , truncation=lowerCamelCase_ ).input_ids[0] self.assertEqual(ids[0] , lowerCamelCase_ ) self.assertEqual(ids[-1] , 2 ) self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) def __lowerCAmelCase ( self :str ) -> List[str]: '''simple docstring''' self.assertListEqual(self.tokenizer.convert_tokens_to_ids(['''<mask>''', '''ar_AR'''] ) , [25_00_53, 25_00_01] ) def __lowerCAmelCase ( self :List[str] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = tempfile.mkdtemp() SCREAMING_SNAKE_CASE : Dict = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = MBartaaTokenizer.from_pretrained(lowerCamelCase_ ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , lowerCamelCase_ ) @require_torch def __lowerCAmelCase ( self :str ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.tokenizer(self.src_text , text_target=self.tgt_text , padding=lowerCamelCase_ , return_tensors='''pt''' ) SCREAMING_SNAKE_CASE : Dict = shift_tokens_right(batch['''labels'''] , self.tokenizer.pad_token_id ) # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 assert batch.input_ids[1][0] == EN_CODE assert batch.input_ids[1][-1] == 2 assert batch.labels[1][0] == RO_CODE assert batch.labels[1][-1] == 2 assert batch.decoder_input_ids[1][:2].tolist() == [2, RO_CODE] @require_torch def __lowerCAmelCase ( self :Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=lowerCamelCase_ , truncation=lowerCamelCase_ , max_length=len(self.expected_src_tokens ) , return_tensors='''pt''' , ) SCREAMING_SNAKE_CASE : List[Any] = shift_tokens_right(batch['''labels'''] , self.tokenizer.pad_token_id ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) self.assertEqual((2, 14) , batch.input_ids.shape ) self.assertEqual((2, 14) , batch.attention_mask.shape ) SCREAMING_SNAKE_CASE : List[str] = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , lowerCamelCase_ ) self.assertEqual(2 , batch.decoder_input_ids[0, 0] ) # decoder_start_token_id # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [EN_CODE] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.tokenizer(self.src_text , padding=lowerCamelCase_ , truncation=lowerCamelCase_ , max_length=3 , return_tensors='''pt''' ) SCREAMING_SNAKE_CASE : Tuple = self.tokenizer( text_target=self.tgt_text , padding=lowerCamelCase_ , truncation=lowerCamelCase_ , max_length=10 , return_tensors='''pt''' ) SCREAMING_SNAKE_CASE : List[Any] = targets['''input_ids'''] SCREAMING_SNAKE_CASE : Optional[int] = shift_tokens_right(lowerCamelCase_ , self.tokenizer.pad_token_id ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.decoder_input_ids.shape[1] , 10 ) @require_torch def __lowerCAmelCase ( self :Any ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.tokenizer._build_translation_inputs( '''A test''' , return_tensors='''pt''' , src_lang='''en_XX''' , tgt_lang='''ar_AR''' ) self.assertEqual( nested_simplify(lowerCamelCase_ ) , { # en_XX, A, test, EOS '''input_ids''': [[25_00_04, 62, 30_34, 2]], '''attention_mask''': [[1, 1, 1, 1]], # ar_AR '''forced_bos_token_id''': 25_00_01, } , )
698
1
"""simple docstring""" from dataclasses import dataclass from typing import Optional, Tuple import torch from torch import nn from transformers import RobertaPreTrainedModel, XLMRobertaConfig, XLMRobertaModel from transformers.utils import ModelOutput @dataclass class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = None UpperCamelCase = None UpperCamelCase = None UpperCamelCase = None class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __init__( self :Optional[Any] , lowerCamelCase_ :Dict=1 , lowerCamelCase_ :Dict=0 , lowerCamelCase_ :int=2 , lowerCamelCase_ :int=5_12 , lowerCamelCase_ :str="cls" , lowerCamelCase_ :List[Any]=False , lowerCamelCase_ :str=True , **lowerCamelCase_ :Dict , ) -> Dict: '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = project_dim SCREAMING_SNAKE_CASE : str = pooler_fn SCREAMING_SNAKE_CASE : Tuple = learn_encoder SCREAMING_SNAKE_CASE : Any = use_attention_mask class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = [r"""pooler""", r"""logit_scale"""] UpperCamelCase = [r"""position_ids""", r"""predictions.decoder.bias"""] UpperCamelCase = """roberta""" UpperCamelCase = RobertaSeriesConfig def __init__( self :List[str] , lowerCamelCase_ :List[str] ) -> Any: '''simple docstring''' super().__init__(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = XLMRobertaModel(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = nn.Linear(config.hidden_size , config.project_dim ) SCREAMING_SNAKE_CASE : Tuple = getattr(lowerCamelCase_ , '''has_pre_transformation''' , lowerCamelCase_ ) if self.has_pre_transformation: SCREAMING_SNAKE_CASE : str = nn.Linear(config.hidden_size , config.project_dim ) SCREAMING_SNAKE_CASE : List[Any] = nn.LayerNorm(config.hidden_size , eps=config.layer_norm_eps ) self.post_init() def __lowerCAmelCase ( self :Tuple , lowerCamelCase_ :Optional[torch.Tensor] = None , lowerCamelCase_ :Optional[torch.Tensor] = None , lowerCamelCase_ :Optional[torch.Tensor] = None , lowerCamelCase_ :Optional[torch.Tensor] = None , lowerCamelCase_ :Optional[torch.Tensor] = None , lowerCamelCase_ :Optional[torch.Tensor] = None , lowerCamelCase_ :Optional[torch.Tensor] = None , lowerCamelCase_ :Optional[torch.Tensor] = None , lowerCamelCase_ :Optional[bool] = None , lowerCamelCase_ :Optional[bool] = None , lowerCamelCase_ :Optional[bool] = None , ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = return_dict if return_dict is not None else self.config.use_return_dict SCREAMING_SNAKE_CASE : Optional[Any] = self.base_model( input_ids=lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , position_ids=lowerCamelCase_ , head_mask=lowerCamelCase_ , inputs_embeds=lowerCamelCase_ , encoder_hidden_states=lowerCamelCase_ , encoder_attention_mask=lowerCamelCase_ , output_attentions=lowerCamelCase_ , output_hidden_states=True if self.has_pre_transformation else output_hidden_states , return_dict=lowerCamelCase_ , ) if self.has_pre_transformation: SCREAMING_SNAKE_CASE : Optional[Any] = outputs['''hidden_states'''][-2] SCREAMING_SNAKE_CASE : List[Any] = self.pre_LN(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = self.transformation_pre(lowerCamelCase_ ) return TransformationModelOutput( projection_state=lowerCamelCase_ , last_hidden_state=outputs.last_hidden_state , hidden_states=outputs.hidden_states , attentions=outputs.attentions , ) else: SCREAMING_SNAKE_CASE : List[Any] = self.transformation(outputs.last_hidden_state ) return TransformationModelOutput( projection_state=lowerCamelCase_ , last_hidden_state=outputs.last_hidden_state , hidden_states=outputs.hidden_states , attentions=outputs.attentions , )
698
"""simple docstring""" from unittest import TestCase from datasets import Sequence, Value from datasets.arrow_dataset import Dataset class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __lowerCAmelCase ( self :Union[str, Any] ) -> str: '''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 __lowerCAmelCase ( self :Dict ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = {'''col_1''': [3, 2, 1, 0], '''col_2''': ['''a''', '''b''', '''c''', '''d''']} return Dataset.from_dict(lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[int] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self._create_example_records() SCREAMING_SNAKE_CASE : List[Any] = Dataset.from_list(lowerCamelCase_ ) self.assertListEqual(dset.column_names , ['''col_1''', '''col_2'''] ) for i, r in enumerate(lowerCamelCase_ ): self.assertDictEqual(lowerCamelCase_ , example_records[i] ) def __lowerCAmelCase ( self :Dict ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : str = self._create_example_records() SCREAMING_SNAKE_CASE : Optional[int] = Dataset.from_list(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = 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 __lowerCAmelCase ( self :List[str] ) -> Dict: # checks what happens with missing columns '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = [{'''col_1''': 1}, {'''col_2''': '''x'''}] SCREAMING_SNAKE_CASE : Optional[int] = Dataset.from_list(lowerCamelCase_ ) self.assertDictEqual(dset[0] , {'''col_1''': 1} ) self.assertDictEqual(dset[1] , {'''col_1''': None} ) # NB: first record is used for columns def __lowerCAmelCase ( self :Tuple ) -> Optional[Any]: # checks if the type can be inferred from the second record '''simple docstring''' SCREAMING_SNAKE_CASE : int = [{'''col_1''': []}, {'''col_1''': [1, 2]}] SCREAMING_SNAKE_CASE : List[str] = Dataset.from_list(lowerCamelCase_ ) self.assertEqual(dset.info.features['''col_1'''] , Sequence(Value('''int64''' ) ) ) def __lowerCAmelCase ( self :Any ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = Dataset.from_list([] ) self.assertEqual(len(lowerCamelCase_ ) , 0 ) self.assertListEqual(dset.column_names , [] )
698
1
"""simple docstring""" def __A ( a_ : int = 10 , a_ : int = 10_00 , a_ : bool = True )-> int: '''simple docstring''' assert ( isinstance(a_ , a_ ) and isinstance(a_ , a_ ) and isinstance(a_ , a_ ) ), "Invalid type of value(s) specified to function!" if min_val > max_val: raise ValueError('''Invalid value for min_val or max_val (min_value < max_value)''' ) return min_val if option else max_val def __A ( a_ : int , a_ : int )-> int: '''simple docstring''' return int((number_a + number_a) / 2 ) def __A ( a_ : int , a_ : int , a_ : int )-> None: '''simple docstring''' assert ( isinstance(a_ , a_ ) and isinstance(a_ , a_ ) and isinstance(a_ , a_ ) ), 'argument values must be type of "int"' if lower > higher: raise ValueError('''argument value for lower and higher must be(lower > higher)''' ) if not lower < to_guess < higher: raise ValueError( '''guess value must be within the range of lower and higher value''' ) def answer(a_ : int ) -> str: if number > to_guess: return "high" elif number < to_guess: return "low" else: return "same" print('''started...''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = lower SCREAMING_SNAKE_CASE : int = higher SCREAMING_SNAKE_CASE : List[str] = [] while True: SCREAMING_SNAKE_CASE : Any = get_avg(a_ , a_ ) last_numbers.append(a_ ) if answer(a_ ) == "low": SCREAMING_SNAKE_CASE : Dict = number elif answer(a_ ) == "high": SCREAMING_SNAKE_CASE : Tuple = number else: break print(F"guess the number : {last_numbers[-1]}" ) print(F"details : {last_numbers!s}" ) def __A ( )-> None: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = int(input('''Enter lower value : ''' ).strip() ) SCREAMING_SNAKE_CASE : Tuple = int(input('''Enter high value : ''' ).strip() ) SCREAMING_SNAKE_CASE : List[str] = int(input('''Enter value to guess : ''' ).strip() ) guess_the_number(a_ , a_ , a_ ) if __name__ == "__main__": main()
698
"""simple docstring""" from __future__ import annotations import math from collections.abc import Callable def __A ( a_ : Callable[[int | float], int | float] , a_ : int | float , a_ : int | float , a_ : int = 1_00 , )-> float: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = x_start SCREAMING_SNAKE_CASE : Union[str, Any] = fnc(a_ ) SCREAMING_SNAKE_CASE : Optional[int] = 0.0 for _ in range(a_ ): # Approximates curve as a sequence of linear lines and sums their length SCREAMING_SNAKE_CASE : int = (x_end - x_start) / steps + xa SCREAMING_SNAKE_CASE : Optional[int] = fnc(a_ ) length += math.hypot(xa - xa , fxa - fxa ) # Increment step SCREAMING_SNAKE_CASE : str = xa SCREAMING_SNAKE_CASE : Any = fxa return length if __name__ == "__main__": def __A ( a_ : Optional[Any] )-> List[Any]: '''simple docstring''' return math.sin(10 * x ) print("f(x) = sin(10 * x)") print("The length of the curve from x = -10 to x = 10 is:") lowerCamelCase__ : str = 10 while i <= 100000: print(f'''With {i} steps: {line_length(f, -10, 10, i)}''') i *= 10
698
1
"""simple docstring""" import numpy as np import pandas as pd from sklearn.preprocessing import MinMaxScaler from tensorflow.keras.layers import LSTM, Dense from tensorflow.keras.models import Sequential if __name__ == "__main__": lowerCamelCase__ : List[Any] = pd.read_csv("sample_data.csv", header=None) lowerCamelCase__ : Dict = df.shape[:1][0] # If you're using some other dataset input the target column lowerCamelCase__ : List[Any] = df.iloc[:, 1:2] lowerCamelCase__ : Union[str, Any] = actual_data.values.reshape(len_data, 1) lowerCamelCase__ : Dict = MinMaxScaler().fit_transform(actual_data) lowerCamelCase__ : int = 10 lowerCamelCase__ : Dict = 5 lowerCamelCase__ : Any = 20 lowerCamelCase__ : int = len_data - periods * look_back lowerCamelCase__ : str = actual_data[:division] lowerCamelCase__ : Optional[int] = actual_data[division - look_back :] lowerCamelCase__ , lowerCamelCase__ : Dict = [], [] lowerCamelCase__ , lowerCamelCase__ : Optional[int] = [], [] for i in range(0, len(train_data) - forward_days - look_back + 1): train_x.append(train_data[i : i + look_back]) train_y.append(train_data[i + look_back : i + look_back + forward_days]) for i in range(0, len(test_data) - forward_days - look_back + 1): test_x.append(test_data[i : i + look_back]) test_y.append(test_data[i + look_back : i + look_back + forward_days]) lowerCamelCase__ : int = np.array(train_x) lowerCamelCase__ : Tuple = np.array(test_x) lowerCamelCase__ : str = np.array([list(i.ravel()) for i in train_y]) lowerCamelCase__ : str = np.array([list(i.ravel()) for i in test_y]) lowerCamelCase__ : Any = Sequential() model.add(LSTM(128, input_shape=(look_back, 1), return_sequences=True)) model.add(LSTM(64, input_shape=(128, 1))) model.add(Dense(forward_days)) model.compile(loss="mean_squared_error", optimizer="adam") lowerCamelCase__ : Union[str, Any] = model.fit( x_train, y_train, epochs=150, verbose=1, shuffle=True, batch_size=4 ) lowerCamelCase__ : List[str] = model.predict(x_test)
698
"""simple docstring""" # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os from accelerate.test_utils import execute_subprocess_async def __A ( a_ : int=None )-> Tuple: '''simple docstring''' if subparsers is not None: SCREAMING_SNAKE_CASE : List[str] = subparsers.add_parser('''test''' ) else: SCREAMING_SNAKE_CASE : int = argparse.ArgumentParser('''Accelerate test command''' ) parser.add_argument( '''--config_file''' , default=a_ , help=( '''The path to use to store the config file. Will default to a file named default_config.yaml in the cache ''' '''location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have ''' '''such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed ''' '''with \'huggingface\'.''' ) , ) if subparsers is not None: parser.set_defaults(func=a_ ) return parser def __A ( a_ : Any )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = os.path.sep.join(__file__.split(os.path.sep )[:-2] + ['''test_utils''', '''scripts''', '''test_script.py'''] ) if args.config_file is None: SCREAMING_SNAKE_CASE : Tuple = script_name else: SCREAMING_SNAKE_CASE : Optional[Any] = F"--config_file={args.config_file} {script_name}" SCREAMING_SNAKE_CASE : str = ['''accelerate-launch'''] + test_args.split() SCREAMING_SNAKE_CASE : List[str] = execute_subprocess_async(a_ , env=os.environ.copy() ) if result.returncode == 0: print('''Test is a success! You are ready for your distributed training!''' ) def __A ( )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : str = test_command_parser() SCREAMING_SNAKE_CASE : Optional[Any] = parser.parse_args() test_command(a_ ) if __name__ == "__main__": main()
698
1
"""simple docstring""" import copy from collections import OrderedDict from typing import Dict, Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto import CONFIG_MAPPING lowerCamelCase__ : int = logging.get_logger(__name__) lowerCamelCase__ : Union[str, Any] = { "facebook/detr-resnet-50": "https://huggingface.co/facebook/detr-resnet-50/resolve/main/config.json", # See all DETR models at https://huggingface.co/models?filter=detr } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """detr""" UpperCamelCase = ["""past_key_values"""] UpperCamelCase = { """hidden_size""": """d_model""", """num_attention_heads""": """encoder_attention_heads""", } def __init__( self :int , lowerCamelCase_ :Dict=True , lowerCamelCase_ :str=None , lowerCamelCase_ :Tuple=3 , lowerCamelCase_ :Optional[int]=1_00 , lowerCamelCase_ :List[Any]=6 , lowerCamelCase_ :Dict=20_48 , lowerCamelCase_ :Optional[Any]=8 , lowerCamelCase_ :int=6 , lowerCamelCase_ :Union[str, Any]=20_48 , lowerCamelCase_ :int=8 , lowerCamelCase_ :str=0.0 , lowerCamelCase_ :Union[str, Any]=0.0 , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :Union[str, Any]="relu" , lowerCamelCase_ :Any=2_56 , lowerCamelCase_ :Optional[Any]=0.1 , lowerCamelCase_ :List[Any]=0.0 , lowerCamelCase_ :Tuple=0.0 , lowerCamelCase_ :Any=0.0_2 , lowerCamelCase_ :Any=1.0 , lowerCamelCase_ :int=False , lowerCamelCase_ :str="sine" , lowerCamelCase_ :Tuple="resnet50" , lowerCamelCase_ :Union[str, Any]=True , lowerCamelCase_ :Optional[int]=False , lowerCamelCase_ :Dict=1 , lowerCamelCase_ :Optional[int]=5 , lowerCamelCase_ :Optional[Any]=2 , lowerCamelCase_ :int=1 , lowerCamelCase_ :str=1 , lowerCamelCase_ :int=5 , lowerCamelCase_ :str=2 , lowerCamelCase_ :str=0.1 , **lowerCamelCase_ :str , ) -> List[Any]: '''simple docstring''' if backbone_config is not None and use_timm_backbone: raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.''' ) if not use_timm_backbone: if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) SCREAMING_SNAKE_CASE : Optional[int] = CONFIG_MAPPING['''resnet'''](out_features=['''stage4'''] ) elif isinstance(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE : List[Any] = backbone_config.get('''model_type''' ) SCREAMING_SNAKE_CASE : int = CONFIG_MAPPING[backbone_model_type] SCREAMING_SNAKE_CASE : Optional[Any] = config_class.from_dict(lowerCamelCase_ ) # set timm attributes to None SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = None, None, None SCREAMING_SNAKE_CASE : Tuple = use_timm_backbone SCREAMING_SNAKE_CASE : List[Any] = backbone_config SCREAMING_SNAKE_CASE : Union[str, Any] = num_channels SCREAMING_SNAKE_CASE : Optional[Any] = num_queries SCREAMING_SNAKE_CASE : Optional[Any] = d_model SCREAMING_SNAKE_CASE : List[str] = encoder_ffn_dim SCREAMING_SNAKE_CASE : List[Any] = encoder_layers SCREAMING_SNAKE_CASE : Tuple = encoder_attention_heads SCREAMING_SNAKE_CASE : Union[str, Any] = decoder_ffn_dim SCREAMING_SNAKE_CASE : Union[str, Any] = decoder_layers SCREAMING_SNAKE_CASE : List[str] = decoder_attention_heads SCREAMING_SNAKE_CASE : Any = dropout SCREAMING_SNAKE_CASE : int = attention_dropout SCREAMING_SNAKE_CASE : List[str] = activation_dropout SCREAMING_SNAKE_CASE : Union[str, Any] = activation_function SCREAMING_SNAKE_CASE : Any = init_std SCREAMING_SNAKE_CASE : Tuple = init_xavier_std SCREAMING_SNAKE_CASE : Tuple = encoder_layerdrop SCREAMING_SNAKE_CASE : Dict = decoder_layerdrop SCREAMING_SNAKE_CASE : Dict = encoder_layers SCREAMING_SNAKE_CASE : List[Any] = auxiliary_loss SCREAMING_SNAKE_CASE : Union[str, Any] = position_embedding_type SCREAMING_SNAKE_CASE : Any = backbone SCREAMING_SNAKE_CASE : List[str] = use_pretrained_backbone SCREAMING_SNAKE_CASE : List[Any] = dilation # Hungarian matcher SCREAMING_SNAKE_CASE : int = class_cost SCREAMING_SNAKE_CASE : int = bbox_cost SCREAMING_SNAKE_CASE : int = giou_cost # Loss coefficients SCREAMING_SNAKE_CASE : Tuple = mask_loss_coefficient SCREAMING_SNAKE_CASE : Dict = dice_loss_coefficient SCREAMING_SNAKE_CASE : Dict = bbox_loss_coefficient SCREAMING_SNAKE_CASE : Optional[int] = giou_loss_coefficient SCREAMING_SNAKE_CASE : Optional[int] = eos_coefficient super().__init__(is_encoder_decoder=lowerCamelCase_ , **lowerCamelCase_ ) @property def __lowerCAmelCase ( self :Dict ) -> int: '''simple docstring''' return self.encoder_attention_heads @property def __lowerCAmelCase ( self :Optional[int] ) -> int: '''simple docstring''' return self.d_model @classmethod def __lowerCAmelCase ( cls :Union[str, Any] , lowerCamelCase_ :PretrainedConfig , **lowerCamelCase_ :Optional[Any] ) -> List[str]: '''simple docstring''' return cls(backbone_config=lowerCamelCase_ , **lowerCamelCase_ ) def __lowerCAmelCase ( self :str ) -> Dict[str, any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = copy.deepcopy(self.__dict__ ) if output["backbone_config"] is not None: SCREAMING_SNAKE_CASE : str = self.backbone_config.to_dict() SCREAMING_SNAKE_CASE : List[Any] = self.__class__.model_type return output class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = version.parse("""1.11""" ) @property def __lowerCAmelCase ( self :Optional[int] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ('''pixel_mask''', {0: '''batch'''}), ] ) @property def __lowerCAmelCase ( self :Tuple ) -> float: '''simple docstring''' return 1E-5 @property def __lowerCAmelCase ( self :Any ) -> int: '''simple docstring''' return 12
698
"""simple docstring""" def __A ( a_ : int = 10 , a_ : int = 10_00 , a_ : bool = True )-> int: '''simple docstring''' assert ( isinstance(a_ , a_ ) and isinstance(a_ , a_ ) and isinstance(a_ , a_ ) ), "Invalid type of value(s) specified to function!" if min_val > max_val: raise ValueError('''Invalid value for min_val or max_val (min_value < max_value)''' ) return min_val if option else max_val def __A ( a_ : int , a_ : int )-> int: '''simple docstring''' return int((number_a + number_a) / 2 ) def __A ( a_ : int , a_ : int , a_ : int )-> None: '''simple docstring''' assert ( isinstance(a_ , a_ ) and isinstance(a_ , a_ ) and isinstance(a_ , a_ ) ), 'argument values must be type of "int"' if lower > higher: raise ValueError('''argument value for lower and higher must be(lower > higher)''' ) if not lower < to_guess < higher: raise ValueError( '''guess value must be within the range of lower and higher value''' ) def answer(a_ : int ) -> str: if number > to_guess: return "high" elif number < to_guess: return "low" else: return "same" print('''started...''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = lower SCREAMING_SNAKE_CASE : int = higher SCREAMING_SNAKE_CASE : List[str] = [] while True: SCREAMING_SNAKE_CASE : Any = get_avg(a_ , a_ ) last_numbers.append(a_ ) if answer(a_ ) == "low": SCREAMING_SNAKE_CASE : Dict = number elif answer(a_ ) == "high": SCREAMING_SNAKE_CASE : Tuple = number else: break print(F"guess the number : {last_numbers[-1]}" ) print(F"details : {last_numbers!s}" ) def __A ( )-> None: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = int(input('''Enter lower value : ''' ).strip() ) SCREAMING_SNAKE_CASE : Tuple = int(input('''Enter high value : ''' ).strip() ) SCREAMING_SNAKE_CASE : List[str] = int(input('''Enter value to guess : ''' ).strip() ) guess_the_number(a_ , a_ , a_ ) if __name__ == "__main__": main()
698
1
"""simple docstring""" from math import isqrt, loga def __A ( a_ : int )-> list[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = [True] * max_number for i in range(2 , isqrt(max_number - 1 ) + 1 ): if is_prime[i]: for j in range(i**2 , a_ , a_ ): SCREAMING_SNAKE_CASE : Optional[Any] = False return [i for i in range(2 , a_ ) if is_prime[i]] def __A ( a_ : int = 80_08_00 , a_ : int = 80_08_00 )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : int = degree * loga(a_ ) SCREAMING_SNAKE_CASE : str = int(a_ ) SCREAMING_SNAKE_CASE : Dict = calculate_prime_numbers(a_ ) SCREAMING_SNAKE_CASE : Tuple = 0 SCREAMING_SNAKE_CASE : Optional[int] = 0 SCREAMING_SNAKE_CASE : Optional[int] = len(a_ ) - 1 while left < right: while ( prime_numbers[right] * loga(prime_numbers[left] ) + prime_numbers[left] * loga(prime_numbers[right] ) > upper_bound ): right -= 1 hybrid_integers_count += right - left left += 1 return hybrid_integers_count if __name__ == "__main__": print(f'''{solution() = }''')
698
"""simple docstring""" import argparse import fairseq import torch from torch import nn from transformers import ( MBartaaTokenizer, MBartConfig, MBartForCausalLM, SpeechEncoderDecoderConfig, SpeechEncoderDecoderModel, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaModel, logging, ) logging.set_verbosity_info() lowerCamelCase__ : List[Any] = logging.get_logger(__name__) lowerCamelCase__ : Tuple = { "post_extract_proj": "feature_projection.projection", "encoder.pos_conv.0": "encoder.pos_conv_embed.conv", "self_attn.k_proj": "encoder.layers.*.attention.k_proj", "self_attn.v_proj": "encoder.layers.*.attention.v_proj", "self_attn.q_proj": "encoder.layers.*.attention.q_proj", "self_attn.out_proj": "encoder.layers.*.attention.out_proj", "self_attn_layer_norm": "encoder.layers.*.layer_norm", "fc1": "encoder.layers.*.feed_forward.intermediate_dense", "fc2": "encoder.layers.*.feed_forward.output_dense", "final_layer_norm": "encoder.layers.*.final_layer_norm", "encoder.layer_norm": "encoder.layer_norm", "w2v_model.layer_norm": "feature_projection.layer_norm", "quantizer.weight_proj": "quantizer.weight_proj", "quantizer.vars": "quantizer.codevectors", "project_q": "project_q", "final_proj": "project_hid", "w2v_encoder.proj": "lm_head", "mask_emb": "masked_spec_embed", } lowerCamelCase__ : List[str] = [ "lm_head", "quantizer.weight_proj", "quantizer.codevectors", "project_q", "project_hid", ] def __A ( a_ : Optional[int] , a_ : str , a_ : str , a_ : str , a_ : List[str] )-> Tuple: '''simple docstring''' for attribute in key.split('''.''' ): SCREAMING_SNAKE_CASE : Any = getattr(a_ , a_ ) if weight_type is not None: SCREAMING_SNAKE_CASE : Optional[int] = getattr(a_ , a_ ).shape else: SCREAMING_SNAKE_CASE : Any = hf_pointer.shape assert hf_shape == value.shape, ( F"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be" F" {value.shape} for {full_name}" ) if weight_type == "weight": SCREAMING_SNAKE_CASE : List[Any] = value elif weight_type == "weight_g": SCREAMING_SNAKE_CASE : Optional[int] = value elif weight_type == "weight_v": SCREAMING_SNAKE_CASE : Any = value elif weight_type == "bias": SCREAMING_SNAKE_CASE : List[Any] = value else: SCREAMING_SNAKE_CASE : List[str] = value logger.info(F"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." ) def __A ( a_ : Optional[Any] , a_ : Dict )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = [] SCREAMING_SNAKE_CASE : Optional[Any] = fairseq_model.state_dict() SCREAMING_SNAKE_CASE : Tuple = hf_model.feature_extractor SCREAMING_SNAKE_CASE : Tuple = hf_model.adapter for name, value in fairseq_dict.items(): SCREAMING_SNAKE_CASE : int = False if "conv_layers" in name: load_conv_layer( a_ , a_ , a_ , a_ , hf_model.config.feat_extract_norm == '''group''' , ) SCREAMING_SNAKE_CASE : List[str] = True elif any(x in name for x in ['''adaptor''', '''w2v_encoder.proj.''', '''w2v_proj_ln.'''] ): load_adapter(a_ , a_ , a_ , a_ ) SCREAMING_SNAKE_CASE : List[Any] = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split('''w2v_model.''' )[-1] == name.split('''.''' )[0]: SCREAMING_SNAKE_CASE : Union[str, Any] = True if "*" in mapped_key: SCREAMING_SNAKE_CASE : Dict = name.split(a_ )[0].split('''.''' )[-2] SCREAMING_SNAKE_CASE : Optional[int] = mapped_key.replace('''*''' , a_ ) if "weight_g" in name: SCREAMING_SNAKE_CASE : List[str] = '''weight_g''' elif "weight_v" in name: SCREAMING_SNAKE_CASE : Union[str, Any] = '''weight_v''' elif "bias" in name: SCREAMING_SNAKE_CASE : str = '''bias''' elif "weight" in name: SCREAMING_SNAKE_CASE : Tuple = '''weight''' else: SCREAMING_SNAKE_CASE : str = None set_recursively(a_ , a_ , a_ , a_ , a_ ) continue if not is_used: unused_weights.append(a_ ) logger.warning(F"Unused weights: {unused_weights}" ) def __A ( a_ : Dict , a_ : int , a_ : Optional[int] , a_ : Optional[int] , a_ : Dict )-> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = full_name.split('''conv_layers.''' )[-1] SCREAMING_SNAKE_CASE : List[str] = name.split('''.''' ) SCREAMING_SNAKE_CASE : Dict = int(items[0] ) SCREAMING_SNAKE_CASE : Optional[Any] = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found." ) SCREAMING_SNAKE_CASE : List[Any] = value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found." ) SCREAMING_SNAKE_CASE : str = value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( F"{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was" " found." ) SCREAMING_SNAKE_CASE : str = value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found." ) SCREAMING_SNAKE_CASE : Union[str, Any] = value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) else: unused_weights.append(a_ ) def __A ( a_ : Optional[int] , a_ : Optional[int] , a_ : Any , a_ : Any )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = full_name.split('''adaptor.''' )[-1] SCREAMING_SNAKE_CASE : List[Any] = name.split('''.''' ) if items[1].isdigit(): SCREAMING_SNAKE_CASE : List[Any] = int(items[1] ) else: SCREAMING_SNAKE_CASE : str = None if "adaptor" not in full_name: if "proj_ln" in full_name: # has to be layer norm if "bias" in name: assert ( value.shape == adapter.proj_layer_norm.bias.data.shape ), F"{full_name} has size {value.shape}, but {adapter.proj_layer_norm.bias.data.shape} was found." SCREAMING_SNAKE_CASE : str = value logger.info(F"Adapter proj layer norm bias was initialized from {full_name}." ) if "weight" in name: assert ( value.shape == adapter.proj_layer_norm.weight.data.shape ), F"{full_name} has size {value.shape}, but {adapter.proj_layer_norm.weight.data.shape} was found." SCREAMING_SNAKE_CASE : Optional[Any] = value else: # has to be projection layer if "bias" in name: assert ( value.shape == adapter.proj.bias.data.shape ), F"{full_name} has size {value.shape}, but {adapter.proj.bias.data.shape} was found." SCREAMING_SNAKE_CASE : Union[str, Any] = value logger.info(F"Adapter proj layer bias was initialized from {full_name}." ) if "weight" in name: assert ( value.shape == adapter.proj.weight.data.shape ), F"{full_name} has size {value.shape}, but {adapter.proj.weight.data.shape} was found." SCREAMING_SNAKE_CASE : int = value logger.info(F"Adapter proj layer weight was initialized from {full_name}." ) elif isinstance(a_ , a_ ): if "bias" in name: assert ( value.shape == adapter.layers[layer_id].conv.bias.data.shape ), F"{full_name} has size {value.shape}, but {adapter.layers[layer_id].conv.bias.data.shape} was found." SCREAMING_SNAKE_CASE : str = value logger.info(F"Adapter layer {layer_id} bias was initialized from {full_name}." ) elif "weight" in name: assert ( value.shape == adapter.layers[layer_id].conv.weight.data.shape ), F"{full_name} has size {value.shape}, but {adapter.layers[layer_id].conv.weight.data.shape} was found." SCREAMING_SNAKE_CASE : List[str] = value logger.info(F"Adapter layer {layer_id} bias was initialized from {full_name}." ) else: unused_weights.append(a_ ) def __A ( a_ : Optional[Any] )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = emb.weight.shape SCREAMING_SNAKE_CASE : Any = nn.Linear(a_ , a_ , bias=a_ ) SCREAMING_SNAKE_CASE : Optional[int] = emb.weight.data return lin_layer @torch.no_grad() def __A ( a_ : Tuple , a_ : Optional[int] , a_ : List[Any] , a_ : Any , a_ : Tuple , a_ : int , a_ : Any , a_ : str , a_ : Tuple , a_ : Union[str, Any] , a_ : Union[str, Any] , )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = WavaVecaConfig.from_pretrained( a_ , add_adapter=a_ , adapter_stride=a_ , adapter_kernel_size=a_ , use_auth_token=a_ , output_hidden_size=a_ , ) SCREAMING_SNAKE_CASE : Union[str, Any] = MBartConfig.from_pretrained(a_ ) # load model SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={ '''config_yaml''': config_yaml_path, '''data''': '''/'''.join(dict_path.split('''/''' )[:-1] ), '''w2v_path''': checkpoint_path, '''load_pretrained_decoder_from''': None, } , ) SCREAMING_SNAKE_CASE : int = model[0].eval() # load feature extractor SCREAMING_SNAKE_CASE : Union[str, Any] = WavaVecaFeatureExtractor.from_pretrained(a_ , use_auth_token=a_ ) # set weights for wav2vec2 encoder SCREAMING_SNAKE_CASE : str = WavaVecaModel(a_ ) recursively_load_weights_wavaveca(model.encoder , a_ ) # load decoder weights SCREAMING_SNAKE_CASE : Dict = MBartForCausalLM(a_ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict() , strict=a_ ) logger.warning(F"The following keys are missing when loading the decoder weights: {missing_keys}" ) logger.warning(F"The following keys are unexpected when loading the decoder weights: {unexpected_keys}" ) SCREAMING_SNAKE_CASE : Union[str, Any] = SpeechEncoderDecoderModel(encoder=a_ , decoder=a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = False SCREAMING_SNAKE_CASE : Optional[Any] = MBartaaTokenizer(a_ ) tokenizer.save_pretrained(a_ ) SCREAMING_SNAKE_CASE : Tuple = hf_wavavec.config.to_dict() SCREAMING_SNAKE_CASE : Any = tokenizer.pad_token_id SCREAMING_SNAKE_CASE : List[str] = tokenizer.bos_token_id SCREAMING_SNAKE_CASE : Dict = tokenizer.eos_token_id SCREAMING_SNAKE_CASE : Optional[Any] = '''mbart50''' SCREAMING_SNAKE_CASE : Optional[int] = '''wav2vec2''' SCREAMING_SNAKE_CASE : Optional[Any] = tokenizer.eos_token_id SCREAMING_SNAKE_CASE : List[str] = 25_00_04 SCREAMING_SNAKE_CASE : Dict = tokenizer.eos_token_id SCREAMING_SNAKE_CASE : Any = SpeechEncoderDecoderConfig.from_dict(a_ ) hf_wavavec.save_pretrained(a_ ) feature_extractor.save_pretrained(a_ ) if __name__ == "__main__": lowerCamelCase__ : Any = argparse.ArgumentParser() parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to fairseq checkpoint") parser.add_argument("--dict_path", default=None, type=str, help="Path to dict of fine-tuned model") parser.add_argument("--config_yaml_path", default=None, type=str, help="Path to yaml file of fine-tuned model") parser.add_argument( "--encoder_config_path", default="facebook/wav2vec2-xls-r-1b", type=str, help="Path to hf encoder wav2vec2 checkpoint config", ) parser.add_argument( "--decoder_config_path", default="facebook/mbart-large-50-one-to-many-mmt", type=str, help="Path to hf decoder checkpoint config", ) parser.add_argument("--add_adapter", default=True, type=bool, help="whethere to add model adapter layers") parser.add_argument("--adapter_stride", default=2, type=int, help="stride of adapter layers") parser.add_argument("--adapter_kernel_size", default=3, type=int, help="kernel size of adapter layers") parser.add_argument("--encoder_output_dim", default=1024, type=int, help="encoder output dim") parser.add_argument("--start_token_id", default=250004, type=int, help="`decoder_start_token_id` of model config") lowerCamelCase__ : Dict = parser.parse_args() convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.dict_path, args.config_yaml_path, encoder_config_path=args.encoder_config_path, decoder_config_path=args.decoder_config_path, add_adapter=args.add_adapter, adapter_kernel_size=args.adapter_kernel_size, adapter_stride=args.adapter_stride, decoder_start_token_id=args.start_token_id, encoder_output_dim=args.encoder_output_dim, )
698
1
"""simple docstring""" # Lint as: python3 import os import re import urllib.parse from pathlib import Path from typing import Callable, List, Optional, Union from zipfile import ZipFile from ..utils.file_utils import cached_path, hf_github_url from ..utils.logging import get_logger from ..utils.version import Version lowerCamelCase__ : List[str] = get_logger(__name__) class lowercase__: '''simple docstring''' UpperCamelCase = """dummy_data""" UpperCamelCase = """datasets""" UpperCamelCase = False def __init__( self :Optional[int] , lowerCamelCase_ :str , lowerCamelCase_ :str , lowerCamelCase_ :Union[Version, str] , lowerCamelCase_ :Optional[str] = None , lowerCamelCase_ :bool = False , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[List[Callable]] = None , ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = 0 SCREAMING_SNAKE_CASE : Union[str, Any] = dataset_name SCREAMING_SNAKE_CASE : Dict = cache_dir SCREAMING_SNAKE_CASE : List[Any] = use_local_dummy_data SCREAMING_SNAKE_CASE : Dict = config # download_callbacks take a single url as input SCREAMING_SNAKE_CASE : List[Callable] = download_callbacks or [] # if False, it doesn't load existing files and it returns the paths of the dummy files relative # to the dummy_data zip file root SCREAMING_SNAKE_CASE : Union[str, Any] = load_existing_dummy_data # TODO(PVP, QL) might need to make this more general SCREAMING_SNAKE_CASE : Any = str(lowerCamelCase_ ) # to be downloaded SCREAMING_SNAKE_CASE : List[Any] = None SCREAMING_SNAKE_CASE : List[str] = None @property def __lowerCAmelCase ( self :Optional[Any] ) -> str: '''simple docstring''' if self._dummy_file is None: SCREAMING_SNAKE_CASE : Optional[Any] = self.download_dummy_data() return self._dummy_file @property def __lowerCAmelCase ( self :Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' if self.config is not None: # structure is dummy / config_name / version_name return os.path.join('''dummy''' , self.config.name , self.version_name ) # structure is dummy / version_name return os.path.join('''dummy''' , self.version_name ) @property def __lowerCAmelCase ( self :List[Any] ) -> List[str]: '''simple docstring''' return os.path.join(self.dummy_data_folder , '''dummy_data.zip''' ) def __lowerCAmelCase ( self :List[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = ( self.local_path_to_dummy_data if self.use_local_dummy_data is True else self.github_path_to_dummy_data ) SCREAMING_SNAKE_CASE : Dict = cached_path( lowerCamelCase_ , cache_dir=self.cache_dir , extract_compressed_file=lowerCamelCase_ , force_extract=lowerCamelCase_ ) return os.path.join(lowerCamelCase_ , self.dummy_file_name ) @property def __lowerCAmelCase ( self :Any ) -> List[Any]: '''simple docstring''' return os.path.join(self.datasets_scripts_dir , self.dataset_name , self.dummy_zip_file ) @property def __lowerCAmelCase ( self :Union[str, Any] ) -> List[str]: '''simple docstring''' if self._bucket_url is None: SCREAMING_SNAKE_CASE : int = hf_github_url(self.dataset_name , self.dummy_zip_file.replace(os.sep , '''/''' ) ) return self._bucket_url @property def __lowerCAmelCase ( self :Any ) -> List[Any]: '''simple docstring''' if os.path.isdir(self.dummy_file ): return self.dummy_file # else cut off path to file -> example `xsum`. return "/".join(self.dummy_file.replace(os.sep , '''/''' ).split('''/''' )[:-1] ) def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :Tuple , *lowerCamelCase_ :Tuple ) -> int: '''simple docstring''' if self.load_existing_dummy_data: # dummy data is downloaded and tested SCREAMING_SNAKE_CASE : Any = self.dummy_file else: # dummy data cannot be downloaded and only the path to dummy file is returned SCREAMING_SNAKE_CASE : Union[str, Any] = self.dummy_file_name # special case when data_url is a dict if isinstance(lowerCamelCase_ , lowerCamelCase_ ): return self.create_dummy_data_dict(lowerCamelCase_ , lowerCamelCase_ ) elif isinstance(lowerCamelCase_ , (list, tuple) ): return self.create_dummy_data_list(lowerCamelCase_ , lowerCamelCase_ ) else: return self.create_dummy_data_single(lowerCamelCase_ , lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :Optional[int] , *lowerCamelCase_ :str ) -> Tuple: '''simple docstring''' return self.download_and_extract(lowerCamelCase_ ) def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :Any , lowerCamelCase_ :Tuple ) -> int: '''simple docstring''' return self.download_and_extract(lowerCamelCase_ ) def __lowerCAmelCase ( self :Tuple , lowerCamelCase_ :int , *lowerCamelCase_ :int , **lowerCamelCase_ :Dict ) -> str: '''simple docstring''' return path def __lowerCAmelCase ( self :List[str] ) -> List[Any]: '''simple docstring''' return {} def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :Tuple , lowerCamelCase_ :Optional[int] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : int = {} for key, single_urls in data_url.items(): for download_callback in self.download_callbacks: if isinstance(lowerCamelCase_ , lowerCamelCase_ ): for single_url in single_urls: download_callback(lowerCamelCase_ ) else: SCREAMING_SNAKE_CASE : Union[str, Any] = single_urls download_callback(lowerCamelCase_ ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus if isinstance(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Union[str, Any] = [os.path.join(lowerCamelCase_ , urllib.parse.quote_plus(Path(lowerCamelCase_ ).name ) ) for x in single_urls] else: SCREAMING_SNAKE_CASE : List[str] = single_urls SCREAMING_SNAKE_CASE : Tuple = os.path.join(lowerCamelCase_ , urllib.parse.quote_plus(Path(lowerCamelCase_ ).name ) ) SCREAMING_SNAKE_CASE : Optional[Any] = value # make sure that values are unique if all(isinstance(lowerCamelCase_ , lowerCamelCase_ ) for i in dummy_data_dict.values() ) and len(set(dummy_data_dict.values() ) ) < len( dummy_data_dict.values() ): # append key to value to make its name unique SCREAMING_SNAKE_CASE : List[str] = {key: value + key for key, value in dummy_data_dict.items()} return dummy_data_dict def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :Optional[int] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = [] # trick: if there are many shards named like `data.txt-000001-of-00300`, only use the first one SCREAMING_SNAKE_CASE : int = all(bool(re.findall('''[0-9]{3,}-of-[0-9]{3,}''' , lowerCamelCase_ ) ) for url in data_url ) SCREAMING_SNAKE_CASE : Optional[Any] = all( url.startswith('''https://ftp.ncbi.nlm.nih.gov/pubmed/baseline/pubmed''' ) for url in data_url ) if data_url and (is_tf_records or is_pubmed_records): SCREAMING_SNAKE_CASE : Optional[Any] = [data_url[0]] * len(lowerCamelCase_ ) for single_url in data_url: for download_callback in self.download_callbacks: download_callback(lowerCamelCase_ ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus SCREAMING_SNAKE_CASE : int = os.path.join(lowerCamelCase_ , urllib.parse.quote_plus(single_url.split('''/''' )[-1] ) ) dummy_data_list.append(lowerCamelCase_ ) return dummy_data_list def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :str , lowerCamelCase_ :Any ) -> Optional[int]: '''simple docstring''' for download_callback in self.download_callbacks: download_callback(lowerCamelCase_ ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus SCREAMING_SNAKE_CASE : Any = os.path.join(lowerCamelCase_ , urllib.parse.quote_plus(data_url.split('''/''' )[-1] ) ) if os.path.exists(lowerCamelCase_ ) or not self.load_existing_dummy_data: return value else: # Backward compatibility, maybe deprecate at one point. # For many datasets with single url calls to dl_manager.download_and_extract, # the dummy_data.zip file is actually the zipped downloaded file # while now we expected the dummy_data.zip file to be a directory containing # the downloaded file. return path_to_dummy_data def __lowerCAmelCase ( self :List[Any] ) -> str: '''simple docstring''' pass def __lowerCAmelCase ( self :List[Any] ) -> List[Any]: '''simple docstring''' pass def __lowerCAmelCase ( self :Tuple , lowerCamelCase_ :Union[str, Any] ) -> Optional[int]: '''simple docstring''' def _iter_archive_members(lowerCamelCase_ :str ): # this preserves the order of the members inside the ZIP archive SCREAMING_SNAKE_CASE : Optional[Any] = Path(self.dummy_file ).parent SCREAMING_SNAKE_CASE : int = path.relative_to(lowerCamelCase_ ) with ZipFile(self.local_path_to_dummy_data ) as zip_file: SCREAMING_SNAKE_CASE : Tuple = zip_file.namelist() for member in members: if member.startswith(relative_path.as_posix() ): yield dummy_parent_path.joinpath(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = Path(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = _iter_archive_members(lowerCamelCase_ ) if self.use_local_dummy_data else path.rglob('''*''' ) for file_path in file_paths: if file_path.is_file() and not file_path.name.startswith(('''.''', '''__''') ): yield file_path.relative_to(lowerCamelCase_ ).as_posix(), file_path.open('''rb''' ) def __lowerCAmelCase ( self :int , lowerCamelCase_ :int ) -> Any: '''simple docstring''' if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Optional[Any] = [paths] for path in paths: if os.path.isfile(lowerCamelCase_ ): if os.path.basename(lowerCamelCase_ ).startswith(('''.''', '''__''') ): return yield path else: for dirpath, dirnames, filenames in os.walk(lowerCamelCase_ ): if os.path.basename(lowerCamelCase_ ).startswith(('''.''', '''__''') ): continue dirnames.sort() for filename in sorted(lowerCamelCase_ ): if filename.startswith(('''.''', '''__''') ): continue yield os.path.join(lowerCamelCase_ , lowerCamelCase_ )
698
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available lowerCamelCase__ : Union[str, Any] = { "configuration_m2m_100": ["M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP", "M2M100Config", "M2M100OnnxConfig"], "tokenization_m2m_100": ["M2M100Tokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : str = [ "M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST", "M2M100ForConditionalGeneration", "M2M100Model", "M2M100PreTrainedModel", ] if TYPE_CHECKING: from .configuration_mam_aaa import M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP, MaMaaaConfig, MaMaaaOnnxConfig from .tokenization_mam_aaa import MaMaaaTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mam_aaa import ( M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST, MaMaaaForConditionalGeneration, MaMaaaModel, MaMaaaPreTrainedModel, ) else: import sys lowerCamelCase__ : Optional[int] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
698
1
"""simple docstring""" import torch from diffusers import DPMSolverSDEScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import require_torchsde from .test_schedulers import SchedulerCommonTest @require_torchsde class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = (DPMSolverSDEScheduler,) UpperCamelCase = 10 def __lowerCAmelCase ( self :Optional[Any] , **lowerCamelCase_ :Optional[int] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = { '''num_train_timesteps''': 11_00, '''beta_start''': 0.0_0_0_1, '''beta_end''': 0.0_2, '''beta_schedule''': '''linear''', '''noise_sampler_seed''': 0, } config.update(**lowerCamelCase_ ) return config def __lowerCAmelCase ( self :Any ) -> Tuple: '''simple docstring''' for timesteps in [10, 50, 1_00, 10_00]: self.check_over_configs(num_train_timesteps=lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[int] ) -> Dict: '''simple docstring''' for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1] , [0.0_0_0_2, 0.0_0_2, 0.0_2] ): self.check_over_configs(beta_start=lowerCamelCase_ , beta_end=lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[int] ) -> str: '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[int] ) -> List[Any]: '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=lowerCamelCase_ ) def __lowerCAmelCase ( self :Dict ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.scheduler_classes[0] SCREAMING_SNAKE_CASE : List[str] = self.get_scheduler_config() SCREAMING_SNAKE_CASE : List[Any] = scheduler_class(**lowerCamelCase_ ) scheduler.set_timesteps(self.num_inference_steps ) SCREAMING_SNAKE_CASE : Tuple = self.dummy_model() SCREAMING_SNAKE_CASE : int = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE : Any = sample.to(lowerCamelCase_ ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE : Any = scheduler.scale_model_input(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = model(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = output.prev_sample SCREAMING_SNAKE_CASE : Dict = torch.sum(torch.abs(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : List[Any] = torch.mean(torch.abs(lowerCamelCase_ ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_6_7.4_7_8_2_1_0_4_4_9_2_1_8_7_5 ) < 1E-2 assert abs(result_mean.item() - 0.2_1_7_8_7_0_5_9_6_4_5_6_5_2_7_7 ) < 1E-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_7_1.5_9_3_5_2_1_1_1_8_1_6_4_0_6 ) < 1E-2 assert abs(result_mean.item() - 0.2_2_3_4_2_9_0_6_8_9_2_2_9_9_6_5_2 ) < 1E-3 else: assert abs(result_sum.item() - 1_6_2.5_2_3_8_3_4_2_2_8_5_1_5_6_2 ) < 1E-2 assert abs(result_mean.item() - 0.2_1_1_6_1_9_5_7_0_8_5_1_3_2_6 ) < 1E-3 def __lowerCAmelCase ( self :Union[str, Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.scheduler_classes[0] SCREAMING_SNAKE_CASE : Optional[int] = self.get_scheduler_config(prediction_type='''v_prediction''' ) SCREAMING_SNAKE_CASE : Optional[Any] = scheduler_class(**lowerCamelCase_ ) scheduler.set_timesteps(self.num_inference_steps ) SCREAMING_SNAKE_CASE : List[str] = self.dummy_model() SCREAMING_SNAKE_CASE : Optional[Any] = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE : Tuple = sample.to(lowerCamelCase_ ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE : Any = scheduler.scale_model_input(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = model(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = output.prev_sample SCREAMING_SNAKE_CASE : Tuple = torch.sum(torch.abs(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : str = torch.mean(torch.abs(lowerCamelCase_ ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_2_4.7_7_1_4_9_2_0_0_4_3_9_4_5_3 ) < 1E-2 assert abs(result_mean.item() - 0.1_6_2_2_6_2_8_9_0_1_4_8_1_6_2_8_4 ) < 1E-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_2_8.1_6_6_3_3_6_0_5_9_5_7_0_3 ) < 1E-2 assert abs(result_mean.item() - 0.1_6_6_8_8_3_2_6_0_0_1_1_6_7_2_9_7 ) < 1E-3 else: assert abs(result_sum.item() - 1_1_9.8_4_8_7_5_4_8_8_2_8_1_2_5 ) < 1E-2 assert abs(result_mean.item() - 0.1_5_6_0_5_3_0_6_6_2_5_3_6_6_2_1 ) < 1E-3 def __lowerCAmelCase ( self :Optional[int] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.scheduler_classes[0] SCREAMING_SNAKE_CASE : Optional[Any] = self.get_scheduler_config() SCREAMING_SNAKE_CASE : List[str] = scheduler_class(**lowerCamelCase_ ) scheduler.set_timesteps(self.num_inference_steps , device=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = self.dummy_model() SCREAMING_SNAKE_CASE : List[Any] = self.dummy_sample_deter.to(lowerCamelCase_ ) * scheduler.init_noise_sigma for t in scheduler.timesteps: SCREAMING_SNAKE_CASE : List[Any] = scheduler.scale_model_input(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = model(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = output.prev_sample SCREAMING_SNAKE_CASE : List[str] = torch.sum(torch.abs(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : str = torch.mean(torch.abs(lowerCamelCase_ ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_6_7.4_6_9_5_7_3_9_7_4_6_0_9_3_8 ) < 1E-2 assert abs(result_mean.item() - 0.2_1_8_0_5_9_3_4_6_0_7_9_8_2_6_3_5 ) < 1E-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_7_1.5_9_3_5_3_6_3_7_6_9_5_3_1_2 ) < 1E-2 assert abs(result_mean.item() - 0.2_2_3_4_2_9_0_8_3_8_2_4_1_5_7_7_1 ) < 1E-3 else: assert abs(result_sum.item() - 1_6_2.5_2_3_8_3_4_2_2_8_5_1_5_6_2 ) < 1E-2 assert abs(result_mean.item() - 0.2_1_1_6_1_9_5_7_0_8_5_1_3_2_6 ) < 1E-3 def __lowerCAmelCase ( self :str ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.scheduler_classes[0] SCREAMING_SNAKE_CASE : Optional[Any] = self.get_scheduler_config() SCREAMING_SNAKE_CASE : Dict = scheduler_class(**lowerCamelCase_ , use_karras_sigmas=lowerCamelCase_ ) scheduler.set_timesteps(self.num_inference_steps , device=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = self.dummy_model() SCREAMING_SNAKE_CASE : List[str] = self.dummy_sample_deter.to(lowerCamelCase_ ) * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE : Any = sample.to(lowerCamelCase_ ) for t in scheduler.timesteps: SCREAMING_SNAKE_CASE : Any = scheduler.scale_model_input(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = model(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = output.prev_sample SCREAMING_SNAKE_CASE : Tuple = torch.sum(torch.abs(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Any = torch.mean(torch.abs(lowerCamelCase_ ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_7_6.6_6_9_7_4_1_3_5_7_4_2_1_8_8 ) < 1E-2 assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1 ) < 1E-2 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_7_7.6_3_6_5_3_5_6_4_4_5_3_1_2_5 ) < 1E-2 assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1 ) < 1E-2 else: assert abs(result_sum.item() - 1_7_0.3_1_3_5_2_2_3_3_8_8_6_7_2 ) < 1E-2 assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1 ) < 1E-2
698
"""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 lowerCamelCase__ : List[Any] = "\\n@inproceedings{pillutla-etal:mauve:neurips2021,\n title={MAUVE: Measuring the Gap Between Neural Text and Human Text using Divergence Frontiers},\n author={Pillutla, Krishna and Swayamdipta, Swabha and Zellers, Rowan and Thickstun, John and Welleck, Sean and Choi, Yejin and Harchaoui, Zaid},\n booktitle = {NeurIPS},\n year = {2021}\n}\n\n" lowerCamelCase__ : List[str] = "\\nMAUVE is a library built on PyTorch and HuggingFace Transformers to measure the gap between neural text and human text with the eponymous MAUVE measure.\n\nMAUVE summarizes both Type I and Type II errors measured softly using Kullback–Leibler (KL) divergences.\n\nFor details, see the MAUVE paper: https://arxiv.org/abs/2102.01454 (Neurips, 2021).\n\nThis metrics is a wrapper around the official implementation of MAUVE:\nhttps://github.com/krishnap25/mauve\n" lowerCamelCase__ : List[Any] = "\nCalculates MAUVE scores between two lists of generated text and reference text.\nArgs:\n predictions: list of generated text to score. Each predictions\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\nOptional Args:\n num_buckets: the size of the histogram to quantize P and Q. Options: 'auto' (default) or an integer\n pca_max_data: the number data points to use for PCA dimensionality reduction prior to clustering. If -1, use all the data. Default -1\n kmeans_explained_var: amount of variance of the data to keep in dimensionality reduction by PCA. Default 0.9\n kmeans_num_redo: number of times to redo k-means clustering (the best objective is kept). Default 5\n kmeans_max_iter: maximum number of k-means iterations. Default 500\n 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'].\n 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\n max_text_length: maximum number of tokens to consider. Default 1024\n divergence_curve_discretization_size: Number of points to consider on the divergence curve. Default 25\n mauve_scaling_factor: \"c\" from the paper. Default 5.\n verbose: If True (default), print running time updates\n seed: random seed to initialize k-means cluster assignments.\nReturns:\n mauve: MAUVE score, a number between 0 and 1. Larger values indicate that P and Q are closer,\n frontier_integral: Frontier Integral, a number between 0 and 1. Smaller values indicate that P and Q are closer,\n divergence_curve: a numpy.ndarray of shape (m, 2); plot it with matplotlib to view the divergence curve,\n p_hist: a discrete distribution, which is a quantized version of the text distribution p_text,\n q_hist: same as above, but with q_text.\nExamples:\n\n >>> # faiss segfaults in doctest for some reason, so the .compute call is not tested with doctest\n >>> import datasets\n >>> mauve = datasets.load_metric('mauve')\n >>> predictions = [\"hello there\", \"general kenobi\"]\n >>> references = [\"hello there\", \"general kenobi\"]\n >>> out = mauve.compute(predictions=predictions, references=references) # doctest: +SKIP\n >>> print(out.mauve) # doctest: +SKIP\n 1.0\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowercase__( datasets.Metric ): '''simple docstring''' def __lowerCAmelCase ( self :Optional[int] ) -> 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 __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :List[str] , lowerCamelCase_ :Optional[Any]=None , lowerCamelCase_ :str=None , lowerCamelCase_ :Tuple=None , lowerCamelCase_ :Optional[Any]=None , lowerCamelCase_ :Optional[int]="auto" , lowerCamelCase_ :Dict=-1 , lowerCamelCase_ :str=0.9 , lowerCamelCase_ :str=5 , lowerCamelCase_ :Tuple=5_00 , lowerCamelCase_ :str="gpt2-large" , lowerCamelCase_ :List[Any]=-1 , lowerCamelCase_ :Dict=10_24 , lowerCamelCase_ :Tuple=25 , lowerCamelCase_ :List[Any]=5 , lowerCamelCase_ :Dict=True , lowerCamelCase_ :List[Any]=25 , ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = compute_mauve( p_text=lowerCamelCase_ , q_text=lowerCamelCase_ , p_features=lowerCamelCase_ , q_features=lowerCamelCase_ , p_tokens=lowerCamelCase_ , q_tokens=lowerCamelCase_ , num_buckets=lowerCamelCase_ , pca_max_data=lowerCamelCase_ , kmeans_explained_var=lowerCamelCase_ , kmeans_num_redo=lowerCamelCase_ , kmeans_max_iter=lowerCamelCase_ , featurize_model_name=lowerCamelCase_ , device_id=lowerCamelCase_ , max_text_length=lowerCamelCase_ , divergence_curve_discretization_size=lowerCamelCase_ , mauve_scaling_factor=lowerCamelCase_ , verbose=lowerCamelCase_ , seed=lowerCamelCase_ , ) return out
698
1
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCamelCase__ : Optional[Any] = logging.get_logger(__name__) lowerCamelCase__ : Union[str, Any] = { "microsoft/resnet-50": "https://huggingface.co/microsoft/resnet-50/blob/main/config.json", } class lowercase__( _UpperCAmelCase , _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """resnet""" UpperCamelCase = ["""basic""", """bottleneck"""] def __init__( self :Optional[int] , lowerCamelCase_ :Tuple=3 , lowerCamelCase_ :Tuple=64 , lowerCamelCase_ :Union[str, Any]=[2_56, 5_12, 10_24, 20_48] , lowerCamelCase_ :int=[3, 4, 6, 3] , lowerCamelCase_ :Any="bottleneck" , lowerCamelCase_ :Optional[int]="relu" , lowerCamelCase_ :Optional[int]=False , lowerCamelCase_ :Any=None , lowerCamelCase_ :Optional[int]=None , **lowerCamelCase_ :Optional[int] , ) -> Tuple: '''simple docstring''' super().__init__(**lowerCamelCase_ ) if layer_type not in self.layer_types: raise ValueError(f"layer_type={layer_type} is not one of {','.join(self.layer_types )}" ) SCREAMING_SNAKE_CASE : Tuple = num_channels SCREAMING_SNAKE_CASE : Union[str, Any] = embedding_size SCREAMING_SNAKE_CASE : List[str] = hidden_sizes SCREAMING_SNAKE_CASE : Optional[Any] = depths SCREAMING_SNAKE_CASE : List[Any] = layer_type SCREAMING_SNAKE_CASE : str = hidden_act SCREAMING_SNAKE_CASE : Optional[Any] = downsample_in_first_stage SCREAMING_SNAKE_CASE : int = ['''stem'''] + [f"stage{idx}" for idx in range(1 , len(lowerCamelCase_ ) + 1 )] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = get_aligned_output_features_output_indices( out_features=lowerCamelCase_ , out_indices=lowerCamelCase_ , stage_names=self.stage_names ) class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = version.parse("""1.11""" ) @property def __lowerCAmelCase ( self :Optional[Any] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def __lowerCAmelCase ( self :str ) -> float: '''simple docstring''' return 1E-3
698
"""simple docstring""" import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert_fast import BertTokenizerFast from .tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer, DPRReaderTokenizer lowerCamelCase__ : Union[str, Any] = logging.get_logger(__name__) lowerCamelCase__ : Union[str, Any] = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} lowerCamelCase__ : Any = { "vocab_file": { "facebook/dpr-ctx_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt" ), "facebook/dpr-ctx_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "facebook/dpr-ctx_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json" ), "facebook/dpr-ctx_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json" ), }, } lowerCamelCase__ : str = { "vocab_file": { "facebook/dpr-question_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt" ), "facebook/dpr-question_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "facebook/dpr-question_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json" ), "facebook/dpr-question_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json" ), }, } lowerCamelCase__ : Any = { "vocab_file": { "facebook/dpr-reader-single-nq-base": ( "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt" ), "facebook/dpr-reader-multiset-base": ( "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "facebook/dpr-reader-single-nq-base": ( "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json" ), "facebook/dpr-reader-multiset-base": ( "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json" ), }, } lowerCamelCase__ : Optional[Any] = { "facebook/dpr-ctx_encoder-single-nq-base": 512, "facebook/dpr-ctx_encoder-multiset-base": 512, } lowerCamelCase__ : Tuple = { "facebook/dpr-question_encoder-single-nq-base": 512, "facebook/dpr-question_encoder-multiset-base": 512, } lowerCamelCase__ : Dict = { "facebook/dpr-reader-single-nq-base": 512, "facebook/dpr-reader-multiset-base": 512, } lowerCamelCase__ : int = { "facebook/dpr-ctx_encoder-single-nq-base": {"do_lower_case": True}, "facebook/dpr-ctx_encoder-multiset-base": {"do_lower_case": True}, } lowerCamelCase__ : Tuple = { "facebook/dpr-question_encoder-single-nq-base": {"do_lower_case": True}, "facebook/dpr-question_encoder-multiset-base": {"do_lower_case": True}, } lowerCamelCase__ : Dict = { "facebook/dpr-reader-single-nq-base": {"do_lower_case": True}, "facebook/dpr-reader-multiset-base": {"do_lower_case": True}, } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = VOCAB_FILES_NAMES UpperCamelCase = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase = DPRContextEncoderTokenizer class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = VOCAB_FILES_NAMES UpperCamelCase = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase = DPRQuestionEncoderTokenizer lowerCamelCase__ : Union[str, Any] = collections.namedtuple( "DPRSpanPrediction", ["span_score", "relevance_score", "doc_id", "start_index", "end_index", "text"] ) lowerCamelCase__ : int = collections.namedtuple("DPRReaderOutput", ["start_logits", "end_logits", "relevance_logits"]) lowerCamelCase__ : str = r"\n Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`.\n It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers),\n using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)`\n with the format:\n\n [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids>\n\n Args:\n questions (`str` or `List[str]`):\n The questions to be encoded. You can specify one question for many passages. In this case, the question\n will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in\n `titles` or `texts`.\n titles (`str` or `List[str]`):\n The passages titles to be encoded. This can be a string or a list of strings if there are several passages.\n texts (`str` or `List[str]`):\n The passages texts to be encoded. This can be a string or a list of strings if there are several passages.\n padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`):\n Activates and controls padding. Accepts the following values:\n\n - `True` or `'longest'`: Pad to the longest sequence in the batch (or no padding if only a single sequence\n if provided).\n - `'max_length'`: Pad to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided.\n - `False` or `'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of different\n lengths).\n truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`):\n Activates and controls truncation. Accepts the following values:\n\n - `True` or `'longest_first'`: Truncate to a maximum length specified with the argument `max_length` or to\n the maximum acceptable input length for the model if that argument is not provided. This will truncate\n token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch\n of pairs) is provided.\n - `'only_first'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the first\n sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `'only_second'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the\n second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `False` or `'do_not_truncate'` (default): No truncation (i.e., can output batch with sequence lengths\n greater than the model maximum admissible input size).\n max_length (`int`, *optional*):\n Controls the maximum length to use by one of the truncation/padding parameters.\n\n If left unset or set to `None`, this will use the predefined model maximum length if a maximum length\n is required by one of the truncation/padding parameters. If the model has no specific maximum input\n length (like XLNet) truncation/padding to a maximum length will be deactivated.\n return_tensors (`str` or [`~utils.TensorType`], *optional*):\n If set, will return tensors instead of list of python integers. Acceptable values are:\n\n - `'tf'`: Return TensorFlow `tf.constant` objects.\n - `'pt'`: Return PyTorch `torch.Tensor` objects.\n - `'np'`: Return Numpy `np.ndarray` objects.\n return_attention_mask (`bool`, *optional*):\n Whether or not to return the attention mask. If not set, will return the attention mask according to the\n specific tokenizer's default, defined by the `return_outputs` attribute.\n\n [What are attention masks?](../glossary#attention-mask)\n\n Return:\n `Dict[str, List[List[int]]]`: A dictionary with the following keys:\n\n - `input_ids`: List of token ids to be fed to a model.\n - `attention_mask`: List of indices specifying which tokens should be attended to by the model.\n " @add_start_docstrings(_UpperCAmelCase ) class lowercase__: '''simple docstring''' def __call__( self :str , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Optional[str] = None , lowerCamelCase_ :Optional[str] = None , lowerCamelCase_ :Union[bool, str] = False , lowerCamelCase_ :Union[bool, str] = False , lowerCamelCase_ :Optional[int] = None , lowerCamelCase_ :Optional[Union[str, TensorType]] = None , lowerCamelCase_ :Optional[bool] = None , **lowerCamelCase_ :Tuple , ) -> BatchEncoding: '''simple docstring''' if titles is None and texts is None: return super().__call__( lowerCamelCase_ , padding=lowerCamelCase_ , truncation=lowerCamelCase_ , max_length=lowerCamelCase_ , return_tensors=lowerCamelCase_ , return_attention_mask=lowerCamelCase_ , **lowerCamelCase_ , ) elif titles is None or texts is None: SCREAMING_SNAKE_CASE : List[str] = titles if texts is None else texts return super().__call__( lowerCamelCase_ , lowerCamelCase_ , padding=lowerCamelCase_ , truncation=lowerCamelCase_ , max_length=lowerCamelCase_ , return_tensors=lowerCamelCase_ , return_attention_mask=lowerCamelCase_ , **lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Dict = titles if not isinstance(lowerCamelCase_ , lowerCamelCase_ ) else [titles] SCREAMING_SNAKE_CASE : Dict = texts if not isinstance(lowerCamelCase_ , lowerCamelCase_ ) else [texts] SCREAMING_SNAKE_CASE : Optional[int] = len(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = questions if not isinstance(lowerCamelCase_ , lowerCamelCase_ ) else [questions] * n_passages assert len(lowerCamelCase_ ) == len( lowerCamelCase_ ), f"There should be as many titles than texts but got {len(lowerCamelCase_ )} titles and {len(lowerCamelCase_ )} texts." SCREAMING_SNAKE_CASE : Any = super().__call__(lowerCamelCase_ , lowerCamelCase_ , padding=lowerCamelCase_ , truncation=lowerCamelCase_ )['''input_ids'''] SCREAMING_SNAKE_CASE : Dict = super().__call__(lowerCamelCase_ , add_special_tokens=lowerCamelCase_ , padding=lowerCamelCase_ , truncation=lowerCamelCase_ )['''input_ids'''] SCREAMING_SNAKE_CASE : int = { '''input_ids''': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(lowerCamelCase_ , lowerCamelCase_ ) ] } if return_attention_mask is not False: SCREAMING_SNAKE_CASE : List[str] = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) SCREAMING_SNAKE_CASE : int = attention_mask return self.pad(lowerCamelCase_ , padding=lowerCamelCase_ , max_length=lowerCamelCase_ , return_tensors=lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :BatchEncoding , lowerCamelCase_ :DPRReaderOutput , lowerCamelCase_ :int = 16 , lowerCamelCase_ :int = 64 , lowerCamelCase_ :int = 4 , ) -> List[DPRSpanPrediction]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = reader_input['''input_ids'''] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = reader_output[:3] SCREAMING_SNAKE_CASE : Dict = len(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = sorted(range(lowerCamelCase_ ) , reverse=lowerCamelCase_ , key=relevance_logits.__getitem__ ) SCREAMING_SNAKE_CASE : List[DPRReaderOutput] = [] for doc_id in sorted_docs: SCREAMING_SNAKE_CASE : Union[str, Any] = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence SCREAMING_SNAKE_CASE : int = sequence_ids.index(self.sep_token_id , 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: SCREAMING_SNAKE_CASE : Dict = sequence_ids.index(self.pad_token_id ) else: SCREAMING_SNAKE_CASE : Optional[int] = len(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=lowerCamelCase_ , top_spans=lowerCamelCase_ , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=lowerCamelCase_ , start_index=lowerCamelCase_ , end_index=lowerCamelCase_ , text=self.decode(sequence_ids[start_index : end_index + 1] ) , ) ) if len(lowerCamelCase_ ) >= num_spans: break return nbest_spans_predictions[:num_spans] def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :List[int] , lowerCamelCase_ :List[int] , lowerCamelCase_ :int , lowerCamelCase_ :int , ) -> List[DPRSpanPrediction]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = [] for start_index, start_score in enumerate(lowerCamelCase_ ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) SCREAMING_SNAKE_CASE : Dict = sorted(lowerCamelCase_ , key=lambda lowerCamelCase_ : x[1] , reverse=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = [] for (start_index, end_index), score in scores: assert start_index <= end_index, f"Wrong span indices: [{start_index}:{end_index}]" SCREAMING_SNAKE_CASE : Optional[int] = end_index - start_index + 1 assert length <= max_answer_length, f"Span is too long: {length} > {max_answer_length}" if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(lowerCamelCase_ ) == top_spans: break return chosen_span_intervals @add_end_docstrings(_UpperCAmelCase ) class lowercase__( _UpperCAmelCase , _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = VOCAB_FILES_NAMES UpperCamelCase = READER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase = READER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase = ["""input_ids""", """attention_mask"""] UpperCamelCase = DPRReaderTokenizer
698
1
"""simple docstring""" def __A ( a_ : int )-> bool: '''simple docstring''' return sum(i for i in range(1 , number // 2 + 1 ) if number % i == 0 ) == number if __name__ == "__main__": print("Program to check whether a number is a Perfect number or not...") lowerCamelCase__ : List[str] = int(input("Enter number: ").strip()) print(f'''{number} is {"" if perfect(number) else "not "}a Perfect Number.''')
698
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : Optional[Any] = { "microsoft/markuplm-base": "https://huggingface.co/microsoft/markuplm-base/resolve/main/config.json", "microsoft/markuplm-large": "https://huggingface.co/microsoft/markuplm-large/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """markuplm""" def __init__( self :int , lowerCamelCase_ :List[str]=3_05_22 , lowerCamelCase_ :Union[str, Any]=7_68 , lowerCamelCase_ :str=12 , lowerCamelCase_ :Dict=12 , lowerCamelCase_ :str=30_72 , lowerCamelCase_ :Union[str, Any]="gelu" , lowerCamelCase_ :Union[str, Any]=0.1 , lowerCamelCase_ :Optional[Any]=0.1 , lowerCamelCase_ :Union[str, Any]=5_12 , lowerCamelCase_ :Any=2 , lowerCamelCase_ :Optional[Any]=0.0_2 , lowerCamelCase_ :Any=1E-12 , lowerCamelCase_ :Dict=0 , lowerCamelCase_ :Optional[Any]=0 , lowerCamelCase_ :Optional[int]=2 , lowerCamelCase_ :str=2_56 , lowerCamelCase_ :List[Any]=10_24 , lowerCamelCase_ :Union[str, Any]=2_16 , lowerCamelCase_ :Dict=10_01 , lowerCamelCase_ :Any=32 , lowerCamelCase_ :str=50 , lowerCamelCase_ :List[str]="absolute" , lowerCamelCase_ :List[str]=True , lowerCamelCase_ :int=None , **lowerCamelCase_ :Dict , ) -> List[Any]: '''simple docstring''' super().__init__( pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , **lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : str = vocab_size SCREAMING_SNAKE_CASE : Optional[int] = hidden_size SCREAMING_SNAKE_CASE : int = num_hidden_layers SCREAMING_SNAKE_CASE : List[str] = num_attention_heads SCREAMING_SNAKE_CASE : List[str] = hidden_act SCREAMING_SNAKE_CASE : int = intermediate_size SCREAMING_SNAKE_CASE : Optional[int] = hidden_dropout_prob SCREAMING_SNAKE_CASE : List[str] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = max_position_embeddings SCREAMING_SNAKE_CASE : Tuple = type_vocab_size SCREAMING_SNAKE_CASE : Any = initializer_range SCREAMING_SNAKE_CASE : int = layer_norm_eps SCREAMING_SNAKE_CASE : int = position_embedding_type SCREAMING_SNAKE_CASE : Tuple = use_cache SCREAMING_SNAKE_CASE : str = classifier_dropout # additional properties SCREAMING_SNAKE_CASE : Optional[Any] = max_depth SCREAMING_SNAKE_CASE : Dict = max_xpath_tag_unit_embeddings SCREAMING_SNAKE_CASE : Optional[int] = max_xpath_subs_unit_embeddings SCREAMING_SNAKE_CASE : Tuple = tag_pad_id SCREAMING_SNAKE_CASE : str = subs_pad_id SCREAMING_SNAKE_CASE : List[Any] = xpath_unit_hidden_size
698
1
"""simple docstring""" # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import warnings from typing import List from unittest.mock import Mock import torch from torch.utils.data import DataLoader, IterableDataset, TensorDataset from accelerate.accelerator import Accelerator from accelerate.utils.dataclasses import DistributedType class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __init__( self :int , lowerCamelCase_ :Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = data def __iter__( self :Optional[int] ) -> Any: '''simple docstring''' for element in self.data: yield element def __A ( a_ : Union[str, Any]=True )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = Accelerator(even_batches=a_ ) assert accelerator.num_processes == 2, "this script expects that two GPUs are available" return accelerator def __A ( a_ : Accelerator , a_ : int , a_ : int , a_ : bool = False )-> Dict: '''simple docstring''' if iterable: SCREAMING_SNAKE_CASE : List[Any] = DummyIterableDataset(torch.as_tensor(range(a_ ) ) ) else: SCREAMING_SNAKE_CASE : str = TensorDataset(torch.as_tensor(range(a_ ) ) ) SCREAMING_SNAKE_CASE : Tuple = DataLoader(a_ , batch_size=a_ ) SCREAMING_SNAKE_CASE : Dict = accelerator.prepare(a_ ) return dl def __A ( a_ : Accelerator , a_ : int , a_ : int , a_ : List[int] , a_ : List[int] , )-> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = create_dataloader(accelerator=a_ , dataset_size=a_ , batch_size=a_ ) SCREAMING_SNAKE_CASE : Any = [len(batch[0] ) for batch in dl] if accelerator.process_index == 0: assert batch_sizes == process_0_expected_batch_sizes elif accelerator.process_index == 1: assert batch_sizes == process_1_expected_batch_sizes def __A ( )-> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : int = create_accelerator() # without padding, we would expect a different number of batches verify_dataloader_batch_sizes( a_ , dataset_size=3 , batch_size=1 , process_0_expected_batch_sizes=[1, 1] , process_1_expected_batch_sizes=[1, 1] , ) # without padding, we would expect the same number of batches, but different sizes verify_dataloader_batch_sizes( a_ , dataset_size=7 , batch_size=2 , process_0_expected_batch_sizes=[2, 2] , process_1_expected_batch_sizes=[2, 2] , ) def __A ( )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = create_accelerator(even_batches=a_ ) verify_dataloader_batch_sizes( a_ , dataset_size=3 , batch_size=1 , process_0_expected_batch_sizes=[1, 1] , process_1_expected_batch_sizes=[1] , ) verify_dataloader_batch_sizes( a_ , dataset_size=7 , batch_size=2 , process_0_expected_batch_sizes=[2, 2] , process_1_expected_batch_sizes=[2, 1] , ) def __A ( )-> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = create_accelerator(even_batches=a_ ) SCREAMING_SNAKE_CASE : Optional[Any] = torch.nn.Linear(1 , 1 ) SCREAMING_SNAKE_CASE : str = accelerator.prepare(a_ ) SCREAMING_SNAKE_CASE : str = create_dataloader(a_ , dataset_size=3 , batch_size=1 ) SCREAMING_SNAKE_CASE : Optional[Any] = [] with accelerator.join_uneven_inputs([ddp_model] ): for batch_idx, batch in enumerate(a_ ): SCREAMING_SNAKE_CASE : Tuple = ddp_model(batch[0].float() ) SCREAMING_SNAKE_CASE : Union[str, Any] = output.sum() loss.backward() batch_idxs.append(a_ ) accelerator.wait_for_everyone() if accelerator.process_index == 0: assert batch_idxs == [0, 1] elif accelerator.process_index == 1: assert batch_idxs == [0] def __A ( a_ : Union[str, Any] )-> str: '''simple docstring''' with warnings.catch_warnings(record=a_ ) as w: with accelerator.join_uneven_inputs([Mock()] ): pass assert issubclass(w[-1].category , a_ ) assert "only supported for multi-GPU" in str(w[-1].message ) def __A ( )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = True SCREAMING_SNAKE_CASE : Any = False SCREAMING_SNAKE_CASE : Tuple = create_accelerator(even_batches=a_ ) SCREAMING_SNAKE_CASE : Optional[Any] = torch.nn.Linear(1 , 1 ) SCREAMING_SNAKE_CASE : str = accelerator.prepare(a_ ) SCREAMING_SNAKE_CASE : List[Any] = create_dataloader(a_ , dataset_size=3 , batch_size=1 ) SCREAMING_SNAKE_CASE : Optional[Any] = create_dataloader(a_ , dataset_size=3 , batch_size=1 ) with accelerator.join_uneven_inputs([ddp_model] , even_batches=a_ ): SCREAMING_SNAKE_CASE : int = train_dl.batch_sampler.even_batches SCREAMING_SNAKE_CASE : Tuple = valid_dl.batch_sampler.even_batches assert train_dl_overridden_value == overridden_even_batches assert valid_dl_overridden_value == overridden_even_batches assert train_dl.batch_sampler.even_batches == default_even_batches assert valid_dl.batch_sampler.even_batches == default_even_batches def __A ( )-> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = True SCREAMING_SNAKE_CASE : List[Any] = False SCREAMING_SNAKE_CASE : Any = create_accelerator(even_batches=a_ ) SCREAMING_SNAKE_CASE : List[Any] = torch.nn.Linear(1 , 1 ) SCREAMING_SNAKE_CASE : Any = accelerator.prepare(a_ ) create_dataloader(a_ , dataset_size=3 , batch_size=1 , iterable=a_ ) SCREAMING_SNAKE_CASE : Optional[int] = create_dataloader(a_ , dataset_size=3 , batch_size=1 ) with warnings.catch_warnings(): warnings.filterwarnings('''ignore''' ) try: with accelerator.join_uneven_inputs([ddp_model] , even_batches=a_ ): SCREAMING_SNAKE_CASE : Dict = batch_dl.batch_sampler.even_batches except AttributeError: # ensure attribute error is not raised when processing iterable dl raise AssertionError assert batch_dl_overridden_value == overridden_even_batches assert batch_dl.batch_sampler.even_batches == default_even_batches def __A ( )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = create_accelerator() SCREAMING_SNAKE_CASE : Dict = torch.nn.Linear(1 , 1 ) SCREAMING_SNAKE_CASE : Tuple = accelerator.prepare(a_ ) create_dataloader(a_ , dataset_size=3 , batch_size=1 , iterable=a_ ) with warnings.catch_warnings(record=a_ ) as w: with accelerator.join_uneven_inputs([ddp_model] , even_batches=a_ ): pass assert issubclass(w[-1].category , a_ ) assert "only supported for map-style datasets" in str(w[-1].message ) def __A ( )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = create_accelerator() accelerator.print('''Test that even_batches variable ensures uniform batches across processes''' ) test_default_ensures_even_batch_sizes() accelerator.print('''Run tests with even_batches disabled''' ) test_can_disable_even_batches() accelerator.print('''Test joining uneven inputs''' ) test_can_join_uneven_inputs() accelerator.print('''Test overriding even_batches when joining uneven inputs''' ) test_join_can_override_even_batches() accelerator.print('''Test overriding even_batches for mixed dataloader types''' ) test_join_can_override_for_mixed_type_dataloaders() accelerator.print('''Test overriding even_batches raises a warning for iterable dataloaders''' ) test_join_raises_warning_for_iterable_when_overriding_even_batches() accelerator.print('''Test join with non DDP distributed raises warning''' ) SCREAMING_SNAKE_CASE : List[Any] = accelerator.state.distributed_type SCREAMING_SNAKE_CASE : str = DistributedType.FSDP test_join_raises_warning_for_non_ddp_distributed(a_ ) SCREAMING_SNAKE_CASE : Optional[int] = original_state if __name__ == "__main__": main()
698
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCamelCase__ : Optional[Any] = logging.get_logger(__name__) lowerCamelCase__ : Union[str, Any] = { "microsoft/resnet-50": "https://huggingface.co/microsoft/resnet-50/blob/main/config.json", } class lowercase__( _UpperCAmelCase , _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """resnet""" UpperCamelCase = ["""basic""", """bottleneck"""] def __init__( self :Optional[int] , lowerCamelCase_ :Tuple=3 , lowerCamelCase_ :Tuple=64 , lowerCamelCase_ :Union[str, Any]=[2_56, 5_12, 10_24, 20_48] , lowerCamelCase_ :int=[3, 4, 6, 3] , lowerCamelCase_ :Any="bottleneck" , lowerCamelCase_ :Optional[int]="relu" , lowerCamelCase_ :Optional[int]=False , lowerCamelCase_ :Any=None , lowerCamelCase_ :Optional[int]=None , **lowerCamelCase_ :Optional[int] , ) -> Tuple: '''simple docstring''' super().__init__(**lowerCamelCase_ ) if layer_type not in self.layer_types: raise ValueError(f"layer_type={layer_type} is not one of {','.join(self.layer_types )}" ) SCREAMING_SNAKE_CASE : Tuple = num_channels SCREAMING_SNAKE_CASE : Union[str, Any] = embedding_size SCREAMING_SNAKE_CASE : List[str] = hidden_sizes SCREAMING_SNAKE_CASE : Optional[Any] = depths SCREAMING_SNAKE_CASE : List[Any] = layer_type SCREAMING_SNAKE_CASE : str = hidden_act SCREAMING_SNAKE_CASE : Optional[Any] = downsample_in_first_stage SCREAMING_SNAKE_CASE : int = ['''stem'''] + [f"stage{idx}" for idx in range(1 , len(lowerCamelCase_ ) + 1 )] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = get_aligned_output_features_output_indices( out_features=lowerCamelCase_ , out_indices=lowerCamelCase_ , stage_names=self.stage_names ) class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = version.parse("""1.11""" ) @property def __lowerCAmelCase ( self :Optional[Any] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def __lowerCAmelCase ( self :str ) -> float: '''simple docstring''' return 1E-3
698
1
"""simple docstring""" from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, TensorType lowerCamelCase__ : Optional[Any] = logging.get_logger(__name__) lowerCamelCase__ : List[Any] = { "openai/imagegpt-small": "", "openai/imagegpt-medium": "", "openai/imagegpt-large": "", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """imagegpt""" UpperCamelCase = ["""past_key_values"""] UpperCamelCase = { """hidden_size""": """n_embd""", """max_position_embeddings""": """n_positions""", """num_attention_heads""": """n_head""", """num_hidden_layers""": """n_layer""", } def __init__( self :Union[str, Any] , lowerCamelCase_ :int=5_12 + 1 , lowerCamelCase_ :Tuple=32 * 32 , lowerCamelCase_ :List[Any]=5_12 , lowerCamelCase_ :List[Any]=24 , lowerCamelCase_ :Optional[Any]=8 , lowerCamelCase_ :Tuple=None , lowerCamelCase_ :Tuple="quick_gelu" , lowerCamelCase_ :List[Any]=0.1 , lowerCamelCase_ :Any=0.1 , lowerCamelCase_ :Optional[int]=0.1 , lowerCamelCase_ :Optional[int]=1E-5 , lowerCamelCase_ :Any=0.0_2 , lowerCamelCase_ :List[str]=True , lowerCamelCase_ :str=True , lowerCamelCase_ :int=False , lowerCamelCase_ :Optional[Any]=False , lowerCamelCase_ :List[str]=False , **lowerCamelCase_ :Dict , ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = vocab_size SCREAMING_SNAKE_CASE : Optional[int] = n_positions SCREAMING_SNAKE_CASE : Union[str, Any] = n_embd SCREAMING_SNAKE_CASE : List[Any] = n_layer SCREAMING_SNAKE_CASE : Any = n_head SCREAMING_SNAKE_CASE : Optional[int] = n_inner SCREAMING_SNAKE_CASE : Union[str, Any] = activation_function SCREAMING_SNAKE_CASE : List[str] = resid_pdrop SCREAMING_SNAKE_CASE : str = embd_pdrop SCREAMING_SNAKE_CASE : Any = attn_pdrop SCREAMING_SNAKE_CASE : List[str] = layer_norm_epsilon SCREAMING_SNAKE_CASE : Union[str, Any] = initializer_range SCREAMING_SNAKE_CASE : List[Any] = scale_attn_weights SCREAMING_SNAKE_CASE : int = use_cache SCREAMING_SNAKE_CASE : List[str] = scale_attn_by_inverse_layer_idx SCREAMING_SNAKE_CASE : str = reorder_and_upcast_attn SCREAMING_SNAKE_CASE : List[str] = tie_word_embeddings super().__init__(tie_word_embeddings=lowerCamelCase_ , **lowerCamelCase_ ) class lowercase__( _UpperCAmelCase ): '''simple docstring''' @property def __lowerCAmelCase ( self :Dict ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ] ) def __lowerCAmelCase ( self :str , lowerCamelCase_ :"FeatureExtractionMixin" , lowerCamelCase_ :int = 1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional["TensorType"] = None , lowerCamelCase_ :int = 3 , lowerCamelCase_ :int = 32 , lowerCamelCase_ :int = 32 , ) -> Mapping[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self._generate_dummy_images(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = dict(preprocessor(images=lowerCamelCase_ , return_tensors=lowerCamelCase_ ) ) return inputs
698
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : List[Any] = { "uw-madison/mra-base-512-4": "https://huggingface.co/uw-madison/mra-base-512-4/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """mra""" def __init__( self :int , lowerCamelCase_ :Optional[int]=5_02_65 , lowerCamelCase_ :List[str]=7_68 , lowerCamelCase_ :List[str]=12 , lowerCamelCase_ :Optional[Any]=12 , lowerCamelCase_ :int=30_72 , lowerCamelCase_ :Tuple="gelu" , lowerCamelCase_ :List[Any]=0.1 , lowerCamelCase_ :str=0.1 , lowerCamelCase_ :str=5_12 , lowerCamelCase_ :List[str]=1 , lowerCamelCase_ :int=0.0_2 , lowerCamelCase_ :int=1E-5 , lowerCamelCase_ :List[Any]="absolute" , lowerCamelCase_ :str=4 , lowerCamelCase_ :List[str]="full" , lowerCamelCase_ :List[Any]=0 , lowerCamelCase_ :Optional[Any]=0 , lowerCamelCase_ :Union[str, Any]=1 , lowerCamelCase_ :List[str]=0 , lowerCamelCase_ :List[Any]=2 , **lowerCamelCase_ :str , ) -> Dict: '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = vocab_size SCREAMING_SNAKE_CASE : Tuple = max_position_embeddings SCREAMING_SNAKE_CASE : List[Any] = hidden_size SCREAMING_SNAKE_CASE : Dict = num_hidden_layers SCREAMING_SNAKE_CASE : Tuple = num_attention_heads SCREAMING_SNAKE_CASE : Any = intermediate_size SCREAMING_SNAKE_CASE : Any = hidden_act SCREAMING_SNAKE_CASE : Tuple = hidden_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : str = initializer_range SCREAMING_SNAKE_CASE : Tuple = type_vocab_size SCREAMING_SNAKE_CASE : Union[str, Any] = layer_norm_eps SCREAMING_SNAKE_CASE : str = position_embedding_type SCREAMING_SNAKE_CASE : List[str] = block_per_row SCREAMING_SNAKE_CASE : Optional[int] = approx_mode SCREAMING_SNAKE_CASE : List[Any] = initial_prior_first_n_blocks SCREAMING_SNAKE_CASE : Union[str, Any] = initial_prior_diagonal_n_blocks
698
1
"""simple docstring""" import math def __A ( a_ : list , a_ : int = 0 , a_ : int = 0 )-> list: '''simple docstring''' SCREAMING_SNAKE_CASE : str = end or len(a_ ) for i in range(a_ , a_ ): SCREAMING_SNAKE_CASE : int = i SCREAMING_SNAKE_CASE : Tuple = array[i] while temp_index != start and temp_index_value < array[temp_index - 1]: SCREAMING_SNAKE_CASE : Union[str, Any] = array[temp_index - 1] temp_index -= 1 SCREAMING_SNAKE_CASE : Optional[Any] = temp_index_value return array def __A ( a_ : list , a_ : int , a_ : int )-> None: # Max Heap '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = index SCREAMING_SNAKE_CASE : Optional[int] = 2 * index + 1 # Left Node SCREAMING_SNAKE_CASE : int = 2 * index + 2 # Right Node if left_index < heap_size and array[largest] < array[left_index]: SCREAMING_SNAKE_CASE : Dict = left_index if right_index < heap_size and array[largest] < array[right_index]: SCREAMING_SNAKE_CASE : Tuple = right_index if largest != index: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Union[str, Any] = array[largest], array[index] heapify(a_ , a_ , a_ ) def __A ( a_ : list )-> list: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = len(a_ ) for i in range(n // 2 , -1 , -1 ): heapify(a_ , a_ , a_ ) for i in range(n - 1 , 0 , -1 ): SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = array[0], array[i] heapify(a_ , 0 , a_ ) return array def __A ( a_ : list , a_ : int , a_ : int , a_ : int )-> int: '''simple docstring''' if (array[first_index] > array[middle_index]) != ( array[first_index] > array[last_index] ): return array[first_index] elif (array[middle_index] > array[first_index]) != ( array[middle_index] > array[last_index] ): return array[middle_index] else: return array[last_index] def __A ( a_ : list , a_ : int , a_ : int , a_ : int )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = low SCREAMING_SNAKE_CASE : str = high while True: while array[i] < pivot: i += 1 j -= 1 while pivot < array[j]: j -= 1 if i >= j: return i SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = array[j], array[i] i += 1 def __A ( a_ : list )-> list: '''simple docstring''' if len(a_ ) == 0: return array SCREAMING_SNAKE_CASE : Optional[int] = 2 * math.ceil(math.loga(len(a_ ) ) ) SCREAMING_SNAKE_CASE : Any = 16 return intro_sort(a_ , 0 , len(a_ ) , a_ , a_ ) def __A ( a_ : list , a_ : int , a_ : int , a_ : int , a_ : int )-> list: '''simple docstring''' while end - start > size_threshold: if max_depth == 0: return heap_sort(a_ ) max_depth -= 1 SCREAMING_SNAKE_CASE : Optional[int] = median_of_a(a_ , a_ , start + ((end - start) // 2) + 1 , end - 1 ) SCREAMING_SNAKE_CASE : int = partition(a_ , a_ , a_ , a_ ) intro_sort(a_ , a_ , a_ , a_ , a_ ) SCREAMING_SNAKE_CASE : Optional[int] = p return insertion_sort(a_ , a_ , a_ ) if __name__ == "__main__": import doctest doctest.testmod() lowerCamelCase__ : Tuple = input("Enter numbers separated by a comma : ").strip() lowerCamelCase__ : str = [float(item) for item in user_input.split(",")] print(sort(unsorted))
698
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : str = logging.get_logger(__name__) lowerCamelCase__ : List[str] = { "facebook/nllb-moe-54B": "https://huggingface.co/facebook/nllb-moe-54b/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """nllb-moe""" UpperCamelCase = ["""past_key_values"""] UpperCamelCase = {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""} def __init__( self :List[str] , lowerCamelCase_ :Optional[int]=12_81_12 , lowerCamelCase_ :str=10_24 , lowerCamelCase_ :Any=12 , lowerCamelCase_ :Optional[int]=40_96 , lowerCamelCase_ :int=16 , lowerCamelCase_ :List[str]=12 , lowerCamelCase_ :Optional[int]=40_96 , lowerCamelCase_ :int=16 , lowerCamelCase_ :Union[str, Any]=0.0_5 , lowerCamelCase_ :Optional[int]=0.0_5 , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :Optional[Any]=True , lowerCamelCase_ :Tuple="relu" , lowerCamelCase_ :str=10_24 , lowerCamelCase_ :str=0.1 , lowerCamelCase_ :Optional[int]=0.1 , lowerCamelCase_ :List[str]=0.0 , lowerCamelCase_ :Optional[Any]=0.0_2 , lowerCamelCase_ :Optional[int]=2 , lowerCamelCase_ :Dict=True , lowerCamelCase_ :Any=False , lowerCamelCase_ :Optional[Any]="float32" , lowerCamelCase_ :Optional[Any]=False , lowerCamelCase_ :List[Any]=1_28 , lowerCamelCase_ :Any=64 , lowerCamelCase_ :Optional[int]=4 , lowerCamelCase_ :List[str]=4 , lowerCamelCase_ :Union[str, Any]=0.0_0_1 , lowerCamelCase_ :Optional[int]=0.0_0_1 , lowerCamelCase_ :List[str]="all" , lowerCamelCase_ :Optional[int]=False , lowerCamelCase_ :Any=False , lowerCamelCase_ :Tuple=1.0 , lowerCamelCase_ :Union[str, Any]=0.2 , lowerCamelCase_ :List[str]=1 , lowerCamelCase_ :Optional[int]=0 , lowerCamelCase_ :int=2 , lowerCamelCase_ :List[str]=False , **lowerCamelCase_ :int , ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : int = vocab_size SCREAMING_SNAKE_CASE : str = max_position_embeddings SCREAMING_SNAKE_CASE : str = d_model SCREAMING_SNAKE_CASE : Optional[int] = encoder_ffn_dim SCREAMING_SNAKE_CASE : Any = encoder_layers SCREAMING_SNAKE_CASE : Any = encoder_attention_heads SCREAMING_SNAKE_CASE : List[Any] = decoder_ffn_dim SCREAMING_SNAKE_CASE : str = decoder_layers SCREAMING_SNAKE_CASE : List[Any] = decoder_attention_heads SCREAMING_SNAKE_CASE : List[Any] = dropout SCREAMING_SNAKE_CASE : List[str] = attention_dropout SCREAMING_SNAKE_CASE : str = activation_dropout SCREAMING_SNAKE_CASE : Any = activation_function SCREAMING_SNAKE_CASE : Tuple = init_std SCREAMING_SNAKE_CASE : str = encoder_layerdrop SCREAMING_SNAKE_CASE : Union[str, Any] = decoder_layerdrop SCREAMING_SNAKE_CASE : List[Any] = use_cache SCREAMING_SNAKE_CASE : Optional[int] = encoder_layers SCREAMING_SNAKE_CASE : List[str] = scale_embedding # scale factor will be sqrt(d_model) if True SCREAMING_SNAKE_CASE : int = router_z_loss_coef SCREAMING_SNAKE_CASE : Any = router_aux_loss_coef SCREAMING_SNAKE_CASE : str = decoder_sparse_step SCREAMING_SNAKE_CASE : str = encoder_sparse_step SCREAMING_SNAKE_CASE : List[str] = num_experts SCREAMING_SNAKE_CASE : Union[str, Any] = expert_capacity SCREAMING_SNAKE_CASE : Tuple = router_bias if router_dtype not in ["float32", "float16", "bfloat16"]: raise ValueError(f"`router_dtype` must be one of 'float32', 'float16' or 'bfloat16', got {router_dtype}" ) SCREAMING_SNAKE_CASE : Union[str, Any] = router_dtype SCREAMING_SNAKE_CASE : Union[str, Any] = router_ignore_padding_tokens SCREAMING_SNAKE_CASE : int = batch_prioritized_routing SCREAMING_SNAKE_CASE : Optional[int] = second_expert_policy SCREAMING_SNAKE_CASE : Union[str, Any] = normalize_router_prob_before_dropping SCREAMING_SNAKE_CASE : Any = moe_eval_capacity_token_fraction SCREAMING_SNAKE_CASE : Optional[Any] = moe_token_dropout SCREAMING_SNAKE_CASE : Tuple = output_router_logits super().__init__( pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , is_encoder_decoder=lowerCamelCase_ , decoder_start_token_id=lowerCamelCase_ , **lowerCamelCase_ , )
698
1
"""simple docstring""" from ....configuration_utils import PretrainedConfig from ....utils import logging lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) lowerCamelCase__ : str = { "Visual-Attention-Network/van-base": ( "https://huggingface.co/Visual-Attention-Network/van-base/blob/main/config.json" ), } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """van""" def __init__( self :List[str] , lowerCamelCase_ :Any=2_24 , lowerCamelCase_ :List[str]=3 , lowerCamelCase_ :List[Any]=[7, 3, 3, 3] , lowerCamelCase_ :Tuple=[4, 2, 2, 2] , lowerCamelCase_ :Union[str, Any]=[64, 1_28, 3_20, 5_12] , lowerCamelCase_ :Optional[Any]=[3, 3, 12, 3] , lowerCamelCase_ :str=[8, 8, 4, 4] , lowerCamelCase_ :Union[str, Any]="gelu" , lowerCamelCase_ :List[Any]=0.0_2 , lowerCamelCase_ :Dict=1E-6 , lowerCamelCase_ :Optional[int]=1E-2 , lowerCamelCase_ :Optional[Any]=0.0 , lowerCamelCase_ :Optional[int]=0.0 , **lowerCamelCase_ :List[Any] , ) -> str: '''simple docstring''' super().__init__(**lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = image_size SCREAMING_SNAKE_CASE : Optional[Any] = num_channels SCREAMING_SNAKE_CASE : Dict = patch_sizes SCREAMING_SNAKE_CASE : List[str] = strides SCREAMING_SNAKE_CASE : str = hidden_sizes SCREAMING_SNAKE_CASE : Tuple = depths SCREAMING_SNAKE_CASE : List[str] = mlp_ratios SCREAMING_SNAKE_CASE : Optional[int] = hidden_act SCREAMING_SNAKE_CASE : Dict = initializer_range SCREAMING_SNAKE_CASE : str = layer_norm_eps SCREAMING_SNAKE_CASE : Any = layer_scale_init_value SCREAMING_SNAKE_CASE : Optional[Any] = drop_path_rate SCREAMING_SNAKE_CASE : List[str] = dropout_rate
698
"""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 lowerCamelCase__ : Union[str, Any] = "CompVis/stable-diffusion-v1-1" lowerCamelCase__ : Optional[Any] = "CompVis/stable-diffusion-v1-2" lowerCamelCase__ : Dict = "CompVis/stable-diffusion-v1-3" lowerCamelCase__ : List[str] = "CompVis/stable-diffusion-v1-4" class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __init__( self :Any , lowerCamelCase_ :AutoencoderKL , lowerCamelCase_ :CLIPTextModel , lowerCamelCase_ :CLIPTokenizer , lowerCamelCase_ :UNetaDConditionModel , lowerCamelCase_ :Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , lowerCamelCase_ :StableDiffusionSafetyChecker , lowerCamelCase_ :CLIPImageProcessor , lowerCamelCase_ :bool = True , ) -> List[str]: '''simple docstring''' super()._init_() SCREAMING_SNAKE_CASE : Tuple = StableDiffusionPipeline.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = StableDiffusionPipeline.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = StableDiffusionPipeline.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = StableDiffusionPipeline( vae=lowerCamelCase_ , text_encoder=lowerCamelCase_ , tokenizer=lowerCamelCase_ , unet=lowerCamelCase_ , scheduler=lowerCamelCase_ , safety_checker=lowerCamelCase_ , feature_extractor=lowerCamelCase_ , requires_safety_checker=lowerCamelCase_ , ) self.register_modules(pipelinea=self.pipea , pipelinea=self.pipea , pipelinea=self.pipea , pipelinea=self.pipea ) @property def __lowerCAmelCase ( self :Dict ) -> Dict[str, Any]: '''simple docstring''' return {k: getattr(self , lowerCamelCase_ ) for k in self.config.keys() if not k.startswith('''_''' )} def __lowerCAmelCase ( self :int , lowerCamelCase_ :Optional[Union[str, int]] = "auto" ) -> Tuple: '''simple docstring''' if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory SCREAMING_SNAKE_CASE : str = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(lowerCamelCase_ ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Dict: '''simple docstring''' self.enable_attention_slicing(lowerCamelCase_ ) @torch.no_grad() def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :List[str] , ) -> Tuple: '''simple docstring''' return self.pipea( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) @torch.no_grad() def __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :Tuple , ) -> Optional[Any]: '''simple docstring''' return self.pipea( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) @torch.no_grad() def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :Dict , ) -> List[str]: '''simple docstring''' return self.pipea( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) @torch.no_grad() def __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :List[Any] , ) -> Optional[Any]: '''simple docstring''' return self.pipea( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) @torch.no_grad() def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :Optional[Any] , ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = '''cuda''' if torch.cuda.is_available() else '''cpu''' self.to(lowerCamelCase_ ) # 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 SCREAMING_SNAKE_CASE : str = self.textaimg_sda_a( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) # Get first result from Stable Diffusion Checkpoint v1.2 SCREAMING_SNAKE_CASE : Optional[Any] = self.textaimg_sda_a( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) # Get first result from Stable Diffusion Checkpoint v1.3 SCREAMING_SNAKE_CASE : Tuple = self.textaimg_sda_a( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) # Get first result from Stable Diffusion Checkpoint v1.4 SCREAMING_SNAKE_CASE : Union[str, Any] = self.textaimg_sda_a( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) # 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]] )
698
1
"""simple docstring""" import argparse from transformers import BigBirdConfig, BigBirdForPreTraining, BigBirdForQuestionAnswering, load_tf_weights_in_big_bird from transformers.utils import logging logging.set_verbosity_info() def __A ( a_ : Any , a_ : Any , a_ : str , a_ : Tuple )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = BigBirdConfig.from_json_file(a_ ) print(F"Building PyTorch model from configuration: {config}" ) if is_trivia_qa: SCREAMING_SNAKE_CASE : Optional[Any] = BigBirdForQuestionAnswering(a_ ) else: SCREAMING_SNAKE_CASE : Tuple = BigBirdForPreTraining(a_ ) # Load weights from tf checkpoint load_tf_weights_in_big_bird(a_ , a_ , is_trivia_qa=a_ ) # Save pytorch-model print(F"Save PyTorch model to {pytorch_dump_path}" ) model.save_pretrained(a_ ) if __name__ == "__main__": lowerCamelCase__ : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument( "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path." ) parser.add_argument( "--big_bird_config_file", default=None, type=str, required=True, help=( "The config json file corresponding to the pre-trained BERT model. \n" "This specifies the model architecture." ), ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) parser.add_argument( "--is_trivia_qa", action="store_true", help="Whether to convert a model with a trivia_qa head." ) lowerCamelCase__ : Optional[Any] = parser.parse_args() convert_tf_checkpoint_to_pytorch( args.tf_checkpoint_path, args.big_bird_config_file, args.pytorch_dump_path, args.is_trivia_qa )
698
"""simple docstring""" def __A ( a_ : list , a_ : int = 0 )-> list: '''simple docstring''' SCREAMING_SNAKE_CASE : int = length or len(a_ ) SCREAMING_SNAKE_CASE : List[Any] = False for i in range(length - 1 ): if list_data[i] > list_data[i + 1]: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[Any] = list_data[i + 1], list_data[i] SCREAMING_SNAKE_CASE : Optional[Any] = True return list_data if not swapped else bubble_sort(a_ , length - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
698
1
"""simple docstring""" import gc import unittest import numpy as np import torch from diffusers import ( AudioDiffusionPipeline, AutoencoderKL, DDIMScheduler, DDPMScheduler, DiffusionPipeline, Mel, UNetaDConditionModel, UNetaDModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class lowercase__( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self :List[Any] ) -> Any: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() @property def __lowerCAmelCase ( self :Tuple ) -> int: '''simple docstring''' torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : List[Any] = UNetaDModel( sample_size=(32, 64) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(1_28, 1_28) , down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''') , ) return model @property def __lowerCAmelCase ( self :List[Any] ) -> int: '''simple docstring''' torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Any = UNetaDConditionModel( sample_size=(64, 32) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(1_28, 1_28) , down_block_types=('''CrossAttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''CrossAttnUpBlock2D''') , cross_attention_dim=10 , ) return model @property def __lowerCAmelCase ( self :Dict ) -> Optional[Any]: '''simple docstring''' torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Dict = AutoencoderKL( sample_size=(1_28, 64) , in_channels=1 , out_channels=1 , latent_channels=1 , layers_per_block=2 , block_out_channels=(1_28, 1_28) , down_block_types=('''DownEncoderBlock2D''', '''DownEncoderBlock2D''') , up_block_types=('''UpDecoderBlock2D''', '''UpDecoderBlock2D''') , ) SCREAMING_SNAKE_CASE : List[str] = UNetaDModel( sample_size=(64, 32) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(1_28, 1_28) , down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''') , ) return vqvae, unet @slow def __lowerCAmelCase ( self :List[str] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = '''cpu''' # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE : Any = Mel( x_res=self.dummy_unet.config.sample_size[1] , y_res=self.dummy_unet.config.sample_size[0] , ) SCREAMING_SNAKE_CASE : Optional[int] = DDPMScheduler() SCREAMING_SNAKE_CASE : Union[str, Any] = AudioDiffusionPipeline(vqvae=lowerCamelCase_ , unet=self.dummy_unet , mel=lowerCamelCase_ , scheduler=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = pipe.to(lowerCamelCase_ ) pipe.set_progress_bar_config(disable=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = torch.Generator(device=lowerCamelCase_ ).manual_seed(42 ) SCREAMING_SNAKE_CASE : Tuple = pipe(generator=lowerCamelCase_ , steps=4 ) SCREAMING_SNAKE_CASE : Any = output.audios[0] SCREAMING_SNAKE_CASE : int = output.images[0] SCREAMING_SNAKE_CASE : List[str] = torch.Generator(device=lowerCamelCase_ ).manual_seed(42 ) SCREAMING_SNAKE_CASE : List[Any] = pipe(generator=lowerCamelCase_ , steps=4 , return_dict=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = output[0][0] assert audio.shape == (1, (self.dummy_unet.config.sample_size[1] - 1) * mel.hop_length) assert ( image.height == self.dummy_unet.config.sample_size[0] and image.width == self.dummy_unet.config.sample_size[1] ) SCREAMING_SNAKE_CASE : Union[str, Any] = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] SCREAMING_SNAKE_CASE : Optional[int] = np.frombuffer(image_from_tuple.tobytes() , dtype='''uint8''' )[:10] SCREAMING_SNAKE_CASE : int = np.array([69, 2_55, 2_55, 2_55, 0, 0, 77, 1_81, 12, 1_27] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() == 0 SCREAMING_SNAKE_CASE : int = Mel( x_res=self.dummy_vqvae_and_unet[0].config.sample_size[1] , y_res=self.dummy_vqvae_and_unet[0].config.sample_size[0] , ) SCREAMING_SNAKE_CASE : List[Any] = DDIMScheduler() SCREAMING_SNAKE_CASE : Optional[Any] = self.dummy_vqvae_and_unet SCREAMING_SNAKE_CASE : List[str] = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0] , unet=dummy_vqvae_and_unet[1] , mel=lowerCamelCase_ , scheduler=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = pipe.to(lowerCamelCase_ ) pipe.set_progress_bar_config(disable=lowerCamelCase_ ) np.random.seed(0 ) SCREAMING_SNAKE_CASE : Any = np.random.uniform(-1 , 1 , ((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,) ) SCREAMING_SNAKE_CASE : Dict = torch.Generator(device=lowerCamelCase_ ).manual_seed(42 ) SCREAMING_SNAKE_CASE : str = pipe(raw_audio=lowerCamelCase_ , generator=lowerCamelCase_ , start_step=5 , steps=10 ) SCREAMING_SNAKE_CASE : int = output.images[0] assert ( image.height == self.dummy_vqvae_and_unet[0].config.sample_size[0] and image.width == self.dummy_vqvae_and_unet[0].config.sample_size[1] ) SCREAMING_SNAKE_CASE : List[Any] = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] SCREAMING_SNAKE_CASE : Optional[Any] = np.array([1_20, 1_17, 1_10, 1_09, 1_38, 1_67, 1_38, 1_48, 1_32, 1_21] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 SCREAMING_SNAKE_CASE : str = self.dummy_unet_condition SCREAMING_SNAKE_CASE : Optional[int] = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0] , unet=lowerCamelCase_ , mel=lowerCamelCase_ , scheduler=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = pipe.to(lowerCamelCase_ ) pipe.set_progress_bar_config(disable=lowerCamelCase_ ) np.random.seed(0 ) SCREAMING_SNAKE_CASE : Any = torch.rand((1, 1, 10) ) SCREAMING_SNAKE_CASE : Any = pipe(generator=lowerCamelCase_ , encoding=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = output.images[0] SCREAMING_SNAKE_CASE : Optional[int] = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] SCREAMING_SNAKE_CASE : List[Any] = np.array([1_07, 1_03, 1_20, 1_27, 1_42, 1_22, 1_13, 1_22, 97, 1_11] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 @slow @require_torch_gpu class lowercase__( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self :List[str] ) -> Dict: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __lowerCAmelCase ( self :Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = torch_device SCREAMING_SNAKE_CASE : Any = DiffusionPipeline.from_pretrained('''teticio/audio-diffusion-ddim-256''' ) SCREAMING_SNAKE_CASE : int = pipe.to(lowerCamelCase_ ) pipe.set_progress_bar_config(disable=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = torch.Generator(device=lowerCamelCase_ ).manual_seed(42 ) SCREAMING_SNAKE_CASE : str = pipe(generator=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = output.audios[0] SCREAMING_SNAKE_CASE : str = output.images[0] assert audio.shape == (1, (pipe.unet.config.sample_size[1] - 1) * pipe.mel.hop_length) assert image.height == pipe.unet.config.sample_size[0] and image.width == pipe.unet.config.sample_size[1] SCREAMING_SNAKE_CASE : int = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] SCREAMING_SNAKE_CASE : Dict = np.array([1_51, 1_67, 1_54, 1_44, 1_22, 1_34, 1_21, 1_05, 70, 26] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0
698
"""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 lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = 42 UpperCamelCase = 42 def __init__( self :List[str] , lowerCamelCase_ :UNetaDModel , lowerCamelCase_ :ScoreSdeVeScheduler ) -> int: '''simple docstring''' super().__init__() self.register_modules(unet=lowerCamelCase_ , scheduler=lowerCamelCase_ ) @torch.no_grad() def __call__( self :int , lowerCamelCase_ :int = 1 , lowerCamelCase_ :int = 20_00 , lowerCamelCase_ :Optional[Union[torch.Generator, List[torch.Generator]]] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , **lowerCamelCase_ :Union[str, Any] , ) -> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.unet.config.sample_size SCREAMING_SNAKE_CASE : List[str] = (batch_size, 3, img_size, img_size) SCREAMING_SNAKE_CASE : Any = self.unet SCREAMING_SNAKE_CASE : Dict = randn_tensor(lowerCamelCase_ , generator=lowerCamelCase_ ) * self.scheduler.init_noise_sigma SCREAMING_SNAKE_CASE : Union[str, Any] = sample.to(self.device ) self.scheduler.set_timesteps(lowerCamelCase_ ) self.scheduler.set_sigmas(lowerCamelCase_ ) for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): SCREAMING_SNAKE_CASE : Tuple = self.scheduler.sigmas[i] * torch.ones(shape[0] , device=self.device ) # correction step for _ in range(self.scheduler.config.correct_steps ): SCREAMING_SNAKE_CASE : Optional[Any] = self.unet(lowerCamelCase_ , lowerCamelCase_ ).sample SCREAMING_SNAKE_CASE : List[Any] = self.scheduler.step_correct(lowerCamelCase_ , lowerCamelCase_ , generator=lowerCamelCase_ ).prev_sample # prediction step SCREAMING_SNAKE_CASE : Any = model(lowerCamelCase_ , lowerCamelCase_ ).sample SCREAMING_SNAKE_CASE : Optional[Any] = self.scheduler.step_pred(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , generator=lowerCamelCase_ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Union[str, Any] = output.prev_sample, output.prev_sample_mean SCREAMING_SNAKE_CASE : List[str] = sample_mean.clamp(0 , 1 ) SCREAMING_SNAKE_CASE : Any = sample.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": SCREAMING_SNAKE_CASE : Any = self.numpy_to_pil(lowerCamelCase_ ) if not return_dict: return (sample,) return ImagePipelineOutput(images=lowerCamelCase_ )
698
1
"""simple docstring""" from itertools import product def __A ( a_ : int , a_ : int )-> list[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = sides_number SCREAMING_SNAKE_CASE : Union[str, Any] = max_face_number * dice_number SCREAMING_SNAKE_CASE : Tuple = [0] * (max_total + 1) SCREAMING_SNAKE_CASE : Optional[Any] = 1 SCREAMING_SNAKE_CASE : Optional[int] = range(a_ , max_face_number + 1 ) for dice_numbers in product(a_ , repeat=a_ ): SCREAMING_SNAKE_CASE : Union[str, Any] = sum(a_ ) totals_frequencies[total] += 1 return totals_frequencies def __A ( )-> float: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = total_frequency_distribution( sides_number=4 , dice_number=9 ) SCREAMING_SNAKE_CASE : Dict = total_frequency_distribution( sides_number=6 , dice_number=6 ) SCREAMING_SNAKE_CASE : str = 0 SCREAMING_SNAKE_CASE : Union[str, Any] = 9 SCREAMING_SNAKE_CASE : Optional[Any] = 4 * 9 SCREAMING_SNAKE_CASE : List[str] = 6 for peter_total in range(a_ , max_peter_total + 1 ): peter_wins_count += peter_totals_frequencies[peter_total] * sum( colin_totals_frequencies[min_colin_total:peter_total] ) SCREAMING_SNAKE_CASE : Optional[Any] = (4**9) * (6**6) SCREAMING_SNAKE_CASE : str = peter_wins_count / total_games_number SCREAMING_SNAKE_CASE : List[Any] = round(a_ , ndigits=7 ) return rounded_peter_win_probability if __name__ == "__main__": print(f'''{solution() = }''')
698
"""simple docstring""" import qiskit def __A ( a_ : int , a_ : int )-> qiskit.result.counts.Counts: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = qiskit.Aer.get_backend('''aer_simulator''' ) # Create a Quantum Circuit acting on the q register SCREAMING_SNAKE_CASE : str = qiskit.QuantumCircuit(a_ , a_ ) # Apply X (NOT) Gate to Qubits 0 & 1 circuit.x(0 ) circuit.x(1 ) # Map the quantum measurement to the classical bits circuit.measure([0, 1] , [0, 1] ) # Execute the circuit on the qasm simulator SCREAMING_SNAKE_CASE : int = qiskit.execute(a_ , a_ , shots=10_00 ) # Return the histogram data of the results of the experiment. return job.result().get_counts(a_ ) if __name__ == "__main__": lowerCamelCase__ : List[Any] = single_qubit_measure(2, 2) print(f'''Total count for various states are: {counts}''')
698
1
"""simple docstring""" import argparse from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_controlnet_from_original_ckpt if __name__ == "__main__": lowerCamelCase__ : Dict = argparse.ArgumentParser() parser.add_argument( "--checkpoint_path", default=None, type=str, required=True, help="Path to the checkpoint to convert." ) parser.add_argument( "--original_config_file", type=str, required=True, help="The YAML config file corresponding to the original architecture.", ) parser.add_argument( "--num_in_channels", default=None, type=int, help="The number of input channels. If `None` number of input channels will be automatically inferred.", ) parser.add_argument( "--image_size", default=512, type=int, help=( "The image size that the model was trained on. Use 512 for Stable Diffusion v1.X and Stable Siffusion v2" " Base. Use 768 for Stable Diffusion v2." ), ) parser.add_argument( "--extract_ema", action="store_true", help=( "Only relevant for checkpoints that have both EMA and non-EMA weights. Whether to extract the EMA weights" " or not. Defaults to `False`. Add `--extract_ema` to extract the EMA weights. EMA weights usually yield" " higher quality images for inference. Non-EMA weights are usually better to continue fine-tuning." ), ) parser.add_argument( "--upcast_attention", action="store_true", help=( "Whether the attention computation should always be upcasted. This is necessary when running stable" " diffusion 2.1." ), ) parser.add_argument( "--from_safetensors", action="store_true", help="If `--checkpoint_path` is in `safetensors` format, load checkpoint with safetensors instead of PyTorch.", ) parser.add_argument( "--to_safetensors", action="store_true", help="Whether to store pipeline in safetensors format or not.", ) parser.add_argument("--dump_path", default=None, type=str, required=True, help="Path to the output model.") parser.add_argument("--device", type=str, help="Device to use (e.g. cpu, cuda:0, cuda:1, etc.)") def __A ( a_ : Optional[int] )-> Optional[int]: '''simple docstring''' if string == "True": return True elif string == "False": return False else: raise ValueError(F"could not parse string as bool {string}" ) parser.add_argument( "--use_linear_projection", help="Override for use linear projection", required=False, type=parse_bool ) parser.add_argument("--cross_attention_dim", help="Override for cross attention_dim", required=False, type=int) lowerCamelCase__ : List[str] = parser.parse_args() lowerCamelCase__ : Dict = download_controlnet_from_original_ckpt( checkpoint_path=args.checkpoint_path, original_config_file=args.original_config_file, image_size=args.image_size, extract_ema=args.extract_ema, num_in_channels=args.num_in_channels, upcast_attention=args.upcast_attention, from_safetensors=args.from_safetensors, device=args.device, use_linear_projection=args.use_linear_projection, cross_attention_dim=args.cross_attention_dim, ) controlnet.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)
698
"""simple docstring""" # tests directory-specific settings - this file is run automatically # by pytest before any tests are run import doctest import sys import warnings from os.path import abspath, dirname, join import _pytest from transformers.testing_utils import HfDoctestModule, HfDocTestParser # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. lowerCamelCase__ : Optional[int] = abspath(join(dirname(__file__), "src")) sys.path.insert(1, git_repo_path) # silence FutureWarning warnings in tests since often we can't act on them until # they become normal warnings - i.e. the tests still need to test the current functionality warnings.simplefilter(action="ignore", category=FutureWarning) def __A ( a_ : Dict )-> str: '''simple docstring''' config.addinivalue_line( '''markers''' , '''is_pt_tf_cross_test: mark test to run only when PT and TF interactions are tested''' ) config.addinivalue_line( '''markers''' , '''is_pt_flax_cross_test: mark test to run only when PT and FLAX interactions are tested''' ) config.addinivalue_line('''markers''' , '''is_pipeline_test: mark test to run only when pipelines are tested''' ) config.addinivalue_line('''markers''' , '''is_staging_test: mark test to run only in the staging environment''' ) config.addinivalue_line('''markers''' , '''accelerate_tests: mark test that require accelerate''' ) config.addinivalue_line('''markers''' , '''tool_tests: mark the tool tests that are run on their specific schedule''' ) def __A ( a_ : Dict )-> Tuple: '''simple docstring''' from transformers.testing_utils import pytest_addoption_shared pytest_addoption_shared(a_ ) def __A ( a_ : Union[str, Any] )-> List[Any]: '''simple docstring''' from transformers.testing_utils import pytest_terminal_summary_main SCREAMING_SNAKE_CASE : List[str] = terminalreporter.config.getoption('''--make-reports''' ) if make_reports: pytest_terminal_summary_main(a_ , id=a_ ) def __A ( a_ : Dict , a_ : List[str] )-> Dict: '''simple docstring''' if exitstatus == 5: SCREAMING_SNAKE_CASE : List[str] = 0 # Doctest custom flag to ignore output. lowerCamelCase__ : Tuple = doctest.register_optionflag("IGNORE_RESULT") lowerCamelCase__ : Optional[int] = doctest.OutputChecker class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :int , lowerCamelCase_ :int , lowerCamelCase_ :Optional[Any] ) -> Dict: '''simple docstring''' if IGNORE_RESULT & optionflags: return True return OutputChecker.check_output(self , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) lowerCamelCase__ : str = CustomOutputChecker lowerCamelCase__ : Any = HfDoctestModule lowerCamelCase__ : int = HfDocTestParser
698
1
"""simple docstring""" import os import tempfile import unittest from pathlib import Path from transformers import AutoConfig, is_torch_available from transformers.testing_utils import require_torch, torch_device if is_torch_available(): from transformers import PyTorchBenchmark, PyTorchBenchmarkArguments @require_torch class lowercase__( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :Optional[Any] ) -> Optional[Any]: '''simple docstring''' for model_result in results.values(): for batch_size, sequence_length in zip(model_result['''bs'''] , model_result['''ss'''] ): SCREAMING_SNAKE_CASE : int = model_result['''result'''][batch_size][sequence_length] self.assertIsNotNone(lowerCamelCase_ ) def __lowerCAmelCase ( self :int ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = '''sshleifer/tiny-gpt2''' SCREAMING_SNAKE_CASE : str = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=lowerCamelCase_ , inference=lowerCamelCase_ , sequence_lengths=[8] , batch_sizes=[1] , multi_process=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Optional[int] = PyTorchBenchmark(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def __lowerCAmelCase ( self :Union[str, Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = '''sgugger/tiny-distilbert-classification''' SCREAMING_SNAKE_CASE : List[Any] = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=lowerCamelCase_ , inference=lowerCamelCase_ , sequence_lengths=[8] , batch_sizes=[1] , multi_process=lowerCamelCase_ , only_pretrain_model=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : int = PyTorchBenchmark(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def __lowerCAmelCase ( self :Any ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = '''sshleifer/tiny-gpt2''' SCREAMING_SNAKE_CASE : str = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=lowerCamelCase_ , inference=lowerCamelCase_ , torchscript=lowerCamelCase_ , sequence_lengths=[8] , batch_sizes=[1] , multi_process=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Union[str, Any] = PyTorchBenchmark(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) @unittest.skipIf(torch_device == '''cpu''' , '''Cant do half precision''' ) def __lowerCAmelCase ( self :Optional[int] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = '''sshleifer/tiny-gpt2''' SCREAMING_SNAKE_CASE : Dict = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=lowerCamelCase_ , inference=lowerCamelCase_ , fpaa=lowerCamelCase_ , sequence_lengths=[8] , batch_sizes=[1] , multi_process=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Optional[int] = PyTorchBenchmark(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def __lowerCAmelCase ( self :List[str] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = '''sshleifer/tiny-gpt2''' SCREAMING_SNAKE_CASE : List[str] = AutoConfig.from_pretrained(lowerCamelCase_ ) # set architectures equal to `None` SCREAMING_SNAKE_CASE : List[str] = None SCREAMING_SNAKE_CASE : Dict = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=lowerCamelCase_ , inference=lowerCamelCase_ , sequence_lengths=[8] , batch_sizes=[1] , multi_process=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : List[str] = PyTorchBenchmark(lowerCamelCase_ , configs=[config] ) SCREAMING_SNAKE_CASE : List[Any] = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def __lowerCAmelCase ( self :Tuple ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = '''sshleifer/tiny-gpt2''' SCREAMING_SNAKE_CASE : Optional[int] = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=lowerCamelCase_ , inference=lowerCamelCase_ , sequence_lengths=[8] , batch_sizes=[1] , multi_process=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Optional[int] = PyTorchBenchmark(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) @unittest.skipIf(torch_device == '''cpu''' , '''Can\'t do half precision''' ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = '''sshleifer/tiny-gpt2''' SCREAMING_SNAKE_CASE : List[Any] = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=lowerCamelCase_ , inference=lowerCamelCase_ , sequence_lengths=[8] , batch_sizes=[1] , fpaa=lowerCamelCase_ , multi_process=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : int = PyTorchBenchmark(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) def __lowerCAmelCase ( self :Any ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = '''sshleifer/tiny-gpt2''' SCREAMING_SNAKE_CASE : Any = AutoConfig.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=lowerCamelCase_ , inference=lowerCamelCase_ , sequence_lengths=[8] , batch_sizes=[1] , multi_process=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Optional[int] = PyTorchBenchmark(lowerCamelCase_ , configs=[config] ) SCREAMING_SNAKE_CASE : Optional[int] = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def __lowerCAmelCase ( self :str ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = '''sshleifer/tinier_bart''' SCREAMING_SNAKE_CASE : Tuple = AutoConfig.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=lowerCamelCase_ , inference=lowerCamelCase_ , sequence_lengths=[8] , batch_sizes=[1] , multi_process=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Union[str, Any] = PyTorchBenchmark(lowerCamelCase_ , configs=[config] ) SCREAMING_SNAKE_CASE : Optional[Any] = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def __lowerCAmelCase ( self :Tuple ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : str = '''sshleifer/tiny-gpt2''' SCREAMING_SNAKE_CASE : Tuple = AutoConfig.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=lowerCamelCase_ , inference=lowerCamelCase_ , sequence_lengths=[8] , batch_sizes=[1] , multi_process=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Dict = PyTorchBenchmark(lowerCamelCase_ , configs=[config] ) SCREAMING_SNAKE_CASE : Optional[Any] = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = '''sshleifer/tinier_bart''' SCREAMING_SNAKE_CASE : List[Any] = AutoConfig.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=lowerCamelCase_ , inference=lowerCamelCase_ , sequence_lengths=[8] , batch_sizes=[1] , multi_process=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Optional[int] = PyTorchBenchmark(lowerCamelCase_ , configs=[config] ) SCREAMING_SNAKE_CASE : Any = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) def __lowerCAmelCase ( self :Any ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = '''sshleifer/tiny-gpt2''' with tempfile.TemporaryDirectory() as tmp_dir: SCREAMING_SNAKE_CASE : List[str] = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=lowerCamelCase_ , inference=lowerCamelCase_ , save_to_csv=lowerCamelCase_ , sequence_lengths=[8] , batch_sizes=[1] , inference_time_csv_file=os.path.join(lowerCamelCase_ , '''inf_time.csv''' ) , train_memory_csv_file=os.path.join(lowerCamelCase_ , '''train_mem.csv''' ) , inference_memory_csv_file=os.path.join(lowerCamelCase_ , '''inf_mem.csv''' ) , train_time_csv_file=os.path.join(lowerCamelCase_ , '''train_time.csv''' ) , env_info_csv_file=os.path.join(lowerCamelCase_ , '''env.csv''' ) , multi_process=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Any = PyTorchBenchmark(lowerCamelCase_ ) benchmark.run() self.assertTrue(Path(os.path.join(lowerCamelCase_ , '''inf_time.csv''' ) ).exists() ) self.assertTrue(Path(os.path.join(lowerCamelCase_ , '''train_time.csv''' ) ).exists() ) self.assertTrue(Path(os.path.join(lowerCamelCase_ , '''inf_mem.csv''' ) ).exists() ) self.assertTrue(Path(os.path.join(lowerCamelCase_ , '''train_mem.csv''' ) ).exists() ) self.assertTrue(Path(os.path.join(lowerCamelCase_ , '''env.csv''' ) ).exists() ) def __lowerCAmelCase ( self :Dict ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = '''sshleifer/tiny-gpt2''' def _check_summary_is_not_empty(lowerCamelCase_ :List[str] ): self.assertTrue(hasattr(lowerCamelCase_ , '''sequential''' ) ) self.assertTrue(hasattr(lowerCamelCase_ , '''cumulative''' ) ) self.assertTrue(hasattr(lowerCamelCase_ , '''current''' ) ) self.assertTrue(hasattr(lowerCamelCase_ , '''total''' ) ) with tempfile.TemporaryDirectory() as tmp_dir: SCREAMING_SNAKE_CASE : Dict = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=lowerCamelCase_ , inference=lowerCamelCase_ , sequence_lengths=[8] , batch_sizes=[1] , log_filename=os.path.join(lowerCamelCase_ , '''log.txt''' ) , log_print=lowerCamelCase_ , trace_memory_line_by_line=lowerCamelCase_ , multi_process=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Optional[Any] = PyTorchBenchmark(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = benchmark.run() _check_summary_is_not_empty(result.inference_summary ) _check_summary_is_not_empty(result.train_summary ) self.assertTrue(Path(os.path.join(lowerCamelCase_ , '''log.txt''' ) ).exists() )
698
"""simple docstring""" from __future__ import annotations import os import tempfile import unittest from transformers import ConvBertConfig, 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 ( TFConvBertForMaskedLM, TFConvBertForMultipleChoice, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertModel, ) class lowercase__: '''simple docstring''' def __init__( self :Tuple , lowerCamelCase_ :Tuple , lowerCamelCase_ :Tuple=13 , lowerCamelCase_ :List[str]=7 , lowerCamelCase_ :Dict=True , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :List[str]=True , lowerCamelCase_ :Dict=True , lowerCamelCase_ :str=99 , lowerCamelCase_ :Optional[Any]=32 , lowerCamelCase_ :Tuple=2 , lowerCamelCase_ :int=4 , lowerCamelCase_ :Optional[Any]=37 , lowerCamelCase_ :Any="gelu" , lowerCamelCase_ :Any=0.1 , lowerCamelCase_ :Any=0.1 , lowerCamelCase_ :Optional[int]=5_12 , lowerCamelCase_ :str=16 , lowerCamelCase_ :Optional[int]=2 , lowerCamelCase_ :List[str]=0.0_2 , lowerCamelCase_ :int=3 , lowerCamelCase_ :List[Any]=4 , lowerCamelCase_ :Optional[Any]=None , ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : int = parent SCREAMING_SNAKE_CASE : str = 13 SCREAMING_SNAKE_CASE : str = 7 SCREAMING_SNAKE_CASE : List[Any] = True SCREAMING_SNAKE_CASE : List[str] = True SCREAMING_SNAKE_CASE : Union[str, Any] = True SCREAMING_SNAKE_CASE : str = True SCREAMING_SNAKE_CASE : Any = 99 SCREAMING_SNAKE_CASE : Dict = 3_84 SCREAMING_SNAKE_CASE : List[str] = 2 SCREAMING_SNAKE_CASE : int = 4 SCREAMING_SNAKE_CASE : Any = 37 SCREAMING_SNAKE_CASE : List[str] = '''gelu''' SCREAMING_SNAKE_CASE : List[str] = 0.1 SCREAMING_SNAKE_CASE : int = 0.1 SCREAMING_SNAKE_CASE : Union[str, Any] = 5_12 SCREAMING_SNAKE_CASE : int = 16 SCREAMING_SNAKE_CASE : List[str] = 2 SCREAMING_SNAKE_CASE : Tuple = 0.0_2 SCREAMING_SNAKE_CASE : List[str] = 3 SCREAMING_SNAKE_CASE : Union[str, Any] = 4 SCREAMING_SNAKE_CASE : str = 1_28 SCREAMING_SNAKE_CASE : List[str] = 2 SCREAMING_SNAKE_CASE : Union[str, Any] = 9 SCREAMING_SNAKE_CASE : Dict = 1 SCREAMING_SNAKE_CASE : List[str] = None def __lowerCAmelCase ( self :Optional[Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE : int = None if self.use_input_mask: SCREAMING_SNAKE_CASE : Tuple = random_attention_mask([self.batch_size, self.seq_length] ) SCREAMING_SNAKE_CASE : List[Any] = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) SCREAMING_SNAKE_CASE : List[str] = None SCREAMING_SNAKE_CASE : str = None SCREAMING_SNAKE_CASE : str = None if self.use_labels: SCREAMING_SNAKE_CASE : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor([self.batch_size] , self.num_choices ) SCREAMING_SNAKE_CASE : List[str] = ConvBertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , return_dict=lowerCamelCase_ , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def __lowerCAmelCase ( self :str , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :int , lowerCamelCase_ :List[str] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int , lowerCamelCase_ :List[Any] , lowerCamelCase_ :Tuple ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = TFConvBertModel(config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids} SCREAMING_SNAKE_CASE : Dict = [input_ids, input_mask] SCREAMING_SNAKE_CASE : List[Any] = model(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = model(lowerCamelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __lowerCAmelCase ( self :str , lowerCamelCase_ :List[Any] , lowerCamelCase_ :List[str] , lowerCamelCase_ :int , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :str , lowerCamelCase_ :str , lowerCamelCase_ :Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = TFConvBertForMaskedLM(config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } SCREAMING_SNAKE_CASE : Tuple = model(lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :str , lowerCamelCase_ :Any , lowerCamelCase_ :str , lowerCamelCase_ :Dict , lowerCamelCase_ :int , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.num_labels SCREAMING_SNAKE_CASE : Dict = TFConvBertForSequenceClassification(config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } SCREAMING_SNAKE_CASE : List[Any] = model(lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __lowerCAmelCase ( self :int , lowerCamelCase_ :Dict , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Dict , lowerCamelCase_ :Any , lowerCamelCase_ :Dict , lowerCamelCase_ :List[Any] , lowerCamelCase_ :List[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.num_choices SCREAMING_SNAKE_CASE : Optional[Any] = TFConvBertForMultipleChoice(config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = tf.tile(tf.expand_dims(lowerCamelCase_ , 1 ) , (1, self.num_choices, 1) ) SCREAMING_SNAKE_CASE : Dict = tf.tile(tf.expand_dims(lowerCamelCase_ , 1 ) , (1, self.num_choices, 1) ) SCREAMING_SNAKE_CASE : List[Any] = tf.tile(tf.expand_dims(lowerCamelCase_ , 1 ) , (1, self.num_choices, 1) ) SCREAMING_SNAKE_CASE : Any = { '''input_ids''': multiple_choice_inputs_ids, '''attention_mask''': multiple_choice_input_mask, '''token_type_ids''': multiple_choice_token_type_ids, } SCREAMING_SNAKE_CASE : Optional[int] = model(lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :Tuple , lowerCamelCase_ :Any , lowerCamelCase_ :List[Any] , lowerCamelCase_ :Tuple , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Tuple , lowerCamelCase_ :List[str] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.num_labels SCREAMING_SNAKE_CASE : List[Any] = TFConvBertForTokenClassification(config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } SCREAMING_SNAKE_CASE : int = model(lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Tuple , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :List[str] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = TFConvBertForQuestionAnswering(config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } SCREAMING_SNAKE_CASE : Dict = model(lowerCamelCase_ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __lowerCAmelCase ( self :List[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ) : Optional[Any] = config_and_inputs SCREAMING_SNAKE_CASE : Dict = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class lowercase__( _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase = ( ( TFConvBertModel, TFConvBertForMaskedLM, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertForMultipleChoice, ) if is_tf_available() else () ) UpperCamelCase = ( { """feature-extraction""": TFConvBertModel, """fill-mask""": TFConvBertForMaskedLM, """question-answering""": TFConvBertForQuestionAnswering, """text-classification""": TFConvBertForSequenceClassification, """token-classification""": TFConvBertForTokenClassification, """zero-shot""": TFConvBertForSequenceClassification, } if is_tf_available() else {} ) UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False def __lowerCAmelCase ( self :Optional[int] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = TFConvBertModelTester(self ) SCREAMING_SNAKE_CASE : int = ConfigTester(self , config_class=lowerCamelCase_ , hidden_size=37 ) def __lowerCAmelCase ( self :List[str] ) -> Union[str, Any]: '''simple docstring''' self.config_tester.run_common_tests() def __lowerCAmelCase ( self :Dict ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[int] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase_ ) def __lowerCAmelCase ( self :List[Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase_ ) def __lowerCAmelCase ( self :int ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Any ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*lowerCamelCase_ ) @slow def __lowerCAmelCase ( self :int ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : List[Any] = True SCREAMING_SNAKE_CASE : Tuple = True if hasattr(lowerCamelCase_ , '''use_cache''' ): SCREAMING_SNAKE_CASE : Any = True SCREAMING_SNAKE_CASE : str = getattr(self.model_tester , '''encoder_seq_length''' , self.model_tester.seq_length ) SCREAMING_SNAKE_CASE : Optional[int] = getattr(self.model_tester , '''key_length''' , lowerCamelCase_ ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : str = self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = model_class(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = len(model(lowerCamelCase_ ) ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(lowerCamelCase_ , saved_model=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = os.path.join(lowerCamelCase_ , '''saved_model''' , '''1''' ) SCREAMING_SNAKE_CASE : Tuple = tf.keras.models.load_model(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = model(lowerCamelCase_ ) if self.is_encoder_decoder: SCREAMING_SNAKE_CASE : Optional[int] = outputs['''encoder_hidden_states'''] SCREAMING_SNAKE_CASE : str = outputs['''encoder_attentions'''] else: SCREAMING_SNAKE_CASE : List[str] = outputs['''hidden_states'''] SCREAMING_SNAKE_CASE : List[Any] = outputs['''attentions'''] self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = getattr( self.model_tester , '''expected_num_hidden_layers''' , self.model_tester.num_hidden_layers + 1 ) self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) self.assertListEqual( list(output_hidden_states[0].shape[-2:] ) , [self.model_tester.seq_length, self.model_tester.hidden_size] , ) self.assertEqual(len(lowerCamelCase_ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(output_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length] , ) @slow def __lowerCAmelCase ( self :Any ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = TFConvBertModel.from_pretrained('''YituTech/conv-bert-base''' ) self.assertIsNotNone(lowerCamelCase_ ) def __lowerCAmelCase ( self :Tuple ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : str = True SCREAMING_SNAKE_CASE : List[str] = getattr(self.model_tester , '''decoder_seq_length''' , self.model_tester.seq_length ) SCREAMING_SNAKE_CASE : List[str] = getattr(self.model_tester , '''encoder_seq_length''' , self.model_tester.seq_length ) SCREAMING_SNAKE_CASE : List[str] = getattr(self.model_tester , '''key_length''' , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = getattr(self.model_tester , '''key_length''' , lowerCamelCase_ ) def check_decoder_attentions_output(lowerCamelCase_ :Optional[Any] ): SCREAMING_SNAKE_CASE : Any = len(lowerCamelCase_ ) self.assertEqual(out_len % 2 , 0 ) SCREAMING_SNAKE_CASE : int = outputs.decoder_attentions self.assertEqual(len(lowerCamelCase_ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length] , ) def check_encoder_attentions_output(lowerCamelCase_ :Optional[int] ): SCREAMING_SNAKE_CASE : List[Any] = [ t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions) ] self.assertEqual(len(lowerCamelCase_ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length] , ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Optional[int] = True SCREAMING_SNAKE_CASE : List[str] = False SCREAMING_SNAKE_CASE : str = model_class(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = model(self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Any = len(lowerCamelCase_ ) self.assertEqual(config.output_hidden_states , lowerCamelCase_ ) check_encoder_attentions_output(lowerCamelCase_ ) if self.is_encoder_decoder: SCREAMING_SNAKE_CASE : Union[str, Any] = model_class(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = model(self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) self.assertEqual(config.output_hidden_states , lowerCamelCase_ ) check_decoder_attentions_output(lowerCamelCase_ ) # Check that output attentions can also be changed via the config del inputs_dict["output_attentions"] SCREAMING_SNAKE_CASE : List[Any] = True SCREAMING_SNAKE_CASE : List[str] = model_class(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = model(self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) self.assertEqual(config.output_hidden_states , lowerCamelCase_ ) check_encoder_attentions_output(lowerCamelCase_ ) # Check attention is always last and order is fine SCREAMING_SNAKE_CASE : Optional[int] = True SCREAMING_SNAKE_CASE : str = True SCREAMING_SNAKE_CASE : Optional[Any] = model_class(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = model(self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1) , len(lowerCamelCase_ ) ) self.assertEqual(model.config.output_hidden_states , lowerCamelCase_ ) check_encoder_attentions_output(lowerCamelCase_ ) @require_tf class lowercase__( unittest.TestCase ): '''simple docstring''' @slow def __lowerCAmelCase ( self :int ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = TFConvBertModel.from_pretrained('''YituTech/conv-bert-base''' ) SCREAMING_SNAKE_CASE : Any = tf.constant([[0, 1, 2, 3, 4, 5]] ) SCREAMING_SNAKE_CASE : List[Any] = model(lowerCamelCase_ )[0] SCREAMING_SNAKE_CASE : Optional[Any] = [1, 6, 7_68] self.assertEqual(output.shape , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = tf.constant( [ [ [-0.0_3_4_7_5_4_9_3, -0.4_6_8_6_0_3_4, -0.3_0_6_3_8_8_3_2], [0.2_2_6_3_7_2_4_8, -0.2_6_9_8_8_6_4_6, -0.7_4_2_3_4_2_4], [0.1_0_3_2_4_8_6_8, -0.4_5_0_1_3_5_0_8, -0.5_8_2_8_0_7_8_4], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , lowerCamelCase_ , atol=1E-4 )
698
1
"""simple docstring""" import argparse import glob import logging import os from argparse import Namespace from importlib import import_module import numpy as np import torch from lightning_base import BaseTransformer, add_generic_args, generic_train from seqeval.metrics import accuracy_score, fa_score, precision_score, recall_score from torch.nn import CrossEntropyLoss from torch.utils.data import DataLoader, TensorDataset from utils_ner import TokenClassificationTask lowerCamelCase__ : List[Any] = logging.getLogger(__name__) class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """token-classification""" def __init__( self :Optional[Any] , lowerCamelCase_ :List[str] ) -> List[Any]: '''simple docstring''' if type(lowerCamelCase_ ) == dict: SCREAMING_SNAKE_CASE : List[Any] = Namespace(**lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = import_module('''tasks''' ) try: SCREAMING_SNAKE_CASE : Tuple = getattr(lowerCamelCase_ , hparams.task_type ) SCREAMING_SNAKE_CASE : TokenClassificationTask = token_classification_task_clazz() except AttributeError: raise ValueError( f"Task {hparams.task_type} needs to be defined as a TokenClassificationTask subclass in {module}. " f"Available tasks classes are: {TokenClassificationTask.__subclasses__()}" ) SCREAMING_SNAKE_CASE : List[Any] = self.token_classification_task.get_labels(hparams.labels ) SCREAMING_SNAKE_CASE : Union[str, Any] = CrossEntropyLoss().ignore_index super().__init__(lowerCamelCase_ , len(self.labels ) , self.mode ) def __lowerCAmelCase ( self :Tuple , **lowerCamelCase_ :Dict ) -> List[Any]: '''simple docstring''' return self.model(**lowerCamelCase_ ) def __lowerCAmelCase ( self :int , lowerCamelCase_ :int , lowerCamelCase_ :Any ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = {'''input_ids''': batch[0], '''attention_mask''': batch[1], '''labels''': batch[3]} if self.config.model_type != "distilbert": SCREAMING_SNAKE_CASE : Dict = ( batch[2] if self.config.model_type in ['''bert''', '''xlnet'''] else None ) # XLM and RoBERTa don"t use token_type_ids SCREAMING_SNAKE_CASE : List[Any] = self(**lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = outputs[0] # tensorboard_logs = {"loss": loss, "rate": self.lr_scheduler.get_last_lr()[-1]} return {"loss": loss} def __lowerCAmelCase ( self :Optional[Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.hparams for mode in ["train", "dev", "test"]: SCREAMING_SNAKE_CASE : Optional[int] = self._feature_file(lowerCamelCase_ ) if os.path.exists(lowerCamelCase_ ) and not args.overwrite_cache: logger.info('''Loading features from cached file %s''' , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = torch.load(lowerCamelCase_ ) else: logger.info('''Creating features from dataset file at %s''' , args.data_dir ) SCREAMING_SNAKE_CASE : Union[str, Any] = self.token_classification_task.read_examples_from_file(args.data_dir , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = self.token_classification_task.convert_examples_to_features( lowerCamelCase_ , self.labels , args.max_seq_length , self.tokenizer , cls_token_at_end=bool(self.config.model_type in ['''xlnet'''] ) , cls_token=self.tokenizer.cls_token , cls_token_segment_id=2 if self.config.model_type in ['''xlnet'''] else 0 , sep_token=self.tokenizer.sep_token , sep_token_extra=lowerCamelCase_ , pad_on_left=bool(self.config.model_type in ['''xlnet'''] ) , pad_token=self.tokenizer.pad_token_id , pad_token_segment_id=self.tokenizer.pad_token_type_id , pad_token_label_id=self.pad_token_label_id , ) logger.info('''Saving features into cached file %s''' , lowerCamelCase_ ) torch.save(lowerCamelCase_ , lowerCamelCase_ ) def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :int , lowerCamelCase_ :int , lowerCamelCase_ :bool = False ) -> DataLoader: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self._feature_file(lowerCamelCase_ ) logger.info('''Loading features from cached file %s''' , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = torch.load(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = torch.tensor([f.input_ids for f in features] , dtype=torch.long ) SCREAMING_SNAKE_CASE : Any = torch.tensor([f.attention_mask for f in features] , dtype=torch.long ) if features[0].token_type_ids is not None: SCREAMING_SNAKE_CASE : int = torch.tensor([f.token_type_ids for f in features] , dtype=torch.long ) else: SCREAMING_SNAKE_CASE : List[Any] = torch.tensor([0 for f in features] , dtype=torch.long ) # HACK(we will not use this anymore soon) SCREAMING_SNAKE_CASE : Dict = torch.tensor([f.label_ids for f in features] , dtype=torch.long ) return DataLoader( TensorDataset(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) , batch_size=lowerCamelCase_ ) def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :str , lowerCamelCase_ :int ) -> List[str]: '''simple docstring''' """Compute validation""" "" SCREAMING_SNAKE_CASE : str = {'''input_ids''': batch[0], '''attention_mask''': batch[1], '''labels''': batch[3]} if self.config.model_type != "distilbert": SCREAMING_SNAKE_CASE : Tuple = ( batch[2] if self.config.model_type in ['''bert''', '''xlnet'''] else None ) # XLM and RoBERTa don"t use token_type_ids SCREAMING_SNAKE_CASE : List[Any] = self(**lowerCamelCase_ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = outputs[:2] SCREAMING_SNAKE_CASE : str = logits.detach().cpu().numpy() SCREAMING_SNAKE_CASE : Optional[Any] = inputs['''labels'''].detach().cpu().numpy() return {"val_loss": tmp_eval_loss.detach().cpu(), "pred": preds, "target": out_label_ids} def __lowerCAmelCase ( self :int , lowerCamelCase_ :int ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = torch.stack([x['''val_loss'''] for x in outputs] ).mean() SCREAMING_SNAKE_CASE : Union[str, Any] = np.concatenate([x['''pred'''] for x in outputs] , axis=0 ) SCREAMING_SNAKE_CASE : Tuple = np.argmax(lowerCamelCase_ , axis=2 ) SCREAMING_SNAKE_CASE : Any = np.concatenate([x['''target'''] for x in outputs] , axis=0 ) SCREAMING_SNAKE_CASE : Any = dict(enumerate(self.labels ) ) SCREAMING_SNAKE_CASE : List[str] = [[] for _ in range(out_label_ids.shape[0] )] SCREAMING_SNAKE_CASE : Optional[int] = [[] for _ in range(out_label_ids.shape[0] )] for i in range(out_label_ids.shape[0] ): for j in range(out_label_ids.shape[1] ): if out_label_ids[i, j] != self.pad_token_label_id: out_label_list[i].append(label_map[out_label_ids[i][j]] ) preds_list[i].append(label_map[preds[i][j]] ) SCREAMING_SNAKE_CASE : Optional[Any] = { '''val_loss''': val_loss_mean, '''accuracy_score''': accuracy_score(lowerCamelCase_ , lowerCamelCase_ ), '''precision''': precision_score(lowerCamelCase_ , lowerCamelCase_ ), '''recall''': recall_score(lowerCamelCase_ , lowerCamelCase_ ), '''f1''': fa_score(lowerCamelCase_ , lowerCamelCase_ ), } SCREAMING_SNAKE_CASE : str = dict(results.items() ) SCREAMING_SNAKE_CASE : List[str] = results return ret, preds_list, out_label_list def __lowerCAmelCase ( self :str , lowerCamelCase_ :Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = self._eval_end(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = ret['''log'''] return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs} def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Union[str, Any] = self._eval_end(lowerCamelCase_ ) # Converting to the dict required by pl # https://github.com/PyTorchLightning/pytorch-lightning/blob/master/\ # pytorch_lightning/trainer/logging.py#L139 SCREAMING_SNAKE_CASE : Tuple = ret['''log'''] # `val_loss` is the key returned by `self._eval_end()` but actually refers to `test_loss` return {"avg_test_loss": logs["val_loss"], "log": logs, "progress_bar": logs} @staticmethod def __lowerCAmelCase ( lowerCamelCase_ :List[Any] , lowerCamelCase_ :str ) -> List[Any]: '''simple docstring''' BaseTransformer.add_model_specific_args(lowerCamelCase_ , lowerCamelCase_ ) parser.add_argument( '''--task_type''' , default='''NER''' , type=lowerCamelCase_ , help='''Task type to fine tune in training (e.g. NER, POS, etc)''' ) parser.add_argument( '''--max_seq_length''' , default=1_28 , type=lowerCamelCase_ , help=( '''The maximum total input sequence length after tokenization. Sequences longer ''' '''than this will be truncated, sequences shorter will be padded.''' ) , ) parser.add_argument( '''--labels''' , default='''''' , type=lowerCamelCase_ , help='''Path to a file containing all labels. If not specified, CoNLL-2003 labels are used.''' , ) parser.add_argument( '''--gpus''' , default=0 , type=lowerCamelCase_ , help='''The number of GPUs allocated for this, it is by default 0 meaning none''' , ) parser.add_argument( '''--overwrite_cache''' , action='''store_true''' , help='''Overwrite the cached training and evaluation sets''' ) return parser if __name__ == "__main__": lowerCamelCase__ : int = argparse.ArgumentParser() add_generic_args(parser, os.getcwd()) lowerCamelCase__ : List[Any] = NERTransformer.add_model_specific_args(parser, os.getcwd()) lowerCamelCase__ : List[str] = parser.parse_args() lowerCamelCase__ : str = NERTransformer(args) lowerCamelCase__ : Dict = generic_train(model, args) if args.do_predict: # See https://github.com/huggingface/transformers/issues/3159 # pl use this default format to create a checkpoint: # https://github.com/PyTorchLightning/pytorch-lightning/blob/master\ # /pytorch_lightning/callbacks/model_checkpoint.py#L322 lowerCamelCase__ : Optional[int] = sorted(glob.glob(os.path.join(args.output_dir, "checkpoint-epoch=*.ckpt"), recursive=True)) lowerCamelCase__ : str = model.load_from_checkpoint(checkpoints[-1]) trainer.test(model)
698
"""simple docstring""" from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : Any = { "bert-base-uncased": "https://huggingface.co/bert-base-uncased/resolve/main/config.json", "bert-large-uncased": "https://huggingface.co/bert-large-uncased/resolve/main/config.json", "bert-base-cased": "https://huggingface.co/bert-base-cased/resolve/main/config.json", "bert-large-cased": "https://huggingface.co/bert-large-cased/resolve/main/config.json", "bert-base-multilingual-uncased": "https://huggingface.co/bert-base-multilingual-uncased/resolve/main/config.json", "bert-base-multilingual-cased": "https://huggingface.co/bert-base-multilingual-cased/resolve/main/config.json", "bert-base-chinese": "https://huggingface.co/bert-base-chinese/resolve/main/config.json", "bert-base-german-cased": "https://huggingface.co/bert-base-german-cased/resolve/main/config.json", "bert-large-uncased-whole-word-masking": ( "https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/config.json" ), "bert-large-cased-whole-word-masking": ( "https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/config.json" ), "bert-large-uncased-whole-word-masking-finetuned-squad": ( "https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/config.json" ), "bert-large-cased-whole-word-masking-finetuned-squad": ( "https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/config.json" ), "bert-base-cased-finetuned-mrpc": "https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/config.json", "bert-base-german-dbmdz-cased": "https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/config.json", "bert-base-german-dbmdz-uncased": "https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/config.json", "cl-tohoku/bert-base-japanese": "https://huggingface.co/cl-tohoku/bert-base-japanese/resolve/main/config.json", "cl-tohoku/bert-base-japanese-whole-word-masking": ( "https://huggingface.co/cl-tohoku/bert-base-japanese-whole-word-masking/resolve/main/config.json" ), "cl-tohoku/bert-base-japanese-char": ( "https://huggingface.co/cl-tohoku/bert-base-japanese-char/resolve/main/config.json" ), "cl-tohoku/bert-base-japanese-char-whole-word-masking": ( "https://huggingface.co/cl-tohoku/bert-base-japanese-char-whole-word-masking/resolve/main/config.json" ), "TurkuNLP/bert-base-finnish-cased-v1": ( "https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/config.json" ), "TurkuNLP/bert-base-finnish-uncased-v1": ( "https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/config.json" ), "wietsedv/bert-base-dutch-cased": "https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/config.json", # See all BERT models at https://huggingface.co/models?filter=bert } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """bert""" def __init__( self :Any , lowerCamelCase_ :List[Any]=3_05_22 , lowerCamelCase_ :List[str]=7_68 , lowerCamelCase_ :Tuple=12 , lowerCamelCase_ :List[Any]=12 , lowerCamelCase_ :int=30_72 , lowerCamelCase_ :Dict="gelu" , lowerCamelCase_ :List[Any]=0.1 , lowerCamelCase_ :int=0.1 , lowerCamelCase_ :int=5_12 , lowerCamelCase_ :Optional[int]=2 , lowerCamelCase_ :int=0.0_2 , lowerCamelCase_ :Optional[int]=1E-12 , lowerCamelCase_ :Optional[Any]=0 , lowerCamelCase_ :int="absolute" , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :Optional[Any]=None , **lowerCamelCase_ :List[Any] , ) -> List[str]: '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = vocab_size SCREAMING_SNAKE_CASE : Union[str, Any] = hidden_size SCREAMING_SNAKE_CASE : int = num_hidden_layers SCREAMING_SNAKE_CASE : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE : Dict = hidden_act SCREAMING_SNAKE_CASE : Union[str, Any] = intermediate_size SCREAMING_SNAKE_CASE : Dict = hidden_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : List[Any] = max_position_embeddings SCREAMING_SNAKE_CASE : int = type_vocab_size SCREAMING_SNAKE_CASE : List[str] = initializer_range SCREAMING_SNAKE_CASE : Union[str, Any] = layer_norm_eps SCREAMING_SNAKE_CASE : Optional[Any] = position_embedding_type SCREAMING_SNAKE_CASE : str = use_cache SCREAMING_SNAKE_CASE : Union[str, Any] = classifier_dropout class lowercase__( _UpperCAmelCase ): '''simple docstring''' @property def __lowerCAmelCase ( self :List[str] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": SCREAMING_SNAKE_CASE : Optional[Any] = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: SCREAMING_SNAKE_CASE : Optional[Any] = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis), ] )
698
1
"""simple docstring""" import shutil import tempfile import unittest import numpy as np import pytest from transformers.testing_utils import require_vision from transformers.utils import is_vision_available if is_vision_available(): from PIL import Image from transformers import AutoProcessor, BertTokenizer, BlipImageProcessor, BlipProcessor, PreTrainedTokenizerFast @require_vision class lowercase__( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self :Dict ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = tempfile.mkdtemp() SCREAMING_SNAKE_CASE : Optional[Any] = BlipImageProcessor() SCREAMING_SNAKE_CASE : Any = BertTokenizer.from_pretrained('''hf-internal-testing/tiny-random-BertModel''' ) SCREAMING_SNAKE_CASE : Dict = BlipProcessor(lowerCamelCase_ , lowerCamelCase_ ) processor.save_pretrained(self.tmpdirname ) def __lowerCAmelCase ( self :Any , **lowerCamelCase_ :Any ) -> Tuple: '''simple docstring''' return AutoProcessor.from_pretrained(self.tmpdirname , **lowerCamelCase_ ).tokenizer def __lowerCAmelCase ( self :Optional[Any] , **lowerCamelCase_ :List[str] ) -> Tuple: '''simple docstring''' return AutoProcessor.from_pretrained(self.tmpdirname , **lowerCamelCase_ ).image_processor def __lowerCAmelCase ( self :List[Any] ) -> List[Any]: '''simple docstring''' shutil.rmtree(self.tmpdirname ) def __lowerCAmelCase ( self :Optional[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] SCREAMING_SNAKE_CASE : int = [Image.fromarray(np.moveaxis(lowerCamelCase_ , 0 , -1 ) ) for x in image_inputs] return image_inputs def __lowerCAmelCase ( self :Union[str, Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = BlipProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) SCREAMING_SNAKE_CASE : Optional[Any] = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) SCREAMING_SNAKE_CASE : int = self.get_image_processor(do_normalize=lowerCamelCase_ , padding_value=1.0 ) SCREAMING_SNAKE_CASE : Union[str, Any] = BlipProcessor.from_pretrained( self.tmpdirname , bos_token='''(BOS)''' , eos_token='''(EOS)''' , do_normalize=lowerCamelCase_ , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , lowerCamelCase_ ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , lowerCamelCase_ ) def __lowerCAmelCase ( self :int ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.get_image_processor() SCREAMING_SNAKE_CASE : Tuple = self.get_tokenizer() SCREAMING_SNAKE_CASE : Optional[Any] = BlipProcessor(tokenizer=lowerCamelCase_ , image_processor=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = self.prepare_image_inputs() SCREAMING_SNAKE_CASE : List[Any] = image_processor(lowerCamelCase_ , return_tensors='''np''' ) SCREAMING_SNAKE_CASE : int = processor(images=lowerCamelCase_ , return_tensors='''np''' ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1E-2 ) def __lowerCAmelCase ( self :List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.get_image_processor() SCREAMING_SNAKE_CASE : Any = self.get_tokenizer() SCREAMING_SNAKE_CASE : Dict = BlipProcessor(tokenizer=lowerCamelCase_ , image_processor=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = '''lower newer''' SCREAMING_SNAKE_CASE : Dict = processor(text=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = tokenizer(lowerCamelCase_ , return_token_type_ids=lowerCamelCase_ ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def __lowerCAmelCase ( self :Dict ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : str = self.get_image_processor() SCREAMING_SNAKE_CASE : int = self.get_tokenizer() SCREAMING_SNAKE_CASE : str = BlipProcessor(tokenizer=lowerCamelCase_ , image_processor=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = '''lower newer''' SCREAMING_SNAKE_CASE : str = self.prepare_image_inputs() SCREAMING_SNAKE_CASE : Tuple = processor(text=lowerCamelCase_ , images=lowerCamelCase_ ) self.assertListEqual(list(inputs.keys() ) , ['''pixel_values''', '''input_ids''', '''attention_mask'''] ) # test if it raises when no input is passed with pytest.raises(lowerCamelCase_ ): processor() def __lowerCAmelCase ( self :Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.get_image_processor() SCREAMING_SNAKE_CASE : Optional[Any] = self.get_tokenizer() SCREAMING_SNAKE_CASE : Union[str, Any] = BlipProcessor(tokenizer=lowerCamelCase_ , image_processor=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] SCREAMING_SNAKE_CASE : Optional[Any] = processor.batch_decode(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = tokenizer.batch_decode(lowerCamelCase_ ) self.assertListEqual(lowerCamelCase_ , lowerCamelCase_ ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.get_image_processor() SCREAMING_SNAKE_CASE : int = self.get_tokenizer() SCREAMING_SNAKE_CASE : Optional[Any] = BlipProcessor(tokenizer=lowerCamelCase_ , image_processor=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = '''lower newer''' SCREAMING_SNAKE_CASE : Tuple = self.prepare_image_inputs() SCREAMING_SNAKE_CASE : Optional[Any] = processor(text=lowerCamelCase_ , images=lowerCamelCase_ ) # For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask'] self.assertListEqual(list(inputs.keys() ) , ['''pixel_values''', '''input_ids''', '''attention_mask'''] )
698
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : int = logging.get_logger(__name__) lowerCamelCase__ : str = { "studio-ousia/luke-base": "https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json", "studio-ousia/luke-large": "https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """luke""" def __init__( self :List[Any] , lowerCamelCase_ :Optional[int]=5_02_67 , lowerCamelCase_ :List[Any]=50_00_00 , lowerCamelCase_ :str=7_68 , lowerCamelCase_ :Optional[Any]=2_56 , lowerCamelCase_ :List[Any]=12 , lowerCamelCase_ :List[Any]=12 , lowerCamelCase_ :Any=30_72 , lowerCamelCase_ :Optional[int]="gelu" , lowerCamelCase_ :Dict=0.1 , lowerCamelCase_ :Any=0.1 , lowerCamelCase_ :str=5_12 , lowerCamelCase_ :Tuple=2 , lowerCamelCase_ :Optional[Any]=0.0_2 , lowerCamelCase_ :Optional[int]=1E-12 , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :int=None , lowerCamelCase_ :Dict=1 , lowerCamelCase_ :str=0 , lowerCamelCase_ :int=2 , **lowerCamelCase_ :List[str] , ) -> int: '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = vocab_size SCREAMING_SNAKE_CASE : Optional[Any] = entity_vocab_size SCREAMING_SNAKE_CASE : int = hidden_size SCREAMING_SNAKE_CASE : Dict = entity_emb_size SCREAMING_SNAKE_CASE : Optional[int] = num_hidden_layers SCREAMING_SNAKE_CASE : int = num_attention_heads SCREAMING_SNAKE_CASE : str = hidden_act SCREAMING_SNAKE_CASE : str = intermediate_size SCREAMING_SNAKE_CASE : Tuple = hidden_dropout_prob SCREAMING_SNAKE_CASE : str = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = max_position_embeddings SCREAMING_SNAKE_CASE : Dict = type_vocab_size SCREAMING_SNAKE_CASE : List[Any] = initializer_range SCREAMING_SNAKE_CASE : Optional[int] = layer_norm_eps SCREAMING_SNAKE_CASE : Optional[Any] = use_entity_aware_attention SCREAMING_SNAKE_CASE : str = classifier_dropout
698
1
"""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 lowercase__( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self :int ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ) SCREAMING_SNAKE_CASE : Optional[Any] = AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = -1 SCREAMING_SNAKE_CASE : int = ids_tensor((1, 5) , vocab_size=model.config.vocab_size ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = model.generate(lowerCamelCase_ , max_new_tokens=10 , do_sample=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = tokenizer.decode(greedy_ids[0] ) with CaptureStdout() as cs: SCREAMING_SNAKE_CASE : Any = TextStreamer(lowerCamelCase_ ) model.generate(lowerCamelCase_ , max_new_tokens=10 , do_sample=lowerCamelCase_ , streamer=lowerCamelCase_ ) # The greedy text should be printed to stdout, except for the final "\n" in the streamer SCREAMING_SNAKE_CASE : List[str] = cs.out[:-1] self.assertEqual(lowerCamelCase_ , lowerCamelCase_ ) def __lowerCAmelCase ( self :Any ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ) SCREAMING_SNAKE_CASE : Optional[int] = AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = -1 SCREAMING_SNAKE_CASE : str = ids_tensor((1, 5) , vocab_size=model.config.vocab_size ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = model.generate(lowerCamelCase_ , max_new_tokens=10 , do_sample=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = tokenizer.decode(greedy_ids[0] ) SCREAMING_SNAKE_CASE : Dict = TextIteratorStreamer(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = {'''input_ids''': input_ids, '''max_new_tokens''': 10, '''do_sample''': False, '''streamer''': streamer} SCREAMING_SNAKE_CASE : Union[str, Any] = Thread(target=model.generate , kwargs=lowerCamelCase_ ) thread.start() SCREAMING_SNAKE_CASE : List[Any] = '''''' for new_text in streamer: streamer_text += new_text self.assertEqual(lowerCamelCase_ , lowerCamelCase_ ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ) SCREAMING_SNAKE_CASE : Optional[int] = AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = -1 SCREAMING_SNAKE_CASE : int = ids_tensor((1, 5) , vocab_size=model.config.vocab_size ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = model.generate(lowerCamelCase_ , max_new_tokens=10 , do_sample=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = greedy_ids[:, input_ids.shape[1] :] SCREAMING_SNAKE_CASE : int = tokenizer.decode(new_greedy_ids[0] ) with CaptureStdout() as cs: SCREAMING_SNAKE_CASE : List[str] = TextStreamer(lowerCamelCase_ , skip_prompt=lowerCamelCase_ ) model.generate(lowerCamelCase_ , max_new_tokens=10 , do_sample=lowerCamelCase_ , streamer=lowerCamelCase_ ) # The greedy text should be printed to stdout, except for the final "\n" in the streamer SCREAMING_SNAKE_CASE : Union[str, Any] = cs.out[:-1] self.assertEqual(lowerCamelCase_ , lowerCamelCase_ ) def __lowerCAmelCase ( self :Dict ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = AutoTokenizer.from_pretrained('''distilgpt2''' ) SCREAMING_SNAKE_CASE : List[Any] = AutoModelForCausalLM.from_pretrained('''distilgpt2''' ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = -1 SCREAMING_SNAKE_CASE : Optional[int] = torch.ones((1, 5) , device=lowerCamelCase_ ).long() * model.config.bos_token_id with CaptureStdout() as cs: SCREAMING_SNAKE_CASE : Tuple = 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 SCREAMING_SNAKE_CASE : Dict = cs.out[:-1] # Remove the final "\n" SCREAMING_SNAKE_CASE : Tuple = tokenizer(lowerCamelCase_ , return_tensors='''pt''' ) self.assertEqual(streamer_text_tokenized.input_ids.shape , (1, 1) ) def __lowerCAmelCase ( self :Any ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ) SCREAMING_SNAKE_CASE : Dict = AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = -1 SCREAMING_SNAKE_CASE : List[str] = ids_tensor((1, 5) , vocab_size=model.config.vocab_size ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = TextIteratorStreamer(lowerCamelCase_ , timeout=0.0_0_1 ) SCREAMING_SNAKE_CASE : Dict = {'''input_ids''': input_ids, '''max_new_tokens''': 10, '''do_sample''': False, '''streamer''': streamer} SCREAMING_SNAKE_CASE : Tuple = 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_ ): SCREAMING_SNAKE_CASE : List[Any] = '''''' for new_text in streamer: streamer_text += new_text
698
"""simple docstring""" # using dfs for finding eulerian path traversal def __A ( a_ : Dict , a_ : int , a_ : str , a_ : Optional[Any]=None )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = (path or []) + [u] for v in graph[u]: if visited_edge[u][v] is False: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = True, True SCREAMING_SNAKE_CASE : List[str] = dfs(a_ , a_ , a_ , a_ ) return path def __A ( a_ : List[str] , a_ : Any )-> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = 0 SCREAMING_SNAKE_CASE : str = -1 for i in range(a_ ): if i not in graph.keys(): continue if len(graph[i] ) % 2 == 1: odd_degree_nodes += 1 SCREAMING_SNAKE_CASE : Tuple = i if odd_degree_nodes == 0: return 1, odd_node if odd_degree_nodes == 2: return 2, odd_node return 3, odd_node def __A ( a_ : Any , a_ : int )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = [[False for _ in range(max_node + 1 )] for _ in range(max_node + 1 )] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = check_circuit_or_path(a_ , a_ ) if check == 3: print('''graph is not Eulerian''' ) print('''no path''' ) return SCREAMING_SNAKE_CASE : Tuple = 1 if check == 2: SCREAMING_SNAKE_CASE : Optional[int] = odd_node print('''graph has a Euler path''' ) if check == 1: print('''graph has a Euler cycle''' ) SCREAMING_SNAKE_CASE : Optional[int] = dfs(a_ , a_ , a_ ) print(a_ ) def __A ( )-> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = {1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]} SCREAMING_SNAKE_CASE : str = {1: [2, 3, 4, 5], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [1, 4]} SCREAMING_SNAKE_CASE : str = {1: [2, 3, 4], 2: [1, 3, 4], 3: [1, 2], 4: [1, 2, 5], 5: [4]} SCREAMING_SNAKE_CASE : int = {1: [2, 3], 2: [1, 3], 3: [1, 2]} SCREAMING_SNAKE_CASE : int = { 1: [], 2: [] # all degree is zero } SCREAMING_SNAKE_CASE : List[str] = 10 check_euler(a_ , a_ ) check_euler(a_ , a_ ) check_euler(a_ , a_ ) check_euler(a_ , a_ ) check_euler(a_ , a_ ) if __name__ == "__main__": main()
698
1
"""simple docstring""" import argparse import hashlib # hashlib is only used inside the Test class import struct class lowercase__: '''simple docstring''' def __init__( self :Any , lowerCamelCase_ :List[str] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = data SCREAMING_SNAKE_CASE : List[Any] = [0x6745_2301, 0xEFCD_AB89, 0x98BA_DCFE, 0x1032_5476, 0xC3D2_E1F0] @staticmethod def __lowerCAmelCase ( lowerCamelCase_ :str , lowerCamelCase_ :str ) -> Optional[int]: '''simple docstring''' return ((n << b) | (n >> (32 - b))) & 0xFFFF_FFFF def __lowerCAmelCase ( self :int ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = B'''\x80''' + B'''\x00''' * (63 - (len(self.data ) + 8) % 64) SCREAMING_SNAKE_CASE : Union[str, Any] = self.data + padding + struct.pack('''>Q''' , 8 * len(self.data ) ) return padded_data def __lowerCAmelCase ( self :Optional[int] ) -> Union[str, Any]: '''simple docstring''' return [ self.padded_data[i : i + 64] for i in range(0 , len(self.padded_data ) , 64 ) ] def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :Tuple ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = list(struct.unpack('''>16L''' , lowerCamelCase_ ) ) + [0] * 64 for i in range(16 , 80 ): SCREAMING_SNAKE_CASE : Tuple = self.rotate((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]) , 1 ) return w def __lowerCAmelCase ( self :str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.padding() SCREAMING_SNAKE_CASE : int = self.split_blocks() for block in self.blocks: SCREAMING_SNAKE_CASE : Union[str, Any] = self.expand_block(lowerCamelCase_ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = self.h for i in range(0 , 80 ): if 0 <= i < 20: SCREAMING_SNAKE_CASE : List[str] = (b & c) | ((~b) & d) SCREAMING_SNAKE_CASE : Optional[int] = 0x5A82_7999 elif 20 <= i < 40: SCREAMING_SNAKE_CASE : str = b ^ c ^ d SCREAMING_SNAKE_CASE : Dict = 0x6ED9_EBA1 elif 40 <= i < 60: SCREAMING_SNAKE_CASE : Optional[Any] = (b & c) | (b & d) | (c & d) SCREAMING_SNAKE_CASE : Tuple = 0x8F1B_BCDC elif 60 <= i < 80: SCREAMING_SNAKE_CASE : Dict = b ^ c ^ d SCREAMING_SNAKE_CASE : Any = 0xCA62_C1D6 SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = ( self.rotate(lowerCamelCase_ , 5 ) + f + e + k + expanded_block[i] & 0xFFFF_FFFF, a, self.rotate(lowerCamelCase_ , 30 ), c, d, ) SCREAMING_SNAKE_CASE : List[Any] = ( self.h[0] + a & 0xFFFF_FFFF, self.h[1] + b & 0xFFFF_FFFF, self.h[2] + c & 0xFFFF_FFFF, self.h[3] + d & 0xFFFF_FFFF, self.h[4] + e & 0xFFFF_FFFF, ) return ("{:08x}" * 5).format(*self.h ) def __A ( )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = B'''Test String''' assert SHAaHash(a_ ).final_hash() == hashlib.shaa(a_ ).hexdigest() # noqa: S324 def __A ( )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = argparse.ArgumentParser(description='''Process some strings or files''' ) parser.add_argument( '''--string''' , dest='''input_string''' , default='''Hello World!! Welcome to Cryptography''' , help='''Hash the string''' , ) parser.add_argument('''--file''' , dest='''input_file''' , help='''Hash contents of a file''' ) SCREAMING_SNAKE_CASE : List[str] = parser.parse_args() SCREAMING_SNAKE_CASE : List[str] = args.input_string # In any case hash input should be a bytestring if args.input_file: with open(args.input_file , '''rb''' ) as f: SCREAMING_SNAKE_CASE : Optional[int] = f.read() else: SCREAMING_SNAKE_CASE : Optional[Any] = bytes(a_ , '''utf-8''' ) print(SHAaHash(a_ ).final_hash() ) if __name__ == "__main__": main() import doctest doctest.testmod()
698
"""simple docstring""" import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, BatchEncoding, MBartaaTokenizer, MBartaaTokenizerFast, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, slow, ) from ...test_tokenization_common import TokenizerTesterMixin lowerCamelCase__ : str = get_tests_dir("fixtures/test_sentencepiece.model") if is_torch_available(): from transformers.models.mbart.modeling_mbart import shift_tokens_right lowerCamelCase__ : List[str] = 250004 lowerCamelCase__ : str = 250020 @require_sentencepiece @require_tokenizers class lowercase__( _UpperCAmelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase = MBartaaTokenizer UpperCamelCase = MBartaaTokenizerFast UpperCamelCase = True UpperCamelCase = True def __lowerCAmelCase ( self :Union[str, Any] ) -> str: '''simple docstring''' super().setUp() # We have a SentencePiece fixture for testing SCREAMING_SNAKE_CASE : Optional[int] = MBartaaTokenizer(lowerCamelCase_ , src_lang='''en_XX''' , tgt_lang='''ro_RO''' , keep_accents=lowerCamelCase_ ) tokenizer.save_pretrained(self.tmpdirname ) def __lowerCAmelCase ( self :Union[str, Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = '''<s>''' SCREAMING_SNAKE_CASE : Union[str, Any] = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCamelCase_ ) , lowerCamelCase_ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCamelCase_ ) , lowerCamelCase_ ) def __lowerCAmelCase ( self :str ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<s>''' ) self.assertEqual(vocab_keys[1] , '''<pad>''' ) self.assertEqual(vocab_keys[-1] , '''<mask>''' ) self.assertEqual(len(lowerCamelCase_ ) , 10_54 ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Tuple: '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 10_54 ) def __lowerCAmelCase ( self :Tuple ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = MBartaaTokenizer(lowerCamelCase_ , src_lang='''en_XX''' , tgt_lang='''ro_RO''' , keep_accents=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(lowerCamelCase_ , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(lowerCamelCase_ ) , [value + tokenizer.fairseq_offset for value in [2_85, 46, 10, 1_70, 3_82]] , ) SCREAMING_SNAKE_CASE : Tuple = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( lowerCamelCase_ , [SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.'''] , ) SCREAMING_SNAKE_CASE : int = tokenizer.convert_tokens_to_ids(lowerCamelCase_ ) self.assertListEqual( lowerCamelCase_ , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 6_02, 3_47, 3_47, 3_47, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) SCREAMING_SNAKE_CASE : Union[str, Any] = tokenizer.convert_ids_to_tokens(lowerCamelCase_ ) self.assertListEqual( lowerCamelCase_ , [SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''<unk>''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''<unk>''', '''.'''] , ) @slow def __lowerCAmelCase ( self :Optional[Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = {'''input_ids''': [[25_00_04, 1_10_62, 8_27_72, 7, 15, 8_27_72, 5_38, 5_15_29, 2_37, 1_71_98, 12_90, 2_06, 9, 21_51_75, 13_14, 1_36, 1_71_98, 12_90, 2_06, 9, 5_63_59, 42, 12_20_09, 9, 1_64_66, 16, 8_73_44, 45_37, 9, 47_17, 7_83_81, 6, 15_99_58, 7, 15, 2_44_80, 6_18, 4, 5_27, 2_26_93, 54_28, 4, 27_77, 2_44_80, 98_74, 4, 4_35_23, 5_94, 4, 8_03, 1_83_92, 3_31_89, 18, 4, 4_35_23, 2_44_47, 1_23_99, 1_00, 2_49_55, 8_36_58, 96_26, 14_40_57, 15, 8_39, 2_23_35, 16, 1_36, 2_49_55, 8_36_58, 8_34_79, 15, 3_91_02, 7_24, 16, 6_78, 6_45, 27_89, 13_28, 45_89, 42, 12_20_09, 11_57_74, 23, 8_05, 13_28, 4_68_76, 7, 1_36, 5_38_94, 19_40, 4_22_27, 4_11_59, 1_77_21, 8_23, 4_25, 4, 2_75_12, 9_87_22, 2_06, 1_36, 55_31, 49_70, 9_19, 1_73_36, 5, 2], [25_00_04, 2_00_80, 6_18, 83, 8_27_75, 47, 4_79, 9, 15_17, 73, 5_38_94, 3_33, 8_05_81, 11_01_17, 1_88_11, 52_56, 12_95, 51, 15_25_26, 2_97, 79_86, 3_90, 12_44_16, 5_38, 3_54_31, 2_14, 98, 1_50_44, 2_57_37, 1_36, 71_08, 4_37_01, 23, 7_56, 13_53_55, 7, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [25_00_04, 5_81, 6_37_73, 11_94_55, 6, 14_77_97, 8_82_03, 7, 6_45, 70, 21, 32_85, 1_02_69, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=lowerCamelCase_ , model_name='''facebook/mbart-large-50''' , revision='''d3913889c59cd5c9e456b269c376325eabad57e2''' , ) def __lowerCAmelCase ( self :Optional[int] ) -> List[Any]: '''simple docstring''' if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return SCREAMING_SNAKE_CASE : str = (self.rust_tokenizer_class, '''hf-internal-testing/tiny-random-mbart50''', {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})" ): SCREAMING_SNAKE_CASE : Tuple = self.rust_tokenizer_class.from_pretrained(lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = self.tokenizer_class.from_pretrained(lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = tempfile.mkdtemp() SCREAMING_SNAKE_CASE : Dict = tokenizer_r.save_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = tokenizer_p.save_pretrained(lowerCamelCase_ ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any('''tokenizer.json''' in f for f in tokenizer_r_files ) ) SCREAMING_SNAKE_CASE : Any = tuple(f for f in tokenizer_r_files if '''tokenizer.json''' not in f ) self.assertSequenceEqual(lowerCamelCase_ , lowerCamelCase_ ) # Checks everything loads correctly in the same way SCREAMING_SNAKE_CASE : int = tokenizer_r.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = tokenizer_p.from_pretrained(lowerCamelCase_ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(lowerCamelCase_ , lowerCamelCase_ ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(lowerCamelCase_ ) # Save tokenizer rust, legacy_format=True SCREAMING_SNAKE_CASE : Optional[int] = tempfile.mkdtemp() SCREAMING_SNAKE_CASE : List[Any] = tokenizer_r.save_pretrained(lowerCamelCase_ , legacy_format=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = tokenizer_p.save_pretrained(lowerCamelCase_ ) # Checks it save with the same files self.assertSequenceEqual(lowerCamelCase_ , lowerCamelCase_ ) # Checks everything loads correctly in the same way SCREAMING_SNAKE_CASE : List[Any] = tokenizer_r.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = tokenizer_p.from_pretrained(lowerCamelCase_ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(lowerCamelCase_ , lowerCamelCase_ ) ) shutil.rmtree(lowerCamelCase_ ) # Save tokenizer rust, legacy_format=False SCREAMING_SNAKE_CASE : Union[str, Any] = tempfile.mkdtemp() SCREAMING_SNAKE_CASE : Optional[int] = tokenizer_r.save_pretrained(lowerCamelCase_ , legacy_format=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = tokenizer_p.save_pretrained(lowerCamelCase_ ) # Checks it saved the tokenizer.json file self.assertTrue(any('''tokenizer.json''' in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way SCREAMING_SNAKE_CASE : Tuple = tokenizer_r.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = tokenizer_p.from_pretrained(lowerCamelCase_ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(lowerCamelCase_ , lowerCamelCase_ ) ) shutil.rmtree(lowerCamelCase_ ) @require_torch @require_sentencepiece @require_tokenizers class lowercase__( unittest.TestCase ): '''simple docstring''' UpperCamelCase = """facebook/mbart-large-50-one-to-many-mmt""" UpperCamelCase = [ """ UN Chief Says There Is No Military Solution in Syria""", """ Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for Syria is that \"there is no military solution\" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.""", ] UpperCamelCase = [ """Şeful ONU declară că nu există o soluţie militară în Siria""", """Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei""" """ pentru Siria este că \"nu există o soluţie militară\" la conflictul de aproape cinci ani şi că noi arme nu vor""" """ face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.""", ] UpperCamelCase = [EN_CODE, 82_74, 12_78_73, 2_59_16, 7, 86_22, 20_71, 4_38, 6_74_85, 53, 18_78_95, 23, 5_17_12, 2] @classmethod def __lowerCAmelCase ( cls :Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : MBartaaTokenizer = MBartaaTokenizer.from_pretrained( cls.checkpoint_name , src_lang='''en_XX''' , tgt_lang='''ro_RO''' ) SCREAMING_SNAKE_CASE : Dict = 1 return cls def __lowerCAmelCase ( self :Any ) -> int: '''simple docstring''' self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''ar_AR'''] , 25_00_01 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''en_EN'''] , 25_00_04 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''ro_RO'''] , 25_00_20 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''mr_IN'''] , 25_00_38 ) def __lowerCAmelCase ( self :List[str] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , lowerCamelCase_ ) def __lowerCAmelCase ( self :str ) -> Optional[Any]: '''simple docstring''' self.assertIn(lowerCamelCase_ , self.tokenizer.all_special_ids ) SCREAMING_SNAKE_CASE : int = [RO_CODE, 8_84, 90_19, 96, 9, 9_16, 8_67_92, 36, 1_87_43, 1_55_96, 5, 2] SCREAMING_SNAKE_CASE : Optional[int] = self.tokenizer.decode(lowerCamelCase_ , skip_special_tokens=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=lowerCamelCase_ ) self.assertEqual(lowerCamelCase_ , lowerCamelCase_ ) self.assertNotIn(self.tokenizer.eos_token , lowerCamelCase_ ) def __lowerCAmelCase ( self :Tuple ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = ['''this is gunna be a long sentence ''' * 20] assert isinstance(src_text[0] , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = 10 SCREAMING_SNAKE_CASE : Union[str, Any] = self.tokenizer(lowerCamelCase_ , max_length=lowerCamelCase_ , truncation=lowerCamelCase_ ).input_ids[0] self.assertEqual(ids[0] , lowerCamelCase_ ) self.assertEqual(ids[-1] , 2 ) self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) def __lowerCAmelCase ( self :str ) -> List[str]: '''simple docstring''' self.assertListEqual(self.tokenizer.convert_tokens_to_ids(['''<mask>''', '''ar_AR'''] ) , [25_00_53, 25_00_01] ) def __lowerCAmelCase ( self :List[str] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = tempfile.mkdtemp() SCREAMING_SNAKE_CASE : Dict = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = MBartaaTokenizer.from_pretrained(lowerCamelCase_ ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , lowerCamelCase_ ) @require_torch def __lowerCAmelCase ( self :str ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.tokenizer(self.src_text , text_target=self.tgt_text , padding=lowerCamelCase_ , return_tensors='''pt''' ) SCREAMING_SNAKE_CASE : Dict = shift_tokens_right(batch['''labels'''] , self.tokenizer.pad_token_id ) # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 assert batch.input_ids[1][0] == EN_CODE assert batch.input_ids[1][-1] == 2 assert batch.labels[1][0] == RO_CODE assert batch.labels[1][-1] == 2 assert batch.decoder_input_ids[1][:2].tolist() == [2, RO_CODE] @require_torch def __lowerCAmelCase ( self :Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=lowerCamelCase_ , truncation=lowerCamelCase_ , max_length=len(self.expected_src_tokens ) , return_tensors='''pt''' , ) SCREAMING_SNAKE_CASE : List[Any] = shift_tokens_right(batch['''labels'''] , self.tokenizer.pad_token_id ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) self.assertEqual((2, 14) , batch.input_ids.shape ) self.assertEqual((2, 14) , batch.attention_mask.shape ) SCREAMING_SNAKE_CASE : List[str] = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , lowerCamelCase_ ) self.assertEqual(2 , batch.decoder_input_ids[0, 0] ) # decoder_start_token_id # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [EN_CODE] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.tokenizer(self.src_text , padding=lowerCamelCase_ , truncation=lowerCamelCase_ , max_length=3 , return_tensors='''pt''' ) SCREAMING_SNAKE_CASE : Tuple = self.tokenizer( text_target=self.tgt_text , padding=lowerCamelCase_ , truncation=lowerCamelCase_ , max_length=10 , return_tensors='''pt''' ) SCREAMING_SNAKE_CASE : List[Any] = targets['''input_ids'''] SCREAMING_SNAKE_CASE : Optional[int] = shift_tokens_right(lowerCamelCase_ , self.tokenizer.pad_token_id ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.decoder_input_ids.shape[1] , 10 ) @require_torch def __lowerCAmelCase ( self :Any ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.tokenizer._build_translation_inputs( '''A test''' , return_tensors='''pt''' , src_lang='''en_XX''' , tgt_lang='''ar_AR''' ) self.assertEqual( nested_simplify(lowerCamelCase_ ) , { # en_XX, A, test, EOS '''input_ids''': [[25_00_04, 62, 30_34, 2]], '''attention_mask''': [[1, 1, 1, 1]], # ar_AR '''forced_bos_token_id''': 25_00_01, } , )
698
1
"""simple docstring""" from datetime import datetime as dt import os from github import Github lowerCamelCase__ : Optional[Any] = [ "good first issue", "good second issue", "good difficult issue", "feature request", "new model", "wip", ] def __A ( )-> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = Github(os.environ['''GITHUB_TOKEN'''] ) SCREAMING_SNAKE_CASE : str = g.get_repo('''huggingface/transformers''' ) SCREAMING_SNAKE_CASE : str = repo.get_issues(state='''open''' ) for issue in open_issues: SCREAMING_SNAKE_CASE : Dict = sorted([comment for comment in issue.get_comments()] , key=lambda a_ : i.created_at , reverse=a_ ) SCREAMING_SNAKE_CASE : int = comments[0] if len(a_ ) > 0 else None if ( last_comment is not None and last_comment.user.login == "github-actions[bot]" and (dt.utcnow() - issue.updated_at).days > 7 and (dt.utcnow() - issue.created_at).days >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # print(f"Would close issue {issue.number} since it has been 7 days of inactivity since bot mention.") issue.edit(state='''closed''' ) elif ( (dt.utcnow() - issue.updated_at).days > 23 and (dt.utcnow() - issue.created_at).days >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # print(f"Would add stale comment to {issue.number}") issue.create_comment( '''This issue has been automatically marked as stale because it has not had ''' '''recent activity. If you think this still needs to be addressed ''' '''please comment on this thread.\n\nPlease note that issues that do not follow the ''' '''[contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) ''' '''are likely to be ignored.''' ) if __name__ == "__main__": main()
698
"""simple docstring""" from unittest import TestCase from datasets import Sequence, Value from datasets.arrow_dataset import Dataset class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __lowerCAmelCase ( self :Union[str, Any] ) -> str: '''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 __lowerCAmelCase ( self :Dict ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = {'''col_1''': [3, 2, 1, 0], '''col_2''': ['''a''', '''b''', '''c''', '''d''']} return Dataset.from_dict(lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[int] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self._create_example_records() SCREAMING_SNAKE_CASE : List[Any] = Dataset.from_list(lowerCamelCase_ ) self.assertListEqual(dset.column_names , ['''col_1''', '''col_2'''] ) for i, r in enumerate(lowerCamelCase_ ): self.assertDictEqual(lowerCamelCase_ , example_records[i] ) def __lowerCAmelCase ( self :Dict ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : str = self._create_example_records() SCREAMING_SNAKE_CASE : Optional[int] = Dataset.from_list(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = 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 __lowerCAmelCase ( self :List[str] ) -> Dict: # checks what happens with missing columns '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = [{'''col_1''': 1}, {'''col_2''': '''x'''}] SCREAMING_SNAKE_CASE : Optional[int] = Dataset.from_list(lowerCamelCase_ ) self.assertDictEqual(dset[0] , {'''col_1''': 1} ) self.assertDictEqual(dset[1] , {'''col_1''': None} ) # NB: first record is used for columns def __lowerCAmelCase ( self :Tuple ) -> Optional[Any]: # checks if the type can be inferred from the second record '''simple docstring''' SCREAMING_SNAKE_CASE : int = [{'''col_1''': []}, {'''col_1''': [1, 2]}] SCREAMING_SNAKE_CASE : List[str] = Dataset.from_list(lowerCamelCase_ ) self.assertEqual(dset.info.features['''col_1'''] , Sequence(Value('''int64''' ) ) ) def __lowerCAmelCase ( self :Any ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = Dataset.from_list([] ) self.assertEqual(len(lowerCamelCase_ ) , 0 ) self.assertListEqual(dset.column_names , [] )
698
1
"""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 lowercase__: '''simple docstring''' @staticmethod def __lowerCAmelCase ( *lowerCamelCase_ :str , **lowerCamelCase_ :Tuple ) -> Tuple: '''simple docstring''' pass @is_pipeline_test @require_vision class lowercase__( unittest.TestCase ): '''simple docstring''' @require_torch def __lowerCAmelCase ( self :str ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = pipeline( model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification''' , ) SCREAMING_SNAKE_CASE : Tuple = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) SCREAMING_SNAKE_CASE : str = image_classifier(lowerCamelCase_ , 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(lowerCamelCase_ ) , [ [{'''score''': 0.3_3_3, '''label''': '''a'''}, {'''score''': 0.3_3_3, '''label''': '''b'''}, {'''score''': 0.3_3_3, '''label''': '''c'''}], [{'''score''': 0.3_3_3, '''label''': '''a'''}, {'''score''': 0.3_3_3, '''label''': '''c'''}, {'''score''': 0.3_3_3, '''label''': '''b'''}], ] , ) SCREAMING_SNAKE_CASE : Optional[Any] = image_classifier([image] * 5 , candidate_labels=['''A''', '''B''', '''C'''] , batch_size=2 ) self.assertEqual( nested_simplify(lowerCamelCase_ ) , [ [ {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, ], [ {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, ], [ {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, ], [ {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, ], [ {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, ], ] , ) @require_tf def __lowerCAmelCase ( self :Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = pipeline( model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification''' , framework='''tf''' ) SCREAMING_SNAKE_CASE : List[str] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) SCREAMING_SNAKE_CASE : Optional[Any] = image_classifier(lowerCamelCase_ , candidate_labels=['''a''', '''b''', '''c'''] ) self.assertEqual( nested_simplify(lowerCamelCase_ ) , [{'''score''': 0.3_3_3, '''label''': '''a'''}, {'''score''': 0.3_3_3, '''label''': '''b'''}, {'''score''': 0.3_3_3, '''label''': '''c'''}] , ) SCREAMING_SNAKE_CASE : List[Any] = image_classifier([image] * 5 , candidate_labels=['''A''', '''B''', '''C'''] , batch_size=2 ) self.assertEqual( nested_simplify(lowerCamelCase_ ) , [ [ {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, ], [ {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, ], [ {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, ], [ {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, ], [ {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, {'''score''': 0.3_3_3, '''label''': ANY(lowerCamelCase_ )}, ], ] , ) @slow @require_torch def __lowerCAmelCase ( self :Any ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[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 SCREAMING_SNAKE_CASE : int = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = image_classifier(lowerCamelCase_ , candidate_labels=['''cat''', '''plane''', '''remote'''] ) self.assertEqual( nested_simplify(lowerCamelCase_ ) , [ {'''score''': 0.5_1_1, '''label''': '''remote'''}, {'''score''': 0.4_8_5, '''label''': '''cat'''}, {'''score''': 0.0_0_4, '''label''': '''plane'''}, ] , ) SCREAMING_SNAKE_CASE : Optional[int] = image_classifier([image] * 5 , candidate_labels=['''cat''', '''plane''', '''remote'''] , batch_size=2 ) self.assertEqual( nested_simplify(lowerCamelCase_ ) , [ [ {'''score''': 0.5_1_1, '''label''': '''remote'''}, {'''score''': 0.4_8_5, '''label''': '''cat'''}, {'''score''': 0.0_0_4, '''label''': '''plane'''}, ], ] * 5 , ) @slow @require_tf def __lowerCAmelCase ( self :Union[str, Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = 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 SCREAMING_SNAKE_CASE : List[Any] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) SCREAMING_SNAKE_CASE : str = image_classifier(lowerCamelCase_ , candidate_labels=['''cat''', '''plane''', '''remote'''] ) self.assertEqual( nested_simplify(lowerCamelCase_ ) , [ {'''score''': 0.5_1_1, '''label''': '''remote'''}, {'''score''': 0.4_8_5, '''label''': '''cat'''}, {'''score''': 0.0_0_4, '''label''': '''plane'''}, ] , ) SCREAMING_SNAKE_CASE : str = image_classifier([image] * 5 , candidate_labels=['''cat''', '''plane''', '''remote'''] , batch_size=2 ) self.assertEqual( nested_simplify(lowerCamelCase_ ) , [ [ {'''score''': 0.5_1_1, '''label''': '''remote'''}, {'''score''': 0.4_8_5, '''label''': '''cat'''}, {'''score''': 0.0_0_4, '''label''': '''plane'''}, ], ] * 5 , )
698
"""simple docstring""" from __future__ import annotations import math from collections.abc import Callable def __A ( a_ : Callable[[int | float], int | float] , a_ : int | float , a_ : int | float , a_ : int = 1_00 , )-> float: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = x_start SCREAMING_SNAKE_CASE : Union[str, Any] = fnc(a_ ) SCREAMING_SNAKE_CASE : Optional[int] = 0.0 for _ in range(a_ ): # Approximates curve as a sequence of linear lines and sums their length SCREAMING_SNAKE_CASE : int = (x_end - x_start) / steps + xa SCREAMING_SNAKE_CASE : Optional[int] = fnc(a_ ) length += math.hypot(xa - xa , fxa - fxa ) # Increment step SCREAMING_SNAKE_CASE : str = xa SCREAMING_SNAKE_CASE : Any = fxa return length if __name__ == "__main__": def __A ( a_ : Optional[Any] )-> List[Any]: '''simple docstring''' return math.sin(10 * x ) print("f(x) = sin(10 * x)") print("The length of the curve from x = -10 to x = 10 is:") lowerCamelCase__ : str = 10 while i <= 100000: print(f'''With {i} steps: {line_length(f, -10, 10, i)}''') i *= 10
698
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available lowerCamelCase__ : Dict = { "configuration_longt5": ["LONGT5_PRETRAINED_CONFIG_ARCHIVE_MAP", "LongT5Config", "LongT5OnnxConfig"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : List[Any] = [ "LONGT5_PRETRAINED_MODEL_ARCHIVE_LIST", "LongT5EncoderModel", "LongT5ForConditionalGeneration", "LongT5Model", "LongT5PreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : str = [ "FlaxLongT5ForConditionalGeneration", "FlaxLongT5Model", "FlaxLongT5PreTrainedModel", ] if TYPE_CHECKING: from .configuration_longta import LONGT5_PRETRAINED_CONFIG_ARCHIVE_MAP, LongTaConfig, LongTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_longta import ( LONGT5_PRETRAINED_MODEL_ARCHIVE_LIST, LongTaEncoderModel, LongTaForConditionalGeneration, LongTaModel, LongTaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_longta import ( FlaxLongTaForConditionalGeneration, FlaxLongTaModel, FlaxLongTaPreTrainedModel, ) else: import sys lowerCamelCase__ : str = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
698
"""simple docstring""" # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os from accelerate.test_utils import execute_subprocess_async def __A ( a_ : int=None )-> Tuple: '''simple docstring''' if subparsers is not None: SCREAMING_SNAKE_CASE : List[str] = subparsers.add_parser('''test''' ) else: SCREAMING_SNAKE_CASE : int = argparse.ArgumentParser('''Accelerate test command''' ) parser.add_argument( '''--config_file''' , default=a_ , help=( '''The path to use to store the config file. Will default to a file named default_config.yaml in the cache ''' '''location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have ''' '''such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed ''' '''with \'huggingface\'.''' ) , ) if subparsers is not None: parser.set_defaults(func=a_ ) return parser def __A ( a_ : Any )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = os.path.sep.join(__file__.split(os.path.sep )[:-2] + ['''test_utils''', '''scripts''', '''test_script.py'''] ) if args.config_file is None: SCREAMING_SNAKE_CASE : Tuple = script_name else: SCREAMING_SNAKE_CASE : Optional[Any] = F"--config_file={args.config_file} {script_name}" SCREAMING_SNAKE_CASE : str = ['''accelerate-launch'''] + test_args.split() SCREAMING_SNAKE_CASE : List[str] = execute_subprocess_async(a_ , env=os.environ.copy() ) if result.returncode == 0: print('''Test is a success! You are ready for your distributed training!''' ) def __A ( )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : str = test_command_parser() SCREAMING_SNAKE_CASE : Optional[Any] = parser.parse_args() test_command(a_ ) if __name__ == "__main__": main()
698
1
"""simple docstring""" import unittest from transformers import MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING, AutoTokenizer, is_vision_available from transformers.pipelines import pipeline from transformers.pipelines.document_question_answering import apply_tesseract from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_detectrona, require_pytesseract, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image from transformers.image_utils import load_image else: class lowercase__: '''simple docstring''' @staticmethod def __lowerCAmelCase ( *lowerCamelCase_ :Any , **lowerCamelCase_ :int ) -> Union[str, Any]: '''simple docstring''' pass def __A ( a_ : Tuple )-> Any: '''simple docstring''' return None # This is a pinned image from a specific revision of a document question answering space, hosted by HuggingFace, # so we can expect it to be available. lowerCamelCase__ : List[Any] = ( "https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png" ) @is_pipeline_test @require_torch @require_vision class lowercase__( unittest.TestCase ): '''simple docstring''' UpperCamelCase = MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING @require_pytesseract @require_vision def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :List[Any] , lowerCamelCase_ :Dict , lowerCamelCase_ :List[str] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = pipeline( '''document-question-answering''' , model=lowerCamelCase_ , tokenizer=lowerCamelCase_ , image_processor=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = INVOICE_URL SCREAMING_SNAKE_CASE : Any = list(zip(*apply_tesseract(load_image(lowerCamelCase_ ) , lowerCamelCase_ , '''''' ) ) ) SCREAMING_SNAKE_CASE : Optional[int] = '''What is the placebo?''' SCREAMING_SNAKE_CASE : int = [ { '''image''': load_image(lowerCamelCase_ ), '''question''': question, }, { '''image''': image, '''question''': question, }, { '''image''': image, '''question''': question, '''word_boxes''': word_boxes, }, ] return dqa_pipeline, examples def __lowerCAmelCase ( self :str , lowerCamelCase_ :Tuple , lowerCamelCase_ :Optional[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = dqa_pipeline(lowerCamelCase_ , top_k=2 ) self.assertEqual( lowerCamelCase_ , [ [ {'''score''': ANY(lowerCamelCase_ ), '''answer''': ANY(lowerCamelCase_ ), '''start''': ANY(lowerCamelCase_ ), '''end''': ANY(lowerCamelCase_ )}, {'''score''': ANY(lowerCamelCase_ ), '''answer''': ANY(lowerCamelCase_ ), '''start''': ANY(lowerCamelCase_ ), '''end''': ANY(lowerCamelCase_ )}, ] ] * 3 , ) @require_torch @require_detectrona @require_pytesseract def __lowerCAmelCase ( self :Any ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = pipeline('''document-question-answering''' , model='''hf-internal-testing/tiny-random-layoutlmv2''' ) SCREAMING_SNAKE_CASE : Optional[int] = INVOICE_URL SCREAMING_SNAKE_CASE : Optional[Any] = '''How many cats are there?''' SCREAMING_SNAKE_CASE : Dict = [ {'''score''': 0.0_0_0_1, '''answer''': '''oy 2312/2019''', '''start''': 38, '''end''': 39}, {'''score''': 0.0_0_0_1, '''answer''': '''oy 2312/2019 DUE''', '''start''': 38, '''end''': 40}, ] SCREAMING_SNAKE_CASE : Optional[int] = dqa_pipeline(image=lowerCamelCase_ , question=lowerCamelCase_ , top_k=2 ) self.assertEqual(nested_simplify(lowerCamelCase_ , decimals=4 ) , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = dqa_pipeline({'''image''': image, '''question''': question} , top_k=2 ) self.assertEqual(nested_simplify(lowerCamelCase_ , decimals=4 ) , lowerCamelCase_ ) # This image does not detect ANY text in it, meaning layoutlmv2 should fail. # Empty answer probably SCREAMING_SNAKE_CASE : List[Any] = '''./tests/fixtures/tests_samples/COCO/000000039769.png''' SCREAMING_SNAKE_CASE : int = dqa_pipeline(image=lowerCamelCase_ , question=lowerCamelCase_ , top_k=2 ) self.assertEqual(lowerCamelCase_ , [] ) # We can optionnally pass directly the words and bounding boxes SCREAMING_SNAKE_CASE : Any = '''./tests/fixtures/tests_samples/COCO/000000039769.png''' SCREAMING_SNAKE_CASE : Any = [] SCREAMING_SNAKE_CASE : str = [] SCREAMING_SNAKE_CASE : str = dqa_pipeline(image=lowerCamelCase_ , question=lowerCamelCase_ , words=lowerCamelCase_ , boxes=lowerCamelCase_ , top_k=2 ) self.assertEqual(lowerCamelCase_ , [] ) @slow @require_torch @require_detectrona @require_pytesseract def __lowerCAmelCase ( self :Optional[int] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = pipeline( '''document-question-answering''' , model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa''' , revision='''9977165''' , ) SCREAMING_SNAKE_CASE : int = INVOICE_URL SCREAMING_SNAKE_CASE : str = '''What is the invoice number?''' SCREAMING_SNAKE_CASE : List[str] = dqa_pipeline(image=lowerCamelCase_ , question=lowerCamelCase_ , top_k=2 ) self.assertEqual( nested_simplify(lowerCamelCase_ , decimals=4 ) , [ {'''score''': 0.9_9_4_4, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0_0_0_9, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) SCREAMING_SNAKE_CASE : Tuple = dqa_pipeline({'''image''': image, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(lowerCamelCase_ , decimals=4 ) , [ {'''score''': 0.9_9_4_4, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0_0_0_9, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) SCREAMING_SNAKE_CASE : str = dqa_pipeline( [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2 ) self.assertEqual( nested_simplify(lowerCamelCase_ , decimals=4 ) , [ [ {'''score''': 0.9_9_4_4, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0_0_0_9, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ], ] * 2 , ) @slow @require_torch @require_detectrona @require_pytesseract def __lowerCAmelCase ( self :Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = pipeline( '''document-question-answering''' , model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa''' , revision='''9977165''' , max_seq_len=50 , ) SCREAMING_SNAKE_CASE : Any = INVOICE_URL SCREAMING_SNAKE_CASE : Optional[Any] = '''What is the invoice number?''' SCREAMING_SNAKE_CASE : Union[str, Any] = dqa_pipeline(image=lowerCamelCase_ , question=lowerCamelCase_ , top_k=2 ) self.assertEqual( nested_simplify(lowerCamelCase_ , decimals=4 ) , [ {'''score''': 0.9_9_7_4, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, {'''score''': 0.9_9_4_8, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) SCREAMING_SNAKE_CASE : Optional[Any] = dqa_pipeline({'''image''': image, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(lowerCamelCase_ , decimals=4 ) , [ {'''score''': 0.9_9_7_4, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, {'''score''': 0.9_9_4_8, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) SCREAMING_SNAKE_CASE : str = dqa_pipeline( [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2 ) self.assertEqual( nested_simplify(lowerCamelCase_ , decimals=4 ) , [ [ {'''score''': 0.9_9_7_4, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, {'''score''': 0.9_9_4_8, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] ] * 2 , ) @slow @require_torch @require_pytesseract @require_vision def __lowerCAmelCase ( self :Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = AutoTokenizer.from_pretrained( '''impira/layoutlm-document-qa''' , revision='''3dc6de3''' , add_prefix_space=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = pipeline( '''document-question-answering''' , model='''impira/layoutlm-document-qa''' , tokenizer=lowerCamelCase_ , revision='''3dc6de3''' , ) SCREAMING_SNAKE_CASE : Optional[Any] = INVOICE_URL SCREAMING_SNAKE_CASE : Any = '''What is the invoice number?''' SCREAMING_SNAKE_CASE : Dict = dqa_pipeline(image=lowerCamelCase_ , question=lowerCamelCase_ , top_k=2 ) self.assertEqual( nested_simplify(lowerCamelCase_ , decimals=4 ) , [ {'''score''': 0.4_2_5_1, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0_8_1_9, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, ] , ) SCREAMING_SNAKE_CASE : Optional[int] = dqa_pipeline({'''image''': image, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(lowerCamelCase_ , decimals=4 ) , [ {'''score''': 0.4_2_5_1, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0_8_1_9, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, ] , ) SCREAMING_SNAKE_CASE : int = dqa_pipeline( [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2 ) self.assertEqual( nested_simplify(lowerCamelCase_ , decimals=4 ) , [ [ {'''score''': 0.4_2_5_1, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0_8_1_9, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, ] ] * 2 , ) SCREAMING_SNAKE_CASE : List[str] = list(zip(*apply_tesseract(load_image(lowerCamelCase_ ) , lowerCamelCase_ , '''''' ) ) ) # This model should also work if `image` is set to None SCREAMING_SNAKE_CASE : List[str] = dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(lowerCamelCase_ , decimals=4 ) , [ {'''score''': 0.4_2_5_1, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0_8_1_9, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, ] , ) @slow @require_torch @require_pytesseract @require_vision def __lowerCAmelCase ( self :int ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = AutoTokenizer.from_pretrained( '''impira/layoutlm-document-qa''' , revision='''3dc6de3''' , add_prefix_space=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = pipeline( '''document-question-answering''' , model='''impira/layoutlm-document-qa''' , tokenizer=lowerCamelCase_ , revision='''3dc6de3''' , max_seq_len=50 , ) SCREAMING_SNAKE_CASE : Any = INVOICE_URL SCREAMING_SNAKE_CASE : str = '''What is the invoice number?''' SCREAMING_SNAKE_CASE : Optional[int] = dqa_pipeline(image=lowerCamelCase_ , question=lowerCamelCase_ , top_k=2 ) self.assertEqual( nested_simplify(lowerCamelCase_ , decimals=4 ) , [ {'''score''': 0.9_9_9_9, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.9_9_9_8, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) SCREAMING_SNAKE_CASE : Tuple = dqa_pipeline( [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2 ) self.assertEqual( nested_simplify(lowerCamelCase_ , decimals=4 ) , [ [ {'''score''': 0.9_9_9_9, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.9_9_9_8, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] ] * 2 , ) SCREAMING_SNAKE_CASE : Any = list(zip(*apply_tesseract(load_image(lowerCamelCase_ ) , lowerCamelCase_ , '''''' ) ) ) # This model should also work if `image` is set to None SCREAMING_SNAKE_CASE : Tuple = dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(lowerCamelCase_ , decimals=4 ) , [ {'''score''': 0.9_9_9_9, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.9_9_9_8, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) @slow @require_torch def __lowerCAmelCase ( self :Optional[int] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = pipeline( '''document-question-answering''' , model='''naver-clova-ix/donut-base-finetuned-docvqa''' , tokenizer=AutoTokenizer.from_pretrained('''naver-clova-ix/donut-base-finetuned-docvqa''' ) , feature_extractor='''naver-clova-ix/donut-base-finetuned-docvqa''' , ) SCREAMING_SNAKE_CASE : List[Any] = INVOICE_URL SCREAMING_SNAKE_CASE : str = '''What is the invoice number?''' SCREAMING_SNAKE_CASE : int = dqa_pipeline(image=lowerCamelCase_ , question=lowerCamelCase_ , top_k=2 ) self.assertEqual(nested_simplify(lowerCamelCase_ , decimals=4 ) , [{'''answer''': '''us-001'''}] ) @require_tf @unittest.skip('''Document question answering not implemented in TF''' ) def __lowerCAmelCase ( self :List[Any] ) -> Tuple: '''simple docstring''' pass
698
"""simple docstring""" def __A ( a_ : int = 10 , a_ : int = 10_00 , a_ : bool = True )-> int: '''simple docstring''' assert ( isinstance(a_ , a_ ) and isinstance(a_ , a_ ) and isinstance(a_ , a_ ) ), "Invalid type of value(s) specified to function!" if min_val > max_val: raise ValueError('''Invalid value for min_val or max_val (min_value < max_value)''' ) return min_val if option else max_val def __A ( a_ : int , a_ : int )-> int: '''simple docstring''' return int((number_a + number_a) / 2 ) def __A ( a_ : int , a_ : int , a_ : int )-> None: '''simple docstring''' assert ( isinstance(a_ , a_ ) and isinstance(a_ , a_ ) and isinstance(a_ , a_ ) ), 'argument values must be type of "int"' if lower > higher: raise ValueError('''argument value for lower and higher must be(lower > higher)''' ) if not lower < to_guess < higher: raise ValueError( '''guess value must be within the range of lower and higher value''' ) def answer(a_ : int ) -> str: if number > to_guess: return "high" elif number < to_guess: return "low" else: return "same" print('''started...''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = lower SCREAMING_SNAKE_CASE : int = higher SCREAMING_SNAKE_CASE : List[str] = [] while True: SCREAMING_SNAKE_CASE : Any = get_avg(a_ , a_ ) last_numbers.append(a_ ) if answer(a_ ) == "low": SCREAMING_SNAKE_CASE : Dict = number elif answer(a_ ) == "high": SCREAMING_SNAKE_CASE : Tuple = number else: break print(F"guess the number : {last_numbers[-1]}" ) print(F"details : {last_numbers!s}" ) def __A ( )-> None: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = int(input('''Enter lower value : ''' ).strip() ) SCREAMING_SNAKE_CASE : Tuple = int(input('''Enter high value : ''' ).strip() ) SCREAMING_SNAKE_CASE : List[str] = int(input('''Enter value to guess : ''' ).strip() ) guess_the_number(a_ , a_ , a_ ) if __name__ == "__main__": main()
698
1
"""simple docstring""" from __future__ import annotations import copy import tempfile import unittest from transformers import CONFIG_MAPPING, AutoConfig, BertConfig, GPTaConfig, TaConfig, TapasConfig, is_tf_available from transformers.testing_utils import ( DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, RequestCounter, require_tensorflow_probability, require_tf, slow, ) from ..bert.test_modeling_bert import BertModelTester if is_tf_available(): from transformers import ( TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSeqaSeqLM, TFAutoModelForSequenceClassification, TFAutoModelForTableQuestionAnswering, TFAutoModelForTokenClassification, TFAutoModelWithLMHead, TFBertForMaskedLM, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertModel, TFFunnelBaseModel, TFFunnelModel, TFGPTaLMHeadModel, TFRobertaForMaskedLM, TFTaForConditionalGeneration, TFTapasForQuestionAnswering, ) from transformers.models.auto.modeling_tf_auto import ( TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_PRETRAINING_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, TF_MODEL_MAPPING, ) from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.tapas.modeling_tf_tapas import TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """new-model""" if is_tf_available(): class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = NewModelConfig @require_tf class lowercase__( unittest.TestCase ): '''simple docstring''' @slow def __lowerCAmelCase ( self :str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = '''bert-base-cased''' SCREAMING_SNAKE_CASE : Union[str, Any] = AutoConfig.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = TFAutoModel.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) @slow def __lowerCAmelCase ( self :Union[str, Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = '''bert-base-cased''' SCREAMING_SNAKE_CASE : Tuple = AutoConfig.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = TFAutoModelForPreTraining.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) @slow def __lowerCAmelCase ( self :Optional[int] ) -> Union[str, Any]: '''simple docstring''' for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE : int = AutoConfig.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = TFAutoModelForCausalLM.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = TFAutoModelForCausalLM.from_pretrained(lowerCamelCase_ , output_loading_info=lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) @slow def __lowerCAmelCase ( self :Optional[Any] ) -> Union[str, Any]: '''simple docstring''' for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE : Optional[int] = AutoConfig.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = TFAutoModelWithLMHead.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) @slow def __lowerCAmelCase ( self :Optional[Any] ) -> int: '''simple docstring''' for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE : Optional[int] = AutoConfig.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = TFAutoModelForMaskedLM.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Tuple = TFAutoModelForMaskedLM.from_pretrained(lowerCamelCase_ , output_loading_info=lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) @slow def __lowerCAmelCase ( self :Optional[Any] ) -> List[str]: '''simple docstring''' for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE : List[str] = AutoConfig.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = TFAutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = TFAutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase_ , output_loading_info=lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) @slow def __lowerCAmelCase ( self :Optional[Any] ) -> Optional[int]: '''simple docstring''' for model_name in ["bert-base-uncased"]: SCREAMING_SNAKE_CASE : Dict = AutoConfig.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = TFAutoModelForSequenceClassification.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) @slow def __lowerCAmelCase ( self :List[str] ) -> Optional[int]: '''simple docstring''' for model_name in ["bert-base-uncased"]: SCREAMING_SNAKE_CASE : Union[str, Any] = AutoConfig.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = TFAutoModelForQuestionAnswering.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) @slow @require_tensorflow_probability def __lowerCAmelCase ( self :List[str] ) -> List[Any]: '''simple docstring''' for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]: SCREAMING_SNAKE_CASE : Optional[int] = AutoConfig.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = TFAutoModelForTableQuestionAnswering.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Tuple = TFAutoModelForTableQuestionAnswering.from_pretrained( lowerCamelCase_ , output_loading_info=lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) def __lowerCAmelCase ( self :str ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = TFAutoModelWithLMHead.from_pretrained(lowerCamelCase_ ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) self.assertEqual(model.num_parameters() , 1_44_10 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase_ ) , 1_44_10 ) def __lowerCAmelCase ( self :Any ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : str = TFAutoModelWithLMHead.from_pretrained(lowerCamelCase_ ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) self.assertEqual(model.num_parameters() , 1_44_10 ) self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase_ ) , 1_44_10 ) def __lowerCAmelCase ( self :Optional[int] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = TFAutoModel.from_pretrained('''sgugger/funnel-random-tiny''' ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = copy.deepcopy(model.config ) SCREAMING_SNAKE_CASE : str = ['''FunnelBaseModel'''] SCREAMING_SNAKE_CASE : Tuple = TFAutoModel.from_config(lowerCamelCase_ ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = TFAutoModel.from_pretrained(lowerCamelCase_ ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) def __lowerCAmelCase ( self :int ) -> Union[str, Any]: '''simple docstring''' try: AutoConfig.register('''new-model''' , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = [ TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSequenceClassification, TFAutoModelForTokenClassification, ] for auto_class in auto_classes: with self.subTest(auto_class.__name__ ): # Wrong config class will raise an error with self.assertRaises(lowerCamelCase_ ): auto_class.register(lowerCamelCase_ , lowerCamelCase_ ) auto_class.register(lowerCamelCase_ , lowerCamelCase_ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(lowerCamelCase_ ): auto_class.register(lowerCamelCase_ , lowerCamelCase_ ) # Now that the config is registered, it can be used as any other config with the auto-API SCREAMING_SNAKE_CASE : Optional[Any] = BertModelTester(self ).get_config() SCREAMING_SNAKE_CASE : Tuple = NewModelConfig(**tiny_config.to_dict() ) SCREAMING_SNAKE_CASE : List[Any] = auto_class.from_config(lowerCamelCase_ ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = auto_class.from_pretrained(lowerCamelCase_ ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) finally: if "new-model" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["new-model"] for mapping in ( TF_MODEL_MAPPING, TF_MODEL_FOR_PRETRAINING_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, ): if NewModelConfig in mapping._extra_content: del mapping._extra_content[NewModelConfig] def __lowerCAmelCase ( self :Optional[int] ) -> Any: '''simple docstring''' with self.assertRaisesRegex( lowerCamelCase_ , '''bert-base is not a local folder and is not a valid model identifier''' ): SCREAMING_SNAKE_CASE : Dict = TFAutoModel.from_pretrained('''bert-base''' ) def __lowerCAmelCase ( self :Any ) -> Any: '''simple docstring''' with self.assertRaisesRegex( lowerCamelCase_ , R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): SCREAMING_SNAKE_CASE : int = TFAutoModel.from_pretrained(lowerCamelCase_ , revision='''aaaaaa''' ) def __lowerCAmelCase ( self :List[Any] ) -> Any: '''simple docstring''' with self.assertRaisesRegex( lowerCamelCase_ , '''hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin''' , ): SCREAMING_SNAKE_CASE : Any = TFAutoModel.from_pretrained('''hf-internal-testing/config-no-model''' ) def __lowerCAmelCase ( self :List[str] ) -> Any: '''simple docstring''' with self.assertRaisesRegex(lowerCamelCase_ , '''Use `from_pt=True` to load this model''' ): SCREAMING_SNAKE_CASE : Dict = TFAutoModel.from_pretrained('''hf-internal-testing/tiny-bert-pt-only''' ) def __lowerCAmelCase ( self :int ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = TFAutoModel.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) with RequestCounter() as counter: SCREAMING_SNAKE_CASE : int = TFAutoModel.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 ) # With a sharded checkpoint SCREAMING_SNAKE_CASE : Optional[Any] = TFAutoModel.from_pretrained('''ArthurZ/tiny-random-bert-sharded''' ) with RequestCounter() as counter: SCREAMING_SNAKE_CASE : List[str] = TFAutoModel.from_pretrained('''ArthurZ/tiny-random-bert-sharded''' ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 )
698
"""simple docstring""" import argparse import fairseq import torch from torch import nn from transformers import ( MBartaaTokenizer, MBartConfig, MBartForCausalLM, SpeechEncoderDecoderConfig, SpeechEncoderDecoderModel, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaModel, logging, ) logging.set_verbosity_info() lowerCamelCase__ : List[Any] = logging.get_logger(__name__) lowerCamelCase__ : Tuple = { "post_extract_proj": "feature_projection.projection", "encoder.pos_conv.0": "encoder.pos_conv_embed.conv", "self_attn.k_proj": "encoder.layers.*.attention.k_proj", "self_attn.v_proj": "encoder.layers.*.attention.v_proj", "self_attn.q_proj": "encoder.layers.*.attention.q_proj", "self_attn.out_proj": "encoder.layers.*.attention.out_proj", "self_attn_layer_norm": "encoder.layers.*.layer_norm", "fc1": "encoder.layers.*.feed_forward.intermediate_dense", "fc2": "encoder.layers.*.feed_forward.output_dense", "final_layer_norm": "encoder.layers.*.final_layer_norm", "encoder.layer_norm": "encoder.layer_norm", "w2v_model.layer_norm": "feature_projection.layer_norm", "quantizer.weight_proj": "quantizer.weight_proj", "quantizer.vars": "quantizer.codevectors", "project_q": "project_q", "final_proj": "project_hid", "w2v_encoder.proj": "lm_head", "mask_emb": "masked_spec_embed", } lowerCamelCase__ : List[str] = [ "lm_head", "quantizer.weight_proj", "quantizer.codevectors", "project_q", "project_hid", ] def __A ( a_ : Optional[int] , a_ : str , a_ : str , a_ : str , a_ : List[str] )-> Tuple: '''simple docstring''' for attribute in key.split('''.''' ): SCREAMING_SNAKE_CASE : Any = getattr(a_ , a_ ) if weight_type is not None: SCREAMING_SNAKE_CASE : Optional[int] = getattr(a_ , a_ ).shape else: SCREAMING_SNAKE_CASE : Any = hf_pointer.shape assert hf_shape == value.shape, ( F"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be" F" {value.shape} for {full_name}" ) if weight_type == "weight": SCREAMING_SNAKE_CASE : List[Any] = value elif weight_type == "weight_g": SCREAMING_SNAKE_CASE : Optional[int] = value elif weight_type == "weight_v": SCREAMING_SNAKE_CASE : Any = value elif weight_type == "bias": SCREAMING_SNAKE_CASE : List[Any] = value else: SCREAMING_SNAKE_CASE : List[str] = value logger.info(F"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." ) def __A ( a_ : Optional[Any] , a_ : Dict )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = [] SCREAMING_SNAKE_CASE : Optional[Any] = fairseq_model.state_dict() SCREAMING_SNAKE_CASE : Tuple = hf_model.feature_extractor SCREAMING_SNAKE_CASE : Tuple = hf_model.adapter for name, value in fairseq_dict.items(): SCREAMING_SNAKE_CASE : int = False if "conv_layers" in name: load_conv_layer( a_ , a_ , a_ , a_ , hf_model.config.feat_extract_norm == '''group''' , ) SCREAMING_SNAKE_CASE : List[str] = True elif any(x in name for x in ['''adaptor''', '''w2v_encoder.proj.''', '''w2v_proj_ln.'''] ): load_adapter(a_ , a_ , a_ , a_ ) SCREAMING_SNAKE_CASE : List[Any] = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split('''w2v_model.''' )[-1] == name.split('''.''' )[0]: SCREAMING_SNAKE_CASE : Union[str, Any] = True if "*" in mapped_key: SCREAMING_SNAKE_CASE : Dict = name.split(a_ )[0].split('''.''' )[-2] SCREAMING_SNAKE_CASE : Optional[int] = mapped_key.replace('''*''' , a_ ) if "weight_g" in name: SCREAMING_SNAKE_CASE : List[str] = '''weight_g''' elif "weight_v" in name: SCREAMING_SNAKE_CASE : Union[str, Any] = '''weight_v''' elif "bias" in name: SCREAMING_SNAKE_CASE : str = '''bias''' elif "weight" in name: SCREAMING_SNAKE_CASE : Tuple = '''weight''' else: SCREAMING_SNAKE_CASE : str = None set_recursively(a_ , a_ , a_ , a_ , a_ ) continue if not is_used: unused_weights.append(a_ ) logger.warning(F"Unused weights: {unused_weights}" ) def __A ( a_ : Dict , a_ : int , a_ : Optional[int] , a_ : Optional[int] , a_ : Dict )-> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = full_name.split('''conv_layers.''' )[-1] SCREAMING_SNAKE_CASE : List[str] = name.split('''.''' ) SCREAMING_SNAKE_CASE : Dict = int(items[0] ) SCREAMING_SNAKE_CASE : Optional[Any] = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found." ) SCREAMING_SNAKE_CASE : List[Any] = value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found." ) SCREAMING_SNAKE_CASE : str = value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( F"{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was" " found." ) SCREAMING_SNAKE_CASE : str = value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found." ) SCREAMING_SNAKE_CASE : Union[str, Any] = value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) else: unused_weights.append(a_ ) def __A ( a_ : Optional[int] , a_ : Optional[int] , a_ : Any , a_ : Any )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = full_name.split('''adaptor.''' )[-1] SCREAMING_SNAKE_CASE : List[Any] = name.split('''.''' ) if items[1].isdigit(): SCREAMING_SNAKE_CASE : List[Any] = int(items[1] ) else: SCREAMING_SNAKE_CASE : str = None if "adaptor" not in full_name: if "proj_ln" in full_name: # has to be layer norm if "bias" in name: assert ( value.shape == adapter.proj_layer_norm.bias.data.shape ), F"{full_name} has size {value.shape}, but {adapter.proj_layer_norm.bias.data.shape} was found." SCREAMING_SNAKE_CASE : str = value logger.info(F"Adapter proj layer norm bias was initialized from {full_name}." ) if "weight" in name: assert ( value.shape == adapter.proj_layer_norm.weight.data.shape ), F"{full_name} has size {value.shape}, but {adapter.proj_layer_norm.weight.data.shape} was found." SCREAMING_SNAKE_CASE : Optional[Any] = value else: # has to be projection layer if "bias" in name: assert ( value.shape == adapter.proj.bias.data.shape ), F"{full_name} has size {value.shape}, but {adapter.proj.bias.data.shape} was found." SCREAMING_SNAKE_CASE : Union[str, Any] = value logger.info(F"Adapter proj layer bias was initialized from {full_name}." ) if "weight" in name: assert ( value.shape == adapter.proj.weight.data.shape ), F"{full_name} has size {value.shape}, but {adapter.proj.weight.data.shape} was found." SCREAMING_SNAKE_CASE : int = value logger.info(F"Adapter proj layer weight was initialized from {full_name}." ) elif isinstance(a_ , a_ ): if "bias" in name: assert ( value.shape == adapter.layers[layer_id].conv.bias.data.shape ), F"{full_name} has size {value.shape}, but {adapter.layers[layer_id].conv.bias.data.shape} was found." SCREAMING_SNAKE_CASE : str = value logger.info(F"Adapter layer {layer_id} bias was initialized from {full_name}." ) elif "weight" in name: assert ( value.shape == adapter.layers[layer_id].conv.weight.data.shape ), F"{full_name} has size {value.shape}, but {adapter.layers[layer_id].conv.weight.data.shape} was found." SCREAMING_SNAKE_CASE : List[str] = value logger.info(F"Adapter layer {layer_id} bias was initialized from {full_name}." ) else: unused_weights.append(a_ ) def __A ( a_ : Optional[Any] )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = emb.weight.shape SCREAMING_SNAKE_CASE : Any = nn.Linear(a_ , a_ , bias=a_ ) SCREAMING_SNAKE_CASE : Optional[int] = emb.weight.data return lin_layer @torch.no_grad() def __A ( a_ : Tuple , a_ : Optional[int] , a_ : List[Any] , a_ : Any , a_ : Tuple , a_ : int , a_ : Any , a_ : str , a_ : Tuple , a_ : Union[str, Any] , a_ : Union[str, Any] , )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = WavaVecaConfig.from_pretrained( a_ , add_adapter=a_ , adapter_stride=a_ , adapter_kernel_size=a_ , use_auth_token=a_ , output_hidden_size=a_ , ) SCREAMING_SNAKE_CASE : Union[str, Any] = MBartConfig.from_pretrained(a_ ) # load model SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={ '''config_yaml''': config_yaml_path, '''data''': '''/'''.join(dict_path.split('''/''' )[:-1] ), '''w2v_path''': checkpoint_path, '''load_pretrained_decoder_from''': None, } , ) SCREAMING_SNAKE_CASE : int = model[0].eval() # load feature extractor SCREAMING_SNAKE_CASE : Union[str, Any] = WavaVecaFeatureExtractor.from_pretrained(a_ , use_auth_token=a_ ) # set weights for wav2vec2 encoder SCREAMING_SNAKE_CASE : str = WavaVecaModel(a_ ) recursively_load_weights_wavaveca(model.encoder , a_ ) # load decoder weights SCREAMING_SNAKE_CASE : Dict = MBartForCausalLM(a_ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict() , strict=a_ ) logger.warning(F"The following keys are missing when loading the decoder weights: {missing_keys}" ) logger.warning(F"The following keys are unexpected when loading the decoder weights: {unexpected_keys}" ) SCREAMING_SNAKE_CASE : Union[str, Any] = SpeechEncoderDecoderModel(encoder=a_ , decoder=a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = False SCREAMING_SNAKE_CASE : Optional[Any] = MBartaaTokenizer(a_ ) tokenizer.save_pretrained(a_ ) SCREAMING_SNAKE_CASE : Tuple = hf_wavavec.config.to_dict() SCREAMING_SNAKE_CASE : Any = tokenizer.pad_token_id SCREAMING_SNAKE_CASE : List[str] = tokenizer.bos_token_id SCREAMING_SNAKE_CASE : Dict = tokenizer.eos_token_id SCREAMING_SNAKE_CASE : Optional[Any] = '''mbart50''' SCREAMING_SNAKE_CASE : Optional[int] = '''wav2vec2''' SCREAMING_SNAKE_CASE : Optional[Any] = tokenizer.eos_token_id SCREAMING_SNAKE_CASE : List[str] = 25_00_04 SCREAMING_SNAKE_CASE : Dict = tokenizer.eos_token_id SCREAMING_SNAKE_CASE : Any = SpeechEncoderDecoderConfig.from_dict(a_ ) hf_wavavec.save_pretrained(a_ ) feature_extractor.save_pretrained(a_ ) if __name__ == "__main__": lowerCamelCase__ : Any = argparse.ArgumentParser() parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to fairseq checkpoint") parser.add_argument("--dict_path", default=None, type=str, help="Path to dict of fine-tuned model") parser.add_argument("--config_yaml_path", default=None, type=str, help="Path to yaml file of fine-tuned model") parser.add_argument( "--encoder_config_path", default="facebook/wav2vec2-xls-r-1b", type=str, help="Path to hf encoder wav2vec2 checkpoint config", ) parser.add_argument( "--decoder_config_path", default="facebook/mbart-large-50-one-to-many-mmt", type=str, help="Path to hf decoder checkpoint config", ) parser.add_argument("--add_adapter", default=True, type=bool, help="whethere to add model adapter layers") parser.add_argument("--adapter_stride", default=2, type=int, help="stride of adapter layers") parser.add_argument("--adapter_kernel_size", default=3, type=int, help="kernel size of adapter layers") parser.add_argument("--encoder_output_dim", default=1024, type=int, help="encoder output dim") parser.add_argument("--start_token_id", default=250004, type=int, help="`decoder_start_token_id` of model config") lowerCamelCase__ : Dict = parser.parse_args() convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.dict_path, args.config_yaml_path, encoder_config_path=args.encoder_config_path, decoder_config_path=args.decoder_config_path, add_adapter=args.add_adapter, adapter_kernel_size=args.adapter_kernel_size, adapter_stride=args.adapter_stride, decoder_start_token_id=args.start_token_id, encoder_output_dim=args.encoder_output_dim, )
698
1
"""simple docstring""" from __future__ import annotations lowerCamelCase__ : List[Any] = [-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0] lowerCamelCase__ : List[Any] = [-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1] def __A ( a_ : list[float] )-> list[float]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = [] SCREAMING_SNAKE_CASE : Any = len(a_ ) for i in range(a_ ): SCREAMING_SNAKE_CASE : float = -1 for j in range(i + 1 , a_ ): if arr[i] < arr[j]: SCREAMING_SNAKE_CASE : Any = arr[j] break result.append(a_ ) return result def __A ( a_ : list[float] )-> list[float]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = [] for i, outer in enumerate(a_ ): SCREAMING_SNAKE_CASE : float = -1 for inner in arr[i + 1 :]: if outer < inner: SCREAMING_SNAKE_CASE : Optional[int] = inner break result.append(a_ ) return result def __A ( a_ : list[float] )-> list[float]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = len(a_ ) SCREAMING_SNAKE_CASE : list[float] = [] SCREAMING_SNAKE_CASE : list[float] = [-1] * arr_size for index in reversed(range(a_ ) ): if stack: while stack[-1] <= arr[index]: stack.pop() if not stack: break if stack: SCREAMING_SNAKE_CASE : str = stack[-1] stack.append(arr[index] ) return result if __name__ == "__main__": from doctest import testmod from timeit import timeit testmod() print(next_greatest_element_slow(arr)) print(next_greatest_element_fast(arr)) print(next_greatest_element(arr)) lowerCamelCase__ : List[str] = ( "from __main__ import arr, next_greatest_element_slow, " "next_greatest_element_fast, next_greatest_element" ) print( "next_greatest_element_slow():", timeit("next_greatest_element_slow(arr)", setup=setup), ) print( "next_greatest_element_fast():", timeit("next_greatest_element_fast(arr)", setup=setup), ) print( " next_greatest_element():", timeit("next_greatest_element(arr)", setup=setup), )
698
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available lowerCamelCase__ : Union[str, Any] = { "configuration_m2m_100": ["M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP", "M2M100Config", "M2M100OnnxConfig"], "tokenization_m2m_100": ["M2M100Tokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : str = [ "M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST", "M2M100ForConditionalGeneration", "M2M100Model", "M2M100PreTrainedModel", ] if TYPE_CHECKING: from .configuration_mam_aaa import M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP, MaMaaaConfig, MaMaaaOnnxConfig from .tokenization_mam_aaa import MaMaaaTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mam_aaa import ( M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST, MaMaaaForConditionalGeneration, MaMaaaModel, MaMaaaPreTrainedModel, ) else: import sys lowerCamelCase__ : Optional[int] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
698
1
"""simple docstring""" from collections.abc import Sequence def __A ( a_ : Sequence[int] | None = None )-> int: '''simple docstring''' if nums is None or not nums: raise ValueError('''Input sequence should not be empty''' ) SCREAMING_SNAKE_CASE : str = nums[0] for i in range(1 , len(a_ ) ): SCREAMING_SNAKE_CASE : Optional[Any] = nums[i] SCREAMING_SNAKE_CASE : Tuple = max(a_ , ans + num , a_ ) return ans if __name__ == "__main__": import doctest doctest.testmod() # Try on a sample input from the user lowerCamelCase__ : int = int(input("Enter number of elements : ").strip()) lowerCamelCase__ : Union[str, Any] = list(map(int, input("\nEnter the numbers : ").strip().split()))[:n] print(max_subsequence_sum(array))
698
"""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 lowerCamelCase__ : List[Any] = "\\n@inproceedings{pillutla-etal:mauve:neurips2021,\n title={MAUVE: Measuring the Gap Between Neural Text and Human Text using Divergence Frontiers},\n author={Pillutla, Krishna and Swayamdipta, Swabha and Zellers, Rowan and Thickstun, John and Welleck, Sean and Choi, Yejin and Harchaoui, Zaid},\n booktitle = {NeurIPS},\n year = {2021}\n}\n\n" lowerCamelCase__ : List[str] = "\\nMAUVE is a library built on PyTorch and HuggingFace Transformers to measure the gap between neural text and human text with the eponymous MAUVE measure.\n\nMAUVE summarizes both Type I and Type II errors measured softly using Kullback–Leibler (KL) divergences.\n\nFor details, see the MAUVE paper: https://arxiv.org/abs/2102.01454 (Neurips, 2021).\n\nThis metrics is a wrapper around the official implementation of MAUVE:\nhttps://github.com/krishnap25/mauve\n" lowerCamelCase__ : List[Any] = "\nCalculates MAUVE scores between two lists of generated text and reference text.\nArgs:\n predictions: list of generated text to score. Each predictions\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\nOptional Args:\n num_buckets: the size of the histogram to quantize P and Q. Options: 'auto' (default) or an integer\n pca_max_data: the number data points to use for PCA dimensionality reduction prior to clustering. If -1, use all the data. Default -1\n kmeans_explained_var: amount of variance of the data to keep in dimensionality reduction by PCA. Default 0.9\n kmeans_num_redo: number of times to redo k-means clustering (the best objective is kept). Default 5\n kmeans_max_iter: maximum number of k-means iterations. Default 500\n 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'].\n 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\n max_text_length: maximum number of tokens to consider. Default 1024\n divergence_curve_discretization_size: Number of points to consider on the divergence curve. Default 25\n mauve_scaling_factor: \"c\" from the paper. Default 5.\n verbose: If True (default), print running time updates\n seed: random seed to initialize k-means cluster assignments.\nReturns:\n mauve: MAUVE score, a number between 0 and 1. Larger values indicate that P and Q are closer,\n frontier_integral: Frontier Integral, a number between 0 and 1. Smaller values indicate that P and Q are closer,\n divergence_curve: a numpy.ndarray of shape (m, 2); plot it with matplotlib to view the divergence curve,\n p_hist: a discrete distribution, which is a quantized version of the text distribution p_text,\n q_hist: same as above, but with q_text.\nExamples:\n\n >>> # faiss segfaults in doctest for some reason, so the .compute call is not tested with doctest\n >>> import datasets\n >>> mauve = datasets.load_metric('mauve')\n >>> predictions = [\"hello there\", \"general kenobi\"]\n >>> references = [\"hello there\", \"general kenobi\"]\n >>> out = mauve.compute(predictions=predictions, references=references) # doctest: +SKIP\n >>> print(out.mauve) # doctest: +SKIP\n 1.0\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowercase__( datasets.Metric ): '''simple docstring''' def __lowerCAmelCase ( self :Optional[int] ) -> 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 __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :List[str] , lowerCamelCase_ :Optional[Any]=None , lowerCamelCase_ :str=None , lowerCamelCase_ :Tuple=None , lowerCamelCase_ :Optional[Any]=None , lowerCamelCase_ :Optional[int]="auto" , lowerCamelCase_ :Dict=-1 , lowerCamelCase_ :str=0.9 , lowerCamelCase_ :str=5 , lowerCamelCase_ :Tuple=5_00 , lowerCamelCase_ :str="gpt2-large" , lowerCamelCase_ :List[Any]=-1 , lowerCamelCase_ :Dict=10_24 , lowerCamelCase_ :Tuple=25 , lowerCamelCase_ :List[Any]=5 , lowerCamelCase_ :Dict=True , lowerCamelCase_ :List[Any]=25 , ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = compute_mauve( p_text=lowerCamelCase_ , q_text=lowerCamelCase_ , p_features=lowerCamelCase_ , q_features=lowerCamelCase_ , p_tokens=lowerCamelCase_ , q_tokens=lowerCamelCase_ , num_buckets=lowerCamelCase_ , pca_max_data=lowerCamelCase_ , kmeans_explained_var=lowerCamelCase_ , kmeans_num_redo=lowerCamelCase_ , kmeans_max_iter=lowerCamelCase_ , featurize_model_name=lowerCamelCase_ , device_id=lowerCamelCase_ , max_text_length=lowerCamelCase_ , divergence_curve_discretization_size=lowerCamelCase_ , mauve_scaling_factor=lowerCamelCase_ , verbose=lowerCamelCase_ , seed=lowerCamelCase_ , ) return out
698
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available lowerCamelCase__ : Optional[int] = { "configuration_groupvit": [ "GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP", "GroupViTConfig", "GroupViTOnnxConfig", "GroupViTTextConfig", "GroupViTVisionConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Optional[int] = [ "GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST", "GroupViTModel", "GroupViTPreTrainedModel", "GroupViTTextModel", "GroupViTVisionModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : List[Any] = [ "TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST", "TFGroupViTModel", "TFGroupViTPreTrainedModel", "TFGroupViTTextModel", "TFGroupViTVisionModel", ] if TYPE_CHECKING: from .configuration_groupvit import ( GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GroupViTConfig, GroupViTOnnxConfig, GroupViTTextConfig, GroupViTVisionConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_groupvit import ( GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, GroupViTModel, GroupViTPreTrainedModel, GroupViTTextModel, GroupViTVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_groupvit import ( TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFGroupViTModel, TFGroupViTPreTrainedModel, TFGroupViTTextModel, TFGroupViTVisionModel, ) else: import sys lowerCamelCase__ : Optional[int] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
698
"""simple docstring""" import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert_fast import BertTokenizerFast from .tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer, DPRReaderTokenizer lowerCamelCase__ : Union[str, Any] = logging.get_logger(__name__) lowerCamelCase__ : Union[str, Any] = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} lowerCamelCase__ : Any = { "vocab_file": { "facebook/dpr-ctx_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt" ), "facebook/dpr-ctx_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "facebook/dpr-ctx_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json" ), "facebook/dpr-ctx_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json" ), }, } lowerCamelCase__ : str = { "vocab_file": { "facebook/dpr-question_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt" ), "facebook/dpr-question_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "facebook/dpr-question_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json" ), "facebook/dpr-question_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json" ), }, } lowerCamelCase__ : Any = { "vocab_file": { "facebook/dpr-reader-single-nq-base": ( "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt" ), "facebook/dpr-reader-multiset-base": ( "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "facebook/dpr-reader-single-nq-base": ( "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json" ), "facebook/dpr-reader-multiset-base": ( "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json" ), }, } lowerCamelCase__ : Optional[Any] = { "facebook/dpr-ctx_encoder-single-nq-base": 512, "facebook/dpr-ctx_encoder-multiset-base": 512, } lowerCamelCase__ : Tuple = { "facebook/dpr-question_encoder-single-nq-base": 512, "facebook/dpr-question_encoder-multiset-base": 512, } lowerCamelCase__ : Dict = { "facebook/dpr-reader-single-nq-base": 512, "facebook/dpr-reader-multiset-base": 512, } lowerCamelCase__ : int = { "facebook/dpr-ctx_encoder-single-nq-base": {"do_lower_case": True}, "facebook/dpr-ctx_encoder-multiset-base": {"do_lower_case": True}, } lowerCamelCase__ : Tuple = { "facebook/dpr-question_encoder-single-nq-base": {"do_lower_case": True}, "facebook/dpr-question_encoder-multiset-base": {"do_lower_case": True}, } lowerCamelCase__ : Dict = { "facebook/dpr-reader-single-nq-base": {"do_lower_case": True}, "facebook/dpr-reader-multiset-base": {"do_lower_case": True}, } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = VOCAB_FILES_NAMES UpperCamelCase = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase = DPRContextEncoderTokenizer class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = VOCAB_FILES_NAMES UpperCamelCase = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase = DPRQuestionEncoderTokenizer lowerCamelCase__ : Union[str, Any] = collections.namedtuple( "DPRSpanPrediction", ["span_score", "relevance_score", "doc_id", "start_index", "end_index", "text"] ) lowerCamelCase__ : int = collections.namedtuple("DPRReaderOutput", ["start_logits", "end_logits", "relevance_logits"]) lowerCamelCase__ : str = r"\n Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`.\n It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers),\n using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)`\n with the format:\n\n [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids>\n\n Args:\n questions (`str` or `List[str]`):\n The questions to be encoded. You can specify one question for many passages. In this case, the question\n will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in\n `titles` or `texts`.\n titles (`str` or `List[str]`):\n The passages titles to be encoded. This can be a string or a list of strings if there are several passages.\n texts (`str` or `List[str]`):\n The passages texts to be encoded. This can be a string or a list of strings if there are several passages.\n padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`):\n Activates and controls padding. Accepts the following values:\n\n - `True` or `'longest'`: Pad to the longest sequence in the batch (or no padding if only a single sequence\n if provided).\n - `'max_length'`: Pad to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided.\n - `False` or `'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of different\n lengths).\n truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`):\n Activates and controls truncation. Accepts the following values:\n\n - `True` or `'longest_first'`: Truncate to a maximum length specified with the argument `max_length` or to\n the maximum acceptable input length for the model if that argument is not provided. This will truncate\n token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch\n of pairs) is provided.\n - `'only_first'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the first\n sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `'only_second'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the\n second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `False` or `'do_not_truncate'` (default): No truncation (i.e., can output batch with sequence lengths\n greater than the model maximum admissible input size).\n max_length (`int`, *optional*):\n Controls the maximum length to use by one of the truncation/padding parameters.\n\n If left unset or set to `None`, this will use the predefined model maximum length if a maximum length\n is required by one of the truncation/padding parameters. If the model has no specific maximum input\n length (like XLNet) truncation/padding to a maximum length will be deactivated.\n return_tensors (`str` or [`~utils.TensorType`], *optional*):\n If set, will return tensors instead of list of python integers. Acceptable values are:\n\n - `'tf'`: Return TensorFlow `tf.constant` objects.\n - `'pt'`: Return PyTorch `torch.Tensor` objects.\n - `'np'`: Return Numpy `np.ndarray` objects.\n return_attention_mask (`bool`, *optional*):\n Whether or not to return the attention mask. If not set, will return the attention mask according to the\n specific tokenizer's default, defined by the `return_outputs` attribute.\n\n [What are attention masks?](../glossary#attention-mask)\n\n Return:\n `Dict[str, List[List[int]]]`: A dictionary with the following keys:\n\n - `input_ids`: List of token ids to be fed to a model.\n - `attention_mask`: List of indices specifying which tokens should be attended to by the model.\n " @add_start_docstrings(_UpperCAmelCase ) class lowercase__: '''simple docstring''' def __call__( self :str , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Optional[str] = None , lowerCamelCase_ :Optional[str] = None , lowerCamelCase_ :Union[bool, str] = False , lowerCamelCase_ :Union[bool, str] = False , lowerCamelCase_ :Optional[int] = None , lowerCamelCase_ :Optional[Union[str, TensorType]] = None , lowerCamelCase_ :Optional[bool] = None , **lowerCamelCase_ :Tuple , ) -> BatchEncoding: '''simple docstring''' if titles is None and texts is None: return super().__call__( lowerCamelCase_ , padding=lowerCamelCase_ , truncation=lowerCamelCase_ , max_length=lowerCamelCase_ , return_tensors=lowerCamelCase_ , return_attention_mask=lowerCamelCase_ , **lowerCamelCase_ , ) elif titles is None or texts is None: SCREAMING_SNAKE_CASE : List[str] = titles if texts is None else texts return super().__call__( lowerCamelCase_ , lowerCamelCase_ , padding=lowerCamelCase_ , truncation=lowerCamelCase_ , max_length=lowerCamelCase_ , return_tensors=lowerCamelCase_ , return_attention_mask=lowerCamelCase_ , **lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Dict = titles if not isinstance(lowerCamelCase_ , lowerCamelCase_ ) else [titles] SCREAMING_SNAKE_CASE : Dict = texts if not isinstance(lowerCamelCase_ , lowerCamelCase_ ) else [texts] SCREAMING_SNAKE_CASE : Optional[int] = len(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = questions if not isinstance(lowerCamelCase_ , lowerCamelCase_ ) else [questions] * n_passages assert len(lowerCamelCase_ ) == len( lowerCamelCase_ ), f"There should be as many titles than texts but got {len(lowerCamelCase_ )} titles and {len(lowerCamelCase_ )} texts." SCREAMING_SNAKE_CASE : Any = super().__call__(lowerCamelCase_ , lowerCamelCase_ , padding=lowerCamelCase_ , truncation=lowerCamelCase_ )['''input_ids'''] SCREAMING_SNAKE_CASE : Dict = super().__call__(lowerCamelCase_ , add_special_tokens=lowerCamelCase_ , padding=lowerCamelCase_ , truncation=lowerCamelCase_ )['''input_ids'''] SCREAMING_SNAKE_CASE : int = { '''input_ids''': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(lowerCamelCase_ , lowerCamelCase_ ) ] } if return_attention_mask is not False: SCREAMING_SNAKE_CASE : List[str] = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) SCREAMING_SNAKE_CASE : int = attention_mask return self.pad(lowerCamelCase_ , padding=lowerCamelCase_ , max_length=lowerCamelCase_ , return_tensors=lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :BatchEncoding , lowerCamelCase_ :DPRReaderOutput , lowerCamelCase_ :int = 16 , lowerCamelCase_ :int = 64 , lowerCamelCase_ :int = 4 , ) -> List[DPRSpanPrediction]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = reader_input['''input_ids'''] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = reader_output[:3] SCREAMING_SNAKE_CASE : Dict = len(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = sorted(range(lowerCamelCase_ ) , reverse=lowerCamelCase_ , key=relevance_logits.__getitem__ ) SCREAMING_SNAKE_CASE : List[DPRReaderOutput] = [] for doc_id in sorted_docs: SCREAMING_SNAKE_CASE : Union[str, Any] = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence SCREAMING_SNAKE_CASE : int = sequence_ids.index(self.sep_token_id , 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: SCREAMING_SNAKE_CASE : Dict = sequence_ids.index(self.pad_token_id ) else: SCREAMING_SNAKE_CASE : Optional[int] = len(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=lowerCamelCase_ , top_spans=lowerCamelCase_ , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=lowerCamelCase_ , start_index=lowerCamelCase_ , end_index=lowerCamelCase_ , text=self.decode(sequence_ids[start_index : end_index + 1] ) , ) ) if len(lowerCamelCase_ ) >= num_spans: break return nbest_spans_predictions[:num_spans] def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :List[int] , lowerCamelCase_ :List[int] , lowerCamelCase_ :int , lowerCamelCase_ :int , ) -> List[DPRSpanPrediction]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = [] for start_index, start_score in enumerate(lowerCamelCase_ ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) SCREAMING_SNAKE_CASE : Dict = sorted(lowerCamelCase_ , key=lambda lowerCamelCase_ : x[1] , reverse=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = [] for (start_index, end_index), score in scores: assert start_index <= end_index, f"Wrong span indices: [{start_index}:{end_index}]" SCREAMING_SNAKE_CASE : Optional[int] = end_index - start_index + 1 assert length <= max_answer_length, f"Span is too long: {length} > {max_answer_length}" if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(lowerCamelCase_ ) == top_spans: break return chosen_span_intervals @add_end_docstrings(_UpperCAmelCase ) class lowercase__( _UpperCAmelCase , _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = VOCAB_FILES_NAMES UpperCamelCase = READER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase = READER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase = ["""input_ids""", """attention_mask"""] UpperCamelCase = DPRReaderTokenizer
698
1
"""simple docstring""" import numpy as np # Importing the Keras libraries and packages import tensorflow as tf from tensorflow.keras import layers, models if __name__ == "__main__": # Initialising the CNN # (Sequential- Building the model layer by layer) lowerCamelCase__ : str = models.Sequential() # Step 1 - Convolution # Here 64,64 is the length & breadth of dataset images and 3 is for the RGB channel # (3,3) is the kernel size (filter matrix) classifier.add( layers.ConvaD(32, (3, 3), input_shape=(64, 64, 3), activation="relu") ) # Step 2 - Pooling classifier.add(layers.MaxPoolingaD(pool_size=(2, 2))) # Adding a second convolutional layer classifier.add(layers.ConvaD(32, (3, 3), activation="relu")) classifier.add(layers.MaxPoolingaD(pool_size=(2, 2))) # Step 3 - Flattening classifier.add(layers.Flatten()) # Step 4 - Full connection classifier.add(layers.Dense(units=128, activation="relu")) classifier.add(layers.Dense(units=1, activation="sigmoid")) # Compiling the CNN classifier.compile( optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"] ) # Part 2 - Fitting the CNN to the images # Load Trained model weights # from keras.models import load_model # regressor=load_model('cnn.h5') lowerCamelCase__ : Union[str, Any] = tf.keras.preprocessing.image.ImageDataGenerator( rescale=1.0 / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True ) lowerCamelCase__ : List[Any] = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1.0 / 255) lowerCamelCase__ : List[str] = train_datagen.flow_from_directory( "dataset/training_set", target_size=(64, 64), batch_size=32, class_mode="binary" ) lowerCamelCase__ : Union[str, Any] = test_datagen.flow_from_directory( "dataset/test_set", target_size=(64, 64), batch_size=32, class_mode="binary" ) classifier.fit_generator( training_set, steps_per_epoch=5, epochs=30, validation_data=test_set ) classifier.save("cnn.h5") # Part 3 - Making new predictions lowerCamelCase__ : Optional[int] = tf.keras.preprocessing.image.load_img( "dataset/single_prediction/image.png", target_size=(64, 64) ) lowerCamelCase__ : int = tf.keras.preprocessing.image.img_to_array(test_image) lowerCamelCase__ : str = np.expand_dims(test_image, axis=0) lowerCamelCase__ : Optional[int] = classifier.predict(test_image) # training_set.class_indices if result[0][0] == 0: lowerCamelCase__ : List[Any] = "Normal" if result[0][0] == 1: lowerCamelCase__ : Any = "Abnormality detected"
698
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : Optional[Any] = { "microsoft/markuplm-base": "https://huggingface.co/microsoft/markuplm-base/resolve/main/config.json", "microsoft/markuplm-large": "https://huggingface.co/microsoft/markuplm-large/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """markuplm""" def __init__( self :int , lowerCamelCase_ :List[str]=3_05_22 , lowerCamelCase_ :Union[str, Any]=7_68 , lowerCamelCase_ :str=12 , lowerCamelCase_ :Dict=12 , lowerCamelCase_ :str=30_72 , lowerCamelCase_ :Union[str, Any]="gelu" , lowerCamelCase_ :Union[str, Any]=0.1 , lowerCamelCase_ :Optional[Any]=0.1 , lowerCamelCase_ :Union[str, Any]=5_12 , lowerCamelCase_ :Any=2 , lowerCamelCase_ :Optional[Any]=0.0_2 , lowerCamelCase_ :Any=1E-12 , lowerCamelCase_ :Dict=0 , lowerCamelCase_ :Optional[Any]=0 , lowerCamelCase_ :Optional[int]=2 , lowerCamelCase_ :str=2_56 , lowerCamelCase_ :List[Any]=10_24 , lowerCamelCase_ :Union[str, Any]=2_16 , lowerCamelCase_ :Dict=10_01 , lowerCamelCase_ :Any=32 , lowerCamelCase_ :str=50 , lowerCamelCase_ :List[str]="absolute" , lowerCamelCase_ :List[str]=True , lowerCamelCase_ :int=None , **lowerCamelCase_ :Dict , ) -> List[Any]: '''simple docstring''' super().__init__( pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , **lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : str = vocab_size SCREAMING_SNAKE_CASE : Optional[int] = hidden_size SCREAMING_SNAKE_CASE : int = num_hidden_layers SCREAMING_SNAKE_CASE : List[str] = num_attention_heads SCREAMING_SNAKE_CASE : List[str] = hidden_act SCREAMING_SNAKE_CASE : int = intermediate_size SCREAMING_SNAKE_CASE : Optional[int] = hidden_dropout_prob SCREAMING_SNAKE_CASE : List[str] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = max_position_embeddings SCREAMING_SNAKE_CASE : Tuple = type_vocab_size SCREAMING_SNAKE_CASE : Any = initializer_range SCREAMING_SNAKE_CASE : int = layer_norm_eps SCREAMING_SNAKE_CASE : int = position_embedding_type SCREAMING_SNAKE_CASE : Tuple = use_cache SCREAMING_SNAKE_CASE : str = classifier_dropout # additional properties SCREAMING_SNAKE_CASE : Optional[Any] = max_depth SCREAMING_SNAKE_CASE : Dict = max_xpath_tag_unit_embeddings SCREAMING_SNAKE_CASE : Optional[int] = max_xpath_subs_unit_embeddings SCREAMING_SNAKE_CASE : Tuple = tag_pad_id SCREAMING_SNAKE_CASE : str = subs_pad_id SCREAMING_SNAKE_CASE : List[Any] = xpath_unit_hidden_size
698
1
"""simple docstring""" import argparse import json from pathlib import Path import torch import torchaudio from datasets import load_dataset from huggingface_hub import hf_hub_download from transformers import ASTConfig, ASTFeatureExtractor, ASTForAudioClassification from transformers.utils import logging logging.set_verbosity_info() lowerCamelCase__ : Union[str, Any] = logging.get_logger(__name__) def __A ( a_ : Optional[Any] )-> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = ASTConfig() if "10-10" in model_name: pass elif "speech-commands" in model_name: SCREAMING_SNAKE_CASE : Optional[int] = 1_28 elif "12-12" in model_name: SCREAMING_SNAKE_CASE : List[Any] = 12 SCREAMING_SNAKE_CASE : Optional[Any] = 12 elif "14-14" in model_name: SCREAMING_SNAKE_CASE : List[Any] = 14 SCREAMING_SNAKE_CASE : str = 14 elif "16-16" in model_name: SCREAMING_SNAKE_CASE : Tuple = 16 SCREAMING_SNAKE_CASE : str = 16 else: raise ValueError('''Model not supported''' ) SCREAMING_SNAKE_CASE : Any = '''huggingface/label-files''' if "speech-commands" in model_name: SCREAMING_SNAKE_CASE : Union[str, Any] = 35 SCREAMING_SNAKE_CASE : Optional[int] = '''speech-commands-v2-id2label.json''' else: SCREAMING_SNAKE_CASE : Any = 5_27 SCREAMING_SNAKE_CASE : Dict = '''audioset-id2label.json''' SCREAMING_SNAKE_CASE : int = json.load(open(hf_hub_download(a_ , a_ , repo_type='''dataset''' ) , '''r''' ) ) SCREAMING_SNAKE_CASE : str = {int(a_ ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE : Dict = idalabel SCREAMING_SNAKE_CASE : Optional[int] = {v: k for k, v in idalabel.items()} return config def __A ( a_ : Union[str, Any] )-> List[Any]: '''simple docstring''' if "module.v" in name: SCREAMING_SNAKE_CASE : Union[str, Any] = name.replace('''module.v''' , '''audio_spectrogram_transformer''' ) if "cls_token" in name: SCREAMING_SNAKE_CASE : Union[str, Any] = name.replace('''cls_token''' , '''embeddings.cls_token''' ) if "dist_token" in name: SCREAMING_SNAKE_CASE : Any = name.replace('''dist_token''' , '''embeddings.distillation_token''' ) if "pos_embed" in name: SCREAMING_SNAKE_CASE : Tuple = name.replace('''pos_embed''' , '''embeddings.position_embeddings''' ) if "patch_embed.proj" in name: SCREAMING_SNAKE_CASE : int = name.replace('''patch_embed.proj''' , '''embeddings.patch_embeddings.projection''' ) # transformer blocks if "blocks" in name: SCREAMING_SNAKE_CASE : Optional[int] = name.replace('''blocks''' , '''encoder.layer''' ) if "attn.proj" in name: SCREAMING_SNAKE_CASE : Optional[Any] = name.replace('''attn.proj''' , '''attention.output.dense''' ) if "attn" in name: SCREAMING_SNAKE_CASE : List[Any] = name.replace('''attn''' , '''attention.self''' ) if "norm1" in name: SCREAMING_SNAKE_CASE : str = name.replace('''norm1''' , '''layernorm_before''' ) if "norm2" in name: SCREAMING_SNAKE_CASE : Tuple = name.replace('''norm2''' , '''layernorm_after''' ) if "mlp.fc1" in name: SCREAMING_SNAKE_CASE : str = name.replace('''mlp.fc1''' , '''intermediate.dense''' ) if "mlp.fc2" in name: SCREAMING_SNAKE_CASE : List[str] = name.replace('''mlp.fc2''' , '''output.dense''' ) # final layernorm if "audio_spectrogram_transformer.norm" in name: SCREAMING_SNAKE_CASE : str = name.replace('''audio_spectrogram_transformer.norm''' , '''audio_spectrogram_transformer.layernorm''' ) # classifier head if "module.mlp_head.0" in name: SCREAMING_SNAKE_CASE : Tuple = name.replace('''module.mlp_head.0''' , '''classifier.layernorm''' ) if "module.mlp_head.1" in name: SCREAMING_SNAKE_CASE : List[Any] = name.replace('''module.mlp_head.1''' , '''classifier.dense''' ) return name def __A ( a_ : List[str] , a_ : Optional[Any] )-> List[str]: '''simple docstring''' for key in orig_state_dict.copy().keys(): SCREAMING_SNAKE_CASE : Any = orig_state_dict.pop(a_ ) if "qkv" in key: SCREAMING_SNAKE_CASE : Tuple = key.split('''.''' ) SCREAMING_SNAKE_CASE : int = int(key_split[3] ) SCREAMING_SNAKE_CASE : int = config.hidden_size if "weight" in key: SCREAMING_SNAKE_CASE : Optional[int] = val[:dim, :] SCREAMING_SNAKE_CASE : Dict = val[dim : dim * 2, :] SCREAMING_SNAKE_CASE : int = val[-dim:, :] else: SCREAMING_SNAKE_CASE : int = val[:dim] SCREAMING_SNAKE_CASE : List[str] = val[dim : dim * 2] SCREAMING_SNAKE_CASE : Dict = val[-dim:] else: SCREAMING_SNAKE_CASE : Optional[Any] = val return orig_state_dict def __A ( a_ : int )-> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = [ '''module.v.head.weight''', '''module.v.head.bias''', '''module.v.head_dist.weight''', '''module.v.head_dist.bias''', ] for k in ignore_keys: state_dict.pop(a_ , a_ ) @torch.no_grad() def __A ( a_ : Dict , a_ : Dict , a_ : Any=False )-> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = get_audio_spectrogram_transformer_config(a_ ) SCREAMING_SNAKE_CASE : Dict = { '''ast-finetuned-audioset-10-10-0.4593''': ( '''https://www.dropbox.com/s/ca0b1v2nlxzyeb4/audioset_10_10_0.4593.pth?dl=1''' ), '''ast-finetuned-audioset-10-10-0.450''': ( '''https://www.dropbox.com/s/1tv0hovue1bxupk/audioset_10_10_0.4495.pth?dl=1''' ), '''ast-finetuned-audioset-10-10-0.448''': ( '''https://www.dropbox.com/s/6u5sikl4b9wo4u5/audioset_10_10_0.4483.pth?dl=1''' ), '''ast-finetuned-audioset-10-10-0.448-v2''': ( '''https://www.dropbox.com/s/kt6i0v9fvfm1mbq/audioset_10_10_0.4475.pth?dl=1''' ), '''ast-finetuned-audioset-12-12-0.447''': ( '''https://www.dropbox.com/s/snfhx3tizr4nuc8/audioset_12_12_0.4467.pth?dl=1''' ), '''ast-finetuned-audioset-14-14-0.443''': ( '''https://www.dropbox.com/s/z18s6pemtnxm4k7/audioset_14_14_0.4431.pth?dl=1''' ), '''ast-finetuned-audioset-16-16-0.442''': ( '''https://www.dropbox.com/s/mdsa4t1xmcimia6/audioset_16_16_0.4422.pth?dl=1''' ), '''ast-finetuned-speech-commands-v2''': ( '''https://www.dropbox.com/s/q0tbqpwv44pquwy/speechcommands_10_10_0.9812.pth?dl=1''' ), } # load original state_dict SCREAMING_SNAKE_CASE : List[str] = model_name_to_url[model_name] SCREAMING_SNAKE_CASE : str = torch.hub.load_state_dict_from_url(a_ , map_location='''cpu''' ) # remove some keys remove_keys(a_ ) # rename some keys SCREAMING_SNAKE_CASE : List[Any] = convert_state_dict(a_ , a_ ) # load 🤗 model SCREAMING_SNAKE_CASE : Dict = ASTForAudioClassification(a_ ) model.eval() model.load_state_dict(a_ ) # verify outputs on dummy input # source: https://github.com/YuanGongND/ast/blob/79e873b8a54d0a3b330dd522584ff2b9926cd581/src/run.py#L62 SCREAMING_SNAKE_CASE : Dict = -4.267_7393 if '''speech-commands''' not in model_name else -6.84_5978 SCREAMING_SNAKE_CASE : Tuple = 4.568_9974 if '''speech-commands''' not in model_name else 5.565_4526 SCREAMING_SNAKE_CASE : int = 10_24 if '''speech-commands''' not in model_name else 1_28 SCREAMING_SNAKE_CASE : List[Any] = ASTFeatureExtractor(mean=a_ , std=a_ , max_length=a_ ) if "speech-commands" in model_name: SCREAMING_SNAKE_CASE : Any = load_dataset('''speech_commands''' , '''v0.02''' , split='''validation''' ) SCREAMING_SNAKE_CASE : List[str] = dataset[0]['''audio''']['''array'''] else: SCREAMING_SNAKE_CASE : int = hf_hub_download( repo_id='''nielsr/audio-spectogram-transformer-checkpoint''' , filename='''sample_audio.flac''' , repo_type='''dataset''' , ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Union[str, Any] = torchaudio.load(a_ ) SCREAMING_SNAKE_CASE : int = waveform.squeeze().numpy() SCREAMING_SNAKE_CASE : str = feature_extractor(a_ , sampling_rate=1_60_00 , return_tensors='''pt''' ) # forward pass SCREAMING_SNAKE_CASE : str = model(**a_ ) SCREAMING_SNAKE_CASE : str = outputs.logits if model_name == "ast-finetuned-audioset-10-10-0.4593": SCREAMING_SNAKE_CASE : Any = torch.tensor([-0.8760, -7.0042, -8.6602] ) elif model_name == "ast-finetuned-audioset-10-10-0.450": SCREAMING_SNAKE_CASE : Optional[int] = torch.tensor([-1.1986, -7.0903, -8.2718] ) elif model_name == "ast-finetuned-audioset-10-10-0.448": SCREAMING_SNAKE_CASE : List[Any] = torch.tensor([-2.6128, -8.0080, -9.4344] ) elif model_name == "ast-finetuned-audioset-10-10-0.448-v2": SCREAMING_SNAKE_CASE : List[str] = torch.tensor([-1.5080, -7.4534, -8.8917] ) elif model_name == "ast-finetuned-audioset-12-12-0.447": SCREAMING_SNAKE_CASE : Optional[int] = torch.tensor([-0.5050, -6.5833, -8.0843] ) elif model_name == "ast-finetuned-audioset-14-14-0.443": SCREAMING_SNAKE_CASE : List[str] = torch.tensor([-0.3826, -7.0336, -8.2413] ) elif model_name == "ast-finetuned-audioset-16-16-0.442": SCREAMING_SNAKE_CASE : List[Any] = torch.tensor([-1.2113, -6.9101, -8.3470] ) elif model_name == "ast-finetuned-speech-commands-v2": SCREAMING_SNAKE_CASE : Tuple = torch.tensor([6.1589, -8.0566, -8.7984] ) else: raise ValueError('''Unknown model name''' ) if not torch.allclose(logits[0, :3] , a_ , atol=1E-4 ): raise ValueError('''Logits don\'t match''' ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: Path(a_ ).mkdir(exist_ok=a_ ) print(F"Saving model {model_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(a_ ) print(F"Saving feature extractor to {pytorch_dump_folder_path}" ) feature_extractor.save_pretrained(a_ ) if push_to_hub: print('''Pushing model and feature extractor to the hub...''' ) model.push_to_hub(F"MIT/{model_name}" ) feature_extractor.push_to_hub(F"MIT/{model_name}" ) if __name__ == "__main__": lowerCamelCase__ : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default="ast-finetuned-audioset-10-10-0.4593", type=str, help="Name of the Audio Spectrogram Transformer 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." ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether or not to push the converted model to the 🤗 hub." ) lowerCamelCase__ : Any = parser.parse_args() convert_audio_spectrogram_transformer_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
698
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCamelCase__ : Optional[Any] = logging.get_logger(__name__) lowerCamelCase__ : Union[str, Any] = { "microsoft/resnet-50": "https://huggingface.co/microsoft/resnet-50/blob/main/config.json", } class lowercase__( _UpperCAmelCase , _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """resnet""" UpperCamelCase = ["""basic""", """bottleneck"""] def __init__( self :Optional[int] , lowerCamelCase_ :Tuple=3 , lowerCamelCase_ :Tuple=64 , lowerCamelCase_ :Union[str, Any]=[2_56, 5_12, 10_24, 20_48] , lowerCamelCase_ :int=[3, 4, 6, 3] , lowerCamelCase_ :Any="bottleneck" , lowerCamelCase_ :Optional[int]="relu" , lowerCamelCase_ :Optional[int]=False , lowerCamelCase_ :Any=None , lowerCamelCase_ :Optional[int]=None , **lowerCamelCase_ :Optional[int] , ) -> Tuple: '''simple docstring''' super().__init__(**lowerCamelCase_ ) if layer_type not in self.layer_types: raise ValueError(f"layer_type={layer_type} is not one of {','.join(self.layer_types )}" ) SCREAMING_SNAKE_CASE : Tuple = num_channels SCREAMING_SNAKE_CASE : Union[str, Any] = embedding_size SCREAMING_SNAKE_CASE : List[str] = hidden_sizes SCREAMING_SNAKE_CASE : Optional[Any] = depths SCREAMING_SNAKE_CASE : List[Any] = layer_type SCREAMING_SNAKE_CASE : str = hidden_act SCREAMING_SNAKE_CASE : Optional[Any] = downsample_in_first_stage SCREAMING_SNAKE_CASE : int = ['''stem'''] + [f"stage{idx}" for idx in range(1 , len(lowerCamelCase_ ) + 1 )] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = get_aligned_output_features_output_indices( out_features=lowerCamelCase_ , out_indices=lowerCamelCase_ , stage_names=self.stage_names ) class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = version.parse("""1.11""" ) @property def __lowerCAmelCase ( self :Optional[Any] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def __lowerCAmelCase ( self :str ) -> float: '''simple docstring''' return 1E-3
698
1
"""simple docstring""" def __A ( a_ : list[int] , a_ : int )-> bool: '''simple docstring''' SCREAMING_SNAKE_CASE : int = len(a_ ) SCREAMING_SNAKE_CASE : str = [[False] * (required_sum + 1) for _ in range(arr_len + 1 )] # for each arr value, a sum of zero(0) can be formed by not taking any element # hence True/1 for i in range(arr_len + 1 ): SCREAMING_SNAKE_CASE : Tuple = True # sum is not zero and set is empty then false for i in range(1 , required_sum + 1 ): SCREAMING_SNAKE_CASE : Optional[Any] = False for i in range(1 , arr_len + 1 ): for j in range(1 , required_sum + 1 ): if arr[i - 1] > j: SCREAMING_SNAKE_CASE : Dict = subset[i - 1][j] if arr[i - 1] <= j: SCREAMING_SNAKE_CASE : Any = subset[i - 1][j] or subset[i - 1][j - arr[i - 1]] return subset[arr_len][required_sum] if __name__ == "__main__": import doctest doctest.testmod()
698
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : List[Any] = { "uw-madison/mra-base-512-4": "https://huggingface.co/uw-madison/mra-base-512-4/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """mra""" def __init__( self :int , lowerCamelCase_ :Optional[int]=5_02_65 , lowerCamelCase_ :List[str]=7_68 , lowerCamelCase_ :List[str]=12 , lowerCamelCase_ :Optional[Any]=12 , lowerCamelCase_ :int=30_72 , lowerCamelCase_ :Tuple="gelu" , lowerCamelCase_ :List[Any]=0.1 , lowerCamelCase_ :str=0.1 , lowerCamelCase_ :str=5_12 , lowerCamelCase_ :List[str]=1 , lowerCamelCase_ :int=0.0_2 , lowerCamelCase_ :int=1E-5 , lowerCamelCase_ :List[Any]="absolute" , lowerCamelCase_ :str=4 , lowerCamelCase_ :List[str]="full" , lowerCamelCase_ :List[Any]=0 , lowerCamelCase_ :Optional[Any]=0 , lowerCamelCase_ :Union[str, Any]=1 , lowerCamelCase_ :List[str]=0 , lowerCamelCase_ :List[Any]=2 , **lowerCamelCase_ :str , ) -> Dict: '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = vocab_size SCREAMING_SNAKE_CASE : Tuple = max_position_embeddings SCREAMING_SNAKE_CASE : List[Any] = hidden_size SCREAMING_SNAKE_CASE : Dict = num_hidden_layers SCREAMING_SNAKE_CASE : Tuple = num_attention_heads SCREAMING_SNAKE_CASE : Any = intermediate_size SCREAMING_SNAKE_CASE : Any = hidden_act SCREAMING_SNAKE_CASE : Tuple = hidden_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : str = initializer_range SCREAMING_SNAKE_CASE : Tuple = type_vocab_size SCREAMING_SNAKE_CASE : Union[str, Any] = layer_norm_eps SCREAMING_SNAKE_CASE : str = position_embedding_type SCREAMING_SNAKE_CASE : List[str] = block_per_row SCREAMING_SNAKE_CASE : Optional[int] = approx_mode SCREAMING_SNAKE_CASE : List[Any] = initial_prior_first_n_blocks SCREAMING_SNAKE_CASE : Union[str, Any] = initial_prior_diagonal_n_blocks
698
1
"""simple docstring""" # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os from accelerate.test_utils import execute_subprocess_async def __A ( a_ : int=None )-> Tuple: '''simple docstring''' if subparsers is not None: SCREAMING_SNAKE_CASE : List[str] = subparsers.add_parser('''test''' ) else: SCREAMING_SNAKE_CASE : int = argparse.ArgumentParser('''Accelerate test command''' ) parser.add_argument( '''--config_file''' , default=a_ , help=( '''The path to use to store the config file. Will default to a file named default_config.yaml in the cache ''' '''location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have ''' '''such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed ''' '''with \'huggingface\'.''' ) , ) if subparsers is not None: parser.set_defaults(func=a_ ) return parser def __A ( a_ : Any )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = os.path.sep.join(__file__.split(os.path.sep )[:-2] + ['''test_utils''', '''scripts''', '''test_script.py'''] ) if args.config_file is None: SCREAMING_SNAKE_CASE : Tuple = script_name else: SCREAMING_SNAKE_CASE : Optional[Any] = F"--config_file={args.config_file} {script_name}" SCREAMING_SNAKE_CASE : str = ['''accelerate-launch'''] + test_args.split() SCREAMING_SNAKE_CASE : List[str] = execute_subprocess_async(a_ , env=os.environ.copy() ) if result.returncode == 0: print('''Test is a success! You are ready for your distributed training!''' ) def __A ( )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : str = test_command_parser() SCREAMING_SNAKE_CASE : Optional[Any] = parser.parse_args() test_command(a_ ) if __name__ == "__main__": main()
698
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : str = logging.get_logger(__name__) lowerCamelCase__ : List[str] = { "facebook/nllb-moe-54B": "https://huggingface.co/facebook/nllb-moe-54b/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """nllb-moe""" UpperCamelCase = ["""past_key_values"""] UpperCamelCase = {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""} def __init__( self :List[str] , lowerCamelCase_ :Optional[int]=12_81_12 , lowerCamelCase_ :str=10_24 , lowerCamelCase_ :Any=12 , lowerCamelCase_ :Optional[int]=40_96 , lowerCamelCase_ :int=16 , lowerCamelCase_ :List[str]=12 , lowerCamelCase_ :Optional[int]=40_96 , lowerCamelCase_ :int=16 , lowerCamelCase_ :Union[str, Any]=0.0_5 , lowerCamelCase_ :Optional[int]=0.0_5 , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :Optional[Any]=True , lowerCamelCase_ :Tuple="relu" , lowerCamelCase_ :str=10_24 , lowerCamelCase_ :str=0.1 , lowerCamelCase_ :Optional[int]=0.1 , lowerCamelCase_ :List[str]=0.0 , lowerCamelCase_ :Optional[Any]=0.0_2 , lowerCamelCase_ :Optional[int]=2 , lowerCamelCase_ :Dict=True , lowerCamelCase_ :Any=False , lowerCamelCase_ :Optional[Any]="float32" , lowerCamelCase_ :Optional[Any]=False , lowerCamelCase_ :List[Any]=1_28 , lowerCamelCase_ :Any=64 , lowerCamelCase_ :Optional[int]=4 , lowerCamelCase_ :List[str]=4 , lowerCamelCase_ :Union[str, Any]=0.0_0_1 , lowerCamelCase_ :Optional[int]=0.0_0_1 , lowerCamelCase_ :List[str]="all" , lowerCamelCase_ :Optional[int]=False , lowerCamelCase_ :Any=False , lowerCamelCase_ :Tuple=1.0 , lowerCamelCase_ :Union[str, Any]=0.2 , lowerCamelCase_ :List[str]=1 , lowerCamelCase_ :Optional[int]=0 , lowerCamelCase_ :int=2 , lowerCamelCase_ :List[str]=False , **lowerCamelCase_ :int , ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : int = vocab_size SCREAMING_SNAKE_CASE : str = max_position_embeddings SCREAMING_SNAKE_CASE : str = d_model SCREAMING_SNAKE_CASE : Optional[int] = encoder_ffn_dim SCREAMING_SNAKE_CASE : Any = encoder_layers SCREAMING_SNAKE_CASE : Any = encoder_attention_heads SCREAMING_SNAKE_CASE : List[Any] = decoder_ffn_dim SCREAMING_SNAKE_CASE : str = decoder_layers SCREAMING_SNAKE_CASE : List[Any] = decoder_attention_heads SCREAMING_SNAKE_CASE : List[Any] = dropout SCREAMING_SNAKE_CASE : List[str] = attention_dropout SCREAMING_SNAKE_CASE : str = activation_dropout SCREAMING_SNAKE_CASE : Any = activation_function SCREAMING_SNAKE_CASE : Tuple = init_std SCREAMING_SNAKE_CASE : str = encoder_layerdrop SCREAMING_SNAKE_CASE : Union[str, Any] = decoder_layerdrop SCREAMING_SNAKE_CASE : List[Any] = use_cache SCREAMING_SNAKE_CASE : Optional[int] = encoder_layers SCREAMING_SNAKE_CASE : List[str] = scale_embedding # scale factor will be sqrt(d_model) if True SCREAMING_SNAKE_CASE : int = router_z_loss_coef SCREAMING_SNAKE_CASE : Any = router_aux_loss_coef SCREAMING_SNAKE_CASE : str = decoder_sparse_step SCREAMING_SNAKE_CASE : str = encoder_sparse_step SCREAMING_SNAKE_CASE : List[str] = num_experts SCREAMING_SNAKE_CASE : Union[str, Any] = expert_capacity SCREAMING_SNAKE_CASE : Tuple = router_bias if router_dtype not in ["float32", "float16", "bfloat16"]: raise ValueError(f"`router_dtype` must be one of 'float32', 'float16' or 'bfloat16', got {router_dtype}" ) SCREAMING_SNAKE_CASE : Union[str, Any] = router_dtype SCREAMING_SNAKE_CASE : Union[str, Any] = router_ignore_padding_tokens SCREAMING_SNAKE_CASE : int = batch_prioritized_routing SCREAMING_SNAKE_CASE : Optional[int] = second_expert_policy SCREAMING_SNAKE_CASE : Union[str, Any] = normalize_router_prob_before_dropping SCREAMING_SNAKE_CASE : Any = moe_eval_capacity_token_fraction SCREAMING_SNAKE_CASE : Optional[Any] = moe_token_dropout SCREAMING_SNAKE_CASE : Tuple = output_router_logits super().__init__( pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , is_encoder_decoder=lowerCamelCase_ , decoder_start_token_id=lowerCamelCase_ , **lowerCamelCase_ , )
698
1
"""simple docstring""" from typing import List, Union import numpy as np from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): import torch from ..models.auto.modeling_auto import MODEL_FOR_DEPTH_ESTIMATION_MAPPING lowerCamelCase__ : Union[str, Any] = logging.get_logger(__name__) @add_end_docstrings(_UpperCAmelCase ) class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __init__( self :Optional[int] , *lowerCamelCase_ :Any , **lowerCamelCase_ :Dict ) -> Tuple: '''simple docstring''' super().__init__(*lowerCamelCase_ , **lowerCamelCase_ ) requires_backends(self , '''vision''' ) self.check_model_type(lowerCamelCase_ ) def __call__( self :List[Any] , lowerCamelCase_ :Union[str, List[str], "Image.Image", List["Image.Image"]] , **lowerCamelCase_ :Tuple ) -> List[Any]: '''simple docstring''' return super().__call__(lowerCamelCase_ , **lowerCamelCase_ ) def __lowerCAmelCase ( self :int , **lowerCamelCase_ :List[Any] ) -> Optional[Any]: '''simple docstring''' return {}, {}, {} def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :Dict ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = load_image(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = image.size SCREAMING_SNAKE_CASE : Optional[int] = self.image_processor(images=lowerCamelCase_ , return_tensors=self.framework ) return model_inputs def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.model(**lowerCamelCase_ ) return model_outputs def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :Any ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = model_outputs.predicted_depth SCREAMING_SNAKE_CASE : Tuple = torch.nn.functional.interpolate( predicted_depth.unsqueeze(1 ) , size=self.image_size[::-1] , mode='''bicubic''' , align_corners=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = prediction.squeeze().cpu().numpy() SCREAMING_SNAKE_CASE : Union[str, Any] = (output * 2_55 / np.max(lowerCamelCase_ )).astype('''uint8''' ) SCREAMING_SNAKE_CASE : Dict = Image.fromarray(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = {} SCREAMING_SNAKE_CASE : str = predicted_depth SCREAMING_SNAKE_CASE : Tuple = depth return output_dict
698
"""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 lowerCamelCase__ : Union[str, Any] = "CompVis/stable-diffusion-v1-1" lowerCamelCase__ : Optional[Any] = "CompVis/stable-diffusion-v1-2" lowerCamelCase__ : Dict = "CompVis/stable-diffusion-v1-3" lowerCamelCase__ : List[str] = "CompVis/stable-diffusion-v1-4" class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __init__( self :Any , lowerCamelCase_ :AutoencoderKL , lowerCamelCase_ :CLIPTextModel , lowerCamelCase_ :CLIPTokenizer , lowerCamelCase_ :UNetaDConditionModel , lowerCamelCase_ :Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , lowerCamelCase_ :StableDiffusionSafetyChecker , lowerCamelCase_ :CLIPImageProcessor , lowerCamelCase_ :bool = True , ) -> List[str]: '''simple docstring''' super()._init_() SCREAMING_SNAKE_CASE : Tuple = StableDiffusionPipeline.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = StableDiffusionPipeline.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = StableDiffusionPipeline.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = StableDiffusionPipeline( vae=lowerCamelCase_ , text_encoder=lowerCamelCase_ , tokenizer=lowerCamelCase_ , unet=lowerCamelCase_ , scheduler=lowerCamelCase_ , safety_checker=lowerCamelCase_ , feature_extractor=lowerCamelCase_ , requires_safety_checker=lowerCamelCase_ , ) self.register_modules(pipelinea=self.pipea , pipelinea=self.pipea , pipelinea=self.pipea , pipelinea=self.pipea ) @property def __lowerCAmelCase ( self :Dict ) -> Dict[str, Any]: '''simple docstring''' return {k: getattr(self , lowerCamelCase_ ) for k in self.config.keys() if not k.startswith('''_''' )} def __lowerCAmelCase ( self :int , lowerCamelCase_ :Optional[Union[str, int]] = "auto" ) -> Tuple: '''simple docstring''' if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory SCREAMING_SNAKE_CASE : str = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(lowerCamelCase_ ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Dict: '''simple docstring''' self.enable_attention_slicing(lowerCamelCase_ ) @torch.no_grad() def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :List[str] , ) -> Tuple: '''simple docstring''' return self.pipea( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) @torch.no_grad() def __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :Tuple , ) -> Optional[Any]: '''simple docstring''' return self.pipea( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) @torch.no_grad() def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :Dict , ) -> List[str]: '''simple docstring''' return self.pipea( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) @torch.no_grad() def __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :List[Any] , ) -> Optional[Any]: '''simple docstring''' return self.pipea( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) @torch.no_grad() def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 50 , lowerCamelCase_ :float = 7.5 , lowerCamelCase_ :Optional[Union[str, List[str]]] = None , lowerCamelCase_ :Optional[int] = 1 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :Optional[torch.Generator] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , **lowerCamelCase_ :Optional[Any] , ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = '''cuda''' if torch.cuda.is_available() else '''cpu''' self.to(lowerCamelCase_ ) # 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 SCREAMING_SNAKE_CASE : str = self.textaimg_sda_a( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) # Get first result from Stable Diffusion Checkpoint v1.2 SCREAMING_SNAKE_CASE : Optional[Any] = self.textaimg_sda_a( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) # Get first result from Stable Diffusion Checkpoint v1.3 SCREAMING_SNAKE_CASE : Tuple = self.textaimg_sda_a( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) # Get first result from Stable Diffusion Checkpoint v1.4 SCREAMING_SNAKE_CASE : Union[str, Any] = self.textaimg_sda_a( prompt=lowerCamelCase_ , height=lowerCamelCase_ , width=lowerCamelCase_ , num_inference_steps=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , negative_prompt=lowerCamelCase_ , num_images_per_prompt=lowerCamelCase_ , eta=lowerCamelCase_ , generator=lowerCamelCase_ , latents=lowerCamelCase_ , output_type=lowerCamelCase_ , return_dict=lowerCamelCase_ , callback=lowerCamelCase_ , callback_steps=lowerCamelCase_ , **lowerCamelCase_ , ) # 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]] )
698
1
"""simple docstring""" import argparse import os import re import packaging.version lowerCamelCase__ : Any = "examples/" lowerCamelCase__ : Optional[Any] = { "examples": (re.compile(r"^check_min_version\(\"[^\"]+\"\)\s*$", re.MULTILINE), "check_min_version(\"VERSION\")\n"), "init": (re.compile(r"^__version__\s+=\s+\"([^\"]+)\"\s*$", re.MULTILINE), "__version__ = \"VERSION\"\n"), "setup": (re.compile(r"^(\s*)version\s*=\s*\"[^\"]+\",", re.MULTILINE), r"\1version=\"VERSION\","), "doc": (re.compile(r"^(\s*)release\s*=\s*\"[^\"]+\"$", re.MULTILINE), "release = \"VERSION\"\n"), } lowerCamelCase__ : Any = { "init": "src/transformers/__init__.py", "setup": "setup.py", } lowerCamelCase__ : Optional[int] = "README.md" def __A ( a_ : List[str] , a_ : int , a_ : Optional[int] )-> Optional[Any]: '''simple docstring''' with open(a_ , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: SCREAMING_SNAKE_CASE : Optional[Any] = f.read() SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = REPLACE_PATTERNS[pattern] SCREAMING_SNAKE_CASE : Optional[Any] = replace.replace('''VERSION''' , a_ ) SCREAMING_SNAKE_CASE : Any = re_pattern.sub(a_ , a_ ) with open(a_ , '''w''' , encoding='''utf-8''' , newline='''\n''' ) as f: f.write(a_ ) def __A ( a_ : Union[str, Any] )-> Optional[int]: '''simple docstring''' for folder, directories, fnames in os.walk(a_ ): # Removing some of the folders with non-actively maintained examples from the walk if "research_projects" in directories: directories.remove('''research_projects''' ) if "legacy" in directories: directories.remove('''legacy''' ) for fname in fnames: if fname.endswith('''.py''' ): update_version_in_file(os.path.join(a_ , a_ ) , a_ , pattern='''examples''' ) def __A ( a_ : str , a_ : Optional[int]=False )-> Dict: '''simple docstring''' for pattern, fname in REPLACE_FILES.items(): update_version_in_file(a_ , a_ , a_ ) if not patch: update_version_in_examples(a_ ) def __A ( )-> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = '''🤗 Transformers currently provides the following architectures''' SCREAMING_SNAKE_CASE : str = '''1. Want to contribute a new model?''' with open(a_ , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: SCREAMING_SNAKE_CASE : List[Any] = f.readlines() # Find the start of the list. SCREAMING_SNAKE_CASE : Any = 0 while not lines[start_index].startswith(_start_prompt ): start_index += 1 start_index += 1 SCREAMING_SNAKE_CASE : Union[str, Any] = start_index # Update the lines in the model list. while not lines[index].startswith(_end_prompt ): if lines[index].startswith('''1.''' ): SCREAMING_SNAKE_CASE : Any = lines[index].replace( '''https://huggingface.co/docs/transformers/main/model_doc''' , '''https://huggingface.co/docs/transformers/model_doc''' , ) index += 1 with open(a_ , '''w''' , encoding='''utf-8''' , newline='''\n''' ) as f: f.writelines(a_ ) def __A ( )-> Dict: '''simple docstring''' with open(REPLACE_FILES['''init'''] , '''r''' ) as f: SCREAMING_SNAKE_CASE : Dict = f.read() SCREAMING_SNAKE_CASE : List[str] = REPLACE_PATTERNS['''init'''][0].search(a_ ).groups()[0] return packaging.version.parse(a_ ) def __A ( a_ : Tuple=False )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = get_version() if patch and default_version.is_devrelease: raise ValueError('''Can\'t create a patch version from the dev branch, checkout a released version!''' ) if default_version.is_devrelease: SCREAMING_SNAKE_CASE : Any = default_version.base_version elif patch: SCREAMING_SNAKE_CASE : Tuple = F"{default_version.major}.{default_version.minor}.{default_version.micro + 1}" else: SCREAMING_SNAKE_CASE : List[str] = F"{default_version.major}.{default_version.minor + 1}.0" # Now let's ask nicely if that's the right one. SCREAMING_SNAKE_CASE : List[Any] = input(F"Which version are you releasing? [{default_version}]" ) if len(a_ ) == 0: SCREAMING_SNAKE_CASE : Optional[int] = default_version print(F"Updating version to {version}." ) global_version_update(a_ , patch=a_ ) if not patch: print('''Cleaning main README, don\'t forget to run `make fix-copies`.''' ) clean_main_ref_in_model_list() def __A ( )-> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = get_version() SCREAMING_SNAKE_CASE : Optional[int] = F"{current_version.major}.{current_version.minor + 1}.0.dev0" SCREAMING_SNAKE_CASE : str = current_version.base_version # Check with the user we got that right. SCREAMING_SNAKE_CASE : Dict = input(F"Which version are we developing now? [{dev_version}]" ) if len(a_ ) == 0: SCREAMING_SNAKE_CASE : Any = dev_version print(F"Updating version to {version}." ) global_version_update(a_ ) print('''Cleaning main README, don\'t forget to run `make fix-copies`.''' ) clean_main_ref_in_model_list() if __name__ == "__main__": lowerCamelCase__ : List[Any] = argparse.ArgumentParser() parser.add_argument("--post_release", action="store_true", help="Whether this is pre or post release.") parser.add_argument("--patch", action="store_true", help="Whether or not this is a patch release.") lowerCamelCase__ : Any = parser.parse_args() if not args.post_release: pre_release_work(patch=args.patch) elif args.patch: print("Nothing to do after a patch :-)") else: post_release_work()
698
"""simple docstring""" def __A ( a_ : list , a_ : int = 0 )-> list: '''simple docstring''' SCREAMING_SNAKE_CASE : int = length or len(a_ ) SCREAMING_SNAKE_CASE : List[Any] = False for i in range(length - 1 ): if list_data[i] > list_data[i + 1]: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[Any] = list_data[i + 1], list_data[i] SCREAMING_SNAKE_CASE : Optional[Any] = True return list_data if not swapped else bubble_sort(a_ , length - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
698
1