code
stringlengths
82
53.2k
code_codestyle
int64
0
721
style_context
stringlengths
91
41.9k
style_context_codestyle
int64
0
699
label
int64
0
1
from sklearn.metrics import fa_score, matthews_corrcoef import datasets from .record_evaluation import evaluate as evaluate_record lowercase : Union[str, Any] = """\ @article{wang2019superglue, title={SuperGLUE: A Stickier Benchmark for General-Purpose Language Understanding Systems}, author={Wang, Alex and Pruksachatkun, Yada and Nangia, Nikita and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R}, journal={arXiv preprint arXiv:1905.00537}, year={2019} } """ lowercase : Union[str, Any] = """\ SuperGLUE (https://super.gluebenchmark.com/) is a new benchmark styled after GLUE with a new set of more difficult language understanding tasks, improved resources, and a new public leaderboard. """ lowercase : Tuple = """ Compute SuperGLUE evaluation metric associated to each SuperGLUE dataset. Args: predictions: list of predictions to score. Depending on the SuperGlUE subset: - for 'record': list of question-answer dictionaries with the following keys: - 'idx': index of the question as specified by the dataset - 'prediction_text': the predicted answer text - for 'multirc': list of question-answer dictionaries with the following keys: - 'idx': index of the question-answer pair as specified by the dataset - 'prediction': the predicted answer label - otherwise: list of predicted labels references: list of reference labels. Depending on the SuperGLUE subset: - for 'record': list of question-answers dictionaries with the following keys: - 'idx': index of the question as specified by the dataset - 'answers': list of possible answers - otherwise: list of reference labels Returns: depending on the SuperGLUE subset: - for 'record': - 'exact_match': Exact match between answer and gold answer - 'f1': F1 score - for 'multirc': - 'exact_match': Exact match between answer and gold answer - 'f1_m': Per-question macro-F1 score - 'f1_a': Average F1 score over all answers - for 'axb': 'matthews_correlation': Matthew Correlation - for 'cb': - 'accuracy': Accuracy - 'f1': F1 score - for all others: - 'accuracy': Accuracy Examples: >>> super_glue_metric = datasets.load_metric('super_glue', 'copa') # any of [\"copa\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"boolq\", \"axg\"] >>> predictions = [0, 1] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'accuracy': 1.0} >>> super_glue_metric = datasets.load_metric('super_glue', 'cb') >>> predictions = [0, 1] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'accuracy': 1.0, 'f1': 1.0} >>> super_glue_metric = datasets.load_metric('super_glue', 'record') >>> predictions = [{'idx': {'passage': 0, 'query': 0}, 'prediction_text': 'answer'}] >>> references = [{'idx': {'passage': 0, 'query': 0}, 'answers': ['answer', 'another_answer']}] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'exact_match': 1.0, 'f1': 1.0} >>> super_glue_metric = datasets.load_metric('super_glue', 'multirc') >>> predictions = [{'idx': {'answer': 0, 'paragraph': 0, 'question': 0}, 'prediction': 0}, {'idx': {'answer': 1, 'paragraph': 2, 'question': 3}, 'prediction': 1}] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'exact_match': 1.0, 'f1_m': 1.0, 'f1_a': 1.0} >>> super_glue_metric = datasets.load_metric('super_glue', 'axb') >>> references = [0, 1] >>> predictions = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'matthews_correlation': 1.0} """ def A_ ( A__ , A__ ) -> Optional[Any]: return float((preds == labels).mean() ) def A_ ( A__ , A__ , A__="binary" ) -> Optional[Any]: a__ : int = simple_accuracy(A__ , A__ ) a__ : Dict = float(fa_score(y_true=A__ , y_pred=A__ , average=A__ ) ) return { "accuracy": acc, "f1": fa, } def A_ ( A__ , A__ ) -> str: a__ : str = {} for id_pred, label in zip(A__ , A__ ): a__ : Optional[int] = F'{id_pred["idx"]["paragraph"]}-{id_pred["idx"]["question"]}' a__ : Dict = id_pred['prediction'] if question_id in question_map: question_map[question_id].append((pred, label) ) else: a__ : Tuple = [(pred, label)] a__ , a__ : Optional[int] = [], [] for question, preds_labels in question_map.items(): a__ , a__ : Union[str, Any] = zip(*A__ ) a__ : List[Any] = fa_score(y_true=A__ , y_pred=A__ , average='macro' ) fas.append(A__ ) a__ : Optional[int] = int(sum(pred == label for pred, label in preds_labels ) == len(A__ ) ) ems.append(A__ ) a__ : str = float(sum(A__ ) / len(A__ ) ) a__ : Dict = sum(A__ ) / len(A__ ) a__ : Any = float(fa_score(y_true=A__ , y_pred=[id_pred['prediction'] for id_pred in ids_preds] ) ) return {"exact_match": em, "f1_m": fa_m, "f1_a": fa_a} @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A__ ( datasets.Metric ): """simple docstring""" def __lowercase ( self) -> List[str]: '''simple docstring''' if self.config_name not in [ "boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg", ]: raise KeyError( 'You should supply a configuration name selected in ' '["boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg",]') return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features(self._get_feature_types()) , codebase_urls=[] , reference_urls=[] , format='numpy' if not self.config_name == 'record' and not self.config_name == 'multirc' else None , ) def __lowercase ( self) -> Tuple: '''simple docstring''' if self.config_name == "record": return { "predictions": { "idx": { "passage": datasets.Value('int64'), "query": datasets.Value('int64'), }, "prediction_text": datasets.Value('string'), }, "references": { "idx": { "passage": datasets.Value('int64'), "query": datasets.Value('int64'), }, "answers": datasets.Sequence(datasets.Value('string')), }, } elif self.config_name == "multirc": return { "predictions": { "idx": { "answer": datasets.Value('int64'), "paragraph": datasets.Value('int64'), "question": datasets.Value('int64'), }, "prediction": datasets.Value('int64'), }, "references": datasets.Value('int64'), } else: return { "predictions": datasets.Value('int64'), "references": datasets.Value('int64'), } def __lowercase ( self , lowercase , lowercase) -> Dict: '''simple docstring''' if self.config_name == "axb": return {"matthews_correlation": matthews_corrcoef(lowercase , lowercase)} elif self.config_name == "cb": return acc_and_fa(lowercase , lowercase , fa_avg='macro') elif self.config_name == "record": a__ : List[Any] = [ { 'qas': [ {'id': ref['idx']['query'], 'answers': [{'text': ans} for ans in ref['answers']]} for ref in references ] } ] a__ : Dict = {pred['idx']['query']: pred['prediction_text'] for pred in predictions} return evaluate_record(lowercase , lowercase)[0] elif self.config_name == "multirc": return evaluate_multirc(lowercase , lowercase) elif self.config_name in ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"]: return {"accuracy": simple_accuracy(lowercase , lowercase)} else: raise KeyError( 'You should supply a configuration name selected in ' '["boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg",]')
302
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPImageProcessor, CLIPProcessor @require_vision class A__ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self) -> Dict: '''simple docstring''' a__ : str = tempfile.mkdtemp() # fmt: off a__ : Tuple = ['l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', 'lo', 'l</w>', 'w</w>', 'r</w>', 't</w>', 'low</w>', 'er</w>', 'lowest</w>', 'newer</w>', 'wider', '<unk>', '<|startoftext|>', '<|endoftext|>'] # fmt: on a__ : Tuple = dict(zip(lowercase , range(len(lowercase)))) a__ : List[Any] = ['#version: 0.2', 'l o', 'lo w</w>', 'e r</w>', ''] a__ : Dict = {'unk_token': '<unk>'} a__ : List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file']) a__ : Optional[Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['merges_file']) with open(self.vocab_file , 'w' , encoding='utf-8') as fp: fp.write(json.dumps(lowercase) + '\n') with open(self.merges_file , 'w' , encoding='utf-8') as fp: fp.write('\n'.join(lowercase)) a__ : str = { 'do_resize': True, 'size': 20, 'do_center_crop': True, 'crop_size': 18, 'do_normalize': True, 'image_mean': [0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73], 'image_std': [0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11], } a__ : Optional[Any] = os.path.join(self.tmpdirname , lowercase) with open(self.image_processor_file , 'w' , encoding='utf-8') as fp: json.dump(lowercase , lowercase) def __lowercase ( self , **lowercase) -> str: '''simple docstring''' return CLIPTokenizer.from_pretrained(self.tmpdirname , **lowercase) def __lowercase ( self , **lowercase) -> int: '''simple docstring''' return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **lowercase) def __lowercase ( self , **lowercase) -> List[Any]: '''simple docstring''' return CLIPImageProcessor.from_pretrained(self.tmpdirname , **lowercase) def __lowercase ( self) -> Tuple: '''simple docstring''' shutil.rmtree(self.tmpdirname) def __lowercase ( self) -> List[Any]: '''simple docstring''' a__ : List[Any] = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta)] a__ : Tuple = [Image.fromarray(np.moveaxis(lowercase , 0 , -1)) for x in image_inputs] return image_inputs def __lowercase ( self) -> Optional[Any]: '''simple docstring''' a__ : List[Any] = self.get_tokenizer() a__ : Optional[Any] = self.get_rust_tokenizer() a__ : str = self.get_image_processor() a__ : str = CLIPProcessor(tokenizer=lowercase , image_processor=lowercase) processor_slow.save_pretrained(self.tmpdirname) a__ : List[Any] = CLIPProcessor.from_pretrained(self.tmpdirname , use_fast=lowercase) a__ : Tuple = CLIPProcessor(tokenizer=lowercase , image_processor=lowercase) processor_fast.save_pretrained(self.tmpdirname) a__ : Optional[Any] = CLIPProcessor.from_pretrained(self.tmpdirname) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab()) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab()) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab()) self.assertIsInstance(processor_slow.tokenizer , lowercase) self.assertIsInstance(processor_fast.tokenizer , lowercase) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string()) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string()) self.assertIsInstance(processor_slow.image_processor , lowercase) self.assertIsInstance(processor_fast.image_processor , lowercase) def __lowercase ( self) -> Tuple: '''simple docstring''' a__ : Dict = CLIPProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor()) processor.save_pretrained(self.tmpdirname) a__ : Optional[int] = self.get_tokenizer(bos_token='(BOS)' , eos_token='(EOS)') a__ : Any = self.get_image_processor(do_normalize=lowercase , padding_value=1.0) a__ : int = CLIPProcessor.from_pretrained( self.tmpdirname , bos_token='(BOS)' , eos_token='(EOS)' , do_normalize=lowercase , padding_value=1.0) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab()) self.assertIsInstance(processor.tokenizer , lowercase) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string()) self.assertIsInstance(processor.image_processor , lowercase) def __lowercase ( self) -> List[Any]: '''simple docstring''' a__ : Optional[Any] = self.get_image_processor() a__ : str = self.get_tokenizer() a__ : str = CLIPProcessor(tokenizer=lowercase , image_processor=lowercase) a__ : Optional[int] = self.prepare_image_inputs() a__ : Optional[int] = image_processor(lowercase , return_tensors='np') a__ : Optional[int] = processor(images=lowercase , return_tensors='np') for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1e-2) def __lowercase ( self) -> Tuple: '''simple docstring''' a__ : List[Any] = self.get_image_processor() a__ : Union[str, Any] = self.get_tokenizer() a__ : Any = CLIPProcessor(tokenizer=lowercase , image_processor=lowercase) a__ : Optional[Any] = 'lower newer' a__ : Optional[int] = processor(text=lowercase) a__ : Tuple = tokenizer(lowercase) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key]) def __lowercase ( self) -> List[Any]: '''simple docstring''' a__ : Optional[int] = self.get_image_processor() a__ : Tuple = self.get_tokenizer() a__ : Union[str, Any] = CLIPProcessor(tokenizer=lowercase , image_processor=lowercase) a__ : List[str] = 'lower newer' a__ : Tuple = self.prepare_image_inputs() a__ : List[str] = processor(text=lowercase , images=lowercase) self.assertListEqual(list(inputs.keys()) , ['input_ids', 'attention_mask', 'pixel_values']) # test if it raises when no input is passed with pytest.raises(lowercase): processor() def __lowercase ( self) -> Tuple: '''simple docstring''' a__ : Optional[int] = self.get_image_processor() a__ : Dict = self.get_tokenizer() a__ : Any = CLIPProcessor(tokenizer=lowercase , image_processor=lowercase) a__ : List[str] = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] a__ : Optional[int] = processor.batch_decode(lowercase) a__ : int = tokenizer.batch_decode(lowercase) self.assertListEqual(lowercase , lowercase) def __lowercase ( self) -> str: '''simple docstring''' a__ : List[Any] = self.get_image_processor() a__ : Union[str, Any] = self.get_tokenizer() a__ : List[str] = CLIPProcessor(tokenizer=lowercase , image_processor=lowercase) a__ : int = 'lower newer' a__ : List[Any] = self.prepare_image_inputs() a__ : List[str] = processor(text=lowercase , images=lowercase) self.assertListEqual(list(inputs.keys()) , processor.model_input_names)
302
1
"""simple docstring""" import gc import unittest import numpy as np import torch from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNetaDModel from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps from ..pipeline_params import UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS, UNCONDITIONAL_AUDIO_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class _lowercase ( a__ , unittest.TestCase ): '''simple docstring''' _A = DanceDiffusionPipeline _A = UNCONDITIONAL_AUDIO_GENERATION_PARAMS _A = PipelineTesterMixin.required_optional_params - { 'callback', 'latents', 'callback_steps', 'output_type', 'num_images_per_prompt', } _A = UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS _A = False _A = False def lowerCAmelCase__ ( self )-> Optional[Any]: torch.manual_seed(0 ) UpperCAmelCase__ : List[Any] = UNetaDModel( block_out_channels=(32, 32, 64) , extra_in_channels=16 , sample_size=5_12 , sample_rate=1_60_00 , in_channels=2 , out_channels=2 , flip_sin_to_cos=_A , use_timestep_embedding=_A , time_embedding_type="fourier" , mid_block_type="UNetMidBlock1D" , down_block_types=("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D") , up_block_types=("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip") , ) UpperCAmelCase__ : int = IPNDMScheduler() UpperCAmelCase__ : Union[str, Any] = { 'unet': unet, 'scheduler': scheduler, } return components def lowerCAmelCase__ ( self , __UpperCamelCase , __UpperCamelCase=0 )-> Optional[Any]: if str(_A ).startswith("mps" ): UpperCAmelCase__ : str = torch.manual_seed(_A ) else: UpperCAmelCase__ : Optional[Any] = torch.Generator(device=_A ).manual_seed(_A ) UpperCAmelCase__ : int = { 'batch_size': 1, 'generator': generator, 'num_inference_steps': 4, } return inputs def lowerCAmelCase__ ( self )-> Any: UpperCAmelCase__ : List[str] = 'cpu' # ensure determinism for the device-dependent torch.Generator UpperCAmelCase__ : int = self.get_dummy_components() UpperCAmelCase__ : Optional[Any] = DanceDiffusionPipeline(**_A ) UpperCAmelCase__ : int = pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) UpperCAmelCase__ : Union[str, Any] = self.get_dummy_inputs(_A ) UpperCAmelCase__ : List[str] = pipe(**_A ) UpperCAmelCase__ : List[Any] = output.audios UpperCAmelCase__ : List[str] = audio[0, -3:, -3:] assert audio.shape == (1, 2, components["unet"].sample_size) UpperCAmelCase__ : Optional[Any] = np.array([-0.7265, 1.0000, -0.8388, 0.1175, 0.9498, -1.0000] ) assert np.abs(audio_slice.flatten() - expected_slice ).max() < 1E-2 @skip_mps def lowerCAmelCase__ ( self )-> str: return super().test_save_load_local() @skip_mps def lowerCAmelCase__ ( self )-> str: return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3 ) @skip_mps def lowerCAmelCase__ ( self )-> Any: return super().test_save_load_optional_components() @skip_mps def lowerCAmelCase__ ( self )-> List[Any]: return super().test_attention_slicing_forward_pass() def lowerCAmelCase__ ( self )-> Dict: super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class _lowercase ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase__ ( self )-> List[Any]: super().tearDown() gc.collect() torch.cuda.empty_cache() def lowerCAmelCase__ ( self )-> Optional[Any]: UpperCAmelCase__ : List[Any] = torch_device UpperCAmelCase__ : int = DanceDiffusionPipeline.from_pretrained("harmonai/maestro-150k" ) UpperCAmelCase__ : int = pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) UpperCAmelCase__ : Optional[int] = torch.manual_seed(0 ) UpperCAmelCase__ : str = pipe(generator=_A , num_inference_steps=1_00 , audio_length_in_s=4.096 ) UpperCAmelCase__ : str = output.audios UpperCAmelCase__ : List[str] = audio[0, -3:, -3:] assert audio.shape == (1, 2, pipe.unet.sample_size) UpperCAmelCase__ : Union[str, Any] = np.array([-0.0192, -0.0231, -0.0318, -0.0059, 0.0002, -0.0020] ) assert np.abs(audio_slice.flatten() - expected_slice ).max() < 1E-2 def lowerCAmelCase__ ( self )-> str: UpperCAmelCase__ : str = torch_device UpperCAmelCase__ : Tuple = DanceDiffusionPipeline.from_pretrained("harmonai/maestro-150k" , torch_dtype=torch.floataa ) UpperCAmelCase__ : Optional[int] = pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) UpperCAmelCase__ : Union[str, Any] = torch.manual_seed(0 ) UpperCAmelCase__ : Optional[int] = pipe(generator=_A , num_inference_steps=1_00 , audio_length_in_s=4.096 ) UpperCAmelCase__ : Union[str, Any] = output.audios UpperCAmelCase__ : int = audio[0, -3:, -3:] assert audio.shape == (1, 2, pipe.unet.sample_size) UpperCAmelCase__ : List[str] = np.array([-0.0367, -0.0488, -0.0771, -0.0525, -0.0444, -0.0341] ) assert np.abs(audio_slice.flatten() - expected_slice ).max() < 1E-2
716
"""simple docstring""" import timeit import numpy as np import datasets from datasets.arrow_writer import ArrowWriter from datasets.features.features import _ArrayXD def a__ ( lowerCAmelCase : List[str] ): '''simple docstring''' def wrapper(*lowerCAmelCase : Any , **lowerCAmelCase : Tuple ): UpperCAmelCase__ : Optional[int] = timeit.default_timer() UpperCAmelCase__ : int = func(*lowerCAmelCase , **lowerCAmelCase ) UpperCAmelCase__ : List[Any] = timeit.default_timer() - starttime return delta UpperCAmelCase__ : int = func.__name__ return wrapper def a__ ( lowerCAmelCase : dict , lowerCAmelCase : Optional[int]=100 , lowerCAmelCase : List[str]=None ): '''simple docstring''' UpperCAmelCase__ : str = [] UpperCAmelCase__ : Optional[Any] = seq_shapes or {} for i in range(lowerCAmelCase ): UpperCAmelCase__ : int = {} for col_id, (k, v) in enumerate(features.items() ): if isinstance(lowerCAmelCase , _ArrayXD ): UpperCAmelCase__ : List[str] = np.random.rand(*v.shape ).astype(v.dtype ) elif isinstance(lowerCAmelCase , datasets.Value ): if v.dtype == "string": UpperCAmelCase__ : Dict = "The small grey turtle was surprisingly fast when challenged." else: UpperCAmelCase__ : str = np.random.randint(10 , size=1 ).astype(v.dtype ).item() elif isinstance(lowerCAmelCase , datasets.Sequence ): while isinstance(lowerCAmelCase , datasets.Sequence ): UpperCAmelCase__ : List[str] = v.feature UpperCAmelCase__ : Optional[int] = seq_shapes[k] UpperCAmelCase__ : Optional[int] = np.random.rand(*lowerCAmelCase ).astype(v.dtype ) UpperCAmelCase__ : Union[str, Any] = data dummy_data.append((i, example) ) return dummy_data def a__ ( lowerCAmelCase : List[str] , lowerCAmelCase : Tuple , lowerCAmelCase : List[str]=100 , lowerCAmelCase : Optional[int]=None ): '''simple docstring''' UpperCAmelCase__ : int = generate_examples(lowerCAmelCase , num_examples=lowerCAmelCase , seq_shapes=lowerCAmelCase ) with ArrowWriter(features=lowerCAmelCase , path=lowerCAmelCase ) as writer: for key, record in dummy_data: UpperCAmelCase__ : List[Any] = features.encode_example(lowerCAmelCase ) writer.write(lowerCAmelCase ) UpperCAmelCase__ , UpperCAmelCase__ : Optional[Any] = writer.finalize() if not num_final_examples == num_examples: raise ValueError( F"Error writing the dataset, wrote {num_final_examples} examples but should have written {num_examples}." ) UpperCAmelCase__ : Optional[int] = datasets.Dataset.from_file(filename=lowerCAmelCase , info=datasets.DatasetInfo(features=lowerCAmelCase ) ) return dataset
660
0
"""simple docstring""" from __future__ import annotations def lowercase__ ( snake_case_ :int ): __UpperCAmelCase = [True] * limit __UpperCAmelCase = False __UpperCAmelCase = False __UpperCAmelCase = True for i in range(3 , int(limit**0.5 + 1 ) , 2 ): __UpperCAmelCase = i * 2 while index < limit: __UpperCAmelCase = False __UpperCAmelCase = index + i __UpperCAmelCase = [2] for i in range(3 , snake_case_ , 2 ): if is_prime[i]: primes.append(snake_case_ ) return primes def lowercase__ ( snake_case_ :int = 1_000_000 ): __UpperCAmelCase = prime_sieve(snake_case_ ) __UpperCAmelCase = 0 __UpperCAmelCase = 0 for i in range(len(snake_case_ ) ): for j in range(i + length , len(snake_case_ ) ): __UpperCAmelCase = sum(primes[i:j] ) if sol >= ceiling: break if sol in primes: __UpperCAmelCase = j - i __UpperCAmelCase = sol return largest if __name__ == "__main__": print(f"""{solution() = }""")
49
from __future__ import annotations lowerCAmelCase__ : Union[str, Any] =[ [-1, 0], # left [0, -1], # down [1, 0], # right [0, 1], # up ] def a__ ( A__, A__, A__, A__, A__, ): SCREAMING_SNAKE_CASE_ : List[Any] = [ [0 for col in range(len(grid[0] ) )] for row in range(len(A__ ) ) ] # the reference grid SCREAMING_SNAKE_CASE_ : List[Any] = 1 SCREAMING_SNAKE_CASE_ : Union[str, Any] = [ [0 for col in range(len(grid[0] ) )] for row in range(len(A__ ) ) ] # the action grid SCREAMING_SNAKE_CASE_ : Optional[int] = init[0] SCREAMING_SNAKE_CASE_ : List[str] = init[1] SCREAMING_SNAKE_CASE_ : Dict = 0 SCREAMING_SNAKE_CASE_ : List[Any] = g + heuristic[x][y] # cost from starting cell to destination cell SCREAMING_SNAKE_CASE_ : List[str] = [[f, g, x, y]] SCREAMING_SNAKE_CASE_ : Dict = False # flag that is set when search is complete SCREAMING_SNAKE_CASE_ : Tuple = False # flag set if we can't find expand while not found and not resign: if len(A__ ) == 0: raise ValueError('Algorithm is unable to find solution' ) else: # to choose the least costliest action so as to move closer to the goal cell.sort() cell.reverse() SCREAMING_SNAKE_CASE_ : Any = cell.pop() SCREAMING_SNAKE_CASE_ : Any = next_cell[2] SCREAMING_SNAKE_CASE_ : str = next_cell[3] SCREAMING_SNAKE_CASE_ : Tuple = next_cell[1] if x == goal[0] and y == goal[1]: SCREAMING_SNAKE_CASE_ : Dict = True else: for i in range(len(A__ ) ): # to try out different valid actions SCREAMING_SNAKE_CASE_ : Optional[int] = x + DIRECTIONS[i][0] SCREAMING_SNAKE_CASE_ : List[Any] = y + DIRECTIONS[i][1] if xa >= 0 and xa < len(A__ ) and ya >= 0 and ya < len(grid[0] ): if closed[xa][ya] == 0 and grid[xa][ya] == 0: SCREAMING_SNAKE_CASE_ : List[Any] = g + cost SCREAMING_SNAKE_CASE_ : List[Any] = ga + heuristic[xa][ya] cell.append([fa, ga, xa, ya] ) SCREAMING_SNAKE_CASE_ : int = 1 SCREAMING_SNAKE_CASE_ : int = i SCREAMING_SNAKE_CASE_ : Union[str, Any] = [] SCREAMING_SNAKE_CASE_ : List[str] = goal[0] SCREAMING_SNAKE_CASE_ : Optional[Any] = goal[1] invpath.append([x, y] ) # we get the reverse path from here while x != init[0] or y != init[1]: SCREAMING_SNAKE_CASE_ : Any = x - DIRECTIONS[action[x][y]][0] SCREAMING_SNAKE_CASE_ : int = y - DIRECTIONS[action[x][y]][1] SCREAMING_SNAKE_CASE_ : int = xa SCREAMING_SNAKE_CASE_ : List[Any] = ya invpath.append([x, y] ) SCREAMING_SNAKE_CASE_ : List[Any] = [] for i in range(len(A__ ) ): path.append(invpath[len(A__ ) - 1 - i] ) return path, action if __name__ == "__main__": lowerCAmelCase__ : Union[str, Any] =[ [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0], [0, 0, 0, 0, 1, 0], ] lowerCAmelCase__ : Any =[0, 0] # all coordinates are given in format [y,x] lowerCAmelCase__ : List[str] =[len(grid) - 1, len(grid[0]) - 1] lowerCAmelCase__ : str =1 # the cost map which pushes the path closer to the goal lowerCAmelCase__ : Union[str, Any] =[[0 for row in range(len(grid[0]))] for col in range(len(grid))] for i in range(len(grid)): for j in range(len(grid[0])): lowerCAmelCase__ : List[str] =abs(i - goal[0]) + abs(j - goal[1]) if grid[i][j] == 1: # added extra penalty in the heuristic map lowerCAmelCase__ : List[str] =99 lowerCAmelCase__ ,lowerCAmelCase__ : Any =search(grid, init, goal, cost, heuristic) print('ACTION MAP') for i in range(len(action)): print(action[i]) for i in range(len(path)): print(path[i])
101
0
from __future__ import annotations # This is the precision for this function which can be altered. # It is recommended for users to keep this number greater than or equal to 10. _snake_case = 10 def A ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): '''simple docstring''' for i in range(_lowerCamelCase , _lowerCamelCase ): if array[i] == target: return i return -1 def A ( _lowerCamelCase , _lowerCamelCase ): '''simple docstring''' _lowerCAmelCase : int = 0 _lowerCAmelCase : Union[str, Any] = len(_lowerCamelCase ) while left <= right: if right - left < precision: return lin_search(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) _lowerCAmelCase : Optional[int] = (left + right) // 3 + 1 _lowerCAmelCase : Optional[int] = 2 * (left + right) // 3 + 1 if array[one_third] == target: return one_third elif array[two_third] == target: return two_third elif target < array[one_third]: _lowerCAmelCase : List[Any] = one_third - 1 elif array[two_third] < target: _lowerCAmelCase : Any = two_third + 1 else: _lowerCAmelCase : Optional[int] = one_third + 1 _lowerCAmelCase : Union[str, Any] = two_third - 1 else: return -1 def A ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): '''simple docstring''' if left < right: if right - left < precision: return lin_search(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) _lowerCAmelCase : int = (left + right) // 3 + 1 _lowerCAmelCase : int = 2 * (left + right) // 3 + 1 if array[one_third] == target: return one_third elif array[two_third] == target: return two_third elif target < array[one_third]: return rec_ternary_search(_lowerCamelCase , one_third - 1 , _lowerCamelCase , _lowerCamelCase ) elif array[two_third] < target: return rec_ternary_search(two_third + 1 , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) else: return rec_ternary_search(one_third + 1 , two_third - 1 , _lowerCamelCase , _lowerCamelCase ) else: return -1 if __name__ == "__main__": import doctest doctest.testmod() _snake_case = input("Enter numbers separated by comma:\n").strip() _snake_case = [int(item.strip()) for item in user_input.split(",")] assert collection == sorted(collection), f"List must be ordered.\n{collection}." _snake_case = int(input("Enter the number to be found in the list:\n").strip()) _snake_case = ite_ternary_search(collection, target) _snake_case = rec_ternary_search(0, len(collection) - 1, collection, target) if resulta != -1: print(f'''Iterative search: {target} found at positions: {resulta}''') print(f'''Recursive search: {target} found at positions: {resulta}''') else: print("Not found")
708
def A ( _lowerCamelCase ): '''simple docstring''' _lowerCAmelCase : int = len(_lowerCamelCase ) for i in range(1 , _lowerCamelCase ): _lowerCAmelCase : List[Any] = collection[i] _lowerCAmelCase : str = 0 _lowerCAmelCase : Union[str, Any] = i - 1 while low <= high: _lowerCAmelCase : List[str] = (low + high) // 2 if val < collection[mid]: _lowerCAmelCase : Optional[int] = mid - 1 else: _lowerCAmelCase : List[str] = mid + 1 for j in range(_lowerCamelCase , _lowerCamelCase , -1 ): _lowerCAmelCase : int = collection[j - 1] _lowerCAmelCase : Optional[int] = val return collection if __name__ == "__main__": _snake_case = input("Enter numbers separated by a comma:\n").strip() _snake_case = [int(item) for item in user_input.split(",")] print(binary_insertion_sort(unsorted))
658
0
from typing import Optional import pyspark from .. import Features, NamedSplit from ..download import DownloadMode from ..packaged_modules.spark.spark import Spark from .abc import AbstractDatasetReader class UpperCAmelCase ( __snake_case ): def __init__( self : int , __magic_name__ : pyspark.sql.DataFrame , __magic_name__ : Optional[NamedSplit] = None , __magic_name__ : Optional[Features] = None , __magic_name__ : bool = True , __magic_name__ : str = None , __magic_name__ : bool = False , __magic_name__ : str = None , __magic_name__ : bool = True , __magic_name__ : str = "arrow" , **__magic_name__ : List[str] , ): """simple docstring""" super().__init__( split=SCREAMING_SNAKE_CASE__ , features=SCREAMING_SNAKE_CASE__ , cache_dir=SCREAMING_SNAKE_CASE__ , keep_in_memory=SCREAMING_SNAKE_CASE__ , streaming=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ , ) UpperCamelCase = load_from_cache_file UpperCamelCase = file_format UpperCamelCase = Spark( df=SCREAMING_SNAKE_CASE__ , features=SCREAMING_SNAKE_CASE__ , cache_dir=SCREAMING_SNAKE_CASE__ , working_dir=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ , ) def lowerCamelCase_ ( self : Union[str, Any] ): """simple docstring""" if self.streaming: return self.builder.as_streaming_dataset(split=self.split ) UpperCamelCase = None if self._load_from_cache_file else DownloadMode.FORCE_REDOWNLOAD self.builder.download_and_prepare( download_mode=SCREAMING_SNAKE_CASE__ , file_format=self._file_format , ) return self.builder.as_dataset(split=self.split )
386
def __lowerCamelCase ( _lowercase ) -> list: for i in range(len(_lowercase ) - 1 , 0 , -1 ): UpperCamelCase = False for j in range(_lowercase , 0 , -1 ): if unsorted[j] < unsorted[j - 1]: UpperCamelCase , UpperCamelCase = unsorted[j - 1], unsorted[j] UpperCamelCase = True for j in range(_lowercase ): if unsorted[j] > unsorted[j + 1]: UpperCamelCase , UpperCamelCase = unsorted[j + 1], unsorted[j] UpperCamelCase = True if not swapped: break return unsorted if __name__ == "__main__": import doctest doctest.testmod() _snake_case = input('''Enter numbers separated by a comma:\n''').strip() _snake_case = [int(item) for item in user_input.split(''',''')] print(F"{cocktail_shaker_sort(unsorted) = }")
282
0
"""simple docstring""" def lowerCamelCase__ ( _lowerCamelCase ): '''simple docstring''' if not isinstance(_lowerCamelCase , _lowerCamelCase ): _lowerCAmelCase : List[str] = f"""Input value of [number={number}] must be an integer""" raise TypeError(_lowerCamelCase ) if number < 0: return False _lowerCAmelCase : str = number * number while number > 0: if number % 10 != number_square % 10: return False number //= 10 number_square //= 10 return True if __name__ == "__main__": import doctest doctest.testmod()
16
"""simple docstring""" from collections.abc import Callable class __UpperCamelCase : def __init__( self ,_A = None ): '''simple docstring''' _lowerCAmelCase : list = [] # Stores indexes of each item for supporting updates and deletion. _lowerCAmelCase : dict = {} # Stores current size of heap. _lowerCAmelCase : Union[str, Any] = 0 # Stores function used to evaluate the score of an item on which basis ordering # will be done. _lowerCAmelCase : Union[str, Any] = key or (lambda _A : x) def __lowerCamelCase ( self ,_A ): '''simple docstring''' return int((i - 1) / 2 ) if i > 0 else None def __lowerCamelCase ( self ,_A ): '''simple docstring''' _lowerCAmelCase : Union[str, Any] = int(2 * i + 1 ) return left if 0 < left < self.size else None def __lowerCamelCase ( self ,_A ): '''simple docstring''' _lowerCAmelCase : str = int(2 * i + 2 ) return right if 0 < right < self.size else None def __lowerCamelCase ( self ,_A ,_A ): '''simple docstring''' _lowerCAmelCase, _lowerCAmelCase : Tuple = ( self.pos_map[self.arr[j][0]], self.pos_map[self.arr[i][0]], ) # Then swap the items in the list. _lowerCAmelCase, _lowerCAmelCase : Tuple = self.arr[j], self.arr[i] def __lowerCamelCase ( self ,_A ,_A ): '''simple docstring''' return self.arr[i][1] < self.arr[j][1] def __lowerCamelCase ( self ,_A ): '''simple docstring''' _lowerCAmelCase : Tuple = self._left(_A ) _lowerCAmelCase : str = self._right(_A ) _lowerCAmelCase : Tuple = i if left is not None and not self._cmp(_A ,_A ): _lowerCAmelCase : int = left if right is not None and not self._cmp(_A ,_A ): _lowerCAmelCase : Optional[int] = right return valid_parent def __lowerCamelCase ( self ,_A ): '''simple docstring''' _lowerCAmelCase : Any = self._parent(_A ) while parent is not None and not self._cmp(_A ,_A ): self._swap(_A ,_A ) _lowerCAmelCase, _lowerCAmelCase : List[str] = parent, self._parent(_A ) def __lowerCamelCase ( self ,_A ): '''simple docstring''' _lowerCAmelCase : Union[str, Any] = self._get_valid_parent(_A ) while valid_parent != index: self._swap(_A ,_A ) _lowerCAmelCase, _lowerCAmelCase : Optional[int] = valid_parent, self._get_valid_parent(_A ) def __lowerCamelCase ( self ,_A ,_A ): '''simple docstring''' if item not in self.pos_map: return _lowerCAmelCase : int = self.pos_map[item] _lowerCAmelCase : Dict = [item, self.key(_A )] # Make sure heap is right in both up and down direction. # Ideally only one of them will make any change. self._heapify_up(_A ) self._heapify_down(_A ) def __lowerCamelCase ( self ,_A ): '''simple docstring''' if item not in self.pos_map: return _lowerCAmelCase : List[str] = self.pos_map[item] del self.pos_map[item] _lowerCAmelCase : Dict = self.arr[self.size - 1] _lowerCAmelCase : Optional[Any] = index self.size -= 1 # Make sure heap is right in both up and down direction. Ideally only one # of them will make any change- so no performance loss in calling both. if self.size > index: self._heapify_up(_A ) self._heapify_down(_A ) def __lowerCamelCase ( self ,_A ,_A ): '''simple docstring''' _lowerCAmelCase : Tuple = len(self.arr ) if arr_len == self.size: self.arr.append([item, self.key(_A )] ) else: _lowerCAmelCase : Any = [item, self.key(_A )] _lowerCAmelCase : str = self.size self.size += 1 self._heapify_up(self.size - 1 ) def __lowerCamelCase ( self ): '''simple docstring''' return self.arr[0] if self.size else None def __lowerCamelCase ( self ): '''simple docstring''' _lowerCAmelCase : int = self.get_top() if top_item_tuple: self.delete_item(top_item_tuple[0] ) return top_item_tuple def lowerCamelCase__ ( ): '''simple docstring''' if __name__ == "__main__": import doctest doctest.testmod()
16
1
"""simple docstring""" import os from typing import Dict, List, Tuple, TypeVar, Union __A = TypeVar("""T""") __A = Union[List[T], Tuple[T, ...]] __A = Union[T, List[T], Dict[str, T]] __A = Union[str, bytes, os.PathLike]
93
from typing import TYPE_CHECKING from ....utils import _LazyModule _lowercase = {'''tokenization_tapex''': ['''TapexTokenizer''']} if TYPE_CHECKING: from .tokenization_tapex import TapexTokenizer else: import sys _lowercase = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
659
0
'''simple docstring''' # Copyright 2022 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os import platform import numpy as np import psutil import torch from accelerate import __version__ as version from accelerate.commands.config import default_config_file, load_config_from_file from ..utils import is_npu_available, is_xpu_available def _UpperCamelCase ( UpperCamelCase__=None ): if subparsers is not None: UpperCAmelCase__ : Dict = subparsers.add_parser("""env""" ) else: UpperCAmelCase__ : Dict = argparse.ArgumentParser("""Accelerate env command""" ) parser.add_argument( """--config_file""" , default=UpperCamelCase__ , help="""The config file to use for the default values in the launching script.""" ) if subparsers is not None: parser.set_defaults(func=UpperCamelCase__ ) return parser def _UpperCamelCase ( UpperCamelCase__ ): UpperCAmelCase__ : List[Any] = torch.__version__ UpperCAmelCase__ : Union[str, Any] = torch.cuda.is_available() UpperCAmelCase__ : Dict = is_xpu_available() UpperCAmelCase__ : Tuple = is_npu_available() UpperCAmelCase__ : int = """Not found""" # Get the default from the config file. if args.config_file is not None or os.path.isfile(UpperCamelCase__ ): UpperCAmelCase__ : List[str] = load_config_from_file(args.config_file ).to_dict() UpperCAmelCase__ : List[str] = { """`Accelerate` version""": version, """Platform""": platform.platform(), """Python version""": platform.python_version(), """Numpy version""": np.__version__, """PyTorch version (GPU?)""": f'''{pt_version} ({pt_cuda_available})''', """PyTorch XPU available""": str(UpperCamelCase__ ), """PyTorch NPU available""": str(UpperCamelCase__ ), """System RAM""": f'''{psutil.virtual_memory().total / 1_0_2_4 ** 3:.2f} GB''', } if pt_cuda_available: UpperCAmelCase__ : str = torch.cuda.get_device_name() print("""\nCopy-and-paste the text below in your GitHub issue\n""" ) print("""\n""".join([f'''- {prop}: {val}''' for prop, val in info.items()] ) ) print("""- `Accelerate` default config:""" if args.config_file is None else """- `Accelerate` config passed:""" ) UpperCAmelCase__ : str = ( """\n""".join([f'''\t- {prop}: {val}''' for prop, val in accelerate_config.items()] ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else f'''\t{accelerate_config}''' ) print(UpperCamelCase__ ) UpperCAmelCase__ : Dict = accelerate_config return info def _UpperCamelCase ( ): UpperCAmelCase__ : Any = env_command_parser() UpperCAmelCase__ : Tuple = parser.parse_args() env_command(UpperCamelCase__ ) return 0 if __name__ == "__main__": raise SystemExit(main())
113
'''simple docstring''' import os def _UpperCamelCase ( UpperCamelCase__ = "input.txt" ): with open(os.path.join(os.path.dirname(UpperCamelCase__ ) , UpperCamelCase__ ) ) as input_file: UpperCAmelCase__ : Tuple = [ [int(UpperCamelCase__ ) for element in line.split(""",""" )] for line in input_file.readlines() ] UpperCAmelCase__ : Optional[Any] = len(UpperCamelCase__ ) UpperCAmelCase__ : Any = len(matrix[0] ) UpperCAmelCase__ : Optional[int] = [[-1 for _ in range(UpperCamelCase__ )] for _ in range(UpperCamelCase__ )] for i in range(UpperCamelCase__ ): UpperCAmelCase__ : Any = matrix[i][0] for j in range(1 , UpperCamelCase__ ): for i in range(UpperCamelCase__ ): UpperCAmelCase__ : Any = minimal_path_sums[i][j - 1] + matrix[i][j] for i in range(1 , UpperCamelCase__ ): UpperCAmelCase__ : Dict = min( minimal_path_sums[i][j] , minimal_path_sums[i - 1][j] + matrix[i][j] ) for i in range(rows - 2 , -1 , -1 ): UpperCAmelCase__ : int = min( minimal_path_sums[i][j] , minimal_path_sums[i + 1][j] + matrix[i][j] ) return min(minimal_path_sums_row[-1] for minimal_path_sums_row in minimal_path_sums ) if __name__ == "__main__": print(f"""{solution() = }""")
113
1
from __future__ import annotations from math import pi def _a ( lowercase__ : float , lowercase__ : float , lowercase__ : float ): '''simple docstring''' if (inductance, frequency, reactance).count(0 ) != 1: raise ValueError('One and only one argument must be 0' ) if inductance < 0: raise ValueError('Inductance cannot be negative' ) if frequency < 0: raise ValueError('Frequency cannot be negative' ) if reactance < 0: raise ValueError('Inductive reactance cannot be negative' ) if inductance == 0: return {"inductance": reactance / (2 * pi * frequency)} elif frequency == 0: return {"frequency": reactance / (2 * pi * inductance)} elif reactance == 0: return {"reactance": 2 * pi * frequency * inductance} else: raise ValueError('Exactly one argument must be 0' ) if __name__ == "__main__": import doctest doctest.testmod()
85
"""simple docstring""" from math import sqrt def UpperCAmelCase ( A : int = 100_0000 ): '''simple docstring''' _UpperCAmelCase = 0 _UpperCAmelCase = 0 _UpperCAmelCase = 42 while num_cuboids <= limit: max_cuboid_size += 1 for sum_shortest_sides in range(2 , 2 * max_cuboid_size + 1 ): if sqrt(sum_shortest_sides**2 + max_cuboid_size**2 ).is_integer(): num_cuboids += ( min(A , sum_shortest_sides // 2 ) - max(1 , sum_shortest_sides - max_cuboid_size ) + 1 ) return max_cuboid_size if __name__ == "__main__": print(F'''{solution() = }''')
573
0
"""simple docstring""" __UpperCAmelCase ={ """A""": """.-""", """B""": """-...""", """C""": """-.-.""", """D""": """-..""", """E""": """.""", """F""": """..-.""", """G""": """--.""", """H""": """....""", """I""": """..""", """J""": """.---""", """K""": """-.-""", """L""": """.-..""", """M""": """--""", """N""": """-.""", """O""": """---""", """P""": """.--.""", """Q""": """--.-""", """R""": """.-.""", """S""": """...""", """T""": """-""", """U""": """..-""", """V""": """...-""", """W""": """.--""", """X""": """-..-""", """Y""": """-.--""", """Z""": """--..""", """1""": """.----""", """2""": """..---""", """3""": """...--""", """4""": """....-""", """5""": """.....""", """6""": """-....""", """7""": """--...""", """8""": """---..""", """9""": """----.""", """0""": """-----""", """&""": """.-...""", """@""": """.--.-.""", """:""": """---...""", """,""": """--..--""", """.""": """.-.-.-""", """'""": """.----.""", """\"""": """.-..-.""", """?""": """..--..""", """/""": """-..-.""", """=""": """-...-""", """+""": """.-.-.""", """-""": """-....-""", """(""": """-.--.""", """)""": """-.--.-""", """!""": """-.-.--""", """ """: """/""" } # Exclamation mark is not in ITU-R recommendation # fmt: on __UpperCAmelCase ={value: key for key, value in MORSE_CODE_DICT.items()} def __a ( A ) -> str: '''simple docstring''' return " ".join(MORSE_CODE_DICT[char] for char in message.upper() ) def __a ( A ) -> str: '''simple docstring''' return "".join(REVERSE_DICT[char] for char in message.split() ) def __a ( ) -> None: '''simple docstring''' A__ = "Morse code here!" print(__snake_case ) A__ = encrypt(__snake_case ) print(__snake_case ) A__ = decrypt(__snake_case ) print(__snake_case ) if __name__ == "__main__": main()
712
"""simple docstring""" import io import os import unicodedata from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ="""▁""" __UpperCAmelCase ={"""vocab_file""": """vocab.txt""", """sentencepiece_model_ckpt""": """sentencepiece.bpe.model"""} __UpperCAmelCase ={ """sentencepiece_model_file""": """sentencepiece.bpe.model""", """vocab_file""": """vocab.txt""", } __UpperCAmelCase ={ """vocab_file""": { """ernie-m-base""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt""", """ernie-m-large""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt""", }, """sentencepiece_model_file""": { """ernie-m-base""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model""", """ernie-m-large""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model""", }, } __UpperCAmelCase ={ """ernie-m-base""": 514, """ernie-m-large""": 514, } __UpperCAmelCase ={ """ernie-m-base""": {"""do_lower_case""": False}, """ernie-m-large""": {"""do_lower_case""": False}, } class lowerCAmelCase__ ( UpperCAmelCase_ ): lowercase__ : List[str] = ["input_ids"] lowercase__ : Tuple = VOCAB_FILES_NAMES lowercase__ : Dict = PRETRAINED_INIT_CONFIGURATION lowercase__ : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase__ : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP lowercase__ : Dict = RESOURCE_FILES_NAMES def __init__( self , UpperCamelCase__ , UpperCamelCase__=None , UpperCamelCase__=False , UpperCamelCase__="utf8" , UpperCamelCase__="[UNK]" , UpperCamelCase__="[SEP]" , UpperCamelCase__="[PAD]" , UpperCamelCase__="[CLS]" , UpperCamelCase__="[MASK]" , UpperCamelCase__ = None , **UpperCamelCase__ , ): '''simple docstring''' A__ = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=UpperCamelCase__ , unk_token=UpperCamelCase__ , sep_token=UpperCamelCase__ , pad_token=UpperCamelCase__ , cls_token=UpperCamelCase__ , mask_token=UpperCamelCase__ , vocab_file=UpperCamelCase__ , encoding=UpperCamelCase__ , sp_model_kwargs=self.sp_model_kwargs , **UpperCamelCase__ , ) A__ = do_lower_case A__ = sentencepiece_model_ckpt A__ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(UpperCamelCase__ ) # to mimic paddlenlp.transformers.ernie_m.tokenizer.ErnieMTokenizer functioning if vocab_file is not None: A__ = self.load_vocab(filepath=UpperCamelCase__ ) else: A__ = {self.sp_model.id_to_piece(UpperCamelCase__ ): id for id in range(self.sp_model.get_piece_size() )} A__ = {v: k for k, v in self.vocab.items()} def lowercase_ ( self , UpperCamelCase__ ): '''simple docstring''' if text is None: return None A__ = self.tokenize(UpperCamelCase__ ) A__ , A__ = "", [] for i, ch in enumerate(UpperCamelCase__ ): if ch in self.SP_CHAR_MAPPING: A__ = self.SP_CHAR_MAPPING.get(UpperCamelCase__ ) else: A__ = unicodedata.normalize("NFKC" , UpperCamelCase__ ) if self.is_whitespace(UpperCamelCase__ ): continue normalized_text += ch char_mapping.extend([i] * len(UpperCamelCase__ ) ) A__ , A__ , A__ = normalized_text, [], 0 if self.do_lower_case: A__ = text.lower() for token in split_tokens: if token[:1] == "▁": A__ = token[1:] A__ = text[offset:].index(UpperCamelCase__ ) + offset A__ = start + len(UpperCamelCase__ ) token_mapping.append((char_mapping[start], char_mapping[end - 1] + 1) ) A__ = end return token_mapping @property def lowercase_ ( self ): '''simple docstring''' return len(self.vocab ) def lowercase_ ( self ): '''simple docstring''' return dict(self.vocab , **self.added_tokens_encoder ) def __getstate__( self ): '''simple docstring''' A__ = self.__dict__.copy() A__ = None return state def __setstate__( self , UpperCamelCase__ ): '''simple docstring''' A__ = d # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): A__ = {} A__ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.sentencepiece_model_ckpt ) def lowercase_ ( self , UpperCamelCase__ ): '''simple docstring''' return "".join((self.SP_CHAR_MAPPING.get(UpperCamelCase__ , UpperCamelCase__ ) for c in text) ) def lowercase_ ( self , UpperCamelCase__ , UpperCamelCase__=False , UpperCamelCase__=64 , UpperCamelCase__=0.1 ): '''simple docstring''' if self.sp_model_kwargs.get("enable_sampling" ) is True: A__ = True if self.sp_model_kwargs.get("alpha" ) is not None: A__ = self.sp_model_kwargs.get("alpha" ) if self.sp_model_kwargs.get("nbest_size" ) is not None: A__ = self.sp_model_kwargs.get("nbest_size" ) if not enable_sampling: A__ = self.sp_model.EncodeAsPieces(UpperCamelCase__ ) else: A__ = self.sp_model.SampleEncodeAsPieces(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) A__ = [] for pi, piece in enumerate(UpperCamelCase__ ): if piece == SPIECE_UNDERLINE: if not pieces[pi + 1].startswith(UpperCamelCase__ ) and pi != 0: new_pieces.append(UpperCamelCase__ ) continue else: continue A__ = 0 for i, chunk in enumerate(UpperCamelCase__ ): if chunk == SPIECE_UNDERLINE: continue if self.is_ch_char(UpperCamelCase__ ) or self.is_punct(UpperCamelCase__ ): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) new_pieces.append(UpperCamelCase__ ) A__ = i + 1 elif chunk.isdigit() and i > 0 and not piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) A__ = i elif not chunk.isdigit() and i > 0 and piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) A__ = i if len(UpperCamelCase__ ) > lst_i: new_pieces.append(piece[lst_i:] ) return new_pieces def lowercase_ ( self , UpperCamelCase__ ): '''simple docstring''' A__ = "".join(UpperCamelCase__ ).replace(UpperCamelCase__ , " " ).strip() return out_string def lowercase_ ( self , UpperCamelCase__ ): '''simple docstring''' A__ = self.convert_ids_to_tokens(UpperCamelCase__ ) A__ = "".join(UpperCamelCase__ ).replace(UpperCamelCase__ , " " ).strip() return out_string def lowercase_ ( self , UpperCamelCase__ ): '''simple docstring''' return self.vocab.get(UpperCamelCase__ , self.vocab.get(self.unk_token ) ) def lowercase_ ( self , UpperCamelCase__ ): '''simple docstring''' return self.reverse_vocab.get(UpperCamelCase__ , self.unk_token ) def lowercase_ ( self , UpperCamelCase__ , UpperCamelCase__=None ): '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] A__ = [self.cls_token_id] A__ = [self.sep_token_id] return _cls + token_ids_a + _sep + _sep + token_ids_a + _sep def lowercase_ ( self , UpperCamelCase__ , UpperCamelCase__=None ): '''simple docstring''' if offset_mapping_a is None: return [(0, 0)] + offset_mapping_a + [(0, 0)] return [(0, 0)] + offset_mapping_a + [(0, 0), (0, 0)] + offset_mapping_a + [(0, 0)] def lowercase_ ( self , UpperCamelCase__ , UpperCamelCase__=None , UpperCamelCase__=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(UpperCamelCase__ )) + [1, 1] + ([0] * len(UpperCamelCase__ )) + [1] return [1] + ([0] * len(UpperCamelCase__ )) + [1] def lowercase_ ( self , UpperCamelCase__ , UpperCamelCase__ = None ): '''simple docstring''' if token_ids_a is None: # [CLS] X [SEP] return (len(UpperCamelCase__ ) + 2) * [0] # [CLS] A [SEP] [SEP] B [SEP] return [0] * (len(UpperCamelCase__ ) + 1) + [1] * (len(UpperCamelCase__ ) + 3) def lowercase_ ( self , UpperCamelCase__ ): '''simple docstring''' if "\u4e00" <= char <= "\u9fff": return True return False def lowercase_ ( self , UpperCamelCase__ ): '''simple docstring''' if ("a" <= char <= "z") or ("A" <= char <= "Z"): return True return False def lowercase_ ( self , UpperCamelCase__ ): '''simple docstring''' if char in ",;:.?!~,;:。?!《》【】": return True return False def lowercase_ ( self , UpperCamelCase__ ): '''simple docstring''' if char == " " or char == "\t" or char == "\n" or char == "\r": return True if len(UpperCamelCase__ ) == 1: A__ = unicodedata.category(UpperCamelCase__ ) if cat == "Zs": return True return False def lowercase_ ( self , UpperCamelCase__ ): '''simple docstring''' A__ = {} with io.open(UpperCamelCase__ , "r" , encoding="utf-8" ) as f: for index, line in enumerate(UpperCamelCase__ ): A__ = line.rstrip("\n" ) A__ = int(UpperCamelCase__ ) return token_to_idx def lowercase_ ( self , UpperCamelCase__ , UpperCamelCase__ = None ): '''simple docstring''' A__ = 0 if os.path.isdir(UpperCamelCase__ ): A__ = os.path.join( UpperCamelCase__ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) else: A__ = (filename_prefix + "-" if filename_prefix else "") + save_directory with open(UpperCamelCase__ , "w" , encoding="utf-8" ) as writer: for token, token_index in sorted(self.vocab.items() , key=lambda UpperCamelCase__ : kv[1] ): if index != token_index: logger.warning( f"""Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.""" " Please check that the vocabulary is not corrupted!" ) A__ = token_index writer.write(token + "\n" ) index += 1 A__ = os.path.join(UpperCamelCase__ , "sentencepiece.bpe.model" ) with open(UpperCamelCase__ , "wb" ) as fi: A__ = self.sp_model.serialized_model_proto() fi.write(UpperCamelCase__ ) return (vocab_file,)
261
0
from __future__ import annotations __a = list[list[int]] # assigning initial values to the grid __a = [ [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 __a = [ [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 lowerCamelCase__ ( _lowercase , _lowercase , _lowercase , _lowercase ): '''simple docstring''' 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 lowerCamelCase__ ( _lowercase ): '''simple docstring''' for i in range(9 ): for j in range(9 ): if grid[i][j] == 0: return i, j return None def lowerCamelCase__ ( _lowercase ): '''simple docstring''' if location := find_empty_location(_lowercase ): UpperCAmelCase_, UpperCAmelCase_ : List[Any] = location else: # If the location is ``None``, then the grid is solved. return grid for digit in range(1 , 10 ): if is_safe(_lowercase , _lowercase , _lowercase , _lowercase ): UpperCAmelCase_ : Union[str, Any] = digit if sudoku(_lowercase ) is not None: return grid UpperCAmelCase_ : Tuple = 0 return None def lowerCamelCase__ ( _lowercase ): '''simple docstring''' for row in grid: for cell in row: print(_lowercase , 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' + '=' * 20) print_solution(example_grid) print('\nExample grid solution:') __a = sudoku(example_grid) if solution is not None: print_solution(solution) else: print('Cannot find a solution.')
30
import os import shutil import sys import tempfile import unittest from pathlib import Path import pytest import transformers from transformers import ( BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, AutoTokenizer, BertConfig, BertTokenizer, BertTokenizerFast, CTRLTokenizer, GPTaTokenizer, GPTaTokenizerFast, PreTrainedTokenizerFast, RobertaTokenizer, RobertaTokenizerFast, is_tokenizers_available, ) from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig from transformers.models.auto.tokenization_auto import ( TOKENIZER_MAPPING, get_tokenizer_config, tokenizer_class_from_name, ) from transformers.models.roberta.configuration_roberta import RobertaConfig from transformers.testing_utils import ( DUMMY_DIFF_TOKENIZER_IDENTIFIER, DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, RequestCounter, require_tokenizers, slow, ) sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils')) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_tokenization import CustomTokenizer # noqa E402 if is_tokenizers_available(): from test_module.custom_tokenization_fast import CustomTokenizerFast class __a( unittest.TestCase ): """simple docstring""" def a__ ( self ) -> Union[str, Any]: UpperCAmelCase_ : Tuple = 0 @slow def a__ ( self ) -> Any: for model_name in (x for x in BERT_PRETRAINED_CONFIG_ARCHIVE_MAP.keys() if "japanese" not in x): UpperCAmelCase_ : Optional[Any] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,(BertTokenizer, BertTokenizerFast) ) self.assertGreater(len(_SCREAMING_SNAKE_CASE ) ,0 ) for model_name in GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP.keys(): UpperCAmelCase_ : Union[str, Any] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,(GPTaTokenizer, GPTaTokenizerFast) ) self.assertGreater(len(_SCREAMING_SNAKE_CASE ) ,0 ) def a__ ( self ) -> Optional[Any]: UpperCAmelCase_ : List[str] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,(BertTokenizer, BertTokenizerFast) ) self.assertEqual(tokenizer.vocab_size ,12 ) def a__ ( self ) -> Tuple: UpperCAmelCase_ : Any = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,(RobertaTokenizer, RobertaTokenizerFast) ) self.assertEqual(tokenizer.vocab_size ,20 ) def a__ ( self ) -> List[str]: UpperCAmelCase_ : int = AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) # Check that tokenizer_type ≠ model_type UpperCAmelCase_ : Optional[int] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,config=_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,(BertTokenizer, BertTokenizerFast) ) self.assertEqual(tokenizer.vocab_size ,12 ) def a__ ( self ) -> Dict: with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.txt''' ,os.path.join(_SCREAMING_SNAKE_CASE ,'''vocab.txt''' ) ) UpperCAmelCase_ : Optional[int] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,tokenizer_type='''bert''' ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.json''' ,os.path.join(_SCREAMING_SNAKE_CASE ,'''vocab.json''' ) ) shutil.copy('''./tests/fixtures/merges.txt''' ,os.path.join(_SCREAMING_SNAKE_CASE ,'''merges.txt''' ) ) UpperCAmelCase_ : Optional[Any] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,tokenizer_type='''gpt2''' ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) @require_tokenizers def a__ ( self ) -> Optional[int]: with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.txt''' ,os.path.join(_SCREAMING_SNAKE_CASE ,'''vocab.txt''' ) ) UpperCAmelCase_ : Tuple = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,tokenizer_type='''bert''' ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.json''' ,os.path.join(_SCREAMING_SNAKE_CASE ,'''vocab.json''' ) ) shutil.copy('''./tests/fixtures/merges.txt''' ,os.path.join(_SCREAMING_SNAKE_CASE ,'''merges.txt''' ) ) UpperCAmelCase_ : Union[str, Any] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,tokenizer_type='''gpt2''' ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) def a__ ( self ) -> int: with pytest.raises(_SCREAMING_SNAKE_CASE ): AutoTokenizer.from_pretrained('''./''' ,tokenizer_type='''xxx''' ) @require_tokenizers def a__ ( self ) -> Optional[Any]: for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]: UpperCAmelCase_ : Any = tokenizer_class.from_pretrained('''wietsedv/bert-base-dutch-cased''' ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,(BertTokenizer, BertTokenizerFast) ) if isinstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ): self.assertEqual(tokenizer.basic_tokenizer.do_lower_case ,_SCREAMING_SNAKE_CASE ) else: self.assertEqual(tokenizer.do_lower_case ,_SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.model_max_length ,512 ) @require_tokenizers def a__ ( self ) -> List[Any]: for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]: with self.assertRaisesRegex( _SCREAMING_SNAKE_CASE ,'''julien-c/herlolip-not-exists is not a local folder and is not a valid model identifier''' ,): UpperCAmelCase_ : int = tokenizer_class.from_pretrained('''julien-c/herlolip-not-exists''' ) def a__ ( self ) -> Optional[Any]: # tests: https://github.com/huggingface/transformers/pull/13251 # 1. models with `-`, e.g. xlm-roberta -> xlm_roberta # 2. models that don't remap 1-1 from model-name to model file, e.g., openai-gpt -> openai UpperCAmelCase_ : int = TOKENIZER_MAPPING.values() UpperCAmelCase_ : List[Any] = [] for slow_tok, fast_tok in tokenizers: if slow_tok is not None: tokenizer_names.append(slow_tok.__name__ ) if fast_tok is not None: tokenizer_names.append(fast_tok.__name__ ) for tokenizer_name in tokenizer_names: # must find the right class tokenizer_class_from_name(_SCREAMING_SNAKE_CASE ) @require_tokenizers def a__ ( self ) -> Tuple: self.assertIsInstance(AutoTokenizer.from_pretrained('''bert-base-cased''' ,use_fast=_SCREAMING_SNAKE_CASE ) ,_SCREAMING_SNAKE_CASE ) self.assertIsInstance(AutoTokenizer.from_pretrained('''bert-base-cased''' ) ,_SCREAMING_SNAKE_CASE ) @require_tokenizers def a__ ( self ) -> Optional[int]: UpperCAmelCase_ : str = AutoTokenizer.from_pretrained('''distilbert-base-uncased''' ,do_lower_case=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = '''Hello, world. How are you?''' UpperCAmelCase_ : List[Any] = tokenizer.tokenize(_SCREAMING_SNAKE_CASE ) self.assertEqual('''[UNK]''' ,tokens[0] ) UpperCAmelCase_ : Any = AutoTokenizer.from_pretrained('''microsoft/mpnet-base''' ,do_lower_case=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Tuple = tokenizer.tokenize(_SCREAMING_SNAKE_CASE ) self.assertEqual('''[UNK]''' ,tokens[0] ) @require_tokenizers def a__ ( self ) -> Dict: UpperCAmelCase_ : List[Any] = AutoTokenizer.from_pretrained('''robot-test/dummy-tokenizer-fast-with-model-config''' ) self.assertEqual(type(_SCREAMING_SNAKE_CASE ) ,_SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.model_max_length ,512 ) self.assertEqual(tokenizer.vocab_size ,30_000 ) self.assertEqual(tokenizer.unk_token ,'''[UNK]''' ) self.assertEqual(tokenizer.padding_side ,'''right''' ) self.assertEqual(tokenizer.truncation_side ,'''right''' ) def a__ ( self ) -> Dict: UpperCAmelCase_ : Any = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,(BertTokenizer, BertTokenizerFast) ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,tokenizer.__class__ ) self.assertEqual(tokenizera.vocab_size ,12 ) def a__ ( self ) -> Optional[Any]: UpperCAmelCase_ : Tuple = AutoTokenizer.from_pretrained('''ctrl''' ) # There is no fast CTRL so this always gives us a slow tokenizer. self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) def a__ ( self ) -> str: # Check we can load the tokenizer config of an online model. UpperCAmelCase_ : int = get_tokenizer_config('''bert-base-cased''' ) UpperCAmelCase_ : Optional[int] = config.pop('''_commit_hash''' ,_SCREAMING_SNAKE_CASE ) # If we ever update bert-base-cased tokenizer config, this dict here will need to be updated. self.assertEqual(_SCREAMING_SNAKE_CASE ,{'''do_lower_case''': False} ) # This model does not have a tokenizer_config so we get back an empty dict. UpperCAmelCase_ : Any = get_tokenizer_config(_SCREAMING_SNAKE_CASE ) self.assertDictEqual(_SCREAMING_SNAKE_CASE ,{} ) # A tokenizer saved with `save_pretrained` always creates a tokenizer config. UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = get_tokenizer_config(_SCREAMING_SNAKE_CASE ) # Check the class of the tokenizer was properly saved (note that it always saves the slow class). self.assertEqual(config['''tokenizer_class'''] ,'''BertTokenizer''' ) def a__ ( self ) -> str: try: AutoConfig.register('''custom''' ,_SCREAMING_SNAKE_CASE ) AutoTokenizer.register(_SCREAMING_SNAKE_CASE ,slow_tokenizer_class=_SCREAMING_SNAKE_CASE ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_SCREAMING_SNAKE_CASE ): AutoTokenizer.register(_SCREAMING_SNAKE_CASE ,slow_tokenizer_class=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : str = CustomTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] @require_tokenizers def a__ ( self ) -> int: try: AutoConfig.register('''custom''' ,_SCREAMING_SNAKE_CASE ) # Can register in two steps AutoTokenizer.register(_SCREAMING_SNAKE_CASE ,slow_tokenizer_class=_SCREAMING_SNAKE_CASE ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] ,(CustomTokenizer, None) ) AutoTokenizer.register(_SCREAMING_SNAKE_CASE ,fast_tokenizer_class=_SCREAMING_SNAKE_CASE ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] ,(CustomTokenizer, CustomTokenizerFast) ) del TOKENIZER_MAPPING._extra_content[CustomConfig] # Can register in one step AutoTokenizer.register( _SCREAMING_SNAKE_CASE ,slow_tokenizer_class=_SCREAMING_SNAKE_CASE ,fast_tokenizer_class=_SCREAMING_SNAKE_CASE ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] ,(CustomTokenizer, CustomTokenizerFast) ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_SCREAMING_SNAKE_CASE ): AutoTokenizer.register(_SCREAMING_SNAKE_CASE ,fast_tokenizer_class=_SCREAMING_SNAKE_CASE ) # We pass through a bert tokenizer fast cause there is no converter slow to fast for our new toknizer # and that model does not have a tokenizer.json with tempfile.TemporaryDirectory() as tmp_dir: UpperCAmelCase_ : List[str] = BertTokenizerFast.from_pretrained(_SCREAMING_SNAKE_CASE ) bert_tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : str = CustomTokenizerFast.from_pretrained(_SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[int] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Any = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] def a__ ( self ) -> Optional[int]: # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Tuple = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' ) # If remote code is disabled, we can't load this config. with self.assertRaises(_SCREAMING_SNAKE_CASE ): UpperCAmelCase_ : Union[str, Any] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ) self.assertTrue(tokenizer.special_attribute_present ) # Test tokenizer can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,trust_remote_code=_SCREAMING_SNAKE_CASE ) self.assertTrue(reloaded_tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizerFast''' ) self.assertEqual(reloaded_tokenizer.__class__.__name__ ,'''NewTokenizerFast''' ) # Test we can also load the slow version UpperCAmelCase_ : List[str] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertTrue(tokenizer.special_attribute_present ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) # Test tokenizer can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[int] = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,trust_remote_code=_SCREAMING_SNAKE_CASE ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertEqual(reloaded_tokenizer.__class__.__name__ ,'''NewTokenizer''' ) self.assertTrue(reloaded_tokenizer.special_attribute_present ) else: self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) self.assertEqual(reloaded_tokenizer.__class__.__name__ ,'''NewTokenizer''' ) @require_tokenizers def a__ ( self ) -> Optional[int]: class __a( _a ): """simple docstring""" lowerCAmelCase = False class __a( _a ): """simple docstring""" lowerCAmelCase = NewTokenizer lowerCAmelCase = False try: AutoConfig.register('''custom''' ,_SCREAMING_SNAKE_CASE ) AutoTokenizer.register(_SCREAMING_SNAKE_CASE ,slow_tokenizer_class=_SCREAMING_SNAKE_CASE ) AutoTokenizer.register(_SCREAMING_SNAKE_CASE ,fast_tokenizer_class=_SCREAMING_SNAKE_CASE ) # If remote code is not set, the default is to use local UpperCAmelCase_ : int = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizerFast''' ) self.assertFalse(tokenizer.special_attribute_present ) UpperCAmelCase_ : Any = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) self.assertFalse(tokenizer.special_attribute_present ) # If remote code is disabled, we load the local one. UpperCAmelCase_ : Tuple = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizerFast''' ) self.assertFalse(tokenizer.special_attribute_present ) UpperCAmelCase_ : List[str] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) self.assertFalse(tokenizer.special_attribute_present ) # If remote is enabled, we load from the Hub UpperCAmelCase_ : Optional[int] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizerFast''' ) self.assertTrue(tokenizer.special_attribute_present ) UpperCAmelCase_ : Any = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) self.assertTrue(tokenizer.special_attribute_present ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] def a__ ( self ) -> int: UpperCAmelCase_ : Optional[int] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer_legacy''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ) self.assertTrue(tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizerFast''' ) # Test we can also load the slow version UpperCAmelCase_ : Tuple = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer_legacy''' ,trust_remote_code=_SCREAMING_SNAKE_CASE ,use_fast=_SCREAMING_SNAKE_CASE ) self.assertTrue(tokenizer.special_attribute_present ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) else: self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) def a__ ( self ) -> Optional[Any]: with self.assertRaisesRegex( _SCREAMING_SNAKE_CASE ,'''bert-base is not a local folder and is not a valid model identifier''' ): UpperCAmelCase_ : Tuple = AutoTokenizer.from_pretrained('''bert-base''' ) def a__ ( self ) -> List[Any]: with self.assertRaisesRegex( _SCREAMING_SNAKE_CASE ,R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): UpperCAmelCase_ : str = AutoTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,revision='''aaaaaa''' ) def a__ ( self ) -> Any: # Make sure we have cached the tokenizer. UpperCAmelCase_ : Optional[int] = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) with RequestCounter() as counter: UpperCAmelCase_ : List[str] = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) self.assertEqual(counter.get_request_count ,0 ) self.assertEqual(counter.head_request_count ,1 ) self.assertEqual(counter.other_request_count ,0 )
30
1
import argparse import os import re import packaging.version _lowercase = "examples/" _lowercase = { "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"), } _lowercase = { "init": "src/diffusers/__init__.py", "setup": "setup.py", } _lowercase = "README.md" def lowerCAmelCase__ ( UpperCamelCase_ : int , UpperCamelCase_ : Union[str, Any] , UpperCamelCase_ : Any )-> 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 lowerCAmelCase__ ( UpperCamelCase_ : Optional[Any] )-> Union[str, 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 lowerCAmelCase__ ( UpperCamelCase_ : Optional[Any] , UpperCamelCase_ : Optional[Any]=False )-> Union[str, Any]: for pattern, fname in REPLACE_FILES.items(): update_version_in_file(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ) if not patch: update_version_in_examples(UpperCamelCase_ ) def lowerCAmelCase__ ( )-> Any: 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/diffusers/main/model_doc''' , '''https://huggingface.co/docs/diffusers/model_doc''' , ) index += 1 with open(UpperCamelCase_ , '''w''' , encoding='''utf-8''' , newline='''\n''' ) as f: f.writelines(UpperCamelCase_ ) def lowerCAmelCase__ ( )-> str: 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 lowerCAmelCase__ ( UpperCamelCase_ : str=False )-> Optional[int]: 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_ ) def lowerCAmelCase__ ( )-> 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__": _lowercase = 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.") _lowercase = 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()
713
import numpy as np from matplotlib import pyplot as plt from sklearn.datasets import load_iris from sklearn.metrics import ConfusionMatrixDisplay from sklearn.model_selection import train_test_split from xgboost import XGBClassifier def lowerCAmelCase__ ( UpperCamelCase_ : dict )-> tuple: return (data["data"], data["target"]) def lowerCAmelCase__ ( UpperCamelCase_ : np.ndarray , UpperCamelCase_ : np.ndarray )-> XGBClassifier: A__ = XGBClassifier() classifier.fit(UpperCamelCase_ , UpperCamelCase_ ) return classifier def lowerCAmelCase__ ( )-> None: A__ = load_iris() A__ , A__ = data_handling(UpperCamelCase_ ) A__ , A__ , A__ , A__ = train_test_split( UpperCamelCase_ , UpperCamelCase_ , test_size=0.25 ) A__ = iris['''target_names'''] # Create an XGBoost Classifier from the training data A__ = xgboost(UpperCamelCase_ , UpperCamelCase_ ) # Display the confusion matrix of the classifier with both training and test sets ConfusionMatrixDisplay.from_estimator( UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , display_labels=UpperCamelCase_ , cmap='''Blues''' , normalize='''true''' , ) plt.title('''Normalized Confusion Matrix - IRIS Dataset''' ) plt.show() if __name__ == "__main__": import doctest doctest.testmod(verbose=True) main()
526
0
from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import pyarrow as pa if TYPE_CHECKING: from .features import FeatureType @dataclass class lowerCAmelCase_ : _UpperCamelCase : Dict = 42 _UpperCamelCase : Any = None # Automatically constructed _UpperCamelCase : Dict = "dict" _UpperCamelCase : Union[str, Any] = None _UpperCamelCase : List[str] = field(default="Translation" , init=__SCREAMING_SNAKE_CASE , repr=__SCREAMING_SNAKE_CASE ) def __call__( self ): return pa.struct({lang: pa.string() for lang in sorted(self.languages )} ) def __a ( self ): from .features import Value return {k: Value('string' ) for k in sorted(self.languages )} @dataclass class lowerCAmelCase_ : _UpperCamelCase : str = None _UpperCamelCase : List[Any] = None _UpperCamelCase : Dict = None # Automatically constructed _UpperCamelCase : Union[str, Any] = "dict" _UpperCamelCase : Optional[Any] = None _UpperCamelCase : Dict = field(default="TranslationVariableLanguages" , init=__SCREAMING_SNAKE_CASE , repr=__SCREAMING_SNAKE_CASE ) def __a ( self ): _lowercase : Tuple = sorted(set(self.languages ) ) if self.languages else None _lowercase : Union[str, Any] = len(self.languages ) if self.languages else None def __call__( self ): return pa.struct({'language': pa.list_(pa.string() ), 'translation': pa.list_(pa.string() )} ) def __a ( self , _lowerCAmelCase ): _lowercase : List[str] = set(self.languages ) if self.languages and set(lowerCAmelCase__ ) - lang_set: raise ValueError( F"""Some languages in example ({', '.join(sorted(set(lowerCAmelCase__ ) - lang_set ) )}) are not in valid set ({', '.join(lowerCAmelCase__ )}).""" ) # Convert dictionary into tuples, splitting out cases where there are # multiple translations for a single language. _lowercase : Dict = [] for lang, text in translation_dict.items(): if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): translation_tuples.append((lang, text) ) else: translation_tuples.extend([(lang, el) for el in text] ) # Ensure translations are in ascending order by language code. _lowercase : List[str] = zip(*sorted(lowerCAmelCase__ ) ) return {"language": languages, "translation": translations} def __a ( self ): from .features import Sequence, Value return { "language": Sequence(Value('string' ) ), "translation": Sequence(Value('string' ) ), }
66
def a__ ( A__ = 5_0_0_0_0_0_0_0 ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = set() SCREAMING_SNAKE_CASE_ : Optional[int] = int((limit - 2_4) ** (1 / 2) ) SCREAMING_SNAKE_CASE_ : Dict = set(range(3, prime_square_limit + 1, 2 ) ) primes.add(2 ) for p in range(3, prime_square_limit + 1, 2 ): if p not in primes: continue primes.difference_update(set(range(p * p, prime_square_limit + 1, A__ ) ) ) for primea in primes: SCREAMING_SNAKE_CASE_ : int = primea * primea for primea in primes: SCREAMING_SNAKE_CASE_ : Dict = primea * primea * primea if square + cube >= limit - 1_6: break for primea in primes: SCREAMING_SNAKE_CASE_ : Optional[int] = primea * primea * primea * primea SCREAMING_SNAKE_CASE_ : str = square + cube + tetr if total >= limit: break ret.add(A__ ) return len(A__ ) if __name__ == "__main__": print(F"""{solution() = }""")
101
0
from __future__ import annotations a_ : Optional[Any] = 'Muhammad Umer Farooq' a_ : List[Any] = 'MIT' a_ : Optional[Any] = '1.0.0' a_ : List[Any] = 'Muhammad Umer Farooq' a_ : Union[str, Any] = 'contact@muhammadumerfarooq.me' a_ : Optional[Any] = 'Alpha' import re from html.parser import HTMLParser from urllib import parse import requests class __UpperCamelCase ( _lowercase ): """simple docstring""" def __init__( self , SCREAMING_SNAKE_CASE ) -> None: super().__init__() a__ = [] a__ = domain def _UpperCAmelCase ( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) -> None: # Only parse the 'anchor' tag. if tag == "a": # Check the list of defined attributes. for name, value in attrs: # If href is defined, and not empty nor # print it. if name == "href" and value != "#" and value != "": # If not already in urls. if value not in self.urls: a__ = parse.urljoin(self.domain , SCREAMING_SNAKE_CASE ) self.urls.append(SCREAMING_SNAKE_CASE ) def __a ( __UpperCAmelCase ): return ".".join(get_sub_domain_name(__UpperCAmelCase ).split('''.''' )[-2:] ) def __a ( __UpperCAmelCase ): return parse.urlparse(__UpperCAmelCase ).netloc def __a ( __UpperCAmelCase = "https://github.com" ): a__ = get_domain_name(__UpperCAmelCase ) # Initialize the parser a__ = Parser(__UpperCAmelCase ) try: # Open URL a__ = requests.get(__UpperCAmelCase ) # pass the raw HTML to the parser to get links parser.feed(r.text ) # Get links and loop through a__ = set() for link in parser.urls: # open URL. # read = requests.get(link) try: a__ = requests.get(__UpperCAmelCase ) # Get the valid email. a__ = re.findall('''[a-zA-Z0-9]+@''' + domain , read.text ) # If not in list then append it. for email in emails: valid_emails.add(__UpperCAmelCase ) except ValueError: pass except ValueError: raise SystemExit(1 ) # Finally return a sorted list of email addresses with no duplicates. return sorted(__UpperCAmelCase ) if __name__ == "__main__": a_ : Dict = emails_from_url('https://github.com') print(f'{len(emails)} emails found:') print('\n'.join(sorted(emails)))
148
from __future__ import annotations import unittest from transformers import LEDConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFLEDForConditionalGeneration, TFLEDModel @require_tf class __UpperCamelCase : """simple docstring""" _lowercase : Optional[Any] = LEDConfig _lowercase : int = {} _lowercase : Optional[Any] = '''gelu''' def __init__( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE=1_3 , SCREAMING_SNAKE_CASE=7 , SCREAMING_SNAKE_CASE=True , SCREAMING_SNAKE_CASE=False , SCREAMING_SNAKE_CASE=9_9 , SCREAMING_SNAKE_CASE=3_2 , SCREAMING_SNAKE_CASE=2 , SCREAMING_SNAKE_CASE=4 , SCREAMING_SNAKE_CASE=3_7 , SCREAMING_SNAKE_CASE=0.1 , SCREAMING_SNAKE_CASE=0.1 , SCREAMING_SNAKE_CASE=2_0 , SCREAMING_SNAKE_CASE=2 , SCREAMING_SNAKE_CASE=1 , SCREAMING_SNAKE_CASE=0 , SCREAMING_SNAKE_CASE=4 , ) -> Tuple: a__ = parent a__ = batch_size a__ = seq_length a__ = is_training a__ = use_labels a__ = vocab_size a__ = hidden_size a__ = num_hidden_layers a__ = num_attention_heads a__ = intermediate_size a__ = hidden_dropout_prob a__ = attention_probs_dropout_prob a__ = max_position_embeddings a__ = eos_token_id a__ = pad_token_id a__ = bos_token_id a__ = attention_window # `ModelTesterMixin.test_attention_outputs` is expecting attention tensors to be of size # [num_attention_heads, encoder_seq_length, encoder_key_length], but TFLongformerSelfAttention # returns attention of shape [num_attention_heads, encoder_seq_length, self.attention_window + 1] # because its local attention only attends to `self.attention_window` and one before and one after a__ = self.attention_window + 2 # because of padding `encoder_seq_length`, is different from `seq_length`. Relevant for # the `test_attention_outputs` and `test_hidden_states_output` tests a__ = ( self.seq_length + (self.attention_window - self.seq_length % self.attention_window) % self.attention_window ) def _UpperCAmelCase ( self ) -> str: a__ = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) a__ = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) a__ = tf.concat([input_ids, eos_tensor] , axis=1 ) a__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) a__ = self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , attention_window=self.attention_window , **self.config_updates , ) a__ = prepare_led_inputs_dict(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) a__ = tf.concat( [tf.zeros_like(SCREAMING_SNAKE_CASE )[:, :-1], tf.ones_like(SCREAMING_SNAKE_CASE )[:, -1:]] , axis=-1 , ) a__ = global_attention_mask return config, inputs_dict def _UpperCAmelCase ( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) -> List[str]: a__ = TFLEDModel(config=SCREAMING_SNAKE_CASE ).get_decoder() a__ = inputs_dict['''input_ids'''] a__ = input_ids[:1, :] a__ = inputs_dict['''attention_mask'''][:1, :] a__ = 1 # first forward pass a__ = model(SCREAMING_SNAKE_CASE , attention_mask=SCREAMING_SNAKE_CASE , use_cache=SCREAMING_SNAKE_CASE ) a__ , a__ = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids a__ = ids_tensor((self.batch_size, 3) , config.vocab_size ) a__ = tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and a__ = tf.concat([input_ids, next_tokens] , axis=-1 ) a__ = tf.concat([attention_mask, next_attn_mask] , axis=-1 ) a__ = model(SCREAMING_SNAKE_CASE , attention_mask=SCREAMING_SNAKE_CASE )[0] a__ = model(SCREAMING_SNAKE_CASE , attention_mask=SCREAMING_SNAKE_CASE , past_key_values=SCREAMING_SNAKE_CASE )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice a__ = int(ids_tensor((1,) , output_from_past.shape[-1] ) ) a__ = output_from_no_past[:, -3:, random_slice_idx] a__ = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , rtol=1e-3 ) def __a ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=None , __UpperCAmelCase=None , __UpperCAmelCase=None , __UpperCAmelCase=None , ): if attention_mask is None: a__ = tf.cast(tf.math.not_equal(__UpperCAmelCase , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: a__ = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: a__ = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: a__ = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "attention_mask": attention_mask, "decoder_input_ids": decoder_input_ids, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, } @require_tf class __UpperCamelCase ( _lowercase , _lowercase , unittest.TestCase ): """simple docstring""" _lowercase : str = (TFLEDForConditionalGeneration, TFLEDModel) if is_tf_available() else () _lowercase : List[Any] = (TFLEDForConditionalGeneration,) if is_tf_available() else () _lowercase : int = ( { '''conversational''': TFLEDForConditionalGeneration, '''feature-extraction''': TFLEDModel, '''summarization''': TFLEDForConditionalGeneration, '''text2text-generation''': TFLEDForConditionalGeneration, '''translation''': TFLEDForConditionalGeneration, } if is_tf_available() else {} ) _lowercase : Any = True _lowercase : Union[str, Any] = False _lowercase : List[Any] = False _lowercase : List[Any] = False def _UpperCAmelCase ( self ) -> Optional[Any]: a__ = TFLEDModelTester(self ) a__ = ConfigTester(self , config_class=SCREAMING_SNAKE_CASE ) def _UpperCAmelCase ( self ) -> int: self.config_tester.run_common_tests() def _UpperCAmelCase ( self ) -> Union[str, Any]: a__ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*SCREAMING_SNAKE_CASE ) def _UpperCAmelCase ( self ) -> Dict: a__ , a__ = self.model_tester.prepare_config_and_inputs_for_common() a__ = tf.zeros_like(inputs_dict['''attention_mask'''] ) a__ = 2 a__ = tf.where( tf.range(self.model_tester.seq_length )[None, :] < num_global_attn_indices , 1 , inputs_dict['''global_attention_mask'''] , ) a__ = True a__ = self.model_tester.seq_length a__ = self.model_tester.encoder_seq_length def check_decoder_attentions_output(SCREAMING_SNAKE_CASE ): a__ = outputs.decoder_attentions self.assertEqual(len(SCREAMING_SNAKE_CASE ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_length, seq_length] , ) def check_encoder_attentions_output(SCREAMING_SNAKE_CASE ): a__ = [t.numpy() for t in outputs.encoder_attentions] a__ = [t.numpy() for t in outputs.encoder_global_attentions] self.assertEqual(len(SCREAMING_SNAKE_CASE ) , self.model_tester.num_hidden_layers ) self.assertEqual(len(SCREAMING_SNAKE_CASE ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_length, seq_length] , ) self.assertListEqual( list(global_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, num_global_attn_indices] , ) for model_class in self.all_model_classes: a__ = True a__ = False a__ = False a__ = model_class(SCREAMING_SNAKE_CASE ) a__ = model(self._prepare_for_class(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) ) a__ = len(SCREAMING_SNAKE_CASE ) self.assertEqual(config.output_hidden_states , SCREAMING_SNAKE_CASE ) check_encoder_attentions_output(SCREAMING_SNAKE_CASE ) if self.is_encoder_decoder: a__ = model_class(SCREAMING_SNAKE_CASE ) a__ = model(self._prepare_for_class(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) ) self.assertEqual(config.output_hidden_states , SCREAMING_SNAKE_CASE ) check_decoder_attentions_output(SCREAMING_SNAKE_CASE ) # Check that output attentions can also be changed via the config del inputs_dict["output_attentions"] a__ = True a__ = model_class(SCREAMING_SNAKE_CASE ) a__ = model(self._prepare_for_class(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) ) self.assertEqual(config.output_hidden_states , SCREAMING_SNAKE_CASE ) check_encoder_attentions_output(SCREAMING_SNAKE_CASE ) # Check attention is always last and order is fine a__ = True a__ = True a__ = model_class(SCREAMING_SNAKE_CASE ) a__ = model(self._prepare_for_class(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) ) self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1) , len(SCREAMING_SNAKE_CASE ) ) self.assertEqual(model.config.output_hidden_states , SCREAMING_SNAKE_CASE ) check_encoder_attentions_output(SCREAMING_SNAKE_CASE ) @unittest.skip('''LED keeps using potentially symbolic tensors in conditionals and breaks tracing.''' ) def _UpperCAmelCase ( self ) -> int: pass def _UpperCAmelCase ( self ) -> int: # TODO: Head-masking not yet implement pass def __a ( __UpperCAmelCase ): return tf.constant(__UpperCAmelCase , dtype=tf.intaa ) a_ : Dict = 1E-4 @slow @require_tf class __UpperCamelCase ( unittest.TestCase ): """simple docstring""" def _UpperCAmelCase ( self ) -> List[str]: a__ = TFLEDForConditionalGeneration.from_pretrained('''allenai/led-base-16384''' ).led # change to intended input here a__ = _long_tensor([5_1_2 * [0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9]] ) a__ = _long_tensor([1_2_8 * [0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9]] ) a__ = prepare_led_inputs_dict(model.config , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) a__ = model(**SCREAMING_SNAKE_CASE )[0] a__ = (1, 1_0_2_4, 7_6_8) self.assertEqual(output.shape , SCREAMING_SNAKE_CASE ) # change to expected output here a__ = tf.convert_to_tensor( [[2.30_50, 2.82_79, 0.65_31], [-1.84_57, -0.14_55, -3.56_61], [-1.01_86, 0.45_86, -2.20_43]] , ) tf.debugging.assert_near(output[:, :3, :3] , SCREAMING_SNAKE_CASE , atol=1e-3 ) def _UpperCAmelCase ( self ) -> str: a__ = TFLEDForConditionalGeneration.from_pretrained('''allenai/led-base-16384''' ) # change to intended input here a__ = _long_tensor([5_1_2 * [0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9]] ) a__ = _long_tensor([1_2_8 * [0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9]] ) a__ = prepare_led_inputs_dict(model.config , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) a__ = model(**SCREAMING_SNAKE_CASE )[0] a__ = (1, 1_0_2_4, model.config.vocab_size) self.assertEqual(output.shape , SCREAMING_SNAKE_CASE ) # change to expected output here a__ = tf.convert_to_tensor( [[33.65_07, 6.45_72, 16.80_89], [5.87_39, -2.42_38, 11.29_02], [-3.21_39, -4.31_49, 4.27_83]] , ) tf.debugging.assert_near(output[:, :3, :3] , SCREAMING_SNAKE_CASE , atol=1e-3 , rtol=1e-3 )
148
1
'''simple docstring''' def UpperCamelCase_ ( A__ : int ): '''simple docstring''' if not isinstance(A__ , A__ ): raise ValueError("""check_bouncy() accepts only integer arguments""" ) lowerCAmelCase_ : Any = str(A__ ) lowerCAmelCase_ : Optional[Any] = """""".join(sorted(A__ ) ) return sorted_str_n != str_n and sorted_str_n[::-1] != str_n def UpperCamelCase_ ( A__ : float = 99 ): '''simple docstring''' if not 0 < percent < 1_00: raise ValueError("""solution() only accepts values from 0 to 100""" ) lowerCAmelCase_ : Union[str, Any] = 0 lowerCAmelCase_ : Tuple = 1 while True: if check_bouncy(A__ ): bouncy_num += 1 if (bouncy_num / num) * 1_00 >= percent: return num num += 1 if __name__ == "__main__": from doctest import testmod testmod() print(F'''{solution(99)}''')
275
'''simple docstring''' from collections.abc import Iterator, MutableMapping from dataclasses import dataclass from typing import Generic, TypeVar __A : Union[str, Any] = TypeVar("KEY") __A : Union[str, Any] = TypeVar("VAL") @dataclass(frozen=_SCREAMING_SNAKE_CASE ,slots=_SCREAMING_SNAKE_CASE) class __snake_case ( Generic[KEY, VAL]): """simple docstring""" lowercase = 42 lowercase = 42 class __snake_case ( _Item): """simple docstring""" def __init__( self : int ) -> None: super().__init__(lowerCamelCase , lowerCamelCase ) def __bool__( self : Tuple ) -> bool: return False __A : Any = _DeletedItem() class __snake_case ( MutableMapping[KEY, VAL]): """simple docstring""" def __init__( self : Optional[int] , lowerCamelCase : int = 8 , lowerCamelCase : float = 0.75 ) -> None: lowerCAmelCase_ : str = initial_block_size lowerCAmelCase_ : list[_Item | None] = [None] * initial_block_size assert 0.0 < capacity_factor < 1.0 lowerCAmelCase_ : Optional[Any] = capacity_factor lowerCAmelCase_ : List[Any] = 0 def __lowercase ( self : List[Any] , lowerCamelCase : KEY ) -> int: return hash(lowerCamelCase ) % len(self._buckets ) def __lowercase ( self : Optional[Any] , lowerCamelCase : int ) -> int: return (ind + 1) % len(self._buckets ) def __lowercase ( self : Union[str, Any] , lowerCamelCase : int , lowerCamelCase : KEY , lowerCamelCase : VAL ) -> bool: lowerCAmelCase_ : Union[str, Any] = self._buckets[ind] if not stored: lowerCAmelCase_ : List[Any] = _Item(lowerCamelCase , lowerCamelCase ) self._len += 1 return True elif stored.key == key: lowerCAmelCase_ : List[str] = _Item(lowerCamelCase , lowerCamelCase ) return True else: return False def __lowercase ( self : Optional[Any] ) -> bool: lowerCAmelCase_ : str = len(self._buckets ) * self._capacity_factor return len(self ) >= int(lowerCamelCase ) def __lowercase ( self : Dict ) -> bool: if len(self._buckets ) <= self._initial_block_size: return False lowerCAmelCase_ : Optional[int] = len(self._buckets ) * self._capacity_factor / 2 return len(self ) < limit def __lowercase ( self : Tuple , lowerCamelCase : int ) -> None: lowerCAmelCase_ : Union[str, Any] = self._buckets lowerCAmelCase_ : str = [None] * new_size lowerCAmelCase_ : str = 0 for item in old_buckets: if item: self._add_item(item.key , item.val ) def __lowercase ( self : Tuple ) -> None: self._resize(len(self._buckets ) * 2 ) def __lowercase ( self : Union[str, Any] ) -> None: self._resize(len(self._buckets ) // 2 ) def __lowercase ( self : Optional[int] , lowerCamelCase : KEY ) -> Iterator[int]: lowerCAmelCase_ : int = self._get_bucket_index(lowerCamelCase ) for _ in range(len(self._buckets ) ): yield ind lowerCAmelCase_ : Dict = self._get_next_ind(lowerCamelCase ) def __lowercase ( self : Tuple , lowerCamelCase : KEY , lowerCamelCase : VAL ) -> None: for ind in self._iterate_buckets(lowerCamelCase ): if self._try_set(lowerCamelCase , lowerCamelCase , lowerCamelCase ): break def __setitem__( self : Tuple , lowerCamelCase : KEY , lowerCamelCase : VAL ) -> None: if self._is_full(): self._size_up() self._add_item(lowerCamelCase , lowerCamelCase ) def __delitem__( self : Optional[int] , lowerCamelCase : KEY ) -> None: for ind in self._iterate_buckets(lowerCamelCase ): lowerCAmelCase_ : int = self._buckets[ind] if item is None: raise KeyError(lowerCamelCase ) if item is _deleted: continue if item.key == key: lowerCAmelCase_ : List[Any] = _deleted self._len -= 1 break if self._is_sparse(): self._size_down() def __getitem__( self : Any , lowerCamelCase : KEY ) -> VAL: for ind in self._iterate_buckets(lowerCamelCase ): lowerCAmelCase_ : Dict = self._buckets[ind] if item is None: break if item is _deleted: continue if item.key == key: return item.val raise KeyError(lowerCamelCase ) def __len__( self : Any ) -> int: return self._len def __iter__( self : Optional[Any] ) -> Iterator[KEY]: yield from (item.key for item in self._buckets if item) def __repr__( self : List[Any] ) -> str: lowerCAmelCase_ : int = """ ,""".join( F'{item.key}: {item.val}' for item in self._buckets if item ) return F'HashMap({val_string})'
275
1
"""simple docstring""" import copy import unittest from transformers.models.auto import get_values from transformers.testing_utils import require_torch, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_MULTIPLE_CHOICE_MAPPING, MODEL_FOR_QUESTION_ANSWERING_MAPPING, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, LayoutLMvaForQuestionAnswering, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaModel, ) from transformers.models.layoutlmva.modeling_layoutlmva import LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class snake_case : def __init__( self , __UpperCAmelCase , __UpperCAmelCase=2 , __UpperCAmelCase=3 , __UpperCAmelCase=4 , __UpperCAmelCase=2 , __UpperCAmelCase=7 , __UpperCAmelCase=True , __UpperCAmelCase=True , __UpperCAmelCase=True , __UpperCAmelCase=True , __UpperCAmelCase=99 , __UpperCAmelCase=36 , __UpperCAmelCase=3 , __UpperCAmelCase=4 , __UpperCAmelCase=37 , __UpperCAmelCase="gelu" , __UpperCAmelCase=0.1 , __UpperCAmelCase=0.1 , __UpperCAmelCase=5_12 , __UpperCAmelCase=16 , __UpperCAmelCase=2 , __UpperCAmelCase=0.02 , __UpperCAmelCase=6 , __UpperCAmelCase=6 , __UpperCAmelCase=3 , __UpperCAmelCase=4 , __UpperCAmelCase=None , __UpperCAmelCase=10_00 , ) ->List[Any]: a_ = parent a_ = batch_size a_ = num_channels a_ = image_size a_ = patch_size a_ = text_seq_length a_ = is_training a_ = use_input_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_ = coordinate_size a_ = shape_size a_ = num_labels a_ = num_choices a_ = scope a_ = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) a_ = text_seq_length a_ = (image_size // patch_size) ** 2 + 1 a_ = self.text_seq_length + self.image_seq_length def UpperCAmelCase__ ( self) ->Optional[Any]: a_ = ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size) a_ = ids_tensor([self.batch_size, self.text_seq_length, 4] , self.range_bbox) # Ensure that bbox is legal for i in range(bbox.shape[0]): for j in range(bbox.shape[1]): if bbox[i, j, 3] < bbox[i, j, 1]: a_ = bbox[i, j, 3] a_ = bbox[i, j, 1] a_ = t if bbox[i, j, 2] < bbox[i, j, 0]: a_ = bbox[i, j, 2] a_ = bbox[i, j, 0] a_ = t a_ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]) a_ = None if self.use_input_mask: a_ = random_attention_mask([self.batch_size, self.text_seq_length]) a_ = None if self.use_token_type_ids: a_ = ids_tensor([self.batch_size, self.text_seq_length] , self.type_vocab_size) a_ = None a_ = None if self.use_labels: a_ = ids_tensor([self.batch_size] , self.type_sequence_label_size) a_ = ids_tensor([self.batch_size, self.text_seq_length] , self.num_labels) a_ = LayoutLMvaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , coordinate_size=self.coordinate_size , shape_size=self.shape_size , input_size=self.image_size , patch_size=self.patch_size , ) return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase) ->str: a_ = LayoutLMvaModel(config=__UpperCAmelCase) model.to(__UpperCAmelCase) model.eval() # text + image a_ = model(__UpperCAmelCase , pixel_values=__UpperCAmelCase) a_ = model( __UpperCAmelCase , bbox=__UpperCAmelCase , pixel_values=__UpperCAmelCase , attention_mask=__UpperCAmelCase , token_type_ids=__UpperCAmelCase) a_ = model(__UpperCAmelCase , bbox=__UpperCAmelCase , pixel_values=__UpperCAmelCase , token_type_ids=__UpperCAmelCase) a_ = model(__UpperCAmelCase , bbox=__UpperCAmelCase , pixel_values=__UpperCAmelCase) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) # text only a_ = model(__UpperCAmelCase) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size)) # image only a_ = model(pixel_values=__UpperCAmelCase) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size)) def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase) ->List[str]: a_ = self.num_labels a_ = LayoutLMvaForSequenceClassification(__UpperCAmelCase) model.to(__UpperCAmelCase) model.eval() a_ = model( __UpperCAmelCase , bbox=__UpperCAmelCase , pixel_values=__UpperCAmelCase , attention_mask=__UpperCAmelCase , token_type_ids=__UpperCAmelCase , labels=__UpperCAmelCase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels)) def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase) ->List[str]: a_ = self.num_labels a_ = LayoutLMvaForTokenClassification(config=__UpperCAmelCase) model.to(__UpperCAmelCase) model.eval() a_ = model( __UpperCAmelCase , bbox=__UpperCAmelCase , pixel_values=__UpperCAmelCase , attention_mask=__UpperCAmelCase , token_type_ids=__UpperCAmelCase , labels=__UpperCAmelCase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels)) def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase) ->Union[str, Any]: a_ = LayoutLMvaForQuestionAnswering(config=__UpperCAmelCase) model.to(__UpperCAmelCase) model.eval() a_ = model( __UpperCAmelCase , bbox=__UpperCAmelCase , pixel_values=__UpperCAmelCase , attention_mask=__UpperCAmelCase , token_type_ids=__UpperCAmelCase , start_positions=__UpperCAmelCase , end_positions=__UpperCAmelCase , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length)) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length)) def UpperCAmelCase__ ( self) ->Optional[int]: a_ = self.prepare_config_and_inputs() ( ( a_ ) , ( a_ ) , ( a_ ) , ( a_ ) , ( a_ ) , ( a_ ) , ( a_ ) , ( a_ ) , ) = config_and_inputs a_ = { "input_ids": input_ids, "bbox": bbox, "pixel_values": pixel_values, "token_type_ids": token_type_ids, "attention_mask": input_mask, } return config, inputs_dict @require_torch class snake_case ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): a_ : Tuple = False a_ : Tuple = False a_ : List[Any] = False a_ : Union[str, Any] = ( ( LayoutLMvaModel, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaForQuestionAnswering, ) if is_torch_available() else () ) a_ : int = ( {"""document-question-answering""": LayoutLMvaForQuestionAnswering, """feature-extraction""": LayoutLMvaModel} if is_torch_available() else {} ) def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase) ->Tuple: # `DocumentQuestionAnsweringPipeline` is expected to work with this model, but it combines the text and visual # embedding along the sequence dimension (dim 1), which causes an error during post-processing as `p_mask` has # the sequence dimension of the text embedding only. # (see the line `embedding_output = torch.cat([embedding_output, visual_embeddings], dim=1)`) return True def UpperCAmelCase__ ( self) ->List[str]: a_ = LayoutLMvaModelTester(self) a_ = ConfigTester(self , config_class=__UpperCAmelCase , hidden_size=37) def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=False) ->List[str]: a_ = copy.deepcopy(__UpperCAmelCase) if model_class in get_values(__UpperCAmelCase): a_ = { k: v.unsqueeze(1).expand(-1 , self.model_tester.num_choices , -1).contiguous() if isinstance(__UpperCAmelCase , torch.Tensor) and v.ndim > 1 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(__UpperCAmelCase): a_ = torch.ones(self.model_tester.batch_size , dtype=torch.long , device=__UpperCAmelCase) elif model_class in get_values(__UpperCAmelCase): a_ = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=__UpperCAmelCase) a_ = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=__UpperCAmelCase) elif model_class in [ *get_values(__UpperCAmelCase), ]: a_ = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=__UpperCAmelCase) elif model_class in [ *get_values(__UpperCAmelCase), ]: a_ = torch.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=torch.long , device=__UpperCAmelCase , ) return inputs_dict def UpperCAmelCase__ ( self) ->List[Any]: self.config_tester.run_common_tests() def UpperCAmelCase__ ( self) ->Tuple: a_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__UpperCAmelCase) def UpperCAmelCase__ ( self) ->Tuple: a_ = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: a_ = type self.model_tester.create_and_check_model(*__UpperCAmelCase) def UpperCAmelCase__ ( self) ->List[Any]: a_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*__UpperCAmelCase) def UpperCAmelCase__ ( self) ->List[Any]: a_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*__UpperCAmelCase) def UpperCAmelCase__ ( self) ->Optional[Any]: a_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*__UpperCAmelCase) @slow def UpperCAmelCase__ ( self) ->int: for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a_ = LayoutLMvaModel.from_pretrained(__UpperCAmelCase) self.assertIsNotNone(__UpperCAmelCase) def UpperCamelCase ( ) ->Any: """simple docstring""" a_ = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch class snake_case ( unittest.TestCase ): @cached_property def UpperCAmelCase__ ( self) ->List[str]: return LayoutLMvaImageProcessor(apply_ocr=__UpperCAmelCase) if is_vision_available() else None @slow def UpperCAmelCase__ ( self) ->Union[str, Any]: a_ = LayoutLMvaModel.from_pretrained("microsoft/layoutlmv3-base").to(__UpperCAmelCase) a_ = self.default_image_processor a_ = prepare_img() a_ = image_processor(images=__UpperCAmelCase , return_tensors="pt").pixel_values.to(__UpperCAmelCase) a_ = torch.tensor([[1, 2]]) a_ = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]]).unsqueeze(0) # forward pass a_ = model( input_ids=input_ids.to(__UpperCAmelCase) , bbox=bbox.to(__UpperCAmelCase) , pixel_values=pixel_values.to(__UpperCAmelCase) , ) # verify the logits a_ = torch.Size((1, 1_99, 7_68)) self.assertEqual(outputs.last_hidden_state.shape , __UpperCAmelCase) a_ = torch.tensor( [[-0.0_529, 0.3_618, 0.1_632], [-0.1_587, -0.1_667, -0.0_400], [-0.1_557, -0.1_671, -0.0_505]]).to(__UpperCAmelCase) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , __UpperCAmelCase , atol=1E-4))
210
"""simple docstring""" def UpperCamelCase ( UpperCAmelCase ) ->bool: """simple docstring""" a_ = 0 for ch in input_str: a_ = ord(UpperCAmelCase ) a_ = pow(2 , UpperCAmelCase ) # If we already turned on bit for current character's unicode if bitmap >> ch_unicode & 1 == 1: return False bitmap |= ch_bit_index_on return True if __name__ == "__main__": import doctest doctest.testmod()
210
1
import inspect import unittest from transformers import MobileNetVaConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MobileNetVaForImageClassification, MobileNetVaModel from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import MobileNetVaImageProcessor class lowercase ( _UpperCAmelCase ): def lowercase__ ( self : Tuple ): SCREAMING_SNAKE_CASE__ : str = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(_lowercase , '''tf_padding''' ) ) self.parent.assertTrue(hasattr(_lowercase , '''depth_multiplier''' ) ) class lowercase : def __init__( self : List[Any] , _lowercase : List[str] , _lowercase : str=13 , _lowercase : List[str]=3 , _lowercase : Dict=32 , _lowercase : List[Any]=0.25 , _lowercase : List[str]=8 , _lowercase : Tuple=True , _lowercase : Tuple=10_24 , _lowercase : Tuple=32 , _lowercase : Union[str, Any]="relu6" , _lowercase : Dict=0.1 , _lowercase : Optional[int]=0.02 , _lowercase : Optional[int]=True , _lowercase : int=True , _lowercase : int=10 , _lowercase : Dict=None , ): SCREAMING_SNAKE_CASE__ : Optional[Any] = parent SCREAMING_SNAKE_CASE__ : Any = batch_size SCREAMING_SNAKE_CASE__ : Any = num_channels SCREAMING_SNAKE_CASE__ : Optional[int] = image_size SCREAMING_SNAKE_CASE__ : List[str] = depth_multiplier SCREAMING_SNAKE_CASE__ : str = min_depth SCREAMING_SNAKE_CASE__ : Dict = tf_padding SCREAMING_SNAKE_CASE__ : List[str] = int(last_hidden_size * depth_multiplier ) SCREAMING_SNAKE_CASE__ : List[str] = output_stride SCREAMING_SNAKE_CASE__ : Optional[Any] = hidden_act SCREAMING_SNAKE_CASE__ : int = classifier_dropout_prob SCREAMING_SNAKE_CASE__ : Optional[int] = use_labels SCREAMING_SNAKE_CASE__ : Any = is_training SCREAMING_SNAKE_CASE__ : int = num_labels SCREAMING_SNAKE_CASE__ : Dict = initializer_range SCREAMING_SNAKE_CASE__ : Optional[int] = scope def lowercase__ ( self : str ): SCREAMING_SNAKE_CASE__ : List[Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE__ : int = None SCREAMING_SNAKE_CASE__ : List[Any] = None if self.use_labels: SCREAMING_SNAKE_CASE__ : Any = ids_tensor([self.batch_size] , self.num_labels ) SCREAMING_SNAKE_CASE__ : int = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) SCREAMING_SNAKE_CASE__ : Optional[Any] = self.get_config() return config, pixel_values, labels, pixel_labels def lowercase__ ( self : int ): return MobileNetVaConfig( num_channels=self.num_channels , image_size=self.image_size , depth_multiplier=self.depth_multiplier , min_depth=self.min_depth , tf_padding=self.tf_padding , hidden_act=self.hidden_act , classifier_dropout_prob=self.classifier_dropout_prob , initializer_range=self.initializer_range , ) def lowercase__ ( self : List[Any] , _lowercase : List[str] , _lowercase : int , _lowercase : int , _lowercase : List[str] ): SCREAMING_SNAKE_CASE__ : str = MobileNetVaModel(config=_lowercase ) model.to(_lowercase ) model.eval() SCREAMING_SNAKE_CASE__ : List[str] = model(_lowercase ) self.parent.assertEqual( result.last_hidden_state.shape , ( self.batch_size, self.last_hidden_size, self.image_size // self.output_stride, self.image_size // self.output_stride, ) , ) def lowercase__ ( self : Union[str, Any] , _lowercase : Optional[Any] , _lowercase : Dict , _lowercase : Any , _lowercase : str ): SCREAMING_SNAKE_CASE__ : int = self.num_labels SCREAMING_SNAKE_CASE__ : Dict = MobileNetVaForImageClassification(_lowercase ) model.to(_lowercase ) model.eval() SCREAMING_SNAKE_CASE__ : List[str] = model(_lowercase , labels=_lowercase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowercase__ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE__ : str = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : str = config_and_inputs SCREAMING_SNAKE_CASE__ : List[str] = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class lowercase ( _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): lowerCamelCase : Optional[int] = (MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else () lowerCamelCase : Tuple = ( {'''feature-extraction''': MobileNetVaModel, '''image-classification''': MobileNetVaForImageClassification} if is_torch_available() else {} ) lowerCamelCase : Union[str, Any] = False lowerCamelCase : List[str] = False lowerCamelCase : Optional[Any] = False lowerCamelCase : int = False def lowercase__ ( self : int ): SCREAMING_SNAKE_CASE__ : Tuple = MobileNetVaModelTester(self ) SCREAMING_SNAKE_CASE__ : str = MobileNetVaConfigTester(self , config_class=_lowercase , has_text_modality=_lowercase ) def lowercase__ ( self : str ): self.config_tester.run_common_tests() @unittest.skip(reason='''MobileNetV1 does not use inputs_embeds''' ) def lowercase__ ( self : List[str] ): pass @unittest.skip(reason='''MobileNetV1 does not support input and output embeddings''' ) def lowercase__ ( self : Tuple ): pass @unittest.skip(reason='''MobileNetV1 does not output attentions''' ) def lowercase__ ( self : List[Any] ): pass def lowercase__ ( self : Dict ): SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE__ : Optional[Any] = model_class(_lowercase ) SCREAMING_SNAKE_CASE__ : int = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE__ : List[Any] = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE__ : Tuple = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _lowercase ) def lowercase__ ( self : Optional[int] ): SCREAMING_SNAKE_CASE__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_lowercase ) def lowercase__ ( self : Optional[Any] ): def check_hidden_states_output(_lowercase : Optional[int] , _lowercase : str , _lowercase : Tuple ): SCREAMING_SNAKE_CASE__ : int = model_class(_lowercase ) model.to(_lowercase ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE__ : Union[str, Any] = model(**self._prepare_for_class(_lowercase , _lowercase ) ) SCREAMING_SNAKE_CASE__ : Tuple = outputs.hidden_states SCREAMING_SNAKE_CASE__ : Union[str, Any] = 26 self.assertEqual(len(_lowercase ) , _lowercase ) SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE__ : int = True check_hidden_states_output(_lowercase , _lowercase , _lowercase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE__ : List[str] = True check_hidden_states_output(_lowercase , _lowercase , _lowercase ) def lowercase__ ( self : int ): SCREAMING_SNAKE_CASE__ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_lowercase ) @slow def lowercase__ ( self : Union[str, Any] ): for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE__ : int = MobileNetVaModel.from_pretrained(_lowercase ) self.assertIsNotNone(_lowercase ) def a ( ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE__ : Union[str, Any] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class lowercase ( unittest.TestCase ): @cached_property def lowercase__ ( self : Tuple ): return ( MobileNetVaImageProcessor.from_pretrained('''google/mobilenet_v1_1.0_224''' ) if is_vision_available() else None ) @slow def lowercase__ ( self : int ): SCREAMING_SNAKE_CASE__ : str = MobileNetVaForImageClassification.from_pretrained('''google/mobilenet_v1_1.0_224''' ).to(_lowercase ) SCREAMING_SNAKE_CASE__ : Any = self.default_image_processor SCREAMING_SNAKE_CASE__ : Optional[int] = prepare_img() SCREAMING_SNAKE_CASE__ : Tuple = image_processor(images=_lowercase , return_tensors='''pt''' ).to(_lowercase ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE__ : str = model(**_lowercase ) # verify the logits SCREAMING_SNAKE_CASE__ : Optional[Any] = torch.Size((1, 10_01) ) self.assertEqual(outputs.logits.shape , _lowercase ) SCREAMING_SNAKE_CASE__ : Any = torch.tensor([-4.1739, -1.1233, 3.1205] ).to(_lowercase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _lowercase , atol=1E-4 ) )
35
import logging import os from dataclasses import dataclass from enum import Enum from typing import List, Optional, Union from filelock import FileLock from transformers import PreTrainedTokenizer, is_tf_available, is_torch_available a_ :List[Any] = logging.getLogger(__name__) @dataclass class lowercase : lowerCamelCase : str lowerCamelCase : List[str] lowerCamelCase : Optional[List[str]] @dataclass class lowercase : lowerCamelCase : List[int] lowerCamelCase : List[int] lowerCamelCase : Optional[List[int]] = None lowerCamelCase : Optional[List[int]] = None class lowercase ( _UpperCAmelCase ): lowerCamelCase : Optional[Any] = '''train''' lowerCamelCase : Tuple = '''dev''' lowerCamelCase : Any = '''test''' class lowercase : @staticmethod def lowercase__ ( _lowercase : Any , _lowercase : Union[Split, str] ): raise NotImplementedError @staticmethod def lowercase__ ( _lowercase : str ): raise NotImplementedError @staticmethod def lowercase__ ( _lowercase : List[InputExample] , _lowercase : List[str] , _lowercase : int , _lowercase : PreTrainedTokenizer , _lowercase : int=False , _lowercase : Optional[Any]="[CLS]" , _lowercase : Tuple=1 , _lowercase : Optional[Any]="[SEP]" , _lowercase : Tuple=False , _lowercase : Optional[Any]=False , _lowercase : List[Any]=0 , _lowercase : Optional[int]=0 , _lowercase : Optional[Any]=-1_00 , _lowercase : Tuple=0 , _lowercase : Union[str, Any]=True , ): SCREAMING_SNAKE_CASE__ : Tuple = {label: i for i, label in enumerate(_lowercase )} SCREAMING_SNAKE_CASE__ : Dict = [] for ex_index, example in enumerate(_lowercase ): if ex_index % 1_00_00 == 0: logger.info('''Writing example %d of %d''' , _lowercase , len(_lowercase ) ) SCREAMING_SNAKE_CASE__ : Tuple = [] SCREAMING_SNAKE_CASE__ : Optional[Any] = [] for word, label in zip(example.words , example.labels ): SCREAMING_SNAKE_CASE__ : Optional[Any] = tokenizer.tokenize(_lowercase ) # bert-base-multilingual-cased sometimes output "nothing ([]) when calling tokenize with just a space. if len(_lowercase ) > 0: tokens.extend(_lowercase ) # Use the real label id for the first token of the word, and padding ids for the remaining tokens label_ids.extend([label_map[label]] + [pad_token_label_id] * (len(_lowercase ) - 1) ) # Account for [CLS] and [SEP] with "- 2" and with "- 3" for RoBERTa. SCREAMING_SNAKE_CASE__ : Tuple = tokenizer.num_special_tokens_to_add() if len(_lowercase ) > max_seq_length - special_tokens_count: SCREAMING_SNAKE_CASE__ : List[str] = tokens[: (max_seq_length - special_tokens_count)] SCREAMING_SNAKE_CASE__ : Any = label_ids[: (max_seq_length - special_tokens_count)] # The convention in BERT is: # (a) For sequence pairs: # tokens: [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP] # type_ids: 0 0 0 0 0 0 0 0 1 1 1 1 1 1 # (b) For single sequences: # tokens: [CLS] the dog is hairy . [SEP] # type_ids: 0 0 0 0 0 0 0 # # Where "type_ids" are used to indicate whether this is the first # sequence or the second sequence. The embedding vectors for `type=0` and # `type=1` were learned during pre-training and are added to the wordpiece # embedding vector (and position vector). This is not *strictly* necessary # since the [SEP] token unambiguously separates the sequences, but it makes # it easier for the model to learn the concept of sequences. # # For classification tasks, the first vector (corresponding to [CLS]) is # used as the "sentence vector". Note that this only makes sense because # the entire model is fine-tuned. tokens += [sep_token] label_ids += [pad_token_label_id] if sep_token_extra: # roberta uses an extra separator b/w pairs of sentences tokens += [sep_token] label_ids += [pad_token_label_id] SCREAMING_SNAKE_CASE__ : Optional[int] = [sequence_a_segment_id] * len(_lowercase ) if cls_token_at_end: tokens += [cls_token] label_ids += [pad_token_label_id] segment_ids += [cls_token_segment_id] else: SCREAMING_SNAKE_CASE__ : Optional[Any] = [cls_token] + tokens SCREAMING_SNAKE_CASE__ : Tuple = [pad_token_label_id] + label_ids SCREAMING_SNAKE_CASE__ : Tuple = [cls_token_segment_id] + segment_ids SCREAMING_SNAKE_CASE__ : Optional[int] = tokenizer.convert_tokens_to_ids(_lowercase ) # The mask has 1 for real tokens and 0 for padding tokens. Only real # tokens are attended to. SCREAMING_SNAKE_CASE__ : str = [1 if mask_padding_with_zero else 0] * len(_lowercase ) # Zero-pad up to the sequence length. SCREAMING_SNAKE_CASE__ : List[str] = max_seq_length - len(_lowercase ) if pad_on_left: SCREAMING_SNAKE_CASE__ : Union[str, Any] = ([pad_token] * padding_length) + input_ids SCREAMING_SNAKE_CASE__ : str = ([0 if mask_padding_with_zero else 1] * padding_length) + input_mask SCREAMING_SNAKE_CASE__ : Tuple = ([pad_token_segment_id] * padding_length) + segment_ids SCREAMING_SNAKE_CASE__ : int = ([pad_token_label_id] * padding_length) + label_ids else: input_ids += [pad_token] * padding_length input_mask += [0 if mask_padding_with_zero else 1] * padding_length segment_ids += [pad_token_segment_id] * padding_length label_ids += [pad_token_label_id] * padding_length assert len(_lowercase ) == max_seq_length assert len(_lowercase ) == max_seq_length assert len(_lowercase ) == max_seq_length assert len(_lowercase ) == max_seq_length if ex_index < 5: logger.info('''*** Example ***''' ) logger.info('''guid: %s''' , example.guid ) logger.info('''tokens: %s''' , ''' '''.join([str(_lowercase ) for x in tokens] ) ) logger.info('''input_ids: %s''' , ''' '''.join([str(_lowercase ) for x in input_ids] ) ) logger.info('''input_mask: %s''' , ''' '''.join([str(_lowercase ) for x in input_mask] ) ) logger.info('''segment_ids: %s''' , ''' '''.join([str(_lowercase ) for x in segment_ids] ) ) logger.info('''label_ids: %s''' , ''' '''.join([str(_lowercase ) for x in label_ids] ) ) if "token_type_ids" not in tokenizer.model_input_names: SCREAMING_SNAKE_CASE__ : List[Any] = None features.append( InputFeatures( input_ids=_lowercase , attention_mask=_lowercase , token_type_ids=_lowercase , label_ids=_lowercase ) ) return features if is_torch_available(): import torch from torch import nn from torch.utils.data import Dataset class lowercase ( _UpperCAmelCase ): lowerCamelCase : List[InputFeatures] lowerCamelCase : int = nn.CrossEntropyLoss().ignore_index def __init__( self : int , _lowercase : TokenClassificationTask , _lowercase : str , _lowercase : PreTrainedTokenizer , _lowercase : List[str] , _lowercase : str , _lowercase : Optional[int] = None , _lowercase : Optional[int]=False , _lowercase : Split = Split.train , ): # Load data features from cache or dataset file SCREAMING_SNAKE_CASE__ : List[Any] = os.path.join( _lowercase , '''cached_{}_{}_{}'''.format(mode.value , tokenizer.__class__.__name__ , str(_lowercase ) ) , ) # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. SCREAMING_SNAKE_CASE__ : Optional[int] = cached_features_file + '''.lock''' with FileLock(_lowercase ): if os.path.exists(_lowercase ) and not overwrite_cache: logger.info(f"""Loading features from cached file {cached_features_file}""" ) SCREAMING_SNAKE_CASE__ : Any = torch.load(_lowercase ) else: logger.info(f"""Creating features from dataset file at {data_dir}""" ) SCREAMING_SNAKE_CASE__ : str = token_classification_task.read_examples_from_file(_lowercase , _lowercase ) # TODO clean up all this to leverage built-in features of tokenizers SCREAMING_SNAKE_CASE__ : Any = token_classification_task.convert_examples_to_features( _lowercase , _lowercase , _lowercase , _lowercase , cls_token_at_end=bool(model_type in ['''xlnet'''] ) , cls_token=tokenizer.cls_token , cls_token_segment_id=2 if model_type in ['''xlnet'''] else 0 , sep_token=tokenizer.sep_token , sep_token_extra=_lowercase , pad_on_left=bool(tokenizer.padding_side == '''left''' ) , pad_token=tokenizer.pad_token_id , pad_token_segment_id=tokenizer.pad_token_type_id , pad_token_label_id=self.pad_token_label_id , ) logger.info(f"""Saving features into cached file {cached_features_file}""" ) torch.save(self.features , _lowercase ) def __len__( self : Tuple ): return len(self.features ) def __getitem__( self : Optional[int] , _lowercase : List[str] ): return self.features[i] if is_tf_available(): import tensorflow as tf class lowercase : lowerCamelCase : List[InputFeatures] lowerCamelCase : int = -100 def __init__( self : int , _lowercase : TokenClassificationTask , _lowercase : str , _lowercase : PreTrainedTokenizer , _lowercase : List[str] , _lowercase : str , _lowercase : Optional[int] = None , _lowercase : List[str]=False , _lowercase : Split = Split.train , ): SCREAMING_SNAKE_CASE__ : Optional[int] = token_classification_task.read_examples_from_file(_lowercase , _lowercase ) # TODO clean up all this to leverage built-in features of tokenizers SCREAMING_SNAKE_CASE__ : List[str] = token_classification_task.convert_examples_to_features( _lowercase , _lowercase , _lowercase , _lowercase , cls_token_at_end=bool(model_type in ['''xlnet'''] ) , cls_token=tokenizer.cls_token , cls_token_segment_id=2 if model_type in ['''xlnet'''] else 0 , sep_token=tokenizer.sep_token , sep_token_extra=_lowercase , pad_on_left=bool(tokenizer.padding_side == '''left''' ) , pad_token=tokenizer.pad_token_id , pad_token_segment_id=tokenizer.pad_token_type_id , pad_token_label_id=self.pad_token_label_id , ) def gen(): for ex in self.features: if ex.token_type_ids is None: yield ( {"input_ids": ex.input_ids, "attention_mask": ex.attention_mask}, ex.label_ids, ) else: yield ( { "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label_ids, ) if "token_type_ids" not in tokenizer.model_input_names: SCREAMING_SNAKE_CASE__ : int = tf.data.Dataset.from_generator( _lowercase , ({'''input_ids''': tf.intaa, '''attention_mask''': tf.intaa}, tf.intaa) , ( {'''input_ids''': tf.TensorShape([None] ), '''attention_mask''': tf.TensorShape([None] )}, tf.TensorShape([None] ), ) , ) else: SCREAMING_SNAKE_CASE__ : int = tf.data.Dataset.from_generator( _lowercase , ({'''input_ids''': tf.intaa, '''attention_mask''': tf.intaa, '''token_type_ids''': tf.intaa}, tf.intaa) , ( { '''input_ids''': tf.TensorShape([None] ), '''attention_mask''': tf.TensorShape([None] ), '''token_type_ids''': tf.TensorShape([None] ), }, tf.TensorShape([None] ), ) , ) def lowercase__ ( self : Tuple ): SCREAMING_SNAKE_CASE__ : Optional[int] = self.dataset.apply(tf.data.experimental.assert_cardinality(len(self.features ) ) ) return self.dataset def __len__( self : Dict ): return len(self.features ) def __getitem__( self : Optional[Any] , _lowercase : Union[str, Any] ): return self.features[i]
35
1
"""simple docstring""" import math def lowercase_ ( _snake_case ): if not isinstance(_lowerCAmelCase ,_lowerCAmelCase ): SCREAMING_SNAKE_CASE__ : List[Any] = f'''Input value of [number={number}] must be an integer''' raise TypeError(_lowerCAmelCase ) if number < 1: SCREAMING_SNAKE_CASE__ : Union[str, Any] = f'''Input value of [number={number}] must be > 0''' raise ValueError(_lowerCAmelCase ) elif number == 1: return 3 elif number == 2: return 5 else: SCREAMING_SNAKE_CASE__ : List[Any] = int(math.log(number // 3 ,2 ) ) + 2 SCREAMING_SNAKE_CASE__ : Optional[Any] = [3, 5] SCREAMING_SNAKE_CASE__ : Optional[int] = 2 SCREAMING_SNAKE_CASE__ : int = 3 for block in range(1 ,_lowerCAmelCase ): for _ in range(_lowerCAmelCase ): proth_list.append(2 ** (block + 1) + proth_list[proth_index - 1] ) proth_index += 1 increment *= 2 return proth_list[number - 1] if __name__ == "__main__": import doctest doctest.testmod() for number in range(1_1): UpperCAmelCase__ : List[str] = 0 try: UpperCAmelCase__ : Optional[Any] = proth(number) except ValueError: print(f"""ValueError: there is no {number}th Proth number""") continue print(f"""The {number}th Proth number: {value}""")
716
"""simple docstring""" import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import MobileViTImageProcessor class lowerCAmelCase_ (unittest.TestCase ): """simple docstring""" def __init__(self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=7 , SCREAMING_SNAKE_CASE__=3 , SCREAMING_SNAKE_CASE__=18 , SCREAMING_SNAKE_CASE__=30 , SCREAMING_SNAKE_CASE__=4_00 , SCREAMING_SNAKE_CASE__=True , SCREAMING_SNAKE_CASE__=None , SCREAMING_SNAKE_CASE__=True , SCREAMING_SNAKE_CASE__=None , SCREAMING_SNAKE_CASE__=True , ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : List[str] = size if size is not None else {"""shortest_edge""": 20} SCREAMING_SNAKE_CASE__ : List[Any] = crop_size if crop_size is not None else {"""height""": 18, """width""": 18} SCREAMING_SNAKE_CASE__ : Any = parent SCREAMING_SNAKE_CASE__ : List[str] = batch_size SCREAMING_SNAKE_CASE__ : Dict = num_channels SCREAMING_SNAKE_CASE__ : List[Any] = image_size SCREAMING_SNAKE_CASE__ : Dict = min_resolution SCREAMING_SNAKE_CASE__ : Dict = max_resolution SCREAMING_SNAKE_CASE__ : Optional[int] = do_resize SCREAMING_SNAKE_CASE__ : Optional[Any] = size SCREAMING_SNAKE_CASE__ : Union[str, Any] = do_center_crop SCREAMING_SNAKE_CASE__ : Optional[Any] = crop_size SCREAMING_SNAKE_CASE__ : List[str] = do_flip_channel_order def __magic_name__ (self ) -> int: """simple docstring""" return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_flip_channel_order": self.do_flip_channel_order, } @require_torch @require_vision class lowerCAmelCase_ (a__ , unittest.TestCase ): """simple docstring""" __UpperCamelCase : str = MobileViTImageProcessor if is_vision_available() else None def __magic_name__ (self ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE__ : Optional[Any] = MobileViTImageProcessingTester(self ) @property def __magic_name__ (self ) -> Any: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def __magic_name__ (self ) -> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__ , """do_resize""" ) ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__ , """size""" ) ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__ , """do_center_crop""" ) ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__ , """center_crop""" ) ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__ , """do_flip_channel_order""" ) ) def __magic_name__ (self ) -> Tuple: """simple docstring""" SCREAMING_SNAKE_CASE__ : Dict = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""shortest_edge""": 20} ) self.assertEqual(image_processor.crop_size , {"""height""": 18, """width""": 18} ) SCREAMING_SNAKE_CASE__ : Optional[Any] = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {"""shortest_edge""": 42} ) self.assertEqual(image_processor.crop_size , {"""height""": 84, """width""": 84} ) def __magic_name__ (self ) -> Union[str, Any]: """simple docstring""" pass def __magic_name__ (self ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE__ : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE__ : List[str] = prepare_image_inputs(self.image_processor_tester , equal_resolution=SCREAMING_SNAKE_CASE__ ) for image in image_inputs: self.assertIsInstance(SCREAMING_SNAKE_CASE__ , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE__ : Dict = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched SCREAMING_SNAKE_CASE__ : List[str] = image_processing(SCREAMING_SNAKE_CASE__ , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) def __magic_name__ (self ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE__ : Optional[int] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE__ : Tuple = prepare_image_inputs(self.image_processor_tester , equal_resolution=SCREAMING_SNAKE_CASE__ , numpify=SCREAMING_SNAKE_CASE__ ) for image in image_inputs: self.assertIsInstance(SCREAMING_SNAKE_CASE__ , np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE__ : Optional[Any] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched SCREAMING_SNAKE_CASE__ : Optional[int] = image_processing(SCREAMING_SNAKE_CASE__ , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) def __magic_name__ (self ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE__ : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE__ : Any = prepare_image_inputs(self.image_processor_tester , equal_resolution=SCREAMING_SNAKE_CASE__ , torchify=SCREAMING_SNAKE_CASE__ ) for image in image_inputs: self.assertIsInstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE__ : List[str] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched SCREAMING_SNAKE_CASE__ : Optional[Any] = image_processing(SCREAMING_SNAKE_CASE__ , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , )
545
0
'''simple docstring''' def __magic_name__ ( __UpperCAmelCase ) -> list: '''simple docstring''' if any(not isinstance(__snake_case , __snake_case ) or x < 0 for x in sequence ): raise TypeError("""Sequence must be list of non-negative integers""" ) for _ in range(len(__snake_case ) ): for i, (rod_upper, rod_lower) in enumerate(zip(__snake_case , sequence[1:] ) ): if rod_upper > rod_lower: sequence[i] -= rod_upper - rod_lower sequence[i + 1] += rod_upper - rod_lower return sequence if __name__ == "__main__": assert bead_sort([5, 4, 3, 2, 1]) == [1, 2, 3, 4, 5] assert bead_sort([7, 9, 4, 3, 5]) == [3, 4, 5, 7, 9]
109
"""simple docstring""" def A ( __snake_case: str ) -> list: """simple docstring""" return [ txt[:a] + txt[a].upper() + txt[a + 1 :] for a in range(len(__snake_case ) ) if txt[a].isalpha() ] if __name__ == "__main__": __import__("""doctest""").testmod()
545
0
import argparse import os import shutil import torch from emmental.modules import MagnitudeBinarizer, ThresholdBinarizer, TopKBinarizer def snake_case__ ( UpperCAmelCase : Optional[int] ): lowerCAmelCase__ :List[Any] = args.pruning_method lowerCAmelCase__ :Any = args.threshold lowerCAmelCase__ :List[Any] = args.model_name_or_path.rstrip("/" ) lowerCAmelCase__ :int = args.target_model_path print(F'''Load fine-pruned model from {model_name_or_path}''' ) lowerCAmelCase__ :str = torch.load(os.path.join(UpperCAmelCase , "pytorch_model.bin" ) ) lowerCAmelCase__ :str = {} for name, tensor in model.items(): if "embeddings" in name or "LayerNorm" in name or "pooler" in name: lowerCAmelCase__ :Dict = tensor print(F'''Copied layer {name}''' ) elif "classifier" in name or "qa_output" in name: lowerCAmelCase__ :Union[str, Any] = tensor print(F'''Copied layer {name}''' ) elif "bias" in name: lowerCAmelCase__ :Tuple = tensor print(F'''Copied layer {name}''' ) else: if pruning_method == "magnitude": lowerCAmelCase__ :List[str] = MagnitudeBinarizer.apply(inputs=UpperCAmelCase , threshold=UpperCAmelCase ) lowerCAmelCase__ :int = tensor * mask print(F'''Pruned layer {name}''' ) elif pruning_method == "topK": if "mask_scores" in name: continue lowerCAmelCase__ :List[str] = name[:-6] lowerCAmelCase__ :Dict = model[F'''{prefix_}mask_scores'''] lowerCAmelCase__ :Optional[int] = TopKBinarizer.apply(UpperCAmelCase , UpperCAmelCase ) lowerCAmelCase__ :int = tensor * mask print(F'''Pruned layer {name}''' ) elif pruning_method == "sigmoied_threshold": if "mask_scores" in name: continue lowerCAmelCase__ :Optional[Any] = name[:-6] lowerCAmelCase__ :List[Any] = model[F'''{prefix_}mask_scores'''] lowerCAmelCase__ :Any = ThresholdBinarizer.apply(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) lowerCAmelCase__ :List[Any] = tensor * mask print(F'''Pruned layer {name}''' ) elif pruning_method == "l0": if "mask_scores" in name: continue lowerCAmelCase__ :Tuple = name[:-6] lowerCAmelCase__ :Optional[int] = model[F'''{prefix_}mask_scores'''] lowerCAmelCase__ :Union[str, Any] = -0.1, 1.1 lowerCAmelCase__ :Union[str, Any] = torch.sigmoid(UpperCAmelCase ) lowerCAmelCase__ :str = s * (r - l) + l lowerCAmelCase__ :Optional[int] = s_bar.clamp(min=0.0 , max=1.0 ) lowerCAmelCase__ :Optional[Any] = tensor * mask print(F'''Pruned layer {name}''' ) else: raise ValueError("Unknown pruning method" ) if target_model_path is None: lowerCAmelCase__ :Tuple = os.path.join( os.path.dirname(UpperCAmelCase ) , F'''bertarized_{os.path.basename(UpperCAmelCase )}''' ) if not os.path.isdir(UpperCAmelCase ): shutil.copytree(UpperCAmelCase , UpperCAmelCase ) print(F'''\nCreated folder {target_model_path}''' ) torch.save(UpperCAmelCase , os.path.join(UpperCAmelCase , "pytorch_model.bin" ) ) print("\nPruned model saved! See you later!" ) if __name__ == "__main__": _a : List[Any] = argparse.ArgumentParser() parser.add_argument( """--pruning_method""", choices=["""l0""", """magnitude""", """topK""", """sigmoied_threshold"""], type=str, required=True, help=( """Pruning Method (l0 = L0 regularization, magnitude = Magnitude pruning, topK = Movement pruning,""" """ sigmoied_threshold = Soft movement pruning)""" ), ) parser.add_argument( """--threshold""", type=float, required=False, help=( """For `magnitude` and `topK`, it is the level of remaining weights (in %) in the fine-pruned model.""" """For `sigmoied_threshold`, it is the threshold \tau against which the (sigmoied) scores are compared.""" """Not needed for `l0`""" ), ) parser.add_argument( """--model_name_or_path""", type=str, required=True, help="""Folder containing the model that was previously fine-pruned""", ) parser.add_argument( """--target_model_path""", default=None, type=str, required=False, help="""Folder containing the model that was previously fine-pruned""", ) _a : List[Any] = parser.parse_args() main(args)
704
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 _a : Tuple = logging.get_logger(__name__) _a : Optional[int] = { """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 _UpperCAmelCase ( _A ): """simple docstring""" A = '''levit''' def __init__( self , _lowerCAmelCase=224 , _lowerCAmelCase=3 , _lowerCAmelCase=3 , _lowerCAmelCase=2 , _lowerCAmelCase=1 , _lowerCAmelCase=16 , _lowerCAmelCase=[128, 256, 384] , _lowerCAmelCase=[4, 8, 12] , _lowerCAmelCase=[4, 4, 4] , _lowerCAmelCase=[16, 16, 16] , _lowerCAmelCase=0 , _lowerCAmelCase=[2, 2, 2] , _lowerCAmelCase=[2, 2, 2] , _lowerCAmelCase=0.02 , **_lowerCAmelCase , ): '''simple docstring''' super().__init__(**_lowerCAmelCase ) lowerCAmelCase__ :List[str] = image_size lowerCAmelCase__ :Dict = num_channels lowerCAmelCase__ :List[Any] = kernel_size lowerCAmelCase__ :List[Any] = stride lowerCAmelCase__ :Tuple = padding lowerCAmelCase__ :str = hidden_sizes lowerCAmelCase__ :Union[str, Any] = num_attention_heads lowerCAmelCase__ :Optional[int] = depths lowerCAmelCase__ :Union[str, Any] = key_dim lowerCAmelCase__ :str = drop_path_rate lowerCAmelCase__ :int = patch_size lowerCAmelCase__ :Dict = attention_ratio lowerCAmelCase__ :Dict = mlp_ratio lowerCAmelCase__ :Optional[int] = initializer_range lowerCAmelCase__ :Tuple = [ ["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 _UpperCAmelCase ( _A ): """simple docstring""" A = version.parse('''1.11''' ) @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def snake_case_ ( self ): '''simple docstring''' return 1e-4
111
0
"""simple docstring""" import unittest from transformers import ( MODEL_FOR_OBJECT_DETECTION_MAPPING, AutoFeatureExtractor, AutoModelForObjectDetection, ObjectDetectionPipeline, is_vision_available, pipeline, ) from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_pytesseract, require_tf, require_timm, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class UpperCAmelCase : @staticmethod def __UpperCAmelCase ( *__lowerCamelCase : Tuple , **__lowerCamelCase : str ): """simple docstring""" pass @is_pipeline_test @require_vision @require_timm @require_torch class UpperCAmelCase ( unittest.TestCase ): A__ : Optional[Any] = MODEL_FOR_OBJECT_DETECTION_MAPPING def __UpperCAmelCase ( self : Union[str, Any] , __lowerCamelCase : Optional[int] , __lowerCamelCase : Optional[int] , __lowerCamelCase : str ): """simple docstring""" _snake_case = ObjectDetectionPipeline(model=__lowerCamelCase , image_processor=__lowerCamelCase ) return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"] def __UpperCAmelCase ( self : List[str] , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : Any ): """simple docstring""" _snake_case = object_detector('''./tests/fixtures/tests_samples/COCO/000000039769.png''' , threshold=0.0 ) self.assertGreater(len(__lowerCamelCase ) , 0 ) for detected_object in outputs: self.assertEqual( __lowerCamelCase , { '''score''': ANY(__lowerCamelCase ), '''label''': ANY(__lowerCamelCase ), '''box''': {'''xmin''': ANY(__lowerCamelCase ), '''ymin''': ANY(__lowerCamelCase ), '''xmax''': ANY(__lowerCamelCase ), '''ymax''': ANY(__lowerCamelCase )}, } , ) import datasets _snake_case = datasets.load_dataset('''hf-internal-testing/fixtures_image_utils''' , '''image''' , split='''test''' ) _snake_case = [ Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ), '''http://images.cocodataset.org/val2017/000000039769.jpg''', # RGBA dataset[0]['''file'''], # LA dataset[1]['''file'''], # L dataset[2]['''file'''], ] _snake_case = object_detector(__lowerCamelCase , threshold=0.0 ) self.assertEqual(len(__lowerCamelCase ) , len(__lowerCamelCase ) ) for outputs in batch_outputs: self.assertGreater(len(__lowerCamelCase ) , 0 ) for detected_object in outputs: self.assertEqual( __lowerCamelCase , { '''score''': ANY(__lowerCamelCase ), '''label''': ANY(__lowerCamelCase ), '''box''': {'''xmin''': ANY(__lowerCamelCase ), '''ymin''': ANY(__lowerCamelCase ), '''xmax''': ANY(__lowerCamelCase ), '''ymax''': ANY(__lowerCamelCase )}, } , ) @require_tf @unittest.skip('''Object detection not implemented in TF''' ) def __UpperCAmelCase ( self : Optional[Any] ): """simple docstring""" pass @require_torch def __UpperCAmelCase ( self : Any ): """simple docstring""" _snake_case = '''hf-internal-testing/tiny-detr-mobilenetsv3''' _snake_case = AutoModelForObjectDetection.from_pretrained(__lowerCamelCase ) _snake_case = AutoFeatureExtractor.from_pretrained(__lowerCamelCase ) _snake_case = ObjectDetectionPipeline(model=__lowerCamelCase , feature_extractor=__lowerCamelCase ) _snake_case = object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg''' , threshold=0.0 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {'''score''': 0.3_3_7_6, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 1_5_9, '''ymin''': 1_2_0, '''xmax''': 4_8_0, '''ymax''': 3_5_9}}, {'''score''': 0.3_3_7_6, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 1_5_9, '''ymin''': 1_2_0, '''xmax''': 4_8_0, '''ymax''': 3_5_9}}, ] , ) _snake_case = object_detector( [ '''http://images.cocodataset.org/val2017/000000039769.jpg''', '''http://images.cocodataset.org/val2017/000000039769.jpg''', ] , threshold=0.0 , ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ [ {'''score''': 0.3_3_7_6, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 1_5_9, '''ymin''': 1_2_0, '''xmax''': 4_8_0, '''ymax''': 3_5_9}}, {'''score''': 0.3_3_7_6, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 1_5_9, '''ymin''': 1_2_0, '''xmax''': 4_8_0, '''ymax''': 3_5_9}}, ], [ {'''score''': 0.3_3_7_6, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 1_5_9, '''ymin''': 1_2_0, '''xmax''': 4_8_0, '''ymax''': 3_5_9}}, {'''score''': 0.3_3_7_6, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 1_5_9, '''ymin''': 1_2_0, '''xmax''': 4_8_0, '''ymax''': 3_5_9}}, ], ] , ) @require_torch @slow def __UpperCAmelCase ( self : Any ): """simple docstring""" _snake_case = '''facebook/detr-resnet-50''' _snake_case = AutoModelForObjectDetection.from_pretrained(__lowerCamelCase ) _snake_case = AutoFeatureExtractor.from_pretrained(__lowerCamelCase ) _snake_case = ObjectDetectionPipeline(model=__lowerCamelCase , feature_extractor=__lowerCamelCase ) _snake_case = object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg''' ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {'''score''': 0.9_9_8_2, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_0, '''xmax''': 1_7_5, '''ymax''': 1_1_7}}, {'''score''': 0.9_9_6_0, '''label''': '''remote''', '''box''': {'''xmin''': 3_3_3, '''ymin''': 7_2, '''xmax''': 3_6_8, '''ymax''': 1_8_7}}, {'''score''': 0.9_9_5_5, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 6_3_9, '''ymax''': 4_7_3}}, {'''score''': 0.9_9_8_8, '''label''': '''cat''', '''box''': {'''xmin''': 1_3, '''ymin''': 5_2, '''xmax''': 3_1_4, '''ymax''': 4_7_0}}, {'''score''': 0.9_9_8_7, '''label''': '''cat''', '''box''': {'''xmin''': 3_4_5, '''ymin''': 2_3, '''xmax''': 6_4_0, '''ymax''': 3_6_8}}, ] , ) _snake_case = object_detector( [ '''http://images.cocodataset.org/val2017/000000039769.jpg''', '''http://images.cocodataset.org/val2017/000000039769.jpg''', ] ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ [ {'''score''': 0.9_9_8_2, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_0, '''xmax''': 1_7_5, '''ymax''': 1_1_7}}, {'''score''': 0.9_9_6_0, '''label''': '''remote''', '''box''': {'''xmin''': 3_3_3, '''ymin''': 7_2, '''xmax''': 3_6_8, '''ymax''': 1_8_7}}, {'''score''': 0.9_9_5_5, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 6_3_9, '''ymax''': 4_7_3}}, {'''score''': 0.9_9_8_8, '''label''': '''cat''', '''box''': {'''xmin''': 1_3, '''ymin''': 5_2, '''xmax''': 3_1_4, '''ymax''': 4_7_0}}, {'''score''': 0.9_9_8_7, '''label''': '''cat''', '''box''': {'''xmin''': 3_4_5, '''ymin''': 2_3, '''xmax''': 6_4_0, '''ymax''': 3_6_8}}, ], [ {'''score''': 0.9_9_8_2, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_0, '''xmax''': 1_7_5, '''ymax''': 1_1_7}}, {'''score''': 0.9_9_6_0, '''label''': '''remote''', '''box''': {'''xmin''': 3_3_3, '''ymin''': 7_2, '''xmax''': 3_6_8, '''ymax''': 1_8_7}}, {'''score''': 0.9_9_5_5, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 6_3_9, '''ymax''': 4_7_3}}, {'''score''': 0.9_9_8_8, '''label''': '''cat''', '''box''': {'''xmin''': 1_3, '''ymin''': 5_2, '''xmax''': 3_1_4, '''ymax''': 4_7_0}}, {'''score''': 0.9_9_8_7, '''label''': '''cat''', '''box''': {'''xmin''': 3_4_5, '''ymin''': 2_3, '''xmax''': 6_4_0, '''ymax''': 3_6_8}}, ], ] , ) @require_torch @slow def __UpperCAmelCase ( self : Optional[int] ): """simple docstring""" _snake_case = '''facebook/detr-resnet-50''' _snake_case = pipeline('''object-detection''' , model=__lowerCamelCase ) _snake_case = object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg''' ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {'''score''': 0.9_9_8_2, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_0, '''xmax''': 1_7_5, '''ymax''': 1_1_7}}, {'''score''': 0.9_9_6_0, '''label''': '''remote''', '''box''': {'''xmin''': 3_3_3, '''ymin''': 7_2, '''xmax''': 3_6_8, '''ymax''': 1_8_7}}, {'''score''': 0.9_9_5_5, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 6_3_9, '''ymax''': 4_7_3}}, {'''score''': 0.9_9_8_8, '''label''': '''cat''', '''box''': {'''xmin''': 1_3, '''ymin''': 5_2, '''xmax''': 3_1_4, '''ymax''': 4_7_0}}, {'''score''': 0.9_9_8_7, '''label''': '''cat''', '''box''': {'''xmin''': 3_4_5, '''ymin''': 2_3, '''xmax''': 6_4_0, '''ymax''': 3_6_8}}, ] , ) _snake_case = object_detector( [ '''http://images.cocodataset.org/val2017/000000039769.jpg''', '''http://images.cocodataset.org/val2017/000000039769.jpg''', ] ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ [ {'''score''': 0.9_9_8_2, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_0, '''xmax''': 1_7_5, '''ymax''': 1_1_7}}, {'''score''': 0.9_9_6_0, '''label''': '''remote''', '''box''': {'''xmin''': 3_3_3, '''ymin''': 7_2, '''xmax''': 3_6_8, '''ymax''': 1_8_7}}, {'''score''': 0.9_9_5_5, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 6_3_9, '''ymax''': 4_7_3}}, {'''score''': 0.9_9_8_8, '''label''': '''cat''', '''box''': {'''xmin''': 1_3, '''ymin''': 5_2, '''xmax''': 3_1_4, '''ymax''': 4_7_0}}, {'''score''': 0.9_9_8_7, '''label''': '''cat''', '''box''': {'''xmin''': 3_4_5, '''ymin''': 2_3, '''xmax''': 6_4_0, '''ymax''': 3_6_8}}, ], [ {'''score''': 0.9_9_8_2, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_0, '''xmax''': 1_7_5, '''ymax''': 1_1_7}}, {'''score''': 0.9_9_6_0, '''label''': '''remote''', '''box''': {'''xmin''': 3_3_3, '''ymin''': 7_2, '''xmax''': 3_6_8, '''ymax''': 1_8_7}}, {'''score''': 0.9_9_5_5, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 6_3_9, '''ymax''': 4_7_3}}, {'''score''': 0.9_9_8_8, '''label''': '''cat''', '''box''': {'''xmin''': 1_3, '''ymin''': 5_2, '''xmax''': 3_1_4, '''ymax''': 4_7_0}}, {'''score''': 0.9_9_8_7, '''label''': '''cat''', '''box''': {'''xmin''': 3_4_5, '''ymin''': 2_3, '''xmax''': 6_4_0, '''ymax''': 3_6_8}}, ], ] , ) @require_torch @slow def __UpperCAmelCase ( self : Union[str, Any] ): """simple docstring""" _snake_case = 0.9_9_8_5 _snake_case = '''facebook/detr-resnet-50''' _snake_case = pipeline('''object-detection''' , model=__lowerCamelCase ) _snake_case = object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg''' , threshold=__lowerCamelCase ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {'''score''': 0.9_9_8_8, '''label''': '''cat''', '''box''': {'''xmin''': 1_3, '''ymin''': 5_2, '''xmax''': 3_1_4, '''ymax''': 4_7_0}}, {'''score''': 0.9_9_8_7, '''label''': '''cat''', '''box''': {'''xmin''': 3_4_5, '''ymin''': 2_3, '''xmax''': 6_4_0, '''ymax''': 3_6_8}}, ] , ) @require_torch @require_pytesseract @slow def __UpperCAmelCase ( self : Union[str, Any] ): """simple docstring""" _snake_case = '''Narsil/layoutlmv3-finetuned-funsd''' _snake_case = 0.9_9_9_3 _snake_case = pipeline('''object-detection''' , model=__lowerCamelCase , threshold=__lowerCamelCase ) _snake_case = object_detector( '''https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png''' ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {'''score''': 0.9_9_9_3, '''label''': '''I-ANSWER''', '''box''': {'''xmin''': 2_9_4, '''ymin''': 2_5_4, '''xmax''': 3_4_3, '''ymax''': 2_6_4}}, {'''score''': 0.9_9_9_3, '''label''': '''I-ANSWER''', '''box''': {'''xmin''': 2_9_4, '''ymin''': 2_5_4, '''xmax''': 3_4_3, '''ymax''': 2_6_4}}, ] , )
103
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from ..models.whisper import WhisperForConditionalGeneration, WhisperProcessor from .base import PipelineTool class __snake_case ( lowerCAmelCase ): _a : Optional[int]= "openai/whisper-base" _a : int= ( "This is a tool that transcribes an audio into text. It takes an input named `audio` and returns the " "transcribed text." ) _a : int= "transcriber" _a : List[str]= WhisperProcessor _a : Optional[int]= WhisperForConditionalGeneration _a : List[str]= ["audio"] _a : Any= ["text"] def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' return self.pre_processor(snake_case ,return_tensors="""pt""" ).input_features def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' return self.model.generate(inputs=snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' return self.pre_processor.batch_decode(snake_case ,skip_special_tokens=snake_case )[0]
336
0
"""simple docstring""" from abc import ABC, abstractmethod from argparse import ArgumentParser class lowerCAmelCase__ ( UpperCamelCase_ ): @staticmethod @abstractmethod def lowercase_ ( UpperCamelCase__ ): '''simple docstring''' raise NotImplementedError() @abstractmethod def lowercase_ ( self ): '''simple docstring''' raise NotImplementedError()
712
"""simple docstring""" import io import os import unicodedata from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ="""▁""" __UpperCAmelCase ={"""vocab_file""": """vocab.txt""", """sentencepiece_model_ckpt""": """sentencepiece.bpe.model"""} __UpperCAmelCase ={ """sentencepiece_model_file""": """sentencepiece.bpe.model""", """vocab_file""": """vocab.txt""", } __UpperCAmelCase ={ """vocab_file""": { """ernie-m-base""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt""", """ernie-m-large""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt""", }, """sentencepiece_model_file""": { """ernie-m-base""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model""", """ernie-m-large""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model""", }, } __UpperCAmelCase ={ """ernie-m-base""": 514, """ernie-m-large""": 514, } __UpperCAmelCase ={ """ernie-m-base""": {"""do_lower_case""": False}, """ernie-m-large""": {"""do_lower_case""": False}, } class lowerCAmelCase__ ( UpperCAmelCase_ ): lowercase__ : List[str] = ["input_ids"] lowercase__ : Tuple = VOCAB_FILES_NAMES lowercase__ : Dict = PRETRAINED_INIT_CONFIGURATION lowercase__ : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase__ : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP lowercase__ : Dict = RESOURCE_FILES_NAMES def __init__( self , UpperCamelCase__ , UpperCamelCase__=None , UpperCamelCase__=False , UpperCamelCase__="utf8" , UpperCamelCase__="[UNK]" , UpperCamelCase__="[SEP]" , UpperCamelCase__="[PAD]" , UpperCamelCase__="[CLS]" , UpperCamelCase__="[MASK]" , UpperCamelCase__ = None , **UpperCamelCase__ , ): '''simple docstring''' A__ = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=UpperCamelCase__ , unk_token=UpperCamelCase__ , sep_token=UpperCamelCase__ , pad_token=UpperCamelCase__ , cls_token=UpperCamelCase__ , mask_token=UpperCamelCase__ , vocab_file=UpperCamelCase__ , encoding=UpperCamelCase__ , sp_model_kwargs=self.sp_model_kwargs , **UpperCamelCase__ , ) A__ = do_lower_case A__ = sentencepiece_model_ckpt A__ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(UpperCamelCase__ ) # to mimic paddlenlp.transformers.ernie_m.tokenizer.ErnieMTokenizer functioning if vocab_file is not None: A__ = self.load_vocab(filepath=UpperCamelCase__ ) else: A__ = {self.sp_model.id_to_piece(UpperCamelCase__ ): id for id in range(self.sp_model.get_piece_size() )} A__ = {v: k for k, v in self.vocab.items()} def lowercase_ ( self , UpperCamelCase__ ): '''simple docstring''' if text is None: return None A__ = self.tokenize(UpperCamelCase__ ) A__ , A__ = "", [] for i, ch in enumerate(UpperCamelCase__ ): if ch in self.SP_CHAR_MAPPING: A__ = self.SP_CHAR_MAPPING.get(UpperCamelCase__ ) else: A__ = unicodedata.normalize("NFKC" , UpperCamelCase__ ) if self.is_whitespace(UpperCamelCase__ ): continue normalized_text += ch char_mapping.extend([i] * len(UpperCamelCase__ ) ) A__ , A__ , A__ = normalized_text, [], 0 if self.do_lower_case: A__ = text.lower() for token in split_tokens: if token[:1] == "▁": A__ = token[1:] A__ = text[offset:].index(UpperCamelCase__ ) + offset A__ = start + len(UpperCamelCase__ ) token_mapping.append((char_mapping[start], char_mapping[end - 1] + 1) ) A__ = end return token_mapping @property def lowercase_ ( self ): '''simple docstring''' return len(self.vocab ) def lowercase_ ( self ): '''simple docstring''' return dict(self.vocab , **self.added_tokens_encoder ) def __getstate__( self ): '''simple docstring''' A__ = self.__dict__.copy() A__ = None return state def __setstate__( self , UpperCamelCase__ ): '''simple docstring''' A__ = d # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): A__ = {} A__ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.sentencepiece_model_ckpt ) def lowercase_ ( self , UpperCamelCase__ ): '''simple docstring''' return "".join((self.SP_CHAR_MAPPING.get(UpperCamelCase__ , UpperCamelCase__ ) for c in text) ) def lowercase_ ( self , UpperCamelCase__ , UpperCamelCase__=False , UpperCamelCase__=64 , UpperCamelCase__=0.1 ): '''simple docstring''' if self.sp_model_kwargs.get("enable_sampling" ) is True: A__ = True if self.sp_model_kwargs.get("alpha" ) is not None: A__ = self.sp_model_kwargs.get("alpha" ) if self.sp_model_kwargs.get("nbest_size" ) is not None: A__ = self.sp_model_kwargs.get("nbest_size" ) if not enable_sampling: A__ = self.sp_model.EncodeAsPieces(UpperCamelCase__ ) else: A__ = self.sp_model.SampleEncodeAsPieces(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) A__ = [] for pi, piece in enumerate(UpperCamelCase__ ): if piece == SPIECE_UNDERLINE: if not pieces[pi + 1].startswith(UpperCamelCase__ ) and pi != 0: new_pieces.append(UpperCamelCase__ ) continue else: continue A__ = 0 for i, chunk in enumerate(UpperCamelCase__ ): if chunk == SPIECE_UNDERLINE: continue if self.is_ch_char(UpperCamelCase__ ) or self.is_punct(UpperCamelCase__ ): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) new_pieces.append(UpperCamelCase__ ) A__ = i + 1 elif chunk.isdigit() and i > 0 and not piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) A__ = i elif not chunk.isdigit() and i > 0 and piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) A__ = i if len(UpperCamelCase__ ) > lst_i: new_pieces.append(piece[lst_i:] ) return new_pieces def lowercase_ ( self , UpperCamelCase__ ): '''simple docstring''' A__ = "".join(UpperCamelCase__ ).replace(UpperCamelCase__ , " " ).strip() return out_string def lowercase_ ( self , UpperCamelCase__ ): '''simple docstring''' A__ = self.convert_ids_to_tokens(UpperCamelCase__ ) A__ = "".join(UpperCamelCase__ ).replace(UpperCamelCase__ , " " ).strip() return out_string def lowercase_ ( self , UpperCamelCase__ ): '''simple docstring''' return self.vocab.get(UpperCamelCase__ , self.vocab.get(self.unk_token ) ) def lowercase_ ( self , UpperCamelCase__ ): '''simple docstring''' return self.reverse_vocab.get(UpperCamelCase__ , self.unk_token ) def lowercase_ ( self , UpperCamelCase__ , UpperCamelCase__=None ): '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] A__ = [self.cls_token_id] A__ = [self.sep_token_id] return _cls + token_ids_a + _sep + _sep + token_ids_a + _sep def lowercase_ ( self , UpperCamelCase__ , UpperCamelCase__=None ): '''simple docstring''' if offset_mapping_a is None: return [(0, 0)] + offset_mapping_a + [(0, 0)] return [(0, 0)] + offset_mapping_a + [(0, 0), (0, 0)] + offset_mapping_a + [(0, 0)] def lowercase_ ( self , UpperCamelCase__ , UpperCamelCase__=None , UpperCamelCase__=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(UpperCamelCase__ )) + [1, 1] + ([0] * len(UpperCamelCase__ )) + [1] return [1] + ([0] * len(UpperCamelCase__ )) + [1] def lowercase_ ( self , UpperCamelCase__ , UpperCamelCase__ = None ): '''simple docstring''' if token_ids_a is None: # [CLS] X [SEP] return (len(UpperCamelCase__ ) + 2) * [0] # [CLS] A [SEP] [SEP] B [SEP] return [0] * (len(UpperCamelCase__ ) + 1) + [1] * (len(UpperCamelCase__ ) + 3) def lowercase_ ( self , UpperCamelCase__ ): '''simple docstring''' if "\u4e00" <= char <= "\u9fff": return True return False def lowercase_ ( self , UpperCamelCase__ ): '''simple docstring''' if ("a" <= char <= "z") or ("A" <= char <= "Z"): return True return False def lowercase_ ( self , UpperCamelCase__ ): '''simple docstring''' if char in ",;:.?!~,;:。?!《》【】": return True return False def lowercase_ ( self , UpperCamelCase__ ): '''simple docstring''' if char == " " or char == "\t" or char == "\n" or char == "\r": return True if len(UpperCamelCase__ ) == 1: A__ = unicodedata.category(UpperCamelCase__ ) if cat == "Zs": return True return False def lowercase_ ( self , UpperCamelCase__ ): '''simple docstring''' A__ = {} with io.open(UpperCamelCase__ , "r" , encoding="utf-8" ) as f: for index, line in enumerate(UpperCamelCase__ ): A__ = line.rstrip("\n" ) A__ = int(UpperCamelCase__ ) return token_to_idx def lowercase_ ( self , UpperCamelCase__ , UpperCamelCase__ = None ): '''simple docstring''' A__ = 0 if os.path.isdir(UpperCamelCase__ ): A__ = os.path.join( UpperCamelCase__ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) else: A__ = (filename_prefix + "-" if filename_prefix else "") + save_directory with open(UpperCamelCase__ , "w" , encoding="utf-8" ) as writer: for token, token_index in sorted(self.vocab.items() , key=lambda UpperCamelCase__ : kv[1] ): if index != token_index: logger.warning( f"""Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.""" " Please check that the vocabulary is not corrupted!" ) A__ = token_index writer.write(token + "\n" ) index += 1 A__ = os.path.join(UpperCamelCase__ , "sentencepiece.bpe.model" ) with open(UpperCamelCase__ , "wb" ) as fi: A__ = self.sp_model.serialized_model_proto() fi.write(UpperCamelCase__ ) return (vocab_file,)
261
0
"""simple docstring""" import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_video_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import VivitImageProcessor class UpperCamelCase (unittest.TestCase ): def __init__( self :List[str] , __magic_name__ :List[Any] , __magic_name__ :Optional[Any]=7 , __magic_name__ :Dict=3 , __magic_name__ :Any=10 , __magic_name__ :Optional[int]=18 , __magic_name__ :Optional[int]=30 , __magic_name__ :Optional[int]=400 , __magic_name__ :List[str]=True , __magic_name__ :Optional[Any]=None , __magic_name__ :int=True , __magic_name__ :Optional[int]=[0.5, 0.5, 0.5] , __magic_name__ :List[str]=[0.5, 0.5, 0.5] , __magic_name__ :Optional[Any]=None , ) ->Any: lowercase : List[str] = size if size is not None else {"""shortest_edge""": 18} lowercase : List[str] = crop_size if crop_size is not None else {"""height""": 18, """width""": 18} lowercase : Union[str, Any] = parent lowercase : Any = batch_size lowercase : Union[str, Any] = num_channels lowercase : Union[str, Any] = num_frames lowercase : List[str] = image_size lowercase : List[str] = min_resolution lowercase : int = max_resolution lowercase : Optional[Any] = do_resize lowercase : Optional[Any] = size lowercase : Optional[int] = do_normalize lowercase : str = image_mean lowercase : List[str] = image_std lowercase : Tuple = crop_size def __snake_case ( self :List[str] ) ->str: return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, "crop_size": self.crop_size, } @require_torch @require_vision class UpperCamelCase (__snake_case , unittest.TestCase ): _SCREAMING_SNAKE_CASE : List[Any] = VivitImageProcessor if is_vision_available() else None def __snake_case ( self :str ) ->Tuple: lowercase : Dict = VivitImageProcessingTester(self ) @property def __snake_case ( self :int ) ->Optional[int]: return self.image_processor_tester.prepare_image_processor_dict() def __snake_case ( self :Dict ) ->int: lowercase : Dict = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__ , """image_mean""" ) ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__ , """image_std""" ) ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__ , """do_normalize""" ) ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__ , """do_resize""" ) ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__ , """do_center_crop""" ) ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__ , """size""" ) ) def __snake_case ( self :int ) ->str: lowercase : Optional[Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""shortest_edge""": 18} ) self.assertEqual(image_processor.crop_size , {"""height""": 18, """width""": 18} ) lowercase : Union[str, Any] = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {"""shortest_edge""": 42} ) self.assertEqual(image_processor.crop_size , {"""height""": 84, """width""": 84} ) def __snake_case ( self :str ) ->Dict: lowercase : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL videos lowercase : List[str] = prepare_video_inputs(self.image_processor_tester , equal_resolution=SCREAMING_SNAKE_CASE__ ) for video in video_inputs: self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(video[0] , Image.Image ) # Test not batched input lowercase : Optional[Any] = image_processing(video_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched lowercase : Optional[Any] = image_processing(SCREAMING_SNAKE_CASE__ , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) def __snake_case ( self :Union[str, Any] ) ->Optional[Any]: lowercase : str = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors lowercase : Optional[int] = prepare_video_inputs(self.image_processor_tester , equal_resolution=SCREAMING_SNAKE_CASE__ , numpify=SCREAMING_SNAKE_CASE__ ) for video in video_inputs: self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(video[0] , np.ndarray ) # Test not batched input lowercase : List[str] = image_processing(video_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched lowercase : List[str] = image_processing(SCREAMING_SNAKE_CASE__ , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) def __snake_case ( self :str ) ->Optional[Any]: lowercase : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors lowercase : Optional[int] = prepare_video_inputs(self.image_processor_tester , equal_resolution=SCREAMING_SNAKE_CASE__ , torchify=SCREAMING_SNAKE_CASE__ ) for video in video_inputs: self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(video[0] , torch.Tensor ) # Test not batched input lowercase : List[str] = image_processing(video_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched lowercase : Dict = image_processing(SCREAMING_SNAKE_CASE__ , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , )
264
import logging import os import sys from dataclasses import dataclass, field from typing import Optional import torch from datasets import load_dataset from torchvision.transforms import Compose, Lambda, Normalize, RandomHorizontalFlip, RandomResizedCrop, ToTensor from torchvision.transforms.functional import InterpolationMode import transformers from transformers import ( HfArgumentParser, Trainer, TrainingArguments, ViTImageProcessor, ViTMAEConfig, ViTMAEForPreTraining, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version _snake_case = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version('''4.31.0''') require_version('''datasets>=1.8.0''', '''To fix: pip install -r examples/pytorch/image-pretraining/requirements.txt''') @dataclass class _lowerCAmelCase : """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[str] =field( default="cifar10" , metadata={"help": "Name of a dataset from the datasets package"} ) SCREAMING_SNAKE_CASE_ : Optional[str] =field( default=__magic_name__ , metadata={"help": "The configuration name of the dataset to use (via the datasets library)."} ) SCREAMING_SNAKE_CASE_ : Optional[str] =field( default=__magic_name__ , metadata={"help": "The column name of the images in the files."} ) SCREAMING_SNAKE_CASE_ : Optional[str] =field(default=__magic_name__ , metadata={"help": "A folder containing the training data."} ) SCREAMING_SNAKE_CASE_ : Optional[str] =field(default=__magic_name__ , metadata={"help": "A folder containing the validation data."} ) SCREAMING_SNAKE_CASE_ : Optional[float] =field( default=0.15 , metadata={"help": "Percent to split off of train for validation."} ) SCREAMING_SNAKE_CASE_ : Optional[int] =field( default=__magic_name__ , metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of training examples to this " "value if set." ) } , ) SCREAMING_SNAKE_CASE_ : Optional[int] =field( default=__magic_name__ , metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of evaluation examples to this " "value if set." ) } , ) def __lowerCAmelCase ( self : Optional[Any] ): """simple docstring""" UpperCamelCase = {} if self.train_dir is not None: UpperCamelCase = self.train_dir if self.validation_dir is not None: UpperCamelCase = self.validation_dir UpperCamelCase = data_files if data_files else None @dataclass class _lowerCAmelCase : """simple docstring""" SCREAMING_SNAKE_CASE_ : str =field( default=__magic_name__ , metadata={ "help": ( "The model checkpoint for weights initialization.Don't set if you want to train a model from scratch." ) } , ) SCREAMING_SNAKE_CASE_ : Optional[str] =field( default=__magic_name__ , metadata={"help": "Pretrained config name or path if not the same as model_name_or_path"} ) SCREAMING_SNAKE_CASE_ : Optional[str] =field( default=__magic_name__ , metadata={ "help": ( "Override some existing default config settings when a model is trained from scratch. Example: " "n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index" ) } , ) SCREAMING_SNAKE_CASE_ : Optional[str] =field( default=__magic_name__ , metadata={"help": "Where do you want to store the pretrained models downloaded from s3"} ) SCREAMING_SNAKE_CASE_ : str =field( default="main" , metadata={"help": "The specific model version to use (can be a branch name, tag name or commit id)."} , ) SCREAMING_SNAKE_CASE_ : str =field(default=__magic_name__ , metadata={"help": "Name or path of preprocessor config."} ) SCREAMING_SNAKE_CASE_ : bool =field( default=__magic_name__ , metadata={ "help": ( "Will use the token generated when running `huggingface-cli login` (necessary to use this script " "with private models)." ) } , ) SCREAMING_SNAKE_CASE_ : float =field( default=0.75 , metadata={"help": "The ratio of the number of masked tokens in the input sequence."} ) SCREAMING_SNAKE_CASE_ : bool =field( default=__magic_name__ , metadata={"help": "Whether or not to train with normalized pixel values as target."} ) @dataclass class _lowerCAmelCase ( __magic_name__ ): """simple docstring""" SCREAMING_SNAKE_CASE_ : float =field( default=1E-3 , metadata={"help": "Base learning rate: absolute_lr = base_lr * total_batch_size / 256."} ) def __lowerCamelCase ( _lowercase ) -> Optional[int]: UpperCamelCase = torch.stack([example['pixel_values'] for example in examples] ) return {"pixel_values": pixel_values} def __lowerCamelCase ( ) -> List[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. UpperCamelCase = HfArgumentParser((ModelArguments, DataTrainingArguments, CustomTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('.json' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCamelCase , UpperCamelCase , UpperCamelCase = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCamelCase , UpperCamelCase , UpperCamelCase = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry('run_mae' , _lowercase , _lowercase ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() UpperCamelCase = training_args.get_process_log_level() logger.setLevel(_lowercase ) transformers.utils.logging.set_verbosity(_lowercase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F'Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}' + F'distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}' ) logger.info(F'Training/evaluation parameters {training_args}' ) # Detecting last checkpoint. UpperCamelCase = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: UpperCamelCase = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F'Output directory ({training_args.output_dir}) already exists and is not empty. ' 'Use --overwrite_output_dir to overcome.' ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F'Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change ' 'the `--output_dir` or add `--overwrite_output_dir` to train from scratch.' ) # Initialize our dataset. UpperCamelCase = load_dataset( data_args.dataset_name , data_args.dataset_config_name , data_files=data_args.data_files , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) # If we don't have a validation split, split off a percentage of train as validation. UpperCamelCase = None if 'validation' in ds.keys() else data_args.train_val_split if isinstance(data_args.train_val_split , _lowercase ) and data_args.train_val_split > 0.0: UpperCamelCase = ds['train'].train_test_split(data_args.train_val_split ) UpperCamelCase = split['train'] UpperCamelCase = split['test'] # Load pretrained model and image processor # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. UpperCamelCase = { 'cache_dir': model_args.cache_dir, 'revision': model_args.model_revision, 'use_auth_token': True if model_args.use_auth_token else None, } if model_args.config_name: UpperCamelCase = ViTMAEConfig.from_pretrained(model_args.config_name , **_lowercase ) elif model_args.model_name_or_path: UpperCamelCase = ViTMAEConfig.from_pretrained(model_args.model_name_or_path , **_lowercase ) else: UpperCamelCase = ViTMAEConfig() logger.warning('You are instantiating a new config instance from scratch.' ) if model_args.config_overrides is not None: logger.info(F'Overriding config: {model_args.config_overrides}' ) config.update_from_string(model_args.config_overrides ) logger.info(F'New config: {config}' ) # adapt config config.update( { 'mask_ratio': model_args.mask_ratio, 'norm_pix_loss': model_args.norm_pix_loss, } ) # create image processor if model_args.image_processor_name: UpperCamelCase = ViTImageProcessor.from_pretrained(model_args.image_processor_name , **_lowercase ) elif model_args.model_name_or_path: UpperCamelCase = ViTImageProcessor.from_pretrained(model_args.model_name_or_path , **_lowercase ) else: UpperCamelCase = ViTImageProcessor() # create model if model_args.model_name_or_path: UpperCamelCase = ViTMAEForPreTraining.from_pretrained( model_args.model_name_or_path , from_tf=bool('.ckpt' in model_args.model_name_or_path ) , config=_lowercase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info('Training new model from scratch' ) UpperCamelCase = ViTMAEForPreTraining(_lowercase ) if training_args.do_train: UpperCamelCase = ds['train'].column_names else: UpperCamelCase = ds['validation'].column_names if data_args.image_column_name is not None: UpperCamelCase = data_args.image_column_name elif "image" in column_names: UpperCamelCase = 'image' elif "img" in column_names: UpperCamelCase = 'img' else: UpperCamelCase = column_names[0] # transformations as done in original MAE paper # source: https://github.com/facebookresearch/mae/blob/main/main_pretrain.py if "shortest_edge" in image_processor.size: UpperCamelCase = image_processor.size['shortest_edge'] else: UpperCamelCase = (image_processor.size['height'], image_processor.size['width']) UpperCamelCase = Compose( [ Lambda(lambda _lowercase : img.convert('RGB' ) if img.mode != "RGB" else img ), RandomResizedCrop(_lowercase , scale=(0.2, 1.0) , interpolation=InterpolationMode.BICUBIC ), RandomHorizontalFlip(), ToTensor(), Normalize(mean=image_processor.image_mean , std=image_processor.image_std ), ] ) def preprocess_images(_lowercase ): UpperCamelCase = [transforms(_lowercase ) for image in examples[image_column_name]] return examples if training_args.do_train: if "train" not in ds: raise ValueError('--do_train requires a train dataset' ) if data_args.max_train_samples is not None: UpperCamelCase = ds['train'].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) # Set the training transforms ds["train"].set_transform(_lowercase ) if training_args.do_eval: if "validation" not in ds: raise ValueError('--do_eval requires a validation dataset' ) if data_args.max_eval_samples is not None: UpperCamelCase = ( ds['validation'].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms ds["validation"].set_transform(_lowercase ) # Compute absolute learning rate UpperCamelCase = ( training_args.train_batch_size * training_args.gradient_accumulation_steps * training_args.world_size ) if training_args.base_learning_rate is not None: UpperCamelCase = training_args.base_learning_rate * total_train_batch_size / 256 # Initialize our trainer UpperCamelCase = Trainer( model=_lowercase , args=_lowercase , train_dataset=ds['train'] if training_args.do_train else None , eval_dataset=ds['validation'] if training_args.do_eval else None , tokenizer=_lowercase , data_collator=_lowercase , ) # Training if training_args.do_train: UpperCamelCase = None if training_args.resume_from_checkpoint is not None: UpperCamelCase = training_args.resume_from_checkpoint elif last_checkpoint is not None: UpperCamelCase = last_checkpoint UpperCamelCase = trainer.train(resume_from_checkpoint=_lowercase ) trainer.save_model() trainer.log_metrics('train' , train_result.metrics ) trainer.save_metrics('train' , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: UpperCamelCase = trainer.evaluate() trainer.log_metrics('eval' , _lowercase ) trainer.save_metrics('eval' , _lowercase ) # Write model card and (optionally) push to hub UpperCamelCase = { 'tasks': 'masked-auto-encoding', 'dataset': data_args.dataset_name, 'tags': ['masked-auto-encoding'], } if training_args.push_to_hub: trainer.push_to_hub(**_lowercase ) else: trainer.create_model_card(**_lowercase ) def __lowerCamelCase ( _lowercase ) -> int: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
282
0
import argparse import json from dataclasses import dataclass, field from functools import partial from pathlib import Path from typing import Callable, Dict, List, Tuple import timm import torch import torch.nn as nn from classy_vision.models.regnet import RegNet, RegNetParams, RegNetYaagf, RegNetYaagf, RegNetYaaagf from huggingface_hub import cached_download, hf_hub_url from torch import Tensor from vissl.models.model_helpers import get_trunk_forward_outputs from transformers import AutoImageProcessor, RegNetConfig, RegNetForImageClassification, RegNetModel from transformers.utils import logging logging.set_verbosity_info() a_ = logging.get_logger() @dataclass class UpperCAmelCase__ : """simple docstring""" lowerCAmelCase__ : nn.Module lowerCAmelCase__ : List[nn.Module] = field(default_factory=snake_case ) lowerCAmelCase__ : list = field(default_factory=snake_case ) def _UpperCAmelCase ( self: Any , __lowerCAmelCase: List[Any] , __lowerCAmelCase: Tensor , __lowerCAmelCase: Tensor ) -> List[str]: '''simple docstring''' __UpperCAmelCase = len(list(m.modules() ) ) == 1 or isinstance(__lowerCAmelCase , nn.Convad ) or isinstance(__lowerCAmelCase , nn.BatchNormad ) if has_not_submodules: self.traced.append(__lowerCAmelCase ) def __call__( self: Dict , __lowerCAmelCase: Tensor ) -> Any: '''simple docstring''' for m in self.module.modules(): self.handles.append(m.register_forward_hook(self._forward_hook ) ) self.module(__lowerCAmelCase ) [x.remove() for x in self.handles] return self @property def _UpperCAmelCase ( self: List[Any] ) -> int: '''simple docstring''' return list(filter(lambda __lowerCAmelCase : len(list(x.state_dict().keys() ) ) > 0 , self.traced ) ) @dataclass class UpperCAmelCase__ : """simple docstring""" lowerCAmelCase__ : nn.Module lowerCAmelCase__ : nn.Module lowerCAmelCase__ : int = 1 lowerCAmelCase__ : List = field(default_factory=snake_case ) lowerCAmelCase__ : List = field(default_factory=snake_case ) lowerCAmelCase__ : bool = True def __call__( self: Union[str, Any] , __lowerCAmelCase: Tensor ) -> int: '''simple docstring''' __UpperCAmelCase = Tracker(self.dest )(__lowerCAmelCase ).parametrized __UpperCAmelCase = Tracker(self.src )(__lowerCAmelCase ).parametrized __UpperCAmelCase = list(filter(lambda __lowerCAmelCase : type(__lowerCAmelCase ) not in self.src_skip , __lowerCAmelCase ) ) __UpperCAmelCase = list(filter(lambda __lowerCAmelCase : type(__lowerCAmelCase ) not in self.dest_skip , __lowerCAmelCase ) ) if len(__lowerCAmelCase ) != len(__lowerCAmelCase ) and self.raise_if_mismatch: raise Exception( F'''Numbers of operations are different. Source module has {len(__lowerCAmelCase )} operations while''' F''' destination module has {len(__lowerCAmelCase )}.''' ) for dest_m, src_m in zip(__lowerCAmelCase , __lowerCAmelCase ): dest_m.load_state_dict(src_m.state_dict() ) if self.verbose == 1: print(F'''Transfered from={src_m} to={dest_m}''' ) class UpperCAmelCase__ ( nn.Module ): """simple docstring""" def __init__( self: List[str] , __lowerCAmelCase: nn.Module ) -> Any: '''simple docstring''' super().__init__() __UpperCAmelCase = [] # - get the stem feature_blocks.append(("conv1", model.stem) ) # - get all the feature blocks for k, v in model.trunk_output.named_children(): assert k.startswith("block" ), F'''Unexpected layer name {k}''' __UpperCAmelCase = len(__lowerCAmelCase ) + 1 feature_blocks.append((F'''res{block_index}''', v) ) __UpperCAmelCase = nn.ModuleDict(__lowerCAmelCase ) def _UpperCAmelCase ( self: Optional[Any] , __lowerCAmelCase: Tensor ) -> List[Any]: '''simple docstring''' return get_trunk_forward_outputs( __lowerCAmelCase , out_feat_keys=__lowerCAmelCase , feature_blocks=self._feature_blocks , ) class UpperCAmelCase__ ( snake_case ): """simple docstring""" def _UpperCAmelCase ( self: Any , __lowerCAmelCase: str ) -> str: '''simple docstring''' __UpperCAmelCase = x.split("-" ) return x_split[0] + x_split[1] + "_" + "".join(x_split[2:] ) def __getitem__( self: Any , __lowerCAmelCase: str ) -> Callable[[], Tuple[nn.Module, Dict]]: '''simple docstring''' if x not in self: __UpperCAmelCase = self.convert_name_to_timm(__lowerCAmelCase ) __UpperCAmelCase = partial(lambda: (timm.create_model(__lowerCAmelCase , pretrained=__lowerCAmelCase ).eval(), None) ) else: __UpperCAmelCase = super().__getitem__(__lowerCAmelCase ) return val class UpperCAmelCase__ ( snake_case ): """simple docstring""" def __getitem__( self: Dict , __lowerCAmelCase: str ) -> Callable[[], nn.Module]: '''simple docstring''' if "seer" in x and "in1k" not in x: __UpperCAmelCase = RegNetModel else: __UpperCAmelCase = RegNetForImageClassification return val def __lowerCAmelCase ( A_ : Union[str, Any] , A_ : Dict , A_ : List[Tuple[str, str]] ) -> int: for from_key, to_key in keys: __UpperCAmelCase = from_state_dict[from_key].clone() print(F'''Copied key={from_key} to={to_key}''' ) return to_state_dict def __lowerCAmelCase ( A_ : str , A_ : Callable[[], nn.Module] , A_ : Callable[[], nn.Module] , A_ : RegNetConfig , A_ : Path , A_ : bool = True , ) -> Tuple: print(F'''Converting {name}...''' ) with torch.no_grad(): __UpperCAmelCase , __UpperCAmelCase = from_model_func() __UpperCAmelCase = our_model_func(A_ ).eval() __UpperCAmelCase = ModuleTransfer(src=A_ , dest=A_ , raise_if_mismatch=A_ ) __UpperCAmelCase = torch.randn((1, 3, 2_24, 2_24) ) module_transfer(A_ ) if from_state_dict is not None: __UpperCAmelCase = [] # for seer - in1k finetuned we have to manually copy the head if "seer" in name and "in1k" in name: __UpperCAmelCase = [("0.clf.0.weight", "classifier.1.weight"), ("0.clf.0.bias", "classifier.1.bias")] __UpperCAmelCase = manually_copy_vissl_head(A_ , our_model.state_dict() , A_ ) our_model.load_state_dict(A_ ) __UpperCAmelCase = our_model(A_ , output_hidden_states=A_ ) __UpperCAmelCase = ( our_outputs.logits if isinstance(A_ , A_ ) else our_outputs.last_hidden_state ) __UpperCAmelCase = from_model(A_ ) __UpperCAmelCase = from_output[-1] if type(A_ ) is list else from_output # now since I don't want to use any config files, vissl seer model doesn't actually have an head, so let's just check the last hidden state if "seer" in name and "in1k" in name: __UpperCAmelCase = our_outputs.hidden_states[-1] assert torch.allclose(A_ , A_ ), "The model logits don't match the original one." if push_to_hub: our_model.push_to_hub( repo_path_or_name=save_directory / name , commit_message="Add model" , use_temp_dir=A_ , ) __UpperCAmelCase = 2_24 if "seer" not in name else 3_84 # we can use the convnext one __UpperCAmelCase = AutoImageProcessor.from_pretrained("facebook/convnext-base-224-22k-1k" , size=A_ ) image_processor.push_to_hub( repo_path_or_name=save_directory / name , commit_message="Add image processor" , use_temp_dir=A_ , ) print(F'''Pushed {name}''' ) def __lowerCAmelCase ( A_ : Path , A_ : str = None , A_ : bool = True ) -> Union[str, Any]: __UpperCAmelCase = "imagenet-1k-id2label.json" __UpperCAmelCase = 10_00 __UpperCAmelCase = (1, num_labels) __UpperCAmelCase = "huggingface/label-files" __UpperCAmelCase = num_labels __UpperCAmelCase = json.load(open(cached_download(hf_hub_url(A_ , A_ , repo_type="dataset" ) ) , "r" ) ) __UpperCAmelCase = {int(A_ ): v for k, v in idalabel.items()} __UpperCAmelCase = idalabel __UpperCAmelCase = {v: k for k, v in idalabel.items()} __UpperCAmelCase = partial(A_ , num_labels=A_ , idalabel=A_ , labelaid=A_ ) __UpperCAmelCase = { "regnet-x-002": ImageNetPreTrainedConfig( depths=[1, 1, 4, 7] , hidden_sizes=[24, 56, 1_52, 3_68] , groups_width=8 , layer_type="x" ), "regnet-x-004": ImageNetPreTrainedConfig( depths=[1, 2, 7, 12] , hidden_sizes=[32, 64, 1_60, 3_84] , groups_width=16 , layer_type="x" ), "regnet-x-006": ImageNetPreTrainedConfig( depths=[1, 3, 5, 7] , hidden_sizes=[48, 96, 2_40, 5_28] , groups_width=24 , layer_type="x" ), "regnet-x-008": ImageNetPreTrainedConfig( depths=[1, 3, 7, 5] , hidden_sizes=[64, 1_28, 2_88, 6_72] , groups_width=16 , layer_type="x" ), "regnet-x-016": ImageNetPreTrainedConfig( depths=[2, 4, 10, 2] , hidden_sizes=[72, 1_68, 4_08, 9_12] , groups_width=24 , layer_type="x" ), "regnet-x-032": ImageNetPreTrainedConfig( depths=[2, 6, 15, 2] , hidden_sizes=[96, 1_92, 4_32, 10_08] , groups_width=48 , layer_type="x" ), "regnet-x-040": ImageNetPreTrainedConfig( depths=[2, 5, 14, 2] , hidden_sizes=[80, 2_40, 5_60, 13_60] , groups_width=40 , layer_type="x" ), "regnet-x-064": ImageNetPreTrainedConfig( depths=[2, 4, 10, 1] , hidden_sizes=[1_68, 3_92, 7_84, 16_24] , groups_width=56 , layer_type="x" ), "regnet-x-080": ImageNetPreTrainedConfig( depths=[2, 5, 15, 1] , hidden_sizes=[80, 2_40, 7_20, 19_20] , groups_width=1_20 , layer_type="x" ), "regnet-x-120": ImageNetPreTrainedConfig( depths=[2, 5, 11, 1] , hidden_sizes=[2_24, 4_48, 8_96, 22_40] , groups_width=1_12 , layer_type="x" ), "regnet-x-160": ImageNetPreTrainedConfig( depths=[2, 6, 13, 1] , hidden_sizes=[2_56, 5_12, 8_96, 20_48] , groups_width=1_28 , layer_type="x" ), "regnet-x-320": ImageNetPreTrainedConfig( depths=[2, 7, 13, 1] , hidden_sizes=[3_36, 6_72, 13_44, 25_20] , groups_width=1_68 , layer_type="x" ), # y variant "regnet-y-002": ImageNetPreTrainedConfig(depths=[1, 1, 4, 7] , hidden_sizes=[24, 56, 1_52, 3_68] , groups_width=8 ), "regnet-y-004": ImageNetPreTrainedConfig( depths=[1, 3, 6, 6] , hidden_sizes=[48, 1_04, 2_08, 4_40] , groups_width=8 ), "regnet-y-006": ImageNetPreTrainedConfig( depths=[1, 3, 7, 4] , hidden_sizes=[48, 1_12, 2_56, 6_08] , groups_width=16 ), "regnet-y-008": ImageNetPreTrainedConfig( depths=[1, 3, 8, 2] , hidden_sizes=[64, 1_28, 3_20, 7_68] , groups_width=16 ), "regnet-y-016": ImageNetPreTrainedConfig( depths=[2, 6, 17, 2] , hidden_sizes=[48, 1_20, 3_36, 8_88] , groups_width=24 ), "regnet-y-032": ImageNetPreTrainedConfig( depths=[2, 5, 13, 1] , hidden_sizes=[72, 2_16, 5_76, 15_12] , groups_width=24 ), "regnet-y-040": ImageNetPreTrainedConfig( depths=[2, 6, 12, 2] , hidden_sizes=[1_28, 1_92, 5_12, 10_88] , groups_width=64 ), "regnet-y-064": ImageNetPreTrainedConfig( depths=[2, 7, 14, 2] , hidden_sizes=[1_44, 2_88, 5_76, 12_96] , groups_width=72 ), "regnet-y-080": ImageNetPreTrainedConfig( depths=[2, 4, 10, 1] , hidden_sizes=[1_68, 4_48, 8_96, 20_16] , groups_width=56 ), "regnet-y-120": ImageNetPreTrainedConfig( depths=[2, 5, 11, 1] , hidden_sizes=[2_24, 4_48, 8_96, 22_40] , groups_width=1_12 ), "regnet-y-160": ImageNetPreTrainedConfig( depths=[2, 4, 11, 1] , hidden_sizes=[2_24, 4_48, 12_32, 30_24] , groups_width=1_12 ), "regnet-y-320": ImageNetPreTrainedConfig( depths=[2, 5, 12, 1] , hidden_sizes=[2_32, 6_96, 13_92, 37_12] , groups_width=2_32 ), # models created by SEER -> https://arxiv.org/abs/2202.08360 "regnet-y-320-seer": RegNetConfig(depths=[2, 5, 12, 1] , hidden_sizes=[2_32, 6_96, 13_92, 37_12] , groups_width=2_32 ), "regnet-y-640-seer": RegNetConfig(depths=[2, 5, 12, 1] , hidden_sizes=[3_28, 9_84, 19_68, 49_20] , groups_width=3_28 ), "regnet-y-1280-seer": RegNetConfig( depths=[2, 7, 17, 1] , hidden_sizes=[5_28, 10_56, 29_04, 73_92] , groups_width=2_64 ), "regnet-y-2560-seer": RegNetConfig( depths=[3, 7, 16, 1] , hidden_sizes=[6_40, 16_96, 25_44, 50_88] , groups_width=6_40 ), "regnet-y-10b-seer": ImageNetPreTrainedConfig( depths=[2, 7, 17, 1] , hidden_sizes=[20_20, 40_40, 1_11_10, 2_82_80] , groups_width=10_10 ), # finetuned on imagenet "regnet-y-320-seer-in1k": ImageNetPreTrainedConfig( depths=[2, 5, 12, 1] , hidden_sizes=[2_32, 6_96, 13_92, 37_12] , groups_width=2_32 ), "regnet-y-640-seer-in1k": ImageNetPreTrainedConfig( depths=[2, 5, 12, 1] , hidden_sizes=[3_28, 9_84, 19_68, 49_20] , groups_width=3_28 ), "regnet-y-1280-seer-in1k": ImageNetPreTrainedConfig( depths=[2, 7, 17, 1] , hidden_sizes=[5_28, 10_56, 29_04, 73_92] , groups_width=2_64 ), "regnet-y-2560-seer-in1k": ImageNetPreTrainedConfig( depths=[3, 7, 16, 1] , hidden_sizes=[6_40, 16_96, 25_44, 50_88] , groups_width=6_40 ), "regnet-y-10b-seer-in1k": ImageNetPreTrainedConfig( depths=[2, 7, 17, 1] , hidden_sizes=[20_20, 40_40, 1_11_10, 2_82_80] , groups_width=10_10 ), } __UpperCAmelCase = NameToOurModelFuncMap() __UpperCAmelCase = NameToFromModelFuncMap() # add seer weights logic def load_using_classy_vision(A_ : str , A_ : Callable[[], nn.Module] ) -> Tuple[nn.Module, Dict]: __UpperCAmelCase = torch.hub.load_state_dict_from_url(A_ , model_dir=str(A_ ) , map_location="cpu" ) __UpperCAmelCase = model_func() # check if we have a head, if yes add it __UpperCAmelCase = files["classy_state_dict"]["base_model"]["model"] __UpperCAmelCase = model_state_dict["trunk"] model.load_state_dict(A_ ) return model.eval(), model_state_dict["heads"] # pretrained __UpperCAmelCase = partial( A_ , "https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet32d/seer_regnet32gf_model_iteration244000.torch" , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) __UpperCAmelCase = partial( A_ , "https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet64/seer_regnet64gf_model_final_checkpoint_phase0.torch" , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) __UpperCAmelCase = partial( A_ , "https://dl.fbaipublicfiles.com/vissl/model_zoo/swav_ig1b_regnet128Gf_cnstant_bs32_node16_sinkhorn10_proto16k_syncBN64_warmup8k/model_final_checkpoint_phase0.torch" , lambda: FakeRegNetVisslWrapper(RegNetYaaagf() ) , ) __UpperCAmelCase = partial( A_ , "https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet10B/model_iteration124500_conso.torch" , lambda: FakeRegNetVisslWrapper( RegNet(RegNetParams(depth=27 , group_width=10_10 , w_a=17_44 , w_a=6_20.83 , w_m=2.52 ) ) ) , ) # IN1K finetuned __UpperCAmelCase = partial( A_ , "https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet32_finetuned_in1k_model_final_checkpoint_phase78.torch" , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) __UpperCAmelCase = partial( A_ , "https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet64_finetuned_in1k_model_final_checkpoint_phase78.torch" , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) __UpperCAmelCase = partial( A_ , "https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet128_finetuned_in1k_model_final_checkpoint_phase78.torch" , lambda: FakeRegNetVisslWrapper(RegNetYaaagf() ) , ) __UpperCAmelCase = partial( A_ , "https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_10b_finetuned_in1k_model_phase28_conso.torch" , lambda: FakeRegNetVisslWrapper( RegNet(RegNetParams(depth=27 , group_width=10_10 , w_a=17_44 , w_a=6_20.83 , w_m=2.52 ) ) ) , ) if model_name: convert_weight_and_push( A_ , names_to_from_model_map[model_name] , names_to_ours_model_map[model_name] , names_to_config[model_name] , A_ , A_ , ) else: for model_name, config in names_to_config.items(): convert_weight_and_push( A_ , names_to_from_model_map[model_name] , names_to_ours_model_map[model_name] , A_ , A_ , A_ , ) return config, expected_shape if __name__ == "__main__": a_ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default=None, type=str, help=( """The name of the model you wish to convert, it must be one of the supported regnet* architecture,""" """ currently: regnetx-*, regnety-*. If `None`, all of them will the converted.""" ), ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=Path, required=True, help="""Path to the output PyTorch model directory.""", ) parser.add_argument( """--push_to_hub""", default=True, type=bool, required=False, help="""If True, push model and image processor to the hub.""", ) a_ = parser.parse_args() a_ = args.pytorch_dump_folder_path pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True) convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)
714
from ...configuration_utils import PretrainedConfig from ...utils import logging a_ = logging.get_logger(__name__) a_ = { """facebook/vit-mae-base""": """https://huggingface.co/facebook/vit-mae-base/resolve/main/config.json""", # See all ViT MAE models at https://huggingface.co/models?filter=vit-mae } class UpperCAmelCase__ ( snake_case ): """simple docstring""" lowerCAmelCase__ : Dict = 'vit_mae' def __init__( self: List[Any] , __lowerCAmelCase: Any=768 , __lowerCAmelCase: List[str]=12 , __lowerCAmelCase: Optional[int]=12 , __lowerCAmelCase: Tuple=3_072 , __lowerCAmelCase: List[Any]="gelu" , __lowerCAmelCase: Dict=0.0 , __lowerCAmelCase: Tuple=0.0 , __lowerCAmelCase: Any=0.02 , __lowerCAmelCase: List[Any]=1E-12 , __lowerCAmelCase: List[str]=224 , __lowerCAmelCase: Optional[Any]=16 , __lowerCAmelCase: Union[str, Any]=3 , __lowerCAmelCase: Tuple=True , __lowerCAmelCase: Union[str, Any]=16 , __lowerCAmelCase: Optional[int]=512 , __lowerCAmelCase: int=8 , __lowerCAmelCase: int=2_048 , __lowerCAmelCase: str=0.75 , __lowerCAmelCase: Union[str, Any]=False , **__lowerCAmelCase: List[Any] , ) -> Optional[int]: '''simple docstring''' super().__init__(**__lowerCAmelCase ) __UpperCAmelCase = hidden_size __UpperCAmelCase = num_hidden_layers __UpperCAmelCase = num_attention_heads __UpperCAmelCase = intermediate_size __UpperCAmelCase = hidden_act __UpperCAmelCase = hidden_dropout_prob __UpperCAmelCase = attention_probs_dropout_prob __UpperCAmelCase = initializer_range __UpperCAmelCase = layer_norm_eps __UpperCAmelCase = image_size __UpperCAmelCase = patch_size __UpperCAmelCase = num_channels __UpperCAmelCase = qkv_bias __UpperCAmelCase = decoder_num_attention_heads __UpperCAmelCase = decoder_hidden_size __UpperCAmelCase = decoder_num_hidden_layers __UpperCAmelCase = decoder_intermediate_size __UpperCAmelCase = mask_ratio __UpperCAmelCase = norm_pix_loss
286
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices _a = logging.get_logger(__name__) _a = { """shi-labs/nat-mini-in1k-224""": """https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json""", # See all Nat models at https://huggingface.co/models?filter=nat } class _UpperCAmelCase( lowerCamelCase , lowerCamelCase ): lowercase__ = 'nat' lowercase__ = { 'num_attention_heads': 'num_heads', 'num_hidden_layers': 'num_layers', } def __init__( self , __a=4 , __a=3 , __a=64 , __a=[3, 4, 6, 5] , __a=[2, 4, 8, 16] , __a=7 , __a=3.0 , __a=True , __a=0.0 , __a=0.0 , __a=0.1 , __a="gelu" , __a=0.02 , __a=1e-5 , __a=0.0 , __a=None , __a=None , **__a , ) -> Tuple: '''simple docstring''' super().__init__(**__a) _UpperCamelCase = patch_size _UpperCamelCase = num_channels _UpperCamelCase = embed_dim _UpperCamelCase = depths _UpperCamelCase = len(__a) _UpperCamelCase = num_heads _UpperCamelCase = kernel_size _UpperCamelCase = mlp_ratio _UpperCamelCase = qkv_bias _UpperCamelCase = hidden_dropout_prob _UpperCamelCase = attention_probs_dropout_prob _UpperCamelCase = drop_path_rate _UpperCamelCase = hidden_act _UpperCamelCase = layer_norm_eps _UpperCamelCase = initializer_range # we set the hidden_size attribute in order to make Nat work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model _UpperCamelCase = int(embed_dim * 2 ** (len(__a) - 1)) _UpperCamelCase = layer_scale_init_value _UpperCamelCase = ['''stem'''] + [F'''stage{idx}''' for idx in range(1 , len(__a) + 1)] _UpperCamelCase , _UpperCamelCase = get_aligned_output_features_output_indices( out_features=__a , out_indices=__a , stage_names=self.stage_names)
19
from typing import List, Optional, Union import numpy as np import PIL import torch from PIL import Image from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) __a = logging.get_logger(__name__) # pylint: disable=invalid-name __a = ''' Examples: ```py >>> from diffusers import KandinskyV22Img2ImgPipeline, KandinskyV22PriorPipeline >>> from diffusers.utils import load_image >>> import torch >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained( ... "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16 ... ) >>> pipe_prior.to("cuda") >>> prompt = "A red cartoon frog, 4k" >>> image_emb, zero_image_emb = pipe_prior(prompt, return_dict=False) >>> pipe = KandinskyV22Img2ImgPipeline.from_pretrained( ... "kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16 ... ) >>> pipe.to("cuda") >>> init_image = load_image( ... "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" ... "/kandinsky/frog.png" ... ) >>> image = pipe( ... image=init_image, ... image_embeds=image_emb, ... negative_image_embeds=zero_image_emb, ... height=768, ... width=768, ... num_inference_steps=100, ... strength=0.2, ... ).images >>> image[0].save("red_frog.png") ``` ''' def __lowercase ( _UpperCamelCase, _UpperCamelCase, _UpperCamelCase=8 ) ->List[str]: """simple docstring""" lowercase : Tuple = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 lowercase : Optional[Any] = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor def __lowercase ( _UpperCamelCase, _UpperCamelCase=512, _UpperCamelCase=512 ) ->Optional[Any]: """simple docstring""" lowercase : Union[str, Any] = pil_image.resize((w, h), resample=Image.BICUBIC, reducing_gap=1 ) lowercase : List[Any] = np.array(pil_image.convert('''RGB''' ) ) lowercase : Optional[Any] = arr.astype(np.floataa ) / 1_2_7.5 - 1 lowercase : Tuple = np.transpose(_UpperCamelCase, [2, 0, 1] ) lowercase : Optional[Any] = torch.from_numpy(_UpperCamelCase ).unsqueeze(0 ) return image class __SCREAMING_SNAKE_CASE ( A__ ): def __init__( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ): super().__init__() self.register_modules( unet=SCREAMING_SNAKE_CASE__ , scheduler=SCREAMING_SNAKE_CASE__ , movq=SCREAMING_SNAKE_CASE__ , ) lowercase : Optional[int] = 2 ** (len(self.movq.config.block_out_channels ) - 1) def __lowerCamelCase ( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): # get the original timestep using init_timestep lowercase : Optional[Any] = min(int(num_inference_steps * strength ) , SCREAMING_SNAKE_CASE__ ) lowercase : List[Any] = max(num_inference_steps - init_timestep , 0 ) lowercase : Any = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def __lowerCamelCase ( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=None ): if not isinstance(SCREAMING_SNAKE_CASE__ , (torch.Tensor, PIL.Image.Image, list) ): raise ValueError( f"""`image` has to be of type `torch.Tensor`, `PIL.Image.Image` or list but is {type(SCREAMING_SNAKE_CASE__ )}""" ) lowercase : Tuple = image.to(device=SCREAMING_SNAKE_CASE__ , dtype=SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = batch_size * num_images_per_prompt if image.shape[1] == 4: lowercase : int = image else: if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and len(SCREAMING_SNAKE_CASE__ ) != batch_size: raise ValueError( f"""You have passed a list of generators of length {len(SCREAMING_SNAKE_CASE__ )}, but requested an effective batch""" f""" size of {batch_size}. Make sure the batch size matches the length of the generators.""" ) elif isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): lowercase : List[Any] = [ self.movq.encode(image[i : i + 1] ).latent_dist.sample(generator[i] ) for i in range(SCREAMING_SNAKE_CASE__ ) ] lowercase : Dict = torch.cat(SCREAMING_SNAKE_CASE__ , dim=0 ) else: lowercase : int = self.movq.encode(SCREAMING_SNAKE_CASE__ ).latent_dist.sample(SCREAMING_SNAKE_CASE__ ) lowercase : Optional[Any] = self.movq.config.scaling_factor * init_latents lowercase : Dict = torch.cat([init_latents] , dim=0 ) lowercase : List[str] = init_latents.shape lowercase : List[str] = randn_tensor(SCREAMING_SNAKE_CASE__ , generator=SCREAMING_SNAKE_CASE__ , device=SCREAMING_SNAKE_CASE__ , dtype=SCREAMING_SNAKE_CASE__ ) # get latents lowercase : Optional[int] = self.scheduler.add_noise(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = init_latents return latents def __lowerCamelCase ( self , SCREAMING_SNAKE_CASE__=0 ): if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('''Please install accelerate via `pip install accelerate`''' ) lowercase : Optional[int] = torch.device(f"""cuda:{gpu_id}""" ) lowercase : List[Any] = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def __lowerCamelCase ( self , SCREAMING_SNAKE_CASE__=0 ): if is_accelerate_available() and is_accelerate_version('''>=''' , '''0.17.0.dev0''' ): from accelerate import cpu_offload_with_hook else: raise ImportError('''`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.''' ) lowercase : Optional[Any] = torch.device(f"""cuda:{gpu_id}""" ) if self.device.type != "cpu": self.to('''cpu''' , silence_dtype_warnings=SCREAMING_SNAKE_CASE__ ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) lowercase : Any = None for cpu_offloaded_model in [self.unet, self.movq]: lowercase , lowercase : Union[str, Any] = cpu_offload_with_hook(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , prev_module_hook=SCREAMING_SNAKE_CASE__ ) # We'll offload the last model manually. lowercase : Optional[Any] = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def __lowerCamelCase ( self ): if not hasattr(self.unet , '''_hf_hook''' ): return self.device for module in self.unet.modules(): if ( hasattr(SCREAMING_SNAKE_CASE__ , '''_hf_hook''' ) and hasattr(module._hf_hook , '''execution_device''' ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(SCREAMING_SNAKE_CASE__ ) def __call__( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = 512 , SCREAMING_SNAKE_CASE__ = 512 , SCREAMING_SNAKE_CASE__ = 100 , SCREAMING_SNAKE_CASE__ = 4.0 , SCREAMING_SNAKE_CASE__ = 0.3 , SCREAMING_SNAKE_CASE__ = 1 , SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__ = "pil" , SCREAMING_SNAKE_CASE__ = True , ): lowercase : Tuple = self._execution_device lowercase : Any = guidance_scale > 1.0 if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): lowercase : int = torch.cat(SCREAMING_SNAKE_CASE__ , dim=0 ) lowercase : List[Any] = image_embeds.shape[0] if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): lowercase : str = torch.cat(SCREAMING_SNAKE_CASE__ , dim=0 ) if do_classifier_free_guidance: lowercase : Optional[Any] = image_embeds.repeat_interleave(SCREAMING_SNAKE_CASE__ , dim=0 ) lowercase : List[str] = negative_image_embeds.repeat_interleave(SCREAMING_SNAKE_CASE__ , dim=0 ) lowercase : Union[str, Any] = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to(dtype=self.unet.dtype , device=SCREAMING_SNAKE_CASE__ ) if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): lowercase : Dict = [image] if not all(isinstance(SCREAMING_SNAKE_CASE__ , (PIL.Image.Image, torch.Tensor) ) for i in image ): raise ValueError( f"""Input is in incorrect format: {[type(SCREAMING_SNAKE_CASE__ ) for i in image]}. Currently, we only support PIL image and pytorch tensor""" ) lowercase : int = torch.cat([prepare_image(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) for i in image] , dim=0 ) lowercase : Any = image.to(dtype=image_embeds.dtype , device=SCREAMING_SNAKE_CASE__ ) lowercase : Optional[int] = self.movq.encode(SCREAMING_SNAKE_CASE__ )['''latents'''] lowercase : Optional[Any] = latents.repeat_interleave(SCREAMING_SNAKE_CASE__ , dim=0 ) self.scheduler.set_timesteps(SCREAMING_SNAKE_CASE__ , device=SCREAMING_SNAKE_CASE__ ) lowercase , lowercase : Any = self.get_timesteps(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Dict = timesteps[:1].repeat(batch_size * num_images_per_prompt ) lowercase , lowercase : Optional[int] = downscale_height_and_width(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , self.movq_scale_factor ) lowercase : Any = self.prepare_latents( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , image_embeds.dtype , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) for i, t in enumerate(self.progress_bar(SCREAMING_SNAKE_CASE__ ) ): # expand the latents if we are doing classifier free guidance lowercase : str = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents lowercase : Dict = {'''image_embeds''': image_embeds} lowercase : List[Any] = self.unet( sample=SCREAMING_SNAKE_CASE__ , timestep=SCREAMING_SNAKE_CASE__ , encoder_hidden_states=SCREAMING_SNAKE_CASE__ , added_cond_kwargs=SCREAMING_SNAKE_CASE__ , return_dict=SCREAMING_SNAKE_CASE__ , )[0] if do_classifier_free_guidance: lowercase , lowercase : Optional[Any] = noise_pred.split(latents.shape[1] , dim=1 ) lowercase , lowercase : str = noise_pred.chunk(2 ) lowercase , lowercase : Dict = variance_pred.chunk(2 ) lowercase : Optional[Any] = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) lowercase : List[Any] = torch.cat([noise_pred, variance_pred_text] , dim=1 ) if not ( hasattr(self.scheduler.config , '''variance_type''' ) and self.scheduler.config.variance_type in ["learned", "learned_range"] ): lowercase , lowercase : Dict = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 lowercase : Optional[int] = self.scheduler.step( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , generator=SCREAMING_SNAKE_CASE__ , )[0] # post-processing lowercase : Dict = self.movq.decode(SCREAMING_SNAKE_CASE__ , force_not_quantize=SCREAMING_SNAKE_CASE__ )['''sample'''] if output_type not in ["pt", "np", "pil"]: raise ValueError(f"""Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}""" ) if output_type in ["np", "pil"]: lowercase : List[str] = image * 0.5 + 0.5 lowercase : Any = image.clamp(0 , 1 ) lowercase : List[str] = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": lowercase : List[Any] = self.numpy_to_pil(SCREAMING_SNAKE_CASE__ ) if not return_dict: return (image,) return ImagePipelineOutput(images=SCREAMING_SNAKE_CASE__ )
319
0
'''simple docstring''' import inspect import os import unittest from dataclasses import dataclass import torch from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs from accelerate.state import AcceleratorState from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu from accelerate.utils import KwargsHandler @dataclass class SCREAMING_SNAKE_CASE ( __lowercase): """simple docstring""" lowercase : int = 0 lowercase : bool = False lowercase : float = 3.0 class SCREAMING_SNAKE_CASE ( unittest.TestCase): """simple docstring""" def UpperCamelCase__ ( self ) -> Optional[Any]: # If no defaults are changed, `to_kwargs` returns an empty dict. self.assertDictEqual(MockClass().to_kwargs() , {} ) self.assertDictEqual(MockClass(a=2 ).to_kwargs() , {'a': 2} ) self.assertDictEqual(MockClass(a=2 , b=__A ).to_kwargs() , {'a': 2, 'b': True} ) self.assertDictEqual(MockClass(a=2 , c=2.25 ).to_kwargs() , {'a': 2, 'c': 2.25} ) @require_cuda def UpperCamelCase__ ( self ) -> int: # If no defaults are changed, `to_kwargs` returns an empty dict. _lowerCAmelCase =GradScalerKwargs(init_scale=1024 , growth_factor=2 ) AcceleratorState._reset_state() _lowerCAmelCase =Accelerator(mixed_precision='fp16' , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) _lowerCAmelCase =accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 1024.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2000 ) self.assertEqual(scaler._enabled , __A ) @require_multi_gpu def UpperCamelCase__ ( self ) -> List[str]: _lowerCAmelCase =['torchrun', F'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] execute_subprocess_async(__A , env=os.environ.copy() ) if __name__ == "__main__": lowercase_ = DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True) lowercase_ = Accelerator(kwargs_handlers=[ddp_scaler]) lowercase_ = torch.nn.Linear(100, 200) lowercase_ = accelerator.prepare(model) # Check the values changed in kwargs lowercase_ = '''''' lowercase_ = model.bucket_bytes_cap // (1024 * 1024) if observed_bucket_cap_map != 15: error_msg += F"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n" if model.find_unused_parameters is not True: error_msg += F"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n" # Check the values of the defaults if model.dim != 0: error_msg += F"Default value not respected, should have `0` but found {model.dim}.\n" if model.broadcast_buffers is not True: error_msg += F"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n" if model.gradient_as_bucket_view is not False: error_msg += F"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n" # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
718
'''simple docstring''' lowercase_ = ''' # Installazione di Transformers ! pip install transformers datasets # Per installare dalla fonte invece dell\'ultima versione rilasciata, commenta il comando sopra e # rimuovi la modalità commento al comando seguente. # ! pip install git+https://github.com/huggingface/transformers.git ''' lowercase_ = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] lowercase_ = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
58
0
from __future__ import annotations def a_ ( lowerCAmelCase_ : list[int], lowerCAmelCase_ : int, lowerCAmelCase_ : int, lowerCAmelCase_ : int ): if (direction == 1 and array[indexa] > array[indexa]) or ( direction == 0 and array[indexa] < array[indexa] ): __lowerCAmelCase , __lowerCAmelCase = array[indexa], array[indexa] def a_ ( lowerCAmelCase_ : list[int], lowerCAmelCase_ : int, lowerCAmelCase_ : int, lowerCAmelCase_ : int ): if length > 1: __lowerCAmelCase = int(length / 2 ) for i in range(lowerCAmelCase_, low + middle ): comp_and_swap(lowerCAmelCase_, lowerCAmelCase_, i + middle, lowerCAmelCase_ ) bitonic_merge(lowerCAmelCase_, lowerCAmelCase_, lowerCAmelCase_, lowerCAmelCase_ ) bitonic_merge(lowerCAmelCase_, low + middle, lowerCAmelCase_, lowerCAmelCase_ ) def a_ ( lowerCAmelCase_ : list[int], lowerCAmelCase_ : int, lowerCAmelCase_ : int, lowerCAmelCase_ : int ): if length > 1: __lowerCAmelCase = int(length / 2 ) bitonic_sort(lowerCAmelCase_, lowerCAmelCase_, lowerCAmelCase_, 1 ) bitonic_sort(lowerCAmelCase_, low + middle, lowerCAmelCase_, 0 ) bitonic_merge(lowerCAmelCase_, lowerCAmelCase_, lowerCAmelCase_, lowerCAmelCase_ ) if __name__ == "__main__": _snake_case : str = input('Enter numbers separated by a comma:\n').strip() _snake_case : List[Any] = [int(item.strip()) for item in user_input.split(',')] bitonic_sort(unsorted, 0, len(unsorted), 1) print('\nSorted array in ascending order is: ', end='') print(*unsorted, sep=', ') bitonic_merge(unsorted, 0, len(unsorted), 0) print('Sorted array in descending order is: ', end='') print(*unsorted, sep=', ')
53
'''simple docstring''' from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _UpperCamelCase = logging.get_logger(__name__) _UpperCamelCase = { """microsoft/beit-base-patch16-224-pt22k""": ( """https://huggingface.co/microsoft/beit-base-patch16-224-pt22k/resolve/main/config.json""" ), # See all BEiT models at https://huggingface.co/models?filter=beit } class __magic_name__ ( lowerCAmelCase ): """simple docstring""" lowerCamelCase__ = 'beit' def __init__( self , lowerCamelCase=8192 , lowerCamelCase=768 , lowerCamelCase=12 , lowerCamelCase=12 , lowerCamelCase=3072 , lowerCamelCase="gelu" , lowerCamelCase=0.0 , lowerCamelCase=0.0 , lowerCamelCase=0.02 , lowerCamelCase=1E-12 , lowerCamelCase=224 , lowerCamelCase=16 , lowerCamelCase=3 , lowerCamelCase=False , lowerCamelCase=False , lowerCamelCase=False , lowerCamelCase=False , lowerCamelCase=0.1 , lowerCamelCase=0.1 , lowerCamelCase=True , lowerCamelCase=[3, 5, 7, 11] , lowerCamelCase=[1, 2, 3, 6] , lowerCamelCase=True , lowerCamelCase=0.4 , lowerCamelCase=256 , lowerCamelCase=1 , lowerCamelCase=False , lowerCamelCase=255 , **lowerCamelCase , ): '''simple docstring''' super().__init__(**lowerCamelCase ) __A : int = vocab_size __A : Tuple = hidden_size __A : List[str] = num_hidden_layers __A : Optional[int] = num_attention_heads __A : Dict = intermediate_size __A : Optional[int] = hidden_act __A : str = hidden_dropout_prob __A : Dict = attention_probs_dropout_prob __A : int = initializer_range __A : List[str] = layer_norm_eps __A : str = image_size __A : Optional[int] = patch_size __A : List[Any] = num_channels __A : List[str] = use_mask_token __A : Union[str, Any] = use_absolute_position_embeddings __A : Optional[int] = use_relative_position_bias __A : int = use_shared_relative_position_bias __A : int = layer_scale_init_value __A : Union[str, Any] = drop_path_rate __A : Dict = use_mean_pooling # decode head attributes (semantic segmentation) __A : List[str] = out_indices __A : str = pool_scales # auxiliary head attributes (semantic segmentation) __A : List[str] = use_auxiliary_head __A : Tuple = auxiliary_loss_weight __A : Dict = auxiliary_channels __A : Tuple = auxiliary_num_convs __A : List[str] = auxiliary_concat_input __A : int = semantic_loss_ignore_index class __magic_name__ ( lowerCAmelCase ): """simple docstring""" lowerCamelCase__ = version.parse('1.11' ) @property def lowerCAmelCase__ ( self ): '''simple docstring''' return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def lowerCAmelCase__ ( self ): '''simple docstring''' return 1E-4
111
0
'''simple docstring''' from typing import List import datasets from datasets.tasks import AudioClassification from ..folder_based_builder import folder_based_builder _A : Optional[Any] = datasets.utils.logging.get_logger(__name__) class _lowercase ( folder_based_builder.FolderBasedBuilderConfig ): '''simple docstring''' _SCREAMING_SNAKE_CASE : bool = None _SCREAMING_SNAKE_CASE : bool = None class _lowercase ( folder_based_builder.FolderBasedBuilder ): '''simple docstring''' _SCREAMING_SNAKE_CASE : List[str] = datasets.Audio() _SCREAMING_SNAKE_CASE : Any = """audio""" _SCREAMING_SNAKE_CASE : List[str] = AudioFolderConfig _SCREAMING_SNAKE_CASE : List[str] # definition at the bottom of the script _SCREAMING_SNAKE_CASE : Tuple = AudioClassification(audio_column="""audio""" , label_column="""label""" ) _A : Any = [ '''.aiff''', '''.au''', '''.avr''', '''.caf''', '''.flac''', '''.htk''', '''.svx''', '''.mat4''', '''.mat5''', '''.mpc2k''', '''.ogg''', '''.paf''', '''.pvf''', '''.raw''', '''.rf64''', '''.sd2''', '''.sds''', '''.ircam''', '''.voc''', '''.w64''', '''.wav''', '''.nist''', '''.wavex''', '''.wve''', '''.xi''', '''.mp3''', '''.opus''', ] _A : Tuple = AUDIO_EXTENSIONS
330
'''simple docstring''' from ...processing_utils import ProcessorMixin class _lowercase ( UpperCAmelCase__ ): '''simple docstring''' _SCREAMING_SNAKE_CASE : str = """WhisperFeatureExtractor""" _SCREAMING_SNAKE_CASE : Any = """WhisperTokenizer""" def __init__( self : Any , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Any ) -> Tuple: super().__init__(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) __lowerCAmelCase = self.feature_extractor __lowerCAmelCase = False def a ( self : Dict , SCREAMING_SNAKE_CASE__ : Dict=None , SCREAMING_SNAKE_CASE__ : Optional[int]=None , SCREAMING_SNAKE_CASE__ : Tuple=True ) -> List[str]: return self.tokenizer.get_decoder_prompt_ids(task=SCREAMING_SNAKE_CASE__ , language=SCREAMING_SNAKE_CASE__ , no_timestamps=SCREAMING_SNAKE_CASE__ ) def __call__( self : Any , *SCREAMING_SNAKE_CASE__ : Dict , **SCREAMING_SNAKE_CASE__ : Tuple ) -> Dict: # For backward compatibility if self._in_target_context_manager: return self.current_processor(*SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) __lowerCAmelCase = kwargs.pop("""audio""" , SCREAMING_SNAKE_CASE__ ) __lowerCAmelCase = kwargs.pop("""sampling_rate""" , SCREAMING_SNAKE_CASE__ ) __lowerCAmelCase = kwargs.pop("""text""" , SCREAMING_SNAKE_CASE__ ) if len(SCREAMING_SNAKE_CASE__ ) > 0: __lowerCAmelCase = args[0] __lowerCAmelCase = args[1:] if audio is None and text is None: raise ValueError("""You need to specify either an `audio` or `text` input to process.""" ) if audio is not None: __lowerCAmelCase = self.feature_extractor(SCREAMING_SNAKE_CASE__ , *SCREAMING_SNAKE_CASE__ , sampling_rate=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) if text is not None: __lowerCAmelCase = self.tokenizer(SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) if text is None: return inputs elif audio is None: return encodings else: __lowerCAmelCase = encodings["""input_ids"""] return inputs def a ( self : List[str] , *SCREAMING_SNAKE_CASE__ : Tuple , **SCREAMING_SNAKE_CASE__ : Union[str, Any] ) -> Optional[int]: return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) def a ( self : List[str] , *SCREAMING_SNAKE_CASE__ : int , **SCREAMING_SNAKE_CASE__ : str ) -> List[Any]: return self.tokenizer.decode(*SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) def a ( self : List[str] , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Tuple="np" ) -> List[Any]: return self.tokenizer.get_prompt_ids(SCREAMING_SNAKE_CASE__ , return_tensors=SCREAMING_SNAKE_CASE__ )
330
1
import argparse from pathlib import Path from transformers import AutoConfig, AutoTokenizer, RagConfig, RagSequenceForGeneration, RagTokenForGeneration def _snake_case (__lowercase , __lowercase , __lowercase , __lowercase , __lowercase = None , __lowercase = None , __lowercase = None , ): if config_name_or_path is None: UpperCamelCase_ = 'facebook/rag-token-base' if model_type == 'rag_token' else 'facebook/rag-sequence-base' if generator_tokenizer_name_or_path is None: UpperCamelCase_ = generator_name_or_path if question_encoder_tokenizer_name_or_path is None: UpperCamelCase_ = question_encoder_name_or_path UpperCamelCase_ = RagTokenForGeneration if model_type == 'rag_token' else RagSequenceForGeneration # Save model. UpperCamelCase_ = RagConfig.from_pretrained(__SCREAMING_SNAKE_CASE) UpperCamelCase_ = AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE) UpperCamelCase_ = AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE) UpperCamelCase_ = gen_config UpperCamelCase_ = question_encoder_config UpperCamelCase_ = model_class.from_pretrained_question_encoder_generator( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , config=__SCREAMING_SNAKE_CASE) rag_model.save_pretrained(__SCREAMING_SNAKE_CASE) # Sanity check. model_class.from_pretrained(__SCREAMING_SNAKE_CASE) # Save tokenizers. UpperCamelCase_ = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE) gen_tokenizer.save_pretrained(dest_dir / 'generator_tokenizer/') UpperCamelCase_ = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE) question_encoder_tokenizer.save_pretrained(dest_dir / 'question_encoder_tokenizer/') if __name__ == "__main__": snake_case__ : int = 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``""" ), ) snake_case__ : int = parser.parse_args() snake_case__ : Dict = 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, )
23
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available UpperCAmelCase = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase = ['''MLukeTokenizer'''] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_mluke import MLukeTokenizer else: import sys UpperCAmelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
84
0
import os def lowerCamelCase__ ( ): '''simple docstring''' snake_case_ = os.path.join(os.path.dirname(_A ) , "num.txt" ) with open(_A ) as file_hand: return str(sum(int(_A ) for line in file_hand ) )[:10] if __name__ == "__main__": print(solution())
139
from transformers import DistilBertTokenizer, DistilBertTokenizerFast from transformers.testing_utils import require_tokenizers, slow from ..bert.test_tokenization_bert import BertTokenizationTest @require_tokenizers class UpperCAmelCase ( UpperCAmelCase__ ): '''simple docstring''' lowerCAmelCase_ = DistilBertTokenizer lowerCAmelCase_ = DistilBertTokenizerFast lowerCAmelCase_ = True @slow def snake_case__ ( self : Tuple ): """simple docstring""" snake_case_ = DistilBertTokenizer.from_pretrained("distilbert-base-uncased" ) snake_case_ = tokenizer.encode("sequence builders" , add_special_tokens=__lowercase ) snake_case_ = tokenizer.encode("multi-sequence build" , add_special_tokens=__lowercase ) snake_case_ = tokenizer.build_inputs_with_special_tokens(__lowercase ) snake_case_ = tokenizer.build_inputs_with_special_tokens(__lowercase , __lowercase ) assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [ tokenizer.sep_token_id ]
139
1
from binascii import hexlify from hashlib import shaaaa from os import urandom # RFC 3526 - More Modular Exponential (MODP) Diffie-Hellman groups for # Internet Key Exchange (IKE) https://tools.ietf.org/html/rfc3526 snake_case = { # 1536-bit 5: { """prime""": int( """FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1""" + """29024E088A67CC74020BBEA63B139B22514A08798E3404DD""" + """EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245""" + """E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED""" + """EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D""" + """C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F""" + """83655D23DCA3AD961C62F356208552BB9ED529077096966D""" + """670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF""", base=1_6, ), """generator""": 2, }, # 2048-bit 1_4: { """prime""": int( """FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1""" + """29024E088A67CC74020BBEA63B139B22514A08798E3404DD""" + """EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245""" + """E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED""" + """EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D""" + """C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F""" + """83655D23DCA3AD961C62F356208552BB9ED529077096966D""" + """670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B""" + """E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9""" + """DE2BCBF6955817183995497CEA956AE515D2261898FA0510""" + """15728E5A8AACAA68FFFFFFFFFFFFFFFF""", base=1_6, ), """generator""": 2, }, # 3072-bit 1_5: { """prime""": int( """FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1""" + """29024E088A67CC74020BBEA63B139B22514A08798E3404DD""" + """EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245""" + """E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED""" + """EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D""" + """C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F""" + """83655D23DCA3AD961C62F356208552BB9ED529077096966D""" + """670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B""" + """E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9""" + """DE2BCBF6955817183995497CEA956AE515D2261898FA0510""" + """15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64""" + """ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7""" + """ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B""" + """F12FFA06D98A0864D87602733EC86A64521F2B18177B200C""" + """BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31""" + """43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF""", base=1_6, ), """generator""": 2, }, # 4096-bit 1_6: { """prime""": int( """FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1""" + """29024E088A67CC74020BBEA63B139B22514A08798E3404DD""" + """EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245""" + """E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED""" + """EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D""" + """C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F""" + """83655D23DCA3AD961C62F356208552BB9ED529077096966D""" + """670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B""" + """E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9""" + """DE2BCBF6955817183995497CEA956AE515D2261898FA0510""" + """15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64""" + """ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7""" + """ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B""" + """F12FFA06D98A0864D87602733EC86A64521F2B18177B200C""" + """BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31""" + """43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7""" + """88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA""" + """2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6""" + """287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED""" + """1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9""" + """93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199""" + """FFFFFFFFFFFFFFFF""", base=1_6, ), """generator""": 2, }, # 6144-bit 1_7: { """prime""": int( """FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08""" + """8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B""" + """302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9""" + """A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6""" + """49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8""" + """FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D""" + """670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C""" + """180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718""" + """3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D""" + """04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D""" + """B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226""" + """1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C""" + """BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC""" + """E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26""" + """99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB""" + """04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2""" + """233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127""" + """D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492""" + """36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406""" + """AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918""" + """DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151""" + """2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03""" + """F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F""" + """BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA""" + """CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B""" + """B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632""" + """387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E""" + """6DCC4024FFFFFFFFFFFFFFFF""", base=1_6, ), """generator""": 2, }, # 8192-bit 1_8: { """prime""": int( """FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1""" + """29024E088A67CC74020BBEA63B139B22514A08798E3404DD""" + """EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245""" + """E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED""" + """EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D""" + """C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F""" + """83655D23DCA3AD961C62F356208552BB9ED529077096966D""" + """670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B""" + """E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9""" + """DE2BCBF6955817183995497CEA956AE515D2261898FA0510""" + """15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64""" + """ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7""" + """ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B""" + """F12FFA06D98A0864D87602733EC86A64521F2B18177B200C""" + """BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31""" + """43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7""" + """88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA""" + """2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6""" + """287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED""" + """1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9""" + """93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492""" + """36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD""" + """F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831""" + """179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B""" + """DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF""" + """5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6""" + """D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3""" + """23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA""" + """CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328""" + """06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C""" + """DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE""" + """12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4""" + """38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300""" + """741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568""" + """3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9""" + """22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B""" + """4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A""" + """062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36""" + """4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1""" + """B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92""" + """4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47""" + """9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71""" + """60C980DD98EDD3DFFFFFFFFFFFFFFFFF""", base=1_6, ), """generator""": 2, }, } class A_ : """simple docstring""" def __init__( self : Union[str, Any] ,__A : int = 14 ) -> None: if group not in primes: raise ValueError('Unsupported Group' ) _lowercase = primes[group]['prime'] _lowercase = primes[group]['generator'] _lowercase = int(hexlify(urandom(32 ) ) ,base=16 ) def __UpperCAmelCase ( self : List[Any] ) -> str: return hex(self.__private_key )[2:] def __UpperCAmelCase ( self : Any ) -> str: _lowercase = pow(self.generator ,self.__private_key ,self.prime ) return hex(__A )[2:] def __UpperCAmelCase ( self : str ,__A : int ) -> bool: # check if the other public key is valid based on NIST SP800-56 return ( 2 <= key <= self.prime - 2 and pow(__A ,(self.prime - 1) // 2 ,self.prime ) == 1 ) def __UpperCAmelCase ( self : Dict ,__A : str ) -> str: _lowercase = int(__A ,base=16 ) if not self.is_valid_public_key(__A ): raise ValueError('Invalid public key' ) _lowercase = pow(__A ,self.__private_key ,self.prime ) return shaaaa(str(__A ).encode() ).hexdigest() @staticmethod def __UpperCAmelCase ( __A : int ,__A : int ) -> bool: # check if the other public key is valid based on NIST SP800-56 return ( 2 <= remote_public_key_str <= prime - 2 and pow(__A ,(prime - 1) // 2 ,__A ) == 1 ) @staticmethod def __UpperCAmelCase ( __A : str ,__A : str ,__A : int = 14 ) -> str: _lowercase = int(__A ,base=16 ) _lowercase = int(__A ,base=16 ) _lowercase = primes[group]['prime'] if not DiffieHellman.is_valid_public_key_static(__A ,__A ): raise ValueError('Invalid public key' ) _lowercase = pow(__A ,__A ,__A ) return shaaaa(str(__A ).encode() ).hexdigest() if __name__ == "__main__": import doctest doctest.testmod()
67
'''simple docstring''' import os from datetime import datetime as dt from github import Github _lowerCAmelCase = [ '''good first issue''', '''good second issue''', '''good difficult issue''', '''enhancement''', '''new pipeline/model''', '''new scheduler''', '''wip''', ] def _SCREAMING_SNAKE_CASE ( ): """simple docstring""" lowerCAmelCase__ : List[str] = Github(os.environ["""GITHUB_TOKEN"""] ) lowerCAmelCase__ : Optional[Any] = g.get_repo("""huggingface/diffusers""" ) lowerCAmelCase__ : Tuple = repo.get_issues(state="""open""" ) for issue in open_issues: lowerCAmelCase__ : Optional[int] = sorted(issue.get_comments() , key=lambda UpperCamelCase : i.created_at , reverse=UpperCamelCase ) lowerCAmelCase__ : Any = comments[0] if len(UpperCamelCase ) > 0 else None if ( last_comment is not None and last_comment.user.login == "github-actions[bot]" and (dt.utcnow() - issue.updated_at).days > 7 and (dt.utcnow() - issue.created_at).days >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # Closes the issue after 7 days of inactivity since the Stalebot notification. issue.edit(state="""closed""" ) elif ( "stale" in issue.get_labels() and last_comment is not None and last_comment.user.login != "github-actions[bot]" ): # Opens the issue if someone other than Stalebot commented. issue.edit(state="""open""" ) issue.remove_from_labels("""stale""" ) elif ( (dt.utcnow() - issue.updated_at).days > 23 and (dt.utcnow() - issue.created_at).days >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # Post a Stalebot notification after 23 days of inactivity. issue.create_comment( """This issue has been automatically marked as stale because it has not had """ """recent activity. If you think this still needs to be addressed """ """please comment on this thread.\n\nPlease note that issues that do not follow the """ """[contributing guidelines](https://github.com/huggingface/diffusers/blob/main/CONTRIBUTING.md) """ """are likely to be ignored.""" ) issue.add_to_labels("""stale""" ) if __name__ == "__main__": main()
565
0
from ...processing_utils import ProcessorMixin class a (_lowerCAmelCase ): """simple docstring""" __UpperCAmelCase : Any = "SpeechT5FeatureExtractor" __UpperCAmelCase : Union[str, Any] = "SpeechT5Tokenizer" def __init__( self : List[str] , lowerCamelCase : Optional[int] , lowerCamelCase : Union[str, Any] ) -> int: super().__init__(lowerCamelCase , lowerCamelCase ) def __call__( self : Union[str, Any] , *lowerCamelCase : Optional[int] , **lowerCamelCase : List[Any] ) -> str: __snake_case : Optional[Any] = kwargs.pop("audio" , lowerCamelCase ) __snake_case : Optional[Any] = kwargs.pop("text" , lowerCamelCase ) __snake_case : str = kwargs.pop("text_target" , lowerCamelCase ) __snake_case : Any = kwargs.pop("audio_target" , lowerCamelCase ) __snake_case : Optional[Any] = kwargs.pop("sampling_rate" , lowerCamelCase ) 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: __snake_case : Optional[int] = self.feature_extractor(lowerCamelCase , *lowerCamelCase , sampling_rate=lowerCamelCase , **lowerCamelCase ) elif text is not None: __snake_case : Union[str, Any] = self.tokenizer(lowerCamelCase , **lowerCamelCase ) else: __snake_case : str = None if audio_target is not None: __snake_case : Optional[int] = self.feature_extractor(audio_target=lowerCamelCase , *lowerCamelCase , sampling_rate=lowerCamelCase , **lowerCamelCase ) __snake_case : Any = targets["input_values"] elif text_target is not None: __snake_case : Dict = self.tokenizer(lowerCamelCase , **lowerCamelCase ) __snake_case : Dict = targets["input_ids"] else: __snake_case : int = None if inputs is None: return targets if targets is not None: __snake_case : Union[str, Any] = labels __snake_case : Optional[Any] = targets.get("attention_mask" ) if decoder_attention_mask is not None: __snake_case : str = decoder_attention_mask return inputs def __snake_case ( self : int , *lowerCamelCase : Optional[Any] , **lowerCamelCase : Optional[int] ) -> Dict: __snake_case : Union[str, Any] = kwargs.pop("input_values" , lowerCamelCase ) __snake_case : Optional[Any] = kwargs.pop("input_ids" , lowerCamelCase ) __snake_case : List[str] = kwargs.pop("labels" , lowerCamelCase ) 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: __snake_case : Union[str, Any] = self.feature_extractor.pad(lowerCamelCase , *lowerCamelCase , **lowerCamelCase ) elif input_ids is not None: __snake_case : str = self.tokenizer.pad(lowerCamelCase , **lowerCamelCase ) else: __snake_case : Optional[Any] = None if labels is not None: if "input_ids" in labels or (isinstance(lowerCamelCase , lowerCamelCase ) and "input_ids" in labels[0]): __snake_case : str = self.tokenizer.pad(lowerCamelCase , **lowerCamelCase ) __snake_case : Any = targets["input_ids"] else: __snake_case : Optional[Any] = self.feature_extractor.feature_size __snake_case : List[Any] = self.feature_extractor.num_mel_bins __snake_case : List[Any] = self.feature_extractor.pad(lowerCamelCase , *lowerCamelCase , **lowerCamelCase ) __snake_case : Dict = feature_size_hack __snake_case : List[Any] = targets["input_values"] else: __snake_case : Tuple = None if inputs is None: return targets if targets is not None: __snake_case : Tuple = labels __snake_case : List[str] = targets.get("attention_mask" ) if decoder_attention_mask is not None: __snake_case : Dict = decoder_attention_mask return inputs def __snake_case ( self : Any , *lowerCamelCase : str , **lowerCamelCase : Optional[Any] ) -> str: return self.tokenizer.batch_decode(*lowerCamelCase , **lowerCamelCase ) def __snake_case ( self : Optional[Any] , *lowerCamelCase : Any , **lowerCamelCase : Union[str, Any] ) -> Optional[int]: return self.tokenizer.decode(*lowerCamelCase , **lowerCamelCase )
203
import unittest import numpy as np def lowerCAmelCase_ ( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = None , ): __snake_case : List[str] = np.shape(__lowerCamelCase ) __snake_case : Optional[Any] = np.shape(__lowerCamelCase ) __snake_case : List[str] = np.shape(__lowerCamelCase ) if shape_a[0] != shape_b[0]: __snake_case : Any = ( "Expected the same number of rows for A and B. " F'Instead found A of size {shape_a} and B of size {shape_b}' ) raise ValueError(__lowerCamelCase ) if shape_b[1] != shape_c[1]: __snake_case : int = ( "Expected the same number of columns for B and C. " F'Instead found B of size {shape_b} and C of size {shape_c}' ) raise ValueError(__lowerCamelCase ) __snake_case : str = pseudo_inv if a_inv is None: try: __snake_case : Optional[Any] = np.linalg.inv(__lowerCamelCase ) except np.linalg.LinAlgError: raise ValueError( "Input matrix A is not invertible. Cannot compute Schur complement." ) return mat_c - mat_b.T @ a_inv @ mat_b class a (unittest.TestCase ): """simple docstring""" def __snake_case ( self : Tuple ) -> None: __snake_case : str = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) __snake_case : str = np.array([[0, 3], [3, 0], [2, 3]] ) __snake_case : Dict = np.array([[2, 1], [6, 3]] ) __snake_case : Dict = schur_complement(lowerCamelCase , lowerCamelCase , lowerCamelCase ) __snake_case : int = np.block([[a, b], [b.T, c]] ) __snake_case : Optional[int] = np.linalg.det(lowerCamelCase ) __snake_case : Any = np.linalg.det(lowerCamelCase ) __snake_case : Tuple = np.linalg.det(lowerCamelCase ) self.assertAlmostEqual(lowerCamelCase , det_a * det_s ) def __snake_case ( self : int ) -> None: __snake_case : str = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) __snake_case : Dict = np.array([[0, 3], [3, 0], [2, 3]] ) __snake_case : Tuple = np.array([[2, 1], [6, 3]] ) with self.assertRaises(lowerCamelCase ): schur_complement(lowerCamelCase , lowerCamelCase , lowerCamelCase ) def __snake_case ( self : List[Any] ) -> None: __snake_case : List[Any] = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) __snake_case : Tuple = np.array([[0, 3], [3, 0], [2, 3]] ) __snake_case : List[Any] = np.array([[2, 1, 3], [6, 3, 5]] ) with self.assertRaises(lowerCamelCase ): schur_complement(lowerCamelCase , lowerCamelCase , lowerCamelCase ) if __name__ == "__main__": import doctest doctest.testmod() unittest.main()
203
1
from __future__ import annotations from decimal import Decimal from numpy import array def A_ ( _lowerCAmelCase ) -> list[list[float]]: UpperCamelCase : List[str] = Decimal # Check if the provided matrix has 2 rows and 2 columns # since this implementation only works for 2x2 matrices if len(_lowerCAmelCase ) == 2 and len(matrix[0] ) == 2 and len(matrix[1] ) == 2: # Calculate the determinant of the matrix UpperCamelCase : List[str] = float( d(matrix[0][0] ) * d(matrix[1][1] ) - d(matrix[1][0] ) * d(matrix[0][1] ) ) if determinant == 0: raise ValueError("This matrix has no inverse." ) # Creates a copy of the matrix with swapped positions of the elements UpperCamelCase : List[str] = [[0.0, 0.0], [0.0, 0.0]] UpperCamelCase , UpperCamelCase : List[Any] = matrix[1][1], matrix[0][0] UpperCamelCase , UpperCamelCase : Optional[Any] = -matrix[1][0], -matrix[0][1] # Calculate the inverse of the matrix return [ [(float(d(_lowerCAmelCase ) ) / determinant) or 0.0 for n in row] for row in swapped_matrix ] elif ( len(_lowerCAmelCase ) == 3 and len(matrix[0] ) == 3 and len(matrix[1] ) == 3 and len(matrix[2] ) == 3 ): # Calculate the determinant of the matrix using Sarrus rule UpperCamelCase : List[Any] = float( ( (d(matrix[0][0] ) * d(matrix[1][1] ) * d(matrix[2][2] )) + (d(matrix[0][1] ) * d(matrix[1][2] ) * d(matrix[2][0] )) + (d(matrix[0][2] ) * d(matrix[1][0] ) * d(matrix[2][1] )) ) - ( (d(matrix[0][2] ) * d(matrix[1][1] ) * d(matrix[2][0] )) + (d(matrix[0][1] ) * d(matrix[1][0] ) * d(matrix[2][2] )) + (d(matrix[0][0] ) * d(matrix[1][2] ) * d(matrix[2][1] )) ) ) if determinant == 0: raise ValueError("This matrix has no inverse." ) # Creating cofactor matrix UpperCamelCase : Dict = [ [d(0.0 ), d(0.0 ), d(0.0 )], [d(0.0 ), d(0.0 ), d(0.0 )], [d(0.0 ), d(0.0 ), d(0.0 )], ] UpperCamelCase : Union[str, Any] = (d(matrix[1][1] ) * d(matrix[2][2] )) - ( d(matrix[1][2] ) * d(matrix[2][1] ) ) UpperCamelCase : List[Any] = -( (d(matrix[1][0] ) * d(matrix[2][2] )) - (d(matrix[1][2] ) * d(matrix[2][0] )) ) UpperCamelCase : str = (d(matrix[1][0] ) * d(matrix[2][1] )) - ( d(matrix[1][1] ) * d(matrix[2][0] ) ) UpperCamelCase : Optional[int] = -( (d(matrix[0][1] ) * d(matrix[2][2] )) - (d(matrix[0][2] ) * d(matrix[2][1] )) ) UpperCamelCase : List[Any] = (d(matrix[0][0] ) * d(matrix[2][2] )) - ( d(matrix[0][2] ) * d(matrix[2][0] ) ) UpperCamelCase : Dict = -( (d(matrix[0][0] ) * d(matrix[2][1] )) - (d(matrix[0][1] ) * d(matrix[2][0] )) ) UpperCamelCase : Tuple = (d(matrix[0][1] ) * d(matrix[1][2] )) - ( d(matrix[0][2] ) * d(matrix[1][1] ) ) UpperCamelCase : Optional[Any] = -( (d(matrix[0][0] ) * d(matrix[1][2] )) - (d(matrix[0][2] ) * d(matrix[1][0] )) ) UpperCamelCase : List[str] = (d(matrix[0][0] ) * d(matrix[1][1] )) - ( d(matrix[0][1] ) * d(matrix[1][0] ) ) # Transpose the cofactor matrix (Adjoint matrix) UpperCamelCase : Optional[Any] = array(_lowerCAmelCase ) for i in range(3 ): for j in range(3 ): UpperCamelCase : Tuple = cofactor_matrix[j][i] # Inverse of the matrix using the formula (1/determinant) * adjoint matrix UpperCamelCase : Optional[int] = array(_lowerCAmelCase ) for i in range(3 ): for j in range(3 ): inverse_matrix[i][j] /= d(_lowerCAmelCase ) # Calculate the inverse of the matrix return [[float(d(_lowerCAmelCase ) ) or 0.0 for n in row] for row in inverse_matrix] raise ValueError("Please provide a matrix of size 2x2 or 3x3." )
629
import json import os from functools import lru_cache from typing import Dict, List, Optional, Tuple, Union import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...tokenization_utils_base import BatchEncoding, EncodedInput from ...utils import PaddingStrategy, logging __lowerCamelCase : Tuple = logging.get_logger(__name__) __lowerCamelCase : int = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt"""} # See all LED models at https://huggingface.co/models?filter=LED __lowerCamelCase : str = { """vocab_file""": { """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/vocab.json""", }, """merges_file""": { """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/merges.txt""", }, """tokenizer_file""": { """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/tokenizer.json""", }, } __lowerCamelCase : List[str] = { """allenai/led-base-16384""": 1_6384, } @lru_cache() # Copied from transformers.models.bart.tokenization_bart.bytes_to_unicode def A_ ( ) -> List[str]: UpperCamelCase : Optional[int] = ( list(range(ord("!" ) , ord("~" ) + 1 ) ) + list(range(ord("¡" ) , ord("¬" ) + 1 ) ) + list(range(ord("®" ) , ord("ÿ" ) + 1 ) ) ) UpperCamelCase : List[Any] = bs[:] UpperCamelCase : List[Any] = 0 for b in range(2**8 ): if b not in bs: bs.append(_lowerCAmelCase ) cs.append(2**8 + n ) n += 1 UpperCamelCase : Union[str, Any] = [chr(_lowerCAmelCase ) for n in cs] return dict(zip(_lowerCAmelCase , _lowerCAmelCase ) ) def A_ ( _lowerCAmelCase ) -> Optional[Any]: UpperCamelCase : int = set() UpperCamelCase : List[str] = word[0] for char in word[1:]: pairs.add((prev_char, char) ) UpperCamelCase : List[str] = char return pairs class A__ ( __snake_case ): _UpperCAmelCase :Dict = VOCAB_FILES_NAMES _UpperCAmelCase :List[Any] = PRETRAINED_VOCAB_FILES_MAP _UpperCAmelCase :List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _UpperCAmelCase :Union[str, Any] = ['input_ids', 'attention_mask'] def __init__( self , A_ , A_ , A_="replace" , A_="<s>" , A_="</s>" , A_="</s>" , A_="<s>" , A_="<unk>" , A_="<pad>" , A_="<mask>" , A_=False , **A_ , ): '''simple docstring''' UpperCamelCase : int = AddedToken(A_ , lstrip=A_ , rstrip=A_ ) if isinstance(A_ , A_ ) else bos_token UpperCamelCase : Tuple = AddedToken(A_ , lstrip=A_ , rstrip=A_ ) if isinstance(A_ , A_ ) else eos_token UpperCamelCase : Optional[Any] = AddedToken(A_ , lstrip=A_ , rstrip=A_ ) if isinstance(A_ , A_ ) else sep_token UpperCamelCase : Union[str, Any] = AddedToken(A_ , lstrip=A_ , rstrip=A_ ) if isinstance(A_ , A_ ) else cls_token UpperCamelCase : Dict = AddedToken(A_ , lstrip=A_ , rstrip=A_ ) if isinstance(A_ , A_ ) else unk_token UpperCamelCase : int = AddedToken(A_ , lstrip=A_ , rstrip=A_ ) if isinstance(A_ , A_ ) else pad_token # Mask token behave like a normal word, i.e. include the space before it UpperCamelCase : Optional[Any] = AddedToken(A_ , lstrip=A_ , rstrip=A_ ) if isinstance(A_ , A_ ) else mask_token super().__init__( errors=A_ , bos_token=A_ , eos_token=A_ , unk_token=A_ , sep_token=A_ , cls_token=A_ , pad_token=A_ , mask_token=A_ , add_prefix_space=A_ , **A_ , ) with open(A_ , encoding="utf-8" ) as vocab_handle: UpperCamelCase : int = json.load(A_ ) UpperCamelCase : Dict = {v: k for k, v in self.encoder.items()} UpperCamelCase : List[str] = errors # how to handle errors in decoding UpperCamelCase : Optional[Any] = bytes_to_unicode() UpperCamelCase : Tuple = {v: k for k, v in self.byte_encoder.items()} with open(A_ , encoding="utf-8" ) as merges_handle: UpperCamelCase : str = merges_handle.read().split("\n" )[1:-1] UpperCamelCase : Optional[Any] = [tuple(merge.split() ) for merge in bpe_merges] UpperCamelCase : Any = dict(zip(A_ , range(len(A_ ) ) ) ) UpperCamelCase : List[str] = {} UpperCamelCase : Any = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions UpperCamelCase : Any = 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.bart.tokenization_bart.BartTokenizer.vocab_size def __UpperCamelCase( self ): '''simple docstring''' return len(self.encoder ) def __UpperCamelCase( self ): '''simple docstring''' return dict(self.encoder , **self.added_tokens_encoder ) def __UpperCamelCase( self , A_ ): '''simple docstring''' if token in self.cache: return self.cache[token] UpperCamelCase : Union[str, Any] = tuple(A_ ) UpperCamelCase : str = get_pairs(A_ ) if not pairs: return token while True: UpperCamelCase : Any = min(A_ , key=lambda A_ : self.bpe_ranks.get(A_ , float("inf" ) ) ) if bigram not in self.bpe_ranks: break UpperCamelCase , UpperCamelCase : Union[str, Any] = bigram UpperCamelCase : List[Any] = [] UpperCamelCase : Union[str, Any] = 0 while i < len(A_ ): try: UpperCamelCase : Union[str, Any] = word.index(A_ , A_ ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) UpperCamelCase : Union[str, Any] = j if word[i] == first and i < len(A_ ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 UpperCamelCase : Tuple = tuple(A_ ) UpperCamelCase : Union[str, Any] = new_word if len(A_ ) == 1: break else: UpperCamelCase : int = get_pairs(A_ ) UpperCamelCase : Union[str, Any] = " ".join(A_ ) UpperCamelCase : Any = word return word def __UpperCamelCase( self , A_ ): '''simple docstring''' UpperCamelCase : Union[str, Any] = [] for token in re.findall(self.pat , A_ ): UpperCamelCase : Optional[Any] = "".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(A_ ).split(" " ) ) return bpe_tokens def __UpperCamelCase( self , A_ ): '''simple docstring''' return self.encoder.get(A_ , self.encoder.get(self.unk_token ) ) def __UpperCamelCase( self , A_ ): '''simple docstring''' return self.decoder.get(A_ ) def __UpperCamelCase( self , A_ ): '''simple docstring''' UpperCamelCase : List[Any] = "".join(A_ ) UpperCamelCase : List[Any] = bytearray([self.byte_decoder[c] for c in text] ).decode("utf-8" , errors=self.errors ) return text def __UpperCamelCase( self , A_ , A_ = None ): '''simple docstring''' if not os.path.isdir(A_ ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return UpperCamelCase : List[str] = os.path.join( A_ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) UpperCamelCase : Any = os.path.join( A_ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"] ) with open(A_ , "w" , encoding="utf-8" ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=A_ , ensure_ascii=A_ ) + "\n" ) UpperCamelCase : str = 0 with open(A_ , "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 A_ : 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!" ) UpperCamelCase : Any = token_index writer.write(" ".join(A_ ) + "\n" ) index += 1 return vocab_file, merge_file def __UpperCamelCase( self , A_ , A_ = None ): '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] UpperCamelCase : List[str] = [self.cls_token_id] UpperCamelCase : Optional[Any] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def __UpperCamelCase( self , A_ , A_ = None , A_ = False ): '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=A_ , token_ids_a=A_ , already_has_special_tokens=A_ ) if token_ids_a is None: return [1] + ([0] * len(A_ )) + [1] return [1] + ([0] * len(A_ )) + [1, 1] + ([0] * len(A_ )) + [1] def __UpperCamelCase( self , A_ , A_ = None ): '''simple docstring''' UpperCamelCase : Any = [self.sep_token_id] UpperCamelCase : Any = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def __UpperCamelCase( self , A_ , A_=False , **A_ ): '''simple docstring''' UpperCamelCase : Tuple = kwargs.pop("add_prefix_space" , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(A_ ) > 0 and not text[0].isspace()): UpperCamelCase : List[str] = " " + text return (text, kwargs) def __UpperCamelCase( self , A_ , A_ = None , A_ = PaddingStrategy.DO_NOT_PAD , A_ = None , A_ = None , ): '''simple docstring''' UpperCamelCase : Optional[Any] = super()._pad( encoded_inputs=A_ , max_length=A_ , padding_strategy=A_ , pad_to_multiple_of=A_ , return_attention_mask=A_ , ) # Load from model defaults if return_attention_mask is None: UpperCamelCase : Union[str, Any] = "attention_mask" in self.model_input_names if return_attention_mask and "global_attention_mask" in encoded_inputs: UpperCamelCase : int = encoded_inputs[self.model_input_names[0]] # `global_attention_mask` need to have the same length as other (sequential) inputs. UpperCamelCase : str = len(encoded_inputs["global_attention_mask"] ) != len(A_ ) if needs_to_be_padded: UpperCamelCase : Optional[int] = len(A_ ) - len(encoded_inputs["global_attention_mask"] ) if self.padding_side == "right": # Use `-1` since `0` in `global_attention_mask` means `local attention` instead of `not to attend` UpperCamelCase : Union[str, Any] = ( encoded_inputs["global_attention_mask"] + [-1] * difference ) elif self.padding_side == "left": UpperCamelCase : Tuple = [-1] * difference + encoded_inputs[ "global_attention_mask" ] else: raise ValueError("Invalid padding strategy:" + str(self.padding_side ) ) return encoded_inputs
629
1
from .glue import glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels from .squad import SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features from .utils import DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor from .xnli import xnli_output_modes, xnli_processors, xnli_tasks_num_labels
718
import unittest import numpy as np from transformers.testing_utils import require_pytesseract, require_torch from transformers.utils import is_pytesseract_available, is_torch_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_pytesseract_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class a_ ( unittest.TestCase ): def __init__( self : Optional[Any] , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : Optional[int]=7 , UpperCAmelCase__ : Union[str, Any]=3 , UpperCAmelCase__ : int=18 , UpperCAmelCase__ : Optional[int]=30 , UpperCAmelCase__ : Optional[int]=400 , UpperCAmelCase__ : int=True , UpperCAmelCase__ : Optional[int]=None , UpperCAmelCase__ : int=True , ): """simple docstring""" snake_case : int = size if size is not None else {'''height''': 18, '''width''': 18} snake_case : Optional[Any] = parent snake_case : Any = batch_size snake_case : Any = num_channels snake_case : Union[str, Any] = image_size snake_case : Dict = min_resolution snake_case : Dict = max_resolution snake_case : int = do_resize snake_case : List[str] = size snake_case : List[Any] = apply_ocr def lowerCAmelCase( self : int ): """simple docstring""" return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class a_ ( a , unittest.TestCase ): A__ : List[Any] = LayoutLMvaImageProcessor if is_pytesseract_available() else None def lowerCAmelCase( self : Dict ): """simple docstring""" snake_case : Optional[Any] = LayoutLMvaImageProcessingTester(self ) @property def lowerCAmelCase( self : Dict ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def lowerCAmelCase( self : List[Any] ): """simple docstring""" snake_case : List[str] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(UpperCAmelCase__ , '''do_resize''' ) ) self.assertTrue(hasattr(UpperCAmelCase__ , '''size''' ) ) self.assertTrue(hasattr(UpperCAmelCase__ , '''apply_ocr''' ) ) def lowerCAmelCase( self : Optional[int] ): """simple docstring""" snake_case : Optional[Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 18, '''width''': 18} ) snake_case : Optional[int] = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'''height''': 42, '''width''': 42} ) def lowerCAmelCase( self : str ): """simple docstring""" pass def lowerCAmelCase( self : Dict ): """simple docstring""" # Initialize image_processing snake_case : Tuple = self.image_processing_class(**self.image_processor_dict ) # create random PIL images snake_case : Dict = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCAmelCase__ ) for image in image_inputs: self.assertIsInstance(UpperCAmelCase__ , Image.Image ) # Test not batched input snake_case : Optional[Any] = image_processing(image_inputs[0] , return_tensors='''pt''' ) self.assertEqual( encoding.pixel_values.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) self.assertIsInstance(encoding.words , UpperCAmelCase__ ) self.assertIsInstance(encoding.boxes , UpperCAmelCase__ ) # Test batched snake_case : Dict = image_processing(UpperCAmelCase__ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def lowerCAmelCase( self : Union[str, Any] ): """simple docstring""" # Initialize image_processing snake_case : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors snake_case : Union[str, Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCAmelCase__ , numpify=UpperCAmelCase__ ) for image in image_inputs: self.assertIsInstance(UpperCAmelCase__ , np.ndarray ) # Test not batched input snake_case : List[Any] = 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 snake_case : List[Any] = image_processing(UpperCAmelCase__ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def lowerCAmelCase( self : Optional[Any] ): """simple docstring""" # Initialize image_processing snake_case : str = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors snake_case : Dict = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCAmelCase__ , torchify=UpperCAmelCase__ ) for image in image_inputs: self.assertIsInstance(UpperCAmelCase__ , torch.Tensor ) # Test not batched input snake_case : List[Any] = 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 snake_case : Tuple = image_processing(UpperCAmelCase__ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def lowerCAmelCase( self : Optional[Any] ): """simple docstring""" # with apply_OCR = True snake_case : int = LayoutLMvaImageProcessor() from datasets import load_dataset snake_case : List[Any] = load_dataset('''hf-internal-testing/fixtures_docvqa''' , split='''test''' ) snake_case : List[Any] = Image.open(ds[0]['''file'''] ).convert('''RGB''' ) snake_case : Any = image_processing(UpperCAmelCase__ , return_tensors='''pt''' ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 224, 224) ) self.assertEqual(len(encoding.words ) , len(encoding.boxes ) ) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 snake_case : Optional[Any] = [['''11:14''', '''to''', '''11:39''', '''a.m''', '''11:39''', '''to''', '''11:44''', '''a.m.''', '''11:44''', '''a.m.''', '''to''', '''12:25''', '''p.m.''', '''12:25''', '''to''', '''12:58''', '''p.m.''', '''12:58''', '''to''', '''4:00''', '''p.m.''', '''2:00''', '''to''', '''5:00''', '''p.m.''', '''Coffee''', '''Break''', '''Coffee''', '''will''', '''be''', '''served''', '''for''', '''men''', '''and''', '''women''', '''in''', '''the''', '''lobby''', '''adjacent''', '''to''', '''exhibit''', '''area.''', '''Please''', '''move''', '''into''', '''exhibit''', '''area.''', '''(Exhibits''', '''Open)''', '''TRRF''', '''GENERAL''', '''SESSION''', '''(PART''', '''|)''', '''Presiding:''', '''Lee''', '''A.''', '''Waller''', '''TRRF''', '''Vice''', '''President''', '''“Introductory''', '''Remarks”''', '''Lee''', '''A.''', '''Waller,''', '''TRRF''', '''Vice''', '''Presi-''', '''dent''', '''Individual''', '''Interviews''', '''with''', '''TRRF''', '''Public''', '''Board''', '''Members''', '''and''', '''Sci-''', '''entific''', '''Advisory''', '''Council''', '''Mem-''', '''bers''', '''Conducted''', '''by''', '''TRRF''', '''Treasurer''', '''Philip''', '''G.''', '''Kuehn''', '''to''', '''get''', '''answers''', '''which''', '''the''', '''public''', '''refrigerated''', '''warehousing''', '''industry''', '''is''', '''looking''', '''for.''', '''Plus''', '''questions''', '''from''', '''the''', '''floor.''', '''Dr.''', '''Emil''', '''M.''', '''Mrak,''', '''University''', '''of''', '''Cal-''', '''ifornia,''', '''Chairman,''', '''TRRF''', '''Board;''', '''Sam''', '''R.''', '''Cecil,''', '''University''', '''of''', '''Georgia''', '''College''', '''of''', '''Agriculture;''', '''Dr.''', '''Stanley''', '''Charm,''', '''Tufts''', '''University''', '''School''', '''of''', '''Medicine;''', '''Dr.''', '''Robert''', '''H.''', '''Cotton,''', '''ITT''', '''Continental''', '''Baking''', '''Company;''', '''Dr.''', '''Owen''', '''Fennema,''', '''University''', '''of''', '''Wis-''', '''consin;''', '''Dr.''', '''Robert''', '''E.''', '''Hardenburg,''', '''USDA.''', '''Questions''', '''and''', '''Answers''', '''Exhibits''', '''Open''', '''Capt.''', '''Jack''', '''Stoney''', '''Room''', '''TRRF''', '''Scientific''', '''Advisory''', '''Council''', '''Meeting''', '''Ballroom''', '''Foyer''']] # noqa: E231 snake_case : Union[str, Any] = [[[141, 57, 214, 69], [228, 58, 252, 69], [141, 75, 216, 88], [230, 79, 280, 88], [142, 260, 218, 273], [230, 261, 255, 273], [143, 279, 218, 290], [231, 282, 290, 291], [143, 342, 218, 354], [231, 345, 289, 355], [202, 362, 227, 373], [143, 379, 220, 392], [231, 382, 291, 394], [144, 714, 220, 726], [231, 715, 256, 726], [144, 732, 220, 745], [232, 736, 291, 747], [144, 769, 218, 782], [231, 770, 256, 782], [141, 788, 202, 801], [215, 791, 274, 804], [143, 826, 204, 838], [215, 826, 240, 838], [142, 844, 202, 857], [215, 847, 274, 859], [334, 57, 427, 69], [440, 57, 522, 69], [369, 75, 461, 88], [469, 75, 516, 88], [528, 76, 562, 88], [570, 76, 667, 88], [675, 75, 711, 87], [721, 79, 778, 88], [789, 75, 840, 88], [369, 97, 470, 107], [484, 94, 507, 106], [518, 94, 562, 107], [576, 94, 655, 110], [668, 94, 792, 109], [804, 95, 829, 107], [369, 113, 465, 125], [477, 116, 547, 125], [562, 113, 658, 125], [671, 116, 748, 125], [761, 113, 811, 125], [369, 131, 465, 143], [477, 133, 548, 143], [563, 130, 698, 145], [710, 130, 802, 146], [336, 171, 412, 183], [423, 171, 572, 183], [582, 170, 716, 184], [728, 171, 817, 187], [829, 171, 844, 186], [338, 197, 482, 212], [507, 196, 557, 209], [569, 196, 595, 208], [610, 196, 702, 209], [505, 214, 583, 226], [595, 214, 656, 227], [670, 215, 807, 227], [335, 259, 543, 274], [556, 259, 708, 272], [372, 279, 422, 291], [435, 279, 460, 291], [474, 279, 574, 292], [587, 278, 664, 291], [676, 278, 738, 291], [751, 279, 834, 291], [372, 298, 434, 310], [335, 341, 483, 354], [497, 341, 655, 354], [667, 341, 728, 354], [740, 341, 825, 354], [335, 360, 430, 372], [442, 360, 534, 372], [545, 359, 687, 372], [697, 360, 754, 372], [765, 360, 823, 373], [334, 378, 428, 391], [440, 378, 577, 394], [590, 378, 705, 391], [720, 378, 801, 391], [334, 397, 400, 409], [370, 416, 529, 429], [544, 416, 576, 432], [587, 416, 665, 428], [677, 416, 814, 429], [372, 435, 452, 450], [465, 434, 495, 447], [511, 434, 600, 447], [611, 436, 637, 447], [649, 436, 694, 451], [705, 438, 824, 447], [369, 453, 452, 466], [464, 454, 509, 466], [522, 453, 611, 469], [625, 453, 792, 469], [370, 472, 556, 488], [570, 472, 684, 487], [697, 472, 718, 485], [732, 472, 835, 488], [369, 490, 411, 503], [425, 490, 484, 503], [496, 490, 635, 506], [645, 490, 707, 503], [718, 491, 761, 503], [771, 490, 840, 503], [336, 510, 374, 521], [388, 510, 447, 522], [460, 510, 489, 521], [503, 510, 580, 522], [592, 509, 736, 525], [745, 509, 770, 522], [781, 509, 840, 522], [338, 528, 434, 541], [448, 528, 596, 541], [609, 527, 687, 540], [700, 528, 792, 541], [336, 546, 397, 559], [407, 546, 431, 559], [443, 546, 525, 560], [537, 546, 680, 562], [688, 546, 714, 559], [722, 546, 837, 562], [336, 565, 449, 581], [461, 565, 485, 577], [497, 565, 665, 581], [681, 565, 718, 577], [732, 565, 837, 580], [337, 584, 438, 597], [452, 583, 521, 596], [535, 584, 677, 599], [690, 583, 787, 596], [801, 583, 825, 596], [338, 602, 478, 615], [492, 602, 530, 614], [543, 602, 638, 615], [650, 602, 676, 614], [688, 602, 788, 615], [802, 602, 843, 614], [337, 621, 502, 633], [516, 621, 615, 637], [629, 621, 774, 636], [789, 621, 827, 633], [337, 639, 418, 652], [432, 640, 571, 653], [587, 639, 731, 655], [743, 639, 769, 652], [780, 639, 841, 652], [338, 658, 440, 673], [455, 658, 491, 670], [508, 658, 602, 671], [616, 658, 638, 670], [654, 658, 835, 674], [337, 677, 429, 689], [337, 714, 482, 726], [495, 714, 548, 726], [561, 714, 683, 726], [338, 770, 461, 782], [474, 769, 554, 785], [489, 788, 562, 803], [576, 788, 643, 801], [656, 787, 751, 804], [764, 788, 844, 801], [334, 825, 421, 838], [430, 824, 574, 838], [584, 824, 723, 841], [335, 844, 450, 857], [464, 843, 583, 860], [628, 862, 755, 875], [769, 861, 848, 878]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words , UpperCAmelCase__ ) self.assertListEqual(encoding.boxes , UpperCAmelCase__ ) # with apply_OCR = False snake_case : str = LayoutLMvaImageProcessor(apply_ocr=UpperCAmelCase__ ) snake_case : Optional[Any] = image_processing(UpperCAmelCase__ , return_tensors='''pt''' ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 224, 224) )
84
0
import numpy # List of input, output pairs A_ : Any = ( ((5, 2, 3), 15), ((6, 5, 9), 25), ((11, 12, 13), 41), ((1, 1, 1), 8), ((11, 12, 13), 41), ) A_ : List[Any] = (((515, 22, 13), 555), ((61, 35, 49), 150)) A_ : Any = [2, 4, 1, 5] A_ : List[Any] = len(train_data) A_ : List[Any] = 0.009 def snake_case (UpperCAmelCase__ , UpperCAmelCase__="train" ) -> Optional[int]: return calculate_hypothesis_value(UpperCAmelCase__ , UpperCAmelCase__ ) - output( UpperCAmelCase__ , UpperCAmelCase__ ) def snake_case (UpperCAmelCase__ ) -> Optional[Any]: UpperCamelCase_: Optional[Any] = 0 for i in range(len(UpperCAmelCase__ ) - 1 ): hyp_val += data_input_tuple[i] * parameter_vector[i + 1] hyp_val += parameter_vector[0] return hyp_val def snake_case (UpperCAmelCase__ , UpperCAmelCase__ ) -> List[Any]: if data_set == "train": return train_data[example_no][1] elif data_set == "test": return test_data[example_no][1] return None def snake_case (UpperCAmelCase__ , UpperCAmelCase__ ) -> Optional[Any]: if data_set == "train": return _hypothesis_value(train_data[example_no][0] ) elif data_set == "test": return _hypothesis_value(test_data[example_no][0] ) return None def snake_case (UpperCAmelCase__ , UpperCAmelCase__=m ) -> Optional[Any]: UpperCamelCase_: Any = 0 for i in range(UpperCAmelCase__ ): if index == -1: summation_value += _error(UpperCAmelCase__ ) else: summation_value += _error(UpperCAmelCase__ ) * train_data[i][0][index] return summation_value def snake_case (UpperCAmelCase__ ) -> Optional[Any]: UpperCamelCase_: Optional[int] = summation_of_cost_derivative(UpperCAmelCase__ , UpperCAmelCase__ ) / m return cost_derivative_value def snake_case () -> Union[str, Any]: global parameter_vector # Tune these values to set a tolerance value for predicted output UpperCamelCase_: str = 0.00_0002 UpperCamelCase_: Any = 0 UpperCamelCase_: int = 0 while True: j += 1 UpperCamelCase_: int = [0, 0, 0, 0] for i in range(0 , len(UpperCAmelCase__ ) ): UpperCamelCase_: Any = get_cost_derivative(i - 1 ) UpperCamelCase_: Optional[int] = ( parameter_vector[i] - LEARNING_RATE * cost_derivative ) if numpy.allclose( UpperCAmelCase__ , UpperCAmelCase__ , atol=UpperCAmelCase__ , rtol=UpperCAmelCase__ , ): break UpperCamelCase_: Optional[int] = temp_parameter_vector print(('Number of iterations:', j) ) def snake_case () -> int: for i in range(len(UpperCAmelCase__ ) ): print(('Actual output value:', output(UpperCAmelCase__ , 'test' )) ) print(('Hypothesis output:', calculate_hypothesis_value(UpperCAmelCase__ , 'test' )) ) if __name__ == "__main__": run_gradient_descent() print('\nTesting gradient descent for a linear hypothesis function.\n') test_gradient_descent()
57
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) __magic_name__ = { 'configuration_deberta': ['DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP', 'DebertaConfig', 'DebertaOnnxConfig'], 'tokenization_deberta': ['DebertaTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __magic_name__ = ['DebertaTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __magic_name__ = [ 'DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'DebertaForMaskedLM', 'DebertaForQuestionAnswering', 'DebertaForSequenceClassification', 'DebertaForTokenClassification', 'DebertaModel', 'DebertaPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __magic_name__ = [ 'TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFDebertaForMaskedLM', 'TFDebertaForQuestionAnswering', 'TFDebertaForSequenceClassification', 'TFDebertaForTokenClassification', 'TFDebertaModel', 'TFDebertaPreTrainedModel', ] if TYPE_CHECKING: from .configuration_deberta import DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, DebertaConfig, DebertaOnnxConfig from .tokenization_deberta import DebertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_deberta_fast import DebertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_deberta import ( DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, DebertaForMaskedLM, DebertaForQuestionAnswering, DebertaForSequenceClassification, DebertaForTokenClassification, DebertaModel, DebertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_deberta import ( TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFDebertaForMaskedLM, TFDebertaForQuestionAnswering, TFDebertaForSequenceClassification, TFDebertaForTokenClassification, TFDebertaModel, TFDebertaPreTrainedModel, ) else: import sys __magic_name__ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
665
0
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_distilbert import DistilBertTokenizer _lowercase = logging.get_logger(__name__) _lowercase = {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''} _lowercase = { '''vocab_file''': { '''distilbert-base-uncased''': '''https://huggingface.co/distilbert-base-uncased/resolve/main/vocab.txt''', '''distilbert-base-uncased-distilled-squad''': ( '''https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/vocab.txt''' ), '''distilbert-base-cased''': '''https://huggingface.co/distilbert-base-cased/resolve/main/vocab.txt''', '''distilbert-base-cased-distilled-squad''': ( '''https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/vocab.txt''' ), '''distilbert-base-german-cased''': '''https://huggingface.co/distilbert-base-german-cased/resolve/main/vocab.txt''', '''distilbert-base-multilingual-cased''': ( '''https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''distilbert-base-uncased''': '''https://huggingface.co/distilbert-base-uncased/resolve/main/tokenizer.json''', '''distilbert-base-uncased-distilled-squad''': ( '''https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/tokenizer.json''' ), '''distilbert-base-cased''': '''https://huggingface.co/distilbert-base-cased/resolve/main/tokenizer.json''', '''distilbert-base-cased-distilled-squad''': ( '''https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/tokenizer.json''' ), '''distilbert-base-german-cased''': ( '''https://huggingface.co/distilbert-base-german-cased/resolve/main/tokenizer.json''' ), '''distilbert-base-multilingual-cased''': ( '''https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/tokenizer.json''' ), }, } _lowercase = { '''distilbert-base-uncased''': 512, '''distilbert-base-uncased-distilled-squad''': 512, '''distilbert-base-cased''': 512, '''distilbert-base-cased-distilled-squad''': 512, '''distilbert-base-german-cased''': 512, '''distilbert-base-multilingual-cased''': 512, } _lowercase = { '''distilbert-base-uncased''': {'''do_lower_case''': True}, '''distilbert-base-uncased-distilled-squad''': {'''do_lower_case''': True}, '''distilbert-base-cased''': {'''do_lower_case''': False}, '''distilbert-base-cased-distilled-squad''': {'''do_lower_case''': False}, '''distilbert-base-german-cased''': {'''do_lower_case''': False}, '''distilbert-base-multilingual-cased''': {'''do_lower_case''': False}, } class __snake_case ( snake_case__ ): """simple docstring""" UpperCamelCase_ = VOCAB_FILES_NAMES UpperCamelCase_ = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ = PRETRAINED_INIT_CONFIGURATION UpperCamelCase_ = ['input_ids', 'attention_mask'] UpperCamelCase_ = DistilBertTokenizer def __init__( self : Optional[int] ,lowerCAmelCase__ : Tuple=None ,lowerCAmelCase__ : List[str]=None ,lowerCAmelCase__ : Dict=True ,lowerCAmelCase__ : Any="[UNK]" ,lowerCAmelCase__ : int="[SEP]" ,lowerCAmelCase__ : str="[PAD]" ,lowerCAmelCase__ : List[Any]="[CLS]" ,lowerCAmelCase__ : Dict="[MASK]" ,lowerCAmelCase__ : Any=True ,lowerCAmelCase__ : Any=None ,**lowerCAmelCase__ : Tuple ,): '''simple docstring''' super().__init__( lowerCAmelCase__ ,tokenizer_file=lowerCAmelCase__ ,do_lower_case=lowerCAmelCase__ ,unk_token=lowerCAmelCase__ ,sep_token=lowerCAmelCase__ ,pad_token=lowerCAmelCase__ ,cls_token=lowerCAmelCase__ ,mask_token=lowerCAmelCase__ ,tokenize_chinese_chars=lowerCAmelCase__ ,strip_accents=lowerCAmelCase__ ,**lowerCAmelCase__ ,) lowerCAmelCase_ : Tuple = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("lowercase" ,lowerCAmelCase__ ) != do_lower_case or normalizer_state.get("strip_accents" ,lowerCAmelCase__ ) != strip_accents or normalizer_state.get("handle_chinese_chars" ,lowerCAmelCase__ ) != tokenize_chinese_chars ): lowerCAmelCase_ : List[Any] = getattr(lowerCAmelCase__ ,normalizer_state.pop("type" ) ) lowerCAmelCase_ : Optional[int] = do_lower_case lowerCAmelCase_ : Dict = strip_accents lowerCAmelCase_ : Any = tokenize_chinese_chars lowerCAmelCase_ : Union[str, Any] = normalizer_class(**lowerCAmelCase__ ) lowerCAmelCase_ : List[Any] = do_lower_case def UpperCAmelCase_ ( self : List[Any] ,lowerCAmelCase__ : Dict ,lowerCAmelCase__ : Optional[int]=None ): '''simple docstring''' lowerCAmelCase_ : Optional[int] = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def UpperCAmelCase_ ( self : str ,lowerCAmelCase__ : List[int] ,lowerCAmelCase__ : Optional[List[int]] = None ): '''simple docstring''' lowerCAmelCase_ : Tuple = [self.sep_token_id] lowerCAmelCase_ : Tuple = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def UpperCAmelCase_ ( self : Union[str, Any] ,lowerCAmelCase__ : str ,lowerCAmelCase__ : Optional[str] = None ): '''simple docstring''' lowerCAmelCase_ : int = self._tokenizer.model.save(lowerCAmelCase__ ,name=lowerCAmelCase__ ) return tuple(lowerCAmelCase__ )
714
from __future__ import annotations from random import random class __snake_case : """simple docstring""" def __init__( self : Optional[int] ,lowerCAmelCase__ : int | None = None ) -> int: '''simple docstring''' lowerCAmelCase_ : Dict = value lowerCAmelCase_ : Any = random() lowerCAmelCase_ : Node | None = None lowerCAmelCase_ : Node | None = None def __repr__( self : Any ) -> str: '''simple docstring''' from pprint import pformat if self.left is None and self.right is None: return f'''\'{self.value}: {self.prior:.5}\'''' else: return pformat( {f'''{self.value}: {self.prior:.5}''': (self.left, self.right)} ,indent=1 ) def __str__( self : str ) -> str: '''simple docstring''' lowerCAmelCase_ : List[Any] = str(self.value ) + " " lowerCAmelCase_ : List[Any] = str(self.left or "" ) lowerCAmelCase_ : Union[str, Any] = str(self.right or "" ) return value + left + right def UpperCamelCase ( snake_case__ , snake_case__): if root is None: # None tree is split into 2 Nones return None, None elif root.value is None: return None, None else: if value < root.value: lowerCAmelCase_ , lowerCAmelCase_ : Any = split(root.left , snake_case__) return left, root else: lowerCAmelCase_ , lowerCAmelCase_ : Optional[Any] = split(root.right , snake_case__) return root, right def UpperCamelCase ( snake_case__ , snake_case__): if (not left) or (not right): # If one node is None, return the other return left or right elif left.prior < right.prior: lowerCAmelCase_ : Dict = merge(left.right , snake_case__) return left else: lowerCAmelCase_ : List[str] = merge(snake_case__ , right.left) return right def UpperCamelCase ( snake_case__ , snake_case__): lowerCAmelCase_ : List[Any] = Node(snake_case__) lowerCAmelCase_ , lowerCAmelCase_ : Tuple = split(snake_case__ , snake_case__) return merge(merge(snake_case__ , snake_case__) , snake_case__) def UpperCamelCase ( snake_case__ , snake_case__): lowerCAmelCase_ , lowerCAmelCase_ : List[str] = split(snake_case__ , value - 1) lowerCAmelCase_ , lowerCAmelCase_ : Union[str, Any] = split(snake_case__ , snake_case__) return merge(snake_case__ , snake_case__) def UpperCamelCase ( snake_case__): if not root: # None return else: inorder(root.left) print(root.value , end=",") inorder(root.right) def UpperCamelCase ( snake_case__ , snake_case__): for arg in args.split(): if arg[0] == "+": lowerCAmelCase_ : List[str] = insert(snake_case__ , int(arg[1:])) elif arg[0] == "-": lowerCAmelCase_ : Optional[int] = erase(snake_case__ , int(arg[1:])) else: print("Unknown command") return root def UpperCamelCase ( ): lowerCAmelCase_ : str = None print( "enter numbers to create a tree, + value to add value into treap, " "- value to erase all nodes with value. 'q' to quit. ") lowerCAmelCase_ : str = input() while args != "q": lowerCAmelCase_ : int = interact_treap(snake_case__ , snake_case__) print(snake_case__) lowerCAmelCase_ : str = input() print("good by!") if __name__ == "__main__": import doctest doctest.testmod() main()
683
0
import glob import os import random from string import ascii_lowercase, digits import cva lowerCamelCase_ = '''''' lowerCamelCase_ = '''''' lowerCamelCase_ = '''''' lowerCamelCase_ = 1 # (0 is vertical, 1 is horizontal) def __magic_name__ ( ): '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ = get_dataset(__a , __a ) print("""Processing...""" ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = update_image_and_anno(__a , __a , __a ) for index, image in enumerate(__a ): # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' UpperCamelCase__ = random_chars(32 ) UpperCamelCase__ = paths[index].split(os.sep )[-1].rsplit(""".""" , 1 )[0] UpperCamelCase__ = f"{OUTPUT_DIR}/{file_name}_FLIP_{letter_code}" cva.imwrite(f"/{file_root}.jpg" , __a , [cva.IMWRITE_JPEG_QUALITY, 85] ) print(f"Success {index+1}/{len(__a )} with {file_name}" ) UpperCamelCase__ = [] for anno in new_annos[index]: UpperCamelCase__ = f"{anno[0]} {anno[1]} {anno[2]} {anno[3]} {anno[4]}" annos_list.append(__a ) with open(f"/{file_root}.txt" , """w""" ) as outfile: outfile.write("""\n""".join(line for line in annos_list ) ) def __magic_name__ ( __a : str , __a : str ): '''simple docstring''' UpperCamelCase__ = [] UpperCamelCase__ = [] for label_file in glob.glob(os.path.join(__a , """*.txt""" ) ): UpperCamelCase__ = label_file.split(os.sep )[-1].rsplit(""".""" , 1 )[0] with open(__a ) as in_file: UpperCamelCase__ = in_file.readlines() UpperCamelCase__ = os.path.join(__a , f"{label_name}.jpg" ) UpperCamelCase__ = [] for obj_list in obj_lists: UpperCamelCase__ = obj_list.rstrip("""\n""" ).split(""" """ ) boxes.append( [ int(obj[0] ), float(obj[1] ), float(obj[2] ), float(obj[3] ), float(obj[4] ), ] ) if not boxes: continue img_paths.append(__a ) labels.append(__a ) return img_paths, labels def __magic_name__ ( __a : list , __a : list , __a : int = 1 ): '''simple docstring''' UpperCamelCase__ = [] UpperCamelCase__ = [] UpperCamelCase__ = [] for idx in range(len(__a ) ): UpperCamelCase__ = [] UpperCamelCase__ = img_list[idx] path_list.append(__a ) UpperCamelCase__ = anno_list[idx] UpperCamelCase__ = cva.imread(__a ) if flip_type == 1: UpperCamelCase__ = cva.flip(__a , __a ) for bbox in img_annos: UpperCamelCase__ = 1 - bbox[1] new_annos.append([bbox[0], x_center_new, bbox[2], bbox[3], bbox[4]] ) elif flip_type == 0: UpperCamelCase__ = cva.flip(__a , __a ) for bbox in img_annos: UpperCamelCase__ = 1 - bbox[2] new_annos.append([bbox[0], bbox[1], y_center_new, bbox[3], bbox[4]] ) new_annos_lists.append(__a ) new_imgs_list.append(__a ) return new_imgs_list, new_annos_lists, path_list def __magic_name__ ( __a : int = 32 ): '''simple docstring''' assert number_char > 1, "The number of character should greater than 1" UpperCamelCase__ = ascii_lowercase + digits return "".join(random.choice(__a ) for _ in range(__a ) ) if __name__ == "__main__": main() print('''DONE ✅''')
513
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCamelCase_ = { '''configuration_instructblip''': [ '''INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''InstructBlipConfig''', '''InstructBlipQFormerConfig''', '''InstructBlipVisionConfig''', ], '''processing_instructblip''': ['''InstructBlipProcessor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase_ = [ '''INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''InstructBlipQFormerModel''', '''InstructBlipPreTrainedModel''', '''InstructBlipForConditionalGeneration''', '''InstructBlipVisionModel''', ] if TYPE_CHECKING: from .configuration_instructblip import ( INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, InstructBlipConfig, InstructBlipQFormerConfig, InstructBlipVisionConfig, ) from .processing_instructblip import InstructBlipProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_instructblip import ( INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST, InstructBlipForConditionalGeneration, InstructBlipPreTrainedModel, InstructBlipQFormerModel, InstructBlipVisionModel, ) else: import sys lowerCamelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
513
1
import math def __lowercase ( _SCREAMING_SNAKE_CASE ) -> list: '''simple docstring''' SCREAMING_SNAKE_CASE = [True] * n SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = True for i in range(3 , int(n**0.5 + 1 ) , 2 ): SCREAMING_SNAKE_CASE = i * 2 while index < n: SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = index + i SCREAMING_SNAKE_CASE = [2] for i in range(3 , _SCREAMING_SNAKE_CASE , 2 ): if is_prime[i]: primes.append(_SCREAMING_SNAKE_CASE ) return primes def __lowercase ( _SCREAMING_SNAKE_CASE = 99_99_66_66_33_33 ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = math.floor(math.sqrt(_SCREAMING_SNAKE_CASE ) ) + 1_00 SCREAMING_SNAKE_CASE = prime_sieve(_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = primes[prime_index] while (last_prime**2) <= limit: SCREAMING_SNAKE_CASE = primes[prime_index + 1] SCREAMING_SNAKE_CASE = last_prime**2 SCREAMING_SNAKE_CASE = next_prime**2 # Get numbers divisible by lps(current) SCREAMING_SNAKE_CASE = lower_bound + last_prime while upper_bound > current <= limit: matches_sum += current current += last_prime # Reset the upper_bound while (upper_bound - next_prime) > limit: upper_bound -= next_prime # Add the numbers divisible by ups(current) SCREAMING_SNAKE_CASE = upper_bound - next_prime while current > lower_bound: matches_sum += current current -= next_prime # Remove the numbers divisible by both ups and lps SCREAMING_SNAKE_CASE = 0 while upper_bound > current <= limit: if current <= lower_bound: # Increment the current number current += last_prime * next_prime continue if current > limit: break # Remove twice since it was added by both ups and lps matches_sum -= current * 2 # Increment the current number current += last_prime * next_prime # Setup for next pair SCREAMING_SNAKE_CASE = next_prime prime_index += 1 return matches_sum if __name__ == "__main__": print(solution())
116
import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from torchvision import transforms from transformers import BitImageProcessor, FocalNetConfig, FocalNetForImageClassification from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, PILImageResampling def __lowercase ( _SCREAMING_SNAKE_CASE ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = [2, 2, 6, 2] if """tiny""" in model_name else [2, 2, 18, 2] SCREAMING_SNAKE_CASE = True if """large""" in model_name or """huge""" in model_name else False SCREAMING_SNAKE_CASE = True if """large""" in model_name or """huge""" in model_name else False SCREAMING_SNAKE_CASE = True if """large""" in model_name or """huge""" in model_name else False if "large" in model_name or "xlarge" in model_name or "huge" in model_name: if "fl3" in model_name: SCREAMING_SNAKE_CASE = [3, 3, 3, 3] SCREAMING_SNAKE_CASE = [5, 5, 5, 5] elif "fl4" in model_name: SCREAMING_SNAKE_CASE = [4, 4, 4, 4] SCREAMING_SNAKE_CASE = [3, 3, 3, 3] if "tiny" in model_name or "small" in model_name or "base" in model_name: SCREAMING_SNAKE_CASE = [3, 3, 3, 3] if "lrf" in model_name: SCREAMING_SNAKE_CASE = [3, 3, 3, 3] else: SCREAMING_SNAKE_CASE = [2, 2, 2, 2] if "tiny" in model_name: SCREAMING_SNAKE_CASE = 96 elif "small" in model_name: SCREAMING_SNAKE_CASE = 96 elif "base" in model_name: SCREAMING_SNAKE_CASE = 1_28 elif "large" in model_name: SCREAMING_SNAKE_CASE = 1_92 elif "xlarge" in model_name: SCREAMING_SNAKE_CASE = 2_56 elif "huge" in model_name: SCREAMING_SNAKE_CASE = 3_52 # set label information SCREAMING_SNAKE_CASE = """huggingface/label-files""" if "large" in model_name or "huge" in model_name: SCREAMING_SNAKE_CASE = """imagenet-22k-id2label.json""" else: SCREAMING_SNAKE_CASE = """imagenet-1k-id2label.json""" SCREAMING_SNAKE_CASE = json.load(open(hf_hub_download(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , repo_type="""dataset""" ) , """r""" ) ) SCREAMING_SNAKE_CASE = {int(_SCREAMING_SNAKE_CASE ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE = {v: k for k, v in idalabel.items()} SCREAMING_SNAKE_CASE = FocalNetConfig( embed_dim=_SCREAMING_SNAKE_CASE , depths=_SCREAMING_SNAKE_CASE , focal_levels=_SCREAMING_SNAKE_CASE , focal_windows=_SCREAMING_SNAKE_CASE , use_conv_embed=_SCREAMING_SNAKE_CASE , idalabel=_SCREAMING_SNAKE_CASE , labelaid=_SCREAMING_SNAKE_CASE , use_post_layernorm=_SCREAMING_SNAKE_CASE , use_layerscale=_SCREAMING_SNAKE_CASE , ) return config def __lowercase ( _SCREAMING_SNAKE_CASE ) -> Optional[int]: '''simple docstring''' if "patch_embed.proj" in name: SCREAMING_SNAKE_CASE = name.replace("""patch_embed.proj""" , """embeddings.patch_embeddings.projection""" ) if "patch_embed.norm" in name: SCREAMING_SNAKE_CASE = name.replace("""patch_embed.norm""" , """embeddings.norm""" ) if "layers" in name: SCREAMING_SNAKE_CASE = """encoder.""" + name if "encoder.layers" in name: SCREAMING_SNAKE_CASE = name.replace("""encoder.layers""" , """encoder.stages""" ) if "downsample.proj" in name: SCREAMING_SNAKE_CASE = name.replace("""downsample.proj""" , """downsample.projection""" ) if "blocks" in name: SCREAMING_SNAKE_CASE = name.replace("""blocks""" , """layers""" ) if "modulation.f.weight" in name or "modulation.f.bias" in name: SCREAMING_SNAKE_CASE = name.replace("""modulation.f""" , """modulation.projection_in""" ) if "modulation.h.weight" in name or "modulation.h.bias" in name: SCREAMING_SNAKE_CASE = name.replace("""modulation.h""" , """modulation.projection_context""" ) if "modulation.proj.weight" in name or "modulation.proj.bias" in name: SCREAMING_SNAKE_CASE = name.replace("""modulation.proj""" , """modulation.projection_out""" ) if name == "norm.weight": SCREAMING_SNAKE_CASE = """layernorm.weight""" if name == "norm.bias": SCREAMING_SNAKE_CASE = """layernorm.bias""" if "head" in name: SCREAMING_SNAKE_CASE = name.replace("""head""" , """classifier""" ) else: SCREAMING_SNAKE_CASE = """focalnet.""" + name return name def __lowercase ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=False ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = { """focalnet-tiny""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_tiny_srf.pth""", """focalnet-tiny-lrf""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_tiny_lrf.pth""", """focalnet-small""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_small_srf.pth""", """focalnet-small-lrf""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_small_lrf.pth""", """focalnet-base""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_base_srf.pth""", """focalnet-base-lrf""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_base_lrf.pth""", """focalnet-large-lrf-fl3""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_large_lrf_384.pth""", """focalnet-large-lrf-fl4""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_large_lrf_384_fl4.pth""", """focalnet-xlarge-lrf-fl3""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_xlarge_lrf_384.pth""", """focalnet-xlarge-lrf-fl4""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_xlarge_lrf_384_fl4.pth""", } # fmt: on SCREAMING_SNAKE_CASE = model_name_to_url[model_name] print("""Checkpoint URL: """ , _SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE = torch.hub.load_state_dict_from_url(_SCREAMING_SNAKE_CASE , map_location="""cpu""" )["""model"""] # rename keys for key in state_dict.copy().keys(): SCREAMING_SNAKE_CASE = state_dict.pop(_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE = val SCREAMING_SNAKE_CASE = get_focalnet_config(_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE = FocalNetForImageClassification(_SCREAMING_SNAKE_CASE ) model.eval() # load state dict model.load_state_dict(_SCREAMING_SNAKE_CASE ) # verify conversion SCREAMING_SNAKE_CASE = """http://images.cocodataset.org/val2017/000000039769.jpg""" SCREAMING_SNAKE_CASE = BitImageProcessor( do_resize=_SCREAMING_SNAKE_CASE , size={"""shortest_edge""": 2_56} , resample=PILImageResampling.BILINEAR , do_center_crop=_SCREAMING_SNAKE_CASE , crop_size=2_24 , do_normalize=_SCREAMING_SNAKE_CASE , image_mean=_SCREAMING_SNAKE_CASE , image_std=_SCREAMING_SNAKE_CASE , ) SCREAMING_SNAKE_CASE = Image.open(requests.get(_SCREAMING_SNAKE_CASE , stream=_SCREAMING_SNAKE_CASE ).raw ) SCREAMING_SNAKE_CASE = processor(images=_SCREAMING_SNAKE_CASE , return_tensors="""pt""" ) SCREAMING_SNAKE_CASE = transforms.Compose( [ transforms.Resize(2_56 ), transforms.CenterCrop(2_24 ), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406] , std=[0.229, 0.224, 0.225] ), ] ) SCREAMING_SNAKE_CASE = image_transforms(_SCREAMING_SNAKE_CASE ).unsqueeze(0 ) # verify pixel_values assert torch.allclose(inputs.pixel_values , _SCREAMING_SNAKE_CASE , atol=1E-4 ) SCREAMING_SNAKE_CASE = model(**_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE = outputs.logits.argmax(-1 ).item() print("""Predicted class:""" , model.config.idalabel[predicted_class_idx] ) print("""First values of logits:""" , outputs.logits[0, :3] ) if model_name == "focalnet-tiny": SCREAMING_SNAKE_CASE = torch.tensor([0.2_166, -0.4_368, 0.2_191] ) elif model_name == "focalnet-tiny-lrf": SCREAMING_SNAKE_CASE = torch.tensor([1.1_669, 0.0_125, -0.1_695] ) elif model_name == "focalnet-small": SCREAMING_SNAKE_CASE = torch.tensor([0.4_917, -0.0_430, 0.1_341] ) elif model_name == "focalnet-small-lrf": SCREAMING_SNAKE_CASE = torch.tensor([-0.2_588, -0.5_342, -0.2_331] ) elif model_name == "focalnet-base": SCREAMING_SNAKE_CASE = torch.tensor([-0.1_655, -0.4_090, -0.1_730] ) elif model_name == "focalnet-base-lrf": SCREAMING_SNAKE_CASE = torch.tensor([0.5_306, -0.0_483, -0.3_928] ) assert torch.allclose(outputs.logits[0, :3] , _SCREAMING_SNAKE_CASE , atol=1E-4 ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: print(F"""Saving model and processor of {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(_SCREAMING_SNAKE_CASE ) processor.save_pretrained(_SCREAMING_SNAKE_CASE ) if push_to_hub: print(F"""Pushing model and processor of {model_name} to the hub...""" ) model.push_to_hub(F"""{model_name}""" ) processor.push_to_hub(F"""{model_name}""" ) if __name__ == "__main__": SCREAMING_SNAKE_CASE_ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default="""focalnet-tiny""", type=str, help="""Name of the FocalNet model you'd like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether to push the model and processor to the hub.""", ) SCREAMING_SNAKE_CASE_ = parser.parse_args() convert_focalnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
116
1
import numpy as np from nltk.translate import meteor_score import datasets from datasets.config import importlib_metadata, version UpperCAmelCase : List[str] = version.parse(importlib_metadata.version("""nltk""")) if NLTK_VERSION >= version.Version("""3.6.4"""): from nltk import word_tokenize UpperCAmelCase : int = """\ @inproceedings{banarjee2005, title = {{METEOR}: An Automatic Metric for {MT} Evaluation with Improved Correlation with Human Judgments}, author = {Banerjee, Satanjeev and Lavie, Alon}, booktitle = {Proceedings of the {ACL} Workshop on Intrinsic and Extrinsic Evaluation Measures for Machine Translation and/or Summarization}, month = jun, year = {2005}, address = {Ann Arbor, Michigan}, publisher = {Association for Computational Linguistics}, url = {https://www.aclweb.org/anthology/W05-0909}, pages = {65--72}, } """ UpperCAmelCase : Optional[Any] = """\ METEOR, an automatic metric for machine translation evaluation that is based on a generalized concept of unigram matching between the machine-produced translation and human-produced reference translations. Unigrams can be matched based on their surface forms, stemmed forms, and meanings; furthermore, METEOR can be easily extended to include more advanced matching strategies. Once all generalized unigram matches between the two strings have been found, METEOR computes a score for this matching using a combination of unigram-precision, unigram-recall, and a measure of fragmentation that is designed to directly capture how well-ordered the matched words in the machine translation are in relation to the reference. METEOR gets an R correlation value of 0.347 with human evaluation on the Arabic data and 0.331 on the Chinese data. This is shown to be an improvement on using simply unigram-precision, unigram-recall and their harmonic F1 combination. """ UpperCAmelCase : Optional[Any] = """ Computes METEOR score of translated segments against one or more references. Args: predictions: list of predictions to score. Each prediction should be a string with tokens separated by spaces. references: list of reference for each prediction. Each reference should be a string with tokens separated by spaces. alpha: Parameter for controlling relative weights of precision and recall. default: 0.9 beta: Parameter for controlling shape of penalty as a function of fragmentation. default: 3 gamma: Relative weight assigned to fragmentation penalty. default: 0.5 Returns: 'meteor': meteor score. Examples: >>> meteor = datasets.load_metric('meteor') >>> predictions = [\"It is a guide to action which ensures that the military always obeys the commands of the party\"] >>> references = [\"It is a guide to action that ensures that the military will forever heed Party commands\"] >>> results = meteor.compute(predictions=predictions, references=references) >>> print(round(results[\"meteor\"], 4)) 0.6944 """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class __lowerCAmelCase ( datasets.Metric): def _lowercase ( self ) -> int: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Value("string" , id="sequence" ), } ) , codebase_urls=["https://github.com/nltk/nltk/blob/develop/nltk/translate/meteor_score.py"] , reference_urls=[ "https://www.nltk.org/api/nltk.translate.html#module-nltk.translate.meteor_score", "https://en.wikipedia.org/wiki/METEOR", ] , ) def _lowercase ( self , lowerCAmelCase__ ) -> str: '''simple docstring''' import nltk nltk.download("wordnet" ) if NLTK_VERSION >= version.Version("3.6.5" ): nltk.download("punkt" ) if NLTK_VERSION >= version.Version("3.6.6" ): nltk.download("omw-1.4" ) def _lowercase ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__=0.9 , lowerCAmelCase__=3 , lowerCAmelCase__=0.5 ) -> int: '''simple docstring''' if NLTK_VERSION >= version.Version("3.6.5" ): a__ : Tuple =[ meteor_score.single_meteor_score( word_tokenize(lowerCAmelCase__ ) , word_tokenize(lowerCAmelCase__ ) , alpha=lowerCAmelCase__ , beta=lowerCAmelCase__ , gamma=lowerCAmelCase__ ) for ref, pred in zip(lowerCAmelCase__ , lowerCAmelCase__ ) ] else: a__ : Dict =[ meteor_score.single_meteor_score(lowerCAmelCase__ , lowerCAmelCase__ , alpha=lowerCAmelCase__ , beta=lowerCAmelCase__ , gamma=lowerCAmelCase__ ) for ref, pred in zip(lowerCAmelCase__ , lowerCAmelCase__ ) ] return {"meteor": np.mean(lowerCAmelCase__ )}
563
from itertools import count def _A ( SCREAMING_SNAKE_CASE : int = 50 ): """simple docstring""" a__ : Union[str, Any] =[1] * min_block_length for n in count(SCREAMING_SNAKE_CASE ): fill_count_functions.append(1 ) for block_length in range(SCREAMING_SNAKE_CASE , n + 1 ): for block_start in range(n - block_length ): fill_count_functions[n] += fill_count_functions[ n - block_start - block_length - 1 ] fill_count_functions[n] += 1 if fill_count_functions[n] > 1_000_000: break return n if __name__ == "__main__": print(F"""{solution() = }""")
563
1
import os from itertools import chain from random import randrange, shuffle import pytest from .sola import PokerHand UpperCamelCase_ = ( "4S 3H 2C 7S 5H", "9D 8H 2C 6S 7H", "2D 6D 9D TH 7D", "TC 8C 2S JH 6C", "JH 8S TH AH QH", "TS KS 5S 9S AC", "KD 6S 9D TH AD", "KS 8D 4D 9S 4S", # pair "8C 4S KH JS 4D", # pair "QH 8H KD JH 8S", # pair "KC 4H KS 2H 8D", # pair "KD 4S KC 3H 8S", # pair "AH 8S AS KC JH", # pair "3H 4C 4H 3S 2H", # 2 pairs "5S 5D 2C KH KH", # 2 pairs "3C KH 5D 5S KH", # 2 pairs "AS 3C KH AD KH", # 2 pairs "7C 7S 3S 7H 5S", # 3 of a kind "7C 7S KH 2H 7H", # 3 of a kind "AC KH QH AH AS", # 3 of a kind "2H 4D 3C AS 5S", # straight (low ace) "3C 5C 4C 2C 6H", # straight "6S 8S 7S 5H 9H", # straight "JS QS 9H TS KH", # straight "QC KH TS JS AH", # straight (high ace) "8C 9C 5C 3C TC", # flush "3S 8S 9S 5S KS", # flush "4C 5C 9C 8C KC", # flush "JH 8H AH KH QH", # flush "3D 2H 3H 2C 2D", # full house "2H 2C 3S 3H 3D", # full house "KH KC 3S 3H 3D", # full house "JC 6H JS JD JH", # 4 of a kind "JC 7H JS JD JH", # 4 of a kind "JC KH JS JD JH", # 4 of a kind "2S AS 4S 5S 3S", # straight flush (low ace) "2D 6D 3D 4D 5D", # straight flush "5C 6C 3C 7C 4C", # straight flush "JH 9H TH KH QH", # straight flush "JH AH TH KH QH", # royal flush (high ace straight flush) ) UpperCamelCase_ = ( ("2H 3H 4H 5H 6H", "KS AS TS QS JS", "Loss"), ("2H 3H 4H 5H 6H", "AS AD AC AH JD", "Win"), ("AS AH 2H AD AC", "JS JD JC JH 3D", "Win"), ("2S AH 2H AS AC", "JS JD JC JH AD", "Loss"), ("2S AH 2H AS AC", "2H 3H 5H 6H 7H", "Win"), ("AS 3S 4S 8S 2S", "2H 3H 5H 6H 7H", "Win"), ("2H 3H 5H 6H 7H", "2S 3H 4H 5S 6C", "Win"), ("2S 3H 4H 5S 6C", "3D 4C 5H 6H 2S", "Tie"), ("2S 3H 4H 5S 6C", "AH AC 5H 6H AS", "Win"), ("2S 2H 4H 5S 4C", "AH AC 5H 6H AS", "Loss"), ("2S 2H 4H 5S 4C", "AH AC 5H 6H 7S", "Win"), ("6S AD 7H 4S AS", "AH AC 5H 6H 7S", "Loss"), ("2S AH 4H 5S KC", "AH AC 5H 6H 7S", "Loss"), ("2S 3H 6H 7S 9C", "7H 3C TH 6H 9S", "Loss"), ("4S 5H 6H TS AC", "3S 5H 6H TS AC", "Win"), ("2S AH 4H 5S 6C", "AD 4C 5H 6H 2C", "Tie"), ("AS AH 3H AD AC", "AS AH 2H AD AC", "Win"), ("AH AC 5H 5C QS", "AH AC 5H 5C KS", "Loss"), ("AH AC 5H 5C QS", "KH KC 5H 5C QS", "Win"), ("7C 7S KH 2H 7H", "3C 3S AH 2H 3H", "Win"), ("3C 3S AH 2H 3H", "7C 7S KH 2H 7H", "Loss"), ("6H 5H 4H 3H 2H", "5H 4H 3H 2H AH", "Win"), ("5H 4H 3H 2H AH", "5H 4H 3H 2H AH", "Tie"), ("5H 4H 3H 2H AH", "6H 5H 4H 3H 2H", "Loss"), ("AH AD KS KC AC", "AH KD KH AC KC", "Win"), ("2H 4D 3C AS 5S", "2H 4D 3C 6S 5S", "Loss"), ("2H 3S 3C 3H 2S", "3S 3C 2S 2H 2D", "Win"), ("4D 6D 5D 2D JH", "3S 8S 3H TC KH", "Loss"), ("4S 6C 8S 3S 7S", "AD KS 2D 7D 7C", "Loss"), ("6S 4C 7H 8C 3H", "5H JC AH 9D 9C", "Loss"), ("9D 9H JH TC QH", "3C 2S JS 5C 7H", "Win"), ("2H TC 8S AD 9S", "4H TS 7H 2C 5C", "Win"), ("9D 3S 2C 7S 7C", "JC TD 3C TC 9H", "Loss"), ) UpperCamelCase_ = ( ("2H 3H 4H 5H 6H", True), ("AS AH 2H AD AC", False), ("2H 3H 5H 6H 7H", True), ("KS AS TS QS JS", True), ("8H 9H QS JS TH", False), ("AS 3S 4S 8S 2S", True), ) UpperCamelCase_ = ( ("2H 3H 4H 5H 6H", True), ("AS AH 2H AD AC", False), ("2H 3H 5H 6H 7H", False), ("KS AS TS QS JS", True), ("8H 9H QS JS TH", True), ) UpperCamelCase_ = ( ("2H 4D 3C AS 5S", True, [5, 4, 3, 2, 14]), ("2H 5D 3C AS 5S", False, [14, 5, 5, 3, 2]), ("JH QD KC AS TS", False, [14, 13, 12, 11, 10]), ("9D 3S 2C 7S 7C", False, [9, 7, 7, 3, 2]), ) UpperCamelCase_ = ( ("JH AH TH KH QH", 0), ("JH 9H TH KH QH", 0), ("JC KH JS JD JH", 7), ("KH KC 3S 3H 3D", 6), ("8C 9C 5C 3C TC", 0), ("JS QS 9H TS KH", 0), ("7C 7S KH 2H 7H", 3), ("3C KH 5D 5S KH", 2), ("QH 8H KD JH 8S", 1), ("2D 6D 9D TH 7D", 0), ) UpperCamelCase_ = ( ("JH AH TH KH QH", 23), ("JH 9H TH KH QH", 22), ("JC KH JS JD JH", 21), ("KH KC 3S 3H 3D", 20), ("8C 9C 5C 3C TC", 19), ("JS QS 9H TS KH", 18), ("7C 7S KH 2H 7H", 17), ("3C KH 5D 5S KH", 16), ("QH 8H KD JH 8S", 15), ("2D 6D 9D TH 7D", 14), ) def lowerCamelCase_ ( ) -> Dict: '''simple docstring''' UpperCAmelCase_ , UpperCAmelCase_ : Any = randrange(len(__lowerCAmelCase ) ), randrange(len(__lowerCAmelCase ) ) UpperCAmelCase_ : List[str] = ["""Loss""", """Tie""", """Win"""][(play >= oppo) + (play > oppo)] UpperCAmelCase_ , UpperCAmelCase_ : Optional[int] = SORTED_HANDS[play], SORTED_HANDS[oppo] return hand, other, expected def lowerCamelCase_ ( _a : Dict = 100 ) -> Optional[Any]: '''simple docstring''' return (generate_random_hand() for _ in range(__lowerCAmelCase )) @pytest.mark.parametrize("""hand, expected""" , __lowerCAmelCase ) def lowerCamelCase_ ( _a : Any , _a : Optional[int] ) -> Optional[Any]: '''simple docstring''' assert PokerHand(__lowerCAmelCase )._is_flush() == expected @pytest.mark.parametrize("""hand, expected""" , __lowerCAmelCase ) def lowerCamelCase_ ( _a : Dict , _a : Optional[Any] ) -> Optional[int]: '''simple docstring''' assert PokerHand(__lowerCAmelCase )._is_straight() == expected @pytest.mark.parametrize("""hand, expected, card_values""" , __lowerCAmelCase ) def lowerCamelCase_ ( _a : List[str] , _a : Any , _a : List[Any] ) -> int: '''simple docstring''' UpperCAmelCase_ : Tuple = PokerHand(__lowerCAmelCase ) assert player._is_five_high_straight() == expected assert player._card_values == card_values @pytest.mark.parametrize("""hand, expected""" , __lowerCAmelCase ) def lowerCamelCase_ ( _a : List[Any] , _a : Tuple ) -> int: '''simple docstring''' assert PokerHand(__lowerCAmelCase )._is_same_kind() == expected @pytest.mark.parametrize("""hand, expected""" , __lowerCAmelCase ) def lowerCamelCase_ ( _a : Any , _a : Optional[Any] ) -> Optional[int]: '''simple docstring''' assert PokerHand(__lowerCAmelCase )._hand_type == expected @pytest.mark.parametrize("""hand, other, expected""" , __lowerCAmelCase ) def lowerCamelCase_ ( _a : Any , _a : List[Any] , _a : Optional[int] ) -> Dict: '''simple docstring''' assert PokerHand(__lowerCAmelCase ).compare_with(PokerHand(__lowerCAmelCase ) ) == expected @pytest.mark.parametrize("""hand, other, expected""" , generate_random_hands() ) def lowerCamelCase_ ( _a : Any , _a : List[Any] , _a : Dict ) -> List[str]: '''simple docstring''' assert PokerHand(__lowerCAmelCase ).compare_with(PokerHand(__lowerCAmelCase ) ) == expected def lowerCamelCase_ ( ) -> Dict: '''simple docstring''' UpperCAmelCase_ : Dict = [PokerHand(__lowerCAmelCase ) for hand in SORTED_HANDS] UpperCAmelCase_ : str = poker_hands.copy() shuffle(__lowerCAmelCase ) UpperCAmelCase_ : Tuple = chain(sorted(__lowerCAmelCase ) ) for index, hand in enumerate(__lowerCAmelCase ): assert hand == poker_hands[index] def lowerCamelCase_ ( ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase_ : Optional[int] = [PokerHand("""2D AC 3H 4H 5S""" ), PokerHand("""2S 3H 4H 5S 6C""" )] pokerhands.sort(reverse=__lowerCAmelCase ) assert pokerhands[0].__str__() == "2S 3H 4H 5S 6C" def lowerCamelCase_ ( ) -> List[Any]: '''simple docstring''' UpperCAmelCase_ : str = PokerHand("""2C 4S AS 3D 5C""" ) UpperCAmelCase_ : str = True UpperCAmelCase_ : Optional[int] = [5, 4, 3, 2, 14] for _ in range(10 ): assert pokerhand._is_five_high_straight() == expected assert pokerhand._card_values == expected_card_values def lowerCamelCase_ ( ) -> Tuple: '''simple docstring''' UpperCAmelCase_ : Optional[Any] = 0 UpperCAmelCase_ : Tuple = os.path.abspath(os.path.dirname(__lowerCAmelCase ) ) UpperCAmelCase_ : int = os.path.join(__lowerCAmelCase , """poker_hands.txt""" ) with open(__lowerCAmelCase ) as file_hand: for line in file_hand: UpperCAmelCase_ : Union[str, Any] = line[:14].strip() UpperCAmelCase_ : Optional[int] = line[15:].strip() UpperCAmelCase_ , UpperCAmelCase_ : List[str] = PokerHand(__lowerCAmelCase ), PokerHand(__lowerCAmelCase ) UpperCAmelCase_ : Dict = player.compare_with(__lowerCAmelCase ) if output == "Win": answer += 1 assert answer == 376
712
def lowerCamelCase_ ( _a : list ): '''simple docstring''' for i in range(len(_a ) - 1 , 0 , -1 ): UpperCAmelCase_ : Optional[int] = False for j in range(_a , 0 , -1 ): if unsorted[j] < unsorted[j - 1]: UpperCAmelCase_ , UpperCAmelCase_ : int = unsorted[j - 1], unsorted[j] UpperCAmelCase_ : int = True for j in range(_a ): if unsorted[j] > unsorted[j + 1]: UpperCAmelCase_ , UpperCAmelCase_ : Any = unsorted[j + 1], unsorted[j] UpperCAmelCase_ : str = True if not swapped: break return unsorted if __name__ == "__main__": import doctest doctest.testmod() UpperCamelCase_ = input('''Enter numbers separated by a comma:\n''').strip() UpperCamelCase_ = [int(item) for item in user_input.split(''',''')] print(F"{cocktail_shaker_sort(unsorted) = }")
322
0
'''simple docstring''' import argparse from collections import defaultdict def lowercase_ ( __A : Optional[Any] , __A : Optional[int] , __A : str , __A : Tuple , __A : int ) -> Dict: """simple docstring""" lowercase : int =F'{file}_{class_name}_{test_name}' done_test[_id] += 1 with open(__A , '''r''' ) as f: lowercase : List[Any] =f.readlines() lowercase : Tuple =F'class {class_name}(' lowercase : Optional[Any] =F'{4 * " "}def {test_name}(' lowercase : Union[str, Any] =F'{8 * " "}{correct_line.split()[0]}' lowercase : List[Any] =F'{1_6 * " "}{correct_line.split()[0]}' lowercase : int =False lowercase : Dict =False lowercase : Optional[Any] =False lowercase : str =False lowercase : str =0 lowercase : Optional[Any] =0 lowercase : List[Any] =[] for line in lines: if line.startswith(__A ): lowercase : List[str] =True elif in_class and line.startswith(__A ): lowercase : int =True elif in_class and in_func and (line.startswith(__A ) or line.startswith(__A )): lowercase : str =len(line.split(correct_line.split()[0] )[0] ) count += 1 if count == done_test[_id]: lowercase : Dict =True if in_class and in_func and in_line: if ")" not in line: continue else: lowercase : Any =True if in_class and in_func and in_line and insert_line: new_lines.append(F'{spaces * " "}{correct_line}' ) lowercase : List[str] =False else: new_lines.append(__A ) with open(__A , '''w''' ) as f: for line in new_lines: f.write(__A ) def lowercase_ ( __A : Any , __A : List[Any]=None ) -> str: """simple docstring""" if fail is not None: with open(__A , '''r''' ) as f: lowercase : Optional[Any] ={l.strip() for l in f.readlines()} else: lowercase : int =None with open(__A , '''r''' ) as f: lowercase : Union[str, Any] =f.readlines() lowercase : Union[str, Any] =defaultdict(__A ) for line in correct_lines: lowercase , lowercase , lowercase , lowercase : Optional[int] =line.split(''';''' ) if test_failures is None or "::".join([file, class_name, test_name] ) in test_failures: overwrite_file(__A , __A , __A , __A , __A ) if __name__ == "__main__": SCREAMING_SNAKE_CASE = argparse.ArgumentParser() parser.add_argument('--correct_filename', help='filename of tests with expected result') parser.add_argument('--fail_filename', help='filename of test failures', type=str, default=None) SCREAMING_SNAKE_CASE = parser.parse_args() main(args.correct_filename, args.fail_filename)
94
'''simple docstring''' from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, is_valid_image, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL SCREAMING_SNAKE_CASE = logging.get_logger(__name__) def lowercase_ ( __A : Tuple ) -> List[List[ImageInput]]: """simple docstring""" if isinstance(__A , (list, tuple) ) and isinstance(videos[0] , (list, tuple) ) and is_valid_image(videos[0][0] ): return videos elif isinstance(__A , (list, tuple) ) and is_valid_image(videos[0] ): return [videos] elif is_valid_image(__A ): return [[videos]] raise ValueError(F'Could not make batched video from {videos}' ) class UpperCAmelCase_ ( __A ): """simple docstring""" UpperCamelCase_ = ['''pixel_values'''] def __init__( self : List[Any] , UpperCAmelCase : bool = True , UpperCAmelCase : Dict[str, int] = None , UpperCAmelCase : PILImageResampling = PILImageResampling.BILINEAR , UpperCAmelCase : bool = True , UpperCAmelCase : Dict[str, int] = None , UpperCAmelCase : bool = True , UpperCAmelCase : Union[int, float] = 1 / 255 , UpperCAmelCase : bool = True , UpperCAmelCase : Optional[Union[float, List[float]]] = None , UpperCAmelCase : Optional[Union[float, List[float]]] = None , **UpperCAmelCase : Tuple , ) -> None: '''simple docstring''' super().__init__(**UpperCAmelCase ) lowercase : Tuple =size if size is not None else {'''shortest_edge''': 224} lowercase : str =get_size_dict(UpperCAmelCase , default_to_square=UpperCAmelCase ) lowercase : List[str] =crop_size if crop_size is not None else {'''height''': 224, '''width''': 224} lowercase : List[Any] =get_size_dict(UpperCAmelCase , param_name='''crop_size''' ) lowercase : Tuple =do_resize lowercase : Any =size lowercase : Optional[Any] =do_center_crop lowercase : str =crop_size lowercase : Any =resample lowercase : List[Any] =do_rescale lowercase : Dict =rescale_factor lowercase : List[str] =do_normalize lowercase : Optional[int] =image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN lowercase : List[Any] =image_std if image_std is not None else IMAGENET_STANDARD_STD def A__ ( self : Dict , UpperCAmelCase : np.ndarray , UpperCAmelCase : Dict[str, int] , UpperCAmelCase : PILImageResampling = PILImageResampling.BILINEAR , UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase : List[str] , ) -> np.ndarray: '''simple docstring''' lowercase : str =get_size_dict(UpperCAmelCase , default_to_square=UpperCAmelCase ) if "shortest_edge" in size: lowercase : int =get_resize_output_image_size(UpperCAmelCase , size['''shortest_edge'''] , default_to_square=UpperCAmelCase ) elif "height" in size and "width" in size: lowercase : str =(size['''height'''], size['''width''']) else: raise ValueError(f'Size must have \'height\' and \'width\' or \'shortest_edge\' as keys. Got {size.keys()}' ) return resize(UpperCAmelCase , size=UpperCAmelCase , resample=UpperCAmelCase , data_format=UpperCAmelCase , **UpperCAmelCase ) def A__ ( self : Any , UpperCAmelCase : np.ndarray , UpperCAmelCase : Dict[str, int] , UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase : Optional[Any] , ) -> np.ndarray: '''simple docstring''' lowercase : List[str] =get_size_dict(UpperCAmelCase ) if "height" not in size or "width" not in size: raise ValueError(f'Size must have \'height\' and \'width\' as keys. Got {size.keys()}' ) return center_crop(UpperCAmelCase , size=(size['''height'''], size['''width''']) , data_format=UpperCAmelCase , **UpperCAmelCase ) def A__ ( self : int , UpperCAmelCase : np.ndarray , UpperCAmelCase : Union[int, float] , UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase : Tuple , ) -> List[Any]: '''simple docstring''' return rescale(UpperCAmelCase , scale=UpperCAmelCase , data_format=UpperCAmelCase , **UpperCAmelCase ) def A__ ( self : Any , UpperCAmelCase : np.ndarray , UpperCAmelCase : Union[float, List[float]] , UpperCAmelCase : Union[float, List[float]] , UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase : int , ) -> np.ndarray: '''simple docstring''' return normalize(UpperCAmelCase , mean=UpperCAmelCase , std=UpperCAmelCase , data_format=UpperCAmelCase , **UpperCAmelCase ) def A__ ( self : int , UpperCAmelCase : ImageInput , UpperCAmelCase : bool = None , UpperCAmelCase : Dict[str, int] = None , UpperCAmelCase : PILImageResampling = None , UpperCAmelCase : bool = None , UpperCAmelCase : Dict[str, int] = None , UpperCAmelCase : bool = None , UpperCAmelCase : float = None , UpperCAmelCase : bool = None , UpperCAmelCase : Optional[Union[float, List[float]]] = None , UpperCAmelCase : Optional[Union[float, List[float]]] = None , UpperCAmelCase : Optional[ChannelDimension] = ChannelDimension.FIRST , ) -> np.ndarray: '''simple docstring''' if do_resize and size is None or resample is None: raise ValueError('''Size and resample must be specified if do_resize is True.''' ) if do_center_crop and crop_size is None: raise ValueError('''Crop size must be specified if do_center_crop is True.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) # All transformations expect numpy arrays. lowercase : Dict =to_numpy_array(UpperCAmelCase ) if do_resize: lowercase : Union[str, Any] =self.resize(image=UpperCAmelCase , size=UpperCAmelCase , resample=UpperCAmelCase ) if do_center_crop: lowercase : Optional[Any] =self.center_crop(UpperCAmelCase , size=UpperCAmelCase ) if do_rescale: lowercase : Dict =self.rescale(image=UpperCAmelCase , scale=UpperCAmelCase ) if do_normalize: lowercase : int =self.normalize(image=UpperCAmelCase , mean=UpperCAmelCase , std=UpperCAmelCase ) lowercase : Optional[Any] =to_channel_dimension_format(UpperCAmelCase , UpperCAmelCase ) return image def A__ ( self : List[str] , UpperCAmelCase : ImageInput , UpperCAmelCase : bool = None , UpperCAmelCase : Dict[str, int] = None , UpperCAmelCase : PILImageResampling = None , UpperCAmelCase : bool = None , UpperCAmelCase : Dict[str, int] = None , UpperCAmelCase : bool = None , UpperCAmelCase : float = None , UpperCAmelCase : bool = None , UpperCAmelCase : Optional[Union[float, List[float]]] = None , UpperCAmelCase : Optional[Union[float, List[float]]] = None , UpperCAmelCase : Optional[Union[str, TensorType]] = None , UpperCAmelCase : ChannelDimension = ChannelDimension.FIRST , **UpperCAmelCase : Optional[int] , ) -> PIL.Image.Image: '''simple docstring''' lowercase : Any =do_resize if do_resize is not None else self.do_resize lowercase : Union[str, Any] =resample if resample is not None else self.resample lowercase : Tuple =do_center_crop if do_center_crop is not None else self.do_center_crop lowercase : List[Any] =do_rescale if do_rescale is not None else self.do_rescale lowercase : str =rescale_factor if rescale_factor is not None else self.rescale_factor lowercase : List[str] =do_normalize if do_normalize is not None else self.do_normalize lowercase : List[str] =image_mean if image_mean is not None else self.image_mean lowercase : Optional[int] =image_std if image_std is not None else self.image_std lowercase : Optional[Any] =size if size is not None else self.size lowercase : Any =get_size_dict(UpperCAmelCase , default_to_square=UpperCAmelCase ) lowercase : Union[str, Any] =crop_size if crop_size is not None else self.crop_size lowercase : Optional[int] =get_size_dict(UpperCAmelCase , param_name='''crop_size''' ) if not valid_images(UpperCAmelCase ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) lowercase : List[str] =make_batched(UpperCAmelCase ) lowercase : Union[str, Any] =[ [ self._preprocess_image( image=UpperCAmelCase , do_resize=UpperCAmelCase , size=UpperCAmelCase , resample=UpperCAmelCase , do_center_crop=UpperCAmelCase , crop_size=UpperCAmelCase , do_rescale=UpperCAmelCase , rescale_factor=UpperCAmelCase , do_normalize=UpperCAmelCase , image_mean=UpperCAmelCase , image_std=UpperCAmelCase , data_format=UpperCAmelCase , ) for img in video ] for video in videos ] lowercase : Dict ={'''pixel_values''': videos} return BatchFeature(data=UpperCAmelCase , tensor_type=UpperCAmelCase )
94
1
import fire from utils import calculate_rouge, save_json def __A ( _lowercase , _lowercase , _lowercase=None , **_lowercase ): '''simple docstring''' _A = [x.strip() for x in open(_lowercase ).readlines()] _A = [x.strip() for x in open(_lowercase ).readlines()][: len(_lowercase )] _A = calculate_rouge(_lowercase , _lowercase , **_lowercase ) if save_path is not None: save_json(_lowercase , _lowercase , indent=_lowercase ) return metrics # these print nicely if __name__ == "__main__": fire.Fire(calculate_rouge_path)
700
import os import warnings from typing import List, Optional from ...tokenization_utils_base import BatchEncoding from ...utils import logging from .configuration_rag import RagConfig __A = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE : """simple docstring""" def __init__( self: int , __A: Optional[int] , __A: Optional[Any] ) -> str: _A = question_encoder _A = generator _A = self.question_encoder def __A ( self: Optional[int] , __A: Union[str, Any] ) -> Dict: if os.path.isfile(__A ): raise ValueError(f"""Provided path ({save_directory}) should be a directory, not a file""" ) os.makedirs(__A , exist_ok=__A ) _A = os.path.join(__A , '''question_encoder_tokenizer''' ) _A = os.path.join(__A , '''generator_tokenizer''' ) self.question_encoder.save_pretrained(__A ) self.generator.save_pretrained(__A ) @classmethod def __A ( cls: Optional[Any] , __A: List[str] , **__A: int ) -> Any: # dynamically import AutoTokenizer from ..auto.tokenization_auto import AutoTokenizer _A = kwargs.pop('''config''' , __A ) if config is None: _A = RagConfig.from_pretrained(__A ) _A = AutoTokenizer.from_pretrained( __A , config=config.question_encoder , subfolder='''question_encoder_tokenizer''' ) _A = AutoTokenizer.from_pretrained( __A , config=config.generator , subfolder='''generator_tokenizer''' ) return cls(question_encoder=__A , generator=__A ) def __call__( self: int , *__A: Optional[int] , **__A: List[str] ) -> int: return self.current_tokenizer(*__A , **__A ) def __A ( self: Dict , *__A: List[str] , **__A: List[str] ) -> Dict: return self.generator.batch_decode(*__A , **__A ) def __A ( self: Union[str, Any] , *__A: Tuple , **__A: List[str] ) -> Tuple: return self.generator.decode(*__A , **__A ) def __A ( self: Dict ) -> List[str]: _A = self.question_encoder def __A ( self: Union[str, Any] ) -> int: _A = self.generator def __A ( self: Dict , __A: List[str] , __A: Optional[List[str]] = None , __A: Optional[int] = None , __A: Optional[int] = None , __A: str = "longest" , __A: str = None , __A: bool = True , **__A: Tuple , ) -> BatchEncoding: warnings.warn( '''`prepare_seq2seq_batch` is deprecated and will be removed in version 5 of 🤗 Transformers. Use the ''' '''regular `__call__` method to prepare your inputs and the tokenizer under the `with_target_tokenizer` ''' '''context manager to prepare your targets. See the documentation of your specific tokenizer for more ''' '''details''' , __A , ) if max_length is None: _A = self.current_tokenizer.model_max_length _A = self( __A , add_special_tokens=__A , return_tensors=__A , max_length=__A , padding=__A , truncation=__A , **__A , ) if tgt_texts is None: return model_inputs # Process tgt_texts if max_target_length is None: _A = self.current_tokenizer.model_max_length _A = self( text_target=__A , add_special_tokens=__A , return_tensors=__A , padding=__A , max_length=__A , truncation=__A , **__A , ) _A = labels['''input_ids'''] return model_inputs
62
0
import argparse import json from typing import List from ltp import LTP from transformers.models.bert.tokenization_bert import BertTokenizer def UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ : List[str] ) -> Dict: # This defines a "chinese character" as anything in the CJK Unicode block: # https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block) # # Note that the CJK Unicode block is NOT all Japanese and Korean characters, # despite its name. The modern Korean Hangul alphabet is a different block, # as is Japanese Hiragana and Katakana. Those alphabets are used to write # space-separated words, so they are not treated specially and handled # like the all of the other languages. if ( (cp >= 0x4e00 and cp <= 0x9fff) or (cp >= 0x3400 and cp <= 0x4dbf) # or (cp >= 0x20000 and cp <= 0x2a6df) # or (cp >= 0x2a700 and cp <= 0x2b73f) # or (cp >= 0x2b740 and cp <= 0x2b81f) # or (cp >= 0x2b820 and cp <= 0x2ceaf) # or (cp >= 0xf900 and cp <= 0xfaff) or (cp >= 0x2f800 and cp <= 0x2fa1f) # ): # return True return False def UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ : str ) -> Optional[Any]: # word like '180' or '身高' or '神' for char in word: _lowercase = ord(SCREAMING_SNAKE_CASE_ ) if not _is_chinese_char(SCREAMING_SNAKE_CASE_ ): return 0 return 1 def UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ : List[str] ) -> int: _lowercase = set() for token in tokens: _lowercase = len(SCREAMING_SNAKE_CASE_ ) > 1 and is_chinese(SCREAMING_SNAKE_CASE_ ) if chinese_word: word_set.add(SCREAMING_SNAKE_CASE_ ) _lowercase = list(SCREAMING_SNAKE_CASE_ ) return word_list def UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : set() ) -> Optional[int]: if not chinese_word_set: return bert_tokens _lowercase = max([len(SCREAMING_SNAKE_CASE_ ) for w in chinese_word_set] ) _lowercase = bert_tokens _lowercase , _lowercase = 0, len(SCREAMING_SNAKE_CASE_ ) while start < end: _lowercase = True if is_chinese(bert_word[start] ): _lowercase = min(end - start , SCREAMING_SNAKE_CASE_ ) for i in range(SCREAMING_SNAKE_CASE_ , 1 , -1 ): _lowercase = """""".join(bert_word[start : start + i] ) if whole_word in chinese_word_set: for j in range(start + 1 , start + i ): _lowercase = """##""" + bert_word[j] _lowercase = start + i _lowercase = False break if single_word: start += 1 return bert_word def UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : LTP , SCREAMING_SNAKE_CASE_ : BertTokenizer ) -> Dict: _lowercase = [] for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) , 1_00 ): _lowercase = ltp_tokenizer.pipeline(lines[i : i + 1_00] , tasks=["""cws"""] ).cws _lowercase = [get_chinese_word(SCREAMING_SNAKE_CASE_ ) for r in res] ltp_res.extend(SCREAMING_SNAKE_CASE_ ) assert len(SCREAMING_SNAKE_CASE_ ) == len(SCREAMING_SNAKE_CASE_ ) _lowercase = [] for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) , 1_00 ): _lowercase = bert_tokenizer(lines[i : i + 1_00] , add_special_tokens=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , max_length=5_12 ) bert_res.extend(res["""input_ids"""] ) assert len(SCREAMING_SNAKE_CASE_ ) == len(SCREAMING_SNAKE_CASE_ ) _lowercase = [] for input_ids, chinese_word in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): _lowercase = [] for id in input_ids: _lowercase = bert_tokenizer._convert_id_to_token(SCREAMING_SNAKE_CASE_ ) input_tokens.append(SCREAMING_SNAKE_CASE_ ) _lowercase = add_sub_symbol(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) _lowercase = [] # We only save pos of chinese subwords start with ##, which mean is part of a whole word. for i, token in enumerate(SCREAMING_SNAKE_CASE_ ): if token[:2] == "##": _lowercase = token[2:] # save chinese tokens' pos if len(SCREAMING_SNAKE_CASE_ ) == 1 and _is_chinese_char(ord(SCREAMING_SNAKE_CASE_ ) ): ref_id.append(SCREAMING_SNAKE_CASE_ ) ref_ids.append(SCREAMING_SNAKE_CASE_ ) assert len(SCREAMING_SNAKE_CASE_ ) == len(SCREAMING_SNAKE_CASE_ ) return ref_ids def UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ : Optional[int] ) -> List[str]: # For Chinese (Ro)Bert, the best result is from : RoBERTa-wwm-ext (https://github.com/ymcui/Chinese-BERT-wwm) # If we want to fine-tune these model, we have to use same tokenizer : LTP (https://github.com/HIT-SCIR/ltp) with open(args.file_name , """r""" , encoding="""utf-8""" ) as f: _lowercase = f.readlines() _lowercase = [line.strip() for line in data if len(SCREAMING_SNAKE_CASE_ ) > 0 and not line.isspace()] # avoid delimiter like '\u2029' _lowercase = LTP(args.ltp ) # faster in GPU device _lowercase = BertTokenizer.from_pretrained(args.bert ) _lowercase = prepare_ref(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) with open(args.save_path , """w""" , encoding="""utf-8""" ) as f: _lowercase = [json.dumps(SCREAMING_SNAKE_CASE_ ) + """\n""" for ref in ref_ids] f.writelines(SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": A : Union[str, Any] = argparse.ArgumentParser(description='''prepare_chinese_ref''') parser.add_argument( '''--file_name''', required=False, type=str, default='''./resources/chinese-demo.txt''', help='''file need process, same as training data in lm''', ) parser.add_argument( '''--ltp''', required=False, type=str, default='''./resources/ltp''', help='''resources for LTP tokenizer, usually a path''', ) parser.add_argument( '''--bert''', required=False, type=str, default='''./resources/robert''', help='''resources for Bert tokenizer''', ) parser.add_argument( '''--save_path''', required=False, type=str, default='''./resources/ref.txt''', help='''path to save res''', ) A : Any = parser.parse_args() main(args)
287
def UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ : int = 2_00_00_00 ) -> int: _lowercase = [0 for i in range(n + 1 )] _lowercase = 1 _lowercase = 1 for i in range(2 , int(n**0.5 ) + 1 ): if primality_list[i] == 0: for j in range(i * i , n + 1 , SCREAMING_SNAKE_CASE_ ): _lowercase = 1 _lowercase = 0 for i in range(SCREAMING_SNAKE_CASE_ ): if primality_list[i] == 0: sum_of_primes += i return sum_of_primes if __name__ == "__main__": print(F'{solution() = }')
287
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging a_ = logging.get_logger(__name__) a_ = { '''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 __lowercase ( _UpperCAmelCase): """simple docstring""" _A : Optional[Any] = """trocr""" _A : Union[str, Any] = ["""past_key_values"""] _A : str = { """num_attention_heads""": """decoder_attention_heads""", """hidden_size""": """d_model""", """num_hidden_layers""": """decoder_layers""", } def __init__(self , lowercase__=5_02_65 , lowercase__=10_24 , lowercase__=12 , lowercase__=16 , lowercase__=40_96 , lowercase__="gelu" , lowercase__=5_12 , lowercase__=0.1 , lowercase__=0.0 , lowercase__=0.0 , lowercase__=2 , lowercase__=0.02 , lowercase__=0.0 , lowercase__=True , lowercase__=False , lowercase__=True , lowercase__=True , lowercase__=1 , lowercase__=0 , lowercase__=2 , **lowercase__ , ): snake_case_ : Union[str, Any] = vocab_size snake_case_ : int = d_model snake_case_ : Any = decoder_layers snake_case_ : str = decoder_attention_heads snake_case_ : Optional[Any] = decoder_ffn_dim snake_case_ : str = activation_function snake_case_ : Dict = max_position_embeddings snake_case_ : Dict = dropout snake_case_ : Optional[int] = attention_dropout snake_case_ : Dict = activation_dropout snake_case_ : Tuple = init_std snake_case_ : Optional[int] = decoder_layerdrop snake_case_ : Optional[int] = use_cache snake_case_ : Union[str, Any] = scale_embedding snake_case_ : Optional[Any] = use_learned_position_embeddings snake_case_ : Dict = layernorm_embedding super().__init__( pad_token_id=lowercase__ , bos_token_id=lowercase__ , eos_token_id=lowercase__ , decoder_start_token_id=lowercase__ , **lowercase__ , )
715
"""simple docstring""" import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, Features, Value from .base import TaskTemplate @dataclass(frozen=_UpperCAmelCase) class __lowercase ( _UpperCAmelCase): """simple docstring""" _A : str = field(default="""automatic-speech-recognition""" , metadata={"""include_in_asdict_even_if_is_default""": True}) _A : ClassVar[Features] = Features({"""audio""": Audio()}) _A : ClassVar[Features] = Features({"""transcription""": Value("""string""")}) _A : str = "audio" _A : str = "transcription" def __UpperCamelCase (self , lowercase__ ): if self.audio_column not in features: raise ValueError(f'Column {self.audio_column} is not present in features.' ) if not isinstance(features[self.audio_column] , lowercase__ ): raise ValueError(f'Column {self.audio_column} is not an Audio type.' ) snake_case_ : Optional[int] = copy.deepcopy(self ) snake_case_ : Tuple = self.input_schema.copy() snake_case_ : List[str] = features[self.audio_column] snake_case_ : Any = input_schema return task_template @property def __UpperCamelCase (self ): return {self.audio_column: "audio", self.transcription_column: "transcription"}
48
0
"""simple docstring""" from collections import Counter from pathlib import Path from typing import Optional, Tuple import yaml class _a ( yaml.SafeLoader): def __lowercase ( self : List[Any] , _lowercase : Dict ) -> Union[str, Any]: snake_case : Tuple = [self.constructed_objects[key_node] for key_node, _ in node.value] snake_case : List[Any] = [tuple(_lowercase ) if isinstance(_lowercase , _lowercase ) else key for key in keys] snake_case : List[str] = Counter(_lowercase ) snake_case : Any = [key for key in counter if counter[key] > 1] if duplicate_keys: raise TypeError(F'''Got duplicate yaml keys: {duplicate_keys}''' ) def __lowercase ( self : Any , _lowercase : Dict , _lowercase : Tuple=False ) -> Dict: snake_case : Optional[int] = super().construct_mapping(_lowercase , deep=_lowercase ) self._check_no_duplicates_on_constructed_node(_lowercase ) return mapping def __SCREAMING_SNAKE_CASE ( lowerCamelCase_: str ): """simple docstring""" snake_case : List[Any] = list(readme_content.splitlines() ) if full_content and full_content[0] == "---" and "---" in full_content[1:]: snake_case : List[str] = full_content[1:].index("---" ) + 1 snake_case : Optional[Any] = "\n".join(full_content[1:sep_idx] ) return yamlblock, "\n".join(full_content[sep_idx + 1 :] ) return None, "\n".join(lowerCamelCase_ ) class _a ( SCREAMING_SNAKE_CASE__): # class attributes __magic_name__ = {"""train_eval_index"""} # train-eval-index in the YAML metadata @classmethod def __lowercase ( cls : str , _lowercase : Path ) -> "DatasetMetadata": with open(_lowercase , encoding="utf-8" ) as readme_file: snake_case , snake_case : Tuple = _split_yaml_from_readme(readme_file.read() ) if yaml_string is not None: return cls.from_yaml_string(_lowercase ) else: return cls() def __lowercase ( self : Any , _lowercase : Path ) -> Tuple: if path.exists(): with open(_lowercase , encoding="utf-8" ) as readme_file: snake_case : Any = readme_file.read() else: snake_case : List[str] = None snake_case : Optional[Any] = self._to_readme(_lowercase ) with open(_lowercase , "w" , encoding="utf-8" ) as readme_file: readme_file.write(_lowercase ) def __lowercase ( self : Dict , _lowercase : Optional[str] = None ) -> str: if readme_content is not None: snake_case , snake_case : Any = _split_yaml_from_readme(_lowercase ) snake_case : Union[str, Any] = "---\n" + self.to_yaml_string() + "---\n" + content else: snake_case : int = "---\n" + self.to_yaml_string() + "---\n" return full_content @classmethod def __lowercase ( cls : Optional[int] , _lowercase : str ) -> "DatasetMetadata": snake_case : int = yaml.load(_lowercase , Loader=_NoDuplicateSafeLoader ) or {} # Convert the YAML keys to DatasetMetadata fields snake_case : List[Any] = { (key.replace("-" , "_" ) if key.replace("-" , "_" ) in cls._FIELDS_WITH_DASHES else key): value for key, value in metadata_dict.items() } return cls(**_lowercase ) def __lowercase ( self : List[str] ) -> str: return yaml.safe_dump( { (key.replace("_" , "-" ) if key in self._FIELDS_WITH_DASHES else key): value for key, value in self.items() } , sort_keys=_lowercase , allow_unicode=_lowercase , encoding="utf-8" , ).decode("utf-8" ) A = { 'image-classification': [], 'translation': [], 'image-segmentation': [], 'fill-mask': [], 'automatic-speech-recognition': [], 'token-classification': [], 'sentence-similarity': [], 'audio-classification': [], 'question-answering': [], 'summarization': [], 'zero-shot-classification': [], 'table-to-text': [], 'feature-extraction': [], 'other': [], 'multiple-choice': [], 'text-classification': [], 'text-to-image': [], 'text2text-generation': [], 'zero-shot-image-classification': [], 'tabular-classification': [], 'tabular-regression': [], 'image-to-image': [], 'tabular-to-text': [], 'unconditional-image-generation': [], 'text-retrieval': [], 'text-to-speech': [], 'object-detection': [], 'audio-to-audio': [], 'text-generation': [], 'conversational': [], 'table-question-answering': [], 'visual-question-answering': [], 'image-to-text': [], 'reinforcement-learning': [], 'voice-activity-detection': [], 'time-series-forecasting': [], 'document-question-answering': [], } if __name__ == "__main__": from argparse import ArgumentParser A = ArgumentParser(usage='Validate the yaml metadata block of a README.md file.') ap.add_argument('readme_filepath') A = ap.parse_args() A = Path(args.readme_filepath) A = DatasetMetadata.from_readme(readme_filepath) print(dataset_metadata) dataset_metadata.to_readme(readme_filepath)
449
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, is_vision_available, ) A = {'configuration_vit': ['VIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ViTConfig', 'ViTOnnxConfig']} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A = ['ViTFeatureExtractor'] A = ['ViTImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A = [ 'VIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'ViTForImageClassification', 'ViTForMaskedImageModeling', 'ViTModel', 'ViTPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A = [ 'TFViTForImageClassification', 'TFViTModel', 'TFViTPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A = [ 'FlaxViTForImageClassification', 'FlaxViTModel', 'FlaxViTPreTrainedModel', ] if TYPE_CHECKING: from .configuration_vit import VIT_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTConfig, ViTOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_vit import ViTFeatureExtractor from .image_processing_vit import ViTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit import ( VIT_PRETRAINED_MODEL_ARCHIVE_LIST, ViTForImageClassification, ViTForMaskedImageModeling, ViTModel, ViTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vit import TFViTForImageClassification, TFViTModel, TFViTPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel, FlaxViTPreTrainedModel else: import sys A = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
449
1
from collections.abc import Sequence from queue import Queue class _snake_case : def __init__( self ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase=None ,UpperCamelCase=None ) -> Tuple: snake_case__ :Any = start snake_case__ :int = end snake_case__ :str = val snake_case__ :Union[str, Any] = (start + end) // 2 snake_case__ :List[Any] = left snake_case__ :int = right def __repr__( self ) -> int: return f'SegmentTreeNode(start={self.start}, end={self.end}, val={self.val})' class _snake_case : def __init__( self ,UpperCamelCase ,UpperCamelCase ) -> Optional[int]: snake_case__ :List[Any] = collection snake_case__ :str = function if self.collection: snake_case__ :Any = self._build_tree(0 ,len(UpperCamelCase ) - 1 ) def lowerCAmelCase_ ( self ,UpperCamelCase ,UpperCamelCase ) -> List[str]: self._update_tree(self.root ,UpperCamelCase ,UpperCamelCase ) def lowerCAmelCase_ ( self ,UpperCamelCase ,UpperCamelCase ) -> Optional[int]: return self._query_range(self.root ,UpperCamelCase ,UpperCamelCase ) def lowerCAmelCase_ ( self ,UpperCamelCase ,UpperCamelCase ) -> int: if start == end: return SegmentTreeNode(UpperCamelCase ,UpperCamelCase ,self.collection[start] ) snake_case__ :List[Any] = (start + end) // 2 snake_case__ :Dict = self._build_tree(UpperCamelCase ,UpperCamelCase ) snake_case__ :str = self._build_tree(mid + 1 ,UpperCamelCase ) return SegmentTreeNode(UpperCamelCase ,UpperCamelCase ,self.fn(left.val ,right.val ) ,UpperCamelCase ,UpperCamelCase ) def lowerCAmelCase_ ( self ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ) -> Union[str, Any]: if node.start == i and node.end == i: snake_case__ :Optional[Any] = val return if i <= node.mid: self._update_tree(node.left ,UpperCamelCase ,UpperCamelCase ) else: self._update_tree(node.right ,UpperCamelCase ,UpperCamelCase ) snake_case__ :Tuple = self.fn(node.left.val ,node.right.val ) def lowerCAmelCase_ ( self ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase ) -> Union[str, Any]: if node.start == i and node.end == j: return node.val if i <= node.mid: if j <= node.mid: # range in left child tree return self._query_range(node.left ,UpperCamelCase ,UpperCamelCase ) else: # range in left child tree and right child tree return self.fn( self._query_range(node.left ,UpperCamelCase ,node.mid ) ,self._query_range(node.right ,node.mid + 1 ,UpperCamelCase ) ,) else: # range in right child tree return self._query_range(node.right ,UpperCamelCase ,UpperCamelCase ) def lowerCAmelCase_ ( self ) -> Optional[Any]: if self.root is not None: snake_case__ :Optional[int] = Queue() queue.put(self.root ) while not queue.empty(): snake_case__ :Tuple = queue.get() yield node if node.left is not None: queue.put(node.left ) if node.right is not None: queue.put(node.right ) if __name__ == "__main__": import operator for fn in [operator.add, max, min]: print("*" * 5_0) __UpperCAmelCase : Dict = SegmentTree([2, 1, 5, 3, 4], fn) for node in arr.traverse(): print(node) print() arr.update(1, 5) for node in arr.traverse(): print(node) print() print(arr.query_range(3, 4)) # 7 print(arr.query_range(2, 2)) # 5 print(arr.query_range(1, 3)) # 13 print()
57
from ...utils import ( OptionalDependencyNotAvailable, is_flax_available, is_torch_available, is_transformers_available, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .multicontrolnet import MultiControlNetModel from .pipeline_controlnet import StableDiffusionControlNetPipeline from .pipeline_controlnet_imgaimg import StableDiffusionControlNetImgaImgPipeline from .pipeline_controlnet_inpaint import StableDiffusionControlNetInpaintPipeline if is_transformers_available() and is_flax_available(): from .pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline
57
1
'''simple docstring''' def lowerCamelCase__ ( _A ): return credit_card_number.startswith(('34', '35', '37', '4', '5', '6') ) def lowerCamelCase__ ( _A ): a : Optional[int] = credit_card_number a : List[Any] = 0 a : Tuple = len(_A ) - 2 for i in range(_A , -1 , -2 ): # double the value of every second digit a : Optional[Any] = int(cc_number[i] ) digit *= 2 # If doubling of a number results in a two digit number # i.e greater than 9(e.g., 6 × 2 = 12), # then add the digits of the product (e.g., 12: 1 + 2 = 3, 15: 1 + 5 = 6), # to get a single digit number. if digit > 9: digit %= 10 digit += 1 a : Dict = cc_number[:i] + str(_A ) + cc_number[i + 1 :] total += digit # Sum up the remaining digits for i in range(len(_A ) - 1 , -1 , -2 ): total += int(cc_number[i] ) return total % 10 == 0 def lowerCamelCase__ ( _A ): a : Optional[int] = f"""{credit_card_number} is an invalid credit card number because""" if not credit_card_number.isdigit(): print(f"""{error_message} it has nonnumerical characters.""" ) return False if not 13 <= len(_A ) <= 16: print(f"""{error_message} of its length.""" ) return False if not validate_initial_digits(_A ): print(f"""{error_message} of its first two digits.""" ) return False if not luhn_validation(_A ): print(f"""{error_message} it fails the Luhn check.""" ) return False print(f"""{credit_card_number} is a valid credit card number.""" ) return True if __name__ == "__main__": import doctest doctest.testmod() validate_credit_card_number('4111111111111111') validate_credit_card_number('32323')
526
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCAmelCase: List[str] = logging.get_logger(__name__) lowerCAmelCase: int = { 'bert-base-uncased': 'https://huggingface.co/bert-base-uncased/resolve/main/config.json', 'bert-large-uncased': 'https://huggingface.co/bert-large-uncased/resolve/main/config.json', 'bert-base-cased': 'https://huggingface.co/bert-base-cased/resolve/main/config.json', 'bert-large-cased': 'https://huggingface.co/bert-large-cased/resolve/main/config.json', 'bert-base-multilingual-uncased': 'https://huggingface.co/bert-base-multilingual-uncased/resolve/main/config.json', 'bert-base-multilingual-cased': 'https://huggingface.co/bert-base-multilingual-cased/resolve/main/config.json', 'bert-base-chinese': 'https://huggingface.co/bert-base-chinese/resolve/main/config.json', 'bert-base-german-cased': 'https://huggingface.co/bert-base-german-cased/resolve/main/config.json', 'bert-large-uncased-whole-word-masking': ( 'https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/config.json' ), 'bert-large-cased-whole-word-masking': ( 'https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/config.json' ), 'bert-large-uncased-whole-word-masking-finetuned-squad': ( 'https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/config.json' ), 'bert-large-cased-whole-word-masking-finetuned-squad': ( 'https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/config.json' ), 'bert-base-cased-finetuned-mrpc': 'https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/config.json', 'bert-base-german-dbmdz-cased': 'https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/config.json', 'bert-base-german-dbmdz-uncased': 'https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/config.json', 'cl-tohoku/bert-base-japanese': 'https://huggingface.co/cl-tohoku/bert-base-japanese/resolve/main/config.json', 'cl-tohoku/bert-base-japanese-whole-word-masking': ( 'https://huggingface.co/cl-tohoku/bert-base-japanese-whole-word-masking/resolve/main/config.json' ), 'cl-tohoku/bert-base-japanese-char': ( 'https://huggingface.co/cl-tohoku/bert-base-japanese-char/resolve/main/config.json' ), 'cl-tohoku/bert-base-japanese-char-whole-word-masking': ( 'https://huggingface.co/cl-tohoku/bert-base-japanese-char-whole-word-masking/resolve/main/config.json' ), 'TurkuNLP/bert-base-finnish-cased-v1': ( 'https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/config.json' ), 'TurkuNLP/bert-base-finnish-uncased-v1': ( 'https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/config.json' ), 'wietsedv/bert-base-dutch-cased': 'https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/config.json', # See all BERT models at https://huggingface.co/models?filter=bert } class a__( lowerCamelCase__ ): lowercase__ = """bert""" def __init__( self : List[str] , __snake_case : Optional[Any]=3_05_22 , __snake_case : Any=7_68 , __snake_case : str=12 , __snake_case : Optional[int]=12 , __snake_case : Tuple=30_72 , __snake_case : Optional[Any]="gelu" , __snake_case : Dict=0.1 , __snake_case : Optional[int]=0.1 , __snake_case : List[Any]=5_12 , __snake_case : Optional[Any]=2 , __snake_case : Optional[Any]=0.02 , __snake_case : List[str]=1e-1_2 , __snake_case : Dict=0 , __snake_case : List[str]="absolute" , __snake_case : List[Any]=True , __snake_case : List[str]=None , **__snake_case : Any , ): super().__init__(pad_token_id=__snake_case , **__snake_case ) a : str = vocab_size a : List[Any] = hidden_size a : Union[str, Any] = num_hidden_layers a : List[Any] = num_attention_heads a : str = hidden_act a : str = intermediate_size a : Tuple = hidden_dropout_prob a : Union[str, Any] = attention_probs_dropout_prob a : List[Any] = max_position_embeddings a : Union[str, Any] = type_vocab_size a : List[Any] = initializer_range a : Dict = layer_norm_eps a : Any = position_embedding_type a : List[str] = use_cache a : List[str] = classifier_dropout class a__( lowerCamelCase__ ): @property def lowercase_ ( self : List[str] ): if self.task == "multiple-choice": a : Tuple = {0: 'batch', 1: 'choice', 2: 'sequence'} else: a : List[Any] = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ('token_type_ids', dynamic_axis), ] )
526
1
'''simple docstring''' import os import re import shutil import sys import tempfile import unittest import black lowercase__ = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, "utils")) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If DDPMSchedulerOutput is changed in scheduling_ddpm.py, this code needs to be manually updated. lowercase__ = " \"\"\"\n Output class for the scheduler's step function output.\n\n Args:\n prev_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):\n Computed sample (x_{t-1}) of previous timestep. `prev_sample` should be used as next model input in the\n denoising loop.\n pred_original_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):\n The predicted denoised sample (x_{0}) based on the model output from the current timestep.\n `pred_original_sample` can be used to preview progress or for guidance.\n \"\"\"\n\n prev_sample: torch.FloatTensor\n pred_original_sample: Optional[torch.FloatTensor] = None\n" class A_ ( unittest.TestCase ): '''simple docstring''' def UpperCAmelCase_ ( self : Any ) -> Optional[Any]: UpperCAmelCase : Any = tempfile.mkdtemp() os.makedirs(os.path.join(self.diffusers_dir , 'schedulers/' ) ) UpperCAmelCase : Tuple = self.diffusers_dir shutil.copy( os.path.join(lowercase_ , 'src/diffusers/schedulers/scheduling_ddpm.py' ) , os.path.join(self.diffusers_dir , 'schedulers/scheduling_ddpm.py' ) , ) def UpperCAmelCase_ ( self : Optional[int] ) -> List[Any]: UpperCAmelCase : Optional[Any] = 'src/diffusers' shutil.rmtree(self.diffusers_dir ) def UpperCAmelCase_ ( self : List[str] , lowercase_ : Optional[int] , lowercase_ : Union[str, Any] , lowercase_ : Dict , lowercase_ : str=None ) -> Union[str, Any]: UpperCAmelCase : Optional[int] = comment + f"""\nclass {class_name}(nn.Module):\n""" + class_code if overwrite_result is not None: UpperCAmelCase : Optional[Any] = comment + f"""\nclass {class_name}(nn.Module):\n""" + overwrite_result UpperCAmelCase : int = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) UpperCAmelCase : Union[str, Any] = black.format_str(lowercase_ , mode=lowercase_ ) UpperCAmelCase : List[str] = os.path.join(self.diffusers_dir , 'new_code.py' ) with open(lowercase_ , 'w' , newline='\n' ) as f: f.write(lowercase_ ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(lowercase_ ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=lowercase_ ) with open(lowercase_ , 'r' ) as f: self.assertTrue(f.read() , lowercase_ ) def UpperCAmelCase_ ( self : int ) -> List[Any]: UpperCAmelCase : List[Any] = check_copies.find_code_in_diffusers('schedulers.scheduling_ddpm.DDPMSchedulerOutput' ) self.assertEqual(lowercase_ , lowercase_ ) def UpperCAmelCase_ ( self : Dict ) -> Any: # Base copy consistency self.check_copy_consistency( '# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput' , 'DDPMSchedulerOutput' , REFERENCE_CODE + '\n' , ) # With no empty line at the end self.check_copy_consistency( '# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput' , 'DDPMSchedulerOutput' , lowercase_ , ) # Copy consistency with rename self.check_copy_consistency( '# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test' , 'TestSchedulerOutput' , re.sub('DDPM' , 'Test' , lowercase_ ) , ) # Copy consistency with a really long name UpperCAmelCase : Optional[int] = 'TestClassWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason' self.check_copy_consistency( f"""# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->{long_class_name}""" , f"""{long_class_name}SchedulerOutput""" , re.sub('Bert' , lowercase_ , lowercase_ ) , ) # Copy consistency with overwrite self.check_copy_consistency( '# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test' , 'TestSchedulerOutput' , lowercase_ , overwrite_result=re.sub('DDPM' , 'Test' , lowercase_ ) , )
704
'''simple docstring''' # Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from ..models.whisper import WhisperForConditionalGeneration, WhisperProcessor from .base import PipelineTool class A_ ( _snake_case ): '''simple docstring''' UpperCAmelCase_ : List[Any] = """openai/whisper-base""" UpperCAmelCase_ : Union[str, Any] = ( """This is a tool that transcribes an audio into text. It takes an input named `audio` and returns the """ """transcribed text.""" ) UpperCAmelCase_ : Dict = """transcriber""" UpperCAmelCase_ : int = WhisperProcessor UpperCAmelCase_ : Optional[int] = WhisperForConditionalGeneration UpperCAmelCase_ : Dict = ["""audio"""] UpperCAmelCase_ : Optional[int] = ["""text"""] def UpperCAmelCase_ ( self : Tuple , lowercase_ : str ) -> Optional[int]: return self.pre_processor(lowercase_ , return_tensors='pt' ).input_features def UpperCAmelCase_ ( self : Tuple , lowercase_ : int ) -> List[str]: return self.model.generate(inputs=lowercase_ ) def UpperCAmelCase_ ( self : str , lowercase_ : List[Any] ) -> List[str]: return self.pre_processor.batch_decode(lowercase_ , skip_special_tokens=lowercase_ )[0]
695
0
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 __lowercase ( __snake_case ): UpperCamelCase = ['''image_processor''', '''tokenizer'''] UpperCamelCase = '''BridgeTowerImageProcessor''' UpperCamelCase = ('''RobertaTokenizer''', '''RobertaTokenizerFast''') def __init__( self : int , __lowerCamelCase : Tuple , __lowerCamelCase : List[Any] ) -> Union[str, Any]: """simple docstring""" super().__init__(__lowerCamelCase , __lowerCamelCase ) def __call__( self : Any , __lowerCamelCase : Optional[int] , __lowerCamelCase : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , __lowerCamelCase : bool = True , __lowerCamelCase : Union[bool, str, PaddingStrategy] = False , __lowerCamelCase : Union[bool, str, TruncationStrategy] = None , __lowerCamelCase : Optional[int] = None , __lowerCamelCase : int = 0 , __lowerCamelCase : Optional[int] = None , __lowerCamelCase : Optional[bool] = None , __lowerCamelCase : Optional[bool] = None , __lowerCamelCase : bool = False , __lowerCamelCase : bool = False , __lowerCamelCase : bool = False , __lowerCamelCase : bool = False , __lowerCamelCase : bool = True , __lowerCamelCase : Optional[Union[str, TensorType]] = None , **__lowerCamelCase : Dict , ) -> BatchEncoding: """simple docstring""" UpperCAmelCase = self.tokenizer( text=__lowerCamelCase , add_special_tokens=__lowerCamelCase , padding=__lowerCamelCase , truncation=__lowerCamelCase , max_length=__lowerCamelCase , stride=__lowerCamelCase , pad_to_multiple_of=__lowerCamelCase , return_token_type_ids=__lowerCamelCase , return_attention_mask=__lowerCamelCase , return_overflowing_tokens=__lowerCamelCase , return_special_tokens_mask=__lowerCamelCase , return_offsets_mapping=__lowerCamelCase , return_length=__lowerCamelCase , verbose=__lowerCamelCase , return_tensors=__lowerCamelCase , **__lowerCamelCase , ) # add pixel_values + pixel_mask UpperCAmelCase = self.image_processor( __lowerCamelCase , return_tensors=__lowerCamelCase , do_normalize=__lowerCamelCase , do_center_crop=__lowerCamelCase , **__lowerCamelCase ) encoding.update(__lowerCamelCase ) return encoding def _lowercase ( self : Optional[Any] , *__lowerCamelCase : Optional[int] , **__lowerCamelCase : List[str] ) -> str: """simple docstring""" return self.tokenizer.batch_decode(*__lowerCamelCase , **__lowerCamelCase ) def _lowercase ( self : Dict , *__lowerCamelCase : Any , **__lowerCamelCase : List[Any] ) -> Optional[int]: """simple docstring""" return self.tokenizer.decode(*__lowerCamelCase , **__lowerCamelCase ) @property def _lowercase ( self : List[Any] ) -> Optional[int]: """simple docstring""" UpperCAmelCase = self.tokenizer.model_input_names UpperCAmelCase = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
377
import argparse import os import re import packaging.version __a = """examples/""" __a = { """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"""), } __a = { """init""": """src/diffusers/__init__.py""", """setup""": """setup.py""", } __a = """README.md""" def _UpperCamelCase ( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) ->Optional[Any]: with open(lowerCAmelCase_ , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: UpperCAmelCase = f.read() UpperCAmelCase , UpperCAmelCase = REPLACE_PATTERNS[pattern] UpperCAmelCase = replace.replace("""VERSION""" , lowerCAmelCase_ ) UpperCAmelCase = re_pattern.sub(lowerCAmelCase_ , lowerCAmelCase_ ) with open(lowerCAmelCase_ , """w""" , encoding="""utf-8""" , newline="""\n""" ) as f: f.write(lowerCAmelCase_ ) def _UpperCamelCase ( lowerCAmelCase_ ) ->int: for folder, directories, fnames in os.walk(lowerCAmelCase_ ): # 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(lowerCAmelCase_ , lowerCAmelCase_ ) , lowerCAmelCase_ , pattern="""examples""" ) def _UpperCamelCase ( lowerCAmelCase_ , lowerCAmelCase_=False ) ->Optional[int]: for pattern, fname in REPLACE_FILES.items(): update_version_in_file(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) if not patch: update_version_in_examples(lowerCAmelCase_ ) def _UpperCamelCase ( ) ->List[str]: UpperCAmelCase = """🤗 Transformers currently provides the following architectures""" UpperCAmelCase = """1. Want to contribute a new model?""" with open(lowerCAmelCase_ , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: UpperCAmelCase = f.readlines() # Find the start of the list. UpperCAmelCase = 0 while not lines[start_index].startswith(_start_prompt ): start_index += 1 start_index += 1 UpperCAmelCase = start_index # Update the lines in the model list. while not lines[index].startswith(_end_prompt ): if lines[index].startswith("""1.""" ): UpperCAmelCase = lines[index].replace( """https://huggingface.co/docs/diffusers/main/model_doc""" , """https://huggingface.co/docs/diffusers/model_doc""" , ) index += 1 with open(lowerCAmelCase_ , """w""" , encoding="""utf-8""" , newline="""\n""" ) as f: f.writelines(lowerCAmelCase_ ) def _UpperCamelCase ( ) ->List[str]: with open(REPLACE_FILES["""init"""] , """r""" ) as f: UpperCAmelCase = f.read() UpperCAmelCase = REPLACE_PATTERNS["""init"""][0].search(lowerCAmelCase_ ).groups()[0] return packaging.version.parse(lowerCAmelCase_ ) def _UpperCamelCase ( lowerCAmelCase_=False ) ->int: UpperCAmelCase = 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: UpperCAmelCase = default_version.base_version elif patch: UpperCAmelCase = F"""{default_version.major}.{default_version.minor}.{default_version.micro + 1}""" else: UpperCAmelCase = F"""{default_version.major}.{default_version.minor + 1}.0""" # Now let's ask nicely if that's the right one. UpperCAmelCase = input(F"""Which version are you releasing? [{default_version}]""" ) if len(lowerCAmelCase_ ) == 0: UpperCAmelCase = default_version print(F"""Updating version to {version}.""" ) global_version_update(lowerCAmelCase_ , patch=lowerCAmelCase_ ) def _UpperCamelCase ( ) ->int: UpperCAmelCase = get_version() UpperCAmelCase = F"""{current_version.major}.{current_version.minor + 1}.0.dev0""" UpperCAmelCase = current_version.base_version # Check with the user we got that right. UpperCAmelCase = input(F"""Which version are we developing now? [{dev_version}]""" ) if len(lowerCAmelCase_ ) == 0: UpperCAmelCase = dev_version print(F"""Updating version to {version}.""" ) global_version_update(lowerCAmelCase_ ) # print("Cleaning main README, don't forget to run `make fix-copies`.") # clean_main_ref_in_model_list() if __name__ == "__main__": __a = 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.""") __a = 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()
377
1
import argparse import math import os from copy import deepcopy import torch from audio_diffusion.models import DiffusionAttnUnetaD from diffusion import sampling from torch import nn from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNetaDModel lowerCamelCase : List[Any] = { "gwf-440k": { "url": "https://model-server.zqevans2.workers.dev/gwf-440k.ckpt", "sample_rate": 48_000, "sample_size": 65_536, }, "jmann-small-190k": { "url": "https://model-server.zqevans2.workers.dev/jmann-small-190k.ckpt", "sample_rate": 48_000, "sample_size": 65_536, }, "jmann-large-580k": { "url": "https://model-server.zqevans2.workers.dev/jmann-large-580k.ckpt", "sample_rate": 48_000, "sample_size": 131_072, }, "maestro-uncond-150k": { "url": "https://model-server.zqevans2.workers.dev/maestro-uncond-150k.ckpt", "sample_rate": 16_000, "sample_size": 65_536, }, "unlocked-uncond-250k": { "url": "https://model-server.zqevans2.workers.dev/unlocked-uncond-250k.ckpt", "sample_rate": 16_000, "sample_size": 65_536, }, "honk-140k": { "url": "https://model-server.zqevans2.workers.dev/honk-140k.ckpt", "sample_rate": 16_000, "sample_size": 65_536, }, } def _SCREAMING_SNAKE_CASE ( lowercase : Optional[Any] , lowercase : str ): '''simple docstring''' return torch.atana(lowercase , lowercase ) / math.pi * 2 def _SCREAMING_SNAKE_CASE ( lowercase : Any ): '''simple docstring''' lowerCamelCase_ = torch.sin(t * math.pi / 2 ) ** 2 lowerCamelCase_ = (1 - sigma**2) ** 0.5 return alpha_sigma_to_t(lowercase , lowercase ) class A( UpperCamelCase ): '''simple docstring''' pass class A( nn.Module ): '''simple docstring''' def __init__( self : Optional[int] , A_ : int ) -> List[str]: """simple docstring""" super().__init__() lowerCamelCase_ = DiffusionAttnUnetaD(A_ , n_attn_layers=4 ) lowerCamelCase_ = deepcopy(self.diffusion ) lowerCamelCase_ = torch.quasirandom.SobolEngine(1 , scramble=A_ ) def _SCREAMING_SNAKE_CASE ( lowercase : int ): '''simple docstring''' lowerCamelCase_ = MODELS_MAP[model_name]['url'] os.system(f"""wget {url} ./""" ) return f"""./{model_name}.ckpt""" lowerCamelCase : str = { "1": "resnets.0", "2": "attentions.0", "3": "resnets.1", "4": "attentions.1", "5": "resnets.2", "6": "attentions.2", } lowerCamelCase : str = { "8": "resnets.0", "9": "attentions.0", "10": "resnets.1", "11": "attentions.1", "12": "resnets.2", "13": "attentions.2", } lowerCamelCase : List[str] = { "1": "resnets.0", "2": "attentions.0", "3": "resnets.1", "4": "attentions.1", "5": "resnets.2", "6": "attentions.2", "8": "resnets.3", "9": "attentions.3", "10": "resnets.4", "11": "attentions.4", "12": "resnets.5", "13": "attentions.5", } lowerCamelCase : Union[str, Any] = { "0": "resnets.0", "1": "resnets.1", "2": "resnets.2", "4": "resnets.0", "5": "resnets.1", "6": "resnets.2", } lowerCamelCase : str = { "skip": "conv_skip", "main.0": "conv_1", "main.1": "group_norm_1", "main.3": "conv_2", "main.4": "group_norm_2", } lowerCamelCase : Tuple = { "norm": "group_norm", "qkv_proj": ["query", "key", "value"], "out_proj": ["proj_attn"], } def _SCREAMING_SNAKE_CASE ( lowercase : Optional[Any] ): '''simple docstring''' if name.startswith('skip' ): return name.replace('skip' , RES_CONV_MAP['skip'] ) # name has to be of format main.{digit} if not name.startswith('main.' ): raise ValueError(f"""ResConvBlock error with {name}""" ) return name.replace(name[:6] , RES_CONV_MAP[name[:6]] ) def _SCREAMING_SNAKE_CASE ( lowercase : List[str] ): '''simple docstring''' for key, value in ATTN_MAP.items(): if name.startswith(lowercase ) and not isinstance(lowercase , lowercase ): return name.replace(lowercase , lowercase ) elif name.startswith(lowercase ): return [name.replace(lowercase , lowercase ) for v in value] raise ValueError(f"""Attn error with {name}""" ) def _SCREAMING_SNAKE_CASE ( lowercase : Any , lowercase : Union[str, Any]=13 ): '''simple docstring''' lowerCamelCase_ = input_string if string.split('.' )[0] == "timestep_embed": return string.replace('timestep_embed' , 'time_proj' ) lowerCamelCase_ = 0 if string.startswith('net.3.' ): depth += 1 lowerCamelCase_ = string[6:] elif string.startswith('net.' ): lowerCamelCase_ = string[4:] while string.startswith('main.7.' ): depth += 1 lowerCamelCase_ = string[7:] if string.startswith('main.' ): lowerCamelCase_ = string[5:] # mid block if string[:2].isdigit(): lowerCamelCase_ = string[:2] lowerCamelCase_ = string[2:] else: lowerCamelCase_ = string[0] lowerCamelCase_ = string[1:] if depth == max_depth: lowerCamelCase_ = MID_NUM_TO_LAYER[layer_num] lowerCamelCase_ = 'mid_block' elif depth > 0 and int(lowercase ) < 7: lowerCamelCase_ = DOWN_NUM_TO_LAYER[layer_num] lowerCamelCase_ = f"""down_blocks.{depth}""" elif depth > 0 and int(lowercase ) > 7: lowerCamelCase_ = UP_NUM_TO_LAYER[layer_num] lowerCamelCase_ = f"""up_blocks.{max_depth - depth - 1}""" elif depth == 0: lowerCamelCase_ = DEPTH_0_TO_LAYER[layer_num] lowerCamelCase_ = f"""up_blocks.{max_depth - 1}""" if int(lowercase ) > 3 else 'down_blocks.0' if not string_left.startswith('.' ): raise ValueError(f"""Naming error with {input_string} and string_left: {string_left}.""" ) lowerCamelCase_ = string_left[1:] if "resnets" in new_layer: lowerCamelCase_ = convert_resconv_naming(lowercase ) elif "attentions" in new_layer: lowerCamelCase_ = convert_attn_naming(lowercase ) lowerCamelCase_ = new_string_left if not isinstance(lowercase , lowercase ): lowerCamelCase_ = prefix + '.' + new_layer + '.' + string_left else: lowerCamelCase_ = [prefix + '.' + new_layer + '.' + s for s in string_left] return new_string def _SCREAMING_SNAKE_CASE ( lowercase : str ): '''simple docstring''' lowerCamelCase_ = {} for k, v in state_dict.items(): if k.endswith('kernel' ): # up- and downsample layers, don't have trainable weights continue lowerCamelCase_ = rename(lowercase ) # check if we need to transform from Conv => Linear for attention if isinstance(lowercase , lowercase ): lowerCamelCase_ = transform_conv_attns(lowercase , lowercase , lowercase ) else: lowerCamelCase_ = v return new_state_dict def _SCREAMING_SNAKE_CASE ( lowercase : List[str] , lowercase : Optional[Any] , lowercase : Optional[Any] ): '''simple docstring''' if len(lowercase ) == 1: if len(v.shape ) == 3: # weight lowerCamelCase_ = v[:, :, 0] else: # bias lowerCamelCase_ = v else: # qkv matrices lowerCamelCase_ = v.shape[0] lowerCamelCase_ = trippled_shape // 3 for i in range(3 ): if len(v.shape ) == 3: lowerCamelCase_ = v[i * single_shape : (i + 1) * single_shape, :, 0] else: lowerCamelCase_ = v[i * single_shape : (i + 1) * single_shape] return new_state_dict def _SCREAMING_SNAKE_CASE ( lowercase : List[Any] ): '''simple docstring''' lowerCamelCase_ = torch.device('cuda' if torch.cuda.is_available() else 'cpu' ) lowerCamelCase_ = args.model_path.split('/' )[-1].split('.' )[0] if not os.path.isfile(args.model_path ): assert ( model_name == args.model_path ), f"""Make sure to provide one of the official model names {MODELS_MAP.keys()}""" lowerCamelCase_ = download(lowercase ) lowerCamelCase_ = MODELS_MAP[model_name]['sample_rate'] lowerCamelCase_ = MODELS_MAP[model_name]['sample_size'] lowerCamelCase_ = Object() lowerCamelCase_ = sample_size lowerCamelCase_ = sample_rate lowerCamelCase_ = 0 lowerCamelCase_ = UNetaDModel(sample_size=lowercase , sample_rate=lowercase ) lowerCamelCase_ = diffusers_model.state_dict() lowerCamelCase_ = DiffusionUncond(lowercase ) orig_model.load_state_dict(torch.load(args.model_path , map_location=lowercase )['state_dict'] ) lowerCamelCase_ = orig_model.diffusion_ema.eval() lowerCamelCase_ = orig_model.state_dict() lowerCamelCase_ = rename_orig_weights(lowercase ) lowerCamelCase_ = set(renamed_state_dict.keys() ) - set(diffusers_state_dict.keys() ) lowerCamelCase_ = set(diffusers_state_dict.keys() ) - set(renamed_state_dict.keys() ) assert len(lowercase ) == 0, f"""Problem with {renamed_minus_diffusers}""" assert all(k.endswith('kernel' ) for k in list(lowercase ) ), f"""Problem with {diffusers_minus_renamed}""" for key, value in renamed_state_dict.items(): assert ( diffusers_state_dict[key].squeeze().shape == value.squeeze().shape ), f"""Shape for {key} doesn't match. Diffusers: {diffusers_state_dict[key].shape} vs. {value.shape}""" if key == "time_proj.weight": lowerCamelCase_ = value.squeeze() lowerCamelCase_ = value diffusers_model.load_state_dict(lowercase ) lowerCamelCase_ = 1_00 lowerCamelCase_ = 33 lowerCamelCase_ = IPNDMScheduler(num_train_timesteps=lowercase ) lowerCamelCase_ = torch.manual_seed(lowercase ) lowerCamelCase_ = torch.randn([1, 2, config.sample_size] , generator=lowercase ).to(lowercase ) lowerCamelCase_ = torch.linspace(1 , 0 , steps + 1 , device=lowercase )[:-1] lowerCamelCase_ = get_crash_schedule(lowercase ) lowerCamelCase_ = DanceDiffusionPipeline(unet=lowercase , scheduler=lowercase ) lowerCamelCase_ = torch.manual_seed(33 ) lowerCamelCase_ = pipe(num_inference_steps=lowercase , generator=lowercase ).audios lowerCamelCase_ = sampling.iplms_sample(lowercase , lowercase , lowercase , {} ) lowerCamelCase_ = generated.clamp(-1 , 1 ) lowerCamelCase_ = (generated - audio).abs().sum() lowerCamelCase_ = (generated - audio).abs().max() if args.save: pipe.save_pretrained(args.checkpoint_path ) print('Diff sum' , lowercase ) print('Diff max' , lowercase ) assert diff_max < 1e-3, f"""Diff max: {diff_max} is too much :-/""" print(f"""Conversion for {model_name} successful!""" ) if __name__ == "__main__": lowerCamelCase : Union[str, Any] = argparse.ArgumentParser() parser.add_argument("--model_path", default=None, type=str, required=True, help="Path to the model to convert.") parser.add_argument( "--save", default=True, type=bool, required=False, help="Whether to save the converted model or not." ) parser.add_argument("--checkpoint_path", default=None, type=str, required=True, help="Path to the output model.") lowerCamelCase : Dict = parser.parse_args() main(args)
706
import unittest from transformers import ( MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING, TextGenerationPipeline, logging, pipeline, ) from transformers.testing_utils import ( CaptureLogger, is_pipeline_test, require_accelerate, require_tf, require_torch, require_torch_gpu, require_torch_or_tf, ) from .test_pipelines_common import ANY @is_pipeline_test @require_torch_or_tf class A( unittest.TestCase ): '''simple docstring''' UpperCamelCase = MODEL_FOR_CAUSAL_LM_MAPPING UpperCamelCase = TF_MODEL_FOR_CAUSAL_LM_MAPPING @require_torch def a__ ( self : Any ) -> Union[str, Any]: """simple docstring""" lowerCamelCase_ = pipeline(task='text-generation' , model='sshleifer/tiny-ctrl' , framework='pt' ) # Using `do_sample=False` to force deterministic output lowerCamelCase_ = text_generator('This is a test' , do_sample=A_ ) self.assertEqual( A_ , [ { 'generated_text': ( 'This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope.' ' oscope. FiliFili@@' ) } ] , ) lowerCamelCase_ = text_generator(['This is a test', 'This is a second test'] ) self.assertEqual( A_ , [ [ { 'generated_text': ( 'This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope.' ' oscope. FiliFili@@' ) } ], [ { 'generated_text': ( 'This is a second test ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy' ' oscope. oscope. FiliFili@@' ) } ], ] , ) lowerCamelCase_ = text_generator('This is a test' , do_sample=A_ , num_return_sequences=2 , return_tensors=A_ ) self.assertEqual( A_ , [ {'generated_token_ids': ANY(A_ )}, {'generated_token_ids': ANY(A_ )}, ] , ) lowerCamelCase_ = text_generator.model.config.eos_token_id lowerCamelCase_ = '<pad>' lowerCamelCase_ = text_generator( ['This is a test', 'This is a second test'] , do_sample=A_ , num_return_sequences=2 , batch_size=2 , return_tensors=A_ , ) self.assertEqual( A_ , [ [ {'generated_token_ids': ANY(A_ )}, {'generated_token_ids': ANY(A_ )}, ], [ {'generated_token_ids': ANY(A_ )}, {'generated_token_ids': ANY(A_ )}, ], ] , ) @require_tf def a__ ( self : Optional[int] ) -> str: """simple docstring""" lowerCamelCase_ = pipeline(task='text-generation' , model='sshleifer/tiny-ctrl' , framework='tf' ) # Using `do_sample=False` to force deterministic output lowerCamelCase_ = text_generator('This is a test' , do_sample=A_ ) self.assertEqual( A_ , [ { 'generated_text': ( 'This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵' ' please,' ) } ] , ) lowerCamelCase_ = text_generator(['This is a test', 'This is a second test'] , do_sample=A_ ) self.assertEqual( A_ , [ [ { 'generated_text': ( 'This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵' ' please,' ) } ], [ { 'generated_text': ( 'This is a second test Chieftain Chieftain prefecture prefecture prefecture Cannes Cannes' ' Cannes 閲閲Cannes Cannes Cannes 攵 please,' ) } ], ] , ) def a__ ( self : Optional[int] , A_ : Dict , A_ : int , A_ : List[str] ) -> str: """simple docstring""" lowerCamelCase_ = TextGenerationPipeline(model=A_ , tokenizer=A_ ) return text_generator, ["This is a test", "Another test"] def a__ ( self : Dict ) -> str: """simple docstring""" lowerCamelCase_ = 'Hello I believe in' lowerCamelCase_ = pipeline('text-generation' , model='hf-internal-testing/tiny-random-gpt2' ) lowerCamelCase_ = text_generator(A_ ) self.assertEqual( A_ , [{'generated_text': 'Hello I believe in fe fe fe fe fe fe fe fe fe fe fe fe'}] , ) lowerCamelCase_ = text_generator(A_ , stop_sequence=' fe' ) self.assertEqual(A_ , [{'generated_text': 'Hello I believe in fe'}] ) def a__ ( self : Any , A_ : Optional[Any] , A_ : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" lowerCamelCase_ = text_generator.model lowerCamelCase_ = text_generator.tokenizer lowerCamelCase_ = text_generator('This is a test' ) self.assertEqual(A_ , [{'generated_text': ANY(A_ )}] ) self.assertTrue(outputs[0]['generated_text'].startswith('This is a test' ) ) lowerCamelCase_ = text_generator('This is a test' , return_full_text=A_ ) self.assertEqual(A_ , [{'generated_text': ANY(A_ )}] ) self.assertNotIn('This is a test' , outputs[0]['generated_text'] ) lowerCamelCase_ = pipeline(task='text-generation' , model=A_ , tokenizer=A_ , return_full_text=A_ ) lowerCamelCase_ = text_generator('This is a test' ) self.assertEqual(A_ , [{'generated_text': ANY(A_ )}] ) self.assertNotIn('This is a test' , outputs[0]['generated_text'] ) lowerCamelCase_ = text_generator('This is a test' , return_full_text=A_ ) self.assertEqual(A_ , [{'generated_text': ANY(A_ )}] ) self.assertTrue(outputs[0]['generated_text'].startswith('This is a test' ) ) lowerCamelCase_ = text_generator(['This is great !', 'Something else'] , num_return_sequences=2 , do_sample=A_ ) self.assertEqual( A_ , [ [{'generated_text': ANY(A_ )}, {'generated_text': ANY(A_ )}], [{'generated_text': ANY(A_ )}, {'generated_text': ANY(A_ )}], ] , ) if text_generator.tokenizer.pad_token is not None: lowerCamelCase_ = text_generator( ['This is great !', 'Something else'] , num_return_sequences=2 , batch_size=2 , do_sample=A_ ) self.assertEqual( A_ , [ [{'generated_text': ANY(A_ )}, {'generated_text': ANY(A_ )}], [{'generated_text': ANY(A_ )}, {'generated_text': ANY(A_ )}], ] , ) with self.assertRaises(A_ ): lowerCamelCase_ = text_generator('test' , return_full_text=A_ , return_text=A_ ) with self.assertRaises(A_ ): lowerCamelCase_ = text_generator('test' , return_full_text=A_ , return_tensors=A_ ) with self.assertRaises(A_ ): lowerCamelCase_ = text_generator('test' , return_text=A_ , return_tensors=A_ ) # Empty prompt is slighly special # it requires BOS token to exist. # Special case for Pegasus which will always append EOS so will # work even without BOS. if ( text_generator.tokenizer.bos_token_id is not None or "Pegasus" in tokenizer.__class__.__name__ or "Git" in model.__class__.__name__ ): lowerCamelCase_ = text_generator('' ) self.assertEqual(A_ , [{'generated_text': ANY(A_ )}] ) else: with self.assertRaises((ValueError, AssertionError) ): lowerCamelCase_ = text_generator('' ) if text_generator.framework == "tf": # TF generation does not support max_new_tokens, and it's impossible # to control long generation with only max_length without # fancy calculation, dismissing tests for now. return # We don't care about infinite range models. # They already work. # Skip this test for XGLM, since it uses sinusoidal positional embeddings which are resized on-the-fly. lowerCamelCase_ = ['RwkvForCausalLM', 'XGLMForCausalLM', 'GPTNeoXForCausalLM'] if ( tokenizer.model_max_length < 10000 and text_generator.model.__class__.__name__ not in EXTRA_MODELS_CAN_HANDLE_LONG_INPUTS ): # Handling of large generations with self.assertRaises((RuntimeError, IndexError, ValueError, AssertionError) ): text_generator('This is a test' * 500 , max_new_tokens=20 ) lowerCamelCase_ = text_generator('This is a test' * 500 , handle_long_generation='hole' , max_new_tokens=20 ) # Hole strategy cannot work with self.assertRaises(A_ ): text_generator( 'This is a test' * 500 , handle_long_generation='hole' , max_new_tokens=tokenizer.model_max_length + 10 , ) @require_torch @require_accelerate @require_torch_gpu def a__ ( self : Union[str, Any] ) -> Any: """simple docstring""" import torch # Classic `model_kwargs` lowerCamelCase_ = pipeline( model='hf-internal-testing/tiny-random-bloom' , model_kwargs={'device_map': 'auto', 'torch_dtype': torch.bfloataa} , ) self.assertEqual(pipe.model.device , torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype , torch.bfloataa ) lowerCamelCase_ = pipe('This is a test' ) self.assertEqual( A_ , [ { 'generated_text': ( 'This is a test test test test test test test test test test test test test test test test' ' test' ) } ] , ) # Upgraded those two to real pipeline arguments (they just get sent for the model as they're unlikely to mean anything else.) lowerCamelCase_ = pipeline(model='hf-internal-testing/tiny-random-bloom' , device_map='auto' , torch_dtype=torch.bfloataa ) self.assertEqual(pipe.model.device , torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype , torch.bfloataa ) lowerCamelCase_ = pipe('This is a test' ) self.assertEqual( A_ , [ { 'generated_text': ( 'This is a test test test test test test test test test test test test test test test test' ' test' ) } ] , ) # torch_dtype will be automatically set to float32 if not provided - check: https://github.com/huggingface/transformers/pull/20602 lowerCamelCase_ = pipeline(model='hf-internal-testing/tiny-random-bloom' , device_map='auto' ) self.assertEqual(pipe.model.device , torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype , torch.floataa ) lowerCamelCase_ = pipe('This is a test' ) self.assertEqual( A_ , [ { 'generated_text': ( 'This is a test test test test test test test test test test test test test test test test' ' test' ) } ] , ) @require_torch @require_torch_gpu def a__ ( self : int ) -> str: """simple docstring""" import torch lowerCamelCase_ = pipeline(model='hf-internal-testing/tiny-random-bloom' , device=0 , torch_dtype=torch.floataa ) pipe('This is a test' ) @require_torch @require_accelerate @require_torch_gpu def a__ ( self : List[Any] ) -> Dict: """simple docstring""" import torch lowerCamelCase_ = pipeline(model='hf-internal-testing/tiny-random-bloom' , device_map='auto' , torch_dtype=torch.floataa ) pipe('This is a test' , do_sample=A_ , top_p=0.5 ) def a__ ( self : Tuple ) -> Dict: """simple docstring""" lowerCamelCase_ = 'Hello world' lowerCamelCase_ = pipeline('text-generation' , model='hf-internal-testing/tiny-random-gpt2' ) if text_generator.model.framework == "tf": lowerCamelCase_ = logging.get_logger('transformers.generation.tf_utils' ) else: lowerCamelCase_ = logging.get_logger('transformers.generation.utils' ) lowerCamelCase_ = 'Both `max_new_tokens`' # The beggining of the message to be checked in this test # Both are set by the user -> log warning with CaptureLogger(A_ ) as cl: lowerCamelCase_ = text_generator(A_ , max_length=10 , max_new_tokens=1 ) self.assertIn(A_ , cl.out ) # The user only sets one -> no warning with CaptureLogger(A_ ) as cl: lowerCamelCase_ = text_generator(A_ , max_new_tokens=1 ) self.assertNotIn(A_ , cl.out ) with CaptureLogger(A_ ) as cl: lowerCamelCase_ = text_generator(A_ , max_length=10 ) self.assertNotIn(A_ , cl.out )
651
0
'''simple docstring''' import os import socket from contextlib import contextmanager import torch from ..commands.config.default import write_basic_config # noqa: F401 from ..state import PartialState from .dataclasses import DistributedType from .imports import is_deepspeed_available, is_tpu_available from .transformer_engine import convert_model from .versions import is_torch_version if is_deepspeed_available(): from deepspeed import DeepSpeedEngine if is_tpu_available(check_device=False): import torch_xla.core.xla_model as xm def A_ ( _lowerCAmelCase : Dict ): """simple docstring""" if is_torch_version("<" , "2.0.0" ) or not hasattr(_lowerCAmelCase , "_dynamo" ): return False return isinstance(_lowerCAmelCase , torch._dynamo.eval_frame.OptimizedModule ) def A_ ( _lowerCAmelCase : str , _lowerCAmelCase : bool = True ): """simple docstring""" _lowerCamelCase : Tuple = (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel) _lowerCamelCase : Optional[Any] = is_compiled_module(_lowerCAmelCase ) if is_compiled: _lowerCamelCase : Tuple = model _lowerCamelCase : Dict = model._orig_mod if is_deepspeed_available(): options += (DeepSpeedEngine,) while isinstance(_lowerCAmelCase , _lowerCAmelCase ): _lowerCamelCase : Tuple = model.module if not keep_fpaa_wrapper: _lowerCamelCase : Optional[Any] = getattr(_lowerCAmelCase , "forward" ) _lowerCamelCase : Any = model.__dict__.pop("_original_forward" , _lowerCAmelCase ) if original_forward is not None: while hasattr(_lowerCAmelCase , "__wrapped__" ): _lowerCamelCase : List[str] = forward.__wrapped__ if forward == original_forward: break _lowerCamelCase : str = forward if getattr(_lowerCAmelCase , "_converted_to_transformer_engine" , _lowerCAmelCase ): convert_model(_lowerCAmelCase , to_transformer_engine=_lowerCAmelCase ) if is_compiled: _lowerCamelCase : List[str] = model _lowerCamelCase : Optional[int] = compiled_model return model def A_ ( ): """simple docstring""" PartialState().wait_for_everyone() def A_ ( _lowerCAmelCase : str , _lowerCAmelCase : Tuple ): """simple docstring""" if PartialState().distributed_type == DistributedType.TPU: xm.save(_lowerCAmelCase , _lowerCAmelCase ) elif PartialState().local_process_index == 0: torch.save(_lowerCAmelCase , _lowerCAmelCase ) @contextmanager def A_ ( **_lowerCAmelCase : Any ): """simple docstring""" for key, value in kwargs.items(): _lowerCamelCase : str = str(_lowerCAmelCase ) yield for key in kwargs: if key.upper() in os.environ: del os.environ[key.upper()] def A_ ( _lowerCAmelCase : Any ): """simple docstring""" if not hasattr(_lowerCAmelCase , "__qualname__" ) and not hasattr(_lowerCAmelCase , "__name__" ): _lowerCamelCase : Any = getattr(_lowerCAmelCase , "__class__" , _lowerCAmelCase ) if hasattr(_lowerCAmelCase , "__qualname__" ): return obj.__qualname__ if hasattr(_lowerCAmelCase , "__name__" ): return obj.__name__ return str(_lowerCAmelCase ) def A_ ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : str ): """simple docstring""" for key, value in source.items(): if isinstance(_lowerCAmelCase , _lowerCAmelCase ): _lowerCamelCase : Optional[int] = destination.setdefault(_lowerCAmelCase , {} ) merge_dicts(_lowerCAmelCase , _lowerCAmelCase ) else: _lowerCamelCase : Optional[Any] = value return destination def A_ ( _lowerCAmelCase : int = None ): """simple docstring""" if port is None: _lowerCamelCase : Dict = 29500 with socket.socket(socket.AF_INET , socket.SOCK_STREAM ) as s: return s.connect_ex(("localhost", port) ) == 0
44
'''simple docstring''' import logging import torch from accelerate import Accelerator from arguments import EvaluationArguments from datasets import load_dataset from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, set_seed class UpperCAmelCase__ ( A ): def __init__( self : List[Any],__A : Tuple,__A : Optional[int],__A : Optional[int]=1_0_2_4,__A : int=1_0_2_4,__A : Any=3.6 ): _lowerCamelCase : List[str] = tokenizer _lowerCamelCase : Dict = tokenizer.bos_token_id _lowerCamelCase : Tuple = dataset _lowerCamelCase : Any = seq_length _lowerCamelCase : List[Any] = seq_length * chars_per_token * num_of_sequences def __iter__( self : Tuple ): _lowerCamelCase : Union[str, Any] = iter(self.dataset ) _lowerCamelCase : str = True while more_examples: _lowerCamelCase , _lowerCamelCase : Optional[int] = [], 0 while True: if buffer_len >= self.input_characters: break try: buffer.append(next(__A )["content"] ) buffer_len += len(buffer[-1] ) except StopIteration: _lowerCamelCase : Tuple = False break _lowerCamelCase : int = tokenizer(__A,truncation=__A )["input_ids"] _lowerCamelCase : int = [] for tokenized_input in tokenized_inputs: all_token_ids.extend(tokenized_input + [self.concat_token_id] ) for i in range(0,len(__A ),self.seq_length ): _lowerCamelCase : List[str] = all_token_ids[i : i + self.seq_length] if len(__A ) == self.seq_length: yield torch.tensor(__A ) def A_ ( _lowerCAmelCase : Dict ): """simple docstring""" _lowerCamelCase : Optional[Any] = {"streaming": True} _lowerCamelCase : Optional[Any] = load_dataset(args.dataset_name , split="train" , **_lowerCAmelCase ) _lowerCamelCase : int = ConstantLengthDataset(_lowerCAmelCase , _lowerCAmelCase , seq_length=args.seq_length ) _lowerCamelCase : Dict = DataLoader(_lowerCAmelCase , batch_size=args.batch_size ) return eval_dataloader def A_ ( _lowerCAmelCase : Optional[Any] ): """simple docstring""" model.eval() _lowerCamelCase : Optional[int] = [] for step, batch in enumerate(_lowerCAmelCase ): with torch.no_grad(): _lowerCamelCase : List[str] = model(_lowerCAmelCase , labels=_lowerCAmelCase ) _lowerCamelCase : List[Any] = outputs.loss.repeat(args.batch_size ) losses.append(accelerator.gather(_lowerCAmelCase ) ) if args.max_eval_steps > 0 and step >= args.max_eval_steps: break _lowerCamelCase : Dict = torch.mean(torch.cat(_lowerCAmelCase ) ) try: _lowerCamelCase : List[Any] = torch.exp(_lowerCAmelCase ) except OverflowError: _lowerCamelCase : Optional[int] = float("inf" ) return loss.item(), perplexity.item() # Setup Accelerator UpperCAmelCase_ : List[str] = Accelerator() # Parse configuration UpperCAmelCase_ : Tuple = HfArgumentParser(EvaluationArguments) UpperCAmelCase_ : Dict = parser.parse_args() set_seed(args.seed) # Logging UpperCAmelCase_ : Optional[int] = logging.getLogger(__name__) logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s', datefmt='%m/%d/%Y %H:%M:%S', level=logging.INFO ) # Load model and tokenizer UpperCAmelCase_ : Tuple = AutoModelForCausalLM.from_pretrained(args.model_ckpt) UpperCAmelCase_ : Dict = AutoTokenizer.from_pretrained(args.model_ckpt) # Load dataset and dataloader UpperCAmelCase_ : int = create_dataloader(args) # Prepare everything with our `accelerator`. UpperCAmelCase_, UpperCAmelCase_ : Dict = accelerator.prepare(model, eval_dataloader) # Evaluate and save the last checkpoint logger.info('Evaluating and saving model after training') UpperCAmelCase_, UpperCAmelCase_ : str = evaluate(args) logger.info(f'''loss/eval: {eval_loss}, perplexity: {perplexity}''')
44
1
"""simple docstring""" import argparse import json import os import pickle import shutil import numpy as np import torch from distiller import Distiller from lm_seqs_dataset import LmSeqsDataset from transformers import ( BertConfig, BertForMaskedLM, BertTokenizer, DistilBertConfig, DistilBertForMaskedLM, DistilBertTokenizer, GPTaConfig, GPTaLMHeadModel, GPTaTokenizer, RobertaConfig, RobertaForMaskedLM, RobertaTokenizer, ) from utils import git_log, init_gpu_params, logger, set_seed a = { 'distilbert': (DistilBertConfig, DistilBertForMaskedLM, DistilBertTokenizer), 'roberta': (RobertaConfig, RobertaForMaskedLM, RobertaTokenizer), 'bert': (BertConfig, BertForMaskedLM, BertTokenizer), 'gpt2': (GPTaConfig, GPTaLMHeadModel, GPTaTokenizer), } def lowercase (snake_case__ : List[str] ) -> str: '''simple docstring''' assert (args.mlm and args.alpha_mlm > 0.0) or (not args.mlm and args.alpha_mlm == 0.0) assert (args.alpha_mlm > 0.0 and args.alpha_clm == 0.0) or (args.alpha_mlm == 0.0 and args.alpha_clm > 0.0) if args.mlm: assert os.path.isfile(args.token_counts ) assert (args.student_type in ["roberta", "distilbert"]) and (args.teacher_type in ["roberta", "bert"]) else: assert (args.student_type in ["gpt2"]) and (args.teacher_type in ["gpt2"]) assert args.teacher_type == args.student_type or ( args.student_type == "distilbert" and args.teacher_type == "bert" ) assert os.path.isfile(args.student_config ) if args.student_pretrained_weights is not None: assert os.path.isfile(args.student_pretrained_weights ) if args.freeze_token_type_embds: assert args.student_type in ["roberta"] assert args.alpha_ce >= 0.0 assert args.alpha_mlm >= 0.0 assert args.alpha_clm >= 0.0 assert args.alpha_mse >= 0.0 assert args.alpha_cos >= 0.0 assert args.alpha_ce + args.alpha_mlm + args.alpha_clm + args.alpha_mse + args.alpha_cos > 0.0 def lowercase (snake_case__ : List[str] , snake_case__ : int ) -> Any: '''simple docstring''' if args.student_type == "roberta": lowerCAmelCase = False elif args.student_type == "gpt2": lowerCAmelCase = False def lowercase (snake_case__ : str , snake_case__ : str ) -> Optional[Any]: '''simple docstring''' if args.student_type == "roberta": lowerCAmelCase = False def lowercase () -> Dict: '''simple docstring''' lowerCAmelCase = argparse.ArgumentParser(description="""Training""" ) parser.add_argument("""--force""" , action="""store_true""" , help="""Overwrite dump_path if it already exists.""" ) parser.add_argument( """--dump_path""" , type=snake_case__ , required=snake_case__ , help="""The output directory (log, checkpoints, parameters, etc.)""" ) parser.add_argument( """--data_file""" , type=snake_case__ , required=snake_case__ , help="""The binarized file (tokenized + tokens_to_ids) and grouped by sequence.""" , ) parser.add_argument( """--student_type""" , type=snake_case__ , choices=["""distilbert""", """roberta""", """gpt2"""] , required=snake_case__ , help="""The student type (DistilBERT, RoBERTa).""" , ) parser.add_argument("""--student_config""" , type=snake_case__ , required=snake_case__ , help="""Path to the student configuration.""" ) parser.add_argument( """--student_pretrained_weights""" , default=snake_case__ , type=snake_case__ , help="""Load student initialization checkpoint.""" ) parser.add_argument( """--teacher_type""" , choices=["""bert""", """roberta""", """gpt2"""] , required=snake_case__ , help="""Teacher type (BERT, RoBERTa).""" ) parser.add_argument("""--teacher_name""" , type=snake_case__ , required=snake_case__ , help="""The teacher model.""" ) parser.add_argument("""--temperature""" , default=2.0 , type=snake_case__ , help="""Temperature for the softmax temperature.""" ) parser.add_argument( """--alpha_ce""" , default=0.5 , type=snake_case__ , help="""Linear weight for the distillation loss. Must be >=0.""" ) parser.add_argument( """--alpha_mlm""" , default=0.0 , type=snake_case__ , help="""Linear weight for the MLM loss. Must be >=0. Should be used in conjunction with `mlm` flag.""" , ) parser.add_argument("""--alpha_clm""" , default=0.5 , type=snake_case__ , help="""Linear weight for the CLM loss. Must be >=0.""" ) parser.add_argument("""--alpha_mse""" , default=0.0 , type=snake_case__ , help="""Linear weight of the MSE loss. Must be >=0.""" ) parser.add_argument( """--alpha_cos""" , default=0.0 , type=snake_case__ , help="""Linear weight of the cosine embedding loss. Must be >=0.""" ) parser.add_argument( """--mlm""" , action="""store_true""" , help="""The LM step: MLM or CLM. If `mlm` is True, the MLM is used over CLM.""" ) parser.add_argument( """--mlm_mask_prop""" , default=0.15 , type=snake_case__ , help="""Proportion of tokens for which we need to make a prediction.""" , ) parser.add_argument("""--word_mask""" , default=0.8 , type=snake_case__ , help="""Proportion of tokens to mask out.""" ) parser.add_argument("""--word_keep""" , default=0.1 , type=snake_case__ , help="""Proportion of tokens to keep.""" ) parser.add_argument("""--word_rand""" , default=0.1 , type=snake_case__ , help="""Proportion of tokens to randomly replace.""" ) parser.add_argument( """--mlm_smoothing""" , default=0.7 , type=snake_case__ , help="""Smoothing parameter to emphasize more rare tokens (see XLM, similar to word2vec).""" , ) parser.add_argument("""--token_counts""" , type=snake_case__ , help="""The token counts in the data_file for MLM.""" ) parser.add_argument( """--restrict_ce_to_mask""" , action="""store_true""" , help="""If true, compute the distillation loss only the [MLM] prediction distribution.""" , ) parser.add_argument( """--freeze_pos_embs""" , action="""store_true""" , help="""Freeze positional embeddings during distillation. For student_type in ['roberta', 'gpt2'] only.""" , ) parser.add_argument( """--freeze_token_type_embds""" , action="""store_true""" , help="""Freeze token type embeddings during distillation if existent. For student_type in ['roberta'] only.""" , ) parser.add_argument("""--n_epoch""" , type=snake_case__ , default=3 , help="""Number of pass on the whole dataset.""" ) parser.add_argument("""--batch_size""" , type=snake_case__ , default=5 , help="""Batch size (for each process).""" ) parser.add_argument( """--group_by_size""" , action="""store_false""" , help="""If true, group sequences that have similar length into the same batch. Default is true.""" , ) parser.add_argument( """--gradient_accumulation_steps""" , type=snake_case__ , default=50 , help="""Gradient accumulation for larger training batches.""" , ) parser.add_argument("""--warmup_prop""" , default=0.05 , type=snake_case__ , help="""Linear warmup proportion.""" ) parser.add_argument("""--weight_decay""" , default=0.0 , type=snake_case__ , help="""Weight decay if we apply some.""" ) parser.add_argument("""--learning_rate""" , default=5e-4 , type=snake_case__ , help="""The initial learning rate for Adam.""" ) parser.add_argument("""--adam_epsilon""" , default=1e-6 , type=snake_case__ , help="""Epsilon for Adam optimizer.""" ) parser.add_argument("""--max_grad_norm""" , default=5.0 , type=snake_case__ , help="""Max gradient norm.""" ) parser.add_argument("""--initializer_range""" , default=0.02 , type=snake_case__ , help="""Random initialization range.""" ) parser.add_argument( """--fp16""" , action="""store_true""" , help="""Whether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bit""" , ) parser.add_argument( """--fp16_opt_level""" , type=snake_case__ , default="""O1""" , help=( """For fp16: Apex AMP optimization level selected in ['O0', 'O1', 'O2', and 'O3'].""" """See details at https://nvidia.github.io/apex/amp.html""" ) , ) parser.add_argument("""--n_gpu""" , type=snake_case__ , default=1 , help="""Number of GPUs in the node.""" ) parser.add_argument("""--local_rank""" , type=snake_case__ , default=-1 , help="""Distributed training - Local rank""" ) parser.add_argument("""--seed""" , type=snake_case__ , default=56 , help="""Random seed""" ) parser.add_argument("""--log_interval""" , type=snake_case__ , default=500 , help="""Tensorboard logging interval.""" ) parser.add_argument("""--checkpoint_interval""" , type=snake_case__ , default=4_000 , help="""Checkpoint interval.""" ) lowerCAmelCase = parser.parse_args() sanity_checks(snake_case__ ) # ARGS # init_gpu_params(snake_case__ ) set_seed(snake_case__ ) if args.is_master: if os.path.exists(args.dump_path ): if not args.force: raise ValueError( f'''Serialization dir {args.dump_path} already exists, but you have not precised wheter to overwrite''' """ itUse `--force` if you want to overwrite it""" ) else: shutil.rmtree(args.dump_path ) if not os.path.exists(args.dump_path ): os.makedirs(args.dump_path ) logger.info(f'''Experiment will be dumped and logged in {args.dump_path}''' ) # SAVE PARAMS # logger.info(f'''Param: {args}''' ) with open(os.path.join(args.dump_path , """parameters.json""" ) , """w""" ) as f: json.dump(vars(snake_case__ ) , snake_case__ , indent=4 ) git_log(args.dump_path ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = MODEL_CLASSES[args.student_type] lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = MODEL_CLASSES[args.teacher_type] # TOKENIZER # lowerCAmelCase = teacher_tokenizer_class.from_pretrained(args.teacher_name ) lowerCAmelCase = {} for tok_name, tok_symbol in tokenizer.special_tokens_map.items(): lowerCAmelCase = tokenizer.all_special_tokens.index(snake_case__ ) lowerCAmelCase = tokenizer.all_special_ids[idx] logger.info(f'''Special tokens {special_tok_ids}''' ) lowerCAmelCase = special_tok_ids lowerCAmelCase = tokenizer.max_model_input_sizes[args.teacher_name] # DATA LOADER # logger.info(f'''Loading data from {args.data_file}''' ) with open(args.data_file , """rb""" ) as fp: lowerCAmelCase = pickle.load(snake_case__ ) if args.mlm: logger.info(f'''Loading token counts from {args.token_counts} (already pre-computed)''' ) with open(args.token_counts , """rb""" ) as fp: lowerCAmelCase = pickle.load(snake_case__ ) lowerCAmelCase = np.maximum(snake_case__ , 1 ) ** -args.mlm_smoothing for idx in special_tok_ids.values(): lowerCAmelCase = 0.0 # do not predict special tokens lowerCAmelCase = torch.from_numpy(snake_case__ ) else: lowerCAmelCase = None lowerCAmelCase = LmSeqsDataset(params=snake_case__ , data=snake_case__ ) logger.info("""Data loader created.""" ) # STUDENT # logger.info(f'''Loading student config from {args.student_config}''' ) lowerCAmelCase = student_config_class.from_pretrained(args.student_config ) lowerCAmelCase = True if args.student_pretrained_weights is not None: logger.info(f'''Loading pretrained weights from {args.student_pretrained_weights}''' ) lowerCAmelCase = student_model_class.from_pretrained(args.student_pretrained_weights , config=snake_case__ ) else: lowerCAmelCase = student_model_class(snake_case__ ) if args.n_gpu > 0: student.to(f'''cuda:{args.local_rank}''' ) logger.info("""Student loaded.""" ) # TEACHER # lowerCAmelCase = teacher_model_class.from_pretrained(args.teacher_name , output_hidden_states=snake_case__ ) if args.n_gpu > 0: teacher.to(f'''cuda:{args.local_rank}''' ) logger.info(f'''Teacher loaded from {args.teacher_name}.''' ) # FREEZING # if args.freeze_pos_embs: freeze_pos_embeddings(snake_case__ , snake_case__ ) if args.freeze_token_type_embds: freeze_token_type_embeddings(snake_case__ , snake_case__ ) # SANITY CHECKS # assert student.config.vocab_size == teacher.config.vocab_size assert student.config.hidden_size == teacher.config.hidden_size assert student.config.max_position_embeddings == teacher.config.max_position_embeddings if args.mlm: assert token_probs.size(0 ) == stu_architecture_config.vocab_size # DISTILLER # torch.cuda.empty_cache() lowerCAmelCase = Distiller( params=snake_case__ , dataset=snake_case__ , token_probs=snake_case__ , student=snake_case__ , teacher=snake_case__ ) distiller.train() logger.info("""Let's go get some drinks.""" ) if __name__ == "__main__": main()
529
"""simple docstring""" import random import timeit from functools import wraps from typing import Callable, Optional from ..configuration_utils import PretrainedConfig from ..models.auto.modeling_tf_auto import TF_MODEL_MAPPING, TF_MODEL_WITH_LM_HEAD_MAPPING from ..utils import is_pyanvml_available, is_tf_available, logging from .benchmark_utils import ( Benchmark, Memory, MemorySummary, measure_peak_memory_cpu, start_memory_tracing, stop_memory_tracing, ) if is_tf_available(): import tensorflow as tf from tensorflow.python.framework.errors_impl import ResourceExhaustedError from .benchmark_args_tf import TensorFlowBenchmarkArguments if is_pyanvml_available(): import pyanvml.pyanvml as nvml a = logging.get_logger(__name__) def lowercase (snake_case__ : bool , snake_case__ : bool ) -> Tuple: '''simple docstring''' def run_func(snake_case__ : Any ): @wraps(snake_case__ ) def run_in_eager_mode(*snake_case__ : Optional[Any] , **snake_case__ : int ): return func(*snake_case__ , **snake_case__ ) @wraps(snake_case__ ) @tf.function(experimental_compile=snake_case__ ) def run_in_graph_mode(*snake_case__ : int , **snake_case__ : Tuple ): return func(*snake_case__ , **snake_case__ ) if do_eager_mode is True: if use_xla is not False: raise ValueError( """Cannot run model in XLA, if `args.eager_mode` is set to `True`. Please set `args.eager_mode=False`.""" ) return run_in_eager_mode else: return run_in_graph_mode return run_func def lowercase (snake_case__ : int , snake_case__ : int , snake_case__ : int ) -> ["tf.Tensor"]: '''simple docstring''' lowerCAmelCase = random.Random() lowerCAmelCase = [rng.randint(0 , vocab_size - 1 ) for i in range(batch_size * sequence_length )] return tf.constant(snake_case__ , shape=(batch_size, sequence_length) , dtype=tf.intaa ) class SCREAMING_SNAKE_CASE__ ( _a ): _a = 42 _a = 42 _a = "TensorFlow" @property def __lowercase ( self : Optional[int] ): return tf.__version__ def __lowercase ( self : Optional[Any] , lowerCAmelCase : str , lowerCAmelCase : int , lowerCAmelCase : int ): # initialize GPU on separate process lowerCAmelCase = self.args.strategy if strategy is None: raise ValueError("""A device strategy has to be initialized before using TensorFlow.""" ) lowerCAmelCase = self._prepare_inference_func(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ) return self._measure_speed(_inference ) def __lowercase ( self : str , lowerCAmelCase : str , lowerCAmelCase : int , lowerCAmelCase : int ): lowerCAmelCase = self.args.strategy if strategy is None: raise ValueError("""A device strategy has to be initialized before using TensorFlow.""" ) lowerCAmelCase = self._prepare_train_func(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ) return self._measure_speed(_train ) def __lowercase ( self : str , lowerCAmelCase : str , lowerCAmelCase : int , lowerCAmelCase : int ): # initialize GPU on separate process if self.args.is_gpu: tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx] , lowerCAmelCase ) lowerCAmelCase = self.args.strategy if strategy is None: raise ValueError("""A device strategy has to be initialized before using TensorFlow.""" ) lowerCAmelCase = self._prepare_inference_func(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ) return self._measure_memory(_inference ) def __lowercase ( self : int , lowerCAmelCase : str , lowerCAmelCase : int , lowerCAmelCase : int ): if self.args.is_gpu: tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx] , lowerCAmelCase ) lowerCAmelCase = self.args.strategy if strategy is None: raise ValueError("""A device strategy has to be initialized before using TensorFlow.""" ) lowerCAmelCase = self._prepare_train_func(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ) return self._measure_memory(_train ) def __lowercase ( self : List[str] , lowerCAmelCase : str , lowerCAmelCase : int , lowerCAmelCase : int ): lowerCAmelCase = self.config_dict[model_name] if self.args.fpaa: raise NotImplementedError("""Mixed precision is currently not supported.""" ) lowerCAmelCase = ( hasattr(lowerCAmelCase , """architectures""" ) and isinstance(config.architectures , lowerCAmelCase ) and len(config.architectures ) > 0 ) if not self.args.only_pretrain_model and has_model_class_in_config: try: lowerCAmelCase = """TF""" + config.architectures[0] # prepend 'TF' for tensorflow model lowerCAmelCase = __import__("""transformers""" , fromlist=[model_class] ) lowerCAmelCase = getattr(lowerCAmelCase , lowerCAmelCase ) lowerCAmelCase = model_cls(lowerCAmelCase ) except ImportError: raise ImportError( f'''{model_class} does not exist. If you just want to test the pretrained model, you might want to''' """ set `--only_pretrain_model` or `args.only_pretrain_model=True`.""" ) else: lowerCAmelCase = TF_MODEL_MAPPING[config.__class__](lowerCAmelCase ) # encoder-decoder has vocab size saved differently lowerCAmelCase = config.vocab_size if hasattr(lowerCAmelCase , """vocab_size""" ) else config.encoder.vocab_size lowerCAmelCase = random_input_ids(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ) @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla ) def encoder_decoder_forward(): return model(lowerCAmelCase , decoder_input_ids=lowerCAmelCase , training=lowerCAmelCase ) @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla ) def encoder_forward(): return model(lowerCAmelCase , training=lowerCAmelCase ) lowerCAmelCase = encoder_decoder_forward if config.is_encoder_decoder else encoder_forward return _inference def __lowercase ( self : Optional[int] , lowerCAmelCase : str , lowerCAmelCase : int , lowerCAmelCase : int ): lowerCAmelCase = self.config_dict[model_name] if self.args.eager_mode is not False: raise ValueError("""Training cannot be done in eager mode. Please make sure that `args.eager_mode = False`.""" ) if self.args.fpaa: raise NotImplementedError("""Mixed precision is currently not supported.""" ) lowerCAmelCase = ( hasattr(lowerCAmelCase , """architectures""" ) and isinstance(config.architectures , lowerCAmelCase ) and len(config.architectures ) > 0 ) if not self.args.only_pretrain_model and has_model_class_in_config: try: lowerCAmelCase = """TF""" + config.architectures[0] # prepend 'TF' for tensorflow model lowerCAmelCase = __import__("""transformers""" , fromlist=[model_class] ) lowerCAmelCase = getattr(lowerCAmelCase , lowerCAmelCase ) lowerCAmelCase = model_cls(lowerCAmelCase ) except ImportError: raise ImportError( f'''{model_class} does not exist. If you just want to test the pretrained model, you might want to''' """ set `--only_pretrain_model` or `args.only_pretrain_model=True`.""" ) else: lowerCAmelCase = TF_MODEL_WITH_LM_HEAD_MAPPING[config.__class__](lowerCAmelCase ) # encoder-decoder has vocab size saved differently lowerCAmelCase = config.vocab_size if hasattr(lowerCAmelCase , """vocab_size""" ) else config.encoder.vocab_size lowerCAmelCase = random_input_ids(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ) @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla ) def encoder_decoder_train(): lowerCAmelCase = model(lowerCAmelCase , decoder_input_ids=lowerCAmelCase , labels=lowerCAmelCase , training=lowerCAmelCase )[0] lowerCAmelCase = tf.gradients(lowerCAmelCase , model.trainable_variables ) return gradients @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla ) def encoder_train(): lowerCAmelCase = model(lowerCAmelCase , labels=lowerCAmelCase , training=lowerCAmelCase )[0] lowerCAmelCase = tf.gradients(lowerCAmelCase , model.trainable_variables ) return gradients lowerCAmelCase = encoder_decoder_train if config.is_encoder_decoder else encoder_train return _train def __lowercase ( self : Optional[int] , lowerCAmelCase : List[str] ): with self.args.strategy.scope(): try: if self.args.is_tpu or self.args.use_xla: # run additional 10 times to stabilize compilation for tpu logger.info("""Do inference on TPU. Running model 5 times to stabilize compilation""" ) timeit.repeat(lowerCAmelCase , repeat=1 , number=5 ) # as written in https://docs.python.org/2/library/timeit.html#timeit.Timer.repeat, min should be taken rather than the average lowerCAmelCase = timeit.repeat( lowerCAmelCase , repeat=self.args.repeat , number=10 , ) return min(lowerCAmelCase ) / 10.0 except ResourceExhaustedError as e: self.print_fn(f'''Doesn\'t fit on GPU. {e}''' ) def __lowercase ( self : Optional[Any] , lowerCAmelCase : Callable[[], None] ): logger.info( """Note that TensorFlow allocates more memory than """ """it might need to speed up computation. """ """The memory reported here corresponds to the memory """ """reported by `nvidia-smi`, which can vary depending """ """on total available memory on the GPU that is used.""" ) with self.args.strategy.scope(): try: if self.args.trace_memory_line_by_line: if not self.args.eager_mode: raise ValueError( """`args.eager_mode` is set to `False`. Make sure to run model in eager mode to measure memory""" """ consumption line by line.""" ) lowerCAmelCase = start_memory_tracing("""transformers""" ) if self.args.is_tpu: # tpu raise NotImplementedError( """Memory Benchmarking is currently not implemented for TPU. Please disable memory benchmarking""" """ with `args.memory=False`""" ) elif self.args.is_gpu: # gpu if not is_pyanvml_available(): logger.warning( """py3nvml not installed, we won't log GPU memory usage. """ """Install py3nvml (pip install py3nvml) to log information about GPU.""" ) lowerCAmelCase = """N/A""" else: logger.info( """Measuring total GPU usage on GPU device. Make sure to not have additional processes""" """ running on the same GPU.""" ) # init nvml nvml.nvmlInit() func() lowerCAmelCase = nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx ) lowerCAmelCase = nvml.nvmlDeviceGetMemoryInfo(lowerCAmelCase ) lowerCAmelCase = meminfo.used lowerCAmelCase = Memory(lowerCAmelCase ) # shutdown nvml nvml.nvmlShutdown() else: # cpu if self.args.trace_memory_line_by_line: logger.info( """When enabling line by line tracing, the max peak memory for CPU is inaccurate in""" """ TensorFlow.""" ) lowerCAmelCase = None else: lowerCAmelCase = measure_peak_memory_cpu(lowerCAmelCase ) lowerCAmelCase = Memory(lowerCAmelCase ) if isinstance(lowerCAmelCase , lowerCAmelCase ) else memory_bytes if self.args.trace_memory_line_by_line: lowerCAmelCase = stop_memory_tracing(lowerCAmelCase ) if memory is None: lowerCAmelCase = summary.total else: lowerCAmelCase = None return memory, summary except ResourceExhaustedError as e: self.print_fn(f'''Doesn\'t fit on GPU. {e}''' ) return "N/A", None
529
1
from __future__ import annotations def _lowerCAmelCase ( __lowerCAmelCase , __lowerCAmelCase ) -> Union[str, Any]: """simple docstring""" if len(lowerCamelCase__ ) <= 1 or n <= 1: return insert_next(lowerCamelCase__ , n - 1 ) rec_insertion_sort(lowerCamelCase__ , n - 1 ) def _lowerCAmelCase ( __lowerCAmelCase , __lowerCAmelCase ) -> str: """simple docstring""" if index >= len(lowerCamelCase__ ) or collection[index - 1] <= collection[index]: return # Swaps adjacent elements since they are not in ascending order snake_case__ , snake_case__ : Optional[Any] = ( collection[index], collection[index - 1], ) insert_next(lowerCamelCase__ , index + 1 ) if __name__ == "__main__": A__ = input('''Enter integers separated by spaces: ''') A__ = [int(num) for num in numbers.split()] rec_insertion_sort(number_list, len(number_list)) print(number_list)
252
'''simple docstring''' from io import BytesIO from typing import List, Union import requests from ..utils import add_end_docstrings, is_decord_available, is_torch_available, logging, requires_backends from .base import PIPELINE_INIT_ARGS, Pipeline if is_decord_available(): import numpy as np from decord import VideoReader if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING __snake_case : Dict = logging.get_logger(__name__) @add_end_docstrings(a ) class A ( a ): def __init__( self , *snake_case_ , **snake_case_ ) -> int: super().__init__(*snake_case_ , **snake_case_ ) requires_backends(self , "decord" ) self.check_model_type(snake_case_ ) def __lowerCAmelCase ( self , snake_case_=None , snake_case_=None , snake_case_=None ) -> Optional[Any]: _a = {} if frame_sampling_rate is not None: _a = frame_sampling_rate if num_frames is not None: _a = num_frames _a = {} if top_k is not None: _a = top_k return preprocess_params, {}, postprocess_params def __call__( self , snake_case_ , **snake_case_ ) -> int: return super().__call__(snake_case_ , **snake_case_ ) def __lowerCAmelCase ( self , snake_case_ , snake_case_=None , snake_case_=1 ) -> List[str]: if num_frames is None: _a = self.model.config.num_frames if video.startswith("http://" ) or video.startswith("https://" ): _a = BytesIO(requests.get(snake_case_ ).content ) _a = VideoReader(snake_case_ ) videoreader.seek(0 ) _a = 0 _a = num_frames * frame_sampling_rate - 1 _a = np.linspace(snake_case_ , snake_case_ , num=snake_case_ , dtype=np.intaa ) _a = videoreader.get_batch(snake_case_ ).asnumpy() _a = list(snake_case_ ) _a = self.image_processor(snake_case_ , return_tensors=self.framework ) return model_inputs def __lowerCAmelCase ( self , snake_case_ ) -> Dict: _a = self.model(**snake_case_ ) return model_outputs def __lowerCAmelCase ( self , snake_case_ , snake_case_=5 ) -> Optional[Any]: if top_k > self.model.config.num_labels: _a = self.model.config.num_labels if self.framework == "pt": _a = model_outputs.logits.softmax(-1 )[0] _a , _a = probs.topk(snake_case_ ) else: raise ValueError(F'''Unsupported framework: {self.framework}''' ) _a = scores.tolist() _a = ids.tolist() return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(snake_case_ , snake_case_ )]
131
0
"""simple docstring""" import math UpperCamelCase_ : Optional[int] = 10 UpperCamelCase_ : int = 7 UpperCamelCase_ : List[Any] = BALLS_PER_COLOUR * NUM_COLOURS def __lowercase ( a : int = 20 ) -> Any: __snake_case : Dict =math.comb(a , a ) __snake_case : Any =math.comb(NUM_BALLS - BALLS_PER_COLOUR , a ) __snake_case : Dict =NUM_COLOURS * (1 - missing_colour / total) return f'''{result:.9f}''' if __name__ == "__main__": print(solution(20))
705
"""simple docstring""" import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging UpperCamelCase_ : Optional[Any] = logging.get_logger(__name__) UpperCamelCase_ : Union[str, Any] = {"""vocab_file""": """sentencepiece.bpe.model"""} UpperCamelCase_ : Optional[Any] = { """vocab_file""": { """moussaKam/mbarthez""": """https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model""", """moussaKam/barthez""": """https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model""", """moussaKam/barthez-orangesum-title""": ( """https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model""" ), }, } UpperCamelCase_ : int = { """moussaKam/mbarthez""": 1024, """moussaKam/barthez""": 1024, """moussaKam/barthez-orangesum-title""": 1024, } UpperCamelCase_ : Optional[int] = """▁""" class _lowercase ( lowerCAmelCase ): _a : int = VOCAB_FILES_NAMES _a : int = PRETRAINED_VOCAB_FILES_MAP _a : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _a : Optional[int] = ['''input_ids''', '''attention_mask'''] def __init__( self : str , a : Optional[Any] , a : Optional[Any]="<s>" , a : Dict="</s>" , a : Any="</s>" , a : Optional[int]="<s>" , a : Optional[Any]="<unk>" , a : int="<pad>" , a : Tuple="<mask>" , a : Optional[Dict[str, Any]] = None , **a : List[Any] , ): """simple docstring""" __snake_case : Dict =AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else mask_token __snake_case : List[str] ={} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=a , eos_token=a , unk_token=a , sep_token=a , cls_token=a , pad_token=a , mask_token=a , sp_model_kwargs=self.sp_model_kwargs , **a , ) __snake_case : Optional[int] =vocab_file __snake_case : Dict =spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(a ) ) __snake_case : Optional[Any] ={'''<s>''': 0, '''<pad>''': 1, '''</s>''': 2, '''<unk>''': 3} __snake_case : Optional[int] =len(self.sp_model ) - 1 __snake_case : Union[str, Any] ={v: k for k, v in self.fairseq_tokens_to_ids.items()} def _UpperCamelCase ( self : Tuple , a : List[int] , a : Optional[List[int]] = None ): """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __snake_case : Dict =[self.cls_token_id] __snake_case : List[str] =[self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def _UpperCamelCase ( self : str , a : List[int] , a : Optional[List[int]] = None , a : bool = False ): """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=a , token_ids_a=a , already_has_special_tokens=a ) if token_ids_a is None: return [1] + ([0] * len(a )) + [1] return [1] + ([0] * len(a )) + [1, 1] + ([0] * len(a )) + [1] def _UpperCamelCase ( self : Dict , a : List[int] , a : Optional[List[int]] = None ): """simple docstring""" __snake_case : Optional[int] =[self.sep_token_id] __snake_case : int =[self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def _UpperCamelCase ( self : List[Any] ): """simple docstring""" return len(self.sp_model ) def _UpperCamelCase ( self : Union[str, Any] ): """simple docstring""" __snake_case : List[Any] ={self.convert_ids_to_tokens(a ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def _UpperCamelCase ( self : str , a : str ): """simple docstring""" return self.sp_model.encode(a , out_type=a ) def _UpperCamelCase ( self : int , a : Union[str, Any] ): """simple docstring""" if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] __snake_case : List[Any] =self.sp_model.PieceToId(a ) return spm_id if spm_id else self.unk_token_id def _UpperCamelCase ( self : List[str] , a : Dict ): """simple docstring""" if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(a ) def _UpperCamelCase ( self : Optional[int] , a : int ): """simple docstring""" __snake_case : int =[] __snake_case : Optional[int] ='''''' __snake_case : Optional[int] =False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(a ) + token __snake_case : str =True __snake_case : int =[] else: current_sub_tokens.append(a ) __snake_case : Tuple =False out_string += self.sp_model.decode(a ) return out_string.strip() def __getstate__( self : Optional[int] ): """simple docstring""" __snake_case : List[Any] =self.__dict__.copy() __snake_case : Optional[Any] =None return state def __setstate__( self : Optional[int] , a : Optional[Any] ): """simple docstring""" __snake_case : Any =d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): __snake_case : Tuple ={} __snake_case : int =spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def _UpperCamelCase ( self : Optional[Any] , a : str , a : Optional[str] = None ): """simple docstring""" if not os.path.isdir(a ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return __snake_case : int =os.path.join( a , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(a ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , a ) elif not os.path.isfile(self.vocab_file ): with open(a , '''wb''' ) as fi: __snake_case : List[Any] =self.sp_model.serialized_model_proto() fi.write(a ) return (out_vocab_file,)
497
0
import os import tempfile import unittest from transformers import FlaubertConfig, is_torch_available from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( FlaubertForMultipleChoice, FlaubertForQuestionAnswering, FlaubertForQuestionAnsweringSimple, FlaubertForSequenceClassification, FlaubertForTokenClassification, FlaubertModel, FlaubertWithLMHeadModel, ) from transformers.models.flaubert.modeling_flaubert import FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST class _a ( UpperCamelCase__ ): def __init__( self: Optional[int] , UpperCamelCase_: Dict , UpperCamelCase_: Optional[int]=13 , UpperCamelCase_: List[Any]=7 , UpperCamelCase_: Tuple=True , UpperCamelCase_: List[Any]=True , UpperCamelCase_: Dict=True , UpperCamelCase_: Optional[Any]=True , UpperCamelCase_: Dict=True , UpperCamelCase_: Optional[int]=False , UpperCamelCase_: str=False , UpperCamelCase_: Any=False , UpperCamelCase_: int=2 , UpperCamelCase_: Dict=99 , UpperCamelCase_: Tuple=0 , UpperCamelCase_: Tuple=32 , UpperCamelCase_: List[str]=5 , UpperCamelCase_: Tuple=4 , UpperCamelCase_: Union[str, Any]=0.1 , UpperCamelCase_: Tuple=0.1 , UpperCamelCase_: Any=512 , UpperCamelCase_: Union[str, Any]=12 , UpperCamelCase_: Optional[Any]=2 , UpperCamelCase_: Any=0.02 , UpperCamelCase_: Tuple=3 , UpperCamelCase_: Optional[int]=4 , UpperCamelCase_: Dict="last" , UpperCamelCase_: Optional[Any]=None , UpperCamelCase_: Union[str, Any]=None , ) -> List[str]: """simple docstring""" lowercase__ = parent lowercase__ = batch_size lowercase__ = seq_length lowercase__ = is_training lowercase__ = use_input_lengths lowercase__ = use_token_type_ids lowercase__ = use_labels lowercase__ = gelu_activation lowercase__ = sinusoidal_embeddings lowercase__ = causal lowercase__ = asm lowercase__ = n_langs lowercase__ = vocab_size lowercase__ = n_special lowercase__ = hidden_size lowercase__ = num_hidden_layers lowercase__ = num_attention_heads lowercase__ = hidden_dropout_prob lowercase__ = attention_probs_dropout_prob lowercase__ = max_position_embeddings lowercase__ = type_vocab_size lowercase__ = type_sequence_label_size lowercase__ = initializer_range lowercase__ = num_labels lowercase__ = num_choices lowercase__ = summary_type lowercase__ = use_proj lowercase__ = scope def lowerCamelCase_ ( self: Optional[Any] ) -> str: """simple docstring""" lowercase__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowercase__ = random_attention_mask([self.batch_size, self.seq_length] ) lowercase__ = None if self.use_input_lengths: lowercase__ = ( ids_tensor([self.batch_size] , vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length lowercase__ = None if self.use_token_type_ids: lowercase__ = ids_tensor([self.batch_size, self.seq_length] , self.n_langs ) lowercase__ = None lowercase__ = None lowercase__ = None if self.use_labels: lowercase__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowercase__ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) lowercase__ = ids_tensor([self.batch_size] , 2 ).float() lowercase__ = ids_tensor([self.batch_size] , self.num_choices ) lowercase__ = self.get_config() return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def lowerCamelCase_ ( self: int ) -> Optional[Any]: """simple docstring""" return FlaubertConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , ) def lowerCamelCase_ ( self: Optional[int] , UpperCamelCase_: Tuple , UpperCamelCase_: List[Any] , UpperCamelCase_: Union[str, Any] , UpperCamelCase_: List[Any] , UpperCamelCase_: List[Any] , UpperCamelCase_: Optional[int] , UpperCamelCase_: Dict , UpperCamelCase_: Any , UpperCamelCase_: Any , ) -> Optional[int]: """simple docstring""" lowercase__ = FlaubertModel(config=UpperCamelCase_ ) model.to(UpperCamelCase_ ) model.eval() lowercase__ = model(UpperCamelCase_ , lengths=UpperCamelCase_ , langs=UpperCamelCase_ ) lowercase__ = model(UpperCamelCase_ , langs=UpperCamelCase_ ) lowercase__ = model(UpperCamelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowerCamelCase_ ( self: Any , UpperCamelCase_: Tuple , UpperCamelCase_: List[str] , UpperCamelCase_: List[Any] , UpperCamelCase_: Optional[int] , UpperCamelCase_: Dict , UpperCamelCase_: Dict , UpperCamelCase_: str , UpperCamelCase_: Optional[int] , UpperCamelCase_: Any , ) -> str: """simple docstring""" lowercase__ = FlaubertWithLMHeadModel(UpperCamelCase_ ) model.to(UpperCamelCase_ ) model.eval() lowercase__ = model(UpperCamelCase_ , token_type_ids=UpperCamelCase_ , labels=UpperCamelCase_ ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowerCamelCase_ ( self: str , UpperCamelCase_: Any , UpperCamelCase_: Optional[int] , UpperCamelCase_: Optional[int] , UpperCamelCase_: int , UpperCamelCase_: List[str] , UpperCamelCase_: Tuple , UpperCamelCase_: List[str] , UpperCamelCase_: List[Any] , UpperCamelCase_: Optional[Any] , ) -> Dict: """simple docstring""" lowercase__ = FlaubertForQuestionAnsweringSimple(UpperCamelCase_ ) model.to(UpperCamelCase_ ) model.eval() lowercase__ = model(UpperCamelCase_ ) lowercase__ = model(UpperCamelCase_ , start_positions=UpperCamelCase_ , end_positions=UpperCamelCase_ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def lowerCamelCase_ ( self: Optional[int] , UpperCamelCase_: Any , UpperCamelCase_: Union[str, Any] , UpperCamelCase_: Optional[int] , UpperCamelCase_: str , UpperCamelCase_: Optional[int] , UpperCamelCase_: Optional[int] , UpperCamelCase_: List[str] , UpperCamelCase_: Any , UpperCamelCase_: int , ) -> Optional[int]: """simple docstring""" lowercase__ = FlaubertForQuestionAnswering(UpperCamelCase_ ) model.to(UpperCamelCase_ ) model.eval() lowercase__ = model(UpperCamelCase_ ) lowercase__ = model( UpperCamelCase_ , start_positions=UpperCamelCase_ , end_positions=UpperCamelCase_ , cls_index=UpperCamelCase_ , is_impossible=UpperCamelCase_ , p_mask=UpperCamelCase_ , ) lowercase__ = model( UpperCamelCase_ , start_positions=UpperCamelCase_ , end_positions=UpperCamelCase_ , cls_index=UpperCamelCase_ , is_impossible=UpperCamelCase_ , ) ((lowercase__) , ) = result_with_labels.to_tuple() lowercase__ = model(UpperCamelCase_ , start_positions=UpperCamelCase_ , end_positions=UpperCamelCase_ ) ((lowercase__) , ) = result_with_labels.to_tuple() self.parent.assertEqual(result_with_labels.loss.shape , () ) self.parent.assertEqual(result.start_top_log_probs.shape , (self.batch_size, model.config.start_n_top) ) self.parent.assertEqual(result.start_top_index.shape , (self.batch_size, model.config.start_n_top) ) self.parent.assertEqual( result.end_top_log_probs.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual( result.end_top_index.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual(result.cls_logits.shape , (self.batch_size,) ) def lowerCamelCase_ ( self: List[Any] , UpperCamelCase_: Any , UpperCamelCase_: Dict , UpperCamelCase_: str , UpperCamelCase_: Optional[int] , UpperCamelCase_: str , UpperCamelCase_: str , UpperCamelCase_: Any , UpperCamelCase_: Optional[Any] , UpperCamelCase_: Tuple , ) -> Optional[Any]: """simple docstring""" lowercase__ = FlaubertForSequenceClassification(UpperCamelCase_ ) model.to(UpperCamelCase_ ) model.eval() lowercase__ = model(UpperCamelCase_ ) lowercase__ = model(UpperCamelCase_ , labels=UpperCamelCase_ ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def lowerCamelCase_ ( self: Optional[int] , UpperCamelCase_: Dict , UpperCamelCase_: Optional[Any] , UpperCamelCase_: Dict , UpperCamelCase_: int , UpperCamelCase_: Union[str, Any] , UpperCamelCase_: Union[str, Any] , UpperCamelCase_: Any , UpperCamelCase_: List[str] , UpperCamelCase_: List[str] , ) -> int: """simple docstring""" lowercase__ = self.num_labels lowercase__ = FlaubertForTokenClassification(UpperCamelCase_ ) model.to(UpperCamelCase_ ) model.eval() lowercase__ = model(UpperCamelCase_ , attention_mask=UpperCamelCase_ , labels=UpperCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowerCamelCase_ ( self: Any , UpperCamelCase_: Optional[int] , UpperCamelCase_: Union[str, Any] , UpperCamelCase_: Optional[Any] , UpperCamelCase_: Optional[int] , UpperCamelCase_: Any , UpperCamelCase_: Dict , UpperCamelCase_: Optional[Any] , UpperCamelCase_: Tuple , UpperCamelCase_: int , ) -> List[str]: """simple docstring""" lowercase__ = self.num_choices lowercase__ = FlaubertForMultipleChoice(config=UpperCamelCase_ ) model.to(UpperCamelCase_ ) model.eval() lowercase__ = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowercase__ = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowercase__ = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowercase__ = model( UpperCamelCase_ , attention_mask=UpperCamelCase_ , token_type_ids=UpperCamelCase_ , labels=UpperCamelCase_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def lowerCamelCase_ ( self: List[str] ) -> Optional[int]: """simple docstring""" lowercase__ = self.prepare_config_and_inputs() ( ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ) = config_and_inputs lowercase__ = { '''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''lengths''': input_lengths, '''attention_mask''': input_mask, } return config, inputs_dict @require_torch class _a ( UpperCamelCase__ , UpperCamelCase__ , unittest.TestCase ): _lowercase : Optional[int] = ( ( FlaubertModel, FlaubertWithLMHeadModel, FlaubertForQuestionAnswering, FlaubertForQuestionAnsweringSimple, FlaubertForSequenceClassification, FlaubertForTokenClassification, FlaubertForMultipleChoice, ) if is_torch_available() else () ) _lowercase : str = ( { '''feature-extraction''': FlaubertModel, '''fill-mask''': FlaubertWithLMHeadModel, '''question-answering''': FlaubertForQuestionAnsweringSimple, '''text-classification''': FlaubertForSequenceClassification, '''token-classification''': FlaubertForTokenClassification, '''zero-shot''': FlaubertForSequenceClassification, } if is_torch_available() else {} ) def lowerCamelCase_ ( self: Any , UpperCamelCase_: str , UpperCamelCase_: Union[str, Any] , UpperCamelCase_: Tuple , UpperCamelCase_: Dict , UpperCamelCase_: Any ) -> Dict: """simple docstring""" if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith('''Fast''' ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def lowerCamelCase_ ( self: Tuple , UpperCamelCase_: Optional[int] , UpperCamelCase_: int , UpperCamelCase_: Any=False ) -> Tuple: """simple docstring""" lowercase__ = super()._prepare_for_class(UpperCamelCase_ , UpperCamelCase_ , return_labels=UpperCamelCase_ ) if return_labels: if model_class.__name__ == "FlaubertForQuestionAnswering": lowercase__ = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=UpperCamelCase_ ) lowercase__ = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=UpperCamelCase_ ) return inputs_dict def lowerCamelCase_ ( self: Any ) -> Dict: """simple docstring""" lowercase__ = FlaubertModelTester(self ) lowercase__ = ConfigTester(self , config_class=UpperCamelCase_ , emb_dim=37 ) def lowerCamelCase_ ( self: int ) -> int: """simple docstring""" self.config_tester.run_common_tests() def lowerCamelCase_ ( self: str ) -> Union[str, Any]: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*UpperCamelCase_ ) def lowerCamelCase_ ( self: List[Any] ) -> List[str]: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*UpperCamelCase_ ) def lowerCamelCase_ ( self: int ) -> Union[str, Any]: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_simple_qa(*UpperCamelCase_ ) def lowerCamelCase_ ( self: Union[str, Any] ) -> str: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*UpperCamelCase_ ) def lowerCamelCase_ ( self: Optional[int] ) -> List[Any]: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*UpperCamelCase_ ) def lowerCamelCase_ ( self: Union[str, Any] ) -> List[Any]: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_token_classif(*UpperCamelCase_ ) def lowerCamelCase_ ( self: int ) -> Tuple: """simple docstring""" lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_multiple_choice(*UpperCamelCase_ ) @slow def lowerCamelCase_ ( self: int ) -> List[str]: """simple docstring""" for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase__ = FlaubertModel.from_pretrained(UpperCamelCase_ ) self.assertIsNotNone(UpperCamelCase_ ) @slow @require_torch_gpu def lowerCamelCase_ ( self: Union[str, Any] ) -> List[Any]: """simple docstring""" lowercase__ , lowercase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: # FlauBertForMultipleChoice behaves incorrectly in JIT environments. if model_class == FlaubertForMultipleChoice: return lowercase__ = True lowercase__ = model_class(config=UpperCamelCase_ ) lowercase__ = self._prepare_for_class(UpperCamelCase_ , UpperCamelCase_ ) lowercase__ = torch.jit.trace( UpperCamelCase_ , (inputs_dict['''input_ids'''].to('''cpu''' ), inputs_dict['''attention_mask'''].to('''cpu''' )) ) with tempfile.TemporaryDirectory() as tmp: torch.jit.save(UpperCamelCase_ , os.path.join(UpperCamelCase_ , '''traced_model.pt''' ) ) lowercase__ = torch.jit.load(os.path.join(UpperCamelCase_ , '''traced_model.pt''' ) , map_location=UpperCamelCase_ ) loaded(inputs_dict['''input_ids'''].to(UpperCamelCase_ ) , inputs_dict['''attention_mask'''].to(UpperCamelCase_ ) ) @require_torch class _a ( unittest.TestCase ): @slow def lowerCamelCase_ ( self: Any ) -> Optional[int]: """simple docstring""" lowercase__ = FlaubertModel.from_pretrained('''flaubert/flaubert_base_cased''' ) lowercase__ = torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]] ) with torch.no_grad(): lowercase__ = model(UpperCamelCase_ )[0] lowercase__ = torch.Size((1, 11, 768) ) self.assertEqual(output.shape , UpperCamelCase_ ) lowercase__ = torch.tensor( [[[-2.6251, -1.4298, -0.0227], [-2.8510, -1.6387, 0.2258], [-2.8114, -1.1832, -0.3066]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , UpperCamelCase_ , atol=1E-4 ) )
43
class _a : def __init__( self: Tuple , UpperCamelCase_: Dict ) -> List[str]: """simple docstring""" lowercase__ = val lowercase__ = None lowercase__ = None def lowerCamelCase_ ( self: Any , UpperCamelCase_: Any ) -> Union[str, Any]: """simple docstring""" if self.val: if val < self.val: if self.left is None: lowercase__ = Node(UpperCamelCase_ ) else: self.left.insert(UpperCamelCase_ ) elif val > self.val: if self.right is None: lowercase__ = Node(UpperCamelCase_ ) else: self.right.insert(UpperCamelCase_ ) else: lowercase__ = val def _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): """simple docstring""" if root: inorder(root.left , SCREAMING_SNAKE_CASE ) res.append(root.val ) inorder(root.right , SCREAMING_SNAKE_CASE ) def _a ( SCREAMING_SNAKE_CASE ): """simple docstring""" if len(SCREAMING_SNAKE_CASE ) == 0: return arr lowercase__ = Node(arr[0] ) for i in range(1 , len(SCREAMING_SNAKE_CASE ) ): root.insert(arr[i] ) # Traverse BST in order. lowercase__ = [] inorder(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) return res if __name__ == "__main__": print(tree_sort([10, 1, 3, 2, 9, 14, 13]))
43
1
"""simple docstring""" class _SCREAMING_SNAKE_CASE : """simple docstring""" def __init__( self: Optional[int] , __A: str = "" , __A: bool = False ): '''simple docstring''' a__ = {} # A node will be a leaf if the tree contains its word a__ = is_leaf a__ = prefix def lowercase ( self: Optional[Any] , __A: str ): '''simple docstring''' a__ = 0 for q, w in zip(self.prefix , __A ): if q != w: break x += 1 return self.prefix[:x], self.prefix[x:], word[x:] def lowercase ( self: Optional[int] , __A: list[str] ): '''simple docstring''' for word in words: self.insert(__A ) def lowercase ( self: Any , __A: str ): '''simple docstring''' if self.prefix == word: a__ = True # Case 2: The node has no edges that have a prefix to the word # Solution: We create an edge from the current node to a new one # containing the word elif word[0] not in self.nodes: a__ = RadixNode(prefix=__A , is_leaf=__A ) else: a__ = self.nodes[word[0]] a__ ,a__ ,a__ = incoming_node.match( __A ) # Case 3: The node prefix is equal to the matching # Solution: We insert remaining word on the next node if remaining_prefix == "": self.nodes[matching_string[0]].insert(__A ) # Case 4: The word is greater equal to the matching # Solution: Create a node in between both nodes, change # prefixes and add the new node for the remaining word else: a__ = remaining_prefix a__ = self.nodes[matching_string[0]] a__ = RadixNode(__A , __A ) a__ = aux_node if remaining_word == "": a__ = True else: self.nodes[matching_string[0]].insert(__A ) def lowercase ( self: List[Any] , __A: str ): '''simple docstring''' a__ = self.nodes.get(word[0] , __A ) if not incoming_node: return False else: a__ ,a__ ,a__ = incoming_node.match( __A ) # If there is remaining prefix, the word can't be on the tree if remaining_prefix != "": return False # This applies when the word and the prefix are equal elif remaining_word == "": return incoming_node.is_leaf # We have word remaining so we check the next node else: return incoming_node.find(__A ) def lowercase ( self: Optional[int] , __A: str ): '''simple docstring''' a__ = self.nodes.get(word[0] , __A ) if not incoming_node: return False else: a__ ,a__ ,a__ = incoming_node.match( __A ) # If there is remaining prefix, the word can't be on the tree if remaining_prefix != "": return False # We have word remaining so we check the next node elif remaining_word != "": return incoming_node.delete(__A ) else: # If it is not a leaf, we don't have to delete if not incoming_node.is_leaf: return False else: # We delete the nodes if no edges go from it if len(incoming_node.nodes ) == 0: del self.nodes[word[0]] # We merge the current node with its only child if len(self.nodes ) == 1 and not self.is_leaf: a__ = list(self.nodes.values() )[0] a__ = merging_node.is_leaf self.prefix += merging_node.prefix a__ = merging_node.nodes # If there is more than 1 edge, we just mark it as non-leaf elif len(incoming_node.nodes ) > 1: a__ = False # If there is 1 edge, we merge it with its child else: a__ = list(incoming_node.nodes.values() )[0] a__ = merging_node.is_leaf incoming_node.prefix += merging_node.prefix a__ = merging_node.nodes return True def lowercase ( self: Tuple , __A: int = 0 ): '''simple docstring''' if self.prefix != "": print('''-''' * height , self.prefix , ''' (leaf)''' if self.is_leaf else '''''' ) for value in self.nodes.values(): value.print_tree(height + 1 ) def SCREAMING_SNAKE_CASE ( ): a__ = '''banana bananas bandana band apple all beast'''.split() a__ = RadixNode() root.insert_many(lowerCamelCase_) assert all(root.find(lowerCamelCase_) for word in words) assert not root.find('''bandanas''') assert not root.find('''apps''') root.delete('''all''') assert not root.find('''all''') root.delete('''banana''') assert not root.find('''banana''') assert root.find('''bananas''') return True def SCREAMING_SNAKE_CASE ( ): assert test_trie() def SCREAMING_SNAKE_CASE ( ): a__ = RadixNode() a__ = '''banana bananas bandanas bandana band apple all beast'''.split() root.insert_many(lowerCamelCase_) print('''Words:''' , lowerCamelCase_) print('''Tree:''') root.print_tree() if __name__ == "__main__": main()
200
"""simple docstring""" def SCREAMING_SNAKE_CASE ( lowerCamelCase_): assert column_title.isupper() a__ = 0 a__ = len(lowerCamelCase_) - 1 a__ = 0 while index >= 0: a__ = (ord(column_title[index]) - 64) * pow(26 , lowerCamelCase_) answer += value power += 1 index -= 1 return answer if __name__ == "__main__": from doctest import testmod testmod()
200
1
'''simple docstring''' def __lowercase (_SCREAMING_SNAKE_CASE :list[list[int | float]] ): SCREAMING_SNAKE_CASE : Any = len(_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE : Optional[Any] = len(matrix[0] ) SCREAMING_SNAKE_CASE : int = min(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) for row in range(_SCREAMING_SNAKE_CASE ): # Check if diagonal element is not zero if matrix[row][row] != 0: # Eliminate all the elements below the diagonal for col in range(row + 1 , _SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE : str = matrix[col][row] / matrix[row][row] for i in range(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): matrix[col][i] -= multiplier * matrix[row][i] else: # Find a non-zero diagonal element to swap rows SCREAMING_SNAKE_CASE : str = True for i in range(row + 1 , _SCREAMING_SNAKE_CASE ): if matrix[i][row] != 0: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Dict = matrix[i], matrix[row] SCREAMING_SNAKE_CASE : int = False break if reduce: rank -= 1 for i in range(_SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE : List[str] = matrix[i][rank] # Reduce the row pointer by one to stay on the same row row -= 1 return rank if __name__ == "__main__": import doctest doctest.testmod()
507
'''simple docstring''' def __lowercase (_SCREAMING_SNAKE_CASE :int , _SCREAMING_SNAKE_CASE :int ): return numa ^ numa < 0 if __name__ == "__main__": import doctest doctest.testmod()
507
1
"""simple docstring""" __lowercase = { "km/h": 1.0, "m/s": 3.6, "mph": 1.60_93_44, "knot": 1.8_52, } __lowercase = { "km/h": 1.0, "m/s": 0.2_77_77_77_78, "mph": 0.6_21_37_11_92, "knot": 0.5_39_95_68_03, } def lowerCAmelCase (__UpperCamelCase : float , __UpperCamelCase : str , __UpperCamelCase : str ): """simple docstring""" if unit_to not in speed_chart or unit_from not in speed_chart_inverse: __UpperCamelCase =( F"""Incorrect 'from_type' or 'to_type' value: {unit_from!r}, {unit_to!r}\n""" F"""Valid values are: {", ".join(__UpperCamelCase )}""" ) raise ValueError(__UpperCamelCase ) return round(speed * speed_chart[unit_from] * speed_chart_inverse[unit_to] , 3 ) if __name__ == "__main__": import doctest doctest.testmod()
296
"""simple docstring""" from pickle import UnpicklingError import jax import jax.numpy as jnp import numpy as np from flax.serialization import from_bytes from flax.traverse_util import flatten_dict from ..utils import logging __lowercase = logging.get_logger(__name__) def lowerCAmelCase (__UpperCamelCase : Optional[int] , __UpperCamelCase : Optional[Any] ): """simple docstring""" try: with open(__UpperCamelCase , '''rb''' ) as flax_state_f: __UpperCamelCase =from_bytes(__UpperCamelCase , flax_state_f.read() ) except UnpicklingError as e: try: with open(__UpperCamelCase ) as f: if f.read().startswith('''version''' ): raise OSError( '''You seem to have cloned a repository without having git-lfs installed. Please''' ''' install git-lfs and run `git lfs install` followed by `git lfs pull` in the''' ''' folder you cloned.''' ) else: raise ValueError from e except (UnicodeDecodeError, ValueError): raise EnvironmentError(F"""Unable to convert {model_file} to Flax deserializable object. """ ) return load_flax_weights_in_pytorch_model(__UpperCamelCase , __UpperCamelCase ) def lowerCAmelCase (__UpperCamelCase : Tuple , __UpperCamelCase : List[str] ): """simple docstring""" try: import torch # noqa: F401 except ImportError: logger.error( '''Loading Flax weights in PyTorch requires both PyTorch and Flax to be installed. Please see''' ''' https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation''' ''' instructions.''' ) raise # check if we have bf16 weights __UpperCamelCase =flatten_dict(jax.tree_util.tree_map(lambda __UpperCamelCase : x.dtype == jnp.bfloataa , __UpperCamelCase ) ).values() if any(__UpperCamelCase ): # convert all weights to fp32 if they are bf16 since torch.from_numpy can-not handle bf16 # and bf16 is not fully supported in PT yet. logger.warning( '''Found ``bfloat16`` weights in Flax model. Casting all ``bfloat16`` weights to ``float32`` ''' '''before loading those in PyTorch model.''' ) __UpperCamelCase =jax.tree_util.tree_map( lambda __UpperCamelCase : params.astype(np.floataa ) if params.dtype == jnp.bfloataa else params , __UpperCamelCase ) __UpperCamelCase ='''''' __UpperCamelCase =flatten_dict(__UpperCamelCase , sep='''.''' ) __UpperCamelCase =pt_model.state_dict() # keep track of unexpected & missing keys __UpperCamelCase =[] __UpperCamelCase =set(pt_model_dict.keys() ) for flax_key_tuple, flax_tensor in flax_state_dict.items(): __UpperCamelCase =flax_key_tuple.split('''.''' ) if flax_key_tuple_array[-1] == "kernel" and flax_tensor.ndim == 4: __UpperCamelCase =flax_key_tuple_array[:-1] + ['''weight'''] __UpperCamelCase =jnp.transpose(__UpperCamelCase , (3, 2, 0, 1) ) elif flax_key_tuple_array[-1] == "kernel": __UpperCamelCase =flax_key_tuple_array[:-1] + ['''weight'''] __UpperCamelCase =flax_tensor.T elif flax_key_tuple_array[-1] == "scale": __UpperCamelCase =flax_key_tuple_array[:-1] + ['''weight'''] if "time_embedding" not in flax_key_tuple_array: for i, flax_key_tuple_string in enumerate(__UpperCamelCase ): __UpperCamelCase =( flax_key_tuple_string.replace('''_0''' , '''.0''' ) .replace('''_1''' , '''.1''' ) .replace('''_2''' , '''.2''' ) .replace('''_3''' , '''.3''' ) .replace('''_4''' , '''.4''' ) .replace('''_5''' , '''.5''' ) .replace('''_6''' , '''.6''' ) .replace('''_7''' , '''.7''' ) .replace('''_8''' , '''.8''' ) .replace('''_9''' , '''.9''' ) ) __UpperCamelCase ='''.'''.join(__UpperCamelCase ) if flax_key in pt_model_dict: if flax_tensor.shape != pt_model_dict[flax_key].shape: raise ValueError( F"""Flax checkpoint seems to be incorrect. Weight {flax_key_tuple} was expected """ F"""to be of shape {pt_model_dict[flax_key].shape}, but is {flax_tensor.shape}.""" ) else: # add weight to pytorch dict __UpperCamelCase =np.asarray(__UpperCamelCase ) if not isinstance(__UpperCamelCase , np.ndarray ) else flax_tensor __UpperCamelCase =torch.from_numpy(__UpperCamelCase ) # remove from missing keys missing_keys.remove(__UpperCamelCase ) else: # weight is not expected by PyTorch model unexpected_keys.append(__UpperCamelCase ) pt_model.load_state_dict(__UpperCamelCase ) # re-transform missing_keys to list __UpperCamelCase =list(__UpperCamelCase ) if len(__UpperCamelCase ) > 0: logger.warning( '''Some weights of the Flax model were not used when initializing the PyTorch model''' F""" {pt_model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are initializing""" F""" {pt_model.__class__.__name__} from a Flax model trained on another task or with another architecture""" ''' (e.g. initializing a BertForSequenceClassification model from a FlaxBertForPreTraining model).\n- This''' F""" IS NOT expected if you are initializing {pt_model.__class__.__name__} from a Flax model that you expect""" ''' to be exactly identical (e.g. initializing a BertForSequenceClassification model from a''' ''' FlaxBertForSequenceClassification model).''' ) if len(__UpperCamelCase ) > 0: logger.warning( F"""Some weights of {pt_model.__class__.__name__} were not initialized from the Flax model and are newly""" F""" initialized: {missing_keys}\nYou should probably TRAIN this model on a down-stream task to be able to""" ''' use it for predictions and inference.''' ) return pt_model
296
1
from itertools import zip_longest import requests from bsa import BeautifulSoup from pandas import DataFrame def __UpperCamelCase (_SCREAMING_SNAKE_CASE = "laptop" ) -> DataFrame: lowercase__ = F"""https://www.amazon.in/laptop/s?k={product}""" lowercase__ = { 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36\n (KHTML, like Gecko)Chrome/44.0.2403.157 Safari/537.36', 'Accept-Language': 'en-US, en;q=0.5', } lowercase__ = BeautifulSoup(requests.get(_SCREAMING_SNAKE_CASE , headers=_SCREAMING_SNAKE_CASE ).text ) # Initialize a Pandas dataframe with the column titles lowercase__ = DataFrame( columns=[ 'Product Title', 'Product Link', 'Current Price of the product', 'Product Rating', 'MRP of the product', 'Discount', ] ) # Loop through each entry and store them in the dataframe for item, _ in zip_longest( soup.find_all( 'div' , attrs={'class': 's-result-item', 'data-component-type': 's-search-result'} , ) , soup.find_all('div' , attrs={'class': 'a-row a-size-base a-color-base'} ) , ): try: lowercase__ = item.ha.text lowercase__ = 'https://www.amazon.in/' + item.ha.a['href'] lowercase__ = item.find('span' , attrs={'class': 'a-offscreen'} ).text try: lowercase__ = item.find('span' , attrs={'class': 'a-icon-alt'} ).text except AttributeError: lowercase__ = 'Not available' try: lowercase__ = ( '₹' + item.find( 'span' , attrs={'class': 'a-price a-text-price'} ).text.split('₹' )[1] ) except AttributeError: lowercase__ = '' try: lowercase__ = float( ( ( float(product_mrp.strip('₹' ).replace(',' , '' ) ) - float(product_price.strip('₹' ).replace(',' , '' ) ) ) / float(product_mrp.strip('₹' ).replace(',' , '' ) ) ) * 100 ) except ValueError: lowercase__ = float('nan' ) except AttributeError: pass lowercase__ = [ product_title, product_link, product_price, product_rating, product_mrp, discount, ] lowercase__ = ' ' lowercase__ = ' ' data_frame.index += 1 return data_frame if __name__ == "__main__": lowercase_ = """headphones""" get_amazon_product_data(product).to_csv(f'''Amazon Product Data for {product}.csv''')
235
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import re from ..utils import cached_file # docstyle-ignore lowercase_ = """ Human: <<task>> Assistant: """ lowercase_ = """huggingface-tools/default-prompts""" lowercase_ = {"""chat""": """chat_prompt_template.txt""", """run""": """run_prompt_template.txt"""} def __UpperCamelCase (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE="run" ) -> Union[str, Any]: if prompt_or_repo_id is None: lowercase__ = DEFAULT_PROMPTS_REPO # prompt is considered a repo ID when it does not contain any kind of space if re.search('\\s' , _SCREAMING_SNAKE_CASE ) is not None: return prompt_or_repo_id lowercase__ = cached_file( _SCREAMING_SNAKE_CASE , PROMPT_FILES[mode] , repo_type='dataset' , user_agent={'agent': agent_name} ) with open(_SCREAMING_SNAKE_CASE , 'r' , encoding='utf-8' ) as f: return f.read()
235
1
import os import sys import unittest lowercase : Tuple = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, """utils""")) import get_test_info # noqa: E402 from get_test_info import ( # noqa: E402 get_model_to_test_mapping, get_model_to_tester_mapping, get_test_to_tester_mapping, ) lowercase : Union[str, Any] = os.path.join("""tests""", """models""", """bert""", """test_modeling_bert.py""") lowercase : Dict = os.path.join("""tests""", """models""", """blip""", """test_modeling_blip.py""") class a__ ( unittest.TestCase ): def lowerCAmelCase ( self : List[Any] ) -> str: """simple docstring""" lowerCamelCase_: Union[str, Any] = get_test_to_tester_mapping(UpperCamelCase_ ) lowerCamelCase_: Optional[int] = get_test_to_tester_mapping(UpperCamelCase_ ) lowerCamelCase_: str = {'BertModelTest': 'BertModelTester'} lowerCamelCase_: List[Any] = { 'BlipModelTest': 'BlipModelTester', 'BlipTextImageModelTest': 'BlipTextImageModelsModelTester', 'BlipTextModelTest': 'BlipTextModelTester', 'BlipTextRetrievalModelTest': 'BlipTextRetrievalModelTester', 'BlipVQAModelTest': 'BlipVQAModelTester', 'BlipVisionModelTest': 'BlipVisionModelTester', } self.assertEqual(get_test_info.to_json(UpperCamelCase_ ) , UpperCamelCase_ ) self.assertEqual(get_test_info.to_json(UpperCamelCase_ ) , UpperCamelCase_ ) def lowerCAmelCase ( self : Dict ) -> List[str]: """simple docstring""" lowerCamelCase_: Union[str, Any] = get_model_to_test_mapping(UpperCamelCase_ ) lowerCamelCase_: int = get_model_to_test_mapping(UpperCamelCase_ ) lowerCamelCase_: Union[str, Any] = { 'BertForMaskedLM': ['BertModelTest'], 'BertForMultipleChoice': ['BertModelTest'], 'BertForNextSentencePrediction': ['BertModelTest'], 'BertForPreTraining': ['BertModelTest'], 'BertForQuestionAnswering': ['BertModelTest'], 'BertForSequenceClassification': ['BertModelTest'], 'BertForTokenClassification': ['BertModelTest'], 'BertLMHeadModel': ['BertModelTest'], 'BertModel': ['BertModelTest'], } lowerCamelCase_: Optional[int] = { 'BlipForConditionalGeneration': ['BlipTextImageModelTest'], 'BlipForImageTextRetrieval': ['BlipTextRetrievalModelTest'], 'BlipForQuestionAnswering': ['BlipVQAModelTest'], 'BlipModel': ['BlipModelTest'], 'BlipTextModel': ['BlipTextModelTest'], 'BlipVisionModel': ['BlipVisionModelTest'], } self.assertEqual(get_test_info.to_json(UpperCamelCase_ ) , UpperCamelCase_ ) self.assertEqual(get_test_info.to_json(UpperCamelCase_ ) , UpperCamelCase_ ) def lowerCAmelCase ( self : int ) -> str: """simple docstring""" lowerCamelCase_: Dict = get_model_to_tester_mapping(UpperCamelCase_ ) lowerCamelCase_: Union[str, Any] = get_model_to_tester_mapping(UpperCamelCase_ ) lowerCamelCase_: int = { 'BertForMaskedLM': ['BertModelTester'], 'BertForMultipleChoice': ['BertModelTester'], 'BertForNextSentencePrediction': ['BertModelTester'], 'BertForPreTraining': ['BertModelTester'], 'BertForQuestionAnswering': ['BertModelTester'], 'BertForSequenceClassification': ['BertModelTester'], 'BertForTokenClassification': ['BertModelTester'], 'BertLMHeadModel': ['BertModelTester'], 'BertModel': ['BertModelTester'], } lowerCamelCase_: Union[str, Any] = { 'BlipForConditionalGeneration': ['BlipTextImageModelsModelTester'], 'BlipForImageTextRetrieval': ['BlipTextRetrievalModelTester'], 'BlipForQuestionAnswering': ['BlipVQAModelTester'], 'BlipModel': ['BlipModelTester'], 'BlipTextModel': ['BlipTextModelTester'], 'BlipVisionModel': ['BlipVisionModelTester'], } self.assertEqual(get_test_info.to_json(UpperCamelCase_ ) , UpperCamelCase_ ) self.assertEqual(get_test_info.to_json(UpperCamelCase_ ) , UpperCamelCase_ )
704
from __future__ import annotations lowercase : Optional[int] = [True] * 1_0_0_0_0_0_1 lowercase : Tuple = 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): lowercase : int = False i += 1 def UpperCAmelCase_ ( _UpperCAmelCase ): return seive[n] def UpperCAmelCase_ ( _UpperCAmelCase ): return any(digit in """02468""" for digit in str(_UpperCAmelCase ) ) def UpperCAmelCase_ ( _UpperCAmelCase = 1_0_0_0_0_0_0 ): lowerCamelCase_: Tuple = [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 ): lowerCamelCase_: List[str] = str(_UpperCAmelCase ) lowerCamelCase_: str = [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 UpperCAmelCase_ ( ): return len(find_circular_primes() ) if __name__ == "__main__": print(F"{len(find_circular_primes()) = }")
584
0
from functools import reduce UpperCAmelCase_ = ( """73167176531330624919225119674426574742355349194934""" """96983520312774506326239578318016984801869478851843""" """85861560789112949495459501737958331952853208805511""" """12540698747158523863050715693290963295227443043557""" """66896648950445244523161731856403098711121722383113""" """62229893423380308135336276614282806444486645238749""" """30358907296290491560440772390713810515859307960866""" """70172427121883998797908792274921901699720888093776""" """65727333001053367881220235421809751254540594752243""" """52584907711670556013604839586446706324415722155397""" """53697817977846174064955149290862569321978468622482""" """83972241375657056057490261407972968652414535100474""" """82166370484403199890008895243450658541227588666881""" """16427171479924442928230863465674813919123162824586""" """17866458359124566529476545682848912883142607690042""" """24219022671055626321111109370544217506941658960408""" """07198403850962455444362981230987879927244284909188""" """84580156166097919133875499200524063689912560717606""" """05886116467109405077541002256983155200055935729725""" """71636269561882670428252483600823257530420752963450""" ) def SCREAMING_SNAKE_CASE_ ( _snake_case :str = N ) -> int: return max( # mypy cannot properly interpret reduce int(reduce(lambda _snake_case , _snake_case : str(int(_snake_case ) * int(_snake_case ) ) , n[i : i + 13] ) ) for i in range(len(_snake_case ) - 12 ) ) if __name__ == "__main__": print(f'{solution() = }')
2
'''simple docstring''' import json from typing import TYPE_CHECKING, 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_blenderbot import BlenderbotTokenizer if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation UpperCAmelCase_ : List[Any] = logging.get_logger(__name__) UpperCAmelCase_ : Union[str, Any] = { 'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_config_file': 'tokenizer_config.json', } UpperCAmelCase_ : List[Any] = { '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' }, } UpperCAmelCase_ : str = {'facebook/blenderbot-3B': 1_2_8} class _lowerCamelCase ( snake_case_ ): '''simple docstring''' __lowercase : Optional[int] = VOCAB_FILES_NAMES __lowercase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __lowercase : Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowercase : Tuple = ['''input_ids''', '''attention_mask'''] __lowercase : str = BlenderbotTokenizer def __init__( self , __lowercase=None , __lowercase=None , __lowercase=None , __lowercase="replace" , __lowercase="<s>" , __lowercase="</s>" , __lowercase="</s>" , __lowercase="<s>" , __lowercase="<unk>" , __lowercase="<pad>" , __lowercase="<mask>" , __lowercase=False , __lowercase=True , **__lowercase , ): """simple docstring""" super().__init__( __lowercase , __lowercase , tokenizer_file=__lowercase , errors=__lowercase , bos_token=__lowercase , eos_token=__lowercase , sep_token=__lowercase , cls_token=__lowercase , unk_token=__lowercase , pad_token=__lowercase , mask_token=__lowercase , add_prefix_space=__lowercase , trim_offsets=__lowercase , **__lowercase , ) __A : str = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('add_prefix_space' , __lowercase ) != add_prefix_space: __A : List[Any] = getattr(__lowercase , pre_tok_state.pop('type' ) ) __A : Any = add_prefix_space __A : Any = pre_tok_class(**__lowercase ) __A : Union[str, Any] = add_prefix_space __A : List[Any] = 'post_processor' __A : Optional[Any] = getattr(self.backend_tokenizer , __lowercase , __lowercase ) if tokenizer_component_instance: __A : Optional[int] = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: __A : Union[str, Any] = tuple(state['sep'] ) if "cls" in state: __A : int = tuple(state['cls'] ) __A : Any = False if state.get('add_prefix_space' , __lowercase ) != add_prefix_space: __A : Any = add_prefix_space __A : int = True if state.get('trim_offsets' , __lowercase ) != trim_offsets: __A : List[Any] = trim_offsets __A : List[str] = True if changes_to_apply: __A : Optional[int] = getattr(__lowercase , state.pop('type' ) ) __A : Optional[int] = component_class(**__lowercase ) setattr(self.backend_tokenizer , __lowercase , __lowercase ) @property # Copied from transformers.models.roberta.tokenization_roberta_fast.RobertaTokenizerFast.mask_token with Roberta->Blenderbot, RoBERTa->Blenderbot def snake_case__ ( self ): """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 snake_case__ ( self , __lowercase ): """simple docstring""" __A : Union[str, Any] = AddedToken(__lowercase , lstrip=__lowercase , rstrip=__lowercase ) if isinstance(__lowercase , __lowercase ) else value __A : List[str] = value def snake_case__ ( self , *__lowercase , **__lowercase ): """simple docstring""" __A : Tuple = kwargs.get('is_split_into_words' , __lowercase ) assert self.add_prefix_space or not is_split_into_words, ( F"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """ "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*__lowercase , **__lowercase ) def snake_case__ ( self , *__lowercase , **__lowercase ): """simple docstring""" __A : Any = kwargs.get('is_split_into_words' , __lowercase ) assert self.add_prefix_space or not is_split_into_words, ( F"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """ "to use it with pretokenized inputs." ) return super()._encode_plus(*__lowercase , **__lowercase ) def snake_case__ ( self , __lowercase , __lowercase = None ): """simple docstring""" __A : str = self._tokenizer.model.save(__lowercase , name=__lowercase ) return tuple(__lowercase ) def snake_case__ ( self , __lowercase , __lowercase = None ): """simple docstring""" __A : str = [self.sep_token_id] __A : Any = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def snake_case__ ( self , __lowercase , __lowercase = None ): """simple docstring""" return token_ids_a + [self.eos_token_id] def snake_case__ ( self , __lowercase ): """simple docstring""" __A : List[str] = [] 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(__lowercase ) __A : Tuple = ' '.join(__lowercase ) __A : List[Any] = self.encode(__lowercase ) if len(__lowercase ) > self.model_max_length: __A : Any = 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
365
0
"""simple docstring""" import coval # From: git+https://github.com/ns-moosavi/coval.git # noqa: F401 from coval.conll import reader, util from coval.eval import evaluator import datasets __lowerCAmelCase : Tuple = datasets.logging.get_logger(__name__) __lowerCAmelCase : List[str] = '''\ @InProceedings{moosavi2019minimum, author = { Nafise Sadat Moosavi, Leo Born, Massimo Poesio and Michael Strube}, title = {Using Automatically Extracted Minimum Spans to Disentangle Coreference Evaluation from Boundary Detection}, year = {2019}, booktitle = {Proceedings of the 57th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)}, publisher = {Association for Computational Linguistics}, address = {Florence, Italy}, } @inproceedings{10.3115/1072399.1072405, author = {Vilain, Marc and Burger, John and Aberdeen, John and Connolly, Dennis and Hirschman, Lynette}, title = {A Model-Theoretic Coreference Scoring Scheme}, year = {1995}, isbn = {1558604022}, publisher = {Association for Computational Linguistics}, address = {USA}, url = {https://doi.org/10.3115/1072399.1072405}, doi = {10.3115/1072399.1072405}, booktitle = {Proceedings of the 6th Conference on Message Understanding}, pages = {45–52}, numpages = {8}, location = {Columbia, Maryland}, series = {MUC6 ’95} } @INPROCEEDINGS{Bagga98algorithmsfor, author = {Amit Bagga and Breck Baldwin}, title = {Algorithms for Scoring Coreference Chains}, booktitle = {In The First International Conference on Language Resources and Evaluation Workshop on Linguistics Coreference}, year = {1998}, pages = {563--566} } @INPROCEEDINGS{Luo05oncoreference, author = {Xiaoqiang Luo}, title = {On coreference resolution performance metrics}, booktitle = {In Proc. of HLT/EMNLP}, year = {2005}, pages = {25--32}, publisher = {URL} } @inproceedings{moosavi-strube-2016-coreference, title = "Which Coreference Evaluation Metric Do You Trust? A Proposal for a Link-based Entity Aware Metric", author = "Moosavi, Nafise Sadat and Strube, Michael", booktitle = "Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)", month = aug, year = "2016", address = "Berlin, Germany", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/P16-1060", doi = "10.18653/v1/P16-1060", pages = "632--642", } ''' __lowerCAmelCase : int = '''\ CoVal is a coreference evaluation tool for the CoNLL and ARRAU datasets which implements of the common evaluation metrics including MUC [Vilain et al, 1995], B-cubed [Bagga and Baldwin, 1998], CEAFe [Luo et al., 2005], LEA [Moosavi and Strube, 2016] and the averaged CoNLL score (the average of the F1 values of MUC, B-cubed and CEAFe) [Denis and Baldridge, 2009a; Pradhan et al., 2011]. This wrapper of CoVal currently only work with CoNLL line format: The CoNLL format has one word per line with all the annotation for this word in column separated by spaces: Column Type Description 1 Document ID This is a variation on the document filename 2 Part number Some files are divided into multiple parts numbered as 000, 001, 002, ... etc. 3 Word number 4 Word itself This is the token as segmented/tokenized in the Treebank. Initially the *_skel file contain the placeholder [WORD] which gets replaced by the actual token from the Treebank which is part of the OntoNotes release. 5 Part-of-Speech 6 Parse bit This is the bracketed structure broken before the first open parenthesis in the parse, and the word/part-of-speech leaf replaced with a *. The full parse can be created by substituting the asterix with the "([pos] [word])" string (or leaf) and concatenating the items in the rows of that column. 7 Predicate lemma The predicate lemma is mentioned for the rows for which we have semantic role information. All other rows are marked with a "-" 8 Predicate Frameset ID This is the PropBank frameset ID of the predicate in Column 7. 9 Word sense This is the word sense of the word in Column 3. 10 Speaker/Author This is the speaker or author name where available. Mostly in Broadcast Conversation and Web Log data. 11 Named Entities These columns identifies the spans representing various named entities. 12:N Predicate Arguments There is one column each of predicate argument structure information for the predicate mentioned in Column 7. N Coreference Coreference chain information encoded in a parenthesis structure. More informations on the format can be found here (section "*_conll File Format"): http://www.conll.cemantix.org/2012/data.html Details on the evaluation on CoNLL can be found here: https://github.com/ns-moosavi/coval/blob/master/conll/README.md CoVal code was written by @ns-moosavi. Some parts are borrowed from https://github.com/clarkkev/deep-coref/blob/master/evaluation.py The test suite is taken from https://github.com/conll/reference-coreference-scorers/ Mention evaluation and the test suite are added by @andreasvc. Parsing CoNLL files is developed by Leo Born. ''' __lowerCAmelCase : Optional[int] = ''' Calculates coreference evaluation metrics. Args: predictions: list of sentences. Each sentence is a list of word predictions to score in the CoNLL format. Each prediction is a word with its annotations as a string made of columns joined with spaces. Only columns 4, 5, 6 and the last column are used (word, POS, Pars and coreference annotation) See the details on the format in the description of the metric. references: list of sentences. Each sentence is a list of word reference to score in the CoNLL format. Each reference is a word with its annotations as a string made of columns joined with spaces. Only columns 4, 5, 6 and the last column are used (word, POS, Pars and coreference annotation) See the details on the format in the description of the metric. keep_singletons: After extracting all mentions of key or system files, mentions whose corresponding coreference chain is of size one, are considered as singletons. The default evaluation mode will include singletons in evaluations if they are included in the key or the system files. By setting \'keep_singletons=False\', all singletons in the key and system files will be excluded from the evaluation. NP_only: Most of the recent coreference resolvers only resolve NP mentions and leave out the resolution of VPs. By setting the \'NP_only\' option, the scorer will only evaluate the resolution of NPs. min_span: By setting \'min_span\', the scorer reports the results based on automatically detected minimum spans. Minimum spans are determined using the MINA algorithm. Returns: \'mentions\': mentions \'muc\': MUC metric [Vilain et al, 1995] \'bcub\': B-cubed [Bagga and Baldwin, 1998] \'ceafe\': CEAFe [Luo et al., 2005] \'lea\': LEA [Moosavi and Strube, 2016] \'conll_score\': averaged CoNLL score (the average of the F1 values of MUC, B-cubed and CEAFe) Examples: >>> coval = datasets.load_metric(\'coval\') >>> words = [\'bc/cctv/00/cctv_0005 0 0 Thank VBP (TOP(S(VP* thank 01 1 Xu_li * (V*) * -\', ... \'bc/cctv/00/cctv_0005 0 1 you PRP (NP*) - - - Xu_li * (ARG1*) (ARG0*) (116)\', ... \'bc/cctv/00/cctv_0005 0 2 everyone NN (NP*) - - - Xu_li * (ARGM-DIS*) * (116)\', ... \'bc/cctv/00/cctv_0005 0 3 for IN (PP* - - - Xu_li * (ARG2* * -\', ... \'bc/cctv/00/cctv_0005 0 4 watching VBG (S(VP*)))) watch 01 1 Xu_li * *) (V*) -\', ... \'bc/cctv/00/cctv_0005 0 5 . . *)) - - - Xu_li * * * -\'] >>> references = [words] >>> predictions = [words] >>> results = coval.compute(predictions=predictions, references=references) >>> print(results) # doctest:+ELLIPSIS {\'mentions/recall\': 1.0,[...] \'conll_score\': 100.0} ''' def __lowerCAmelCase ( __UpperCamelCase : List[str] , __UpperCamelCase : List[Any] , __UpperCamelCase : Optional[int]=False , __UpperCamelCase : Tuple=False , __UpperCamelCase : Any=True , __UpperCamelCase : Optional[int]=False , __UpperCamelCase : Any="dummy_doc" ): '''simple docstring''' snake_case_ : Tuple = {doc: key_lines} snake_case_ : Dict = {doc: sys_lines} snake_case_ : Tuple = {} snake_case_ : Dict = 0 snake_case_ : List[Any] = 0 snake_case_ : Tuple = 0 snake_case_ : List[str] = 0 snake_case_ : Any = 0 snake_case_ : Optional[int] = 0 snake_case_ , snake_case_ : Optional[int] = reader.get_doc_mentions(__UpperCamelCase , key_doc_lines[doc] , __UpperCamelCase ) key_singletons_num += singletons_num if NP_only or min_span: snake_case_ : Optional[Any] = reader.set_annotated_parse_trees(__UpperCamelCase , key_doc_lines[doc] , __UpperCamelCase , __UpperCamelCase ) snake_case_ , snake_case_ : Optional[Any] = reader.get_doc_mentions(__UpperCamelCase , sys_doc_lines[doc] , __UpperCamelCase ) sys_singletons_num += singletons_num if NP_only or min_span: snake_case_ : List[str] = reader.set_annotated_parse_trees(__UpperCamelCase , key_doc_lines[doc] , __UpperCamelCase , __UpperCamelCase ) if remove_nested: snake_case_ , snake_case_ : Tuple = reader.remove_nested_coref_mentions(__UpperCamelCase , __UpperCamelCase ) key_nested_coref_num += nested_mentions key_removed_nested_clusters += removed_clusters snake_case_ , snake_case_ : Dict = reader.remove_nested_coref_mentions(__UpperCamelCase , __UpperCamelCase ) sys_nested_coref_num += nested_mentions sys_removed_nested_clusters += removed_clusters snake_case_ : List[str] = reader.get_mention_assignments(__UpperCamelCase , __UpperCamelCase ) snake_case_ : Dict = reader.get_mention_assignments(__UpperCamelCase , __UpperCamelCase ) snake_case_ : List[str] = (key_clusters, sys_clusters, key_mention_sys_cluster, sys_mention_key_cluster) if remove_nested: logger.info( """Number of removed nested coreferring mentions in the key """ F'annotation: {key_nested_coref_num}; and system annotation: {sys_nested_coref_num}' ) logger.info( """Number of resulting singleton clusters in the key """ F'annotation: {key_removed_nested_clusters}; and system annotation: {sys_removed_nested_clusters}' ) if not keep_singletons: logger.info( F'{key_singletons_num:d} and {sys_singletons_num:d} singletons are removed from the key and system ' """files, respectively""" ) return doc_coref_infos def __lowerCAmelCase ( __UpperCamelCase : Union[str, Any] , __UpperCamelCase : int , __UpperCamelCase : str , __UpperCamelCase : Optional[Any] , __UpperCamelCase : int , __UpperCamelCase : List[Any] , __UpperCamelCase : Optional[int] ): '''simple docstring''' snake_case_ : Tuple = get_coref_infos(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) snake_case_ : Union[str, Any] = {} snake_case_ : Dict = 0 snake_case_ : List[str] = 0 for name, metric in metrics: snake_case_ , snake_case_ , snake_case_ : Union[str, Any] = evaluator.evaluate_documents(__UpperCamelCase , __UpperCamelCase , beta=1 ) if name in ["muc", "bcub", "ceafe"]: conll += fa conll_subparts_num += 1 output_scores.update({F'{name}/recall': recall, F'{name}/precision': precision, F'{name}/f1': fa} ) logger.info( name.ljust(1_0 ) , F'Recall: {recall * 1_0_0:.2f}' , F' Precision: {precision * 1_0_0:.2f}' , F' F1: {fa * 1_0_0:.2f}' , ) if conll_subparts_num == 3: snake_case_ : str = (conll / 3) * 1_0_0 logger.info(F'CoNLL score: {conll:.2f}' ) output_scores.update({"""conll_score""": conll} ) return output_scores def __lowerCAmelCase ( __UpperCamelCase : Dict ): '''simple docstring''' snake_case_ : Dict = False for line in key_lines: if not line.startswith("""#""" ): if len(line.split() ) > 6: snake_case_ : List[Any] = line.split()[5] if not parse_col == "-": snake_case_ : Tuple = True break else: break return has_gold_parse @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _lowerCAmelCase ( datasets.Metric ): """simple docstring""" def UpperCAmelCase__ ( self ) -> List[str]: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""string""" ) ), """references""": datasets.Sequence(datasets.Value("""string""" ) ), } ) , codebase_urls=["""https://github.com/ns-moosavi/coval"""] , reference_urls=[ """https://github.com/ns-moosavi/coval""", """https://www.aclweb.org/anthology/P16-1060""", """http://www.conll.cemantix.org/2012/data.html""", ] , ) def UpperCAmelCase__ ( self , _lowercase , _lowercase , _lowercase=True , _lowercase=False , _lowercase=False , _lowercase=False ) -> List[str]: '''simple docstring''' snake_case_ : int = [ ("""mentions""", evaluator.mentions), ("""muc""", evaluator.muc), ("""bcub""", evaluator.b_cubed), ("""ceafe""", evaluator.ceafe), ("""lea""", evaluator.lea), ] if min_span: snake_case_ : Any = util.check_gold_parse_annotation(_lowercase ) if not has_gold_parse: raise NotImplementedError("""References should have gold parse annotation to use 'min_span'.""" ) # util.parse_key_file(key_file) # key_file = key_file + ".parsed" snake_case_ : Optional[int] = evaluate( key_lines=_lowercase , sys_lines=_lowercase , metrics=_lowercase , NP_only=_lowercase , remove_nested=_lowercase , keep_singletons=_lowercase , min_span=_lowercase , ) return score
21
"""simple docstring""" from sklearn.metrics import fa_score, matthews_corrcoef import datasets from .record_evaluation import evaluate as evaluate_record __lowerCAmelCase : List[str] = '''\ @article{wang2019superglue, title={SuperGLUE: A Stickier Benchmark for General-Purpose Language Understanding Systems}, author={Wang, Alex and Pruksachatkun, Yada and Nangia, Nikita and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R}, journal={arXiv preprint arXiv:1905.00537}, year={2019} } ''' __lowerCAmelCase : Optional[Any] = '''\ SuperGLUE (https://super.gluebenchmark.com/) is a new benchmark styled after GLUE with a new set of more difficult language understanding tasks, improved resources, and a new public leaderboard. ''' __lowerCAmelCase : str = ''' Compute SuperGLUE evaluation metric associated to each SuperGLUE dataset. Args: predictions: list of predictions to score. Depending on the SuperGlUE subset: - for \'record\': list of question-answer dictionaries with the following keys: - \'idx\': index of the question as specified by the dataset - \'prediction_text\': the predicted answer text - for \'multirc\': list of question-answer dictionaries with the following keys: - \'idx\': index of the question-answer pair as specified by the dataset - \'prediction\': the predicted answer label - otherwise: list of predicted labels references: list of reference labels. Depending on the SuperGLUE subset: - for \'record\': list of question-answers dictionaries with the following keys: - \'idx\': index of the question as specified by the dataset - \'answers\': list of possible answers - otherwise: list of reference labels Returns: depending on the SuperGLUE subset: - for \'record\': - \'exact_match\': Exact match between answer and gold answer - \'f1\': F1 score - for \'multirc\': - \'exact_match\': Exact match between answer and gold answer - \'f1_m\': Per-question macro-F1 score - \'f1_a\': Average F1 score over all answers - for \'axb\': \'matthews_correlation\': Matthew Correlation - for \'cb\': - \'accuracy\': Accuracy - \'f1\': F1 score - for all others: - \'accuracy\': Accuracy Examples: >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'copa\') # any of ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"] >>> predictions = [0, 1] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'accuracy\': 1.0} >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'cb\') >>> predictions = [0, 1] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'accuracy\': 1.0, \'f1\': 1.0} >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'record\') >>> predictions = [{\'idx\': {\'passage\': 0, \'query\': 0}, \'prediction_text\': \'answer\'}] >>> references = [{\'idx\': {\'passage\': 0, \'query\': 0}, \'answers\': [\'answer\', \'another_answer\']}] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'exact_match\': 1.0, \'f1\': 1.0} >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'multirc\') >>> predictions = [{\'idx\': {\'answer\': 0, \'paragraph\': 0, \'question\': 0}, \'prediction\': 0}, {\'idx\': {\'answer\': 1, \'paragraph\': 2, \'question\': 3}, \'prediction\': 1}] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'exact_match\': 1.0, \'f1_m\': 1.0, \'f1_a\': 1.0} >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'axb\') >>> references = [0, 1] >>> predictions = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'matthews_correlation\': 1.0} ''' def __lowerCAmelCase ( __UpperCamelCase : List[str] , __UpperCamelCase : Any ): '''simple docstring''' return float((preds == labels).mean() ) def __lowerCAmelCase ( __UpperCamelCase : Any , __UpperCamelCase : Optional[int] , __UpperCamelCase : str="binary" ): '''simple docstring''' snake_case_ : Optional[Any] = simple_accuracy(__UpperCamelCase , __UpperCamelCase ) snake_case_ : Dict = float(fa_score(y_true=__UpperCamelCase , y_pred=__UpperCamelCase , average=__UpperCamelCase ) ) return { "accuracy": acc, "f1": fa, } def __lowerCAmelCase ( __UpperCamelCase : str , __UpperCamelCase : int ): '''simple docstring''' snake_case_ : List[Any] = {} for id_pred, label in zip(__UpperCamelCase , __UpperCamelCase ): snake_case_ : Optional[int] = F'{id_pred["idx"]["paragraph"]}-{id_pred["idx"]["question"]}' snake_case_ : Union[str, Any] = id_pred["""prediction"""] if question_id in question_map: question_map[question_id].append((pred, label) ) else: snake_case_ : str = [(pred, label)] snake_case_ , snake_case_ : List[str] = [], [] for question, preds_labels in question_map.items(): snake_case_ , snake_case_ : Optional[Any] = zip(*__UpperCamelCase ) snake_case_ : int = fa_score(y_true=__UpperCamelCase , y_pred=__UpperCamelCase , average="""macro""" ) fas.append(__UpperCamelCase ) snake_case_ : Dict = int(sum(pred == label for pred, label in preds_labels ) == len(__UpperCamelCase ) ) ems.append(__UpperCamelCase ) snake_case_ : Optional[int] = float(sum(__UpperCamelCase ) / len(__UpperCamelCase ) ) snake_case_ : Any = sum(__UpperCamelCase ) / len(__UpperCamelCase ) snake_case_ : int = float(fa_score(y_true=__UpperCamelCase , y_pred=[id_pred["""prediction"""] for id_pred in ids_preds] ) ) return {"exact_match": em, "f1_m": fa_m, "f1_a": fa_a} @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _lowerCAmelCase ( datasets.Metric ): """simple docstring""" def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' if self.config_name not in [ "boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg", ]: raise KeyError( """You should supply a configuration name selected in """ """[\"boolq\", \"cb\", \"copa\", \"multirc\", \"record\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"axb\", \"axg\",]""" ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features(self._get_feature_types() ) , codebase_urls=[] , reference_urls=[] , format="""numpy""" if not self.config_name == """record""" and not self.config_name == """multirc""" else None , ) def UpperCAmelCase__ ( self ) -> Optional[int]: '''simple docstring''' if self.config_name == "record": return { "predictions": { "idx": { "passage": datasets.Value("""int64""" ), "query": datasets.Value("""int64""" ), }, "prediction_text": datasets.Value("""string""" ), }, "references": { "idx": { "passage": datasets.Value("""int64""" ), "query": datasets.Value("""int64""" ), }, "answers": datasets.Sequence(datasets.Value("""string""" ) ), }, } elif self.config_name == "multirc": return { "predictions": { "idx": { "answer": datasets.Value("""int64""" ), "paragraph": datasets.Value("""int64""" ), "question": datasets.Value("""int64""" ), }, "prediction": datasets.Value("""int64""" ), }, "references": datasets.Value("""int64""" ), } else: return { "predictions": datasets.Value("""int64""" ), "references": datasets.Value("""int64""" ), } def UpperCAmelCase__ ( self , _lowercase , _lowercase ) -> List[str]: '''simple docstring''' if self.config_name == "axb": return {"matthews_correlation": matthews_corrcoef(_lowercase , _lowercase )} elif self.config_name == "cb": return acc_and_fa(_lowercase , _lowercase , fa_avg="""macro""" ) elif self.config_name == "record": snake_case_ : Optional[Any] = [ { """qas""": [ {"""id""": ref["""idx"""]["""query"""], """answers""": [{"""text""": ans} for ans in ref["""answers"""]]} for ref in references ] } ] snake_case_ : Dict = {pred["""idx"""]["""query"""]: pred["""prediction_text"""] for pred in predictions} return evaluate_record(_lowercase , _lowercase )[0] elif self.config_name == "multirc": return evaluate_multirc(_lowercase , _lowercase ) elif self.config_name in ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"]: return {"accuracy": simple_accuracy(_lowercase , _lowercase )} else: raise KeyError( """You should supply a configuration name selected in """ """[\"boolq\", \"cb\", \"copa\", \"multirc\", \"record\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"axb\", \"axg\",]""" )
21
1
'''simple docstring''' import re from filelock import FileLock try: import nltk UpperCAmelCase__ : Union[str, Any] = True except (ImportError, ModuleNotFoundError): UpperCAmelCase__ : Tuple = False if NLTK_AVAILABLE: with FileLock(".lock") as lock: nltk.download("punkt", quiet=True) def A ( UpperCamelCase_ : str ) -> str: '''simple docstring''' re.sub("<n>" , "" , UpperCamelCase_ ) # remove pegasus newline char assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)" return "\n".join(nltk.sent_tokenize(UpperCamelCase_ ) )
48
import numpy as np from numpy import ndarray from scipy.optimize import Bounds, LinearConstraint, minimize def snake_case_ ( lowerCAmelCase_ : ndarray ): return np.dot(lowerCAmelCase_ , lowerCAmelCase_ ) class lowerCAmelCase : '''simple docstring''' def __init__( self : Dict , *, __a : float = np.inf , __a : str = "linear" , __a : float = 0.0 , ) -> None: """simple docstring""" __lowercase : Any = regularization __lowercase : List[str] = gamma if kernel == "linear": __lowercase : Dict = self.__linear elif kernel == "rbf": if self.gamma == 0: raise ValueError("""rbf kernel requires gamma""" ) if not isinstance(self.gamma , (float, int) ): raise ValueError("""gamma must be float or int""" ) if not self.gamma > 0: raise ValueError("""gamma must be > 0""" ) __lowercase : Optional[Any] = self.__rbf # in the future, there could be a default value like in sklearn # sklear: def_gamma = 1/(n_features * X.var()) (wiki) # previously it was 1/(n_features) else: __lowercase : List[Any] = F"Unknown kernel: {kernel}" raise ValueError(__a ) def lowerCAmelCase ( self : int , __a : ndarray , __a : ndarray ) -> float: """simple docstring""" return np.dot(__a , __a ) def lowerCAmelCase ( self : str , __a : ndarray , __a : ndarray ) -> float: """simple docstring""" return np.exp(-(self.gamma * norm_squared(vectora - vectora )) ) def lowerCAmelCase ( self : Optional[int] , __a : list[ndarray] , __a : ndarray ) -> None: """simple docstring""" __lowercase : List[Any] = observations __lowercase : Union[str, Any] = classes # using Wolfe's Dual to calculate w. # Primal problem: minimize 1/2*norm_squared(w) # constraint: yn(w . xn + b) >= 1 # # With l a vector # Dual problem: maximize sum_n(ln) - # 1/2 * sum_n(sum_m(ln*lm*yn*ym*xn . xm)) # constraint: self.C >= ln >= 0 # and sum_n(ln*yn) = 0 # Then we get w using w = sum_n(ln*yn*xn) # At the end we can get b ~= mean(yn - w . xn) # # Since we use kernels, we only need l_star to calculate b # and to classify observations ((__lowercase) , ) : Union[str, Any] = np.shape(__a ) def to_minimize(__a : ndarray ) -> float: __lowercase : str = 0 ((__lowercase) , ) : Tuple = np.shape(__a ) for i in range(__a ): for j in range(__a ): s += ( candidate[i] * candidate[j] * classes[i] * classes[j] * self.kernel(observations[i] , observations[j] ) ) return 1 / 2 * s - sum(__a ) __lowercase : Tuple = LinearConstraint(__a , 0 , 0 ) __lowercase : List[Any] = Bounds(0 , self.regularization ) __lowercase : Dict = minimize( __a , np.ones(__a ) , bounds=__a , constraints=[ly_contraint] ).x __lowercase : str = l_star # calculating mean offset of separation plane to points __lowercase : Optional[Any] = 0 for i in range(__a ): for j in range(__a ): s += classes[i] - classes[i] * self.optimum[i] * self.kernel( observations[i] , observations[j] ) __lowercase : Any = s / n def lowerCAmelCase ( self : Any , __a : ndarray ) -> int: """simple docstring""" __lowercase : Optional[Any] = sum( self.optimum[n] * self.classes[n] * self.kernel(self.observations[n] , __a ) for n in range(len(self.classes ) ) ) return 1 if s + self.offset >= 0 else -1 if __name__ == "__main__": import doctest doctest.testmod()
149
0
import sys import webbrowser import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": print('''Googling.....''') lowerCamelCase__ = '''https://www.google.com/search?q=''' + ''' '''.join(sys.argv[1:]) lowerCamelCase__ = requests.get(url, headers={'''UserAgent''': UserAgent().random}) # res.raise_for_status() with open('''project1a.html''', '''wb''') as out_file: # only for knowing the class for data in res.iter_content(1_00_00): out_file.write(data) lowerCamelCase__ = BeautifulSoup(res.text, '''html.parser''') lowerCamelCase__ = list(soup.select('''.eZt8xd'''))[:5] print(len(links)) for link in links: if link.text == "Maps": webbrowser.open(link.get('''href''')) else: webbrowser.open(F'''https://google.com{link.get('href')}''')
226
import os from typing import Optional import fsspec from fsspec.archive import AbstractArchiveFileSystem from fsspec.utils import DEFAULT_BLOCK_SIZE class __magic_name__ (__lowercase ): lowerCamelCase__ = '''''' lowerCamelCase__ = ( None # protocol passed in prefix to the url. ex: "gzip", for gzip://file.txt::http://foo.bar/file.txt.gz ) lowerCamelCase__ = None # compression type in fsspec. ex: "gzip" lowerCamelCase__ = None # extension of the filename to strip. ex: "".gz" to get file.txt from file.txt.gz def __init__( self , _a = "" , _a = None , _a = None , **_a ) -> Any: super().__init__(self , **_a ) # always open as "rb" since fsspec can then use the TextIOWrapper to make it work for "r" mode lowerCAmelCase_ = fsspec.open( _a , mode="rb" , protocol=_a , compression=self.compression , client_kwargs={ "requote_redirect_url": False, # see https://github.com/huggingface/datasets/pull/5459 "trust_env": True, # Enable reading proxy env variables. **(target_options or {}).pop("client_kwargs" , {} ), # To avoid issues if it was already passed. } , **(target_options or {}) , ) lowerCAmelCase_ = os.path.basename(self.file.path.split("::" )[0] ) lowerCAmelCase_ = ( self.compressed_name[: self.compressed_name.rindex("." )] if "." in self.compressed_name else self.compressed_name ) lowerCAmelCase_ = None @classmethod def __a ( cls , _a ) -> List[Any]: # compressed file paths are always relative to the archive root return super()._strip_protocol(_a ).lstrip("/" ) def __a ( self ) -> Union[str, Any]: if self.dir_cache is None: lowerCAmelCase_ = {**self.file.fs.info(self.file.path ), "name": self.uncompressed_name} lowerCAmelCase_ = {f["name"]: f} def __a ( self , _a ) -> Dict: return self.file.open().read() def __a ( self , _a , _a = "rb" , _a=None , _a=True , _a=None , **_a , ) -> Optional[Any]: lowerCAmelCase_ = self._strip_protocol(_a ) if mode != "rb": raise ValueError(f"Tried to read with mode {mode} on file {self.file.path} opened with mode 'rb'" ) return self.file.open() class __magic_name__ (__lowercase ): lowerCamelCase__ = '''bz2''' lowerCamelCase__ = '''bz2''' lowerCamelCase__ = '''.bz2''' class __magic_name__ (__lowercase ): lowerCamelCase__ = '''gzip''' lowerCamelCase__ = '''gzip''' lowerCamelCase__ = '''.gz''' class __magic_name__ (__lowercase ): lowerCamelCase__ = '''lz4''' lowerCamelCase__ = '''lz4''' lowerCamelCase__ = '''.lz4''' class __magic_name__ (__lowercase ): lowerCamelCase__ = '''xz''' lowerCamelCase__ = '''xz''' lowerCamelCase__ = '''.xz''' class __magic_name__ (__lowercase ): lowerCamelCase__ = '''zstd''' lowerCamelCase__ = '''zstd''' lowerCamelCase__ = '''.zst''' def __init__( self , _a , _a = "rb" , _a = None , _a = None , _a = DEFAULT_BLOCK_SIZE , **_a , ) -> Tuple: super().__init__( fo=_a , mode=_a , target_protocol=_a , target_options=_a , block_size=_a , **_a , ) # We need to wrap the zstd decompressor to avoid this error in fsspec==2021.7.0 and zstandard==0.15.2: # # File "/Users/user/.virtualenvs/hf-datasets/lib/python3.7/site-packages/fsspec/core.py", line 145, in open # out.close = close # AttributeError: 'zstd.ZstdDecompressionReader' object attribute 'close' is read-only # # see https://github.com/intake/filesystem_spec/issues/725 lowerCAmelCase_ = self.file.__enter__ class __magic_name__ : def __init__( self , _a ) -> List[str]: lowerCAmelCase_ = file_ def __enter__( self ) -> int: self._file.__enter__() return self def __exit__( self , *_a , **_a ) -> Dict: self._file.__exit__(*_a , **_a ) def __iter__( self ) -> List[Any]: return iter(self._file ) def __a ( self ) -> List[Any]: return next(self._file ) def __getattr__( self , _a ) -> Tuple: return getattr(self._file , _a ) def fixed_enter(*_a , **_a ): return WrappedFile(_enter(*_a , **_a ) ) lowerCAmelCase_ = fixed_enter
226
1
"""simple docstring""" import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, BatchEncoding, PreTrainedTokenizer from ...utils import logging UpperCamelCase__ :Union[str, Any] = logging.get_logger(__name__) UpperCamelCase__ :Optional[int] = """▁""" UpperCamelCase__ :Union[str, Any] = {"""vocab_file""": """sentencepiece.bpe.model"""} UpperCamelCase__ :Union[str, Any] = { """vocab_file""": { """facebook/mbart-large-50-one-to-many-mmt""": ( """https://huggingface.co/facebook/mbart-large-50-one-to-many-mmt/resolve/main/sentencepiece.bpe.model""" ), } } UpperCamelCase__ :Optional[Any] = { """facebook/mbart-large-50-one-to-many-mmt""": 1_024, } # fmt: off UpperCamelCase__ :Any = ["""ar_AR""", """cs_CZ""", """de_DE""", """en_XX""", """es_XX""", """et_EE""", """fi_FI""", """fr_XX""", """gu_IN""", """hi_IN""", """it_IT""", """ja_XX""", """kk_KZ""", """ko_KR""", """lt_LT""", """lv_LV""", """my_MM""", """ne_NP""", """nl_XX""", """ro_RO""", """ru_RU""", """si_LK""", """tr_TR""", """vi_VN""", """zh_CN""", """af_ZA""", """az_AZ""", """bn_IN""", """fa_IR""", """he_IL""", """hr_HR""", """id_ID""", """ka_GE""", """km_KH""", """mk_MK""", """ml_IN""", """mn_MN""", """mr_IN""", """pl_PL""", """ps_AF""", """pt_XX""", """sv_SE""", """sw_KE""", """ta_IN""", """te_IN""", """th_TH""", """tl_XX""", """uk_UA""", """ur_PK""", """xh_ZA""", """gl_ES""", """sl_SI"""] class A( lowerCamelCase__ ): """simple docstring""" A = VOCAB_FILES_NAMES A = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A = PRETRAINED_VOCAB_FILES_MAP A = ["input_ids", "attention_mask"] A = [] A = [] def __init__( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=None , SCREAMING_SNAKE_CASE__=None , SCREAMING_SNAKE_CASE__="</s>" , SCREAMING_SNAKE_CASE__="</s>" , SCREAMING_SNAKE_CASE__="<s>" , SCREAMING_SNAKE_CASE__="<unk>" , SCREAMING_SNAKE_CASE__="<pad>" , SCREAMING_SNAKE_CASE__="<mask>" , SCREAMING_SNAKE_CASE__ = None , **SCREAMING_SNAKE_CASE__ , ) -> None: """simple docstring""" _UpperCamelCase :Tuple = AddedToken(SCREAMING_SNAKE_CASE__ , lstrip=SCREAMING_SNAKE_CASE__ , rstrip=SCREAMING_SNAKE_CASE__ ) if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) else mask_token _UpperCamelCase :List[Any] = {} if sp_model_kwargs is None else sp_model_kwargs _UpperCamelCase :str = kwargs.get('''additional_special_tokens''' , [] ) kwargs["additional_special_tokens"] += [ code for code in FAIRSEQ_LANGUAGE_CODES if code not in kwargs["additional_special_tokens"] ] super().__init__( src_lang=SCREAMING_SNAKE_CASE__ , tgt_lang=SCREAMING_SNAKE_CASE__ , eos_token=SCREAMING_SNAKE_CASE__ , unk_token=SCREAMING_SNAKE_CASE__ , sep_token=SCREAMING_SNAKE_CASE__ , cls_token=SCREAMING_SNAKE_CASE__ , pad_token=SCREAMING_SNAKE_CASE__ , mask_token=SCREAMING_SNAKE_CASE__ , sp_model_kwargs=self.sp_model_kwargs , **SCREAMING_SNAKE_CASE__ , ) _UpperCamelCase :int = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(SCREAMING_SNAKE_CASE__ ) ) _UpperCamelCase :str = vocab_file # Original fairseq vocab and spm vocab must be "aligned": # Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ---- # fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's' | '▁de' | '-' # spm | '<unk>' | '<s>' | '</s>' | ',' | '.' | '▁' | 's' | '▁de' | '-' | '▁a' # Mimic fairseq token-to-id alignment for the first 4 token _UpperCamelCase :Optional[int] = {'''<s>''': 0, '''<pad>''': 1, '''</s>''': 2, '''<unk>''': 3} # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab _UpperCamelCase :Any = 1 _UpperCamelCase :Optional[Any] = len(self.sp_model ) _UpperCamelCase :Any = { code: self.sp_model_size + i + self.fairseq_offset for i, code in enumerate(SCREAMING_SNAKE_CASE__ ) } _UpperCamelCase :Union[str, Any] = {v: k for k, v in self.lang_code_to_id.items()} _UpperCamelCase :Optional[Any] = len(self.sp_model ) + len(self.lang_code_to_id ) + self.fairseq_offset self.fairseq_tokens_to_ids.update(self.lang_code_to_id ) _UpperCamelCase :Tuple = {v: k for k, v in self.fairseq_tokens_to_ids.items()} _UpperCamelCase :int = src_lang if src_lang is not None else '''en_XX''' _UpperCamelCase :int = self.lang_code_to_id[self._src_lang] _UpperCamelCase :int = tgt_lang self.set_src_lang_special_tokens(self._src_lang ) @property def _UpperCamelCase( self ) -> int: """simple docstring""" return len(self.sp_model ) + len(self.lang_code_to_id ) + self.fairseq_offset + 1 # Plus 1 for the mask token @property def _UpperCamelCase( self ) -> str: """simple docstring""" return self._src_lang @src_lang.setter def _UpperCamelCase( self , SCREAMING_SNAKE_CASE__ ) -> None: """simple docstring""" _UpperCamelCase :int = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def __getstate__( self ) -> Dict: """simple docstring""" _UpperCamelCase :int = self.__dict__.copy() _UpperCamelCase :Any = None return state def __setstate__( self , SCREAMING_SNAKE_CASE__ ) -> None: """simple docstring""" _UpperCamelCase :List[str] = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): _UpperCamelCase :int = {} _UpperCamelCase :int = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def _UpperCamelCase( self ) -> Dict: """simple docstring""" _UpperCamelCase :Any = {self.convert_ids_to_tokens(SCREAMING_SNAKE_CASE__ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def _UpperCamelCase( self , SCREAMING_SNAKE_CASE__ ) -> List[str]: """simple docstring""" return self.sp_model.encode(SCREAMING_SNAKE_CASE__ , out_type=SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase( self , SCREAMING_SNAKE_CASE__ ) -> int: """simple docstring""" if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] _UpperCamelCase :Any = self.sp_model.PieceToId(SCREAMING_SNAKE_CASE__ ) # Need to return unknown token if the SP model returned 0 return spm_id + self.fairseq_offset if spm_id else self.unk_token_id def _UpperCamelCase( self , SCREAMING_SNAKE_CASE__ ) -> str: """simple docstring""" if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def _UpperCamelCase( self , SCREAMING_SNAKE_CASE__ ) -> str: """simple docstring""" _UpperCamelCase :Any = [] _UpperCamelCase :List[str] = '''''' _UpperCamelCase :Any = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(SCREAMING_SNAKE_CASE__ ) + token _UpperCamelCase :Union[str, Any] = True _UpperCamelCase :Dict = [] else: current_sub_tokens.append(SCREAMING_SNAKE_CASE__ ) _UpperCamelCase :Dict = False out_string += self.sp_model.decode(SCREAMING_SNAKE_CASE__ ) return out_string.strip() def _UpperCamelCase( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = None ) -> Tuple[str]: """simple docstring""" if not os.path.isdir(SCREAMING_SNAKE_CASE__ ): logger.error(f"Vocabulary path ({save_directory}) should be a directory" ) return _UpperCamelCase :Optional[Any] = os.path.join( SCREAMING_SNAKE_CASE__ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(SCREAMING_SNAKE_CASE__ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , SCREAMING_SNAKE_CASE__ ) elif not os.path.isfile(self.vocab_file ): with open(SCREAMING_SNAKE_CASE__ , '''wb''' ) as fi: _UpperCamelCase :Optional[Any] = self.sp_model.serialized_model_proto() fi.write(SCREAMING_SNAKE_CASE__ ) return (out_vocab_file,) def _UpperCamelCase( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__ = False ) -> List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=SCREAMING_SNAKE_CASE__ , token_ids_a=SCREAMING_SNAKE_CASE__ , already_has_special_tokens=SCREAMING_SNAKE_CASE__ ) _UpperCamelCase :List[str] = [1] * len(self.prefix_tokens ) _UpperCamelCase :Tuple = [1] * len(self.suffix_tokens ) if token_ids_a is None: return prefix_ones + ([0] * len(SCREAMING_SNAKE_CASE__ )) + suffix_ones return prefix_ones + ([0] * len(SCREAMING_SNAKE_CASE__ )) + ([0] * len(SCREAMING_SNAKE_CASE__ )) + suffix_ones def _UpperCamelCase( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = None ) -> List[int]: """simple docstring""" if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def _UpperCamelCase( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) -> List[str]: """simple docstring""" if src_lang is None or tgt_lang is None: raise ValueError('''Translation requires a `src_lang` and a `tgt_lang` for this model''' ) _UpperCamelCase :int = src_lang _UpperCamelCase :Dict = self(SCREAMING_SNAKE_CASE__ , add_special_tokens=SCREAMING_SNAKE_CASE__ , return_tensors=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) _UpperCamelCase :Tuple = self.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) _UpperCamelCase :Optional[int] = tgt_lang_id return inputs def _UpperCamelCase( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = "en_XX" , SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__ = "ro_RO" , **SCREAMING_SNAKE_CASE__ , ) -> BatchEncoding: """simple docstring""" _UpperCamelCase :Dict = src_lang _UpperCamelCase :Union[str, Any] = tgt_lang return super().prepare_seqaseq_batch(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase( self ) -> List[Any]: """simple docstring""" return self.set_src_lang_special_tokens(self.src_lang ) def _UpperCamelCase( self ) -> str: """simple docstring""" return self.set_tgt_lang_special_tokens(self.tgt_lang ) def _UpperCamelCase( self , SCREAMING_SNAKE_CASE__ ) -> None: """simple docstring""" _UpperCamelCase :Tuple = self.lang_code_to_id[src_lang] _UpperCamelCase :Optional[int] = [self.cur_lang_code_id] _UpperCamelCase :Optional[int] = [self.eos_token_id] def _UpperCamelCase( self , SCREAMING_SNAKE_CASE__ ) -> None: """simple docstring""" _UpperCamelCase :Dict = self.lang_code_to_id[tgt_lang] _UpperCamelCase :Tuple = [self.cur_lang_code_id] _UpperCamelCase :Optional[int] = [self.eos_token_id]
355
"""simple docstring""" import warnings from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast from ...onnx.utils import compute_effective_axis_dimension from ...utils import TensorType, is_torch_available, logging UpperCamelCase__ :Dict = logging.get_logger(__name__) UpperCamelCase__ :int = { """facebook/bart-large""": """https://huggingface.co/facebook/bart-large/resolve/main/config.json""", # See all BART models at https://huggingface.co/models?filter=bart } class A( lowerCamelCase__ ): """simple docstring""" A = "bart" A = ["past_key_values"] A = {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"} def __init__( self , SCREAMING_SNAKE_CASE__=5_02_65 , SCREAMING_SNAKE_CASE__=10_24 , SCREAMING_SNAKE_CASE__=12 , SCREAMING_SNAKE_CASE__=40_96 , SCREAMING_SNAKE_CASE__=16 , SCREAMING_SNAKE_CASE__=12 , SCREAMING_SNAKE_CASE__=40_96 , SCREAMING_SNAKE_CASE__=16 , SCREAMING_SNAKE_CASE__=0.0 , SCREAMING_SNAKE_CASE__=0.0 , SCREAMING_SNAKE_CASE__="gelu" , SCREAMING_SNAKE_CASE__=10_24 , SCREAMING_SNAKE_CASE__=0.1 , SCREAMING_SNAKE_CASE__=0.0 , SCREAMING_SNAKE_CASE__=0.0 , SCREAMING_SNAKE_CASE__=0.0_2 , SCREAMING_SNAKE_CASE__=0.0 , SCREAMING_SNAKE_CASE__=False , SCREAMING_SNAKE_CASE__=True , SCREAMING_SNAKE_CASE__=3 , SCREAMING_SNAKE_CASE__=1 , SCREAMING_SNAKE_CASE__=0 , SCREAMING_SNAKE_CASE__=2 , SCREAMING_SNAKE_CASE__=True , SCREAMING_SNAKE_CASE__=2 , SCREAMING_SNAKE_CASE__=2 , **SCREAMING_SNAKE_CASE__ , ) -> Optional[Any]: """simple docstring""" _UpperCamelCase :Union[str, Any] = vocab_size _UpperCamelCase :Union[str, Any] = max_position_embeddings _UpperCamelCase :int = d_model _UpperCamelCase :Any = encoder_ffn_dim _UpperCamelCase :str = encoder_layers _UpperCamelCase :str = encoder_attention_heads _UpperCamelCase :Optional[int] = decoder_ffn_dim _UpperCamelCase :Any = decoder_layers _UpperCamelCase :Dict = decoder_attention_heads _UpperCamelCase :str = dropout _UpperCamelCase :Tuple = attention_dropout _UpperCamelCase :Optional[Any] = activation_dropout _UpperCamelCase :Optional[Any] = activation_function _UpperCamelCase :int = init_std _UpperCamelCase :Any = encoder_layerdrop _UpperCamelCase :List[Any] = decoder_layerdrop _UpperCamelCase :Union[str, Any] = classifier_dropout _UpperCamelCase :List[Any] = use_cache _UpperCamelCase :str = encoder_layers _UpperCamelCase :int = scale_embedding # scale factor will be sqrt(d_model) if True super().__init__( num_labels=SCREAMING_SNAKE_CASE__ , pad_token_id=SCREAMING_SNAKE_CASE__ , bos_token_id=SCREAMING_SNAKE_CASE__ , eos_token_id=SCREAMING_SNAKE_CASE__ , is_encoder_decoder=SCREAMING_SNAKE_CASE__ , decoder_start_token_id=SCREAMING_SNAKE_CASE__ , forced_eos_token_id=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ , ) # ensure backward compatibility for BART CNN models if self.forced_bos_token_id is None and kwargs.get('''force_bos_token_to_be_generated''' , SCREAMING_SNAKE_CASE__ ): _UpperCamelCase :Any = self.bos_token_id warnings.warn( f"Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. " '''The config can simply be saved and uploaded again to be fixed.''' ) class A( lowerCamelCase__ ): """simple docstring""" @property def _UpperCamelCase( self ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task in ["default", "seq2seq-lm"]: _UpperCamelCase :List[Any] = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ] ) if self.use_past: _UpperCamelCase :Optional[Any] = {0: '''batch'''} _UpperCamelCase :int = {0: '''batch''', 1: '''past_decoder_sequence + sequence'''} else: _UpperCamelCase :Optional[Any] = {0: '''batch''', 1: '''decoder_sequence'''} _UpperCamelCase :Optional[Any] = {0: '''batch''', 1: '''decoder_sequence'''} if self.use_past: self.fill_with_past_key_values_(SCREAMING_SNAKE_CASE__ , direction='''inputs''' ) elif self.task == "causal-lm": # TODO: figure this case out. _UpperCamelCase :Tuple = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ] ) if self.use_past: _UpperCamelCase , _UpperCamelCase :List[str] = self.num_layers for i in range(SCREAMING_SNAKE_CASE__ ): _UpperCamelCase :int = {0: '''batch''', 2: '''past_sequence + sequence'''} _UpperCamelCase :List[Any] = {0: '''batch''', 2: '''past_sequence + sequence'''} else: _UpperCamelCase :List[Any] = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''decoder_input_ids''', {0: '''batch''', 1: '''decoder_sequence'''}), ('''decoder_attention_mask''', {0: '''batch''', 1: '''decoder_sequence'''}), ] ) return common_inputs @property def _UpperCamelCase( self ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task in ["default", "seq2seq-lm"]: _UpperCamelCase :Optional[int] = super().outputs else: _UpperCamelCase :List[str] = super(SCREAMING_SNAKE_CASE__ , self ).outputs if self.use_past: _UpperCamelCase , _UpperCamelCase :int = self.num_layers for i in range(SCREAMING_SNAKE_CASE__ ): _UpperCamelCase :Optional[Any] = {0: '''batch''', 2: '''past_sequence + sequence'''} _UpperCamelCase :Optional[Any] = {0: '''batch''', 2: '''past_sequence + sequence'''} return common_outputs def _UpperCamelCase( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = -1 , SCREAMING_SNAKE_CASE__ = -1 , SCREAMING_SNAKE_CASE__ = False , SCREAMING_SNAKE_CASE__ = None , ) -> Mapping[str, Any]: """simple docstring""" _UpperCamelCase :str = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # Generate decoder inputs _UpperCamelCase :List[Any] = seq_length if not self.use_past else 1 _UpperCamelCase :Tuple = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) _UpperCamelCase :Union[str, Any] = {f"decoder_{name}": tensor for name, tensor in decoder_inputs.items()} _UpperCamelCase :str = dict(**SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch _UpperCamelCase , _UpperCamelCase :Any = common_inputs['''input_ids'''].shape _UpperCamelCase :Dict = common_inputs['''decoder_input_ids'''].shape[1] _UpperCamelCase , _UpperCamelCase :Any = self.num_attention_heads _UpperCamelCase :Dict = ( batch, num_encoder_attention_heads, encoder_seq_length, self._config.hidden_size // num_encoder_attention_heads, ) _UpperCamelCase :Tuple = decoder_seq_length + 3 _UpperCamelCase :Optional[int] = ( batch, num_decoder_attention_heads, decoder_past_length, self._config.hidden_size // num_decoder_attention_heads, ) _UpperCamelCase :Optional[int] = torch.cat( [common_inputs['''decoder_attention_mask'''], torch.ones(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )] , dim=1 ) _UpperCamelCase :Union[str, Any] = [] # If the number of encoder and decoder layers are present in the model configuration, both are considered _UpperCamelCase , _UpperCamelCase :int = self.num_layers _UpperCamelCase :Any = min(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) _UpperCamelCase :Optional[int] = max(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) - min_num_layers _UpperCamelCase :int = '''encoder''' if num_encoder_layers > num_decoder_layers else '''decoder''' for _ in range(SCREAMING_SNAKE_CASE__ ): common_inputs["past_key_values"].append( ( torch.zeros(SCREAMING_SNAKE_CASE__ ), torch.zeros(SCREAMING_SNAKE_CASE__ ), torch.zeros(SCREAMING_SNAKE_CASE__ ), torch.zeros(SCREAMING_SNAKE_CASE__ ), ) ) # TODO: test this. _UpperCamelCase :Optional[int] = encoder_shape if remaining_side_name == '''encoder''' else decoder_shape for _ in range(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): common_inputs["past_key_values"].append((torch.zeros(SCREAMING_SNAKE_CASE__ ), torch.zeros(SCREAMING_SNAKE_CASE__ )) ) return common_inputs def _UpperCamelCase( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = -1 , SCREAMING_SNAKE_CASE__ = -1 , SCREAMING_SNAKE_CASE__ = False , SCREAMING_SNAKE_CASE__ = None , ) -> Mapping[str, Any]: """simple docstring""" _UpperCamelCase :Dict = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch _UpperCamelCase , _UpperCamelCase :List[Any] = common_inputs['''input_ids'''].shape # Not using the same length for past_key_values _UpperCamelCase :str = seqlen + 2 _UpperCamelCase , _UpperCamelCase :Dict = self.num_layers _UpperCamelCase , _UpperCamelCase :Union[str, Any] = self.num_attention_heads _UpperCamelCase :List[Any] = ( batch, num_encoder_attention_heads, past_key_values_length, self._config.hidden_size // num_encoder_attention_heads, ) _UpperCamelCase :Tuple = common_inputs['''attention_mask'''].dtype _UpperCamelCase :List[Any] = torch.cat( [common_inputs['''attention_mask'''], torch.ones(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , dtype=SCREAMING_SNAKE_CASE__ )] , dim=1 ) _UpperCamelCase :int = [ (torch.zeros(SCREAMING_SNAKE_CASE__ ), torch.zeros(SCREAMING_SNAKE_CASE__ )) for _ in range(SCREAMING_SNAKE_CASE__ ) ] return common_inputs def _UpperCamelCase( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = -1 , SCREAMING_SNAKE_CASE__ = -1 , SCREAMING_SNAKE_CASE__ = False , SCREAMING_SNAKE_CASE__ = None , ) -> Mapping[str, Any]: """simple docstring""" _UpperCamelCase :List[str] = compute_effective_axis_dimension( SCREAMING_SNAKE_CASE__ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX _UpperCamelCase :str = tokenizer.num_special_tokens_to_add(SCREAMING_SNAKE_CASE__ ) _UpperCamelCase :str = compute_effective_axis_dimension( SCREAMING_SNAKE_CASE__ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=SCREAMING_SNAKE_CASE__ ) # Generate dummy inputs according to compute batch and sequence _UpperCamelCase :List[Any] = [''' '''.join([tokenizer.unk_token] ) * seq_length] * batch_size _UpperCamelCase :Optional[Any] = dict(tokenizer(SCREAMING_SNAKE_CASE__ , return_tensors=SCREAMING_SNAKE_CASE__ ) ) return common_inputs def _UpperCamelCase( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = -1 , SCREAMING_SNAKE_CASE__ = -1 , SCREAMING_SNAKE_CASE__ = False , SCREAMING_SNAKE_CASE__ = None , ) -> Mapping[str, Any]: """simple docstring""" if self.task in ["default", "seq2seq-lm"]: _UpperCamelCase :str = self._generate_dummy_inputs_for_default_and_seqaseq_lm( SCREAMING_SNAKE_CASE__ , batch_size=SCREAMING_SNAKE_CASE__ , seq_length=SCREAMING_SNAKE_CASE__ , is_pair=SCREAMING_SNAKE_CASE__ , framework=SCREAMING_SNAKE_CASE__ ) elif self.task == "causal-lm": _UpperCamelCase :Optional[Any] = self._generate_dummy_inputs_for_causal_lm( SCREAMING_SNAKE_CASE__ , batch_size=SCREAMING_SNAKE_CASE__ , seq_length=SCREAMING_SNAKE_CASE__ , is_pair=SCREAMING_SNAKE_CASE__ , framework=SCREAMING_SNAKE_CASE__ ) else: _UpperCamelCase :Any = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( SCREAMING_SNAKE_CASE__ , batch_size=SCREAMING_SNAKE_CASE__ , seq_length=SCREAMING_SNAKE_CASE__ , is_pair=SCREAMING_SNAKE_CASE__ , framework=SCREAMING_SNAKE_CASE__ ) return common_inputs def _UpperCamelCase( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Dict: """simple docstring""" if self.task in ["default", "seq2seq-lm"]: _UpperCamelCase :Union[str, Any] = super()._flatten_past_key_values_(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) else: _UpperCamelCase :Tuple = super(SCREAMING_SNAKE_CASE__ , self )._flatten_past_key_values_( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )
355
1
# coding=utf-8 # Copyright 2020 The HuggingFace Inc. team. # # 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. # this script dumps information about the environment import os import sys import transformers __UpperCAmelCase : Optional[Any] = '3' print('Python version:', sys.version) print('transformers version:', transformers.__version__) try: import torch print('Torch version:', torch.__version__) print('Cuda available:', torch.cuda.is_available()) print('Cuda version:', torch.version.cuda) print('CuDNN version:', torch.backends.cudnn.version()) print('Number of GPUs available:', torch.cuda.device_count()) print('NCCL version:', torch.cuda.nccl.version()) except ImportError: print('Torch version:', None) try: import deepspeed print('DeepSpeed version:', deepspeed.__version__) except ImportError: print('DeepSpeed version:', None) try: import tensorflow as tf print('TensorFlow version:', tf.__version__) print('TF GPUs available:', bool(tf.config.list_physical_devices('GPU'))) print('Number of TF GPUs available:', len(tf.config.list_physical_devices('GPU'))) except ImportError: print('TensorFlow version:', None)
716
import numpy as np import torch from imwatermark import WatermarkEncoder # Copied from https://github.com/Stability-AI/generative-models/blob/613af104c6b85184091d42d374fef420eddb356d/scripts/demo/streamlit_helpers.py#L66 __UpperCAmelCase : Any = 0B1_0_1_1_0_0_1_1_1_1_1_0_1_1_0_0_1_0_0_1_0_0_0_0_0_1_1_1_1_0_1_1_1_0_1_1_0_0_0_1_1_0_0_1_1_1_1_0 # bin(x)[2:] gives bits of x as str, use int to convert them to 0/1 __UpperCAmelCase : Optional[Any] = [int(bit) for bit in bin(WATERMARK_MESSAGE)[2:]] class lowerCamelCase : def __init__( self : Tuple ) -> List[Any]: _a : List[Any] = WATERMARK_BITS _a : List[str] = WatermarkEncoder() self.encoder.set_watermark('''bits''' , self.watermark ) def snake_case_ ( self : Dict , __snake_case : torch.FloatTensor ) -> Optional[Any]: # can't encode images that are smaller than 256 if images.shape[-1] < 256: return images _a : Union[str, Any] = (255 * (images / 2 + 0.5)).cpu().permute(0 , 2 , 3 , 1 ).float().numpy() _a : Any = [self.encoder.encode(__snake_case , '''dwtDct''' ) for image in images] _a : Optional[int] = torch.from_numpy(np.array(__snake_case ) ).permute(0 , 3 , 1 , 2 ) _a : Any = torch.clamp(2 * (images / 255 - 0.5) , min=-1.0 , max=1.0 ) return images
249
0
import unittest import numpy as np from diffusers import OnnxStableDiffusionInpaintPipelineLegacy from diffusers.utils.testing_utils import ( is_onnx_available, load_image, load_numpy, nightly, require_onnxruntime, require_torch_gpu, ) if is_onnx_available(): import onnxruntime as ort @nightly @require_onnxruntime @require_torch_gpu class lowerCamelCase__ ( unittest.TestCase): """simple docstring""" @property def snake_case_ ( self : List[str] ) -> Dict: return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def snake_case_ ( self : Dict ) -> Any: _A = ort.SessionOptions() _A = False return options def snake_case_ ( self : str ) -> List[Any]: _A = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) _A = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) _A = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/red_cat_sitting_on_a_park_bench_onnx.npy''' ) # using the PNDM scheduler by default _A = OnnxStableDiffusionInpaintPipelineLegacy.from_pretrained( '''CompVis/stable-diffusion-v1-4''' , revision='''onnx''' , safety_checker=__lowerCAmelCase , feature_extractor=__lowerCAmelCase , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=__lowerCAmelCase ) _A = '''A red cat sitting on a park bench''' _A = np.random.RandomState(0 ) _A = pipe( prompt=__lowerCAmelCase , image=__lowerCAmelCase , mask_image=__lowerCAmelCase , strength=0.75 , guidance_scale=7.5 , num_inference_steps=15 , generator=__lowerCAmelCase , output_type='''np''' , ) _A = output.images[0] assert image.shape == (5_12, 5_12, 3) assert np.abs(expected_image - image ).max() < 1E-2
2
'''simple docstring''' from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType __a = logging.get_logger(__name__) __a = { 'microsoft/deberta-v2-xlarge': 'https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json', 'microsoft/deberta-v2-xxlarge': 'https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json', 'microsoft/deberta-v2-xlarge-mnli': ( 'https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json' ), 'microsoft/deberta-v2-xxlarge-mnli': ( 'https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json' ), } class A__ ( UpperCamelCase ): """simple docstring""" UpperCamelCase_ : List[str] = '''deberta-v2''' def __init__( self : Union[str, Any] , lowerCAmelCase__ : Optional[Any]=1_2_8_1_0_0 , lowerCAmelCase__ : Optional[int]=1_5_3_6 , lowerCAmelCase__ : Dict=2_4 , lowerCAmelCase__ : Optional[Any]=2_4 , lowerCAmelCase__ : str=6_1_4_4 , lowerCAmelCase__ : List[str]="gelu" , lowerCAmelCase__ : Union[str, Any]=0.1 , lowerCAmelCase__ : Tuple=0.1 , lowerCAmelCase__ : Optional[Any]=5_1_2 , lowerCAmelCase__ : Optional[Any]=0 , lowerCAmelCase__ : List[str]=0.02 , lowerCAmelCase__ : Tuple=1e-7 , lowerCAmelCase__ : Union[str, Any]=False , lowerCAmelCase__ : Any=-1 , lowerCAmelCase__ : Dict=0 , lowerCAmelCase__ : List[Any]=True , lowerCAmelCase__ : List[Any]=None , lowerCAmelCase__ : int=0 , lowerCAmelCase__ : Optional[int]="gelu" , **lowerCAmelCase__ : List[Any] , ) -> List[Any]: """simple docstring""" super().__init__(**lowerCAmelCase__ ) _UpperCAmelCase : List[Any] = hidden_size _UpperCAmelCase : Optional[Any] = num_hidden_layers _UpperCAmelCase : str = num_attention_heads _UpperCAmelCase : Union[str, Any] = intermediate_size _UpperCAmelCase : Optional[int] = hidden_act _UpperCAmelCase : int = hidden_dropout_prob _UpperCAmelCase : List[str] = attention_probs_dropout_prob _UpperCAmelCase : List[str] = max_position_embeddings _UpperCAmelCase : Any = type_vocab_size _UpperCAmelCase : Dict = initializer_range _UpperCAmelCase : Dict = relative_attention _UpperCAmelCase : Tuple = max_relative_positions _UpperCAmelCase : Optional[int] = pad_token_id _UpperCAmelCase : Optional[int] = position_biased_input # Backwards compatibility if type(lowerCAmelCase__ ) == str: _UpperCAmelCase : List[Any] = [x.strip() for x in pos_att_type.lower().split("|" )] _UpperCAmelCase : Any = pos_att_type _UpperCAmelCase : Optional[int] = vocab_size _UpperCAmelCase : str = layer_norm_eps _UpperCAmelCase : Any = kwargs.get("pooler_hidden_size" , lowerCAmelCase__ ) _UpperCAmelCase : Any = pooler_dropout _UpperCAmelCase : Any = pooler_hidden_act class A__ ( UpperCamelCase ): """simple docstring""" @property def _lowerCAmelCase ( self : List[Any] ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task == "multiple-choice": _UpperCAmelCase : Union[str, Any] = {0: "batch", 1: "choice", 2: "sequence"} else: _UpperCAmelCase : Optional[int] = {0: "batch", 1: "sequence"} if self._config.type_vocab_size > 0: return OrderedDict( [("input_ids", dynamic_axis), ("attention_mask", dynamic_axis), ("token_type_ids", dynamic_axis)] ) else: return OrderedDict([("input_ids", dynamic_axis), ("attention_mask", dynamic_axis)] ) @property def _lowerCAmelCase ( self : str ) -> int: """simple docstring""" return 1_2 def _lowerCAmelCase ( self : List[Any] , lowerCAmelCase__ : Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"] , lowerCAmelCase__ : int = -1 , lowerCAmelCase__ : int = -1 , lowerCAmelCase__ : int = -1 , lowerCAmelCase__ : bool = False , lowerCAmelCase__ : Optional["TensorType"] = None , lowerCAmelCase__ : int = 3 , lowerCAmelCase__ : int = 4_0 , lowerCAmelCase__ : int = 4_0 , lowerCAmelCase__ : "PreTrainedTokenizerBase" = None , ) -> Mapping[str, Any]: """simple docstring""" _UpperCAmelCase : List[Any] = super().generate_dummy_inputs(preprocessor=lowerCAmelCase__ , framework=lowerCAmelCase__ ) if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs: del dummy_inputs["token_type_ids"] return dummy_inputs
494
0
"""simple docstring""" from math import factorial def __a ( _lowercase = 100 ): """simple docstring""" return sum(map(_lowercase , str(factorial(_lowercase ) ) ) ) if __name__ == "__main__": print(solution(int(input("Enter the Number: ").strip())))
121
"""simple docstring""" import gc import tempfile import unittest import numpy as np import torch from diffusers import VersatileDiffusionPipeline from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device UpperCAmelCase : Dict = False class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): pass @nightly @require_torch_gpu class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): def lowercase_ ( self :Optional[Any] ) -> Optional[Any]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def lowercase_ ( self :Dict ) -> List[str]: """simple docstring""" lowerCamelCase__ : Any = VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion''' ,torch_dtype=torch.floataa ) pipe.to(__UpperCAmelCase ) pipe.set_progress_bar_config(disable=__UpperCAmelCase ) lowerCamelCase__ : List[Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg''' ) lowerCamelCase__ : Union[str, Any] = torch.manual_seed(0 ) lowerCamelCase__ : Optional[int] = pipe.dual_guided( prompt='''first prompt''' ,image=__UpperCAmelCase ,text_to_image_strength=0.75 ,generator=__UpperCAmelCase ,guidance_scale=7.5 ,num_inference_steps=2 ,output_type='''numpy''' ,).images with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(__UpperCAmelCase ) lowerCamelCase__ : Dict = VersatileDiffusionPipeline.from_pretrained(__UpperCAmelCase ,torch_dtype=torch.floataa ) pipe.to(__UpperCAmelCase ) pipe.set_progress_bar_config(disable=__UpperCAmelCase ) lowerCamelCase__ : Dict = generator.manual_seed(0 ) lowerCamelCase__ : List[str] = pipe.dual_guided( prompt='''first prompt''' ,image=__UpperCAmelCase ,text_to_image_strength=0.75 ,generator=__UpperCAmelCase ,guidance_scale=7.5 ,num_inference_steps=2 ,output_type='''numpy''' ,).images assert np.abs(image - new_image ).sum() < 1E-5, "Models don't have the same forward pass" def lowercase_ ( self :Tuple ) -> str: """simple docstring""" lowerCamelCase__ : Optional[Any] = VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion''' ,torch_dtype=torch.floataa ) pipe.to(__UpperCAmelCase ) pipe.set_progress_bar_config(disable=__UpperCAmelCase ) lowerCamelCase__ : Tuple = '''cyberpunk 2077''' lowerCamelCase__ : str = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg''' ) lowerCamelCase__ : str = torch.manual_seed(0 ) lowerCamelCase__ : Union[str, Any] = pipe.dual_guided( prompt=__UpperCAmelCase ,image=__UpperCAmelCase ,text_to_image_strength=0.75 ,generator=__UpperCAmelCase ,guidance_scale=7.5 ,num_inference_steps=50 ,output_type='''numpy''' ,).images lowerCamelCase__ : Tuple = image[0, 2_53:2_56, 2_53:2_56, -1] assert image.shape == (1, 5_12, 5_12, 3) lowerCamelCase__ : Optional[Any] = np.array([0.1_448, 0.1_619, 0.1_741, 0.1_086, 0.1_147, 0.1_128, 0.1_199, 0.1_165, 0.1_001] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 lowerCamelCase__ : Tuple = '''A painting of a squirrel eating a burger ''' lowerCamelCase__ : Optional[Any] = torch.manual_seed(0 ) lowerCamelCase__ : List[Any] = pipe.text_to_image( prompt=__UpperCAmelCase ,generator=__UpperCAmelCase ,guidance_scale=7.5 ,num_inference_steps=50 ,output_type='''numpy''' ).images lowerCamelCase__ : List[Any] = image[0, 2_53:2_56, 2_53:2_56, -1] assert image.shape == (1, 5_12, 5_12, 3) lowerCamelCase__ : Optional[Any] = np.array([0.3_367, 0.3_169, 0.2_656, 0.3_870, 0.4_790, 0.3_796, 0.4_009, 0.4_878, 0.4_778] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 lowerCamelCase__ : Any = pipe.image_variation(__UpperCAmelCase ,generator=__UpperCAmelCase ,output_type='''numpy''' ).images lowerCamelCase__ : Union[str, Any] = image[0, 2_53:2_56, 2_53:2_56, -1] assert image.shape == (1, 5_12, 5_12, 3) lowerCamelCase__ : str = np.array([0.3_076, 0.3_123, 0.3_284, 0.3_782, 0.3_770, 0.3_894, 0.4_297, 0.4_331, 0.4_456] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1
121
1
"""simple docstring""" def UpperCAmelCase_ ( __a : Dict , __a : str ): '''simple docstring''' _lowerCamelCase : int = '' for i in table: res += inp[i - 1] return res def UpperCAmelCase_ ( __a : Any ): '''simple docstring''' return data[1:] + data[0] def UpperCAmelCase_ ( __a : Union[str, Any] , __a : Dict ): '''simple docstring''' _lowerCamelCase : Any = '' for i in range(len(__a ) ): if a[i] == b[i]: res += "0" else: res += "1" return res def UpperCAmelCase_ ( __a : List[Any] , __a : Optional[Any] ): '''simple docstring''' _lowerCamelCase : Optional[int] = int('0b' + data[0] + data[-1] , 2 ) _lowerCamelCase : List[Any] = int('0b' + data[1:3] , 2 ) return bin(s[row][col] )[2:] def UpperCAmelCase_ ( __a : Any , __a : Tuple , __a : Optional[int] , __a : Optional[int] , __a : int ): '''simple docstring''' _lowerCamelCase : Tuple = message[:4] _lowerCamelCase : Dict = message[4:] _lowerCamelCase : Optional[int] = apply_table(__a , __a ) _lowerCamelCase : Union[str, Any] = xor(__a , __a ) _lowerCamelCase : Tuple = apply_sbox(__a , temp[:4] ) # noqa: E741 _lowerCamelCase : str = apply_sbox(__a , temp[4:] ) _lowerCamelCase : List[str] = '0' * (2 - len(__a )) + l # noqa: E741 _lowerCamelCase : List[str] = '0' * (2 - len(__a )) + r _lowerCamelCase : Union[str, Any] = apply_table(l + r , __a ) _lowerCamelCase : List[str] = xor(__a , __a ) return temp + right if __name__ == "__main__": a_ = input("""Enter 10 bit key: """) a_ = input("""Enter 8 bit message: """) a_ = [6, 3, 7, 4, 8, 5, 10, 9] a_ = [3, 5, 2, 7, 4, 10, 1, 9, 8, 6] a_ = [2, 4, 3, 1] a_ = [2, 6, 3, 1, 4, 8, 5, 7] a_ = [4, 1, 3, 5, 7, 2, 8, 6] a_ = [4, 1, 2, 3, 2, 3, 4, 1] a_ = [[1, 0, 3, 2], [3, 2, 1, 0], [0, 2, 1, 3], [3, 1, 3, 2]] a_ = [[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 1, 0, 3]] # key generation a_ = apply_table(key, paa_table) a_ = temp[:5] a_ = temp[5:] a_ = left_shift(left) a_ = left_shift(right) a_ = apply_table(left + right, pa_table) a_ = left_shift(left) a_ = left_shift(right) a_ = left_shift(left) a_ = left_shift(right) a_ = apply_table(left + right, pa_table) # encryption a_ = apply_table(message, IP) a_ = function(expansion, sa, sa, keya, temp) a_ = temp[4:] + temp[:4] a_ = function(expansion, sa, sa, keya, temp) a_ = apply_table(temp, IP_inv) print("""Cipher text is:""", CT) # decryption a_ = apply_table(CT, IP) a_ = function(expansion, sa, sa, keya, temp) a_ = temp[4:] + temp[:4] a_ = function(expansion, sa, sa, keya, temp) a_ = apply_table(temp, IP_inv) print("""Plain text after decypting is:""", PT)
437
"""simple docstring""" # Lint as: python3 import itertools import os import re a_ = re.compile(r"""([A-Z]+)([A-Z][a-z])""") a_ = re.compile(r"""([a-z\d])([A-Z])""") a_ = re.compile(r"""(?<!_)_(?!_)""") a_ = re.compile(r"""(_{2,})""") a_ = r"""^\w+(\.\w+)*$""" a_ = r"""<>:/\|?*""" def UpperCAmelCase_ ( __a : Optional[int] ): '''simple docstring''' _lowerCamelCase : str = _uppercase_uppercase_re.sub(r'\1_\2' , __a ) _lowerCamelCase : Tuple = _lowercase_uppercase_re.sub(r'\1_\2' , __a ) return name.lower() def UpperCAmelCase_ ( __a : Optional[int] ): '''simple docstring''' _lowerCamelCase : Dict = _single_underscore_re.split(__a ) _lowerCamelCase : Tuple = [_multiple_underscores_re.split(__a ) for n in name] return "".join(n.capitalize() for n in itertools.chain.from_iterable(__a ) if n != '' ) def UpperCAmelCase_ ( __a : List[Any] ): '''simple docstring''' if os.path.basename(__a ) != name: raise ValueError(f"Should be a dataset name, not a path: {name}" ) return camelcase_to_snakecase(__a ) def UpperCAmelCase_ ( __a : Union[str, Any] , __a : Optional[int] ): '''simple docstring''' if os.path.basename(__a ) != name: raise ValueError(f"Should be a dataset name, not a path: {name}" ) if not re.match(_split_re , __a ): raise ValueError(f"Split name should match '{_split_re}'' but got '{split}'." ) return f"{filename_prefix_for_name(__a )}-{split}" def UpperCAmelCase_ ( __a : Any , __a : Union[str, Any] , __a : List[Any] , __a : List[str]=None ): '''simple docstring''' _lowerCamelCase : List[Any] = filename_prefix_for_split(__a , __a ) if filetype_suffix: prefix += f".{filetype_suffix}" _lowerCamelCase : List[str] = os.path.join(__a , __a ) return f"{filepath}*" def UpperCAmelCase_ ( __a : str , __a : List[Any] , __a : List[str] , __a : Tuple=None , __a : Tuple=None ): '''simple docstring''' _lowerCamelCase : Tuple = filename_prefix_for_split(__a , __a ) _lowerCamelCase : List[str] = os.path.join(__a , __a ) if shard_lengths: _lowerCamelCase : Union[str, Any] = len(__a ) _lowerCamelCase : str = [f"{prefix}-{shard_id:05d}-of-{num_shards:05d}" for shard_id in range(__a )] if filetype_suffix: _lowerCamelCase : int = [filename + f".{filetype_suffix}" for filename in filenames] return filenames else: _lowerCamelCase : int = prefix if filetype_suffix: filename += f".{filetype_suffix}" return [filename]
437
1
import logging import os import random import sys from dataclasses import dataclass, field from typing import Optional import datasets import numpy as np import pandas as pd from datasets import load_dataset import transformers from transformers import ( AutoConfig, BartForSequenceClassification, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, TapexTokenizer, Trainer, TrainingArguments, default_data_collator, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version from transformers.utils.versions import require_version # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("""4.17.0.dev0""") require_version("""datasets>=1.8.0""", """To fix: pip install -r examples/pytorch/text-classification/requirements.txt""") lowercase_ = logging.getLogger(__name__) @dataclass class _snake_case : UpperCamelCase__ : Optional[str] =field( default="""tab_fact""" , metadata={"""help""": """The name of the dataset to use (via the datasets library)."""}) UpperCamelCase__ : Optional[str] =field( default="""tab_fact""" , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} , ) UpperCamelCase__ : int =field( default=1_0_2_4 , metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } , ) UpperCamelCase__ : bool =field( default=lowercase__ , metadata={"""help""": """Overwrite the cached preprocessed datasets or not."""}) UpperCamelCase__ : bool =field( default=lowercase__ , metadata={ """help""": ( """Whether to pad all samples to `max_seq_length`. """ """If False, will pad the samples dynamically when batching to the maximum length in the batch.""" ) } , ) UpperCamelCase__ : Optional[int] =field( default=lowercase__ , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } , ) UpperCamelCase__ : Optional[int] =field( default=lowercase__ , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of evaluation examples to this """ """value if set.""" ) } , ) UpperCamelCase__ : Optional[int] =field( default=lowercase__ , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of prediction examples to this """ """value if set.""" ) } , ) UpperCamelCase__ : Optional[str] =field( default=lowercase__ , metadata={"""help""": """A csv or a json file containing the training data."""}) UpperCamelCase__ : Optional[str] =field( default=lowercase__ , metadata={"""help""": """A csv or a json file containing the validation data."""}) UpperCamelCase__ : Optional[str] =field(default=lowercase__ , metadata={"""help""": """A csv or a json file containing the test data."""}) def A__ ( self : Any ): if self.dataset_name is not None: pass elif self.train_file is None or self.validation_file is None: raise ValueError("Need either a GLUE task, a training/validation file or a dataset name." ) else: lowercase__ = self.train_file.split("." )[-1] assert train_extension in ["csv", "json"], "`train_file` should be a csv or a json file." lowercase__ = self.validation_file.split("." )[-1] assert ( validation_extension == train_extension ), "`validation_file` should have the same extension (csv or json) as `train_file`." @dataclass class _snake_case : UpperCamelCase__ : str =field( default=lowercase__ , metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""}) UpperCamelCase__ : Optional[str] =field( default=lowercase__ , metadata={"""help""": """Pretrained config name or path if not the same as model_name"""}) UpperCamelCase__ : Optional[str] =field( default=lowercase__ , metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""}) UpperCamelCase__ : Optional[str] =field( default=lowercase__ , metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} , ) UpperCamelCase__ : bool =field( default=lowercase__ , metadata={"""help""": """Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."""} , ) UpperCamelCase__ : str =field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) UpperCamelCase__ : bool =field( default=lowercase__ , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) def __lowerCAmelCase ( ): # 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. lowercase__ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(".json" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. lowercase__ , lowercase__ , lowercase__ = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: lowercase__ , lowercase__ , lowercase__ = parser.parse_args_into_dataclasses() # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , handlers=[logging.StreamHandler(sys.stdout )] , ) lowercase__ = training_args.get_process_log_level() logger.setLevel(SCREAMING_SNAKE_CASE_ ) datasets.utils.logging.set_verbosity(SCREAMING_SNAKE_CASE_ ) transformers.utils.logging.set_verbosity(SCREAMING_SNAKE_CASE_ ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( f'''Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}''' + f'''distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}''' ) logger.info(f'''Training/evaluation parameters {training_args}''' ) # Detecting last checkpoint. lowercase__ = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: lowercase__ = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( f'''Output directory ({training_args.output_dir}) already exists and is not empty. ''' "Use --overwrite_output_dir to overcome." ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( f'''Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change ''' "the `--output_dir` or add `--overwrite_output_dir` to train from scratch." ) # Set seed before initializing model. set_seed(training_args.seed ) # Get the datasets: you can either provide your own CSV/JSON training and evaluation files (see below) # or specify a GLUE benchmark task (the dataset will be downloaded automatically from the datasets Hub). # # For JSON files, this script will use the `question` column for the input question and `table` column for the corresponding table. # # If the CSVs/JSONs contain only one non-label column, the script does single sentence classification on this # single column. You can easily tweak this behavior (see below) # # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.dataset_name is not None: # Downloading and loading a dataset from the hub. lowercase__ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , cache_dir=model_args.cache_dir ) else: # Loading a dataset from your local files. # CSV/JSON training and evaluation files are needed. lowercase__ = {"train": data_args.train_file, "validation": data_args.validation_file} # Get the test dataset: you can provide your own CSV/JSON test file (see below) # when you use `do_predict` without specifying a GLUE benchmark task. if training_args.do_predict: if data_args.test_file is not None: lowercase__ = data_args.train_file.split("." )[-1] lowercase__ = data_args.test_file.split("." )[-1] assert ( test_extension == train_extension ), "`test_file` should have the same extension (csv or json) as `train_file`." lowercase__ = data_args.test_file else: raise ValueError("Need either a GLUE task or a test file for `do_predict`." ) for key in data_files.keys(): logger.info(f'''load a local file for {key}: {data_files[key]}''' ) if data_args.train_file.endswith(".csv" ): # Loading a dataset from local csv files lowercase__ = load_dataset("csv" , data_files=SCREAMING_SNAKE_CASE_ , cache_dir=model_args.cache_dir ) else: # Loading a dataset from local json files lowercase__ = load_dataset("json" , data_files=SCREAMING_SNAKE_CASE_ , cache_dir=model_args.cache_dir ) # See more about loading any type of standard or custom dataset at # https://huggingface.co/docs/datasets/loading_datasets.html. # Labels lowercase__ = raw_datasets["train"].features["label"].names lowercase__ = len(SCREAMING_SNAKE_CASE_ ) # Load pretrained model and tokenizer # # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. lowercase__ = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=SCREAMING_SNAKE_CASE_ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # load tapex tokenizer lowercase__ = TapexTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , use_fast=model_args.use_fast_tokenizer , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , add_prefix_space=SCREAMING_SNAKE_CASE_ , ) lowercase__ = BartForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool(".ckpt" in model_args.model_name_or_path ) , config=SCREAMING_SNAKE_CASE_ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # Padding strategy if data_args.pad_to_max_length: lowercase__ = "max_length" else: # We will pad later, dynamically at batch creation, to the max sequence length in each batch lowercase__ = False # Some models have set the order of the labels to use, so let's make sure we do use it. lowercase__ = {"Refused": 0, "Entailed": 1} lowercase__ = {0: "Refused", 1: "Entailed"} if data_args.max_seq_length > tokenizer.model_max_length: logger.warning( f'''The max_seq_length passed ({data_args.max_seq_length}) is larger than the maximum length for the''' f'''model ({tokenizer.model_max_length}). Using max_seq_length={tokenizer.model_max_length}.''' ) lowercase__ = min(data_args.max_seq_length , tokenizer.model_max_length ) def preprocess_tabfact_function(SCREAMING_SNAKE_CASE_ ): # Tokenize the texts def _convert_table_text_to_pandas(SCREAMING_SNAKE_CASE_ ): lowercase__ = [_table_row.split("#" ) for _table_row in _table_text.strip("\n" ).split("\n" )] lowercase__ = pd.DataFrame.from_records(_table_content[1:] , columns=_table_content[0] ) return _table_pd lowercase__ = examples["statement"] lowercase__ = list(map(_convert_table_text_to_pandas , examples["table_text"] ) ) lowercase__ = tokenizer(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ ) lowercase__ = examples["label"] return result with training_args.main_process_first(desc="dataset map pre-processing" ): lowercase__ = raw_datasets.map( SCREAMING_SNAKE_CASE_ , batched=SCREAMING_SNAKE_CASE_ , load_from_cache_file=not data_args.overwrite_cache , desc="Running tokenizer on dataset" , ) if training_args.do_train: if "train" not in raw_datasets: raise ValueError("--do_train requires a train dataset" ) lowercase__ = raw_datasets["train"] if data_args.max_train_samples is not None: lowercase__ = train_dataset.select(range(data_args.max_train_samples ) ) if training_args.do_eval: if "validation" not in raw_datasets and "validation_matched" not in raw_datasets: raise ValueError("--do_eval requires a validation dataset" ) lowercase__ = raw_datasets["validation"] if data_args.max_eval_samples is not None: lowercase__ = eval_dataset.select(range(data_args.max_eval_samples ) ) if training_args.do_predict or data_args.test_file is not None: if "test" not in raw_datasets and "test_matched" not in raw_datasets: raise ValueError("--do_predict requires a test dataset" ) lowercase__ = raw_datasets["test"] if data_args.max_predict_samples is not None: lowercase__ = predict_dataset.select(range(data_args.max_predict_samples ) ) # Log a few random samples from the training set: if training_args.do_train: for index in random.sample(range(len(SCREAMING_SNAKE_CASE_ ) ) , 3 ): logger.info(f'''Sample {index} of the training set: {train_dataset[index]}.''' ) # You can define your custom compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a # predictions and label_ids field) and has to return a dictionary string to float. def compute_metrics(SCREAMING_SNAKE_CASE_ ): lowercase__ = p.predictions[0] if isinstance(p.predictions , SCREAMING_SNAKE_CASE_ ) else p.predictions lowercase__ = np.argmax(SCREAMING_SNAKE_CASE_ , axis=1 ) return {"accuracy": (preds == p.label_ids).astype(np.floataa ).mean().item()} # Data collator will default to DataCollatorWithPadding, so we change it if we already did the padding. if data_args.pad_to_max_length: lowercase__ = default_data_collator elif training_args.fpaa: lowercase__ = DataCollatorWithPadding(SCREAMING_SNAKE_CASE_ , pad_to_multiple_of=8 ) else: lowercase__ = None # Initialize our Trainer lowercase__ = Trainer( model=SCREAMING_SNAKE_CASE_ , args=SCREAMING_SNAKE_CASE_ , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , compute_metrics=SCREAMING_SNAKE_CASE_ , tokenizer=SCREAMING_SNAKE_CASE_ , data_collator=SCREAMING_SNAKE_CASE_ , ) # Training if training_args.do_train: lowercase__ = None if training_args.resume_from_checkpoint is not None: lowercase__ = training_args.resume_from_checkpoint elif last_checkpoint is not None: lowercase__ = last_checkpoint lowercase__ = trainer.train(resume_from_checkpoint=SCREAMING_SNAKE_CASE_ ) lowercase__ = train_result.metrics lowercase__ = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(SCREAMING_SNAKE_CASE_ ) ) lowercase__ = min(SCREAMING_SNAKE_CASE_ , len(SCREAMING_SNAKE_CASE_ ) ) trainer.save_model() # Saves the tokenizer too for easy upload trainer.log_metrics("train" , SCREAMING_SNAKE_CASE_ ) trainer.save_metrics("train" , SCREAMING_SNAKE_CASE_ ) trainer.save_state() # Evaluation if training_args.do_eval: logger.info("*** Evaluate ***" ) lowercase__ = trainer.evaluate(eval_dataset=SCREAMING_SNAKE_CASE_ ) lowercase__ = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(SCREAMING_SNAKE_CASE_ ) lowercase__ = min(SCREAMING_SNAKE_CASE_ , len(SCREAMING_SNAKE_CASE_ ) ) trainer.log_metrics("eval" , SCREAMING_SNAKE_CASE_ ) trainer.save_metrics("eval" , SCREAMING_SNAKE_CASE_ ) if training_args.do_predict: logger.info("*** Predict ***" ) # Removing the `label` columns because it contains -1 and Trainer won't like that. lowercase__ = predict_dataset.remove_columns("label" ) lowercase__ = trainer.predict(SCREAMING_SNAKE_CASE_ , metric_key_prefix="predict" ).predictions lowercase__ = np.argmax(SCREAMING_SNAKE_CASE_ , axis=1 ) lowercase__ = os.path.join(training_args.output_dir , "predict_results_tabfact.txt" ) if trainer.is_world_process_zero(): with open(SCREAMING_SNAKE_CASE_ , "w" ) as writer: logger.info("***** Predict Results *****" ) writer.write("index\tprediction\n" ) for index, item in enumerate(SCREAMING_SNAKE_CASE_ ): lowercase__ = label_list[item] writer.write(f'''{index}\t{item}\n''' ) lowercase__ = {"finetuned_from": model_args.model_name_or_path, "tasks": "text-classification"} if training_args.push_to_hub: trainer.push_to_hub(**SCREAMING_SNAKE_CASE_ ) else: trainer.create_model_card(**SCREAMING_SNAKE_CASE_ ) def __lowerCAmelCase ( SCREAMING_SNAKE_CASE_ ): # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
37
import math def __lowerCAmelCase ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): if 0 not in (x, y): # We use the relation x^y = y*log10(x), where 10 is the base. return y * math.logaa(SCREAMING_SNAKE_CASE_ ) else: if x == 0: # 0 raised to any number is 0 return 0 elif y == 0: return 1 # any number raised to 0 is 1 raise AssertionError("This should never happen" ) if __name__ == "__main__": # Main function # Read two numbers from input and typecast them to int using map function. # Here x is the base and y is the power. lowercase_ = """Enter the base and the power separated by a comma: """ lowercase_ , lowercase_ = map(int, input(prompt).split(""",""")) lowercase_ , lowercase_ = map(int, input(prompt).split(""",""")) # We find the log of each number, using the function res(), which takes two # arguments. lowercase_ = res(xa, ya) lowercase_ = res(xa, ya) # We check for the largest number if resa > resa: print("""Largest number is""", xa, """^""", ya) elif resa > resa: print("""Largest number is""", xa, """^""", ya) else: print("""Both are equal""")
37
1
"""simple docstring""" import json import os from typing import Dict, List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging UpperCAmelCase__ =logging.get_logger(__name__) UpperCAmelCase__ ={ "vocab_file": "vocab.json", "tokenizer_config_file": "tokenizer_config.json", "merges_file": "merges.txt", } UpperCAmelCase__ ={ "vocab_file": { "facebook/s2t-wav2vec2-large-en-de": ( "https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/vocab.json" ), }, "tokenizer_config_file": { "facebook/s2t-wav2vec2-large-en-de": ( "https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/tokenizer_config.json" ), }, "merges_file": { "facebook/s2t-wav2vec2-large-en-de": ( "https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/merges.txt" ), }, } UpperCAmelCase__ ="</w>" UpperCAmelCase__ ="@@ " def lowerCAmelCase_ ( UpperCamelCase__ : Optional[Any] ): """simple docstring""" __lowercase = set() __lowercase = word[0] for char in word[1:]: pairs.add((prev_char, char) ) __lowercase = char return pairs # Speech2Text2 has no max input length UpperCAmelCase__ ={"facebook/s2t-wav2vec2-large-en-de": 1024} class lowerCamelCase__ ( _a ): a : List[Any] = VOCAB_FILES_NAMES a : Optional[int] = PRETRAINED_VOCAB_FILES_MAP a : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES a : str = ["""input_ids""", """attention_mask"""] def __init__( self : Optional[Any] , A_ : Tuple , A_ : str="<s>" , A_ : Optional[Any]="<pad>" , A_ : Dict="</s>" , A_ : List[str]="<unk>" , A_ : List[str]=False , A_ : Dict=None , **A_ : List[Any] , ): '''simple docstring''' super().__init__( unk_token=A_ , bos_token=A_ , eos_token=A_ , pad_token=A_ , do_lower_case=A_ , **A_ , ) __lowercase = do_lower_case with open(A_ , encoding="""utf-8""" ) as vocab_handle: __lowercase = json.load(A_ ) __lowercase = {v: k for k, v in self.encoder.items()} if merges_file is None: logger.info(F'''No merges files provided. {self.__class__.__name__} can only be used for decoding.''' ) __lowercase = None __lowercase = None else: with open(A_ , encoding="""utf-8""" ) as merges_handle: __lowercase = merges_handle.read().split("""\n""" )[:-1] __lowercase = [tuple(merge.split()[:2] ) for merge in merges] __lowercase = dict(zip(A_ , range(len(A_ ) ) ) ) __lowercase = {} @property def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] ): '''simple docstring''' return len(self.decoder ) def SCREAMING_SNAKE_CASE_ ( self : Tuple ): '''simple docstring''' return dict(self.encoder , **self.added_tokens_encoder ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , A_ : List[str] ): '''simple docstring''' __lowercase = tuple(token[:-1] ) + (token[-1] + BPE_TOKEN_MERGES,) if token in self.cache: return self.cache[token] __lowercase = get_pairs(A_ ) if not pairs: return token while True: __lowercase = min(A_ , key=lambda A_ : self.bpe_ranks.get(A_ , float("""inf""" ) ) ) if bigram not in self.bpe_ranks: break __lowercase , __lowercase = bigram __lowercase = [] __lowercase = 0 while i < len(A_ ): try: __lowercase = word.index(A_ , A_ ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) __lowercase = j if word[i] == first and i < len(A_ ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 __lowercase = tuple(A_ ) __lowercase = new_word if len(A_ ) == 1: break else: __lowercase = get_pairs(A_ ) __lowercase = """ """.join(A_ ) if word == "\n " + BPE_TOKEN_MERGES: __lowercase = """\n""" + BPE_TOKEN_MERGES if word.endswith(A_ ): __lowercase = word.replace(A_ , """""" ) __lowercase = word.replace(""" """ , A_ ) __lowercase = word return word def SCREAMING_SNAKE_CASE_ ( self : str , A_ : Tuple ): '''simple docstring''' if self.bpe_ranks is None: raise ValueError( """This tokenizer was instantiated without a `merges.txt` file, so""" """ that it can only be used for decoding, not for encoding.""" """Make sure to provide `merges.txt` file at instantiation to enable """ """encoding.""" ) if self.do_lower_case: __lowercase = text.lower() __lowercase = text.split() __lowercase = [] for token in text: if token: split_tokens.extend(list(self.bpe(A_ ).split(""" """ ) ) ) return split_tokens def SCREAMING_SNAKE_CASE_ ( self : Any , A_ : str ): '''simple docstring''' return self.encoder.get(A_ , self.encoder.get(self.unk_token ) ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , A_ : int ): '''simple docstring''' __lowercase = self.decoder.get(A_ , self.unk_token ) return result def SCREAMING_SNAKE_CASE_ ( self : Dict , A_ : List[str] ): '''simple docstring''' __lowercase = """ """.join(A_ ) # make sure @@ tokens are concatenated __lowercase = """""".join(string.split(A_ ) ) return string def SCREAMING_SNAKE_CASE_ ( self : List[str] , A_ : str , A_ : Optional[str] = None ): '''simple docstring''' if not os.path.isdir(A_ ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return __lowercase = os.path.join( A_ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) __lowercase = os.path.join( A_ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""merges_file"""] ) with open(A_ , """w""" , encoding="""utf-8""" ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=A_ , ensure_ascii=A_ ) + """\n""" ) __lowercase = 0 if self.bpe_ranks is None: return (vocab_file,) with open(A_ , """w""" , encoding="""utf-8""" ) as writer: for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda A_ : kv[1] ): if index != token_index: logger.warning( F'''Saving vocabulary to {merges_file}: BPE merge indices are not consecutive.''' """ Please check that the tokenizer is not corrupted!""" ) __lowercase = token_index writer.write(""" """.join(A_ ) + """\n""" ) index += 1 return (vocab_file, merges_file)
616
"""simple docstring""" import pytest import requests from datasets.utils.file_utils import http_head from .utils import OfflineSimulationMode, RequestWouldHangIndefinitelyError, offline @pytest.mark.integration def lowerCAmelCase_ ( ): """simple docstring""" with offline(OfflineSimulationMode.CONNECTION_TIMES_OUT ): with pytest.raises(UpperCamelCase__ ): requests.request("""GET""" , """https://huggingface.co""" ) with pytest.raises(requests.exceptions.ConnectTimeout ): requests.request("""GET""" , """https://huggingface.co""" , timeout=1.0 ) @pytest.mark.integration def lowerCAmelCase_ ( ): """simple docstring""" with offline(OfflineSimulationMode.CONNECTION_FAILS ): with pytest.raises(requests.exceptions.ConnectionError ): requests.request("""GET""" , """https://huggingface.co""" ) def lowerCAmelCase_ ( ): """simple docstring""" with offline(OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1 ): with pytest.raises(UpperCamelCase__ ): http_head("""https://huggingface.co""" )
616
1
'''simple docstring''' import heapq import sys import numpy as np snake_case = tuple[int, int] class lowerCAmelCase : def __init__( self : Dict ): '''simple docstring''' lowerCAmelCase__ : List[str] = [] lowerCAmelCase__ : List[str] = set() def _A ( self : List[str] ): '''simple docstring''' if not self.empty(): return self.elements[0][0] else: return float("inf" ) def _A ( self : List[Any] ): '''simple docstring''' return len(self.elements ) == 0 def _A ( self : List[str] , a__ : Optional[Any] , a__ : Optional[int] ): '''simple docstring''' if item not in self.set: heapq.heappush(self.elements , (priority, item) ) self.set.add(a__ ) else: # update # print("update", item) lowerCAmelCase__ : Optional[Any] = [] ((lowerCAmelCase__) , (lowerCAmelCase__)) : int = heapq.heappop(self.elements ) while x != item: temp.append((pri, x) ) ((lowerCAmelCase__) , (lowerCAmelCase__)) : Optional[Any] = heapq.heappop(self.elements ) temp.append((priority, item) ) for pro, xxx in temp: heapq.heappush(self.elements , (pro, xxx) ) def _A ( self : List[Any] , a__ : Tuple ): '''simple docstring''' if item in self.set: self.set.remove(a__ ) lowerCAmelCase__ : Union[str, Any] = [] ((lowerCAmelCase__) , (lowerCAmelCase__)) : Optional[Any] = heapq.heappop(self.elements ) while x != item: temp.append((pro, x) ) ((lowerCAmelCase__) , (lowerCAmelCase__)) : str = heapq.heappop(self.elements ) for prito, yyy in temp: heapq.heappush(self.elements , (prito, yyy) ) def _A ( self : List[str] ): '''simple docstring''' return self.elements[0][1] def _A ( self : Tuple ): '''simple docstring''' ((lowerCAmelCase__) , (lowerCAmelCase__)) : Dict = heapq.heappop(self.elements ) self.set.remove(a__ ) return (priority, item) def UpperCAmelCase_ ( lowerCamelCase_ , lowerCamelCase_ ): """simple docstring""" lowerCAmelCase__ : Any = np.array(lowerCamelCase_ ) lowerCAmelCase__ : Optional[int] = np.array(lowerCamelCase_ ) return np.linalg.norm(a - b ) def UpperCAmelCase_ ( lowerCamelCase_ , lowerCamelCase_ ): """simple docstring""" return consistent_heuristic(lowerCamelCase_ , lowerCamelCase_ ) // t def UpperCAmelCase_ ( lowerCamelCase_ , lowerCamelCase_ ): """simple docstring""" return abs(p[0] - goal[0] ) + abs(p[1] - goal[1] ) def UpperCAmelCase_ ( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ): """simple docstring""" lowerCAmelCase__ : int = g_function[start] + Wa * heuristics[i](lowerCamelCase_ , lowerCamelCase_ ) return ans def UpperCAmelCase_ ( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ): """simple docstring""" lowerCAmelCase__ : List[Any] = np.chararray((n, n) ) for i in range(lowerCamelCase_ ): for j in range(lowerCamelCase_ ): lowerCAmelCase__ : List[Any] = "*" for i in range(lowerCamelCase_ ): for j in range(lowerCamelCase_ ): if (j, (n - 1) - i) in blocks: lowerCAmelCase__ : int = "#" lowerCAmelCase__ : int = "-" lowerCAmelCase__ : Tuple = back_pointer[goal] while x != start: ((lowerCAmelCase__) , (lowerCAmelCase__)) : Dict = x # print(x) lowerCAmelCase__ : Optional[Any] = "-" lowerCAmelCase__ : List[Any] = back_pointer[x] lowerCAmelCase__ : Tuple = "-" for i in range(lowerCamelCase_ ): for j in range(lowerCamelCase_ ): if (i, j) == (0, n - 1): print(grid[i][j] , end=" " ) print("<-- End position" , end=" " ) else: print(grid[i][j] , end=" " ) print() print("^" ) print("Start position" ) print() print("# is an obstacle" ) print("- is the path taken by algorithm" ) print("PATH TAKEN BY THE ALGORITHM IS:-" ) lowerCAmelCase__ : Optional[Any] = back_pointer[goal] while x != start: print(lowerCamelCase_ , end=" " ) lowerCAmelCase__ : Union[str, Any] = back_pointer[x] print(lowerCamelCase_ ) sys.exit() def UpperCAmelCase_ ( lowerCamelCase_ ): """simple docstring""" if p[0] < 0 or p[0] > n - 1: return False if p[1] < 0 or p[1] > n - 1: return False return True def UpperCAmelCase_ ( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , ): """simple docstring""" for itera in range(lowerCamelCase_ ): open_list[itera].remove_element(lowerCamelCase_ ) # print("s", s) # print("j", j) ((lowerCAmelCase__) , (lowerCAmelCase__)) : Any = s lowerCAmelCase__ : List[Any] = (x - 1, y) lowerCAmelCase__ : Optional[Any] = (x + 1, y) lowerCAmelCase__ : int = (x, y + 1) lowerCAmelCase__ : Optional[Any] = (x, y - 1) for neighbours in [left, right, up, down]: if neighbours not in blocks: if valid(lowerCamelCase_ ) and neighbours not in visited: # print("neighbour", neighbours) visited.add(lowerCamelCase_ ) lowerCAmelCase__ : Optional[Any] = -1 lowerCAmelCase__ : Union[str, Any] = float("inf" ) if valid(lowerCamelCase_ ) and g_function[neighbours] > g_function[s] + 1: lowerCAmelCase__ : Dict = g_function[s] + 1 lowerCAmelCase__ : Any = s if neighbours not in close_list_anchor: open_list[0].put(lowerCamelCase_ , key(lowerCamelCase_ , 0 , lowerCamelCase_ , lowerCamelCase_ ) ) if neighbours not in close_list_inad: for var in range(1 , lowerCamelCase_ ): if key(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) <= Wa * key( lowerCamelCase_ , 0 , lowerCamelCase_ , lowerCamelCase_ ): open_list[j].put( lowerCamelCase_ , key(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) ) def UpperCAmelCase_ ( ): """simple docstring""" lowerCAmelCase__ : Optional[int] = [] for x in range(1 , 5 ): for y in range(1 , 6 ): some_list.append((x, y) ) for x in range(1_5 , 2_0 ): some_list.append((x, 1_7) ) for x in range(1_0 , 1_9 ): for y in range(1 , 1_5 ): some_list.append((x, y) ) # L block for x in range(1 , 4 ): for y in range(1_2 , 1_9 ): some_list.append((x, y) ) for x in range(3 , 1_3 ): for y in range(1_6 , 1_9 ): some_list.append((x, y) ) return some_list snake_case = {0: consistent_heuristic, 1: heuristic_a, 2: heuristic_a} snake_case = [ (0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1), (8, 1), (9, 1), (10, 1), (11, 1), (12, 1), (13, 1), (14, 1), (15, 1), (16, 1), (17, 1), (18, 1), (19, 1), ] snake_case = make_common_ground() snake_case = blocks_blk # hyper parameters snake_case = 1 snake_case = 1 snake_case = 20 snake_case = 3 # one consistent and two other inconsistent # start and end destination snake_case = (0, 0) snake_case = (n - 1, n - 1) snake_case = 1 def UpperCAmelCase_ ( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ): """simple docstring""" lowerCAmelCase__ : Optional[Any] = {start: 0, goal: float("inf" )} lowerCAmelCase__ : Optional[int] = {start: -1, goal: -1} lowerCAmelCase__ : Any = [] lowerCAmelCase__ : str = set() for i in range(lowerCamelCase_ ): open_list.append(PriorityQueue() ) open_list[i].put(lowerCamelCase_ , key(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) ) lowerCAmelCase__ : list[int] = [] lowerCAmelCase__ : list[int] = [] while open_list[0].minkey() < float("inf" ): for i in range(1 , lowerCamelCase_ ): # print(open_list[0].minkey(), open_list[i].minkey()) if open_list[i].minkey() <= Wa * open_list[0].minkey(): global t t += 1 if g_function[goal] <= open_list[i].minkey(): if g_function[goal] < float("inf" ): do_something(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) else: lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = open_list[i].top_show() visited.add(lowerCamelCase_ ) expand_state( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , ) close_list_inad.append(lowerCamelCase_ ) else: if g_function[goal] <= open_list[0].minkey(): if g_function[goal] < float("inf" ): do_something(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) else: lowerCAmelCase__ : List[str] = open_list[0].top_show() visited.add(lowerCamelCase_ ) expand_state( lowerCamelCase_ , 0 , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , ) close_list_anchor.append(lowerCamelCase_ ) print("No path found to goal" ) print() for i in range(n - 1 , -1 , -1 ): for j in range(lowerCamelCase_ ): if (j, i) in blocks: print("#" , end=" " ) elif (j, i) in back_pointer: if (j, i) == (n - 1, n - 1): print("*" , end=" " ) else: print("-" , end=" " ) else: print("*" , end=" " ) if (j, i) == (n - 1, n - 1): print("<-- End position" , end=" " ) print() print("^" ) print("Start position" ) print() print("# is an obstacle" ) print("- is the path taken by algorithm" ) if __name__ == "__main__": multi_a_star(start, goal, n_heuristic)
568
'''simple docstring''' import os import unittest from transformers import FunnelTokenizer, FunnelTokenizerFast from transformers.models.funnel.tokenization_funnel import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowerCAmelCase ( UpperCamelCase_ , unittest.TestCase ): A_ : Any = FunnelTokenizer A_ : Dict = FunnelTokenizerFast A_ : Dict = True A_ : Tuple = True def _A ( self : List[Any] ): '''simple docstring''' super().setUp() lowerCAmelCase__ : Dict = [ "<unk>", "<cls>", "<sep>", "want", "##want", "##ed", "wa", "un", "runn", "##ing", ",", "low", "lowest", ] lowerCAmelCase__ : Union[str, Any] = 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 : Union[str, Any] , **a__ : str ): '''simple docstring''' return FunnelTokenizer.from_pretrained(self.tmpdirname , **a__ ) def _A ( self : Optional[int] , **a__ : str ): '''simple docstring''' return FunnelTokenizerFast.from_pretrained(self.tmpdirname , **a__ ) def _A ( self : Tuple , a__ : List[str] ): '''simple docstring''' lowerCAmelCase__ : Dict = "UNwant\u00E9d,running" lowerCAmelCase__ : Union[str, Any] = "unwanted, running" return input_text, output_text def _A ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Any = self.tokenizer_class(self.vocab_file ) lowerCAmelCase__ : List[str] = tokenizer.tokenize("UNwant\u00E9d,running" ) self.assertListEqual(a__ , ["un", "##want", "##ed", ",", "runn", "##ing"] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(a__ ) , [7, 4, 5, 10, 8, 9] ) def _A ( self : List[str] ): '''simple docstring''' lowerCAmelCase__ : Dict = self.get_tokenizers(do_lower_case=a__ ) for tokenizer in tokenizers: lowerCAmelCase__ : Tuple = tokenizer("UNwant\u00E9d,running" ) lowerCAmelCase__ : str = len(inputs["input_ids"] ) - 1 self.assertListEqual(inputs["token_type_ids"] , [2] + [0] * sentence_len ) lowerCAmelCase__ : Optional[int] = tokenizer("UNwant\u00E9d,running" , "UNwant\u00E9d,running" ) self.assertListEqual(inputs["token_type_ids"] , [2] + [0] * sentence_len + [1] * sentence_len )
568
1
from __future__ import annotations from collections.abc import Callable __UpperCamelCase : List[str] = list[list[float | int]] def _UpperCAmelCase ( UpperCAmelCase : Matrix , UpperCAmelCase : Matrix ): """simple docstring""" __lowerCamelCase : int = len(UpperCAmelCase ) __lowerCamelCase : Matrix = [[0 for _ in range(size + 1 )] for _ in range(UpperCAmelCase )] __lowerCamelCase : int __lowerCamelCase : int __lowerCamelCase : int __lowerCamelCase : int __lowerCamelCase : int __lowerCamelCase : float for row in range(UpperCAmelCase ): for col in range(UpperCAmelCase ): __lowerCamelCase : int = matrix[row][col] __lowerCamelCase : Optional[Any] = vector[row][0] __lowerCamelCase : int = 0 __lowerCamelCase : Tuple = 0 while row < size and col < size: # pivoting __lowerCamelCase : Optional[Any] = max((abs(augmented[rowa][col] ), rowa) for rowa in range(UpperCAmelCase , UpperCAmelCase ) )[ 1 ] if augmented[pivot_row][col] == 0: col += 1 continue else: __lowerCamelCase , __lowerCamelCase : int = augmented[pivot_row], augmented[row] for rowa in range(row + 1 , UpperCAmelCase ): __lowerCamelCase : str = augmented[rowa][col] / augmented[row][col] __lowerCamelCase : int = 0 for cola in range(col + 1 , size + 1 ): augmented[rowa][cola] -= augmented[row][cola] * ratio row += 1 col += 1 # back substitution for col in range(1 , UpperCAmelCase ): for row in range(UpperCAmelCase ): __lowerCamelCase : Tuple = augmented[row][col] / augmented[col][col] for cola in range(UpperCAmelCase , size + 1 ): augmented[row][cola] -= augmented[col][cola] * ratio # round to get rid of numbers like 2.000000000000004 return [ [round(augmented[row][size] / augmented[row][row] , 10 )] for row in range(UpperCAmelCase ) ] def _UpperCAmelCase ( UpperCAmelCase : list[int] ): """simple docstring""" __lowerCamelCase : int = len(UpperCAmelCase ) __lowerCamelCase : Matrix = [[0 for _ in range(UpperCAmelCase )] for _ in range(UpperCAmelCase )] __lowerCamelCase : Matrix = [[0] for _ in range(UpperCAmelCase )] __lowerCamelCase : Matrix __lowerCamelCase : int __lowerCamelCase : int __lowerCamelCase : int for x_val, y_val in enumerate(UpperCAmelCase ): for col in range(UpperCAmelCase ): __lowerCamelCase : Optional[Any] = (x_val + 1) ** (size - col - 1) __lowerCamelCase : int = y_val __lowerCamelCase : List[Any] = solve(UpperCAmelCase , UpperCAmelCase ) def interpolated_func(UpperCAmelCase : int ) -> int: return sum( round(coeffs[x_val][0] ) * (var ** (size - x_val - 1)) for x_val in range(UpperCAmelCase ) ) return interpolated_func def _UpperCAmelCase ( UpperCAmelCase : int ): """simple docstring""" return ( 1 - variable + variable**2 - variable**3 + variable**4 - variable**5 + variable**6 - variable**7 + variable**8 - variable**9 + variable**10 ) def _UpperCAmelCase ( UpperCAmelCase : Callable[[int], int] = question_function , UpperCAmelCase : int = 10 ): """simple docstring""" __lowerCamelCase : list[int] = [func(UpperCAmelCase ) for x_val in range(1 , order + 1 )] __lowerCamelCase : list[Callable[[int], int]] = [ interpolate(data_points[:max_coeff] ) for max_coeff in range(1 , order + 1 ) ] __lowerCamelCase : int = 0 __lowerCamelCase : Callable[[int], int] __lowerCamelCase : int for poly in polynomials: __lowerCamelCase : Tuple = 1 while func(UpperCAmelCase ) == poly(UpperCAmelCase ): x_val += 1 ret += poly(UpperCAmelCase ) return ret if __name__ == "__main__": print(F'''{solution() = }''')
519
from __future__ import annotations def _UpperCAmelCase ( UpperCAmelCase : str , UpperCAmelCase : str ): """simple docstring""" __lowerCamelCase : int = get_failure_array(UpperCAmelCase ) # 2) Step through text searching for pattern __lowerCamelCase , __lowerCamelCase : Any = 0, 0 # index into text, pattern while i < len(UpperCAmelCase ): if pattern[j] == text[i]: if j == (len(UpperCAmelCase ) - 1): return True j += 1 # if this is a prefix in our pattern # just go back far enough to continue elif j > 0: __lowerCamelCase : Optional[Any] = failure[j - 1] continue i += 1 return False def _UpperCAmelCase ( UpperCAmelCase : str ): """simple docstring""" __lowerCamelCase : List[str] = [0] __lowerCamelCase : Dict = 0 __lowerCamelCase : List[Any] = 1 while j < len(UpperCAmelCase ): if pattern[i] == pattern[j]: i += 1 elif i > 0: __lowerCamelCase : Any = failure[i - 1] continue j += 1 failure.append(UpperCAmelCase ) return failure if __name__ == "__main__": # Test 1) __UpperCamelCase : List[str] = 'abc1abc12' __UpperCamelCase : Dict = 'alskfjaldsabc1abc1abc12k23adsfabcabc' __UpperCamelCase : Optional[int] = 'alskfjaldsk23adsfabcabc' assert kmp(pattern, texta) and not kmp(pattern, texta) # Test 2) __UpperCamelCase : Any = 'ABABX' __UpperCamelCase : List[str] = 'ABABZABABYABABX' assert kmp(pattern, text) # Test 3) __UpperCamelCase : List[Any] = 'AAAB' __UpperCamelCase : Any = 'ABAAAAAB' assert kmp(pattern, text) # Test 4) __UpperCamelCase : Union[str, Any] = 'abcdabcy' __UpperCamelCase : Tuple = 'abcxabcdabxabcdabcdabcy' assert kmp(pattern, text) # Test 5) __UpperCamelCase : Union[str, Any] = 'aabaabaaa' assert get_failure_array(pattern) == [0, 1, 0, 1, 2, 3, 4, 5, 2]
519
1
from __future__ import annotations from random import random from typing import Generic, TypeVar UpperCamelCase = TypeVar('KT') UpperCamelCase = TypeVar('VT') class _a ( Generic[KT, VT] ): '''simple docstring''' def __init__( self , __UpperCAmelCase = "root" , __UpperCAmelCase = None ): __A : Optional[Any] = key __A : str = value __A : list[Node[KT, VT]] = [] def __repr__( self ): return F"Node({self.key}: {self.value})" @property def __UpperCAmelCase( self ): return len(self.forward ) class _a ( Generic[KT, VT] ): '''simple docstring''' def __init__( self , __UpperCAmelCase = 0.5 , __UpperCAmelCase = 16 ): __A : Node[KT, VT] = Node[KT, VT]() __A : Union[str, Any] = 0 __A : int = p __A : int = max_level def __str__( self ): __A : List[Any] = list(self ) if len(__UpperCAmelCase ) == 0: return F"SkipList(level={self.level})" __A : Dict = max((len(str(__UpperCAmelCase ) ) for item in items) , default=4 ) __A : Tuple = max(__UpperCAmelCase , 4 ) + 4 __A : Optional[Any] = self.head __A : Optional[int] = [] __A : Dict = node.forward.copy() lines.append(F"[{node.key}]".ljust(__UpperCAmelCase , "-" ) + "* " * len(__UpperCAmelCase ) ) lines.append(" " * label_size + "| " * len(__UpperCAmelCase ) ) while len(node.forward ) != 0: __A : List[Any] = node.forward[0] lines.append( F"[{node.key}]".ljust(__UpperCAmelCase , "-" ) + " ".join(str(n.key ) if n.key == node.key else "|" for n in forwards ) ) lines.append(" " * label_size + "| " * len(__UpperCAmelCase ) ) __A : Union[str, Any] = node.forward lines.append("None".ljust(__UpperCAmelCase ) + "* " * len(__UpperCAmelCase ) ) return F"SkipList(level={self.level})\n" + "\n".join(__UpperCAmelCase ) def __iter__( self ): __A : Optional[Any] = self.head while len(node.forward ) != 0: yield node.forward[0].key __A : int = node.forward[0] def __UpperCAmelCase( self ): __A : List[Any] = 1 while random() < self.p and level < self.max_level: level += 1 return level def __UpperCAmelCase( self , __UpperCAmelCase ): __A : Union[str, Any] = [] __A : Optional[int] = self.head for i in reversed(range(self.level ) ): # i < node.level - When node level is lesser than `i` decrement `i`. # node.forward[i].key < key - Jumping to node with key value higher # or equal to searched key would result # in skipping searched key. while i < node.level and node.forward[i].key < key: __A : int = node.forward[i] # Each leftmost node (relative to searched node) will potentially have to # be updated. update_vector.append(__UpperCAmelCase ) update_vector.reverse() # Note that we were inserting values in reverse order. # len(node.forward) != 0 - If current node doesn't contain any further # references then searched key is not present. # node.forward[0].key == key - Next node key should be equal to search key # if key is present. if len(node.forward ) != 0 and node.forward[0].key == key: return node.forward[0], update_vector else: return None, update_vector def __UpperCAmelCase( self , __UpperCAmelCase ): __A : List[Any] = self._locate_node(__UpperCAmelCase ) if node is not None: for i, update_node in enumerate(__UpperCAmelCase ): # Remove or replace all references to removed node. if update_node.level > i and update_node.forward[i].key == key: if node.level > i: __A : Optional[int] = node.forward[i] else: __A : Tuple = update_node.forward[:i] def __UpperCAmelCase( self , __UpperCAmelCase , __UpperCAmelCase ): __A : Any = self._locate_node(__UpperCAmelCase ) if node is not None: __A : str = value else: __A : Any = self.random_level() if level > self.level: # After level increase we have to add additional nodes to head. for _ in range(self.level - 1 , __UpperCAmelCase ): update_vector.append(self.head ) __A : List[Any] = level __A : Any = Node(__UpperCAmelCase , __UpperCAmelCase ) for i, update_node in enumerate(update_vector[:level] ): # Change references to pass through new node. if update_node.level > i: new_node.forward.append(update_node.forward[i] ) if update_node.level < i + 1: update_node.forward.append(__UpperCAmelCase ) else: __A : Any = new_node def __UpperCAmelCase( self , __UpperCAmelCase ): __A : Union[str, Any] = self._locate_node(__UpperCAmelCase ) if node is not None: return node.value return None def lowerCamelCase_ ( ) -> Optional[int]: __A : Optional[int] = SkipList() skip_list.insert("Key1" , 3 ) skip_list.insert("Key2" , 12 ) skip_list.insert("Key3" , 41 ) skip_list.insert("Key4" , -19 ) __A : Tuple = skip_list.head __A : Optional[Any] = {} while node.level != 0: __A : int = node.forward[0] __A : List[Any] = node.value assert len(_lowercase ) == 4 assert all_values["Key1"] == 3 assert all_values["Key2"] == 12 assert all_values["Key3"] == 41 assert all_values["Key4"] == -19 def lowerCamelCase_ ( ) -> Union[str, Any]: __A : Optional[Any] = SkipList() skip_list.insert("Key1" , 10 ) skip_list.insert("Key1" , 12 ) skip_list.insert("Key5" , 7 ) skip_list.insert("Key7" , 10 ) skip_list.insert("Key10" , 5 ) skip_list.insert("Key7" , 7 ) skip_list.insert("Key5" , 5 ) skip_list.insert("Key10" , 10 ) __A : Union[str, Any] = skip_list.head __A : int = {} while node.level != 0: __A : Union[str, Any] = node.forward[0] __A : int = node.value if len(_lowercase ) != 4: print() assert len(_lowercase ) == 4 assert all_values["Key1"] == 12 assert all_values["Key7"] == 7 assert all_values["Key5"] == 5 assert all_values["Key10"] == 10 def lowerCamelCase_ ( ) -> List[str]: __A : List[Any] = SkipList() assert skip_list.find("Some key" ) is None def lowerCamelCase_ ( ) -> Optional[int]: __A : int = SkipList() skip_list.insert("Key2" , 20 ) assert skip_list.find("Key2" ) == 20 skip_list.insert("Some Key" , 10 ) skip_list.insert("Key2" , 8 ) skip_list.insert("V" , 13 ) assert skip_list.find("Y" ) is None assert skip_list.find("Key2" ) == 8 assert skip_list.find("Some Key" ) == 10 assert skip_list.find("V" ) == 13 def lowerCamelCase_ ( ) -> int: __A : int = SkipList() skip_list.delete("Some key" ) assert len(skip_list.head.forward ) == 0 def lowerCamelCase_ ( ) -> Optional[Any]: __A : int = SkipList() skip_list.insert("Key1" , 12 ) skip_list.insert("V" , 13 ) skip_list.insert("X" , 14 ) skip_list.insert("Key2" , 15 ) skip_list.delete("V" ) skip_list.delete("Key2" ) assert skip_list.find("V" ) is None assert skip_list.find("Key2" ) is None def lowerCamelCase_ ( ) -> List[str]: __A : List[Any] = SkipList() skip_list.insert("Key1" , 12 ) skip_list.insert("V" , 13 ) skip_list.insert("X" , 14 ) skip_list.insert("Key2" , 15 ) skip_list.delete("V" ) assert skip_list.find("V" ) is None assert skip_list.find("X" ) == 14 assert skip_list.find("Key1" ) == 12 assert skip_list.find("Key2" ) == 15 skip_list.delete("X" ) assert skip_list.find("V" ) is None assert skip_list.find("X" ) is None assert skip_list.find("Key1" ) == 12 assert skip_list.find("Key2" ) == 15 skip_list.delete("Key1" ) assert skip_list.find("V" ) is None assert skip_list.find("X" ) is None assert skip_list.find("Key1" ) is None assert skip_list.find("Key2" ) == 15 skip_list.delete("Key2" ) assert skip_list.find("V" ) is None assert skip_list.find("X" ) is None assert skip_list.find("Key1" ) is None assert skip_list.find("Key2" ) is None def lowerCamelCase_ ( ) -> Tuple: __A : List[Any] = SkipList() skip_list.insert("Key1" , 12 ) skip_list.insert("V" , 13 ) skip_list.insert("X" , 142 ) skip_list.insert("Key2" , 15 ) skip_list.delete("X" ) def traverse_keys(_lowercase ): yield node.key for forward_node in node.forward: yield from traverse_keys(_lowercase ) assert len(set(traverse_keys(skip_list.head ) ) ) == 4 def lowerCamelCase_ ( ) -> Union[str, Any]: def is_sorted(_lowercase ): return all(next_item >= item for item, next_item in zip(_lowercase , lst[1:] ) ) __A : Union[str, Any] = SkipList() for i in range(10 ): skip_list.insert(_lowercase , _lowercase ) assert is_sorted(list(_lowercase ) ) skip_list.delete(5 ) skip_list.delete(8 ) skip_list.delete(2 ) assert is_sorted(list(_lowercase ) ) skip_list.insert(-12 , -12 ) skip_list.insert(77 , 77 ) assert is_sorted(list(_lowercase ) ) def lowerCamelCase_ ( ) -> List[Any]: for _ in range(100 ): # Repeat test 100 times due to the probabilistic nature of skip list # random values == random bugs test_insert() test_insert_overrides_existing_value() test_searching_empty_list_returns_none() test_search() test_deleting_item_from_empty_list_do_nothing() test_deleted_items_are_not_founded_by_find_method() test_delete_removes_only_given_key() test_delete_doesnt_leave_dead_nodes() test_iter_always_yields_sorted_values() def lowerCamelCase_ ( ) -> int: __A : Optional[Any] = SkipList() skip_list.insert(2 , "2" ) skip_list.insert(4 , "4" ) skip_list.insert(6 , "4" ) skip_list.insert(4 , "5" ) skip_list.insert(8 , "4" ) skip_list.insert(9 , "4" ) skip_list.delete(4 ) print(_lowercase ) if __name__ == "__main__": import doctest doctest.testmod() main()
706
import os import string import sys UpperCamelCase = 1 << 8 UpperCamelCase = { 'tab': ord('\t'), 'newline': ord('\r'), 'esc': 27, 'up': 65 + ARROW_KEY_FLAG, 'down': 66 + ARROW_KEY_FLAG, 'right': 67 + ARROW_KEY_FLAG, 'left': 68 + ARROW_KEY_FLAG, 'mod_int': 91, 'undefined': sys.maxsize, 'interrupt': 3, 'insert': 50, 'delete': 51, 'pg_up': 53, 'pg_down': 54, } UpperCamelCase = KEYMAP['up'] UpperCamelCase = KEYMAP['left'] if sys.platform == "win32": UpperCamelCase = [] UpperCamelCase = { b'\xe0H': KEYMAP['up'] - ARROW_KEY_FLAG, b'\x00H': KEYMAP['up'] - ARROW_KEY_FLAG, b'\xe0P': KEYMAP['down'] - ARROW_KEY_FLAG, b'\x00P': KEYMAP['down'] - ARROW_KEY_FLAG, b'\xe0M': KEYMAP['right'] - ARROW_KEY_FLAG, b'\x00M': KEYMAP['right'] - ARROW_KEY_FLAG, b'\xe0K': KEYMAP['left'] - ARROW_KEY_FLAG, b'\x00K': KEYMAP['left'] - ARROW_KEY_FLAG, } for i in range(10): UpperCamelCase = ord(str(i)) def lowerCamelCase_ ( ) -> Tuple: if os.name == "nt": import msvcrt __A : Optional[int] = "mbcs" # Flush the keyboard buffer while msvcrt.kbhit(): msvcrt.getch() if len(_lowercase ) == 0: # Read the keystroke __A : Union[str, Any] = msvcrt.getch() # If it is a prefix char, get second part if ch in (b"\x00", b"\xe0"): __A : Tuple = ch + msvcrt.getch() # Translate actual Win chars to bullet char types try: __A : int = chr(WIN_KEYMAP[cha] ) WIN_CH_BUFFER.append(chr(KEYMAP["mod_int"] ) ) WIN_CH_BUFFER.append(_lowercase ) if ord(_lowercase ) in ( KEYMAP["insert"] - 1 << 9, KEYMAP["delete"] - 1 << 9, KEYMAP["pg_up"] - 1 << 9, KEYMAP["pg_down"] - 1 << 9, ): WIN_CH_BUFFER.append(chr(126 ) ) __A : Tuple = chr(KEYMAP["esc"] ) except KeyError: __A : Union[str, Any] = cha[1] else: __A : Optional[int] = ch.decode(_lowercase ) else: __A : Dict = WIN_CH_BUFFER.pop(0 ) elif os.name == "posix": import termios import tty __A : str = sys.stdin.fileno() __A : Tuple = termios.tcgetattr(_lowercase ) try: tty.setraw(_lowercase ) __A : int = sys.stdin.read(1 ) finally: termios.tcsetattr(_lowercase , termios.TCSADRAIN , _lowercase ) return ch def lowerCamelCase_ ( ) -> Union[str, Any]: __A : Any = get_raw_chars() if ord(_lowercase ) in [KEYMAP["interrupt"], KEYMAP["newline"]]: return char elif ord(_lowercase ) == KEYMAP["esc"]: __A : Tuple = get_raw_chars() if ord(_lowercase ) == KEYMAP["mod_int"]: __A : Optional[int] = get_raw_chars() if ord(_lowercase ) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(_lowercase ) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG: return chr(ord(_lowercase ) + ARROW_KEY_FLAG ) else: return KEYMAP["undefined"] else: return get_raw_chars() else: if char in string.printable: return char else: return KEYMAP["undefined"]
387
0
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) lowerCamelCase__ : List[str] = { 'tanreinama/GPTSAN-2.8B-spout_is_uniform': ( 'https://huggingface.co/tanreinama/GPTSAN-2.8B-spout_is_uniform/resolve/main/config.json' ), } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "gptsan-japanese" lowercase_ = [ "past_key_values", ] lowercase_ = { "hidden_size": "d_model", "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self : List[str] , _lowerCAmelCase : Any=36_000 , _lowerCAmelCase : Any=1_280 , _lowerCAmelCase : Any=1_024 , _lowerCAmelCase : Union[str, Any]=8_192 , _lowerCAmelCase : int=4_096 , _lowerCAmelCase : int=128 , _lowerCAmelCase : int=10 , _lowerCAmelCase : Tuple=0 , _lowerCAmelCase : Union[str, Any]=16 , _lowerCAmelCase : Union[str, Any]=16 , _lowerCAmelCase : Any=128 , _lowerCAmelCase : Dict=0.0 , _lowerCAmelCase : List[str]=1E-5 , _lowerCAmelCase : Union[str, Any]=False , _lowerCAmelCase : Optional[Any]=0.0 , _lowerCAmelCase : Any="float32" , _lowerCAmelCase : Union[str, Any]=False , _lowerCAmelCase : List[Any]=False , _lowerCAmelCase : int=False , _lowerCAmelCase : int=0.002 , _lowerCAmelCase : Tuple=False , _lowerCAmelCase : List[Any]=True , _lowerCAmelCase : List[str]=35_998 , _lowerCAmelCase : int=35_995 , _lowerCAmelCase : str=35_999 , **_lowerCAmelCase : str , ): SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = max_position_embeddings SCREAMING_SNAKE_CASE_ = d_model SCREAMING_SNAKE_CASE_ = d_ff SCREAMING_SNAKE_CASE_ = d_ext SCREAMING_SNAKE_CASE_ = d_spout SCREAMING_SNAKE_CASE_ = num_switch_layers SCREAMING_SNAKE_CASE_ = num_ext_layers SCREAMING_SNAKE_CASE_ = num_switch_layers + num_ext_layers SCREAMING_SNAKE_CASE_ = num_heads SCREAMING_SNAKE_CASE_ = num_experts SCREAMING_SNAKE_CASE_ = expert_capacity SCREAMING_SNAKE_CASE_ = dropout_rate SCREAMING_SNAKE_CASE_ = layer_norm_epsilon SCREAMING_SNAKE_CASE_ = router_bias SCREAMING_SNAKE_CASE_ = router_jitter_noise SCREAMING_SNAKE_CASE_ = router_dtype SCREAMING_SNAKE_CASE_ = router_ignore_padding_tokens SCREAMING_SNAKE_CASE_ = output_hidden_states SCREAMING_SNAKE_CASE_ = output_attentions SCREAMING_SNAKE_CASE_ = initializer_factor SCREAMING_SNAKE_CASE_ = output_router_logits SCREAMING_SNAKE_CASE_ = use_cache super().__init__( separator_token_id=_lowerCAmelCase , pad_token_id=_lowerCAmelCase , eos_token_id=_lowerCAmelCase , **_lowerCAmelCase , )
31
UpperCAmelCase__ = '''Input must be a string of 8 numbers plus letter''' UpperCAmelCase__ = '''TRWAGMYFPDXBNJZSQVHLCKE''' def a_ (__A ) -> bool: """simple docstring""" if not isinstance(__A , __A ): __a : Any = f'Expected string as input, found {type(__A ).__name__}' raise TypeError(__A ) __a : int = spanish_id.replace("-" , "" ).upper() if len(__A ) != 9: raise ValueError(__A ) try: __a : Tuple = int(spanish_id_clean[0:8] ) __a : Optional[int] = spanish_id_clean[8] except ValueError as ex: raise ValueError(__A ) from ex if letter.isdigit(): raise ValueError(__A ) return letter == LOOKUP_LETTERS[number % 23] if __name__ == "__main__": import doctest doctest.testmod()
351
0
import math def A_ ( a , a ): """simple docstring""" if 0 not in (x, y): # We use the relation x^y = y*log10(x), where 10 is the base. return y * math.logaa(a ) else: if x == 0: # 0 raised to any number is 0 return 0 elif y == 0: return 1 # any number raised to 0 is 1 raise AssertionError('This should never happen' ) if __name__ == "__main__": # Main function # Read two numbers from input and typecast them to int using map function. # Here x is the base and y is the power. lowerCAmelCase : int = 'Enter the base and the power separated by a comma: ' lowerCAmelCase , lowerCAmelCase : Tuple = map(int, input(prompt).split(',')) lowerCAmelCase , lowerCAmelCase : int = map(int, input(prompt).split(',')) # We find the log of each number, using the function res(), which takes two # arguments. lowerCAmelCase : List[Any] = res(xa, ya) lowerCAmelCase : List[Any] = res(xa, ya) # We check for the largest number if resa > resa: print('Largest number is', xa, '^', ya) elif resa > resa: print('Largest number is', xa, '^', ya) else: print('Both are equal')
353
import argparse from transformers import TaConfig, TaForConditionalGeneration, load_tf_weights_in_ta from transformers.utils import logging logging.set_verbosity_info() def A_ ( a , a , a ): """simple docstring""" SCREAMING_SNAKE_CASE_ : int = TaConfig.from_json_file(a ) print(f"Building PyTorch model from configuration: {config}" ) SCREAMING_SNAKE_CASE_ : Tuple = TaForConditionalGeneration(a ) # Load weights from tf checkpoint load_tf_weights_in_ta(a , a , a ) # Save pytorch-model print(f"Save PyTorch model to {pytorch_dump_path}" ) model.save_pretrained(a ) if __name__ == "__main__": lowerCAmelCase : int = argparse.ArgumentParser() # Required parameters parser.add_argument( '--tf_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.' ) parser.add_argument( '--config_file', default=None, type=str, required=True, help=( 'The config json file corresponding to the pre-trained T5 model. \nThis specifies the model architecture.' ), ) parser.add_argument( '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) lowerCAmelCase : List[str] = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path)
353
1
import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DPTImageProcessor class lowerCamelCase( unittest.TestCase ): '''simple docstring''' def __init__( self , snake_case_ , snake_case_=7 , snake_case_=3 , snake_case_=18 , snake_case_=30 , snake_case_=400 , snake_case_=True , snake_case_=None , snake_case_=True , snake_case_=[0.5, 0.5, 0.5] , snake_case_=[0.5, 0.5, 0.5] , ): _A = size if size is not None else {'height': 18, 'width': 18} _A = parent _A = batch_size _A = num_channels _A = image_size _A = min_resolution _A = max_resolution _A = do_resize _A = size _A = do_normalize _A = image_mean _A = image_std def lowerCAmelCase__ ( self ): return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class lowerCamelCase( __snake_case , unittest.TestCase ): '''simple docstring''' __magic_name__ = DPTImageProcessor if is_vision_available() else None def lowerCAmelCase__ ( self ): _A = DPTImageProcessingTester(self ) @property def lowerCAmelCase__ ( self ): return self.image_processor_tester.prepare_image_processor_dict() def lowerCAmelCase__ ( self ): _A = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(snake_case_ , 'image_mean' ) ) self.assertTrue(hasattr(snake_case_ , 'image_std' ) ) self.assertTrue(hasattr(snake_case_ , 'do_normalize' ) ) self.assertTrue(hasattr(snake_case_ , 'do_resize' ) ) self.assertTrue(hasattr(snake_case_ , 'size' ) ) def lowerCAmelCase__ ( self ): _A = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'height': 18, 'width': 18} ) _A = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'height': 42, 'width': 42} ) def lowerCAmelCase__ ( self ): # Initialize image_processing _A = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _A = prepare_image_inputs(self.image_processor_tester , equal_resolution=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'], ) , ) def lowerCAmelCase__ ( self ): # Initialize image_processing _A = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _A = prepare_image_inputs(self.image_processor_tester , equal_resolution=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'], ) , ) def lowerCAmelCase__ ( self ): # Initialize image_processing _A = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _A = prepare_image_inputs(self.image_processor_tester , equal_resolution=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'], ) , )
27
'''simple docstring''' import warnings from ...utils import logging from .image_processing_deit import DeiTImageProcessor lowercase_ = logging.get_logger(__name__) class a_ ( snake_case_ ): '''simple docstring''' def __init__( self , *A , **A ) -> None: warnings.warn( """The class DeiTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please""" """ use DeiTImageProcessor instead.""" , A , ) super().__init__(*A , **A )
314
0
import json import os import tempfile import datasets from utils import generate_example_dataset, get_duration _UpperCamelCase : List[Any] =50000 _UpperCamelCase : Dict =5000 _UpperCamelCase : List[Any] =os.path.split(__file__) _UpperCamelCase : Union[str, Any] =os.path.join(RESULTS_BASEPATH, 'results', RESULTS_FILENAME.replace('.py', '.json')) @get_duration def a__ (__lowercase :datasets.Dataset , __lowercase :int ) -> Tuple: for i in range(__lowercase ): _A : Union[str, Any] = dataset[i] @get_duration def a__ (__lowercase :datasets.Dataset , __lowercase :List[str] , __lowercase :List[str] ) -> List[str]: for i in range(0 , len(__lowercase ) , __lowercase ): _A : Optional[Any] = dataset[i : i + batch_size] @get_duration def a__ (__lowercase :datasets.Dataset , __lowercase :Dict , __lowercase :Union[str, Any] ) -> str: with dataset.formatted_as(type=__lowercase ): for i in range(__lowercase ): _A : str = dataset[i] @get_duration def a__ (__lowercase :datasets.Dataset , __lowercase :Optional[Any] , __lowercase :List[Any] , __lowercase :str ) -> Tuple: with dataset.formatted_as(type=__lowercase ): for i in range(0 , __lowercase , __lowercase ): _A : Union[str, Any] = dataset[i : i + batch_size] def a__ () -> Dict: _A : Optional[Any] = {'''num examples''': SPEED_TEST_N_EXAMPLES} _A : Tuple = [ (read, {'''length''': SMALL_TEST}), (read, {'''length''': SPEED_TEST_N_EXAMPLES}), (read_batch, {'''length''': SPEED_TEST_N_EXAMPLES, '''batch_size''': 10}), (read_batch, {'''length''': SPEED_TEST_N_EXAMPLES, '''batch_size''': 100}), (read_batch, {'''length''': SPEED_TEST_N_EXAMPLES, '''batch_size''': 1000}), (read_formatted, {'''type''': '''numpy''', '''length''': SMALL_TEST}), (read_formatted, {'''type''': '''pandas''', '''length''': SMALL_TEST}), (read_formatted, {'''type''': '''torch''', '''length''': SMALL_TEST}), (read_formatted, {'''type''': '''tensorflow''', '''length''': SMALL_TEST}), (read_formatted_batch, {'''type''': '''numpy''', '''length''': SMALL_TEST, '''batch_size''': 10}), (read_formatted_batch, {'''type''': '''numpy''', '''length''': SMALL_TEST, '''batch_size''': 1000}), ] _A : Optional[Any] = [ (read, {'''length''': SMALL_TEST}), (read, {'''length''': SPEED_TEST_N_EXAMPLES}), (read_batch, {'''length''': SPEED_TEST_N_EXAMPLES, '''batch_size''': 10}), (read_batch, {'''length''': SPEED_TEST_N_EXAMPLES, '''batch_size''': 100}), (read_batch, {'''length''': SPEED_TEST_N_EXAMPLES, '''batch_size''': 1000}), (read_formatted, {'''type''': '''numpy''', '''length''': SMALL_TEST}), (read_formatted_batch, {'''type''': '''numpy''', '''length''': SMALL_TEST, '''batch_size''': 10}), (read_formatted_batch, {'''type''': '''numpy''', '''length''': SMALL_TEST, '''batch_size''': 1000}), ] with tempfile.TemporaryDirectory() as tmp_dir: print('''generating dataset''' ) _A : int = datasets.Features( {'''list''': datasets.Sequence(datasets.Value('''float32''' ) ), '''numbers''': datasets.Value('''float32''' )} ) _A : Any = generate_example_dataset( os.path.join(__lowercase , '''dataset.arrow''' ) , __lowercase , num_examples=__lowercase , seq_shapes={'''list''': (100,)} , ) print('''first set of iterations''' ) for func, kwargs in functions: print(func.__name__ , str(__lowercase ) ) _A : Tuple = func(__lowercase , **__lowercase ) print('''shuffling dataset''' ) _A : Any = dataset.shuffle() print('''Second set of iterations (after shuffling''' ) for func, kwargs in functions_shuffled: print('''shuffled ''' , func.__name__ , str(__lowercase ) ) _A : Optional[Any] = func( __lowercase , **__lowercase ) with open(__lowercase , '''wb''' ) as f: f.write(json.dumps(__lowercase ).encode('''utf-8''' ) ) if __name__ == "__main__": # useful to run the profiler benchmark_iterating()
702
from ...configuration_utils import PretrainedConfig _UpperCamelCase : Tuple ={ 'google/tapas-base-finetuned-sqa': ( 'https://huggingface.co/google/tapas-base-finetuned-sqa/resolve/main/config.json' ), 'google/tapas-base-finetuned-wtq': ( 'https://huggingface.co/google/tapas-base-finetuned-wtq/resolve/main/config.json' ), 'google/tapas-base-finetuned-wikisql-supervised': ( 'https://huggingface.co/google/tapas-base-finetuned-wikisql-supervised/resolve/main/config.json' ), 'google/tapas-base-finetuned-tabfact': ( 'https://huggingface.co/google/tapas-base-finetuned-tabfact/resolve/main/config.json' ), } class UpperCAmelCase__ ( __snake_case ): __snake_case : List[Any] = "tapas" def __init__( self ,A__=30522 ,A__=768 ,A__=12 ,A__=12 ,A__=3072 ,A__="gelu" ,A__=0.1 ,A__=0.1 ,A__=1024 ,A__=[3, 256, 256, 2, 256, 256, 10] ,A__=0.02 ,A__=1E-12 ,A__=0 ,A__=10.0 ,A__=0 ,A__=1.0 ,A__=None ,A__=1.0 ,A__=False ,A__=None ,A__=1.0 ,A__=1.0 ,A__=False ,A__=False ,A__="ratio" ,A__=None ,A__=None ,A__=64 ,A__=32 ,A__=False ,A__=True ,A__=False ,A__=False ,A__=True ,A__=False ,A__=None ,A__=None ,**A__ ,): super().__init__(pad_token_id=A__ ,**A__ ) # BERT hyperparameters (with updated max_position_embeddings and type_vocab_sizes) _A : int = vocab_size _A : Dict = hidden_size _A : List[str] = num_hidden_layers _A : Tuple = num_attention_heads _A : Any = hidden_act _A : str = intermediate_size _A : List[str] = hidden_dropout_prob _A : List[Any] = attention_probs_dropout_prob _A : Tuple = max_position_embeddings _A : Optional[Any] = type_vocab_sizes _A : List[Any] = initializer_range _A : Optional[Any] = layer_norm_eps # Fine-tuning task hyperparameters _A : Optional[Any] = positive_label_weight _A : List[Any] = num_aggregation_labels _A : Union[str, Any] = aggregation_loss_weight _A : Optional[int] = use_answer_as_supervision _A : Tuple = answer_loss_importance _A : Dict = use_normalized_answer_loss _A : Union[str, Any] = huber_loss_delta _A : Union[str, Any] = temperature _A : Optional[Any] = aggregation_temperature _A : Optional[int] = use_gumbel_for_cells _A : List[str] = use_gumbel_for_aggregation _A : int = average_approximation_function _A : Union[str, Any] = cell_selection_preference _A : List[str] = answer_loss_cutoff _A : List[Any] = max_num_rows _A : List[str] = max_num_columns _A : Union[str, Any] = average_logits_per_cell _A : Optional[Any] = select_one_column _A : List[str] = allow_empty_column_selection _A : int = init_cell_selection_weights_to_zero _A : Dict = reset_position_index_per_cell _A : Dict = disable_per_token_loss # Aggregation hyperparameters _A : Tuple = aggregation_labels _A : Dict = no_aggregation_label_index if isinstance(self.aggregation_labels ,A__ ): _A : Dict = {int(A__ ): v for k, v in aggregation_labels.items()}
332
0
"""simple docstring""" import logging import os import sys import warnings from dataclasses import dataclass, field from random import randint from typing import Optional import datasets import evaluate import numpy as np from datasets import DatasetDict, load_dataset import transformers from transformers import ( AutoConfig, AutoFeatureExtractor, AutoModelForAudioClassification, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version __UpperCAmelCase = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version('4.31.0') require_version('datasets>=1.14.0', 'To fix: pip install -r examples/pytorch/audio-classification/requirements.txt') def lowerCAmelCase ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = 16000 ): '''simple docstring''' UpperCAmelCase__ : int = int(round(sample_rate * max_length ) ) if len(__UpperCamelCase ) <= sample_length: return wav UpperCAmelCase__ : List[str] = randint(0 , len(__UpperCamelCase ) - sample_length - 1 ) return wav[random_offset : random_offset + sample_length] @dataclass class __lowercase : snake_case_ = field(default=__lowerCamelCase , metadata={"""help""": """Name of a dataset from the datasets package"""} ) snake_case_ = field( default=__lowerCamelCase , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} ) snake_case_ = field( default=__lowerCamelCase , metadata={"""help""": """A file containing the training audio paths and labels."""} ) snake_case_ = field( default=__lowerCamelCase , metadata={"""help""": """A file containing the validation audio paths and labels."""} ) snake_case_ = field( default="""train""" , metadata={ """help""": """The name of the training data set split to use (via the datasets library). Defaults to 'train'""" } , ) snake_case_ = field( default="""validation""" , metadata={ """help""": ( """The name of the training data set split to use (via the datasets library). Defaults to 'validation'""" ) } , ) snake_case_ = field( default="""audio""" , metadata={"""help""": """The name of the dataset column containing the audio data. Defaults to 'audio'"""} , ) snake_case_ = field( default="""label""" , metadata={"""help""": """The name of the dataset column containing the labels. Defaults to 'label'"""} ) snake_case_ = field( default=__lowerCamelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } , ) snake_case_ = field( default=__lowerCamelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of evaluation examples to this """ """value if set.""" ) } , ) snake_case_ = field( default=2_0 , metadata={"""help""": """Audio clips will be randomly cut to this length during training if the value is set."""} , ) @dataclass class __lowercase : snake_case_ = field( default="""facebook/wav2vec2-base""" , metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} , ) snake_case_ = field( default=__lowerCamelCase , metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) snake_case_ = field( default=__lowerCamelCase , metadata={"""help""": """Where do you want to store the pretrained models downloaded from the Hub"""} ) snake_case_ = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) snake_case_ = field( default=__lowerCamelCase , metadata={"""help""": """Name or path of preprocessor config."""} ) snake_case_ = field( default=__lowerCamelCase , metadata={"""help""": """Whether to freeze the feature encoder layers of the model."""} ) snake_case_ = field( default=__lowerCamelCase , metadata={"""help""": """Whether to generate an attention mask in the feature extractor."""} ) snake_case_ = field( default=__lowerCamelCase , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) snake_case_ = field( default=__lowerCamelCase , metadata={"""help""": """Whether to freeze the feature extractor layers of the model."""} ) snake_case_ = field( default=__lowerCamelCase , metadata={"""help""": """Will enable to load a pretrained model whose head dimensions are different."""} , ) def __lowercase ( self : Dict ): '''simple docstring''' if not self.freeze_feature_extractor and self.freeze_feature_encoder: warnings.warn( """The argument `--freeze_feature_extractor` is deprecated and """ """will be removed in a future version. Use `--freeze_feature_encoder`""" """instead. Setting `freeze_feature_encoder==True`.""" ,A ,) if self.freeze_feature_extractor and not self.freeze_feature_encoder: raise ValueError( """The argument `--freeze_feature_extractor` is deprecated and """ """should not be used in combination with `--freeze_feature_encoder`.""" """Only make use of `--freeze_feature_encoder`.""" ) def lowerCAmelCase ( ): '''simple docstring''' UpperCAmelCase__ : Any = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(""".json""" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ : List[str] = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ : Dict = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry("""run_audio_classification""" , __UpperCamelCase , __UpperCamelCase ) # Setup logging logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() UpperCAmelCase__ : Any = training_args.get_process_log_level() logger.setLevel(__UpperCamelCase ) transformers.utils.logging.set_verbosity(__UpperCamelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu} " + F"distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}" ) logger.info(F"Training/evaluation parameters {training_args}" ) # Set seed before initializing model. set_seed(training_args.seed ) # Detecting last checkpoint. UpperCAmelCase__ : Optional[Any] = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: UpperCAmelCase__ : Optional[Any] = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F"Output directory ({training_args.output_dir}) already exists and is not empty. " """Use --overwrite_output_dir to train from scratch.""" ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change " """the `--output_dir` or add `--overwrite_output_dir` to train from scratch.""" ) # Initialize our dataset and prepare it for the audio classification task. UpperCAmelCase__ : Union[str, Any] = DatasetDict() UpperCAmelCase__ : Tuple = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=data_args.train_split_name , use_auth_token=True if model_args.use_auth_token else None , ) UpperCAmelCase__ : Optional[int] = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=data_args.eval_split_name , use_auth_token=True if model_args.use_auth_token else None , ) if data_args.audio_column_name not in raw_datasets["train"].column_names: raise ValueError( F"--audio_column_name {data_args.audio_column_name} not found in dataset '{data_args.dataset_name}'. " """Make sure to set `--audio_column_name` to the correct audio column - one of """ F"{', '.join(raw_datasets['train'].column_names )}." ) if data_args.label_column_name not in raw_datasets["train"].column_names: raise ValueError( F"--label_column_name {data_args.label_column_name} not found in dataset '{data_args.dataset_name}'. " """Make sure to set `--label_column_name` to the correct text column - one of """ F"{', '.join(raw_datasets['train'].column_names )}." ) # Setting `return_attention_mask=True` is the way to get a correctly masked mean-pooling over # transformer outputs in the classifier, but it doesn't always lead to better accuracy UpperCAmelCase__ : List[Any] = AutoFeatureExtractor.from_pretrained( model_args.feature_extractor_name or model_args.model_name_or_path , return_attention_mask=model_args.attention_mask , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # `datasets` takes care of automatically loading and resampling the audio, # so we just need to set the correct target sampling rate. UpperCAmelCase__ : List[Any] = raw_datasets.cast_column( data_args.audio_column_name , datasets.features.Audio(sampling_rate=feature_extractor.sampling_rate ) ) UpperCAmelCase__ : Dict = feature_extractor.model_input_names[0] def train_transforms(__UpperCamelCase ): UpperCAmelCase__ : List[str] = [] for audio in batch[data_args.audio_column_name]: UpperCAmelCase__ : Dict = random_subsample( audio["""array"""] , max_length=data_args.max_length_seconds , sample_rate=feature_extractor.sampling_rate ) subsampled_wavs.append(__UpperCamelCase ) UpperCAmelCase__ : List[str] = feature_extractor(__UpperCamelCase , sampling_rate=feature_extractor.sampling_rate ) UpperCAmelCase__ : List[str] = {model_input_name: inputs.get(__UpperCamelCase )} UpperCAmelCase__ : Tuple = list(batch[data_args.label_column_name] ) return output_batch def val_transforms(__UpperCamelCase ): UpperCAmelCase__ : str = [audio["""array"""] for audio in batch[data_args.audio_column_name]] UpperCAmelCase__ : List[Any] = feature_extractor(__UpperCamelCase , sampling_rate=feature_extractor.sampling_rate ) UpperCAmelCase__ : Dict = {model_input_name: inputs.get(__UpperCamelCase )} UpperCAmelCase__ : List[str] = list(batch[data_args.label_column_name] ) return output_batch # Prepare label mappings. # We'll include these in the model's config to get human readable labels in the Inference API. UpperCAmelCase__ : Any = raw_datasets["""train"""].features[data_args.label_column_name].names UpperCAmelCase__ , UpperCAmelCase__ : List[str] = {}, {} for i, label in enumerate(__UpperCamelCase ): UpperCAmelCase__ : Any = str(__UpperCamelCase ) UpperCAmelCase__ : int = label # Load the accuracy metric from the datasets package UpperCAmelCase__ : Any = evaluate.load("""accuracy""" ) # Define our compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with # `predictions` and `label_ids` fields) and has to return a dictionary string to float. def compute_metrics(__UpperCamelCase ): UpperCAmelCase__ : Dict = np.argmax(eval_pred.predictions , axis=1 ) return metric.compute(predictions=__UpperCamelCase , references=eval_pred.label_ids ) UpperCAmelCase__ : Any = AutoConfig.from_pretrained( model_args.config_name or model_args.model_name_or_path , num_labels=len(__UpperCamelCase ) , labelaid=__UpperCamelCase , idalabel=__UpperCamelCase , finetuning_task="""audio-classification""" , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) UpperCAmelCase__ : List[Any] = AutoModelForAudioClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool(""".ckpt""" in model_args.model_name_or_path ) , config=__UpperCamelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ignore_mismatched_sizes=model_args.ignore_mismatched_sizes , ) # freeze the convolutional waveform encoder if model_args.freeze_feature_encoder: model.freeze_feature_encoder() if training_args.do_train: if data_args.max_train_samples is not None: UpperCAmelCase__ : Optional[int] = ( raw_datasets["""train"""].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) ) # Set the training transforms raw_datasets["train"].set_transform(__UpperCamelCase , output_all_columns=__UpperCamelCase ) if training_args.do_eval: if data_args.max_eval_samples is not None: UpperCAmelCase__ : Tuple = ( raw_datasets["""eval"""].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms raw_datasets["eval"].set_transform(__UpperCamelCase , output_all_columns=__UpperCamelCase ) # Initialize our trainer UpperCAmelCase__ : Optional[Any] = Trainer( model=__UpperCamelCase , args=__UpperCamelCase , train_dataset=raw_datasets["""train"""] if training_args.do_train else None , eval_dataset=raw_datasets["""eval"""] if training_args.do_eval else None , compute_metrics=__UpperCamelCase , tokenizer=__UpperCamelCase , ) # Training if training_args.do_train: UpperCAmelCase__ : Dict = None if training_args.resume_from_checkpoint is not None: UpperCAmelCase__ : List[str] = training_args.resume_from_checkpoint elif last_checkpoint is not None: UpperCAmelCase__ : str = last_checkpoint UpperCAmelCase__ : str = trainer.train(resume_from_checkpoint=__UpperCamelCase ) trainer.save_model() trainer.log_metrics("""train""" , train_result.metrics ) trainer.save_metrics("""train""" , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: UpperCAmelCase__ : int = trainer.evaluate() trainer.log_metrics("""eval""" , __UpperCamelCase ) trainer.save_metrics("""eval""" , __UpperCamelCase ) # Write model card and (optionally) push to hub UpperCAmelCase__ : List[str] = { """finetuned_from""": model_args.model_name_or_path, """tasks""": """audio-classification""", """dataset""": data_args.dataset_name, """tags""": ["""audio-classification"""], } if training_args.push_to_hub: trainer.push_to_hub(**__UpperCamelCase ) else: trainer.create_model_card(**__UpperCamelCase ) if __name__ == "__main__": main()
65
from datetime import datetime import requests from bsa import BeautifulSoup if __name__ == "__main__": A_ : int = input('Enter image url: ').strip() print(F"""Downloading image from {url} ...""") A_ : Dict = BeautifulSoup(requests.get(url).content, 'html.parser') # The image URL is in the content field of the first meta tag with property og:image A_ : Optional[Any] = soup.find('meta', {'property': 'og:image'})['content'] A_ : List[Any] = requests.get(image_url).content A_ : Optional[int] = F"""{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg""" with open(file_name, 'wb') as fp: fp.write(image_data) print(F"""Done. Image saved to disk as {file_name}.""")
303
0
import math import time from transformers import Trainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput, speed_metrics if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class a ( __lowerCAmelCase ): """simple docstring""" def __init__( self , *lowerCAmelCase_ , lowerCAmelCase_=None , lowerCAmelCase_=None , **lowerCAmelCase_ ) -> List[Any]: super().__init__(*lowerCAmelCase_ , **lowerCAmelCase_ ) _A = eval_examples _A = post_process_function def UpperCAmelCase ( self , lowerCAmelCase_=None , lowerCAmelCase_=None , lowerCAmelCase_=None , lowerCAmelCase_ = "eval" ) -> Tuple: _A = self.eval_dataset if eval_dataset is None else eval_dataset _A = self.get_eval_dataloader(lowerCAmelCase_ ) _A = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. _A = self.compute_metrics _A = None _A = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop _A = time.time() try: _A = eval_loop( lowerCAmelCase_ , description="""Evaluation""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=lowerCAmelCase_ , metric_key_prefix=lowerCAmelCase_ , ) finally: _A = compute_metrics _A = self.args.eval_batch_size * self.args.world_size if F'''{metric_key_prefix}_jit_compilation_time''' in output.metrics: start_time += output.metrics[F'''{metric_key_prefix}_jit_compilation_time'''] output.metrics.update( speed_metrics( lowerCAmelCase_ , lowerCAmelCase_ , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save: # Only the main node write the results by default _A = self.post_process_function(lowerCAmelCase_ , lowerCAmelCase_ , output.predictions ) _A = self.compute_metrics(lowerCAmelCase_ ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(F'''{metric_key_prefix}_''' ): _A = metrics.pop(lowerCAmelCase_ ) metrics.update(output.metrics ) else: _A = output.metrics if self.args.should_log: # Only the main node log the results by default self.log(lowerCAmelCase_ ) if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report() ) _A = self.callback_handler.on_evaluate(self.args , self.state , self.control , lowerCAmelCase_ ) return metrics def UpperCAmelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_=None , lowerCAmelCase_ = "test" ) -> Dict: _A = self.get_test_dataloader(lowerCAmelCase_ ) # Temporarily disable metric computation, we will do it in the loop here. _A = self.compute_metrics _A = None _A = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop _A = time.time() try: _A = eval_loop( lowerCAmelCase_ , description="""Prediction""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=lowerCAmelCase_ , metric_key_prefix=lowerCAmelCase_ , ) finally: _A = compute_metrics _A = self.args.eval_batch_size * self.args.world_size if F'''{metric_key_prefix}_jit_compilation_time''' in output.metrics: start_time += output.metrics[F'''{metric_key_prefix}_jit_compilation_time'''] output.metrics.update( speed_metrics( lowerCAmelCase_ , lowerCAmelCase_ , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is None or self.compute_metrics is None: return output _A = self.post_process_function(lowerCAmelCase_ , lowerCAmelCase_ , output.predictions , """predict""" ) _A = self.compute_metrics(lowerCAmelCase_ ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(F'''{metric_key_prefix}_''' ): _A = metrics.pop(lowerCAmelCase_ ) metrics.update(output.metrics ) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=lowerCAmelCase_ )
83
_SCREAMING_SNAKE_CASE = { 'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--', 'Z': '--..', '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.', '0': '-----', '&': '.-...', '@': '.--.-.', ':': '---...', ',': '--..--', '.': '.-.-.-', '\'': '.----.', '"': '.-..-.', '?': '..--..', '/': '-..-.', '=': '-...-', '+': '.-.-.', '-': '-....-', '(': '-.--.', ')': '-.--.-', '!': '-.-.--', ' ': '/' } # Exclamation mark is not in ITU-R recommendation # fmt: on _SCREAMING_SNAKE_CASE = {value: key for key, value in MORSE_CODE_DICT.items()} def snake_case ( snake_case__ :str) -> str: return " ".join(MORSE_CODE_DICT[char] for char in message.upper()) def snake_case ( snake_case__ :str) -> str: return "".join(REVERSE_DICT[char] for char in message.split()) def snake_case ( ) -> None: _A = """Morse code here!""" print(snake_case__) _A = encrypt(snake_case__) print(snake_case__) _A = decrypt(snake_case__) print(snake_case__) if __name__ == "__main__": main()
83
1
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ..models.auto import AutoModelForVisionaSeq from ..utils import requires_backends from .base import PipelineTool if TYPE_CHECKING: from PIL import Image class SCREAMING_SNAKE_CASE ( lowerCAmelCase ): '''simple docstring''' UpperCamelCase_ : Tuple = '''Salesforce/blip-image-captioning-base''' UpperCamelCase_ : List[str] = ( '''This is a tool that generates a description of an image. It takes an input named `image` which should be the ''' '''image to caption, and returns a text that contains the description in English.''' ) UpperCamelCase_ : str = '''image_captioner''' UpperCamelCase_ : Any = AutoModelForVisionaSeq UpperCamelCase_ : List[Any] = ['''image'''] UpperCamelCase_ : Optional[int] = ['''text'''] def __init__( self : List[str] , *UpperCAmelCase_ : Any , **UpperCAmelCase_ : str ): requires_backends(self , ["vision"] ) super().__init__(*UpperCAmelCase_ , **UpperCAmelCase_ ) def _A ( self : List[Any] , UpperCAmelCase_ : "Image" ): return self.pre_processor(images=UpperCAmelCase_ , return_tensors="pt" ) def _A ( self : List[str] , UpperCAmelCase_ : int ): return self.model.generate(**UpperCAmelCase_ ) def _A ( self : Optional[int] , UpperCAmelCase_ : Dict ): return self.pre_processor.batch_decode(UpperCAmelCase_ , skip_special_tokens=UpperCAmelCase_ )[0].strip()
62
'''simple docstring''' import unittest import numpy as np import torch from diffusers import VersatileDiffusionImageVariationPipeline from diffusers.utils.testing_utils import load_image, require_torch_gpu, slow, torch_device _UpperCAmelCase : List[Any] = False class lowercase_ ( unittest.TestCase ): """simple docstring""" pass @slow @require_torch_gpu class lowercase_ ( unittest.TestCase ): """simple docstring""" def __UpperCAmelCase ( self : List[str] ) -> Dict: _A = VersatileDiffusionImageVariationPipeline.from_pretrained('shi-labs/versatile-diffusion' ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) _A = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg' ) _A = torch.manual_seed(0 ) _A = pipe( image=UpperCamelCase__, generator=UpperCamelCase__, guidance_scale=7.5, num_inference_steps=50, output_type='numpy', ).images _A = image[0, 2_53:2_56, 2_53:2_56, -1] assert image.shape == (1, 5_12, 5_12, 3) _A = np.array([0.0_441, 0.0_469, 0.0_507, 0.0_575, 0.0_632, 0.0_650, 0.0_865, 0.0_909, 0.0_945] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
107
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowercase = { '''configuration_swinv2''': ['''SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Swinv2Config'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase = [ '''SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST''', '''Swinv2ForImageClassification''', '''Swinv2ForMaskedImageModeling''', '''Swinv2Model''', '''Swinv2PreTrainedModel''', ] if TYPE_CHECKING: from .configuration_swinva import SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinvaConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swinva import ( SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST, SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel, SwinvaPreTrainedModel, ) else: import sys lowercase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
41
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase = logging.get_logger(__name__) lowercase = { '''google/switch-base-8''': '''https://huggingface.co/google/switch-base-8/blob/main/config.json''', } class UpperCAmelCase ( __a): '''simple docstring''' __magic_name__ : int = "switch_transformers" __magic_name__ : List[Any] = ["past_key_values"] __magic_name__ : Union[str, Any] = {"hidden_size": "d_model", "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers"} def __init__( self , lowerCAmelCase_=3_2_1_2_8 , lowerCAmelCase_=7_6_8 , lowerCAmelCase_=6_4 , lowerCAmelCase_=2_0_4_8 , lowerCAmelCase_=6_4 , lowerCAmelCase_=1_2 , lowerCAmelCase_=3 , lowerCAmelCase_=1_2 , lowerCAmelCase_=3 , lowerCAmelCase_=1_2 , lowerCAmelCase_=8 , lowerCAmelCase_=False , lowerCAmelCase_=0.0_1 , lowerCAmelCase_="float32" , lowerCAmelCase_=False , lowerCAmelCase_=3_2 , lowerCAmelCase_=1_2_8 , lowerCAmelCase_=0.1 , lowerCAmelCase_=1e-6 , lowerCAmelCase_=0.0_0_1 , lowerCAmelCase_=0.0_0_1 , lowerCAmelCase_=1.0 , lowerCAmelCase_="relu" , lowerCAmelCase_=True , lowerCAmelCase_=False , lowerCAmelCase_=True , lowerCAmelCase_=0 , lowerCAmelCase_=1 , **lowerCAmelCase_ , ) -> Optional[int]: """simple docstring""" a_ =vocab_size a_ =d_model a_ =d_kv a_ =d_ff a_ =num_sparse_encoder_layers a_ =num_layers a_ =( num_decoder_layers if num_decoder_layers is not None else self.num_layers ) # default = symmetry a_ =num_sparse_decoder_layers # This tells us, each how many encoder layer we'll have to set a sparse layer. if self.num_sparse_encoder_layers > 0: a_ =self.num_layers // self.num_sparse_encoder_layers else: a_ =self.num_layers # HACK: this will create 0 sparse layers # This tells us, each how many encoder layer we'll have to set a sparse layer. if self.num_sparse_decoder_layers > 0: a_ =self.num_decoder_layers // self.num_sparse_decoder_layers else: a_ =self.num_decoder_layers # HACK: this will create 0 sparse layers a_ =num_heads a_ =num_experts a_ =expert_capacity a_ =router_bias a_ =router_jitter_noise if router_dtype not in ["float32", "float16", "bfloat16"]: raise ValueError(f"""`router_dtype` must be one of 'float32', 'float16' or 'bfloat16', got {router_dtype}""") a_ =router_dtype a_ =router_ignore_padding_tokens a_ =relative_attention_num_buckets a_ =relative_attention_max_distance a_ =dropout_rate a_ =layer_norm_epsilon a_ =initializer_factor a_ =feed_forward_proj a_ =use_cache a_ =add_router_probs a_ =router_z_loss_coef a_ =router_aux_loss_coef a_ =self.feed_forward_proj.split("-") a_ =act_info[-1] a_ =act_info[0] == "gated" if len(lowerCAmelCase_) > 1 and act_info[0] != "gated" or len(lowerCAmelCase_) > 2: raise ValueError( f"""`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer.""" "Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. " "'gated-gelu' or 'relu'") # for backwards compatibility if feed_forward_proj == "gated-gelu": a_ ="gelu_new" super().__init__( pad_token_id=lowerCAmelCase_ , eos_token_id=lowerCAmelCase_ , is_encoder_decoder=lowerCAmelCase_ , **lowerCAmelCase_ , )
41
1
'''simple docstring''' def lowercase__( __UpperCamelCase: int = 10_00 ): """simple docstring""" return sum(e for e in range(3 ,__UpperCamelCase ) if e % 3 == 0 or e % 5 == 0 ) if __name__ == "__main__": print(F"""{solution() = }""")
28
import requests from bsa import BeautifulSoup def lowerCamelCase( a__ = "https://www.worldometers.info/coronavirus"): _SCREAMING_SNAKE_CASE =BeautifulSoup(requests.get(a__).text ,'''html.parser''') _SCREAMING_SNAKE_CASE =soup.findAll('''h1''') _SCREAMING_SNAKE_CASE =soup.findAll('''div''' ,{'''class''': '''maincounter-number'''}) keys += soup.findAll('''span''' ,{'''class''': '''panel-title'''}) values += soup.findAll('''div''' ,{'''class''': '''number-table-main'''}) return {key.text.strip(): value.text.strip() for key, value in zip(a__ ,a__)} if __name__ == "__main__": print('''\033[1m''' + '''COVID-19 Status of the World''' + '''\033[0m\n''') for key, value in world_covidaa_stats().items(): print(f"""{key}\n{value}\n""")
691
0
'''simple docstring''' import unittest from transformers import CamembertTokenizer, CamembertTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.utils import is_torch_available from ...test_tokenization_common import TokenizerTesterMixin SCREAMING_SNAKE_CASE__ : int = get_tests_dir('''fixtures/test_sentencepiece.model''') SCREAMING_SNAKE_CASE__ : Dict = get_tests_dir('''fixtures/test_sentencepiece_bpe.model''') SCREAMING_SNAKE_CASE__ : Dict = '''pt''' if is_torch_available() else '''tf''' @require_sentencepiece @require_tokenizers class a__( snake_case__ , unittest.TestCase ): a_ : List[str] = CamembertTokenizer a_ : List[str] = CamembertTokenizerFast a_ : Tuple = True a_ : Union[str, Any] = True def _lowercase ( self ) -> str: super().setUp() # We have a SentencePiece fixture for testing snake_case__ =CamembertTokenizer(_UpperCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) def _lowercase ( self ) -> List[Any]: snake_case__ ='<pad>' snake_case__ =1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_UpperCAmelCase ) , _UpperCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_UpperCAmelCase ) , _UpperCAmelCase ) def _lowercase ( self ) -> List[Any]: snake_case__ =list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '<s>NOTUSED' ) self.assertEqual(vocab_keys[1] , '<pad>' ) self.assertEqual(vocab_keys[-1] , '<mask>' ) self.assertEqual(len(_UpperCAmelCase ) , 1004 ) def _lowercase ( self ) -> str: self.assertEqual(self.get_tokenizer().vocab_size , 1005 ) def _lowercase ( self ) -> List[Any]: snake_case__ =CamembertTokenizer(_UpperCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) snake_case__ =CamembertTokenizerFast.from_pretrained(self.tmpdirname ) snake_case__ ='I was born in 92000, and this is falsé.' snake_case__ =tokenizer.encode(_UpperCAmelCase ) snake_case__ =rust_tokenizer.encode(_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) snake_case__ =tokenizer.encode(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) snake_case__ =rust_tokenizer.encode(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) # <unk> tokens are not the same for `rust` than for `slow`. # Because spm gives back raw token instead of `unk` in EncodeAsPieces # tokens = tokenizer.tokenize(sequence) snake_case__ =tokenizer.convert_ids_to_tokens(_UpperCAmelCase ) snake_case__ =rust_tokenizer.tokenize(_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) def _lowercase ( self ) -> Tuple: if not self.test_rust_tokenizer: return snake_case__ =self.get_tokenizer() snake_case__ =self.get_rust_tokenizer() snake_case__ ='I was born in 92000, and this is falsé.' snake_case__ =tokenizer.tokenize(_UpperCAmelCase ) snake_case__ =rust_tokenizer.tokenize(_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) snake_case__ =tokenizer.encode(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) snake_case__ =rust_tokenizer.encode(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) snake_case__ =self.get_rust_tokenizer() snake_case__ =tokenizer.encode(_UpperCAmelCase ) snake_case__ =rust_tokenizer.encode(_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) @slow def _lowercase ( self ) -> int: # fmt: off snake_case__ ={'input_ids': [[5, 54, 7196, 297, 30, 23, 776, 18, 11, 3215, 3705, 8252, 22, 3164, 1181, 2116, 29, 16, 813, 25, 791, 3314, 20, 3446, 38, 2_7575, 120, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [5, 468, 17, 11, 9088, 20, 1517, 8, 2_2804, 1_8818, 10, 38, 629, 607, 607, 142, 19, 7196, 867, 56, 1_0326, 24, 2267, 20, 416, 5072, 1_5612, 233, 734, 7, 2399, 27, 16, 3015, 1649, 7, 24, 20, 4338, 2399, 27, 13, 3400, 14, 13, 6189, 8, 930, 9, 6]], '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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]} # noqa: E501 # fmt: on # camembert is a french model. So we also use french texts. snake_case__ =[ 'Le transformeur est un modèle d\'apprentissage profond introduit en 2017, ' 'utilisé principalement dans le domaine du traitement automatique des langues (TAL).', 'À l\'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus ' 'pour gérer des données séquentielles, telles que le langage naturel, pour des tâches ' 'telles que la traduction et la synthèse de texte.', ] self.tokenizer_integration_test_util( expected_encoding=_UpperCAmelCase , model_name='camembert-base' , revision='3a0641d9a1aeb7e848a74299e7e4c4bca216b4cf' , sequences=_UpperCAmelCase , )
581
'''simple docstring''' from __future__ import annotations def a ( UpperCamelCase_ : list[float] , UpperCamelCase_ : list[float] ) -> float: snake_case__ =sorted(numsa + numsa ) snake_case__ , snake_case__ =divmod(len(UpperCamelCase_ ) , 2 ) if mod == 1: return all_numbers[div] else: return (all_numbers[div] + all_numbers[div - 1]) / 2 if __name__ == "__main__": import doctest doctest.testmod() SCREAMING_SNAKE_CASE__ : Optional[Any] = [float(x) for x in input('''Enter the elements of first array: ''').split()] SCREAMING_SNAKE_CASE__ : Union[str, Any] = [float(x) for x in input('''Enter the elements of second array: ''').split()] print(f"""The median of two arrays is: {median_of_two_arrays(array_a, array_a)}""")
581
1
'''simple docstring''' # DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from typing import Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import randn_tensor from .scheduling_utils import SchedulerMixin class __UpperCamelCase ( lowercase__ , lowercase__ ): lowercase : Tuple = 1 @register_to_config def __init__( self :Optional[Any] ,_UpperCamelCase :Tuple=2_0_0_0 ,_UpperCamelCase :List[str]=0.1 ,_UpperCamelCase :Optional[int]=2_0 ,_UpperCamelCase :Any=1E-3 ): snake_case_ : int = None snake_case_ : Tuple = None snake_case_ : Optional[Any] = None def a__ ( self :Tuple ,_UpperCamelCase :List[str] ,_UpperCamelCase :Union[str, torch.device] = None ): snake_case_ : List[Any] = torch.linspace(1 ,self.config.sampling_eps ,_UpperCamelCase ,device=_UpperCamelCase ) def a__ ( self :Any ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :Any ,_UpperCamelCase :Tuple=None ): if self.timesteps is None: raise ValueError( """`self.timesteps` is not set, you need to run 'set_timesteps' after creating the scheduler""" ) # TODO(Patrick) better comments + non-PyTorch # postprocess model score snake_case_ : Union[str, Any] = ( -0.25 * t**2 * (self.config.beta_max - self.config.beta_min) - 0.5 * t * self.config.beta_min ) snake_case_ : List[str] = torch.sqrt(1.0 - torch.exp(2.0 * log_mean_coeff ) ) snake_case_ : Tuple = std.flatten() while len(std.shape ) < len(score.shape ): snake_case_ : Dict = std.unsqueeze(-1 ) snake_case_ : Tuple = -score / std # compute snake_case_ : Any = -1.0 / len(self.timesteps ) snake_case_ : Union[str, Any] = self.config.beta_min + t * (self.config.beta_max - self.config.beta_min) snake_case_ : Any = beta_t.flatten() while len(beta_t.shape ) < len(x.shape ): snake_case_ : Optional[int] = beta_t.unsqueeze(-1 ) snake_case_ : List[str] = -0.5 * beta_t * x snake_case_ : Tuple = torch.sqrt(_UpperCamelCase ) snake_case_ : Any = drift - diffusion**2 * score snake_case_ : List[str] = x + drift * dt # add noise snake_case_ : List[Any] = randn_tensor(x.shape ,layout=x.layout ,generator=_UpperCamelCase ,device=x.device ,dtype=x.dtype ) snake_case_ : int = x_mean + diffusion * math.sqrt(-dt ) * noise return x, x_mean def __len__( self :List[Any] ): return self.config.num_train_timesteps
334
'''simple docstring''' import copy from typing import Dict, Optional from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING from ..detr import DetrConfig from ..swin import SwinConfig __A : List[Any] = { 'facebook/maskformer-swin-base-ade': ( 'https://huggingface.co/facebook/maskformer-swin-base-ade/blob/main/config.json' ) # See all MaskFormer models at https://huggingface.co/models?filter=maskformer } __A : Optional[Any] = logging.get_logger(__name__) class __UpperCamelCase ( lowercase__ ): lowercase : Union[str, Any] = 'maskformer' lowercase : List[str] = {'hidden_size': 'mask_feature_size'} lowercase : int = ['resnet', 'swin'] lowercase : List[str] = ['detr'] def __init__( self :Dict ,_UpperCamelCase :int = 2_5_6 ,_UpperCamelCase :int = 2_5_6 ,_UpperCamelCase :float = 0.1 ,_UpperCamelCase :bool = False ,_UpperCamelCase :Optional[Dict] = None ,_UpperCamelCase :Optional[Dict] = None ,_UpperCamelCase :float = 0.02 ,_UpperCamelCase :float = 1.0 ,_UpperCamelCase :float = 1.0 ,_UpperCamelCase :float = 1.0 ,_UpperCamelCase :float = 20.0 ,_UpperCamelCase :Optional[bool] = None ,**_UpperCamelCase :List[str] ,): if backbone_config is None: # fall back to https://huggingface.co/microsoft/swin-base-patch4-window12-384-in22k snake_case_ : Any = SwinConfig( image_size=3_8_4 ,in_channels=3 ,patch_size=4 ,embed_dim=1_2_8 ,depths=[2, 2, 1_8, 2] ,num_heads=[4, 8, 1_6, 3_2] ,window_size=1_2 ,drop_path_rate=0.3 ,out_features=["""stage1""", """stage2""", """stage3""", """stage4"""] ,) if isinstance(_UpperCamelCase ,_UpperCamelCase ): snake_case_ : Optional[Any] = backbone_config.pop("""model_type""" ) snake_case_ : List[Any] = CONFIG_MAPPING[backbone_model_type] snake_case_ : List[Any] = config_class.from_dict(_UpperCamelCase ) # verify that the backbone is supported if backbone_config.model_type not in self.backbones_supported: logger.warning_once( F'''Backbone {backbone_config.model_type} is not a supported model and may not be compatible with MaskFormer. ''' F'''Supported model types: {",".join(self.backbones_supported )}''' ) if decoder_config is None: # fall back to https://huggingface.co/facebook/detr-resnet-50 snake_case_ : str = DetrConfig() else: # verify that the decoder is supported snake_case_ : Tuple = ( decoder_config.pop("""model_type""" ) if isinstance(_UpperCamelCase ,_UpperCamelCase ) else decoder_config.model_type ) if decoder_type not in self.decoders_supported: raise ValueError( F'''Transformer Decoder {decoder_type} not supported, please use one of''' F''' {",".join(self.decoders_supported )}''' ) if isinstance(_UpperCamelCase ,_UpperCamelCase ): snake_case_ : Optional[Any] = CONFIG_MAPPING[decoder_type] snake_case_ : List[Any] = config_class.from_dict(_UpperCamelCase ) snake_case_ : List[Any] = backbone_config snake_case_ : str = decoder_config # main feature dimension for the model snake_case_ : Dict = fpn_feature_size snake_case_ : Any = mask_feature_size # initializer snake_case_ : str = init_std snake_case_ : str = init_xavier_std # Hungarian matcher && loss snake_case_ : Any = cross_entropy_weight snake_case_ : Optional[int] = dice_weight snake_case_ : str = mask_weight snake_case_ : Any = use_auxiliary_loss snake_case_ : Optional[int] = no_object_weight snake_case_ : Tuple = output_auxiliary_logits snake_case_ : Tuple = self.decoder_config.encoder_attention_heads snake_case_ : Optional[int] = self.decoder_config.num_hidden_layers super().__init__(**_UpperCamelCase ) @classmethod def a__ ( cls :str ,_UpperCamelCase :PretrainedConfig ,_UpperCamelCase :PretrainedConfig ,**_UpperCamelCase :Any ): return cls( backbone_config=_UpperCamelCase ,decoder_config=_UpperCamelCase ,**_UpperCamelCase ,) def a__ ( self :Optional[int] ): snake_case_ : List[str] = copy.deepcopy(self.__dict__ ) snake_case_ : List[str] = self.backbone_config.to_dict() snake_case_ : List[str] = self.decoder_config.to_dict() snake_case_ : List[Any] = self.__class__.model_type return output
334
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__ = { '''shi-labs/nat-mini-in1k-224''': '''https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json''', # See all Nat models at https://huggingface.co/models?filter=nat } class __lowerCAmelCase ( UpperCAmelCase__ , UpperCAmelCase__ ): """simple docstring""" A__ : List[Any] = 'nat' A__ : Optional[Any] = { 'num_attention_heads': 'num_heads', 'num_hidden_layers': 'num_layers', } def __init__( self : Optional[Any] , _snake_case : Optional[int]=4 , _snake_case : int=3 , _snake_case : Any=64 , _snake_case : Union[str, Any]=[3, 4, 6, 5] , _snake_case : Optional[Any]=[2, 4, 8, 16] , _snake_case : Any=7 , _snake_case : int=3.0 , _snake_case : Optional[int]=True , _snake_case : Optional[int]=0.0 , _snake_case : int=0.0 , _snake_case : List[Any]=0.1 , _snake_case : Dict="gelu" , _snake_case : Dict=0.02 , _snake_case : Optional[Any]=1E-5 , _snake_case : str=0.0 , _snake_case : Dict=None , _snake_case : Dict=None , **_snake_case : str , ): """simple docstring""" super().__init__(**_snake_case ) A__ = patch_size A__ = num_channels A__ = embed_dim A__ = depths A__ = len(_snake_case ) A__ = num_heads A__ = kernel_size A__ = mlp_ratio A__ = qkv_bias A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = drop_path_rate A__ = hidden_act A__ = layer_norm_eps A__ = initializer_range # we set the hidden_size attribute in order to make Nat work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model A__ = int(embed_dim * 2 ** (len(_snake_case ) - 1) ) A__ = layer_scale_init_value A__ = ["""stem"""] + [F'''stage{idx}''' for idx in range(1 , len(_snake_case ) + 1 )] A__ = get_aligned_output_features_output_indices( out_features=_snake_case , out_indices=_snake_case , stage_names=self.stage_names )
714
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
0
"""simple docstring""" import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import ClassLabel, Features, Image from .base import TaskTemplate @dataclass(frozen=snake_case_ ) class UpperCamelCase ( snake_case_ ): UpperCamelCase : str = field(default='''image-classification''' , metadata={'''include_in_asdict_even_if_is_default''': True} ) UpperCamelCase : ClassVar[Features] = Features({'''image''': Image()} ) UpperCamelCase : ClassVar[Features] = Features({'''labels''': ClassLabel} ) UpperCamelCase : str = "image" UpperCamelCase : str = "labels" def _lowercase ( self : Tuple , UpperCAmelCase__ : Optional[Any] ) -> Optional[Any]: 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] , UpperCAmelCase__ ): raise ValueError(f"""Column {self.label_column} is not a ClassLabel.""" ) _a : Optional[Any] = copy.deepcopy(self ) _a : Optional[Any] = self.label_schema.copy() _a : Tuple = features[self.label_column] _a : Tuple = label_schema return task_template @property def _lowercase ( self : int ) -> Dict[str, str]: return { self.image_column: "image", self.label_column: "labels", }
389
"""simple docstring""" import torch from diffusers import DiffusionPipeline class UpperCamelCase ( snake_case_ ): def __init__( self : List[Any] , UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[int] ) -> str: super().__init__() self.register_modules(unet=UpperCAmelCase__ , scheduler=UpperCAmelCase__ ) def __call__( self : Union[str, Any] ) -> Tuple: _a : Dict = torch.randn( (1, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size) , ) _a : Any = 1 _a : Any = self.unet(UpperCAmelCase__ , UpperCAmelCase__ ).sample _a : int = self.scheduler.step(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ).prev_sample _a : int = scheduler_output - scheduler_output + torch.ones_like(UpperCAmelCase__ ) return result
389
1
def a ( A__ : str , A__ : int ) -> list[str]: """simple docstring""" return [sentence[i : i + ngram_size] for i in range(len(A__ ) - ngram_size + 1 )] if __name__ == "__main__": from doctest import testmod testmod()
380
from __future__ import annotations from typing import Any class __lowerCAmelCase : def __init__( self , lowerCAmelCase ) -> None: '''simple docstring''' _lowercase =num_of_nodes _lowercase =[] _lowercase ={} def A__ ( self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ) -> None: '''simple docstring''' self.m_edges.append([u_node, v_node, weight] ) def A__ ( self , lowerCAmelCase ) -> int: '''simple docstring''' if self.m_component[u_node] == u_node: return u_node return self.find_component(self.m_component[u_node] ) def A__ ( self , lowerCAmelCase ) -> None: '''simple docstring''' if self.m_component[u_node] != u_node: for k in self.m_component: _lowercase =self.find_component(lowerCAmelCase ) def A__ ( self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ) -> None: '''simple docstring''' if component_size[u_node] <= component_size[v_node]: _lowercase =v_node component_size[v_node] += component_size[u_node] self.set_component(lowerCAmelCase ) elif component_size[u_node] >= component_size[v_node]: _lowercase =self.find_component(lowerCAmelCase ) component_size[u_node] += component_size[v_node] self.set_component(lowerCAmelCase ) def A__ ( self ) -> None: '''simple docstring''' _lowercase =[] _lowercase =0 _lowercase =[-1] * self.m_num_of_nodes # A list of components (initialized to all of the nodes) for node in range(self.m_num_of_nodes ): self.m_component.update({node: node} ) component_size.append(1 ) _lowercase =self.m_num_of_nodes while num_of_components > 1: for edge in self.m_edges: _lowercase , _lowercase , _lowercase =edge _lowercase =self.m_component[u] _lowercase =self.m_component[v] if u_component != v_component: for component in (u_component, v_component): if ( minimum_weight_edge[component] == -1 or minimum_weight_edge[component][2] > w ): _lowercase =[u, v, w] for edge in minimum_weight_edge: if isinstance(lowerCAmelCase , lowerCAmelCase ): _lowercase , _lowercase , _lowercase =edge _lowercase =self.m_component[u] _lowercase =self.m_component[v] if u_component != v_component: mst_weight += w self.union(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ) print(F'''Added edge [{u} - {v}]\nAdded weight: {w}\n''' ) num_of_components -= 1 _lowercase =[-1] * self.m_num_of_nodes print(F'''The total weight of the minimal spanning tree is: {mst_weight}''' ) def a ( ) -> None: """simple docstring""" if __name__ == "__main__": import doctest doctest.testmod()
380
1
import os import tempfile from functools import partial from unittest import TestCase from unittest.mock import patch import datasets import datasets.config from .utils import require_beam class _a ( datasets.BeamBasedBuilder ): """simple docstring""" def _UpperCAmelCase ( self ) -> List[str]: return datasets.DatasetInfo( features=datasets.Features({'content': datasets.Value('string' )} ) , supervised_keys=_UpperCAmelCase , ) def _UpperCAmelCase ( self , _UpperCAmelCase , _UpperCAmelCase ) -> Optional[Any]: return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'examples': get_test_dummy_examples()} )] def _UpperCAmelCase ( self , _UpperCAmelCase , _UpperCAmelCase ) -> Tuple: import apache_beam as beam return pipeline | "Load Examples" >> beam.Create(_UpperCAmelCase ) class _a ( datasets.BeamBasedBuilder ): """simple docstring""" def _UpperCAmelCase ( self ) -> Any: return datasets.DatasetInfo( features=datasets.Features({'a': datasets.Sequence({'b': datasets.Value('string' )} )} ) , supervised_keys=_UpperCAmelCase , ) def _UpperCAmelCase ( self , _UpperCAmelCase , _UpperCAmelCase ) -> Tuple: return [ datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'examples': get_test_nested_examples()} ) ] def _UpperCAmelCase ( self , _UpperCAmelCase , _UpperCAmelCase ) -> Union[str, Any]: import apache_beam as beam return pipeline | "Load Examples" >> beam.Create(_UpperCAmelCase ) def _snake_case (): return [(i, {"content": content}) for i, content in enumerate(['foo', 'bar', 'foobar'])] def _snake_case (): return [(i, {"a": {"b": [content]}}) for i, content in enumerate(['foo', 'bar', 'foobar'])] class _a ( UpperCAmelCase__ ): """simple docstring""" @require_beam def _UpperCAmelCase ( self ) -> Dict: UpperCamelCase_ = len(get_test_dummy_examples() ) with tempfile.TemporaryDirectory() as tmp_cache_dir: UpperCamelCase_ = DummyBeamDataset(cache_dir=_UpperCAmelCase , beam_runner='DirectRunner' ) builder.download_and_prepare() self.assertTrue( os.path.exists( os.path.join(_UpperCAmelCase , builder.name , 'default' , '0.0.0' , f"""{builder.name}-train.arrow""" ) ) ) self.assertDictEqual(builder.info.features , datasets.Features({'content': datasets.Value('string' )} ) ) UpperCamelCase_ = builder.as_dataset() self.assertEqual(dset['train'].num_rows , _UpperCAmelCase ) self.assertEqual(dset['train'].info.splits['train'].num_examples , _UpperCAmelCase ) self.assertDictEqual(dset['train'][0] , get_test_dummy_examples()[0][1] ) self.assertDictEqual( dset['train'][expected_num_examples - 1] , get_test_dummy_examples()[expected_num_examples - 1][1] ) self.assertTrue( os.path.exists(os.path.join(_UpperCAmelCase , builder.name , 'default' , '0.0.0' , 'dataset_info.json' ) ) ) del dset @require_beam def _UpperCAmelCase ( self ) -> List[str]: import apache_beam as beam UpperCamelCase_ = beam.io.parquetio.WriteToParquet UpperCamelCase_ = len(get_test_dummy_examples() ) with tempfile.TemporaryDirectory() as tmp_cache_dir: UpperCamelCase_ = DummyBeamDataset(cache_dir=_UpperCAmelCase , beam_runner='DirectRunner' ) with patch('apache_beam.io.parquetio.WriteToParquet' ) as write_parquet_mock: UpperCamelCase_ = partial(_UpperCAmelCase , num_shards=2 ) builder.download_and_prepare() self.assertTrue( os.path.exists( os.path.join( _UpperCAmelCase , builder.name , 'default' , '0.0.0' , f"""{builder.name}-train-00000-of-00002.arrow""" ) ) ) self.assertTrue( os.path.exists( os.path.join( _UpperCAmelCase , builder.name , 'default' , '0.0.0' , f"""{builder.name}-train-00000-of-00002.arrow""" ) ) ) self.assertDictEqual(builder.info.features , datasets.Features({'content': datasets.Value('string' )} ) ) UpperCamelCase_ = builder.as_dataset() self.assertEqual(dset['train'].num_rows , _UpperCAmelCase ) self.assertEqual(dset['train'].info.splits['train'].num_examples , _UpperCAmelCase ) # Order is not preserved when sharding, so we just check that all the elements are there self.assertListEqual(sorted(dset['train']['content'] ) , sorted(['foo', 'bar', 'foobar'] ) ) self.assertTrue( os.path.exists(os.path.join(_UpperCAmelCase , builder.name , 'default' , '0.0.0' , 'dataset_info.json' ) ) ) del dset @require_beam def _UpperCAmelCase ( self ) -> Any: with tempfile.TemporaryDirectory() as tmp_cache_dir: UpperCamelCase_ = DummyBeamDataset(cache_dir=_UpperCAmelCase ) self.assertRaises(datasets.builder.MissingBeamOptions , builder.download_and_prepare ) @require_beam def _UpperCAmelCase ( self ) -> Union[str, Any]: UpperCamelCase_ = len(get_test_nested_examples() ) with tempfile.TemporaryDirectory() as tmp_cache_dir: UpperCamelCase_ = NestedBeamDataset(cache_dir=_UpperCAmelCase , beam_runner='DirectRunner' ) builder.download_and_prepare() self.assertTrue( os.path.exists( os.path.join(_UpperCAmelCase , builder.name , 'default' , '0.0.0' , f"""{builder.name}-train.arrow""" ) ) ) self.assertDictEqual( builder.info.features , datasets.Features({'a': datasets.Sequence({'b': datasets.Value('string' )} )} ) ) UpperCamelCase_ = builder.as_dataset() self.assertEqual(dset['train'].num_rows , _UpperCAmelCase ) self.assertEqual(dset['train'].info.splits['train'].num_examples , _UpperCAmelCase ) self.assertDictEqual(dset['train'][0] , get_test_nested_examples()[0][1] ) self.assertDictEqual( dset['train'][expected_num_examples - 1] , get_test_nested_examples()[expected_num_examples - 1][1] ) self.assertTrue( os.path.exists(os.path.join(_UpperCAmelCase , builder.name , 'default' , '0.0.0' , 'dataset_info.json' ) ) ) del dset
23
a__ = [0, 2, 4, 6, 8] a__ = [1, 3, 5, 7, 9] def _UpperCAmelCase ( a : int , a : int , a : list[int] , a : int ): if remaining_length == 0: if digits[0] == 0 or digits[-1] == 0: return 0 for i in range(length // 2 - 1 , -1 , -1 ): remainder += digits[i] + digits[length - i - 1] if remainder % 2 == 0: return 0 remainder //= 10 return 1 if remaining_length == 1: if remainder % 2 == 0: return 0 snake_case__ = 0 for digit in range(10 ): snake_case__ = digit result += reversible_numbers( 0 , (remainder + 2 * digit) // 10 , a , a ) return result snake_case__ = 0 for digita in range(10 ): snake_case__ = digita if (remainder + digita) % 2 == 0: snake_case__ = ODD_DIGITS else: snake_case__ = EVEN_DIGITS for digita in other_parity_digits: snake_case__ = digita result += reversible_numbers( remaining_length - 2 , (remainder + digita + digita) // 10 , a , a , ) return result def _UpperCAmelCase ( a : int = 9 ): snake_case__ = 0 for length in range(1 , max_power + 1 ): result += reversible_numbers(a , 0 , [0] * length , a ) return result if __name__ == "__main__": print(F'''{solution() = }''')
654
0
"""simple docstring""" import inspect import unittest from transformers import YolosConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import YolosForObjectDetection, YolosModel from transformers.models.yolos.modeling_yolos import YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class a : """simple docstring""" def __init__( self , snake_case_ , snake_case_=13 , snake_case_=[30, 30] , snake_case_=2 , snake_case_=3 , snake_case_=True , snake_case_=True , snake_case_=32 , snake_case_=5 , snake_case_=4 , snake_case_=37 , snake_case_="gelu" , snake_case_=0.1 , snake_case_=0.1 , snake_case_=10 , snake_case_=0.02 , snake_case_=3 , snake_case_=None , snake_case_=8 , snake_case_=10 , ) -> Union[str, Any]: _UpperCAmelCase = parent _UpperCAmelCase = batch_size _UpperCAmelCase = image_size _UpperCAmelCase = patch_size _UpperCAmelCase = num_channels _UpperCAmelCase = is_training _UpperCAmelCase = use_labels _UpperCAmelCase = hidden_size _UpperCAmelCase = num_hidden_layers _UpperCAmelCase = num_attention_heads _UpperCAmelCase = intermediate_size _UpperCAmelCase = hidden_act _UpperCAmelCase = hidden_dropout_prob _UpperCAmelCase = attention_probs_dropout_prob _UpperCAmelCase = type_sequence_label_size _UpperCAmelCase = initializer_range _UpperCAmelCase = num_labels _UpperCAmelCase = scope _UpperCAmelCase = n_targets _UpperCAmelCase = num_detection_tokens # we set the expected sequence length (which is used in several tests) # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) + num_detection_tokens _UpperCAmelCase = (image_size[1] // patch_size) * (image_size[0] // patch_size) _UpperCAmelCase = num_patches + 1 + self.num_detection_tokens def __A ( self ) -> Union[str, Any]: _UpperCAmelCase = floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]] ) _UpperCAmelCase = None if self.use_labels: # labels is a list of Dict (each Dict being the labels for a given example in the batch) _UpperCAmelCase = [] for i in range(self.batch_size ): _UpperCAmelCase = {} _UpperCAmelCase = torch.randint( high=self.num_labels , size=(self.n_targets,) , device=snake_case_ ) _UpperCAmelCase = torch.rand(self.n_targets , 4 , device=snake_case_ ) labels.append(snake_case_ ) _UpperCAmelCase = self.get_config() return config, pixel_values, labels def __A ( self ) -> Tuple: return YolosConfig( 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 , num_detection_tokens=self.num_detection_tokens , num_labels=self.num_labels , ) def __A ( self , snake_case_ , snake_case_ , snake_case_ ) -> Dict: _UpperCAmelCase = YolosModel(config=snake_case_ ) model.to(snake_case_ ) model.eval() _UpperCAmelCase = model(snake_case_ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.expected_seq_len, self.hidden_size) ) def __A ( self , snake_case_ , snake_case_ , snake_case_ ) -> List[str]: _UpperCAmelCase = YolosForObjectDetection(snake_case_ ) model.to(snake_case_ ) model.eval() _UpperCAmelCase = model(pixel_values=snake_case_ ) _UpperCAmelCase = model(snake_case_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_detection_tokens, self.num_labels + 1) ) self.parent.assertEqual(result.pred_boxes.shape , (self.batch_size, self.num_detection_tokens, 4) ) _UpperCAmelCase = model(pixel_values=snake_case_ , labels=snake_case_ ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_detection_tokens, self.num_labels + 1) ) self.parent.assertEqual(result.pred_boxes.shape , (self.batch_size, self.num_detection_tokens, 4) ) def __A ( self ) -> Optional[Any]: _UpperCAmelCase = self.prepare_config_and_inputs() _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = config_and_inputs _UpperCAmelCase = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class a ( _SCREAMING_SNAKE_CASE, _SCREAMING_SNAKE_CASE, unittest.TestCase ): """simple docstring""" A__ : Any = (YolosModel, YolosForObjectDetection) if is_torch_available() else () A__ : Any = ( {"feature-extraction": YolosModel, "object-detection": YolosForObjectDetection} if is_torch_available() else {} ) A__ : int = False A__ : Any = False A__ : List[str] = False A__ : Any = False def __A ( self , snake_case_ , snake_case_ , snake_case_=False ) -> Any: _UpperCAmelCase = super()._prepare_for_class(snake_case_ , snake_case_ , return_labels=snake_case_ ) if return_labels: if model_class.__name__ == "YolosForObjectDetection": _UpperCAmelCase = [] for i in range(self.model_tester.batch_size ): _UpperCAmelCase = {} _UpperCAmelCase = torch.ones( size=(self.model_tester.n_targets,) , device=snake_case_ , dtype=torch.long ) _UpperCAmelCase = torch.ones( self.model_tester.n_targets , 4 , device=snake_case_ , dtype=torch.float ) labels.append(snake_case_ ) _UpperCAmelCase = labels return inputs_dict def __A ( self ) -> Dict: _UpperCAmelCase = YolosModelTester(self ) _UpperCAmelCase = ConfigTester(self , config_class=snake_case_ , has_text_modality=snake_case_ , hidden_size=37 ) def __A ( self ) -> List[Any]: self.config_tester.run_common_tests() def __A ( self ) -> str: # YOLOS does not use inputs_embeds pass def __A ( self ) -> Dict: _UpperCAmelCase , _UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _UpperCAmelCase = model_class(snake_case_ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) _UpperCAmelCase = model.get_output_embeddings() self.assertTrue(x is None or isinstance(snake_case_ , nn.Linear ) ) def __A ( self ) -> str: _UpperCAmelCase , _UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _UpperCAmelCase = model_class(snake_case_ ) _UpperCAmelCase = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _UpperCAmelCase = [*signature.parameters.keys()] _UpperCAmelCase = ["pixel_values"] self.assertListEqual(arg_names[:1] , snake_case_ ) def __A ( self ) -> List[str]: _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case_ ) def __A ( self ) -> str: _UpperCAmelCase , _UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() _UpperCAmelCase = True # in YOLOS, the seq_len is different _UpperCAmelCase = self.model_tester.expected_seq_len for model_class in self.all_model_classes: _UpperCAmelCase = True _UpperCAmelCase = False _UpperCAmelCase = True _UpperCAmelCase = model_class(snake_case_ ) model.to(snake_case_ ) model.eval() with torch.no_grad(): _UpperCAmelCase = model(**self._prepare_for_class(snake_case_ , snake_case_ ) ) _UpperCAmelCase = outputs.attentions self.assertEqual(len(snake_case_ ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] _UpperCAmelCase = True _UpperCAmelCase = model_class(snake_case_ ) model.to(snake_case_ ) model.eval() with torch.no_grad(): _UpperCAmelCase = model(**self._prepare_for_class(snake_case_ , snake_case_ ) ) _UpperCAmelCase = outputs.attentions self.assertEqual(len(snake_case_ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) _UpperCAmelCase = len(snake_case_ ) # Check attention is always last and order is fine _UpperCAmelCase = True _UpperCAmelCase = True _UpperCAmelCase = model_class(snake_case_ ) model.to(snake_case_ ) model.eval() with torch.no_grad(): _UpperCAmelCase = model(**self._prepare_for_class(snake_case_ , snake_case_ ) ) _UpperCAmelCase = 1 self.assertEqual(out_len + added_hidden_states , len(snake_case_ ) ) _UpperCAmelCase = outputs.attentions self.assertEqual(len(snake_case_ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) def __A ( self ) -> Any: def check_hidden_states_output(snake_case_ , snake_case_ , snake_case_ ): _UpperCAmelCase = model_class(snake_case_ ) model.to(snake_case_ ) model.eval() with torch.no_grad(): _UpperCAmelCase = model(**self._prepare_for_class(snake_case_ , snake_case_ ) ) _UpperCAmelCase = outputs.hidden_states _UpperCAmelCase = getattr( self.model_tester , "expected_num_hidden_layers" , self.model_tester.num_hidden_layers + 1 ) self.assertEqual(len(snake_case_ ) , snake_case_ ) # YOLOS has a different seq_length _UpperCAmelCase = self.model_tester.expected_seq_len self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [seq_length, self.model_tester.hidden_size] , ) _UpperCAmelCase , _UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _UpperCAmelCase = True check_hidden_states_output(snake_case_ , snake_case_ , snake_case_ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _UpperCAmelCase = True check_hidden_states_output(snake_case_ , snake_case_ , snake_case_ ) def __A ( self ) -> Optional[Any]: _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_object_detection(*snake_case_ ) @slow def __A ( self ) -> Optional[Any]: for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _UpperCAmelCase = YolosModel.from_pretrained(snake_case_ ) self.assertIsNotNone(snake_case_ ) def A__ ( ) -> str: '''simple docstring''' _UpperCAmelCase = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class a ( unittest.TestCase ): """simple docstring""" @cached_property def __A ( self ) -> Union[str, Any]: return AutoImageProcessor.from_pretrained("hustvl/yolos-small" ) if is_vision_available() else None @slow def __A ( self ) -> Optional[int]: _UpperCAmelCase = YolosForObjectDetection.from_pretrained("hustvl/yolos-small" ).to(snake_case_ ) _UpperCAmelCase = self.default_image_processor _UpperCAmelCase = prepare_img() _UpperCAmelCase = image_processor(images=snake_case_ , return_tensors="pt" ).to(snake_case_ ) # forward pass with torch.no_grad(): _UpperCAmelCase = model(inputs.pixel_values ) # verify outputs _UpperCAmelCase = torch.Size((1, 100, 92) ) self.assertEqual(outputs.logits.shape , snake_case_ ) _UpperCAmelCase = torch.tensor( [[-24.02_48, -10.30_24, -14.82_90], [-42.03_92, -16.82_00, -27.43_34], [-27.27_43, -11.81_54, -18.71_48]] , device=snake_case_ , ) _UpperCAmelCase = torch.tensor( [[0.25_59, 0.54_55, 0.47_06], [0.29_89, 0.72_79, 0.18_75], [0.77_32, 0.40_17, 0.44_62]] , device=snake_case_ ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , snake_case_ , atol=1e-4 ) ) self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3] , snake_case_ , atol=1e-4 ) ) # verify postprocessing _UpperCAmelCase = image_processor.post_process_object_detection( snake_case_ , threshold=0.3 , target_sizes=[image.size[::-1]] )[0] _UpperCAmelCase = torch.tensor([0.99_94, 0.97_90, 0.99_64, 0.99_72, 0.98_61] ).to(snake_case_ ) _UpperCAmelCase = [75, 75, 17, 63, 17] _UpperCAmelCase = torch.tensor([3_35.06_09, 79.38_48, 3_75.42_16, 1_87.24_95] ).to(snake_case_ ) self.assertEqual(len(results["scores"] ) , 5 ) self.assertTrue(torch.allclose(results["scores"] , snake_case_ , atol=1e-4 ) ) self.assertSequenceEqual(results["labels"].tolist() , snake_case_ ) self.assertTrue(torch.allclose(results["boxes"][0, :] , snake_case_ ) )
579
"""simple docstring""" import unittest from transformers import LiltConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( LiltForQuestionAnswering, LiltForSequenceClassification, LiltForTokenClassification, LiltModel, ) from transformers.models.lilt.modeling_lilt import LILT_PRETRAINED_MODEL_ARCHIVE_LIST class a : """simple docstring""" def __init__( self , snake_case_ , snake_case_=13 , snake_case_=7 , snake_case_=True , snake_case_=True , snake_case_=True , snake_case_=True , snake_case_=99 , snake_case_=24 , snake_case_=2 , snake_case_=6 , snake_case_=37 , snake_case_="gelu" , snake_case_=0.1 , snake_case_=0.1 , snake_case_=512 , snake_case_=16 , snake_case_=2 , snake_case_=0.02 , snake_case_=3 , snake_case_=None , snake_case_=1000 , ) -> Optional[int]: _UpperCAmelCase = parent _UpperCAmelCase = batch_size _UpperCAmelCase = seq_length _UpperCAmelCase = is_training _UpperCAmelCase = use_input_mask _UpperCAmelCase = use_token_type_ids _UpperCAmelCase = use_labels _UpperCAmelCase = vocab_size _UpperCAmelCase = hidden_size _UpperCAmelCase = num_hidden_layers _UpperCAmelCase = num_attention_heads _UpperCAmelCase = intermediate_size _UpperCAmelCase = hidden_act _UpperCAmelCase = hidden_dropout_prob _UpperCAmelCase = attention_probs_dropout_prob _UpperCAmelCase = max_position_embeddings _UpperCAmelCase = type_vocab_size _UpperCAmelCase = type_sequence_label_size _UpperCAmelCase = initializer_range _UpperCAmelCase = num_labels _UpperCAmelCase = scope _UpperCAmelCase = range_bbox def __A ( self ) -> Dict: _UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length, 4] , self.range_bbox ) # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: _UpperCAmelCase = bbox[i, j, 3] _UpperCAmelCase = bbox[i, j, 1] _UpperCAmelCase = t if bbox[i, j, 2] < bbox[i, j, 0]: _UpperCAmelCase = bbox[i, j, 2] _UpperCAmelCase = bbox[i, j, 0] _UpperCAmelCase = t _UpperCAmelCase = None if self.use_input_mask: _UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) _UpperCAmelCase = None if self.use_token_type_ids: _UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _UpperCAmelCase = None _UpperCAmelCase = None if self.use_labels: _UpperCAmelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _UpperCAmelCase = self.get_config() return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels def __A ( self ) -> Dict: return LiltConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , ) def __A ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , ) -> Any: _UpperCAmelCase = LiltModel(config=snake_case_ ) model.to(snake_case_ ) model.eval() _UpperCAmelCase = model(snake_case_ , bbox=snake_case_ , attention_mask=snake_case_ , token_type_ids=snake_case_ ) _UpperCAmelCase = model(snake_case_ , bbox=snake_case_ , token_type_ids=snake_case_ ) _UpperCAmelCase = model(snake_case_ , bbox=snake_case_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def __A ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , ) -> Optional[Any]: _UpperCAmelCase = self.num_labels _UpperCAmelCase = LiltForTokenClassification(config=snake_case_ ) model.to(snake_case_ ) model.eval() _UpperCAmelCase = model( snake_case_ , bbox=snake_case_ , attention_mask=snake_case_ , token_type_ids=snake_case_ , labels=snake_case_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __A ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , ) -> str: _UpperCAmelCase = LiltForQuestionAnswering(config=snake_case_ ) model.to(snake_case_ ) model.eval() _UpperCAmelCase = model( snake_case_ , bbox=snake_case_ , attention_mask=snake_case_ , token_type_ids=snake_case_ , start_positions=snake_case_ , end_positions=snake_case_ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __A ( self ) -> Optional[int]: _UpperCAmelCase = self.prepare_config_and_inputs() ( ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ) = config_and_inputs _UpperCAmelCase = { "input_ids": input_ids, "bbox": bbox, "token_type_ids": token_type_ids, "attention_mask": input_mask, } return config, inputs_dict @require_torch class a ( _SCREAMING_SNAKE_CASE, _SCREAMING_SNAKE_CASE, _SCREAMING_SNAKE_CASE, unittest.TestCase ): """simple docstring""" A__ : List[Any] = ( ( LiltModel, LiltForSequenceClassification, LiltForTokenClassification, LiltForQuestionAnswering, ) if is_torch_available() else () ) A__ : List[str] = ( { "feature-extraction": LiltModel, "question-answering": LiltForQuestionAnswering, "text-classification": LiltForSequenceClassification, "token-classification": LiltForTokenClassification, "zero-shot": LiltForSequenceClassification, } if is_torch_available() else {} ) A__ : int = False A__ : Tuple = False def __A ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ ) -> Tuple: return True def __A ( self ) -> str: _UpperCAmelCase = LiltModelTester(self ) _UpperCAmelCase = ConfigTester(self , config_class=snake_case_ , hidden_size=37 ) def __A ( self ) -> Tuple: self.config_tester.run_common_tests() def __A ( self ) -> List[Any]: _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case_ ) def __A ( self ) -> str: _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: _UpperCAmelCase = type self.model_tester.create_and_check_model(*snake_case_ ) def __A ( self ) -> Any: _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*snake_case_ ) def __A ( self ) -> List[Any]: _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*snake_case_ ) @slow def __A ( self ) -> Optional[Any]: for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _UpperCAmelCase = LiltModel.from_pretrained(snake_case_ ) self.assertIsNotNone(snake_case_ ) @require_torch @slow class a ( unittest.TestCase ): """simple docstring""" def __A ( self ) -> Optional[Any]: _UpperCAmelCase = LiltModel.from_pretrained("SCUT-DLVCLab/lilt-roberta-en-base" ).to(snake_case_ ) _UpperCAmelCase = torch.tensor([[1, 2]] , device=snake_case_ ) _UpperCAmelCase = torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]] , device=snake_case_ ) # forward pass with torch.no_grad(): _UpperCAmelCase = model(input_ids=snake_case_ , bbox=snake_case_ ) _UpperCAmelCase = torch.Size([1, 2, 768] ) _UpperCAmelCase = torch.tensor( [[-0.06_53, 0.09_50, -0.00_61], [-0.05_45, 0.09_26, -0.03_24]] , device=snake_case_ , ) self.assertTrue(outputs.last_hidden_state.shape , snake_case_ ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :, :3] , snake_case_ , atol=1e-3 ) )
579
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
194
import argparse import json import os from tensorflow.core.protobuf.saved_model_pba import SavedModel # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_copies.py a_ : Optional[int] = '.' # Internal TensorFlow ops that can be safely ignored (mostly specific to a saved model) a_ : List[str] = [ 'Assert', 'AssignVariableOp', 'EmptyTensorList', 'MergeV2Checkpoints', 'ReadVariableOp', 'ResourceGather', 'RestoreV2', 'SaveV2', 'ShardedFilename', 'StatefulPartitionedCall', 'StaticRegexFullMatch', 'VarHandleOp', ] def __a ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ): a__ = SavedModel() a__ = [] with open(os.path.join(__UpperCAmelCase , '''utils''' , '''tf_ops''' , '''onnx.json''' ) ) as f: a__ = json.load(__UpperCAmelCase )['''opsets'''] for i in range(1 , opset + 1 ): onnx_ops.extend(onnx_opsets[str(__UpperCAmelCase )] ) with open(__UpperCAmelCase , '''rb''' ) as f: saved_model.ParseFromString(f.read() ) a__ = set() # Iterate over every metagraph in case there is more than one (a saved model can contain multiple graphs) for meta_graph in saved_model.meta_graphs: # Add operations in the graph definition model_op_names.update(node.op for node in meta_graph.graph_def.node ) # Go through the functions in the graph definition for func in meta_graph.graph_def.library.function: # Add operations in each function model_op_names.update(node.op for node in func.node_def ) # Convert to list, sorted if you want a__ = sorted(__UpperCAmelCase ) a__ = [] for op in model_op_names: if op not in onnx_ops and op not in INTERNAL_OPS: incompatible_ops.append(__UpperCAmelCase ) if strict and len(__UpperCAmelCase ) > 0: raise Exception(f"Found the following incompatible ops for the opset {opset}:\n" + incompatible_ops ) elif len(__UpperCAmelCase ) > 0: print(f"Found the following incompatible ops for the opset {opset}:" ) print(*__UpperCAmelCase , sep='''\n''' ) else: print(f"The saved model {saved_model_path} can properly be converted with ONNX." ) if __name__ == "__main__": a_ : Dict = argparse.ArgumentParser() parser.add_argument('--saved_model_path', help='Path of the saved model to check (the .pb file).') parser.add_argument( '--opset', default=12, type=int, help='The ONNX opset against which the model has to be tested.' ) parser.add_argument( '--framework', choices=['onnx'], default='onnx', help='Frameworks against which to test the saved model.' ) parser.add_argument( '--strict', action='store_true', help='Whether make the checking strict (raise errors) or not (raise warnings)' ) a_ : str = parser.parse_args() if args.framework == "onnx": onnx_compliancy(args.saved_model_path, args.strict, args.opset)
194
1
def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_: int , SCREAMING_SNAKE_CASE_: int ) -> int: '''simple docstring''' return int((input_a, input_a).count(0 ) == 0 ) def lowerCAmelCase__ ( ) -> None: '''simple docstring''' assert and_gate(0 , 0 ) == 0 assert and_gate(0 , 1 ) == 0 assert and_gate(1 , 0 ) == 0 assert and_gate(1 , 1 ) == 1 if __name__ == "__main__": test_and_gate() print(and_gate(1, 0)) print(and_gate(0, 0)) print(and_gate(0, 1)) print(and_gate(1, 1))
626
from __future__ import annotations def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_: list[int] , SCREAMING_SNAKE_CASE_: list[int] , SCREAMING_SNAKE_CASE_: list[int] , SCREAMING_SNAKE_CASE_: list[list[str]] , SCREAMING_SNAKE_CASE_: int , ) -> None: '''simple docstring''' A__ = len(SCREAMING_SNAKE_CASE_ ) # If row is equal to the size of the board it means there are a queen in each row in # the current board (possible_board) if row == n: # We convert the variable possible_board that looks like this: [1, 3, 0, 2] to # this: ['. Q . . ', '. . . Q ', 'Q . . . ', '. . Q . '] boards.append([". " * i + "Q " + ". " * (n - 1 - i) for i in possible_board] ) return # We iterate each column in the row to find all possible results in each row for col in range(SCREAMING_SNAKE_CASE_ ): # We apply that we learned previously. First we check that in the current board # (possible_board) there are not other same value because if there is it means # that there are a collision in vertical. Then we apply the two formulas we # learned before: # # 45º: y - x = b or 45: row - col = b # 135º: y + x = b or row + col = b. # # And we verify if the results of this two formulas not exist in their variables # respectively. (diagonal_right_collisions, diagonal_left_collisions) # # If any or these are True it means there is a collision so we continue to the # next value in the for loop. if ( col in possible_board or row - col in diagonal_right_collisions or row + col in diagonal_left_collisions ): continue # If it is False we call dfs function again and we update the inputs depth_first_search( [*possible_board, col] , [*diagonal_right_collisions, row - col] , [*diagonal_left_collisions, row + col] , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_: int ) -> None: '''simple docstring''' A__ = [] depth_first_search([] , [] , [] , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # Print all the boards for board in boards: for column in board: print(SCREAMING_SNAKE_CASE_ ) print("" ) print(len(SCREAMING_SNAKE_CASE_ ) , "solutions were found." ) if __name__ == "__main__": import doctest doctest.testmod() n_queens_solution(4)
626
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging __magic_name__ : Dict = logging.get_logger(__name__) __magic_name__ : List[str] = { """microsoft/swinv2-tiny-patch4-window8-256""": ( """https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json""" ), } class lowercase__ ( __SCREAMING_SNAKE_CASE ): """simple docstring""" __lowerCAmelCase : List[str] = """swinv2""" __lowerCAmelCase : Optional[Any] = { """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers""", } def __init__( self , _A=2_2_4 , _A=4 , _A=3 , _A=9_6 , _A=[2, 2, 6, 2] , _A=[3, 6, 1_2, 2_4] , _A=7 , _A=4.0 , _A=True , _A=0.0 , _A=0.0 , _A=0.1 , _A="gelu" , _A=False , _A=0.02 , _A=1e-5 , _A=3_2 , **_A , ): '''simple docstring''' super().__init__(**_A ) UpperCamelCase : Optional[Any] = image_size UpperCamelCase : Tuple = patch_size UpperCamelCase : str = num_channels UpperCamelCase : Dict = embed_dim UpperCamelCase : List[str] = depths UpperCamelCase : Optional[Any] = len(_A ) UpperCamelCase : Optional[int] = num_heads UpperCamelCase : Optional[int] = window_size UpperCamelCase : List[Any] = mlp_ratio UpperCamelCase : str = qkv_bias UpperCamelCase : str = hidden_dropout_prob UpperCamelCase : Tuple = attention_probs_dropout_prob UpperCamelCase : List[Any] = drop_path_rate UpperCamelCase : str = hidden_act UpperCamelCase : Optional[Any] = use_absolute_embeddings UpperCamelCase : Dict = layer_norm_eps UpperCamelCase : Dict = initializer_range UpperCamelCase : str = encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model UpperCamelCase : Any = int(embed_dim * 2 ** (len(_A ) - 1) ) UpperCamelCase : str = (0, 0, 0, 0)
102
def _lowercase ( a__ : list ) -> list: """simple docstring""" if any(not isinstance(a__ , a__ ) or x < 0 for x in sequence ): raise TypeError("Sequence must be list of non-negative integers" ) for _ in range(len(a__ ) ): for i, (rod_upper, rod_lower) in enumerate(zip(a__ , sequence[1:] ) ): if rod_upper > rod_lower: sequence[i] -= rod_upper - rod_lower sequence[i + 1] += rod_upper - rod_lower return sequence if __name__ == "__main__": assert bead_sort([5, 4, 3, 2, 1]) == [1, 2, 3, 4, 5] assert bead_sort([7, 9, 4, 3, 5]) == [3, 4, 5, 7, 9]
147
0
from numpy import exp, pi, sqrt def UpperCamelCase__ ( lowerCAmelCase__ ,lowerCAmelCase__ = 0.0 ,lowerCAmelCase__ = 1.0 ): return 1 / sqrt(2 * pi * sigma**2 ) * exp(-((x - mu) ** 2) / (2 * sigma**2) ) if __name__ == "__main__": import doctest doctest.testmod()
72
# using dfs for finding eulerian path traversal def UpperCamelCase__ ( lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__=None ): lowercase = (path or []) + [u] for v in graph[u]: if visited_edge[u][v] is False: lowercase , lowercase = True, True lowercase = dfs(lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ) return path def UpperCamelCase__ ( lowerCAmelCase__ ,lowerCAmelCase__ ): lowercase = 0 lowercase = -1 for i in range(lowerCAmelCase__ ): if i not in graph.keys(): continue if len(graph[i] ) % 2 == 1: odd_degree_nodes += 1 lowercase = i if odd_degree_nodes == 0: return 1, odd_node if odd_degree_nodes == 2: return 2, odd_node return 3, odd_node def UpperCamelCase__ ( lowerCAmelCase__ ,lowerCAmelCase__ ): lowercase = [[False for _ in range(max_node + 1 )] for _ in range(max_node + 1 )] lowercase , lowercase = check_circuit_or_path(lowerCAmelCase__ ,lowerCAmelCase__ ) if check == 3: print("""graph is not Eulerian""" ) print("""no path""" ) return lowercase = 1 if check == 2: lowercase = odd_node print("""graph has a Euler path""" ) if check == 1: print("""graph has a Euler cycle""" ) lowercase = dfs(lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ) print(lowerCAmelCase__ ) def UpperCamelCase__ ( ): lowercase = {1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]} lowercase = {1: [2, 3, 4, 5], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [1, 4]} lowercase = {1: [2, 3, 4], 2: [1, 3, 4], 3: [1, 2], 4: [1, 2, 5], 5: [4]} lowercase = {1: [2, 3], 2: [1, 3], 3: [1, 2]} lowercase = { 1: [], 2: [] # all degree is zero } lowercase = 10 check_euler(lowerCAmelCase__ ,lowerCAmelCase__ ) check_euler(lowerCAmelCase__ ,lowerCAmelCase__ ) check_euler(lowerCAmelCase__ ,lowerCAmelCase__ ) check_euler(lowerCAmelCase__ ,lowerCAmelCase__ ) check_euler(lowerCAmelCase__ ,lowerCAmelCase__ ) if __name__ == "__main__": main()
72
1
"""simple docstring""" import torch from diffusers import DDPMScheduler from .test_schedulers import SchedulerCommonTest class __snake_case ( _lowercase): snake_case__ : Any = (DDPMScheduler,) def SCREAMING_SNAKE_CASE ( self : str , **__lowerCAmelCase : Dict ): """simple docstring""" _lowerCamelCase : Optional[int] = { '''num_train_timesteps''': 1_0_0_0, '''beta_start''': 0.00_01, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', '''variance_type''': '''fixed_small''', '''clip_sample''': True, } config.update(**__lowerCAmelCase ) return config def SCREAMING_SNAKE_CASE ( self : Tuple ): """simple docstring""" for timesteps in [1, 5, 1_0_0, 1_0_0_0]: self.check_over_configs(num_train_timesteps=__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : int ): """simple docstring""" for beta_start, beta_end in zip([0.00_01, 0.0_01, 0.01, 0.1] , [0.0_02, 0.02, 0.2, 2] ): self.check_over_configs(beta_start=__lowerCAmelCase , beta_end=__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : Any ): """simple docstring""" for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : int ): """simple docstring""" for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : List[Any] ): """simple docstring""" for clip_sample in [True, False]: self.check_over_configs(clip_sample=__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : Tuple ): """simple docstring""" self.check_over_configs(thresholding=__lowerCAmelCase ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=__lowerCAmelCase , prediction_type=__lowerCAmelCase , sample_max_value=__lowerCAmelCase , ) def SCREAMING_SNAKE_CASE ( self : List[str] ): """simple docstring""" for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ): """simple docstring""" for t in [0, 5_0_0, 9_9_9]: self.check_over_forward(time_step=__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : int ): """simple docstring""" _lowerCamelCase : Tuple = self.scheduler_classes[0] _lowerCamelCase : int = self.get_scheduler_config() _lowerCamelCase : Any = scheduler_class(**__lowerCAmelCase ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1E-5 assert torch.sum(torch.abs(scheduler._get_variance(4_8_7 ) - 0.0_09_79 ) ) < 1E-5 assert torch.sum(torch.abs(scheduler._get_variance(9_9_9 ) - 0.02 ) ) < 1E-5 def SCREAMING_SNAKE_CASE ( self : List[Any] ): """simple docstring""" _lowerCamelCase : str = self.scheduler_classes[0] _lowerCamelCase : List[Any] = self.get_scheduler_config() _lowerCamelCase : Optional[int] = scheduler_class(**__lowerCAmelCase ) _lowerCamelCase : Tuple = len(__lowerCAmelCase ) _lowerCamelCase : Dict = self.dummy_model() _lowerCamelCase : str = self.dummy_sample_deter _lowerCamelCase : Any = torch.manual_seed(0 ) for t in reversed(range(__lowerCAmelCase ) ): # 1. predict noise residual _lowerCamelCase : Optional[int] = model(__lowerCAmelCase , __lowerCAmelCase ) # 2. predict previous mean of sample x_t-1 _lowerCamelCase : Dict = scheduler.step(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , generator=__lowerCAmelCase ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance _lowerCamelCase : Dict = pred_prev_sample _lowerCamelCase : str = torch.sum(torch.abs(__lowerCAmelCase ) ) _lowerCamelCase : str = torch.mean(torch.abs(__lowerCAmelCase ) ) assert abs(result_sum.item() - 2_58.96_06 ) < 1E-2 assert abs(result_mean.item() - 0.33_72 ) < 1E-3 def SCREAMING_SNAKE_CASE ( self : Optional[Any] ): """simple docstring""" _lowerCamelCase : List[str] = self.scheduler_classes[0] _lowerCamelCase : Union[str, Any] = self.get_scheduler_config(prediction_type='''v_prediction''' ) _lowerCamelCase : Union[str, Any] = scheduler_class(**__lowerCAmelCase ) _lowerCamelCase : Dict = len(__lowerCAmelCase ) _lowerCamelCase : str = self.dummy_model() _lowerCamelCase : Optional[Any] = self.dummy_sample_deter _lowerCamelCase : Dict = torch.manual_seed(0 ) for t in reversed(range(__lowerCAmelCase ) ): # 1. predict noise residual _lowerCamelCase : Union[str, Any] = model(__lowerCAmelCase , __lowerCAmelCase ) # 2. predict previous mean of sample x_t-1 _lowerCamelCase : List[Any] = scheduler.step(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , generator=__lowerCAmelCase ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance _lowerCamelCase : Any = pred_prev_sample _lowerCamelCase : List[str] = torch.sum(torch.abs(__lowerCAmelCase ) ) _lowerCamelCase : str = torch.mean(torch.abs(__lowerCAmelCase ) ) assert abs(result_sum.item() - 2_02.02_96 ) < 1E-2 assert abs(result_mean.item() - 0.26_31 ) < 1E-3 def SCREAMING_SNAKE_CASE ( self : Optional[int] ): """simple docstring""" _lowerCamelCase : List[str] = self.scheduler_classes[0] _lowerCamelCase : List[str] = self.get_scheduler_config() _lowerCamelCase : Dict = scheduler_class(**__lowerCAmelCase ) _lowerCamelCase : List[str] = [1_0_0, 8_7, 5_0, 1, 0] scheduler.set_timesteps(timesteps=__lowerCAmelCase ) _lowerCamelCase : Union[str, Any] = scheduler.timesteps for i, timestep in enumerate(__lowerCAmelCase ): if i == len(__lowerCAmelCase ) - 1: _lowerCamelCase : str = -1 else: _lowerCamelCase : List[Any] = timesteps[i + 1] _lowerCamelCase : Optional[int] = scheduler.previous_timestep(__lowerCAmelCase ) _lowerCamelCase : Tuple = prev_t.item() self.assertEqual(__lowerCAmelCase , __lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : int ): """simple docstring""" _lowerCamelCase : str = self.scheduler_classes[0] _lowerCamelCase : Optional[int] = self.get_scheduler_config() _lowerCamelCase : str = scheduler_class(**__lowerCAmelCase ) _lowerCamelCase : Any = [1_0_0, 8_7, 5_0, 5_1, 0] with self.assertRaises(__lowerCAmelCase , msg='''`custom_timesteps` must be in descending order.''' ): scheduler.set_timesteps(timesteps=__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : str ): """simple docstring""" _lowerCamelCase : Tuple = self.scheduler_classes[0] _lowerCamelCase : Optional[Any] = self.get_scheduler_config() _lowerCamelCase : List[str] = scheduler_class(**__lowerCAmelCase ) _lowerCamelCase : List[Any] = [1_0_0, 8_7, 5_0, 1, 0] _lowerCamelCase : List[str] = len(__lowerCAmelCase ) with self.assertRaises(__lowerCAmelCase , msg='''Can only pass one of `num_inference_steps` or `custom_timesteps`.''' ): scheduler.set_timesteps(num_inference_steps=__lowerCAmelCase , timesteps=__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : str ): """simple docstring""" _lowerCamelCase : Optional[Any] = self.scheduler_classes[0] _lowerCamelCase : str = self.get_scheduler_config() _lowerCamelCase : Optional[Any] = scheduler_class(**__lowerCAmelCase ) _lowerCamelCase : int = [scheduler.config.num_train_timesteps] with self.assertRaises( __lowerCAmelCase , msg='''`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}''' , ): scheduler.set_timesteps(timesteps=__lowerCAmelCase )
83
"""simple docstring""" SCREAMING_SNAKE_CASE__ : Dict =[ 999, 800, 799, 600, 599, 500, 400, 399, 377, 355, 333, 311, 288, 266, 244, 222, 200, 199, 177, 155, 133, 111, 88, 66, 44, 22, 0, ] SCREAMING_SNAKE_CASE__ : List[Any] =[ 999, 976, 952, 928, 905, 882, 858, 857, 810, 762, 715, 714, 572, 429, 428, 286, 285, 238, 190, 143, 142, 118, 95, 71, 47, 24, 0, ] SCREAMING_SNAKE_CASE__ : Optional[int] =[ 999, 988, 977, 966, 955, 944, 933, 922, 911, 900, 899, 879, 859, 840, 820, 800, 799, 766, 733, 700, 699, 650, 600, 599, 500, 499, 400, 399, 350, 300, 299, 266, 233, 200, 199, 179, 159, 140, 120, 100, 99, 88, 77, 66, 55, 44, 33, 22, 11, 0, ] SCREAMING_SNAKE_CASE__ : List[Any] =[ 999, 995, 992, 989, 985, 981, 978, 975, 971, 967, 964, 961, 957, 956, 951, 947, 942, 937, 933, 928, 923, 919, 914, 913, 908, 903, 897, 892, 887, 881, 876, 871, 870, 864, 858, 852, 846, 840, 834, 828, 827, 820, 813, 806, 799, 792, 785, 784, 777, 770, 763, 756, 749, 742, 741, 733, 724, 716, 707, 699, 698, 688, 677, 666, 656, 655, 645, 634, 623, 613, 612, 598, 584, 570, 569, 555, 541, 527, 526, 505, 484, 483, 462, 440, 439, 396, 395, 352, 351, 308, 307, 264, 263, 220, 219, 176, 132, 88, 44, 0, ] SCREAMING_SNAKE_CASE__ : Union[str, Any] =[ 999, 997, 995, 992, 990, 988, 986, 984, 981, 979, 977, 975, 972, 970, 968, 966, 964, 961, 959, 957, 956, 954, 951, 949, 946, 944, 941, 939, 936, 934, 931, 929, 926, 924, 921, 919, 916, 914, 913, 910, 907, 905, 902, 899, 896, 893, 891, 888, 885, 882, 879, 877, 874, 871, 870, 867, 864, 861, 858, 855, 852, 849, 846, 843, 840, 837, 834, 831, 828, 827, 824, 821, 817, 814, 811, 808, 804, 801, 798, 795, 791, 788, 785, 784, 780, 777, 774, 770, 766, 763, 760, 756, 752, 749, 746, 742, 741, 737, 733, 730, 726, 722, 718, 714, 710, 707, 703, 699, 698, 694, 690, 685, 681, 677, 673, 669, 664, 660, 656, 655, 650, 646, 641, 636, 632, 627, 622, 618, 613, 612, 607, 602, 596, 591, 586, 580, 575, 570, 569, 563, 557, 551, 545, 539, 533, 527, 526, 519, 512, 505, 498, 491, 484, 483, 474, 466, 457, 449, 440, 439, 428, 418, 407, 396, 395, 381, 366, 352, 351, 330, 308, 307, 286, 264, 263, 242, 220, 219, 176, 175, 132, 131, 88, 44, 0, ] SCREAMING_SNAKE_CASE__ : Optional[int] =[ 999, 991, 982, 974, 966, 958, 950, 941, 933, 925, 916, 908, 900, 899, 874, 850, 825, 800, 799, 700, 600, 500, 400, 300, 200, 100, 0, ] SCREAMING_SNAKE_CASE__ : Optional[Any] =[ 999, 992, 985, 978, 971, 964, 957, 949, 942, 935, 928, 921, 914, 907, 900, 899, 879, 859, 840, 820, 800, 799, 766, 733, 700, 699, 650, 600, 599, 500, 499, 400, 399, 300, 299, 200, 199, 100, 99, 0, ] SCREAMING_SNAKE_CASE__ : Optional[Any] =[ 999, 996, 992, 989, 985, 982, 979, 975, 972, 968, 965, 961, 958, 955, 951, 948, 944, 941, 938, 934, 931, 927, 924, 920, 917, 914, 910, 907, 903, 900, 899, 891, 884, 876, 869, 861, 853, 846, 838, 830, 823, 815, 808, 800, 799, 788, 777, 766, 755, 744, 733, 722, 711, 700, 699, 688, 677, 666, 655, 644, 633, 622, 611, 600, 599, 585, 571, 557, 542, 528, 514, 500, 499, 485, 471, 457, 442, 428, 414, 400, 399, 379, 359, 340, 320, 300, 299, 279, 259, 240, 220, 200, 199, 166, 133, 100, 99, 66, 33, 0, ]
434
0
"""simple docstring""" from typing import Optional, Union import torch from torch import nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention from ...modeling_utils import PreTrainedModel from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging from .configuration_mobilenet_va import MobileNetVaConfig a : Dict = logging.get_logger(__name__) # General docstring a : Optional[Any] = '''MobileNetV1Config''' # Base docstring a : Tuple = '''google/mobilenet_v1_1.0_224''' a : Dict = [1, 1024, 7, 7] # Image classification docstring a : str = '''google/mobilenet_v1_1.0_224''' a : Any = '''tabby, tabby cat''' a : List[Any] = [ '''google/mobilenet_v1_1.0_224''', '''google/mobilenet_v1_0.75_192''', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 ] def _SCREAMING_SNAKE_CASE ( _lowercase : List[Any] , _lowercase : Optional[Any] , _lowercase : Dict=None ) ->int: '''simple docstring''' a : Union[str, Any] = {} if isinstance(_lowercase , _lowercase ): a : List[Any] = model.mobilenet_va else: a : List[str] = model a : int = "MobilenetV1/Conv2d_0/" a : str = backbone.conv_stem.convolution.weight a : int = backbone.conv_stem.normalization.bias a : Optional[int] = backbone.conv_stem.normalization.weight a : List[Any] = backbone.conv_stem.normalization.running_mean a : List[str] = backbone.conv_stem.normalization.running_var for i in range(13 ): a : Dict = i + 1 a : Union[str, Any] = i * 2 a : Union[str, Any] = backbone.layer[pt_index] a : Optional[int] = F"""MobilenetV1/Conv2d_{tf_index}_depthwise/""" a : str = pointer.convolution.weight a : str = pointer.normalization.bias a : Optional[Any] = pointer.normalization.weight a : List[str] = pointer.normalization.running_mean a : Optional[int] = pointer.normalization.running_var a : str = backbone.layer[pt_index + 1] a : List[str] = F"""MobilenetV1/Conv2d_{tf_index}_pointwise/""" a : Tuple = pointer.convolution.weight a : Tuple = pointer.normalization.bias a : int = pointer.normalization.weight a : Dict = pointer.normalization.running_mean a : List[Any] = pointer.normalization.running_var if isinstance(_lowercase , _lowercase ): a : List[Any] = "MobilenetV1/Logits/Conv2d_1c_1x1/" a : int = model.classifier.weight a : Dict = model.classifier.bias return tf_to_pt_map def _SCREAMING_SNAKE_CASE ( _lowercase : str , _lowercase : int , _lowercase : Tuple ) ->List[Any]: '''simple docstring''' try: import numpy as np import tensorflow as tf except ImportError: logger.error( "Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see " "https://www.tensorflow.org/install/ for installation instructions." ) raise # Load weights from TF model a : Any = tf.train.list_variables(_lowercase ) a : Tuple = {} for name, shape in init_vars: logger.info(F"""Loading TF weight {name} with shape {shape}""" ) a : int = tf.train.load_variable(_lowercase , _lowercase ) a : Union[str, Any] = array # Build TF to PyTorch weights loading map a : List[str] = _build_tf_to_pytorch_map(_lowercase , _lowercase , _lowercase ) for name, pointer in tf_to_pt_map.items(): logger.info(F"""Importing {name}""" ) if name not in tf_weights: logger.info(F"""{name} not in tf pre-trained weights, skipping""" ) continue a : int = tf_weights[name] if "depthwise_weights" in name: logger.info("Transposing depthwise" ) a : List[Any] = np.transpose(_lowercase , (2, 3, 0, 1) ) elif "weights" in name: logger.info("Transposing" ) if len(pointer.shape ) == 2: # copying into linear layer a : List[str] = array.squeeze().transpose() else: a : Dict = np.transpose(_lowercase , (3, 2, 0, 1) ) if pointer.shape != array.shape: raise ValueError(F"""Pointer shape {pointer.shape} and array shape {array.shape} mismatched""" ) logger.info(F"""Initialize PyTorch weight {name} {array.shape}""" ) a : List[Any] = torch.from_numpy(_lowercase ) tf_weights.pop(_lowercase , _lowercase ) tf_weights.pop(name + "/RMSProp" , _lowercase ) tf_weights.pop(name + "/RMSProp_1" , _lowercase ) tf_weights.pop(name + "/ExponentialMovingAverage" , _lowercase ) logger.info(F"""Weights not copied to PyTorch model: {', '.join(tf_weights.keys() )}""" ) return model def _SCREAMING_SNAKE_CASE ( _lowercase : torch.Tensor , _lowercase : nn.Convad ) ->torch.Tensor: '''simple docstring''' a, a : Tuple = features.shape[-2:] a, a : List[Any] = conv_layer.stride a, a : Any = conv_layer.kernel_size if in_height % stride_height == 0: a : Union[str, Any] = max(kernel_height - stride_height , 0 ) else: a : int = max(kernel_height - (in_height % stride_height) , 0 ) if in_width % stride_width == 0: a : Union[str, Any] = max(kernel_width - stride_width , 0 ) else: a : Optional[Any] = max(kernel_width - (in_width % stride_width) , 0 ) a : Tuple = pad_along_width // 2 a : Union[str, Any] = pad_along_width - pad_left a : str = pad_along_height // 2 a : Tuple = pad_along_height - pad_top a : Any = (pad_left, pad_right, pad_top, pad_bottom) return nn.functional.pad(_lowercase , _lowercase , "constant" , 0.0 ) class __UpperCamelCase ( nn.Module ): def __init__( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ = 1 , lowerCAmelCase__ = 1 , lowerCAmelCase__ = False , lowerCAmelCase__ = True , lowerCAmelCase__ = True , ) -> None: super().__init__() a : str = config if in_channels % groups != 0: raise ValueError(f"""Input channels ({in_channels}) are not divisible by {groups} groups.""" ) if out_channels % groups != 0: raise ValueError(f"""Output channels ({out_channels}) are not divisible by {groups} groups.""" ) a : List[str] = 0 if config.tf_padding else int((kernel_size - 1) / 2 ) a : List[Any] = nn.Convad( in_channels=lowerCAmelCase__ , out_channels=lowerCAmelCase__ , kernel_size=lowerCAmelCase__ , stride=lowerCAmelCase__ , padding=lowerCAmelCase__ , groups=lowerCAmelCase__ , bias=lowerCAmelCase__ , padding_mode="zeros" , ) if use_normalization: a : Optional[int] = nn.BatchNormad( num_features=lowerCAmelCase__ , eps=config.layer_norm_eps , momentum=0.9_997 , affine=lowerCAmelCase__ , track_running_stats=lowerCAmelCase__ , ) else: a : List[str] = None if use_activation: if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): a : Any = ACTaFN[use_activation] elif isinstance(config.hidden_act , lowerCAmelCase__ ): a : Optional[Any] = ACTaFN[config.hidden_act] else: a : Any = config.hidden_act else: a : Tuple = None def __a ( self , lowerCAmelCase__ ) -> torch.Tensor: if self.config.tf_padding: a : str = apply_tf_padding(lowerCAmelCase__ , self.convolution ) a : Any = self.convolution(lowerCAmelCase__ ) if self.normalization is not None: a : str = self.normalization(lowerCAmelCase__ ) if self.activation is not None: a : Any = self.activation(lowerCAmelCase__ ) return features class __UpperCamelCase ( a__ ): lowerCamelCase : Any =MobileNetVaConfig lowerCamelCase : Any =load_tf_weights_in_mobilenet_va lowerCamelCase : Tuple ="""mobilenet_v1""" lowerCamelCase : List[Any] ="""pixel_values""" lowerCamelCase : List[str] =False def __a ( self , lowerCAmelCase__ ) -> None: if isinstance(lowerCAmelCase__ , (nn.Linear, nn.Convad) ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() elif isinstance(lowerCAmelCase__ , nn.BatchNormad ): module.bias.data.zero_() module.weight.data.fill_(1.0 ) a : Any = R''' This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior. Parameters: config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights. ''' a : List[str] = R''' Args: pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`MobileNetV1ImageProcessor.__call__`] for details. output_hidden_states (`bool`, *optional*): Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for more detail. return_dict (`bool`, *optional*): Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. ''' @add_start_docstrings( """The bare MobileNetV1 model outputting raw hidden-states without any specific head on top.""" , a__ , ) class __UpperCamelCase ( a__ ): def __init__( self , lowerCAmelCase__ , lowerCAmelCase__ = True ) -> int: super().__init__(lowerCAmelCase__ ) a : Any = config a : Tuple = 32 a : Union[str, Any] = max(int(depth * config.depth_multiplier ) , config.min_depth ) a : Optional[Any] = MobileNetVaConvLayer( lowerCAmelCase__ , in_channels=config.num_channels , out_channels=lowerCAmelCase__ , kernel_size=3 , stride=2 , ) a : Optional[int] = [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1] a : List[str] = nn.ModuleList() for i in range(13 ): a : Union[str, Any] = out_channels if strides[i] == 2 or i == 0: depth *= 2 a : int = max(int(depth * config.depth_multiplier ) , config.min_depth ) self.layer.append( MobileNetVaConvLayer( lowerCAmelCase__ , in_channels=lowerCAmelCase__ , out_channels=lowerCAmelCase__ , kernel_size=3 , stride=strides[i] , groups=lowerCAmelCase__ , ) ) self.layer.append( MobileNetVaConvLayer( lowerCAmelCase__ , in_channels=lowerCAmelCase__ , out_channels=lowerCAmelCase__ , kernel_size=1 , ) ) a : Any = nn.AdaptiveAvgPoolad((1, 1) ) if add_pooling_layer else None # Initialize weights and apply final processing self.post_init() def __a ( self , lowerCAmelCase__ ) -> str: raise NotImplementedError @add_start_docstrings_to_model_forward(lowerCAmelCase__ ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=lowerCAmelCase__ , config_class=_CONFIG_FOR_DOC , modality="vision" , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def __a ( self , lowerCAmelCase__ = None , lowerCAmelCase__ = None , lowerCAmelCase__ = None , ) -> Union[tuple, BaseModelOutputWithPoolingAndNoAttention]: a : Optional[Any] = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) a : List[Any] = return_dict if return_dict is not None else self.config.use_return_dict if pixel_values is None: raise ValueError("You have to specify pixel_values" ) a : str = self.conv_stem(lowerCAmelCase__ ) a : Tuple = () if output_hidden_states else None for i, layer_module in enumerate(self.layer ): a : Optional[int] = layer_module(lowerCAmelCase__ ) if output_hidden_states: a : List[str] = all_hidden_states + (hidden_states,) a : List[Any] = hidden_states if self.pooler is not None: a : str = torch.flatten(self.pooler(lowerCAmelCase__ ) , start_dim=1 ) else: a : str = None if not return_dict: return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None ) return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=lowerCAmelCase__ , pooler_output=lowerCAmelCase__ , hidden_states=lowerCAmelCase__ , ) @add_start_docstrings( """ MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. """ , a__ , ) class __UpperCamelCase ( a__ ): def __init__( self , lowerCAmelCase__ ) -> None: super().__init__(lowerCAmelCase__ ) a : Tuple = config.num_labels a : Optional[Any] = MobileNetVaModel(lowerCAmelCase__ ) a : List[Any] = self.mobilenet_va.layer[-1].convolution.out_channels # Classifier head a : str = nn.Dropout(config.classifier_dropout_prob , inplace=lowerCAmelCase__ ) a : int = nn.Linear(lowerCAmelCase__ , config.num_labels ) if config.num_labels > 0 else nn.Identity() # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(lowerCAmelCase__ ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=lowerCAmelCase__ , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def __a ( self , lowerCAmelCase__ = None , lowerCAmelCase__ = None , lowerCAmelCase__ = None , lowerCAmelCase__ = None , ) -> Union[tuple, ImageClassifierOutputWithNoAttention]: a : Tuple = return_dict if return_dict is not None else self.config.use_return_dict a : Dict = self.mobilenet_va(lowerCAmelCase__ , output_hidden_states=lowerCAmelCase__ , return_dict=lowerCAmelCase__ ) a : Tuple = outputs.pooler_output if return_dict else outputs[1] a : Optional[int] = self.classifier(self.dropout(lowerCAmelCase__ ) ) a : Optional[Any] = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: a : List[Any] = "regression" elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): a : Optional[Any] = "single_label_classification" else: a : Optional[int] = "multi_label_classification" if self.config.problem_type == "regression": a : Any = MSELoss() if self.num_labels == 1: a : Optional[Any] = loss_fct(logits.squeeze() , labels.squeeze() ) else: a : Dict = loss_fct(lowerCAmelCase__ , lowerCAmelCase__ ) elif self.config.problem_type == "single_label_classification": a : Any = CrossEntropyLoss() a : str = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": a : Union[str, Any] = BCEWithLogitsLoss() a : Optional[Any] = loss_fct(lowerCAmelCase__ , lowerCAmelCase__ ) if not return_dict: a : Optional[int] = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return ImageClassifierOutputWithNoAttention( loss=lowerCAmelCase__ , logits=lowerCAmelCase__ , hidden_states=outputs.hidden_states , )
31
"""simple docstring""" import gc import random import unittest import torch from diffusers import ( IFImgaImgPipeline, IFImgaImgSuperResolutionPipeline, IFInpaintingPipeline, IFInpaintingSuperResolutionPipeline, IFPipeline, IFSuperResolutionPipeline, ) from diffusers.models.attention_processor import AttnAddedKVProcessor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import floats_tensor, load_numpy, require_torch_gpu, skip_mps, slow, torch_device from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference from . import IFPipelineTesterMixin @skip_mps class __UpperCamelCase ( a__ , a__ , unittest.TestCase ): lowerCamelCase : Dict =IFPipeline lowerCamelCase : int =TEXT_TO_IMAGE_PARAMS - {"""width""", """height""", """latents"""} lowerCamelCase : int =TEXT_TO_IMAGE_BATCH_PARAMS lowerCamelCase : int =PipelineTesterMixin.required_optional_params - {"""latents"""} def __a ( self ) -> List[str]: return self._get_dummy_components() def __a ( self , lowerCAmelCase__ , lowerCAmelCase__=0 ) -> Dict: if str(lowerCAmelCase__ ).startswith("mps" ): a : Tuple = torch.manual_seed(lowerCAmelCase__ ) else: a : int = torch.Generator(device=lowerCAmelCase__ ).manual_seed(lowerCAmelCase__ ) a : Optional[Any] = { "prompt": "A painting of a squirrel eating a burger", "generator": generator, "num_inference_steps": 2, "output_type": "numpy", } return inputs def __a ( self ) -> Union[str, Any]: self._test_save_load_optional_components() @unittest.skipIf(torch_device != "cuda" , reason="float16 requires CUDA" ) def __a ( self ) -> Any: # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder super().test_save_load_floataa(expected_max_diff=1E-1 ) def __a ( self ) -> Union[str, Any]: self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def __a ( self ) -> Optional[int]: self._test_save_load_local() def __a ( self ) -> Tuple: self._test_inference_batch_single_identical( expected_max_diff=1E-2 , ) @unittest.skipIf( torch_device != "cuda" or not is_xformers_available() , reason="XFormers attention is only available with CUDA and `xformers` installed" , ) def __a ( self ) -> str: self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) @slow @require_torch_gpu class __UpperCamelCase ( unittest.TestCase ): def __a ( self ) -> Optional[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __a ( self ) -> Tuple: # if a : Tuple = IFPipeline.from_pretrained("DeepFloyd/IF-I-XL-v1.0" , variant="fp16" , torch_dtype=torch.floataa ) a : str = IFSuperResolutionPipeline.from_pretrained( "DeepFloyd/IF-II-L-v1.0" , variant="fp16" , torch_dtype=torch.floataa , text_encoder=lowerCAmelCase__ , tokenizer=lowerCAmelCase__ ) # pre compute text embeddings and remove T5 to save memory pipe_a.text_encoder.to("cuda" ) a, a : List[str] = pipe_a.encode_prompt("anime turtle" , device="cuda" ) del pipe_a.tokenizer del pipe_a.text_encoder gc.collect() a : Optional[int] = None a : Optional[int] = None pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) pipe_a.remove_all_hooks() pipe_a.remove_all_hooks() # img2img a : Union[str, Any] = IFImgaImgPipeline(**pipe_a.components ) a : List[Any] = IFImgaImgSuperResolutionPipeline(**pipe_a.components ) pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if_imgaimg(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) pipe_a.remove_all_hooks() pipe_a.remove_all_hooks() # inpainting a : Union[str, Any] = IFInpaintingPipeline(**pipe_a.components ) a : List[str] = IFInpaintingSuperResolutionPipeline(**pipe_a.components ) pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if_inpainting(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def __a ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) -> Dict: # pipeline 1 _start_torch_memory_measurement() a : List[str] = torch.Generator(device="cpu" ).manual_seed(0 ) a : Dict = pipe_a( prompt_embeds=lowerCAmelCase__ , negative_prompt_embeds=lowerCAmelCase__ , num_inference_steps=2 , generator=lowerCAmelCase__ , output_type="np" , ) a : List[str] = output.images[0] assert image.shape == (64, 64, 3) a : Dict = torch.cuda.max_memory_allocated() assert mem_bytes < 13 * 10**9 a : Optional[Any] = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if.npy" ) assert_mean_pixel_difference(lowerCAmelCase__ , lowerCAmelCase__ ) # pipeline 2 _start_torch_memory_measurement() a : List[str] = torch.Generator(device="cpu" ).manual_seed(0 ) a : Optional[int] = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(lowerCAmelCase__ ) a : Union[str, Any] = pipe_a( prompt_embeds=lowerCAmelCase__ , negative_prompt_embeds=lowerCAmelCase__ , image=lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=2 , output_type="np" , ) a : List[str] = output.images[0] assert image.shape == (256, 256, 3) a : int = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 a : Union[str, Any] = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_superresolution_stage_II.npy" ) assert_mean_pixel_difference(lowerCAmelCase__ , lowerCAmelCase__ ) def __a ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) -> int: # pipeline 1 _start_torch_memory_measurement() a : int = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(lowerCAmelCase__ ) a : Tuple = torch.Generator(device="cpu" ).manual_seed(0 ) a : List[Any] = pipe_a( prompt_embeds=lowerCAmelCase__ , negative_prompt_embeds=lowerCAmelCase__ , image=lowerCAmelCase__ , num_inference_steps=2 , generator=lowerCAmelCase__ , output_type="np" , ) a : Tuple = output.images[0] assert image.shape == (64, 64, 3) a : int = torch.cuda.max_memory_allocated() assert mem_bytes < 10 * 10**9 a : Optional[int] = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img.npy" ) assert_mean_pixel_difference(lowerCAmelCase__ , lowerCAmelCase__ ) # pipeline 2 _start_torch_memory_measurement() a : Optional[int] = torch.Generator(device="cpu" ).manual_seed(0 ) a : List[Any] = floats_tensor((1, 3, 256, 256) , rng=random.Random(0 ) ).to(lowerCAmelCase__ ) a : str = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(lowerCAmelCase__ ) a : Dict = pipe_a( prompt_embeds=lowerCAmelCase__ , negative_prompt_embeds=lowerCAmelCase__ , image=lowerCAmelCase__ , original_image=lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=2 , output_type="np" , ) a : int = output.images[0] assert image.shape == (256, 256, 3) a : str = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 a : Any = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img_superresolution_stage_II.npy" ) assert_mean_pixel_difference(lowerCAmelCase__ , lowerCAmelCase__ ) def __a ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) -> Optional[int]: # pipeline 1 _start_torch_memory_measurement() a : Union[str, Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(lowerCAmelCase__ ) a : List[Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(1 ) ).to(lowerCAmelCase__ ) a : Optional[int] = torch.Generator(device="cpu" ).manual_seed(0 ) a : List[str] = pipe_a( prompt_embeds=lowerCAmelCase__ , negative_prompt_embeds=lowerCAmelCase__ , image=lowerCAmelCase__ , mask_image=lowerCAmelCase__ , num_inference_steps=2 , generator=lowerCAmelCase__ , output_type="np" , ) a : List[Any] = output.images[0] assert image.shape == (64, 64, 3) a : Tuple = torch.cuda.max_memory_allocated() assert mem_bytes < 10 * 10**9 a : Union[str, Any] = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting.npy" ) assert_mean_pixel_difference(lowerCAmelCase__ , lowerCAmelCase__ ) # pipeline 2 _start_torch_memory_measurement() a : str = torch.Generator(device="cpu" ).manual_seed(0 ) a : Optional[int] = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(lowerCAmelCase__ ) a : int = floats_tensor((1, 3, 256, 256) , rng=random.Random(0 ) ).to(lowerCAmelCase__ ) a : Dict = floats_tensor((1, 3, 256, 256) , rng=random.Random(1 ) ).to(lowerCAmelCase__ ) a : Optional[int] = pipe_a( prompt_embeds=lowerCAmelCase__ , negative_prompt_embeds=lowerCAmelCase__ , image=lowerCAmelCase__ , mask_image=lowerCAmelCase__ , original_image=lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=2 , output_type="np" , ) a : List[str] = output.images[0] assert image.shape == (256, 256, 3) a : Tuple = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 a : Optional[Any] = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting_superresolution_stage_II.npy" ) assert_mean_pixel_difference(lowerCAmelCase__ , lowerCAmelCase__ ) def _SCREAMING_SNAKE_CASE ( ) ->List[str]: '''simple docstring''' torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats()
31
1
import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( UniSpeechConfig, UniSpeechForCTC, UniSpeechForPreTraining, WavaVecaFeatureExtractor, WavaVecaPhonemeCTCTokenizer, WavaVecaProcessor, logging, ) logging.set_verbosity_info() a__ = logging.get_logger(__name__) a__ = { '''post_extract_proj''': '''feature_projection.projection''', '''encoder.pos_conv.0''': '''encoder.pos_conv_embed.conv''', '''self_attn.k_proj''': '''encoder.layers.*.attention.k_proj''', '''self_attn.v_proj''': '''encoder.layers.*.attention.v_proj''', '''self_attn.q_proj''': '''encoder.layers.*.attention.q_proj''', '''self_attn.out_proj''': '''encoder.layers.*.attention.out_proj''', '''self_attn_layer_norm''': '''encoder.layers.*.layer_norm''', '''fc1''': '''encoder.layers.*.feed_forward.intermediate_dense''', '''fc2''': '''encoder.layers.*.feed_forward.output_dense''', '''final_layer_norm''': '''encoder.layers.*.final_layer_norm''', '''encoder.layer_norm''': '''encoder.layer_norm''', '''w2v_model.layer_norm''': '''feature_projection.layer_norm''', '''quantizer.weight_proj''': '''quantizer.weight_proj''', '''quantizer.vars''': '''quantizer.codevectors''', '''project_q''': '''project_q''', '''final_proj''': '''project_hid''', '''w2v_encoder.proj''': '''ctc_proj''', '''mask_emb''': '''masked_spec_embed''', } a__ = [ '''ctc_proj''', '''quantizer.weight_proj''', '''quantizer.codevectors''', '''project_q''', '''project_hid''', ] def A__ (snake_case : Union[str, Any] , snake_case : Tuple , snake_case : Union[str, Any] , snake_case : Any , snake_case : Optional[int] , snake_case : Any ) -> Dict: for attribute in key.split(""".""" ): if is_finetuned: if attribute in ["quantizer", "project_q", "project_hid"]: # those layers are only relevant for pretraining and should be dropped return if attribute == "ctc_proj": # we should rename `ctc_proj` to `lm_head` for fine-tuned phoneme models __UpperCamelCase : int = '''lm_head''' __UpperCamelCase : Optional[Any] = getattr(lowercase_ , lowercase_ ) if weight_type is not None: __UpperCamelCase : Any = getattr(lowercase_ , lowercase_ ).shape else: __UpperCamelCase : Tuple = hf_pointer.shape assert hf_shape == value.shape, ( F'''Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be''' F''' {value.shape} for {full_name}''' ) if weight_type == "weight": __UpperCamelCase : Dict = value elif weight_type == "weight_g": __UpperCamelCase : Optional[Any] = value elif weight_type == "weight_v": __UpperCamelCase : str = value elif weight_type == "bias": __UpperCamelCase : List[Any] = value else: __UpperCamelCase : int = value logger.info(F'''{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.''' ) def A__ (snake_case : List[Any] , snake_case : List[str] , snake_case : Optional[int] ) -> Any: __UpperCamelCase : str = [] __UpperCamelCase : Tuple = fairseq_model.state_dict() __UpperCamelCase : Optional[int] = hf_model.unispeech.feature_extractor for name, value in fairseq_dict.items(): __UpperCamelCase : List[str] = False if "conv_layers" in name: load_conv_layer( lowercase_ , lowercase_ , lowercase_ , lowercase_ , hf_model.config.feat_extract_norm == """group""" , ) __UpperCamelCase : Any = True else: for key, mapped_key in MAPPING.items(): __UpperCamelCase : Optional[Any] = '''unispeech.''' + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if key in name or key.split("""w2v_model.""" )[-1] == name.split(""".""" )[0]: __UpperCamelCase : Dict = True if "*" in mapped_key: __UpperCamelCase : Tuple = name.split(lowercase_ )[0].split(""".""" )[-2] __UpperCamelCase : Optional[int] = mapped_key.replace("""*""" , lowercase_ ) if "weight_g" in name: __UpperCamelCase : Dict = '''weight_g''' elif "weight_v" in name: __UpperCamelCase : Optional[Any] = '''weight_v''' elif "bias" in name: __UpperCamelCase : int = '''bias''' elif "weight" in name: # TODO: don't match quantizer.weight_proj __UpperCamelCase : Dict = '''weight''' else: __UpperCamelCase : Tuple = None set_recursively(lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ ) continue if not is_used: unused_weights.append(lowercase_ ) logger.warning(F'''Unused weights: {unused_weights}''' ) def A__ (snake_case : Tuple , snake_case : Optional[int] , snake_case : Union[str, Any] , snake_case : str , snake_case : int ) -> Dict: __UpperCamelCase : Union[str, Any] = full_name.split("""conv_layers.""" )[-1] __UpperCamelCase : Tuple = name.split(""".""" ) __UpperCamelCase : List[Any] = int(items[0] ) __UpperCamelCase : Dict = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( F'''{full_name} has size {value.shape}, but''' F''' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.''' ) __UpperCamelCase : str = value logger.info(F'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( F'''{full_name} has size {value.shape}, but''' F''' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.''' ) __UpperCamelCase : Union[str, Any] = value logger.info(F'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( F'''{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was''' " found." ) __UpperCamelCase : List[Any] = value logger.info(F'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( F'''{full_name} has size {value.shape}, but''' F''' {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.''' ) __UpperCamelCase : Optional[int] = value logger.info(F'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) else: unused_weights.append(lowercase_ ) @torch.no_grad() def A__ (snake_case : List[Any] , snake_case : Any , snake_case : Union[str, Any]=None , snake_case : Optional[Any]=None , snake_case : str=True ) -> List[str]: if config_path is not None: __UpperCamelCase : Union[str, Any] = UniSpeechConfig.from_pretrained(lowercase_ ) else: __UpperCamelCase : Tuple = UniSpeechConfig() if is_finetuned: if dict_path: __UpperCamelCase : Tuple = Dictionary.load_from_json(lowercase_ ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq __UpperCamelCase : List[str] = target_dict.pad_index __UpperCamelCase : Dict = target_dict.bos_index __UpperCamelCase : Optional[Any] = target_dict.eos_index __UpperCamelCase : List[Any] = len(target_dict.symbols ) __UpperCamelCase : List[str] = os.path.join(lowercase_ , """vocab.json""" ) if not os.path.isdir(lowercase_ ): logger.error("""--pytorch_dump_folder_path ({}) should be a directory""".format(lowercase_ ) ) return os.makedirs(lowercase_ , exist_ok=lowercase_ ) __UpperCamelCase : Dict = target_dict.indices # fairseq has the <pad> and <s> switched __UpperCamelCase : List[Any] = 42 __UpperCamelCase : Tuple = 43 with open(lowercase_ , """w""" , encoding="""utf-8""" ) as vocab_handle: json.dump(lowercase_ , lowercase_ ) __UpperCamelCase : List[str] = WavaVecaPhonemeCTCTokenizer( lowercase_ , unk_token=target_dict.unk_word , pad_token=target_dict.pad_word , bos_token=target_dict.bos_word , eos_token=target_dict.eos_word , word_delimiter_token="""|""" , do_lower_case=lowercase_ , ) __UpperCamelCase : str = True if config.feat_extract_norm == '''layer''' else False __UpperCamelCase : Any = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_60_00 , padding_value=0 , do_normalize=lowercase_ , return_attention_mask=lowercase_ , ) __UpperCamelCase : Tuple = WavaVecaProcessor(feature_extractor=lowercase_ , tokenizer=lowercase_ ) processor.save_pretrained(lowercase_ ) __UpperCamelCase : List[str] = UniSpeechForCTC(lowercase_ ) else: __UpperCamelCase : Union[str, Any] = UniSpeechForPreTraining(lowercase_ ) if is_finetuned: __UpperCamelCase : Optional[Any] = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"""data""": """/""".join(dict_path.split("""/""" )[:-1] ), """w2v_path""": checkpoint_path} ) else: __UpperCamelCase : Any = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] ) __UpperCamelCase : Optional[Any] = model[0].eval() recursively_load_weights(lowercase_ , lowercase_ , lowercase_ ) hf_unispeech.save_pretrained(lowercase_ ) if __name__ == "__main__": a__ = argparse.ArgumentParser() parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument('''--checkpoint_path''', default=None, type=str, help='''Path to fairseq checkpoint''') parser.add_argument('''--dict_path''', default=None, type=str, help='''Path to dict of fine-tuned model''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') parser.add_argument( '''--not_finetuned''', action='''store_true''', help='''Whether the model to convert is a fine-tuned model or not''' ) a__ = parser.parse_args() convert_unispeech_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned )
279
"""simple docstring""" import itertools import json import linecache import os import pickle import re import socket import string from collections import Counter from logging import getLogger from pathlib import Path from typing import Callable, Dict, Iterable, List import git import torch from torch.utils.data import Dataset from transformers import BartTokenizer, RagTokenizer, TaTokenizer def lowerCAmelCase_ ( lowercase_ : List[str] , lowercase_ : List[str] , lowercase_ : Dict , lowercase_ : Tuple , lowercase_ : Optional[Any]=True , lowercase_ : Any="pt" ): '''simple docstring''' __SCREAMING_SNAKE_CASE : Union[str, Any] = {'''add_prefix_space''': True} if isinstance(lowercase_ , lowercase_ ) and not line.startswith(''' ''' ) else {} __SCREAMING_SNAKE_CASE : Optional[int] = padding_side return tokenizer( [line] , max_length=lowercase_ , padding='''max_length''' if pad_to_max_length else None , truncation=lowercase_ , return_tensors=lowercase_ , add_special_tokens=lowercase_ , **lowercase_ , ) def lowerCAmelCase_ ( lowercase_ : Optional[int] , lowercase_ : Tuple , lowercase_ : List[Any]=None , ): '''simple docstring''' __SCREAMING_SNAKE_CASE : Dict = input_ids.ne(lowercase_ ).any(dim=0 ) if attention_mask is None: return input_ids[:, keep_column_mask] else: return (input_ids[:, keep_column_mask], attention_mask[:, keep_column_mask]) class snake_case ( __UpperCAmelCase ): def __init__( self :Optional[Any] , _lowerCamelCase :Dict , _lowerCamelCase :Any , _lowerCamelCase :Optional[Any] , _lowerCamelCase :Tuple , _lowerCamelCase :Any="train" , _lowerCamelCase :str=None , _lowerCamelCase :Optional[Any]=None , _lowerCamelCase :List[Any]=None , _lowerCamelCase :Tuple="" , ): super().__init__() __SCREAMING_SNAKE_CASE : Dict = Path(_lowerCamelCase ).joinpath(type_path + '''.source''' ) __SCREAMING_SNAKE_CASE : Any = Path(_lowerCamelCase ).joinpath(type_path + '''.target''' ) __SCREAMING_SNAKE_CASE : Any = self.get_char_lens(self.src_file ) __SCREAMING_SNAKE_CASE : List[str] = max_source_length __SCREAMING_SNAKE_CASE : Dict = max_target_length assert min(self.src_lens ) > 0, f'''found empty line in {self.src_file}''' __SCREAMING_SNAKE_CASE : Dict = tokenizer __SCREAMING_SNAKE_CASE : Union[str, Any] = prefix if n_obs is not None: __SCREAMING_SNAKE_CASE : Any = self.src_lens[:n_obs] __SCREAMING_SNAKE_CASE : List[str] = src_lang __SCREAMING_SNAKE_CASE : str = tgt_lang def __len__( self :int ): return len(self.src_lens ) def __getitem__( self :Optional[Any] , _lowerCamelCase :Any ): __SCREAMING_SNAKE_CASE : Optional[Any] = index + 1 # linecache starts at 1 __SCREAMING_SNAKE_CASE : Any = self.prefix + linecache.getline(str(self.src_file ) , _lowerCamelCase ).rstrip('''\n''' ) __SCREAMING_SNAKE_CASE : Dict = linecache.getline(str(self.tgt_file ) , _lowerCamelCase ).rstrip('''\n''' ) assert source_line, f'''empty source line for index {index}''' assert tgt_line, f'''empty tgt line for index {index}''' # Need to add eos token manually for T5 if isinstance(self.tokenizer , _lowerCamelCase ): source_line += self.tokenizer.eos_token tgt_line += self.tokenizer.eos_token # Pad source and target to the right __SCREAMING_SNAKE_CASE : Dict = ( self.tokenizer.question_encoder if isinstance(self.tokenizer , _lowerCamelCase ) else self.tokenizer ) __SCREAMING_SNAKE_CASE : Optional[Any] = self.tokenizer.generator if isinstance(self.tokenizer , _lowerCamelCase ) else self.tokenizer __SCREAMING_SNAKE_CASE : Dict = encode_line(_lowerCamelCase , _lowerCamelCase , self.max_source_length , '''right''' ) __SCREAMING_SNAKE_CASE : Dict = encode_line(_lowerCamelCase , _lowerCamelCase , self.max_target_length , '''right''' ) __SCREAMING_SNAKE_CASE : Any = source_inputs['''input_ids'''].squeeze() __SCREAMING_SNAKE_CASE : Any = target_inputs['''input_ids'''].squeeze() __SCREAMING_SNAKE_CASE : Dict = source_inputs['''attention_mask'''].squeeze() return { "input_ids": source_ids, "attention_mask": src_mask, "decoder_input_ids": target_ids, } @staticmethod def SCREAMING_SNAKE_CASE_ ( _lowerCamelCase :Any ): return [len(_lowerCamelCase ) for x in Path(_lowerCamelCase ).open().readlines()] def SCREAMING_SNAKE_CASE_ ( self :List[str] , _lowerCamelCase :List[str] ): __SCREAMING_SNAKE_CASE : int = torch.stack([x['''input_ids'''] for x in batch] ) __SCREAMING_SNAKE_CASE : str = torch.stack([x['''attention_mask'''] for x in batch] ) __SCREAMING_SNAKE_CASE : int = torch.stack([x['''decoder_input_ids'''] for x in batch] ) __SCREAMING_SNAKE_CASE : str = ( self.tokenizer.generator.pad_token_id if isinstance(self.tokenizer , _lowerCamelCase ) else self.tokenizer.pad_token_id ) __SCREAMING_SNAKE_CASE : Union[str, Any] = ( self.tokenizer.question_encoder.pad_token_id if isinstance(self.tokenizer , _lowerCamelCase ) else self.tokenizer.pad_token_id ) __SCREAMING_SNAKE_CASE : List[str] = trim_batch(_lowerCamelCase , _lowerCamelCase ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Any = trim_batch(_lowerCamelCase , _lowerCamelCase , attention_mask=_lowerCamelCase ) __SCREAMING_SNAKE_CASE : Dict = { '''input_ids''': source_ids, '''attention_mask''': source_mask, '''decoder_input_ids''': y, } return batch _lowerCamelCase = getLogger(__name__) def lowerCAmelCase_ ( lowercase_ : List[List] ): '''simple docstring''' return list(itertools.chain.from_iterable(lowercase_ ) ) def lowerCAmelCase_ ( lowercase_ : str ): '''simple docstring''' __SCREAMING_SNAKE_CASE : Any = get_git_info() save_json(lowercase_ , os.path.join(lowercase_ , '''git_log.json''' ) ) def lowerCAmelCase_ ( lowercase_ : Any , lowercase_ : Optional[int] , lowercase_ : str=4 , **lowercase_ : List[str] ): '''simple docstring''' with open(lowercase_ , '''w''' ) as f: json.dump(lowercase_ , lowercase_ , indent=lowercase_ , **lowercase_ ) def lowerCAmelCase_ ( lowercase_ : Union[str, Any] ): '''simple docstring''' with open(lowercase_ ) as f: return json.load(lowercase_ ) def lowerCAmelCase_ ( ): '''simple docstring''' __SCREAMING_SNAKE_CASE : Any = git.Repo(search_parent_directories=lowercase_ ) __SCREAMING_SNAKE_CASE : List[str] = { '''repo_id''': str(lowercase_ ), '''repo_sha''': str(repo.head.object.hexsha ), '''repo_branch''': str(repo.active_branch ), '''hostname''': str(socket.gethostname() ), } return repo_infos def lowerCAmelCase_ ( lowercase_ : Callable , lowercase_ : Iterable ): '''simple docstring''' return list(map(lowercase_ , lowercase_ ) ) def lowerCAmelCase_ ( lowercase_ : Any , lowercase_ : Any ): '''simple docstring''' with open(lowercase_ , '''wb''' ) as f: return pickle.dump(lowercase_ , lowercase_ ) def lowerCAmelCase_ ( lowercase_ : Any ): '''simple docstring''' def remove_articles(lowercase_ : Dict ): return re.sub(r'''\b(a|an|the)\b''' , ''' ''' , lowercase_ ) def white_space_fix(lowercase_ : Optional[int] ): return " ".join(text.split() ) def remove_punc(lowercase_ : Any ): __SCREAMING_SNAKE_CASE : Optional[int] = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(lowercase_ : int ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(lowercase_ ) ) ) ) def lowerCAmelCase_ ( lowercase_ : Optional[int] , lowercase_ : int ): '''simple docstring''' __SCREAMING_SNAKE_CASE : Any = normalize_answer(lowercase_ ).split() __SCREAMING_SNAKE_CASE : Any = normalize_answer(lowercase_ ).split() __SCREAMING_SNAKE_CASE : Tuple = Counter(lowercase_ ) & Counter(lowercase_ ) __SCREAMING_SNAKE_CASE : Tuple = sum(common.values() ) if num_same == 0: return 0 __SCREAMING_SNAKE_CASE : Any = 1.0 * num_same / len(lowercase_ ) __SCREAMING_SNAKE_CASE : List[str] = 1.0 * num_same / len(lowercase_ ) __SCREAMING_SNAKE_CASE : Optional[int] = (2 * precision * recall) / (precision + recall) return fa def lowerCAmelCase_ ( lowercase_ : str , lowercase_ : Union[str, Any] ): '''simple docstring''' return normalize_answer(lowercase_ ) == normalize_answer(lowercase_ ) def lowerCAmelCase_ ( lowercase_ : List[str] , lowercase_ : List[str] ): '''simple docstring''' assert len(lowercase_ ) == len(lowercase_ ) __SCREAMING_SNAKE_CASE : Union[str, Any] = 0 for hypo, pred in zip(lowercase_ , lowercase_ ): em += exact_match_score(lowercase_ , lowercase_ ) if len(lowercase_ ) > 0: em /= len(lowercase_ ) return {"em": em} def lowerCAmelCase_ ( lowercase_ : str ): '''simple docstring''' return model_prefix.startswith('''rag''' ) def lowerCAmelCase_ ( lowercase_ : Optional[Any] , lowercase_ : Tuple , lowercase_ : Optional[int] ): '''simple docstring''' __SCREAMING_SNAKE_CASE : Optional[int] = {p: p for p in extra_params} # T5 models don't have `dropout` param, they have `dropout_rate` instead __SCREAMING_SNAKE_CASE : Any = '''dropout_rate''' for p in extra_params: if getattr(lowercase_ , lowercase_ , lowercase_ ): if not hasattr(lowercase_ , lowercase_ ) and not hasattr(lowercase_ , equivalent_param[p] ): logger.info('''config doesn\'t have a `{}` attribute'''.format(lowercase_ ) ) delattr(lowercase_ , lowercase_ ) continue __SCREAMING_SNAKE_CASE : Optional[int] = p if hasattr(lowercase_ , lowercase_ ) else equivalent_param[p] setattr(lowercase_ , lowercase_ , getattr(lowercase_ , lowercase_ ) ) delattr(lowercase_ , lowercase_ ) return hparams, config
674
0
# Copyright 2021 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from packaging import version from .. import __version__ from .constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD from .doc import ( add_code_sample_docstrings, add_end_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, copy_func, replace_return_docstrings, ) from .generic import ( ContextManagers, ExplicitEnum, ModelOutput, PaddingStrategy, TensorType, add_model_info_to_auto_map, cached_property, can_return_loss, expand_dims, find_labels, flatten_dict, infer_framework, is_jax_tensor, is_numpy_array, is_tensor, is_tf_symbolic_tensor, is_tf_tensor, is_torch_device, is_torch_dtype, is_torch_tensor, reshape, squeeze, strtobool, tensor_size, to_numpy, to_py_obj, transpose, working_or_temp_dir, ) from .hub import ( CLOUDFRONT_DISTRIB_PREFIX, DISABLE_TELEMETRY, HF_MODULES_CACHE, HUGGINGFACE_CO_PREFIX, HUGGINGFACE_CO_RESOLVE_ENDPOINT, PYTORCH_PRETRAINED_BERT_CACHE, PYTORCH_TRANSFORMERS_CACHE, S3_BUCKET_PREFIX, TRANSFORMERS_CACHE, TRANSFORMERS_DYNAMIC_MODULE_NAME, EntryNotFoundError, PushToHubMixin, RepositoryNotFoundError, RevisionNotFoundError, cached_file, default_cache_path, define_sagemaker_information, download_url, extract_commit_hash, get_cached_models, get_file_from_repo, get_full_repo_name, has_file, http_user_agent, is_offline_mode, is_remote_url, move_cache, send_example_telemetry, try_to_load_from_cache, ) from .import_utils import ( ENV_VARS_TRUE_AND_AUTO_VALUES, ENV_VARS_TRUE_VALUES, TORCH_FX_REQUIRED_VERSION, USE_JAX, USE_TF, USE_TORCH, DummyObject, OptionalDependencyNotAvailable, _LazyModule, ccl_version, direct_transformers_import, get_torch_version, is_accelerate_available, is_apex_available, is_bitsandbytes_available, is_bsa_available, is_coloredlogs_available, is_cython_available, is_datasets_available, is_decord_available, is_detectrona_available, is_faiss_available, is_flax_available, is_ftfy_available, is_in_notebook, is_ipex_available, is_jieba_available, is_jumanpp_available, is_kenlm_available, is_keras_nlp_available, is_librosa_available, is_natten_available, is_ninja_available, is_onnx_available, is_openai_available, is_optimum_available, is_pandas_available, is_peft_available, is_phonemizer_available, is_protobuf_available, is_psutil_available, is_pyanvml_available, is_pyctcdecode_available, is_pytesseract_available, is_pytest_available, is_pytorch_quantization_available, is_rjieba_available, is_sacremoses_available, is_safetensors_available, is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_scipy_available, is_sentencepiece_available, is_seqio_available, is_sklearn_available, is_soundfile_availble, is_spacy_available, is_speech_available, is_sudachi_available, is_tensorflow_probability_available, is_tensorflow_text_available, is_tfaonnx_available, is_tf_available, is_timm_available, is_tokenizers_available, is_torch_available, is_torch_bfaa_available, is_torch_bfaa_cpu_available, is_torch_bfaa_gpu_available, is_torch_compile_available, is_torch_cuda_available, is_torch_fx_available, is_torch_fx_proxy, is_torch_mps_available, is_torch_neuroncore_available, is_torch_tensorrt_fx_available, is_torch_tfaa_available, is_torch_tpu_available, is_torchaudio_available, is_torchdistx_available, is_torchdynamo_available, is_torchvision_available, is_training_run_on_sagemaker, is_vision_available, requires_backends, torch_only_method, ) __a : Optional[Any] = """pytorch_model.bin""" __a : Dict = """pytorch_model.bin.index.json""" __a : Any = """adapter_config.json""" __a : Union[str, Any] = """adapter_model.bin""" __a : List[Any] = """adapter_model.safetensors""" __a : List[Any] = """tf_model.h5""" __a : Optional[int] = """tf_model.h5.index.json""" __a : Optional[Any] = """model.ckpt""" __a : List[Any] = """flax_model.msgpack""" __a : List[str] = """flax_model.msgpack.index.json""" __a : str = """model.safetensors""" __a : List[str] = """model.safetensors.index.json""" __a : Union[str, Any] = """config.json""" __a : str = """preprocessor_config.json""" __a : Union[str, Any] = FEATURE_EXTRACTOR_NAME __a : Any = """generation_config.json""" __a : int = """modelcard.json""" __a : List[Any] = """▁""" __a : Optional[int] = SENTENCEPIECE_UNDERLINE # Kept for backward compatibility __a : Optional[Any] = [ [[0, 1, 0, 1], [1, 0, 0, 1]] ] * 2 # Needs to have 0s and 1s only since XLM uses it for langs too. __a : Optional[Any] = [[7, 6, 0, 0, 1], [1, 2, 3, 0, 0], [0, 0, 0, 4, 5]] __a : Tuple = [[1, 1, 1, 1, 1], [1, 1, 1, 0, 0], [0, 0, 0, 1, 1]] def a_ ( __snake_case ) -> Optional[int]: '''simple docstring''' if version.parse(__snake_case ) < version.parse(__snake_case ): if "dev" in min_version: UpperCamelCase_ = ( 'This example requires a source install from HuggingFace Transformers (see ' '`https://huggingface.co/docs/transformers/installation#install-from-source`),' ) else: UpperCamelCase_ = F'''This example requires a minimum version of {min_version},''' error_message += F''' but the version found is {__version__}.\n''' raise ImportError( error_message + 'Check out https://github.com/huggingface/transformers/tree/main/examples#important-note for the examples corresponding to other ' 'versions of HuggingFace Transformers.' )
708
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_big_bird import BigBirdTokenizer else: __a : Optional[int] = None __a : List[Any] = logging.get_logger(__name__) __a : int = {"""vocab_file""": """spiece.model""", """tokenizer_file""": """tokenizer.json"""} __a : Optional[Any] = { """vocab_file""": { """google/bigbird-roberta-base""": """https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model""", """google/bigbird-roberta-large""": ( """https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model""" ), """google/bigbird-base-trivia-itc""": ( """https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model""" ), }, """tokenizer_file""": { """google/bigbird-roberta-base""": ( """https://huggingface.co/google/bigbird-roberta-base/resolve/main/tokenizer.json""" ), """google/bigbird-roberta-large""": ( """https://huggingface.co/google/bigbird-roberta-large/resolve/main/tokenizer.json""" ), """google/bigbird-base-trivia-itc""": ( """https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/tokenizer.json""" ), }, } __a : int = { """google/bigbird-roberta-base""": 40_96, """google/bigbird-roberta-large""": 40_96, """google/bigbird-base-trivia-itc""": 40_96, } __a : Tuple = """▁""" class A ( lowerCamelCase_ ): _SCREAMING_SNAKE_CASE : Union[str, Any] = VOCAB_FILES_NAMES _SCREAMING_SNAKE_CASE : Dict = PRETRAINED_VOCAB_FILES_MAP _SCREAMING_SNAKE_CASE : Tuple = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _SCREAMING_SNAKE_CASE : List[str] = BigBirdTokenizer _SCREAMING_SNAKE_CASE : Optional[int] = ['''input_ids''', '''attention_mask'''] _SCREAMING_SNAKE_CASE : List[int] = [] def __init__( self : Optional[Any] , __UpperCAmelCase : Optional[Any]=None , __UpperCAmelCase : List[str]=None , __UpperCAmelCase : Optional[Any]="<unk>" , __UpperCAmelCase : Optional[int]="<s>" , __UpperCAmelCase : Optional[Any]="</s>" , __UpperCAmelCase : Tuple="<pad>" , __UpperCAmelCase : Dict="[SEP]" , __UpperCAmelCase : Optional[Any]="[MASK]" , __UpperCAmelCase : List[str]="[CLS]" , **__UpperCAmelCase : str , ) -> Union[str, Any]: """simple docstring""" UpperCamelCase_ = AddedToken(__UpperCAmelCase , lstrip=__UpperCAmelCase , rstrip=__UpperCAmelCase ) if isinstance(__UpperCAmelCase , __UpperCAmelCase ) else bos_token UpperCamelCase_ = AddedToken(__UpperCAmelCase , lstrip=__UpperCAmelCase , rstrip=__UpperCAmelCase ) if isinstance(__UpperCAmelCase , __UpperCAmelCase ) else eos_token UpperCamelCase_ = AddedToken(__UpperCAmelCase , lstrip=__UpperCAmelCase , rstrip=__UpperCAmelCase ) if isinstance(__UpperCAmelCase , __UpperCAmelCase ) else unk_token UpperCamelCase_ = AddedToken(__UpperCAmelCase , lstrip=__UpperCAmelCase , rstrip=__UpperCAmelCase ) if isinstance(__UpperCAmelCase , __UpperCAmelCase ) else pad_token UpperCamelCase_ = AddedToken(__UpperCAmelCase , lstrip=__UpperCAmelCase , rstrip=__UpperCAmelCase ) if isinstance(__UpperCAmelCase , __UpperCAmelCase ) else cls_token UpperCamelCase_ = AddedToken(__UpperCAmelCase , lstrip=__UpperCAmelCase , rstrip=__UpperCAmelCase ) if isinstance(__UpperCAmelCase , __UpperCAmelCase ) else sep_token # Mask token behave like a normal word, i.e. include the space before it UpperCamelCase_ = AddedToken(__UpperCAmelCase , lstrip=__UpperCAmelCase , rstrip=__UpperCAmelCase ) if isinstance(__UpperCAmelCase , __UpperCAmelCase ) else mask_token super().__init__( __UpperCAmelCase , tokenizer_file=__UpperCAmelCase , bos_token=__UpperCAmelCase , eos_token=__UpperCAmelCase , unk_token=__UpperCAmelCase , sep_token=__UpperCAmelCase , pad_token=__UpperCAmelCase , cls_token=__UpperCAmelCase , mask_token=__UpperCAmelCase , **__UpperCAmelCase , ) UpperCamelCase_ = vocab_file UpperCamelCase_ = False if not self.vocab_file else True def lowercase__ ( self : Tuple , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" UpperCamelCase_ = [self.sep_token_id] UpperCamelCase_ = [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 lowercase__ ( self : Union[str, Any] , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None , __UpperCAmelCase : bool = False ) -> List[int]: """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 None: return [1] + ([0] * len(__UpperCAmelCase )) + [1] return [1] + ([0] * len(__UpperCAmelCase )) + [1] + ([0] * len(__UpperCAmelCase )) + [1] def lowercase__ ( self : Any , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" UpperCamelCase_ = [self.sep_token_id] UpperCamelCase_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def lowercase__ ( self : Dict , __UpperCAmelCase : str , __UpperCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( 'Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ' 'tokenizer.' ) if not os.path.isdir(__UpperCAmelCase ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return UpperCamelCase_ = os.path.join( __UpperCAmelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__UpperCAmelCase ): copyfile(self.vocab_file , __UpperCAmelCase ) return (out_vocab_file,)
559
0
from math import isclose, sqrt def lowerCAmelCase ( UpperCamelCase__ : float , UpperCamelCase__ : float , UpperCamelCase__ : float ) -> tuple[float, float, float]: """simple docstring""" __SCREAMING_SNAKE_CASE: Tuple = point_y / 4 / point_x __SCREAMING_SNAKE_CASE: Any = 2 * normal_gradient / (1 + normal_gradient * normal_gradient) __SCREAMING_SNAKE_CASE: int = (1 - normal_gradient * normal_gradient) / ( 1 + normal_gradient * normal_gradient ) __SCREAMING_SNAKE_CASE: Tuple = (sa - ca * incoming_gradient) / (ca + sa * incoming_gradient) # to find the next point, solve the simultaeneous equations: # y^2 + 4x^2 = 100 # y - b = m * (x - a) # ==> A x^2 + B x + C = 0 __SCREAMING_SNAKE_CASE: Union[str, Any] = outgoing_gradient**2 + 4 __SCREAMING_SNAKE_CASE: int = 2 * outgoing_gradient * (point_y - outgoing_gradient * point_x) __SCREAMING_SNAKE_CASE: Any = (point_y - outgoing_gradient * point_x) ** 2 - 100 __SCREAMING_SNAKE_CASE: List[Any] = ( -linear_term - sqrt(linear_term**2 - 4 * quadratic_term * constant_term ) ) / (2 * quadratic_term) __SCREAMING_SNAKE_CASE: Dict = ( -linear_term + sqrt(linear_term**2 - 4 * quadratic_term * constant_term ) ) / (2 * quadratic_term) # two solutions, one of which is our input point __SCREAMING_SNAKE_CASE: Optional[int] = x_minus if isclose(UpperCamelCase__ , UpperCamelCase__ ) else x_plus __SCREAMING_SNAKE_CASE: Any = point_y + outgoing_gradient * (next_x - point_x) return next_x, next_y, outgoing_gradient def lowerCAmelCase ( UpperCamelCase__ : float = 1.4 , UpperCamelCase__ : float = -9.6 ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE: int = 0 __SCREAMING_SNAKE_CASE: float = first_x_coord __SCREAMING_SNAKE_CASE: float = first_y_coord __SCREAMING_SNAKE_CASE: float = (10.1 - point_y) / (0.0 - point_x) while not (-0.01 <= point_x <= 0.01 and point_y > 0): __SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE: str = next_point(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) num_reflections += 1 return num_reflections if __name__ == "__main__": print(f'''{solution() = }''')
202
import argparse from argparse import Namespace import torch from torch import nn from transformers import XGLMConfig, XGLMForCausalLM def lowerCAmelCase ( UpperCamelCase__ : Union[str, Any] ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE: List[Any] = [ '''decoder.version''', '''decoder.output_projection.weight''', '''_float_tensor''', '''decoder.embed_positions._float_tensor''', ] for k in ignore_keys: state_dict.pop(UpperCamelCase__ , UpperCamelCase__ ) def lowerCAmelCase ( UpperCamelCase__ : str ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE: str = emb.weight.shape __SCREAMING_SNAKE_CASE: List[str] = nn.Linear(UpperCamelCase__ , UpperCamelCase__ , bias=UpperCamelCase__ ) __SCREAMING_SNAKE_CASE: Tuple = emb.weight.data return lin_layer def lowerCAmelCase ( UpperCamelCase__ : Optional[int] ) -> Optional[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE: int = torch.load(UpperCamelCase__ , map_location='''cpu''' ) __SCREAMING_SNAKE_CASE: Optional[int] = Namespace(**checkpoint['''cfg''']['''model'''] ) __SCREAMING_SNAKE_CASE: Union[str, Any] = checkpoint['''model'''] remove_ignore_keys_(UpperCamelCase__ ) __SCREAMING_SNAKE_CASE: int = state_dict['''decoder.embed_tokens.weight'''].shape[0] __SCREAMING_SNAKE_CASE: List[Any] = {key.replace('''decoder''' , '''model''' ): val for key, val in state_dict.items()} __SCREAMING_SNAKE_CASE: Dict = XGLMConfig( vocab_size=UpperCamelCase__ , max_position_embeddings=args.max_target_positions , num_layers=args.decoder_layers , attention_heads=args.decoder_attention_heads , ffn_dim=args.decoder_ffn_embed_dim , d_model=args.decoder_embed_dim , layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function='''gelu''' , scale_embedding=not args.no_scale_embedding , tie_word_embeddings=args.share_decoder_input_output_embed , ) __SCREAMING_SNAKE_CASE: int = XGLMForCausalLM(UpperCamelCase__ ) __SCREAMING_SNAKE_CASE: Optional[Any] = model.load_state_dict(UpperCamelCase__ , strict=UpperCamelCase__ ) print(UpperCamelCase__ ) __SCREAMING_SNAKE_CASE: List[str] = make_linear_from_emb(model.model.embed_tokens ) return model if __name__ == "__main__": lowerCAmelCase : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument("""fairseq_path""", type=str, help="""path to a model.pt on local filesystem.""") parser.add_argument("""pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") lowerCAmelCase : List[str] = parser.parse_args() lowerCAmelCase : Optional[int] = convert_fairseq_xglm_checkpoint_from_disk(args.fairseq_path) model.save_pretrained(args.pytorch_dump_folder_path)
202
1
"""simple docstring""" import unittest import numpy as np import timeout_decorator # noqa from transformers import BlenderbotSmallConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor if is_flax_available(): import os # The slow tests are often failing with OOM error on GPU # This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed # but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html lowerCamelCase__ : List[str] = "platform" import jax import jax.numpy as jnp from transformers.models.blenderbot_small.modeling_flax_blenderbot_small import ( FlaxBlenderbotSmallForConditionalGeneration, FlaxBlenderbotSmallModel, shift_tokens_right, ) def __A ( a_ : Dict , a_ : List[str] , a_ : Optional[Any]=None , a_ : Optional[Any]=None , a_ : List[Any]=None , a_ : Tuple=None , a_ : List[Any]=None , a_ : Optional[int]=None , )-> Dict: '''simple docstring''' if attention_mask is None: SCREAMING_SNAKE_CASE : List[Any] = np.where(input_ids != config.pad_token_id , 1 , 0 ) if decoder_attention_mask is None: SCREAMING_SNAKE_CASE : List[str] = np.where(decoder_input_ids != config.pad_token_id , 1 , 0 ) if head_mask is None: SCREAMING_SNAKE_CASE : Union[str, Any] = np.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: SCREAMING_SNAKE_CASE : Optional[int] = np.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: SCREAMING_SNAKE_CASE : Tuple = np.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": attention_mask, } class lowercase__: '''simple docstring''' def __init__( self :str , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :Tuple=13 , lowerCamelCase_ :List[str]=7 , lowerCamelCase_ :int=True , lowerCamelCase_ :int=False , lowerCamelCase_ :List[Any]=99 , lowerCamelCase_ :Optional[int]=16 , lowerCamelCase_ :List[Any]=2 , lowerCamelCase_ :List[Any]=4 , lowerCamelCase_ :Dict=4 , lowerCamelCase_ :Optional[int]="gelu" , lowerCamelCase_ :Dict=0.1 , lowerCamelCase_ :Any=0.1 , lowerCamelCase_ :Any=32 , lowerCamelCase_ :Tuple=2 , lowerCamelCase_ :List[Any]=1 , lowerCamelCase_ :List[str]=0 , lowerCamelCase_ :Any=0.0_2 , ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = parent SCREAMING_SNAKE_CASE : Dict = batch_size SCREAMING_SNAKE_CASE : Tuple = seq_length SCREAMING_SNAKE_CASE : Dict = is_training SCREAMING_SNAKE_CASE : List[str] = use_labels SCREAMING_SNAKE_CASE : Tuple = vocab_size SCREAMING_SNAKE_CASE : int = hidden_size SCREAMING_SNAKE_CASE : Optional[Any] = num_hidden_layers SCREAMING_SNAKE_CASE : Any = num_attention_heads SCREAMING_SNAKE_CASE : Optional[int] = intermediate_size SCREAMING_SNAKE_CASE : Dict = hidden_act SCREAMING_SNAKE_CASE : str = hidden_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Optional[Any] = max_position_embeddings SCREAMING_SNAKE_CASE : Tuple = eos_token_id SCREAMING_SNAKE_CASE : List[Any] = pad_token_id SCREAMING_SNAKE_CASE : Dict = bos_token_id SCREAMING_SNAKE_CASE : Dict = initializer_range def __lowerCAmelCase ( self :Optional[int] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : str = np.clip(ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) , 3 , self.vocab_size ) SCREAMING_SNAKE_CASE : Optional[int] = np.concatenate((input_ids, 2 * np.ones((self.batch_size, 1) , dtype=np.intaa )) , -1 ) SCREAMING_SNAKE_CASE : List[Any] = shift_tokens_right(lowerCamelCase_ , 1 , 2 ) SCREAMING_SNAKE_CASE : Any = BlenderbotSmallConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , initializer_range=self.initializer_range , use_cache=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : int = prepare_blenderbot_inputs_dict(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) return config, inputs_dict def __lowerCAmelCase ( self :Optional[int] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = self.prepare_config_and_inputs() return config, inputs_dict def __lowerCAmelCase ( self :Any , lowerCamelCase_ :List[str] , lowerCamelCase_ :List[Any] , lowerCamelCase_ :List[str] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = 20 SCREAMING_SNAKE_CASE : Optional[Any] = model_class_name(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = model.encode(inputs_dict['''input_ids'''] ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = ( inputs_dict['''decoder_input_ids'''], inputs_dict['''decoder_attention_mask'''], ) SCREAMING_SNAKE_CASE : Dict = model.init_cache(decoder_input_ids.shape[0] , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = jnp.ones((decoder_input_ids.shape[0], max_decoder_length) , dtype='''i4''' ) SCREAMING_SNAKE_CASE : Optional[Any] = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) SCREAMING_SNAKE_CASE : str = model.decode( decoder_input_ids[:, :-1] , lowerCamelCase_ , decoder_attention_mask=lowerCamelCase_ , past_key_values=lowerCamelCase_ , decoder_position_ids=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : str = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype='''i4''' ) SCREAMING_SNAKE_CASE : List[Any] = model.decode( decoder_input_ids[:, -1:] , lowerCamelCase_ , decoder_attention_mask=lowerCamelCase_ , past_key_values=outputs_cache.past_key_values , decoder_position_ids=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Dict = model.decode(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 , msg=f"Max diff is {diff}" ) def __lowerCAmelCase ( self :Any , lowerCamelCase_ :List[Any] , lowerCamelCase_ :int , lowerCamelCase_ :Union[str, Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = 20 SCREAMING_SNAKE_CASE : Tuple = model_class_name(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = model.encode(inputs_dict['''input_ids'''] ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = ( inputs_dict['''decoder_input_ids'''], inputs_dict['''decoder_attention_mask'''], ) SCREAMING_SNAKE_CASE : Optional[int] = jnp.concatenate( [ decoder_attention_mask, jnp.zeros((decoder_attention_mask.shape[0], max_decoder_length - decoder_attention_mask.shape[1]) ), ] , axis=-1 , ) SCREAMING_SNAKE_CASE : Optional[Any] = model.init_cache(decoder_input_ids.shape[0] , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) SCREAMING_SNAKE_CASE : Optional[Any] = model.decode( decoder_input_ids[:, :-1] , lowerCamelCase_ , decoder_attention_mask=lowerCamelCase_ , past_key_values=lowerCamelCase_ , decoder_position_ids=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Optional[int] = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype='''i4''' ) SCREAMING_SNAKE_CASE : Dict = model.decode( decoder_input_ids[:, -1:] , lowerCamelCase_ , past_key_values=outputs_cache.past_key_values , decoder_attention_mask=lowerCamelCase_ , decoder_position_ids=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : int = model.decode(lowerCamelCase_ , lowerCamelCase_ , decoder_attention_mask=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 , msg=f"Max diff is {diff}" ) @require_flax class lowercase__( unittest.TestCase ): '''simple docstring''' UpperCamelCase = 99 def __lowerCAmelCase ( self :Any ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = np.array( [ [71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 82, 2], [5, 97, 17, 39, 94, 40, 2], [76, 83, 94, 25, 70, 78, 2], [87, 59, 41, 35, 48, 66, 2], [55, 13, 16, 58, 5, 2, 1], # note padding [64, 27, 31, 51, 12, 75, 2], [52, 64, 86, 17, 83, 39, 2], [48, 61, 9, 24, 71, 82, 2], [26, 1, 60, 48, 22, 13, 2], [21, 5, 62, 28, 14, 76, 2], [45, 98, 37, 86, 59, 48, 2], [70, 70, 50, 9, 28, 0, 2], ] , dtype=np.intaa , ) SCREAMING_SNAKE_CASE : Optional[Any] = input_ids.shape[0] SCREAMING_SNAKE_CASE : Union[str, Any] = BlenderbotSmallConfig( vocab_size=self.vocab_size , d_model=24 , encoder_layers=2 , decoder_layers=2 , encoder_attention_heads=2 , decoder_attention_heads=2 , encoder_ffn_dim=32 , decoder_ffn_dim=32 , max_position_embeddings=48 , eos_token_id=2 , pad_token_id=1 , bos_token_id=0 , ) return config, input_ids, batch_size def __lowerCAmelCase ( self :Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = self._get_config_and_data() SCREAMING_SNAKE_CASE : List[str] = FlaxBlenderbotSmallForConditionalGeneration(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = lm_model(input_ids=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = (batch_size, input_ids.shape[1], config.vocab_size) self.assertEqual(outputs['''logits'''].shape , lowerCamelCase_ ) def __lowerCAmelCase ( self :Union[str, Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = BlenderbotSmallConfig( vocab_size=self.vocab_size , d_model=14 , encoder_layers=2 , decoder_layers=2 , encoder_attention_heads=2 , decoder_attention_heads=2 , encoder_ffn_dim=8 , decoder_ffn_dim=8 , max_position_embeddings=48 , ) SCREAMING_SNAKE_CASE : int = FlaxBlenderbotSmallForConditionalGeneration(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = np.array([[71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 2, 1]] , dtype=np.intaa ) SCREAMING_SNAKE_CASE : str = np.array([[82, 71, 82, 18, 2], [58, 68, 2, 1, 1]] , dtype=np.intaa ) SCREAMING_SNAKE_CASE : List[str] = lm_model(input_ids=lowerCamelCase_ , decoder_input_ids=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = (*summary.shape, config.vocab_size) self.assertEqual(outputs['''logits'''].shape , lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = np.array([[71, 82, 18, 33, 2, 1, 1], [68, 34, 26, 58, 30, 82, 2]] , dtype=np.intaa ) SCREAMING_SNAKE_CASE : Tuple = shift_tokens_right(lowerCamelCase_ , 1 , 2 ) SCREAMING_SNAKE_CASE : List[str] = np.equal(lowerCamelCase_ , 1 ).astype(np.floataa ).sum() SCREAMING_SNAKE_CASE : Dict = np.equal(lowerCamelCase_ , 1 ).astype(np.floataa ).sum() self.assertEqual(shifted.shape , input_ids.shape ) self.assertEqual(lowerCamelCase_ , n_pad_before - 1 ) self.assertTrue(np.equal(shifted[:, 0] , 2 ).all() ) @require_flax class lowercase__( _UpperCAmelCase , unittest.TestCase , _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = True UpperCamelCase = ( ( FlaxBlenderbotSmallModel, FlaxBlenderbotSmallForConditionalGeneration, ) if is_flax_available() else () ) UpperCamelCase = (FlaxBlenderbotSmallForConditionalGeneration,) if is_flax_available() else () def __lowerCAmelCase ( self :Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = FlaxBlenderbotSmallModelTester(self ) def __lowerCAmelCase ( self :Dict ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[Any] = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward_with_attn_mask(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) def __lowerCAmelCase ( self :List[str] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): SCREAMING_SNAKE_CASE : Dict = self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = model_class(lowerCamelCase_ ) @jax.jit def encode_jitted(lowerCamelCase_ :List[str] , lowerCamelCase_ :Union[str, Any]=None , **lowerCamelCase_ :Union[str, Any] ): return model.encode(input_ids=lowerCamelCase_ , attention_mask=lowerCamelCase_ ) with self.subTest('''JIT Enabled''' ): SCREAMING_SNAKE_CASE : Optional[Any] = encode_jitted(**lowerCamelCase_ ).to_tuple() with self.subTest('''JIT Disabled''' ): with jax.disable_jit(): SCREAMING_SNAKE_CASE : Tuple = encode_jitted(**lowerCamelCase_ ).to_tuple() self.assertEqual(len(lowerCamelCase_ ) , len(lowerCamelCase_ ) ) for jitted_output, output in zip(lowerCamelCase_ , lowerCamelCase_ ): self.assertEqual(jitted_output.shape , output.shape ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): SCREAMING_SNAKE_CASE : Optional[Any] = model_class(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = model.encode(inputs_dict['''input_ids'''] , inputs_dict['''attention_mask'''] ) SCREAMING_SNAKE_CASE : Dict = { '''decoder_input_ids''': inputs_dict['''decoder_input_ids'''], '''decoder_attention_mask''': inputs_dict['''decoder_attention_mask'''], '''encoder_outputs''': encoder_outputs, } @jax.jit def decode_jitted(lowerCamelCase_ :List[Any] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :List[Any] ): return model.decode( decoder_input_ids=lowerCamelCase_ , decoder_attention_mask=lowerCamelCase_ , encoder_outputs=lowerCamelCase_ , ) with self.subTest('''JIT Enabled''' ): SCREAMING_SNAKE_CASE : str = decode_jitted(**lowerCamelCase_ ).to_tuple() with self.subTest('''JIT Disabled''' ): with jax.disable_jit(): SCREAMING_SNAKE_CASE : List[Any] = decode_jitted(**lowerCamelCase_ ).to_tuple() self.assertEqual(len(lowerCamelCase_ ) , len(lowerCamelCase_ ) ) for jitted_output, output in zip(lowerCamelCase_ , lowerCamelCase_ ): self.assertEqual(jitted_output.shape , output.shape ) @slow def __lowerCAmelCase ( self :Optional[int] ) -> Optional[int]: '''simple docstring''' for model_class_name in self.all_model_classes: SCREAMING_SNAKE_CASE : Dict = model_class_name.from_pretrained('''facebook/blenderbot_small-90M''' ) # FlaxBlenderbotForSequenceClassification expects eos token in input_ids SCREAMING_SNAKE_CASE : str = np.ones((1, 1) ) * model.config.eos_token_id SCREAMING_SNAKE_CASE : Dict = model(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ )
18
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCamelCase__ : Optional[Any] = { "configuration_funnel": ["FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP", "FunnelConfig"], "convert_funnel_original_tf_checkpoint_to_pytorch": [], "tokenization_funnel": ["FunnelTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Optional[Any] = ["FunnelTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Dict = [ "FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST", "FunnelBaseModel", "FunnelForMaskedLM", "FunnelForMultipleChoice", "FunnelForPreTraining", "FunnelForQuestionAnswering", "FunnelForSequenceClassification", "FunnelForTokenClassification", "FunnelModel", "FunnelPreTrainedModel", "load_tf_weights_in_funnel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = [ "TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST", "TFFunnelBaseModel", "TFFunnelForMaskedLM", "TFFunnelForMultipleChoice", "TFFunnelForPreTraining", "TFFunnelForQuestionAnswering", "TFFunnelForSequenceClassification", "TFFunnelForTokenClassification", "TFFunnelModel", "TFFunnelPreTrainedModel", ] if TYPE_CHECKING: from .configuration_funnel import FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP, FunnelConfig from .tokenization_funnel import FunnelTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_funnel_fast import FunnelTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_funnel import ( FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, FunnelBaseModel, FunnelForMaskedLM, FunnelForMultipleChoice, FunnelForPreTraining, FunnelForQuestionAnswering, FunnelForSequenceClassification, FunnelForTokenClassification, FunnelModel, FunnelPreTrainedModel, load_tf_weights_in_funnel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_funnel import ( TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, TFFunnelBaseModel, TFFunnelForMaskedLM, TFFunnelForMultipleChoice, TFFunnelForPreTraining, TFFunnelForQuestionAnswering, TFFunnelForSequenceClassification, TFFunnelForTokenClassification, TFFunnelModel, TFFunnelPreTrainedModel, ) else: import sys lowerCamelCase__ : Optional[int] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
18
1