code
stringlengths
81
54k
code_codestyle
int64
0
721
style_context
stringlengths
91
41.9k
style_context_codestyle
int64
0
699
label
int64
0
1
import json from typing import List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_mvp import MvpTokenizer SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''} # See all MVP models at https://huggingface.co/models?filter=mvp SCREAMING_SNAKE_CASE__ = { '''vocab_file''': { '''RUCAIBox/mvp''': '''https://huggingface.co/RUCAIBox/mvp/resolve/main/vocab.json''', }, '''added_tokens.json''': { '''RUCAIBox/mvp''': '''https://huggingface.co/RUCAIBox/mvp/resolve/main/added_tokens.json''', }, '''merges_file''': { '''RUCAIBox/mvp''': '''https://huggingface.co/RUCAIBox/mvp/resolve/main/merges.txt''', }, '''tokenizer_file''': { '''RUCAIBox/mvp''': '''https://huggingface.co/RUCAIBox/mvp/resolve/main/tokenizer.json''', }, } SCREAMING_SNAKE_CASE__ = { '''RUCAIBox/mvp''': 1_0_2_4, } class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Optional[int] = VOCAB_FILES_NAMES A__ : List[str] = PRETRAINED_VOCAB_FILES_MAP A__ : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A__ : Union[str, Any] = ["input_ids", "attention_mask"] A__ : List[str] = MvpTokenizer def __init__( self : Tuple , _snake_case : int=None , _snake_case : Union[str, Any]=None , _snake_case : str=None , _snake_case : str="replace" , _snake_case : List[Any]="<s>" , _snake_case : Optional[int]="</s>" , _snake_case : List[Any]="</s>" , _snake_case : Optional[Any]="<s>" , _snake_case : int="<unk>" , _snake_case : int="<pad>" , _snake_case : Union[str, Any]="<mask>" , _snake_case : List[Any]=False , _snake_case : int=True , **_snake_case : Optional[int] , ): """simple docstring""" super().__init__( _snake_case , _snake_case , tokenizer_file=_snake_case , errors=_snake_case , bos_token=_snake_case , eos_token=_snake_case , sep_token=_snake_case , cls_token=_snake_case , unk_token=_snake_case , pad_token=_snake_case , mask_token=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case , **_snake_case , ) A__ = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('add_prefix_space' , _snake_case ) != add_prefix_space: A__ = getattr(_snake_case , pre_tok_state.pop('type' ) ) A__ = add_prefix_space A__ = pre_tok_class(**_snake_case ) A__ = add_prefix_space # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__` A__ = 'post_processor' A__ = getattr(self.backend_tokenizer , _snake_case , _snake_case ) if tokenizer_component_instance: A__ = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: A__ = tuple(state['sep'] ) if "cls" in state: A__ = tuple(state['cls'] ) A__ = False if state.get('add_prefix_space' , _snake_case ) != add_prefix_space: A__ = add_prefix_space A__ = True if state.get('trim_offsets' , _snake_case ) != trim_offsets: A__ = trim_offsets A__ = True if changes_to_apply: A__ = getattr(_snake_case , state.pop('type' ) ) A__ = component_class(**_snake_case ) setattr(self.backend_tokenizer , _snake_case , _snake_case ) @property def _a ( self : Union[str, Any] ): """simple docstring""" if self._mask_token is None: if self.verbose: logger.error('Using mask_token, but it is not set yet.' ) return None return str(self._mask_token ) @mask_token.setter def _a ( self : str , _snake_case : Union[str, Any] ): """simple docstring""" A__ = AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case ) if isinstance(_snake_case , _snake_case ) else value A__ = value def _a ( self : Optional[Any] , *_snake_case : Union[str, Any] , **_snake_case : Any ): """simple docstring""" A__ = kwargs.get('is_split_into_words' , _snake_case ) if is_split_into_words and not self.add_prefix_space: raise ValueError( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' 'to use it with pretokenized inputs.' ) return super()._batch_encode_plus(*_snake_case , **_snake_case ) def _a ( self : Optional[int] , *_snake_case : Union[str, Any] , **_snake_case : Dict ): """simple docstring""" A__ = kwargs.get('is_split_into_words' , _snake_case ) if is_split_into_words and not self.add_prefix_space: raise ValueError( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' 'to use it with pretokenized inputs.' ) return super()._encode_plus(*_snake_case , **_snake_case ) def _a ( self : Tuple , _snake_case : str , _snake_case : Optional[str] = None ): """simple docstring""" A__ = self._tokenizer.model.save(_snake_case , name=_snake_case ) return tuple(_snake_case ) def _a ( self : Dict , _snake_case : List[str] , _snake_case : Optional[int]=None ): """simple docstring""" A__ = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def _a ( self : List[str] , _snake_case : List[int] , _snake_case : Optional[List[int]] = None ): """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0]
52
import itertools import json import os import unittest from transformers import AddedToken, LongformerTokenizer, LongformerTokenizerFast from transformers.models.longformer.tokenization_longformer import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __lowerCAmelCase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : int = LongformerTokenizer A__ : Optional[int] = True A__ : Any = LongformerTokenizerFast A__ : Dict = True def _a ( self : int ): """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt A__ = [ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', '\u0120', '\u0120l', '\u0120n', '\u0120lo', '\u0120low', 'er', '\u0120lowest', '\u0120newer', '\u0120wider', '<unk>', ] A__ = dict(zip(_snake_case , range(len(_snake_case ) ) ) ) A__ = ['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', ''] A__ = {'unk_token': '<unk>'} A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(_snake_case ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(_snake_case ) ) def _a ( self : int , **_snake_case : Union[str, Any] ): """simple docstring""" kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **_snake_case ) def _a ( self : Optional[int] , **_snake_case : List[Any] ): """simple docstring""" kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **_snake_case ) def _a ( self : Any , _snake_case : Optional[Any] ): """simple docstring""" A__ = 'lower newer' A__ = 'lower newer' return input_text, output_text def _a ( self : Any ): """simple docstring""" A__ = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) A__ = 'lower newer' A__ = ['l', 'o', 'w', 'er', '\u0120', 'n', 'e', 'w', 'er'] A__ = tokenizer.tokenize(_snake_case ) # , add_prefix_space=True) self.assertListEqual(_snake_case , _snake_case ) A__ = tokens + [tokenizer.unk_token] A__ = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(_snake_case ) , _snake_case ) def _a ( self : List[str] ): """simple docstring""" A__ = self.get_tokenizer() self.assertListEqual(tokenizer.encode('Hello world!' , add_special_tokens=_snake_case ) , [0, 3_14_14, 2_32, 3_28, 2] ) self.assertListEqual( tokenizer.encode('Hello world! cécé herlolip 418' , add_special_tokens=_snake_case ) , [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2] , ) @slow def _a ( self : List[Any] ): """simple docstring""" A__ = self.tokenizer_class.from_pretrained('allenai/longformer-base-4096' ) A__ = tokenizer.encode('sequence builders' , add_special_tokens=_snake_case ) A__ = tokenizer.encode('multi-sequence build' , add_special_tokens=_snake_case ) A__ = tokenizer.encode( 'sequence builders' , add_special_tokens=_snake_case , add_prefix_space=_snake_case ) A__ = tokenizer.encode( 'sequence builders' , 'multi-sequence build' , add_special_tokens=_snake_case , add_prefix_space=_snake_case ) A__ = tokenizer.build_inputs_with_special_tokens(_snake_case ) A__ = tokenizer.build_inputs_with_special_tokens(_snake_case , _snake_case ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def _a ( self : List[str] ): """simple docstring""" A__ = self.get_tokenizer() A__ = 'Encode this sequence.' A__ = tokenizer.byte_encoder[' '.encode('utf-8' )[0]] # Testing encoder arguments A__ = tokenizer.encode(_snake_case , add_special_tokens=_snake_case , add_prefix_space=_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(_snake_case , _snake_case ) A__ = tokenizer.encode(_snake_case , add_special_tokens=_snake_case , add_prefix_space=_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(_snake_case , _snake_case ) tokenizer.add_special_tokens({'bos_token': '<s>'} ) A__ = tokenizer.encode(_snake_case , add_special_tokens=_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(_snake_case , _snake_case ) # Testing spaces after special tokens A__ = '<mask>' tokenizer.add_special_tokens( {'mask_token': AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case )} ) # mask token has a left space A__ = tokenizer.convert_tokens_to_ids(_snake_case ) A__ = 'Encode <mask> sequence' A__ = 'Encode <mask>sequence' A__ = tokenizer.encode(_snake_case ) A__ = encoded.index(_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(_snake_case , _snake_case ) A__ = tokenizer.encode(_snake_case ) A__ = encoded.index(_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(_snake_case , _snake_case ) def _a ( self : Dict ): """simple docstring""" pass def _a ( self : Union[str, Any] ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): A__ = self.rust_tokenizer_class.from_pretrained(_snake_case , **_snake_case ) A__ = self.tokenizer_class.from_pretrained(_snake_case , **_snake_case ) A__ = 'A, <mask> AllenNLP sentence.' A__ = tokenizer_r.encode_plus(_snake_case , add_special_tokens=_snake_case , return_token_type_ids=_snake_case ) A__ = tokenizer_p.encode_plus(_snake_case , add_special_tokens=_snake_case , return_token_type_ids=_snake_case ) # token_type_ids should put 0 everywhere self.assertEqual(sum(tokens_r['token_type_ids'] ) , sum(tokens_p['token_type_ids'] ) ) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEqual( sum(tokens_r['attention_mask'] ) / len(tokens_r['attention_mask'] ) , sum(tokens_p['attention_mask'] ) / len(tokens_p['attention_mask'] ) , ) A__ = tokenizer_r.convert_ids_to_tokens(tokens_r['input_ids'] ) A__ = tokenizer_p.convert_ids_to_tokens(tokens_p['input_ids'] ) # Rust correctly handles the space before the mask while python doesnt self.assertSequenceEqual(tokens_p['input_ids'] , [0, 2_50, 6, 5_02_64, 38_23, 4_87, 2_19_92, 36_45, 4, 2] ) self.assertSequenceEqual(tokens_r['input_ids'] , [0, 2_50, 6, 5_02_64, 38_23, 4_87, 2_19_92, 36_45, 4, 2] ) self.assertSequenceEqual( _snake_case , ['<s>', 'A', ',', '<mask>', 'ĠAllen', 'N', 'LP', 'Ġsentence', '.', '</s>'] ) self.assertSequenceEqual( _snake_case , ['<s>', 'A', ',', '<mask>', 'ĠAllen', 'N', 'LP', 'Ġsentence', '.', '</s>'] ) def _a ( self : List[Any] ): """simple docstring""" for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): A__ = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) A__ = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state['add_prefix_space'] , _snake_case ) self.assertEqual(post_processor_state['add_prefix_space'] , _snake_case ) self.assertEqual(post_processor_state['trim_offsets'] , _snake_case ) def _a ( self : Optional[Any] ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): A__ = 'hello' # `hello` is a token in the vocabulary of `pretrained_name` A__ = F'''{text_of_1_token} {text_of_1_token}''' A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_snake_case ) + 1, len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_snake_case ) + 1, len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_snake_case ), len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_snake_case ), len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = F''' {text}''' # tokenizer_r = self.rust_tokenizer_class.from_pretrained( # pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True # ) # encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False) # self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token))) # self.assertEqual( # encoding.offset_mapping[1], # (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)), # ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_snake_case ) + 1, 1 + len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_snake_case ), 1 + len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_snake_case ), 1 + len(_snake_case ) + 1 + len(_snake_case )) , )
52
1
from typing import List from ...configuration_utils import PretrainedConfig from ...utils import logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = { '''snap-research/efficientformer-l1-300''': ( '''https://huggingface.co/snap-research/efficientformer-l1-300/resolve/main/config.json''' ), } class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : str = "efficientformer" def __init__( self : List[Any] , _snake_case : List[int] = [3, 2, 6, 4] , _snake_case : List[int] = [48, 96, 2_24, 4_48] , _snake_case : List[bool] = [True, True, True, True] , _snake_case : int = 4_48 , _snake_case : int = 32 , _snake_case : int = 4 , _snake_case : int = 7 , _snake_case : int = 5 , _snake_case : int = 8 , _snake_case : int = 4 , _snake_case : float = 0.0 , _snake_case : int = 16 , _snake_case : int = 3 , _snake_case : int = 3 , _snake_case : int = 3 , _snake_case : int = 2 , _snake_case : int = 1 , _snake_case : float = 0.0 , _snake_case : int = 1 , _snake_case : bool = True , _snake_case : bool = True , _snake_case : float = 1E-5 , _snake_case : str = "gelu" , _snake_case : float = 0.02 , _snake_case : float = 1E-12 , _snake_case : int = 2_24 , _snake_case : float = 1E-05 , **_snake_case : Optional[Any] , ): """simple docstring""" super().__init__(**_snake_case ) A__ = hidden_act A__ = hidden_dropout_prob A__ = hidden_sizes A__ = num_hidden_layers A__ = num_attention_heads A__ = initializer_range A__ = layer_norm_eps A__ = patch_size A__ = num_channels A__ = depths A__ = mlp_expansion_ratio A__ = downsamples A__ = dim A__ = key_dim A__ = attention_ratio A__ = resolution A__ = pool_size A__ = downsample_patch_size A__ = downsample_stride A__ = downsample_pad A__ = drop_path_rate A__ = num_metaad_blocks A__ = distillation A__ = use_layer_scale A__ = layer_scale_init_value A__ = image_size A__ = batch_norm_eps
52
import pytest import datasets # Import fixture modules as plugins SCREAMING_SNAKE_CASE__ = ['''tests.fixtures.files''', '''tests.fixtures.hub''', '''tests.fixtures.fsspec'''] def A ( __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: # Mark tests as "unit" by default if not marked as "integration" (or already marked as "unit") for item in items: if any(marker in item.keywords for marker in ['integration', 'unit'] ): continue item.add_marker(pytest.mark.unit ) def A ( __UpperCamelCase ) -> str: config.addinivalue_line('markers' , 'torchaudio_latest: mark test to run with torchaudio>=0.12' ) @pytest.fixture(autouse=__UpperCamelCase ) def A ( __UpperCamelCase , __UpperCamelCase ) -> Union[str, Any]: # test_hf_cache_home = tmp_path_factory.mktemp("cache") # TODO: why a cache dir per test function does not work? A__ = tmp_path_factory.getbasetemp() / 'cache' A__ = test_hf_cache_home / 'datasets' A__ = test_hf_cache_home / 'metrics' A__ = test_hf_cache_home / 'modules' monkeypatch.setattr('datasets.config.HF_DATASETS_CACHE' , str(__UpperCamelCase ) ) monkeypatch.setattr('datasets.config.HF_METRICS_CACHE' , str(__UpperCamelCase ) ) monkeypatch.setattr('datasets.config.HF_MODULES_CACHE' , str(__UpperCamelCase ) ) A__ = test_hf_datasets_cache / 'downloads' monkeypatch.setattr('datasets.config.DOWNLOADED_DATASETS_PATH' , str(__UpperCamelCase ) ) A__ = test_hf_datasets_cache / 'downloads' / 'extracted' monkeypatch.setattr('datasets.config.EXTRACTED_DATASETS_PATH' , str(__UpperCamelCase ) ) @pytest.fixture(autouse=__UpperCamelCase , scope='session' ) def A ( ) -> Union[str, Any]: datasets.disable_progress_bar() @pytest.fixture(autouse=__UpperCamelCase ) def A ( __UpperCamelCase ) -> int: # don't take tests into account when counting downloads monkeypatch.setattr('datasets.config.HF_UPDATE_DOWNLOAD_COUNTS' , __UpperCamelCase ) @pytest.fixture def A ( __UpperCamelCase ) -> Any: # Required to suppress RemovedIn20Warning when feature(s) are not compatible with SQLAlchemy 2.0 # To be removed once SQLAlchemy 2.0 supported monkeypatch.setattr('sqlalchemy.util.deprecations.SILENCE_UBER_WARNING' , __UpperCamelCase )
52
1
from __future__ import annotations class __lowerCAmelCase : """simple docstring""" def __init__( self : str , _snake_case : str , _snake_case : str ): """simple docstring""" A__ , A__ = text, pattern A__ , A__ = len(_snake_case ), len(_snake_case ) def _a ( self : Optional[Any] , _snake_case : str ): """simple docstring""" for i in range(self.patLen - 1 , -1 , -1 ): if char == self.pattern[i]: return i return -1 def _a ( self : Any , _snake_case : int ): """simple docstring""" for i in range(self.patLen - 1 , -1 , -1 ): if self.pattern[i] != self.text[current_pos + i]: return current_pos + i return -1 def _a ( self : List[Any] ): """simple docstring""" A__ = [] for i in range(self.textLen - self.patLen + 1 ): A__ = self.mismatch_in_text(_snake_case ) if mismatch_index == -1: positions.append(_snake_case ) else: A__ = self.match_in_pattern(self.text[mismatch_index] ) A__ = ( mismatch_index - match_index ) # shifting index lgtm [py/multiple-definition] return positions SCREAMING_SNAKE_CASE__ = '''ABAABA''' SCREAMING_SNAKE_CASE__ = '''AB''' SCREAMING_SNAKE_CASE__ = BoyerMooreSearch(text, pattern) SCREAMING_SNAKE_CASE__ = bms.bad_character_heuristic() if len(positions) == 0: print('''No match found''') else: print('''Pattern found in following positions: ''') print(positions)
52
import argparse import re from typing import Dict import torch from datasets import Audio, Dataset, load_dataset, load_metric from transformers import AutoFeatureExtractor, pipeline def A ( __UpperCamelCase , __UpperCamelCase ) -> Tuple: A__ = args.log_outputs A__ = '_'.join(args.dataset.split('/' ) + [args.config, args.split] ) # load metric A__ = load_metric('wer' ) A__ = load_metric('cer' ) # compute metrics A__ = wer.compute(references=result['target'] , predictions=result['prediction'] ) A__ = cer.compute(references=result['target'] , predictions=result['prediction'] ) # print & log results A__ = f'''WER: {wer_result}\nCER: {cer_result}''' print(__UpperCamelCase ) with open(f'''{dataset_id}_eval_results.txt''' , 'w' ) as f: f.write(__UpperCamelCase ) # log all results in text file. Possibly interesting for analysis if log_outputs is not None: A__ = f'''log_{dataset_id}_predictions.txt''' A__ = f'''log_{dataset_id}_targets.txt''' with open(__UpperCamelCase , 'w' ) as p, open(__UpperCamelCase , 'w' ) as t: # mapping function to write output def write_to_file(__UpperCamelCase , __UpperCamelCase ): p.write(f'''{i}''' + '\n' ) p.write(batch['prediction'] + '\n' ) t.write(f'''{i}''' + '\n' ) t.write(batch['target'] + '\n' ) result.map(__UpperCamelCase , with_indices=__UpperCamelCase ) def A ( __UpperCamelCase ) -> str: A__ = '[,?.!\-\;\:"“%‘”�—’…–]' # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training A__ = re.sub(__UpperCamelCase , '' , text.lower() ) # In addition, we can normalize the target text, e.g. removing new lines characters etc... # note that order is important here! A__ = ['\n\n', '\n', ' ', ' '] for t in token_sequences_to_ignore: A__ = ' '.join(text.split(__UpperCamelCase ) ) return text def A ( __UpperCamelCase ) -> Union[str, Any]: # load dataset A__ = load_dataset(args.dataset , args.config , split=args.split , use_auth_token=__UpperCamelCase ) # for testing: only process the first two examples as a test # dataset = dataset.select(range(10)) # load processor A__ = AutoFeatureExtractor.from_pretrained(args.model_id ) A__ = feature_extractor.sampling_rate # resample audio A__ = dataset.cast_column('audio' , Audio(sampling_rate=__UpperCamelCase ) ) # load eval pipeline if args.device is None: A__ = 0 if torch.cuda.is_available() else -1 A__ = pipeline('automatic-speech-recognition' , model=args.model_id , device=args.device ) # map function to decode audio def map_to_pred(__UpperCamelCase ): A__ = asr( batch['audio']['array'] , chunk_length_s=args.chunk_length_s , stride_length_s=args.stride_length_s ) A__ = prediction['text'] A__ = normalize_text(batch['sentence'] ) return batch # run inference on all examples A__ = dataset.map(__UpperCamelCase , remove_columns=dataset.column_names ) # compute and log_results # do not change function below log_results(__UpperCamelCase , __UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument( '''--model_id''', type=str, required=True, help='''Model identifier. Should be loadable with 🤗 Transformers''' ) parser.add_argument( '''--dataset''', type=str, required=True, help='''Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets''', ) parser.add_argument( '''--config''', type=str, required=True, help='''Config of the dataset. *E.g.* `\'en\'` for Common Voice''' ) parser.add_argument('''--split''', type=str, required=True, help='''Split of the dataset. *E.g.* `\'test\'`''') parser.add_argument( '''--chunk_length_s''', type=float, default=None, help='''Chunk length in seconds. Defaults to 5 seconds.''' ) parser.add_argument( '''--stride_length_s''', type=float, default=None, help='''Stride of the audio chunks. Defaults to 1 second.''' ) parser.add_argument( '''--log_outputs''', action='''store_true''', help='''If defined, write outputs to log file for analysis.''' ) parser.add_argument( '''--device''', type=int, default=None, help='''The device to run the pipeline on. -1 for CPU (default), 0 for the first GPU and so on.''', ) SCREAMING_SNAKE_CASE__ = parser.parse_args() main(args)
52
1
from typing import Optional from torch import nn from .transformer_ad import TransformeraDModel, TransformeraDModelOutput class __lowerCAmelCase ( nn.Module ): """simple docstring""" def __init__( self : Optional[int] , _snake_case : int = 16 , _snake_case : int = 88 , _snake_case : Optional[int] = None , _snake_case : int = 1 , _snake_case : float = 0.0 , _snake_case : int = 32 , _snake_case : Optional[int] = None , _snake_case : bool = False , _snake_case : Optional[int] = None , _snake_case : Optional[int] = None , _snake_case : str = "geglu" , _snake_case : Optional[int] = None , ): """simple docstring""" super().__init__() A__ = nn.ModuleList( [ TransformeraDModel( num_attention_heads=_snake_case , attention_head_dim=_snake_case , in_channels=_snake_case , num_layers=_snake_case , dropout=_snake_case , norm_num_groups=_snake_case , cross_attention_dim=_snake_case , attention_bias=_snake_case , sample_size=_snake_case , num_vector_embeds=_snake_case , activation_fn=_snake_case , num_embeds_ada_norm=_snake_case , ) for _ in range(2 ) ] ) # Variables that can be set by a pipeline: # The ratio of transformer1 to transformer2's output states to be combined during inference A__ = 0.5 # The shape of `encoder_hidden_states` is expected to be # `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)` A__ = [77, 2_57] # Which transformer to use to encode which condition. # E.g. `(1, 0)` means that we'll use `transformers[1](conditions[0])` and `transformers[0](conditions[1])` A__ = [1, 0] def _a ( self : Optional[Any] , _snake_case : Any , _snake_case : Union[str, Any] , _snake_case : str=None , _snake_case : Optional[int]=None , _snake_case : Union[str, Any]=None , _snake_case : bool = True , ): """simple docstring""" A__ = hidden_states A__ = [] A__ = 0 # attention_mask is not used yet for i in range(2 ): # for each of the two transformers, pass the corresponding condition tokens A__ = encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]] A__ = self.transformer_index_for_condition[i] A__ = self.transformers[transformer_index]( _snake_case , encoder_hidden_states=_snake_case , timestep=_snake_case , cross_attention_kwargs=_snake_case , return_dict=_snake_case , )[0] encoded_states.append(encoded_state - input_states ) tokens_start += self.condition_lengths[i] A__ = encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio) A__ = output_states + input_states if not return_dict: return (output_states,) return TransformeraDModelOutput(sample=_snake_case )
52
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import YolosConfig, YolosForObjectDetection, YolosImageProcessor from transformers.utils import logging logging.set_verbosity_info() SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) def A ( __UpperCamelCase ) -> YolosConfig: A__ = YolosConfig() # size of the architecture if "yolos_ti" in yolos_name: A__ = 192 A__ = 768 A__ = 12 A__ = 3 A__ = [800, 1_333] A__ = False elif yolos_name == "yolos_s_dWr": A__ = 330 A__ = 14 A__ = 6 A__ = 1_320 elif "yolos_s" in yolos_name: A__ = 384 A__ = 1_536 A__ = 12 A__ = 6 elif "yolos_b" in yolos_name: A__ = [800, 1_344] A__ = 91 A__ = 'huggingface/label-files' A__ = 'coco-detection-id2label.json' A__ = json.load(open(hf_hub_download(__UpperCamelCase , __UpperCamelCase , repo_type='dataset' ) , 'r' ) ) A__ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} A__ = idalabel A__ = {v: k for k, v in idalabel.items()} return config def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = False ) -> str: for i in range(config.num_hidden_layers ): # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) A__ = state_dict.pop(f'''blocks.{i}.attn.qkv.weight''' ) A__ = state_dict.pop(f'''blocks.{i}.attn.qkv.bias''' ) # next, add query, keys and values (in that order) to the state dict A__ = in_proj_weight[: config.hidden_size, :] A__ = in_proj_bias[: config.hidden_size] A__ = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] A__ = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] A__ = in_proj_weight[-config.hidden_size :, :] A__ = in_proj_bias[-config.hidden_size :] def A ( __UpperCamelCase ) -> str: if "backbone" in name: A__ = name.replace('backbone' , 'vit' ) if "cls_token" in name: A__ = name.replace('cls_token' , 'embeddings.cls_token' ) if "det_token" in name: A__ = name.replace('det_token' , 'embeddings.detection_tokens' ) if "mid_pos_embed" in name: A__ = name.replace('mid_pos_embed' , 'encoder.mid_position_embeddings' ) if "pos_embed" in name: A__ = name.replace('pos_embed' , 'embeddings.position_embeddings' ) if "patch_embed.proj" in name: A__ = name.replace('patch_embed.proj' , 'embeddings.patch_embeddings.projection' ) if "blocks" in name: A__ = name.replace('blocks' , 'encoder.layer' ) if "attn.proj" in name: A__ = name.replace('attn.proj' , 'attention.output.dense' ) if "attn" in name: A__ = name.replace('attn' , 'attention.self' ) if "norm1" in name: A__ = name.replace('norm1' , 'layernorm_before' ) if "norm2" in name: A__ = name.replace('norm2' , 'layernorm_after' ) if "mlp.fc1" in name: A__ = name.replace('mlp.fc1' , 'intermediate.dense' ) if "mlp.fc2" in name: A__ = name.replace('mlp.fc2' , 'output.dense' ) if "class_embed" in name: A__ = name.replace('class_embed' , 'class_labels_classifier' ) if "bbox_embed" in name: A__ = name.replace('bbox_embed' , 'bbox_predictor' ) if "vit.norm" in name: A__ = name.replace('vit.norm' , 'vit.layernorm' ) return name def A ( __UpperCamelCase , __UpperCamelCase ) -> dict: for key in orig_state_dict.copy().keys(): A__ = orig_state_dict.pop(__UpperCamelCase ) if "qkv" in key: A__ = key.split('.' ) A__ = int(key_split[2] ) A__ = model.vit.encoder.layer[layer_num].attention.attention.all_head_size if "weight" in key: A__ = val[:dim, :] A__ = val[ dim : dim * 2, : ] A__ = val[-dim:, :] else: A__ = val[:dim] A__ = val[dim : dim * 2] A__ = val[-dim:] else: A__ = val return orig_state_dict def A ( ) -> torch.Tensor: A__ = 'http://images.cocodataset.org/val2017/000000039769.jpg' A__ = Image.open(requests.get(__UpperCamelCase , stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = False ) -> List[str]: A__ = get_yolos_config(__UpperCamelCase ) # load original state_dict A__ = torch.load(__UpperCamelCase , map_location='cpu' )['model'] # load 🤗 model A__ = YolosForObjectDetection(__UpperCamelCase ) model.eval() A__ = convert_state_dict(__UpperCamelCase , __UpperCamelCase ) model.load_state_dict(__UpperCamelCase ) # Check outputs on an image, prepared by YolosImageProcessor A__ = 800 if yolos_name != 'yolos_ti' else 512 A__ = YolosImageProcessor(format='coco_detection' , size=__UpperCamelCase ) A__ = image_processor(images=prepare_img() , return_tensors='pt' ) A__ = model(**__UpperCamelCase ) A__ , A__ = outputs.logits, outputs.pred_boxes A__ , A__ = None, None if yolos_name == "yolos_ti": A__ = torch.tensor( [[-39.5022, -11.9820, -17.6888], [-29.9574, -9.9769, -17.7691], [-42.3281, -20.7200, -30.6294]] ) A__ = torch.tensor( [[0.4021, 0.0836, 0.7979], [0.0184, 0.2609, 0.0364], [0.1781, 0.2004, 0.2095]] ) elif yolos_name == "yolos_s_200_pre": A__ = torch.tensor( [[-24.0248, -10.3024, -14.8290], [-42.0392, -16.8200, -27.4334], [-27.2743, -11.8154, -18.7148]] ) A__ = torch.tensor( [[0.2559, 0.5455, 0.4706], [0.2989, 0.7279, 0.1875], [0.7732, 0.4017, 0.4462]] ) elif yolos_name == "yolos_s_300_pre": A__ = torch.tensor( [[-36.2220, -14.4385, -23.5457], [-35.6970, -14.7583, -21.3935], [-31.5939, -13.6042, -16.8049]] ) A__ = torch.tensor( [[0.7614, 0.2316, 0.4728], [0.7168, 0.4495, 0.3855], [0.4996, 0.1466, 0.9996]] ) elif yolos_name == "yolos_s_dWr": A__ = torch.tensor( [[-42.8668, -24.1049, -41.1690], [-34.7456, -14.1274, -24.9194], [-33.7898, -12.1946, -25.6495]] ) A__ = torch.tensor( [[0.5587, 0.2773, 0.0605], [0.5004, 0.3014, 0.9994], [0.4999, 0.1548, 0.9994]] ) elif yolos_name == "yolos_base": A__ = torch.tensor( [[-40.6064, -24.3084, -32.6447], [-55.1990, -30.7719, -35.5877], [-51.4311, -33.3507, -35.6462]] ) A__ = torch.tensor( [[0.5555, 0.2794, 0.0655], [0.9049, 0.2664, 0.1894], [0.9183, 0.1984, 0.1635]] ) else: raise ValueError(f'''Unknown yolos_name: {yolos_name}''' ) assert torch.allclose(logits[0, :3, :3] , __UpperCamelCase , atol=1E-4 ) assert torch.allclose(pred_boxes[0, :3, :3] , __UpperCamelCase , atol=1E-4 ) Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) print(f'''Saving model {yolos_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(__UpperCamelCase ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(__UpperCamelCase ) if push_to_hub: A__ = { 'yolos_ti': 'yolos-tiny', 'yolos_s_200_pre': 'yolos-small', 'yolos_s_300_pre': 'yolos-small-300', 'yolos_s_dWr': 'yolos-small-dwr', 'yolos_base': 'yolos-base', } print('Pushing to the hub...' ) A__ = model_mapping[yolos_name] image_processor.push_to_hub(__UpperCamelCase , organization='hustvl' ) model.push_to_hub(__UpperCamelCase , organization='hustvl' ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--yolos_name''', default='''yolos_s_200_pre''', type=str, help=( '''Name of the YOLOS model you\'d like to convert. Should be one of \'yolos_ti\', \'yolos_s_200_pre\',''' ''' \'yolos_s_300_pre\', \'yolos_s_dWr\', \'yolos_base\'.''' ), ) parser.add_argument( '''--checkpoint_path''', default=None, type=str, help='''Path to the original state dict (.pth file).''' ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_yolos_checkpoint(args.yolos_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub)
52
1
import argparse import shutil from pathlib import Path from tqdm import tqdm from transformers import AutoTokenizer def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=1_024 ) -> Dict: A__ , A__ = [], [] A__ = list(zip(__UpperCamelCase , __UpperCamelCase ) ) A__ , A__ = sorted_examples[0] def is_too_big(__UpperCamelCase ): return tok(__UpperCamelCase , return_tensors='pt' ).input_ids.shape[1] > max_tokens for src, tgt in tqdm(sorted_examples[1:] ): A__ = new_src + ' ' + src A__ = new_tgt + ' ' + tgt if is_too_big(__UpperCamelCase ) or is_too_big(__UpperCamelCase ): # cant fit, finalize example finished_src.append(__UpperCamelCase ) finished_tgt.append(__UpperCamelCase ) A__ , A__ = src, tgt else: # can fit, keep adding A__ , A__ = cand_src, cand_tgt # cleanup if new_src: assert new_tgt finished_src.append(__UpperCamelCase ) finished_tgt.append(__UpperCamelCase ) return finished_src, finished_tgt def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[Any]: A__ = Path(__UpperCamelCase ) save_path.mkdir(exist_ok=__UpperCamelCase ) for split in ["train"]: A__ , A__ = data_dir / f'''{split}.source''', data_dir / f'''{split}.target''' A__ = [x.rstrip() for x in Path(__UpperCamelCase ).open().readlines()] A__ = [x.rstrip() for x in Path(__UpperCamelCase ).open().readlines()] A__ , A__ = pack_examples(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) print(f'''packed {split} split from {len(__UpperCamelCase )} examples -> {len(__UpperCamelCase )}.''' ) Path(save_path / f'''{split}.source''' ).open('w' ).write('\n'.join(__UpperCamelCase ) ) Path(save_path / f'''{split}.target''' ).open('w' ).write('\n'.join(__UpperCamelCase ) ) for split in ["val", "test"]: A__ , A__ = data_dir / f'''{split}.source''', data_dir / f'''{split}.target''' shutil.copyfile(__UpperCamelCase , save_path / f'''{split}.source''' ) shutil.copyfile(__UpperCamelCase , save_path / f'''{split}.target''' ) def A ( ) -> str: A__ = argparse.ArgumentParser() parser.add_argument('--tok_name' , type=__UpperCamelCase , help='like facebook/bart-large-cnn,t5-base, etc.' ) parser.add_argument('--max_seq_len' , type=__UpperCamelCase , default=128 ) parser.add_argument('--data_dir' , type=__UpperCamelCase ) parser.add_argument('--save_path' , type=__UpperCamelCase ) A__ = parser.parse_args() A__ = AutoTokenizer.from_pretrained(args.tok_name ) return pack_data_dir(__UpperCamelCase , Path(args.data_dir ) , args.max_seq_len , args.save_path ) if __name__ == "__main__": packer_cli()
52
from typing import TYPE_CHECKING from ..utils import _LazyModule SCREAMING_SNAKE_CASE__ = { '''config''': [ '''EXTERNAL_DATA_FORMAT_SIZE_LIMIT''', '''OnnxConfig''', '''OnnxConfigWithPast''', '''OnnxSeq2SeqConfigWithPast''', '''PatchingSpec''', ], '''convert''': ['''export''', '''validate_model_outputs'''], '''features''': ['''FeaturesManager'''], '''utils''': ['''ParameterFormat''', '''compute_serialized_parameters_size'''], } if TYPE_CHECKING: from .config import ( EXTERNAL_DATA_FORMAT_SIZE_LIMIT, OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast, PatchingSpec, ) from .convert import export, validate_model_outputs from .features import FeaturesManager from .utils import ParameterFormat, compute_serialized_parameters_size else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
52
1
import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @slow def _a ( self : int ): """simple docstring""" A__ = FlaxMTaForConditionalGeneration.from_pretrained('google/mt5-small' ) A__ = AutoTokenizer.from_pretrained('google/mt5-small' ) A__ = tokenizer('Hello there' , return_tensors='np' ).input_ids A__ = tokenizer('Hi I am' , return_tensors='np' ).input_ids A__ = shift_tokens_right(_snake_case , model.config.pad_token_id , model.config.decoder_start_token_id ) A__ = model(_snake_case , decoder_input_ids=_snake_case ).logits A__ = optax.softmax_cross_entropy(_snake_case , onehot(_snake_case , logits.shape[-1] ) ).mean() A__ = -(labels.shape[-1] * loss.item()) A__ = -84.9127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1E-4 )
52
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_rembert import RemBertTokenizer else: SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = {'''vocab_file''': '''sentencepiece.model''', '''tokenizer_file''': '''tokenizer.json'''} SCREAMING_SNAKE_CASE__ = { '''vocab_file''': { '''google/rembert''': '''https://huggingface.co/google/rembert/resolve/main/sentencepiece.model''', }, '''tokenizer_file''': { '''google/rembert''': '''https://huggingface.co/google/rembert/resolve/main/tokenizer.json''', }, } SCREAMING_SNAKE_CASE__ = { '''google/rembert''': 2_5_6, } SCREAMING_SNAKE_CASE__ = '''▁''' class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Any = VOCAB_FILES_NAMES A__ : str = PRETRAINED_VOCAB_FILES_MAP A__ : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A__ : int = RemBertTokenizer def __init__( self : Union[str, Any] , _snake_case : Any=None , _snake_case : Optional[Any]=None , _snake_case : Any=True , _snake_case : Optional[int]=True , _snake_case : Dict=False , _snake_case : Dict="[CLS]" , _snake_case : List[Any]="[SEP]" , _snake_case : Union[str, Any]="<unk>" , _snake_case : List[str]="[SEP]" , _snake_case : List[str]="<pad>" , _snake_case : str="[CLS]" , _snake_case : Any="[MASK]" , **_snake_case : Any , ): """simple docstring""" A__ = AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case ) if isinstance(_snake_case , _snake_case ) else mask_token super().__init__( _snake_case , tokenizer_file=_snake_case , do_lower_case=_snake_case , remove_space=_snake_case , keep_accents=_snake_case , bos_token=_snake_case , eos_token=_snake_case , unk_token=_snake_case , sep_token=_snake_case , pad_token=_snake_case , cls_token=_snake_case , mask_token=_snake_case , **_snake_case , ) A__ = do_lower_case A__ = remove_space A__ = keep_accents A__ = vocab_file A__ = False if not self.vocab_file else True def _a ( self : Any , _snake_case : List[int] , _snake_case : Optional[List[int]] = None ): """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def _a ( self : Tuple , _snake_case : List[int] , _snake_case : Optional[List[int]] = None , _snake_case : bool = False ): """simple docstring""" if already_has_special_tokens: if token_ids_a is not None: raise ValueError( 'You should not supply a second sequence if the provided sequence of ' 'ids is already formatted with special tokens for the model.' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(_snake_case )) + [1] + ([0] * len(_snake_case )) + [1] return [1] + ([0] * len(_snake_case )) + [1] def _a ( self : Dict , _snake_case : List[int] , _snake_case : Optional[List[int]] = None ): """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def _a ( self : Any , _snake_case : str , _snake_case : Optional[str] = None ): """simple docstring""" if not os.path.isdir(_snake_case ): logger.error('Vocabulary path ({}) should be a directory'.format(_snake_case ) ) return A__ = os.path.join( _snake_case , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_snake_case ): copyfile(self.vocab_file , _snake_case ) return (out_vocab_file,)
52
1
# NOTE: This file is deprecated and will be removed in a future version. # It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works from ...utils import deprecate from ..controlnet.multicontrolnet import MultiControlNetModel # noqa: F401 from ..controlnet.pipeline_controlnet import StableDiffusionControlNetPipeline # noqa: F401 deprecate( '''stable diffusion controlnet''', '''0.22.0''', '''Importing `StableDiffusionControlNetPipeline` or `MultiControlNetModel` from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet is deprecated. Please import `from diffusers import StableDiffusionControlNetPipeline` instead.''', standard_warn=False, stacklevel=3, )
52
import tempfile import unittest from make_student import create_student_by_copying_alternating_layers from transformers import AutoConfig from transformers.file_utils import cached_property from transformers.testing_utils import require_torch SCREAMING_SNAKE_CASE__ = '''sshleifer/bart-tiny-random''' SCREAMING_SNAKE_CASE__ = '''patrickvonplaten/t5-tiny-random''' @require_torch class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @cached_property def _a ( self : Optional[int] ): """simple docstring""" return AutoConfig.from_pretrained(_snake_case ) def _a ( self : Optional[Any] ): """simple docstring""" A__ , *A__ = create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=1 , d=1 ) self.assertEqual(student.config.num_hidden_layers , 1 ) def _a ( self : Optional[int] ): """simple docstring""" A__ , *A__ = create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=1 , d=_snake_case ) def _a ( self : int ): """simple docstring""" A__ , *A__ = create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=1 , d=_snake_case ) self.assertEqual(student.config.encoder_layers , 1 ) self.assertEqual(student.config.decoder_layers , self.teacher_config.encoder_layers ) def _a ( self : str ): """simple docstring""" A__ , *A__ = create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=1 , d=1 ) self.assertEqual(student.config.encoder_layers , 1 ) self.assertEqual(student.config.decoder_layers , 1 ) def _a ( self : str ): """simple docstring""" with self.assertRaises(_snake_case ): create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=_snake_case , d=_snake_case )
52
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available SCREAMING_SNAKE_CASE__ = { '''configuration_xlm''': ['''XLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XLMConfig''', '''XLMOnnxConfig'''], '''tokenization_xlm''': ['''XLMTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''XLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XLMForMultipleChoice''', '''XLMForQuestionAnswering''', '''XLMForQuestionAnsweringSimple''', '''XLMForSequenceClassification''', '''XLMForTokenClassification''', '''XLMModel''', '''XLMPreTrainedModel''', '''XLMWithLMHeadModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXLMForMultipleChoice''', '''TFXLMForQuestionAnsweringSimple''', '''TFXLMForSequenceClassification''', '''TFXLMForTokenClassification''', '''TFXLMMainLayer''', '''TFXLMModel''', '''TFXLMPreTrainedModel''', '''TFXLMWithLMHeadModel''', ] if TYPE_CHECKING: from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig, XLMOnnxConfig from .tokenization_xlm import XLMTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm import ( XLM_PRETRAINED_MODEL_ARCHIVE_LIST, XLMForMultipleChoice, XLMForQuestionAnswering, XLMForQuestionAnsweringSimple, XLMForSequenceClassification, XLMForTokenClassification, XLMModel, XLMPreTrainedModel, XLMWithLMHeadModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlm import ( TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLMForMultipleChoice, TFXLMForQuestionAnsweringSimple, TFXLMForSequenceClassification, TFXLMForTokenClassification, TFXLMMainLayer, TFXLMModel, TFXLMPreTrainedModel, TFXLMWithLMHeadModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
52
from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Union[str, Any] = ["image_processor", "tokenizer"] A__ : Optional[Any] = "BridgeTowerImageProcessor" A__ : List[Any] = ("RobertaTokenizer", "RobertaTokenizerFast") def __init__( self : List[Any] , _snake_case : Optional[Any] , _snake_case : Optional[int] ): """simple docstring""" super().__init__(_snake_case , _snake_case ) def __call__( self : List[Any] , _snake_case : int , _snake_case : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , _snake_case : bool = True , _snake_case : Union[bool, str, PaddingStrategy] = False , _snake_case : Union[bool, str, TruncationStrategy] = None , _snake_case : Optional[int] = None , _snake_case : int = 0 , _snake_case : Optional[int] = None , _snake_case : Optional[bool] = None , _snake_case : Optional[bool] = None , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = True , _snake_case : Optional[Union[str, TensorType]] = None , **_snake_case : Optional[int] , ): """simple docstring""" A__ = self.tokenizer( text=_snake_case , add_special_tokens=_snake_case , padding=_snake_case , truncation=_snake_case , max_length=_snake_case , stride=_snake_case , pad_to_multiple_of=_snake_case , return_token_type_ids=_snake_case , return_attention_mask=_snake_case , return_overflowing_tokens=_snake_case , return_special_tokens_mask=_snake_case , return_offsets_mapping=_snake_case , return_length=_snake_case , verbose=_snake_case , return_tensors=_snake_case , **_snake_case , ) # add pixel_values + pixel_mask A__ = self.image_processor( _snake_case , return_tensors=_snake_case , do_normalize=_snake_case , do_center_crop=_snake_case , **_snake_case ) encoding.update(_snake_case ) return encoding def _a ( self : Any , *_snake_case : Tuple , **_snake_case : List[Any] ): """simple docstring""" return self.tokenizer.batch_decode(*_snake_case , **_snake_case ) def _a ( self : Dict , *_snake_case : Dict , **_snake_case : List[str] ): """simple docstring""" return self.tokenizer.decode(*_snake_case , **_snake_case ) @property def _a ( self : Tuple ): """simple docstring""" A__ = self.tokenizer.model_input_names A__ = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
52
1
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, ClassLabel, Features from .base import TaskTemplate @dataclass(frozen=UpperCAmelCase_ ) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : str = field(default="audio-classification" , metadata={"include_in_asdict_even_if_is_default": True} ) A__ : ClassVar[Features] = Features({"audio": Audio()} ) A__ : ClassVar[Features] = Features({"labels": ClassLabel} ) A__ : str = "audio" A__ : str = "labels" def _a ( self : str , _snake_case : Union[str, Any] ): """simple docstring""" if self.label_column not in features: raise ValueError(F'''Column {self.label_column} is not present in features.''' ) if not isinstance(features[self.label_column] , _snake_case ): raise ValueError(F'''Column {self.label_column} is not a ClassLabel.''' ) A__ = copy.deepcopy(self ) A__ = self.label_schema.copy() A__ = features[self.label_column] A__ = label_schema return task_template @property def _a ( self : List[str] ): """simple docstring""" return { self.audio_column: "audio", self.label_column: "labels", }
52
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) SCREAMING_SNAKE_CASE__ = { '''configuration_xlm_roberta''': [ '''XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XLMRobertaConfig''', '''XLMRobertaOnnxConfig''', ], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = ['''XLMRobertaTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = ['''XLMRobertaTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XLMRobertaForCausalLM''', '''XLMRobertaForMaskedLM''', '''XLMRobertaForMultipleChoice''', '''XLMRobertaForQuestionAnswering''', '''XLMRobertaForSequenceClassification''', '''XLMRobertaForTokenClassification''', '''XLMRobertaModel''', '''XLMRobertaPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXLMRobertaForCausalLM''', '''TFXLMRobertaForMaskedLM''', '''TFXLMRobertaForMultipleChoice''', '''TFXLMRobertaForQuestionAnswering''', '''TFXLMRobertaForSequenceClassification''', '''TFXLMRobertaForTokenClassification''', '''TFXLMRobertaModel''', '''TFXLMRobertaPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''FlaxXLMRobertaForMaskedLM''', '''FlaxXLMRobertaForCausalLM''', '''FlaxXLMRobertaForMultipleChoice''', '''FlaxXLMRobertaForQuestionAnswering''', '''FlaxXLMRobertaForSequenceClassification''', '''FlaxXLMRobertaForTokenClassification''', '''FlaxXLMRobertaModel''', '''FlaxXLMRobertaPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_xlm_roberta import ( XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMRobertaConfig, XLMRobertaOnnxConfig, ) try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlm_roberta import XLMRobertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlm_roberta_fast import XLMRobertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm_roberta import ( XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, XLMRobertaForCausalLM, XLMRobertaForMaskedLM, XLMRobertaForMultipleChoice, XLMRobertaForQuestionAnswering, XLMRobertaForSequenceClassification, XLMRobertaForTokenClassification, XLMRobertaModel, XLMRobertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlm_roberta import ( TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLMRobertaForCausalLM, TFXLMRobertaForMaskedLM, TFXLMRobertaForMultipleChoice, TFXLMRobertaForQuestionAnswering, TFXLMRobertaForSequenceClassification, TFXLMRobertaForTokenClassification, TFXLMRobertaModel, TFXLMRobertaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xlm_roberta import ( FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxXLMRobertaForCausalLM, FlaxXLMRobertaForMaskedLM, FlaxXLMRobertaForMultipleChoice, FlaxXLMRobertaForQuestionAnswering, FlaxXLMRobertaForSequenceClassification, FlaxXLMRobertaForTokenClassification, FlaxXLMRobertaModel, FlaxXLMRobertaPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
52
1
# using dfs for finding eulerian path traversal def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=None ) -> List[Any]: A__ = (path or []) + [u] for v in graph[u]: if visited_edge[u][v] is False: A__ , A__ = True, True A__ = dfs(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) return path def A ( __UpperCamelCase , __UpperCamelCase ) -> str: A__ = 0 A__ = -1 for i in range(__UpperCamelCase ): if i not in graph.keys(): continue if len(graph[i] ) % 2 == 1: odd_degree_nodes += 1 A__ = 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 ( __UpperCamelCase , __UpperCamelCase ) -> Union[str, Any]: A__ = [[False for _ in range(max_node + 1 )] for _ in range(max_node + 1 )] A__ , A__ = check_circuit_or_path(__UpperCamelCase , __UpperCamelCase ) if check == 3: print('graph is not Eulerian' ) print('no path' ) return A__ = 1 if check == 2: A__ = odd_node print('graph has a Euler path' ) if check == 1: print('graph has a Euler cycle' ) A__ = dfs(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) print(__UpperCamelCase ) def A ( ) -> Optional[Any]: A__ = {1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]} A__ = {1: [2, 3, 4, 5], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [1, 4]} A__ = {1: [2, 3, 4], 2: [1, 3, 4], 3: [1, 2], 4: [1, 2, 5], 5: [4]} A__ = {1: [2, 3], 2: [1, 3], 3: [1, 2]} A__ = { 1: [], 2: [] # all degree is zero } A__ = 10 check_euler(__UpperCamelCase , __UpperCamelCase ) check_euler(__UpperCamelCase , __UpperCamelCase ) check_euler(__UpperCamelCase , __UpperCamelCase ) check_euler(__UpperCamelCase , __UpperCamelCase ) check_euler(__UpperCamelCase , __UpperCamelCase ) if __name__ == "__main__": main()
52
import argparse import ast import logging import os import sys import pandas as pd import torch from tqdm import tqdm from transformers import BartForConditionalGeneration, RagRetriever, RagSequenceForGeneration, RagTokenForGeneration from transformers import logging as transformers_logging sys.path.append(os.path.join(os.getcwd())) # noqa: E402 # isort:skip from utils_rag import exact_match_score, fa_score # noqa: E402 # isort:skip SCREAMING_SNAKE_CASE__ = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) transformers_logging.set_verbosity_info() def A ( __UpperCamelCase ) -> Tuple: if "token" in model_name_or_path: return "rag_token" if "sequence" in model_name_or_path: return "rag_sequence" if "bart" in model_name_or_path: return "bart" return None def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[Any]: return max(metric_fn(__UpperCamelCase , __UpperCamelCase ) for gt in ground_truths ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[str]: A__ = [line.strip() for line in open(__UpperCamelCase , 'r' ).readlines()] A__ = [] if args.gold_data_mode == "qa": A__ = pd.read_csv(__UpperCamelCase , sep='\t' , header=__UpperCamelCase ) for answer_list in data[1]: A__ = ast.literal_eval(__UpperCamelCase ) answers.append(__UpperCamelCase ) else: A__ = [line.strip() for line in open(__UpperCamelCase , 'r' ).readlines()] A__ = [[reference] for reference in references] A__ = A__ = A__ = 0 for prediction, ground_truths in zip(__UpperCamelCase , __UpperCamelCase ): total += 1 em += metric_max_over_ground_truths(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) fa += metric_max_over_ground_truths(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) A__ = 100.0 * em / total A__ = 100.0 * fa / total logger.info(f'''F1: {fa:.2f}''' ) logger.info(f'''EM: {em:.2f}''' ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: A__ = args.k A__ = [line.strip() for line in open(__UpperCamelCase , 'r' ).readlines()] A__ = [line.strip() for line in open(__UpperCamelCase , 'r' ).readlines()] A__ = A__ = 0 for hypo, reference in zip(__UpperCamelCase , __UpperCamelCase ): A__ = set(hypo.split('\t' )[:k] ) A__ = set(reference.split('\t' ) ) total += 1 em += len(hypo_provenance & ref_provenance ) / k A__ = 100.0 * em / total logger.info(f'''Precision@{k}: {em: .2f}''' ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Union[str, Any]: def strip_title(__UpperCamelCase ): if title.startswith('"' ): A__ = title[1:] if title.endswith('"' ): A__ = title[:-1] return title A__ = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( __UpperCamelCase , return_tensors='pt' , padding=__UpperCamelCase , truncation=__UpperCamelCase , )['input_ids'].to(args.device ) A__ = rag_model.rag.question_encoder(__UpperCamelCase ) A__ = question_enc_outputs[0] A__ = rag_model.retriever( __UpperCamelCase , question_enc_pool_output.cpu().detach().to(torch.floataa ).numpy() , prefix=rag_model.rag.generator.config.prefix , n_docs=rag_model.config.n_docs , return_tensors='pt' , ) A__ = rag_model.retriever.index.get_doc_dicts(result.doc_ids ) A__ = [] for docs in all_docs: A__ = [strip_title(__UpperCamelCase ) for title in docs['title']] provenance_strings.append('\t'.join(__UpperCamelCase ) ) return provenance_strings def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Optional[Any]: with torch.no_grad(): A__ = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( __UpperCamelCase , return_tensors='pt' , padding=__UpperCamelCase , truncation=__UpperCamelCase ) A__ = inputs_dict.input_ids.to(args.device ) A__ = inputs_dict.attention_mask.to(args.device ) A__ = rag_model.generate( # rag_model overwrites generate __UpperCamelCase , attention_mask=__UpperCamelCase , num_beams=args.num_beams , min_length=args.min_length , max_length=args.max_length , early_stopping=__UpperCamelCase , num_return_sequences=1 , bad_words_ids=[[0, 0]] , ) A__ = rag_model.retriever.generator_tokenizer.batch_decode(__UpperCamelCase , skip_special_tokens=__UpperCamelCase ) if args.print_predictions: for q, a in zip(__UpperCamelCase , __UpperCamelCase ): logger.info('Q: {} - A: {}'.format(__UpperCamelCase , __UpperCamelCase ) ) return answers def A ( ) -> Any: A__ = argparse.ArgumentParser() parser.add_argument( '--model_type' , choices=['rag_sequence', 'rag_token', 'bart'] , type=__UpperCamelCase , help=( 'RAG model type: rag_sequence, rag_token or bart, if none specified, the type is inferred from the' ' model_name_or_path' ) , ) parser.add_argument( '--index_name' , default=__UpperCamelCase , choices=['exact', 'compressed', 'legacy'] , type=__UpperCamelCase , help='RAG model retriever type' , ) parser.add_argument( '--index_path' , default=__UpperCamelCase , type=__UpperCamelCase , help='Path to the retrieval index' , ) parser.add_argument('--n_docs' , default=5 , type=__UpperCamelCase , help='Number of retrieved docs' ) parser.add_argument( '--model_name_or_path' , default=__UpperCamelCase , type=__UpperCamelCase , required=__UpperCamelCase , help='Path to pretrained checkpoints or model identifier from huggingface.co/models' , ) parser.add_argument( '--eval_mode' , choices=['e2e', 'retrieval'] , default='e2e' , type=__UpperCamelCase , help=( 'Evaluation mode, e2e calculates exact match and F1 of the downstream task, retrieval calculates' ' precision@k.' ) , ) parser.add_argument('--k' , default=1 , type=__UpperCamelCase , help='k for the precision@k calculation' ) parser.add_argument( '--evaluation_set' , default=__UpperCamelCase , type=__UpperCamelCase , required=__UpperCamelCase , help='Path to a file containing evaluation samples' , ) parser.add_argument( '--gold_data_path' , default=__UpperCamelCase , type=__UpperCamelCase , required=__UpperCamelCase , help='Path to a tab-separated file with gold samples' , ) parser.add_argument( '--gold_data_mode' , default='qa' , type=__UpperCamelCase , choices=['qa', 'ans'] , help=( 'Format of the gold data file' 'qa - a single line in the following format: question [tab] answer_list' 'ans - a single line of the gold file contains the expected answer string' ) , ) parser.add_argument( '--predictions_path' , type=__UpperCamelCase , default='predictions.txt' , help='Name of the predictions file, to be stored in the checkpoints directory' , ) parser.add_argument( '--eval_all_checkpoints' , action='store_true' , help='Evaluate all checkpoints starting with the same prefix as model_name ending and ending with step number' , ) parser.add_argument( '--eval_batch_size' , default=8 , type=__UpperCamelCase , help='Batch size per GPU/CPU for evaluation.' , ) parser.add_argument( '--recalculate' , help='Recalculate predictions even if the prediction file exists' , action='store_true' , ) parser.add_argument( '--num_beams' , default=4 , type=__UpperCamelCase , help='Number of beams to be used when generating answers' , ) parser.add_argument('--min_length' , default=1 , type=__UpperCamelCase , help='Min length of the generated answers' ) parser.add_argument('--max_length' , default=50 , type=__UpperCamelCase , help='Max length of the generated answers' ) parser.add_argument( '--print_predictions' , action='store_true' , help='If True, prints predictions while evaluating.' , ) parser.add_argument( '--print_docs' , action='store_true' , help='If True, prints docs retried while generating.' , ) A__ = parser.parse_args() A__ = torch.device('cuda' if torch.cuda.is_available() else 'cpu' ) return args def A ( __UpperCamelCase ) -> int: A__ = {} if args.model_type is None: A__ = infer_model_type(args.model_name_or_path ) assert args.model_type is not None if args.model_type.startswith('rag' ): A__ = RagTokenForGeneration if args.model_type == 'rag_token' else RagSequenceForGeneration A__ = args.n_docs if args.index_name is not None: A__ = args.index_name if args.index_path is not None: A__ = args.index_path else: A__ = BartForConditionalGeneration A__ = ( [f.path for f in os.scandir(args.model_name_or_path ) if f.is_dir()] if args.eval_all_checkpoints else [args.model_name_or_path] ) logger.info('Evaluate the following checkpoints: %s' , __UpperCamelCase ) A__ = get_scores if args.eval_mode == 'e2e' else get_precision_at_k A__ = evaluate_batch_eae if args.eval_mode == 'e2e' else evaluate_batch_retrieval for checkpoint in checkpoints: if os.path.exists(args.predictions_path ) and (not args.recalculate): logger.info('Calculating metrics based on an existing predictions file: {}'.format(args.predictions_path ) ) score_fn(__UpperCamelCase , args.predictions_path , args.gold_data_path ) continue logger.info('***** Running evaluation for {} *****'.format(__UpperCamelCase ) ) logger.info(' Batch size = %d' , args.eval_batch_size ) logger.info(' Predictions will be stored under {}'.format(args.predictions_path ) ) if args.model_type.startswith('rag' ): A__ = RagRetriever.from_pretrained(__UpperCamelCase , **__UpperCamelCase ) A__ = model_class.from_pretrained(__UpperCamelCase , retriever=__UpperCamelCase , **__UpperCamelCase ) model.retriever.init_retrieval() else: A__ = model_class.from_pretrained(__UpperCamelCase , **__UpperCamelCase ) model.to(args.device ) with open(args.evaluation_set , 'r' ) as eval_file, open(args.predictions_path , 'w' ) as preds_file: A__ = [] for line in tqdm(__UpperCamelCase ): questions.append(line.strip() ) if len(__UpperCamelCase ) == args.eval_batch_size: A__ = evaluate_batch_fn(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) preds_file.write('\n'.join(__UpperCamelCase ) + '\n' ) preds_file.flush() A__ = [] if len(__UpperCamelCase ) > 0: A__ = evaluate_batch_fn(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) preds_file.write('\n'.join(__UpperCamelCase ) ) preds_file.flush() score_fn(__UpperCamelCase , args.predictions_path , args.gold_data_path ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = get_args() main(args)
52
1
import json import os from functools import lru_cache from typing import TYPE_CHECKING, List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = { '''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_config_file''': '''tokenizer_config.json''', } SCREAMING_SNAKE_CASE__ = { '''vocab_file''': {'''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/vocab.json'''}, '''merges_file''': {'''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/merges.txt'''}, '''tokenizer_config_file''': { '''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/tokenizer_config.json''' }, } SCREAMING_SNAKE_CASE__ = {'''facebook/blenderbot-3B''': 1_2_8} @lru_cache() # Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode def A ( ) -> int: A__ = ( list(range(ord('!' ) , ord('~' ) + 1 ) ) + list(range(ord('¡' ) , ord('¬' ) + 1 ) ) + list(range(ord('®' ) , ord('ÿ' ) + 1 ) ) ) A__ = bs[:] A__ = 0 for b in range(2**8 ): if b not in bs: bs.append(__UpperCamelCase ) cs.append(2**8 + n ) n += 1 A__ = [chr(__UpperCamelCase ) for n in cs] return dict(zip(__UpperCamelCase , __UpperCamelCase ) ) def A ( __UpperCamelCase ) -> Union[str, Any]: A__ = set() A__ = word[0] for char in word[1:]: pairs.add((prev_char, char) ) A__ = char return pairs class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Union[str, Any] = VOCAB_FILES_NAMES A__ : Dict = PRETRAINED_VOCAB_FILES_MAP A__ : Any = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A__ : List[str] = ["input_ids", "attention_mask"] def __init__( self : Tuple , _snake_case : str , _snake_case : Tuple , _snake_case : int="replace" , _snake_case : List[str]="<s>" , _snake_case : List[Any]="</s>" , _snake_case : Optional[Any]="</s>" , _snake_case : List[str]="<s>" , _snake_case : int="<unk>" , _snake_case : List[str]="<pad>" , _snake_case : int="<mask>" , _snake_case : Optional[int]=False , **_snake_case : int , ): """simple docstring""" A__ = AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case ) if isinstance(_snake_case , _snake_case ) else bos_token A__ = AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case ) if isinstance(_snake_case , _snake_case ) else eos_token A__ = AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case ) if isinstance(_snake_case , _snake_case ) else sep_token A__ = AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case ) if isinstance(_snake_case , _snake_case ) else cls_token A__ = AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case ) if isinstance(_snake_case , _snake_case ) else unk_token A__ = AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case ) if isinstance(_snake_case , _snake_case ) else pad_token # Mask token behave like a normal word, i.e. include the space before it A__ = AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case ) if isinstance(_snake_case , _snake_case ) else mask_token super().__init__( errors=_snake_case , bos_token=_snake_case , eos_token=_snake_case , unk_token=_snake_case , sep_token=_snake_case , cls_token=_snake_case , pad_token=_snake_case , mask_token=_snake_case , add_prefix_space=_snake_case , **_snake_case , ) with open(_snake_case , encoding='utf-8' ) as vocab_handle: A__ = json.load(_snake_case ) A__ = {v: k for k, v in self.encoder.items()} A__ = errors # how to handle errors in decoding A__ = bytes_to_unicode() A__ = {v: k for k, v in self.byte_encoder.items()} with open(_snake_case , encoding='utf-8' ) as merges_handle: A__ = merges_handle.read().split('\n' )[1:-1] A__ = [tuple(merge.split() ) for merge in bpe_merges] A__ = dict(zip(_snake_case , range(len(_snake_case ) ) ) ) A__ = {} A__ = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions A__ = re.compile(R'\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+' ) @property # Copied from transformers.models.roberta.tokenization_roberta.RobertaTokenizer.vocab_size with Roberta->Blenderbot, RoBERTa->Blenderbot def _a ( self : int ): """simple docstring""" return len(self.encoder ) def _a ( self : List[Any] ): """simple docstring""" return dict(self.encoder , **self.added_tokens_encoder ) def _a ( self : Optional[Any] , _snake_case : Any ): """simple docstring""" if token in self.cache: return self.cache[token] A__ = tuple(_snake_case ) A__ = get_pairs(_snake_case ) if not pairs: return token while True: A__ = min(_snake_case , key=lambda _snake_case : self.bpe_ranks.get(_snake_case , float('inf' ) ) ) if bigram not in self.bpe_ranks: break A__ , A__ = bigram A__ = [] A__ = 0 while i < len(_snake_case ): try: A__ = word.index(_snake_case , _snake_case ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) A__ = j if word[i] == first and i < len(_snake_case ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 A__ = tuple(_snake_case ) A__ = new_word if len(_snake_case ) == 1: break else: A__ = get_pairs(_snake_case ) A__ = ' '.join(_snake_case ) A__ = word return word def _a ( self : int , _snake_case : Dict ): """simple docstring""" A__ = [] for token in re.findall(self.pat , _snake_case ): A__ = ''.join( self.byte_encoder[b] for b in token.encode('utf-8' ) ) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(_snake_case ).split(' ' ) ) return bpe_tokens def _a ( self : Any , _snake_case : List[str] ): """simple docstring""" return self.encoder.get(_snake_case , self.encoder.get(self.unk_token ) ) def _a ( self : str , _snake_case : Optional[Any] ): """simple docstring""" return self.decoder.get(_snake_case ) def _a ( self : Optional[int] , _snake_case : List[str] ): """simple docstring""" A__ = ''.join(_snake_case ) A__ = bytearray([self.byte_decoder[c] for c in text] ).decode('utf-8' , errors=self.errors ) return text def _a ( self : List[str] , _snake_case : str , _snake_case : Optional[str] = None ): """simple docstring""" if not os.path.isdir(_snake_case ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return A__ = os.path.join( _snake_case , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) A__ = os.path.join( _snake_case , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['merges_file'] ) with open(_snake_case , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=_snake_case , ensure_ascii=_snake_case ) + '\n' ) A__ = 0 with open(_snake_case , 'w' , encoding='utf-8' ) as writer: writer.write('#version: 0.2\n' ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda _snake_case : kv[1] ): if index != token_index: logger.warning( F'''Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.''' ' Please check that the tokenizer is not corrupted!' ) A__ = token_index writer.write(' '.join(_snake_case ) + '\n' ) index += 1 return vocab_file, merge_file def _a ( self : Union[str, Any] , _snake_case : List[int] , _snake_case : Optional[List[int]] = None , _snake_case : bool = False ): """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_snake_case , token_ids_a=_snake_case , already_has_special_tokens=_snake_case ) if token_ids_a is None: return [1] + ([0] * len(_snake_case )) + [1] return [1] + ([0] * len(_snake_case )) + [1, 1] + ([0] * len(_snake_case )) + [1] def _a ( self : str , _snake_case : List[int] , _snake_case : Optional[List[int]] = None ): """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def _a ( self : Optional[int] , _snake_case : Optional[Any] , _snake_case : str=False , **_snake_case : int ): """simple docstring""" A__ = kwargs.pop('add_prefix_space' , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(_snake_case ) > 0 and not text[0].isspace()): A__ = ' ' + text return (text, kwargs) def _a ( self : str , _snake_case : List[int] , _snake_case : Optional[List[int]] = None ): """simple docstring""" return token_ids_a + [self.eos_token_id] def _a ( self : List[Any] , _snake_case : "Conversation" ): """simple docstring""" A__ = [] for is_user, text in conversation.iter_texts(): if is_user: # We need to space prefix as it's being done within blenderbot inputs.append(' ' + text ) else: # Generated responses should contain them already. inputs.append(_snake_case ) A__ = ' '.join(_snake_case ) A__ = self.encode(_snake_case ) if len(_snake_case ) > self.model_max_length: A__ = input_ids[-self.model_max_length :] logger.warning(F'''Trimmed input from conversation as it was longer than {self.model_max_length} tokens.''' ) return input_ids
52
import inspect import unittest from transformers import ViTHybridConfig from transformers.testing_utils import require_accelerate, require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class __lowerCAmelCase : """simple docstring""" def __init__( self : List[Any] , _snake_case : Any , _snake_case : Optional[int]=13 , _snake_case : Optional[Any]=64 , _snake_case : List[str]=2 , _snake_case : Any=3 , _snake_case : Union[str, Any]=True , _snake_case : Dict=True , _snake_case : int=32 , _snake_case : int=5 , _snake_case : Union[str, Any]=4 , _snake_case : int=37 , _snake_case : Tuple="gelu" , _snake_case : Optional[int]=0.1 , _snake_case : Dict=0.1 , _snake_case : List[str]=10 , _snake_case : Union[str, Any]=0.02 , _snake_case : Dict=[1, 16, 4, 4] , _snake_case : Dict=None , ): """simple docstring""" A__ = parent A__ = batch_size A__ = image_size A__ = patch_size A__ = num_channels A__ = is_training A__ = use_labels A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = type_sequence_label_size A__ = initializer_range A__ = scope A__ = backbone_featmap_shape # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) # the number of patches is based on the feature map of the backbone, which by default uses an output stride # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size A__ = (self.image_size // 32) ** 2 A__ = num_patches + 1 def _a ( self : Any ): """simple docstring""" A__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A__ = self.get_config() return config, pixel_values, labels def _a ( self : Tuple ): """simple docstring""" A__ = { 'global_padding': 'same', 'layer_type': 'bottleneck', 'depths': [3, 4, 9], 'out_features': ['stage1', 'stage2', 'stage3'], 'embedding_dynamic_padding': True, 'hidden_sizes': [4, 8, 16, 32], 'num_groups': 2, } return ViTHybridConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_snake_case , initializer_range=self.initializer_range , backbone_featmap_shape=self.backbone_featmap_shape , backbone_config=_snake_case , ) def _a ( self : int , _snake_case : Optional[int] , _snake_case : Union[str, Any] , _snake_case : Optional[int] ): """simple docstring""" A__ = ViTHybridModel(config=_snake_case ) model.to(_snake_case ) model.eval() A__ = model(_snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _a ( self : List[str] , _snake_case : str , _snake_case : Union[str, Any] , _snake_case : Any ): """simple docstring""" A__ = self.type_sequence_label_size A__ = ViTHybridForImageClassification(_snake_case ) model.to(_snake_case ) model.eval() A__ = model(_snake_case , labels=_snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _a ( self : Dict ): """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ = config_and_inputs A__ = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class __lowerCAmelCase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Union[str, Any] = (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else () A__ : str = ( {"feature-extraction": ViTHybridModel, "image-classification": ViTHybridForImageClassification} if is_torch_available() else {} ) A__ : Union[str, Any] = False A__ : Any = False A__ : Union[str, Any] = False def _a ( self : Dict ): """simple docstring""" A__ = ViTHybridModelTester(self ) A__ = ConfigTester(self , config_class=_snake_case , has_text_modality=_snake_case , hidden_size=37 ) def _a ( self : int ): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='ViT does not use inputs_embeds' ) def _a ( self : int ): """simple docstring""" pass def _a ( self : int ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(_snake_case ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A__ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_snake_case , nn.Linear ) ) def _a ( self : List[str] ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(_snake_case ) A__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ = [*signature.parameters.keys()] A__ = ['pixel_values'] self.assertListEqual(arg_names[:1] , _snake_case ) def _a ( self : Any ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def _a ( self : str ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_snake_case ) def _a ( self : Any ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = _config_zero_init(_snake_case ) for model_class in self.all_model_classes: A__ = model_class(config=_snake_case ) # Skip the check for the backbone for name, module in model.named_modules(): if module.__class__.__name__ == "ViTHybridPatchEmbeddings": A__ = [F'''{name}.{key}''' for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() , [0.0, 1.0] , msg=F'''Parameter {name} of model {model_class} seems not properly initialized''' , ) @slow def _a ( self : int ): """simple docstring""" for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = ViTHybridModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def A ( ) -> Union[str, Any]: A__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @cached_property def _a ( self : Tuple ): """simple docstring""" return ( ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def _a ( self : Optional[Any] ): """simple docstring""" A__ = ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to( _snake_case ) A__ = self.default_image_processor A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ).to(_snake_case ) # forward pass with torch.no_grad(): A__ = model(**_snake_case ) # verify the logits A__ = torch.Size((1, 10_00) ) self.assertEqual(outputs.logits.shape , _snake_case ) A__ = torch.tensor([-1.9090, -0.4993, -0.2389] ).to(_snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _snake_case , atol=1E-4 ) ) @slow @require_accelerate def _a ( self : List[Any] ): """simple docstring""" A__ = ViTHybridImageProcessor.from_pretrained('google/vit-hybrid-base-bit-384' ) A__ = ViTHybridForImageClassification.from_pretrained('google/vit-hybrid-base-bit-384' , device_map='auto' ) A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ) A__ = model(**_snake_case ) A__ = outputs.logits # model predicts one of the 1000 ImageNet classes A__ = logits.argmax(-1 ).item() self.assertTrue(model.config.idalabel[predicted_class_idx] , 'tabby, tabby cat' )
52
1
from PIL import Image def A ( __UpperCamelCase ) -> Image: A__ , A__ = image.size A__ = 0 A__ = image.load() for i in range(__UpperCamelCase ): for j in range(__UpperCamelCase ): A__ = pixels[j, i] mean += pixel mean //= width * height for j in range(__UpperCamelCase ): for i in range(__UpperCamelCase ): A__ = 255 if pixels[i, j] > mean else 0 return image if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = mean_threshold(Image.open('''path_to_image''').convert('''L''')) image.save('''output_image_path''')
52
def A ( __UpperCamelCase ) -> bool: return number & 1 == 0 if __name__ == "__main__": import doctest doctest.testmod()
52
1
import logging from dataclasses import dataclass, field from typing import Optional from seqaseq_trainer import arg_to_scheduler from transformers import TrainingArguments SCREAMING_SNAKE_CASE__ = logging.getLogger(__name__) @dataclass class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Optional[float] = field( default=0.0 , metadata={"help": "The label smoothing epsilon to apply (if not zero)."} ) A__ : bool = field(default=UpperCAmelCase_ , metadata={"help": "Whether to SortishSamler or not."} ) A__ : bool = field( default=UpperCAmelCase_ , metadata={"help": "Whether to use generate to calculate generative metrics (ROUGE, BLEU)."} ) A__ : bool = field(default=UpperCAmelCase_ , metadata={"help": "whether to use adafactor"} ) A__ : Optional[float] = field( default=UpperCAmelCase_ , metadata={"help": "Encoder layer dropout probability. Goes into model.config."} ) A__ : Optional[float] = field( default=UpperCAmelCase_ , metadata={"help": "Decoder layer dropout probability. Goes into model.config."} ) A__ : Optional[float] = field(default=UpperCAmelCase_ , metadata={"help": "Dropout probability. Goes into model.config."} ) A__ : Optional[float] = field( default=UpperCAmelCase_ , metadata={"help": "Attention dropout probability. Goes into model.config."} ) A__ : Optional[str] = field( default="linear" , metadata={"help": f"""Which lr scheduler to use. Selected in {sorted(arg_to_scheduler.keys() )}"""} , )
52
from typing import Dict from .base import GenericTensor, Pipeline class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def _a ( self : Any , _snake_case : str=None , _snake_case : Dict=None , _snake_case : Any=None , **_snake_case : str ): """simple docstring""" if tokenize_kwargs is None: A__ = {} if truncation is not None: if "truncation" in tokenize_kwargs: raise ValueError( 'truncation parameter defined twice (given as keyword argument as well as in tokenize_kwargs)' ) A__ = truncation A__ = tokenize_kwargs A__ = {} if return_tensors is not None: A__ = return_tensors return preprocess_params, {}, postprocess_params def _a ( self : Any , _snake_case : Dict , **_snake_case : Optional[Any] ): """simple docstring""" A__ = self.framework A__ = self.tokenizer(_snake_case , return_tensors=_snake_case , **_snake_case ) return model_inputs def _a ( self : List[Any] , _snake_case : Dict ): """simple docstring""" A__ = self.model(**_snake_case ) return model_outputs def _a ( self : Optional[Any] , _snake_case : List[Any] , _snake_case : str=False ): """simple docstring""" if return_tensors: return model_outputs[0] if self.framework == "pt": return model_outputs[0].tolist() elif self.framework == "tf": return model_outputs[0].numpy().tolist() def __call__( self : Dict , *_snake_case : int , **_snake_case : List[str] ): """simple docstring""" return super().__call__(*_snake_case , **_snake_case )
52
1
import inspect import jax import jax.lax as lax import jax.numpy as jnp from ..utils import add_start_docstrings from ..utils.logging import get_logger SCREAMING_SNAKE_CASE__ = get_logger(__name__) SCREAMING_SNAKE_CASE__ = r''' Args: input_ids (`jnp.ndarray` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`PreTrainedTokenizer`]. See [`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`] for details. [What are input IDs?](../glossary#input-ids) scores (`jnp.ndarray` of shape `(batch_size, config.vocab_size)`): Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search kwargs (`Dict[str, Any]`, *optional*): Additional logits processor specific kwargs. Return: `jnp.ndarray` of shape `(batch_size, config.vocab_size)`: The processed prediction scores. ''' class __lowerCAmelCase : """simple docstring""" @add_start_docstrings(_snake_case ) def __call__( self : Optional[int] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray ): """simple docstring""" raise NotImplementedError( F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''' ) class __lowerCAmelCase : """simple docstring""" @add_start_docstrings(_snake_case ) def __call__( self : List[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray ): """simple docstring""" raise NotImplementedError( F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''' ) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" @add_start_docstrings(_snake_case ) def __call__( self : Any , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int , **_snake_case : Optional[int] ): """simple docstring""" for processor in self: A__ = inspect.signature(processor.__call__ ).parameters if len(_snake_case ) > 3: if not all(arg in kwargs for arg in list(function_args.keys() )[2:] ): raise ValueError( F'''Make sure that all the required parameters: {list(function_args.keys() )} for ''' F'''{processor.__class__} are passed to the logits processor.''' ) A__ = processor(_snake_case , _snake_case , _snake_case , **_snake_case ) else: A__ = processor(_snake_case , _snake_case , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Any , _snake_case : float ): """simple docstring""" if not isinstance(_snake_case , _snake_case ) or not (temperature > 0): raise ValueError(F'''`temperature` has to be a strictly positive float, but is {temperature}''' ) A__ = temperature def __call__( self : str , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = scores / self.temperature return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Optional[Any] , _snake_case : float , _snake_case : float = -float('Inf' ) , _snake_case : int = 1 ): """simple docstring""" if not isinstance(_snake_case , _snake_case ) or (top_p < 0 or top_p > 1.0): raise ValueError(F'''`top_p` has to be a float > 0 and < 1, but is {top_p}''' ) if not isinstance(_snake_case , _snake_case ) or (min_tokens_to_keep < 1): raise ValueError(F'''`min_tokens_to_keep` has to be a positive integer, but is {min_tokens_to_keep}''' ) A__ = top_p A__ = filter_value A__ = min_tokens_to_keep def __call__( self : str , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ , A__ = lax.top_k(_snake_case , scores.shape[-1] ) A__ = jnp.full_like(_snake_case , self.filter_value ) A__ = jax.nn.softmax(_snake_case , axis=-1 ).cumsum(axis=-1 ) A__ = cumulative_probs < self.top_p # include the token that is higher than top_p as well A__ = jnp.roll(_snake_case , 1 ) score_mask |= score_mask.at[:, 0].set(_snake_case ) # min tokens to keep A__ = score_mask.at[:, : self.min_tokens_to_keep].set(_snake_case ) A__ = jnp.where(_snake_case , _snake_case , _snake_case ) A__ = jax.lax.sort_key_val(_snake_case , _snake_case )[-1] return next_scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Union[str, Any] , _snake_case : int , _snake_case : float = -float('Inf' ) , _snake_case : int = 1 ): """simple docstring""" if not isinstance(_snake_case , _snake_case ) or top_k <= 0: raise ValueError(F'''`top_k` has to be a strictly positive integer, but is {top_k}''' ) A__ = max(_snake_case , _snake_case ) A__ = filter_value def __call__( self : Optional[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ , A__ = scores.shape A__ = jnp.full(batch_size * vocab_size , self.filter_value ) A__ = min(self.top_k , scores.shape[-1] ) # Safety check A__ , A__ = lax.top_k(_snake_case , _snake_case ) A__ = jnp.broadcast_to((jnp.arange(_snake_case ) * vocab_size)[:, None] , (batch_size, topk) ).flatten() A__ = topk_scores.flatten() A__ = topk_indices.flatten() + shift A__ = next_scores_flat.at[topk_indices_flat].set(_snake_case ) A__ = next_scores_flat.reshape(_snake_case , _snake_case ) return next_scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Any , _snake_case : int ): """simple docstring""" A__ = bos_token_id def __call__( self : Optional[int] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = jnp.full(scores.shape , -float('inf' ) ) A__ = 1 - jnp.bool_(cur_len - 1 ) A__ = jnp.where(_snake_case , new_scores.at[:, self.bos_token_id].set(0 ) , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Any , _snake_case : int , _snake_case : int ): """simple docstring""" A__ = max_length A__ = eos_token_id def __call__( self : List[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = jnp.full(scores.shape , -float('inf' ) ) A__ = 1 - jnp.bool_(cur_len - self.max_length + 1 ) A__ = jnp.where(_snake_case , new_scores.at[:, self.eos_token_id].set(0 ) , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Dict , _snake_case : int , _snake_case : int ): """simple docstring""" if not isinstance(_snake_case , _snake_case ) or min_length < 0: raise ValueError(F'''`min_length` has to be a positive integer, but is {min_length}''' ) if not isinstance(_snake_case , _snake_case ) or eos_token_id < 0: raise ValueError(F'''`eos_token_id` has to be a positive integer, but is {eos_token_id}''' ) A__ = min_length A__ = eos_token_id def __call__( self : int , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = 1 - jnp.clip(cur_len - self.min_length , 0 , 1 ) A__ = jnp.where(_snake_case , scores.at[:, self.eos_token_id].set(-float('inf' ) ) , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : int , _snake_case : Tuple , _snake_case : Union[str, Any] ): """simple docstring""" A__ = list(_snake_case ) A__ = begin_index def __call__( self : Union[str, Any] , _snake_case : Optional[Any] , _snake_case : str , _snake_case : int ): """simple docstring""" A__ = 1 - jnp.bool_(cur_len - self.begin_index ) A__ = jnp.where(_snake_case , scores.at[:, self.begin_suppress_tokens].set(-float('inf' ) ) , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : int , _snake_case : list ): """simple docstring""" A__ = list(_snake_case ) def __call__( self : List[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = scores.at[..., self.suppress_tokens].set(-float('inf' ) ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : List[str] , _snake_case : Optional[Any] ): """simple docstring""" A__ = dict(_snake_case ) # Converts the dictionary of format {index: token} containing the tokens to be forced to an array, where the # index of the array corresponds to the index of the token to be forced, for XLA compatibility. # Indexes without forced tokens will have a negative value. A__ = jnp.ones((max(force_token_map.keys() ) + 1) , dtype=jnp.intaa ) * -1 for index, token in force_token_map.items(): if token is not None: A__ = force_token_array.at[index].set(_snake_case ) A__ = jnp.intaa(_snake_case ) def __call__( self : List[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" def _force_token(_snake_case : Dict ): A__ = scores.shape[0] A__ = self.force_token_array[generation_idx] A__ = jnp.ones_like(_snake_case , dtype=scores.dtype ) * -float('inf' ) A__ = jnp.zeros((batch_size, 1) , dtype=scores.dtype ) A__ = lax.dynamic_update_slice(_snake_case , _snake_case , (0, current_token) ) return new_scores A__ = lax.cond( cur_len >= self.force_token_array.shape[0] , lambda: scores , lambda: lax.cond( self.force_token_array[cur_len] >= 0 , lambda: _force_token(_snake_case ) , lambda: scores , ) , ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Union[str, Any] , _snake_case : Union[str, Any] , _snake_case : Dict , _snake_case : List[Any] ): """simple docstring""" A__ = generate_config.eos_token_id A__ = generate_config.no_timestamps_token_id A__ = generate_config.no_timestamps_token_id + 1 A__ = decoder_input_length + 1 if generate_config.is_multilingual: # room for language token and task token self.begin_index += 2 if hasattr(_snake_case , 'max_initial_timestamp_index' ): A__ = generate_config.max_initial_timestamp_index else: A__ = model_config.vocab_size if self.max_initial_timestamp_index is None: A__ = model_config.vocab_size def __call__( self : Tuple , _snake_case : List[Any] , _snake_case : Dict , _snake_case : Dict ): """simple docstring""" A__ = scores.at[:, self.no_timestamps_token_id].set(-float('inf' ) ) def handle_pairs(_snake_case : Dict , _snake_case : str ): A__ = jnp.where((cur_len - self.begin_index) >= 1 , _snake_case , _snake_case ) A__ = jnp.where( input_ids_k[cur_len - 1] >= self.timestamp_begin , True and last_was_timestamp , _snake_case , ) A__ = jnp.where((cur_len - self.begin_index) < 2 , _snake_case , _snake_case ) A__ = jnp.where( input_ids_k[cur_len - 2] >= self.timestamp_begin , _snake_case , _snake_case , ) return jnp.where( _snake_case , jnp.where( penultimate_was_timestamp > 0 , scores_k.at[self.timestamp_begin :].set(-float('inf' ) ) , scores_k.at[: self.eos_token_id].set(-float('inf' ) ) , ) , _snake_case , ) A__ = jax.vmap(_snake_case )(_snake_case , _snake_case ) A__ = jnp.where(cur_len == self.begin_index , _snake_case , _snake_case ) A__ = jnp.where( self.max_initial_timestamp_index is not None , True and apply_max_initial_timestamp , _snake_case , ) A__ = self.timestamp_begin + self.max_initial_timestamp_index A__ = jnp.where( _snake_case , scores.at[:, last_allowed + 1 :].set(-float('inf' ) ) , _snake_case , ) # if sum of probability over timestamps is above any other token, sample timestamp A__ = jax.nn.log_softmax(_snake_case , axis=-1 ) def handle_cumulative_probs(_snake_case : List[Any] , _snake_case : Union[str, Any] ): A__ = jax.nn.logsumexp(logprobs_k[self.timestamp_begin :] , axis=-1 ) A__ = jnp.max(logprobs_k[: self.timestamp_begin] ) return jnp.where( timestamp_logprob > max_text_token_logprob , scores_k.at[: self.timestamp_begin].set(-float('inf' ) ) , _snake_case , ) A__ = jax.vmap(_snake_case )(_snake_case , _snake_case ) return scores
52
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import numpy as np from utils_multiple_choice import MultipleChoiceDataset, Split, processors import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process SCREAMING_SNAKE_CASE__ = logging.getLogger(__name__) def A ( __UpperCamelCase , __UpperCamelCase ) -> List[Any]: return (preds == labels).mean() @dataclass class __lowerCAmelCase : """simple docstring""" A__ : str = field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "Pretrained config name or path if not the same as model_name"} ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"} ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) @dataclass class __lowerCAmelCase : """simple docstring""" A__ : str = field(metadata={"help": "The name of the task to train on: " + ", ".join(processors.keys() )} ) A__ : str = field(metadata={"help": "Should contain the data files for the task."} ) A__ : int = field( default=1_28 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) A__ : bool = field( default=UpperCAmelCase_ , metadata={"help": "Overwrite the cached training and evaluation sets"} ) def A ( ) -> Any: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. A__ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) A__ , A__ , A__ = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f'''Output directory ({training_args.output_dir}) already exists and is not empty. Use''' ' --overwrite_output_dir to overcome.' ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( 'Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s' , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.local_rank != -1 ) , training_args.fpaa , ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('Training/evaluation parameters %s' , __UpperCamelCase ) # Set seed set_seed(training_args.seed ) try: A__ = processors[data_args.task_name]() A__ = processor.get_labels() A__ = len(__UpperCamelCase ) except KeyError: raise ValueError('Task not found: %s' % (data_args.task_name) ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. A__ = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=__UpperCamelCase , finetuning_task=data_args.task_name , cache_dir=model_args.cache_dir , ) A__ = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) A__ = AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path , from_tf=bool('.ckpt' in model_args.model_name_or_path ) , config=__UpperCamelCase , cache_dir=model_args.cache_dir , ) # Get datasets A__ = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=__UpperCamelCase , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.train , ) if training_args.do_train else None ) A__ = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=__UpperCamelCase , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.dev , ) if training_args.do_eval else None ) def compute_metrics(__UpperCamelCase ) -> Dict: A__ = np.argmax(p.predictions , axis=1 ) return {"acc": simple_accuracy(__UpperCamelCase , p.label_ids )} # Data collator A__ = DataCollatorWithPadding(__UpperCamelCase , pad_to_multiple_of=8 ) if training_args.fpaa else None # Initialize our Trainer A__ = Trainer( model=__UpperCamelCase , args=__UpperCamelCase , train_dataset=__UpperCamelCase , eval_dataset=__UpperCamelCase , compute_metrics=__UpperCamelCase , data_collator=__UpperCamelCase , ) # Training if training_args.do_train: trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir ) # Evaluation A__ = {} if training_args.do_eval: logger.info('*** Evaluate ***' ) A__ = trainer.evaluate() A__ = os.path.join(training_args.output_dir , 'eval_results.txt' ) if trainer.is_world_master(): with open(__UpperCamelCase , 'w' ) as writer: logger.info('***** Eval results *****' ) for key, value in result.items(): logger.info(' %s = %s' , __UpperCamelCase , __UpperCamelCase ) writer.write('%s = %s\n' % (key, value) ) results.update(__UpperCamelCase ) return results def A ( __UpperCamelCase ) -> List[Any]: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
52
1
def A ( __UpperCamelCase ) -> bool: return number & 1 == 0 if __name__ == "__main__": import doctest doctest.testmod()
52
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available SCREAMING_SNAKE_CASE__ = {'''configuration_mra''': ['''MRA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MraConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''MRA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MraForMaskedLM''', '''MraForMultipleChoice''', '''MraForQuestionAnswering''', '''MraForSequenceClassification''', '''MraForTokenClassification''', '''MraLayer''', '''MraModel''', '''MraPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_mra import MRA_PRETRAINED_CONFIG_ARCHIVE_MAP, MraConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mra import ( MRA_PRETRAINED_MODEL_ARCHIVE_LIST, MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, MraLayer, MraModel, MraPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
52
1
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import numpy as np from utils_multiple_choice import MultipleChoiceDataset, Split, processors import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process SCREAMING_SNAKE_CASE__ = logging.getLogger(__name__) def A ( __UpperCamelCase , __UpperCamelCase ) -> List[Any]: return (preds == labels).mean() @dataclass class __lowerCAmelCase : """simple docstring""" A__ : str = field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "Pretrained config name or path if not the same as model_name"} ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"} ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) @dataclass class __lowerCAmelCase : """simple docstring""" A__ : str = field(metadata={"help": "The name of the task to train on: " + ", ".join(processors.keys() )} ) A__ : str = field(metadata={"help": "Should contain the data files for the task."} ) A__ : int = field( default=1_28 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) A__ : bool = field( default=UpperCAmelCase_ , metadata={"help": "Overwrite the cached training and evaluation sets"} ) def A ( ) -> Any: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. A__ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) A__ , A__ , A__ = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f'''Output directory ({training_args.output_dir}) already exists and is not empty. Use''' ' --overwrite_output_dir to overcome.' ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( 'Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s' , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.local_rank != -1 ) , training_args.fpaa , ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('Training/evaluation parameters %s' , __UpperCamelCase ) # Set seed set_seed(training_args.seed ) try: A__ = processors[data_args.task_name]() A__ = processor.get_labels() A__ = len(__UpperCamelCase ) except KeyError: raise ValueError('Task not found: %s' % (data_args.task_name) ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. A__ = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=__UpperCamelCase , finetuning_task=data_args.task_name , cache_dir=model_args.cache_dir , ) A__ = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) A__ = AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path , from_tf=bool('.ckpt' in model_args.model_name_or_path ) , config=__UpperCamelCase , cache_dir=model_args.cache_dir , ) # Get datasets A__ = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=__UpperCamelCase , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.train , ) if training_args.do_train else None ) A__ = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=__UpperCamelCase , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.dev , ) if training_args.do_eval else None ) def compute_metrics(__UpperCamelCase ) -> Dict: A__ = np.argmax(p.predictions , axis=1 ) return {"acc": simple_accuracy(__UpperCamelCase , p.label_ids )} # Data collator A__ = DataCollatorWithPadding(__UpperCamelCase , pad_to_multiple_of=8 ) if training_args.fpaa else None # Initialize our Trainer A__ = Trainer( model=__UpperCamelCase , args=__UpperCamelCase , train_dataset=__UpperCamelCase , eval_dataset=__UpperCamelCase , compute_metrics=__UpperCamelCase , data_collator=__UpperCamelCase , ) # Training if training_args.do_train: trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir ) # Evaluation A__ = {} if training_args.do_eval: logger.info('*** Evaluate ***' ) A__ = trainer.evaluate() A__ = os.path.join(training_args.output_dir , 'eval_results.txt' ) if trainer.is_world_master(): with open(__UpperCamelCase , 'w' ) as writer: logger.info('***** Eval results *****' ) for key, value in result.items(): logger.info(' %s = %s' , __UpperCamelCase , __UpperCamelCase ) writer.write('%s = %s\n' % (key, value) ) results.update(__UpperCamelCase ) return results def A ( __UpperCamelCase ) -> List[Any]: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
52
import webbrowser from sys import argv from urllib.parse import parse_qs, quote import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = '''%20'''.join(argv[1:]) if len(argv) > 1 else quote(str(input('''Search: '''))) print('''Googling.....''') SCREAMING_SNAKE_CASE__ = f'https://www.google.com/search?q={query}&num=100' SCREAMING_SNAKE_CASE__ = requests.get( url, headers={'''User-Agent''': str(UserAgent().random)}, ) try: SCREAMING_SNAKE_CASE__ = ( BeautifulSoup(res.text, '''html.parser''') .find('''div''', attrs={'''class''': '''yuRUbf'''}) .find('''a''') .get('''href''') ) except AttributeError: SCREAMING_SNAKE_CASE__ = parse_qs( BeautifulSoup(res.text, '''html.parser''') .find('''div''', attrs={'''class''': '''kCrYT'''}) .find('''a''') .get('''href''') )['''url'''][0] webbrowser.open(link)
52
1
import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers from ...tokenization_utils_base import BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = {'''tokenizer_file''': '''tokenizer.json'''} SCREAMING_SNAKE_CASE__ = { '''tokenizer_file''': { '''bigscience/tokenizer''': '''https://huggingface.co/bigscience/tokenizer/blob/main/tokenizer.json''', '''bigscience/bloom-560m''': '''https://huggingface.co/bigscience/bloom-560m/blob/main/tokenizer.json''', '''bigscience/bloom-1b1''': '''https://huggingface.co/bigscience/bloom-1b1/blob/main/tokenizer.json''', '''bigscience/bloom-1b7''': '''https://huggingface.co/bigscience/bloom-1b7/blob/main/tokenizer.json''', '''bigscience/bloom-3b''': '''https://huggingface.co/bigscience/bloom-3b/blob/main/tokenizer.json''', '''bigscience/bloom-7b1''': '''https://huggingface.co/bigscience/bloom-7b1/blob/main/tokenizer.json''', '''bigscience/bloom''': '''https://huggingface.co/bigscience/bloom/blob/main/tokenizer.json''', }, } class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Tuple = VOCAB_FILES_NAMES A__ : int = PRETRAINED_VOCAB_FILES_MAP A__ : Dict = ["input_ids", "attention_mask"] A__ : Optional[Any] = None def __init__( self : Dict , _snake_case : str=None , _snake_case : int=None , _snake_case : str=None , _snake_case : Union[str, Any]="<unk>" , _snake_case : str="<s>" , _snake_case : int="</s>" , _snake_case : Tuple="<pad>" , _snake_case : Dict=False , _snake_case : int=False , **_snake_case : str , ): """simple docstring""" super().__init__( _snake_case , _snake_case , tokenizer_file=_snake_case , unk_token=_snake_case , bos_token=_snake_case , eos_token=_snake_case , pad_token=_snake_case , add_prefix_space=_snake_case , clean_up_tokenization_spaces=_snake_case , **_snake_case , ) A__ = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('add_prefix_space' , _snake_case ) != add_prefix_space: A__ = getattr(_snake_case , pre_tok_state.pop('type' ) ) A__ = add_prefix_space A__ = pre_tok_class(**_snake_case ) A__ = add_prefix_space def _a ( self : Union[str, Any] , *_snake_case : List[Any] , **_snake_case : Union[str, Any] ): """simple docstring""" A__ = kwargs.get('is_split_into_words' , _snake_case ) if not (self.add_prefix_space or not is_split_into_words): raise Exception( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True to use it with''' ' pretokenized inputs.' ) return super()._batch_encode_plus(*_snake_case , **_snake_case ) def _a ( self : str , *_snake_case : Optional[int] , **_snake_case : Any ): """simple docstring""" A__ = kwargs.get('is_split_into_words' , _snake_case ) if not (self.add_prefix_space or not is_split_into_words): raise Exception( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True to use it with''' ' pretokenized inputs.' ) return super()._encode_plus(*_snake_case , **_snake_case ) def _a ( self : int , _snake_case : str , _snake_case : Optional[str] = None ): """simple docstring""" A__ = self._tokenizer.model.save(_snake_case , name=_snake_case ) return tuple(_snake_case ) def _a ( self : Tuple , _snake_case : "Conversation" ): """simple docstring""" A__ = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(_snake_case , add_special_tokens=_snake_case ) + [self.eos_token_id] ) if len(_snake_case ) > self.model_max_length: A__ = input_ids[-self.model_max_length :] return input_ids
52
import random import unittest import torch from diffusers import IFInpaintingPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class __lowerCAmelCase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Any = IFInpaintingPipeline A__ : Dict = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"width", "height"} A__ : List[Any] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS A__ : Dict = PipelineTesterMixin.required_optional_params - {"latents"} def _a ( self : Any ): """simple docstring""" return self._get_dummy_components() def _a ( self : Optional[int] , _snake_case : Any , _snake_case : str=0 ): """simple docstring""" if str(_snake_case ).startswith('mps' ): A__ = torch.manual_seed(_snake_case ) else: A__ = torch.Generator(device=_snake_case ).manual_seed(_snake_case ) A__ = floats_tensor((1, 3, 32, 32) , rng=random.Random(_snake_case ) ).to(_snake_case ) A__ = floats_tensor((1, 3, 32, 32) , rng=random.Random(_snake_case ) ).to(_snake_case ) A__ = { 'prompt': 'A painting of a squirrel eating a burger', 'image': image, 'mask_image': mask_image, 'generator': generator, 'num_inference_steps': 2, 'output_type': 'numpy', } return inputs @unittest.skipIf( torch_device != 'cuda' or not is_xformers_available() , reason='XFormers attention is only available with CUDA and `xformers` installed' , ) def _a ( self : Dict ): """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) def _a ( self : int ): """simple docstring""" self._test_save_load_optional_components() @unittest.skipIf(torch_device != 'cuda' , reason='float16 requires CUDA' ) def _a ( self : Optional[int] ): """simple docstring""" super().test_save_load_floataa(expected_max_diff=1E-1 ) def _a ( self : List[str] ): """simple docstring""" self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def _a ( self : Dict ): """simple docstring""" self._test_save_load_local() def _a ( self : Optional[int] ): """simple docstring""" self._test_inference_batch_single_identical( expected_max_diff=1E-2 , )
52
1
from __future__ import annotations def A ( __UpperCamelCase ) -> float: if not nums: raise ValueError('List is empty' ) return sum(__UpperCamelCase ) / len(__UpperCamelCase ) if __name__ == "__main__": import doctest doctest.testmod()
52
import inspect import jax import jax.lax as lax import jax.numpy as jnp from ..utils import add_start_docstrings from ..utils.logging import get_logger SCREAMING_SNAKE_CASE__ = get_logger(__name__) SCREAMING_SNAKE_CASE__ = r''' Args: input_ids (`jnp.ndarray` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`PreTrainedTokenizer`]. See [`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`] for details. [What are input IDs?](../glossary#input-ids) scores (`jnp.ndarray` of shape `(batch_size, config.vocab_size)`): Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search kwargs (`Dict[str, Any]`, *optional*): Additional logits processor specific kwargs. Return: `jnp.ndarray` of shape `(batch_size, config.vocab_size)`: The processed prediction scores. ''' class __lowerCAmelCase : """simple docstring""" @add_start_docstrings(_snake_case ) def __call__( self : Optional[int] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray ): """simple docstring""" raise NotImplementedError( F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''' ) class __lowerCAmelCase : """simple docstring""" @add_start_docstrings(_snake_case ) def __call__( self : List[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray ): """simple docstring""" raise NotImplementedError( F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''' ) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" @add_start_docstrings(_snake_case ) def __call__( self : Any , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int , **_snake_case : Optional[int] ): """simple docstring""" for processor in self: A__ = inspect.signature(processor.__call__ ).parameters if len(_snake_case ) > 3: if not all(arg in kwargs for arg in list(function_args.keys() )[2:] ): raise ValueError( F'''Make sure that all the required parameters: {list(function_args.keys() )} for ''' F'''{processor.__class__} are passed to the logits processor.''' ) A__ = processor(_snake_case , _snake_case , _snake_case , **_snake_case ) else: A__ = processor(_snake_case , _snake_case , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Any , _snake_case : float ): """simple docstring""" if not isinstance(_snake_case , _snake_case ) or not (temperature > 0): raise ValueError(F'''`temperature` has to be a strictly positive float, but is {temperature}''' ) A__ = temperature def __call__( self : str , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = scores / self.temperature return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Optional[Any] , _snake_case : float , _snake_case : float = -float('Inf' ) , _snake_case : int = 1 ): """simple docstring""" if not isinstance(_snake_case , _snake_case ) or (top_p < 0 or top_p > 1.0): raise ValueError(F'''`top_p` has to be a float > 0 and < 1, but is {top_p}''' ) if not isinstance(_snake_case , _snake_case ) or (min_tokens_to_keep < 1): raise ValueError(F'''`min_tokens_to_keep` has to be a positive integer, but is {min_tokens_to_keep}''' ) A__ = top_p A__ = filter_value A__ = min_tokens_to_keep def __call__( self : str , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ , A__ = lax.top_k(_snake_case , scores.shape[-1] ) A__ = jnp.full_like(_snake_case , self.filter_value ) A__ = jax.nn.softmax(_snake_case , axis=-1 ).cumsum(axis=-1 ) A__ = cumulative_probs < self.top_p # include the token that is higher than top_p as well A__ = jnp.roll(_snake_case , 1 ) score_mask |= score_mask.at[:, 0].set(_snake_case ) # min tokens to keep A__ = score_mask.at[:, : self.min_tokens_to_keep].set(_snake_case ) A__ = jnp.where(_snake_case , _snake_case , _snake_case ) A__ = jax.lax.sort_key_val(_snake_case , _snake_case )[-1] return next_scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Union[str, Any] , _snake_case : int , _snake_case : float = -float('Inf' ) , _snake_case : int = 1 ): """simple docstring""" if not isinstance(_snake_case , _snake_case ) or top_k <= 0: raise ValueError(F'''`top_k` has to be a strictly positive integer, but is {top_k}''' ) A__ = max(_snake_case , _snake_case ) A__ = filter_value def __call__( self : Optional[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ , A__ = scores.shape A__ = jnp.full(batch_size * vocab_size , self.filter_value ) A__ = min(self.top_k , scores.shape[-1] ) # Safety check A__ , A__ = lax.top_k(_snake_case , _snake_case ) A__ = jnp.broadcast_to((jnp.arange(_snake_case ) * vocab_size)[:, None] , (batch_size, topk) ).flatten() A__ = topk_scores.flatten() A__ = topk_indices.flatten() + shift A__ = next_scores_flat.at[topk_indices_flat].set(_snake_case ) A__ = next_scores_flat.reshape(_snake_case , _snake_case ) return next_scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Any , _snake_case : int ): """simple docstring""" A__ = bos_token_id def __call__( self : Optional[int] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = jnp.full(scores.shape , -float('inf' ) ) A__ = 1 - jnp.bool_(cur_len - 1 ) A__ = jnp.where(_snake_case , new_scores.at[:, self.bos_token_id].set(0 ) , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Any , _snake_case : int , _snake_case : int ): """simple docstring""" A__ = max_length A__ = eos_token_id def __call__( self : List[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = jnp.full(scores.shape , -float('inf' ) ) A__ = 1 - jnp.bool_(cur_len - self.max_length + 1 ) A__ = jnp.where(_snake_case , new_scores.at[:, self.eos_token_id].set(0 ) , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Dict , _snake_case : int , _snake_case : int ): """simple docstring""" if not isinstance(_snake_case , _snake_case ) or min_length < 0: raise ValueError(F'''`min_length` has to be a positive integer, but is {min_length}''' ) if not isinstance(_snake_case , _snake_case ) or eos_token_id < 0: raise ValueError(F'''`eos_token_id` has to be a positive integer, but is {eos_token_id}''' ) A__ = min_length A__ = eos_token_id def __call__( self : int , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = 1 - jnp.clip(cur_len - self.min_length , 0 , 1 ) A__ = jnp.where(_snake_case , scores.at[:, self.eos_token_id].set(-float('inf' ) ) , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : int , _snake_case : Tuple , _snake_case : Union[str, Any] ): """simple docstring""" A__ = list(_snake_case ) A__ = begin_index def __call__( self : Union[str, Any] , _snake_case : Optional[Any] , _snake_case : str , _snake_case : int ): """simple docstring""" A__ = 1 - jnp.bool_(cur_len - self.begin_index ) A__ = jnp.where(_snake_case , scores.at[:, self.begin_suppress_tokens].set(-float('inf' ) ) , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : int , _snake_case : list ): """simple docstring""" A__ = list(_snake_case ) def __call__( self : List[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = scores.at[..., self.suppress_tokens].set(-float('inf' ) ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : List[str] , _snake_case : Optional[Any] ): """simple docstring""" A__ = dict(_snake_case ) # Converts the dictionary of format {index: token} containing the tokens to be forced to an array, where the # index of the array corresponds to the index of the token to be forced, for XLA compatibility. # Indexes without forced tokens will have a negative value. A__ = jnp.ones((max(force_token_map.keys() ) + 1) , dtype=jnp.intaa ) * -1 for index, token in force_token_map.items(): if token is not None: A__ = force_token_array.at[index].set(_snake_case ) A__ = jnp.intaa(_snake_case ) def __call__( self : List[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" def _force_token(_snake_case : Dict ): A__ = scores.shape[0] A__ = self.force_token_array[generation_idx] A__ = jnp.ones_like(_snake_case , dtype=scores.dtype ) * -float('inf' ) A__ = jnp.zeros((batch_size, 1) , dtype=scores.dtype ) A__ = lax.dynamic_update_slice(_snake_case , _snake_case , (0, current_token) ) return new_scores A__ = lax.cond( cur_len >= self.force_token_array.shape[0] , lambda: scores , lambda: lax.cond( self.force_token_array[cur_len] >= 0 , lambda: _force_token(_snake_case ) , lambda: scores , ) , ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Union[str, Any] , _snake_case : Union[str, Any] , _snake_case : Dict , _snake_case : List[Any] ): """simple docstring""" A__ = generate_config.eos_token_id A__ = generate_config.no_timestamps_token_id A__ = generate_config.no_timestamps_token_id + 1 A__ = decoder_input_length + 1 if generate_config.is_multilingual: # room for language token and task token self.begin_index += 2 if hasattr(_snake_case , 'max_initial_timestamp_index' ): A__ = generate_config.max_initial_timestamp_index else: A__ = model_config.vocab_size if self.max_initial_timestamp_index is None: A__ = model_config.vocab_size def __call__( self : Tuple , _snake_case : List[Any] , _snake_case : Dict , _snake_case : Dict ): """simple docstring""" A__ = scores.at[:, self.no_timestamps_token_id].set(-float('inf' ) ) def handle_pairs(_snake_case : Dict , _snake_case : str ): A__ = jnp.where((cur_len - self.begin_index) >= 1 , _snake_case , _snake_case ) A__ = jnp.where( input_ids_k[cur_len - 1] >= self.timestamp_begin , True and last_was_timestamp , _snake_case , ) A__ = jnp.where((cur_len - self.begin_index) < 2 , _snake_case , _snake_case ) A__ = jnp.where( input_ids_k[cur_len - 2] >= self.timestamp_begin , _snake_case , _snake_case , ) return jnp.where( _snake_case , jnp.where( penultimate_was_timestamp > 0 , scores_k.at[self.timestamp_begin :].set(-float('inf' ) ) , scores_k.at[: self.eos_token_id].set(-float('inf' ) ) , ) , _snake_case , ) A__ = jax.vmap(_snake_case )(_snake_case , _snake_case ) A__ = jnp.where(cur_len == self.begin_index , _snake_case , _snake_case ) A__ = jnp.where( self.max_initial_timestamp_index is not None , True and apply_max_initial_timestamp , _snake_case , ) A__ = self.timestamp_begin + self.max_initial_timestamp_index A__ = jnp.where( _snake_case , scores.at[:, last_allowed + 1 :].set(-float('inf' ) ) , _snake_case , ) # if sum of probability over timestamps is above any other token, sample timestamp A__ = jax.nn.log_softmax(_snake_case , axis=-1 ) def handle_cumulative_probs(_snake_case : List[Any] , _snake_case : Union[str, Any] ): A__ = jax.nn.logsumexp(logprobs_k[self.timestamp_begin :] , axis=-1 ) A__ = jnp.max(logprobs_k[: self.timestamp_begin] ) return jnp.where( timestamp_logprob > max_text_token_logprob , scores_k.at[: self.timestamp_begin].set(-float('inf' ) ) , _snake_case , ) A__ = jax.vmap(_snake_case )(_snake_case , _snake_case ) return scores
52
1
import webbrowser from sys import argv from urllib.parse import parse_qs, quote import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = '''%20'''.join(argv[1:]) if len(argv) > 1 else quote(str(input('''Search: '''))) print('''Googling.....''') SCREAMING_SNAKE_CASE__ = f'https://www.google.com/search?q={query}&num=100' SCREAMING_SNAKE_CASE__ = requests.get( url, headers={'''User-Agent''': str(UserAgent().random)}, ) try: SCREAMING_SNAKE_CASE__ = ( BeautifulSoup(res.text, '''html.parser''') .find('''div''', attrs={'''class''': '''yuRUbf'''}) .find('''a''') .get('''href''') ) except AttributeError: SCREAMING_SNAKE_CASE__ = parse_qs( BeautifulSoup(res.text, '''html.parser''') .find('''div''', attrs={'''class''': '''kCrYT'''}) .find('''a''') .get('''href''') )['''url'''][0] webbrowser.open(link)
52
import argparse import struct import unittest class __lowerCAmelCase : """simple docstring""" def __init__( self : List[str] , _snake_case : bytes ): """simple docstring""" A__ = data # Initialize hash values A__ = [ 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19, ] # Initialize round constants A__ = [ 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA, 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967, 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070, 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3, 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2, ] A__ = self.preprocessing(self.data ) self.final_hash() @staticmethod def _a ( _snake_case : bytes ): """simple docstring""" A__ = B'\x80' + (B'\x00' * (63 - (len(_snake_case ) + 8) % 64)) A__ = struct.pack('>Q' , (len(_snake_case ) * 8) ) return data + padding + big_endian_integer def _a ( self : Optional[int] ): """simple docstring""" A__ = [ self.preprocessed_data[x : x + 64] for x in range(0 , len(self.preprocessed_data ) , 64 ) ] for block in self.blocks: # Convert the given block into a list of 4 byte integers A__ = list(struct.unpack('>16L' , _snake_case ) ) # add 48 0-ed integers words += [0] * 48 A__ , A__ , A__ , A__ , A__ , A__ , A__ , A__ = self.hashes for index in range(0 , 64 ): if index > 15: # modify the zero-ed indexes at the end of the array A__ = ( self.ror(words[index - 15] , 7 ) ^ self.ror(words[index - 15] , 18 ) ^ (words[index - 15] >> 3) ) A__ = ( self.ror(words[index - 2] , 17 ) ^ self.ror(words[index - 2] , 19 ) ^ (words[index - 2] >> 10) ) A__ = ( words[index - 16] + sa + words[index - 7] + sa ) % 0x100000000 # Compression A__ = self.ror(_snake_case , 6 ) ^ self.ror(_snake_case , 11 ) ^ self.ror(_snake_case , 25 ) A__ = (e & f) ^ ((~e & 0xFFFFFFFF) & g) A__ = ( h + sa + ch + self.round_constants[index] + words[index] ) % 0x100000000 A__ = self.ror(_snake_case , 2 ) ^ self.ror(_snake_case , 13 ) ^ self.ror(_snake_case , 22 ) A__ = (a & b) ^ (a & c) ^ (b & c) A__ = (sa + maj) % 0x100000000 A__ , A__ , A__ , A__ , A__ , A__ , A__ , A__ = ( g, f, e, ((d + tempa) % 0x100000000), c, b, a, ((tempa + tempa) % 0x100000000), ) A__ = [a, b, c, d, e, f, g, h] # Modify final values A__ = [ ((element + mutated_hash_values[index]) % 0x100000000) for index, element in enumerate(self.hashes ) ] A__ = ''.join([hex(_snake_case )[2:].zfill(8 ) for value in self.hashes] ) def _a ( self : Dict , _snake_case : int , _snake_case : int ): """simple docstring""" return 0xFFFFFFFF & (value << (32 - rotations)) | (value >> rotations) class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def _a ( self : str ): """simple docstring""" import hashlib A__ = bytes('Test String' , 'utf-8' ) self.assertEqual(SHAaaa(_snake_case ).hash , hashlib.shaaaa(_snake_case ).hexdigest() ) def A ( ) -> None: import doctest doctest.testmod() A__ = argparse.ArgumentParser() parser.add_argument( '-s' , '--string' , dest='input_string' , default='Hello World!! Welcome to Cryptography' , help='Hash the string' , ) parser.add_argument( '-f' , '--file' , dest='input_file' , help='Hash contents of a file' ) A__ = parser.parse_args() A__ = args.input_string # hash input should be a bytestring if args.input_file: with open(args.input_file , 'rb' ) as f: A__ = f.read() else: A__ = bytes(__UpperCamelCase , 'utf-8' ) print(SHAaaa(__UpperCamelCase ).hash ) if __name__ == "__main__": main()
52
1
import os import sys from contextlib import contextmanager # Windows only if os.name == "nt": import ctypes import msvcrt # noqa class __lowerCAmelCase ( ctypes.Structure ): """simple docstring""" A__ : Dict = [("size", ctypes.c_int), ("visible", ctypes.c_byte)] def A ( ) -> Dict: if os.name == "nt": A__ = CursorInfo() A__ = ctypes.windll.kernelaa.GetStdHandle(-11 ) ctypes.windll.kernelaa.GetConsoleCursorInfo(__UpperCamelCase , ctypes.byref(__UpperCamelCase ) ) A__ = False ctypes.windll.kernelaa.SetConsoleCursorInfo(__UpperCamelCase , ctypes.byref(__UpperCamelCase ) ) elif os.name == "posix": sys.stdout.write('\033[?25l' ) sys.stdout.flush() def A ( ) -> Optional[int]: if os.name == "nt": A__ = CursorInfo() A__ = ctypes.windll.kernelaa.GetStdHandle(-11 ) ctypes.windll.kernelaa.GetConsoleCursorInfo(__UpperCamelCase , ctypes.byref(__UpperCamelCase ) ) A__ = True ctypes.windll.kernelaa.SetConsoleCursorInfo(__UpperCamelCase , ctypes.byref(__UpperCamelCase ) ) elif os.name == "posix": sys.stdout.write('\033[?25h' ) sys.stdout.flush() @contextmanager def A ( ) -> int: try: hide_cursor() yield finally: show_cursor()
52
import math import random def A ( __UpperCamelCase , __UpperCamelCase = False ) -> float: if deriv: return value * (1 - value) return 1 / (1 + math.exp(-value )) # Initial Value SCREAMING_SNAKE_CASE__ = 0.02 def A ( __UpperCamelCase , __UpperCamelCase ) -> float: A__ = float(2 * (random.randint(1 , 100 )) - 1 ) for _ in range(__UpperCamelCase ): # Forward propagation A__ = sigmoid_function(INITIAL_VALUE * weight ) # How much did we miss? A__ = (expected / 100) - layer_a # Error delta A__ = layer_1_error * sigmoid_function(__UpperCamelCase , __UpperCamelCase ) # Update weight weight += INITIAL_VALUE * layer_1_delta return layer_a * 100 if __name__ == "__main__": import doctest doctest.testmod() SCREAMING_SNAKE_CASE__ = int(input('''Expected value: ''')) SCREAMING_SNAKE_CASE__ = int(input('''Number of propagations: ''')) print(forward_propagation(expected, number_propagations))
52
1
import warnings from typing import Dict import numpy as np from ..utils import ExplicitEnum, add_end_docstrings, is_tf_available, is_torch_available from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING def A ( __UpperCamelCase ) -> str: return 1.0 / (1.0 + np.exp(-_outputs )) def A ( __UpperCamelCase ) -> Any: A__ = np.max(_outputs , axis=-1 , keepdims=__UpperCamelCase ) A__ = np.exp(_outputs - maxes ) return shifted_exp / shifted_exp.sum(axis=-1 , keepdims=__UpperCamelCase ) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : List[Any] = "sigmoid" A__ : Tuple = "softmax" A__ : Any = "none" @add_end_docstrings( UpperCAmelCase_ , R"\n return_all_scores (`bool`, *optional*, defaults to `False`):\n Whether to return all prediction scores or just the one of the predicted class.\n function_to_apply (`str`, *optional*, defaults to `\"default\"`):\n The function to apply to the model outputs in order to retrieve the scores. Accepts four different values:\n\n - `\"default\"`: if the model has a single label, will apply the sigmoid function on the output. If the model\n has several labels, will apply the softmax function on the output.\n - `\"sigmoid\"`: Applies the sigmoid function on the output.\n - `\"softmax\"`: Applies the softmax function on the output.\n - `\"none\"`: Does not apply any function on the output.\n " , ) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : str = False A__ : Tuple = ClassificationFunction.NONE def __init__( self : Optional[int] , **_snake_case : List[Any] ): """simple docstring""" super().__init__(**_snake_case ) self.check_model_type( TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING if self.framework == 'tf' else MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING ) def _a ( self : Tuple , _snake_case : int=None , _snake_case : Dict=None , _snake_case : List[Any]="" , **_snake_case : Dict ): """simple docstring""" A__ = tokenizer_kwargs A__ = {} if hasattr(self.model.config , 'return_all_scores' ) and return_all_scores is None: A__ = self.model.config.return_all_scores if isinstance(_snake_case , _snake_case ) or top_k is None: A__ = top_k A__ = False elif return_all_scores is not None: warnings.warn( '`return_all_scores` is now deprecated, if want a similar functionality use `top_k=None` instead of' ' `return_all_scores=True` or `top_k=1` instead of `return_all_scores=False`.' , _snake_case , ) if return_all_scores: A__ = None else: A__ = 1 if isinstance(_snake_case , _snake_case ): A__ = ClassificationFunction[function_to_apply.upper()] if function_to_apply is not None: A__ = function_to_apply return preprocess_params, {}, postprocess_params def __call__( self : int , *_snake_case : List[str] , **_snake_case : Tuple ): """simple docstring""" A__ = super().__call__(*_snake_case , **_snake_case ) # TODO try and retrieve it in a nicer way from _sanitize_parameters. A__ = 'top_k' not in kwargs if isinstance(args[0] , _snake_case ) and _legacy: # This pipeline is odd, and return a list when single item is run return [result] else: return result def _a ( self : List[str] , _snake_case : Tuple , **_snake_case : Any ): """simple docstring""" A__ = self.framework if isinstance(_snake_case , _snake_case ): return self.tokenizer(**_snake_case , return_tensors=_snake_case , **_snake_case ) elif isinstance(_snake_case , _snake_case ) and len(_snake_case ) == 1 and isinstance(inputs[0] , _snake_case ) and len(inputs[0] ) == 2: # It used to be valid to use a list of list of list for text pairs, keeping this path for BC return self.tokenizer( text=inputs[0][0] , text_pair=inputs[0][1] , return_tensors=_snake_case , **_snake_case ) elif isinstance(_snake_case , _snake_case ): # This is likely an invalid usage of the pipeline attempting to pass text pairs. raise ValueError( 'The pipeline received invalid inputs, if you are trying to send text pairs, you can try to send a' ' dictionary `{"text": "My text", "text_pair": "My pair"}` in order to send a text pair.' ) return self.tokenizer(_snake_case , return_tensors=_snake_case , **_snake_case ) def _a ( self : Dict , _snake_case : List[Any] ): """simple docstring""" return self.model(**_snake_case ) def _a ( self : Any , _snake_case : List[Any] , _snake_case : Optional[int]=None , _snake_case : int=1 , _snake_case : Optional[Any]=True ): """simple docstring""" if function_to_apply is None: if self.model.config.problem_type == "multi_label_classification" or self.model.config.num_labels == 1: A__ = ClassificationFunction.SIGMOID elif self.model.config.problem_type == "single_label_classification" or self.model.config.num_labels > 1: A__ = ClassificationFunction.SOFTMAX elif hasattr(self.model.config , 'function_to_apply' ) and function_to_apply is None: A__ = self.model.config.function_to_apply else: A__ = ClassificationFunction.NONE A__ = model_outputs['logits'][0] A__ = outputs.numpy() if function_to_apply == ClassificationFunction.SIGMOID: A__ = sigmoid(_snake_case ) elif function_to_apply == ClassificationFunction.SOFTMAX: A__ = softmax(_snake_case ) elif function_to_apply == ClassificationFunction.NONE: A__ = outputs else: raise ValueError(F'''Unrecognized `function_to_apply` argument: {function_to_apply}''' ) if top_k == 1 and _legacy: return {"label": self.model.config.idalabel[scores.argmax().item()], "score": scores.max().item()} A__ = [ {'label': self.model.config.idalabel[i], 'score': score.item()} for i, score in enumerate(_snake_case ) ] if not _legacy: dict_scores.sort(key=lambda _snake_case : x["score"] , reverse=_snake_case ) if top_k is not None: A__ = dict_scores[:top_k] return dict_scores
52
import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @slow def _a ( self : int ): """simple docstring""" A__ = FlaxMTaForConditionalGeneration.from_pretrained('google/mt5-small' ) A__ = AutoTokenizer.from_pretrained('google/mt5-small' ) A__ = tokenizer('Hello there' , return_tensors='np' ).input_ids A__ = tokenizer('Hi I am' , return_tensors='np' ).input_ids A__ = shift_tokens_right(_snake_case , model.config.pad_token_id , model.config.decoder_start_token_id ) A__ = model(_snake_case , decoder_input_ids=_snake_case ).logits A__ = optax.softmax_cross_entropy(_snake_case , onehot(_snake_case , logits.shape[-1] ) ).mean() A__ = -(labels.shape[-1] * loss.item()) A__ = -84.9127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1E-4 )
52
1
from __future__ import annotations import time SCREAMING_SNAKE_CASE__ = list[tuple[int, int]] SCREAMING_SNAKE_CASE__ = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] SCREAMING_SNAKE_CASE__ = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right class __lowerCAmelCase : """simple docstring""" def __init__( self : Dict , _snake_case : int , _snake_case : int , _snake_case : int , _snake_case : int , _snake_case : Node | None ): """simple docstring""" A__ = pos_x A__ = pos_y A__ = (pos_y, pos_x) A__ = goal_x A__ = goal_y A__ = parent class __lowerCAmelCase : """simple docstring""" def __init__( self : Any , _snake_case : tuple[int, int] , _snake_case : tuple[int, int] ): """simple docstring""" A__ = Node(start[1] , start[0] , goal[1] , goal[0] , _snake_case ) A__ = Node(goal[1] , goal[0] , goal[1] , goal[0] , _snake_case ) A__ = [self.start] A__ = False def _a ( self : Tuple ): """simple docstring""" while self.node_queue: A__ = self.node_queue.pop(0 ) if current_node.pos == self.target.pos: A__ = True return self.retrace_path(_snake_case ) A__ = self.get_successors(_snake_case ) for node in successors: self.node_queue.append(_snake_case ) if not self.reached: return [self.start.pos] return None def _a ( self : List[str] , _snake_case : Node ): """simple docstring""" A__ = [] for action in delta: A__ = parent.pos_x + action[1] A__ = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(_snake_case ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node(_snake_case , _snake_case , self.target.pos_y , self.target.pos_x , _snake_case ) ) return successors def _a ( self : Dict , _snake_case : Node | None ): """simple docstring""" A__ = node A__ = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) A__ = current_node.parent path.reverse() return path class __lowerCAmelCase : """simple docstring""" def __init__( self : str , _snake_case : str , _snake_case : int ): """simple docstring""" A__ = BreadthFirstSearch(_snake_case , _snake_case ) A__ = BreadthFirstSearch(_snake_case , _snake_case ) A__ = False def _a ( self : List[str] ): """simple docstring""" while self.fwd_bfs.node_queue or self.bwd_bfs.node_queue: A__ = self.fwd_bfs.node_queue.pop(0 ) A__ = self.bwd_bfs.node_queue.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: A__ = True return self.retrace_bidirectional_path( _snake_case , _snake_case ) A__ = current_bwd_node A__ = current_fwd_node A__ = { self.fwd_bfs: self.fwd_bfs.get_successors(_snake_case ), self.bwd_bfs: self.bwd_bfs.get_successors(_snake_case ), } for bfs in [self.fwd_bfs, self.bwd_bfs]: for node in successors[bfs]: bfs.node_queue.append(_snake_case ) if not self.reached: return [self.fwd_bfs.start.pos] return None def _a ( self : Optional[Any] , _snake_case : Node , _snake_case : Node ): """simple docstring""" A__ = self.fwd_bfs.retrace_path(_snake_case ) A__ = self.bwd_bfs.retrace_path(_snake_case ) bwd_path.pop() bwd_path.reverse() A__ = fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] import doctest doctest.testmod() SCREAMING_SNAKE_CASE__ = (0, 0) SCREAMING_SNAKE_CASE__ = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) SCREAMING_SNAKE_CASE__ = time.time() SCREAMING_SNAKE_CASE__ = BreadthFirstSearch(init, goal) SCREAMING_SNAKE_CASE__ = bfs.search() SCREAMING_SNAKE_CASE__ = time.time() - start_bfs_time print('''Unidirectional BFS computation time : ''', bfs_time) SCREAMING_SNAKE_CASE__ = time.time() SCREAMING_SNAKE_CASE__ = BidirectionalBreadthFirstSearch(init, goal) SCREAMING_SNAKE_CASE__ = bd_bfs.search() SCREAMING_SNAKE_CASE__ = time.time() - start_bd_bfs_time print('''Bidirectional BFS computation time : ''', bd_bfs_time)
52
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/config.json''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/config.json''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/config.json''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/config.json''', '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/config.json''', '''roberta-large-openai-detector''': '''https://huggingface.co/roberta-large-openai-detector/resolve/main/config.json''', } class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : List[str] = "roberta" def __init__( self : List[str] , _snake_case : Union[str, Any]=5_02_65 , _snake_case : List[Any]=7_68 , _snake_case : List[str]=12 , _snake_case : List[str]=12 , _snake_case : Any=30_72 , _snake_case : Union[str, Any]="gelu" , _snake_case : int=0.1 , _snake_case : Union[str, Any]=0.1 , _snake_case : Tuple=5_12 , _snake_case : Union[str, Any]=2 , _snake_case : Any=0.02 , _snake_case : Any=1E-12 , _snake_case : List[Any]=1 , _snake_case : int=0 , _snake_case : Any=2 , _snake_case : Optional[Any]="absolute" , _snake_case : int=True , _snake_case : Any=None , **_snake_case : Any , ): """simple docstring""" super().__init__(pad_token_id=_snake_case , bos_token_id=_snake_case , eos_token_id=_snake_case , **_snake_case ) 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__ = use_cache A__ = classifier_dropout class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" @property def _a ( self : Dict ): """simple docstring""" if self.task == "multiple-choice": A__ = {0: 'batch', 1: 'choice', 2: 'sequence'} else: A__ = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ] )
52
1
SCREAMING_SNAKE_CASE__ = { "joule": 1.0, "kilojoule": 1_0_0_0, "megajoule": 1_0_0_0_0_0_0, "gigajoule": 1_0_0_0_0_0_0_0_0_0, "wattsecond": 1.0, "watthour": 3_6_0_0, "kilowatthour": 3_6_0_0_0_0_0, "newtonmeter": 1.0, "calorie_nutr": 4_1_8_6.8, "kilocalorie_nutr": 4_1_8_6_8_0_0.0_0, "electronvolt": 1.6_0_2_1_7_6_6_3_4e-1_9, "britishthermalunit_it": 1_0_5_5.0_5_5_8_5, "footpound": 1.355_818, } def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> float: if to_type not in ENERGY_CONVERSION or from_type not in ENERGY_CONVERSION: A__ = ( f'''Incorrect \'from_type\' or \'to_type\' value: {from_type!r}, {to_type!r}\n''' f'''Valid values are: {", ".join(__UpperCamelCase )}''' ) raise ValueError(__UpperCamelCase ) return value * ENERGY_CONVERSION[from_type] / ENERGY_CONVERSION[to_type] if __name__ == "__main__": import doctest doctest.testmod()
52
import itertools import json import os import unittest from transformers import AddedToken, LongformerTokenizer, LongformerTokenizerFast from transformers.models.longformer.tokenization_longformer import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __lowerCAmelCase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : int = LongformerTokenizer A__ : Optional[int] = True A__ : Any = LongformerTokenizerFast A__ : Dict = True def _a ( self : int ): """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt A__ = [ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', '\u0120', '\u0120l', '\u0120n', '\u0120lo', '\u0120low', 'er', '\u0120lowest', '\u0120newer', '\u0120wider', '<unk>', ] A__ = dict(zip(_snake_case , range(len(_snake_case ) ) ) ) A__ = ['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', ''] A__ = {'unk_token': '<unk>'} A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(_snake_case ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(_snake_case ) ) def _a ( self : int , **_snake_case : Union[str, Any] ): """simple docstring""" kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **_snake_case ) def _a ( self : Optional[int] , **_snake_case : List[Any] ): """simple docstring""" kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **_snake_case ) def _a ( self : Any , _snake_case : Optional[Any] ): """simple docstring""" A__ = 'lower newer' A__ = 'lower newer' return input_text, output_text def _a ( self : Any ): """simple docstring""" A__ = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) A__ = 'lower newer' A__ = ['l', 'o', 'w', 'er', '\u0120', 'n', 'e', 'w', 'er'] A__ = tokenizer.tokenize(_snake_case ) # , add_prefix_space=True) self.assertListEqual(_snake_case , _snake_case ) A__ = tokens + [tokenizer.unk_token] A__ = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(_snake_case ) , _snake_case ) def _a ( self : List[str] ): """simple docstring""" A__ = self.get_tokenizer() self.assertListEqual(tokenizer.encode('Hello world!' , add_special_tokens=_snake_case ) , [0, 3_14_14, 2_32, 3_28, 2] ) self.assertListEqual( tokenizer.encode('Hello world! cécé herlolip 418' , add_special_tokens=_snake_case ) , [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2] , ) @slow def _a ( self : List[Any] ): """simple docstring""" A__ = self.tokenizer_class.from_pretrained('allenai/longformer-base-4096' ) A__ = tokenizer.encode('sequence builders' , add_special_tokens=_snake_case ) A__ = tokenizer.encode('multi-sequence build' , add_special_tokens=_snake_case ) A__ = tokenizer.encode( 'sequence builders' , add_special_tokens=_snake_case , add_prefix_space=_snake_case ) A__ = tokenizer.encode( 'sequence builders' , 'multi-sequence build' , add_special_tokens=_snake_case , add_prefix_space=_snake_case ) A__ = tokenizer.build_inputs_with_special_tokens(_snake_case ) A__ = tokenizer.build_inputs_with_special_tokens(_snake_case , _snake_case ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def _a ( self : List[str] ): """simple docstring""" A__ = self.get_tokenizer() A__ = 'Encode this sequence.' A__ = tokenizer.byte_encoder[' '.encode('utf-8' )[0]] # Testing encoder arguments A__ = tokenizer.encode(_snake_case , add_special_tokens=_snake_case , add_prefix_space=_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(_snake_case , _snake_case ) A__ = tokenizer.encode(_snake_case , add_special_tokens=_snake_case , add_prefix_space=_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(_snake_case , _snake_case ) tokenizer.add_special_tokens({'bos_token': '<s>'} ) A__ = tokenizer.encode(_snake_case , add_special_tokens=_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(_snake_case , _snake_case ) # Testing spaces after special tokens A__ = '<mask>' tokenizer.add_special_tokens( {'mask_token': AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case )} ) # mask token has a left space A__ = tokenizer.convert_tokens_to_ids(_snake_case ) A__ = 'Encode <mask> sequence' A__ = 'Encode <mask>sequence' A__ = tokenizer.encode(_snake_case ) A__ = encoded.index(_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(_snake_case , _snake_case ) A__ = tokenizer.encode(_snake_case ) A__ = encoded.index(_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(_snake_case , _snake_case ) def _a ( self : Dict ): """simple docstring""" pass def _a ( self : Union[str, Any] ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): A__ = self.rust_tokenizer_class.from_pretrained(_snake_case , **_snake_case ) A__ = self.tokenizer_class.from_pretrained(_snake_case , **_snake_case ) A__ = 'A, <mask> AllenNLP sentence.' A__ = tokenizer_r.encode_plus(_snake_case , add_special_tokens=_snake_case , return_token_type_ids=_snake_case ) A__ = tokenizer_p.encode_plus(_snake_case , add_special_tokens=_snake_case , return_token_type_ids=_snake_case ) # token_type_ids should put 0 everywhere self.assertEqual(sum(tokens_r['token_type_ids'] ) , sum(tokens_p['token_type_ids'] ) ) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEqual( sum(tokens_r['attention_mask'] ) / len(tokens_r['attention_mask'] ) , sum(tokens_p['attention_mask'] ) / len(tokens_p['attention_mask'] ) , ) A__ = tokenizer_r.convert_ids_to_tokens(tokens_r['input_ids'] ) A__ = tokenizer_p.convert_ids_to_tokens(tokens_p['input_ids'] ) # Rust correctly handles the space before the mask while python doesnt self.assertSequenceEqual(tokens_p['input_ids'] , [0, 2_50, 6, 5_02_64, 38_23, 4_87, 2_19_92, 36_45, 4, 2] ) self.assertSequenceEqual(tokens_r['input_ids'] , [0, 2_50, 6, 5_02_64, 38_23, 4_87, 2_19_92, 36_45, 4, 2] ) self.assertSequenceEqual( _snake_case , ['<s>', 'A', ',', '<mask>', 'ĠAllen', 'N', 'LP', 'Ġsentence', '.', '</s>'] ) self.assertSequenceEqual( _snake_case , ['<s>', 'A', ',', '<mask>', 'ĠAllen', 'N', 'LP', 'Ġsentence', '.', '</s>'] ) def _a ( self : List[Any] ): """simple docstring""" for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): A__ = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) A__ = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state['add_prefix_space'] , _snake_case ) self.assertEqual(post_processor_state['add_prefix_space'] , _snake_case ) self.assertEqual(post_processor_state['trim_offsets'] , _snake_case ) def _a ( self : Optional[Any] ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): A__ = 'hello' # `hello` is a token in the vocabulary of `pretrained_name` A__ = F'''{text_of_1_token} {text_of_1_token}''' A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_snake_case ) + 1, len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_snake_case ) + 1, len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_snake_case ), len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_snake_case ), len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = F''' {text}''' # tokenizer_r = self.rust_tokenizer_class.from_pretrained( # pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True # ) # encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False) # self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token))) # self.assertEqual( # encoding.offset_mapping[1], # (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)), # ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_snake_case ) + 1, 1 + len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_snake_case ), 1 + len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_snake_case ), 1 + len(_snake_case ) + 1 + len(_snake_case )) , )
52
1
from __future__ import annotations SCREAMING_SNAKE_CASE__ = list[list[int]] # assigning initial values to the grid SCREAMING_SNAKE_CASE__ = [ [3, 0, 6, 5, 0, 8, 4, 0, 0], [5, 2, 0, 0, 0, 0, 0, 0, 0], [0, 8, 7, 0, 0, 0, 0, 3, 1], [0, 0, 3, 0, 1, 0, 0, 8, 0], [9, 0, 0, 8, 6, 3, 0, 0, 5], [0, 5, 0, 0, 9, 0, 6, 0, 0], [1, 3, 0, 0, 0, 0, 2, 5, 0], [0, 0, 0, 0, 0, 0, 0, 7, 4], [0, 0, 5, 2, 0, 6, 3, 0, 0], ] # a grid with no solution SCREAMING_SNAKE_CASE__ = [ [5, 0, 6, 5, 0, 8, 4, 0, 3], [5, 2, 0, 0, 0, 0, 0, 0, 2], [1, 8, 7, 0, 0, 0, 0, 3, 1], [0, 0, 3, 0, 1, 0, 0, 8, 0], [9, 0, 0, 8, 6, 3, 0, 0, 5], [0, 5, 0, 0, 9, 0, 6, 0, 0], [1, 3, 0, 0, 0, 0, 2, 5, 0], [0, 0, 0, 0, 0, 0, 0, 7, 4], [0, 0, 5, 2, 0, 6, 3, 0, 0], ] def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> bool: for i in range(9 ): if grid[row][i] == n or grid[i][column] == n: return False for i in range(3 ): for j in range(3 ): if grid[(row - row % 3) + i][(column - column % 3) + j] == n: return False return True def A ( __UpperCamelCase ) -> tuple[int, int] | None: for i in range(9 ): for j in range(9 ): if grid[i][j] == 0: return i, j return None def A ( __UpperCamelCase ) -> Matrix | None: if location := find_empty_location(__UpperCamelCase ): A__ , A__ = location else: # If the location is ``None``, then the grid is solved. return grid for digit in range(1 , 10 ): if is_safe(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): A__ = digit if sudoku(__UpperCamelCase ) is not None: return grid A__ = 0 return None def A ( __UpperCamelCase ) -> None: for row in grid: for cell in row: print(__UpperCamelCase , end=' ' ) print() if __name__ == "__main__": # make a copy of grid so that you can compare with the unmodified grid for example_grid in (initial_grid, no_solution): print('''\nExample grid:\n''' + '''=''' * 2_0) print_solution(example_grid) print('''\nExample grid solution:''') SCREAMING_SNAKE_CASE__ = sudoku(example_grid) if solution is not None: print_solution(solution) else: print('''Cannot find a solution.''')
52
import pytest import datasets # Import fixture modules as plugins SCREAMING_SNAKE_CASE__ = ['''tests.fixtures.files''', '''tests.fixtures.hub''', '''tests.fixtures.fsspec'''] def A ( __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: # Mark tests as "unit" by default if not marked as "integration" (or already marked as "unit") for item in items: if any(marker in item.keywords for marker in ['integration', 'unit'] ): continue item.add_marker(pytest.mark.unit ) def A ( __UpperCamelCase ) -> str: config.addinivalue_line('markers' , 'torchaudio_latest: mark test to run with torchaudio>=0.12' ) @pytest.fixture(autouse=__UpperCamelCase ) def A ( __UpperCamelCase , __UpperCamelCase ) -> Union[str, Any]: # test_hf_cache_home = tmp_path_factory.mktemp("cache") # TODO: why a cache dir per test function does not work? A__ = tmp_path_factory.getbasetemp() / 'cache' A__ = test_hf_cache_home / 'datasets' A__ = test_hf_cache_home / 'metrics' A__ = test_hf_cache_home / 'modules' monkeypatch.setattr('datasets.config.HF_DATASETS_CACHE' , str(__UpperCamelCase ) ) monkeypatch.setattr('datasets.config.HF_METRICS_CACHE' , str(__UpperCamelCase ) ) monkeypatch.setattr('datasets.config.HF_MODULES_CACHE' , str(__UpperCamelCase ) ) A__ = test_hf_datasets_cache / 'downloads' monkeypatch.setattr('datasets.config.DOWNLOADED_DATASETS_PATH' , str(__UpperCamelCase ) ) A__ = test_hf_datasets_cache / 'downloads' / 'extracted' monkeypatch.setattr('datasets.config.EXTRACTED_DATASETS_PATH' , str(__UpperCamelCase ) ) @pytest.fixture(autouse=__UpperCamelCase , scope='session' ) def A ( ) -> Union[str, Any]: datasets.disable_progress_bar() @pytest.fixture(autouse=__UpperCamelCase ) def A ( __UpperCamelCase ) -> int: # don't take tests into account when counting downloads monkeypatch.setattr('datasets.config.HF_UPDATE_DOWNLOAD_COUNTS' , __UpperCamelCase ) @pytest.fixture def A ( __UpperCamelCase ) -> Any: # Required to suppress RemovedIn20Warning when feature(s) are not compatible with SQLAlchemy 2.0 # To be removed once SQLAlchemy 2.0 supported monkeypatch.setattr('sqlalchemy.util.deprecations.SILENCE_UBER_WARNING' , __UpperCamelCase )
52
1
import copy import json import os import tempfile from transformers import is_torch_available from .test_configuration_utils import config_common_kwargs class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : List[str] , _snake_case : Optional[int] , _snake_case : Optional[Any]=None , _snake_case : Dict=True , _snake_case : Union[str, Any]=None , **_snake_case : int ): """simple docstring""" A__ = parent A__ = config_class A__ = has_text_modality A__ = kwargs A__ = common_properties def _a ( self : Tuple ): """simple docstring""" A__ = self.config_class(**self.inputs_dict ) A__ = ( ['hidden_size', 'num_attention_heads', 'num_hidden_layers'] if self.common_properties is None else self.common_properties ) # Add common fields for text models if self.has_text_modality: common_properties.extend(['vocab_size'] ) # Test that config has the common properties as getters for prop in common_properties: self.parent.assertTrue(hasattr(_snake_case , _snake_case ) , msg=F'''`{prop}` does not exist''' ) # Test that config has the common properties as setter for idx, name in enumerate(_snake_case ): try: setattr(_snake_case , _snake_case , _snake_case ) self.parent.assertEqual( getattr(_snake_case , _snake_case ) , _snake_case , msg=F'''`{name} value {idx} expected, but was {getattr(_snake_case , _snake_case )}''' ) except NotImplementedError: # Some models might not be able to implement setters for common_properties # In that case, a NotImplementedError is raised pass # Test if config class can be called with Config(prop_name=..) for idx, name in enumerate(_snake_case ): try: A__ = self.config_class(**{name: idx} ) self.parent.assertEqual( getattr(_snake_case , _snake_case ) , _snake_case , msg=F'''`{name} value {idx} expected, but was {getattr(_snake_case , _snake_case )}''' ) except NotImplementedError: # Some models might not be able to implement setters for common_properties # In that case, a NotImplementedError is raised pass def _a ( self : Optional[int] ): """simple docstring""" A__ = self.config_class(**self.inputs_dict ) A__ = json.loads(config.to_json_string() ) for key, value in self.inputs_dict.items(): self.parent.assertEqual(obj[key] , _snake_case ) def _a ( self : Optional[int] ): """simple docstring""" A__ = self.config_class(**self.inputs_dict ) with tempfile.TemporaryDirectory() as tmpdirname: A__ = os.path.join(_snake_case , 'config.json' ) config_first.to_json_file(_snake_case ) A__ = self.config_class.from_json_file(_snake_case ) self.parent.assertEqual(config_second.to_dict() , config_first.to_dict() ) def _a ( self : List[str] ): """simple docstring""" A__ = self.config_class(**self.inputs_dict ) with tempfile.TemporaryDirectory() as tmpdirname: config_first.save_pretrained(_snake_case ) A__ = self.config_class.from_pretrained(_snake_case ) self.parent.assertEqual(config_second.to_dict() , config_first.to_dict() ) def _a ( self : Dict ): """simple docstring""" A__ = self.config_class(**self.inputs_dict ) A__ = 'test' with tempfile.TemporaryDirectory() as tmpdirname: A__ = os.path.join(_snake_case , _snake_case ) config_first.save_pretrained(_snake_case ) A__ = self.config_class.from_pretrained(_snake_case , subfolder=_snake_case ) self.parent.assertEqual(config_second.to_dict() , config_first.to_dict() ) def _a ( self : Dict ): """simple docstring""" A__ = self.config_class(**self.inputs_dict , num_labels=5 ) self.parent.assertEqual(len(config.idalabel ) , 5 ) self.parent.assertEqual(len(config.labelaid ) , 5 ) A__ = 3 self.parent.assertEqual(len(config.idalabel ) , 3 ) self.parent.assertEqual(len(config.labelaid ) , 3 ) def _a ( self : List[Any] ): """simple docstring""" if self.config_class.is_composition: return A__ = self.config_class() self.parent.assertIsNotNone(_snake_case ) def _a ( self : Optional[Any] ): """simple docstring""" A__ = copy.deepcopy(_snake_case ) A__ = self.config_class(**_snake_case ) A__ = [] for key, value in config_common_kwargs.items(): if key == "torch_dtype": if not is_torch_available(): continue else: import torch if config.torch_dtype != torch.floataa: wrong_values.append(('torch_dtype', config.torch_dtype, torch.floataa) ) elif getattr(_snake_case , _snake_case ) != value: wrong_values.append((key, getattr(_snake_case , _snake_case ), value) ) if len(_snake_case ) > 0: A__ = '\n'.join([F'''- {v[0]}: got {v[1]} instead of {v[2]}''' for v in wrong_values] ) raise ValueError(F'''The following keys were not properly set in the config:\n{errors}''' ) def _a ( self : Dict ): """simple docstring""" self.create_and_test_config_common_properties() self.create_and_test_config_to_json_string() self.create_and_test_config_to_json_file() self.create_and_test_config_from_and_save_pretrained() self.create_and_test_config_from_and_save_pretrained_subfolder() self.create_and_test_config_with_num_labels() self.check_config_can_be_init_without_params() self.check_config_arguments_init()
52
import argparse import re from typing import Dict import torch from datasets import Audio, Dataset, load_dataset, load_metric from transformers import AutoFeatureExtractor, pipeline def A ( __UpperCamelCase , __UpperCamelCase ) -> Tuple: A__ = args.log_outputs A__ = '_'.join(args.dataset.split('/' ) + [args.config, args.split] ) # load metric A__ = load_metric('wer' ) A__ = load_metric('cer' ) # compute metrics A__ = wer.compute(references=result['target'] , predictions=result['prediction'] ) A__ = cer.compute(references=result['target'] , predictions=result['prediction'] ) # print & log results A__ = f'''WER: {wer_result}\nCER: {cer_result}''' print(__UpperCamelCase ) with open(f'''{dataset_id}_eval_results.txt''' , 'w' ) as f: f.write(__UpperCamelCase ) # log all results in text file. Possibly interesting for analysis if log_outputs is not None: A__ = f'''log_{dataset_id}_predictions.txt''' A__ = f'''log_{dataset_id}_targets.txt''' with open(__UpperCamelCase , 'w' ) as p, open(__UpperCamelCase , 'w' ) as t: # mapping function to write output def write_to_file(__UpperCamelCase , __UpperCamelCase ): p.write(f'''{i}''' + '\n' ) p.write(batch['prediction'] + '\n' ) t.write(f'''{i}''' + '\n' ) t.write(batch['target'] + '\n' ) result.map(__UpperCamelCase , with_indices=__UpperCamelCase ) def A ( __UpperCamelCase ) -> str: A__ = '[,?.!\-\;\:"“%‘”�—’…–]' # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training A__ = re.sub(__UpperCamelCase , '' , text.lower() ) # In addition, we can normalize the target text, e.g. removing new lines characters etc... # note that order is important here! A__ = ['\n\n', '\n', ' ', ' '] for t in token_sequences_to_ignore: A__ = ' '.join(text.split(__UpperCamelCase ) ) return text def A ( __UpperCamelCase ) -> Union[str, Any]: # load dataset A__ = load_dataset(args.dataset , args.config , split=args.split , use_auth_token=__UpperCamelCase ) # for testing: only process the first two examples as a test # dataset = dataset.select(range(10)) # load processor A__ = AutoFeatureExtractor.from_pretrained(args.model_id ) A__ = feature_extractor.sampling_rate # resample audio A__ = dataset.cast_column('audio' , Audio(sampling_rate=__UpperCamelCase ) ) # load eval pipeline if args.device is None: A__ = 0 if torch.cuda.is_available() else -1 A__ = pipeline('automatic-speech-recognition' , model=args.model_id , device=args.device ) # map function to decode audio def map_to_pred(__UpperCamelCase ): A__ = asr( batch['audio']['array'] , chunk_length_s=args.chunk_length_s , stride_length_s=args.stride_length_s ) A__ = prediction['text'] A__ = normalize_text(batch['sentence'] ) return batch # run inference on all examples A__ = dataset.map(__UpperCamelCase , remove_columns=dataset.column_names ) # compute and log_results # do not change function below log_results(__UpperCamelCase , __UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument( '''--model_id''', type=str, required=True, help='''Model identifier. Should be loadable with 🤗 Transformers''' ) parser.add_argument( '''--dataset''', type=str, required=True, help='''Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets''', ) parser.add_argument( '''--config''', type=str, required=True, help='''Config of the dataset. *E.g.* `\'en\'` for Common Voice''' ) parser.add_argument('''--split''', type=str, required=True, help='''Split of the dataset. *E.g.* `\'test\'`''') parser.add_argument( '''--chunk_length_s''', type=float, default=None, help='''Chunk length in seconds. Defaults to 5 seconds.''' ) parser.add_argument( '''--stride_length_s''', type=float, default=None, help='''Stride of the audio chunks. Defaults to 1 second.''' ) parser.add_argument( '''--log_outputs''', action='''store_true''', help='''If defined, write outputs to log file for analysis.''' ) parser.add_argument( '''--device''', type=int, default=None, help='''The device to run the pipeline on. -1 for CPU (default), 0 for the first GPU and so on.''', ) SCREAMING_SNAKE_CASE__ = parser.parse_args() main(args)
52
1
def A ( __UpperCamelCase , __UpperCamelCase ) -> tuple[float, float]: # Check if the input is valid if not len(__UpperCamelCase ) == len(__UpperCamelCase ) == 3: raise ValueError('Please enter a valid equation.' ) if equationa[0] == equationa[1] == equationa[0] == equationa[1] == 0: raise ValueError('Both a & b of two equations can\'t be zero.' ) # Extract the coefficients A__ , A__ , A__ = equationa A__ , A__ , A__ = equationa # Calculate the determinants of the matrices A__ = aa * ba - aa * ba A__ = ca * ba - ca * ba A__ = aa * ca - aa * ca # Check if the system of linear equations has a solution (using Cramer's rule) if determinant == 0: if determinant_x == determinant_y == 0: raise ValueError('Infinite solutions. (Consistent system)' ) else: raise ValueError('No solution. (Inconsistent system)' ) else: if determinant_x == determinant_y == 0: # Trivial solution (Inconsistent system) return (0.0, 0.0) else: A__ = determinant_x / determinant A__ = determinant_y / determinant # Non-Trivial Solution (Consistent system) return (x, y)
52
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import YolosConfig, YolosForObjectDetection, YolosImageProcessor from transformers.utils import logging logging.set_verbosity_info() SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) def A ( __UpperCamelCase ) -> YolosConfig: A__ = YolosConfig() # size of the architecture if "yolos_ti" in yolos_name: A__ = 192 A__ = 768 A__ = 12 A__ = 3 A__ = [800, 1_333] A__ = False elif yolos_name == "yolos_s_dWr": A__ = 330 A__ = 14 A__ = 6 A__ = 1_320 elif "yolos_s" in yolos_name: A__ = 384 A__ = 1_536 A__ = 12 A__ = 6 elif "yolos_b" in yolos_name: A__ = [800, 1_344] A__ = 91 A__ = 'huggingface/label-files' A__ = 'coco-detection-id2label.json' A__ = json.load(open(hf_hub_download(__UpperCamelCase , __UpperCamelCase , repo_type='dataset' ) , 'r' ) ) A__ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} A__ = idalabel A__ = {v: k for k, v in idalabel.items()} return config def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = False ) -> str: for i in range(config.num_hidden_layers ): # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) A__ = state_dict.pop(f'''blocks.{i}.attn.qkv.weight''' ) A__ = state_dict.pop(f'''blocks.{i}.attn.qkv.bias''' ) # next, add query, keys and values (in that order) to the state dict A__ = in_proj_weight[: config.hidden_size, :] A__ = in_proj_bias[: config.hidden_size] A__ = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] A__ = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] A__ = in_proj_weight[-config.hidden_size :, :] A__ = in_proj_bias[-config.hidden_size :] def A ( __UpperCamelCase ) -> str: if "backbone" in name: A__ = name.replace('backbone' , 'vit' ) if "cls_token" in name: A__ = name.replace('cls_token' , 'embeddings.cls_token' ) if "det_token" in name: A__ = name.replace('det_token' , 'embeddings.detection_tokens' ) if "mid_pos_embed" in name: A__ = name.replace('mid_pos_embed' , 'encoder.mid_position_embeddings' ) if "pos_embed" in name: A__ = name.replace('pos_embed' , 'embeddings.position_embeddings' ) if "patch_embed.proj" in name: A__ = name.replace('patch_embed.proj' , 'embeddings.patch_embeddings.projection' ) if "blocks" in name: A__ = name.replace('blocks' , 'encoder.layer' ) if "attn.proj" in name: A__ = name.replace('attn.proj' , 'attention.output.dense' ) if "attn" in name: A__ = name.replace('attn' , 'attention.self' ) if "norm1" in name: A__ = name.replace('norm1' , 'layernorm_before' ) if "norm2" in name: A__ = name.replace('norm2' , 'layernorm_after' ) if "mlp.fc1" in name: A__ = name.replace('mlp.fc1' , 'intermediate.dense' ) if "mlp.fc2" in name: A__ = name.replace('mlp.fc2' , 'output.dense' ) if "class_embed" in name: A__ = name.replace('class_embed' , 'class_labels_classifier' ) if "bbox_embed" in name: A__ = name.replace('bbox_embed' , 'bbox_predictor' ) if "vit.norm" in name: A__ = name.replace('vit.norm' , 'vit.layernorm' ) return name def A ( __UpperCamelCase , __UpperCamelCase ) -> dict: for key in orig_state_dict.copy().keys(): A__ = orig_state_dict.pop(__UpperCamelCase ) if "qkv" in key: A__ = key.split('.' ) A__ = int(key_split[2] ) A__ = model.vit.encoder.layer[layer_num].attention.attention.all_head_size if "weight" in key: A__ = val[:dim, :] A__ = val[ dim : dim * 2, : ] A__ = val[-dim:, :] else: A__ = val[:dim] A__ = val[dim : dim * 2] A__ = val[-dim:] else: A__ = val return orig_state_dict def A ( ) -> torch.Tensor: A__ = 'http://images.cocodataset.org/val2017/000000039769.jpg' A__ = Image.open(requests.get(__UpperCamelCase , stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = False ) -> List[str]: A__ = get_yolos_config(__UpperCamelCase ) # load original state_dict A__ = torch.load(__UpperCamelCase , map_location='cpu' )['model'] # load 🤗 model A__ = YolosForObjectDetection(__UpperCamelCase ) model.eval() A__ = convert_state_dict(__UpperCamelCase , __UpperCamelCase ) model.load_state_dict(__UpperCamelCase ) # Check outputs on an image, prepared by YolosImageProcessor A__ = 800 if yolos_name != 'yolos_ti' else 512 A__ = YolosImageProcessor(format='coco_detection' , size=__UpperCamelCase ) A__ = image_processor(images=prepare_img() , return_tensors='pt' ) A__ = model(**__UpperCamelCase ) A__ , A__ = outputs.logits, outputs.pred_boxes A__ , A__ = None, None if yolos_name == "yolos_ti": A__ = torch.tensor( [[-39.5022, -11.9820, -17.6888], [-29.9574, -9.9769, -17.7691], [-42.3281, -20.7200, -30.6294]] ) A__ = torch.tensor( [[0.4021, 0.0836, 0.7979], [0.0184, 0.2609, 0.0364], [0.1781, 0.2004, 0.2095]] ) elif yolos_name == "yolos_s_200_pre": A__ = torch.tensor( [[-24.0248, -10.3024, -14.8290], [-42.0392, -16.8200, -27.4334], [-27.2743, -11.8154, -18.7148]] ) A__ = torch.tensor( [[0.2559, 0.5455, 0.4706], [0.2989, 0.7279, 0.1875], [0.7732, 0.4017, 0.4462]] ) elif yolos_name == "yolos_s_300_pre": A__ = torch.tensor( [[-36.2220, -14.4385, -23.5457], [-35.6970, -14.7583, -21.3935], [-31.5939, -13.6042, -16.8049]] ) A__ = torch.tensor( [[0.7614, 0.2316, 0.4728], [0.7168, 0.4495, 0.3855], [0.4996, 0.1466, 0.9996]] ) elif yolos_name == "yolos_s_dWr": A__ = torch.tensor( [[-42.8668, -24.1049, -41.1690], [-34.7456, -14.1274, -24.9194], [-33.7898, -12.1946, -25.6495]] ) A__ = torch.tensor( [[0.5587, 0.2773, 0.0605], [0.5004, 0.3014, 0.9994], [0.4999, 0.1548, 0.9994]] ) elif yolos_name == "yolos_base": A__ = torch.tensor( [[-40.6064, -24.3084, -32.6447], [-55.1990, -30.7719, -35.5877], [-51.4311, -33.3507, -35.6462]] ) A__ = torch.tensor( [[0.5555, 0.2794, 0.0655], [0.9049, 0.2664, 0.1894], [0.9183, 0.1984, 0.1635]] ) else: raise ValueError(f'''Unknown yolos_name: {yolos_name}''' ) assert torch.allclose(logits[0, :3, :3] , __UpperCamelCase , atol=1E-4 ) assert torch.allclose(pred_boxes[0, :3, :3] , __UpperCamelCase , atol=1E-4 ) Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) print(f'''Saving model {yolos_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(__UpperCamelCase ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(__UpperCamelCase ) if push_to_hub: A__ = { 'yolos_ti': 'yolos-tiny', 'yolos_s_200_pre': 'yolos-small', 'yolos_s_300_pre': 'yolos-small-300', 'yolos_s_dWr': 'yolos-small-dwr', 'yolos_base': 'yolos-base', } print('Pushing to the hub...' ) A__ = model_mapping[yolos_name] image_processor.push_to_hub(__UpperCamelCase , organization='hustvl' ) model.push_to_hub(__UpperCamelCase , organization='hustvl' ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--yolos_name''', default='''yolos_s_200_pre''', type=str, help=( '''Name of the YOLOS model you\'d like to convert. Should be one of \'yolos_ti\', \'yolos_s_200_pre\',''' ''' \'yolos_s_300_pre\', \'yolos_s_dWr\', \'yolos_base\'.''' ), ) parser.add_argument( '''--checkpoint_path''', default=None, type=str, help='''Path to the original state dict (.pth file).''' ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_yolos_checkpoint(args.yolos_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub)
52
1
from __future__ import annotations def A ( __UpperCamelCase ) -> bool: A__ = str(__UpperCamelCase ) return n == n[::-1] def A ( __UpperCamelCase = 1_000_000 ) -> Dict: A__ = 0 for i in range(1 , __UpperCamelCase ): if is_palindrome(__UpperCamelCase ) and is_palindrome(bin(__UpperCamelCase ).split('b' )[1] ): total += i return total if __name__ == "__main__": print(solution(int(str(input().strip()))))
52
from typing import TYPE_CHECKING from ..utils import _LazyModule SCREAMING_SNAKE_CASE__ = { '''config''': [ '''EXTERNAL_DATA_FORMAT_SIZE_LIMIT''', '''OnnxConfig''', '''OnnxConfigWithPast''', '''OnnxSeq2SeqConfigWithPast''', '''PatchingSpec''', ], '''convert''': ['''export''', '''validate_model_outputs'''], '''features''': ['''FeaturesManager'''], '''utils''': ['''ParameterFormat''', '''compute_serialized_parameters_size'''], } if TYPE_CHECKING: from .config import ( EXTERNAL_DATA_FORMAT_SIZE_LIMIT, OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast, PatchingSpec, ) from .convert import export, validate_model_outputs from .features import FeaturesManager from .utils import ParameterFormat, compute_serialized_parameters_size else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
52
1
import argparse import json import logging import os import sys from unittest.mock import patch from transformers.testing_utils import TestCasePlus, get_gpu_count, slow SCREAMING_SNAKE_CASE__ = [ os.path.join(os.path.dirname(__file__), dirname) for dirname in [ '''text-classification''', '''language-modeling''', '''summarization''', '''token-classification''', '''question-answering''', ] ] sys.path.extend(SRC_DIRS) if SRC_DIRS is not None: import run_clm_flax import run_flax_glue import run_flax_ner import run_mlm_flax import run_qa import run_summarization_flax import run_ta_mlm_flax logging.basicConfig(level=logging.DEBUG) SCREAMING_SNAKE_CASE__ = logging.getLogger() def A ( ) -> int: A__ = argparse.ArgumentParser() parser.add_argument('-f' ) A__ = parser.parse_args() return args.f def A ( __UpperCamelCase , __UpperCamelCase="eval" ) -> Optional[int]: A__ = os.path.join(__UpperCamelCase , f'''{split}_results.json''' ) if os.path.exists(__UpperCamelCase ): with open(__UpperCamelCase , 'r' ) as f: return json.load(__UpperCamelCase ) raise ValueError(f'''can\'t find {path}''' ) SCREAMING_SNAKE_CASE__ = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def _a ( self : int ): """simple docstring""" A__ = self.get_auto_remove_tmp_dir() A__ = F''' run_glue.py --model_name_or_path distilbert-base-uncased --output_dir {tmp_dir} --train_file ./tests/fixtures/tests_samples/MRPC/train.csv --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --learning_rate=1e-4 --eval_steps=2 --warmup_steps=2 --seed=42 --max_seq_length=128 '''.split() with patch.object(_snake_case , 'argv' , _snake_case ): run_flax_glue.main() A__ = get_results(_snake_case ) self.assertGreaterEqual(result['eval_accuracy'] , 0.75 ) @slow def _a ( self : List[Any] ): """simple docstring""" A__ = self.get_auto_remove_tmp_dir() A__ = F''' run_clm_flax.py --model_name_or_path distilgpt2 --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --do_train --do_eval --block_size 128 --per_device_train_batch_size 4 --per_device_eval_batch_size 4 --num_train_epochs 2 --logging_steps 2 --eval_steps 2 --output_dir {tmp_dir} --overwrite_output_dir '''.split() with patch.object(_snake_case , 'argv' , _snake_case ): run_clm_flax.main() A__ = get_results(_snake_case ) self.assertLess(result['eval_perplexity'] , 1_00 ) @slow def _a ( self : Optional[Any] ): """simple docstring""" A__ = self.get_auto_remove_tmp_dir() A__ = F''' run_summarization.py --model_name_or_path t5-small --train_file tests/fixtures/tests_samples/xsum/sample.json --validation_file tests/fixtures/tests_samples/xsum/sample.json --test_file tests/fixtures/tests_samples/xsum/sample.json --output_dir {tmp_dir} --overwrite_output_dir --num_train_epochs=3 --warmup_steps=8 --do_train --do_eval --do_predict --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --predict_with_generate '''.split() with patch.object(_snake_case , 'argv' , _snake_case ): run_summarization_flax.main() A__ = get_results(_snake_case , split='test' ) self.assertGreaterEqual(result['test_rouge1'] , 10 ) self.assertGreaterEqual(result['test_rouge2'] , 2 ) self.assertGreaterEqual(result['test_rougeL'] , 7 ) self.assertGreaterEqual(result['test_rougeLsum'] , 7 ) @slow def _a ( self : List[str] ): """simple docstring""" A__ = self.get_auto_remove_tmp_dir() A__ = F''' run_mlm.py --model_name_or_path distilroberta-base --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --output_dir {tmp_dir} --overwrite_output_dir --max_seq_length 128 --per_device_train_batch_size 4 --per_device_eval_batch_size 4 --logging_steps 2 --eval_steps 2 --do_train --do_eval --num_train_epochs=1 '''.split() with patch.object(_snake_case , 'argv' , _snake_case ): run_mlm_flax.main() A__ = get_results(_snake_case ) self.assertLess(result['eval_perplexity'] , 42 ) @slow def _a ( self : Dict ): """simple docstring""" A__ = self.get_auto_remove_tmp_dir() A__ = F''' run_t5_mlm_flax.py --model_name_or_path t5-small --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --do_train --do_eval --max_seq_length 128 --per_device_train_batch_size 4 --per_device_eval_batch_size 4 --num_train_epochs 2 --logging_steps 2 --eval_steps 2 --output_dir {tmp_dir} --overwrite_output_dir '''.split() with patch.object(_snake_case , 'argv' , _snake_case ): run_ta_mlm_flax.main() A__ = get_results(_snake_case ) self.assertGreaterEqual(result['eval_accuracy'] , 0.42 ) @slow def _a ( self : Tuple ): """simple docstring""" A__ = 7 if get_gpu_count() > 1 else 2 A__ = self.get_auto_remove_tmp_dir() A__ = F''' run_flax_ner.py --model_name_or_path bert-base-uncased --train_file tests/fixtures/tests_samples/conll/sample.json --validation_file tests/fixtures/tests_samples/conll/sample.json --output_dir {tmp_dir} --overwrite_output_dir --do_train --do_eval --warmup_steps=2 --learning_rate=2e-4 --logging_steps 2 --eval_steps 2 --per_device_train_batch_size=2 --per_device_eval_batch_size=2 --num_train_epochs={epochs} --seed 7 '''.split() with patch.object(_snake_case , 'argv' , _snake_case ): run_flax_ner.main() A__ = get_results(_snake_case ) self.assertGreaterEqual(result['eval_accuracy'] , 0.75 ) self.assertGreaterEqual(result['eval_f1'] , 0.3 ) @slow def _a ( self : List[str] ): """simple docstring""" A__ = self.get_auto_remove_tmp_dir() A__ = F''' run_qa.py --model_name_or_path bert-base-uncased --version_2_with_negative --train_file tests/fixtures/tests_samples/SQUAD/sample.json --validation_file tests/fixtures/tests_samples/SQUAD/sample.json --output_dir {tmp_dir} --overwrite_output_dir --num_train_epochs=3 --warmup_steps=2 --do_train --do_eval --logging_steps 2 --eval_steps 2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 '''.split() with patch.object(_snake_case , 'argv' , _snake_case ): run_qa.main() A__ = get_results(_snake_case ) self.assertGreaterEqual(result['eval_f1'] , 30 ) self.assertGreaterEqual(result['eval_exact'] , 30 )
52
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_rembert import RemBertTokenizer else: SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = {'''vocab_file''': '''sentencepiece.model''', '''tokenizer_file''': '''tokenizer.json'''} SCREAMING_SNAKE_CASE__ = { '''vocab_file''': { '''google/rembert''': '''https://huggingface.co/google/rembert/resolve/main/sentencepiece.model''', }, '''tokenizer_file''': { '''google/rembert''': '''https://huggingface.co/google/rembert/resolve/main/tokenizer.json''', }, } SCREAMING_SNAKE_CASE__ = { '''google/rembert''': 2_5_6, } SCREAMING_SNAKE_CASE__ = '''▁''' class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Any = VOCAB_FILES_NAMES A__ : str = PRETRAINED_VOCAB_FILES_MAP A__ : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A__ : int = RemBertTokenizer def __init__( self : Union[str, Any] , _snake_case : Any=None , _snake_case : Optional[Any]=None , _snake_case : Any=True , _snake_case : Optional[int]=True , _snake_case : Dict=False , _snake_case : Dict="[CLS]" , _snake_case : List[Any]="[SEP]" , _snake_case : Union[str, Any]="<unk>" , _snake_case : List[str]="[SEP]" , _snake_case : List[str]="<pad>" , _snake_case : str="[CLS]" , _snake_case : Any="[MASK]" , **_snake_case : Any , ): """simple docstring""" A__ = AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case ) if isinstance(_snake_case , _snake_case ) else mask_token super().__init__( _snake_case , tokenizer_file=_snake_case , do_lower_case=_snake_case , remove_space=_snake_case , keep_accents=_snake_case , bos_token=_snake_case , eos_token=_snake_case , unk_token=_snake_case , sep_token=_snake_case , pad_token=_snake_case , cls_token=_snake_case , mask_token=_snake_case , **_snake_case , ) A__ = do_lower_case A__ = remove_space A__ = keep_accents A__ = vocab_file A__ = False if not self.vocab_file else True def _a ( self : Any , _snake_case : List[int] , _snake_case : Optional[List[int]] = None ): """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def _a ( self : Tuple , _snake_case : List[int] , _snake_case : Optional[List[int]] = None , _snake_case : bool = False ): """simple docstring""" if already_has_special_tokens: if token_ids_a is not None: raise ValueError( 'You should not supply a second sequence if the provided sequence of ' 'ids is already formatted with special tokens for the model.' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(_snake_case )) + [1] + ([0] * len(_snake_case )) + [1] return [1] + ([0] * len(_snake_case )) + [1] def _a ( self : Dict , _snake_case : List[int] , _snake_case : Optional[List[int]] = None ): """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def _a ( self : Any , _snake_case : str , _snake_case : Optional[str] = None ): """simple docstring""" if not os.path.isdir(_snake_case ): logger.error('Vocabulary path ({}) should be a directory'.format(_snake_case ) ) return A__ = os.path.join( _snake_case , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_snake_case ): copyfile(self.vocab_file , _snake_case ) return (out_vocab_file,)
52
1
from __future__ import annotations import os import tempfile import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers import is_tensorflow_text_available, is_tf_available from transformers.testing_utils import require_tensorflow_text, require_tf, slow from ..test_modeling_tf_common import floats_tensor from .test_framework_agnostic import GenerationIntegrationTestsMixin if is_tf_available(): import tensorflow as tf from transformers import ( AutoTokenizer, TFAutoModelForCausalLM, TFAutoModelForSeqaSeqLM, TFAutoModelForSpeechSeqaSeq, TFAutoModelForVisionaSeq, TFBartForConditionalGeneration, TFLogitsProcessorList, TFMinLengthLogitsProcessor, tf_top_k_top_p_filtering, ) if is_tensorflow_text_available(): import tensorflow_text as text @require_tf class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def _a ( self : Union[str, Any] ): """simple docstring""" A__ = tf.convert_to_tensor( [ [ 8.222_0991, # 3rd highest value; idx. 0 -0.562_0044, 5.2322_9752, 4.038_6393, -6.879_8378, -0.5478_5802, -3.201_2153, 2.9277_7176, 1.8817_1953, 7.3534_1276, # 5th highest value; idx. 9 8.4320_7833, # 2nd highest value; idx. 10 -9.8571_1836, -5.9620_9236, -1.1303_9161, -7.111_5294, -0.836_9633, -5.318_6408, 7.0642_7407, 0.8136_9344, -0.8202_3817, -5.917_9796, 0.5881_3443, -6.9977_8438, 4.7155_1189, -0.1877_1637, 7.4402_0759, # 4th highest value; idx. 25 9.3845_0987, # 1st highest value; idx. 26 2.1266_2941, -9.3256_2038, 2.3565_2522, ], # cummulative prob of 5 highest values <= 0.6 [ 0.5842_5518, 4.5313_9238, -5.5751_0464, -6.2803_0699, -7.1952_9503, -4.0212_2551, 1.3933_7037, -6.0670_7057, 1.5948_0517, -9.64_3119, 0.0390_7799, 0.6723_1762, -8.8820_6726, 6.2711_5922, # 4th highest value; idx. 13 2.2852_0723, 4.8276_7506, 4.3042_1368, 8.827_5313, # 2nd highest value; idx. 17 5.4402_9958, # 5th highest value; idx. 18 -4.473_5794, 7.3857_9536, # 3rd highest value; idx. 20 -2.9105_1663, 2.6194_6077, -2.567_4762, -9.4895_9302, -4.0292_2645, -1.3541_6918, 9.6770_2323, # 1st highest value; idx. 27 -5.8947_8553, 1.8537_0467, ], # cummulative prob of 5 highest values <= 0.6 ] , dtype=tf.floataa , ) A__ = tf.convert_to_tensor( [[0, 0], [0, 9], [0, 10], [0, 25], [0, 26], [1, 13], [1, 17], [1, 18], [1, 20], [1, 27]] , dtype=tf.intaa , ) # expected non filtered idx as noted above A__ = tf.convert_to_tensor( [8.22_2099, 7.353_4126, 8.43_2078, 7.440_2075, 9.3_8451, 6.27_1159, 8.82_7531, 5.440_2995, 7.385_7956, 9.67_7023] , dtype=tf.floataa , ) # expected non filtered values as noted above A__ = tf_top_k_top_p_filtering(_snake_case , top_k=10 , top_p=0.6 , min_tokens_to_keep=4 ) A__ = output[output != -float('inf' )] A__ = tf.cast( tf.where(tf.not_equal(_snake_case , tf.constant(-float('inf' ) , dtype=tf.floataa ) ) ) , dtype=tf.intaa , ) tf.debugging.assert_near(_snake_case , _snake_case , rtol=1E-12 ) tf.debugging.assert_equal(_snake_case , _snake_case ) @require_tf class __lowerCAmelCase ( unittest.TestCase , UpperCAmelCase_ ): """simple docstring""" if is_tf_available(): A__ : List[Any] = { "AutoModelForCausalLM": TFAutoModelForCausalLM, "AutoModelForSpeechSeq2Seq": TFAutoModelForSpeechSeqaSeq, "AutoModelForSeq2SeqLM": TFAutoModelForSeqaSeqLM, "AutoModelForVision2Seq": TFAutoModelForVisionaSeq, "LogitsProcessorList": TFLogitsProcessorList, "MinLengthLogitsProcessor": TFMinLengthLogitsProcessor, "create_tensor_fn": tf.convert_to_tensor, "floats_tensor": floats_tensor, "return_tensors": "tf", } @slow def _a ( self : str ): """simple docstring""" A__ = TFAutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2' ) A__ = 2 A__ = 2 class __lowerCAmelCase ( tf.Module ): """simple docstring""" def __init__( self : Any , _snake_case : str ): """simple docstring""" super(_snake_case , self ).__init__() A__ = model @tf.function( input_signature=( tf.TensorSpec((None, input_length) , tf.intaa , name='input_ids' ), tf.TensorSpec((None, input_length) , tf.intaa , name='attention_mask' ), ) , jit_compile=_snake_case , ) def _a ( self : Optional[int] , _snake_case : Dict , _snake_case : List[str] ): """simple docstring""" A__ = self.model.generate( input_ids=_snake_case , attention_mask=_snake_case , max_new_tokens=_snake_case , return_dict_in_generate=_snake_case , ) return {"sequences": outputs["sequences"]} A__ = [[2, 0], [1_02, 1_03]] A__ = [[1, 0], [1, 1]] A__ = DummyModel(model=_snake_case ) with tempfile.TemporaryDirectory() as tmp_dir: tf.saved_model.save(_snake_case , _snake_case , signatures={'serving_default': dummy_model.serving} ) A__ = tf.saved_model.load(_snake_case ).signatures['serving_default'] for batch_size in range(1 , len(_snake_case ) + 1 ): A__ = { 'input_ids': tf.constant(dummy_input_ids[:batch_size] ), 'attention_mask': tf.constant(dummy_attention_masks[:batch_size] ), } A__ = serving_func(**_snake_case )['sequences'] A__ = test_model.generate(**_snake_case , max_new_tokens=_snake_case ) tf.debugging.assert_equal(_snake_case , _snake_case ) @slow def _a ( self : Tuple ): """simple docstring""" A__ = TFAutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2' ) A__ = 1 A__ = 2 class __lowerCAmelCase ( tf.Module ): """simple docstring""" def __init__( self : Optional[int] , _snake_case : Tuple ): """simple docstring""" super(_snake_case , self ).__init__() A__ = model @tf.function( input_signature=( tf.TensorSpec((batch_size, None) , tf.intaa , name='input_ids' ), tf.TensorSpec((batch_size, None) , tf.intaa , name='attention_mask' ), ) , jit_compile=_snake_case , ) def _a ( self : Tuple , _snake_case : Any , _snake_case : List[Any] ): """simple docstring""" A__ = self.model.generate( input_ids=_snake_case , attention_mask=_snake_case , max_new_tokens=_snake_case , return_dict_in_generate=_snake_case , ) return {"sequences": outputs["sequences"]} A__ = [[2], [1_02, 1_03]] A__ = [[1], [1, 1]] A__ = DummyModel(model=_snake_case ) with tempfile.TemporaryDirectory() as tmp_dir: tf.saved_model.save(_snake_case , _snake_case , signatures={'serving_default': dummy_model.serving} ) A__ = tf.saved_model.load(_snake_case ).signatures['serving_default'] for input_row in range(len(_snake_case ) ): A__ = { 'input_ids': tf.constant([dummy_input_ids[input_row]] ), 'attention_mask': tf.constant([dummy_attention_masks[input_row]] ), } A__ = serving_func(**_snake_case )['sequences'] A__ = test_model.generate(**_snake_case , max_new_tokens=_snake_case ) tf.debugging.assert_equal(_snake_case , _snake_case ) @slow @require_tensorflow_text def _a ( self : Optional[Any] ): """simple docstring""" with tempfile.TemporaryDirectory() as tmp_dir: # file needed to load the TF tokenizer hf_hub_download(repo_id='google/flan-t5-small' , filename='spiece.model' , local_dir=_snake_case ) class __lowerCAmelCase ( tf.keras.layers.Layer ): """simple docstring""" def __init__( self : Dict ): """simple docstring""" super().__init__() A__ = text.SentencepieceTokenizer( model=tf.io.gfile.GFile(os.path.join(_snake_case , 'spiece.model' ) , 'rb' ).read() ) A__ = TFAutoModelForSeqaSeqLM.from_pretrained('hf-internal-testing/tiny-random-t5' ) def _a ( self : Any , _snake_case : Tuple , *_snake_case : Optional[Any] , **_snake_case : Union[str, Any] ): """simple docstring""" A__ = self.tokenizer.tokenize(_snake_case ) A__ , A__ = text.pad_model_inputs( _snake_case , max_seq_length=64 , pad_value=self.model.config.pad_token_id ) A__ = self.model.generate(input_ids=_snake_case , attention_mask=_snake_case ) return self.tokenizer.detokenize(_snake_case ) A__ = CompleteSentenceTransformer() A__ = tf.keras.layers.Input(shape=(1,) , dtype=tf.string , name='inputs' ) A__ = complete_model(_snake_case ) A__ = tf.keras.Model(_snake_case , _snake_case ) keras_model.save(_snake_case ) def _a ( self : List[str] ): """simple docstring""" A__ = { 'do_sample': True, 'num_beams': 1, 'top_p': 0.7, 'top_k': 10, 'temperature': 0.7, } A__ = 14 A__ = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-gpt2' ) A__ = 'Hello, my dog is cute and' A__ = tokenizer(_snake_case , return_tensors='tf' ) A__ = TFAutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2' ) A__ = 6_38 # forces the generation to happen on CPU, to avoid GPU-related quirks with tf.device(':/CPU:0' ): tf.random.set_seed(0 ) A__ = model.generate(**_snake_case , eos_token_id=_snake_case , **_snake_case ) self.assertTrue(expectation == len(generated_tokens[0] ) ) A__ = [6_38, 1_98] with tf.device(':/CPU:0' ): tf.random.set_seed(0 ) A__ = model.generate(**_snake_case , eos_token_id=_snake_case , **_snake_case ) self.assertTrue(expectation == len(generated_tokens[0] ) ) def _a ( self : Union[str, Any] ): """simple docstring""" A__ = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-bart' ) A__ = 'Hugging Face is a technology company based in New York and Paris.' A__ = bart_tokenizer(_snake_case , return_tensors='tf' ).input_ids A__ = TFBartForConditionalGeneration.from_pretrained('hf-internal-testing/tiny-random-bart' ) A__ = bart_model.generate(_snake_case ).numpy() class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def _a ( self : List[str] , _snake_case : List[Any] , _snake_case : Dict=None , **_snake_case : int ): """simple docstring""" return super().call(_snake_case , **_snake_case ) A__ = FakeBart.from_pretrained('hf-internal-testing/tiny-random-bart' ) A__ = bart_model.generate(_snake_case , foo='bar' ).numpy() self.assertTrue(np.array_equal(_snake_case , _snake_case ) ) class __lowerCAmelCase ( bart_model.model.encoder.__class__ ): """simple docstring""" def _a ( self : str , _snake_case : Tuple , **_snake_case : str ): """simple docstring""" return super().call(_snake_case , **_snake_case ) A__ = FakeEncoder(bart_model.config , bart_model.model.shared ) A__ = fake_encoder # Normal generation still works (the output will be different because the encoder weights are different) A__ = bart_model.generate(_snake_case ).numpy() with self.assertRaises(_snake_case ): # FakeEncoder.call() accepts **kwargs -> no filtering -> value error due to unexpected input "foo" bart_model.generate(_snake_case , foo='bar' )
52
import tempfile import unittest from make_student import create_student_by_copying_alternating_layers from transformers import AutoConfig from transformers.file_utils import cached_property from transformers.testing_utils import require_torch SCREAMING_SNAKE_CASE__ = '''sshleifer/bart-tiny-random''' SCREAMING_SNAKE_CASE__ = '''patrickvonplaten/t5-tiny-random''' @require_torch class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @cached_property def _a ( self : Optional[int] ): """simple docstring""" return AutoConfig.from_pretrained(_snake_case ) def _a ( self : Optional[Any] ): """simple docstring""" A__ , *A__ = create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=1 , d=1 ) self.assertEqual(student.config.num_hidden_layers , 1 ) def _a ( self : Optional[int] ): """simple docstring""" A__ , *A__ = create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=1 , d=_snake_case ) def _a ( self : int ): """simple docstring""" A__ , *A__ = create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=1 , d=_snake_case ) self.assertEqual(student.config.encoder_layers , 1 ) self.assertEqual(student.config.decoder_layers , self.teacher_config.encoder_layers ) def _a ( self : str ): """simple docstring""" A__ , *A__ = create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=1 , d=1 ) self.assertEqual(student.config.encoder_layers , 1 ) self.assertEqual(student.config.decoder_layers , 1 ) def _a ( self : str ): """simple docstring""" with self.assertRaises(_snake_case ): create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=_snake_case , d=_snake_case )
52
1
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = { '''facebook/convnextv2-tiny-1k-224''': '''https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json''', } class __lowerCAmelCase ( UpperCAmelCase_ , UpperCAmelCase_ ): """simple docstring""" A__ : Optional[int] = "convnextv2" def __init__( self : Union[str, Any] , _snake_case : Tuple=3 , _snake_case : Any=4 , _snake_case : Union[str, Any]=4 , _snake_case : Optional[Any]=None , _snake_case : Tuple=None , _snake_case : Optional[Any]="gelu" , _snake_case : Tuple=0.02 , _snake_case : str=1E-12 , _snake_case : Tuple=0.0 , _snake_case : List[str]=2_24 , _snake_case : Union[str, Any]=None , _snake_case : int=None , **_snake_case : Optional[int] , ): """simple docstring""" super().__init__(**_snake_case ) A__ = num_channels A__ = patch_size A__ = num_stages A__ = [96, 1_92, 3_84, 7_68] if hidden_sizes is None else hidden_sizes A__ = [3, 3, 9, 3] if depths is None else depths A__ = hidden_act A__ = initializer_range A__ = layer_norm_eps A__ = drop_path_rate A__ = image_size A__ = ['stem'] + [F'''stage{idx}''' for idx in range(1 , len(self.depths ) + 1 )] A__ , A__ = get_aligned_output_features_output_indices( out_features=_snake_case , out_indices=_snake_case , stage_names=self.stage_names )
52
from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Union[str, Any] = ["image_processor", "tokenizer"] A__ : Optional[Any] = "BridgeTowerImageProcessor" A__ : List[Any] = ("RobertaTokenizer", "RobertaTokenizerFast") def __init__( self : List[Any] , _snake_case : Optional[Any] , _snake_case : Optional[int] ): """simple docstring""" super().__init__(_snake_case , _snake_case ) def __call__( self : List[Any] , _snake_case : int , _snake_case : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , _snake_case : bool = True , _snake_case : Union[bool, str, PaddingStrategy] = False , _snake_case : Union[bool, str, TruncationStrategy] = None , _snake_case : Optional[int] = None , _snake_case : int = 0 , _snake_case : Optional[int] = None , _snake_case : Optional[bool] = None , _snake_case : Optional[bool] = None , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = True , _snake_case : Optional[Union[str, TensorType]] = None , **_snake_case : Optional[int] , ): """simple docstring""" A__ = self.tokenizer( text=_snake_case , add_special_tokens=_snake_case , padding=_snake_case , truncation=_snake_case , max_length=_snake_case , stride=_snake_case , pad_to_multiple_of=_snake_case , return_token_type_ids=_snake_case , return_attention_mask=_snake_case , return_overflowing_tokens=_snake_case , return_special_tokens_mask=_snake_case , return_offsets_mapping=_snake_case , return_length=_snake_case , verbose=_snake_case , return_tensors=_snake_case , **_snake_case , ) # add pixel_values + pixel_mask A__ = self.image_processor( _snake_case , return_tensors=_snake_case , do_normalize=_snake_case , do_center_crop=_snake_case , **_snake_case ) encoding.update(_snake_case ) return encoding def _a ( self : Any , *_snake_case : Tuple , **_snake_case : List[Any] ): """simple docstring""" return self.tokenizer.batch_decode(*_snake_case , **_snake_case ) def _a ( self : Dict , *_snake_case : Dict , **_snake_case : List[str] ): """simple docstring""" return self.tokenizer.decode(*_snake_case , **_snake_case ) @property def _a ( self : Tuple ): """simple docstring""" A__ = self.tokenizer.model_input_names A__ = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
52
1
import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : List[Any] = (KDPMaDiscreteScheduler,) A__ : Dict = 10 def _a ( self : Union[str, Any] , **_snake_case : Optional[int] ): """simple docstring""" A__ = { 'num_train_timesteps': 11_00, 'beta_start': 0.0001, 'beta_end': 0.02, 'beta_schedule': 'linear', } config.update(**_snake_case ) return config def _a ( self : List[Any] ): """simple docstring""" for timesteps in [10, 50, 1_00, 10_00]: self.check_over_configs(num_train_timesteps=_snake_case ) def _a ( self : Union[str, Any] ): """simple docstring""" for beta_start, beta_end in zip([0.0_0001, 0.0001, 0.001] , [0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=_snake_case , beta_end=_snake_case ) def _a ( self : int ): """simple docstring""" for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=_snake_case ) def _a ( self : List[Any] ): """simple docstring""" for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_snake_case ) def _a ( self : Tuple ): """simple docstring""" A__ = self.scheduler_classes[0] A__ = self.get_scheduler_config(prediction_type='v_prediction' ) A__ = scheduler_class(**_snake_case ) scheduler.set_timesteps(self.num_inference_steps ) A__ = self.dummy_model() A__ = self.dummy_sample_deter * scheduler.init_noise_sigma A__ = sample.to(_snake_case ) for i, t in enumerate(scheduler.timesteps ): A__ = scheduler.scale_model_input(_snake_case , _snake_case ) A__ = model(_snake_case , _snake_case ) A__ = scheduler.step(_snake_case , _snake_case , _snake_case ) A__ = output.prev_sample A__ = torch.sum(torch.abs(_snake_case ) ) A__ = torch.mean(torch.abs(_snake_case ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.69_34E-07 ) < 1E-2 assert abs(result_mean.item() - 6.11_12E-10 ) < 1E-3 else: # CUDA assert abs(result_sum.item() - 4.6_93_42_86_50_17_09_72E-07 ) < 1E-2 assert abs(result_mean.item() - 0.0002 ) < 1E-3 def _a ( self : Optional[int] ): """simple docstring""" if torch_device == "mps": return A__ = self.scheduler_classes[0] A__ = self.get_scheduler_config() A__ = scheduler_class(**_snake_case ) scheduler.set_timesteps(self.num_inference_steps ) A__ = self.dummy_model() A__ = self.dummy_sample_deter * scheduler.init_noise_sigma A__ = sample.to(_snake_case ) for i, t in enumerate(scheduler.timesteps ): A__ = scheduler.scale_model_input(_snake_case , _snake_case ) A__ = model(_snake_case , _snake_case ) A__ = scheduler.step(_snake_case , _snake_case , _snake_case ) A__ = output.prev_sample A__ = torch.sum(torch.abs(_snake_case ) ) A__ = torch.mean(torch.abs(_snake_case ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 20.4125 ) < 1E-2 assert abs(result_mean.item() - 0.0266 ) < 1E-3 else: # CUDA assert abs(result_sum.item() - 20.4125 ) < 1E-2 assert abs(result_mean.item() - 0.0266 ) < 1E-3 def _a ( self : Dict ): """simple docstring""" if torch_device == "mps": return A__ = self.scheduler_classes[0] A__ = self.get_scheduler_config() A__ = scheduler_class(**_snake_case ) scheduler.set_timesteps(self.num_inference_steps , device=_snake_case ) A__ = self.dummy_model() A__ = self.dummy_sample_deter.to(_snake_case ) * scheduler.init_noise_sigma for t in scheduler.timesteps: A__ = scheduler.scale_model_input(_snake_case , _snake_case ) A__ = model(_snake_case , _snake_case ) A__ = scheduler.step(_snake_case , _snake_case , _snake_case ) A__ = output.prev_sample A__ = torch.sum(torch.abs(_snake_case ) ) A__ = torch.mean(torch.abs(_snake_case ) ) if str(_snake_case ).startswith('cpu' ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 20.4125 ) < 1E-2 assert abs(result_mean.item() - 0.0266 ) < 1E-3 else: # CUDA assert abs(result_sum.item() - 20.4125 ) < 1E-2 assert abs(result_mean.item() - 0.0266 ) < 1E-3
52
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) SCREAMING_SNAKE_CASE__ = { '''configuration_xlm_roberta''': [ '''XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XLMRobertaConfig''', '''XLMRobertaOnnxConfig''', ], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = ['''XLMRobertaTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = ['''XLMRobertaTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XLMRobertaForCausalLM''', '''XLMRobertaForMaskedLM''', '''XLMRobertaForMultipleChoice''', '''XLMRobertaForQuestionAnswering''', '''XLMRobertaForSequenceClassification''', '''XLMRobertaForTokenClassification''', '''XLMRobertaModel''', '''XLMRobertaPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXLMRobertaForCausalLM''', '''TFXLMRobertaForMaskedLM''', '''TFXLMRobertaForMultipleChoice''', '''TFXLMRobertaForQuestionAnswering''', '''TFXLMRobertaForSequenceClassification''', '''TFXLMRobertaForTokenClassification''', '''TFXLMRobertaModel''', '''TFXLMRobertaPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''FlaxXLMRobertaForMaskedLM''', '''FlaxXLMRobertaForCausalLM''', '''FlaxXLMRobertaForMultipleChoice''', '''FlaxXLMRobertaForQuestionAnswering''', '''FlaxXLMRobertaForSequenceClassification''', '''FlaxXLMRobertaForTokenClassification''', '''FlaxXLMRobertaModel''', '''FlaxXLMRobertaPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_xlm_roberta import ( XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMRobertaConfig, XLMRobertaOnnxConfig, ) try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlm_roberta import XLMRobertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlm_roberta_fast import XLMRobertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm_roberta import ( XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, XLMRobertaForCausalLM, XLMRobertaForMaskedLM, XLMRobertaForMultipleChoice, XLMRobertaForQuestionAnswering, XLMRobertaForSequenceClassification, XLMRobertaForTokenClassification, XLMRobertaModel, XLMRobertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlm_roberta import ( TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLMRobertaForCausalLM, TFXLMRobertaForMaskedLM, TFXLMRobertaForMultipleChoice, TFXLMRobertaForQuestionAnswering, TFXLMRobertaForSequenceClassification, TFXLMRobertaForTokenClassification, TFXLMRobertaModel, TFXLMRobertaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xlm_roberta import ( FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxXLMRobertaForCausalLM, FlaxXLMRobertaForMaskedLM, FlaxXLMRobertaForMultipleChoice, FlaxXLMRobertaForQuestionAnswering, FlaxXLMRobertaForSequenceClassification, FlaxXLMRobertaForTokenClassification, FlaxXLMRobertaModel, FlaxXLMRobertaPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
52
1
from ...configuration_utils import PretrainedConfig from ...utils import logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = { '''transfo-xl-wt103''': '''https://huggingface.co/transfo-xl-wt103/resolve/main/config.json''', } class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : List[Any] = "transfo-xl" A__ : Optional[int] = ["mems"] A__ : Dict = { "n_token": "vocab_size", "hidden_size": "d_model", "num_attention_heads": "n_head", "num_hidden_layers": "n_layer", } def __init__( self : Optional[Any] , _snake_case : Tuple=26_77_35 , _snake_case : Union[str, Any]=[2_00_00, 4_00_00, 20_00_00] , _snake_case : int=10_24 , _snake_case : int=10_24 , _snake_case : int=16 , _snake_case : Any=64 , _snake_case : int=40_96 , _snake_case : Dict=4 , _snake_case : int=False , _snake_case : Optional[int]=18 , _snake_case : List[str]=16_00 , _snake_case : Optional[Any]=10_00 , _snake_case : Union[str, Any]=True , _snake_case : List[Any]=True , _snake_case : Optional[Any]=0 , _snake_case : Union[str, Any]=-1 , _snake_case : Tuple=True , _snake_case : Tuple=0.1 , _snake_case : Union[str, Any]=0.0 , _snake_case : str=True , _snake_case : Union[str, Any]="normal" , _snake_case : Optional[Any]=0.01 , _snake_case : List[str]=0.01 , _snake_case : Union[str, Any]=0.02 , _snake_case : Optional[int]=1E-5 , _snake_case : int=0 , **_snake_case : Union[str, Any] , ): """simple docstring""" A__ = vocab_size A__ = [] self.cutoffs.extend(_snake_case ) if proj_share_all_but_first: A__ = [False] + [True] * len(self.cutoffs ) else: A__ = [False] + [False] * len(self.cutoffs ) A__ = d_model A__ = d_embed A__ = d_head A__ = d_inner A__ = div_val A__ = pre_lnorm A__ = n_layer A__ = n_head A__ = mem_len A__ = same_length A__ = attn_type A__ = clamp_len A__ = sample_softmax A__ = adaptive A__ = dropout A__ = dropatt A__ = untie_r A__ = init A__ = init_range A__ = proj_init_std A__ = init_std A__ = layer_norm_epsilon super().__init__(eos_token_id=_snake_case , **_snake_case ) @property def _a ( self : Optional[Any] ): """simple docstring""" logger.info(F'''The model {self.model_type} is one of the few models that has no sequence length limit.''' ) return -1 @max_position_embeddings.setter def _a ( self : Any , _snake_case : Any ): """simple docstring""" raise NotImplementedError( F'''The model {self.model_type} is one of the few models that has no sequence length limit.''' )
52
import argparse import ast import logging import os import sys import pandas as pd import torch from tqdm import tqdm from transformers import BartForConditionalGeneration, RagRetriever, RagSequenceForGeneration, RagTokenForGeneration from transformers import logging as transformers_logging sys.path.append(os.path.join(os.getcwd())) # noqa: E402 # isort:skip from utils_rag import exact_match_score, fa_score # noqa: E402 # isort:skip SCREAMING_SNAKE_CASE__ = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) transformers_logging.set_verbosity_info() def A ( __UpperCamelCase ) -> Tuple: if "token" in model_name_or_path: return "rag_token" if "sequence" in model_name_or_path: return "rag_sequence" if "bart" in model_name_or_path: return "bart" return None def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[Any]: return max(metric_fn(__UpperCamelCase , __UpperCamelCase ) for gt in ground_truths ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[str]: A__ = [line.strip() for line in open(__UpperCamelCase , 'r' ).readlines()] A__ = [] if args.gold_data_mode == "qa": A__ = pd.read_csv(__UpperCamelCase , sep='\t' , header=__UpperCamelCase ) for answer_list in data[1]: A__ = ast.literal_eval(__UpperCamelCase ) answers.append(__UpperCamelCase ) else: A__ = [line.strip() for line in open(__UpperCamelCase , 'r' ).readlines()] A__ = [[reference] for reference in references] A__ = A__ = A__ = 0 for prediction, ground_truths in zip(__UpperCamelCase , __UpperCamelCase ): total += 1 em += metric_max_over_ground_truths(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) fa += metric_max_over_ground_truths(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) A__ = 100.0 * em / total A__ = 100.0 * fa / total logger.info(f'''F1: {fa:.2f}''' ) logger.info(f'''EM: {em:.2f}''' ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: A__ = args.k A__ = [line.strip() for line in open(__UpperCamelCase , 'r' ).readlines()] A__ = [line.strip() for line in open(__UpperCamelCase , 'r' ).readlines()] A__ = A__ = 0 for hypo, reference in zip(__UpperCamelCase , __UpperCamelCase ): A__ = set(hypo.split('\t' )[:k] ) A__ = set(reference.split('\t' ) ) total += 1 em += len(hypo_provenance & ref_provenance ) / k A__ = 100.0 * em / total logger.info(f'''Precision@{k}: {em: .2f}''' ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Union[str, Any]: def strip_title(__UpperCamelCase ): if title.startswith('"' ): A__ = title[1:] if title.endswith('"' ): A__ = title[:-1] return title A__ = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( __UpperCamelCase , return_tensors='pt' , padding=__UpperCamelCase , truncation=__UpperCamelCase , )['input_ids'].to(args.device ) A__ = rag_model.rag.question_encoder(__UpperCamelCase ) A__ = question_enc_outputs[0] A__ = rag_model.retriever( __UpperCamelCase , question_enc_pool_output.cpu().detach().to(torch.floataa ).numpy() , prefix=rag_model.rag.generator.config.prefix , n_docs=rag_model.config.n_docs , return_tensors='pt' , ) A__ = rag_model.retriever.index.get_doc_dicts(result.doc_ids ) A__ = [] for docs in all_docs: A__ = [strip_title(__UpperCamelCase ) for title in docs['title']] provenance_strings.append('\t'.join(__UpperCamelCase ) ) return provenance_strings def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Optional[Any]: with torch.no_grad(): A__ = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( __UpperCamelCase , return_tensors='pt' , padding=__UpperCamelCase , truncation=__UpperCamelCase ) A__ = inputs_dict.input_ids.to(args.device ) A__ = inputs_dict.attention_mask.to(args.device ) A__ = rag_model.generate( # rag_model overwrites generate __UpperCamelCase , attention_mask=__UpperCamelCase , num_beams=args.num_beams , min_length=args.min_length , max_length=args.max_length , early_stopping=__UpperCamelCase , num_return_sequences=1 , bad_words_ids=[[0, 0]] , ) A__ = rag_model.retriever.generator_tokenizer.batch_decode(__UpperCamelCase , skip_special_tokens=__UpperCamelCase ) if args.print_predictions: for q, a in zip(__UpperCamelCase , __UpperCamelCase ): logger.info('Q: {} - A: {}'.format(__UpperCamelCase , __UpperCamelCase ) ) return answers def A ( ) -> Any: A__ = argparse.ArgumentParser() parser.add_argument( '--model_type' , choices=['rag_sequence', 'rag_token', 'bart'] , type=__UpperCamelCase , help=( 'RAG model type: rag_sequence, rag_token or bart, if none specified, the type is inferred from the' ' model_name_or_path' ) , ) parser.add_argument( '--index_name' , default=__UpperCamelCase , choices=['exact', 'compressed', 'legacy'] , type=__UpperCamelCase , help='RAG model retriever type' , ) parser.add_argument( '--index_path' , default=__UpperCamelCase , type=__UpperCamelCase , help='Path to the retrieval index' , ) parser.add_argument('--n_docs' , default=5 , type=__UpperCamelCase , help='Number of retrieved docs' ) parser.add_argument( '--model_name_or_path' , default=__UpperCamelCase , type=__UpperCamelCase , required=__UpperCamelCase , help='Path to pretrained checkpoints or model identifier from huggingface.co/models' , ) parser.add_argument( '--eval_mode' , choices=['e2e', 'retrieval'] , default='e2e' , type=__UpperCamelCase , help=( 'Evaluation mode, e2e calculates exact match and F1 of the downstream task, retrieval calculates' ' precision@k.' ) , ) parser.add_argument('--k' , default=1 , type=__UpperCamelCase , help='k for the precision@k calculation' ) parser.add_argument( '--evaluation_set' , default=__UpperCamelCase , type=__UpperCamelCase , required=__UpperCamelCase , help='Path to a file containing evaluation samples' , ) parser.add_argument( '--gold_data_path' , default=__UpperCamelCase , type=__UpperCamelCase , required=__UpperCamelCase , help='Path to a tab-separated file with gold samples' , ) parser.add_argument( '--gold_data_mode' , default='qa' , type=__UpperCamelCase , choices=['qa', 'ans'] , help=( 'Format of the gold data file' 'qa - a single line in the following format: question [tab] answer_list' 'ans - a single line of the gold file contains the expected answer string' ) , ) parser.add_argument( '--predictions_path' , type=__UpperCamelCase , default='predictions.txt' , help='Name of the predictions file, to be stored in the checkpoints directory' , ) parser.add_argument( '--eval_all_checkpoints' , action='store_true' , help='Evaluate all checkpoints starting with the same prefix as model_name ending and ending with step number' , ) parser.add_argument( '--eval_batch_size' , default=8 , type=__UpperCamelCase , help='Batch size per GPU/CPU for evaluation.' , ) parser.add_argument( '--recalculate' , help='Recalculate predictions even if the prediction file exists' , action='store_true' , ) parser.add_argument( '--num_beams' , default=4 , type=__UpperCamelCase , help='Number of beams to be used when generating answers' , ) parser.add_argument('--min_length' , default=1 , type=__UpperCamelCase , help='Min length of the generated answers' ) parser.add_argument('--max_length' , default=50 , type=__UpperCamelCase , help='Max length of the generated answers' ) parser.add_argument( '--print_predictions' , action='store_true' , help='If True, prints predictions while evaluating.' , ) parser.add_argument( '--print_docs' , action='store_true' , help='If True, prints docs retried while generating.' , ) A__ = parser.parse_args() A__ = torch.device('cuda' if torch.cuda.is_available() else 'cpu' ) return args def A ( __UpperCamelCase ) -> int: A__ = {} if args.model_type is None: A__ = infer_model_type(args.model_name_or_path ) assert args.model_type is not None if args.model_type.startswith('rag' ): A__ = RagTokenForGeneration if args.model_type == 'rag_token' else RagSequenceForGeneration A__ = args.n_docs if args.index_name is not None: A__ = args.index_name if args.index_path is not None: A__ = args.index_path else: A__ = BartForConditionalGeneration A__ = ( [f.path for f in os.scandir(args.model_name_or_path ) if f.is_dir()] if args.eval_all_checkpoints else [args.model_name_or_path] ) logger.info('Evaluate the following checkpoints: %s' , __UpperCamelCase ) A__ = get_scores if args.eval_mode == 'e2e' else get_precision_at_k A__ = evaluate_batch_eae if args.eval_mode == 'e2e' else evaluate_batch_retrieval for checkpoint in checkpoints: if os.path.exists(args.predictions_path ) and (not args.recalculate): logger.info('Calculating metrics based on an existing predictions file: {}'.format(args.predictions_path ) ) score_fn(__UpperCamelCase , args.predictions_path , args.gold_data_path ) continue logger.info('***** Running evaluation for {} *****'.format(__UpperCamelCase ) ) logger.info(' Batch size = %d' , args.eval_batch_size ) logger.info(' Predictions will be stored under {}'.format(args.predictions_path ) ) if args.model_type.startswith('rag' ): A__ = RagRetriever.from_pretrained(__UpperCamelCase , **__UpperCamelCase ) A__ = model_class.from_pretrained(__UpperCamelCase , retriever=__UpperCamelCase , **__UpperCamelCase ) model.retriever.init_retrieval() else: A__ = model_class.from_pretrained(__UpperCamelCase , **__UpperCamelCase ) model.to(args.device ) with open(args.evaluation_set , 'r' ) as eval_file, open(args.predictions_path , 'w' ) as preds_file: A__ = [] for line in tqdm(__UpperCamelCase ): questions.append(line.strip() ) if len(__UpperCamelCase ) == args.eval_batch_size: A__ = evaluate_batch_fn(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) preds_file.write('\n'.join(__UpperCamelCase ) + '\n' ) preds_file.flush() A__ = [] if len(__UpperCamelCase ) > 0: A__ = evaluate_batch_fn(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) preds_file.write('\n'.join(__UpperCamelCase ) ) preds_file.flush() score_fn(__UpperCamelCase , args.predictions_path , args.gold_data_path ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = get_args() main(args)
52
1
from random import randint from tempfile import TemporaryFile import numpy as np def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> str: A__ = 0 if start < end: A__ = randint(__UpperCamelCase , __UpperCamelCase ) A__ = a[end] A__ = a[pivot] A__ = temp A__ , A__ = _in_place_partition(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) count += _in_place_quick_sort(__UpperCamelCase , __UpperCamelCase , p - 1 ) count += _in_place_quick_sort(__UpperCamelCase , p + 1 , __UpperCamelCase ) return count def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[Any]: A__ = 0 A__ = randint(__UpperCamelCase , __UpperCamelCase ) A__ = a[end] A__ = a[pivot] A__ = temp A__ = start - 1 for index in range(__UpperCamelCase , __UpperCamelCase ): count += 1 if a[index] < a[end]: # check if current val is less than pivot value A__ = new_pivot_index + 1 A__ = a[new_pivot_index] A__ = a[index] A__ = temp A__ = a[new_pivot_index + 1] A__ = a[end] A__ = temp return new_pivot_index + 1, count SCREAMING_SNAKE_CASE__ = TemporaryFile() SCREAMING_SNAKE_CASE__ = 1_0_0 # 1000 elements are to be sorted SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = 0, 1 # mean and standard deviation SCREAMING_SNAKE_CASE__ = np.random.normal(mu, sigma, p) np.save(outfile, X) print('''The array is''') print(X) outfile.seek(0) # using the same array SCREAMING_SNAKE_CASE__ = np.load(outfile) SCREAMING_SNAKE_CASE__ = len(M) - 1 SCREAMING_SNAKE_CASE__ = _in_place_quick_sort(M, 0, r) print( '''No of Comparisons for 100 elements selected from a standard normal distribution''' '''is :''' ) print(z)
52
import inspect import unittest from transformers import ViTHybridConfig from transformers.testing_utils import require_accelerate, require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class __lowerCAmelCase : """simple docstring""" def __init__( self : List[Any] , _snake_case : Any , _snake_case : Optional[int]=13 , _snake_case : Optional[Any]=64 , _snake_case : List[str]=2 , _snake_case : Any=3 , _snake_case : Union[str, Any]=True , _snake_case : Dict=True , _snake_case : int=32 , _snake_case : int=5 , _snake_case : Union[str, Any]=4 , _snake_case : int=37 , _snake_case : Tuple="gelu" , _snake_case : Optional[int]=0.1 , _snake_case : Dict=0.1 , _snake_case : List[str]=10 , _snake_case : Union[str, Any]=0.02 , _snake_case : Dict=[1, 16, 4, 4] , _snake_case : Dict=None , ): """simple docstring""" A__ = parent A__ = batch_size A__ = image_size A__ = patch_size A__ = num_channels A__ = is_training A__ = use_labels A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = type_sequence_label_size A__ = initializer_range A__ = scope A__ = backbone_featmap_shape # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) # the number of patches is based on the feature map of the backbone, which by default uses an output stride # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size A__ = (self.image_size // 32) ** 2 A__ = num_patches + 1 def _a ( self : Any ): """simple docstring""" A__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A__ = self.get_config() return config, pixel_values, labels def _a ( self : Tuple ): """simple docstring""" A__ = { 'global_padding': 'same', 'layer_type': 'bottleneck', 'depths': [3, 4, 9], 'out_features': ['stage1', 'stage2', 'stage3'], 'embedding_dynamic_padding': True, 'hidden_sizes': [4, 8, 16, 32], 'num_groups': 2, } return ViTHybridConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_snake_case , initializer_range=self.initializer_range , backbone_featmap_shape=self.backbone_featmap_shape , backbone_config=_snake_case , ) def _a ( self : int , _snake_case : Optional[int] , _snake_case : Union[str, Any] , _snake_case : Optional[int] ): """simple docstring""" A__ = ViTHybridModel(config=_snake_case ) model.to(_snake_case ) model.eval() A__ = model(_snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _a ( self : List[str] , _snake_case : str , _snake_case : Union[str, Any] , _snake_case : Any ): """simple docstring""" A__ = self.type_sequence_label_size A__ = ViTHybridForImageClassification(_snake_case ) model.to(_snake_case ) model.eval() A__ = model(_snake_case , labels=_snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _a ( self : Dict ): """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ = config_and_inputs A__ = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class __lowerCAmelCase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Union[str, Any] = (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else () A__ : str = ( {"feature-extraction": ViTHybridModel, "image-classification": ViTHybridForImageClassification} if is_torch_available() else {} ) A__ : Union[str, Any] = False A__ : Any = False A__ : Union[str, Any] = False def _a ( self : Dict ): """simple docstring""" A__ = ViTHybridModelTester(self ) A__ = ConfigTester(self , config_class=_snake_case , has_text_modality=_snake_case , hidden_size=37 ) def _a ( self : int ): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='ViT does not use inputs_embeds' ) def _a ( self : int ): """simple docstring""" pass def _a ( self : int ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(_snake_case ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A__ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_snake_case , nn.Linear ) ) def _a ( self : List[str] ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(_snake_case ) A__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ = [*signature.parameters.keys()] A__ = ['pixel_values'] self.assertListEqual(arg_names[:1] , _snake_case ) def _a ( self : Any ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def _a ( self : str ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_snake_case ) def _a ( self : Any ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = _config_zero_init(_snake_case ) for model_class in self.all_model_classes: A__ = model_class(config=_snake_case ) # Skip the check for the backbone for name, module in model.named_modules(): if module.__class__.__name__ == "ViTHybridPatchEmbeddings": A__ = [F'''{name}.{key}''' for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() , [0.0, 1.0] , msg=F'''Parameter {name} of model {model_class} seems not properly initialized''' , ) @slow def _a ( self : int ): """simple docstring""" for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = ViTHybridModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def A ( ) -> Union[str, Any]: A__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @cached_property def _a ( self : Tuple ): """simple docstring""" return ( ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def _a ( self : Optional[Any] ): """simple docstring""" A__ = ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to( _snake_case ) A__ = self.default_image_processor A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ).to(_snake_case ) # forward pass with torch.no_grad(): A__ = model(**_snake_case ) # verify the logits A__ = torch.Size((1, 10_00) ) self.assertEqual(outputs.logits.shape , _snake_case ) A__ = torch.tensor([-1.9090, -0.4993, -0.2389] ).to(_snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _snake_case , atol=1E-4 ) ) @slow @require_accelerate def _a ( self : List[Any] ): """simple docstring""" A__ = ViTHybridImageProcessor.from_pretrained('google/vit-hybrid-base-bit-384' ) A__ = ViTHybridForImageClassification.from_pretrained('google/vit-hybrid-base-bit-384' , device_map='auto' ) A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ) A__ = model(**_snake_case ) A__ = outputs.logits # model predicts one of the 1000 ImageNet classes A__ = logits.argmax(-1 ).item() self.assertTrue(model.config.idalabel[predicted_class_idx] , 'tabby, tabby cat' )
52
1
import sacrebleu as scb from packaging import version from sacrebleu import TER import datasets SCREAMING_SNAKE_CASE__ = '''\ @inproceedings{snover-etal-2006-study, title = "A Study of Translation Edit Rate with Targeted Human Annotation", author = "Snover, Matthew and Dorr, Bonnie and Schwartz, Rich and Micciulla, Linnea and Makhoul, John", booktitle = "Proceedings of the 7th Conference of the Association for Machine Translation in the Americas: Technical Papers", month = aug # " 8-12", year = "2006", address = "Cambridge, Massachusetts, USA", publisher = "Association for Machine Translation in the Americas", url = "https://aclanthology.org/2006.amta-papers.25", pages = "223--231", } @inproceedings{post-2018-call, title = "A Call for Clarity in Reporting {BLEU} Scores", author = "Post, Matt", booktitle = "Proceedings of the Third Conference on Machine Translation: Research Papers", month = oct, year = "2018", address = "Belgium, Brussels", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/W18-6319", pages = "186--191", } ''' SCREAMING_SNAKE_CASE__ = '''\ TER (Translation Edit Rate, also called Translation Error Rate) is a metric to quantify the edit operations that a hypothesis requires to match a reference translation. We use the implementation that is already present in sacrebleu (https://github.com/mjpost/sacreBLEU#ter), which in turn is inspired by the TERCOM implementation, which can be found here: https://github.com/jhclark/tercom. The implementation here is slightly different from sacrebleu in terms of the required input format. The length of the references and hypotheses lists need to be the same, so you may need to transpose your references compared to sacrebleu\'s required input format. See https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534 See the README.md file at https://github.com/mjpost/sacreBLEU#ter for more information. ''' SCREAMING_SNAKE_CASE__ = ''' Produces TER scores alongside the number of edits and reference length. Args: predictions (list of str): The system stream (a sequence of segments). references (list of list of str): A list of one or more reference streams (each a sequence of segments). normalized (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`. ignore_punct (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`. support_zh_ja_chars (boolean): If `True`, tokenization/normalization supports processing of Chinese characters, as well as Japanese Kanji, Hiragana, Katakana, and Phonetic Extensions of Katakana. Only applies if `normalized = True`. Defaults to `False`. case_sensitive (boolean): If `False`, makes all predictions and references lowercase to ignore differences in case. Defaults to `False`. Returns: \'score\' (float): TER score (num_edits / sum_ref_lengths * 100) \'num_edits\' (int): The cumulative number of edits \'ref_length\' (float): The cumulative average reference length Examples: Example 1: >>> predictions = ["does this sentence match??", ... "what about this sentence?", ... "What did the TER metric user say to the developer?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"], ... ["Your jokes are...", "...TERrible"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... case_sensitive=True) >>> print(results) {\'score\': 150.0, \'num_edits\': 15, \'ref_length\': 10.0} Example 2: >>> predictions = ["does this sentence match??", ... "what about this sentence?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... case_sensitive=True) >>> print(results) {\'score\': 62.5, \'num_edits\': 5, \'ref_length\': 8.0} Example 3: >>> predictions = ["does this sentence match??", ... "what about this sentence?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... normalized=True, ... case_sensitive=True) >>> print(results) {\'score\': 57.14285714285714, \'num_edits\': 6, \'ref_length\': 10.5} Example 4: >>> predictions = ["does this sentence match??", ... "what about this sentence?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... ignore_punct=True, ... case_sensitive=False) >>> print(results) {\'score\': 0.0, \'num_edits\': 0, \'ref_length\': 8.0} Example 5: >>> predictions = ["does this sentence match??", ... "what about this sentence?", ... "What did the TER metric user say to the developer?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"], ... ["Your jokes are...", "...TERrible"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... ignore_punct=True, ... case_sensitive=False) >>> print(results) {\'score\': 100.0, \'num_edits\': 10, \'ref_length\': 10.0} ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __lowerCAmelCase ( datasets.Metric ): """simple docstring""" def _a ( self : Optional[Any] ): """simple docstring""" if version.parse(scb.__version__ ) < version.parse('1.4.12' ): raise ImportWarning( 'To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn\'t match this condition.\n' 'You can install it with `pip install "sacrebleu>=1.4.12"`.' ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage='http://www.cs.umd.edu/~snover/tercom/' , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('string' , id='sequence' ), 'references': datasets.Sequence(datasets.Value('string' , id='sequence' ) , id='references' ), } ) , codebase_urls=['https://github.com/mjpost/sacreBLEU#ter'] , reference_urls=[ 'https://github.com/jhclark/tercom', ] , ) def _a ( self : int , _snake_case : Any , _snake_case : Optional[Any] , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = False , ): """simple docstring""" A__ = len(references[0] ) if any(len(_snake_case ) != references_per_prediction for refs in references ): raise ValueError('Sacrebleu requires the same number of references for each prediction' ) A__ = [[refs[i] for refs in references] for i in range(_snake_case )] A__ = TER( normalized=_snake_case , no_punct=_snake_case , asian_support=_snake_case , case_sensitive=_snake_case , ) A__ = sb_ter.corpus_score(_snake_case , _snake_case ) return {"score": output.score, "num_edits": output.num_edits, "ref_length": output.ref_length}
52
def A ( __UpperCamelCase ) -> bool: return number & 1 == 0 if __name__ == "__main__": import doctest doctest.testmod()
52
1
import itertools import string from collections.abc import Generator, Iterable def A ( __UpperCamelCase , __UpperCamelCase ) -> Generator[tuple[str, ...], None, None]: A__ = iter(__UpperCamelCase ) while True: A__ = tuple(itertools.islice(__UpperCamelCase , __UpperCamelCase ) ) if not chunk: return yield chunk def A ( __UpperCamelCase ) -> str: A__ = ''.join([c.upper() for c in dirty if c in string.ascii_letters] ) A__ = '' if len(__UpperCamelCase ) < 2: return dirty for i in range(len(__UpperCamelCase ) - 1 ): clean += dirty[i] if dirty[i] == dirty[i + 1]: clean += "X" clean += dirty[-1] if len(__UpperCamelCase ) & 1: clean += "X" return clean def A ( __UpperCamelCase ) -> list[str]: # I and J are used interchangeably to allow # us to use a 5x5 table (25 letters) A__ = 'ABCDEFGHIKLMNOPQRSTUVWXYZ' # we're using a list instead of a '2d' array because it makes the math # for setting up the table and doing the actual encoding/decoding simpler A__ = [] # copy key chars into the table if they are in `alphabet` ignoring duplicates for char in key.upper(): if char not in table and char in alphabet: table.append(__UpperCamelCase ) # fill the rest of the table in with the remaining alphabet chars for char in alphabet: if char not in table: table.append(__UpperCamelCase ) return table def A ( __UpperCamelCase , __UpperCamelCase ) -> str: A__ = generate_table(__UpperCamelCase ) A__ = prepare_input(__UpperCamelCase ) A__ = '' # https://en.wikipedia.org/wiki/Playfair_cipher#Description for chara, chara in chunker(__UpperCamelCase , 2 ): A__ , A__ = divmod(table.index(__UpperCamelCase ) , 5 ) A__ , A__ = divmod(table.index(__UpperCamelCase ) , 5 ) if rowa == rowa: ciphertext += table[rowa * 5 + (cola + 1) % 5] ciphertext += table[rowa * 5 + (cola + 1) % 5] elif cola == cola: ciphertext += table[((rowa + 1) % 5) * 5 + cola] ciphertext += table[((rowa + 1) % 5) * 5 + cola] else: # rectangle ciphertext += table[rowa * 5 + cola] ciphertext += table[rowa * 5 + cola] return ciphertext def A ( __UpperCamelCase , __UpperCamelCase ) -> str: A__ = generate_table(__UpperCamelCase ) A__ = '' # https://en.wikipedia.org/wiki/Playfair_cipher#Description for chara, chara in chunker(__UpperCamelCase , 2 ): A__ , A__ = divmod(table.index(__UpperCamelCase ) , 5 ) A__ , A__ = divmod(table.index(__UpperCamelCase ) , 5 ) if rowa == rowa: plaintext += table[rowa * 5 + (cola - 1) % 5] plaintext += table[rowa * 5 + (cola - 1) % 5] elif cola == cola: plaintext += table[((rowa - 1) % 5) * 5 + cola] plaintext += table[((rowa - 1) % 5) * 5 + cola] else: # rectangle plaintext += table[rowa * 5 + cola] plaintext += table[rowa * 5 + cola] return plaintext
52
from typing import Dict from .base import GenericTensor, Pipeline class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def _a ( self : Any , _snake_case : str=None , _snake_case : Dict=None , _snake_case : Any=None , **_snake_case : str ): """simple docstring""" if tokenize_kwargs is None: A__ = {} if truncation is not None: if "truncation" in tokenize_kwargs: raise ValueError( 'truncation parameter defined twice (given as keyword argument as well as in tokenize_kwargs)' ) A__ = truncation A__ = tokenize_kwargs A__ = {} if return_tensors is not None: A__ = return_tensors return preprocess_params, {}, postprocess_params def _a ( self : Any , _snake_case : Dict , **_snake_case : Optional[Any] ): """simple docstring""" A__ = self.framework A__ = self.tokenizer(_snake_case , return_tensors=_snake_case , **_snake_case ) return model_inputs def _a ( self : List[Any] , _snake_case : Dict ): """simple docstring""" A__ = self.model(**_snake_case ) return model_outputs def _a ( self : Optional[Any] , _snake_case : List[Any] , _snake_case : str=False ): """simple docstring""" if return_tensors: return model_outputs[0] if self.framework == "pt": return model_outputs[0].tolist() elif self.framework == "tf": return model_outputs[0].numpy().tolist() def __call__( self : Dict , *_snake_case : int , **_snake_case : List[str] ): """simple docstring""" return super().__call__(*_snake_case , **_snake_case )
52
1
import io import json import fsspec import pytest from datasets import Dataset, DatasetDict, Features, NamedSplit, Value from datasets.io.json import JsonDatasetReader, JsonDatasetWriter from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def A ( __UpperCamelCase , __UpperCamelCase ) -> Optional[Any]: assert isinstance(__UpperCamelCase , __UpperCamelCase ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('keep_in_memory' , [False, True] ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Optional[Any]: A__ = tmp_path / 'cache' A__ = {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): A__ = JsonDatasetReader(__UpperCamelCase , cache_dir=__UpperCamelCase , keep_in_memory=__UpperCamelCase ).read() _check_json_dataset(__UpperCamelCase , __UpperCamelCase ) @pytest.mark.parametrize( 'features' , [ None, {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}, {'col_1': 'string', 'col_2': 'string', 'col_3': 'string'}, {'col_1': 'int32', 'col_2': 'int32', 'col_3': 'int32'}, {'col_1': 'float32', 'col_2': 'float32', 'col_3': 'float32'}, ] , ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> str: A__ = tmp_path / 'cache' A__ = {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'} A__ = features.copy() if features else default_expected_features A__ = ( Features({feature: Value(__UpperCamelCase ) for feature, dtype in features.items()} ) if features is not None else None ) A__ = JsonDatasetReader(__UpperCamelCase , features=__UpperCamelCase , cache_dir=__UpperCamelCase ).read() _check_json_dataset(__UpperCamelCase , __UpperCamelCase ) @pytest.mark.parametrize( 'features' , [ None, {'col_3': 'float64', 'col_1': 'string', 'col_2': 'int64'}, ] , ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[Any]: A__ = tmp_path / 'cache' A__ = {'col_3': 'float64', 'col_1': 'string', 'col_2': 'int64'} A__ = features.copy() if features else default_expected_features A__ = ( Features({feature: Value(__UpperCamelCase ) for feature, dtype in features.items()} ) if features is not None else None ) A__ = JsonDatasetReader(__UpperCamelCase , features=__UpperCamelCase , cache_dir=__UpperCamelCase ).read() assert isinstance(__UpperCamelCase , __UpperCamelCase ) assert dataset.num_rows == 2 assert dataset.num_columns == 3 assert dataset.column_names == ["col_3", "col_1", "col_2"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype def A ( __UpperCamelCase , __UpperCamelCase ) -> List[Any]: # jsonl_312_path features are {"col_3": "float64", "col_1": "string", "col_2": "int64"} A__ = {'col_2': 'int64', 'col_3': 'float64', 'col_1': 'string'} A__ = features.copy() A__ = ( Features({feature: Value(__UpperCamelCase ) for feature, dtype in features.items()} ) if features is not None else None ) A__ = tmp_path / 'cache' A__ = JsonDatasetReader(__UpperCamelCase , features=__UpperCamelCase , cache_dir=__UpperCamelCase ).read() assert isinstance(__UpperCamelCase , __UpperCamelCase ) assert dataset.num_rows == 2 assert dataset.num_columns == 3 assert dataset.column_names == ["col_2", "col_3", "col_1"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('split' , [None, NamedSplit('train' ), 'train', 'test'] ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: A__ = tmp_path / 'cache' A__ = {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'} A__ = JsonDatasetReader(__UpperCamelCase , cache_dir=__UpperCamelCase , split=__UpperCamelCase ).read() _check_json_dataset(__UpperCamelCase , __UpperCamelCase ) assert dataset.split == split if split else "train" @pytest.mark.parametrize('path_type' , [str, list] ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[str]: if issubclass(__UpperCamelCase , __UpperCamelCase ): A__ = jsonl_path elif issubclass(__UpperCamelCase , __UpperCamelCase ): A__ = [jsonl_path] A__ = tmp_path / 'cache' A__ = {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'} A__ = JsonDatasetReader(__UpperCamelCase , cache_dir=__UpperCamelCase ).read() _check_json_dataset(__UpperCamelCase , __UpperCamelCase ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=("train",) ) -> Optional[int]: assert isinstance(__UpperCamelCase , __UpperCamelCase ) for split in splits: A__ = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('keep_in_memory' , [False, True] ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: A__ = tmp_path / 'cache' A__ = {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): A__ = JsonDatasetReader({'train': jsonl_path} , cache_dir=__UpperCamelCase , keep_in_memory=__UpperCamelCase ).read() _check_json_datasetdict(__UpperCamelCase , __UpperCamelCase ) @pytest.mark.parametrize( 'features' , [ None, {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}, {'col_1': 'string', 'col_2': 'string', 'col_3': 'string'}, {'col_1': 'int32', 'col_2': 'int32', 'col_3': 'int32'}, {'col_1': 'float32', 'col_2': 'float32', 'col_3': 'float32'}, ] , ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Union[str, Any]: A__ = tmp_path / 'cache' A__ = {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'} A__ = features.copy() if features else default_expected_features A__ = ( Features({feature: Value(__UpperCamelCase ) for feature, dtype in features.items()} ) if features is not None else None ) A__ = JsonDatasetReader({'train': jsonl_path} , features=__UpperCamelCase , cache_dir=__UpperCamelCase ).read() _check_json_datasetdict(__UpperCamelCase , __UpperCamelCase ) @pytest.mark.parametrize('split' , [None, NamedSplit('train' ), 'train', 'test'] ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Tuple: if split: A__ = {split: jsonl_path} else: A__ = 'train' A__ = {'train': jsonl_path, 'test': jsonl_path} A__ = tmp_path / 'cache' A__ = {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'} A__ = JsonDatasetReader(__UpperCamelCase , cache_dir=__UpperCamelCase ).read() _check_json_datasetdict(__UpperCamelCase , __UpperCamelCase , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def A ( __UpperCamelCase ) -> List[str]: return json.load(__UpperCamelCase ) def A ( __UpperCamelCase ) -> Any: return [json.loads(__UpperCamelCase ) for line in buffer] class __lowerCAmelCase : """simple docstring""" @pytest.mark.parametrize('lines, load_json_function' , [(True, load_json_lines), (False, load_json)] ) def _a ( self : int , _snake_case : Dict , _snake_case : int , _snake_case : Any ): """simple docstring""" with io.BytesIO() as buffer: JsonDatasetWriter(_snake_case , _snake_case , lines=_snake_case ).write() buffer.seek(0 ) A__ = load_json_function(_snake_case ) assert isinstance(_snake_case , _snake_case ) assert isinstance(exported_content[0] , _snake_case ) assert len(_snake_case ) == 10 @pytest.mark.parametrize( 'orient, container, keys, len_at' , [ ('records', list, {'tokens', 'labels', 'answers', 'id'}, None), ('split', dict, {'columns', 'data'}, 'data'), ('index', dict, set('0123456789' ), None), ('columns', dict, {'tokens', 'labels', 'answers', 'id'}, 'tokens'), ('values', list, None, None), ('table', dict, {'schema', 'data'}, 'data'), ] , ) def _a ( self : Optional[int] , _snake_case : Dict , _snake_case : Union[str, Any] , _snake_case : str , _snake_case : Optional[Any] , _snake_case : str ): """simple docstring""" with io.BytesIO() as buffer: JsonDatasetWriter(_snake_case , _snake_case , lines=_snake_case , orient=_snake_case ).write() buffer.seek(0 ) A__ = load_json(_snake_case ) assert isinstance(_snake_case , _snake_case ) if keys: if container is dict: assert exported_content.keys() == keys else: assert exported_content[0].keys() == keys else: assert not hasattr(_snake_case , 'keys' ) and not hasattr(exported_content[0] , 'keys' ) if len_at: assert len(exported_content[len_at] ) == 10 else: assert len(_snake_case ) == 10 @pytest.mark.parametrize('lines, load_json_function' , [(True, load_json_lines), (False, load_json)] ) def _a ( self : Optional[Any] , _snake_case : List[Any] , _snake_case : Tuple , _snake_case : Tuple ): """simple docstring""" with io.BytesIO() as buffer: JsonDatasetWriter(_snake_case , _snake_case , lines=_snake_case , num_proc=2 ).write() buffer.seek(0 ) A__ = load_json_function(_snake_case ) assert isinstance(_snake_case , _snake_case ) assert isinstance(exported_content[0] , _snake_case ) assert len(_snake_case ) == 10 @pytest.mark.parametrize( 'orient, container, keys, len_at' , [ ('records', list, {'tokens', 'labels', 'answers', 'id'}, None), ('split', dict, {'columns', 'data'}, 'data'), ('index', dict, set('0123456789' ), None), ('columns', dict, {'tokens', 'labels', 'answers', 'id'}, 'tokens'), ('values', list, None, None), ('table', dict, {'schema', 'data'}, 'data'), ] , ) def _a ( self : Union[str, Any] , _snake_case : Any , _snake_case : Optional[int] , _snake_case : Union[str, Any] , _snake_case : Union[str, Any] , _snake_case : Tuple ): """simple docstring""" with io.BytesIO() as buffer: JsonDatasetWriter(_snake_case , _snake_case , lines=_snake_case , orient=_snake_case , num_proc=2 ).write() buffer.seek(0 ) A__ = load_json(_snake_case ) assert isinstance(_snake_case , _snake_case ) if keys: if container is dict: assert exported_content.keys() == keys else: assert exported_content[0].keys() == keys else: assert not hasattr(_snake_case , 'keys' ) and not hasattr(exported_content[0] , 'keys' ) if len_at: assert len(exported_content[len_at] ) == 10 else: assert len(_snake_case ) == 10 def _a ( self : Tuple , _snake_case : Optional[int] ): """simple docstring""" with pytest.raises(_snake_case ): with io.BytesIO() as buffer: JsonDatasetWriter(_snake_case , _snake_case , num_proc=0 ) @pytest.mark.parametrize('compression, extension' , [('gzip', 'gz'), ('bz2', 'bz2'), ('xz', 'xz')] ) def _a ( self : Optional[int] , _snake_case : Optional[Any] , _snake_case : str , _snake_case : List[str] , _snake_case : Union[str, Any] , _snake_case : Dict ): """simple docstring""" A__ = tmp_path_factory.mktemp('data' ) / F'''test.json.{extension}''' A__ = str(shared_datadir / F'''test_file.json.{extension}''' ) JsonDatasetWriter(_snake_case , _snake_case , compression=_snake_case ).write() with fsspec.open(_snake_case , 'rb' , compression='infer' ) as f: A__ = f.read() with fsspec.open(_snake_case , 'rb' , compression='infer' ) as f: A__ = f.read() assert exported_content == original_content
52
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import numpy as np from utils_multiple_choice import MultipleChoiceDataset, Split, processors import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process SCREAMING_SNAKE_CASE__ = logging.getLogger(__name__) def A ( __UpperCamelCase , __UpperCamelCase ) -> List[Any]: return (preds == labels).mean() @dataclass class __lowerCAmelCase : """simple docstring""" A__ : str = field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "Pretrained config name or path if not the same as model_name"} ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"} ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) @dataclass class __lowerCAmelCase : """simple docstring""" A__ : str = field(metadata={"help": "The name of the task to train on: " + ", ".join(processors.keys() )} ) A__ : str = field(metadata={"help": "Should contain the data files for the task."} ) A__ : int = field( default=1_28 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) A__ : bool = field( default=UpperCAmelCase_ , metadata={"help": "Overwrite the cached training and evaluation sets"} ) def A ( ) -> Any: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. A__ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) A__ , A__ , A__ = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f'''Output directory ({training_args.output_dir}) already exists and is not empty. Use''' ' --overwrite_output_dir to overcome.' ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( 'Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s' , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.local_rank != -1 ) , training_args.fpaa , ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('Training/evaluation parameters %s' , __UpperCamelCase ) # Set seed set_seed(training_args.seed ) try: A__ = processors[data_args.task_name]() A__ = processor.get_labels() A__ = len(__UpperCamelCase ) except KeyError: raise ValueError('Task not found: %s' % (data_args.task_name) ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. A__ = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=__UpperCamelCase , finetuning_task=data_args.task_name , cache_dir=model_args.cache_dir , ) A__ = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) A__ = AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path , from_tf=bool('.ckpt' in model_args.model_name_or_path ) , config=__UpperCamelCase , cache_dir=model_args.cache_dir , ) # Get datasets A__ = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=__UpperCamelCase , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.train , ) if training_args.do_train else None ) A__ = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=__UpperCamelCase , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.dev , ) if training_args.do_eval else None ) def compute_metrics(__UpperCamelCase ) -> Dict: A__ = np.argmax(p.predictions , axis=1 ) return {"acc": simple_accuracy(__UpperCamelCase , p.label_ids )} # Data collator A__ = DataCollatorWithPadding(__UpperCamelCase , pad_to_multiple_of=8 ) if training_args.fpaa else None # Initialize our Trainer A__ = Trainer( model=__UpperCamelCase , args=__UpperCamelCase , train_dataset=__UpperCamelCase , eval_dataset=__UpperCamelCase , compute_metrics=__UpperCamelCase , data_collator=__UpperCamelCase , ) # Training if training_args.do_train: trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir ) # Evaluation A__ = {} if training_args.do_eval: logger.info('*** Evaluate ***' ) A__ = trainer.evaluate() A__ = os.path.join(training_args.output_dir , 'eval_results.txt' ) if trainer.is_world_master(): with open(__UpperCamelCase , 'w' ) as writer: logger.info('***** Eval results *****' ) for key, value in result.items(): logger.info(' %s = %s' , __UpperCamelCase , __UpperCamelCase ) writer.write('%s = %s\n' % (key, value) ) results.update(__UpperCamelCase ) return results def A ( __UpperCamelCase ) -> List[Any]: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
52
1
import torch from diffusers import UnCLIPScheduler from .test_schedulers import SchedulerCommonTest class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Union[str, Any] = (UnCLIPScheduler,) def _a ( self : List[str] , **_snake_case : Union[str, Any] ): """simple docstring""" A__ = { 'num_train_timesteps': 10_00, 'variance_type': 'fixed_small_log', 'clip_sample': True, 'clip_sample_range': 1.0, 'prediction_type': 'epsilon', } config.update(**_snake_case ) return config def _a ( self : List[str] ): """simple docstring""" for timesteps in [1, 5, 1_00, 10_00]: self.check_over_configs(num_train_timesteps=_snake_case ) def _a ( self : List[str] ): """simple docstring""" for variance in ["fixed_small_log", "learned_range"]: self.check_over_configs(variance_type=_snake_case ) def _a ( self : str ): """simple docstring""" for clip_sample in [True, False]: self.check_over_configs(clip_sample=_snake_case ) def _a ( self : Union[str, Any] ): """simple docstring""" for clip_sample_range in [1, 5, 10, 20]: self.check_over_configs(clip_sample_range=_snake_case ) def _a ( self : List[Any] ): """simple docstring""" for prediction_type in ["epsilon", "sample"]: self.check_over_configs(prediction_type=_snake_case ) def _a ( self : str ): """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=_snake_case , prev_timestep=_snake_case ) def _a ( self : Any ): """simple docstring""" A__ = self.scheduler_classes[0] A__ = self.get_scheduler_config(variance_type='fixed_small_log' ) A__ = scheduler_class(**_snake_case ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 1.00_00E-10 ) ) < 1E-5 assert torch.sum(torch.abs(scheduler._get_variance(4_87 ) - 0.054_9625 ) ) < 1E-5 assert torch.sum(torch.abs(scheduler._get_variance(9_99 ) - 0.999_4987 ) ) < 1E-5 def _a ( self : Union[str, Any] ): """simple docstring""" A__ = self.scheduler_classes[0] A__ = self.get_scheduler_config(variance_type='learned_range' ) A__ = scheduler_class(**_snake_case ) A__ = 0.5 assert scheduler._get_variance(1 , predicted_variance=_snake_case ) - -10.171_2790 < 1E-5 assert scheduler._get_variance(4_87 , predicted_variance=_snake_case ) - -5.799_8052 < 1E-5 assert scheduler._get_variance(9_99 , predicted_variance=_snake_case ) - -0.001_0011 < 1E-5 def _a ( self : Union[str, Any] ): """simple docstring""" A__ = self.scheduler_classes[0] A__ = self.get_scheduler_config() A__ = scheduler_class(**_snake_case ) A__ = scheduler.timesteps A__ = self.dummy_model() A__ = self.dummy_sample_deter A__ = torch.manual_seed(0 ) for i, t in enumerate(_snake_case ): # 1. predict noise residual A__ = model(_snake_case , _snake_case ) # 2. predict previous mean of sample x_t-1 A__ = scheduler.step(_snake_case , _snake_case , _snake_case , generator=_snake_case ).prev_sample A__ = pred_prev_sample A__ = torch.sum(torch.abs(_snake_case ) ) A__ = torch.mean(torch.abs(_snake_case ) ) assert abs(result_sum.item() - 252.268_2495 ) < 1E-2 assert abs(result_mean.item() - 0.328_4743 ) < 1E-3 def _a ( self : Optional[int] ): """simple docstring""" A__ = self.scheduler_classes[0] A__ = self.get_scheduler_config() A__ = scheduler_class(**_snake_case ) scheduler.set_timesteps(25 ) A__ = scheduler.timesteps A__ = self.dummy_model() A__ = self.dummy_sample_deter A__ = torch.manual_seed(0 ) for i, t in enumerate(_snake_case ): # 1. predict noise residual A__ = model(_snake_case , _snake_case ) if i + 1 == timesteps.shape[0]: A__ = None else: A__ = timesteps[i + 1] # 2. predict previous mean of sample x_t-1 A__ = scheduler.step( _snake_case , _snake_case , _snake_case , prev_timestep=_snake_case , generator=_snake_case ).prev_sample A__ = pred_prev_sample A__ = torch.sum(torch.abs(_snake_case ) ) A__ = torch.mean(torch.abs(_snake_case ) ) assert abs(result_sum.item() - 258.204_4983 ) < 1E-2 assert abs(result_mean.item() - 0.336_2038 ) < 1E-3 def _a ( self : str ): """simple docstring""" pass def _a ( self : List[Any] ): """simple docstring""" pass
52
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available SCREAMING_SNAKE_CASE__ = {'''configuration_mra''': ['''MRA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MraConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''MRA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MraForMaskedLM''', '''MraForMultipleChoice''', '''MraForQuestionAnswering''', '''MraForSequenceClassification''', '''MraForTokenClassification''', '''MraLayer''', '''MraModel''', '''MraPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_mra import MRA_PRETRAINED_CONFIG_ARCHIVE_MAP, MraConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mra import ( MRA_PRETRAINED_MODEL_ARCHIVE_LIST, MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, MraLayer, MraModel, MraPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
52
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available, ) SCREAMING_SNAKE_CASE__ = { '''configuration_perceiver''': ['''PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''PerceiverConfig''', '''PerceiverOnnxConfig'''], '''tokenization_perceiver''': ['''PerceiverTokenizer'''], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = ['''PerceiverFeatureExtractor'''] SCREAMING_SNAKE_CASE__ = ['''PerceiverImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''PerceiverForImageClassificationConvProcessing''', '''PerceiverForImageClassificationFourier''', '''PerceiverForImageClassificationLearned''', '''PerceiverForMaskedLM''', '''PerceiverForMultimodalAutoencoding''', '''PerceiverForOpticalFlow''', '''PerceiverForSequenceClassification''', '''PerceiverLayer''', '''PerceiverModel''', '''PerceiverPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_perceiver import PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP, PerceiverConfig, PerceiverOnnxConfig from .tokenization_perceiver import PerceiverTokenizer try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_perceiver import PerceiverFeatureExtractor from .image_processing_perceiver import PerceiverImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_perceiver import ( PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST, PerceiverForImageClassificationConvProcessing, PerceiverForImageClassificationFourier, PerceiverForImageClassificationLearned, PerceiverForMaskedLM, PerceiverForMultimodalAutoencoding, PerceiverForOpticalFlow, PerceiverForSequenceClassification, PerceiverLayer, PerceiverModel, PerceiverPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
52
import webbrowser from sys import argv from urllib.parse import parse_qs, quote import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = '''%20'''.join(argv[1:]) if len(argv) > 1 else quote(str(input('''Search: '''))) print('''Googling.....''') SCREAMING_SNAKE_CASE__ = f'https://www.google.com/search?q={query}&num=100' SCREAMING_SNAKE_CASE__ = requests.get( url, headers={'''User-Agent''': str(UserAgent().random)}, ) try: SCREAMING_SNAKE_CASE__ = ( BeautifulSoup(res.text, '''html.parser''') .find('''div''', attrs={'''class''': '''yuRUbf'''}) .find('''a''') .get('''href''') ) except AttributeError: SCREAMING_SNAKE_CASE__ = parse_qs( BeautifulSoup(res.text, '''html.parser''') .find('''div''', attrs={'''class''': '''kCrYT'''}) .find('''a''') .get('''href''') )['''url'''][0] webbrowser.open(link)
52
1
from ...configuration_utils import PretrainedConfig from ...utils import logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = { '''microsoft/trocr-base-handwritten''': ( '''https://huggingface.co/microsoft/trocr-base-handwritten/resolve/main/config.json''' ), # See all TrOCR models at https://huggingface.co/models?filter=trocr } class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Dict = "trocr" A__ : List[Any] = ["past_key_values"] A__ : List[Any] = { "num_attention_heads": "decoder_attention_heads", "hidden_size": "d_model", "num_hidden_layers": "decoder_layers", } def __init__( self : Tuple , _snake_case : Any=5_02_65 , _snake_case : Optional[Any]=10_24 , _snake_case : List[Any]=12 , _snake_case : List[Any]=16 , _snake_case : str=40_96 , _snake_case : List[str]="gelu" , _snake_case : List[Any]=5_12 , _snake_case : Dict=0.1 , _snake_case : Any=0.0 , _snake_case : str=0.0 , _snake_case : List[str]=2 , _snake_case : Tuple=0.02 , _snake_case : int=0.0 , _snake_case : Dict=True , _snake_case : List[str]=False , _snake_case : Optional[int]=True , _snake_case : Any=True , _snake_case : List[Any]=1 , _snake_case : List[Any]=0 , _snake_case : Union[str, Any]=2 , **_snake_case : Optional[int] , ): """simple docstring""" A__ = vocab_size A__ = d_model A__ = decoder_layers A__ = decoder_attention_heads A__ = decoder_ffn_dim A__ = activation_function A__ = max_position_embeddings A__ = dropout A__ = attention_dropout A__ = activation_dropout A__ = init_std A__ = decoder_layerdrop A__ = use_cache A__ = scale_embedding A__ = use_learned_position_embeddings A__ = layernorm_embedding super().__init__( pad_token_id=_snake_case , bos_token_id=_snake_case , eos_token_id=_snake_case , decoder_start_token_id=_snake_case , **_snake_case , )
52
import random import unittest import torch from diffusers import IFInpaintingPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class __lowerCAmelCase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Any = IFInpaintingPipeline A__ : Dict = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"width", "height"} A__ : List[Any] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS A__ : Dict = PipelineTesterMixin.required_optional_params - {"latents"} def _a ( self : Any ): """simple docstring""" return self._get_dummy_components() def _a ( self : Optional[int] , _snake_case : Any , _snake_case : str=0 ): """simple docstring""" if str(_snake_case ).startswith('mps' ): A__ = torch.manual_seed(_snake_case ) else: A__ = torch.Generator(device=_snake_case ).manual_seed(_snake_case ) A__ = floats_tensor((1, 3, 32, 32) , rng=random.Random(_snake_case ) ).to(_snake_case ) A__ = floats_tensor((1, 3, 32, 32) , rng=random.Random(_snake_case ) ).to(_snake_case ) A__ = { 'prompt': 'A painting of a squirrel eating a burger', 'image': image, 'mask_image': mask_image, 'generator': generator, 'num_inference_steps': 2, 'output_type': 'numpy', } return inputs @unittest.skipIf( torch_device != 'cuda' or not is_xformers_available() , reason='XFormers attention is only available with CUDA and `xformers` installed' , ) def _a ( self : Dict ): """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) def _a ( self : int ): """simple docstring""" self._test_save_load_optional_components() @unittest.skipIf(torch_device != 'cuda' , reason='float16 requires CUDA' ) def _a ( self : Optional[int] ): """simple docstring""" super().test_save_load_floataa(expected_max_diff=1E-1 ) def _a ( self : List[str] ): """simple docstring""" self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def _a ( self : Dict ): """simple docstring""" self._test_save_load_local() def _a ( self : Optional[int] ): """simple docstring""" self._test_inference_batch_single_identical( expected_max_diff=1E-2 , )
52
1
import re def A ( __UpperCamelCase ) -> str: if len(re.findall('[ATCG]' , __UpperCamelCase ) ) != len(__UpperCamelCase ): raise ValueError('Invalid Strand' ) return dna.translate(dna.maketrans('ATCG' , 'TAGC' ) ) if __name__ == "__main__": import doctest doctest.testmod()
52
import inspect import jax import jax.lax as lax import jax.numpy as jnp from ..utils import add_start_docstrings from ..utils.logging import get_logger SCREAMING_SNAKE_CASE__ = get_logger(__name__) SCREAMING_SNAKE_CASE__ = r''' Args: input_ids (`jnp.ndarray` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`PreTrainedTokenizer`]. See [`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`] for details. [What are input IDs?](../glossary#input-ids) scores (`jnp.ndarray` of shape `(batch_size, config.vocab_size)`): Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search kwargs (`Dict[str, Any]`, *optional*): Additional logits processor specific kwargs. Return: `jnp.ndarray` of shape `(batch_size, config.vocab_size)`: The processed prediction scores. ''' class __lowerCAmelCase : """simple docstring""" @add_start_docstrings(_snake_case ) def __call__( self : Optional[int] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray ): """simple docstring""" raise NotImplementedError( F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''' ) class __lowerCAmelCase : """simple docstring""" @add_start_docstrings(_snake_case ) def __call__( self : List[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray ): """simple docstring""" raise NotImplementedError( F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''' ) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" @add_start_docstrings(_snake_case ) def __call__( self : Any , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int , **_snake_case : Optional[int] ): """simple docstring""" for processor in self: A__ = inspect.signature(processor.__call__ ).parameters if len(_snake_case ) > 3: if not all(arg in kwargs for arg in list(function_args.keys() )[2:] ): raise ValueError( F'''Make sure that all the required parameters: {list(function_args.keys() )} for ''' F'''{processor.__class__} are passed to the logits processor.''' ) A__ = processor(_snake_case , _snake_case , _snake_case , **_snake_case ) else: A__ = processor(_snake_case , _snake_case , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Any , _snake_case : float ): """simple docstring""" if not isinstance(_snake_case , _snake_case ) or not (temperature > 0): raise ValueError(F'''`temperature` has to be a strictly positive float, but is {temperature}''' ) A__ = temperature def __call__( self : str , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = scores / self.temperature return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Optional[Any] , _snake_case : float , _snake_case : float = -float('Inf' ) , _snake_case : int = 1 ): """simple docstring""" if not isinstance(_snake_case , _snake_case ) or (top_p < 0 or top_p > 1.0): raise ValueError(F'''`top_p` has to be a float > 0 and < 1, but is {top_p}''' ) if not isinstance(_snake_case , _snake_case ) or (min_tokens_to_keep < 1): raise ValueError(F'''`min_tokens_to_keep` has to be a positive integer, but is {min_tokens_to_keep}''' ) A__ = top_p A__ = filter_value A__ = min_tokens_to_keep def __call__( self : str , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ , A__ = lax.top_k(_snake_case , scores.shape[-1] ) A__ = jnp.full_like(_snake_case , self.filter_value ) A__ = jax.nn.softmax(_snake_case , axis=-1 ).cumsum(axis=-1 ) A__ = cumulative_probs < self.top_p # include the token that is higher than top_p as well A__ = jnp.roll(_snake_case , 1 ) score_mask |= score_mask.at[:, 0].set(_snake_case ) # min tokens to keep A__ = score_mask.at[:, : self.min_tokens_to_keep].set(_snake_case ) A__ = jnp.where(_snake_case , _snake_case , _snake_case ) A__ = jax.lax.sort_key_val(_snake_case , _snake_case )[-1] return next_scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Union[str, Any] , _snake_case : int , _snake_case : float = -float('Inf' ) , _snake_case : int = 1 ): """simple docstring""" if not isinstance(_snake_case , _snake_case ) or top_k <= 0: raise ValueError(F'''`top_k` has to be a strictly positive integer, but is {top_k}''' ) A__ = max(_snake_case , _snake_case ) A__ = filter_value def __call__( self : Optional[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ , A__ = scores.shape A__ = jnp.full(batch_size * vocab_size , self.filter_value ) A__ = min(self.top_k , scores.shape[-1] ) # Safety check A__ , A__ = lax.top_k(_snake_case , _snake_case ) A__ = jnp.broadcast_to((jnp.arange(_snake_case ) * vocab_size)[:, None] , (batch_size, topk) ).flatten() A__ = topk_scores.flatten() A__ = topk_indices.flatten() + shift A__ = next_scores_flat.at[topk_indices_flat].set(_snake_case ) A__ = next_scores_flat.reshape(_snake_case , _snake_case ) return next_scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Any , _snake_case : int ): """simple docstring""" A__ = bos_token_id def __call__( self : Optional[int] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = jnp.full(scores.shape , -float('inf' ) ) A__ = 1 - jnp.bool_(cur_len - 1 ) A__ = jnp.where(_snake_case , new_scores.at[:, self.bos_token_id].set(0 ) , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Any , _snake_case : int , _snake_case : int ): """simple docstring""" A__ = max_length A__ = eos_token_id def __call__( self : List[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = jnp.full(scores.shape , -float('inf' ) ) A__ = 1 - jnp.bool_(cur_len - self.max_length + 1 ) A__ = jnp.where(_snake_case , new_scores.at[:, self.eos_token_id].set(0 ) , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Dict , _snake_case : int , _snake_case : int ): """simple docstring""" if not isinstance(_snake_case , _snake_case ) or min_length < 0: raise ValueError(F'''`min_length` has to be a positive integer, but is {min_length}''' ) if not isinstance(_snake_case , _snake_case ) or eos_token_id < 0: raise ValueError(F'''`eos_token_id` has to be a positive integer, but is {eos_token_id}''' ) A__ = min_length A__ = eos_token_id def __call__( self : int , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = 1 - jnp.clip(cur_len - self.min_length , 0 , 1 ) A__ = jnp.where(_snake_case , scores.at[:, self.eos_token_id].set(-float('inf' ) ) , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : int , _snake_case : Tuple , _snake_case : Union[str, Any] ): """simple docstring""" A__ = list(_snake_case ) A__ = begin_index def __call__( self : Union[str, Any] , _snake_case : Optional[Any] , _snake_case : str , _snake_case : int ): """simple docstring""" A__ = 1 - jnp.bool_(cur_len - self.begin_index ) A__ = jnp.where(_snake_case , scores.at[:, self.begin_suppress_tokens].set(-float('inf' ) ) , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : int , _snake_case : list ): """simple docstring""" A__ = list(_snake_case ) def __call__( self : List[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = scores.at[..., self.suppress_tokens].set(-float('inf' ) ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : List[str] , _snake_case : Optional[Any] ): """simple docstring""" A__ = dict(_snake_case ) # Converts the dictionary of format {index: token} containing the tokens to be forced to an array, where the # index of the array corresponds to the index of the token to be forced, for XLA compatibility. # Indexes without forced tokens will have a negative value. A__ = jnp.ones((max(force_token_map.keys() ) + 1) , dtype=jnp.intaa ) * -1 for index, token in force_token_map.items(): if token is not None: A__ = force_token_array.at[index].set(_snake_case ) A__ = jnp.intaa(_snake_case ) def __call__( self : List[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" def _force_token(_snake_case : Dict ): A__ = scores.shape[0] A__ = self.force_token_array[generation_idx] A__ = jnp.ones_like(_snake_case , dtype=scores.dtype ) * -float('inf' ) A__ = jnp.zeros((batch_size, 1) , dtype=scores.dtype ) A__ = lax.dynamic_update_slice(_snake_case , _snake_case , (0, current_token) ) return new_scores A__ = lax.cond( cur_len >= self.force_token_array.shape[0] , lambda: scores , lambda: lax.cond( self.force_token_array[cur_len] >= 0 , lambda: _force_token(_snake_case ) , lambda: scores , ) , ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Union[str, Any] , _snake_case : Union[str, Any] , _snake_case : Dict , _snake_case : List[Any] ): """simple docstring""" A__ = generate_config.eos_token_id A__ = generate_config.no_timestamps_token_id A__ = generate_config.no_timestamps_token_id + 1 A__ = decoder_input_length + 1 if generate_config.is_multilingual: # room for language token and task token self.begin_index += 2 if hasattr(_snake_case , 'max_initial_timestamp_index' ): A__ = generate_config.max_initial_timestamp_index else: A__ = model_config.vocab_size if self.max_initial_timestamp_index is None: A__ = model_config.vocab_size def __call__( self : Tuple , _snake_case : List[Any] , _snake_case : Dict , _snake_case : Dict ): """simple docstring""" A__ = scores.at[:, self.no_timestamps_token_id].set(-float('inf' ) ) def handle_pairs(_snake_case : Dict , _snake_case : str ): A__ = jnp.where((cur_len - self.begin_index) >= 1 , _snake_case , _snake_case ) A__ = jnp.where( input_ids_k[cur_len - 1] >= self.timestamp_begin , True and last_was_timestamp , _snake_case , ) A__ = jnp.where((cur_len - self.begin_index) < 2 , _snake_case , _snake_case ) A__ = jnp.where( input_ids_k[cur_len - 2] >= self.timestamp_begin , _snake_case , _snake_case , ) return jnp.where( _snake_case , jnp.where( penultimate_was_timestamp > 0 , scores_k.at[self.timestamp_begin :].set(-float('inf' ) ) , scores_k.at[: self.eos_token_id].set(-float('inf' ) ) , ) , _snake_case , ) A__ = jax.vmap(_snake_case )(_snake_case , _snake_case ) A__ = jnp.where(cur_len == self.begin_index , _snake_case , _snake_case ) A__ = jnp.where( self.max_initial_timestamp_index is not None , True and apply_max_initial_timestamp , _snake_case , ) A__ = self.timestamp_begin + self.max_initial_timestamp_index A__ = jnp.where( _snake_case , scores.at[:, last_allowed + 1 :].set(-float('inf' ) ) , _snake_case , ) # if sum of probability over timestamps is above any other token, sample timestamp A__ = jax.nn.log_softmax(_snake_case , axis=-1 ) def handle_cumulative_probs(_snake_case : List[Any] , _snake_case : Union[str, Any] ): A__ = jax.nn.logsumexp(logprobs_k[self.timestamp_begin :] , axis=-1 ) A__ = jnp.max(logprobs_k[: self.timestamp_begin] ) return jnp.where( timestamp_logprob > max_text_token_logprob , scores_k.at[: self.timestamp_begin].set(-float('inf' ) ) , _snake_case , ) A__ = jax.vmap(_snake_case )(_snake_case , _snake_case ) return scores
52
1
from __future__ import annotations def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , ) -> tuple: if (electron_conc, hole_conc, intrinsic_conc).count(0 ) != 1: raise ValueError('You cannot supply more or less than 2 values' ) elif electron_conc < 0: raise ValueError('Electron concentration cannot be negative in a semiconductor' ) elif hole_conc < 0: raise ValueError('Hole concentration cannot be negative in a semiconductor' ) elif intrinsic_conc < 0: raise ValueError( 'Intrinsic concentration cannot be negative in a semiconductor' ) elif electron_conc == 0: return ( "electron_conc", intrinsic_conc**2 / hole_conc, ) elif hole_conc == 0: return ( "hole_conc", intrinsic_conc**2 / electron_conc, ) elif intrinsic_conc == 0: return ( "intrinsic_conc", (electron_conc * hole_conc) ** 0.5, ) else: return (-1, -1) if __name__ == "__main__": import doctest doctest.testmod()
52
import argparse import struct import unittest class __lowerCAmelCase : """simple docstring""" def __init__( self : List[str] , _snake_case : bytes ): """simple docstring""" A__ = data # Initialize hash values A__ = [ 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19, ] # Initialize round constants A__ = [ 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA, 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967, 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070, 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3, 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2, ] A__ = self.preprocessing(self.data ) self.final_hash() @staticmethod def _a ( _snake_case : bytes ): """simple docstring""" A__ = B'\x80' + (B'\x00' * (63 - (len(_snake_case ) + 8) % 64)) A__ = struct.pack('>Q' , (len(_snake_case ) * 8) ) return data + padding + big_endian_integer def _a ( self : Optional[int] ): """simple docstring""" A__ = [ self.preprocessed_data[x : x + 64] for x in range(0 , len(self.preprocessed_data ) , 64 ) ] for block in self.blocks: # Convert the given block into a list of 4 byte integers A__ = list(struct.unpack('>16L' , _snake_case ) ) # add 48 0-ed integers words += [0] * 48 A__ , A__ , A__ , A__ , A__ , A__ , A__ , A__ = self.hashes for index in range(0 , 64 ): if index > 15: # modify the zero-ed indexes at the end of the array A__ = ( self.ror(words[index - 15] , 7 ) ^ self.ror(words[index - 15] , 18 ) ^ (words[index - 15] >> 3) ) A__ = ( self.ror(words[index - 2] , 17 ) ^ self.ror(words[index - 2] , 19 ) ^ (words[index - 2] >> 10) ) A__ = ( words[index - 16] + sa + words[index - 7] + sa ) % 0x100000000 # Compression A__ = self.ror(_snake_case , 6 ) ^ self.ror(_snake_case , 11 ) ^ self.ror(_snake_case , 25 ) A__ = (e & f) ^ ((~e & 0xFFFFFFFF) & g) A__ = ( h + sa + ch + self.round_constants[index] + words[index] ) % 0x100000000 A__ = self.ror(_snake_case , 2 ) ^ self.ror(_snake_case , 13 ) ^ self.ror(_snake_case , 22 ) A__ = (a & b) ^ (a & c) ^ (b & c) A__ = (sa + maj) % 0x100000000 A__ , A__ , A__ , A__ , A__ , A__ , A__ , A__ = ( g, f, e, ((d + tempa) % 0x100000000), c, b, a, ((tempa + tempa) % 0x100000000), ) A__ = [a, b, c, d, e, f, g, h] # Modify final values A__ = [ ((element + mutated_hash_values[index]) % 0x100000000) for index, element in enumerate(self.hashes ) ] A__ = ''.join([hex(_snake_case )[2:].zfill(8 ) for value in self.hashes] ) def _a ( self : Dict , _snake_case : int , _snake_case : int ): """simple docstring""" return 0xFFFFFFFF & (value << (32 - rotations)) | (value >> rotations) class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def _a ( self : str ): """simple docstring""" import hashlib A__ = bytes('Test String' , 'utf-8' ) self.assertEqual(SHAaaa(_snake_case ).hash , hashlib.shaaaa(_snake_case ).hexdigest() ) def A ( ) -> None: import doctest doctest.testmod() A__ = argparse.ArgumentParser() parser.add_argument( '-s' , '--string' , dest='input_string' , default='Hello World!! Welcome to Cryptography' , help='Hash the string' , ) parser.add_argument( '-f' , '--file' , dest='input_file' , help='Hash contents of a file' ) A__ = parser.parse_args() A__ = args.input_string # hash input should be a bytestring if args.input_file: with open(args.input_file , 'rb' ) as f: A__ = f.read() else: A__ = bytes(__UpperCamelCase , 'utf-8' ) print(SHAaaa(__UpperCamelCase ).hash ) if __name__ == "__main__": main()
52
1
import unittest from knapsack import greedy_knapsack as kp class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def _a ( self : str ): """simple docstring""" A__ = [10, 20, 30, 40, 50, 60] A__ = [2, 4, 6, 8, 10, 12] A__ = 1_00 self.assertEqual(kp.calc_profit(_snake_case , _snake_case , _snake_case ) , 2_10 ) def _a ( self : Any ): """simple docstring""" self.assertRaisesRegex(_snake_case , 'max_weight must greater than zero.' ) def _a ( self : str ): """simple docstring""" self.assertRaisesRegex(_snake_case , 'Weight can not be negative.' ) def _a ( self : Optional[Any] ): """simple docstring""" self.assertRaisesRegex(_snake_case , 'Profit can not be negative.' ) def _a ( self : Dict ): """simple docstring""" self.assertRaisesRegex(_snake_case , 'max_weight must greater than zero.' ) def _a ( self : List[str] ): """simple docstring""" self.assertRaisesRegex( _snake_case , 'The length of profit and weight must be same.' ) if __name__ == "__main__": unittest.main()
52
import math import random def A ( __UpperCamelCase , __UpperCamelCase = False ) -> float: if deriv: return value * (1 - value) return 1 / (1 + math.exp(-value )) # Initial Value SCREAMING_SNAKE_CASE__ = 0.02 def A ( __UpperCamelCase , __UpperCamelCase ) -> float: A__ = float(2 * (random.randint(1 , 100 )) - 1 ) for _ in range(__UpperCamelCase ): # Forward propagation A__ = sigmoid_function(INITIAL_VALUE * weight ) # How much did we miss? A__ = (expected / 100) - layer_a # Error delta A__ = layer_1_error * sigmoid_function(__UpperCamelCase , __UpperCamelCase ) # Update weight weight += INITIAL_VALUE * layer_1_delta return layer_a * 100 if __name__ == "__main__": import doctest doctest.testmod() SCREAMING_SNAKE_CASE__ = int(input('''Expected value: ''')) SCREAMING_SNAKE_CASE__ = int(input('''Number of propagations: ''')) print(forward_propagation(expected, number_propagations))
52
1
import contextlib import os import sqlitea import pytest from datasets import Dataset, Features, Value from datasets.io.sql import SqlDatasetReader, SqlDatasetWriter from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases, require_sqlalchemy def A ( __UpperCamelCase , __UpperCamelCase ) -> str: assert isinstance(__UpperCamelCase , __UpperCamelCase ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @require_sqlalchemy @pytest.mark.parametrize('keep_in_memory' , [False, True] ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> str: A__ = tmp_path / 'cache' A__ = {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): A__ = SqlDatasetReader( 'dataset' , 'sqlite:///' + sqlite_path , cache_dir=__UpperCamelCase , keep_in_memory=__UpperCamelCase ).read() _check_sql_dataset(__UpperCamelCase , __UpperCamelCase ) @require_sqlalchemy @pytest.mark.parametrize( 'features' , [ None, {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}, {'col_1': 'string', 'col_2': 'string', 'col_3': 'string'}, {'col_1': 'int32', 'col_2': 'int32', 'col_3': 'int32'}, {'col_1': 'float32', 'col_2': 'float32', 'col_3': 'float32'}, ] , ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Union[str, Any]: A__ = tmp_path / 'cache' A__ = {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'} A__ = features.copy() if features else default_expected_features A__ = ( Features({feature: Value(__UpperCamelCase ) for feature, dtype in features.items()} ) if features is not None else None ) A__ = SqlDatasetReader('dataset' , 'sqlite:///' + sqlite_path , features=__UpperCamelCase , cache_dir=__UpperCamelCase ).read() _check_sql_dataset(__UpperCamelCase , __UpperCamelCase ) def A ( __UpperCamelCase ) -> Union[str, Any]: with contextlib.closing(sqlitea.connect(__UpperCamelCase ) ) as con: A__ = con.cursor() cur.execute('SELECT * FROM dataset' ) for row in cur: yield row @require_sqlalchemy def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Any: A__ = tmp_path / 'cache' A__ = os.path.join(__UpperCamelCase , 'tmp.sql' ) A__ = SqlDatasetReader('dataset' , 'sqlite:///' + sqlite_path , cache_dir=__UpperCamelCase ).read() SqlDatasetWriter(__UpperCamelCase , 'dataset' , 'sqlite:///' + output_sqlite_path , num_proc=1 ).write() A__ = iter_sql_file(__UpperCamelCase ) A__ = iter_sql_file(__UpperCamelCase ) for rowa, rowa in zip(__UpperCamelCase , __UpperCamelCase ): assert rowa == rowa @require_sqlalchemy def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Union[str, Any]: A__ = tmp_path / 'cache' A__ = os.path.join(__UpperCamelCase , 'tmp.sql' ) A__ = SqlDatasetReader('dataset' , 'sqlite:///' + sqlite_path , cache_dir=__UpperCamelCase ).read() SqlDatasetWriter(__UpperCamelCase , 'dataset' , 'sqlite:///' + output_sqlite_path , num_proc=2 ).write() A__ = iter_sql_file(__UpperCamelCase ) A__ = iter_sql_file(__UpperCamelCase ) for rowa, rowa in zip(__UpperCamelCase , __UpperCamelCase ): assert rowa == rowa @require_sqlalchemy def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> str: A__ = tmp_path / 'cache' A__ = os.path.join(__UpperCamelCase , 'tmp.sql' ) A__ = SqlDatasetReader('dataset' , 'sqlite:///' + sqlite_path , cache_dir=__UpperCamelCase ).read() with pytest.raises(__UpperCamelCase ): SqlDatasetWriter(__UpperCamelCase , 'dataset' , 'sqlite:///' + output_sqlite_path , num_proc=0 ).write()
52
import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @slow def _a ( self : int ): """simple docstring""" A__ = FlaxMTaForConditionalGeneration.from_pretrained('google/mt5-small' ) A__ = AutoTokenizer.from_pretrained('google/mt5-small' ) A__ = tokenizer('Hello there' , return_tensors='np' ).input_ids A__ = tokenizer('Hi I am' , return_tensors='np' ).input_ids A__ = shift_tokens_right(_snake_case , model.config.pad_token_id , model.config.decoder_start_token_id ) A__ = model(_snake_case , decoder_input_ids=_snake_case ).logits A__ = optax.softmax_cross_entropy(_snake_case , onehot(_snake_case , logits.shape[-1] ) ).mean() A__ = -(labels.shape[-1] * loss.item()) A__ = -84.9127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1E-4 )
52
1
import itertools import json import os import unittest from transformers import AddedToken, LongformerTokenizer, LongformerTokenizerFast from transformers.models.longformer.tokenization_longformer import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __lowerCAmelCase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : int = LongformerTokenizer A__ : Optional[int] = True A__ : Any = LongformerTokenizerFast A__ : Dict = True def _a ( self : int ): """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt A__ = [ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', '\u0120', '\u0120l', '\u0120n', '\u0120lo', '\u0120low', 'er', '\u0120lowest', '\u0120newer', '\u0120wider', '<unk>', ] A__ = dict(zip(_snake_case , range(len(_snake_case ) ) ) ) A__ = ['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', ''] A__ = {'unk_token': '<unk>'} A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(_snake_case ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(_snake_case ) ) def _a ( self : int , **_snake_case : Union[str, Any] ): """simple docstring""" kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **_snake_case ) def _a ( self : Optional[int] , **_snake_case : List[Any] ): """simple docstring""" kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **_snake_case ) def _a ( self : Any , _snake_case : Optional[Any] ): """simple docstring""" A__ = 'lower newer' A__ = 'lower newer' return input_text, output_text def _a ( self : Any ): """simple docstring""" A__ = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) A__ = 'lower newer' A__ = ['l', 'o', 'w', 'er', '\u0120', 'n', 'e', 'w', 'er'] A__ = tokenizer.tokenize(_snake_case ) # , add_prefix_space=True) self.assertListEqual(_snake_case , _snake_case ) A__ = tokens + [tokenizer.unk_token] A__ = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(_snake_case ) , _snake_case ) def _a ( self : List[str] ): """simple docstring""" A__ = self.get_tokenizer() self.assertListEqual(tokenizer.encode('Hello world!' , add_special_tokens=_snake_case ) , [0, 3_14_14, 2_32, 3_28, 2] ) self.assertListEqual( tokenizer.encode('Hello world! cécé herlolip 418' , add_special_tokens=_snake_case ) , [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2] , ) @slow def _a ( self : List[Any] ): """simple docstring""" A__ = self.tokenizer_class.from_pretrained('allenai/longformer-base-4096' ) A__ = tokenizer.encode('sequence builders' , add_special_tokens=_snake_case ) A__ = tokenizer.encode('multi-sequence build' , add_special_tokens=_snake_case ) A__ = tokenizer.encode( 'sequence builders' , add_special_tokens=_snake_case , add_prefix_space=_snake_case ) A__ = tokenizer.encode( 'sequence builders' , 'multi-sequence build' , add_special_tokens=_snake_case , add_prefix_space=_snake_case ) A__ = tokenizer.build_inputs_with_special_tokens(_snake_case ) A__ = tokenizer.build_inputs_with_special_tokens(_snake_case , _snake_case ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def _a ( self : List[str] ): """simple docstring""" A__ = self.get_tokenizer() A__ = 'Encode this sequence.' A__ = tokenizer.byte_encoder[' '.encode('utf-8' )[0]] # Testing encoder arguments A__ = tokenizer.encode(_snake_case , add_special_tokens=_snake_case , add_prefix_space=_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(_snake_case , _snake_case ) A__ = tokenizer.encode(_snake_case , add_special_tokens=_snake_case , add_prefix_space=_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(_snake_case , _snake_case ) tokenizer.add_special_tokens({'bos_token': '<s>'} ) A__ = tokenizer.encode(_snake_case , add_special_tokens=_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(_snake_case , _snake_case ) # Testing spaces after special tokens A__ = '<mask>' tokenizer.add_special_tokens( {'mask_token': AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case )} ) # mask token has a left space A__ = tokenizer.convert_tokens_to_ids(_snake_case ) A__ = 'Encode <mask> sequence' A__ = 'Encode <mask>sequence' A__ = tokenizer.encode(_snake_case ) A__ = encoded.index(_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(_snake_case , _snake_case ) A__ = tokenizer.encode(_snake_case ) A__ = encoded.index(_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(_snake_case , _snake_case ) def _a ( self : Dict ): """simple docstring""" pass def _a ( self : Union[str, Any] ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): A__ = self.rust_tokenizer_class.from_pretrained(_snake_case , **_snake_case ) A__ = self.tokenizer_class.from_pretrained(_snake_case , **_snake_case ) A__ = 'A, <mask> AllenNLP sentence.' A__ = tokenizer_r.encode_plus(_snake_case , add_special_tokens=_snake_case , return_token_type_ids=_snake_case ) A__ = tokenizer_p.encode_plus(_snake_case , add_special_tokens=_snake_case , return_token_type_ids=_snake_case ) # token_type_ids should put 0 everywhere self.assertEqual(sum(tokens_r['token_type_ids'] ) , sum(tokens_p['token_type_ids'] ) ) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEqual( sum(tokens_r['attention_mask'] ) / len(tokens_r['attention_mask'] ) , sum(tokens_p['attention_mask'] ) / len(tokens_p['attention_mask'] ) , ) A__ = tokenizer_r.convert_ids_to_tokens(tokens_r['input_ids'] ) A__ = tokenizer_p.convert_ids_to_tokens(tokens_p['input_ids'] ) # Rust correctly handles the space before the mask while python doesnt self.assertSequenceEqual(tokens_p['input_ids'] , [0, 2_50, 6, 5_02_64, 38_23, 4_87, 2_19_92, 36_45, 4, 2] ) self.assertSequenceEqual(tokens_r['input_ids'] , [0, 2_50, 6, 5_02_64, 38_23, 4_87, 2_19_92, 36_45, 4, 2] ) self.assertSequenceEqual( _snake_case , ['<s>', 'A', ',', '<mask>', 'ĠAllen', 'N', 'LP', 'Ġsentence', '.', '</s>'] ) self.assertSequenceEqual( _snake_case , ['<s>', 'A', ',', '<mask>', 'ĠAllen', 'N', 'LP', 'Ġsentence', '.', '</s>'] ) def _a ( self : List[Any] ): """simple docstring""" for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): A__ = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) A__ = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state['add_prefix_space'] , _snake_case ) self.assertEqual(post_processor_state['add_prefix_space'] , _snake_case ) self.assertEqual(post_processor_state['trim_offsets'] , _snake_case ) def _a ( self : Optional[Any] ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): A__ = 'hello' # `hello` is a token in the vocabulary of `pretrained_name` A__ = F'''{text_of_1_token} {text_of_1_token}''' A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_snake_case ) + 1, len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_snake_case ) + 1, len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_snake_case ), len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_snake_case ), len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = F''' {text}''' # tokenizer_r = self.rust_tokenizer_class.from_pretrained( # pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True # ) # encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False) # self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token))) # self.assertEqual( # encoding.offset_mapping[1], # (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)), # ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_snake_case ) + 1, 1 + len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_snake_case ), 1 + len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_snake_case ), 1 + len(_snake_case ) + 1 + len(_snake_case )) , )
52
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/config.json''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/config.json''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/config.json''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/config.json''', '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/config.json''', '''roberta-large-openai-detector''': '''https://huggingface.co/roberta-large-openai-detector/resolve/main/config.json''', } class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : List[str] = "roberta" def __init__( self : List[str] , _snake_case : Union[str, Any]=5_02_65 , _snake_case : List[Any]=7_68 , _snake_case : List[str]=12 , _snake_case : List[str]=12 , _snake_case : Any=30_72 , _snake_case : Union[str, Any]="gelu" , _snake_case : int=0.1 , _snake_case : Union[str, Any]=0.1 , _snake_case : Tuple=5_12 , _snake_case : Union[str, Any]=2 , _snake_case : Any=0.02 , _snake_case : Any=1E-12 , _snake_case : List[Any]=1 , _snake_case : int=0 , _snake_case : Any=2 , _snake_case : Optional[Any]="absolute" , _snake_case : int=True , _snake_case : Any=None , **_snake_case : Any , ): """simple docstring""" super().__init__(pad_token_id=_snake_case , bos_token_id=_snake_case , eos_token_id=_snake_case , **_snake_case ) 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__ = use_cache A__ = classifier_dropout class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" @property def _a ( self : Dict ): """simple docstring""" if self.task == "multiple-choice": A__ = {0: 'batch', 1: 'choice', 2: 'sequence'} else: A__ = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ] )
52
1
import argparse import logging import os import sys import numpy as np import onnxruntime import torch from bart_onnx.generation_onnx import BARTBeamSearchGenerator from bart_onnx.reduce_onnx_size import remove_dup_initializers import transformers from transformers import BartForConditionalGeneration, BartTokenizer logging.basicConfig( format='''%(asctime)s | %(levelname)s | %(name)s | [%(filename)s:%(lineno)d] %(message)s''', datefmt='''%Y-%m-%d %H:%M:%S''', level=os.environ.get('''LOGLEVEL''', '''INFO''').upper(), stream=sys.stdout, ) SCREAMING_SNAKE_CASE__ = logging.getLogger(__name__) SCREAMING_SNAKE_CASE__ = {'''facebook/bart-base''': BartForConditionalGeneration} SCREAMING_SNAKE_CASE__ = {'''facebook/bart-base''': BartTokenizer} def A ( ) -> Optional[int]: A__ = argparse.ArgumentParser(description='Export Bart model + Beam Search to ONNX graph.' ) parser.add_argument( '--validation_file' , type=__UpperCamelCase , default=__UpperCamelCase , help='A csv or a json file containing the validation data.' ) parser.add_argument( '--max_length' , type=__UpperCamelCase , default=5 , help='The maximum total input sequence length after tokenization.' , ) parser.add_argument( '--num_beams' , type=__UpperCamelCase , default=__UpperCamelCase , help=( 'Number of beams to use for evaluation. This argument will be ' 'passed to ``model.generate``, which is used during ``evaluate`` and ``predict``.' ) , ) parser.add_argument( '--model_name_or_path' , type=__UpperCamelCase , help='Path to pretrained model or model identifier from huggingface.co/models.' , required=__UpperCamelCase , ) parser.add_argument( '--config_name' , type=__UpperCamelCase , default=__UpperCamelCase , help='Pretrained config name or path if not the same as model_name' , ) parser.add_argument( '--device' , type=__UpperCamelCase , default='cpu' , help='Device where the model will be run' , ) parser.add_argument('--output_file_path' , type=__UpperCamelCase , default=__UpperCamelCase , help='Where to store the final ONNX file.' ) A__ = parser.parse_args() return args def A ( __UpperCamelCase , __UpperCamelCase="cpu" ) -> str: A__ = model_dict[model_name].from_pretrained(__UpperCamelCase ).to(__UpperCamelCase ) A__ = tokenizer_dict[model_name].from_pretrained(__UpperCamelCase ) if model_name in ["facebook/bart-base"]: A__ = 0 A__ = None A__ = 0 return huggingface_model, tokenizer def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Tuple: model.eval() A__ = None A__ = torch.jit.script(BARTBeamSearchGenerator(__UpperCamelCase ) ) with torch.no_grad(): A__ = 'My friends are cool but they eat too many carbs.' A__ = tokenizer([ARTICLE_TO_SUMMARIZE] , max_length=1_024 , return_tensors='pt' ).to(model.device ) A__ = model.generate( inputs['input_ids'] , attention_mask=inputs['attention_mask'] , num_beams=__UpperCamelCase , max_length=__UpperCamelCase , early_stopping=__UpperCamelCase , decoder_start_token_id=model.config.decoder_start_token_id , ) torch.onnx.export( __UpperCamelCase , ( inputs['input_ids'], inputs['attention_mask'], num_beams, max_length, model.config.decoder_start_token_id, ) , __UpperCamelCase , opset_version=14 , input_names=['input_ids', 'attention_mask', 'num_beams', 'max_length', 'decoder_start_token_id'] , output_names=['output_ids'] , dynamic_axes={ 'input_ids': {0: 'batch', 1: 'seq'}, 'output_ids': {0: 'batch', 1: 'seq_out'}, } , example_outputs=__UpperCamelCase , ) logger.info('Model exported to {}'.format(__UpperCamelCase ) ) A__ = remove_dup_initializers(os.path.abspath(__UpperCamelCase ) ) logger.info('Deduplicated and optimized model written to {}'.format(__UpperCamelCase ) ) A__ = onnxruntime.InferenceSession(__UpperCamelCase ) A__ = ort_sess.run( __UpperCamelCase , { 'input_ids': inputs['input_ids'].cpu().numpy(), 'attention_mask': inputs['attention_mask'].cpu().numpy(), 'num_beams': np.array(__UpperCamelCase ), 'max_length': np.array(__UpperCamelCase ), 'decoder_start_token_id': np.array(model.config.decoder_start_token_id ), } , ) np.testing.assert_allclose(summary_ids.cpu().numpy() , ort_out[0] , rtol=1E-3 , atol=1E-3 ) logger.info('Model outputs from torch and ONNX Runtime are similar.' ) logger.info('Success.' ) def A ( ) -> Tuple: A__ = parse_args() A__ = 5 A__ = 4 # Make one log on every process with the configuration for debugging. logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , level=logging.INFO , ) logger.setLevel(logging.INFO ) transformers.utils.logging.set_verbosity_error() A__ = torch.device(args.device ) A__ , A__ = load_model_tokenizer(args.model_name_or_path , __UpperCamelCase ) if model.config.decoder_start_token_id is None: raise ValueError('Make sure that `config.decoder_start_token_id` is correctly defined' ) model.to(__UpperCamelCase ) if args.max_length: A__ = args.max_length if args.num_beams: A__ = args.num_beams if args.output_file_path: A__ = args.output_file_path else: A__ = 'BART.onnx' logger.info('Exporting model to ONNX' ) export_and_validate_model(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) if __name__ == "__main__": main()
52
import itertools import json import os import unittest from transformers import AddedToken, LongformerTokenizer, LongformerTokenizerFast from transformers.models.longformer.tokenization_longformer import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __lowerCAmelCase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : int = LongformerTokenizer A__ : Optional[int] = True A__ : Any = LongformerTokenizerFast A__ : Dict = True def _a ( self : int ): """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt A__ = [ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', '\u0120', '\u0120l', '\u0120n', '\u0120lo', '\u0120low', 'er', '\u0120lowest', '\u0120newer', '\u0120wider', '<unk>', ] A__ = dict(zip(_snake_case , range(len(_snake_case ) ) ) ) A__ = ['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', ''] A__ = {'unk_token': '<unk>'} A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(_snake_case ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(_snake_case ) ) def _a ( self : int , **_snake_case : Union[str, Any] ): """simple docstring""" kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **_snake_case ) def _a ( self : Optional[int] , **_snake_case : List[Any] ): """simple docstring""" kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **_snake_case ) def _a ( self : Any , _snake_case : Optional[Any] ): """simple docstring""" A__ = 'lower newer' A__ = 'lower newer' return input_text, output_text def _a ( self : Any ): """simple docstring""" A__ = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) A__ = 'lower newer' A__ = ['l', 'o', 'w', 'er', '\u0120', 'n', 'e', 'w', 'er'] A__ = tokenizer.tokenize(_snake_case ) # , add_prefix_space=True) self.assertListEqual(_snake_case , _snake_case ) A__ = tokens + [tokenizer.unk_token] A__ = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(_snake_case ) , _snake_case ) def _a ( self : List[str] ): """simple docstring""" A__ = self.get_tokenizer() self.assertListEqual(tokenizer.encode('Hello world!' , add_special_tokens=_snake_case ) , [0, 3_14_14, 2_32, 3_28, 2] ) self.assertListEqual( tokenizer.encode('Hello world! cécé herlolip 418' , add_special_tokens=_snake_case ) , [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2] , ) @slow def _a ( self : List[Any] ): """simple docstring""" A__ = self.tokenizer_class.from_pretrained('allenai/longformer-base-4096' ) A__ = tokenizer.encode('sequence builders' , add_special_tokens=_snake_case ) A__ = tokenizer.encode('multi-sequence build' , add_special_tokens=_snake_case ) A__ = tokenizer.encode( 'sequence builders' , add_special_tokens=_snake_case , add_prefix_space=_snake_case ) A__ = tokenizer.encode( 'sequence builders' , 'multi-sequence build' , add_special_tokens=_snake_case , add_prefix_space=_snake_case ) A__ = tokenizer.build_inputs_with_special_tokens(_snake_case ) A__ = tokenizer.build_inputs_with_special_tokens(_snake_case , _snake_case ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def _a ( self : List[str] ): """simple docstring""" A__ = self.get_tokenizer() A__ = 'Encode this sequence.' A__ = tokenizer.byte_encoder[' '.encode('utf-8' )[0]] # Testing encoder arguments A__ = tokenizer.encode(_snake_case , add_special_tokens=_snake_case , add_prefix_space=_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(_snake_case , _snake_case ) A__ = tokenizer.encode(_snake_case , add_special_tokens=_snake_case , add_prefix_space=_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(_snake_case , _snake_case ) tokenizer.add_special_tokens({'bos_token': '<s>'} ) A__ = tokenizer.encode(_snake_case , add_special_tokens=_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(_snake_case , _snake_case ) # Testing spaces after special tokens A__ = '<mask>' tokenizer.add_special_tokens( {'mask_token': AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case )} ) # mask token has a left space A__ = tokenizer.convert_tokens_to_ids(_snake_case ) A__ = 'Encode <mask> sequence' A__ = 'Encode <mask>sequence' A__ = tokenizer.encode(_snake_case ) A__ = encoded.index(_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(_snake_case , _snake_case ) A__ = tokenizer.encode(_snake_case ) A__ = encoded.index(_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(_snake_case , _snake_case ) def _a ( self : Dict ): """simple docstring""" pass def _a ( self : Union[str, Any] ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): A__ = self.rust_tokenizer_class.from_pretrained(_snake_case , **_snake_case ) A__ = self.tokenizer_class.from_pretrained(_snake_case , **_snake_case ) A__ = 'A, <mask> AllenNLP sentence.' A__ = tokenizer_r.encode_plus(_snake_case , add_special_tokens=_snake_case , return_token_type_ids=_snake_case ) A__ = tokenizer_p.encode_plus(_snake_case , add_special_tokens=_snake_case , return_token_type_ids=_snake_case ) # token_type_ids should put 0 everywhere self.assertEqual(sum(tokens_r['token_type_ids'] ) , sum(tokens_p['token_type_ids'] ) ) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEqual( sum(tokens_r['attention_mask'] ) / len(tokens_r['attention_mask'] ) , sum(tokens_p['attention_mask'] ) / len(tokens_p['attention_mask'] ) , ) A__ = tokenizer_r.convert_ids_to_tokens(tokens_r['input_ids'] ) A__ = tokenizer_p.convert_ids_to_tokens(tokens_p['input_ids'] ) # Rust correctly handles the space before the mask while python doesnt self.assertSequenceEqual(tokens_p['input_ids'] , [0, 2_50, 6, 5_02_64, 38_23, 4_87, 2_19_92, 36_45, 4, 2] ) self.assertSequenceEqual(tokens_r['input_ids'] , [0, 2_50, 6, 5_02_64, 38_23, 4_87, 2_19_92, 36_45, 4, 2] ) self.assertSequenceEqual( _snake_case , ['<s>', 'A', ',', '<mask>', 'ĠAllen', 'N', 'LP', 'Ġsentence', '.', '</s>'] ) self.assertSequenceEqual( _snake_case , ['<s>', 'A', ',', '<mask>', 'ĠAllen', 'N', 'LP', 'Ġsentence', '.', '</s>'] ) def _a ( self : List[Any] ): """simple docstring""" for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): A__ = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) A__ = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state['add_prefix_space'] , _snake_case ) self.assertEqual(post_processor_state['add_prefix_space'] , _snake_case ) self.assertEqual(post_processor_state['trim_offsets'] , _snake_case ) def _a ( self : Optional[Any] ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): A__ = 'hello' # `hello` is a token in the vocabulary of `pretrained_name` A__ = F'''{text_of_1_token} {text_of_1_token}''' A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_snake_case ) + 1, len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_snake_case ) + 1, len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_snake_case ), len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_snake_case ), len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = F''' {text}''' # tokenizer_r = self.rust_tokenizer_class.from_pretrained( # pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True # ) # encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False) # self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token))) # self.assertEqual( # encoding.offset_mapping[1], # (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)), # ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_snake_case ) + 1, 1 + len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_snake_case ), 1 + len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_snake_case ), 1 + len(_snake_case ) + 1 + len(_snake_case )) , )
52
1
from typing import Dict import numpy as np from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline, PipelineException if is_tf_available(): import tensorflow as tf from ..tf_utils import stable_softmax if is_torch_available(): import torch SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) @add_end_docstrings( UpperCAmelCase_ , R"\n top_k (`int`, defaults to 5):\n The number of predictions to return.\n targets (`str` or `List[str]`, *optional*):\n When passed, the model will limit the scores to the passed targets instead of looking up in the whole\n vocab. If the provided targets are not in the model vocab, they will be tokenized and the first resulting\n token will be used (with a warning, and that might be slower).\n\n " , ) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def _a ( self : Union[str, Any] , _snake_case : GenericTensor ): """simple docstring""" if self.framework == "tf": A__ = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy() elif self.framework == "pt": A__ = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=_snake_case ) else: raise ValueError('Unsupported framework' ) return masked_index def _a ( self : List[Any] , _snake_case : GenericTensor ): """simple docstring""" A__ = self.get_masked_index(_snake_case ) A__ = np.prod(masked_index.shape ) if numel < 1: raise PipelineException( 'fill-mask' , self.model.base_model_prefix , F'''No mask_token ({self.tokenizer.mask_token}) found on the input''' , ) def _a ( self : Any , _snake_case : GenericTensor ): """simple docstring""" if isinstance(_snake_case , _snake_case ): for model_input in model_inputs: self._ensure_exactly_one_mask_token(model_input['input_ids'][0] ) else: for input_ids in model_inputs["input_ids"]: self._ensure_exactly_one_mask_token(_snake_case ) def _a ( self : str , _snake_case : Tuple , _snake_case : Union[str, Any]=None , **_snake_case : Dict ): """simple docstring""" if return_tensors is None: A__ = self.framework A__ = self.tokenizer(_snake_case , return_tensors=_snake_case ) self.ensure_exactly_one_mask_token(_snake_case ) return model_inputs def _a ( self : List[Any] , _snake_case : Union[str, Any] ): """simple docstring""" A__ = self.model(**_snake_case ) A__ = model_inputs['input_ids'] return model_outputs def _a ( self : int , _snake_case : Any , _snake_case : Dict=5 , _snake_case : List[str]=None ): """simple docstring""" if target_ids is not None and target_ids.shape[0] < top_k: A__ = target_ids.shape[0] A__ = model_outputs['input_ids'][0] A__ = model_outputs['logits'] if self.framework == "tf": A__ = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy()[:, 0] A__ = outputs.numpy() A__ = outputs[0, masked_index, :] A__ = stable_softmax(_snake_case , axis=-1 ) if target_ids is not None: A__ = tf.gather_nd(tf.squeeze(_snake_case , 0 ) , target_ids.reshape(-1 , 1 ) ) A__ = tf.expand_dims(_snake_case , 0 ) A__ = tf.math.top_k(_snake_case , k=_snake_case ) A__ , A__ = topk.values.numpy(), topk.indices.numpy() else: A__ = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=_snake_case ).squeeze(-1 ) # Fill mask pipeline supports only one ${mask_token} per sample A__ = outputs[0, masked_index, :] A__ = logits.softmax(dim=-1 ) if target_ids is not None: A__ = probs[..., target_ids] A__ , A__ = probs.topk(_snake_case ) A__ = [] A__ = values.shape[0] == 1 for i, (_values, _predictions) in enumerate(zip(values.tolist() , predictions.tolist() ) ): A__ = [] for v, p in zip(_values , _predictions ): # Copy is important since we're going to modify this array in place A__ = input_ids.numpy().copy() if target_ids is not None: A__ = target_ids[p].tolist() A__ = p # Filter padding out: A__ = tokens[np.where(tokens != self.tokenizer.pad_token_id )] # Originally we skip special tokens to give readable output. # For multi masks though, the other [MASK] would be removed otherwise # making the output look odd, so we add them back A__ = self.tokenizer.decode(_snake_case , skip_special_tokens=_snake_case ) A__ = {'score': v, 'token': p, 'token_str': self.tokenizer.decode([p] ), 'sequence': sequence} row.append(_snake_case ) result.append(_snake_case ) if single_mask: return result[0] return result def _a ( self : List[str] , _snake_case : List[str] , _snake_case : List[Any]=None ): """simple docstring""" if isinstance(_snake_case , _snake_case ): A__ = [targets] try: A__ = self.tokenizer.get_vocab() except Exception: A__ = {} A__ = [] for target in targets: A__ = vocab.get(_snake_case , _snake_case ) if id_ is None: A__ = self.tokenizer( _snake_case , add_special_tokens=_snake_case , return_attention_mask=_snake_case , return_token_type_ids=_snake_case , max_length=1 , truncation=_snake_case , )['input_ids'] if len(_snake_case ) == 0: logger.warning( F'''The specified target token `{target}` does not exist in the model vocabulary. ''' 'We cannot replace it with anything meaningful, ignoring it' ) continue A__ = input_ids[0] # XXX: If users encounter this pass # it becomes pretty slow, so let's make sure # The warning enables them to fix the input to # get faster performance. logger.warning( F'''The specified target token `{target}` does not exist in the model vocabulary. ''' F'''Replacing with `{self.tokenizer.convert_ids_to_tokens(id_ )}`.''' ) target_ids.append(id_ ) A__ = list(set(_snake_case ) ) if len(_snake_case ) == 0: raise ValueError('At least one target must be provided when passed.' ) A__ = np.array(_snake_case ) return target_ids def _a ( self : List[Any] , _snake_case : List[Any]=None , _snake_case : Any=None ): """simple docstring""" A__ = {} if targets is not None: A__ = self.get_target_ids(_snake_case , _snake_case ) A__ = target_ids if top_k is not None: A__ = top_k if self.tokenizer.mask_token_id is None: raise PipelineException( 'fill-mask' , self.model.base_model_prefix , 'The tokenizer does not define a `mask_token`.' ) return {}, {}, postprocess_params def __call__( self : Any , _snake_case : Optional[int] , *_snake_case : List[Any] , **_snake_case : Any ): """simple docstring""" A__ = super().__call__(_snake_case , **_snake_case ) if isinstance(_snake_case , _snake_case ) and len(_snake_case ) == 1: return outputs[0] return outputs
52
import pytest import datasets # Import fixture modules as plugins SCREAMING_SNAKE_CASE__ = ['''tests.fixtures.files''', '''tests.fixtures.hub''', '''tests.fixtures.fsspec'''] def A ( __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: # Mark tests as "unit" by default if not marked as "integration" (or already marked as "unit") for item in items: if any(marker in item.keywords for marker in ['integration', 'unit'] ): continue item.add_marker(pytest.mark.unit ) def A ( __UpperCamelCase ) -> str: config.addinivalue_line('markers' , 'torchaudio_latest: mark test to run with torchaudio>=0.12' ) @pytest.fixture(autouse=__UpperCamelCase ) def A ( __UpperCamelCase , __UpperCamelCase ) -> Union[str, Any]: # test_hf_cache_home = tmp_path_factory.mktemp("cache") # TODO: why a cache dir per test function does not work? A__ = tmp_path_factory.getbasetemp() / 'cache' A__ = test_hf_cache_home / 'datasets' A__ = test_hf_cache_home / 'metrics' A__ = test_hf_cache_home / 'modules' monkeypatch.setattr('datasets.config.HF_DATASETS_CACHE' , str(__UpperCamelCase ) ) monkeypatch.setattr('datasets.config.HF_METRICS_CACHE' , str(__UpperCamelCase ) ) monkeypatch.setattr('datasets.config.HF_MODULES_CACHE' , str(__UpperCamelCase ) ) A__ = test_hf_datasets_cache / 'downloads' monkeypatch.setattr('datasets.config.DOWNLOADED_DATASETS_PATH' , str(__UpperCamelCase ) ) A__ = test_hf_datasets_cache / 'downloads' / 'extracted' monkeypatch.setattr('datasets.config.EXTRACTED_DATASETS_PATH' , str(__UpperCamelCase ) ) @pytest.fixture(autouse=__UpperCamelCase , scope='session' ) def A ( ) -> Union[str, Any]: datasets.disable_progress_bar() @pytest.fixture(autouse=__UpperCamelCase ) def A ( __UpperCamelCase ) -> int: # don't take tests into account when counting downloads monkeypatch.setattr('datasets.config.HF_UPDATE_DOWNLOAD_COUNTS' , __UpperCamelCase ) @pytest.fixture def A ( __UpperCamelCase ) -> Any: # Required to suppress RemovedIn20Warning when feature(s) are not compatible with SQLAlchemy 2.0 # To be removed once SQLAlchemy 2.0 supported monkeypatch.setattr('sqlalchemy.util.deprecations.SILENCE_UBER_WARNING' , __UpperCamelCase )
52
1
import math import tensorflow as tf from packaging import version def A ( __UpperCamelCase ) -> Optional[int]: A__ = tf.convert_to_tensor(__UpperCamelCase ) A__ = 0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0 ) , x.dtype ) )) return x * cdf def A ( __UpperCamelCase ) -> Union[str, Any]: A__ = tf.convert_to_tensor(__UpperCamelCase ) A__ = tf.cast(math.pi , x.dtype ) A__ = tf.cast(0.04_4715 , x.dtype ) A__ = 0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi ) * (x + coeff * tf.pow(__UpperCamelCase , 3 )) )) return x * cdf def A ( __UpperCamelCase ) -> str: A__ = tf.convert_to_tensor(__UpperCamelCase ) return x * tf.tanh(tf.math.softplus(__UpperCamelCase ) ) def A ( __UpperCamelCase ) -> List[Any]: A__ = tf.convert_to_tensor(__UpperCamelCase ) A__ = tf.cast(0.04_4715 , x.dtype ) A__ = tf.cast(0.79_7884_5608 , x.dtype ) return 0.5 * x * (1.0 + tf.tanh(x * coeffa * (1.0 + coeffa * x * x) )) def A ( __UpperCamelCase ) -> List[str]: A__ = tf.convert_to_tensor(__UpperCamelCase ) A__ = tf.cast(1.702 , x.dtype ) return x * tf.math.sigmoid(coeff * x ) def A ( __UpperCamelCase ) -> List[Any]: return tf.clip_by_value(_gelu(__UpperCamelCase ) , -10 , 10 ) def A ( __UpperCamelCase , __UpperCamelCase=-1 ) -> Any: A__ , A__ = tf.split(__UpperCamelCase , 2 , axis=__UpperCamelCase ) return a * tf.math.sigmoid(__UpperCamelCase ) if version.parse(tf.version.VERSION) >= version.parse('''2.4'''): def A ( __UpperCamelCase ) -> Union[str, Any]: return tf.keras.activations.gelu(__UpperCamelCase , approximate=__UpperCamelCase ) SCREAMING_SNAKE_CASE__ = tf.keras.activations.gelu SCREAMING_SNAKE_CASE__ = approximate_gelu_wrap else: SCREAMING_SNAKE_CASE__ = _gelu SCREAMING_SNAKE_CASE__ = _gelu_new SCREAMING_SNAKE_CASE__ = { '''gelu''': gelu, '''gelu_10''': gelu_aa, '''gelu_fast''': gelu_fast, '''gelu_new''': gelu_new, '''glu''': glu, '''mish''': mish, '''quick_gelu''': quick_gelu, '''relu''': tf.keras.activations.relu, '''sigmoid''': tf.keras.activations.sigmoid, '''silu''': tf.keras.activations.swish, '''swish''': tf.keras.activations.swish, '''tanh''': tf.keras.activations.tanh, } def A ( __UpperCamelCase ) -> str: if activation_string in ACTaFN: return ACTaFN[activation_string] else: raise KeyError(f'''function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys() )}''' )
52
import argparse import re from typing import Dict import torch from datasets import Audio, Dataset, load_dataset, load_metric from transformers import AutoFeatureExtractor, pipeline def A ( __UpperCamelCase , __UpperCamelCase ) -> Tuple: A__ = args.log_outputs A__ = '_'.join(args.dataset.split('/' ) + [args.config, args.split] ) # load metric A__ = load_metric('wer' ) A__ = load_metric('cer' ) # compute metrics A__ = wer.compute(references=result['target'] , predictions=result['prediction'] ) A__ = cer.compute(references=result['target'] , predictions=result['prediction'] ) # print & log results A__ = f'''WER: {wer_result}\nCER: {cer_result}''' print(__UpperCamelCase ) with open(f'''{dataset_id}_eval_results.txt''' , 'w' ) as f: f.write(__UpperCamelCase ) # log all results in text file. Possibly interesting for analysis if log_outputs is not None: A__ = f'''log_{dataset_id}_predictions.txt''' A__ = f'''log_{dataset_id}_targets.txt''' with open(__UpperCamelCase , 'w' ) as p, open(__UpperCamelCase , 'w' ) as t: # mapping function to write output def write_to_file(__UpperCamelCase , __UpperCamelCase ): p.write(f'''{i}''' + '\n' ) p.write(batch['prediction'] + '\n' ) t.write(f'''{i}''' + '\n' ) t.write(batch['target'] + '\n' ) result.map(__UpperCamelCase , with_indices=__UpperCamelCase ) def A ( __UpperCamelCase ) -> str: A__ = '[,?.!\-\;\:"“%‘”�—’…–]' # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training A__ = re.sub(__UpperCamelCase , '' , text.lower() ) # In addition, we can normalize the target text, e.g. removing new lines characters etc... # note that order is important here! A__ = ['\n\n', '\n', ' ', ' '] for t in token_sequences_to_ignore: A__ = ' '.join(text.split(__UpperCamelCase ) ) return text def A ( __UpperCamelCase ) -> Union[str, Any]: # load dataset A__ = load_dataset(args.dataset , args.config , split=args.split , use_auth_token=__UpperCamelCase ) # for testing: only process the first two examples as a test # dataset = dataset.select(range(10)) # load processor A__ = AutoFeatureExtractor.from_pretrained(args.model_id ) A__ = feature_extractor.sampling_rate # resample audio A__ = dataset.cast_column('audio' , Audio(sampling_rate=__UpperCamelCase ) ) # load eval pipeline if args.device is None: A__ = 0 if torch.cuda.is_available() else -1 A__ = pipeline('automatic-speech-recognition' , model=args.model_id , device=args.device ) # map function to decode audio def map_to_pred(__UpperCamelCase ): A__ = asr( batch['audio']['array'] , chunk_length_s=args.chunk_length_s , stride_length_s=args.stride_length_s ) A__ = prediction['text'] A__ = normalize_text(batch['sentence'] ) return batch # run inference on all examples A__ = dataset.map(__UpperCamelCase , remove_columns=dataset.column_names ) # compute and log_results # do not change function below log_results(__UpperCamelCase , __UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument( '''--model_id''', type=str, required=True, help='''Model identifier. Should be loadable with 🤗 Transformers''' ) parser.add_argument( '''--dataset''', type=str, required=True, help='''Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets''', ) parser.add_argument( '''--config''', type=str, required=True, help='''Config of the dataset. *E.g.* `\'en\'` for Common Voice''' ) parser.add_argument('''--split''', type=str, required=True, help='''Split of the dataset. *E.g.* `\'test\'`''') parser.add_argument( '''--chunk_length_s''', type=float, default=None, help='''Chunk length in seconds. Defaults to 5 seconds.''' ) parser.add_argument( '''--stride_length_s''', type=float, default=None, help='''Stride of the audio chunks. Defaults to 1 second.''' ) parser.add_argument( '''--log_outputs''', action='''store_true''', help='''If defined, write outputs to log file for analysis.''' ) parser.add_argument( '''--device''', type=int, default=None, help='''The device to run the pipeline on. -1 for CPU (default), 0 for the first GPU and so on.''', ) SCREAMING_SNAKE_CASE__ = parser.parse_args() main(args)
52
1
from ..utils import ( OptionalDependencyNotAvailable, is_flax_available, is_scipy_available, is_torch_available, is_torchsde_available, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_pt_objects import * # noqa F403 else: from .scheduling_consistency_models import CMStochasticIterativeScheduler from .scheduling_ddim import DDIMScheduler from .scheduling_ddim_inverse import DDIMInverseScheduler from .scheduling_ddim_parallel import DDIMParallelScheduler from .scheduling_ddpm import DDPMScheduler from .scheduling_ddpm_parallel import DDPMParallelScheduler from .scheduling_deis_multistep import DEISMultistepScheduler from .scheduling_dpmsolver_multistep import DPMSolverMultistepScheduler from .scheduling_dpmsolver_multistep_inverse import DPMSolverMultistepInverseScheduler from .scheduling_dpmsolver_singlestep import DPMSolverSinglestepScheduler from .scheduling_euler_ancestral_discrete import EulerAncestralDiscreteScheduler from .scheduling_euler_discrete import EulerDiscreteScheduler from .scheduling_heun_discrete import HeunDiscreteScheduler from .scheduling_ipndm import IPNDMScheduler from .scheduling_k_dpm_2_ancestral_discrete import KDPMaAncestralDiscreteScheduler from .scheduling_k_dpm_2_discrete import KDPMaDiscreteScheduler from .scheduling_karras_ve import KarrasVeScheduler from .scheduling_pndm import PNDMScheduler from .scheduling_repaint import RePaintScheduler from .scheduling_sde_ve import ScoreSdeVeScheduler from .scheduling_sde_vp import ScoreSdeVpScheduler from .scheduling_unclip import UnCLIPScheduler from .scheduling_unipc_multistep import UniPCMultistepScheduler from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin from .scheduling_vq_diffusion import VQDiffusionScheduler try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_flax_objects import * # noqa F403 else: from .scheduling_ddim_flax import FlaxDDIMScheduler from .scheduling_ddpm_flax import FlaxDDPMScheduler from .scheduling_dpmsolver_multistep_flax import FlaxDPMSolverMultistepScheduler from .scheduling_karras_ve_flax import FlaxKarrasVeScheduler from .scheduling_lms_discrete_flax import FlaxLMSDiscreteScheduler from .scheduling_pndm_flax import FlaxPNDMScheduler from .scheduling_sde_ve_flax import FlaxScoreSdeVeScheduler from .scheduling_utils_flax import ( FlaxKarrasDiffusionSchedulers, FlaxSchedulerMixin, FlaxSchedulerOutput, broadcast_to_shape_from_left, ) try: if not (is_torch_available() and is_scipy_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_torch_and_scipy_objects import * # noqa F403 else: from .scheduling_lms_discrete import LMSDiscreteScheduler try: if not (is_torch_available() and is_torchsde_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_torch_and_torchsde_objects import * # noqa F403 else: from .scheduling_dpmsolver_sde import DPMSolverSDEScheduler
52
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import YolosConfig, YolosForObjectDetection, YolosImageProcessor from transformers.utils import logging logging.set_verbosity_info() SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) def A ( __UpperCamelCase ) -> YolosConfig: A__ = YolosConfig() # size of the architecture if "yolos_ti" in yolos_name: A__ = 192 A__ = 768 A__ = 12 A__ = 3 A__ = [800, 1_333] A__ = False elif yolos_name == "yolos_s_dWr": A__ = 330 A__ = 14 A__ = 6 A__ = 1_320 elif "yolos_s" in yolos_name: A__ = 384 A__ = 1_536 A__ = 12 A__ = 6 elif "yolos_b" in yolos_name: A__ = [800, 1_344] A__ = 91 A__ = 'huggingface/label-files' A__ = 'coco-detection-id2label.json' A__ = json.load(open(hf_hub_download(__UpperCamelCase , __UpperCamelCase , repo_type='dataset' ) , 'r' ) ) A__ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} A__ = idalabel A__ = {v: k for k, v in idalabel.items()} return config def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = False ) -> str: for i in range(config.num_hidden_layers ): # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) A__ = state_dict.pop(f'''blocks.{i}.attn.qkv.weight''' ) A__ = state_dict.pop(f'''blocks.{i}.attn.qkv.bias''' ) # next, add query, keys and values (in that order) to the state dict A__ = in_proj_weight[: config.hidden_size, :] A__ = in_proj_bias[: config.hidden_size] A__ = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] A__ = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] A__ = in_proj_weight[-config.hidden_size :, :] A__ = in_proj_bias[-config.hidden_size :] def A ( __UpperCamelCase ) -> str: if "backbone" in name: A__ = name.replace('backbone' , 'vit' ) if "cls_token" in name: A__ = name.replace('cls_token' , 'embeddings.cls_token' ) if "det_token" in name: A__ = name.replace('det_token' , 'embeddings.detection_tokens' ) if "mid_pos_embed" in name: A__ = name.replace('mid_pos_embed' , 'encoder.mid_position_embeddings' ) if "pos_embed" in name: A__ = name.replace('pos_embed' , 'embeddings.position_embeddings' ) if "patch_embed.proj" in name: A__ = name.replace('patch_embed.proj' , 'embeddings.patch_embeddings.projection' ) if "blocks" in name: A__ = name.replace('blocks' , 'encoder.layer' ) if "attn.proj" in name: A__ = name.replace('attn.proj' , 'attention.output.dense' ) if "attn" in name: A__ = name.replace('attn' , 'attention.self' ) if "norm1" in name: A__ = name.replace('norm1' , 'layernorm_before' ) if "norm2" in name: A__ = name.replace('norm2' , 'layernorm_after' ) if "mlp.fc1" in name: A__ = name.replace('mlp.fc1' , 'intermediate.dense' ) if "mlp.fc2" in name: A__ = name.replace('mlp.fc2' , 'output.dense' ) if "class_embed" in name: A__ = name.replace('class_embed' , 'class_labels_classifier' ) if "bbox_embed" in name: A__ = name.replace('bbox_embed' , 'bbox_predictor' ) if "vit.norm" in name: A__ = name.replace('vit.norm' , 'vit.layernorm' ) return name def A ( __UpperCamelCase , __UpperCamelCase ) -> dict: for key in orig_state_dict.copy().keys(): A__ = orig_state_dict.pop(__UpperCamelCase ) if "qkv" in key: A__ = key.split('.' ) A__ = int(key_split[2] ) A__ = model.vit.encoder.layer[layer_num].attention.attention.all_head_size if "weight" in key: A__ = val[:dim, :] A__ = val[ dim : dim * 2, : ] A__ = val[-dim:, :] else: A__ = val[:dim] A__ = val[dim : dim * 2] A__ = val[-dim:] else: A__ = val return orig_state_dict def A ( ) -> torch.Tensor: A__ = 'http://images.cocodataset.org/val2017/000000039769.jpg' A__ = Image.open(requests.get(__UpperCamelCase , stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = False ) -> List[str]: A__ = get_yolos_config(__UpperCamelCase ) # load original state_dict A__ = torch.load(__UpperCamelCase , map_location='cpu' )['model'] # load 🤗 model A__ = YolosForObjectDetection(__UpperCamelCase ) model.eval() A__ = convert_state_dict(__UpperCamelCase , __UpperCamelCase ) model.load_state_dict(__UpperCamelCase ) # Check outputs on an image, prepared by YolosImageProcessor A__ = 800 if yolos_name != 'yolos_ti' else 512 A__ = YolosImageProcessor(format='coco_detection' , size=__UpperCamelCase ) A__ = image_processor(images=prepare_img() , return_tensors='pt' ) A__ = model(**__UpperCamelCase ) A__ , A__ = outputs.logits, outputs.pred_boxes A__ , A__ = None, None if yolos_name == "yolos_ti": A__ = torch.tensor( [[-39.5022, -11.9820, -17.6888], [-29.9574, -9.9769, -17.7691], [-42.3281, -20.7200, -30.6294]] ) A__ = torch.tensor( [[0.4021, 0.0836, 0.7979], [0.0184, 0.2609, 0.0364], [0.1781, 0.2004, 0.2095]] ) elif yolos_name == "yolos_s_200_pre": A__ = torch.tensor( [[-24.0248, -10.3024, -14.8290], [-42.0392, -16.8200, -27.4334], [-27.2743, -11.8154, -18.7148]] ) A__ = torch.tensor( [[0.2559, 0.5455, 0.4706], [0.2989, 0.7279, 0.1875], [0.7732, 0.4017, 0.4462]] ) elif yolos_name == "yolos_s_300_pre": A__ = torch.tensor( [[-36.2220, -14.4385, -23.5457], [-35.6970, -14.7583, -21.3935], [-31.5939, -13.6042, -16.8049]] ) A__ = torch.tensor( [[0.7614, 0.2316, 0.4728], [0.7168, 0.4495, 0.3855], [0.4996, 0.1466, 0.9996]] ) elif yolos_name == "yolos_s_dWr": A__ = torch.tensor( [[-42.8668, -24.1049, -41.1690], [-34.7456, -14.1274, -24.9194], [-33.7898, -12.1946, -25.6495]] ) A__ = torch.tensor( [[0.5587, 0.2773, 0.0605], [0.5004, 0.3014, 0.9994], [0.4999, 0.1548, 0.9994]] ) elif yolos_name == "yolos_base": A__ = torch.tensor( [[-40.6064, -24.3084, -32.6447], [-55.1990, -30.7719, -35.5877], [-51.4311, -33.3507, -35.6462]] ) A__ = torch.tensor( [[0.5555, 0.2794, 0.0655], [0.9049, 0.2664, 0.1894], [0.9183, 0.1984, 0.1635]] ) else: raise ValueError(f'''Unknown yolos_name: {yolos_name}''' ) assert torch.allclose(logits[0, :3, :3] , __UpperCamelCase , atol=1E-4 ) assert torch.allclose(pred_boxes[0, :3, :3] , __UpperCamelCase , atol=1E-4 ) Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) print(f'''Saving model {yolos_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(__UpperCamelCase ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(__UpperCamelCase ) if push_to_hub: A__ = { 'yolos_ti': 'yolos-tiny', 'yolos_s_200_pre': 'yolos-small', 'yolos_s_300_pre': 'yolos-small-300', 'yolos_s_dWr': 'yolos-small-dwr', 'yolos_base': 'yolos-base', } print('Pushing to the hub...' ) A__ = model_mapping[yolos_name] image_processor.push_to_hub(__UpperCamelCase , organization='hustvl' ) model.push_to_hub(__UpperCamelCase , organization='hustvl' ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--yolos_name''', default='''yolos_s_200_pre''', type=str, help=( '''Name of the YOLOS model you\'d like to convert. Should be one of \'yolos_ti\', \'yolos_s_200_pre\',''' ''' \'yolos_s_300_pre\', \'yolos_s_dWr\', \'yolos_base\'.''' ), ) parser.add_argument( '''--checkpoint_path''', default=None, type=str, help='''Path to the original state dict (.pth file).''' ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_yolos_checkpoint(args.yolos_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub)
52
1
from typing import Dict, List from nltk.translate import gleu_score import datasets from datasets import MetricInfo SCREAMING_SNAKE_CASE__ = '''\ @misc{wu2016googles, title={Google\'s Neural Machine Translation System: Bridging the Gap between Human and Machine Translation}, author={Yonghui Wu and Mike Schuster and Zhifeng Chen and Quoc V. Le and Mohammad Norouzi and Wolfgang Macherey and Maxim Krikun and Yuan Cao and Qin Gao and Klaus Macherey and Jeff Klingner and Apurva Shah and Melvin Johnson and Xiaobing Liu and Łukasz Kaiser and Stephan Gouws and Yoshikiyo Kato and Taku Kudo and Hideto Kazawa and Keith Stevens and George Kurian and Nishant Patil and Wei Wang and Cliff Young and Jason Smith and Jason Riesa and Alex Rudnick and Oriol Vinyals and Greg Corrado and Macduff Hughes and Jeffrey Dean}, year={2016}, eprint={1609.08144}, archivePrefix={arXiv}, primaryClass={cs.CL} } ''' SCREAMING_SNAKE_CASE__ = '''\ The BLEU score has some undesirable properties when used for single sentences, as it was designed to be a corpus measure. We therefore use a slightly different score for our RL experiments which we call the \'GLEU score\'. For the GLEU score, we record all sub-sequences of 1, 2, 3 or 4 tokens in output and target sequence (n-grams). We then compute a recall, which is the ratio of the number of matching n-grams to the number of total n-grams in the target (ground truth) sequence, and a precision, which is the ratio of the number of matching n-grams to the number of total n-grams in the generated output sequence. Then GLEU score is simply the minimum of recall and precision. This GLEU score\'s range is always between 0 (no matches) and 1 (all match) and it is symmetrical when switching output and target. According to our experiments, GLEU score correlates quite well with the BLEU metric on a corpus level but does not have its drawbacks for our per sentence reward objective. ''' SCREAMING_SNAKE_CASE__ = '''\ Computes corpus-level Google BLEU (GLEU) score of translated segments against one or more references. Instead of averaging the sentence level GLEU scores (i.e. macro-average precision), Wu et al. (2016) sum up the matching tokens and the max of hypothesis and reference tokens for each sentence, then compute using the aggregate values. Args: predictions (list of str): list of translations to score. Each translation should be tokenized into a list of tokens. references (list of list of str): list of lists of references for each translation. Each reference should be tokenized into a list of tokens. min_len (int): The minimum order of n-gram this function should extract. Defaults to 1. max_len (int): The maximum order of n-gram this function should extract. Defaults to 4. Returns: \'google_bleu\': google_bleu score Examples: Example 1: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results["google_bleu"], 2)) 0.44 Example 2: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\', ... \'heed\', \'the\', \'cat\', \'commands\'] >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\', ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\', ... \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results["google_bleu"], 2)) 0.61 Example 3: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\', ... \'heed\', \'the\', \'cat\', \'commands\'] >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\', ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\', ... \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references, min_len=2) >>> print(round(results["google_bleu"], 2)) 0.53 Example 4: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\', ... \'heed\', \'the\', \'cat\', \'commands\'] >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\', ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\', ... \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses,references=list_of_references, min_len=2, max_len=6) >>> print(round(results["google_bleu"], 2)) 0.4 ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __lowerCAmelCase ( datasets.Metric ): """simple docstring""" def _a ( self : Optional[Any] ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Sequence(datasets.Value('string' , id='token' ) , id='sequence' ), 'references': datasets.Sequence( datasets.Sequence(datasets.Value('string' , id='token' ) , id='sequence' ) , id='references' ), } ) , ) def _a ( self : Tuple , _snake_case : List[List[List[str]]] , _snake_case : List[List[str]] , _snake_case : int = 1 , _snake_case : int = 4 , ): """simple docstring""" return { "google_bleu": gleu_score.corpus_gleu( list_of_references=_snake_case , hypotheses=_snake_case , min_len=_snake_case , max_len=_snake_case ) }
52
from typing import TYPE_CHECKING from ..utils import _LazyModule SCREAMING_SNAKE_CASE__ = { '''config''': [ '''EXTERNAL_DATA_FORMAT_SIZE_LIMIT''', '''OnnxConfig''', '''OnnxConfigWithPast''', '''OnnxSeq2SeqConfigWithPast''', '''PatchingSpec''', ], '''convert''': ['''export''', '''validate_model_outputs'''], '''features''': ['''FeaturesManager'''], '''utils''': ['''ParameterFormat''', '''compute_serialized_parameters_size'''], } if TYPE_CHECKING: from .config import ( EXTERNAL_DATA_FORMAT_SIZE_LIMIT, OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast, PatchingSpec, ) from .convert import export, validate_model_outputs from .features import FeaturesManager from .utils import ParameterFormat, compute_serialized_parameters_size else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
52
1
import os import re import shutil from argparse import ArgumentParser, Namespace from datasets.commands import BaseDatasetsCLICommand from datasets.utils.logging import get_logger SCREAMING_SNAKE_CASE__ = '''<<<<<<< This should probably be modified because it mentions: ''' SCREAMING_SNAKE_CASE__ = '''======= >>>>>>> ''' SCREAMING_SNAKE_CASE__ = [ '''TextEncoderConfig''', '''ByteTextEncoder''', '''SubwordTextEncoder''', '''encoder_config''', '''maybe_build_from_corpus''', '''manual_dir''', ] SCREAMING_SNAKE_CASE__ = [ # (pattern, replacement) # Order is important here for some replacements (r'''tfds\.core''', r'''datasets'''), (r'''tf\.io\.gfile\.GFile''', r'''open'''), (r'''tf\.([\w\d]+)''', r'''datasets.Value(\'\1\')'''), (r'''tfds\.features\.Text\(\)''', r'''datasets.Value(\'string\')'''), (r'''tfds\.features\.Text\(''', r'''datasets.Value(\'string\'),'''), (r'''features\s*=\s*tfds.features.FeaturesDict\(''', r'''features=datasets.Features('''), (r'''tfds\.features\.FeaturesDict\(''', r'''dict('''), (r'''The TensorFlow Datasets Authors''', r'''The TensorFlow Datasets Authors and the HuggingFace Datasets Authors'''), (r'''tfds\.''', r'''datasets.'''), (r'''dl_manager\.manual_dir''', r'''self.config.data_dir'''), (r'''self\.builder_config''', r'''self.config'''), ] def A ( __UpperCamelCase ) -> Optional[Any]: return ConvertCommand(args.tfds_path , args.datasets_directory ) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" @staticmethod def _a ( _snake_case : ArgumentParser ): """simple docstring""" A__ = parser.add_parser( 'convert' , help='Convert a TensorFlow Datasets dataset to a HuggingFace Datasets dataset.' , ) train_parser.add_argument( '--tfds_path' , type=_snake_case , required=_snake_case , help='Path to a TensorFlow Datasets folder to convert or a single tfds file to convert.' , ) train_parser.add_argument( '--datasets_directory' , type=_snake_case , required=_snake_case , help='Path to the HuggingFace Datasets folder.' ) train_parser.set_defaults(func=_snake_case ) def __init__( self : str , _snake_case : str , _snake_case : str , *_snake_case : Dict ): """simple docstring""" A__ = get_logger('datasets-cli/converting' ) A__ = tfds_path A__ = datasets_directory def _a ( self : Dict ): """simple docstring""" if os.path.isdir(self._tfds_path ): A__ = os.path.abspath(self._tfds_path ) elif os.path.isfile(self._tfds_path ): A__ = os.path.dirname(self._tfds_path ) else: raise ValueError('--tfds_path is neither a directory nor a file. Please check path.' ) A__ = os.path.abspath(self._datasets_directory ) self._logger.info(F'''Converting datasets from {abs_tfds_path} to {abs_datasets_path}''' ) A__ = [] A__ = [] A__ = {} if os.path.isdir(self._tfds_path ): A__ = os.listdir(_snake_case ) else: A__ = [os.path.basename(self._tfds_path )] for f_name in file_names: self._logger.info(F'''Looking at file {f_name}''' ) A__ = os.path.join(_snake_case , _snake_case ) A__ = os.path.join(_snake_case , _snake_case ) if not os.path.isfile(_snake_case ) or "__init__" in f_name or "_test" in f_name or ".py" not in f_name: self._logger.info('Skipping file' ) continue with open(_snake_case , encoding='utf-8' ) as f: A__ = f.readlines() A__ = [] A__ = False A__ = False A__ = [] for line in lines: A__ = line # Convert imports if "import tensorflow.compat.v2 as tf" in out_line: continue elif "@tfds.core" in out_line: continue elif "builder=self" in out_line: continue elif "import tensorflow_datasets.public_api as tfds" in out_line: A__ = 'import datasets\n' elif "import tensorflow" in out_line: # order is important here A__ = '' continue elif "from absl import logging" in out_line: A__ = 'from datasets import logging\n' elif "getLogger" in out_line: A__ = out_line.replace('getLogger' , 'get_logger' ) elif any(expression in out_line for expression in TO_HIGHLIGHT ): A__ = True A__ = list(filter(lambda _snake_case : e in out_line , _snake_case ) ) out_lines.append(HIGHLIGHT_MESSAGE_PRE + str(_snake_case ) + '\n' ) out_lines.append(_snake_case ) out_lines.append(_snake_case ) continue else: for pattern, replacement in TO_CONVERT: A__ = re.sub(_snake_case , _snake_case , _snake_case ) # Take care of saving utilities (to later move them together with main script) if "tensorflow_datasets" in out_line: A__ = re.match(R'from\stensorflow_datasets.*import\s([^\.\r\n]+)' , _snake_case ) tfds_imports.extend(imp.strip() for imp in match.group(1 ).split(',' ) ) A__ = 'from . import ' + match.group(1 ) # Check we have not forget anything if "tf." in out_line or "tfds." in out_line or "tensorflow_datasets" in out_line: raise ValueError(F'''Error converting {out_line.strip()}''' ) if "GeneratorBasedBuilder" in out_line or "BeamBasedBuilder" in out_line: A__ = True out_lines.append(_snake_case ) if is_builder or "wmt" in f_name: # We create a new directory for each dataset A__ = f_name.replace('.py' , '' ) A__ = os.path.join(_snake_case , _snake_case ) A__ = os.path.join(_snake_case , _snake_case ) os.makedirs(_snake_case , exist_ok=_snake_case ) self._logger.info(F'''Adding directory {output_dir}''' ) imports_to_builder_map.update({imp: output_dir for imp in tfds_imports} ) else: # Utilities will be moved at the end utils_files.append(_snake_case ) if needs_manual_update: with_manual_update.append(_snake_case ) with open(_snake_case , 'w' , encoding='utf-8' ) as f: f.writelines(_snake_case ) self._logger.info(F'''Converted in {output_file}''' ) for utils_file in utils_files: try: A__ = os.path.basename(_snake_case ) A__ = imports_to_builder_map[f_name.replace('.py' , '' )] self._logger.info(F'''Moving {dest_folder} to {utils_file}''' ) shutil.copy(_snake_case , _snake_case ) except KeyError: self._logger.error(F'''Cannot find destination folder for {utils_file}. Please copy manually.''' ) if with_manual_update: for file_path in with_manual_update: self._logger.warning( F'''You need to manually update file {file_path} to remove configurations using \'TextEncoderConfig\'.''' )
52
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_rembert import RemBertTokenizer else: SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = {'''vocab_file''': '''sentencepiece.model''', '''tokenizer_file''': '''tokenizer.json'''} SCREAMING_SNAKE_CASE__ = { '''vocab_file''': { '''google/rembert''': '''https://huggingface.co/google/rembert/resolve/main/sentencepiece.model''', }, '''tokenizer_file''': { '''google/rembert''': '''https://huggingface.co/google/rembert/resolve/main/tokenizer.json''', }, } SCREAMING_SNAKE_CASE__ = { '''google/rembert''': 2_5_6, } SCREAMING_SNAKE_CASE__ = '''▁''' class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Any = VOCAB_FILES_NAMES A__ : str = PRETRAINED_VOCAB_FILES_MAP A__ : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A__ : int = RemBertTokenizer def __init__( self : Union[str, Any] , _snake_case : Any=None , _snake_case : Optional[Any]=None , _snake_case : Any=True , _snake_case : Optional[int]=True , _snake_case : Dict=False , _snake_case : Dict="[CLS]" , _snake_case : List[Any]="[SEP]" , _snake_case : Union[str, Any]="<unk>" , _snake_case : List[str]="[SEP]" , _snake_case : List[str]="<pad>" , _snake_case : str="[CLS]" , _snake_case : Any="[MASK]" , **_snake_case : Any , ): """simple docstring""" A__ = AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case ) if isinstance(_snake_case , _snake_case ) else mask_token super().__init__( _snake_case , tokenizer_file=_snake_case , do_lower_case=_snake_case , remove_space=_snake_case , keep_accents=_snake_case , bos_token=_snake_case , eos_token=_snake_case , unk_token=_snake_case , sep_token=_snake_case , pad_token=_snake_case , cls_token=_snake_case , mask_token=_snake_case , **_snake_case , ) A__ = do_lower_case A__ = remove_space A__ = keep_accents A__ = vocab_file A__ = False if not self.vocab_file else True def _a ( self : Any , _snake_case : List[int] , _snake_case : Optional[List[int]] = None ): """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def _a ( self : Tuple , _snake_case : List[int] , _snake_case : Optional[List[int]] = None , _snake_case : bool = False ): """simple docstring""" if already_has_special_tokens: if token_ids_a is not None: raise ValueError( 'You should not supply a second sequence if the provided sequence of ' 'ids is already formatted with special tokens for the model.' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(_snake_case )) + [1] + ([0] * len(_snake_case )) + [1] return [1] + ([0] * len(_snake_case )) + [1] def _a ( self : Dict , _snake_case : List[int] , _snake_case : Optional[List[int]] = None ): """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def _a ( self : Any , _snake_case : str , _snake_case : Optional[str] = None ): """simple docstring""" if not os.path.isdir(_snake_case ): logger.error('Vocabulary path ({}) should be a directory'.format(_snake_case ) ) return A__ = os.path.join( _snake_case , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_snake_case ): copyfile(self.vocab_file , _snake_case ) return (out_vocab_file,)
52
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available SCREAMING_SNAKE_CASE__ = { '''configuration_roc_bert''': ['''ROC_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''RoCBertConfig'''], '''tokenization_roc_bert''': ['''RoCBertTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: pass try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''ROC_BERT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''RoCBertForCausalLM''', '''RoCBertForMaskedLM''', '''RoCBertForMultipleChoice''', '''RoCBertForPreTraining''', '''RoCBertForQuestionAnswering''', '''RoCBertForSequenceClassification''', '''RoCBertForTokenClassification''', '''RoCBertLayer''', '''RoCBertModel''', '''RoCBertPreTrainedModel''', '''load_tf_weights_in_roc_bert''', ] if TYPE_CHECKING: from .configuration_roc_bert import ROC_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RoCBertConfig from .tokenization_roc_bert import RoCBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: raise OptionalDependencyNotAvailable() try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roc_bert import ( ROC_BERT_PRETRAINED_MODEL_ARCHIVE_LIST, RoCBertForCausalLM, RoCBertForMaskedLM, RoCBertForMultipleChoice, RoCBertForPreTraining, RoCBertForQuestionAnswering, RoCBertForSequenceClassification, RoCBertForTokenClassification, RoCBertLayer, RoCBertModel, RoCBertPreTrainedModel, load_tf_weights_in_roc_bert, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
52
import tempfile import unittest from make_student import create_student_by_copying_alternating_layers from transformers import AutoConfig from transformers.file_utils import cached_property from transformers.testing_utils import require_torch SCREAMING_SNAKE_CASE__ = '''sshleifer/bart-tiny-random''' SCREAMING_SNAKE_CASE__ = '''patrickvonplaten/t5-tiny-random''' @require_torch class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @cached_property def _a ( self : Optional[int] ): """simple docstring""" return AutoConfig.from_pretrained(_snake_case ) def _a ( self : Optional[Any] ): """simple docstring""" A__ , *A__ = create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=1 , d=1 ) self.assertEqual(student.config.num_hidden_layers , 1 ) def _a ( self : Optional[int] ): """simple docstring""" A__ , *A__ = create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=1 , d=_snake_case ) def _a ( self : int ): """simple docstring""" A__ , *A__ = create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=1 , d=_snake_case ) self.assertEqual(student.config.encoder_layers , 1 ) self.assertEqual(student.config.decoder_layers , self.teacher_config.encoder_layers ) def _a ( self : str ): """simple docstring""" A__ , *A__ = create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=1 , d=1 ) self.assertEqual(student.config.encoder_layers , 1 ) self.assertEqual(student.config.decoder_layers , 1 ) def _a ( self : str ): """simple docstring""" with self.assertRaises(_snake_case ): create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=_snake_case , d=_snake_case )
52
1
# 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.utils import ComputeEnvironment from .cluster import get_cluster_input from .config_args import cache_dir, default_config_file, default_yaml_config_file, load_config_from_file # noqa: F401 from .config_utils import _ask_field, _ask_options, _convert_compute_environment # noqa: F401 from .sagemaker import get_sagemaker_input SCREAMING_SNAKE_CASE__ = '''Launches a series of prompts to create and save a `default_config.yaml` configuration file for your training system. Should always be ran first on your machine''' def A ( ) -> Optional[Any]: A__ = _ask_options( 'In which compute environment are you running?' , ['This machine', 'AWS (Amazon SageMaker)'] , _convert_compute_environment , ) if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER: A__ = get_sagemaker_input() else: A__ = get_cluster_input() return config def A ( __UpperCamelCase=None ) -> Dict: if subparsers is not None: A__ = subparsers.add_parser('config' , description=__UpperCamelCase ) else: A__ = argparse.ArgumentParser('Accelerate config command' , description=__UpperCamelCase ) parser.add_argument( '--config_file' , default=__UpperCamelCase , 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=__UpperCamelCase ) return parser def A ( __UpperCamelCase ) -> str: A__ = get_user_input() if args.config_file is not None: A__ = args.config_file else: if not os.path.isdir(__UpperCamelCase ): os.makedirs(__UpperCamelCase ) A__ = default_yaml_config_file if config_file.endswith('.json' ): config.to_json_file(__UpperCamelCase ) else: config.to_yaml_file(__UpperCamelCase ) print(f'''accelerate configuration saved at {config_file}''' ) def A ( ) -> List[str]: A__ = config_command_parser() A__ = parser.parse_args() config_command(__UpperCamelCase ) if __name__ == "__main__": main()
52
from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Union[str, Any] = ["image_processor", "tokenizer"] A__ : Optional[Any] = "BridgeTowerImageProcessor" A__ : List[Any] = ("RobertaTokenizer", "RobertaTokenizerFast") def __init__( self : List[Any] , _snake_case : Optional[Any] , _snake_case : Optional[int] ): """simple docstring""" super().__init__(_snake_case , _snake_case ) def __call__( self : List[Any] , _snake_case : int , _snake_case : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , _snake_case : bool = True , _snake_case : Union[bool, str, PaddingStrategy] = False , _snake_case : Union[bool, str, TruncationStrategy] = None , _snake_case : Optional[int] = None , _snake_case : int = 0 , _snake_case : Optional[int] = None , _snake_case : Optional[bool] = None , _snake_case : Optional[bool] = None , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = True , _snake_case : Optional[Union[str, TensorType]] = None , **_snake_case : Optional[int] , ): """simple docstring""" A__ = self.tokenizer( text=_snake_case , add_special_tokens=_snake_case , padding=_snake_case , truncation=_snake_case , max_length=_snake_case , stride=_snake_case , pad_to_multiple_of=_snake_case , return_token_type_ids=_snake_case , return_attention_mask=_snake_case , return_overflowing_tokens=_snake_case , return_special_tokens_mask=_snake_case , return_offsets_mapping=_snake_case , return_length=_snake_case , verbose=_snake_case , return_tensors=_snake_case , **_snake_case , ) # add pixel_values + pixel_mask A__ = self.image_processor( _snake_case , return_tensors=_snake_case , do_normalize=_snake_case , do_center_crop=_snake_case , **_snake_case ) encoding.update(_snake_case ) return encoding def _a ( self : Any , *_snake_case : Tuple , **_snake_case : List[Any] ): """simple docstring""" return self.tokenizer.batch_decode(*_snake_case , **_snake_case ) def _a ( self : Dict , *_snake_case : Dict , **_snake_case : List[str] ): """simple docstring""" return self.tokenizer.decode(*_snake_case , **_snake_case ) @property def _a ( self : Tuple ): """simple docstring""" A__ = self.tokenizer.model_input_names A__ = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
52
1
import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO ) SCREAMING_SNAKE_CASE__ = logging.getLogger(__name__) def A ( __UpperCamelCase , __UpperCamelCase ) -> str: A__ = np.argmax(__UpperCamelCase , axis=1 ) return np.sum(outputs == labels ) def A ( __UpperCamelCase ) -> Optional[Any]: with open(__UpperCamelCase , encoding='utf_8' ) as f: A__ = csv.reader(__UpperCamelCase ) A__ = [] next(__UpperCamelCase ) # skip the first line for line in tqdm(__UpperCamelCase ): output.append((' '.join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: A__ = [] for dataset in encoded_datasets: A__ = len(__UpperCamelCase ) A__ = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) A__ = np.zeros((n_batch, 2) , dtype=np.intaa ) A__ = np.full((n_batch, 2, input_len) , fill_value=-100 , dtype=np.intaa ) A__ = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(__UpperCamelCase ): A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = with_conta A__ = with_conta A__ = len(__UpperCamelCase ) - 1 A__ = len(__UpperCamelCase ) - 1 A__ = with_conta A__ = with_conta A__ = mc_label A__ = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(__UpperCamelCase ) for t in all_inputs ) ) return tensor_datasets def A ( ) -> List[Any]: A__ = argparse.ArgumentParser() parser.add_argument('--model_name' , type=__UpperCamelCase , default='openai-gpt' , help='pretrained model name' ) parser.add_argument('--do_train' , action='store_true' , help='Whether to run training.' ) parser.add_argument('--do_eval' , action='store_true' , help='Whether to run eval on the dev set.' ) parser.add_argument( '--output_dir' , default=__UpperCamelCase , type=__UpperCamelCase , required=__UpperCamelCase , help='The output directory where the model predictions and checkpoints will be written.' , ) parser.add_argument('--train_dataset' , type=__UpperCamelCase , default='' ) parser.add_argument('--eval_dataset' , type=__UpperCamelCase , default='' ) parser.add_argument('--seed' , type=__UpperCamelCase , default=42 ) parser.add_argument('--num_train_epochs' , type=__UpperCamelCase , default=3 ) parser.add_argument('--train_batch_size' , type=__UpperCamelCase , default=8 ) parser.add_argument('--eval_batch_size' , type=__UpperCamelCase , default=16 ) parser.add_argument('--adam_epsilon' , default=1E-8 , type=__UpperCamelCase , help='Epsilon for Adam optimizer.' ) parser.add_argument('--max_grad_norm' , type=__UpperCamelCase , default=1 ) parser.add_argument( '--max_steps' , default=-1 , type=__UpperCamelCase , help=( 'If > 0: set total number of training steps to perform. Override num_train_epochs.' ) , ) parser.add_argument( '--gradient_accumulation_steps' , type=__UpperCamelCase , default=1 , help='Number of updates steps to accumulate before performing a backward/update pass.' , ) parser.add_argument('--learning_rate' , type=__UpperCamelCase , default=6.25E-5 ) parser.add_argument('--warmup_steps' , default=0 , type=__UpperCamelCase , help='Linear warmup over warmup_steps.' ) parser.add_argument('--lr_schedule' , type=__UpperCamelCase , default='warmup_linear' ) parser.add_argument('--weight_decay' , type=__UpperCamelCase , default=0.01 ) parser.add_argument('--lm_coef' , type=__UpperCamelCase , default=0.9 ) parser.add_argument('--n_valid' , type=__UpperCamelCase , default=374 ) parser.add_argument('--server_ip' , type=__UpperCamelCase , default='' , help='Can be used for distant debugging.' ) parser.add_argument('--server_port' , type=__UpperCamelCase , default='' , help='Can be used for distant debugging.' ) A__ = parser.parse_args() print(__UpperCamelCase ) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print('Waiting for debugger attach' ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__UpperCamelCase ) ptvsd.wait_for_attach() random.seed(args.seed ) np.random.seed(args.seed ) torch.manual_seed(args.seed ) torch.cuda.manual_seed_all(args.seed ) A__ = torch.device('cuda' if torch.cuda.is_available() else 'cpu' ) A__ = torch.cuda.device_count() logger.info('device: {}, n_gpu {}'.format(__UpperCamelCase , __UpperCamelCase ) ) if not args.do_train and not args.do_eval: raise ValueError('At least one of `do_train` or `do_eval` must be True.' ) if not os.path.exists(args.output_dir ): os.makedirs(args.output_dir ) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset A__ = ['_start_', '_delimiter_', '_classify_'] A__ = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(__UpperCamelCase ) A__ = tokenizer.convert_tokens_to_ids(__UpperCamelCase ) A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(__UpperCamelCase ) ) model.to(__UpperCamelCase ) # Load and encode the datasets def tokenize_and_encode(__UpperCamelCase ): if isinstance(__UpperCamelCase , __UpperCamelCase ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(__UpperCamelCase ) ) elif isinstance(__UpperCamelCase , __UpperCamelCase ): return obj return [tokenize_and_encode(__UpperCamelCase ) for o in obj] logger.info('Encoding dataset...' ) A__ = load_rocstories_dataset(args.train_dataset ) A__ = load_rocstories_dataset(args.eval_dataset ) A__ = (train_dataset, eval_dataset) A__ = tokenize_and_encode(__UpperCamelCase ) # Compute the max input length for the Transformer A__ = model.config.n_positions // 2 - 2 A__ = max( len(story[:max_length] ) + max(len(conta[:max_length] ) , len(conta[:max_length] ) ) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset ) A__ = min(__UpperCamelCase , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders A__ = pre_process_datasets(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , *__UpperCamelCase ) A__ , A__ = tensor_datasets[0], tensor_datasets[1] A__ = TensorDataset(*__UpperCamelCase ) A__ = RandomSampler(__UpperCamelCase ) A__ = DataLoader(__UpperCamelCase , sampler=__UpperCamelCase , batch_size=args.train_batch_size ) A__ = TensorDataset(*__UpperCamelCase ) A__ = SequentialSampler(__UpperCamelCase ) A__ = DataLoader(__UpperCamelCase , sampler=__UpperCamelCase , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: A__ = args.max_steps A__ = args.max_steps // (len(__UpperCamelCase ) // args.gradient_accumulation_steps) + 1 else: A__ = len(__UpperCamelCase ) // args.gradient_accumulation_steps * args.num_train_epochs A__ = list(model.named_parameters() ) A__ = ['bias', 'LayerNorm.bias', 'LayerNorm.weight'] A__ = [ { 'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay )], 'weight_decay': args.weight_decay, }, {'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay )], 'weight_decay': 0.0}, ] A__ = AdamW(__UpperCamelCase , lr=args.learning_rate , eps=args.adam_epsilon ) A__ = get_linear_schedule_with_warmup( __UpperCamelCase , num_warmup_steps=args.warmup_steps , num_training_steps=__UpperCamelCase ) if args.do_train: A__ , A__ , A__ = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc='Epoch' ): A__ = 0 A__ = 0 A__ = tqdm(__UpperCamelCase , desc='Training' ) for step, batch in enumerate(__UpperCamelCase ): A__ = tuple(t.to(__UpperCamelCase ) for t in batch ) A__ , A__ , A__ , A__ = batch A__ = model(__UpperCamelCase , mc_token_ids=__UpperCamelCase , lm_labels=__UpperCamelCase , mc_labels=__UpperCamelCase ) A__ = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() A__ = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 A__ = 'Training loss: {:.2e} lr: {:.2e}'.format(__UpperCamelCase , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer A__ = model.module if hasattr(__UpperCamelCase , 'module' ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` A__ = os.path.join(args.output_dir , __UpperCamelCase ) A__ = os.path.join(args.output_dir , __UpperCamelCase ) torch.save(model_to_save.state_dict() , __UpperCamelCase ) model_to_save.config.to_json_file(__UpperCamelCase ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) A__ = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(__UpperCamelCase ) if args.do_eval: model.eval() A__ , A__ = 0, 0 A__ , A__ = 0, 0 for batch in tqdm(__UpperCamelCase , desc='Evaluating' ): A__ = tuple(t.to(__UpperCamelCase ) for t in batch ) A__ , A__ , A__ , A__ = batch with torch.no_grad(): A__ , A__ , A__ , A__ = model( __UpperCamelCase , mc_token_ids=__UpperCamelCase , lm_labels=__UpperCamelCase , mc_labels=__UpperCamelCase ) A__ = mc_logits.detach().cpu().numpy() A__ = mc_labels.to('cpu' ).numpy() A__ = accuracy(__UpperCamelCase , __UpperCamelCase ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 A__ = eval_loss / nb_eval_steps A__ = eval_accuracy / nb_eval_examples A__ = tr_loss / nb_tr_steps if args.do_train else None A__ = {'eval_loss': eval_loss, 'eval_accuracy': eval_accuracy, 'train_loss': train_loss} A__ = os.path.join(args.output_dir , 'eval_results.txt' ) with open(__UpperCamelCase , 'w' ) as writer: logger.info('***** Eval results *****' ) for key in sorted(result.keys() ): logger.info(' %s = %s' , __UpperCamelCase , str(result[key] ) ) writer.write('%s = %s\n' % (key, str(result[key] )) ) if __name__ == "__main__": main()
52
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) SCREAMING_SNAKE_CASE__ = { '''configuration_xlm_roberta''': [ '''XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XLMRobertaConfig''', '''XLMRobertaOnnxConfig''', ], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = ['''XLMRobertaTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = ['''XLMRobertaTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XLMRobertaForCausalLM''', '''XLMRobertaForMaskedLM''', '''XLMRobertaForMultipleChoice''', '''XLMRobertaForQuestionAnswering''', '''XLMRobertaForSequenceClassification''', '''XLMRobertaForTokenClassification''', '''XLMRobertaModel''', '''XLMRobertaPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXLMRobertaForCausalLM''', '''TFXLMRobertaForMaskedLM''', '''TFXLMRobertaForMultipleChoice''', '''TFXLMRobertaForQuestionAnswering''', '''TFXLMRobertaForSequenceClassification''', '''TFXLMRobertaForTokenClassification''', '''TFXLMRobertaModel''', '''TFXLMRobertaPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''FlaxXLMRobertaForMaskedLM''', '''FlaxXLMRobertaForCausalLM''', '''FlaxXLMRobertaForMultipleChoice''', '''FlaxXLMRobertaForQuestionAnswering''', '''FlaxXLMRobertaForSequenceClassification''', '''FlaxXLMRobertaForTokenClassification''', '''FlaxXLMRobertaModel''', '''FlaxXLMRobertaPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_xlm_roberta import ( XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMRobertaConfig, XLMRobertaOnnxConfig, ) try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlm_roberta import XLMRobertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlm_roberta_fast import XLMRobertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm_roberta import ( XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, XLMRobertaForCausalLM, XLMRobertaForMaskedLM, XLMRobertaForMultipleChoice, XLMRobertaForQuestionAnswering, XLMRobertaForSequenceClassification, XLMRobertaForTokenClassification, XLMRobertaModel, XLMRobertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlm_roberta import ( TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLMRobertaForCausalLM, TFXLMRobertaForMaskedLM, TFXLMRobertaForMultipleChoice, TFXLMRobertaForQuestionAnswering, TFXLMRobertaForSequenceClassification, TFXLMRobertaForTokenClassification, TFXLMRobertaModel, TFXLMRobertaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xlm_roberta import ( FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxXLMRobertaForCausalLM, FlaxXLMRobertaForMaskedLM, FlaxXLMRobertaForMultipleChoice, FlaxXLMRobertaForQuestionAnswering, FlaxXLMRobertaForSequenceClassification, FlaxXLMRobertaForTokenClassification, FlaxXLMRobertaModel, FlaxXLMRobertaPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
52
1
class __lowerCAmelCase : """simple docstring""" def __init__( self : Optional[Any] ): """simple docstring""" A__ = 0 A__ = 0 A__ = {} def _a ( self : str , _snake_case : int ): """simple docstring""" if vertex not in self.adjacency: A__ = {} self.num_vertices += 1 def _a ( self : int , _snake_case : Optional[Any] , _snake_case : Tuple , _snake_case : Dict ): """simple docstring""" self.add_vertex(_snake_case ) self.add_vertex(_snake_case ) if head == tail: return A__ = weight A__ = weight def _a ( self : Optional[int] ): """simple docstring""" A__ = self.get_edges() for edge in edges: A__ , A__ , A__ = edge edges.remove((tail, head, weight) ) for i in range(len(_snake_case ) ): A__ = list(edges[i] ) edges.sort(key=lambda _snake_case : e[2] ) for i in range(len(_snake_case ) - 1 ): if edges[i][2] >= edges[i + 1][2]: A__ = edges[i][2] + 1 for edge in edges: A__ , A__ , A__ = edge A__ = weight A__ = weight def __str__( self : int ): """simple docstring""" A__ = '' for tail in self.adjacency: for head in self.adjacency[tail]: A__ = self.adjacency[head][tail] string += F'''{head} -> {tail} == {weight}\n''' return string.rstrip('\n' ) def _a ( self : List[str] ): """simple docstring""" A__ = [] for tail in self.adjacency: for head in self.adjacency[tail]: output.append((tail, head, self.adjacency[head][tail]) ) return output def _a ( self : Optional[int] ): """simple docstring""" return self.adjacency.keys() @staticmethod def _a ( _snake_case : Any=None , _snake_case : List[Any]=None ): """simple docstring""" A__ = Graph() if vertices is None: A__ = [] if edges is None: A__ = [] for vertex in vertices: g.add_vertex(_snake_case ) for edge in edges: g.add_edge(*_snake_case ) return g class __lowerCAmelCase : """simple docstring""" def __init__( self : Any ): """simple docstring""" A__ = {} A__ = {} def __len__( self : Union[str, Any] ): """simple docstring""" return len(self.parent ) def _a ( self : Optional[int] , _snake_case : List[Any] ): """simple docstring""" if item in self.parent: return self.find(_snake_case ) A__ = item A__ = 0 return item def _a ( self : List[str] , _snake_case : Tuple ): """simple docstring""" if item not in self.parent: return self.make_set(_snake_case ) if item != self.parent[item]: A__ = self.find(self.parent[item] ) return self.parent[item] def _a ( self : Dict , _snake_case : Dict , _snake_case : List[Any] ): """simple docstring""" A__ = self.find(_snake_case ) A__ = self.find(_snake_case ) if roota == roota: return roota if self.rank[roota] > self.rank[roota]: A__ = roota return roota if self.rank[roota] < self.rank[roota]: A__ = roota return roota if self.rank[roota] == self.rank[roota]: self.rank[roota] += 1 A__ = roota return roota return None @staticmethod def _a ( _snake_case : List[Any] ): """simple docstring""" A__ = graph.num_vertices A__ = Graph.UnionFind() A__ = [] while num_components > 1: A__ = {} for vertex in graph.get_vertices(): A__ = -1 A__ = graph.get_edges() for edge in edges: A__ , A__ , A__ = edge edges.remove((tail, head, weight) ) for edge in edges: A__ , A__ , A__ = edge A__ = union_find.find(_snake_case ) A__ = union_find.find(_snake_case ) if seta != seta: if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: A__ = [head, tail, weight] if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: A__ = [head, tail, weight] for vertex in cheap_edge: if cheap_edge[vertex] != -1: A__ , A__ , A__ = cheap_edge[vertex] if union_find.find(_snake_case ) != union_find.find(_snake_case ): union_find.union(_snake_case , _snake_case ) mst_edges.append(cheap_edge[vertex] ) A__ = num_components - 1 A__ = Graph.build(edges=_snake_case ) return mst
52
import argparse import ast import logging import os import sys import pandas as pd import torch from tqdm import tqdm from transformers import BartForConditionalGeneration, RagRetriever, RagSequenceForGeneration, RagTokenForGeneration from transformers import logging as transformers_logging sys.path.append(os.path.join(os.getcwd())) # noqa: E402 # isort:skip from utils_rag import exact_match_score, fa_score # noqa: E402 # isort:skip SCREAMING_SNAKE_CASE__ = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) transformers_logging.set_verbosity_info() def A ( __UpperCamelCase ) -> Tuple: if "token" in model_name_or_path: return "rag_token" if "sequence" in model_name_or_path: return "rag_sequence" if "bart" in model_name_or_path: return "bart" return None def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[Any]: return max(metric_fn(__UpperCamelCase , __UpperCamelCase ) for gt in ground_truths ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[str]: A__ = [line.strip() for line in open(__UpperCamelCase , 'r' ).readlines()] A__ = [] if args.gold_data_mode == "qa": A__ = pd.read_csv(__UpperCamelCase , sep='\t' , header=__UpperCamelCase ) for answer_list in data[1]: A__ = ast.literal_eval(__UpperCamelCase ) answers.append(__UpperCamelCase ) else: A__ = [line.strip() for line in open(__UpperCamelCase , 'r' ).readlines()] A__ = [[reference] for reference in references] A__ = A__ = A__ = 0 for prediction, ground_truths in zip(__UpperCamelCase , __UpperCamelCase ): total += 1 em += metric_max_over_ground_truths(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) fa += metric_max_over_ground_truths(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) A__ = 100.0 * em / total A__ = 100.0 * fa / total logger.info(f'''F1: {fa:.2f}''' ) logger.info(f'''EM: {em:.2f}''' ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: A__ = args.k A__ = [line.strip() for line in open(__UpperCamelCase , 'r' ).readlines()] A__ = [line.strip() for line in open(__UpperCamelCase , 'r' ).readlines()] A__ = A__ = 0 for hypo, reference in zip(__UpperCamelCase , __UpperCamelCase ): A__ = set(hypo.split('\t' )[:k] ) A__ = set(reference.split('\t' ) ) total += 1 em += len(hypo_provenance & ref_provenance ) / k A__ = 100.0 * em / total logger.info(f'''Precision@{k}: {em: .2f}''' ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Union[str, Any]: def strip_title(__UpperCamelCase ): if title.startswith('"' ): A__ = title[1:] if title.endswith('"' ): A__ = title[:-1] return title A__ = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( __UpperCamelCase , return_tensors='pt' , padding=__UpperCamelCase , truncation=__UpperCamelCase , )['input_ids'].to(args.device ) A__ = rag_model.rag.question_encoder(__UpperCamelCase ) A__ = question_enc_outputs[0] A__ = rag_model.retriever( __UpperCamelCase , question_enc_pool_output.cpu().detach().to(torch.floataa ).numpy() , prefix=rag_model.rag.generator.config.prefix , n_docs=rag_model.config.n_docs , return_tensors='pt' , ) A__ = rag_model.retriever.index.get_doc_dicts(result.doc_ids ) A__ = [] for docs in all_docs: A__ = [strip_title(__UpperCamelCase ) for title in docs['title']] provenance_strings.append('\t'.join(__UpperCamelCase ) ) return provenance_strings def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Optional[Any]: with torch.no_grad(): A__ = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( __UpperCamelCase , return_tensors='pt' , padding=__UpperCamelCase , truncation=__UpperCamelCase ) A__ = inputs_dict.input_ids.to(args.device ) A__ = inputs_dict.attention_mask.to(args.device ) A__ = rag_model.generate( # rag_model overwrites generate __UpperCamelCase , attention_mask=__UpperCamelCase , num_beams=args.num_beams , min_length=args.min_length , max_length=args.max_length , early_stopping=__UpperCamelCase , num_return_sequences=1 , bad_words_ids=[[0, 0]] , ) A__ = rag_model.retriever.generator_tokenizer.batch_decode(__UpperCamelCase , skip_special_tokens=__UpperCamelCase ) if args.print_predictions: for q, a in zip(__UpperCamelCase , __UpperCamelCase ): logger.info('Q: {} - A: {}'.format(__UpperCamelCase , __UpperCamelCase ) ) return answers def A ( ) -> Any: A__ = argparse.ArgumentParser() parser.add_argument( '--model_type' , choices=['rag_sequence', 'rag_token', 'bart'] , type=__UpperCamelCase , help=( 'RAG model type: rag_sequence, rag_token or bart, if none specified, the type is inferred from the' ' model_name_or_path' ) , ) parser.add_argument( '--index_name' , default=__UpperCamelCase , choices=['exact', 'compressed', 'legacy'] , type=__UpperCamelCase , help='RAG model retriever type' , ) parser.add_argument( '--index_path' , default=__UpperCamelCase , type=__UpperCamelCase , help='Path to the retrieval index' , ) parser.add_argument('--n_docs' , default=5 , type=__UpperCamelCase , help='Number of retrieved docs' ) parser.add_argument( '--model_name_or_path' , default=__UpperCamelCase , type=__UpperCamelCase , required=__UpperCamelCase , help='Path to pretrained checkpoints or model identifier from huggingface.co/models' , ) parser.add_argument( '--eval_mode' , choices=['e2e', 'retrieval'] , default='e2e' , type=__UpperCamelCase , help=( 'Evaluation mode, e2e calculates exact match and F1 of the downstream task, retrieval calculates' ' precision@k.' ) , ) parser.add_argument('--k' , default=1 , type=__UpperCamelCase , help='k for the precision@k calculation' ) parser.add_argument( '--evaluation_set' , default=__UpperCamelCase , type=__UpperCamelCase , required=__UpperCamelCase , help='Path to a file containing evaluation samples' , ) parser.add_argument( '--gold_data_path' , default=__UpperCamelCase , type=__UpperCamelCase , required=__UpperCamelCase , help='Path to a tab-separated file with gold samples' , ) parser.add_argument( '--gold_data_mode' , default='qa' , type=__UpperCamelCase , choices=['qa', 'ans'] , help=( 'Format of the gold data file' 'qa - a single line in the following format: question [tab] answer_list' 'ans - a single line of the gold file contains the expected answer string' ) , ) parser.add_argument( '--predictions_path' , type=__UpperCamelCase , default='predictions.txt' , help='Name of the predictions file, to be stored in the checkpoints directory' , ) parser.add_argument( '--eval_all_checkpoints' , action='store_true' , help='Evaluate all checkpoints starting with the same prefix as model_name ending and ending with step number' , ) parser.add_argument( '--eval_batch_size' , default=8 , type=__UpperCamelCase , help='Batch size per GPU/CPU for evaluation.' , ) parser.add_argument( '--recalculate' , help='Recalculate predictions even if the prediction file exists' , action='store_true' , ) parser.add_argument( '--num_beams' , default=4 , type=__UpperCamelCase , help='Number of beams to be used when generating answers' , ) parser.add_argument('--min_length' , default=1 , type=__UpperCamelCase , help='Min length of the generated answers' ) parser.add_argument('--max_length' , default=50 , type=__UpperCamelCase , help='Max length of the generated answers' ) parser.add_argument( '--print_predictions' , action='store_true' , help='If True, prints predictions while evaluating.' , ) parser.add_argument( '--print_docs' , action='store_true' , help='If True, prints docs retried while generating.' , ) A__ = parser.parse_args() A__ = torch.device('cuda' if torch.cuda.is_available() else 'cpu' ) return args def A ( __UpperCamelCase ) -> int: A__ = {} if args.model_type is None: A__ = infer_model_type(args.model_name_or_path ) assert args.model_type is not None if args.model_type.startswith('rag' ): A__ = RagTokenForGeneration if args.model_type == 'rag_token' else RagSequenceForGeneration A__ = args.n_docs if args.index_name is not None: A__ = args.index_name if args.index_path is not None: A__ = args.index_path else: A__ = BartForConditionalGeneration A__ = ( [f.path for f in os.scandir(args.model_name_or_path ) if f.is_dir()] if args.eval_all_checkpoints else [args.model_name_or_path] ) logger.info('Evaluate the following checkpoints: %s' , __UpperCamelCase ) A__ = get_scores if args.eval_mode == 'e2e' else get_precision_at_k A__ = evaluate_batch_eae if args.eval_mode == 'e2e' else evaluate_batch_retrieval for checkpoint in checkpoints: if os.path.exists(args.predictions_path ) and (not args.recalculate): logger.info('Calculating metrics based on an existing predictions file: {}'.format(args.predictions_path ) ) score_fn(__UpperCamelCase , args.predictions_path , args.gold_data_path ) continue logger.info('***** Running evaluation for {} *****'.format(__UpperCamelCase ) ) logger.info(' Batch size = %d' , args.eval_batch_size ) logger.info(' Predictions will be stored under {}'.format(args.predictions_path ) ) if args.model_type.startswith('rag' ): A__ = RagRetriever.from_pretrained(__UpperCamelCase , **__UpperCamelCase ) A__ = model_class.from_pretrained(__UpperCamelCase , retriever=__UpperCamelCase , **__UpperCamelCase ) model.retriever.init_retrieval() else: A__ = model_class.from_pretrained(__UpperCamelCase , **__UpperCamelCase ) model.to(args.device ) with open(args.evaluation_set , 'r' ) as eval_file, open(args.predictions_path , 'w' ) as preds_file: A__ = [] for line in tqdm(__UpperCamelCase ): questions.append(line.strip() ) if len(__UpperCamelCase ) == args.eval_batch_size: A__ = evaluate_batch_fn(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) preds_file.write('\n'.join(__UpperCamelCase ) + '\n' ) preds_file.flush() A__ = [] if len(__UpperCamelCase ) > 0: A__ = evaluate_batch_fn(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) preds_file.write('\n'.join(__UpperCamelCase ) ) preds_file.flush() score_fn(__UpperCamelCase , args.predictions_path , args.gold_data_path ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = get_args() main(args)
52
1
def A ( __UpperCamelCase = 10**12 ) -> int: A__ = 1 A__ = 0 A__ = 1 A__ = 1 while numerator <= 2 * min_total - 1: prev_numerator += 2 * numerator numerator += 2 * prev_numerator prev_denominator += 2 * denominator denominator += 2 * prev_denominator return (denominator + 1) // 2 if __name__ == "__main__": print(f'{solution() = }')
52
import inspect import unittest from transformers import ViTHybridConfig from transformers.testing_utils import require_accelerate, require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class __lowerCAmelCase : """simple docstring""" def __init__( self : List[Any] , _snake_case : Any , _snake_case : Optional[int]=13 , _snake_case : Optional[Any]=64 , _snake_case : List[str]=2 , _snake_case : Any=3 , _snake_case : Union[str, Any]=True , _snake_case : Dict=True , _snake_case : int=32 , _snake_case : int=5 , _snake_case : Union[str, Any]=4 , _snake_case : int=37 , _snake_case : Tuple="gelu" , _snake_case : Optional[int]=0.1 , _snake_case : Dict=0.1 , _snake_case : List[str]=10 , _snake_case : Union[str, Any]=0.02 , _snake_case : Dict=[1, 16, 4, 4] , _snake_case : Dict=None , ): """simple docstring""" A__ = parent A__ = batch_size A__ = image_size A__ = patch_size A__ = num_channels A__ = is_training A__ = use_labels A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = type_sequence_label_size A__ = initializer_range A__ = scope A__ = backbone_featmap_shape # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) # the number of patches is based on the feature map of the backbone, which by default uses an output stride # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size A__ = (self.image_size // 32) ** 2 A__ = num_patches + 1 def _a ( self : Any ): """simple docstring""" A__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A__ = self.get_config() return config, pixel_values, labels def _a ( self : Tuple ): """simple docstring""" A__ = { 'global_padding': 'same', 'layer_type': 'bottleneck', 'depths': [3, 4, 9], 'out_features': ['stage1', 'stage2', 'stage3'], 'embedding_dynamic_padding': True, 'hidden_sizes': [4, 8, 16, 32], 'num_groups': 2, } return ViTHybridConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_snake_case , initializer_range=self.initializer_range , backbone_featmap_shape=self.backbone_featmap_shape , backbone_config=_snake_case , ) def _a ( self : int , _snake_case : Optional[int] , _snake_case : Union[str, Any] , _snake_case : Optional[int] ): """simple docstring""" A__ = ViTHybridModel(config=_snake_case ) model.to(_snake_case ) model.eval() A__ = model(_snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _a ( self : List[str] , _snake_case : str , _snake_case : Union[str, Any] , _snake_case : Any ): """simple docstring""" A__ = self.type_sequence_label_size A__ = ViTHybridForImageClassification(_snake_case ) model.to(_snake_case ) model.eval() A__ = model(_snake_case , labels=_snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _a ( self : Dict ): """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ = config_and_inputs A__ = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class __lowerCAmelCase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Union[str, Any] = (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else () A__ : str = ( {"feature-extraction": ViTHybridModel, "image-classification": ViTHybridForImageClassification} if is_torch_available() else {} ) A__ : Union[str, Any] = False A__ : Any = False A__ : Union[str, Any] = False def _a ( self : Dict ): """simple docstring""" A__ = ViTHybridModelTester(self ) A__ = ConfigTester(self , config_class=_snake_case , has_text_modality=_snake_case , hidden_size=37 ) def _a ( self : int ): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='ViT does not use inputs_embeds' ) def _a ( self : int ): """simple docstring""" pass def _a ( self : int ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(_snake_case ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A__ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_snake_case , nn.Linear ) ) def _a ( self : List[str] ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(_snake_case ) A__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ = [*signature.parameters.keys()] A__ = ['pixel_values'] self.assertListEqual(arg_names[:1] , _snake_case ) def _a ( self : Any ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def _a ( self : str ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_snake_case ) def _a ( self : Any ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = _config_zero_init(_snake_case ) for model_class in self.all_model_classes: A__ = model_class(config=_snake_case ) # Skip the check for the backbone for name, module in model.named_modules(): if module.__class__.__name__ == "ViTHybridPatchEmbeddings": A__ = [F'''{name}.{key}''' for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() , [0.0, 1.0] , msg=F'''Parameter {name} of model {model_class} seems not properly initialized''' , ) @slow def _a ( self : int ): """simple docstring""" for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = ViTHybridModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def A ( ) -> Union[str, Any]: A__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @cached_property def _a ( self : Tuple ): """simple docstring""" return ( ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def _a ( self : Optional[Any] ): """simple docstring""" A__ = ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to( _snake_case ) A__ = self.default_image_processor A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ).to(_snake_case ) # forward pass with torch.no_grad(): A__ = model(**_snake_case ) # verify the logits A__ = torch.Size((1, 10_00) ) self.assertEqual(outputs.logits.shape , _snake_case ) A__ = torch.tensor([-1.9090, -0.4993, -0.2389] ).to(_snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _snake_case , atol=1E-4 ) ) @slow @require_accelerate def _a ( self : List[Any] ): """simple docstring""" A__ = ViTHybridImageProcessor.from_pretrained('google/vit-hybrid-base-bit-384' ) A__ = ViTHybridForImageClassification.from_pretrained('google/vit-hybrid-base-bit-384' , device_map='auto' ) A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ) A__ = model(**_snake_case ) A__ = outputs.logits # model predicts one of the 1000 ImageNet classes A__ = logits.argmax(-1 ).item() self.assertTrue(model.config.idalabel[predicted_class_idx] , 'tabby, tabby cat' )
52
1
import unittest from transformers import BigBirdTokenizer, BigBirdTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin SCREAMING_SNAKE_CASE__ = '''▁''' SCREAMING_SNAKE_CASE__ = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece @require_tokenizers class __lowerCAmelCase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : str = BigBirdTokenizer A__ : Any = BigBirdTokenizerFast A__ : List[str] = True A__ : Dict = True def _a ( self : List[str] ): """simple docstring""" super().setUp() A__ = self.tokenizer_class(_snake_case , keep_accents=_snake_case ) tokenizer.save_pretrained(self.tmpdirname ) def _a ( self : List[Any] ): """simple docstring""" A__ = '<s>' A__ = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_snake_case ) , _snake_case ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_snake_case ) , _snake_case ) def _a ( self : Union[str, Any] ): """simple docstring""" A__ = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '<unk>' ) self.assertEqual(vocab_keys[1] , '<s>' ) self.assertEqual(vocab_keys[-1] , '[MASK]' ) self.assertEqual(len(_snake_case ) , 10_04 ) def _a ( self : Dict ): """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 10_00 ) def _a ( self : Any ): """simple docstring""" if not self.test_rust_tokenizer: return A__ = self.get_tokenizer() A__ = self.get_rust_tokenizer() A__ = 'I was born in 92000, and this is falsé.' A__ = tokenizer.tokenize(_snake_case ) A__ = rust_tokenizer.tokenize(_snake_case ) self.assertListEqual(_snake_case , _snake_case ) A__ = tokenizer.encode(_snake_case , add_special_tokens=_snake_case ) A__ = rust_tokenizer.encode(_snake_case , add_special_tokens=_snake_case ) self.assertListEqual(_snake_case , _snake_case ) A__ = self.get_rust_tokenizer() A__ = tokenizer.encode(_snake_case ) A__ = rust_tokenizer.encode(_snake_case ) self.assertListEqual(_snake_case , _snake_case ) def _a ( self : List[Any] ): """simple docstring""" A__ = BigBirdTokenizer(_snake_case , keep_accents=_snake_case ) A__ = tokenizer.tokenize('This is a test' ) self.assertListEqual(_snake_case , ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_snake_case ) , [2_85, 46, 10, 1_70, 3_82] , ) A__ = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( _snake_case , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '9', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', 'é', '.', ] , ) A__ = tokenizer.convert_tokens_to_ids(_snake_case ) self.assertListEqual( _snake_case , [8, 21, 84, 55, 24, 19, 7, 0, 6_02, 3_47, 3_47, 3_47, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) A__ = tokenizer.convert_ids_to_tokens(_snake_case ) self.assertListEqual( _snake_case , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '<unk>', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', '<unk>', '.', ] , ) @cached_property def _a ( self : Tuple ): """simple docstring""" return BigBirdTokenizer.from_pretrained('google/bigbird-roberta-base' ) @slow def _a ( self : List[Any] ): """simple docstring""" A__ = 'Hello World!' A__ = [65, 1_85_36, 22_60, 1_01, 66] self.assertListEqual(_snake_case , self.big_tokenizer.encode(_snake_case ) ) @slow def _a ( self : Any ): """simple docstring""" A__ = ( 'This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will' ' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth' ) # fmt: off A__ = [65, 8_71, 4_19, 3_58, 9_46, 9_91, 25_21, 4_52, 3_58, 13_57, 3_87, 77_51, 35_36, 1_12, 9_85, 4_56, 1_26, 8_65, 9_38, 54_00, 57_34, 4_58, 13_68, 4_67, 7_86, 24_62, 52_46, 11_59, 6_33, 8_65, 45_19, 4_57, 5_82, 8_52, 25_57, 4_27, 9_16, 5_08, 4_05, 3_43_24, 4_97, 3_91, 4_08, 1_13_42, 12_44, 3_85, 1_00, 9_38, 9_85, 4_56, 5_74, 3_62, 1_25_97, 32_00, 31_29, 11_72, 66] # noqa: E231 # fmt: on self.assertListEqual(_snake_case , self.big_tokenizer.encode(_snake_case ) ) @require_torch @slow def _a ( self : Optional[Any] ): """simple docstring""" import torch from transformers import BigBirdConfig, BigBirdModel # Build sequence A__ = list(self.big_tokenizer.get_vocab().keys() )[:10] A__ = ' '.join(_snake_case ) A__ = self.big_tokenizer.encode_plus(_snake_case , return_tensors='pt' , return_token_type_ids=_snake_case ) A__ = self.big_tokenizer.batch_encode_plus( [sequence + ' ' + sequence] , return_tensors='pt' , return_token_type_ids=_snake_case ) A__ = BigBirdConfig(attention_type='original_full' ) A__ = BigBirdModel(_snake_case ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**_snake_case ) model(**_snake_case ) @slow def _a ( self : List[str] ): """simple docstring""" A__ = BigBirdTokenizer.from_pretrained('google/bigbird-roberta-base' ) A__ = tokenizer.decode(tokenizer('Paris is the [MASK].' ).input_ids ) self.assertTrue(decoded_text == '[CLS] Paris is the[MASK].[SEP]' ) @slow def _a ( self : List[Any] ): """simple docstring""" A__ = {'input_ids': [[65, 3_92_86, 4_58, 3_63_35, 20_01, 4_56, 1_30_73, 1_32_66, 4_55, 1_13, 77_46, 17_41, 1_11_57, 3_91, 1_30_73, 1_32_66, 4_55, 1_13, 39_67, 3_54_12, 1_13, 49_36, 1_09, 38_70, 23_77, 1_13, 3_00_84, 4_57_20, 4_58, 1_34, 1_74_96, 1_12, 5_03, 1_16_72, 1_13, 1_18, 1_12, 56_65, 1_33_47, 3_86_87, 1_12, 14_96, 3_13_89, 1_12, 32_68, 4_72_64, 1_34, 9_62, 1_12, 1_63_77, 80_35, 2_31_30, 4_30, 1_21_69, 1_55_18, 2_85_92, 4_58, 1_46, 4_16_97, 1_09, 3_91, 1_21_69, 1_55_18, 1_66_89, 4_58, 1_46, 4_13_58, 1_09, 4_52, 7_26, 40_34, 1_11, 7_63, 3_54_12, 50_82, 3_88, 19_03, 1_11, 90_51, 3_91, 28_70, 4_89_18, 19_00, 11_23, 5_50, 9_98, 1_12, 95_86, 1_59_85, 4_55, 3_91, 4_10, 2_29_55, 3_76_36, 1_14, 66], [65, 4_48, 1_74_96, 4_19, 36_63, 3_85, 7_63, 1_13, 2_75_33, 28_70, 32_83, 1_30_43, 16_39, 2_47_13, 5_23, 6_56, 2_40_13, 1_85_50, 25_21, 5_17, 2_70_14, 2_12_44, 4_20, 12_12, 14_65, 3_91, 9_27, 48_33, 3_88, 5_78, 1_17_86, 1_14, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [65, 4_84, 21_69, 76_87, 2_19_32, 1_81_46, 7_26, 3_63, 1_70_32, 33_91, 1_14, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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=_snake_case , model_name='google/bigbird-roberta-base' , revision='215c99f1600e06f83acce68422f2035b2b5c3510' , )
52
def A ( __UpperCamelCase ) -> bool: return number & 1 == 0 if __name__ == "__main__": import doctest doctest.testmod()
52
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) SCREAMING_SNAKE_CASE__ = {'''configuration_reformer''': ['''REFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ReformerConfig''']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = ['''ReformerTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = ['''ReformerTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''REFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ReformerAttention''', '''ReformerForMaskedLM''', '''ReformerForQuestionAnswering''', '''ReformerForSequenceClassification''', '''ReformerLayer''', '''ReformerModel''', '''ReformerModelWithLMHead''', '''ReformerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_reformer import REFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, ReformerConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_reformer import ReformerTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_reformer_fast import ReformerTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_reformer import ( REFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ReformerAttention, ReformerForMaskedLM, ReformerForQuestionAnswering, ReformerForSequenceClassification, ReformerLayer, ReformerModel, ReformerModelWithLMHead, ReformerPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
52
from typing import Dict from .base import GenericTensor, Pipeline class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def _a ( self : Any , _snake_case : str=None , _snake_case : Dict=None , _snake_case : Any=None , **_snake_case : str ): """simple docstring""" if tokenize_kwargs is None: A__ = {} if truncation is not None: if "truncation" in tokenize_kwargs: raise ValueError( 'truncation parameter defined twice (given as keyword argument as well as in tokenize_kwargs)' ) A__ = truncation A__ = tokenize_kwargs A__ = {} if return_tensors is not None: A__ = return_tensors return preprocess_params, {}, postprocess_params def _a ( self : Any , _snake_case : Dict , **_snake_case : Optional[Any] ): """simple docstring""" A__ = self.framework A__ = self.tokenizer(_snake_case , return_tensors=_snake_case , **_snake_case ) return model_inputs def _a ( self : List[Any] , _snake_case : Dict ): """simple docstring""" A__ = self.model(**_snake_case ) return model_outputs def _a ( self : Optional[Any] , _snake_case : List[Any] , _snake_case : str=False ): """simple docstring""" if return_tensors: return model_outputs[0] if self.framework == "pt": return model_outputs[0].tolist() elif self.framework == "tf": return model_outputs[0].numpy().tolist() def __call__( self : Dict , *_snake_case : int , **_snake_case : List[str] ): """simple docstring""" return super().__call__(*_snake_case , **_snake_case )
52
1
def A ( __UpperCamelCase ) -> list: # bit count represents no. of bits in the gray code if bit_count < 0: raise ValueError('The given input must be positive' ) # get the generated string sequence A__ = gray_code_sequence_string(__UpperCamelCase ) # # convert them to integers for i in range(len(__UpperCamelCase ) ): A__ = int(sequence[i] , 2 ) return sequence def A ( __UpperCamelCase ) -> list: # The approach is a recursive one # Base case achieved when either n = 0 or n=1 if bit_count == 0: return ["0"] if bit_count == 1: return ["0", "1"] A__ = 1 << bit_count # defines the length of the sequence # 1<< n is equivalent to 2^n # recursive answer will generate answer for n-1 bits A__ = gray_code_sequence_string(bit_count - 1 ) A__ = [] # append 0 to first half of the smaller sequence generated for i in range(seq_len // 2 ): A__ = '0' + smaller_sequence[i] sequence.append(__UpperCamelCase ) # append 1 to second half ... start from the end of the list for i in reversed(range(seq_len // 2 ) ): A__ = '1' + smaller_sequence[i] sequence.append(__UpperCamelCase ) return sequence if __name__ == "__main__": import doctest doctest.testmod()
52
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import numpy as np from utils_multiple_choice import MultipleChoiceDataset, Split, processors import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process SCREAMING_SNAKE_CASE__ = logging.getLogger(__name__) def A ( __UpperCamelCase , __UpperCamelCase ) -> List[Any]: return (preds == labels).mean() @dataclass class __lowerCAmelCase : """simple docstring""" A__ : str = field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "Pretrained config name or path if not the same as model_name"} ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"} ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) @dataclass class __lowerCAmelCase : """simple docstring""" A__ : str = field(metadata={"help": "The name of the task to train on: " + ", ".join(processors.keys() )} ) A__ : str = field(metadata={"help": "Should contain the data files for the task."} ) A__ : int = field( default=1_28 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) A__ : bool = field( default=UpperCAmelCase_ , metadata={"help": "Overwrite the cached training and evaluation sets"} ) def A ( ) -> Any: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. A__ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) A__ , A__ , A__ = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f'''Output directory ({training_args.output_dir}) already exists and is not empty. Use''' ' --overwrite_output_dir to overcome.' ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( 'Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s' , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.local_rank != -1 ) , training_args.fpaa , ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('Training/evaluation parameters %s' , __UpperCamelCase ) # Set seed set_seed(training_args.seed ) try: A__ = processors[data_args.task_name]() A__ = processor.get_labels() A__ = len(__UpperCamelCase ) except KeyError: raise ValueError('Task not found: %s' % (data_args.task_name) ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. A__ = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=__UpperCamelCase , finetuning_task=data_args.task_name , cache_dir=model_args.cache_dir , ) A__ = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) A__ = AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path , from_tf=bool('.ckpt' in model_args.model_name_or_path ) , config=__UpperCamelCase , cache_dir=model_args.cache_dir , ) # Get datasets A__ = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=__UpperCamelCase , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.train , ) if training_args.do_train else None ) A__ = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=__UpperCamelCase , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.dev , ) if training_args.do_eval else None ) def compute_metrics(__UpperCamelCase ) -> Dict: A__ = np.argmax(p.predictions , axis=1 ) return {"acc": simple_accuracy(__UpperCamelCase , p.label_ids )} # Data collator A__ = DataCollatorWithPadding(__UpperCamelCase , pad_to_multiple_of=8 ) if training_args.fpaa else None # Initialize our Trainer A__ = Trainer( model=__UpperCamelCase , args=__UpperCamelCase , train_dataset=__UpperCamelCase , eval_dataset=__UpperCamelCase , compute_metrics=__UpperCamelCase , data_collator=__UpperCamelCase , ) # Training if training_args.do_train: trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir ) # Evaluation A__ = {} if training_args.do_eval: logger.info('*** Evaluate ***' ) A__ = trainer.evaluate() A__ = os.path.join(training_args.output_dir , 'eval_results.txt' ) if trainer.is_world_master(): with open(__UpperCamelCase , 'w' ) as writer: logger.info('***** Eval results *****' ) for key, value in result.items(): logger.info(' %s = %s' , __UpperCamelCase , __UpperCamelCase ) writer.write('%s = %s\n' % (key, value) ) results.update(__UpperCamelCase ) return results def A ( __UpperCamelCase ) -> List[Any]: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
52
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available, ) SCREAMING_SNAKE_CASE__ = { '''configuration_layoutlmv2''': ['''LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''LayoutLMv2Config'''], '''processing_layoutlmv2''': ['''LayoutLMv2Processor'''], '''tokenization_layoutlmv2''': ['''LayoutLMv2Tokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = ['''LayoutLMv2TokenizerFast'''] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = ['''LayoutLMv2FeatureExtractor'''] SCREAMING_SNAKE_CASE__ = ['''LayoutLMv2ImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST''', '''LayoutLMv2ForQuestionAnswering''', '''LayoutLMv2ForSequenceClassification''', '''LayoutLMv2ForTokenClassification''', '''LayoutLMv2Layer''', '''LayoutLMv2Model''', '''LayoutLMv2PreTrainedModel''', ] if TYPE_CHECKING: from .configuration_layoutlmva import LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMvaConfig from .processing_layoutlmva import LayoutLMvaProcessor from .tokenization_layoutlmva import LayoutLMvaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor, LayoutLMvaImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_layoutlmva import ( LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST, LayoutLMvaForQuestionAnswering, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaLayer, LayoutLMvaModel, LayoutLMvaPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
52
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available SCREAMING_SNAKE_CASE__ = {'''configuration_mra''': ['''MRA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MraConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''MRA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MraForMaskedLM''', '''MraForMultipleChoice''', '''MraForQuestionAnswering''', '''MraForSequenceClassification''', '''MraForTokenClassification''', '''MraLayer''', '''MraModel''', '''MraPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_mra import MRA_PRETRAINED_CONFIG_ARCHIVE_MAP, MraConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mra import ( MRA_PRETRAINED_MODEL_ARCHIVE_LIST, MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, MraLayer, MraModel, MraPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
52
1
import os try: from .build_directory_md import good_file_paths except ImportError: from build_directory_md import good_file_paths # type: ignore SCREAMING_SNAKE_CASE__ = list(good_file_paths()) assert filepaths, "good_file_paths() failed!" SCREAMING_SNAKE_CASE__ = [file for file in filepaths if file != file.lower()] if upper_files: print(f'{len(upper_files)} files contain uppercase characters:') print('''\n'''.join(upper_files) + '''\n''') SCREAMING_SNAKE_CASE__ = [file for file in filepaths if ''' ''' in file] if space_files: print(f'{len(space_files)} files contain space characters:') print('''\n'''.join(space_files) + '''\n''') SCREAMING_SNAKE_CASE__ = [file for file in filepaths if '''-''' in file] if hyphen_files: print(f'{len(hyphen_files)} files contain hyphen characters:') print('''\n'''.join(hyphen_files) + '''\n''') SCREAMING_SNAKE_CASE__ = [file for file in filepaths if os.sep not in file] if nodir_files: print(f'{len(nodir_files)} files are not in a directory:') print('''\n'''.join(nodir_files) + '''\n''') SCREAMING_SNAKE_CASE__ = len(upper_files + space_files + hyphen_files + nodir_files) if bad_files: import sys sys.exit(bad_files)
52
import webbrowser from sys import argv from urllib.parse import parse_qs, quote import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = '''%20'''.join(argv[1:]) if len(argv) > 1 else quote(str(input('''Search: '''))) print('''Googling.....''') SCREAMING_SNAKE_CASE__ = f'https://www.google.com/search?q={query}&num=100' SCREAMING_SNAKE_CASE__ = requests.get( url, headers={'''User-Agent''': str(UserAgent().random)}, ) try: SCREAMING_SNAKE_CASE__ = ( BeautifulSoup(res.text, '''html.parser''') .find('''div''', attrs={'''class''': '''yuRUbf'''}) .find('''a''') .get('''href''') ) except AttributeError: SCREAMING_SNAKE_CASE__ = parse_qs( BeautifulSoup(res.text, '''html.parser''') .find('''div''', attrs={'''class''': '''kCrYT'''}) .find('''a''') .get('''href''') )['''url'''][0] webbrowser.open(link)
52
1
from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Union[str, Any] = ["image_processor", "tokenizer"] A__ : Optional[Any] = "BridgeTowerImageProcessor" A__ : List[Any] = ("RobertaTokenizer", "RobertaTokenizerFast") def __init__( self : List[Any] , _snake_case : Optional[Any] , _snake_case : Optional[int] ): """simple docstring""" super().__init__(_snake_case , _snake_case ) def __call__( self : List[Any] , _snake_case : int , _snake_case : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , _snake_case : bool = True , _snake_case : Union[bool, str, PaddingStrategy] = False , _snake_case : Union[bool, str, TruncationStrategy] = None , _snake_case : Optional[int] = None , _snake_case : int = 0 , _snake_case : Optional[int] = None , _snake_case : Optional[bool] = None , _snake_case : Optional[bool] = None , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = True , _snake_case : Optional[Union[str, TensorType]] = None , **_snake_case : Optional[int] , ): """simple docstring""" A__ = self.tokenizer( text=_snake_case , add_special_tokens=_snake_case , padding=_snake_case , truncation=_snake_case , max_length=_snake_case , stride=_snake_case , pad_to_multiple_of=_snake_case , return_token_type_ids=_snake_case , return_attention_mask=_snake_case , return_overflowing_tokens=_snake_case , return_special_tokens_mask=_snake_case , return_offsets_mapping=_snake_case , return_length=_snake_case , verbose=_snake_case , return_tensors=_snake_case , **_snake_case , ) # add pixel_values + pixel_mask A__ = self.image_processor( _snake_case , return_tensors=_snake_case , do_normalize=_snake_case , do_center_crop=_snake_case , **_snake_case ) encoding.update(_snake_case ) return encoding def _a ( self : Any , *_snake_case : Tuple , **_snake_case : List[Any] ): """simple docstring""" return self.tokenizer.batch_decode(*_snake_case , **_snake_case ) def _a ( self : Dict , *_snake_case : Dict , **_snake_case : List[str] ): """simple docstring""" return self.tokenizer.decode(*_snake_case , **_snake_case ) @property def _a ( self : Tuple ): """simple docstring""" A__ = self.tokenizer.model_input_names A__ = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
52
import random import unittest import torch from diffusers import IFInpaintingPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class __lowerCAmelCase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Any = IFInpaintingPipeline A__ : Dict = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"width", "height"} A__ : List[Any] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS A__ : Dict = PipelineTesterMixin.required_optional_params - {"latents"} def _a ( self : Any ): """simple docstring""" return self._get_dummy_components() def _a ( self : Optional[int] , _snake_case : Any , _snake_case : str=0 ): """simple docstring""" if str(_snake_case ).startswith('mps' ): A__ = torch.manual_seed(_snake_case ) else: A__ = torch.Generator(device=_snake_case ).manual_seed(_snake_case ) A__ = floats_tensor((1, 3, 32, 32) , rng=random.Random(_snake_case ) ).to(_snake_case ) A__ = floats_tensor((1, 3, 32, 32) , rng=random.Random(_snake_case ) ).to(_snake_case ) A__ = { 'prompt': 'A painting of a squirrel eating a burger', 'image': image, 'mask_image': mask_image, 'generator': generator, 'num_inference_steps': 2, 'output_type': 'numpy', } return inputs @unittest.skipIf( torch_device != 'cuda' or not is_xformers_available() , reason='XFormers attention is only available with CUDA and `xformers` installed' , ) def _a ( self : Dict ): """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) def _a ( self : int ): """simple docstring""" self._test_save_load_optional_components() @unittest.skipIf(torch_device != 'cuda' , reason='float16 requires CUDA' ) def _a ( self : Optional[int] ): """simple docstring""" super().test_save_load_floataa(expected_max_diff=1E-1 ) def _a ( self : List[str] ): """simple docstring""" self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def _a ( self : Dict ): """simple docstring""" self._test_save_load_local() def _a ( self : Optional[int] ): """simple docstring""" self._test_inference_batch_single_identical( expected_max_diff=1E-2 , )
52
1
import requests SCREAMING_SNAKE_CASE__ = '''https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=''' def A ( __UpperCamelCase ) -> None: # fetching a list of articles in json format A__ = requests.get(_NEWS_API + bbc_news_api_key ).json() # each article in the list is a dict for i, article in enumerate(bbc_news_page['articles'] , 1 ): print(f'''{i}.) {article["title"]}''' ) if __name__ == "__main__": fetch_bbc_news(bbc_news_api_key='''<Your BBC News API key goes here>''')
52
import inspect import jax import jax.lax as lax import jax.numpy as jnp from ..utils import add_start_docstrings from ..utils.logging import get_logger SCREAMING_SNAKE_CASE__ = get_logger(__name__) SCREAMING_SNAKE_CASE__ = r''' Args: input_ids (`jnp.ndarray` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`PreTrainedTokenizer`]. See [`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`] for details. [What are input IDs?](../glossary#input-ids) scores (`jnp.ndarray` of shape `(batch_size, config.vocab_size)`): Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search kwargs (`Dict[str, Any]`, *optional*): Additional logits processor specific kwargs. Return: `jnp.ndarray` of shape `(batch_size, config.vocab_size)`: The processed prediction scores. ''' class __lowerCAmelCase : """simple docstring""" @add_start_docstrings(_snake_case ) def __call__( self : Optional[int] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray ): """simple docstring""" raise NotImplementedError( F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''' ) class __lowerCAmelCase : """simple docstring""" @add_start_docstrings(_snake_case ) def __call__( self : List[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray ): """simple docstring""" raise NotImplementedError( F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''' ) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" @add_start_docstrings(_snake_case ) def __call__( self : Any , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int , **_snake_case : Optional[int] ): """simple docstring""" for processor in self: A__ = inspect.signature(processor.__call__ ).parameters if len(_snake_case ) > 3: if not all(arg in kwargs for arg in list(function_args.keys() )[2:] ): raise ValueError( F'''Make sure that all the required parameters: {list(function_args.keys() )} for ''' F'''{processor.__class__} are passed to the logits processor.''' ) A__ = processor(_snake_case , _snake_case , _snake_case , **_snake_case ) else: A__ = processor(_snake_case , _snake_case , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Any , _snake_case : float ): """simple docstring""" if not isinstance(_snake_case , _snake_case ) or not (temperature > 0): raise ValueError(F'''`temperature` has to be a strictly positive float, but is {temperature}''' ) A__ = temperature def __call__( self : str , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = scores / self.temperature return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Optional[Any] , _snake_case : float , _snake_case : float = -float('Inf' ) , _snake_case : int = 1 ): """simple docstring""" if not isinstance(_snake_case , _snake_case ) or (top_p < 0 or top_p > 1.0): raise ValueError(F'''`top_p` has to be a float > 0 and < 1, but is {top_p}''' ) if not isinstance(_snake_case , _snake_case ) or (min_tokens_to_keep < 1): raise ValueError(F'''`min_tokens_to_keep` has to be a positive integer, but is {min_tokens_to_keep}''' ) A__ = top_p A__ = filter_value A__ = min_tokens_to_keep def __call__( self : str , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ , A__ = lax.top_k(_snake_case , scores.shape[-1] ) A__ = jnp.full_like(_snake_case , self.filter_value ) A__ = jax.nn.softmax(_snake_case , axis=-1 ).cumsum(axis=-1 ) A__ = cumulative_probs < self.top_p # include the token that is higher than top_p as well A__ = jnp.roll(_snake_case , 1 ) score_mask |= score_mask.at[:, 0].set(_snake_case ) # min tokens to keep A__ = score_mask.at[:, : self.min_tokens_to_keep].set(_snake_case ) A__ = jnp.where(_snake_case , _snake_case , _snake_case ) A__ = jax.lax.sort_key_val(_snake_case , _snake_case )[-1] return next_scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Union[str, Any] , _snake_case : int , _snake_case : float = -float('Inf' ) , _snake_case : int = 1 ): """simple docstring""" if not isinstance(_snake_case , _snake_case ) or top_k <= 0: raise ValueError(F'''`top_k` has to be a strictly positive integer, but is {top_k}''' ) A__ = max(_snake_case , _snake_case ) A__ = filter_value def __call__( self : Optional[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ , A__ = scores.shape A__ = jnp.full(batch_size * vocab_size , self.filter_value ) A__ = min(self.top_k , scores.shape[-1] ) # Safety check A__ , A__ = lax.top_k(_snake_case , _snake_case ) A__ = jnp.broadcast_to((jnp.arange(_snake_case ) * vocab_size)[:, None] , (batch_size, topk) ).flatten() A__ = topk_scores.flatten() A__ = topk_indices.flatten() + shift A__ = next_scores_flat.at[topk_indices_flat].set(_snake_case ) A__ = next_scores_flat.reshape(_snake_case , _snake_case ) return next_scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Any , _snake_case : int ): """simple docstring""" A__ = bos_token_id def __call__( self : Optional[int] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = jnp.full(scores.shape , -float('inf' ) ) A__ = 1 - jnp.bool_(cur_len - 1 ) A__ = jnp.where(_snake_case , new_scores.at[:, self.bos_token_id].set(0 ) , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Any , _snake_case : int , _snake_case : int ): """simple docstring""" A__ = max_length A__ = eos_token_id def __call__( self : List[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = jnp.full(scores.shape , -float('inf' ) ) A__ = 1 - jnp.bool_(cur_len - self.max_length + 1 ) A__ = jnp.where(_snake_case , new_scores.at[:, self.eos_token_id].set(0 ) , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Dict , _snake_case : int , _snake_case : int ): """simple docstring""" if not isinstance(_snake_case , _snake_case ) or min_length < 0: raise ValueError(F'''`min_length` has to be a positive integer, but is {min_length}''' ) if not isinstance(_snake_case , _snake_case ) or eos_token_id < 0: raise ValueError(F'''`eos_token_id` has to be a positive integer, but is {eos_token_id}''' ) A__ = min_length A__ = eos_token_id def __call__( self : int , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = 1 - jnp.clip(cur_len - self.min_length , 0 , 1 ) A__ = jnp.where(_snake_case , scores.at[:, self.eos_token_id].set(-float('inf' ) ) , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : int , _snake_case : Tuple , _snake_case : Union[str, Any] ): """simple docstring""" A__ = list(_snake_case ) A__ = begin_index def __call__( self : Union[str, Any] , _snake_case : Optional[Any] , _snake_case : str , _snake_case : int ): """simple docstring""" A__ = 1 - jnp.bool_(cur_len - self.begin_index ) A__ = jnp.where(_snake_case , scores.at[:, self.begin_suppress_tokens].set(-float('inf' ) ) , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : int , _snake_case : list ): """simple docstring""" A__ = list(_snake_case ) def __call__( self : List[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = scores.at[..., self.suppress_tokens].set(-float('inf' ) ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : List[str] , _snake_case : Optional[Any] ): """simple docstring""" A__ = dict(_snake_case ) # Converts the dictionary of format {index: token} containing the tokens to be forced to an array, where the # index of the array corresponds to the index of the token to be forced, for XLA compatibility. # Indexes without forced tokens will have a negative value. A__ = jnp.ones((max(force_token_map.keys() ) + 1) , dtype=jnp.intaa ) * -1 for index, token in force_token_map.items(): if token is not None: A__ = force_token_array.at[index].set(_snake_case ) A__ = jnp.intaa(_snake_case ) def __call__( self : List[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" def _force_token(_snake_case : Dict ): A__ = scores.shape[0] A__ = self.force_token_array[generation_idx] A__ = jnp.ones_like(_snake_case , dtype=scores.dtype ) * -float('inf' ) A__ = jnp.zeros((batch_size, 1) , dtype=scores.dtype ) A__ = lax.dynamic_update_slice(_snake_case , _snake_case , (0, current_token) ) return new_scores A__ = lax.cond( cur_len >= self.force_token_array.shape[0] , lambda: scores , lambda: lax.cond( self.force_token_array[cur_len] >= 0 , lambda: _force_token(_snake_case ) , lambda: scores , ) , ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Union[str, Any] , _snake_case : Union[str, Any] , _snake_case : Dict , _snake_case : List[Any] ): """simple docstring""" A__ = generate_config.eos_token_id A__ = generate_config.no_timestamps_token_id A__ = generate_config.no_timestamps_token_id + 1 A__ = decoder_input_length + 1 if generate_config.is_multilingual: # room for language token and task token self.begin_index += 2 if hasattr(_snake_case , 'max_initial_timestamp_index' ): A__ = generate_config.max_initial_timestamp_index else: A__ = model_config.vocab_size if self.max_initial_timestamp_index is None: A__ = model_config.vocab_size def __call__( self : Tuple , _snake_case : List[Any] , _snake_case : Dict , _snake_case : Dict ): """simple docstring""" A__ = scores.at[:, self.no_timestamps_token_id].set(-float('inf' ) ) def handle_pairs(_snake_case : Dict , _snake_case : str ): A__ = jnp.where((cur_len - self.begin_index) >= 1 , _snake_case , _snake_case ) A__ = jnp.where( input_ids_k[cur_len - 1] >= self.timestamp_begin , True and last_was_timestamp , _snake_case , ) A__ = jnp.where((cur_len - self.begin_index) < 2 , _snake_case , _snake_case ) A__ = jnp.where( input_ids_k[cur_len - 2] >= self.timestamp_begin , _snake_case , _snake_case , ) return jnp.where( _snake_case , jnp.where( penultimate_was_timestamp > 0 , scores_k.at[self.timestamp_begin :].set(-float('inf' ) ) , scores_k.at[: self.eos_token_id].set(-float('inf' ) ) , ) , _snake_case , ) A__ = jax.vmap(_snake_case )(_snake_case , _snake_case ) A__ = jnp.where(cur_len == self.begin_index , _snake_case , _snake_case ) A__ = jnp.where( self.max_initial_timestamp_index is not None , True and apply_max_initial_timestamp , _snake_case , ) A__ = self.timestamp_begin + self.max_initial_timestamp_index A__ = jnp.where( _snake_case , scores.at[:, last_allowed + 1 :].set(-float('inf' ) ) , _snake_case , ) # if sum of probability over timestamps is above any other token, sample timestamp A__ = jax.nn.log_softmax(_snake_case , axis=-1 ) def handle_cumulative_probs(_snake_case : List[Any] , _snake_case : Union[str, Any] ): A__ = jax.nn.logsumexp(logprobs_k[self.timestamp_begin :] , axis=-1 ) A__ = jnp.max(logprobs_k[: self.timestamp_begin] ) return jnp.where( timestamp_logprob > max_text_token_logprob , scores_k.at[: self.timestamp_begin].set(-float('inf' ) ) , _snake_case , ) A__ = jax.vmap(_snake_case )(_snake_case , _snake_case ) return scores
52
1
import itertools import random import unittest import numpy as np from transformers import BatchFeature, SpeechTaFeatureExtractor from transformers.testing_utils import require_torch from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch SCREAMING_SNAKE_CASE__ = random.Random() def A ( __UpperCamelCase , __UpperCamelCase=1.0 , __UpperCamelCase=None , __UpperCamelCase=None ) -> Any: if rng is None: A__ = global_rng A__ = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def __init__( self : Any , _snake_case : Union[str, Any] , _snake_case : Tuple=7 , _snake_case : Tuple=4_00 , _snake_case : Any=20_00 , _snake_case : Dict=1 , _snake_case : int=0.0 , _snake_case : List[str]=1_60_00 , _snake_case : Dict=True , _snake_case : Dict=80 , _snake_case : List[str]=16 , _snake_case : Union[str, Any]=64 , _snake_case : Optional[Any]="hann_window" , _snake_case : Any=80 , _snake_case : Tuple=76_00 , _snake_case : Any=1E-10 , _snake_case : str=True , ): """simple docstring""" A__ = parent A__ = batch_size A__ = min_seq_length A__ = max_seq_length A__ = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) A__ = feature_size A__ = padding_value A__ = sampling_rate A__ = do_normalize A__ = num_mel_bins A__ = hop_length A__ = win_length A__ = win_function A__ = fmin A__ = fmax A__ = mel_floor A__ = return_attention_mask def _a ( self : Union[str, Any] ): """simple docstring""" return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "do_normalize": self.do_normalize, "num_mel_bins": self.num_mel_bins, "hop_length": self.hop_length, "win_length": self.win_length, "win_function": self.win_function, "fmin": self.fmin, "fmax": self.fmax, "mel_floor": self.mel_floor, "return_attention_mask": self.return_attention_mask, } def _a ( self : int , _snake_case : Union[str, Any]=False , _snake_case : Union[str, Any]=False ): """simple docstring""" def _flatten(_snake_case : int ): return list(itertools.chain(*_snake_case ) ) if equal_length: A__ = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size A__ = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: A__ = [np.asarray(_snake_case ) for x in speech_inputs] return speech_inputs def _a ( self : Tuple , _snake_case : Any=False , _snake_case : str=False ): """simple docstring""" if equal_length: A__ = [floats_list((self.max_seq_length, self.num_mel_bins) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size A__ = [ floats_list((x, self.num_mel_bins) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: A__ = [np.asarray(_snake_case ) for x in speech_inputs] return speech_inputs @require_torch class __lowerCAmelCase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Dict = SpeechTaFeatureExtractor def _a ( self : Optional[Any] ): """simple docstring""" A__ = SpeechTaFeatureExtractionTester(self ) def _a ( self : Dict , _snake_case : Any ): """simple docstring""" self.assertTrue(np.all(np.mean(_snake_case , axis=0 ) < 1E-3 ) ) self.assertTrue(np.all(np.abs(np.var(_snake_case , axis=0 ) - 1 ) < 1E-3 ) ) def _a ( self : Optional[int] ): """simple docstring""" A__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 A__ = [floats_list((1, x) )[0] for x in range(8_00 , 14_00 , 2_00 )] A__ = [np.asarray(_snake_case ) for speech_input in speech_inputs] # Test not batched input A__ = feat_extract(speech_inputs[0] , return_tensors='np' ).input_values A__ = feat_extract(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(_snake_case , _snake_case , atol=1E-3 ) ) # Test batched A__ = feat_extract(_snake_case , return_tensors='np' ).input_values A__ = feat_extract(_snake_case , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_snake_case , _snake_case ): self.assertTrue(np.allclose(_snake_case , _snake_case , atol=1E-3 ) ) def _a ( self : Optional[int] ): """simple docstring""" A__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) A__ = [floats_list((1, x) )[0] for x in range(8_00 , 14_00 , 2_00 )] A__ = ['longest', 'max_length', 'do_not_pad'] A__ = [None, 16_00, None] for max_length, padding in zip(_snake_case , _snake_case ): A__ = feat_extract(_snake_case , padding=_snake_case , max_length=_snake_case , return_tensors='np' ) A__ = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:8_00] ) self.assertTrue(input_values[0][8_00:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[1][:10_00] ) self.assertTrue(input_values[0][10_00:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[2][:12_00] ) def _a ( self : List[Any] ): """simple docstring""" A__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) A__ = range(8_00 , 14_00 , 2_00 ) A__ = [floats_list((1, x) )[0] for x in lengths] A__ = ['longest', 'max_length', 'do_not_pad'] A__ = [None, 16_00, None] for max_length, padding in zip(_snake_case , _snake_case ): A__ = feat_extract(_snake_case , max_length=_snake_case , padding=_snake_case ) A__ = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:8_00] ) self._check_zero_mean_unit_variance(input_values[1][:10_00] ) self._check_zero_mean_unit_variance(input_values[2][:12_00] ) def _a ( self : Optional[int] ): """simple docstring""" A__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) A__ = [floats_list((1, x) )[0] for x in range(8_00 , 14_00 , 2_00 )] A__ = feat_extract( _snake_case , truncation=_snake_case , max_length=10_00 , padding='max_length' , return_tensors='np' ) A__ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :8_00] ) self._check_zero_mean_unit_variance(input_values[1] ) self._check_zero_mean_unit_variance(input_values[2] ) def _a ( self : str ): """simple docstring""" A__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) A__ = [floats_list((1, x) )[0] for x in range(8_00 , 14_00 , 2_00 )] A__ = feat_extract( _snake_case , truncation=_snake_case , max_length=10_00 , padding='longest' , return_tensors='np' ) A__ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :8_00] ) self._check_zero_mean_unit_variance(input_values[1, :10_00] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertTrue(input_values.shape == (3, 10_00) ) A__ = [floats_list((1, x) )[0] for x in range(8_00 , 14_00 , 2_00 )] A__ = feat_extract( _snake_case , truncation=_snake_case , max_length=20_00 , padding='longest' , return_tensors='np' ) A__ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :8_00] ) self._check_zero_mean_unit_variance(input_values[1, :10_00] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length > longest -> then pad to longest self.assertTrue(input_values.shape == (3, 12_00) ) def _a ( self : Any ): """simple docstring""" A__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) A__ = np.random.rand(1_00 ).astype(np.floataa ) A__ = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: A__ = feature_extractor.pad([{'input_values': inputs}] , return_tensors='np' ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) A__ = feature_extractor.pad([{'input_values': inputs}] , return_tensors='pt' ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) def _a ( self : Dict ): """simple docstring""" A__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 A__ = [floats_list((1, x) )[0] for x in range(8_00 , 14_00 , 2_00 )] A__ = [np.asarray(_snake_case ) for speech_input in speech_inputs] # Test feature size A__ = feature_extractor(audio_target=_snake_case , padding=_snake_case , return_tensors='np' ).input_values self.assertTrue(input_values.ndim == 3 ) self.assertTrue(input_values.shape[-1] == feature_extractor.num_mel_bins ) # Test not batched input A__ = feature_extractor(speech_inputs[0] , return_tensors='np' ).input_values A__ = feature_extractor(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(_snake_case , _snake_case , atol=1E-3 ) ) # Test batched A__ = feature_extractor(_snake_case , return_tensors='np' ).input_values A__ = feature_extractor(_snake_case , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_snake_case , _snake_case ): self.assertTrue(np.allclose(_snake_case , _snake_case , atol=1E-3 ) ) # Test 2-D numpy arrays are batched. A__ = [floats_list((1, x) )[0] for x in (8_00, 8_00, 8_00)] A__ = np.asarray(_snake_case ) A__ = feature_extractor(_snake_case , return_tensors='np' ).input_values A__ = feature_extractor(_snake_case , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_snake_case , _snake_case ): self.assertTrue(np.allclose(_snake_case , _snake_case , atol=1E-3 ) ) def _a ( self : Optional[int] ): """simple docstring""" A__ = self.feat_extract_tester.prepare_inputs_for_target() A__ = self.feature_extraction_class(**self.feat_extract_dict ) A__ = feat_extract.model_input_names[0] A__ = BatchFeature({input_name: speech_inputs} ) self.assertTrue(all(len(_snake_case ) == len(_snake_case ) for x, y in zip(_snake_case , processed_features[input_name] ) ) ) A__ = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_snake_case ) A__ = BatchFeature({input_name: speech_inputs} , tensor_type='np' ) A__ = processed_features[input_name] if len(batch_features_input.shape ) < 3: A__ = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def _a ( self : Tuple ): """simple docstring""" A__ = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_snake_case ) A__ = self.feature_extraction_class(**self.feat_extract_dict ) A__ = feat_extract.model_input_names[0] A__ = BatchFeature({input_name: speech_inputs} , tensor_type='pt' ) A__ = processed_features[input_name] if len(batch_features_input.shape ) < 3: A__ = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def _a ( self : Any ): """simple docstring""" A__ = self.feature_extraction_class(**self.feat_extract_dict ) A__ = self.feat_extract_tester.prepare_inputs_for_target() A__ = feat_extract.model_input_names[0] A__ = BatchFeature({input_name: speech_inputs} ) A__ = feat_extract.num_mel_bins # hack! A__ = feat_extract.pad(_snake_case , padding='longest' , return_tensors='np' )[input_name] A__ = feat_extract.pad(_snake_case , padding='longest' , return_tensors='pt' )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_pt.numpy().astype(np.floataa ).sum() ) < 1E-2 ) def _a ( self : List[Any] ): """simple docstring""" A__ = self.feat_extract_dict A__ = True A__ = self.feature_extraction_class(**_snake_case ) A__ = self.feat_extract_tester.prepare_inputs_for_target() A__ = [len(_snake_case ) for x in speech_inputs] A__ = feat_extract.model_input_names[0] A__ = BatchFeature({input_name: speech_inputs} ) A__ = feat_extract.num_mel_bins # hack! A__ = feat_extract.pad(_snake_case , padding='longest' , return_tensors='np' ) self.assertIn('attention_mask' , _snake_case ) self.assertListEqual(list(processed.attention_mask.shape ) , list(processed[input_name].shape[:2] ) ) self.assertListEqual(processed.attention_mask.sum(-1 ).tolist() , _snake_case ) def _a ( self : Any ): """simple docstring""" A__ = self.feat_extract_dict A__ = True A__ = self.feature_extraction_class(**_snake_case ) A__ = self.feat_extract_tester.prepare_inputs_for_target() A__ = [len(_snake_case ) for x in speech_inputs] A__ = feat_extract.model_input_names[0] A__ = BatchFeature({input_name: speech_inputs} ) A__ = min(_snake_case ) A__ = feat_extract.num_mel_bins # hack! A__ = feat_extract.pad( _snake_case , padding='max_length' , max_length=_snake_case , truncation=_snake_case , return_tensors='np' ) self.assertIn('attention_mask' , _snake_case ) self.assertListEqual( list(processed_pad.attention_mask.shape ) , [processed_pad[input_name].shape[0], max_length] ) self.assertListEqual( processed_pad.attention_mask[:, :max_length].sum(-1 ).tolist() , [max_length for x in speech_inputs] ) def _a ( self : List[str] , _snake_case : int ): """simple docstring""" from datasets import load_dataset A__ = load_dataset('hf-internal-testing/librispeech_asr_dummy' , 'clean' , split='validation' ) # automatic decoding with librispeech A__ = ds.sort('id' ).select(range(_snake_case ) )[:num_samples]['audio'] return [x["array"] for x in speech_samples] def _a ( self : Dict ): """simple docstring""" A__ = torch.tensor( [2.38_04E-03, 2.07_52E-03, 1.98_36E-03, 2.10_57E-03, 1.61_74E-03, 3.05_18E-04, 9.15_53E-05, 3.35_69E-04, 9.76_56E-04, 1.83_11E-03, 2.01_42E-03, 2.10_57E-03, 1.73_95E-03, 4.57_76E-04, -3.96_73E-04, 4.57_76E-04, 1.00_71E-03, 9.15_53E-05, 4.88_28E-04, 1.15_97E-03, 7.32_42E-04, 9.46_04E-04, 1.80_05E-03, 1.83_11E-03, 8.85_01E-04, 4.27_25E-04, 4.88_28E-04, 7.32_42E-04, 1.09_86E-03, 2.10_57E-03] ) # fmt: on A__ = self._load_datasamples(1 ) A__ = SpeechTaFeatureExtractor() A__ = feature_extractor(_snake_case , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 9_36_80) ) self.assertTrue(torch.allclose(input_values[0, :30] , _snake_case , atol=1E-6 ) ) def _a ( self : str ): """simple docstring""" A__ = torch.tensor( [-2.6870, -3.0104, -3.1356, -3.5352, -3.0044, -3.0353, -3.4719, -3.6777, -3.1520, -2.9435, -2.6553, -2.8795, -2.9944, -2.5921, -3.0279, -3.0386, -3.0864, -3.1291, -3.2353, -2.7444, -2.6831, -2.7287, -3.1761, -3.1571, -3.2726, -3.0582, -3.1007, -3.4533, -3.4695, -3.0998] ) # fmt: on A__ = self._load_datasamples(1 ) A__ = SpeechTaFeatureExtractor() A__ = feature_extractor(audio_target=_snake_case , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 3_66, 80) ) self.assertTrue(torch.allclose(input_values[0, 0, :30] , _snake_case , atol=1E-4 ) )
52
import argparse import struct import unittest class __lowerCAmelCase : """simple docstring""" def __init__( self : List[str] , _snake_case : bytes ): """simple docstring""" A__ = data # Initialize hash values A__ = [ 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19, ] # Initialize round constants A__ = [ 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA, 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967, 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070, 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3, 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2, ] A__ = self.preprocessing(self.data ) self.final_hash() @staticmethod def _a ( _snake_case : bytes ): """simple docstring""" A__ = B'\x80' + (B'\x00' * (63 - (len(_snake_case ) + 8) % 64)) A__ = struct.pack('>Q' , (len(_snake_case ) * 8) ) return data + padding + big_endian_integer def _a ( self : Optional[int] ): """simple docstring""" A__ = [ self.preprocessed_data[x : x + 64] for x in range(0 , len(self.preprocessed_data ) , 64 ) ] for block in self.blocks: # Convert the given block into a list of 4 byte integers A__ = list(struct.unpack('>16L' , _snake_case ) ) # add 48 0-ed integers words += [0] * 48 A__ , A__ , A__ , A__ , A__ , A__ , A__ , A__ = self.hashes for index in range(0 , 64 ): if index > 15: # modify the zero-ed indexes at the end of the array A__ = ( self.ror(words[index - 15] , 7 ) ^ self.ror(words[index - 15] , 18 ) ^ (words[index - 15] >> 3) ) A__ = ( self.ror(words[index - 2] , 17 ) ^ self.ror(words[index - 2] , 19 ) ^ (words[index - 2] >> 10) ) A__ = ( words[index - 16] + sa + words[index - 7] + sa ) % 0x100000000 # Compression A__ = self.ror(_snake_case , 6 ) ^ self.ror(_snake_case , 11 ) ^ self.ror(_snake_case , 25 ) A__ = (e & f) ^ ((~e & 0xFFFFFFFF) & g) A__ = ( h + sa + ch + self.round_constants[index] + words[index] ) % 0x100000000 A__ = self.ror(_snake_case , 2 ) ^ self.ror(_snake_case , 13 ) ^ self.ror(_snake_case , 22 ) A__ = (a & b) ^ (a & c) ^ (b & c) A__ = (sa + maj) % 0x100000000 A__ , A__ , A__ , A__ , A__ , A__ , A__ , A__ = ( g, f, e, ((d + tempa) % 0x100000000), c, b, a, ((tempa + tempa) % 0x100000000), ) A__ = [a, b, c, d, e, f, g, h] # Modify final values A__ = [ ((element + mutated_hash_values[index]) % 0x100000000) for index, element in enumerate(self.hashes ) ] A__ = ''.join([hex(_snake_case )[2:].zfill(8 ) for value in self.hashes] ) def _a ( self : Dict , _snake_case : int , _snake_case : int ): """simple docstring""" return 0xFFFFFFFF & (value << (32 - rotations)) | (value >> rotations) class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def _a ( self : str ): """simple docstring""" import hashlib A__ = bytes('Test String' , 'utf-8' ) self.assertEqual(SHAaaa(_snake_case ).hash , hashlib.shaaaa(_snake_case ).hexdigest() ) def A ( ) -> None: import doctest doctest.testmod() A__ = argparse.ArgumentParser() parser.add_argument( '-s' , '--string' , dest='input_string' , default='Hello World!! Welcome to Cryptography' , help='Hash the string' , ) parser.add_argument( '-f' , '--file' , dest='input_file' , help='Hash contents of a file' ) A__ = parser.parse_args() A__ = args.input_string # hash input should be a bytestring if args.input_file: with open(args.input_file , 'rb' ) as f: A__ = f.read() else: A__ = bytes(__UpperCamelCase , 'utf-8' ) print(SHAaaa(__UpperCamelCase ).hash ) if __name__ == "__main__": main()
52
1
from ...processing_utils import ProcessorMixin class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Tuple = "SpeechT5FeatureExtractor" A__ : int = "SpeechT5Tokenizer" def __init__( self : Optional[int] , _snake_case : int , _snake_case : Optional[Any] ): """simple docstring""" super().__init__(_snake_case , _snake_case ) def __call__( self : Tuple , *_snake_case : int , **_snake_case : int ): """simple docstring""" A__ = kwargs.pop('audio' , _snake_case ) A__ = kwargs.pop('text' , _snake_case ) A__ = kwargs.pop('text_target' , _snake_case ) A__ = kwargs.pop('audio_target' , _snake_case ) A__ = kwargs.pop('sampling_rate' , _snake_case ) if audio is not None and text is not None: raise ValueError( 'Cannot process both `audio` and `text` inputs. Did you mean `audio_target` or `text_target`?' ) if audio_target is not None and text_target is not None: raise ValueError( 'Cannot process both `audio_target` and `text_target` inputs. Did you mean `audio` or `text`?' ) if audio is None and audio_target is None and text is None and text_target is None: raise ValueError( 'You need to specify either an `audio`, `audio_target`, `text`, or `text_target` input to process.' ) if audio is not None: A__ = self.feature_extractor(_snake_case , *_snake_case , sampling_rate=_snake_case , **_snake_case ) elif text is not None: A__ = self.tokenizer(_snake_case , **_snake_case ) else: A__ = None if audio_target is not None: A__ = self.feature_extractor(audio_target=_snake_case , *_snake_case , sampling_rate=_snake_case , **_snake_case ) A__ = targets['input_values'] elif text_target is not None: A__ = self.tokenizer(_snake_case , **_snake_case ) A__ = targets['input_ids'] else: A__ = None if inputs is None: return targets if targets is not None: A__ = labels A__ = targets.get('attention_mask' ) if decoder_attention_mask is not None: A__ = decoder_attention_mask return inputs def _a ( self : str , *_snake_case : Union[str, Any] , **_snake_case : int ): """simple docstring""" A__ = kwargs.pop('input_values' , _snake_case ) A__ = kwargs.pop('input_ids' , _snake_case ) A__ = kwargs.pop('labels' , _snake_case ) if input_values is not None and input_ids is not None: raise ValueError('Cannot process both `input_values` and `input_ids` inputs.' ) if input_values is None and input_ids is None and labels is None: raise ValueError( 'You need to specify either an `input_values`, `input_ids`, or `labels` input to be padded.' ) if input_values is not None: A__ = self.feature_extractor.pad(_snake_case , *_snake_case , **_snake_case ) elif input_ids is not None: A__ = self.tokenizer.pad(_snake_case , **_snake_case ) else: A__ = None if labels is not None: if "input_ids" in labels or (isinstance(_snake_case , _snake_case ) and "input_ids" in labels[0]): A__ = self.tokenizer.pad(_snake_case , **_snake_case ) A__ = targets['input_ids'] else: A__ = self.feature_extractor.feature_size A__ = self.feature_extractor.num_mel_bins A__ = self.feature_extractor.pad(_snake_case , *_snake_case , **_snake_case ) A__ = feature_size_hack A__ = targets['input_values'] else: A__ = None if inputs is None: return targets if targets is not None: A__ = labels A__ = targets.get('attention_mask' ) if decoder_attention_mask is not None: A__ = decoder_attention_mask return inputs def _a ( self : Optional[Any] , *_snake_case : Dict , **_snake_case : str ): """simple docstring""" return self.tokenizer.batch_decode(*_snake_case , **_snake_case ) def _a ( self : int , *_snake_case : Any , **_snake_case : str ): """simple docstring""" return self.tokenizer.decode(*_snake_case , **_snake_case )
52
import math import random def A ( __UpperCamelCase , __UpperCamelCase = False ) -> float: if deriv: return value * (1 - value) return 1 / (1 + math.exp(-value )) # Initial Value SCREAMING_SNAKE_CASE__ = 0.02 def A ( __UpperCamelCase , __UpperCamelCase ) -> float: A__ = float(2 * (random.randint(1 , 100 )) - 1 ) for _ in range(__UpperCamelCase ): # Forward propagation A__ = sigmoid_function(INITIAL_VALUE * weight ) # How much did we miss? A__ = (expected / 100) - layer_a # Error delta A__ = layer_1_error * sigmoid_function(__UpperCamelCase , __UpperCamelCase ) # Update weight weight += INITIAL_VALUE * layer_1_delta return layer_a * 100 if __name__ == "__main__": import doctest doctest.testmod() SCREAMING_SNAKE_CASE__ = int(input('''Expected value: ''')) SCREAMING_SNAKE_CASE__ = int(input('''Number of propagations: ''')) print(forward_propagation(expected, number_propagations))
52
1
import unittest from huggingface_hub import hf_hub_download from transformers import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VideoMAEFeatureExtractor from transformers.pipelines import VideoClassificationPipeline, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_decord, require_tf, require_torch, require_torch_or_tf, require_vision, ) from .test_pipelines_common import ANY @is_pipeline_test @require_torch_or_tf @require_vision @require_decord class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" A__ : Optional[int] = MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING def _a ( self : List[Any] , _snake_case : List[str] , _snake_case : Tuple , _snake_case : Union[str, Any] ): """simple docstring""" A__ = hf_hub_download( repo_id='nateraw/video-demo' , filename='archery.mp4' , repo_type='dataset' ) A__ = VideoClassificationPipeline(model=_snake_case , image_processor=_snake_case , top_k=2 ) A__ = [ example_video_filepath, 'https://huggingface.co/datasets/nateraw/video-demo/resolve/main/archery.mp4', ] return video_classifier, examples def _a ( self : List[str] , _snake_case : Optional[Any] , _snake_case : Dict ): """simple docstring""" for example in examples: A__ = video_classifier(_snake_case ) self.assertEqual( _snake_case , [ {'score': ANY(_snake_case ), 'label': ANY(_snake_case )}, {'score': ANY(_snake_case ), 'label': ANY(_snake_case )}, ] , ) @require_torch def _a ( self : Dict ): """simple docstring""" A__ = 'hf-internal-testing/tiny-random-VideoMAEForVideoClassification' A__ = VideoMAEFeatureExtractor( size={'shortest_edge': 10} , crop_size={'height': 10, 'width': 10} ) A__ = pipeline( 'video-classification' , model=_snake_case , feature_extractor=_snake_case , frame_sampling_rate=4 ) A__ = hf_hub_download(repo_id='nateraw/video-demo' , filename='archery.mp4' , repo_type='dataset' ) A__ = video_classifier(_snake_case , top_k=2 ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [{'score': 0.5199, 'label': 'LABEL_0'}, {'score': 0.4801, 'label': 'LABEL_1'}] , ) A__ = video_classifier( [ video_file_path, video_file_path, ] , top_k=2 , ) self.assertEqual( nested_simplify(_snake_case , decimals=4 ) , [ [{'score': 0.5199, 'label': 'LABEL_0'}, {'score': 0.4801, 'label': 'LABEL_1'}], [{'score': 0.5199, 'label': 'LABEL_0'}, {'score': 0.4801, 'label': 'LABEL_1'}], ] , ) @require_tf def _a ( self : List[Any] ): """simple docstring""" pass
52
import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @slow def _a ( self : int ): """simple docstring""" A__ = FlaxMTaForConditionalGeneration.from_pretrained('google/mt5-small' ) A__ = AutoTokenizer.from_pretrained('google/mt5-small' ) A__ = tokenizer('Hello there' , return_tensors='np' ).input_ids A__ = tokenizer('Hi I am' , return_tensors='np' ).input_ids A__ = shift_tokens_right(_snake_case , model.config.pad_token_id , model.config.decoder_start_token_id ) A__ = model(_snake_case , decoder_input_ids=_snake_case ).logits A__ = optax.softmax_cross_entropy(_snake_case , onehot(_snake_case , logits.shape[-1] ) ).mean() A__ = -(labels.shape[-1] * loss.item()) A__ = -84.9127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1E-4 )
52
1
import pytest import datasets # Import fixture modules as plugins SCREAMING_SNAKE_CASE__ = ['''tests.fixtures.files''', '''tests.fixtures.hub''', '''tests.fixtures.fsspec'''] def A ( __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: # Mark tests as "unit" by default if not marked as "integration" (or already marked as "unit") for item in items: if any(marker in item.keywords for marker in ['integration', 'unit'] ): continue item.add_marker(pytest.mark.unit ) def A ( __UpperCamelCase ) -> str: config.addinivalue_line('markers' , 'torchaudio_latest: mark test to run with torchaudio>=0.12' ) @pytest.fixture(autouse=__UpperCamelCase ) def A ( __UpperCamelCase , __UpperCamelCase ) -> Union[str, Any]: # test_hf_cache_home = tmp_path_factory.mktemp("cache") # TODO: why a cache dir per test function does not work? A__ = tmp_path_factory.getbasetemp() / 'cache' A__ = test_hf_cache_home / 'datasets' A__ = test_hf_cache_home / 'metrics' A__ = test_hf_cache_home / 'modules' monkeypatch.setattr('datasets.config.HF_DATASETS_CACHE' , str(__UpperCamelCase ) ) monkeypatch.setattr('datasets.config.HF_METRICS_CACHE' , str(__UpperCamelCase ) ) monkeypatch.setattr('datasets.config.HF_MODULES_CACHE' , str(__UpperCamelCase ) ) A__ = test_hf_datasets_cache / 'downloads' monkeypatch.setattr('datasets.config.DOWNLOADED_DATASETS_PATH' , str(__UpperCamelCase ) ) A__ = test_hf_datasets_cache / 'downloads' / 'extracted' monkeypatch.setattr('datasets.config.EXTRACTED_DATASETS_PATH' , str(__UpperCamelCase ) ) @pytest.fixture(autouse=__UpperCamelCase , scope='session' ) def A ( ) -> Union[str, Any]: datasets.disable_progress_bar() @pytest.fixture(autouse=__UpperCamelCase ) def A ( __UpperCamelCase ) -> int: # don't take tests into account when counting downloads monkeypatch.setattr('datasets.config.HF_UPDATE_DOWNLOAD_COUNTS' , __UpperCamelCase ) @pytest.fixture def A ( __UpperCamelCase ) -> Any: # Required to suppress RemovedIn20Warning when feature(s) are not compatible with SQLAlchemy 2.0 # To be removed once SQLAlchemy 2.0 supported monkeypatch.setattr('sqlalchemy.util.deprecations.SILENCE_UBER_WARNING' , __UpperCamelCase )
52
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/config.json''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/config.json''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/config.json''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/config.json''', '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/config.json''', '''roberta-large-openai-detector''': '''https://huggingface.co/roberta-large-openai-detector/resolve/main/config.json''', } class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : List[str] = "roberta" def __init__( self : List[str] , _snake_case : Union[str, Any]=5_02_65 , _snake_case : List[Any]=7_68 , _snake_case : List[str]=12 , _snake_case : List[str]=12 , _snake_case : Any=30_72 , _snake_case : Union[str, Any]="gelu" , _snake_case : int=0.1 , _snake_case : Union[str, Any]=0.1 , _snake_case : Tuple=5_12 , _snake_case : Union[str, Any]=2 , _snake_case : Any=0.02 , _snake_case : Any=1E-12 , _snake_case : List[Any]=1 , _snake_case : int=0 , _snake_case : Any=2 , _snake_case : Optional[Any]="absolute" , _snake_case : int=True , _snake_case : Any=None , **_snake_case : Any , ): """simple docstring""" super().__init__(pad_token_id=_snake_case , bos_token_id=_snake_case , eos_token_id=_snake_case , **_snake_case ) 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__ = use_cache A__ = classifier_dropout class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" @property def _a ( self : Dict ): """simple docstring""" if self.task == "multiple-choice": A__ = {0: 'batch', 1: 'choice', 2: 'sequence'} else: A__ = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ] )
52
1
import os import unittest from transformers import BatchEncoding from transformers.models.bert.tokenization_bert import ( BasicTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.models.prophetnet.tokenization_prophetnet import VOCAB_FILES_NAMES, ProphetNetTokenizer from transformers.testing_utils import require_torch, slow from ...test_tokenization_common import TokenizerTesterMixin class __lowerCAmelCase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : str = ProphetNetTokenizer A__ : List[str] = False def _a ( self : Dict ): """simple docstring""" super().setUp() A__ = [ '[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) def _a ( self : Any , _snake_case : Union[str, Any] ): """simple docstring""" A__ = 'UNwant\u00E9d,running' A__ = 'unwanted, running' return input_text, output_text def _a ( self : List[Any] ): """simple docstring""" A__ = self.tokenizer_class(self.vocab_file ) A__ = tokenizer.tokenize('UNwant\u00E9d,running' ) self.assertListEqual(_snake_case , ['un', '##want', '##ed', ',', 'runn', '##ing'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_snake_case ) , [9, 6, 7, 12, 10, 11] ) def _a ( self : List[str] ): """simple docstring""" A__ = BasicTokenizer() self.assertListEqual(tokenizer.tokenize('ah\u535A\u63A8zz' ) , ['ah', '\u535A', '\u63A8', 'zz'] ) def _a ( self : Dict ): """simple docstring""" A__ = BasicTokenizer(do_lower_case=_snake_case ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? ' ) , ['hello', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) , ['hello'] ) def _a ( self : List[Any] ): """simple docstring""" A__ = BasicTokenizer(do_lower_case=_snake_case , strip_accents=_snake_case ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['hällo', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) , ['h\u00E9llo'] ) def _a ( self : Any ): """simple docstring""" A__ = BasicTokenizer(do_lower_case=_snake_case , strip_accents=_snake_case ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['hallo', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) , ['hello'] ) def _a ( self : Tuple ): """simple docstring""" A__ = BasicTokenizer(do_lower_case=_snake_case ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['hallo', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) , ['hello'] ) def _a ( self : Any ): """simple docstring""" A__ = BasicTokenizer(do_lower_case=_snake_case ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? ' ) , ['HeLLo', '!', 'how', 'Are', 'yoU', '?'] ) def _a ( self : str ): """simple docstring""" A__ = BasicTokenizer(do_lower_case=_snake_case , strip_accents=_snake_case ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['HäLLo', '!', 'how', 'Are', 'yoU', '?'] ) def _a ( self : Tuple ): """simple docstring""" A__ = BasicTokenizer(do_lower_case=_snake_case , strip_accents=_snake_case ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['HaLLo', '!', 'how', 'Are', 'yoU', '?'] ) def _a ( self : Any ): """simple docstring""" A__ = BasicTokenizer(do_lower_case=_snake_case , never_split=['[UNK]'] ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? [UNK]' ) , ['HeLLo', '!', 'how', 'Are', 'yoU', '?', '[UNK]'] ) def _a ( self : Optional[int] ): """simple docstring""" A__ = ['[UNK]', '[CLS]', '[SEP]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing'] A__ = {} for i, token in enumerate(_snake_case ): A__ = i A__ = WordpieceTokenizer(vocab=_snake_case , unk_token='[UNK]' ) self.assertListEqual(tokenizer.tokenize('' ) , [] ) self.assertListEqual(tokenizer.tokenize('unwanted running' ) , ['un', '##want', '##ed', 'runn', '##ing'] ) self.assertListEqual(tokenizer.tokenize('unwantedX running' ) , ['[UNK]', 'runn', '##ing'] ) @require_torch def _a ( self : List[str] ): """simple docstring""" A__ = self.tokenizer_class.from_pretrained('microsoft/prophetnet-large-uncased' ) A__ = ['A long paragraph for summarization.', 'Another paragraph for summarization.'] A__ = [10_37, 21_46, 2_04_23, 20_05, 76_80, 78_49, 39_89, 10_12, 1_02] A__ = tokenizer(_snake_case , padding=_snake_case , return_tensors='pt' ) self.assertIsInstance(_snake_case , _snake_case ) A__ = list(batch.input_ids.numpy()[0] ) self.assertListEqual(_snake_case , _snake_case ) self.assertEqual((2, 9) , batch.input_ids.shape ) self.assertEqual((2, 9) , batch.attention_mask.shape ) def _a ( self : Union[str, Any] ): """simple docstring""" self.assertTrue(_is_whitespace(' ' ) ) self.assertTrue(_is_whitespace('\t' ) ) self.assertTrue(_is_whitespace('\r' ) ) self.assertTrue(_is_whitespace('\n' ) ) self.assertTrue(_is_whitespace('\u00A0' ) ) self.assertFalse(_is_whitespace('A' ) ) self.assertFalse(_is_whitespace('-' ) ) def _a ( self : str ): """simple docstring""" self.assertTrue(_is_control('\u0005' ) ) self.assertFalse(_is_control('A' ) ) self.assertFalse(_is_control(' ' ) ) self.assertFalse(_is_control('\t' ) ) self.assertFalse(_is_control('\r' ) ) def _a ( self : List[str] ): """simple docstring""" self.assertTrue(_is_punctuation('-' ) ) self.assertTrue(_is_punctuation('$' ) ) self.assertTrue(_is_punctuation('`' ) ) self.assertTrue(_is_punctuation('.' ) ) self.assertFalse(_is_punctuation('A' ) ) self.assertFalse(_is_punctuation(' ' ) ) @slow def _a ( self : Any ): """simple docstring""" A__ = self.tokenizer_class.from_pretrained('microsoft/prophetnet-large-uncased' ) A__ = tokenizer.encode('sequence builders' , add_special_tokens=_snake_case ) A__ = tokenizer.encode('multi-sequence build' , add_special_tokens=_snake_case ) A__ = tokenizer.build_inputs_with_special_tokens(_snake_case ) A__ = tokenizer.build_inputs_with_special_tokens(_snake_case , _snake_case ) assert encoded_sentence == text + [1_02] assert encoded_pair == text + [1_02] + text_a + [1_02]
52
import itertools import json import os import unittest from transformers import AddedToken, LongformerTokenizer, LongformerTokenizerFast from transformers.models.longformer.tokenization_longformer import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __lowerCAmelCase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : int = LongformerTokenizer A__ : Optional[int] = True A__ : Any = LongformerTokenizerFast A__ : Dict = True def _a ( self : int ): """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt A__ = [ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', '\u0120', '\u0120l', '\u0120n', '\u0120lo', '\u0120low', 'er', '\u0120lowest', '\u0120newer', '\u0120wider', '<unk>', ] A__ = dict(zip(_snake_case , range(len(_snake_case ) ) ) ) A__ = ['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', ''] A__ = {'unk_token': '<unk>'} A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(_snake_case ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(_snake_case ) ) def _a ( self : int , **_snake_case : Union[str, Any] ): """simple docstring""" kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **_snake_case ) def _a ( self : Optional[int] , **_snake_case : List[Any] ): """simple docstring""" kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **_snake_case ) def _a ( self : Any , _snake_case : Optional[Any] ): """simple docstring""" A__ = 'lower newer' A__ = 'lower newer' return input_text, output_text def _a ( self : Any ): """simple docstring""" A__ = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) A__ = 'lower newer' A__ = ['l', 'o', 'w', 'er', '\u0120', 'n', 'e', 'w', 'er'] A__ = tokenizer.tokenize(_snake_case ) # , add_prefix_space=True) self.assertListEqual(_snake_case , _snake_case ) A__ = tokens + [tokenizer.unk_token] A__ = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(_snake_case ) , _snake_case ) def _a ( self : List[str] ): """simple docstring""" A__ = self.get_tokenizer() self.assertListEqual(tokenizer.encode('Hello world!' , add_special_tokens=_snake_case ) , [0, 3_14_14, 2_32, 3_28, 2] ) self.assertListEqual( tokenizer.encode('Hello world! cécé herlolip 418' , add_special_tokens=_snake_case ) , [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2] , ) @slow def _a ( self : List[Any] ): """simple docstring""" A__ = self.tokenizer_class.from_pretrained('allenai/longformer-base-4096' ) A__ = tokenizer.encode('sequence builders' , add_special_tokens=_snake_case ) A__ = tokenizer.encode('multi-sequence build' , add_special_tokens=_snake_case ) A__ = tokenizer.encode( 'sequence builders' , add_special_tokens=_snake_case , add_prefix_space=_snake_case ) A__ = tokenizer.encode( 'sequence builders' , 'multi-sequence build' , add_special_tokens=_snake_case , add_prefix_space=_snake_case ) A__ = tokenizer.build_inputs_with_special_tokens(_snake_case ) A__ = tokenizer.build_inputs_with_special_tokens(_snake_case , _snake_case ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def _a ( self : List[str] ): """simple docstring""" A__ = self.get_tokenizer() A__ = 'Encode this sequence.' A__ = tokenizer.byte_encoder[' '.encode('utf-8' )[0]] # Testing encoder arguments A__ = tokenizer.encode(_snake_case , add_special_tokens=_snake_case , add_prefix_space=_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(_snake_case , _snake_case ) A__ = tokenizer.encode(_snake_case , add_special_tokens=_snake_case , add_prefix_space=_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(_snake_case , _snake_case ) tokenizer.add_special_tokens({'bos_token': '<s>'} ) A__ = tokenizer.encode(_snake_case , add_special_tokens=_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(_snake_case , _snake_case ) # Testing spaces after special tokens A__ = '<mask>' tokenizer.add_special_tokens( {'mask_token': AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case )} ) # mask token has a left space A__ = tokenizer.convert_tokens_to_ids(_snake_case ) A__ = 'Encode <mask> sequence' A__ = 'Encode <mask>sequence' A__ = tokenizer.encode(_snake_case ) A__ = encoded.index(_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(_snake_case , _snake_case ) A__ = tokenizer.encode(_snake_case ) A__ = encoded.index(_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(_snake_case , _snake_case ) def _a ( self : Dict ): """simple docstring""" pass def _a ( self : Union[str, Any] ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): A__ = self.rust_tokenizer_class.from_pretrained(_snake_case , **_snake_case ) A__ = self.tokenizer_class.from_pretrained(_snake_case , **_snake_case ) A__ = 'A, <mask> AllenNLP sentence.' A__ = tokenizer_r.encode_plus(_snake_case , add_special_tokens=_snake_case , return_token_type_ids=_snake_case ) A__ = tokenizer_p.encode_plus(_snake_case , add_special_tokens=_snake_case , return_token_type_ids=_snake_case ) # token_type_ids should put 0 everywhere self.assertEqual(sum(tokens_r['token_type_ids'] ) , sum(tokens_p['token_type_ids'] ) ) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEqual( sum(tokens_r['attention_mask'] ) / len(tokens_r['attention_mask'] ) , sum(tokens_p['attention_mask'] ) / len(tokens_p['attention_mask'] ) , ) A__ = tokenizer_r.convert_ids_to_tokens(tokens_r['input_ids'] ) A__ = tokenizer_p.convert_ids_to_tokens(tokens_p['input_ids'] ) # Rust correctly handles the space before the mask while python doesnt self.assertSequenceEqual(tokens_p['input_ids'] , [0, 2_50, 6, 5_02_64, 38_23, 4_87, 2_19_92, 36_45, 4, 2] ) self.assertSequenceEqual(tokens_r['input_ids'] , [0, 2_50, 6, 5_02_64, 38_23, 4_87, 2_19_92, 36_45, 4, 2] ) self.assertSequenceEqual( _snake_case , ['<s>', 'A', ',', '<mask>', 'ĠAllen', 'N', 'LP', 'Ġsentence', '.', '</s>'] ) self.assertSequenceEqual( _snake_case , ['<s>', 'A', ',', '<mask>', 'ĠAllen', 'N', 'LP', 'Ġsentence', '.', '</s>'] ) def _a ( self : List[Any] ): """simple docstring""" for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): A__ = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) A__ = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state['add_prefix_space'] , _snake_case ) self.assertEqual(post_processor_state['add_prefix_space'] , _snake_case ) self.assertEqual(post_processor_state['trim_offsets'] , _snake_case ) def _a ( self : Optional[Any] ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): A__ = 'hello' # `hello` is a token in the vocabulary of `pretrained_name` A__ = F'''{text_of_1_token} {text_of_1_token}''' A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_snake_case ) + 1, len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_snake_case ) + 1, len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_snake_case ), len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_snake_case ), len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = F''' {text}''' # tokenizer_r = self.rust_tokenizer_class.from_pretrained( # pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True # ) # encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False) # self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token))) # self.assertEqual( # encoding.offset_mapping[1], # (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)), # ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_snake_case ) + 1, 1 + len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_snake_case ), 1 + len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_snake_case ), 1 + len(_snake_case ) + 1 + len(_snake_case )) , )
52
1
import unittest import numpy as np from transformers import RobertaConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_flax_available(): from transformers.models.roberta.modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, ) class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def __init__( self : List[Any] , _snake_case : int , _snake_case : List[Any]=13 , _snake_case : List[str]=7 , _snake_case : Any=True , _snake_case : str=True , _snake_case : str=True , _snake_case : int=True , _snake_case : int=99 , _snake_case : Union[str, Any]=32 , _snake_case : Optional[int]=5 , _snake_case : int=4 , _snake_case : List[str]=37 , _snake_case : int="gelu" , _snake_case : Union[str, Any]=0.1 , _snake_case : Optional[int]=0.1 , _snake_case : Optional[int]=5_12 , _snake_case : Optional[int]=16 , _snake_case : Tuple=2 , _snake_case : Tuple=0.02 , _snake_case : Tuple=4 , ): """simple docstring""" A__ = parent A__ = batch_size A__ = seq_length A__ = is_training A__ = use_attention_mask A__ = use_token_type_ids A__ = use_labels A__ = vocab_size 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__ = max_position_embeddings A__ = type_vocab_size A__ = type_sequence_label_size A__ = initializer_range A__ = num_choices def _a ( self : Tuple ): """simple docstring""" A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = None if self.use_attention_mask: A__ = random_attention_mask([self.batch_size, self.seq_length] ) A__ = None if self.use_token_type_ids: A__ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A__ = RobertaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=_snake_case , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def _a ( self : List[str] ): """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ , A__ = config_and_inputs A__ = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': attention_mask} return config, inputs_dict def _a ( self : int ): """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ , A__ = config_and_inputs A__ = True A__ = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) A__ = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, encoder_hidden_states, encoder_attention_mask, ) @require_flax class __lowerCAmelCase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Dict = True A__ : Optional[int] = ( ( FlaxRobertaModel, FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, ) if is_flax_available() else () ) def _a ( self : Dict ): """simple docstring""" A__ = FlaxRobertaModelTester(self ) @slow def _a ( self : List[Any] ): """simple docstring""" for model_class_name in self.all_model_classes: A__ = model_class_name.from_pretrained('roberta-base' , from_pt=_snake_case ) A__ = model(np.ones((1, 1) ) ) self.assertIsNotNone(_snake_case )
52
import pytest import datasets # Import fixture modules as plugins SCREAMING_SNAKE_CASE__ = ['''tests.fixtures.files''', '''tests.fixtures.hub''', '''tests.fixtures.fsspec'''] def A ( __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: # Mark tests as "unit" by default if not marked as "integration" (or already marked as "unit") for item in items: if any(marker in item.keywords for marker in ['integration', 'unit'] ): continue item.add_marker(pytest.mark.unit ) def A ( __UpperCamelCase ) -> str: config.addinivalue_line('markers' , 'torchaudio_latest: mark test to run with torchaudio>=0.12' ) @pytest.fixture(autouse=__UpperCamelCase ) def A ( __UpperCamelCase , __UpperCamelCase ) -> Union[str, Any]: # test_hf_cache_home = tmp_path_factory.mktemp("cache") # TODO: why a cache dir per test function does not work? A__ = tmp_path_factory.getbasetemp() / 'cache' A__ = test_hf_cache_home / 'datasets' A__ = test_hf_cache_home / 'metrics' A__ = test_hf_cache_home / 'modules' monkeypatch.setattr('datasets.config.HF_DATASETS_CACHE' , str(__UpperCamelCase ) ) monkeypatch.setattr('datasets.config.HF_METRICS_CACHE' , str(__UpperCamelCase ) ) monkeypatch.setattr('datasets.config.HF_MODULES_CACHE' , str(__UpperCamelCase ) ) A__ = test_hf_datasets_cache / 'downloads' monkeypatch.setattr('datasets.config.DOWNLOADED_DATASETS_PATH' , str(__UpperCamelCase ) ) A__ = test_hf_datasets_cache / 'downloads' / 'extracted' monkeypatch.setattr('datasets.config.EXTRACTED_DATASETS_PATH' , str(__UpperCamelCase ) ) @pytest.fixture(autouse=__UpperCamelCase , scope='session' ) def A ( ) -> Union[str, Any]: datasets.disable_progress_bar() @pytest.fixture(autouse=__UpperCamelCase ) def A ( __UpperCamelCase ) -> int: # don't take tests into account when counting downloads monkeypatch.setattr('datasets.config.HF_UPDATE_DOWNLOAD_COUNTS' , __UpperCamelCase ) @pytest.fixture def A ( __UpperCamelCase ) -> Any: # Required to suppress RemovedIn20Warning when feature(s) are not compatible with SQLAlchemy 2.0 # To be removed once SQLAlchemy 2.0 supported monkeypatch.setattr('sqlalchemy.util.deprecations.SILENCE_UBER_WARNING' , __UpperCamelCase )
52
1
import torch from transformers import CamembertForMaskedLM, CamembertTokenizer def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=5 ) -> Any: # Adapted from https://github.com/pytorch/fairseq/blob/master/fairseq/models/roberta/hub_interface.py assert masked_input.count('<mask>' ) == 1 A__ = torch.tensor(tokenizer.encode(__UpperCamelCase , add_special_tokens=__UpperCamelCase ) ).unsqueeze(0 ) # Batch size 1 A__ = model(__UpperCamelCase )[0] # The last hidden-state is the first element of the output tuple A__ = (input_ids.squeeze() == tokenizer.mask_token_id).nonzero().item() A__ = logits[0, masked_index, :] A__ = logits.softmax(dim=0 ) A__ , A__ = prob.topk(k=__UpperCamelCase , dim=0 ) A__ = ' '.join( [tokenizer.convert_ids_to_tokens(indices[i].item() ) for i in range(len(__UpperCamelCase ) )] ) A__ = tokenizer.mask_token A__ = [] for index, predicted_token_bpe in enumerate(topk_predicted_token_bpe.split(' ' ) ): A__ = predicted_token_bpe.replace('\u2581' , ' ' ) if " {0}".format(__UpperCamelCase ) in masked_input: topk_filled_outputs.append( ( masked_input.replace(' {0}'.format(__UpperCamelCase ) , __UpperCamelCase ), values[index].item(), predicted_token, ) ) else: topk_filled_outputs.append( ( masked_input.replace(__UpperCamelCase , __UpperCamelCase ), values[index].item(), predicted_token, ) ) return topk_filled_outputs SCREAMING_SNAKE_CASE__ = CamembertTokenizer.from_pretrained('''camembert-base''') SCREAMING_SNAKE_CASE__ = CamembertForMaskedLM.from_pretrained('''camembert-base''') model.eval() SCREAMING_SNAKE_CASE__ = '''Le camembert est <mask> :)''' print(fill_mask(masked_input, model, tokenizer, topk=3))
52
import argparse import re from typing import Dict import torch from datasets import Audio, Dataset, load_dataset, load_metric from transformers import AutoFeatureExtractor, pipeline def A ( __UpperCamelCase , __UpperCamelCase ) -> Tuple: A__ = args.log_outputs A__ = '_'.join(args.dataset.split('/' ) + [args.config, args.split] ) # load metric A__ = load_metric('wer' ) A__ = load_metric('cer' ) # compute metrics A__ = wer.compute(references=result['target'] , predictions=result['prediction'] ) A__ = cer.compute(references=result['target'] , predictions=result['prediction'] ) # print & log results A__ = f'''WER: {wer_result}\nCER: {cer_result}''' print(__UpperCamelCase ) with open(f'''{dataset_id}_eval_results.txt''' , 'w' ) as f: f.write(__UpperCamelCase ) # log all results in text file. Possibly interesting for analysis if log_outputs is not None: A__ = f'''log_{dataset_id}_predictions.txt''' A__ = f'''log_{dataset_id}_targets.txt''' with open(__UpperCamelCase , 'w' ) as p, open(__UpperCamelCase , 'w' ) as t: # mapping function to write output def write_to_file(__UpperCamelCase , __UpperCamelCase ): p.write(f'''{i}''' + '\n' ) p.write(batch['prediction'] + '\n' ) t.write(f'''{i}''' + '\n' ) t.write(batch['target'] + '\n' ) result.map(__UpperCamelCase , with_indices=__UpperCamelCase ) def A ( __UpperCamelCase ) -> str: A__ = '[,?.!\-\;\:"“%‘”�—’…–]' # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training A__ = re.sub(__UpperCamelCase , '' , text.lower() ) # In addition, we can normalize the target text, e.g. removing new lines characters etc... # note that order is important here! A__ = ['\n\n', '\n', ' ', ' '] for t in token_sequences_to_ignore: A__ = ' '.join(text.split(__UpperCamelCase ) ) return text def A ( __UpperCamelCase ) -> Union[str, Any]: # load dataset A__ = load_dataset(args.dataset , args.config , split=args.split , use_auth_token=__UpperCamelCase ) # for testing: only process the first two examples as a test # dataset = dataset.select(range(10)) # load processor A__ = AutoFeatureExtractor.from_pretrained(args.model_id ) A__ = feature_extractor.sampling_rate # resample audio A__ = dataset.cast_column('audio' , Audio(sampling_rate=__UpperCamelCase ) ) # load eval pipeline if args.device is None: A__ = 0 if torch.cuda.is_available() else -1 A__ = pipeline('automatic-speech-recognition' , model=args.model_id , device=args.device ) # map function to decode audio def map_to_pred(__UpperCamelCase ): A__ = asr( batch['audio']['array'] , chunk_length_s=args.chunk_length_s , stride_length_s=args.stride_length_s ) A__ = prediction['text'] A__ = normalize_text(batch['sentence'] ) return batch # run inference on all examples A__ = dataset.map(__UpperCamelCase , remove_columns=dataset.column_names ) # compute and log_results # do not change function below log_results(__UpperCamelCase , __UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument( '''--model_id''', type=str, required=True, help='''Model identifier. Should be loadable with 🤗 Transformers''' ) parser.add_argument( '''--dataset''', type=str, required=True, help='''Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets''', ) parser.add_argument( '''--config''', type=str, required=True, help='''Config of the dataset. *E.g.* `\'en\'` for Common Voice''' ) parser.add_argument('''--split''', type=str, required=True, help='''Split of the dataset. *E.g.* `\'test\'`''') parser.add_argument( '''--chunk_length_s''', type=float, default=None, help='''Chunk length in seconds. Defaults to 5 seconds.''' ) parser.add_argument( '''--stride_length_s''', type=float, default=None, help='''Stride of the audio chunks. Defaults to 1 second.''' ) parser.add_argument( '''--log_outputs''', action='''store_true''', help='''If defined, write outputs to log file for analysis.''' ) parser.add_argument( '''--device''', type=int, default=None, help='''The device to run the pipeline on. -1 for CPU (default), 0 for the first GPU and so on.''', ) SCREAMING_SNAKE_CASE__ = parser.parse_args() main(args)
52
1
from typing import List from .keymap import KEYMAP, get_character def A ( __UpperCamelCase ) -> Any: def decorator(__UpperCamelCase ): A__ = getattr(__UpperCamelCase , 'handle_key' , [] ) handle += [key] setattr(__UpperCamelCase , 'handle_key' , __UpperCamelCase ) return func return decorator def A ( *__UpperCamelCase ) -> str: def decorator(__UpperCamelCase ): A__ = getattr(__UpperCamelCase , 'handle_key' , [] ) handle += keys setattr(__UpperCamelCase , 'handle_key' , __UpperCamelCase ) return func return decorator class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __new__( cls : Dict , _snake_case : int , _snake_case : Any , _snake_case : List[str] ): """simple docstring""" A__ = super().__new__(cls , _snake_case , _snake_case , _snake_case ) if not hasattr(_snake_case , 'key_handler' ): setattr(_snake_case , 'key_handler' , {} ) setattr(_snake_case , 'handle_input' , KeyHandler.handle_input ) for value in attrs.values(): A__ = getattr(_snake_case , 'handle_key' , [] ) for key in handled_keys: A__ = value return new_cls @staticmethod def _a ( cls : Optional[Any] ): """simple docstring""" A__ = get_character() if char != KEYMAP["undefined"]: A__ = ord(_snake_case ) A__ = cls.key_handler.get(_snake_case ) if handler: A__ = char return handler(cls ) else: return None def A ( cls ) -> Any: return KeyHandler(cls.__name__ , cls.__bases__ , cls.__dict__.copy() )
52
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import YolosConfig, YolosForObjectDetection, YolosImageProcessor from transformers.utils import logging logging.set_verbosity_info() SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) def A ( __UpperCamelCase ) -> YolosConfig: A__ = YolosConfig() # size of the architecture if "yolos_ti" in yolos_name: A__ = 192 A__ = 768 A__ = 12 A__ = 3 A__ = [800, 1_333] A__ = False elif yolos_name == "yolos_s_dWr": A__ = 330 A__ = 14 A__ = 6 A__ = 1_320 elif "yolos_s" in yolos_name: A__ = 384 A__ = 1_536 A__ = 12 A__ = 6 elif "yolos_b" in yolos_name: A__ = [800, 1_344] A__ = 91 A__ = 'huggingface/label-files' A__ = 'coco-detection-id2label.json' A__ = json.load(open(hf_hub_download(__UpperCamelCase , __UpperCamelCase , repo_type='dataset' ) , 'r' ) ) A__ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} A__ = idalabel A__ = {v: k for k, v in idalabel.items()} return config def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = False ) -> str: for i in range(config.num_hidden_layers ): # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) A__ = state_dict.pop(f'''blocks.{i}.attn.qkv.weight''' ) A__ = state_dict.pop(f'''blocks.{i}.attn.qkv.bias''' ) # next, add query, keys and values (in that order) to the state dict A__ = in_proj_weight[: config.hidden_size, :] A__ = in_proj_bias[: config.hidden_size] A__ = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] A__ = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] A__ = in_proj_weight[-config.hidden_size :, :] A__ = in_proj_bias[-config.hidden_size :] def A ( __UpperCamelCase ) -> str: if "backbone" in name: A__ = name.replace('backbone' , 'vit' ) if "cls_token" in name: A__ = name.replace('cls_token' , 'embeddings.cls_token' ) if "det_token" in name: A__ = name.replace('det_token' , 'embeddings.detection_tokens' ) if "mid_pos_embed" in name: A__ = name.replace('mid_pos_embed' , 'encoder.mid_position_embeddings' ) if "pos_embed" in name: A__ = name.replace('pos_embed' , 'embeddings.position_embeddings' ) if "patch_embed.proj" in name: A__ = name.replace('patch_embed.proj' , 'embeddings.patch_embeddings.projection' ) if "blocks" in name: A__ = name.replace('blocks' , 'encoder.layer' ) if "attn.proj" in name: A__ = name.replace('attn.proj' , 'attention.output.dense' ) if "attn" in name: A__ = name.replace('attn' , 'attention.self' ) if "norm1" in name: A__ = name.replace('norm1' , 'layernorm_before' ) if "norm2" in name: A__ = name.replace('norm2' , 'layernorm_after' ) if "mlp.fc1" in name: A__ = name.replace('mlp.fc1' , 'intermediate.dense' ) if "mlp.fc2" in name: A__ = name.replace('mlp.fc2' , 'output.dense' ) if "class_embed" in name: A__ = name.replace('class_embed' , 'class_labels_classifier' ) if "bbox_embed" in name: A__ = name.replace('bbox_embed' , 'bbox_predictor' ) if "vit.norm" in name: A__ = name.replace('vit.norm' , 'vit.layernorm' ) return name def A ( __UpperCamelCase , __UpperCamelCase ) -> dict: for key in orig_state_dict.copy().keys(): A__ = orig_state_dict.pop(__UpperCamelCase ) if "qkv" in key: A__ = key.split('.' ) A__ = int(key_split[2] ) A__ = model.vit.encoder.layer[layer_num].attention.attention.all_head_size if "weight" in key: A__ = val[:dim, :] A__ = val[ dim : dim * 2, : ] A__ = val[-dim:, :] else: A__ = val[:dim] A__ = val[dim : dim * 2] A__ = val[-dim:] else: A__ = val return orig_state_dict def A ( ) -> torch.Tensor: A__ = 'http://images.cocodataset.org/val2017/000000039769.jpg' A__ = Image.open(requests.get(__UpperCamelCase , stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = False ) -> List[str]: A__ = get_yolos_config(__UpperCamelCase ) # load original state_dict A__ = torch.load(__UpperCamelCase , map_location='cpu' )['model'] # load 🤗 model A__ = YolosForObjectDetection(__UpperCamelCase ) model.eval() A__ = convert_state_dict(__UpperCamelCase , __UpperCamelCase ) model.load_state_dict(__UpperCamelCase ) # Check outputs on an image, prepared by YolosImageProcessor A__ = 800 if yolos_name != 'yolos_ti' else 512 A__ = YolosImageProcessor(format='coco_detection' , size=__UpperCamelCase ) A__ = image_processor(images=prepare_img() , return_tensors='pt' ) A__ = model(**__UpperCamelCase ) A__ , A__ = outputs.logits, outputs.pred_boxes A__ , A__ = None, None if yolos_name == "yolos_ti": A__ = torch.tensor( [[-39.5022, -11.9820, -17.6888], [-29.9574, -9.9769, -17.7691], [-42.3281, -20.7200, -30.6294]] ) A__ = torch.tensor( [[0.4021, 0.0836, 0.7979], [0.0184, 0.2609, 0.0364], [0.1781, 0.2004, 0.2095]] ) elif yolos_name == "yolos_s_200_pre": A__ = torch.tensor( [[-24.0248, -10.3024, -14.8290], [-42.0392, -16.8200, -27.4334], [-27.2743, -11.8154, -18.7148]] ) A__ = torch.tensor( [[0.2559, 0.5455, 0.4706], [0.2989, 0.7279, 0.1875], [0.7732, 0.4017, 0.4462]] ) elif yolos_name == "yolos_s_300_pre": A__ = torch.tensor( [[-36.2220, -14.4385, -23.5457], [-35.6970, -14.7583, -21.3935], [-31.5939, -13.6042, -16.8049]] ) A__ = torch.tensor( [[0.7614, 0.2316, 0.4728], [0.7168, 0.4495, 0.3855], [0.4996, 0.1466, 0.9996]] ) elif yolos_name == "yolos_s_dWr": A__ = torch.tensor( [[-42.8668, -24.1049, -41.1690], [-34.7456, -14.1274, -24.9194], [-33.7898, -12.1946, -25.6495]] ) A__ = torch.tensor( [[0.5587, 0.2773, 0.0605], [0.5004, 0.3014, 0.9994], [0.4999, 0.1548, 0.9994]] ) elif yolos_name == "yolos_base": A__ = torch.tensor( [[-40.6064, -24.3084, -32.6447], [-55.1990, -30.7719, -35.5877], [-51.4311, -33.3507, -35.6462]] ) A__ = torch.tensor( [[0.5555, 0.2794, 0.0655], [0.9049, 0.2664, 0.1894], [0.9183, 0.1984, 0.1635]] ) else: raise ValueError(f'''Unknown yolos_name: {yolos_name}''' ) assert torch.allclose(logits[0, :3, :3] , __UpperCamelCase , atol=1E-4 ) assert torch.allclose(pred_boxes[0, :3, :3] , __UpperCamelCase , atol=1E-4 ) Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) print(f'''Saving model {yolos_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(__UpperCamelCase ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(__UpperCamelCase ) if push_to_hub: A__ = { 'yolos_ti': 'yolos-tiny', 'yolos_s_200_pre': 'yolos-small', 'yolos_s_300_pre': 'yolos-small-300', 'yolos_s_dWr': 'yolos-small-dwr', 'yolos_base': 'yolos-base', } print('Pushing to the hub...' ) A__ = model_mapping[yolos_name] image_processor.push_to_hub(__UpperCamelCase , organization='hustvl' ) model.push_to_hub(__UpperCamelCase , organization='hustvl' ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--yolos_name''', default='''yolos_s_200_pre''', type=str, help=( '''Name of the YOLOS model you\'d like to convert. Should be one of \'yolos_ti\', \'yolos_s_200_pre\',''' ''' \'yolos_s_300_pre\', \'yolos_s_dWr\', \'yolos_base\'.''' ), ) parser.add_argument( '''--checkpoint_path''', default=None, type=str, help='''Path to the original state dict (.pth file).''' ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_yolos_checkpoint(args.yolos_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub)
52
1
import random def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = False ) -> dict: A__ = {i: [] for i in range(__UpperCamelCase )} # if probability is greater or equal than 1, then generate a complete graph if probability >= 1: return complete_graph(__UpperCamelCase ) # if probability is lower or equal than 0, then return a graph without edges if probability <= 0: return graph # for each couple of nodes, add an edge from u to v # if the number randomly generated is greater than probability probability for i in range(__UpperCamelCase ): for j in range(i + 1 , __UpperCamelCase ): if random.random() < probability: graph[i].append(__UpperCamelCase ) if not directed: # if the graph is undirected, add an edge in from j to i, either graph[j].append(__UpperCamelCase ) return graph def A ( __UpperCamelCase ) -> dict: return { i: [j for j in range(__UpperCamelCase ) if i != j] for i in range(__UpperCamelCase ) } if __name__ == "__main__": import doctest doctest.testmod()
52
from typing import TYPE_CHECKING from ..utils import _LazyModule SCREAMING_SNAKE_CASE__ = { '''config''': [ '''EXTERNAL_DATA_FORMAT_SIZE_LIMIT''', '''OnnxConfig''', '''OnnxConfigWithPast''', '''OnnxSeq2SeqConfigWithPast''', '''PatchingSpec''', ], '''convert''': ['''export''', '''validate_model_outputs'''], '''features''': ['''FeaturesManager'''], '''utils''': ['''ParameterFormat''', '''compute_serialized_parameters_size'''], } if TYPE_CHECKING: from .config import ( EXTERNAL_DATA_FORMAT_SIZE_LIMIT, OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast, PatchingSpec, ) from .convert import export, validate_model_outputs from .features import FeaturesManager from .utils import ParameterFormat, compute_serialized_parameters_size else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
52
1
import baseaa def A ( __UpperCamelCase ) -> bytes: return baseaa.baaencode(string.encode('utf-8' ) ) def A ( __UpperCamelCase ) -> str: return baseaa.baadecode(__UpperCamelCase ).decode('utf-8' ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = '''Hello World!''' SCREAMING_SNAKE_CASE__ = baseaa_encode(test) print(encoded) SCREAMING_SNAKE_CASE__ = baseaa_decode(encoded) print(decoded)
52
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_rembert import RemBertTokenizer else: SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = {'''vocab_file''': '''sentencepiece.model''', '''tokenizer_file''': '''tokenizer.json'''} SCREAMING_SNAKE_CASE__ = { '''vocab_file''': { '''google/rembert''': '''https://huggingface.co/google/rembert/resolve/main/sentencepiece.model''', }, '''tokenizer_file''': { '''google/rembert''': '''https://huggingface.co/google/rembert/resolve/main/tokenizer.json''', }, } SCREAMING_SNAKE_CASE__ = { '''google/rembert''': 2_5_6, } SCREAMING_SNAKE_CASE__ = '''▁''' class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Any = VOCAB_FILES_NAMES A__ : str = PRETRAINED_VOCAB_FILES_MAP A__ : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A__ : int = RemBertTokenizer def __init__( self : Union[str, Any] , _snake_case : Any=None , _snake_case : Optional[Any]=None , _snake_case : Any=True , _snake_case : Optional[int]=True , _snake_case : Dict=False , _snake_case : Dict="[CLS]" , _snake_case : List[Any]="[SEP]" , _snake_case : Union[str, Any]="<unk>" , _snake_case : List[str]="[SEP]" , _snake_case : List[str]="<pad>" , _snake_case : str="[CLS]" , _snake_case : Any="[MASK]" , **_snake_case : Any , ): """simple docstring""" A__ = AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case ) if isinstance(_snake_case , _snake_case ) else mask_token super().__init__( _snake_case , tokenizer_file=_snake_case , do_lower_case=_snake_case , remove_space=_snake_case , keep_accents=_snake_case , bos_token=_snake_case , eos_token=_snake_case , unk_token=_snake_case , sep_token=_snake_case , pad_token=_snake_case , cls_token=_snake_case , mask_token=_snake_case , **_snake_case , ) A__ = do_lower_case A__ = remove_space A__ = keep_accents A__ = vocab_file A__ = False if not self.vocab_file else True def _a ( self : Any , _snake_case : List[int] , _snake_case : Optional[List[int]] = None ): """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def _a ( self : Tuple , _snake_case : List[int] , _snake_case : Optional[List[int]] = None , _snake_case : bool = False ): """simple docstring""" if already_has_special_tokens: if token_ids_a is not None: raise ValueError( 'You should not supply a second sequence if the provided sequence of ' 'ids is already formatted with special tokens for the model.' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(_snake_case )) + [1] + ([0] * len(_snake_case )) + [1] return [1] + ([0] * len(_snake_case )) + [1] def _a ( self : Dict , _snake_case : List[int] , _snake_case : Optional[List[int]] = None ): """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def _a ( self : Any , _snake_case : str , _snake_case : Optional[str] = None ): """simple docstring""" if not os.path.isdir(_snake_case ): logger.error('Vocabulary path ({}) should be a directory'.format(_snake_case ) ) return A__ = os.path.join( _snake_case , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_snake_case ): copyfile(self.vocab_file , _snake_case ) return (out_vocab_file,)
52
1
def A ( __UpperCamelCase , __UpperCamelCase ) -> str: A__ = len(__UpperCamelCase ) A__ = len(__UpperCamelCase ) A__ = ( first_str_length if first_str_length > second_str_length else second_str_length ) A__ = [] for char_count in range(__UpperCamelCase ): if char_count < first_str_length: output_list.append(first_str[char_count] ) if char_count < second_str_length: output_list.append(second_str[char_count] ) return "".join(__UpperCamelCase ) if __name__ == "__main__": print(alternative_string_arrange('''AB''', '''XYZ'''), end=''' ''')
52
import tempfile import unittest from make_student import create_student_by_copying_alternating_layers from transformers import AutoConfig from transformers.file_utils import cached_property from transformers.testing_utils import require_torch SCREAMING_SNAKE_CASE__ = '''sshleifer/bart-tiny-random''' SCREAMING_SNAKE_CASE__ = '''patrickvonplaten/t5-tiny-random''' @require_torch class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @cached_property def _a ( self : Optional[int] ): """simple docstring""" return AutoConfig.from_pretrained(_snake_case ) def _a ( self : Optional[Any] ): """simple docstring""" A__ , *A__ = create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=1 , d=1 ) self.assertEqual(student.config.num_hidden_layers , 1 ) def _a ( self : Optional[int] ): """simple docstring""" A__ , *A__ = create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=1 , d=_snake_case ) def _a ( self : int ): """simple docstring""" A__ , *A__ = create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=1 , d=_snake_case ) self.assertEqual(student.config.encoder_layers , 1 ) self.assertEqual(student.config.decoder_layers , self.teacher_config.encoder_layers ) def _a ( self : str ): """simple docstring""" A__ , *A__ = create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=1 , d=1 ) self.assertEqual(student.config.encoder_layers , 1 ) self.assertEqual(student.config.decoder_layers , 1 ) def _a ( self : str ): """simple docstring""" with self.assertRaises(_snake_case ): create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=_snake_case , d=_snake_case )
52
1
from datetime import datetime import requests def A ( __UpperCamelCase ) -> bytes: A__ = 'https://downloadgram.net/wp-json/wppress/video-downloader/video?url=' A__ = requests.get(base_url + url ).json()[0]['urls'][0]['src'] return requests.get(__UpperCamelCase ).content if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = input('''Enter Video/IGTV url: ''').strip() SCREAMING_SNAKE_CASE__ = f'{datetime.now():%Y-%m-%d_%H:%M:%S}.mp4' with open(file_name, '''wb''') as fp: fp.write(download_video(url)) print(f'Done. Video saved to disk as {file_name}.')
52
from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Union[str, Any] = ["image_processor", "tokenizer"] A__ : Optional[Any] = "BridgeTowerImageProcessor" A__ : List[Any] = ("RobertaTokenizer", "RobertaTokenizerFast") def __init__( self : List[Any] , _snake_case : Optional[Any] , _snake_case : Optional[int] ): """simple docstring""" super().__init__(_snake_case , _snake_case ) def __call__( self : List[Any] , _snake_case : int , _snake_case : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , _snake_case : bool = True , _snake_case : Union[bool, str, PaddingStrategy] = False , _snake_case : Union[bool, str, TruncationStrategy] = None , _snake_case : Optional[int] = None , _snake_case : int = 0 , _snake_case : Optional[int] = None , _snake_case : Optional[bool] = None , _snake_case : Optional[bool] = None , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = True , _snake_case : Optional[Union[str, TensorType]] = None , **_snake_case : Optional[int] , ): """simple docstring""" A__ = self.tokenizer( text=_snake_case , add_special_tokens=_snake_case , padding=_snake_case , truncation=_snake_case , max_length=_snake_case , stride=_snake_case , pad_to_multiple_of=_snake_case , return_token_type_ids=_snake_case , return_attention_mask=_snake_case , return_overflowing_tokens=_snake_case , return_special_tokens_mask=_snake_case , return_offsets_mapping=_snake_case , return_length=_snake_case , verbose=_snake_case , return_tensors=_snake_case , **_snake_case , ) # add pixel_values + pixel_mask A__ = self.image_processor( _snake_case , return_tensors=_snake_case , do_normalize=_snake_case , do_center_crop=_snake_case , **_snake_case ) encoding.update(_snake_case ) return encoding def _a ( self : Any , *_snake_case : Tuple , **_snake_case : List[Any] ): """simple docstring""" return self.tokenizer.batch_decode(*_snake_case , **_snake_case ) def _a ( self : Dict , *_snake_case : Dict , **_snake_case : List[str] ): """simple docstring""" return self.tokenizer.decode(*_snake_case , **_snake_case ) @property def _a ( self : Tuple ): """simple docstring""" A__ = self.tokenizer.model_input_names A__ = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
52
1
SCREAMING_SNAKE_CASE__ = '''0.21.0''' from .accelerator import Accelerator from .big_modeling import ( cpu_offload, cpu_offload_with_hook, disk_offload, dispatch_model, init_empty_weights, init_on_device, load_checkpoint_and_dispatch, ) from .data_loader import skip_first_batches from .launchers import debug_launcher, notebook_launcher from .state import PartialState from .utils import ( DeepSpeedPlugin, DistributedDataParallelKwargs, DistributedType, FullyShardedDataParallelPlugin, GradScalerKwargs, InitProcessGroupKwargs, find_executable_batch_size, infer_auto_device_map, is_rich_available, load_checkpoint_in_model, synchronize_rng_states, ) if is_rich_available(): from .utils import rich
52
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) SCREAMING_SNAKE_CASE__ = { '''configuration_xlm_roberta''': [ '''XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XLMRobertaConfig''', '''XLMRobertaOnnxConfig''', ], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = ['''XLMRobertaTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = ['''XLMRobertaTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XLMRobertaForCausalLM''', '''XLMRobertaForMaskedLM''', '''XLMRobertaForMultipleChoice''', '''XLMRobertaForQuestionAnswering''', '''XLMRobertaForSequenceClassification''', '''XLMRobertaForTokenClassification''', '''XLMRobertaModel''', '''XLMRobertaPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXLMRobertaForCausalLM''', '''TFXLMRobertaForMaskedLM''', '''TFXLMRobertaForMultipleChoice''', '''TFXLMRobertaForQuestionAnswering''', '''TFXLMRobertaForSequenceClassification''', '''TFXLMRobertaForTokenClassification''', '''TFXLMRobertaModel''', '''TFXLMRobertaPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''FlaxXLMRobertaForMaskedLM''', '''FlaxXLMRobertaForCausalLM''', '''FlaxXLMRobertaForMultipleChoice''', '''FlaxXLMRobertaForQuestionAnswering''', '''FlaxXLMRobertaForSequenceClassification''', '''FlaxXLMRobertaForTokenClassification''', '''FlaxXLMRobertaModel''', '''FlaxXLMRobertaPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_xlm_roberta import ( XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMRobertaConfig, XLMRobertaOnnxConfig, ) try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlm_roberta import XLMRobertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlm_roberta_fast import XLMRobertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm_roberta import ( XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, XLMRobertaForCausalLM, XLMRobertaForMaskedLM, XLMRobertaForMultipleChoice, XLMRobertaForQuestionAnswering, XLMRobertaForSequenceClassification, XLMRobertaForTokenClassification, XLMRobertaModel, XLMRobertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlm_roberta import ( TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLMRobertaForCausalLM, TFXLMRobertaForMaskedLM, TFXLMRobertaForMultipleChoice, TFXLMRobertaForQuestionAnswering, TFXLMRobertaForSequenceClassification, TFXLMRobertaForTokenClassification, TFXLMRobertaModel, TFXLMRobertaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xlm_roberta import ( FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxXLMRobertaForCausalLM, FlaxXLMRobertaForMaskedLM, FlaxXLMRobertaForMultipleChoice, FlaxXLMRobertaForQuestionAnswering, FlaxXLMRobertaForSequenceClassification, FlaxXLMRobertaForTokenClassification, FlaxXLMRobertaModel, FlaxXLMRobertaPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
52
1
import argparse SCREAMING_SNAKE_CASE__ = '''docs/source/_static/js/custom.js''' def A ( __UpperCamelCase ) -> Dict: with open(__UpperCamelCase , encoding='utf-8' , newline='\n' ) as f: A__ = f.readlines() A__ = 0 # First let's put the right version while not lines[index].startswith('const stableVersion =' ): index += 1 A__ = f'''const stableVersion = "v{version}"\n''' # Then update the dictionary while not lines[index].startswith('const versionMapping = {' ): index += 1 # We go until the end while not lines[index].startswith('}' ): index += 1 # We add the new version at the end lines[index - 1] += f''' "v{version}": "v{version}",\n''' with open(__UpperCamelCase , 'w' , encoding='utf-8' , newline='\n' ) as f: f.writelines(__UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument('''--version''', help='''Release version.''') SCREAMING_SNAKE_CASE__ = parser.parse_args() update_custom_js(args.version)
52
import argparse import ast import logging import os import sys import pandas as pd import torch from tqdm import tqdm from transformers import BartForConditionalGeneration, RagRetriever, RagSequenceForGeneration, RagTokenForGeneration from transformers import logging as transformers_logging sys.path.append(os.path.join(os.getcwd())) # noqa: E402 # isort:skip from utils_rag import exact_match_score, fa_score # noqa: E402 # isort:skip SCREAMING_SNAKE_CASE__ = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) transformers_logging.set_verbosity_info() def A ( __UpperCamelCase ) -> Tuple: if "token" in model_name_or_path: return "rag_token" if "sequence" in model_name_or_path: return "rag_sequence" if "bart" in model_name_or_path: return "bart" return None def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[Any]: return max(metric_fn(__UpperCamelCase , __UpperCamelCase ) for gt in ground_truths ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[str]: A__ = [line.strip() for line in open(__UpperCamelCase , 'r' ).readlines()] A__ = [] if args.gold_data_mode == "qa": A__ = pd.read_csv(__UpperCamelCase , sep='\t' , header=__UpperCamelCase ) for answer_list in data[1]: A__ = ast.literal_eval(__UpperCamelCase ) answers.append(__UpperCamelCase ) else: A__ = [line.strip() for line in open(__UpperCamelCase , 'r' ).readlines()] A__ = [[reference] for reference in references] A__ = A__ = A__ = 0 for prediction, ground_truths in zip(__UpperCamelCase , __UpperCamelCase ): total += 1 em += metric_max_over_ground_truths(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) fa += metric_max_over_ground_truths(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) A__ = 100.0 * em / total A__ = 100.0 * fa / total logger.info(f'''F1: {fa:.2f}''' ) logger.info(f'''EM: {em:.2f}''' ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: A__ = args.k A__ = [line.strip() for line in open(__UpperCamelCase , 'r' ).readlines()] A__ = [line.strip() for line in open(__UpperCamelCase , 'r' ).readlines()] A__ = A__ = 0 for hypo, reference in zip(__UpperCamelCase , __UpperCamelCase ): A__ = set(hypo.split('\t' )[:k] ) A__ = set(reference.split('\t' ) ) total += 1 em += len(hypo_provenance & ref_provenance ) / k A__ = 100.0 * em / total logger.info(f'''Precision@{k}: {em: .2f}''' ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Union[str, Any]: def strip_title(__UpperCamelCase ): if title.startswith('"' ): A__ = title[1:] if title.endswith('"' ): A__ = title[:-1] return title A__ = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( __UpperCamelCase , return_tensors='pt' , padding=__UpperCamelCase , truncation=__UpperCamelCase , )['input_ids'].to(args.device ) A__ = rag_model.rag.question_encoder(__UpperCamelCase ) A__ = question_enc_outputs[0] A__ = rag_model.retriever( __UpperCamelCase , question_enc_pool_output.cpu().detach().to(torch.floataa ).numpy() , prefix=rag_model.rag.generator.config.prefix , n_docs=rag_model.config.n_docs , return_tensors='pt' , ) A__ = rag_model.retriever.index.get_doc_dicts(result.doc_ids ) A__ = [] for docs in all_docs: A__ = [strip_title(__UpperCamelCase ) for title in docs['title']] provenance_strings.append('\t'.join(__UpperCamelCase ) ) return provenance_strings def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Optional[Any]: with torch.no_grad(): A__ = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( __UpperCamelCase , return_tensors='pt' , padding=__UpperCamelCase , truncation=__UpperCamelCase ) A__ = inputs_dict.input_ids.to(args.device ) A__ = inputs_dict.attention_mask.to(args.device ) A__ = rag_model.generate( # rag_model overwrites generate __UpperCamelCase , attention_mask=__UpperCamelCase , num_beams=args.num_beams , min_length=args.min_length , max_length=args.max_length , early_stopping=__UpperCamelCase , num_return_sequences=1 , bad_words_ids=[[0, 0]] , ) A__ = rag_model.retriever.generator_tokenizer.batch_decode(__UpperCamelCase , skip_special_tokens=__UpperCamelCase ) if args.print_predictions: for q, a in zip(__UpperCamelCase , __UpperCamelCase ): logger.info('Q: {} - A: {}'.format(__UpperCamelCase , __UpperCamelCase ) ) return answers def A ( ) -> Any: A__ = argparse.ArgumentParser() parser.add_argument( '--model_type' , choices=['rag_sequence', 'rag_token', 'bart'] , type=__UpperCamelCase , help=( 'RAG model type: rag_sequence, rag_token or bart, if none specified, the type is inferred from the' ' model_name_or_path' ) , ) parser.add_argument( '--index_name' , default=__UpperCamelCase , choices=['exact', 'compressed', 'legacy'] , type=__UpperCamelCase , help='RAG model retriever type' , ) parser.add_argument( '--index_path' , default=__UpperCamelCase , type=__UpperCamelCase , help='Path to the retrieval index' , ) parser.add_argument('--n_docs' , default=5 , type=__UpperCamelCase , help='Number of retrieved docs' ) parser.add_argument( '--model_name_or_path' , default=__UpperCamelCase , type=__UpperCamelCase , required=__UpperCamelCase , help='Path to pretrained checkpoints or model identifier from huggingface.co/models' , ) parser.add_argument( '--eval_mode' , choices=['e2e', 'retrieval'] , default='e2e' , type=__UpperCamelCase , help=( 'Evaluation mode, e2e calculates exact match and F1 of the downstream task, retrieval calculates' ' precision@k.' ) , ) parser.add_argument('--k' , default=1 , type=__UpperCamelCase , help='k for the precision@k calculation' ) parser.add_argument( '--evaluation_set' , default=__UpperCamelCase , type=__UpperCamelCase , required=__UpperCamelCase , help='Path to a file containing evaluation samples' , ) parser.add_argument( '--gold_data_path' , default=__UpperCamelCase , type=__UpperCamelCase , required=__UpperCamelCase , help='Path to a tab-separated file with gold samples' , ) parser.add_argument( '--gold_data_mode' , default='qa' , type=__UpperCamelCase , choices=['qa', 'ans'] , help=( 'Format of the gold data file' 'qa - a single line in the following format: question [tab] answer_list' 'ans - a single line of the gold file contains the expected answer string' ) , ) parser.add_argument( '--predictions_path' , type=__UpperCamelCase , default='predictions.txt' , help='Name of the predictions file, to be stored in the checkpoints directory' , ) parser.add_argument( '--eval_all_checkpoints' , action='store_true' , help='Evaluate all checkpoints starting with the same prefix as model_name ending and ending with step number' , ) parser.add_argument( '--eval_batch_size' , default=8 , type=__UpperCamelCase , help='Batch size per GPU/CPU for evaluation.' , ) parser.add_argument( '--recalculate' , help='Recalculate predictions even if the prediction file exists' , action='store_true' , ) parser.add_argument( '--num_beams' , default=4 , type=__UpperCamelCase , help='Number of beams to be used when generating answers' , ) parser.add_argument('--min_length' , default=1 , type=__UpperCamelCase , help='Min length of the generated answers' ) parser.add_argument('--max_length' , default=50 , type=__UpperCamelCase , help='Max length of the generated answers' ) parser.add_argument( '--print_predictions' , action='store_true' , help='If True, prints predictions while evaluating.' , ) parser.add_argument( '--print_docs' , action='store_true' , help='If True, prints docs retried while generating.' , ) A__ = parser.parse_args() A__ = torch.device('cuda' if torch.cuda.is_available() else 'cpu' ) return args def A ( __UpperCamelCase ) -> int: A__ = {} if args.model_type is None: A__ = infer_model_type(args.model_name_or_path ) assert args.model_type is not None if args.model_type.startswith('rag' ): A__ = RagTokenForGeneration if args.model_type == 'rag_token' else RagSequenceForGeneration A__ = args.n_docs if args.index_name is not None: A__ = args.index_name if args.index_path is not None: A__ = args.index_path else: A__ = BartForConditionalGeneration A__ = ( [f.path for f in os.scandir(args.model_name_or_path ) if f.is_dir()] if args.eval_all_checkpoints else [args.model_name_or_path] ) logger.info('Evaluate the following checkpoints: %s' , __UpperCamelCase ) A__ = get_scores if args.eval_mode == 'e2e' else get_precision_at_k A__ = evaluate_batch_eae if args.eval_mode == 'e2e' else evaluate_batch_retrieval for checkpoint in checkpoints: if os.path.exists(args.predictions_path ) and (not args.recalculate): logger.info('Calculating metrics based on an existing predictions file: {}'.format(args.predictions_path ) ) score_fn(__UpperCamelCase , args.predictions_path , args.gold_data_path ) continue logger.info('***** Running evaluation for {} *****'.format(__UpperCamelCase ) ) logger.info(' Batch size = %d' , args.eval_batch_size ) logger.info(' Predictions will be stored under {}'.format(args.predictions_path ) ) if args.model_type.startswith('rag' ): A__ = RagRetriever.from_pretrained(__UpperCamelCase , **__UpperCamelCase ) A__ = model_class.from_pretrained(__UpperCamelCase , retriever=__UpperCamelCase , **__UpperCamelCase ) model.retriever.init_retrieval() else: A__ = model_class.from_pretrained(__UpperCamelCase , **__UpperCamelCase ) model.to(args.device ) with open(args.evaluation_set , 'r' ) as eval_file, open(args.predictions_path , 'w' ) as preds_file: A__ = [] for line in tqdm(__UpperCamelCase ): questions.append(line.strip() ) if len(__UpperCamelCase ) == args.eval_batch_size: A__ = evaluate_batch_fn(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) preds_file.write('\n'.join(__UpperCamelCase ) + '\n' ) preds_file.flush() A__ = [] if len(__UpperCamelCase ) > 0: A__ = evaluate_batch_fn(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) preds_file.write('\n'.join(__UpperCamelCase ) ) preds_file.flush() score_fn(__UpperCamelCase , args.predictions_path , args.gold_data_path ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = get_args() main(args)
52
1
import argparse import os import re SCREAMING_SNAKE_CASE__ = '''src/diffusers''' # Pattern that looks at the indentation in a line. SCREAMING_SNAKE_CASE__ = re.compile(r'''^(\s*)\S''') # Pattern that matches `"key":" and puts `key` in group 0. SCREAMING_SNAKE_CASE__ = re.compile(r'''^\s*"([^"]+)":''') # Pattern that matches `_import_structure["key"]` and puts `key` in group 0. SCREAMING_SNAKE_CASE__ = re.compile(r'''^\s*_import_structure\["([^"]+)"\]''') # Pattern that matches `"key",` and puts `key` in group 0. SCREAMING_SNAKE_CASE__ = re.compile(r'''^\s*"([^"]+)",\s*$''') # Pattern that matches any `[stuff]` and puts `stuff` in group 0. SCREAMING_SNAKE_CASE__ = re.compile(r'''\[([^\]]+)\]''') def A ( __UpperCamelCase ) -> str: A__ = _re_indent.search(__UpperCamelCase ) return "" if search is None else search.groups()[0] def A ( __UpperCamelCase , __UpperCamelCase="" , __UpperCamelCase=None , __UpperCamelCase=None ) -> List[str]: A__ = 0 A__ = code.split('\n' ) if start_prompt is not None: while not lines[index].startswith(__UpperCamelCase ): index += 1 A__ = ['\n'.join(lines[:index] )] else: A__ = [] # We split into blocks until we get to the `end_prompt` (or the end of the block). A__ = [lines[index]] index += 1 while index < len(__UpperCamelCase ) and (end_prompt is None or not lines[index].startswith(__UpperCamelCase )): if len(lines[index] ) > 0 and get_indent(lines[index] ) == indent_level: if len(__UpperCamelCase ) > 0 and get_indent(current_block[-1] ).startswith(indent_level + ' ' ): current_block.append(lines[index] ) blocks.append('\n'.join(__UpperCamelCase ) ) if index < len(__UpperCamelCase ) - 1: A__ = [lines[index + 1]] index += 1 else: A__ = [] else: blocks.append('\n'.join(__UpperCamelCase ) ) A__ = [lines[index]] else: current_block.append(lines[index] ) index += 1 # Adds current block if it's nonempty. if len(__UpperCamelCase ) > 0: blocks.append('\n'.join(__UpperCamelCase ) ) # Add final block after end_prompt if provided. if end_prompt is not None and index < len(__UpperCamelCase ): blocks.append('\n'.join(lines[index:] ) ) return blocks def A ( __UpperCamelCase ) -> str: def _inner(__UpperCamelCase ): return key(__UpperCamelCase ).lower().replace('_' , '' ) return _inner def A ( __UpperCamelCase , __UpperCamelCase=None ) -> Tuple: # If no key is provided, we use a noop. def noop(__UpperCamelCase ): return x if key is None: A__ = noop # Constants are all uppercase, they go first. A__ = [obj for obj in objects if key(__UpperCamelCase ).isupper()] # Classes are not all uppercase but start with a capital, they go second. A__ = [obj for obj in objects if key(__UpperCamelCase )[0].isupper() and not key(__UpperCamelCase ).isupper()] # Functions begin with a lowercase, they go last. A__ = [obj for obj in objects if not key(__UpperCamelCase )[0].isupper()] A__ = ignore_underscore(__UpperCamelCase ) return sorted(__UpperCamelCase , key=__UpperCamelCase ) + sorted(__UpperCamelCase , key=__UpperCamelCase ) + sorted(__UpperCamelCase , key=__UpperCamelCase ) def A ( __UpperCamelCase ) -> str: # This inner function sort imports between [ ]. def _replace(__UpperCamelCase ): A__ = match.groups()[0] if "," not in imports: return f'''[{imports}]''' A__ = [part.strip().replace('"' , '' ) for part in imports.split(',' )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: A__ = keys[:-1] return "[" + ", ".join([f'''"{k}"''' for k in sort_objects(__UpperCamelCase )] ) + "]" A__ = import_statement.split('\n' ) if len(__UpperCamelCase ) > 3: # Here we have to sort internal imports that are on several lines (one per name): # key: [ # "object1", # "object2", # ... # ] # We may have to ignore one or two lines on each side. A__ = 2 if lines[1].strip() == '[' else 1 A__ = [(i, _re_strip_line.search(__UpperCamelCase ).groups()[0]) for i, line in enumerate(lines[idx:-idx] )] A__ = sort_objects(__UpperCamelCase , key=lambda __UpperCamelCase : x[1] ) A__ = [lines[x[0] + idx] for x in sorted_indices] return "\n".join(lines[:idx] + sorted_lines + lines[-idx:] ) elif len(__UpperCamelCase ) == 3: # Here we have to sort internal imports that are on one separate line: # key: [ # "object1", "object2", ... # ] if _re_bracket_content.search(lines[1] ) is not None: A__ = _re_bracket_content.sub(_replace , lines[1] ) else: A__ = [part.strip().replace('"' , '' ) for part in lines[1].split(',' )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: A__ = keys[:-1] A__ = get_indent(lines[1] ) + ', '.join([f'''"{k}"''' for k in sort_objects(__UpperCamelCase )] ) return "\n".join(__UpperCamelCase ) else: # Finally we have to deal with imports fitting on one line A__ = _re_bracket_content.sub(_replace , __UpperCamelCase ) return import_statement def A ( __UpperCamelCase , __UpperCamelCase=True ) -> List[str]: with open(__UpperCamelCase , 'r' ) as f: A__ = f.read() if "_import_structure" not in code: return # Blocks of indent level 0 A__ = split_code_in_indented_blocks( __UpperCamelCase , start_prompt='_import_structure = {' , end_prompt='if TYPE_CHECKING:' ) # We ignore block 0 (everything until start_prompt) and the last block (everything after end_prompt). for block_idx in range(1 , len(__UpperCamelCase ) - 1 ): # Check if the block contains some `_import_structure`s thingy to sort. A__ = main_blocks[block_idx] A__ = block.split('\n' ) # Get to the start of the imports. A__ = 0 while line_idx < len(__UpperCamelCase ) and "_import_structure" not in block_lines[line_idx]: # Skip dummy import blocks if "import dummy" in block_lines[line_idx]: A__ = len(__UpperCamelCase ) else: line_idx += 1 if line_idx >= len(__UpperCamelCase ): continue # Ignore beginning and last line: they don't contain anything. A__ = '\n'.join(block_lines[line_idx:-1] ) A__ = get_indent(block_lines[1] ) # Slit the internal block into blocks of indent level 1. A__ = split_code_in_indented_blocks(__UpperCamelCase , indent_level=__UpperCamelCase ) # We have two categories of import key: list or _import_structure[key].append/extend A__ = _re_direct_key if '_import_structure' in block_lines[0] else _re_indirect_key # Grab the keys, but there is a trap: some lines are empty or just comments. A__ = [(pattern.search(__UpperCamelCase ).groups()[0] if pattern.search(__UpperCamelCase ) is not None else None) for b in internal_blocks] # We only sort the lines with a key. A__ = [(i, key) for i, key in enumerate(__UpperCamelCase ) if key is not None] A__ = [x[0] for x in sorted(__UpperCamelCase , key=lambda __UpperCamelCase : x[1] )] # We reorder the blocks by leaving empty lines/comments as they were and reorder the rest. A__ = 0 A__ = [] for i in range(len(__UpperCamelCase ) ): if keys[i] is None: reordered_blocks.append(internal_blocks[i] ) else: A__ = sort_objects_in_import(internal_blocks[sorted_indices[count]] ) reordered_blocks.append(__UpperCamelCase ) count += 1 # And we put our main block back together with its first and last line. A__ = '\n'.join(block_lines[:line_idx] + reordered_blocks + [block_lines[-1]] ) if code != "\n".join(__UpperCamelCase ): if check_only: return True else: print(f'''Overwriting {file}.''' ) with open(__UpperCamelCase , 'w' ) as f: f.write('\n'.join(__UpperCamelCase ) ) def A ( __UpperCamelCase=True ) -> Union[str, Any]: A__ = [] for root, _, files in os.walk(__UpperCamelCase ): if "__init__.py" in files: A__ = sort_imports(os.path.join(__UpperCamelCase , '__init__.py' ) , check_only=__UpperCamelCase ) if result: A__ = [os.path.join(__UpperCamelCase , '__init__.py' )] if len(__UpperCamelCase ) > 0: raise ValueError(f'''Would overwrite {len(__UpperCamelCase )} files, run `make style`.''' ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument('''--check_only''', action='''store_true''', help='''Whether to only check or fix style.''') SCREAMING_SNAKE_CASE__ = parser.parse_args() sort_imports_in_all_inits(check_only=args.check_only)
52
import inspect import unittest from transformers import ViTHybridConfig from transformers.testing_utils import require_accelerate, require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class __lowerCAmelCase : """simple docstring""" def __init__( self : List[Any] , _snake_case : Any , _snake_case : Optional[int]=13 , _snake_case : Optional[Any]=64 , _snake_case : List[str]=2 , _snake_case : Any=3 , _snake_case : Union[str, Any]=True , _snake_case : Dict=True , _snake_case : int=32 , _snake_case : int=5 , _snake_case : Union[str, Any]=4 , _snake_case : int=37 , _snake_case : Tuple="gelu" , _snake_case : Optional[int]=0.1 , _snake_case : Dict=0.1 , _snake_case : List[str]=10 , _snake_case : Union[str, Any]=0.02 , _snake_case : Dict=[1, 16, 4, 4] , _snake_case : Dict=None , ): """simple docstring""" A__ = parent A__ = batch_size A__ = image_size A__ = patch_size A__ = num_channels A__ = is_training A__ = use_labels A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = type_sequence_label_size A__ = initializer_range A__ = scope A__ = backbone_featmap_shape # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) # the number of patches is based on the feature map of the backbone, which by default uses an output stride # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size A__ = (self.image_size // 32) ** 2 A__ = num_patches + 1 def _a ( self : Any ): """simple docstring""" A__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A__ = self.get_config() return config, pixel_values, labels def _a ( self : Tuple ): """simple docstring""" A__ = { 'global_padding': 'same', 'layer_type': 'bottleneck', 'depths': [3, 4, 9], 'out_features': ['stage1', 'stage2', 'stage3'], 'embedding_dynamic_padding': True, 'hidden_sizes': [4, 8, 16, 32], 'num_groups': 2, } return ViTHybridConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_snake_case , initializer_range=self.initializer_range , backbone_featmap_shape=self.backbone_featmap_shape , backbone_config=_snake_case , ) def _a ( self : int , _snake_case : Optional[int] , _snake_case : Union[str, Any] , _snake_case : Optional[int] ): """simple docstring""" A__ = ViTHybridModel(config=_snake_case ) model.to(_snake_case ) model.eval() A__ = model(_snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _a ( self : List[str] , _snake_case : str , _snake_case : Union[str, Any] , _snake_case : Any ): """simple docstring""" A__ = self.type_sequence_label_size A__ = ViTHybridForImageClassification(_snake_case ) model.to(_snake_case ) model.eval() A__ = model(_snake_case , labels=_snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _a ( self : Dict ): """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ = config_and_inputs A__ = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class __lowerCAmelCase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Union[str, Any] = (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else () A__ : str = ( {"feature-extraction": ViTHybridModel, "image-classification": ViTHybridForImageClassification} if is_torch_available() else {} ) A__ : Union[str, Any] = False A__ : Any = False A__ : Union[str, Any] = False def _a ( self : Dict ): """simple docstring""" A__ = ViTHybridModelTester(self ) A__ = ConfigTester(self , config_class=_snake_case , has_text_modality=_snake_case , hidden_size=37 ) def _a ( self : int ): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='ViT does not use inputs_embeds' ) def _a ( self : int ): """simple docstring""" pass def _a ( self : int ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(_snake_case ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A__ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_snake_case , nn.Linear ) ) def _a ( self : List[str] ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(_snake_case ) A__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ = [*signature.parameters.keys()] A__ = ['pixel_values'] self.assertListEqual(arg_names[:1] , _snake_case ) def _a ( self : Any ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def _a ( self : str ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_snake_case ) def _a ( self : Any ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = _config_zero_init(_snake_case ) for model_class in self.all_model_classes: A__ = model_class(config=_snake_case ) # Skip the check for the backbone for name, module in model.named_modules(): if module.__class__.__name__ == "ViTHybridPatchEmbeddings": A__ = [F'''{name}.{key}''' for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() , [0.0, 1.0] , msg=F'''Parameter {name} of model {model_class} seems not properly initialized''' , ) @slow def _a ( self : int ): """simple docstring""" for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = ViTHybridModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def A ( ) -> Union[str, Any]: A__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @cached_property def _a ( self : Tuple ): """simple docstring""" return ( ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def _a ( self : Optional[Any] ): """simple docstring""" A__ = ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to( _snake_case ) A__ = self.default_image_processor A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ).to(_snake_case ) # forward pass with torch.no_grad(): A__ = model(**_snake_case ) # verify the logits A__ = torch.Size((1, 10_00) ) self.assertEqual(outputs.logits.shape , _snake_case ) A__ = torch.tensor([-1.9090, -0.4993, -0.2389] ).to(_snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _snake_case , atol=1E-4 ) ) @slow @require_accelerate def _a ( self : List[Any] ): """simple docstring""" A__ = ViTHybridImageProcessor.from_pretrained('google/vit-hybrid-base-bit-384' ) A__ = ViTHybridForImageClassification.from_pretrained('google/vit-hybrid-base-bit-384' , device_map='auto' ) A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ) A__ = model(**_snake_case ) A__ = outputs.logits # model predicts one of the 1000 ImageNet classes A__ = logits.argmax(-1 ).item() self.assertTrue(model.config.idalabel[predicted_class_idx] , 'tabby, tabby cat' )
52
1
from collections.abc import Iterator, MutableMapping from dataclasses import dataclass from typing import Generic, TypeVar SCREAMING_SNAKE_CASE__ = TypeVar('''KEY''') SCREAMING_SNAKE_CASE__ = TypeVar('''VAL''') @dataclass(frozen=UpperCAmelCase_ , slots=UpperCAmelCase_ ) class __lowerCAmelCase ( Generic[KEY, VAL] ): """simple docstring""" A__ : KEY A__ : VAL class __lowerCAmelCase ( _Item ): """simple docstring""" def __init__( self : int ): """simple docstring""" super().__init__(_snake_case , _snake_case ) def __bool__( self : List[str] ): """simple docstring""" return False SCREAMING_SNAKE_CASE__ = _DeletedItem() class __lowerCAmelCase ( MutableMapping[KEY, VAL] ): """simple docstring""" def __init__( self : List[Any] , _snake_case : int = 8 , _snake_case : float = 0.75 ): """simple docstring""" A__ = initial_block_size A__ = [None] * initial_block_size assert 0.0 < capacity_factor < 1.0 A__ = capacity_factor A__ = 0 def _a ( self : Dict , _snake_case : KEY ): """simple docstring""" return hash(_snake_case ) % len(self._buckets ) def _a ( self : Optional[Any] , _snake_case : int ): """simple docstring""" return (ind + 1) % len(self._buckets ) def _a ( self : Any , _snake_case : int , _snake_case : KEY , _snake_case : VAL ): """simple docstring""" A__ = self._buckets[ind] if not stored: A__ = _Item(_snake_case , _snake_case ) self._len += 1 return True elif stored.key == key: A__ = _Item(_snake_case , _snake_case ) return True else: return False def _a ( self : List[Any] ): """simple docstring""" A__ = len(self._buckets ) * self._capacity_factor return len(self ) >= int(_snake_case ) def _a ( self : Optional[int] ): """simple docstring""" if len(self._buckets ) <= self._initial_block_size: return False A__ = len(self._buckets ) * self._capacity_factor / 2 return len(self ) < limit def _a ( self : List[Any] , _snake_case : int ): """simple docstring""" A__ = self._buckets A__ = [None] * new_size A__ = 0 for item in old_buckets: if item: self._add_item(item.key , item.val ) def _a ( self : Optional[Any] ): """simple docstring""" self._resize(len(self._buckets ) * 2 ) def _a ( self : Dict ): """simple docstring""" self._resize(len(self._buckets ) // 2 ) def _a ( self : str , _snake_case : KEY ): """simple docstring""" A__ = self._get_bucket_index(_snake_case ) for _ in range(len(self._buckets ) ): yield ind A__ = self._get_next_ind(_snake_case ) def _a ( self : Any , _snake_case : KEY , _snake_case : VAL ): """simple docstring""" for ind in self._iterate_buckets(_snake_case ): if self._try_set(_snake_case , _snake_case , _snake_case ): break def __setitem__( self : Union[str, Any] , _snake_case : KEY , _snake_case : VAL ): """simple docstring""" if self._is_full(): self._size_up() self._add_item(_snake_case , _snake_case ) def __delitem__( self : List[str] , _snake_case : KEY ): """simple docstring""" for ind in self._iterate_buckets(_snake_case ): A__ = self._buckets[ind] if item is None: raise KeyError(_snake_case ) if item is _deleted: continue if item.key == key: A__ = _deleted self._len -= 1 break if self._is_sparse(): self._size_down() def __getitem__( self : List[Any] , _snake_case : KEY ): """simple docstring""" for ind in self._iterate_buckets(_snake_case ): A__ = self._buckets[ind] if item is None: break if item is _deleted: continue if item.key == key: return item.val raise KeyError(_snake_case ) def __len__( self : Any ): """simple docstring""" return self._len def __iter__( self : List[str] ): """simple docstring""" yield from (item.key for item in self._buckets if item) def __repr__( self : Optional[int] ): """simple docstring""" A__ = ' ,'.join( F'''{item.key}: {item.val}''' for item in self._buckets if item ) return F'''HashMap({val_string})'''
52
def A ( __UpperCamelCase ) -> bool: return number & 1 == 0 if __name__ == "__main__": import doctest doctest.testmod()
52
1
import argparse from pathlib import Path from typing import Dict, OrderedDict, Tuple import torch from audiocraft.models import MusicGen from transformers import ( AutoFeatureExtractor, AutoTokenizer, EncodecModel, MusicgenDecoderConfig, MusicgenForConditionalGeneration, MusicgenProcessor, TaEncoderModel, ) from transformers.models.musicgen.modeling_musicgen import MusicgenForCausalLM from transformers.utils import logging logging.set_verbosity_info() SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = ['''model.decoder.embed_positions.weights'''] def A ( __UpperCamelCase ) -> int: if "emb" in name: A__ = name.replace('emb' , 'model.decoder.embed_tokens' ) if "transformer" in name: A__ = name.replace('transformer' , 'model.decoder' ) if "cross_attention" in name: A__ = name.replace('cross_attention' , 'encoder_attn' ) if "linear1" in name: A__ = name.replace('linear1' , 'fc1' ) if "linear2" in name: A__ = name.replace('linear2' , 'fc2' ) if "norm1" in name: A__ = name.replace('norm1' , 'self_attn_layer_norm' ) if "norm_cross" in name: A__ = name.replace('norm_cross' , 'encoder_attn_layer_norm' ) if "norm2" in name: A__ = name.replace('norm2' , 'final_layer_norm' ) if "out_norm" in name: A__ = name.replace('out_norm' , 'model.decoder.layer_norm' ) if "linears" in name: A__ = name.replace('linears' , 'lm_heads' ) if "condition_provider.conditioners.description.output_proj" in name: A__ = name.replace('condition_provider.conditioners.description.output_proj' , 'enc_to_dec_proj' ) return name def A ( __UpperCamelCase , __UpperCamelCase ) -> Tuple[Dict, Dict]: A__ = list(state_dict.keys() ) A__ = {} for key in keys: A__ = state_dict.pop(__UpperCamelCase ) A__ = rename_keys(__UpperCamelCase ) if "in_proj_weight" in key: # split fused qkv proj A__ = val[:hidden_size, :] A__ = val[hidden_size : 2 * hidden_size, :] A__ = val[-hidden_size:, :] elif "enc_to_dec_proj" in key: A__ = val else: A__ = val return state_dict, enc_dec_proj_state_dict def A ( __UpperCamelCase ) -> MusicgenDecoderConfig: if checkpoint == "small": # default config values A__ = 1_024 A__ = 24 A__ = 16 elif checkpoint == "medium": A__ = 1_536 A__ = 48 A__ = 24 elif checkpoint == "large": A__ = 2_048 A__ = 48 A__ = 32 else: raise ValueError(f'''Checkpoint should be one of `[\'small\', \'medium\', \'large\']`, got {checkpoint}.''' ) A__ = MusicgenDecoderConfig( hidden_size=__UpperCamelCase , ffn_dim=hidden_size * 4 , num_hidden_layers=__UpperCamelCase , num_attention_heads=__UpperCamelCase , ) return config @torch.no_grad() def A ( __UpperCamelCase , __UpperCamelCase=None , __UpperCamelCase=None , __UpperCamelCase="cpu" ) -> Tuple: A__ = MusicGen.get_pretrained(__UpperCamelCase , device=__UpperCamelCase ) A__ = decoder_config_from_checkpoint(__UpperCamelCase ) A__ = fairseq_model.lm.state_dict() A__ , A__ = rename_state_dict( __UpperCamelCase , hidden_size=decoder_config.hidden_size ) A__ = TaEncoderModel.from_pretrained('t5-base' ) A__ = EncodecModel.from_pretrained('facebook/encodec_32khz' ) A__ = MusicgenForCausalLM(__UpperCamelCase ).eval() # load all decoder weights - expect that we'll be missing embeddings and enc-dec projection A__ , A__ = decoder.load_state_dict(__UpperCamelCase , strict=__UpperCamelCase ) for key in missing_keys.copy(): if key.startswith(('text_encoder', 'audio_encoder') ) or key in EXPECTED_MISSING_KEYS: missing_keys.remove(__UpperCamelCase ) if len(__UpperCamelCase ) > 0: raise ValueError(f'''Missing key(s) in state_dict: {missing_keys}''' ) if len(__UpperCamelCase ) > 0: raise ValueError(f'''Unexpected key(s) in state_dict: {unexpected_keys}''' ) # init the composite model A__ = MusicgenForConditionalGeneration(text_encoder=__UpperCamelCase , audio_encoder=__UpperCamelCase , decoder=__UpperCamelCase ) # load the pre-trained enc-dec projection (from the decoder state dict) model.enc_to_dec_proj.load_state_dict(__UpperCamelCase ) # check we can do a forward pass A__ = torch.arange(0 , 8 , dtype=torch.long ).reshape(2 , -1 ) A__ = input_ids.reshape(2 * 4 , -1 ) with torch.no_grad(): A__ = model(input_ids=__UpperCamelCase , decoder_input_ids=__UpperCamelCase ).logits if logits.shape != (8, 1, 2_048): raise ValueError('Incorrect shape for logits' ) # now construct the processor A__ = AutoTokenizer.from_pretrained('t5-base' ) A__ = AutoFeatureExtractor.from_pretrained('facebook/encodec_32khz' , padding_side='left' ) A__ = MusicgenProcessor(feature_extractor=__UpperCamelCase , tokenizer=__UpperCamelCase ) # set the appropriate bos/pad token ids A__ = 2_048 A__ = 2_048 # set other default generation config params A__ = int(30 * audio_encoder.config.frame_rate ) A__ = True A__ = 3.0 if pytorch_dump_folder is not None: Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) logger.info(f'''Saving model {checkpoint} to {pytorch_dump_folder}''' ) model.save_pretrained(__UpperCamelCase ) processor.save_pretrained(__UpperCamelCase ) if repo_id: logger.info(f'''Pushing model {checkpoint} to {repo_id}''' ) model.push_to_hub(__UpperCamelCase ) processor.push_to_hub(__UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint''', default='''small''', type=str, help='''Checkpoint size of the MusicGen model you\'d like to convert. Can be one of: `[\'small\', \'medium\', \'large\']`.''', ) parser.add_argument( '''--pytorch_dump_folder''', required=True, default=None, type=str, help='''Path to the output PyTorch model directory.''', ) parser.add_argument( '''--push_to_hub''', default=None, type=str, help='''Where to upload the converted model on the 🤗 hub.''' ) parser.add_argument( '''--device''', default='''cpu''', type=str, help='''Torch device to run the conversion, either cpu or cuda.''' ) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_musicgen_checkpoint(args.checkpoint, args.pytorch_dump_folder, args.push_to_hub)
52
from typing import Dict from .base import GenericTensor, Pipeline class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def _a ( self : Any , _snake_case : str=None , _snake_case : Dict=None , _snake_case : Any=None , **_snake_case : str ): """simple docstring""" if tokenize_kwargs is None: A__ = {} if truncation is not None: if "truncation" in tokenize_kwargs: raise ValueError( 'truncation parameter defined twice (given as keyword argument as well as in tokenize_kwargs)' ) A__ = truncation A__ = tokenize_kwargs A__ = {} if return_tensors is not None: A__ = return_tensors return preprocess_params, {}, postprocess_params def _a ( self : Any , _snake_case : Dict , **_snake_case : Optional[Any] ): """simple docstring""" A__ = self.framework A__ = self.tokenizer(_snake_case , return_tensors=_snake_case , **_snake_case ) return model_inputs def _a ( self : List[Any] , _snake_case : Dict ): """simple docstring""" A__ = self.model(**_snake_case ) return model_outputs def _a ( self : Optional[Any] , _snake_case : List[Any] , _snake_case : str=False ): """simple docstring""" if return_tensors: return model_outputs[0] if self.framework == "pt": return model_outputs[0].tolist() elif self.framework == "tf": return model_outputs[0].numpy().tolist() def __call__( self : Dict , *_snake_case : int , **_snake_case : List[str] ): """simple docstring""" return super().__call__(*_snake_case , **_snake_case )
52
1
import warnings from ...utils import logging from .image_processing_donut import DonutImageProcessor SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : str , *_snake_case : Union[str, Any] , **_snake_case : str ): """simple docstring""" warnings.warn( 'The class DonutFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please' ' use DonutImageProcessor instead.' , _snake_case , ) super().__init__(*_snake_case , **_snake_case )
52
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import numpy as np from utils_multiple_choice import MultipleChoiceDataset, Split, processors import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process SCREAMING_SNAKE_CASE__ = logging.getLogger(__name__) def A ( __UpperCamelCase , __UpperCamelCase ) -> List[Any]: return (preds == labels).mean() @dataclass class __lowerCAmelCase : """simple docstring""" A__ : str = field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "Pretrained config name or path if not the same as model_name"} ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"} ) A__ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) @dataclass class __lowerCAmelCase : """simple docstring""" A__ : str = field(metadata={"help": "The name of the task to train on: " + ", ".join(processors.keys() )} ) A__ : str = field(metadata={"help": "Should contain the data files for the task."} ) A__ : int = field( default=1_28 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) A__ : bool = field( default=UpperCAmelCase_ , metadata={"help": "Overwrite the cached training and evaluation sets"} ) def A ( ) -> Any: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. A__ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) A__ , A__ , A__ = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f'''Output directory ({training_args.output_dir}) already exists and is not empty. Use''' ' --overwrite_output_dir to overcome.' ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( 'Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s' , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.local_rank != -1 ) , training_args.fpaa , ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('Training/evaluation parameters %s' , __UpperCamelCase ) # Set seed set_seed(training_args.seed ) try: A__ = processors[data_args.task_name]() A__ = processor.get_labels() A__ = len(__UpperCamelCase ) except KeyError: raise ValueError('Task not found: %s' % (data_args.task_name) ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. A__ = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=__UpperCamelCase , finetuning_task=data_args.task_name , cache_dir=model_args.cache_dir , ) A__ = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) A__ = AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path , from_tf=bool('.ckpt' in model_args.model_name_or_path ) , config=__UpperCamelCase , cache_dir=model_args.cache_dir , ) # Get datasets A__ = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=__UpperCamelCase , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.train , ) if training_args.do_train else None ) A__ = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=__UpperCamelCase , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.dev , ) if training_args.do_eval else None ) def compute_metrics(__UpperCamelCase ) -> Dict: A__ = np.argmax(p.predictions , axis=1 ) return {"acc": simple_accuracy(__UpperCamelCase , p.label_ids )} # Data collator A__ = DataCollatorWithPadding(__UpperCamelCase , pad_to_multiple_of=8 ) if training_args.fpaa else None # Initialize our Trainer A__ = Trainer( model=__UpperCamelCase , args=__UpperCamelCase , train_dataset=__UpperCamelCase , eval_dataset=__UpperCamelCase , compute_metrics=__UpperCamelCase , data_collator=__UpperCamelCase , ) # Training if training_args.do_train: trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir ) # Evaluation A__ = {} if training_args.do_eval: logger.info('*** Evaluate ***' ) A__ = trainer.evaluate() A__ = os.path.join(training_args.output_dir , 'eval_results.txt' ) if trainer.is_world_master(): with open(__UpperCamelCase , 'w' ) as writer: logger.info('***** Eval results *****' ) for key, value in result.items(): logger.info(' %s = %s' , __UpperCamelCase , __UpperCamelCase ) writer.write('%s = %s\n' % (key, value) ) results.update(__UpperCamelCase ) return results def A ( __UpperCamelCase ) -> List[Any]: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
52
1
import numpy as np import torch import torch.nn as nn from transformers import CLIPConfig, CLIPVisionModelWithProjection, PreTrainedModel from ...utils import logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : List[Any] = CLIPConfig A__ : Dict = ["CLIPEncoderLayer"] def __init__( self : List[str] , _snake_case : CLIPConfig ): """simple docstring""" super().__init__(_snake_case ) A__ = CLIPVisionModelWithProjection(config.vision_config ) A__ = nn.Linear(config.vision_config.projection_dim , 1 ) A__ = nn.Linear(config.vision_config.projection_dim , 1 ) @torch.no_grad() def _a ( self : Dict , _snake_case : str , _snake_case : Union[str, Any] , _snake_case : Optional[Any]=0.5 , _snake_case : List[str]=0.5 ): """simple docstring""" A__ = self.vision_model(_snake_case )[0] A__ = self.p_head(_snake_case ) A__ = nsfw_detected.flatten() A__ = nsfw_detected > p_threshold A__ = nsfw_detected.tolist() if any(_snake_case ): logger.warning( 'Potential NSFW content was detected in one or more images. A black image will be returned instead.' ' Try again with a different prompt and/or seed.' ) for idx, nsfw_detected_ in enumerate(_snake_case ): if nsfw_detected_: A__ = np.zeros(images[idx].shape ) A__ = self.w_head(_snake_case ) A__ = watermark_detected.flatten() A__ = watermark_detected > w_threshold A__ = watermark_detected.tolist() if any(_snake_case ): logger.warning( 'Potential watermarked content was detected in one or more images. A black image will be returned instead.' ' Try again with a different prompt and/or seed.' ) for idx, watermark_detected_ in enumerate(_snake_case ): if watermark_detected_: A__ = np.zeros(images[idx].shape ) return images, nsfw_detected, watermark_detected
52
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available SCREAMING_SNAKE_CASE__ = {'''configuration_mra''': ['''MRA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MraConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''MRA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MraForMaskedLM''', '''MraForMultipleChoice''', '''MraForQuestionAnswering''', '''MraForSequenceClassification''', '''MraForTokenClassification''', '''MraLayer''', '''MraModel''', '''MraPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_mra import MRA_PRETRAINED_CONFIG_ARCHIVE_MAP, MraConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mra import ( MRA_PRETRAINED_MODEL_ARCHIVE_LIST, MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, MraLayer, MraModel, MraPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
52
1
from ...configuration_utils import PretrainedConfig from ...utils import logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = {} class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Optional[Any] = "llama" A__ : str = ["past_key_values"] def __init__( self : Union[str, Any] , _snake_case : str=3_20_00 , _snake_case : int=40_96 , _snake_case : Optional[int]=1_10_08 , _snake_case : int=32 , _snake_case : Optional[int]=32 , _snake_case : Dict=None , _snake_case : Tuple="silu" , _snake_case : Union[str, Any]=20_48 , _snake_case : Dict=0.02 , _snake_case : List[str]=1E-6 , _snake_case : str=True , _snake_case : int=0 , _snake_case : List[Any]=1 , _snake_case : Any=2 , _snake_case : List[str]=1 , _snake_case : Dict=False , _snake_case : Optional[Any]=None , **_snake_case : Any , ): """simple docstring""" A__ = vocab_size A__ = max_position_embeddings A__ = hidden_size A__ = intermediate_size A__ = num_hidden_layers A__ = num_attention_heads # for backward compatibility if num_key_value_heads is None: A__ = num_attention_heads A__ = num_key_value_heads A__ = hidden_act A__ = initializer_range A__ = rms_norm_eps A__ = pretraining_tp A__ = use_cache A__ = rope_scaling self._rope_scaling_validation() super().__init__( pad_token_id=_snake_case , bos_token_id=_snake_case , eos_token_id=_snake_case , tie_word_embeddings=_snake_case , **_snake_case , ) def _a ( self : List[str] ): """simple docstring""" if self.rope_scaling is None: return if not isinstance(self.rope_scaling , _snake_case ) or len(self.rope_scaling ) != 2: raise ValueError( '`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, ' F'''got {self.rope_scaling}''' ) A__ = self.rope_scaling.get('type' , _snake_case ) A__ = self.rope_scaling.get('factor' , _snake_case ) if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]: raise ValueError( F'''`rope_scaling`\'s name field must be one of [\'linear\', \'dynamic\'], got {rope_scaling_type}''' ) if rope_scaling_factor is None or not isinstance(_snake_case , _snake_case ) or rope_scaling_factor <= 1.0: raise ValueError(F'''`rope_scaling`\'s factor field must be an float > 1, got {rope_scaling_factor}''' )
52
import webbrowser from sys import argv from urllib.parse import parse_qs, quote import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = '''%20'''.join(argv[1:]) if len(argv) > 1 else quote(str(input('''Search: '''))) print('''Googling.....''') SCREAMING_SNAKE_CASE__ = f'https://www.google.com/search?q={query}&num=100' SCREAMING_SNAKE_CASE__ = requests.get( url, headers={'''User-Agent''': str(UserAgent().random)}, ) try: SCREAMING_SNAKE_CASE__ = ( BeautifulSoup(res.text, '''html.parser''') .find('''div''', attrs={'''class''': '''yuRUbf'''}) .find('''a''') .get('''href''') ) except AttributeError: SCREAMING_SNAKE_CASE__ = parse_qs( BeautifulSoup(res.text, '''html.parser''') .find('''div''', attrs={'''class''': '''kCrYT'''}) .find('''a''') .get('''href''') )['''url'''][0] webbrowser.open(link)
52
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) SCREAMING_SNAKE_CASE__ = { '''configuration_xlm_roberta''': [ '''XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XLMRobertaConfig''', '''XLMRobertaOnnxConfig''', ], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = ['''XLMRobertaTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = ['''XLMRobertaTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XLMRobertaForCausalLM''', '''XLMRobertaForMaskedLM''', '''XLMRobertaForMultipleChoice''', '''XLMRobertaForQuestionAnswering''', '''XLMRobertaForSequenceClassification''', '''XLMRobertaForTokenClassification''', '''XLMRobertaModel''', '''XLMRobertaPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXLMRobertaForCausalLM''', '''TFXLMRobertaForMaskedLM''', '''TFXLMRobertaForMultipleChoice''', '''TFXLMRobertaForQuestionAnswering''', '''TFXLMRobertaForSequenceClassification''', '''TFXLMRobertaForTokenClassification''', '''TFXLMRobertaModel''', '''TFXLMRobertaPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''FlaxXLMRobertaForMaskedLM''', '''FlaxXLMRobertaForCausalLM''', '''FlaxXLMRobertaForMultipleChoice''', '''FlaxXLMRobertaForQuestionAnswering''', '''FlaxXLMRobertaForSequenceClassification''', '''FlaxXLMRobertaForTokenClassification''', '''FlaxXLMRobertaModel''', '''FlaxXLMRobertaPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_xlm_roberta import ( XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMRobertaConfig, XLMRobertaOnnxConfig, ) try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlm_roberta import XLMRobertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlm_roberta_fast import XLMRobertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm_roberta import ( XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, XLMRobertaForCausalLM, XLMRobertaForMaskedLM, XLMRobertaForMultipleChoice, XLMRobertaForQuestionAnswering, XLMRobertaForSequenceClassification, XLMRobertaForTokenClassification, XLMRobertaModel, XLMRobertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlm_roberta import ( TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLMRobertaForCausalLM, TFXLMRobertaForMaskedLM, TFXLMRobertaForMultipleChoice, TFXLMRobertaForQuestionAnswering, TFXLMRobertaForSequenceClassification, TFXLMRobertaForTokenClassification, TFXLMRobertaModel, TFXLMRobertaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xlm_roberta import ( FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxXLMRobertaForCausalLM, FlaxXLMRobertaForMaskedLM, FlaxXLMRobertaForMultipleChoice, FlaxXLMRobertaForQuestionAnswering, FlaxXLMRobertaForSequenceClassification, FlaxXLMRobertaForTokenClassification, FlaxXLMRobertaModel, FlaxXLMRobertaPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
52
import random import unittest import torch from diffusers import IFInpaintingPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class __lowerCAmelCase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Any = IFInpaintingPipeline A__ : Dict = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"width", "height"} A__ : List[Any] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS A__ : Dict = PipelineTesterMixin.required_optional_params - {"latents"} def _a ( self : Any ): """simple docstring""" return self._get_dummy_components() def _a ( self : Optional[int] , _snake_case : Any , _snake_case : str=0 ): """simple docstring""" if str(_snake_case ).startswith('mps' ): A__ = torch.manual_seed(_snake_case ) else: A__ = torch.Generator(device=_snake_case ).manual_seed(_snake_case ) A__ = floats_tensor((1, 3, 32, 32) , rng=random.Random(_snake_case ) ).to(_snake_case ) A__ = floats_tensor((1, 3, 32, 32) , rng=random.Random(_snake_case ) ).to(_snake_case ) A__ = { 'prompt': 'A painting of a squirrel eating a burger', 'image': image, 'mask_image': mask_image, 'generator': generator, 'num_inference_steps': 2, 'output_type': 'numpy', } return inputs @unittest.skipIf( torch_device != 'cuda' or not is_xformers_available() , reason='XFormers attention is only available with CUDA and `xformers` installed' , ) def _a ( self : Dict ): """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) def _a ( self : int ): """simple docstring""" self._test_save_load_optional_components() @unittest.skipIf(torch_device != 'cuda' , reason='float16 requires CUDA' ) def _a ( self : Optional[int] ): """simple docstring""" super().test_save_load_floataa(expected_max_diff=1E-1 ) def _a ( self : List[str] ): """simple docstring""" self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def _a ( self : Dict ): """simple docstring""" self._test_save_load_local() def _a ( self : Optional[int] ): """simple docstring""" self._test_inference_batch_single_identical( expected_max_diff=1E-2 , )
52
1
import os import re import sys import traceback import warnings from pathlib import Path from typing import Dict, Optional, Union from uuid import uuida from huggingface_hub import HfFolder, ModelCard, ModelCardData, hf_hub_download, whoami from huggingface_hub.file_download import REGEX_COMMIT_HASH from huggingface_hub.utils import ( EntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError, is_jinja_available, ) from packaging import version from requests import HTTPError from .. import __version__ from .constants import ( DEPRECATED_REVISION_ARGS, DIFFUSERS_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, ) from .import_utils import ( ENV_VARS_TRUE_VALUES, _flax_version, _jax_version, _onnxruntime_version, _torch_version, is_flax_available, is_onnx_available, is_torch_available, ) from .logging import get_logger SCREAMING_SNAKE_CASE__ = get_logger(__name__) SCREAMING_SNAKE_CASE__ = Path(__file__).parent / '''model_card_template.md''' SCREAMING_SNAKE_CASE__ = uuida().hex SCREAMING_SNAKE_CASE__ = os.getenv('''HF_HUB_OFFLINE''', '''''').upper() in ENV_VARS_TRUE_VALUES SCREAMING_SNAKE_CASE__ = os.getenv('''DISABLE_TELEMETRY''', '''''').upper() in ENV_VARS_TRUE_VALUES SCREAMING_SNAKE_CASE__ = HUGGINGFACE_CO_RESOLVE_ENDPOINT + '''/api/telemetry/''' def A ( __UpperCamelCase = None ) -> str: A__ = f'''diffusers/{__version__}; python/{sys.version.split()[0]}; session_id/{SESSION_ID}''' if DISABLE_TELEMETRY or HF_HUB_OFFLINE: return ua + "; telemetry/off" if is_torch_available(): ua += f'''; torch/{_torch_version}''' if is_flax_available(): ua += f'''; jax/{_jax_version}''' ua += f'''; flax/{_flax_version}''' if is_onnx_available(): ua += f'''; onnxruntime/{_onnxruntime_version}''' # CI will set this value to True if os.environ.get('DIFFUSERS_IS_CI' , '' ).upper() in ENV_VARS_TRUE_VALUES: ua += "; is_ci/true" if isinstance(__UpperCamelCase , __UpperCamelCase ): ua += "; " + "; ".join(f'''{k}/{v}''' for k, v in user_agent.items() ) elif isinstance(__UpperCamelCase , __UpperCamelCase ): ua += "; " + user_agent return ua def A ( __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = None ) -> Optional[int]: if token is None: A__ = HfFolder.get_token() if organization is None: A__ = whoami(__UpperCamelCase )['name'] return f'''{username}/{model_id}''' else: return f'''{organization}/{model_id}''' def A ( __UpperCamelCase , __UpperCamelCase ) -> Dict: if not is_jinja_available(): raise ValueError( 'Modelcard rendering is based on Jinja templates.' ' Please make sure to have `jinja` installed before using `create_model_card`.' ' To install it, please run `pip install Jinja2`.' ) if hasattr(__UpperCamelCase , 'local_rank' ) and args.local_rank not in [-1, 0]: return A__ = args.hub_token if hasattr(__UpperCamelCase , 'hub_token' ) else None A__ = get_full_repo_name(__UpperCamelCase , token=__UpperCamelCase ) A__ = ModelCard.from_template( card_data=ModelCardData( # Card metadata object that will be converted to YAML block language='en' , license='apache-2.0' , library_name='diffusers' , tags=[] , datasets=args.dataset_name , metrics=[] , ) , template_path=__UpperCamelCase , model_name=__UpperCamelCase , repo_name=__UpperCamelCase , dataset_name=args.dataset_name if hasattr(__UpperCamelCase , 'dataset_name' ) else None , learning_rate=args.learning_rate , train_batch_size=args.train_batch_size , eval_batch_size=args.eval_batch_size , gradient_accumulation_steps=( args.gradient_accumulation_steps if hasattr(__UpperCamelCase , 'gradient_accumulation_steps' ) else None ) , adam_betaa=args.adam_betaa if hasattr(__UpperCamelCase , 'adam_beta1' ) else None , adam_betaa=args.adam_betaa if hasattr(__UpperCamelCase , 'adam_beta2' ) else None , adam_weight_decay=args.adam_weight_decay if hasattr(__UpperCamelCase , 'adam_weight_decay' ) else None , adam_epsilon=args.adam_epsilon if hasattr(__UpperCamelCase , 'adam_epsilon' ) else None , lr_scheduler=args.lr_scheduler if hasattr(__UpperCamelCase , 'lr_scheduler' ) else None , lr_warmup_steps=args.lr_warmup_steps if hasattr(__UpperCamelCase , 'lr_warmup_steps' ) else None , ema_inv_gamma=args.ema_inv_gamma if hasattr(__UpperCamelCase , 'ema_inv_gamma' ) else None , ema_power=args.ema_power if hasattr(__UpperCamelCase , 'ema_power' ) else None , ema_max_decay=args.ema_max_decay if hasattr(__UpperCamelCase , 'ema_max_decay' ) else None , mixed_precision=args.mixed_precision , ) A__ = os.path.join(args.output_dir , 'README.md' ) model_card.save(__UpperCamelCase ) def A ( __UpperCamelCase , __UpperCamelCase = None ) -> Optional[int]: if resolved_file is None or commit_hash is not None: return commit_hash A__ = str(Path(__UpperCamelCase ).as_posix() ) A__ = re.search(r'snapshots/([^/]+)/' , __UpperCamelCase ) if search is None: return None A__ = search.groups()[0] return commit_hash if REGEX_COMMIT_HASH.match(__UpperCamelCase ) else None # Old default cache path, potentially to be migrated. # This logic was more or less taken from `transformers`, with the following differences: # - Diffusers doesn't use custom environment variables to specify the cache path. # - There is no need to migrate the cache format, just move the files to the new location. SCREAMING_SNAKE_CASE__ = os.path.expanduser( os.getenv('''HF_HOME''', os.path.join(os.getenv('''XDG_CACHE_HOME''', '''~/.cache'''), '''huggingface''')) ) SCREAMING_SNAKE_CASE__ = os.path.join(hf_cache_home, '''diffusers''') def A ( __UpperCamelCase = None , __UpperCamelCase = None ) -> None: if new_cache_dir is None: A__ = DIFFUSERS_CACHE if old_cache_dir is None: A__ = old_diffusers_cache A__ = Path(__UpperCamelCase ).expanduser() A__ = Path(__UpperCamelCase ).expanduser() for old_blob_path in old_cache_dir.glob('**/blobs/*' ): if old_blob_path.is_file() and not old_blob_path.is_symlink(): A__ = new_cache_dir / old_blob_path.relative_to(__UpperCamelCase ) new_blob_path.parent.mkdir(parents=__UpperCamelCase , exist_ok=__UpperCamelCase ) os.replace(__UpperCamelCase , __UpperCamelCase ) try: os.symlink(__UpperCamelCase , __UpperCamelCase ) except OSError: logger.warning( 'Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded.' ) # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). SCREAMING_SNAKE_CASE__ = os.path.join(DIFFUSERS_CACHE, '''version_diffusers_cache.txt''') if not os.path.isfile(cache_version_file): SCREAMING_SNAKE_CASE__ = 0 else: with open(cache_version_file) as f: try: SCREAMING_SNAKE_CASE__ = int(f.read()) except ValueError: SCREAMING_SNAKE_CASE__ = 0 if cache_version < 1: SCREAMING_SNAKE_CASE__ = os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0 if old_cache_is_not_empty: logger.warning( '''The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your ''' '''existing cached models. This is a one-time operation, you can interrupt it or run it ''' '''later by calling `diffusers.utils.hub_utils.move_cache()`.''' ) try: move_cache() except Exception as e: SCREAMING_SNAKE_CASE__ = '''\n'''.join(traceback.format_tb(e.__traceback__)) logger.error( f'There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease ' '''file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole ''' '''message and we will do our best to help.''' ) if cache_version < 1: try: os.makedirs(DIFFUSERS_CACHE, exist_ok=True) with open(cache_version_file, '''w''') as f: f.write('''1''') except Exception: logger.warning( f'There was a problem when trying to write in your cache folder ({DIFFUSERS_CACHE}). Please, ensure ' '''the directory exists and can be written to.''' ) def A ( __UpperCamelCase , __UpperCamelCase = None ) -> str: if variant is not None: A__ = weights_name.split('.' ) A__ = splits[:-1] + [variant] + splits[-1:] A__ = '.'.join(__UpperCamelCase ) return weights_name def A ( __UpperCamelCase , *, __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=None , ) -> str: A__ = str(__UpperCamelCase ) if os.path.isfile(__UpperCamelCase ): return pretrained_model_name_or_path elif os.path.isdir(__UpperCamelCase ): if os.path.isfile(os.path.join(__UpperCamelCase , __UpperCamelCase ) ): # Load from a PyTorch checkpoint A__ = os.path.join(__UpperCamelCase , __UpperCamelCase ) return model_file elif subfolder is not None and os.path.isfile( os.path.join(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) ): A__ = os.path.join(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) return model_file else: raise EnvironmentError( f'''Error no file named {weights_name} found in directory {pretrained_model_name_or_path}.''' ) else: # 1. First check if deprecated way of loading from branches is used if ( revision in DEPRECATED_REVISION_ARGS and (weights_name == WEIGHTS_NAME or weights_name == SAFETENSORS_WEIGHTS_NAME) and version.parse(version.parse(__UpperCamelCase ).base_version ) >= version.parse('0.20.0' ) ): try: A__ = hf_hub_download( __UpperCamelCase , filename=_add_variant(__UpperCamelCase , __UpperCamelCase ) , cache_dir=__UpperCamelCase , force_download=__UpperCamelCase , proxies=__UpperCamelCase , resume_download=__UpperCamelCase , local_files_only=__UpperCamelCase , use_auth_token=__UpperCamelCase , user_agent=__UpperCamelCase , subfolder=__UpperCamelCase , revision=revision or commit_hash , ) warnings.warn( f'''Loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'` is deprecated. Loading instead from `revision=\'main\'` with `variant={revision}`. Loading model variants via `revision=\'{revision}\'` will be removed in diffusers v1. Please use `variant=\'{revision}\'` instead.''' , __UpperCamelCase , ) return model_file except: # noqa: E722 warnings.warn( f'''You are loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant=\'{revision}\'` instead. However, it appears that {pretrained_model_name_or_path} currently does not have a {_add_variant(__UpperCamelCase , __UpperCamelCase )} file in the \'main\' branch of {pretrained_model_name_or_path}. \n The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title \'{pretrained_model_name_or_path} is missing {_add_variant(__UpperCamelCase , __UpperCamelCase )}\' so that the correct variant file can be added.''' , __UpperCamelCase , ) try: # 2. Load model file as usual A__ = hf_hub_download( __UpperCamelCase , filename=__UpperCamelCase , cache_dir=__UpperCamelCase , force_download=__UpperCamelCase , proxies=__UpperCamelCase , resume_download=__UpperCamelCase , local_files_only=__UpperCamelCase , use_auth_token=__UpperCamelCase , user_agent=__UpperCamelCase , subfolder=__UpperCamelCase , revision=revision or commit_hash , ) return model_file except RepositoryNotFoundError: raise EnvironmentError( f'''{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier ''' 'listed on \'https://huggingface.co/models\'\nIf this is a private repository, make sure to pass a ' 'token having permission to this repo with `use_auth_token` or log in with `huggingface-cli ' 'login`.' ) except RevisionNotFoundError: raise EnvironmentError( f'''{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for ''' 'this model name. Check the model page at ' f'''\'https://huggingface.co/{pretrained_model_name_or_path}\' for available revisions.''' ) except EntryNotFoundError: raise EnvironmentError( f'''{pretrained_model_name_or_path} does not appear to have a file named {weights_name}.''' ) except HTTPError as err: raise EnvironmentError( f'''There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n{err}''' ) except ValueError: raise EnvironmentError( f'''We couldn\'t connect to \'{HUGGINGFACE_CO_RESOLVE_ENDPOINT}\' to load this model, couldn\'t find it''' f''' in the cached files and it looks like {pretrained_model_name_or_path} is not the path to a''' f''' directory containing a file named {weights_name} or''' ' \nCheckout your internet connection or see how to run the library in' ' offline mode at \'https://huggingface.co/docs/diffusers/installation#offline-mode\'.' ) except EnvironmentError: raise EnvironmentError( f'''Can\'t load the model for \'{pretrained_model_name_or_path}\'. If you were trying to load it from ''' '\'https://huggingface.co/models\', make sure you don\'t have a local directory with the same name. ' f'''Otherwise, make sure \'{pretrained_model_name_or_path}\' is the correct path to a directory ''' f'''containing a file named {weights_name}''' )
52
import inspect import jax import jax.lax as lax import jax.numpy as jnp from ..utils import add_start_docstrings from ..utils.logging import get_logger SCREAMING_SNAKE_CASE__ = get_logger(__name__) SCREAMING_SNAKE_CASE__ = r''' Args: input_ids (`jnp.ndarray` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`PreTrainedTokenizer`]. See [`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`] for details. [What are input IDs?](../glossary#input-ids) scores (`jnp.ndarray` of shape `(batch_size, config.vocab_size)`): Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search kwargs (`Dict[str, Any]`, *optional*): Additional logits processor specific kwargs. Return: `jnp.ndarray` of shape `(batch_size, config.vocab_size)`: The processed prediction scores. ''' class __lowerCAmelCase : """simple docstring""" @add_start_docstrings(_snake_case ) def __call__( self : Optional[int] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray ): """simple docstring""" raise NotImplementedError( F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''' ) class __lowerCAmelCase : """simple docstring""" @add_start_docstrings(_snake_case ) def __call__( self : List[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray ): """simple docstring""" raise NotImplementedError( F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''' ) class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" @add_start_docstrings(_snake_case ) def __call__( self : Any , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int , **_snake_case : Optional[int] ): """simple docstring""" for processor in self: A__ = inspect.signature(processor.__call__ ).parameters if len(_snake_case ) > 3: if not all(arg in kwargs for arg in list(function_args.keys() )[2:] ): raise ValueError( F'''Make sure that all the required parameters: {list(function_args.keys() )} for ''' F'''{processor.__class__} are passed to the logits processor.''' ) A__ = processor(_snake_case , _snake_case , _snake_case , **_snake_case ) else: A__ = processor(_snake_case , _snake_case , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Any , _snake_case : float ): """simple docstring""" if not isinstance(_snake_case , _snake_case ) or not (temperature > 0): raise ValueError(F'''`temperature` has to be a strictly positive float, but is {temperature}''' ) A__ = temperature def __call__( self : str , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = scores / self.temperature return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Optional[Any] , _snake_case : float , _snake_case : float = -float('Inf' ) , _snake_case : int = 1 ): """simple docstring""" if not isinstance(_snake_case , _snake_case ) or (top_p < 0 or top_p > 1.0): raise ValueError(F'''`top_p` has to be a float > 0 and < 1, but is {top_p}''' ) if not isinstance(_snake_case , _snake_case ) or (min_tokens_to_keep < 1): raise ValueError(F'''`min_tokens_to_keep` has to be a positive integer, but is {min_tokens_to_keep}''' ) A__ = top_p A__ = filter_value A__ = min_tokens_to_keep def __call__( self : str , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ , A__ = lax.top_k(_snake_case , scores.shape[-1] ) A__ = jnp.full_like(_snake_case , self.filter_value ) A__ = jax.nn.softmax(_snake_case , axis=-1 ).cumsum(axis=-1 ) A__ = cumulative_probs < self.top_p # include the token that is higher than top_p as well A__ = jnp.roll(_snake_case , 1 ) score_mask |= score_mask.at[:, 0].set(_snake_case ) # min tokens to keep A__ = score_mask.at[:, : self.min_tokens_to_keep].set(_snake_case ) A__ = jnp.where(_snake_case , _snake_case , _snake_case ) A__ = jax.lax.sort_key_val(_snake_case , _snake_case )[-1] return next_scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Union[str, Any] , _snake_case : int , _snake_case : float = -float('Inf' ) , _snake_case : int = 1 ): """simple docstring""" if not isinstance(_snake_case , _snake_case ) or top_k <= 0: raise ValueError(F'''`top_k` has to be a strictly positive integer, but is {top_k}''' ) A__ = max(_snake_case , _snake_case ) A__ = filter_value def __call__( self : Optional[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ , A__ = scores.shape A__ = jnp.full(batch_size * vocab_size , self.filter_value ) A__ = min(self.top_k , scores.shape[-1] ) # Safety check A__ , A__ = lax.top_k(_snake_case , _snake_case ) A__ = jnp.broadcast_to((jnp.arange(_snake_case ) * vocab_size)[:, None] , (batch_size, topk) ).flatten() A__ = topk_scores.flatten() A__ = topk_indices.flatten() + shift A__ = next_scores_flat.at[topk_indices_flat].set(_snake_case ) A__ = next_scores_flat.reshape(_snake_case , _snake_case ) return next_scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Any , _snake_case : int ): """simple docstring""" A__ = bos_token_id def __call__( self : Optional[int] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = jnp.full(scores.shape , -float('inf' ) ) A__ = 1 - jnp.bool_(cur_len - 1 ) A__ = jnp.where(_snake_case , new_scores.at[:, self.bos_token_id].set(0 ) , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Any , _snake_case : int , _snake_case : int ): """simple docstring""" A__ = max_length A__ = eos_token_id def __call__( self : List[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = jnp.full(scores.shape , -float('inf' ) ) A__ = 1 - jnp.bool_(cur_len - self.max_length + 1 ) A__ = jnp.where(_snake_case , new_scores.at[:, self.eos_token_id].set(0 ) , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Dict , _snake_case : int , _snake_case : int ): """simple docstring""" if not isinstance(_snake_case , _snake_case ) or min_length < 0: raise ValueError(F'''`min_length` has to be a positive integer, but is {min_length}''' ) if not isinstance(_snake_case , _snake_case ) or eos_token_id < 0: raise ValueError(F'''`eos_token_id` has to be a positive integer, but is {eos_token_id}''' ) A__ = min_length A__ = eos_token_id def __call__( self : int , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = 1 - jnp.clip(cur_len - self.min_length , 0 , 1 ) A__ = jnp.where(_snake_case , scores.at[:, self.eos_token_id].set(-float('inf' ) ) , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : int , _snake_case : Tuple , _snake_case : Union[str, Any] ): """simple docstring""" A__ = list(_snake_case ) A__ = begin_index def __call__( self : Union[str, Any] , _snake_case : Optional[Any] , _snake_case : str , _snake_case : int ): """simple docstring""" A__ = 1 - jnp.bool_(cur_len - self.begin_index ) A__ = jnp.where(_snake_case , scores.at[:, self.begin_suppress_tokens].set(-float('inf' ) ) , _snake_case ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : int , _snake_case : list ): """simple docstring""" A__ = list(_snake_case ) def __call__( self : List[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" A__ = scores.at[..., self.suppress_tokens].set(-float('inf' ) ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : List[str] , _snake_case : Optional[Any] ): """simple docstring""" A__ = dict(_snake_case ) # Converts the dictionary of format {index: token} containing the tokens to be forced to an array, where the # index of the array corresponds to the index of the token to be forced, for XLA compatibility. # Indexes without forced tokens will have a negative value. A__ = jnp.ones((max(force_token_map.keys() ) + 1) , dtype=jnp.intaa ) * -1 for index, token in force_token_map.items(): if token is not None: A__ = force_token_array.at[index].set(_snake_case ) A__ = jnp.intaa(_snake_case ) def __call__( self : List[Any] , _snake_case : jnp.ndarray , _snake_case : jnp.ndarray , _snake_case : int ): """simple docstring""" def _force_token(_snake_case : Dict ): A__ = scores.shape[0] A__ = self.force_token_array[generation_idx] A__ = jnp.ones_like(_snake_case , dtype=scores.dtype ) * -float('inf' ) A__ = jnp.zeros((batch_size, 1) , dtype=scores.dtype ) A__ = lax.dynamic_update_slice(_snake_case , _snake_case , (0, current_token) ) return new_scores A__ = lax.cond( cur_len >= self.force_token_array.shape[0] , lambda: scores , lambda: lax.cond( self.force_token_array[cur_len] >= 0 , lambda: _force_token(_snake_case ) , lambda: scores , ) , ) return scores class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Union[str, Any] , _snake_case : Union[str, Any] , _snake_case : Dict , _snake_case : List[Any] ): """simple docstring""" A__ = generate_config.eos_token_id A__ = generate_config.no_timestamps_token_id A__ = generate_config.no_timestamps_token_id + 1 A__ = decoder_input_length + 1 if generate_config.is_multilingual: # room for language token and task token self.begin_index += 2 if hasattr(_snake_case , 'max_initial_timestamp_index' ): A__ = generate_config.max_initial_timestamp_index else: A__ = model_config.vocab_size if self.max_initial_timestamp_index is None: A__ = model_config.vocab_size def __call__( self : Tuple , _snake_case : List[Any] , _snake_case : Dict , _snake_case : Dict ): """simple docstring""" A__ = scores.at[:, self.no_timestamps_token_id].set(-float('inf' ) ) def handle_pairs(_snake_case : Dict , _snake_case : str ): A__ = jnp.where((cur_len - self.begin_index) >= 1 , _snake_case , _snake_case ) A__ = jnp.where( input_ids_k[cur_len - 1] >= self.timestamp_begin , True and last_was_timestamp , _snake_case , ) A__ = jnp.where((cur_len - self.begin_index) < 2 , _snake_case , _snake_case ) A__ = jnp.where( input_ids_k[cur_len - 2] >= self.timestamp_begin , _snake_case , _snake_case , ) return jnp.where( _snake_case , jnp.where( penultimate_was_timestamp > 0 , scores_k.at[self.timestamp_begin :].set(-float('inf' ) ) , scores_k.at[: self.eos_token_id].set(-float('inf' ) ) , ) , _snake_case , ) A__ = jax.vmap(_snake_case )(_snake_case , _snake_case ) A__ = jnp.where(cur_len == self.begin_index , _snake_case , _snake_case ) A__ = jnp.where( self.max_initial_timestamp_index is not None , True and apply_max_initial_timestamp , _snake_case , ) A__ = self.timestamp_begin + self.max_initial_timestamp_index A__ = jnp.where( _snake_case , scores.at[:, last_allowed + 1 :].set(-float('inf' ) ) , _snake_case , ) # if sum of probability over timestamps is above any other token, sample timestamp A__ = jax.nn.log_softmax(_snake_case , axis=-1 ) def handle_cumulative_probs(_snake_case : List[Any] , _snake_case : Union[str, Any] ): A__ = jax.nn.logsumexp(logprobs_k[self.timestamp_begin :] , axis=-1 ) A__ = jnp.max(logprobs_k[: self.timestamp_begin] ) return jnp.where( timestamp_logprob > max_text_token_logprob , scores_k.at[: self.timestamp_begin].set(-float('inf' ) ) , _snake_case , ) A__ = jax.vmap(_snake_case )(_snake_case , _snake_case ) return scores
52
1
import argparse from pathlib import Path from transformers import AutoConfig, AutoTokenizer, RagConfig, RagSequenceForGeneration, RagTokenForGeneration def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = None , ) -> Any: if config_name_or_path is None: A__ = 'facebook/rag-token-base' if model_type == 'rag_token' else 'facebook/rag-sequence-base' if generator_tokenizer_name_or_path is None: A__ = generator_name_or_path if question_encoder_tokenizer_name_or_path is None: A__ = question_encoder_name_or_path A__ = RagTokenForGeneration if model_type == 'rag_token' else RagSequenceForGeneration # Save model. A__ = RagConfig.from_pretrained(__UpperCamelCase ) A__ = AutoConfig.from_pretrained(__UpperCamelCase ) A__ = AutoConfig.from_pretrained(__UpperCamelCase ) A__ = gen_config A__ = question_encoder_config A__ = model_class.from_pretrained_question_encoder_generator( __UpperCamelCase , __UpperCamelCase , config=__UpperCamelCase ) rag_model.save_pretrained(__UpperCamelCase ) # Sanity check. model_class.from_pretrained(__UpperCamelCase ) # Save tokenizers. A__ = AutoTokenizer.from_pretrained(__UpperCamelCase ) gen_tokenizer.save_pretrained(dest_dir / 'generator_tokenizer/' ) A__ = AutoTokenizer.from_pretrained(__UpperCamelCase ) question_encoder_tokenizer.save_pretrained(dest_dir / 'question_encoder_tokenizer/' ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument( '''--model_type''', choices=['''rag_sequence''', '''rag_token'''], required=True, type=str, help='''RAG model type: rag_sequence, rag_token''', ) parser.add_argument('''--dest''', type=str, required=True, help='''Path to the output checkpoint directory.''') parser.add_argument('''--generator_name_or_path''', type=str, required=True, help='''Generator model identifier''') parser.add_argument( '''--question_encoder_name_or_path''', type=str, required=True, help='''Question encoder model identifier''' ) parser.add_argument( '''--generator_tokenizer_name_or_path''', type=str, help='''Generator tokenizer identifier, if not specified, resolves to ``generator_name_or_path``''', ) parser.add_argument( '''--question_encoder_tokenizer_name_or_path''', type=str, help='''Question encoder tokenizer identifier, if not specified, resolves to ``question_encoder_name_or_path``''', ) parser.add_argument( '''--config_name_or_path''', type=str, help=( '''Identifier of the model config to use, if not provided, resolves to a base config for a given''' ''' ``model_type``''' ), ) SCREAMING_SNAKE_CASE__ = parser.parse_args() SCREAMING_SNAKE_CASE__ = Path(args.dest) dest_dir.mkdir(exist_ok=True) consolidate( args.model_type, args.generator_name_or_path, args.question_encoder_name_or_path, dest_dir, args.config_name_or_path, args.generator_tokenizer_name_or_path, args.question_encoder_tokenizer_name_or_path, )
52
import argparse import struct import unittest class __lowerCAmelCase : """simple docstring""" def __init__( self : List[str] , _snake_case : bytes ): """simple docstring""" A__ = data # Initialize hash values A__ = [ 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19, ] # Initialize round constants A__ = [ 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA, 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967, 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070, 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3, 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2, ] A__ = self.preprocessing(self.data ) self.final_hash() @staticmethod def _a ( _snake_case : bytes ): """simple docstring""" A__ = B'\x80' + (B'\x00' * (63 - (len(_snake_case ) + 8) % 64)) A__ = struct.pack('>Q' , (len(_snake_case ) * 8) ) return data + padding + big_endian_integer def _a ( self : Optional[int] ): """simple docstring""" A__ = [ self.preprocessed_data[x : x + 64] for x in range(0 , len(self.preprocessed_data ) , 64 ) ] for block in self.blocks: # Convert the given block into a list of 4 byte integers A__ = list(struct.unpack('>16L' , _snake_case ) ) # add 48 0-ed integers words += [0] * 48 A__ , A__ , A__ , A__ , A__ , A__ , A__ , A__ = self.hashes for index in range(0 , 64 ): if index > 15: # modify the zero-ed indexes at the end of the array A__ = ( self.ror(words[index - 15] , 7 ) ^ self.ror(words[index - 15] , 18 ) ^ (words[index - 15] >> 3) ) A__ = ( self.ror(words[index - 2] , 17 ) ^ self.ror(words[index - 2] , 19 ) ^ (words[index - 2] >> 10) ) A__ = ( words[index - 16] + sa + words[index - 7] + sa ) % 0x100000000 # Compression A__ = self.ror(_snake_case , 6 ) ^ self.ror(_snake_case , 11 ) ^ self.ror(_snake_case , 25 ) A__ = (e & f) ^ ((~e & 0xFFFFFFFF) & g) A__ = ( h + sa + ch + self.round_constants[index] + words[index] ) % 0x100000000 A__ = self.ror(_snake_case , 2 ) ^ self.ror(_snake_case , 13 ) ^ self.ror(_snake_case , 22 ) A__ = (a & b) ^ (a & c) ^ (b & c) A__ = (sa + maj) % 0x100000000 A__ , A__ , A__ , A__ , A__ , A__ , A__ , A__ = ( g, f, e, ((d + tempa) % 0x100000000), c, b, a, ((tempa + tempa) % 0x100000000), ) A__ = [a, b, c, d, e, f, g, h] # Modify final values A__ = [ ((element + mutated_hash_values[index]) % 0x100000000) for index, element in enumerate(self.hashes ) ] A__ = ''.join([hex(_snake_case )[2:].zfill(8 ) for value in self.hashes] ) def _a ( self : Dict , _snake_case : int , _snake_case : int ): """simple docstring""" return 0xFFFFFFFF & (value << (32 - rotations)) | (value >> rotations) class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def _a ( self : str ): """simple docstring""" import hashlib A__ = bytes('Test String' , 'utf-8' ) self.assertEqual(SHAaaa(_snake_case ).hash , hashlib.shaaaa(_snake_case ).hexdigest() ) def A ( ) -> None: import doctest doctest.testmod() A__ = argparse.ArgumentParser() parser.add_argument( '-s' , '--string' , dest='input_string' , default='Hello World!! Welcome to Cryptography' , help='Hash the string' , ) parser.add_argument( '-f' , '--file' , dest='input_file' , help='Hash contents of a file' ) A__ = parser.parse_args() A__ = args.input_string # hash input should be a bytestring if args.input_file: with open(args.input_file , 'rb' ) as f: A__ = f.read() else: A__ = bytes(__UpperCamelCase , 'utf-8' ) print(SHAaaa(__UpperCamelCase ).hash ) if __name__ == "__main__": main()
52
1
def A ( __UpperCamelCase ) -> int: A__ = abs(__UpperCamelCase ) A__ = 0 while n > 0: res += n % 10 n //= 10 return res def A ( __UpperCamelCase ) -> int: A__ = abs(__UpperCamelCase ) return n if n < 10 else n % 10 + sum_of_digits(n // 10 ) def A ( __UpperCamelCase ) -> int: return sum(int(__UpperCamelCase ) for c in str(abs(__UpperCamelCase ) ) ) def A ( ) -> None: from collections.abc import Callable from timeit import timeit def benchmark_a_function(__UpperCamelCase , __UpperCamelCase ) -> None: A__ = f'''{func.__name__}({value})''' A__ = timeit(f'''__main__.{call}''' , setup='import __main__' ) print(f'''{call:56} = {func(__UpperCamelCase )} -- {timing:.4f} seconds''' ) for value in (262_144, 1_125_899_906_842_624, 1_267_650_600_228_229_401_496_703_205_376): for func in (sum_of_digits, sum_of_digits_recursion, sum_of_digits_compact): benchmark_a_function(__UpperCamelCase , __UpperCamelCase ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
52
import math import random def A ( __UpperCamelCase , __UpperCamelCase = False ) -> float: if deriv: return value * (1 - value) return 1 / (1 + math.exp(-value )) # Initial Value SCREAMING_SNAKE_CASE__ = 0.02 def A ( __UpperCamelCase , __UpperCamelCase ) -> float: A__ = float(2 * (random.randint(1 , 100 )) - 1 ) for _ in range(__UpperCamelCase ): # Forward propagation A__ = sigmoid_function(INITIAL_VALUE * weight ) # How much did we miss? A__ = (expected / 100) - layer_a # Error delta A__ = layer_1_error * sigmoid_function(__UpperCamelCase , __UpperCamelCase ) # Update weight weight += INITIAL_VALUE * layer_1_delta return layer_a * 100 if __name__ == "__main__": import doctest doctest.testmod() SCREAMING_SNAKE_CASE__ = int(input('''Expected value: ''')) SCREAMING_SNAKE_CASE__ = int(input('''Number of propagations: ''')) print(forward_propagation(expected, number_propagations))
52
1
import contextlib import copy import random from typing import Any, Dict, Iterable, Optional, Union import numpy as np import torch from .utils import deprecate, is_transformers_available if is_transformers_available(): import transformers def A ( __UpperCamelCase ) -> str: random.seed(__UpperCamelCase ) np.random.seed(__UpperCamelCase ) torch.manual_seed(__UpperCamelCase ) torch.cuda.manual_seed_all(__UpperCamelCase ) # ^^ safe to call this function even if cuda is not available class __lowerCAmelCase : """simple docstring""" def __init__( self : Any , _snake_case : Iterable[torch.nn.Parameter] , _snake_case : float = 0.9999 , _snake_case : float = 0.0 , _snake_case : int = 0 , _snake_case : bool = False , _snake_case : Union[float, int] = 1.0 , _snake_case : Union[float, int] = 2 / 3 , _snake_case : Optional[Any] = None , _snake_case : Dict[str, Any] = None , **_snake_case : List[Any] , ): """simple docstring""" if isinstance(_snake_case , torch.nn.Module ): A__ = ( 'Passing a `torch.nn.Module` to `ExponentialMovingAverage` is deprecated. ' 'Please pass the parameters of the module instead.' ) deprecate( 'passing a `torch.nn.Module` to `ExponentialMovingAverage`' , '1.0.0' , _snake_case , standard_warn=_snake_case , ) A__ = parameters.parameters() # set use_ema_warmup to True if a torch.nn.Module is passed for backwards compatibility A__ = True if kwargs.get('max_value' , _snake_case ) is not None: A__ = 'The `max_value` argument is deprecated. Please use `decay` instead.' deprecate('max_value' , '1.0.0' , _snake_case , standard_warn=_snake_case ) A__ = kwargs['max_value'] if kwargs.get('min_value' , _snake_case ) is not None: A__ = 'The `min_value` argument is deprecated. Please use `min_decay` instead.' deprecate('min_value' , '1.0.0' , _snake_case , standard_warn=_snake_case ) A__ = kwargs['min_value'] A__ = list(_snake_case ) A__ = [p.clone().detach() for p in parameters] if kwargs.get('device' , _snake_case ) is not None: A__ = 'The `device` argument is deprecated. Please use `to` instead.' deprecate('device' , '1.0.0' , _snake_case , standard_warn=_snake_case ) self.to(device=kwargs['device'] ) A__ = None A__ = decay A__ = min_decay A__ = update_after_step A__ = use_ema_warmup A__ = inv_gamma A__ = power A__ = 0 A__ = None # set in `step()` A__ = model_cls A__ = model_config @classmethod def _a ( cls : Optional[Any] , _snake_case : Union[str, Any] , _snake_case : str ): """simple docstring""" A__ , A__ = model_cls.load_config(_snake_case , return_unused_kwargs=_snake_case ) A__ = model_cls.from_pretrained(_snake_case ) A__ = cls(model.parameters() , model_cls=_snake_case , model_config=model.config ) ema_model.load_state_dict(_snake_case ) return ema_model def _a ( self : str , _snake_case : Dict ): """simple docstring""" if self.model_cls is None: raise ValueError('`save_pretrained` can only be used if `model_cls` was defined at __init__.' ) if self.model_config is None: raise ValueError('`save_pretrained` can only be used if `model_config` was defined at __init__.' ) A__ = self.model_cls.from_config(self.model_config ) A__ = self.state_dict() state_dict.pop('shadow_params' , _snake_case ) model.register_to_config(**_snake_case ) self.copy_to(model.parameters() ) model.save_pretrained(_snake_case ) def _a ( self : List[Any] , _snake_case : int ): """simple docstring""" A__ = max(0 , optimization_step - self.update_after_step - 1 ) if step <= 0: return 0.0 if self.use_ema_warmup: A__ = 1 - (1 + step / self.inv_gamma) ** -self.power else: A__ = (1 + step) / (10 + step) A__ = min(_snake_case , self.decay ) # make sure decay is not smaller than min_decay A__ = max(_snake_case , self.min_decay ) return cur_decay_value @torch.no_grad() def _a ( self : int , _snake_case : Iterable[torch.nn.Parameter] ): """simple docstring""" if isinstance(_snake_case , torch.nn.Module ): A__ = ( 'Passing a `torch.nn.Module` to `ExponentialMovingAverage.step` is deprecated. ' 'Please pass the parameters of the module instead.' ) deprecate( 'passing a `torch.nn.Module` to `ExponentialMovingAverage.step`' , '1.0.0' , _snake_case , standard_warn=_snake_case , ) A__ = parameters.parameters() A__ = list(_snake_case ) self.optimization_step += 1 # Compute the decay factor for the exponential moving average. A__ = self.get_decay(self.optimization_step ) A__ = decay A__ = 1 - decay A__ = contextlib.nullcontext if is_transformers_available() and transformers.deepspeed.is_deepspeed_zeroa_enabled(): import deepspeed for s_param, param in zip(self.shadow_params , _snake_case ): if is_transformers_available() and transformers.deepspeed.is_deepspeed_zeroa_enabled(): A__ = deepspeed.zero.GatheredParameters(_snake_case , modifier_rank=_snake_case ) with context_manager(): if param.requires_grad: s_param.sub_(one_minus_decay * (s_param - param) ) else: s_param.copy_(_snake_case ) def _a ( self : Dict , _snake_case : Iterable[torch.nn.Parameter] ): """simple docstring""" A__ = list(_snake_case ) for s_param, param in zip(self.shadow_params , _snake_case ): param.data.copy_(s_param.to(param.device ).data ) def _a ( self : Optional[int] , _snake_case : Optional[int]=None , _snake_case : List[str]=None ): """simple docstring""" A__ = [ p.to(device=_snake_case , dtype=_snake_case ) if p.is_floating_point() else p.to(device=_snake_case ) for p in self.shadow_params ] def _a ( self : Union[str, Any] ): """simple docstring""" return { "decay": self.decay, "min_decay": self.min_decay, "optimization_step": self.optimization_step, "update_after_step": self.update_after_step, "use_ema_warmup": self.use_ema_warmup, "inv_gamma": self.inv_gamma, "power": self.power, "shadow_params": self.shadow_params, } def _a ( self : Optional[Any] , _snake_case : Iterable[torch.nn.Parameter] ): """simple docstring""" A__ = [param.detach().cpu().clone() for param in parameters] def _a ( self : Tuple , _snake_case : Iterable[torch.nn.Parameter] ): """simple docstring""" if self.temp_stored_params is None: raise RuntimeError('This ExponentialMovingAverage has no `store()`ed weights ' 'to `restore()`' ) for c_param, param in zip(self.temp_stored_params , _snake_case ): param.data.copy_(c_param.data ) # Better memory-wise. A__ = None def _a ( self : Optional[int] , _snake_case : dict ): """simple docstring""" A__ = copy.deepcopy(_snake_case ) A__ = state_dict.get('decay' , self.decay ) if self.decay < 0.0 or self.decay > 1.0: raise ValueError('Decay must be between 0 and 1' ) A__ = state_dict.get('min_decay' , self.min_decay ) if not isinstance(self.min_decay , _snake_case ): raise ValueError('Invalid min_decay' ) A__ = state_dict.get('optimization_step' , self.optimization_step ) if not isinstance(self.optimization_step , _snake_case ): raise ValueError('Invalid optimization_step' ) A__ = state_dict.get('update_after_step' , self.update_after_step ) if not isinstance(self.update_after_step , _snake_case ): raise ValueError('Invalid update_after_step' ) A__ = state_dict.get('use_ema_warmup' , self.use_ema_warmup ) if not isinstance(self.use_ema_warmup , _snake_case ): raise ValueError('Invalid use_ema_warmup' ) A__ = state_dict.get('inv_gamma' , self.inv_gamma ) if not isinstance(self.inv_gamma , (float, int) ): raise ValueError('Invalid inv_gamma' ) A__ = state_dict.get('power' , self.power ) if not isinstance(self.power , (float, int) ): raise ValueError('Invalid power' ) A__ = state_dict.get('shadow_params' , _snake_case ) if shadow_params is not None: A__ = shadow_params if not isinstance(self.shadow_params , _snake_case ): raise ValueError('shadow_params must be a list' ) if not all(isinstance(_snake_case , torch.Tensor ) for p in self.shadow_params ): raise ValueError('shadow_params must all be Tensors' )
52
import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @slow def _a ( self : int ): """simple docstring""" A__ = FlaxMTaForConditionalGeneration.from_pretrained('google/mt5-small' ) A__ = AutoTokenizer.from_pretrained('google/mt5-small' ) A__ = tokenizer('Hello there' , return_tensors='np' ).input_ids A__ = tokenizer('Hi I am' , return_tensors='np' ).input_ids A__ = shift_tokens_right(_snake_case , model.config.pad_token_id , model.config.decoder_start_token_id ) A__ = model(_snake_case , decoder_input_ids=_snake_case ).logits A__ = optax.softmax_cross_entropy(_snake_case , onehot(_snake_case , logits.shape[-1] ) ).mean() A__ = -(labels.shape[-1] * loss.item()) A__ = -84.9127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1E-4 )
52
1
SCREAMING_SNAKE_CASE__ = ''' # Transformers installation ! pip install transformers datasets # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git ''' SCREAMING_SNAKE_CASE__ = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] SCREAMING_SNAKE_CASE__ = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
52
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/config.json''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/config.json''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/config.json''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/config.json''', '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/config.json''', '''roberta-large-openai-detector''': '''https://huggingface.co/roberta-large-openai-detector/resolve/main/config.json''', } class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : List[str] = "roberta" def __init__( self : List[str] , _snake_case : Union[str, Any]=5_02_65 , _snake_case : List[Any]=7_68 , _snake_case : List[str]=12 , _snake_case : List[str]=12 , _snake_case : Any=30_72 , _snake_case : Union[str, Any]="gelu" , _snake_case : int=0.1 , _snake_case : Union[str, Any]=0.1 , _snake_case : Tuple=5_12 , _snake_case : Union[str, Any]=2 , _snake_case : Any=0.02 , _snake_case : Any=1E-12 , _snake_case : List[Any]=1 , _snake_case : int=0 , _snake_case : Any=2 , _snake_case : Optional[Any]="absolute" , _snake_case : int=True , _snake_case : Any=None , **_snake_case : Any , ): """simple docstring""" super().__init__(pad_token_id=_snake_case , bos_token_id=_snake_case , eos_token_id=_snake_case , **_snake_case ) 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__ = use_cache A__ = classifier_dropout class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" @property def _a ( self : Dict ): """simple docstring""" if self.task == "multiple-choice": A__ = {0: 'batch', 1: 'choice', 2: 'sequence'} else: A__ = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ] )
52
1
import unittest import numpy as np from transformers.testing_utils import is_flaky, require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DonutImageProcessor class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def __init__( self : Optional[Any] , _snake_case : List[str] , _snake_case : int=7 , _snake_case : Tuple=3 , _snake_case : Any=18 , _snake_case : str=30 , _snake_case : Union[str, Any]=4_00 , _snake_case : List[str]=True , _snake_case : int=None , _snake_case : List[str]=True , _snake_case : Union[str, Any]=False , _snake_case : Dict=True , _snake_case : Tuple=True , _snake_case : Optional[int]=[0.5, 0.5, 0.5] , _snake_case : Dict=[0.5, 0.5, 0.5] , ): """simple docstring""" A__ = parent A__ = batch_size A__ = num_channels A__ = image_size A__ = min_resolution A__ = max_resolution A__ = do_resize A__ = size if size is not None else {'height': 18, 'width': 20} A__ = do_thumbnail A__ = do_align_axis A__ = do_pad A__ = do_normalize A__ = image_mean A__ = image_std def _a ( self : Union[str, Any] ): """simple docstring""" return { "do_resize": self.do_resize, "size": self.size, "do_thumbnail": self.do_thumbnail, "do_align_long_axis": self.do_align_axis, "do_pad": self.do_pad, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class __lowerCAmelCase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Union[str, Any] = DonutImageProcessor if is_vision_available() else None def _a ( self : str ): """simple docstring""" A__ = DonutImageProcessingTester(self ) @property def _a ( self : Union[str, Any] ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def _a ( self : int ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_snake_case , 'do_resize' ) ) self.assertTrue(hasattr(_snake_case , 'size' ) ) self.assertTrue(hasattr(_snake_case , 'do_thumbnail' ) ) self.assertTrue(hasattr(_snake_case , 'do_align_long_axis' ) ) self.assertTrue(hasattr(_snake_case , 'do_pad' ) ) self.assertTrue(hasattr(_snake_case , 'do_normalize' ) ) self.assertTrue(hasattr(_snake_case , 'image_mean' ) ) self.assertTrue(hasattr(_snake_case , 'image_std' ) ) def _a ( self : Optional[int] ): """simple docstring""" A__ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'height': 18, 'width': 20} ) A__ = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'height': 42, 'width': 42} ) # Previous config had dimensions in (width, height) order A__ = self.image_processing_class.from_dict(self.image_processor_dict , size=(42, 84) ) self.assertEqual(image_processor.size , {'height': 84, 'width': 42} ) def _a ( self : Tuple ): """simple docstring""" pass @is_flaky() def _a ( self : int ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=_snake_case ) for image in image_inputs: self.assertIsInstance(_snake_case , Image.Image ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched A__ = image_processing(_snake_case , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) @is_flaky() def _a ( self : Optional[Any] ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=_snake_case , numpify=_snake_case ) for image in image_inputs: self.assertIsInstance(_snake_case , np.ndarray ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched A__ = image_processing(_snake_case , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) @is_flaky() def _a ( self : Dict ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=_snake_case , torchify=_snake_case ) for image in image_inputs: self.assertIsInstance(_snake_case , torch.Tensor ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched A__ = image_processing(_snake_case , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , )
52
import itertools import json import os import unittest from transformers import AddedToken, LongformerTokenizer, LongformerTokenizerFast from transformers.models.longformer.tokenization_longformer import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __lowerCAmelCase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : int = LongformerTokenizer A__ : Optional[int] = True A__ : Any = LongformerTokenizerFast A__ : Dict = True def _a ( self : int ): """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt A__ = [ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', '\u0120', '\u0120l', '\u0120n', '\u0120lo', '\u0120low', 'er', '\u0120lowest', '\u0120newer', '\u0120wider', '<unk>', ] A__ = dict(zip(_snake_case , range(len(_snake_case ) ) ) ) A__ = ['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', ''] A__ = {'unk_token': '<unk>'} A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(_snake_case ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(_snake_case ) ) def _a ( self : int , **_snake_case : Union[str, Any] ): """simple docstring""" kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **_snake_case ) def _a ( self : Optional[int] , **_snake_case : List[Any] ): """simple docstring""" kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **_snake_case ) def _a ( self : Any , _snake_case : Optional[Any] ): """simple docstring""" A__ = 'lower newer' A__ = 'lower newer' return input_text, output_text def _a ( self : Any ): """simple docstring""" A__ = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) A__ = 'lower newer' A__ = ['l', 'o', 'w', 'er', '\u0120', 'n', 'e', 'w', 'er'] A__ = tokenizer.tokenize(_snake_case ) # , add_prefix_space=True) self.assertListEqual(_snake_case , _snake_case ) A__ = tokens + [tokenizer.unk_token] A__ = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(_snake_case ) , _snake_case ) def _a ( self : List[str] ): """simple docstring""" A__ = self.get_tokenizer() self.assertListEqual(tokenizer.encode('Hello world!' , add_special_tokens=_snake_case ) , [0, 3_14_14, 2_32, 3_28, 2] ) self.assertListEqual( tokenizer.encode('Hello world! cécé herlolip 418' , add_special_tokens=_snake_case ) , [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2] , ) @slow def _a ( self : List[Any] ): """simple docstring""" A__ = self.tokenizer_class.from_pretrained('allenai/longformer-base-4096' ) A__ = tokenizer.encode('sequence builders' , add_special_tokens=_snake_case ) A__ = tokenizer.encode('multi-sequence build' , add_special_tokens=_snake_case ) A__ = tokenizer.encode( 'sequence builders' , add_special_tokens=_snake_case , add_prefix_space=_snake_case ) A__ = tokenizer.encode( 'sequence builders' , 'multi-sequence build' , add_special_tokens=_snake_case , add_prefix_space=_snake_case ) A__ = tokenizer.build_inputs_with_special_tokens(_snake_case ) A__ = tokenizer.build_inputs_with_special_tokens(_snake_case , _snake_case ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def _a ( self : List[str] ): """simple docstring""" A__ = self.get_tokenizer() A__ = 'Encode this sequence.' A__ = tokenizer.byte_encoder[' '.encode('utf-8' )[0]] # Testing encoder arguments A__ = tokenizer.encode(_snake_case , add_special_tokens=_snake_case , add_prefix_space=_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(_snake_case , _snake_case ) A__ = tokenizer.encode(_snake_case , add_special_tokens=_snake_case , add_prefix_space=_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(_snake_case , _snake_case ) tokenizer.add_special_tokens({'bos_token': '<s>'} ) A__ = tokenizer.encode(_snake_case , add_special_tokens=_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(_snake_case , _snake_case ) # Testing spaces after special tokens A__ = '<mask>' tokenizer.add_special_tokens( {'mask_token': AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case )} ) # mask token has a left space A__ = tokenizer.convert_tokens_to_ids(_snake_case ) A__ = 'Encode <mask> sequence' A__ = 'Encode <mask>sequence' A__ = tokenizer.encode(_snake_case ) A__ = encoded.index(_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(_snake_case , _snake_case ) A__ = tokenizer.encode(_snake_case ) A__ = encoded.index(_snake_case ) A__ = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(_snake_case , _snake_case ) def _a ( self : Dict ): """simple docstring""" pass def _a ( self : Union[str, Any] ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): A__ = self.rust_tokenizer_class.from_pretrained(_snake_case , **_snake_case ) A__ = self.tokenizer_class.from_pretrained(_snake_case , **_snake_case ) A__ = 'A, <mask> AllenNLP sentence.' A__ = tokenizer_r.encode_plus(_snake_case , add_special_tokens=_snake_case , return_token_type_ids=_snake_case ) A__ = tokenizer_p.encode_plus(_snake_case , add_special_tokens=_snake_case , return_token_type_ids=_snake_case ) # token_type_ids should put 0 everywhere self.assertEqual(sum(tokens_r['token_type_ids'] ) , sum(tokens_p['token_type_ids'] ) ) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEqual( sum(tokens_r['attention_mask'] ) / len(tokens_r['attention_mask'] ) , sum(tokens_p['attention_mask'] ) / len(tokens_p['attention_mask'] ) , ) A__ = tokenizer_r.convert_ids_to_tokens(tokens_r['input_ids'] ) A__ = tokenizer_p.convert_ids_to_tokens(tokens_p['input_ids'] ) # Rust correctly handles the space before the mask while python doesnt self.assertSequenceEqual(tokens_p['input_ids'] , [0, 2_50, 6, 5_02_64, 38_23, 4_87, 2_19_92, 36_45, 4, 2] ) self.assertSequenceEqual(tokens_r['input_ids'] , [0, 2_50, 6, 5_02_64, 38_23, 4_87, 2_19_92, 36_45, 4, 2] ) self.assertSequenceEqual( _snake_case , ['<s>', 'A', ',', '<mask>', 'ĠAllen', 'N', 'LP', 'Ġsentence', '.', '</s>'] ) self.assertSequenceEqual( _snake_case , ['<s>', 'A', ',', '<mask>', 'ĠAllen', 'N', 'LP', 'Ġsentence', '.', '</s>'] ) def _a ( self : List[Any] ): """simple docstring""" for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): A__ = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) A__ = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state['add_prefix_space'] , _snake_case ) self.assertEqual(post_processor_state['add_prefix_space'] , _snake_case ) self.assertEqual(post_processor_state['trim_offsets'] , _snake_case ) def _a ( self : Optional[Any] ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): A__ = 'hello' # `hello` is a token in the vocabulary of `pretrained_name` A__ = F'''{text_of_1_token} {text_of_1_token}''' A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_snake_case ) + 1, len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_snake_case ) + 1, len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_snake_case ), len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_snake_case ), len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = F''' {text}''' # tokenizer_r = self.rust_tokenizer_class.from_pretrained( # pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True # ) # encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False) # self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token))) # self.assertEqual( # encoding.offset_mapping[1], # (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)), # ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_snake_case ) + 1, 1 + len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_snake_case ), 1 + len(_snake_case ) + 1 + len(_snake_case )) , ) A__ = self.rust_tokenizer_class.from_pretrained( _snake_case , use_fast=_snake_case , add_prefix_space=_snake_case , trim_offsets=_snake_case ) A__ = tokenizer_r(_snake_case , return_offsets_mapping=_snake_case , add_special_tokens=_snake_case ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(_snake_case )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_snake_case ), 1 + len(_snake_case ) + 1 + len(_snake_case )) , )
52
1
import argparse import os import re import packaging.version SCREAMING_SNAKE_CASE__ = '''examples/''' SCREAMING_SNAKE_CASE__ = { '''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'''), } SCREAMING_SNAKE_CASE__ = { '''init''': '''src/transformers/__init__.py''', '''setup''': '''setup.py''', } SCREAMING_SNAKE_CASE__ = '''README.md''' def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[Any]: with open(__UpperCamelCase , 'r' , encoding='utf-8' , newline='\n' ) as f: A__ = f.read() A__ , A__ = REPLACE_PATTERNS[pattern] A__ = replace.replace('VERSION' , __UpperCamelCase ) A__ = re_pattern.sub(__UpperCamelCase , __UpperCamelCase ) with open(__UpperCamelCase , 'w' , encoding='utf-8' , newline='\n' ) as f: f.write(__UpperCamelCase ) def A ( __UpperCamelCase ) -> Any: for folder, directories, fnames in os.walk(__UpperCamelCase ): # 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(__UpperCamelCase , __UpperCamelCase ) , __UpperCamelCase , pattern='examples' ) def A ( __UpperCamelCase , __UpperCamelCase=False ) -> int: for pattern, fname in REPLACE_FILES.items(): update_version_in_file(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) if not patch: update_version_in_examples(__UpperCamelCase ) def A ( ) -> Dict: A__ = '🤗 Transformers currently provides the following architectures' A__ = '1. Want to contribute a new model?' with open(__UpperCamelCase , 'r' , encoding='utf-8' , newline='\n' ) as f: A__ = f.readlines() # Find the start of the list. A__ = 0 while not lines[start_index].startswith(_start_prompt ): start_index += 1 start_index += 1 A__ = start_index # Update the lines in the model list. while not lines[index].startswith(_end_prompt ): if lines[index].startswith('1.' ): A__ = lines[index].replace( 'https://huggingface.co/docs/transformers/main/model_doc' , 'https://huggingface.co/docs/transformers/model_doc' , ) index += 1 with open(__UpperCamelCase , 'w' , encoding='utf-8' , newline='\n' ) as f: f.writelines(__UpperCamelCase ) def A ( ) -> Optional[Any]: with open(REPLACE_FILES['init'] , 'r' ) as f: A__ = f.read() A__ = REPLACE_PATTERNS['init'][0].search(__UpperCamelCase ).groups()[0] return packaging.version.parse(__UpperCamelCase ) def A ( __UpperCamelCase=False ) -> Tuple: A__ = 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: A__ = default_version.base_version elif patch: A__ = f'''{default_version.major}.{default_version.minor}.{default_version.micro + 1}''' else: A__ = f'''{default_version.major}.{default_version.minor + 1}.0''' # Now let's ask nicely if that's the right one. A__ = input(f'''Which version are you releasing? [{default_version}]''' ) if len(__UpperCamelCase ) == 0: A__ = default_version print(f'''Updating version to {version}.''' ) global_version_update(__UpperCamelCase , patch=__UpperCamelCase ) if not patch: print('Cleaning main README, don\'t forget to run `make fix-copies`.' ) clean_main_ref_in_model_list() def A ( ) -> Union[str, Any]: A__ = get_version() A__ = f'''{current_version.major}.{current_version.minor + 1}.0.dev0''' A__ = current_version.base_version # Check with the user we got that right. A__ = input(f'''Which version are we developing now? [{dev_version}]''' ) if len(__UpperCamelCase ) == 0: A__ = dev_version print(f'''Updating version to {version}.''' ) global_version_update(__UpperCamelCase ) print('Cleaning main README, don\'t forget to run `make fix-copies`.' ) clean_main_ref_in_model_list() if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = 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.''') SCREAMING_SNAKE_CASE__ = 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()
52
import pytest import datasets # Import fixture modules as plugins SCREAMING_SNAKE_CASE__ = ['''tests.fixtures.files''', '''tests.fixtures.hub''', '''tests.fixtures.fsspec'''] def A ( __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: # Mark tests as "unit" by default if not marked as "integration" (or already marked as "unit") for item in items: if any(marker in item.keywords for marker in ['integration', 'unit'] ): continue item.add_marker(pytest.mark.unit ) def A ( __UpperCamelCase ) -> str: config.addinivalue_line('markers' , 'torchaudio_latest: mark test to run with torchaudio>=0.12' ) @pytest.fixture(autouse=__UpperCamelCase ) def A ( __UpperCamelCase , __UpperCamelCase ) -> Union[str, Any]: # test_hf_cache_home = tmp_path_factory.mktemp("cache") # TODO: why a cache dir per test function does not work? A__ = tmp_path_factory.getbasetemp() / 'cache' A__ = test_hf_cache_home / 'datasets' A__ = test_hf_cache_home / 'metrics' A__ = test_hf_cache_home / 'modules' monkeypatch.setattr('datasets.config.HF_DATASETS_CACHE' , str(__UpperCamelCase ) ) monkeypatch.setattr('datasets.config.HF_METRICS_CACHE' , str(__UpperCamelCase ) ) monkeypatch.setattr('datasets.config.HF_MODULES_CACHE' , str(__UpperCamelCase ) ) A__ = test_hf_datasets_cache / 'downloads' monkeypatch.setattr('datasets.config.DOWNLOADED_DATASETS_PATH' , str(__UpperCamelCase ) ) A__ = test_hf_datasets_cache / 'downloads' / 'extracted' monkeypatch.setattr('datasets.config.EXTRACTED_DATASETS_PATH' , str(__UpperCamelCase ) ) @pytest.fixture(autouse=__UpperCamelCase , scope='session' ) def A ( ) -> Union[str, Any]: datasets.disable_progress_bar() @pytest.fixture(autouse=__UpperCamelCase ) def A ( __UpperCamelCase ) -> int: # don't take tests into account when counting downloads monkeypatch.setattr('datasets.config.HF_UPDATE_DOWNLOAD_COUNTS' , __UpperCamelCase ) @pytest.fixture def A ( __UpperCamelCase ) -> Any: # Required to suppress RemovedIn20Warning when feature(s) are not compatible with SQLAlchemy 2.0 # To be removed once SQLAlchemy 2.0 supported monkeypatch.setattr('sqlalchemy.util.deprecations.SILENCE_UBER_WARNING' , __UpperCamelCase )
52
1
import json import os import unittest from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer from ...test_tokenization_common import TokenizerTesterMixin class __lowerCAmelCase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : str = CTRLTokenizer A__ : Union[str, Any] = False A__ : Any = False def _a ( self : Tuple ): """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt A__ = ['adapt', 're@@', 'a@@', 'apt', 'c@@', 't', '<unk>'] A__ = dict(zip(_snake_case , range(len(_snake_case ) ) ) ) A__ = ['#version: 0.2', 'a p', 'ap t</w>', 'r e', 'a d', 'ad apt</w>', ''] A__ = {'unk_token': '<unk>'} A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(_snake_case ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(_snake_case ) ) def _a ( self : Tuple , **_snake_case : Tuple ): """simple docstring""" kwargs.update(self.special_tokens_map ) return CTRLTokenizer.from_pretrained(self.tmpdirname , **_snake_case ) def _a ( self : Tuple , _snake_case : Dict ): """simple docstring""" A__ = 'adapt react readapt apt' A__ = 'adapt react readapt apt' return input_text, output_text def _a ( self : Tuple ): """simple docstring""" A__ = CTRLTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) A__ = 'adapt react readapt apt' A__ = 'adapt re@@ a@@ c@@ t re@@ adapt apt'.split() A__ = tokenizer.tokenize(_snake_case ) self.assertListEqual(_snake_case , _snake_case ) A__ = tokens + [tokenizer.unk_token] A__ = [0, 1, 2, 4, 5, 1, 0, 3, 6] self.assertListEqual(tokenizer.convert_tokens_to_ids(_snake_case ) , _snake_case )
52
import argparse import re from typing import Dict import torch from datasets import Audio, Dataset, load_dataset, load_metric from transformers import AutoFeatureExtractor, pipeline def A ( __UpperCamelCase , __UpperCamelCase ) -> Tuple: A__ = args.log_outputs A__ = '_'.join(args.dataset.split('/' ) + [args.config, args.split] ) # load metric A__ = load_metric('wer' ) A__ = load_metric('cer' ) # compute metrics A__ = wer.compute(references=result['target'] , predictions=result['prediction'] ) A__ = cer.compute(references=result['target'] , predictions=result['prediction'] ) # print & log results A__ = f'''WER: {wer_result}\nCER: {cer_result}''' print(__UpperCamelCase ) with open(f'''{dataset_id}_eval_results.txt''' , 'w' ) as f: f.write(__UpperCamelCase ) # log all results in text file. Possibly interesting for analysis if log_outputs is not None: A__ = f'''log_{dataset_id}_predictions.txt''' A__ = f'''log_{dataset_id}_targets.txt''' with open(__UpperCamelCase , 'w' ) as p, open(__UpperCamelCase , 'w' ) as t: # mapping function to write output def write_to_file(__UpperCamelCase , __UpperCamelCase ): p.write(f'''{i}''' + '\n' ) p.write(batch['prediction'] + '\n' ) t.write(f'''{i}''' + '\n' ) t.write(batch['target'] + '\n' ) result.map(__UpperCamelCase , with_indices=__UpperCamelCase ) def A ( __UpperCamelCase ) -> str: A__ = '[,?.!\-\;\:"“%‘”�—’…–]' # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training A__ = re.sub(__UpperCamelCase , '' , text.lower() ) # In addition, we can normalize the target text, e.g. removing new lines characters etc... # note that order is important here! A__ = ['\n\n', '\n', ' ', ' '] for t in token_sequences_to_ignore: A__ = ' '.join(text.split(__UpperCamelCase ) ) return text def A ( __UpperCamelCase ) -> Union[str, Any]: # load dataset A__ = load_dataset(args.dataset , args.config , split=args.split , use_auth_token=__UpperCamelCase ) # for testing: only process the first two examples as a test # dataset = dataset.select(range(10)) # load processor A__ = AutoFeatureExtractor.from_pretrained(args.model_id ) A__ = feature_extractor.sampling_rate # resample audio A__ = dataset.cast_column('audio' , Audio(sampling_rate=__UpperCamelCase ) ) # load eval pipeline if args.device is None: A__ = 0 if torch.cuda.is_available() else -1 A__ = pipeline('automatic-speech-recognition' , model=args.model_id , device=args.device ) # map function to decode audio def map_to_pred(__UpperCamelCase ): A__ = asr( batch['audio']['array'] , chunk_length_s=args.chunk_length_s , stride_length_s=args.stride_length_s ) A__ = prediction['text'] A__ = normalize_text(batch['sentence'] ) return batch # run inference on all examples A__ = dataset.map(__UpperCamelCase , remove_columns=dataset.column_names ) # compute and log_results # do not change function below log_results(__UpperCamelCase , __UpperCamelCase ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() parser.add_argument( '''--model_id''', type=str, required=True, help='''Model identifier. Should be loadable with 🤗 Transformers''' ) parser.add_argument( '''--dataset''', type=str, required=True, help='''Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets''', ) parser.add_argument( '''--config''', type=str, required=True, help='''Config of the dataset. *E.g.* `\'en\'` for Common Voice''' ) parser.add_argument('''--split''', type=str, required=True, help='''Split of the dataset. *E.g.* `\'test\'`''') parser.add_argument( '''--chunk_length_s''', type=float, default=None, help='''Chunk length in seconds. Defaults to 5 seconds.''' ) parser.add_argument( '''--stride_length_s''', type=float, default=None, help='''Stride of the audio chunks. Defaults to 1 second.''' ) parser.add_argument( '''--log_outputs''', action='''store_true''', help='''If defined, write outputs to log file for analysis.''' ) parser.add_argument( '''--device''', type=int, default=None, help='''The device to run the pipeline on. -1 for CPU (default), 0 for the first GPU and so on.''', ) SCREAMING_SNAKE_CASE__ = parser.parse_args() main(args)
52
1
import itertools import os import random import tempfile import unittest import numpy as np from transformers import TvltFeatureExtractor, is_datasets_available from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch if is_datasets_available(): from datasets import load_dataset SCREAMING_SNAKE_CASE__ = random.Random() def A ( __UpperCamelCase , __UpperCamelCase=1.0 , __UpperCamelCase=None , __UpperCamelCase=None ) -> Optional[int]: if rng is None: A__ = global_rng A__ = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def __init__( self : Dict , _snake_case : Union[str, Any] , _snake_case : str=7 , _snake_case : List[str]=4_00 , _snake_case : Union[str, Any]=20_00 , _snake_case : str=20_48 , _snake_case : int=1_28 , _snake_case : Optional[int]=1 , _snake_case : Dict=5_12 , _snake_case : Tuple=30 , _snake_case : int=4_41_00 , ): """simple docstring""" A__ = parent A__ = batch_size A__ = min_seq_length A__ = max_seq_length A__ = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) A__ = spectrogram_length A__ = feature_size A__ = num_audio_channels A__ = hop_length A__ = chunk_length A__ = sampling_rate def _a ( self : Tuple ): """simple docstring""" return { "spectrogram_length": self.spectrogram_length, "feature_size": self.feature_size, "num_audio_channels": self.num_audio_channels, "hop_length": self.hop_length, "chunk_length": self.chunk_length, "sampling_rate": self.sampling_rate, } def _a ( self : int , _snake_case : Tuple=False , _snake_case : str=False ): """simple docstring""" def _flatten(_snake_case : List[str] ): return list(itertools.chain(*_snake_case ) ) if equal_length: A__ = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size A__ = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: A__ = [np.asarray(_snake_case ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class __lowerCAmelCase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Optional[Any] = TvltFeatureExtractor def _a ( self : str ): """simple docstring""" A__ = TvltFeatureExtractionTester(self ) def _a ( self : Tuple ): """simple docstring""" A__ = self.feature_extraction_class(**self.feat_extract_dict ) self.assertTrue(hasattr(_snake_case , 'spectrogram_length' ) ) self.assertTrue(hasattr(_snake_case , 'feature_size' ) ) self.assertTrue(hasattr(_snake_case , 'num_audio_channels' ) ) self.assertTrue(hasattr(_snake_case , 'hop_length' ) ) self.assertTrue(hasattr(_snake_case , 'chunk_length' ) ) self.assertTrue(hasattr(_snake_case , 'sampling_rate' ) ) def _a ( self : str ): """simple docstring""" A__ = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: A__ = feat_extract_first.save_pretrained(_snake_case )[0] check_json_file_has_correct_format(_snake_case ) A__ = self.feature_extraction_class.from_pretrained(_snake_case ) A__ = feat_extract_first.to_dict() A__ = feat_extract_second.to_dict() A__ = dict_first.pop('mel_filters' ) A__ = dict_second.pop('mel_filters' ) self.assertTrue(np.allclose(_snake_case , _snake_case ) ) self.assertEqual(_snake_case , _snake_case ) def _a ( self : Union[str, Any] ): """simple docstring""" A__ = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: A__ = os.path.join(_snake_case , 'feat_extract.json' ) feat_extract_first.to_json_file(_snake_case ) A__ = self.feature_extraction_class.from_json_file(_snake_case ) A__ = feat_extract_first.to_dict() A__ = feat_extract_second.to_dict() A__ = dict_first.pop('mel_filters' ) A__ = dict_second.pop('mel_filters' ) self.assertTrue(np.allclose(_snake_case , _snake_case ) ) self.assertEqual(_snake_case , _snake_case ) def _a ( self : Dict ): """simple docstring""" A__ = self.feature_extraction_class(**self.feat_extract_dict ) # create three inputs of length 800, 1000, and 1200 A__ = [floats_list((1, x) )[0] for x in range(8_00 , 14_00 , 2_00 )] A__ = [np.asarray(_snake_case ) for speech_input in speech_inputs] # Test not batched input A__ = feature_extractor(np_speech_inputs[0] , return_tensors='np' , sampling_rate=4_41_00 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test batched A__ = feature_extractor(_snake_case , return_tensors='np' , sampling_rate=4_41_00 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test audio masking A__ = feature_extractor( _snake_case , return_tensors='np' , sampling_rate=4_41_00 , mask_audio=_snake_case ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test 2-D numpy arrays are batched. A__ = [floats_list((1, x) )[0] for x in (8_00, 8_00, 8_00)] A__ = np.asarray(_snake_case ) A__ = feature_extractor(_snake_case , return_tensors='np' , sampling_rate=4_41_00 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) def _a ( self : List[str] , _snake_case : Tuple ): """simple docstring""" A__ = load_dataset('hf-internal-testing/librispeech_asr_dummy' , 'clean' , split='validation' ) # automatic decoding with librispeech A__ = ds.sort('id' ).select(range(_snake_case ) )[:num_samples]['audio'] return [x["array"] for x in speech_samples] def _a ( self : Dict ): """simple docstring""" A__ = self._load_datasamples(1 ) A__ = TvltFeatureExtractor() A__ = feature_extractor(_snake_case , return_tensors='pt' ).audio_values self.assertEquals(audio_values.shape , (1, 1, 1_92, 1_28) ) A__ = torch.tensor([[-0.3032, -0.2708], [-0.4434, -0.4007]] ) self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2] , _snake_case , atol=1E-4 ) )
52
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import YolosConfig, YolosForObjectDetection, YolosImageProcessor from transformers.utils import logging logging.set_verbosity_info() SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) def A ( __UpperCamelCase ) -> YolosConfig: A__ = YolosConfig() # size of the architecture if "yolos_ti" in yolos_name: A__ = 192 A__ = 768 A__ = 12 A__ = 3 A__ = [800, 1_333] A__ = False elif yolos_name == "yolos_s_dWr": A__ = 330 A__ = 14 A__ = 6 A__ = 1_320 elif "yolos_s" in yolos_name: A__ = 384 A__ = 1_536 A__ = 12 A__ = 6 elif "yolos_b" in yolos_name: A__ = [800, 1_344] A__ = 91 A__ = 'huggingface/label-files' A__ = 'coco-detection-id2label.json' A__ = json.load(open(hf_hub_download(__UpperCamelCase , __UpperCamelCase , repo_type='dataset' ) , 'r' ) ) A__ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} A__ = idalabel A__ = {v: k for k, v in idalabel.items()} return config def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = False ) -> str: for i in range(config.num_hidden_layers ): # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) A__ = state_dict.pop(f'''blocks.{i}.attn.qkv.weight''' ) A__ = state_dict.pop(f'''blocks.{i}.attn.qkv.bias''' ) # next, add query, keys and values (in that order) to the state dict A__ = in_proj_weight[: config.hidden_size, :] A__ = in_proj_bias[: config.hidden_size] A__ = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] A__ = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] A__ = in_proj_weight[-config.hidden_size :, :] A__ = in_proj_bias[-config.hidden_size :] def A ( __UpperCamelCase ) -> str: if "backbone" in name: A__ = name.replace('backbone' , 'vit' ) if "cls_token" in name: A__ = name.replace('cls_token' , 'embeddings.cls_token' ) if "det_token" in name: A__ = name.replace('det_token' , 'embeddings.detection_tokens' ) if "mid_pos_embed" in name: A__ = name.replace('mid_pos_embed' , 'encoder.mid_position_embeddings' ) if "pos_embed" in name: A__ = name.replace('pos_embed' , 'embeddings.position_embeddings' ) if "patch_embed.proj" in name: A__ = name.replace('patch_embed.proj' , 'embeddings.patch_embeddings.projection' ) if "blocks" in name: A__ = name.replace('blocks' , 'encoder.layer' ) if "attn.proj" in name: A__ = name.replace('attn.proj' , 'attention.output.dense' ) if "attn" in name: A__ = name.replace('attn' , 'attention.self' ) if "norm1" in name: A__ = name.replace('norm1' , 'layernorm_before' ) if "norm2" in name: A__ = name.replace('norm2' , 'layernorm_after' ) if "mlp.fc1" in name: A__ = name.replace('mlp.fc1' , 'intermediate.dense' ) if "mlp.fc2" in name: A__ = name.replace('mlp.fc2' , 'output.dense' ) if "class_embed" in name: A__ = name.replace('class_embed' , 'class_labels_classifier' ) if "bbox_embed" in name: A__ = name.replace('bbox_embed' , 'bbox_predictor' ) if "vit.norm" in name: A__ = name.replace('vit.norm' , 'vit.layernorm' ) return name def A ( __UpperCamelCase , __UpperCamelCase ) -> dict: for key in orig_state_dict.copy().keys(): A__ = orig_state_dict.pop(__UpperCamelCase ) if "qkv" in key: A__ = key.split('.' ) A__ = int(key_split[2] ) A__ = model.vit.encoder.layer[layer_num].attention.attention.all_head_size if "weight" in key: A__ = val[:dim, :] A__ = val[ dim : dim * 2, : ] A__ = val[-dim:, :] else: A__ = val[:dim] A__ = val[dim : dim * 2] A__ = val[-dim:] else: A__ = val return orig_state_dict def A ( ) -> torch.Tensor: A__ = 'http://images.cocodataset.org/val2017/000000039769.jpg' A__ = Image.open(requests.get(__UpperCamelCase , stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = False ) -> List[str]: A__ = get_yolos_config(__UpperCamelCase ) # load original state_dict A__ = torch.load(__UpperCamelCase , map_location='cpu' )['model'] # load 🤗 model A__ = YolosForObjectDetection(__UpperCamelCase ) model.eval() A__ = convert_state_dict(__UpperCamelCase , __UpperCamelCase ) model.load_state_dict(__UpperCamelCase ) # Check outputs on an image, prepared by YolosImageProcessor A__ = 800 if yolos_name != 'yolos_ti' else 512 A__ = YolosImageProcessor(format='coco_detection' , size=__UpperCamelCase ) A__ = image_processor(images=prepare_img() , return_tensors='pt' ) A__ = model(**__UpperCamelCase ) A__ , A__ = outputs.logits, outputs.pred_boxes A__ , A__ = None, None if yolos_name == "yolos_ti": A__ = torch.tensor( [[-39.5022, -11.9820, -17.6888], [-29.9574, -9.9769, -17.7691], [-42.3281, -20.7200, -30.6294]] ) A__ = torch.tensor( [[0.4021, 0.0836, 0.7979], [0.0184, 0.2609, 0.0364], [0.1781, 0.2004, 0.2095]] ) elif yolos_name == "yolos_s_200_pre": A__ = torch.tensor( [[-24.0248, -10.3024, -14.8290], [-42.0392, -16.8200, -27.4334], [-27.2743, -11.8154, -18.7148]] ) A__ = torch.tensor( [[0.2559, 0.5455, 0.4706], [0.2989, 0.7279, 0.1875], [0.7732, 0.4017, 0.4462]] ) elif yolos_name == "yolos_s_300_pre": A__ = torch.tensor( [[-36.2220, -14.4385, -23.5457], [-35.6970, -14.7583, -21.3935], [-31.5939, -13.6042, -16.8049]] ) A__ = torch.tensor( [[0.7614, 0.2316, 0.4728], [0.7168, 0.4495, 0.3855], [0.4996, 0.1466, 0.9996]] ) elif yolos_name == "yolos_s_dWr": A__ = torch.tensor( [[-42.8668, -24.1049, -41.1690], [-34.7456, -14.1274, -24.9194], [-33.7898, -12.1946, -25.6495]] ) A__ = torch.tensor( [[0.5587, 0.2773, 0.0605], [0.5004, 0.3014, 0.9994], [0.4999, 0.1548, 0.9994]] ) elif yolos_name == "yolos_base": A__ = torch.tensor( [[-40.6064, -24.3084, -32.6447], [-55.1990, -30.7719, -35.5877], [-51.4311, -33.3507, -35.6462]] ) A__ = torch.tensor( [[0.5555, 0.2794, 0.0655], [0.9049, 0.2664, 0.1894], [0.9183, 0.1984, 0.1635]] ) else: raise ValueError(f'''Unknown yolos_name: {yolos_name}''' ) assert torch.allclose(logits[0, :3, :3] , __UpperCamelCase , atol=1E-4 ) assert torch.allclose(pred_boxes[0, :3, :3] , __UpperCamelCase , atol=1E-4 ) Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) print(f'''Saving model {yolos_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(__UpperCamelCase ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(__UpperCamelCase ) if push_to_hub: A__ = { 'yolos_ti': 'yolos-tiny', 'yolos_s_200_pre': 'yolos-small', 'yolos_s_300_pre': 'yolos-small-300', 'yolos_s_dWr': 'yolos-small-dwr', 'yolos_base': 'yolos-base', } print('Pushing to the hub...' ) A__ = model_mapping[yolos_name] image_processor.push_to_hub(__UpperCamelCase , organization='hustvl' ) model.push_to_hub(__UpperCamelCase , organization='hustvl' ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--yolos_name''', default='''yolos_s_200_pre''', type=str, help=( '''Name of the YOLOS model you\'d like to convert. Should be one of \'yolos_ti\', \'yolos_s_200_pre\',''' ''' \'yolos_s_300_pre\', \'yolos_s_dWr\', \'yolos_base\'.''' ), ) parser.add_argument( '''--checkpoint_path''', default=None, type=str, help='''Path to the original state dict (.pth file).''' ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_yolos_checkpoint(args.yolos_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub)
52
1
import unittest from transformers import TrOCRConfig from transformers.testing_utils import is_torch_available, require_torch, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers.models.trocr.modeling_trocr import TrOCRDecoder, TrOCRForCausalLM @require_torch class __lowerCAmelCase : """simple docstring""" def __init__( self : Any , _snake_case : List[Any] , _snake_case : Dict=99 , _snake_case : List[str]=13 , _snake_case : List[Any]=16 , _snake_case : List[Any]=7 , _snake_case : Tuple=True , _snake_case : Optional[Any]=True , _snake_case : Dict=True , _snake_case : Dict=False , _snake_case : int=True , _snake_case : Union[str, Any]=2 , _snake_case : Optional[int]=32 , _snake_case : Tuple=4 , _snake_case : Tuple=4 , _snake_case : Dict=30 , _snake_case : Tuple=0 , _snake_case : Dict=1 , _snake_case : List[Any]=2 , _snake_case : List[str]=None , ): """simple docstring""" A__ = parent A__ = batch_size A__ = decoder_seq_length # For common tests A__ = self.decoder_seq_length A__ = is_training A__ = use_attention_mask A__ = use_labels A__ = vocab_size A__ = d_model A__ = d_model A__ = decoder_layers A__ = decoder_layers A__ = decoder_ffn_dim A__ = decoder_attention_heads A__ = decoder_attention_heads A__ = eos_token_id A__ = bos_token_id A__ = pad_token_id A__ = decoder_start_token_id A__ = use_cache A__ = max_position_embeddings A__ = None A__ = decoder_seq_length A__ = 2 A__ = 1 def _a ( self : List[Any] ): """simple docstring""" A__ = ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) A__ = None if self.use_attention_mask: A__ = ids_tensor([self.batch_size, self.decoder_seq_length] , vocab_size=2 ) A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) A__ = TrOCRConfig( vocab_size=self.vocab_size , d_model=self.d_model , decoder_layers=self.decoder_layers , decoder_ffn_dim=self.decoder_ffn_dim , decoder_attention_heads=self.decoder_attention_heads , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , use_cache=self.use_cache , pad_token_id=self.pad_token_id , decoder_start_token_id=self.decoder_start_token_id , max_position_embeddings=self.max_position_embeddings , ) return (config, input_ids, attention_mask, lm_labels) def _a ( self : List[Any] , _snake_case : Dict , _snake_case : List[Any] , _snake_case : List[str] , _snake_case : List[str] , ): """simple docstring""" A__ = True A__ = TrOCRDecoder(config=_snake_case ).to(_snake_case ).eval() A__ = input_ids[:2] input_ids[input_ids == 0] += 1 # first forward pass A__ = model(_snake_case , use_cache=_snake_case ) A__ = model(_snake_case ) A__ = model(_snake_case , use_cache=_snake_case ) self.parent.assertTrue(len(_snake_case ) == len(_snake_case ) ) self.parent.assertTrue(len(_snake_case ) == len(_snake_case ) + 1 ) A__ = outputs['past_key_values'] # create hypothetical next token and extent to next_input_ids A__ = ids_tensor((2, 1) , config.vocab_size - 1 ) + 1 # append to next input_ids and A__ = torch.cat([input_ids, next_tokens] , dim=-1 ) A__ = model(_snake_case )['last_hidden_state'] A__ = model(_snake_case , past_key_values=_snake_case )['last_hidden_state'] # select random slice A__ = ids_tensor((1,) , output_from_past.shape[-1] ).item() A__ = output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach() A__ = output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice assert torch.allclose(_snake_case , _snake_case , atol=1E-3 ) def _a ( self : Optional[int] ): """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ , A__ = config_and_inputs A__ = {'input_ids': input_ids, 'attention_mask': attention_mask} return config, inputs_dict @require_torch class __lowerCAmelCase ( UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Any = (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else () A__ : List[str] = (TrOCRForCausalLM,) if is_torch_available() else () A__ : List[str] = {"text-generation": TrOCRForCausalLM} if is_torch_available() else {} A__ : Dict = True A__ : str = False def _a ( self : Tuple ): """simple docstring""" A__ = TrOCRStandaloneDecoderModelTester(self , is_training=_snake_case ) A__ = ConfigTester(self , config_class=_snake_case ) def _a ( self : Tuple ): """simple docstring""" pass def _a ( self : Optional[Any] ): """simple docstring""" pass def _a ( self : Optional[int] ): """simple docstring""" pass def _a ( self : List[Any] ): """simple docstring""" self.config_tester.run_common_tests() def _a ( self : Optional[Any] ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past(*_snake_case ) def _a ( self : Any ): """simple docstring""" return @unittest.skip('The model doesn\'t support left padding' ) # and it's not used enough to be worth fixing :) def _a ( self : Union[str, Any] ): """simple docstring""" pass
52
from typing import TYPE_CHECKING from ..utils import _LazyModule SCREAMING_SNAKE_CASE__ = { '''config''': [ '''EXTERNAL_DATA_FORMAT_SIZE_LIMIT''', '''OnnxConfig''', '''OnnxConfigWithPast''', '''OnnxSeq2SeqConfigWithPast''', '''PatchingSpec''', ], '''convert''': ['''export''', '''validate_model_outputs'''], '''features''': ['''FeaturesManager'''], '''utils''': ['''ParameterFormat''', '''compute_serialized_parameters_size'''], } if TYPE_CHECKING: from .config import ( EXTERNAL_DATA_FORMAT_SIZE_LIMIT, OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast, PatchingSpec, ) from .convert import export, validate_model_outputs from .features import FeaturesManager from .utils import ParameterFormat, compute_serialized_parameters_size else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
52
1
from ...configuration_utils import PretrainedConfig from ...utils import logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = { '''vinvino02/glpn-kitti''': '''https://huggingface.co/vinvino02/glpn-kitti/resolve/main/config.json''', # See all GLPN models at https://huggingface.co/models?filter=glpn } class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : int = "glpn" def __init__( self : Tuple , _snake_case : Union[str, Any]=3 , _snake_case : List[str]=4 , _snake_case : Dict=[2, 2, 2, 2] , _snake_case : Tuple=[8, 4, 2, 1] , _snake_case : int=[32, 64, 1_60, 2_56] , _snake_case : List[Any]=[7, 3, 3, 3] , _snake_case : Any=[4, 2, 2, 2] , _snake_case : Union[str, Any]=[1, 2, 5, 8] , _snake_case : Any=[4, 4, 4, 4] , _snake_case : Optional[Any]="gelu" , _snake_case : str=0.0 , _snake_case : List[Any]=0.0 , _snake_case : Optional[int]=0.02 , _snake_case : List[Any]=0.1 , _snake_case : Optional[int]=1E-6 , _snake_case : Any=64 , _snake_case : Optional[int]=10 , _snake_case : Optional[Any]=-1 , **_snake_case : str , ): """simple docstring""" super().__init__(**_snake_case ) A__ = num_channels A__ = num_encoder_blocks A__ = depths A__ = sr_ratios A__ = hidden_sizes A__ = patch_sizes A__ = strides A__ = mlp_ratios A__ = num_attention_heads A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = initializer_range A__ = drop_path_rate A__ = layer_norm_eps A__ = decoder_hidden_size A__ = max_depth A__ = head_in_index
52
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_rembert import RemBertTokenizer else: SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = {'''vocab_file''': '''sentencepiece.model''', '''tokenizer_file''': '''tokenizer.json'''} SCREAMING_SNAKE_CASE__ = { '''vocab_file''': { '''google/rembert''': '''https://huggingface.co/google/rembert/resolve/main/sentencepiece.model''', }, '''tokenizer_file''': { '''google/rembert''': '''https://huggingface.co/google/rembert/resolve/main/tokenizer.json''', }, } SCREAMING_SNAKE_CASE__ = { '''google/rembert''': 2_5_6, } SCREAMING_SNAKE_CASE__ = '''▁''' class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Any = VOCAB_FILES_NAMES A__ : str = PRETRAINED_VOCAB_FILES_MAP A__ : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A__ : int = RemBertTokenizer def __init__( self : Union[str, Any] , _snake_case : Any=None , _snake_case : Optional[Any]=None , _snake_case : Any=True , _snake_case : Optional[int]=True , _snake_case : Dict=False , _snake_case : Dict="[CLS]" , _snake_case : List[Any]="[SEP]" , _snake_case : Union[str, Any]="<unk>" , _snake_case : List[str]="[SEP]" , _snake_case : List[str]="<pad>" , _snake_case : str="[CLS]" , _snake_case : Any="[MASK]" , **_snake_case : Any , ): """simple docstring""" A__ = AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case ) if isinstance(_snake_case , _snake_case ) else mask_token super().__init__( _snake_case , tokenizer_file=_snake_case , do_lower_case=_snake_case , remove_space=_snake_case , keep_accents=_snake_case , bos_token=_snake_case , eos_token=_snake_case , unk_token=_snake_case , sep_token=_snake_case , pad_token=_snake_case , cls_token=_snake_case , mask_token=_snake_case , **_snake_case , ) A__ = do_lower_case A__ = remove_space A__ = keep_accents A__ = vocab_file A__ = False if not self.vocab_file else True def _a ( self : Any , _snake_case : List[int] , _snake_case : Optional[List[int]] = None ): """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def _a ( self : Tuple , _snake_case : List[int] , _snake_case : Optional[List[int]] = None , _snake_case : bool = False ): """simple docstring""" if already_has_special_tokens: if token_ids_a is not None: raise ValueError( 'You should not supply a second sequence if the provided sequence of ' 'ids is already formatted with special tokens for the model.' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(_snake_case )) + [1] + ([0] * len(_snake_case )) + [1] return [1] + ([0] * len(_snake_case )) + [1] def _a ( self : Dict , _snake_case : List[int] , _snake_case : Optional[List[int]] = None ): """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def _a ( self : Any , _snake_case : str , _snake_case : Optional[str] = None ): """simple docstring""" if not os.path.isdir(_snake_case ): logger.error('Vocabulary path ({}) should be a directory'.format(_snake_case ) ) return A__ = os.path.join( _snake_case , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_snake_case ): copyfile(self.vocab_file , _snake_case ) return (out_vocab_file,)
52
1
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = { '''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 ( UpperCAmelCase_ ): """simple docstring""" A__ : Dict = "levit" def __init__( self : int , _snake_case : Optional[Any]=2_24 , _snake_case : Tuple=3 , _snake_case : Optional[int]=3 , _snake_case : str=2 , _snake_case : Union[str, Any]=1 , _snake_case : Any=16 , _snake_case : List[str]=[1_28, 2_56, 3_84] , _snake_case : Tuple=[4, 8, 12] , _snake_case : Dict=[4, 4, 4] , _snake_case : Optional[int]=[16, 16, 16] , _snake_case : List[Any]=0 , _snake_case : str=[2, 2, 2] , _snake_case : Optional[Any]=[2, 2, 2] , _snake_case : Dict=0.02 , **_snake_case : Union[str, Any] , ): """simple docstring""" super().__init__(**_snake_case ) A__ = image_size A__ = num_channels A__ = kernel_size A__ = stride A__ = padding A__ = hidden_sizes A__ = num_attention_heads A__ = depths A__ = key_dim A__ = drop_path_rate A__ = patch_size A__ = attention_ratio A__ = mlp_ratio A__ = initializer_range A__ = [ ['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 ( UpperCAmelCase_ ): """simple docstring""" A__ : List[Any] = version.parse("1.11" ) @property def _a ( self : Union[str, Any] ): """simple docstring""" return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def _a ( self : List[Any] ): """simple docstring""" return 1E-4
52
import tempfile import unittest from make_student import create_student_by_copying_alternating_layers from transformers import AutoConfig from transformers.file_utils import cached_property from transformers.testing_utils import require_torch SCREAMING_SNAKE_CASE__ = '''sshleifer/bart-tiny-random''' SCREAMING_SNAKE_CASE__ = '''patrickvonplaten/t5-tiny-random''' @require_torch class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @cached_property def _a ( self : Optional[int] ): """simple docstring""" return AutoConfig.from_pretrained(_snake_case ) def _a ( self : Optional[Any] ): """simple docstring""" A__ , *A__ = create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=1 , d=1 ) self.assertEqual(student.config.num_hidden_layers , 1 ) def _a ( self : Optional[int] ): """simple docstring""" A__ , *A__ = create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=1 , d=_snake_case ) def _a ( self : int ): """simple docstring""" A__ , *A__ = create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=1 , d=_snake_case ) self.assertEqual(student.config.encoder_layers , 1 ) self.assertEqual(student.config.decoder_layers , self.teacher_config.encoder_layers ) def _a ( self : str ): """simple docstring""" A__ , *A__ = create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=1 , d=1 ) self.assertEqual(student.config.encoder_layers , 1 ) self.assertEqual(student.config.decoder_layers , 1 ) def _a ( self : str ): """simple docstring""" with self.assertRaises(_snake_case ): create_student_by_copying_alternating_layers(_snake_case , tempfile.mkdtemp() , e=_snake_case , d=_snake_case )
52
1
import datetime import platform import subprocess from typing import Optional, Tuple, Union import numpy as np def A ( __UpperCamelCase , __UpperCamelCase ) -> np.array: A__ = f'''{sampling_rate}''' A__ = '1' A__ = 'f32le' A__ = [ 'ffmpeg', '-i', 'pipe:0', '-ac', ac, '-ar', ar, '-f', format_for_conversion, '-hide_banner', '-loglevel', 'quiet', 'pipe:1', ] try: with subprocess.Popen(__UpperCamelCase , stdin=subprocess.PIPE , stdout=subprocess.PIPE ) as ffmpeg_process: A__ = ffmpeg_process.communicate(__UpperCamelCase ) except FileNotFoundError as error: raise ValueError('ffmpeg was not found but is required to load audio files from filename' ) from error A__ = output_stream[0] A__ = np.frombuffer(__UpperCamelCase , np.floataa ) if audio.shape[0] == 0: raise ValueError('Malformed soundfile' ) return audio def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = "f32le" , ) -> Tuple: A__ = f'''{sampling_rate}''' A__ = '1' if format_for_conversion == "s16le": A__ = 2 elif format_for_conversion == "f32le": A__ = 4 else: raise ValueError(f'''Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`''' ) A__ = platform.system() if system == "Linux": A__ = 'alsa' A__ = 'default' elif system == "Darwin": A__ = 'avfoundation' A__ = ':0' elif system == "Windows": A__ = 'dshow' A__ = 'default' A__ = [ 'ffmpeg', '-f', format_, '-i', input_, '-ac', ac, '-ar', ar, '-f', format_for_conversion, '-fflags', 'nobuffer', '-hide_banner', '-loglevel', 'quiet', 'pipe:1', ] A__ = int(round(sampling_rate * chunk_length_s ) ) * size_of_sample A__ = _ffmpeg_stream(__UpperCamelCase , __UpperCamelCase ) for item in iterator: yield item def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = "f32le" , ) -> List[str]: if stream_chunk_s is not None: A__ = stream_chunk_s else: A__ = chunk_length_s A__ = ffmpeg_microphone(__UpperCamelCase , __UpperCamelCase , format_for_conversion=__UpperCamelCase ) if format_for_conversion == "s16le": A__ = np.intaa A__ = 2 elif format_for_conversion == "f32le": A__ = np.floataa A__ = 4 else: raise ValueError(f'''Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`''' ) if stride_length_s is None: A__ = chunk_length_s / 6 A__ = int(round(sampling_rate * chunk_length_s ) ) * size_of_sample if isinstance(__UpperCamelCase , (int, float) ): A__ = [stride_length_s, stride_length_s] A__ = int(round(sampling_rate * stride_length_s[0] ) ) * size_of_sample A__ = int(round(sampling_rate * stride_length_s[1] ) ) * size_of_sample A__ = datetime.datetime.now() A__ = datetime.timedelta(seconds=__UpperCamelCase ) for item in chunk_bytes_iter(__UpperCamelCase , __UpperCamelCase , stride=(stride_left, stride_right) , stream=__UpperCamelCase ): # Put everything back in numpy scale A__ = np.frombuffer(item['raw'] , dtype=__UpperCamelCase ) A__ = ( item['stride'][0] // size_of_sample, item['stride'][1] // size_of_sample, ) A__ = sampling_rate audio_time += delta if datetime.datetime.now() > audio_time + 10 * delta: # We're late !! SKIP continue yield item def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = False ) -> Dict: A__ = b'' A__ , A__ = stride if stride_left + stride_right >= chunk_len: raise ValueError( f'''Stride needs to be strictly smaller than chunk_len: ({stride_left}, {stride_right}) vs {chunk_len}''' ) A__ = 0 for raw in iterator: acc += raw if stream and len(__UpperCamelCase ) < chunk_len: A__ = (_stride_left, 0) yield {"raw": acc[:chunk_len], "stride": stride, "partial": True} else: while len(__UpperCamelCase ) >= chunk_len: # We are flushing the accumulator A__ = (_stride_left, stride_right) A__ = {'raw': acc[:chunk_len], 'stride': stride} if stream: A__ = False yield item A__ = stride_left A__ = acc[chunk_len - stride_left - stride_right :] # Last chunk if len(__UpperCamelCase ) > stride_left: A__ = {'raw': acc, 'stride': (_stride_left, 0)} if stream: A__ = False yield item def A ( __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: A__ = 2**24 # 16Mo try: with subprocess.Popen(__UpperCamelCase , stdout=subprocess.PIPE , bufsize=__UpperCamelCase ) as ffmpeg_process: while True: A__ = ffmpeg_process.stdout.read(__UpperCamelCase ) if raw == b"": break yield raw except FileNotFoundError as error: raise ValueError('ffmpeg was not found but is required to stream audio files from filename' ) from error
52
from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" A__ : Union[str, Any] = ["image_processor", "tokenizer"] A__ : Optional[Any] = "BridgeTowerImageProcessor" A__ : List[Any] = ("RobertaTokenizer", "RobertaTokenizerFast") def __init__( self : List[Any] , _snake_case : Optional[Any] , _snake_case : Optional[int] ): """simple docstring""" super().__init__(_snake_case , _snake_case ) def __call__( self : List[Any] , _snake_case : int , _snake_case : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , _snake_case : bool = True , _snake_case : Union[bool, str, PaddingStrategy] = False , _snake_case : Union[bool, str, TruncationStrategy] = None , _snake_case : Optional[int] = None , _snake_case : int = 0 , _snake_case : Optional[int] = None , _snake_case : Optional[bool] = None , _snake_case : Optional[bool] = None , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = True , _snake_case : Optional[Union[str, TensorType]] = None , **_snake_case : Optional[int] , ): """simple docstring""" A__ = self.tokenizer( text=_snake_case , add_special_tokens=_snake_case , padding=_snake_case , truncation=_snake_case , max_length=_snake_case , stride=_snake_case , pad_to_multiple_of=_snake_case , return_token_type_ids=_snake_case , return_attention_mask=_snake_case , return_overflowing_tokens=_snake_case , return_special_tokens_mask=_snake_case , return_offsets_mapping=_snake_case , return_length=_snake_case , verbose=_snake_case , return_tensors=_snake_case , **_snake_case , ) # add pixel_values + pixel_mask A__ = self.image_processor( _snake_case , return_tensors=_snake_case , do_normalize=_snake_case , do_center_crop=_snake_case , **_snake_case ) encoding.update(_snake_case ) return encoding def _a ( self : Any , *_snake_case : Tuple , **_snake_case : List[Any] ): """simple docstring""" return self.tokenizer.batch_decode(*_snake_case , **_snake_case ) def _a ( self : Dict , *_snake_case : Dict , **_snake_case : List[str] ): """simple docstring""" return self.tokenizer.decode(*_snake_case , **_snake_case ) @property def _a ( self : Tuple ): """simple docstring""" A__ = self.tokenizer.model_input_names A__ = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
52
1
from __future__ import annotations SCREAMING_SNAKE_CASE__ = [True] * 1_0_0_0_0_0_1 SCREAMING_SNAKE_CASE__ = 2 while i * i <= 1_0_0_0_0_0_0: if seive[i]: for j in range(i * i, 1_0_0_0_0_0_1, i): SCREAMING_SNAKE_CASE__ = False i += 1 def A ( __UpperCamelCase ) -> bool: return seive[n] def A ( __UpperCamelCase ) -> bool: return any(digit in '02468' for digit in str(__UpperCamelCase ) ) def A ( __UpperCamelCase = 1_000_000 ) -> list[int]: A__ = [2] # result already includes the number 2. for num in range(3 , limit + 1 , 2 ): if is_prime(__UpperCamelCase ) and not contains_an_even_digit(__UpperCamelCase ): A__ = str(__UpperCamelCase ) A__ = [int(str_num[j:] + str_num[:j] ) for j in range(len(__UpperCamelCase ) )] if all(is_prime(__UpperCamelCase ) for i in list_nums ): result.append(__UpperCamelCase ) return result def A ( ) -> int: return len(find_circular_primes() ) if __name__ == "__main__": print(f'{len(find_circular_primes()) = }')
52
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) SCREAMING_SNAKE_CASE__ = { '''configuration_xlm_roberta''': [ '''XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XLMRobertaConfig''', '''XLMRobertaOnnxConfig''', ], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = ['''XLMRobertaTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = ['''XLMRobertaTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XLMRobertaForCausalLM''', '''XLMRobertaForMaskedLM''', '''XLMRobertaForMultipleChoice''', '''XLMRobertaForQuestionAnswering''', '''XLMRobertaForSequenceClassification''', '''XLMRobertaForTokenClassification''', '''XLMRobertaModel''', '''XLMRobertaPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXLMRobertaForCausalLM''', '''TFXLMRobertaForMaskedLM''', '''TFXLMRobertaForMultipleChoice''', '''TFXLMRobertaForQuestionAnswering''', '''TFXLMRobertaForSequenceClassification''', '''TFXLMRobertaForTokenClassification''', '''TFXLMRobertaModel''', '''TFXLMRobertaPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''FlaxXLMRobertaForMaskedLM''', '''FlaxXLMRobertaForCausalLM''', '''FlaxXLMRobertaForMultipleChoice''', '''FlaxXLMRobertaForQuestionAnswering''', '''FlaxXLMRobertaForSequenceClassification''', '''FlaxXLMRobertaForTokenClassification''', '''FlaxXLMRobertaModel''', '''FlaxXLMRobertaPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_xlm_roberta import ( XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMRobertaConfig, XLMRobertaOnnxConfig, ) try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlm_roberta import XLMRobertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlm_roberta_fast import XLMRobertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm_roberta import ( XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, XLMRobertaForCausalLM, XLMRobertaForMaskedLM, XLMRobertaForMultipleChoice, XLMRobertaForQuestionAnswering, XLMRobertaForSequenceClassification, XLMRobertaForTokenClassification, XLMRobertaModel, XLMRobertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlm_roberta import ( TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLMRobertaForCausalLM, TFXLMRobertaForMaskedLM, TFXLMRobertaForMultipleChoice, TFXLMRobertaForQuestionAnswering, TFXLMRobertaForSequenceClassification, TFXLMRobertaForTokenClassification, TFXLMRobertaModel, TFXLMRobertaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xlm_roberta import ( FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxXLMRobertaForCausalLM, FlaxXLMRobertaForMaskedLM, FlaxXLMRobertaForMultipleChoice, FlaxXLMRobertaForQuestionAnswering, FlaxXLMRobertaForSequenceClassification, FlaxXLMRobertaForTokenClassification, FlaxXLMRobertaModel, FlaxXLMRobertaPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
52
1
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 __lowerCAmelCase ( UpperCAmelCase_ ): """simple docstring""" @slow @require_torch def _a ( self : Union[str, Any] ): """simple docstring""" A__ = EncoderDecoderModel.from_encoder_decoder_pretrained('prajjwal1/bert-tiny' , 'prajjwal1/bert-tiny' ) A__ = BertTokenizer.from_pretrained('bert-base-uncased' ) A__ = bertabert.config.encoder.vocab_size A__ = tokenizer.sep_token_id A__ = tokenizer.cls_token_id A__ = 1_28 A__ = datasets.load_dataset('cnn_dailymail' , '3.0.0' , split='train[:1%]' ) A__ = datasets.load_dataset('cnn_dailymail' , '3.0.0' , split='validation[:1%]' ) A__ = train_dataset.select(range(32 ) ) A__ = val_dataset.select(range(16 ) ) A__ = 4 def _map_to_encoder_decoder_inputs(_snake_case : Optional[Any] ): # Tokenizer will automatically set [BOS] <text> [EOS] A__ = tokenizer(batch['article'] , padding='max_length' , truncation=_snake_case , max_length=5_12 ) A__ = tokenizer(batch['highlights'] , padding='max_length' , truncation=_snake_case , max_length=1_28 ) A__ = inputs.input_ids A__ = inputs.attention_mask A__ = outputs.input_ids A__ = outputs.input_ids.copy() A__ = [ [-1_00 if token == tokenizer.pad_token_id else token for token in labels] for labels in batch['labels'] ] A__ = outputs.attention_mask assert all(len(_snake_case ) == 5_12 for x in inputs.input_ids ) assert all(len(_snake_case ) == 1_28 for x in outputs.input_ids ) return batch def _compute_metrics(_snake_case : Union[str, Any] ): A__ = pred.label_ids A__ = pred.predictions # all unnecessary tokens are removed A__ = tokenizer.batch_decode(_snake_case , skip_special_tokens=_snake_case ) A__ = tokenizer.batch_decode(_snake_case , skip_special_tokens=_snake_case ) A__ = sum([int(pred_str[i] == label_str[i] ) for i in range(len(_snake_case ) )] ) / len(_snake_case ) return {"accuracy": accuracy} # map train dataset A__ = train_dataset.map( _map_to_encoder_decoder_inputs , batched=_snake_case , batch_size=_snake_case , 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 A__ = val_dataset.map( _map_to_encoder_decoder_inputs , batched=_snake_case , batch_size=_snake_case , remove_columns=['article', 'highlights'] , ) val_dataset.set_format( type='torch' , columns=['input_ids', 'attention_mask', 'decoder_input_ids', 'decoder_attention_mask', 'labels'] , ) A__ = self.get_auto_remove_tmp_dir() A__ = SeqaSeqTrainingArguments( output_dir=_snake_case , per_device_train_batch_size=_snake_case , per_device_eval_batch_size=_snake_case , predict_with_generate=_snake_case , evaluation_strategy='steps' , do_train=_snake_case , do_eval=_snake_case , warmup_steps=0 , eval_steps=2 , logging_steps=2 , ) # instantiate trainer A__ = SeqaSeqTrainer( model=_snake_case , args=_snake_case , compute_metrics=_compute_metrics , train_dataset=_snake_case , eval_dataset=_snake_case , tokenizer=_snake_case , ) # start training trainer.train()
52
import argparse import ast import logging import os import sys import pandas as pd import torch from tqdm import tqdm from transformers import BartForConditionalGeneration, RagRetriever, RagSequenceForGeneration, RagTokenForGeneration from transformers import logging as transformers_logging sys.path.append(os.path.join(os.getcwd())) # noqa: E402 # isort:skip from utils_rag import exact_match_score, fa_score # noqa: E402 # isort:skip SCREAMING_SNAKE_CASE__ = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) transformers_logging.set_verbosity_info() def A ( __UpperCamelCase ) -> Tuple: if "token" in model_name_or_path: return "rag_token" if "sequence" in model_name_or_path: return "rag_sequence" if "bart" in model_name_or_path: return "bart" return None def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[Any]: return max(metric_fn(__UpperCamelCase , __UpperCamelCase ) for gt in ground_truths ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[str]: A__ = [line.strip() for line in open(__UpperCamelCase , 'r' ).readlines()] A__ = [] if args.gold_data_mode == "qa": A__ = pd.read_csv(__UpperCamelCase , sep='\t' , header=__UpperCamelCase ) for answer_list in data[1]: A__ = ast.literal_eval(__UpperCamelCase ) answers.append(__UpperCamelCase ) else: A__ = [line.strip() for line in open(__UpperCamelCase , 'r' ).readlines()] A__ = [[reference] for reference in references] A__ = A__ = A__ = 0 for prediction, ground_truths in zip(__UpperCamelCase , __UpperCamelCase ): total += 1 em += metric_max_over_ground_truths(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) fa += metric_max_over_ground_truths(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) A__ = 100.0 * em / total A__ = 100.0 * fa / total logger.info(f'''F1: {fa:.2f}''' ) logger.info(f'''EM: {em:.2f}''' ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: A__ = args.k A__ = [line.strip() for line in open(__UpperCamelCase , 'r' ).readlines()] A__ = [line.strip() for line in open(__UpperCamelCase , 'r' ).readlines()] A__ = A__ = 0 for hypo, reference in zip(__UpperCamelCase , __UpperCamelCase ): A__ = set(hypo.split('\t' )[:k] ) A__ = set(reference.split('\t' ) ) total += 1 em += len(hypo_provenance & ref_provenance ) / k A__ = 100.0 * em / total logger.info(f'''Precision@{k}: {em: .2f}''' ) def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Union[str, Any]: def strip_title(__UpperCamelCase ): if title.startswith('"' ): A__ = title[1:] if title.endswith('"' ): A__ = title[:-1] return title A__ = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( __UpperCamelCase , return_tensors='pt' , padding=__UpperCamelCase , truncation=__UpperCamelCase , )['input_ids'].to(args.device ) A__ = rag_model.rag.question_encoder(__UpperCamelCase ) A__ = question_enc_outputs[0] A__ = rag_model.retriever( __UpperCamelCase , question_enc_pool_output.cpu().detach().to(torch.floataa ).numpy() , prefix=rag_model.rag.generator.config.prefix , n_docs=rag_model.config.n_docs , return_tensors='pt' , ) A__ = rag_model.retriever.index.get_doc_dicts(result.doc_ids ) A__ = [] for docs in all_docs: A__ = [strip_title(__UpperCamelCase ) for title in docs['title']] provenance_strings.append('\t'.join(__UpperCamelCase ) ) return provenance_strings def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Optional[Any]: with torch.no_grad(): A__ = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( __UpperCamelCase , return_tensors='pt' , padding=__UpperCamelCase , truncation=__UpperCamelCase ) A__ = inputs_dict.input_ids.to(args.device ) A__ = inputs_dict.attention_mask.to(args.device ) A__ = rag_model.generate( # rag_model overwrites generate __UpperCamelCase , attention_mask=__UpperCamelCase , num_beams=args.num_beams , min_length=args.min_length , max_length=args.max_length , early_stopping=__UpperCamelCase , num_return_sequences=1 , bad_words_ids=[[0, 0]] , ) A__ = rag_model.retriever.generator_tokenizer.batch_decode(__UpperCamelCase , skip_special_tokens=__UpperCamelCase ) if args.print_predictions: for q, a in zip(__UpperCamelCase , __UpperCamelCase ): logger.info('Q: {} - A: {}'.format(__UpperCamelCase , __UpperCamelCase ) ) return answers def A ( ) -> Any: A__ = argparse.ArgumentParser() parser.add_argument( '--model_type' , choices=['rag_sequence', 'rag_token', 'bart'] , type=__UpperCamelCase , help=( 'RAG model type: rag_sequence, rag_token or bart, if none specified, the type is inferred from the' ' model_name_or_path' ) , ) parser.add_argument( '--index_name' , default=__UpperCamelCase , choices=['exact', 'compressed', 'legacy'] , type=__UpperCamelCase , help='RAG model retriever type' , ) parser.add_argument( '--index_path' , default=__UpperCamelCase , type=__UpperCamelCase , help='Path to the retrieval index' , ) parser.add_argument('--n_docs' , default=5 , type=__UpperCamelCase , help='Number of retrieved docs' ) parser.add_argument( '--model_name_or_path' , default=__UpperCamelCase , type=__UpperCamelCase , required=__UpperCamelCase , help='Path to pretrained checkpoints or model identifier from huggingface.co/models' , ) parser.add_argument( '--eval_mode' , choices=['e2e', 'retrieval'] , default='e2e' , type=__UpperCamelCase , help=( 'Evaluation mode, e2e calculates exact match and F1 of the downstream task, retrieval calculates' ' precision@k.' ) , ) parser.add_argument('--k' , default=1 , type=__UpperCamelCase , help='k for the precision@k calculation' ) parser.add_argument( '--evaluation_set' , default=__UpperCamelCase , type=__UpperCamelCase , required=__UpperCamelCase , help='Path to a file containing evaluation samples' , ) parser.add_argument( '--gold_data_path' , default=__UpperCamelCase , type=__UpperCamelCase , required=__UpperCamelCase , help='Path to a tab-separated file with gold samples' , ) parser.add_argument( '--gold_data_mode' , default='qa' , type=__UpperCamelCase , choices=['qa', 'ans'] , help=( 'Format of the gold data file' 'qa - a single line in the following format: question [tab] answer_list' 'ans - a single line of the gold file contains the expected answer string' ) , ) parser.add_argument( '--predictions_path' , type=__UpperCamelCase , default='predictions.txt' , help='Name of the predictions file, to be stored in the checkpoints directory' , ) parser.add_argument( '--eval_all_checkpoints' , action='store_true' , help='Evaluate all checkpoints starting with the same prefix as model_name ending and ending with step number' , ) parser.add_argument( '--eval_batch_size' , default=8 , type=__UpperCamelCase , help='Batch size per GPU/CPU for evaluation.' , ) parser.add_argument( '--recalculate' , help='Recalculate predictions even if the prediction file exists' , action='store_true' , ) parser.add_argument( '--num_beams' , default=4 , type=__UpperCamelCase , help='Number of beams to be used when generating answers' , ) parser.add_argument('--min_length' , default=1 , type=__UpperCamelCase , help='Min length of the generated answers' ) parser.add_argument('--max_length' , default=50 , type=__UpperCamelCase , help='Max length of the generated answers' ) parser.add_argument( '--print_predictions' , action='store_true' , help='If True, prints predictions while evaluating.' , ) parser.add_argument( '--print_docs' , action='store_true' , help='If True, prints docs retried while generating.' , ) A__ = parser.parse_args() A__ = torch.device('cuda' if torch.cuda.is_available() else 'cpu' ) return args def A ( __UpperCamelCase ) -> int: A__ = {} if args.model_type is None: A__ = infer_model_type(args.model_name_or_path ) assert args.model_type is not None if args.model_type.startswith('rag' ): A__ = RagTokenForGeneration if args.model_type == 'rag_token' else RagSequenceForGeneration A__ = args.n_docs if args.index_name is not None: A__ = args.index_name if args.index_path is not None: A__ = args.index_path else: A__ = BartForConditionalGeneration A__ = ( [f.path for f in os.scandir(args.model_name_or_path ) if f.is_dir()] if args.eval_all_checkpoints else [args.model_name_or_path] ) logger.info('Evaluate the following checkpoints: %s' , __UpperCamelCase ) A__ = get_scores if args.eval_mode == 'e2e' else get_precision_at_k A__ = evaluate_batch_eae if args.eval_mode == 'e2e' else evaluate_batch_retrieval for checkpoint in checkpoints: if os.path.exists(args.predictions_path ) and (not args.recalculate): logger.info('Calculating metrics based on an existing predictions file: {}'.format(args.predictions_path ) ) score_fn(__UpperCamelCase , args.predictions_path , args.gold_data_path ) continue logger.info('***** Running evaluation for {} *****'.format(__UpperCamelCase ) ) logger.info(' Batch size = %d' , args.eval_batch_size ) logger.info(' Predictions will be stored under {}'.format(args.predictions_path ) ) if args.model_type.startswith('rag' ): A__ = RagRetriever.from_pretrained(__UpperCamelCase , **__UpperCamelCase ) A__ = model_class.from_pretrained(__UpperCamelCase , retriever=__UpperCamelCase , **__UpperCamelCase ) model.retriever.init_retrieval() else: A__ = model_class.from_pretrained(__UpperCamelCase , **__UpperCamelCase ) model.to(args.device ) with open(args.evaluation_set , 'r' ) as eval_file, open(args.predictions_path , 'w' ) as preds_file: A__ = [] for line in tqdm(__UpperCamelCase ): questions.append(line.strip() ) if len(__UpperCamelCase ) == args.eval_batch_size: A__ = evaluate_batch_fn(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) preds_file.write('\n'.join(__UpperCamelCase ) + '\n' ) preds_file.flush() A__ = [] if len(__UpperCamelCase ) > 0: A__ = evaluate_batch_fn(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) preds_file.write('\n'.join(__UpperCamelCase ) ) preds_file.flush() score_fn(__UpperCamelCase , args.predictions_path , args.gold_data_path ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = get_args() main(args)
52
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available SCREAMING_SNAKE_CASE__ = { '''configuration_tapas''': ['''TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TapasConfig'''], '''tokenization_tapas''': ['''TapasTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TapasForMaskedLM''', '''TapasForQuestionAnswering''', '''TapasForSequenceClassification''', '''TapasModel''', '''TapasPreTrainedModel''', '''load_tf_weights_in_tapas''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFTapasForMaskedLM''', '''TFTapasForQuestionAnswering''', '''TFTapasForSequenceClassification''', '''TFTapasModel''', '''TFTapasPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_tapas import TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP, TapasConfig from .tokenization_tapas import TapasTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tapas import ( TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST, TapasForMaskedLM, TapasForQuestionAnswering, TapasForSequenceClassification, TapasModel, TapasPreTrainedModel, load_tf_weights_in_tapas, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_tapas import ( TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST, TFTapasForMaskedLM, TFTapasForQuestionAnswering, TFTapasForSequenceClassification, TFTapasModel, TFTapasPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
52
import inspect import unittest from transformers import ViTHybridConfig from transformers.testing_utils import require_accelerate, require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class __lowerCAmelCase : """simple docstring""" def __init__( self : List[Any] , _snake_case : Any , _snake_case : Optional[int]=13 , _snake_case : Optional[Any]=64 , _snake_case : List[str]=2 , _snake_case : Any=3 , _snake_case : Union[str, Any]=True , _snake_case : Dict=True , _snake_case : int=32 , _snake_case : int=5 , _snake_case : Union[str, Any]=4 , _snake_case : int=37 , _snake_case : Tuple="gelu" , _snake_case : Optional[int]=0.1 , _snake_case : Dict=0.1 , _snake_case : List[str]=10 , _snake_case : Union[str, Any]=0.02 , _snake_case : Dict=[1, 16, 4, 4] , _snake_case : Dict=None , ): """simple docstring""" A__ = parent A__ = batch_size A__ = image_size A__ = patch_size A__ = num_channels A__ = is_training A__ = use_labels A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = type_sequence_label_size A__ = initializer_range A__ = scope A__ = backbone_featmap_shape # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) # the number of patches is based on the feature map of the backbone, which by default uses an output stride # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size A__ = (self.image_size // 32) ** 2 A__ = num_patches + 1 def _a ( self : Any ): """simple docstring""" A__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A__ = self.get_config() return config, pixel_values, labels def _a ( self : Tuple ): """simple docstring""" A__ = { 'global_padding': 'same', 'layer_type': 'bottleneck', 'depths': [3, 4, 9], 'out_features': ['stage1', 'stage2', 'stage3'], 'embedding_dynamic_padding': True, 'hidden_sizes': [4, 8, 16, 32], 'num_groups': 2, } return ViTHybridConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_snake_case , initializer_range=self.initializer_range , backbone_featmap_shape=self.backbone_featmap_shape , backbone_config=_snake_case , ) def _a ( self : int , _snake_case : Optional[int] , _snake_case : Union[str, Any] , _snake_case : Optional[int] ): """simple docstring""" A__ = ViTHybridModel(config=_snake_case ) model.to(_snake_case ) model.eval() A__ = model(_snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _a ( self : List[str] , _snake_case : str , _snake_case : Union[str, Any] , _snake_case : Any ): """simple docstring""" A__ = self.type_sequence_label_size A__ = ViTHybridForImageClassification(_snake_case ) model.to(_snake_case ) model.eval() A__ = model(_snake_case , labels=_snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _a ( self : Dict ): """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ = config_and_inputs A__ = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class __lowerCAmelCase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Union[str, Any] = (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else () A__ : str = ( {"feature-extraction": ViTHybridModel, "image-classification": ViTHybridForImageClassification} if is_torch_available() else {} ) A__ : Union[str, Any] = False A__ : Any = False A__ : Union[str, Any] = False def _a ( self : Dict ): """simple docstring""" A__ = ViTHybridModelTester(self ) A__ = ConfigTester(self , config_class=_snake_case , has_text_modality=_snake_case , hidden_size=37 ) def _a ( self : int ): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='ViT does not use inputs_embeds' ) def _a ( self : int ): """simple docstring""" pass def _a ( self : int ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(_snake_case ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A__ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_snake_case , nn.Linear ) ) def _a ( self : List[str] ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(_snake_case ) A__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ = [*signature.parameters.keys()] A__ = ['pixel_values'] self.assertListEqual(arg_names[:1] , _snake_case ) def _a ( self : Any ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def _a ( self : str ): """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_snake_case ) def _a ( self : Any ): """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = _config_zero_init(_snake_case ) for model_class in self.all_model_classes: A__ = model_class(config=_snake_case ) # Skip the check for the backbone for name, module in model.named_modules(): if module.__class__.__name__ == "ViTHybridPatchEmbeddings": A__ = [F'''{name}.{key}''' for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() , [0.0, 1.0] , msg=F'''Parameter {name} of model {model_class} seems not properly initialized''' , ) @slow def _a ( self : int ): """simple docstring""" for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = ViTHybridModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def A ( ) -> Union[str, Any]: A__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @cached_property def _a ( self : Tuple ): """simple docstring""" return ( ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def _a ( self : Optional[Any] ): """simple docstring""" A__ = ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to( _snake_case ) A__ = self.default_image_processor A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ).to(_snake_case ) # forward pass with torch.no_grad(): A__ = model(**_snake_case ) # verify the logits A__ = torch.Size((1, 10_00) ) self.assertEqual(outputs.logits.shape , _snake_case ) A__ = torch.tensor([-1.9090, -0.4993, -0.2389] ).to(_snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _snake_case , atol=1E-4 ) ) @slow @require_accelerate def _a ( self : List[Any] ): """simple docstring""" A__ = ViTHybridImageProcessor.from_pretrained('google/vit-hybrid-base-bit-384' ) A__ = ViTHybridForImageClassification.from_pretrained('google/vit-hybrid-base-bit-384' , device_map='auto' ) A__ = prepare_img() A__ = image_processor(images=_snake_case , return_tensors='pt' ) A__ = model(**_snake_case ) A__ = outputs.logits # model predicts one of the 1000 ImageNet classes A__ = logits.argmax(-1 ).item() self.assertTrue(model.config.idalabel[predicted_class_idx] , 'tabby, tabby cat' )
52
1
from ...configuration_utils import PretrainedConfig from ...utils import logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = { "facebook/timesformer": "https://huggingface.co/facebook/timesformer/resolve/main/config.json", } class __lowerCAmelCase ( _lowerCamelCase ): """simple docstring""" A__ : Union[str, Any] = "timesformer" def __init__( self : List[Any] , _snake_case : Optional[int]=2_24 , _snake_case : str=16 , _snake_case : List[Any]=3 , _snake_case : Optional[int]=8 , _snake_case : List[Any]=7_68 , _snake_case : str=12 , _snake_case : Tuple=12 , _snake_case : Any=30_72 , _snake_case : Union[str, Any]="gelu" , _snake_case : List[Any]=0.0 , _snake_case : int=0.0 , _snake_case : Any=0.02 , _snake_case : Any=1E-6 , _snake_case : Optional[Any]=True , _snake_case : Union[str, Any]="divided_space_time" , _snake_case : Any=0 , **_snake_case : Union[str, Any] , ): """simple docstring""" super().__init__(**A__ ) A__ = image_size A__ = patch_size A__ = num_channels A__ = num_frames 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__ = qkv_bias A__ = attention_type A__ = drop_path_rate
700
def A ( __UpperCamelCase ) -> bool: return number & 1 == 0 if __name__ == "__main__": import doctest doctest.testmod()
52
0