code
stringlengths
81
54k
code_codestyle
int64
0
721
style_context
stringlengths
91
41.9k
style_context_codestyle
int64
0
699
label
int64
0
1
import importlib import json import os from collections import OrderedDict from typing import Dict, Optional, Union # Build the list of all image processors from ...configuration_utils import PretrainedConfig from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code from ...image_processing_utils import ImageProcessingMixin from ...utils import CONFIG_NAME, IMAGE_PROCESSOR_NAME, get_file_from_repo, logging from .auto_factory import _LazyAutoMapping from .configuration_auto import ( CONFIG_MAPPING_NAMES, AutoConfig, model_type_to_module_name, replace_list_option_in_docstrings, ) __A : Optional[int] = logging.get_logger(__name__) __A : Optional[int] = OrderedDict( [ ('align', 'EfficientNetImageProcessor'), ('beit', 'BeitImageProcessor'), ('bit', 'BitImageProcessor'), ('blip', 'BlipImageProcessor'), ('blip-2', 'BlipImageProcessor'), ('bridgetower', 'BridgeTowerImageProcessor'), ('chinese_clip', 'ChineseCLIPImageProcessor'), ('clip', 'CLIPImageProcessor'), ('clipseg', 'ViTImageProcessor'), ('conditional_detr', 'ConditionalDetrImageProcessor'), ('convnext', 'ConvNextImageProcessor'), ('convnextv2', 'ConvNextImageProcessor'), ('cvt', 'ConvNextImageProcessor'), ('data2vec-vision', 'BeitImageProcessor'), ('deformable_detr', 'DeformableDetrImageProcessor'), ('deit', 'DeiTImageProcessor'), ('deta', 'DetaImageProcessor'), ('detr', 'DetrImageProcessor'), ('dinat', 'ViTImageProcessor'), ('donut-swin', 'DonutImageProcessor'), ('dpt', 'DPTImageProcessor'), ('efficientformer', 'EfficientFormerImageProcessor'), ('efficientnet', 'EfficientNetImageProcessor'), ('flava', 'FlavaImageProcessor'), ('focalnet', 'BitImageProcessor'), ('git', 'CLIPImageProcessor'), ('glpn', 'GLPNImageProcessor'), ('groupvit', 'CLIPImageProcessor'), ('imagegpt', 'ImageGPTImageProcessor'), ('instructblip', 'BlipImageProcessor'), ('layoutlmv2', 'LayoutLMv2ImageProcessor'), ('layoutlmv3', 'LayoutLMv3ImageProcessor'), ('levit', 'LevitImageProcessor'), ('mask2former', 'Mask2FormerImageProcessor'), ('maskformer', 'MaskFormerImageProcessor'), ('mgp-str', 'ViTImageProcessor'), ('mobilenet_v1', 'MobileNetV1ImageProcessor'), ('mobilenet_v2', 'MobileNetV2ImageProcessor'), ('mobilevit', 'MobileViTImageProcessor'), ('mobilevit', 'MobileViTImageProcessor'), ('mobilevitv2', 'MobileViTImageProcessor'), ('nat', 'ViTImageProcessor'), ('oneformer', 'OneFormerImageProcessor'), ('owlvit', 'OwlViTImageProcessor'), ('perceiver', 'PerceiverImageProcessor'), ('pix2struct', 'Pix2StructImageProcessor'), ('poolformer', 'PoolFormerImageProcessor'), ('regnet', 'ConvNextImageProcessor'), ('resnet', 'ConvNextImageProcessor'), ('sam', 'SamImageProcessor'), ('segformer', 'SegformerImageProcessor'), ('swiftformer', 'ViTImageProcessor'), ('swin', 'ViTImageProcessor'), ('swin2sr', 'Swin2SRImageProcessor'), ('swinv2', 'ViTImageProcessor'), ('table-transformer', 'DetrImageProcessor'), ('timesformer', 'VideoMAEImageProcessor'), ('tvlt', 'TvltImageProcessor'), ('upernet', 'SegformerImageProcessor'), ('van', 'ConvNextImageProcessor'), ('videomae', 'VideoMAEImageProcessor'), ('vilt', 'ViltImageProcessor'), ('vit', 'ViTImageProcessor'), ('vit_hybrid', 'ViTHybridImageProcessor'), ('vit_mae', 'ViTImageProcessor'), ('vit_msn', 'ViTImageProcessor'), ('xclip', 'CLIPImageProcessor'), ('yolos', 'YolosImageProcessor'), ] ) __A : Union[str, Any] = _LazyAutoMapping(CONFIG_MAPPING_NAMES, IMAGE_PROCESSOR_MAPPING_NAMES) def __a ( A__ : str ): for module_name, extractors in IMAGE_PROCESSOR_MAPPING_NAMES.items(): if class_name in extractors: SCREAMING_SNAKE_CASE = model_type_to_module_name(A__ ) SCREAMING_SNAKE_CASE = importlib.import_module(F".{module_name}" , "transformers.models" ) try: return getattr(A__ , A__ ) except AttributeError: continue for _, extractor in IMAGE_PROCESSOR_MAPPING._extra_content.items(): if getattr(A__ , "__name__" , A__ ) == class_name: return extractor # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main # init and we return the proper dummy to get an appropriate error message. SCREAMING_SNAKE_CASE = importlib.import_module("transformers" ) if hasattr(A__ , A__ ): return getattr(A__ , A__ ) return None def __a ( A__ : Union[str, os.PathLike] , A__ : Optional[Union[str, os.PathLike]] = None , A__ : bool = False , A__ : bool = False , A__ : Optional[Dict[str, str]] = None , A__ : Optional[Union[bool, str]] = None , A__ : Optional[str] = None , A__ : bool = False , **A__ : str , ): SCREAMING_SNAKE_CASE = get_file_from_repo( A__ , A__ , cache_dir=A__ , force_download=A__ , resume_download=A__ , proxies=A__ , use_auth_token=A__ , revision=A__ , local_files_only=A__ , ) if resolved_config_file is None: logger.info( "Could not locate the image processor configuration file, will try to use the model config instead." ) return {} with open(A__ , encoding="utf-8" ) as reader: return json.load(A__ ) class _SCREAMING_SNAKE_CASE : '''simple docstring''' def __init__( self : Union[str, Any] ): raise EnvironmentError( "AutoImageProcessor is designed to be instantiated " "using the `AutoImageProcessor.from_pretrained(pretrained_model_name_or_path)` method." ) @classmethod @replace_list_option_in_docstrings(__lowerCamelCase ) def _snake_case ( cls : Any , __lowerCamelCase : Tuple , **__lowerCamelCase : Dict ): SCREAMING_SNAKE_CASE = kwargs.pop("config" , __lowerCamelCase ) SCREAMING_SNAKE_CASE = kwargs.pop("trust_remote_code" , __lowerCamelCase ) SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = ImageProcessingMixin.get_image_processor_dict(__lowerCamelCase , **__lowerCamelCase ) SCREAMING_SNAKE_CASE = config_dict.get("image_processor_type" , __lowerCamelCase ) SCREAMING_SNAKE_CASE = None if "AutoImageProcessor" in config_dict.get("auto_map" , {} ): SCREAMING_SNAKE_CASE = config_dict["auto_map"]["AutoImageProcessor"] # If we still don't have the image processor class, check if we're loading from a previous feature extractor config # and if so, infer the image processor class from there. if image_processor_class is None and image_processor_auto_map is None: SCREAMING_SNAKE_CASE = config_dict.pop("feature_extractor_type" , __lowerCamelCase ) if feature_extractor_class is not None: logger.warning( "Could not find image processor class in the image processor config or the model config. Loading" " based on pattern matching with the model's feature extractor configuration." ) SCREAMING_SNAKE_CASE = feature_extractor_class.replace("FeatureExtractor" , "ImageProcessor" ) if "AutoFeatureExtractor" in config_dict.get("auto_map" , {} ): SCREAMING_SNAKE_CASE = config_dict["auto_map"]["AutoFeatureExtractor"] SCREAMING_SNAKE_CASE = feature_extractor_auto_map.replace("FeatureExtractor" , "ImageProcessor" ) logger.warning( "Could not find image processor auto map in the image processor config or the model config." " Loading based on pattern matching with the model's feature extractor configuration." ) # If we don't find the image processor class in the image processor config, let's try the model config. if image_processor_class is None and image_processor_auto_map is None: if not isinstance(__lowerCamelCase , __lowerCamelCase ): SCREAMING_SNAKE_CASE = AutoConfig.from_pretrained(__lowerCamelCase , **__lowerCamelCase ) # It could be in `config.image_processor_type`` SCREAMING_SNAKE_CASE = getattr(__lowerCamelCase , "image_processor_type" , __lowerCamelCase ) if hasattr(__lowerCamelCase , "auto_map" ) and "AutoImageProcessor" in config.auto_map: SCREAMING_SNAKE_CASE = config.auto_map["AutoImageProcessor"] if image_processor_class is not None: SCREAMING_SNAKE_CASE = image_processor_class_from_name(__lowerCamelCase ) SCREAMING_SNAKE_CASE = image_processor_auto_map is not None SCREAMING_SNAKE_CASE = image_processor_class is not None or type(__lowerCamelCase ) in IMAGE_PROCESSOR_MAPPING SCREAMING_SNAKE_CASE = resolve_trust_remote_code( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) if has_remote_code and trust_remote_code: SCREAMING_SNAKE_CASE = get_class_from_dynamic_module( __lowerCamelCase , __lowerCamelCase , **__lowerCamelCase ) SCREAMING_SNAKE_CASE = kwargs.pop("code_revision" , __lowerCamelCase ) if os.path.isdir(__lowerCamelCase ): image_processor_class.register_for_auto_class() return image_processor_class.from_dict(__lowerCamelCase , **__lowerCamelCase ) elif image_processor_class is not None: return image_processor_class.from_dict(__lowerCamelCase , **__lowerCamelCase ) # Last try: we use the IMAGE_PROCESSOR_MAPPING. elif type(__lowerCamelCase ) in IMAGE_PROCESSOR_MAPPING: SCREAMING_SNAKE_CASE = IMAGE_PROCESSOR_MAPPING[type(__lowerCamelCase )] return image_processor_class.from_dict(__lowerCamelCase , **__lowerCamelCase ) raise ValueError( f"Unrecognized image processor in {pretrained_model_name_or_path}. Should have a " f"`image_processor_type` key in its {IMAGE_PROCESSOR_NAME} of {CONFIG_NAME}, or one of the following " f"`model_type` keys in its {CONFIG_NAME}: {', '.join(c for c in IMAGE_PROCESSOR_MAPPING_NAMES.keys() )}" ) @staticmethod def _snake_case ( __lowerCamelCase : Optional[Any] , __lowerCamelCase : Union[str, Any] ): IMAGE_PROCESSOR_MAPPING.register(__lowerCamelCase , __lowerCamelCase )
719
import unittest from transformers import MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING, AutoTokenizer, is_vision_available from transformers.pipelines import pipeline from transformers.pipelines.document_question_answering import apply_tesseract from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_detectrona, require_pytesseract, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image from transformers.image_utils import load_image else: class _SCREAMING_SNAKE_CASE : '''simple docstring''' @staticmethod def _snake_case ( *__lowerCamelCase : Optional[Any] , **__lowerCamelCase : Union[str, Any] ): pass def __a ( A__ : str ): return None # This is a pinned image from a specific revision of a document question answering space, hosted by HuggingFace, # so we can expect it to be available. __A : Tuple = ( 'https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png' ) @is_pipeline_test @require_torch @require_vision class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING @require_pytesseract @require_vision def _snake_case ( self : Optional[Any] , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : Dict , __lowerCamelCase : Optional[Any] ): SCREAMING_SNAKE_CASE = pipeline( "document-question-answering" , model=__lowerCamelCase , tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = list(zip(*apply_tesseract(load_image(__lowerCamelCase ) , __lowerCamelCase , "" ) ) ) SCREAMING_SNAKE_CASE = "What is the placebo?" SCREAMING_SNAKE_CASE = [ { "image": load_image(__lowerCamelCase ), "question": question, }, { "image": image, "question": question, }, { "image": image, "question": question, "word_boxes": word_boxes, }, ] return dqa_pipeline, examples def _snake_case ( self : List[Any] , __lowerCamelCase : int , __lowerCamelCase : int ): SCREAMING_SNAKE_CASE = dqa_pipeline(__lowerCamelCase , top_k=2 ) self.assertEqual( __lowerCamelCase , [ [ {"score": ANY(__lowerCamelCase ), "answer": ANY(__lowerCamelCase ), "start": ANY(__lowerCamelCase ), "end": ANY(__lowerCamelCase )}, {"score": ANY(__lowerCamelCase ), "answer": ANY(__lowerCamelCase ), "start": ANY(__lowerCamelCase ), "end": ANY(__lowerCamelCase )}, ] ] * 3 , ) @require_torch @require_detectrona @require_pytesseract def _snake_case ( self : Optional[int] ): SCREAMING_SNAKE_CASE = pipeline("document-question-answering" , model="hf-internal-testing/tiny-random-layoutlmv2" ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = "How many cats are there?" SCREAMING_SNAKE_CASE = [ {"score": 0.0_001, "answer": "oy 2312/2019", "start": 38, "end": 39}, {"score": 0.0_001, "answer": "oy 2312/2019 DUE", "start": 38, "end": 40}, ] SCREAMING_SNAKE_CASE = dqa_pipeline(image=__lowerCamelCase , question=__lowerCamelCase , top_k=2 ) self.assertEqual(nested_simplify(__lowerCamelCase , decimals=4 ) , __lowerCamelCase ) SCREAMING_SNAKE_CASE = dqa_pipeline({"image": image, "question": question} , top_k=2 ) self.assertEqual(nested_simplify(__lowerCamelCase , decimals=4 ) , __lowerCamelCase ) # This image does not detect ANY text in it, meaning layoutlmv2 should fail. # Empty answer probably SCREAMING_SNAKE_CASE = "./tests/fixtures/tests_samples/COCO/000000039769.png" SCREAMING_SNAKE_CASE = dqa_pipeline(image=__lowerCamelCase , question=__lowerCamelCase , top_k=2 ) self.assertEqual(__lowerCamelCase , [] ) # We can optionnally pass directly the words and bounding boxes SCREAMING_SNAKE_CASE = "./tests/fixtures/tests_samples/COCO/000000039769.png" SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = dqa_pipeline(image=__lowerCamelCase , question=__lowerCamelCase , words=__lowerCamelCase , boxes=__lowerCamelCase , top_k=2 ) self.assertEqual(__lowerCamelCase , [] ) @slow @require_torch @require_detectrona @require_pytesseract def _snake_case ( self : List[str] ): SCREAMING_SNAKE_CASE = pipeline( "document-question-answering" , model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa" , revision="9977165" , ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = "What is the invoice number?" SCREAMING_SNAKE_CASE = dqa_pipeline(image=__lowerCamelCase , question=__lowerCamelCase , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.9_944, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_009, "answer": "us-001", "start": 16, "end": 16}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline({"image": image, "question": question} , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.9_944, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_009, "answer": "us-001", "start": 16, "end": 16}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline( [{"image": image, "question": question}, {"image": image, "question": question}] , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ [ {"score": 0.9_944, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_009, "answer": "us-001", "start": 16, "end": 16}, ], ] * 2 , ) @slow @require_torch @require_detectrona @require_pytesseract def _snake_case ( self : str ): SCREAMING_SNAKE_CASE = pipeline( "document-question-answering" , model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa" , revision="9977165" , max_seq_len=50 , ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = "What is the invoice number?" SCREAMING_SNAKE_CASE = dqa_pipeline(image=__lowerCamelCase , question=__lowerCamelCase , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.9_974, "answer": "1110212019", "start": 23, "end": 23}, {"score": 0.9_948, "answer": "us-001", "start": 16, "end": 16}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline({"image": image, "question": question} , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.9_974, "answer": "1110212019", "start": 23, "end": 23}, {"score": 0.9_948, "answer": "us-001", "start": 16, "end": 16}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline( [{"image": image, "question": question}, {"image": image, "question": question}] , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ [ {"score": 0.9_974, "answer": "1110212019", "start": 23, "end": 23}, {"score": 0.9_948, "answer": "us-001", "start": 16, "end": 16}, ] ] * 2 , ) @slow @require_torch @require_pytesseract @require_vision def _snake_case ( self : str ): SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained( "impira/layoutlm-document-qa" , revision="3dc6de3" , add_prefix_space=__lowerCamelCase ) SCREAMING_SNAKE_CASE = pipeline( "document-question-answering" , model="impira/layoutlm-document-qa" , tokenizer=__lowerCamelCase , revision="3dc6de3" , ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = "What is the invoice number?" SCREAMING_SNAKE_CASE = dqa_pipeline(image=__lowerCamelCase , question=__lowerCamelCase , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.4_251, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_819, "answer": "1110212019", "start": 23, "end": 23}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline({"image": image, "question": question} , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.4_251, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_819, "answer": "1110212019", "start": 23, "end": 23}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline( [{"image": image, "question": question}, {"image": image, "question": question}] , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ [ {"score": 0.4_251, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_819, "answer": "1110212019", "start": 23, "end": 23}, ] ] * 2 , ) SCREAMING_SNAKE_CASE = list(zip(*apply_tesseract(load_image(__lowerCamelCase ) , __lowerCamelCase , "" ) ) ) # This model should also work if `image` is set to None SCREAMING_SNAKE_CASE = dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question} , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.4_251, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_819, "answer": "1110212019", "start": 23, "end": 23}, ] , ) @slow @require_torch @require_pytesseract @require_vision def _snake_case ( self : Dict ): SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained( "impira/layoutlm-document-qa" , revision="3dc6de3" , add_prefix_space=__lowerCamelCase ) SCREAMING_SNAKE_CASE = pipeline( "document-question-answering" , model="impira/layoutlm-document-qa" , tokenizer=__lowerCamelCase , revision="3dc6de3" , max_seq_len=50 , ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = "What is the invoice number?" SCREAMING_SNAKE_CASE = dqa_pipeline(image=__lowerCamelCase , question=__lowerCamelCase , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.9_999, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.9_998, "answer": "us-001", "start": 16, "end": 16}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline( [{"image": image, "question": question}, {"image": image, "question": question}] , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ [ {"score": 0.9_999, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.9_998, "answer": "us-001", "start": 16, "end": 16}, ] ] * 2 , ) SCREAMING_SNAKE_CASE = list(zip(*apply_tesseract(load_image(__lowerCamelCase ) , __lowerCamelCase , "" ) ) ) # This model should also work if `image` is set to None SCREAMING_SNAKE_CASE = dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question} , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.9_999, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.9_998, "answer": "us-001", "start": 16, "end": 16}, ] , ) @slow @require_torch def _snake_case ( self : List[str] ): SCREAMING_SNAKE_CASE = pipeline( "document-question-answering" , model="naver-clova-ix/donut-base-finetuned-docvqa" , tokenizer=AutoTokenizer.from_pretrained("naver-clova-ix/donut-base-finetuned-docvqa" ) , feature_extractor="naver-clova-ix/donut-base-finetuned-docvqa" , ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = "What is the invoice number?" SCREAMING_SNAKE_CASE = dqa_pipeline(image=__lowerCamelCase , question=__lowerCamelCase , top_k=2 ) self.assertEqual(nested_simplify(__lowerCamelCase , decimals=4 ) , [{"answer": "us-001"}] ) @require_tf @unittest.skip("Document question answering not implemented in TF" ) def _snake_case ( self : List[Any] ): pass
698
0
import argparse import ast import logging import os import sys import pandas as pd import torch from tqdm import tqdm from transformers import BartForConditionalGeneration, RagRetriever, RagSequenceForGeneration, RagTokenForGeneration from transformers import logging as transformers_logging sys.path.append(os.path.join(os.getcwd())) # noqa: E402 # isort:skip from utils_rag import exact_match_score, fa_score # noqa: E402 # isort:skip __A : Dict = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) transformers_logging.set_verbosity_info() def __a ( A__ : List[str] ): if "token" in model_name_or_path: return "rag_token" if "sequence" in model_name_or_path: return "rag_sequence" if "bart" in model_name_or_path: return "bart" return None def __a ( A__ : str , A__ : str , A__ : List[str] ): return max(metric_fn(A__ , A__ ) for gt in ground_truths ) def __a ( A__ : Union[str, Any] , A__ : Dict , A__ : Tuple ): SCREAMING_SNAKE_CASE = [line.strip() for line in open(A__ , "r" ).readlines()] SCREAMING_SNAKE_CASE = [] if args.gold_data_mode == "qa": SCREAMING_SNAKE_CASE = pd.read_csv(A__ , sep="\t" , header=A__ ) for answer_list in data[1]: SCREAMING_SNAKE_CASE = ast.literal_eval(A__ ) answers.append(A__ ) else: SCREAMING_SNAKE_CASE = [line.strip() for line in open(A__ , "r" ).readlines()] SCREAMING_SNAKE_CASE = [[reference] for reference in references] SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = 0 for prediction, ground_truths in zip(A__ , A__ ): total += 1 em += metric_max_over_ground_truths(A__ , A__ , A__ ) fa += metric_max_over_ground_truths(A__ , A__ , A__ ) SCREAMING_SNAKE_CASE = 100.0 * em / total SCREAMING_SNAKE_CASE = 100.0 * fa / total logger.info(F"F1: {fa:.2f}" ) logger.info(F"EM: {em:.2f}" ) def __a ( A__ : int , A__ : Optional[int] , A__ : Optional[int] ): SCREAMING_SNAKE_CASE = args.k SCREAMING_SNAKE_CASE = [line.strip() for line in open(A__ , "r" ).readlines()] SCREAMING_SNAKE_CASE = [line.strip() for line in open(A__ , "r" ).readlines()] SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = 0 for hypo, reference in zip(A__ , A__ ): SCREAMING_SNAKE_CASE = set(hypo.split("\t" )[:k] ) SCREAMING_SNAKE_CASE = set(reference.split("\t" ) ) total += 1 em += len(hypo_provenance & ref_provenance ) / k SCREAMING_SNAKE_CASE = 100.0 * em / total logger.info(F"Precision@{k}: {em: .2f}" ) def __a ( A__ : Optional[int] , A__ : Tuple , A__ : Tuple ): def strip_title(A__ : Optional[Any] ): if title.startswith("\"" ): SCREAMING_SNAKE_CASE = title[1:] if title.endswith("\"" ): SCREAMING_SNAKE_CASE = title[:-1] return title SCREAMING_SNAKE_CASE = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( A__ , return_tensors="pt" , padding=A__ , truncation=A__ , )["input_ids"].to(args.device ) SCREAMING_SNAKE_CASE = rag_model.rag.question_encoder(A__ ) SCREAMING_SNAKE_CASE = question_enc_outputs[0] SCREAMING_SNAKE_CASE = rag_model.retriever( A__ , question_enc_pool_output.cpu().detach().to(torch.floataa ).numpy() , prefix=rag_model.rag.generator.config.prefix , n_docs=rag_model.config.n_docs , return_tensors="pt" , ) SCREAMING_SNAKE_CASE = rag_model.retriever.index.get_doc_dicts(result.doc_ids ) SCREAMING_SNAKE_CASE = [] for docs in all_docs: SCREAMING_SNAKE_CASE = [strip_title(A__ ) for title in docs["title"]] provenance_strings.append("\t".join(A__ ) ) return provenance_strings def __a ( A__ : Tuple , A__ : int , A__ : Union[str, Any] ): with torch.no_grad(): SCREAMING_SNAKE_CASE = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( A__ , return_tensors="pt" , padding=A__ , truncation=A__ ) SCREAMING_SNAKE_CASE = inputs_dict.input_ids.to(args.device ) SCREAMING_SNAKE_CASE = inputs_dict.attention_mask.to(args.device ) SCREAMING_SNAKE_CASE = rag_model.generate( # rag_model overwrites generate A__ , attention_mask=A__ , num_beams=args.num_beams , min_length=args.min_length , max_length=args.max_length , early_stopping=A__ , num_return_sequences=1 , bad_words_ids=[[0, 0]] , ) SCREAMING_SNAKE_CASE = rag_model.retriever.generator_tokenizer.batch_decode(A__ , skip_special_tokens=A__ ) if args.print_predictions: for q, a in zip(A__ , A__ ): logger.info("Q: {} - A: {}".format(A__ , A__ ) ) return answers def __a ( ): SCREAMING_SNAKE_CASE = argparse.ArgumentParser() parser.add_argument( "--model_type" , choices=["rag_sequence", "rag_token", "bart"] , type=A__ , help=( "RAG model type: rag_sequence, rag_token or bart, if none specified, the type is inferred from the" " model_name_or_path" ) , ) parser.add_argument( "--index_name" , default=A__ , choices=["exact", "compressed", "legacy"] , type=A__ , help="RAG model retriever type" , ) parser.add_argument( "--index_path" , default=A__ , type=A__ , help="Path to the retrieval index" , ) parser.add_argument("--n_docs" , default=5 , type=A__ , help="Number of retrieved docs" ) parser.add_argument( "--model_name_or_path" , default=A__ , type=A__ , required=A__ , help="Path to pretrained checkpoints or model identifier from huggingface.co/models" , ) parser.add_argument( "--eval_mode" , choices=["e2e", "retrieval"] , default="e2e" , type=A__ , help=( "Evaluation mode, e2e calculates exact match and F1 of the downstream task, retrieval calculates" " precision@k." ) , ) parser.add_argument("--k" , default=1 , type=A__ , help="k for the precision@k calculation" ) parser.add_argument( "--evaluation_set" , default=A__ , type=A__ , required=A__ , help="Path to a file containing evaluation samples" , ) parser.add_argument( "--gold_data_path" , default=A__ , type=A__ , required=A__ , help="Path to a tab-separated file with gold samples" , ) parser.add_argument( "--gold_data_mode" , default="qa" , type=A__ , choices=["qa", "ans"] , help=( "Format of the gold data file" "qa - a single line in the following format: question [tab] answer_list" "ans - a single line of the gold file contains the expected answer string" ) , ) parser.add_argument( "--predictions_path" , type=A__ , default="predictions.txt" , help="Name of the predictions file, to be stored in the checkpoints directory" , ) parser.add_argument( "--eval_all_checkpoints" , action="store_true" , help="Evaluate all checkpoints starting with the same prefix as model_name ending and ending with step number" , ) parser.add_argument( "--eval_batch_size" , default=8 , type=A__ , help="Batch size per GPU/CPU for evaluation." , ) parser.add_argument( "--recalculate" , help="Recalculate predictions even if the prediction file exists" , action="store_true" , ) parser.add_argument( "--num_beams" , default=4 , type=A__ , help="Number of beams to be used when generating answers" , ) parser.add_argument("--min_length" , default=1 , type=A__ , help="Min length of the generated answers" ) parser.add_argument("--max_length" , default=50 , type=A__ , help="Max length of the generated answers" ) parser.add_argument( "--print_predictions" , action="store_true" , help="If True, prints predictions while evaluating." , ) parser.add_argument( "--print_docs" , action="store_true" , help="If True, prints docs retried while generating." , ) SCREAMING_SNAKE_CASE = parser.parse_args() SCREAMING_SNAKE_CASE = torch.device("cuda" if torch.cuda.is_available() else "cpu" ) return args def __a ( A__ : int ): SCREAMING_SNAKE_CASE = {} if args.model_type is None: SCREAMING_SNAKE_CASE = infer_model_type(args.model_name_or_path ) assert args.model_type is not None if args.model_type.startswith("rag" ): SCREAMING_SNAKE_CASE = RagTokenForGeneration if args.model_type == "rag_token" else RagSequenceForGeneration SCREAMING_SNAKE_CASE = args.n_docs if args.index_name is not None: SCREAMING_SNAKE_CASE = args.index_name if args.index_path is not None: SCREAMING_SNAKE_CASE = args.index_path else: SCREAMING_SNAKE_CASE = BartForConditionalGeneration SCREAMING_SNAKE_CASE = ( [f.path for f in os.scandir(args.model_name_or_path ) if f.is_dir()] if args.eval_all_checkpoints else [args.model_name_or_path] ) logger.info("Evaluate the following checkpoints: %s" , A__ ) SCREAMING_SNAKE_CASE = get_scores if args.eval_mode == "e2e" else get_precision_at_k SCREAMING_SNAKE_CASE = evaluate_batch_eae if args.eval_mode == "e2e" else evaluate_batch_retrieval for checkpoint in checkpoints: if os.path.exists(args.predictions_path ) and (not args.recalculate): logger.info("Calculating metrics based on an existing predictions file: {}".format(args.predictions_path ) ) score_fn(A__ , args.predictions_path , args.gold_data_path ) continue logger.info("***** Running evaluation for {} *****".format(A__ ) ) logger.info(" Batch size = %d" , args.eval_batch_size ) logger.info(" Predictions will be stored under {}".format(args.predictions_path ) ) if args.model_type.startswith("rag" ): SCREAMING_SNAKE_CASE = RagRetriever.from_pretrained(A__ , **A__ ) SCREAMING_SNAKE_CASE = model_class.from_pretrained(A__ , retriever=A__ , **A__ ) model.retriever.init_retrieval() else: SCREAMING_SNAKE_CASE = model_class.from_pretrained(A__ , **A__ ) model.to(args.device ) with open(args.evaluation_set , "r" ) as eval_file, open(args.predictions_path , "w" ) as preds_file: SCREAMING_SNAKE_CASE = [] for line in tqdm(A__ ): questions.append(line.strip() ) if len(A__ ) == args.eval_batch_size: SCREAMING_SNAKE_CASE = evaluate_batch_fn(A__ , A__ , A__ ) preds_file.write("\n".join(A__ ) + "\n" ) preds_file.flush() SCREAMING_SNAKE_CASE = [] if len(A__ ) > 0: SCREAMING_SNAKE_CASE = evaluate_batch_fn(A__ , A__ , A__ ) preds_file.write("\n".join(A__ ) ) preds_file.flush() score_fn(A__ , args.predictions_path , args.gold_data_path ) if __name__ == "__main__": __A : Tuple = get_args() main(args)
720
# 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 torch from ..models.auto import AutoModelForSequenceClassification, AutoTokenizer from .base import PipelineTool class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' lowerCamelCase__ = "facebook/bart-large-mnli" lowerCamelCase__ = ( "This is a tool that classifies an English text using provided labels. It takes two inputs: `text`, which " "should be the text to classify, and `labels`, which should be the list of labels to use for classification. " "It returns the most likely label in the list of provided `labels` for the input text." ) lowerCamelCase__ = "text_classifier" lowerCamelCase__ = AutoTokenizer lowerCamelCase__ = AutoModelForSequenceClassification lowerCamelCase__ = ["text", ["text"]] lowerCamelCase__ = ["text"] def _snake_case ( self : Optional[Any] ): super().setup() SCREAMING_SNAKE_CASE = self.model.config SCREAMING_SNAKE_CASE = -1 for idx, label in config.idalabel.items(): if label.lower().startswith("entail" ): SCREAMING_SNAKE_CASE = int(__lowerCamelCase ) if self.entailment_id == -1: raise ValueError("Could not determine the entailment ID from the model config, please pass it at init." ) def _snake_case ( self : List[Any] , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : List[Any] ): SCREAMING_SNAKE_CASE = labels return self.pre_processor( [text] * len(__lowerCamelCase ) , [f"This example is {label}" for label in labels] , return_tensors="pt" , padding="max_length" , ) def _snake_case ( self : str , __lowerCamelCase : Optional[int] ): SCREAMING_SNAKE_CASE = outputs.logits SCREAMING_SNAKE_CASE = torch.argmax(logits[:, 2] ).item() return self._labels[label_id]
698
0
import math def __a ( A__ : int ): '''simple docstring''' assert isinstance(A__ , A__ ) and ( number >= 0 ), "'number' must been an int and positive" if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or not number % 2: # Negatives, 0, 1 and all even numbers are not primes return False SCREAMING_SNAKE_CASE = range(3 , int(math.sqrt(A__ ) + 1 ) , 2 ) return not any(not number % i for i in odd_numbers ) def __a ( A__ : Optional[int] , A__ : List[Any]=1 , **A__ : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE = factor * value SCREAMING_SNAKE_CASE = value while not is_prime(A__ ): value += 1 if not ("desc" in kwargs and kwargs["desc"] is True) else -1 if value == first_value_val: return next_prime(value + 1 , **A__ ) return value
721
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os from accelerate.test_utils import execute_subprocess_async def __a ( A__ : str=None ): if subparsers is not None: SCREAMING_SNAKE_CASE = subparsers.add_parser("test" ) else: SCREAMING_SNAKE_CASE = argparse.ArgumentParser("Accelerate test command" ) parser.add_argument( "--config_file" , default=A__ , help=( "The path to use to store the config file. Will default to a file named default_config.yaml in the cache " "location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have " "such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed " "with 'huggingface'." ) , ) if subparsers is not None: parser.set_defaults(func=A__ ) return parser def __a ( A__ : Tuple ): SCREAMING_SNAKE_CASE = os.path.sep.join(__file__.split(os.path.sep )[:-2] + ["test_utils", "scripts", "test_script.py"] ) if args.config_file is None: SCREAMING_SNAKE_CASE = script_name else: SCREAMING_SNAKE_CASE = F"--config_file={args.config_file} {script_name}" SCREAMING_SNAKE_CASE = ["accelerate-launch"] + test_args.split() SCREAMING_SNAKE_CASE = execute_subprocess_async(A__ , env=os.environ.copy() ) if result.returncode == 0: print("Test is a success! You are ready for your distributed training!" ) def __a ( ): SCREAMING_SNAKE_CASE = test_command_parser() SCREAMING_SNAKE_CASE = parser.parse_args() test_command(A__ ) if __name__ == "__main__": main()
698
0
import math def __a ( A__ : int ): if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(A__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def __a ( A__ : float = 0.1 ): SCREAMING_SNAKE_CASE = 3 SCREAMING_SNAKE_CASE = 3 while primes / (2 * j - 1) >= ratio: for i in range(j * j + j + 1 , (j + 2) * (j + 2) , j + 1 ): primes += is_prime(A__ ) j += 2 return j if __name__ == "__main__": import doctest doctest.testmod()
700
import json import os from functools import lru_cache from typing import TYPE_CHECKING, List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation __A : Optional[int] = logging.get_logger(__name__) __A : List[str] = { 'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_config_file': 'tokenizer_config.json', } __A : Tuple = { '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' }, } __A : Dict = {'facebook/blenderbot-3B': 1_2_8} @lru_cache() # Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode def __a ( ): SCREAMING_SNAKE_CASE = ( list(range(ord("!" ) , ord("~" ) + 1 ) ) + list(range(ord("¡" ) , ord("¬" ) + 1 ) ) + list(range(ord("®" ) , ord("ÿ" ) + 1 ) ) ) SCREAMING_SNAKE_CASE = bs[:] SCREAMING_SNAKE_CASE = 0 for b in range(2**8 ): if b not in bs: bs.append(A__ ) cs.append(2**8 + n ) n += 1 SCREAMING_SNAKE_CASE = [chr(A__ ) for n in cs] return dict(zip(A__ , A__ ) ) def __a ( A__ : Optional[int] ): SCREAMING_SNAKE_CASE = set() SCREAMING_SNAKE_CASE = word[0] for char in word[1:]: pairs.add((prev_char, char) ) SCREAMING_SNAKE_CASE = char return pairs class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' lowerCamelCase__ = VOCAB_FILES_NAMES lowerCamelCase__ = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase__ = ["input_ids", "attention_mask"] def __init__( self : int , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : str , __lowerCamelCase : List[Any]="replace" , __lowerCamelCase : Any="<s>" , __lowerCamelCase : List[Any]="</s>" , __lowerCamelCase : Dict="</s>" , __lowerCamelCase : Any="<s>" , __lowerCamelCase : Optional[Any]="<unk>" , __lowerCamelCase : str="<pad>" , __lowerCamelCase : List[Any]="<mask>" , __lowerCamelCase : List[Any]=False , **__lowerCamelCase : Tuple , ): SCREAMING_SNAKE_CASE = AddedToken(__lowerCamelCase , lstrip=__lowerCamelCase , rstrip=__lowerCamelCase ) if isinstance(__lowerCamelCase , __lowerCamelCase ) else bos_token SCREAMING_SNAKE_CASE = AddedToken(__lowerCamelCase , lstrip=__lowerCamelCase , rstrip=__lowerCamelCase ) if isinstance(__lowerCamelCase , __lowerCamelCase ) else eos_token SCREAMING_SNAKE_CASE = AddedToken(__lowerCamelCase , lstrip=__lowerCamelCase , rstrip=__lowerCamelCase ) if isinstance(__lowerCamelCase , __lowerCamelCase ) else sep_token SCREAMING_SNAKE_CASE = AddedToken(__lowerCamelCase , lstrip=__lowerCamelCase , rstrip=__lowerCamelCase ) if isinstance(__lowerCamelCase , __lowerCamelCase ) else cls_token SCREAMING_SNAKE_CASE = AddedToken(__lowerCamelCase , lstrip=__lowerCamelCase , rstrip=__lowerCamelCase ) if isinstance(__lowerCamelCase , __lowerCamelCase ) else unk_token SCREAMING_SNAKE_CASE = AddedToken(__lowerCamelCase , lstrip=__lowerCamelCase , rstrip=__lowerCamelCase ) if isinstance(__lowerCamelCase , __lowerCamelCase ) else pad_token # Mask token behave like a normal word, i.e. include the space before it SCREAMING_SNAKE_CASE = AddedToken(__lowerCamelCase , lstrip=__lowerCamelCase , rstrip=__lowerCamelCase ) if isinstance(__lowerCamelCase , __lowerCamelCase ) else mask_token super().__init__( errors=__lowerCamelCase , bos_token=__lowerCamelCase , eos_token=__lowerCamelCase , unk_token=__lowerCamelCase , sep_token=__lowerCamelCase , cls_token=__lowerCamelCase , pad_token=__lowerCamelCase , mask_token=__lowerCamelCase , add_prefix_space=__lowerCamelCase , **__lowerCamelCase , ) with open(__lowerCamelCase , encoding="utf-8" ) as vocab_handle: SCREAMING_SNAKE_CASE = json.load(__lowerCamelCase ) SCREAMING_SNAKE_CASE = {v: k for k, v in self.encoder.items()} SCREAMING_SNAKE_CASE = errors # how to handle errors in decoding SCREAMING_SNAKE_CASE = bytes_to_unicode() SCREAMING_SNAKE_CASE = {v: k for k, v in self.byte_encoder.items()} with open(__lowerCamelCase , encoding="utf-8" ) as merges_handle: SCREAMING_SNAKE_CASE = merges_handle.read().split("\n" )[1:-1] SCREAMING_SNAKE_CASE = [tuple(merge.split() ) for merge in bpe_merges] SCREAMING_SNAKE_CASE = dict(zip(__lowerCamelCase , range(len(__lowerCamelCase ) ) ) ) SCREAMING_SNAKE_CASE = {} SCREAMING_SNAKE_CASE = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions SCREAMING_SNAKE_CASE = re.compile(r"'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+" ) @property # Copied from transformers.models.roberta.tokenization_roberta.RobertaTokenizer.vocab_size with Roberta->Blenderbot, RoBERTa->Blenderbot def _snake_case ( self : Dict ): return len(self.encoder ) def _snake_case ( self : int ): return dict(self.encoder , **self.added_tokens_encoder ) def _snake_case ( self : Tuple , __lowerCamelCase : List[str] ): if token in self.cache: return self.cache[token] SCREAMING_SNAKE_CASE = tuple(__lowerCamelCase ) SCREAMING_SNAKE_CASE = get_pairs(__lowerCamelCase ) if not pairs: return token while True: SCREAMING_SNAKE_CASE = min(__lowerCamelCase , key=lambda __lowerCamelCase : self.bpe_ranks.get(__lowerCamelCase , float("inf" ) ) ) if bigram not in self.bpe_ranks: break SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = bigram SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = 0 while i < len(__lowerCamelCase ): try: SCREAMING_SNAKE_CASE = word.index(__lowerCamelCase , __lowerCamelCase ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) SCREAMING_SNAKE_CASE = j if word[i] == first and i < len(__lowerCamelCase ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 SCREAMING_SNAKE_CASE = tuple(__lowerCamelCase ) SCREAMING_SNAKE_CASE = new_word if len(__lowerCamelCase ) == 1: break else: SCREAMING_SNAKE_CASE = get_pairs(__lowerCamelCase ) SCREAMING_SNAKE_CASE = " ".join(__lowerCamelCase ) SCREAMING_SNAKE_CASE = word return word def _snake_case ( self : str , __lowerCamelCase : Dict ): SCREAMING_SNAKE_CASE = [] for token in re.findall(self.pat , __lowerCamelCase ): SCREAMING_SNAKE_CASE = "".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(__lowerCamelCase ).split(" " ) ) return bpe_tokens def _snake_case ( self : List[str] , __lowerCamelCase : str ): return self.encoder.get(__lowerCamelCase , self.encoder.get(self.unk_token ) ) def _snake_case ( self : Optional[int] , __lowerCamelCase : Tuple ): return self.decoder.get(__lowerCamelCase ) def _snake_case ( self : Tuple , __lowerCamelCase : str ): SCREAMING_SNAKE_CASE = "".join(__lowerCamelCase ) SCREAMING_SNAKE_CASE = bytearray([self.byte_decoder[c] for c in text] ).decode("utf-8" , errors=self.errors ) return text def _snake_case ( self : Union[str, Any] , __lowerCamelCase : str , __lowerCamelCase : Optional[str] = None ): if not os.path.isdir(__lowerCamelCase ): logger.error(f"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE = os.path.join( __lowerCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) SCREAMING_SNAKE_CASE = os.path.join( __lowerCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"] ) with open(__lowerCamelCase , "w" , encoding="utf-8" ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=__lowerCamelCase , ensure_ascii=__lowerCamelCase ) + "\n" ) SCREAMING_SNAKE_CASE = 0 with open(__lowerCamelCase , "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 __lowerCamelCase : 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!" ) SCREAMING_SNAKE_CASE = token_index writer.write(" ".join(__lowerCamelCase ) + "\n" ) index += 1 return vocab_file, merge_file def _snake_case ( self : Tuple , __lowerCamelCase : List[int] , __lowerCamelCase : Optional[List[int]] = None , __lowerCamelCase : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__lowerCamelCase , token_ids_a=__lowerCamelCase , already_has_special_tokens=__lowerCamelCase ) if token_ids_a is None: return [1] + ([0] * len(__lowerCamelCase )) + [1] return [1] + ([0] * len(__lowerCamelCase )) + [1, 1] + ([0] * len(__lowerCamelCase )) + [1] def _snake_case ( self : str , __lowerCamelCase : List[int] , __lowerCamelCase : Optional[List[int]] = None ): SCREAMING_SNAKE_CASE = [self.sep_token_id] SCREAMING_SNAKE_CASE = [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 : Any , __lowerCamelCase : Optional[Any] , __lowerCamelCase : Dict=False , **__lowerCamelCase : Optional[int] ): SCREAMING_SNAKE_CASE = kwargs.pop("add_prefix_space" , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(__lowerCamelCase ) > 0 and not text[0].isspace()): SCREAMING_SNAKE_CASE = " " + text return (text, kwargs) def _snake_case ( self : Dict , __lowerCamelCase : List[int] , __lowerCamelCase : Optional[List[int]] = None ): return token_ids_a + [self.eos_token_id] def _snake_case ( self : Tuple , __lowerCamelCase : "Conversation" ): SCREAMING_SNAKE_CASE = [] 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(__lowerCamelCase ) SCREAMING_SNAKE_CASE = " ".join(__lowerCamelCase ) SCREAMING_SNAKE_CASE = self.encode(__lowerCamelCase ) if len(__lowerCamelCase ) > self.model_max_length: SCREAMING_SNAKE_CASE = 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
698
0
import shutil import tempfile import unittest import numpy as np import pytest from transformers.testing_utils import require_vision from transformers.utils import is_vision_available if is_vision_available(): from PIL import Image from transformers import AutoProcessor, BertTokenizer, BlipImageProcessor, BlipProcessor, PreTrainedTokenizerFast @require_vision class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' def _snake_case ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE = tempfile.mkdtemp() SCREAMING_SNAKE_CASE = BlipImageProcessor() SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained("hf-internal-testing/tiny-random-BertModel" ) SCREAMING_SNAKE_CASE = BlipProcessor(__lowerCamelCase , __lowerCamelCase ) processor.save_pretrained(self.tmpdirname ) def _snake_case ( self : Dict , **__lowerCamelCase : Any ): return AutoProcessor.from_pretrained(self.tmpdirname , **__lowerCamelCase ).tokenizer def _snake_case ( self : List[Any] , **__lowerCamelCase : Optional[Any] ): return AutoProcessor.from_pretrained(self.tmpdirname , **__lowerCamelCase ).image_processor def _snake_case ( self : Union[str, Any] ): shutil.rmtree(self.tmpdirname ) def _snake_case ( self : Tuple ): SCREAMING_SNAKE_CASE = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] SCREAMING_SNAKE_CASE = [Image.fromarray(np.moveaxis(__lowerCamelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def _snake_case ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE = BlipProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) SCREAMING_SNAKE_CASE = self.get_tokenizer(bos_token="(BOS)" , eos_token="(EOS)" ) SCREAMING_SNAKE_CASE = self.get_image_processor(do_normalize=__lowerCamelCase , padding_value=1.0 ) SCREAMING_SNAKE_CASE = BlipProcessor.from_pretrained( self.tmpdirname , bos_token="(BOS)" , eos_token="(EOS)" , do_normalize=__lowerCamelCase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __lowerCamelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __lowerCamelCase ) def _snake_case ( self : Optional[int] ): SCREAMING_SNAKE_CASE = self.get_image_processor() SCREAMING_SNAKE_CASE = self.get_tokenizer() SCREAMING_SNAKE_CASE = BlipProcessor(tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) SCREAMING_SNAKE_CASE = self.prepare_image_inputs() SCREAMING_SNAKE_CASE = image_processor(__lowerCamelCase , return_tensors="np" ) SCREAMING_SNAKE_CASE = processor(images=__lowerCamelCase , return_tensors="np" ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def _snake_case ( self : Dict ): SCREAMING_SNAKE_CASE = self.get_image_processor() SCREAMING_SNAKE_CASE = self.get_tokenizer() SCREAMING_SNAKE_CASE = BlipProcessor(tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) SCREAMING_SNAKE_CASE = "lower newer" SCREAMING_SNAKE_CASE = processor(text=__lowerCamelCase ) SCREAMING_SNAKE_CASE = tokenizer(__lowerCamelCase , return_token_type_ids=__lowerCamelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def _snake_case ( self : str ): SCREAMING_SNAKE_CASE = self.get_image_processor() SCREAMING_SNAKE_CASE = self.get_tokenizer() SCREAMING_SNAKE_CASE = BlipProcessor(tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) SCREAMING_SNAKE_CASE = "lower newer" SCREAMING_SNAKE_CASE = self.prepare_image_inputs() SCREAMING_SNAKE_CASE = processor(text=__lowerCamelCase , images=__lowerCamelCase ) self.assertListEqual(list(inputs.keys() ) , ["pixel_values", "input_ids", "attention_mask"] ) # test if it raises when no input is passed with pytest.raises(__lowerCamelCase ): processor() def _snake_case ( self : Any ): SCREAMING_SNAKE_CASE = self.get_image_processor() SCREAMING_SNAKE_CASE = self.get_tokenizer() SCREAMING_SNAKE_CASE = BlipProcessor(tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] SCREAMING_SNAKE_CASE = processor.batch_decode(__lowerCamelCase ) SCREAMING_SNAKE_CASE = tokenizer.batch_decode(__lowerCamelCase ) self.assertListEqual(__lowerCamelCase , __lowerCamelCase ) def _snake_case ( self : Dict ): SCREAMING_SNAKE_CASE = self.get_image_processor() SCREAMING_SNAKE_CASE = self.get_tokenizer() SCREAMING_SNAKE_CASE = BlipProcessor(tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) SCREAMING_SNAKE_CASE = "lower newer" SCREAMING_SNAKE_CASE = self.prepare_image_inputs() SCREAMING_SNAKE_CASE = processor(text=__lowerCamelCase , images=__lowerCamelCase ) # For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask'] self.assertListEqual(list(inputs.keys() ) , ["pixel_values", "input_ids", "attention_mask"] )
701
from __future__ import annotations from cmath import sqrt def __a ( A__ : int , A__ : int , A__ : int ): if a == 0: raise ValueError("Coefficient 'a' must not be zero." ) SCREAMING_SNAKE_CASE = b * b - 4 * a * c SCREAMING_SNAKE_CASE = (-b + sqrt(A__ )) / (2 * a) SCREAMING_SNAKE_CASE = (-b - sqrt(A__ )) / (2 * a) return ( root_a.real if not root_a.imag else root_a, root_a.real if not root_a.imag else root_a, ) def __a ( ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = quadratic_roots(a=5 , b=6 , c=1 ) print(F"The solutions are: {solutiona} and {solutiona}" ) if __name__ == "__main__": main()
698
0
import random import unittest import torch from diffusers import IFInpaintingSuperResolutionPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class _SCREAMING_SNAKE_CASE ( __snake_case , __snake_case , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = IFInpaintingSuperResolutionPipeline lowerCamelCase__ = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"width", "height"} lowerCamelCase__ = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({"original_image"} ) lowerCamelCase__ = PipelineTesterMixin.required_optional_params - {"latents"} def _snake_case ( self : Tuple ): return self._get_superresolution_dummy_components() def _snake_case ( self : int , __lowerCamelCase : List[Any] , __lowerCamelCase : int=0 ): if str(__lowerCamelCase ).startswith("mps" ): SCREAMING_SNAKE_CASE = torch.manual_seed(__lowerCamelCase ) else: SCREAMING_SNAKE_CASE = torch.Generator(device=__lowerCamelCase ).manual_seed(__lowerCamelCase ) SCREAMING_SNAKE_CASE = floats_tensor((1, 3, 16, 16) , rng=random.Random(__lowerCamelCase ) ).to(__lowerCamelCase ) SCREAMING_SNAKE_CASE = floats_tensor((1, 3, 32, 32) , rng=random.Random(__lowerCamelCase ) ).to(__lowerCamelCase ) SCREAMING_SNAKE_CASE = floats_tensor((1, 3, 32, 32) , rng=random.Random(__lowerCamelCase ) ).to(__lowerCamelCase ) SCREAMING_SNAKE_CASE = { "prompt": "A painting of a squirrel eating a burger", "image": image, "original_image": original_image, "mask_image": mask_image, "generator": generator, "num_inference_steps": 2, "output_type": "numpy", } return inputs @unittest.skipIf( torch_device != "cuda" or not is_xformers_available() , reason="XFormers attention is only available with CUDA and `xformers` installed" , ) def _snake_case ( self : Optional[Any] ): self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) def _snake_case ( self : int ): self._test_save_load_optional_components() @unittest.skipIf(torch_device != "cuda" , reason="float16 requires CUDA" ) def _snake_case ( self : Optional[int] ): # 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 _snake_case ( self : Optional[Any] ): self._test_attention_slicing_forward_pass(expected_max_diff=1e-2 ) def _snake_case ( self : List[Any] ): self._test_save_load_local() def _snake_case ( self : Any ): self._test_inference_batch_single_identical( expected_max_diff=1e-2 , )
702
import unittest from queue import Empty from threading import Thread from transformers import AutoTokenizer, TextIteratorStreamer, TextStreamer, is_torch_available from transformers.testing_utils import CaptureStdout, require_torch, torch_device from ..test_modeling_common import ids_tensor if is_torch_available(): import torch from transformers import AutoModelForCausalLM @require_torch class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' def _snake_case ( self : Dict ): SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2" ) SCREAMING_SNAKE_CASE = AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2" ).to(__lowerCamelCase ) SCREAMING_SNAKE_CASE = -1 SCREAMING_SNAKE_CASE = ids_tensor((1, 5) , vocab_size=model.config.vocab_size ).to(__lowerCamelCase ) SCREAMING_SNAKE_CASE = model.generate(__lowerCamelCase , max_new_tokens=10 , do_sample=__lowerCamelCase ) SCREAMING_SNAKE_CASE = tokenizer.decode(greedy_ids[0] ) with CaptureStdout() as cs: SCREAMING_SNAKE_CASE = TextStreamer(__lowerCamelCase ) model.generate(__lowerCamelCase , max_new_tokens=10 , do_sample=__lowerCamelCase , streamer=__lowerCamelCase ) # The greedy text should be printed to stdout, except for the final "\n" in the streamer SCREAMING_SNAKE_CASE = cs.out[:-1] self.assertEqual(__lowerCamelCase , __lowerCamelCase ) def _snake_case ( self : int ): SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2" ) SCREAMING_SNAKE_CASE = AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2" ).to(__lowerCamelCase ) SCREAMING_SNAKE_CASE = -1 SCREAMING_SNAKE_CASE = ids_tensor((1, 5) , vocab_size=model.config.vocab_size ).to(__lowerCamelCase ) SCREAMING_SNAKE_CASE = model.generate(__lowerCamelCase , max_new_tokens=10 , do_sample=__lowerCamelCase ) SCREAMING_SNAKE_CASE = tokenizer.decode(greedy_ids[0] ) SCREAMING_SNAKE_CASE = TextIteratorStreamer(__lowerCamelCase ) SCREAMING_SNAKE_CASE = {"input_ids": input_ids, "max_new_tokens": 10, "do_sample": False, "streamer": streamer} SCREAMING_SNAKE_CASE = Thread(target=model.generate , kwargs=__lowerCamelCase ) thread.start() SCREAMING_SNAKE_CASE = "" for new_text in streamer: streamer_text += new_text self.assertEqual(__lowerCamelCase , __lowerCamelCase ) def _snake_case ( self : List[str] ): SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2" ) SCREAMING_SNAKE_CASE = AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2" ).to(__lowerCamelCase ) SCREAMING_SNAKE_CASE = -1 SCREAMING_SNAKE_CASE = ids_tensor((1, 5) , vocab_size=model.config.vocab_size ).to(__lowerCamelCase ) SCREAMING_SNAKE_CASE = model.generate(__lowerCamelCase , max_new_tokens=10 , do_sample=__lowerCamelCase ) SCREAMING_SNAKE_CASE = greedy_ids[:, input_ids.shape[1] :] SCREAMING_SNAKE_CASE = tokenizer.decode(new_greedy_ids[0] ) with CaptureStdout() as cs: SCREAMING_SNAKE_CASE = TextStreamer(__lowerCamelCase , skip_prompt=__lowerCamelCase ) model.generate(__lowerCamelCase , max_new_tokens=10 , do_sample=__lowerCamelCase , streamer=__lowerCamelCase ) # The greedy text should be printed to stdout, except for the final "\n" in the streamer SCREAMING_SNAKE_CASE = cs.out[:-1] self.assertEqual(__lowerCamelCase , __lowerCamelCase ) def _snake_case ( self : int ): # Tests that we can pass `decode_kwargs` to the streamer to control how the tokens are decoded. Must be tested # with actual models -- the dummy models' tokenizers are not aligned with their models, and # `skip_special_tokens=True` has no effect on them SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("distilgpt2" ) SCREAMING_SNAKE_CASE = AutoModelForCausalLM.from_pretrained("distilgpt2" ).to(__lowerCamelCase ) SCREAMING_SNAKE_CASE = -1 SCREAMING_SNAKE_CASE = torch.ones((1, 5) , device=__lowerCamelCase ).long() * model.config.bos_token_id with CaptureStdout() as cs: SCREAMING_SNAKE_CASE = TextStreamer(__lowerCamelCase , skip_special_tokens=__lowerCamelCase ) model.generate(__lowerCamelCase , max_new_tokens=1 , do_sample=__lowerCamelCase , streamer=__lowerCamelCase ) # The prompt contains a special token, so the streamer should not print it. As such, the output text, when # re-tokenized, must only contain one token SCREAMING_SNAKE_CASE = cs.out[:-1] # Remove the final "\n" SCREAMING_SNAKE_CASE = tokenizer(__lowerCamelCase , return_tensors="pt" ) self.assertEqual(streamer_text_tokenized.input_ids.shape , (1, 1) ) def _snake_case ( self : Optional[int] ): SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2" ) SCREAMING_SNAKE_CASE = AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2" ).to(__lowerCamelCase ) SCREAMING_SNAKE_CASE = -1 SCREAMING_SNAKE_CASE = ids_tensor((1, 5) , vocab_size=model.config.vocab_size ).to(__lowerCamelCase ) SCREAMING_SNAKE_CASE = TextIteratorStreamer(__lowerCamelCase , timeout=0.001 ) SCREAMING_SNAKE_CASE = {"input_ids": input_ids, "max_new_tokens": 10, "do_sample": False, "streamer": streamer} SCREAMING_SNAKE_CASE = Thread(target=model.generate , kwargs=__lowerCamelCase ) thread.start() # The streamer will timeout after 0.001 seconds, so an exception will be raised with self.assertRaises(__lowerCamelCase ): SCREAMING_SNAKE_CASE = "" for new_text in streamer: streamer_text += new_text
698
0
def __a ( A__ : int ): return sum(i for i in range(1 , number // 2 + 1 ) if number % i == 0 ) == number if __name__ == "__main__": print('Program to check whether a number is a Perfect number or not...') __A : Dict = int(input('Enter number: ').strip()) print(f'{number} is {"" if perfect(number) else "not "}a Perfect Number.')
703
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) __A : Union[str, Any] = {'configuration_fnet': ['FNET_PRETRAINED_CONFIG_ARCHIVE_MAP', 'FNetConfig']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[str] = ['FNetTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Any = ['FNetTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Union[str, Any] = [ 'FNET_PRETRAINED_MODEL_ARCHIVE_LIST', 'FNetForMaskedLM', 'FNetForMultipleChoice', 'FNetForNextSentencePrediction', 'FNetForPreTraining', 'FNetForQuestionAnswering', 'FNetForSequenceClassification', 'FNetForTokenClassification', 'FNetLayer', 'FNetModel', 'FNetPreTrainedModel', ] if TYPE_CHECKING: from .configuration_fnet import FNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FNetConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_fnet import FNetTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_fnet_fast import FNetTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_fnet import ( FNET_PRETRAINED_MODEL_ARCHIVE_LIST, FNetForMaskedLM, FNetForMultipleChoice, FNetForNextSentencePrediction, FNetForPreTraining, FNetForQuestionAnswering, FNetForSequenceClassification, FNetForTokenClassification, FNetLayer, FNetModel, FNetPreTrainedModel, ) else: import sys __A : Dict = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
698
0
import json import multiprocessing import os import re from collections import defaultdict import torch from accelerate import Accelerator from accelerate.utils import set_seed from arguments import HumanEvalArguments from datasets import load_dataset, load_metric from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from tqdm import tqdm import transformers from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, StoppingCriteria, StoppingCriteriaList __A : str = ['\nclass', '\ndef', '\n#', '\n@', '\nprint', '\nif'] class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' def __init__( self : int , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : str , __lowerCamelCase : Optional[int]=None , __lowerCamelCase : Dict=1 ): SCREAMING_SNAKE_CASE = tokenizer SCREAMING_SNAKE_CASE = dataset SCREAMING_SNAKE_CASE = len(__lowerCamelCase ) if n_tasks is None else n_tasks SCREAMING_SNAKE_CASE = n_copies def __iter__( self : Optional[Any] ): SCREAMING_SNAKE_CASE = [] for task in range(self.n_tasks ): # without strip, the model generate commented codes ... prompts.append(self.tokenizer.eos_token + self.dataset[task]["prompt"].strip() ) SCREAMING_SNAKE_CASE = self.tokenizer(__lowerCamelCase , padding=__lowerCamelCase , return_tensors="pt" ) for task in range(self.n_tasks ): for _ in range(self.n_copies ): yield { "ids": outputs.input_ids[task], "task_id": task, "input_len": outputs.attention_mask[task].sum(), } class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' def __init__( self : List[str] , __lowerCamelCase : int , __lowerCamelCase : List[Any] , __lowerCamelCase : List[Any] ): SCREAMING_SNAKE_CASE = start_length SCREAMING_SNAKE_CASE = eof_strings SCREAMING_SNAKE_CASE = tokenizer def __call__( self : Tuple , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : List[str] , **__lowerCamelCase : Tuple ): SCREAMING_SNAKE_CASE = self.tokenizer.batch_decode(input_ids[:, self.start_length :] ) SCREAMING_SNAKE_CASE = [] for decoded_generation in decoded_generations: done.append(any(stop_string in decoded_generation for stop_string in self.eof_strings ) ) return all(__lowerCamelCase ) def __a ( A__ : Dict ): SCREAMING_SNAKE_CASE = re.split("(%s)" % "|".join(A__ ) , A__ ) # last string should be "" return "".join(string_list[:-2] ) def __a ( A__ : Any , A__ : Tuple , A__ : Any , A__ : Dict , A__ : str , A__ : Dict=20 , **A__ : Union[str, Any] ): SCREAMING_SNAKE_CASE = defaultdict(A__ ) # dict of list of generated tokens for step, batch in tqdm(enumerate(A__ ) ): with torch.no_grad(): SCREAMING_SNAKE_CASE = batch["ids"].shape[-1] SCREAMING_SNAKE_CASE = accelerator.unwrap_model(A__ ).generate( input_ids=batch["ids"][:, : batch["input_len"]] , num_return_sequences=A__ , **A__ ) # each task is generated batch_size times SCREAMING_SNAKE_CASE = batch["task_id"].repeat(A__ ) SCREAMING_SNAKE_CASE = accelerator.pad_across_processes( A__ , dim=1 , pad_index=tokenizer.pad_token_id ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = accelerator.gather((generated_tokens, generated_tasks) ) SCREAMING_SNAKE_CASE = generated_tokens.cpu().numpy() SCREAMING_SNAKE_CASE = generated_tasks.cpu().numpy() for task, generated_tokens in zip(A__ , A__ ): gen_token_dict[task].append(A__ ) SCREAMING_SNAKE_CASE = [[] for _ in range(A__ )] for task, generated_tokens in gen_token_dict.items(): for s in generated_tokens: SCREAMING_SNAKE_CASE = tokenizer.decode(A__ , skip_special_tokens=A__ , clean_up_tokenization_spaces=A__ ) code_gens[task].append(remove_last_block(A__ ) ) return code_gens def __a ( ): # Setup configuration SCREAMING_SNAKE_CASE = HfArgumentParser(A__ ) SCREAMING_SNAKE_CASE = parser.parse_args() transformers.logging.set_verbosity_error() # enables code execution in code_eval metric SCREAMING_SNAKE_CASE = args.HF_ALLOW_CODE_EVAL # make sure tokenizer plays nice with multiprocessing SCREAMING_SNAKE_CASE = "false" if args.num_workers is None: SCREAMING_SNAKE_CASE = multiprocessing.cpu_count() # Use dataset load to feed to accelerate SCREAMING_SNAKE_CASE = Accelerator() set_seed(args.seed , device_specific=A__ ) # Load model and tokenizer SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained(args.model_ckpt ) SCREAMING_SNAKE_CASE = tokenizer.eos_token SCREAMING_SNAKE_CASE = AutoModelForCausalLM.from_pretrained(args.model_ckpt ) # Generation settings SCREAMING_SNAKE_CASE = { "do_sample": args.do_sample, "temperature": args.temperature, "max_new_tokens": args.max_new_tokens, "top_p": args.top_p, "top_k": args.top_k, "stopping_criteria": StoppingCriteriaList([EndOfFunctionCriteria(0 , A__ , A__ )] ), } # Load evaluation dataset and metric SCREAMING_SNAKE_CASE = load_dataset("openai_humaneval" ) SCREAMING_SNAKE_CASE = load_metric("code_eval" ) SCREAMING_SNAKE_CASE = args.num_tasks if args.num_tasks is not None else len(human_eval["test"] ) SCREAMING_SNAKE_CASE = args.n_samples // args.batch_size SCREAMING_SNAKE_CASE = TokenizedDataset(A__ , human_eval["test"] , n_copies=A__ , n_tasks=A__ ) # do not confuse args.batch_size, which is actually the num_return_sequences SCREAMING_SNAKE_CASE = DataLoader(A__ , batch_size=1 ) # Run a quick test to see if code evaluation is enabled try: SCREAMING_SNAKE_CASE = code_eval_metric.compute(references=[""] , predictions=[[""]] ) except ValueError as exception: print( "Code evaluation not enabled. Read the warning below carefully and then use `--HF_ALLOW_CODE_EVAL=\"1\"`" " flag to enable code evaluation." ) raise exception SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = accelerator.prepare(A__ , A__ ) SCREAMING_SNAKE_CASE = complete_code( A__ , A__ , A__ , A__ , n_tasks=A__ , batch_size=args.batch_size , **A__ , ) if accelerator.is_main_process: SCREAMING_SNAKE_CASE = [] for task in tqdm(range(A__ ) ): SCREAMING_SNAKE_CASE = human_eval["test"][task]["test"] SCREAMING_SNAKE_CASE = F"check({human_eval['test'][task]['entry_point']})" references.append("\n" + test_func + "\n" + entry_point ) # Evaluate completions with "code_eval" metric SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = code_eval_metric.compute( references=A__ , predictions=A__ , num_workers=args.num_workers ) print(F"Results: {pass_at_k}" ) # Save results to json file with open(args.output_file , "w" ) as fp: json.dump(A__ , A__ ) # For some reason the folliwng seems to be necessary sometimes for code_eval to work nice with multiprocessing # https://stackoverflow.com/questions/60804599/python-multiprocessing-keeps-spawning-the-whole-script if __name__ == "__main__": main()
704
from collections import Counter import numpy as np from sklearn import datasets from sklearn.model_selection import train_test_split __A : Optional[Any] = datasets.load_iris() __A : Optional[Any] = np.array(data['data']) __A : Optional[int] = np.array(data['target']) __A : Union[str, Any] = data['target_names'] __A , __A , __A , __A : Optional[int] = train_test_split(X, y) def __a ( A__ : Optional[int] , A__ : Dict ): return np.linalg.norm(np.array(A__ ) - np.array(A__ ) ) def __a ( A__ : Optional[Any] , A__ : int , A__ : Dict , A__ : Optional[Any] , A__ : Dict=5 ): SCREAMING_SNAKE_CASE = zip(A__ , A__ ) # List of distances of all points from the point to be classified SCREAMING_SNAKE_CASE = [] for data_point in data: SCREAMING_SNAKE_CASE = euclidean_distance(data_point[0] , A__ ) distances.append((distance, data_point[1]) ) # Choosing 'k' points with the least distances. SCREAMING_SNAKE_CASE = [i[1] for i in sorted(A__ )[:k]] # Most commonly occurring class among them # is the class into which the point is classified SCREAMING_SNAKE_CASE = Counter(A__ ).most_common(1 )[0][0] return classes[result] if __name__ == "__main__": print(classifier(X_train, y_train, classes, [4.4, 3.1, 1.3, 1.4]))
698
0
from __future__ import annotations import requests __A : Optional[int] = set( 'approved_at_utc approved_by author_flair_background_color\nauthor_flair_css_class author_flair_richtext author_flair_template_id author_fullname\nauthor_premium can_mod_post category clicked content_categories created_utc downs\nedited gilded gildings hidden hide_score is_created_from_ads_ui is_meta\nis_original_content is_reddit_media_domain is_video link_flair_css_class\nlink_flair_richtext link_flair_text link_flair_text_color media_embed mod_reason_title\nname permalink pwls quarantine saved score secure_media secure_media_embed selftext\nsubreddit subreddit_name_prefixed subreddit_type thumbnail title top_awarded_type\ntotal_awards_received ups upvote_ratio url user_reports'.split() ) def __a ( A__ : str , A__ : int = 1 , A__ : str = "new" , A__ : list | None = None ): SCREAMING_SNAKE_CASE = wanted_data or [] if invalid_search_terms := ", ".join(sorted(set(A__ ) - valid_terms ) ): SCREAMING_SNAKE_CASE = F"Invalid search term: {invalid_search_terms}" raise ValueError(A__ ) SCREAMING_SNAKE_CASE = requests.get( F"https://reddit.com/r/{subreddit}/{age}.json?limit={limit}" , headers={"User-agent": "A random string"} , ) if response.status_code == 429: raise requests.HTTPError SCREAMING_SNAKE_CASE = response.json() if not wanted_data: return {id_: data["data"]["children"][id_] for id_ in range(A__ )} SCREAMING_SNAKE_CASE = {} for id_ in range(A__ ): SCREAMING_SNAKE_CASE = { item: data["data"]["children"][id_]["data"][item] for item in wanted_data } return data_dict if __name__ == "__main__": # If you get Error 429, that means you are rate limited.Try after some time print(get_subreddit_data('learnpython', wanted_data=['title', 'url', 'selftext']))
705
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices __A : Union[str, Any] = logging.get_logger(__name__) __A : Dict = { 'microsoft/resnet-50': 'https://huggingface.co/microsoft/resnet-50/blob/main/config.json', } class _SCREAMING_SNAKE_CASE ( __snake_case , __snake_case ): '''simple docstring''' lowerCamelCase__ = "resnet" lowerCamelCase__ = ["basic", "bottleneck"] def __init__( self : Optional[Any] , __lowerCamelCase : int=3 , __lowerCamelCase : Dict=64 , __lowerCamelCase : str=[256, 512, 1024, 2048] , __lowerCamelCase : str=[3, 4, 6, 3] , __lowerCamelCase : Optional[int]="bottleneck" , __lowerCamelCase : int="relu" , __lowerCamelCase : List[str]=False , __lowerCamelCase : Optional[int]=None , __lowerCamelCase : int=None , **__lowerCamelCase : Dict , ): super().__init__(**__lowerCamelCase ) if layer_type not in self.layer_types: raise ValueError(f"layer_type={layer_type} is not one of {','.join(self.layer_types )}" ) SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = embedding_size SCREAMING_SNAKE_CASE = hidden_sizes SCREAMING_SNAKE_CASE = depths SCREAMING_SNAKE_CASE = layer_type SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = downsample_in_first_stage SCREAMING_SNAKE_CASE = ["stem"] + [f"stage{idx}" for idx in range(1 , len(__lowerCamelCase ) + 1 )] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = get_aligned_output_features_output_indices( out_features=__lowerCamelCase , out_indices=__lowerCamelCase , stage_names=self.stage_names ) class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' lowerCamelCase__ = version.parse("1.11" ) @property def _snake_case ( self : Optional[int] ): return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def _snake_case ( self : Optional[int] ): return 1e-3
698
0
import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, BatchEncoding, MBartaaTokenizer, MBartaaTokenizerFast, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, slow, ) from ...test_tokenization_common import TokenizerTesterMixin __A : List[Any] = get_tests_dir('fixtures/test_sentencepiece.model') if is_torch_available(): from transformers.models.mbart.modeling_mbart import shift_tokens_right __A : Optional[Any] = 2_5_0_0_0_4 __A : int = 2_5_0_0_2_0 @require_sentencepiece @require_tokenizers class _SCREAMING_SNAKE_CASE ( __snake_case , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = MBartaaTokenizer lowerCamelCase__ = MBartaaTokenizerFast lowerCamelCase__ = True lowerCamelCase__ = True def _snake_case ( self : Optional[int] ): super().setUp() # We have a SentencePiece fixture for testing SCREAMING_SNAKE_CASE = MBartaaTokenizer(__lowerCamelCase , src_lang="en_XX" , tgt_lang="ro_RO" , keep_accents=__lowerCamelCase ) tokenizer.save_pretrained(self.tmpdirname ) def _snake_case ( self : List[Any] ): SCREAMING_SNAKE_CASE = "<s>" SCREAMING_SNAKE_CASE = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__lowerCamelCase ) , __lowerCamelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__lowerCamelCase ) , __lowerCamelCase ) def _snake_case ( self : List[Any] ): SCREAMING_SNAKE_CASE = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , "<s>" ) self.assertEqual(vocab_keys[1] , "<pad>" ) self.assertEqual(vocab_keys[-1] , "<mask>" ) self.assertEqual(len(__lowerCamelCase ) , 1054 ) def _snake_case ( self : List[Any] ): self.assertEqual(self.get_tokenizer().vocab_size , 1054 ) def _snake_case ( self : List[str] ): SCREAMING_SNAKE_CASE = MBartaaTokenizer(__lowerCamelCase , src_lang="en_XX" , tgt_lang="ro_RO" , keep_accents=__lowerCamelCase ) SCREAMING_SNAKE_CASE = tokenizer.tokenize("This is a test" ) self.assertListEqual(__lowerCamelCase , ["▁This", "▁is", "▁a", "▁t", "est"] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__lowerCamelCase ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) SCREAMING_SNAKE_CASE = tokenizer.tokenize("I was born in 92000, and this is falsé." ) self.assertListEqual( __lowerCamelCase , [SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "9", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "é", "."] , ) SCREAMING_SNAKE_CASE = tokenizer.convert_tokens_to_ids(__lowerCamelCase ) self.assertListEqual( __lowerCamelCase , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) SCREAMING_SNAKE_CASE = tokenizer.convert_ids_to_tokens(__lowerCamelCase ) self.assertListEqual( __lowerCamelCase , [SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "<unk>", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "<unk>", "."] , ) @slow def _snake_case ( self : List[Any] ): # fmt: off SCREAMING_SNAKE_CASE = {"input_ids": [[250004, 11062, 82772, 7, 15, 82772, 538, 51529, 237, 17198, 1290, 206, 9, 215175, 1314, 136, 17198, 1290, 206, 9, 56359, 42, 122009, 9, 16466, 16, 87344, 4537, 9, 4717, 78381, 6, 159958, 7, 15, 24480, 618, 4, 527, 22693, 5428, 4, 2777, 24480, 9874, 4, 43523, 594, 4, 803, 18392, 33189, 18, 4, 43523, 24447, 12399, 100, 24955, 83658, 9626, 144057, 15, 839, 22335, 16, 136, 24955, 83658, 83479, 15, 39102, 724, 16, 678, 645, 2789, 1328, 4589, 42, 122009, 115774, 23, 805, 1328, 46876, 7, 136, 53894, 1940, 42227, 41159, 17721, 823, 425, 4, 27512, 98722, 206, 136, 5531, 4970, 919, 17336, 5, 2], [250004, 20080, 618, 83, 82775, 47, 479, 9, 1517, 73, 53894, 333, 80581, 110117, 18811, 5256, 1295, 51, 152526, 297, 7986, 390, 124416, 538, 35431, 214, 98, 15044, 25737, 136, 7108, 43701, 23, 756, 135355, 7, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [250004, 581, 63773, 119455, 6, 147797, 88203, 7, 645, 70, 21, 3285, 10269, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=__lowerCamelCase , model_name="facebook/mbart-large-50" , revision="d3913889c59cd5c9e456b269c376325eabad57e2" , ) def _snake_case ( self : Tuple ): if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return SCREAMING_SNAKE_CASE = (self.rust_tokenizer_class, "hf-internal-testing/tiny-random-mbart50", {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})" ): SCREAMING_SNAKE_CASE = self.rust_tokenizer_class.from_pretrained(__lowerCamelCase , **__lowerCamelCase ) SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained(__lowerCamelCase , **__lowerCamelCase ) SCREAMING_SNAKE_CASE = tempfile.mkdtemp() SCREAMING_SNAKE_CASE = tokenizer_r.save_pretrained(__lowerCamelCase ) SCREAMING_SNAKE_CASE = tokenizer_p.save_pretrained(__lowerCamelCase ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any("tokenizer.json" in f for f in tokenizer_r_files ) ) SCREAMING_SNAKE_CASE = tuple(f for f in tokenizer_r_files if "tokenizer.json" not in f ) self.assertSequenceEqual(__lowerCamelCase , __lowerCamelCase ) # Checks everything loads correctly in the same way SCREAMING_SNAKE_CASE = tokenizer_r.from_pretrained(__lowerCamelCase ) SCREAMING_SNAKE_CASE = tokenizer_p.from_pretrained(__lowerCamelCase ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(__lowerCamelCase , __lowerCamelCase ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(__lowerCamelCase ) # Save tokenizer rust, legacy_format=True SCREAMING_SNAKE_CASE = tempfile.mkdtemp() SCREAMING_SNAKE_CASE = tokenizer_r.save_pretrained(__lowerCamelCase , legacy_format=__lowerCamelCase ) SCREAMING_SNAKE_CASE = tokenizer_p.save_pretrained(__lowerCamelCase ) # Checks it save with the same files self.assertSequenceEqual(__lowerCamelCase , __lowerCamelCase ) # Checks everything loads correctly in the same way SCREAMING_SNAKE_CASE = tokenizer_r.from_pretrained(__lowerCamelCase ) SCREAMING_SNAKE_CASE = tokenizer_p.from_pretrained(__lowerCamelCase ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(__lowerCamelCase , __lowerCamelCase ) ) shutil.rmtree(__lowerCamelCase ) # Save tokenizer rust, legacy_format=False SCREAMING_SNAKE_CASE = tempfile.mkdtemp() SCREAMING_SNAKE_CASE = tokenizer_r.save_pretrained(__lowerCamelCase , legacy_format=__lowerCamelCase ) SCREAMING_SNAKE_CASE = tokenizer_p.save_pretrained(__lowerCamelCase ) # Checks it saved the tokenizer.json file self.assertTrue(any("tokenizer.json" in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way SCREAMING_SNAKE_CASE = tokenizer_r.from_pretrained(__lowerCamelCase ) SCREAMING_SNAKE_CASE = tokenizer_p.from_pretrained(__lowerCamelCase ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(__lowerCamelCase , __lowerCamelCase ) ) shutil.rmtree(__lowerCamelCase ) @require_torch @require_sentencepiece @require_tokenizers class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = "facebook/mbart-large-50-one-to-many-mmt" lowerCamelCase__ = [ " UN Chief Says There Is No Military Solution in Syria", " Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for Syria is that \"there is no military solution\" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.", ] lowerCamelCase__ = [ "Şeful ONU declară că nu există o soluţie militară în Siria", "Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei" " pentru Siria este că \"nu există o soluţie militară\" la conflictul de aproape cinci ani şi că noi arme nu vor" " face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.", ] lowerCamelCase__ = [EN_CODE, 8_2_7_4, 1_2_7_8_7_3, 2_5_9_1_6, 7, 8_6_2_2, 2_0_7_1, 4_3_8, 6_7_4_8_5, 5_3, 1_8_7_8_9_5, 2_3, 5_1_7_1_2, 2] @classmethod def _snake_case ( cls : List[str] ): SCREAMING_SNAKE_CASE = MBartaaTokenizer.from_pretrained( cls.checkpoint_name , src_lang="en_XX" , tgt_lang="ro_RO" ) SCREAMING_SNAKE_CASE = 1 return cls def _snake_case ( self : Tuple ): self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["ar_AR"] , 250001 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["en_EN"] , 250004 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["ro_RO"] , 250020 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["mr_IN"] , 250038 ) def _snake_case ( self : Dict ): SCREAMING_SNAKE_CASE = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , __lowerCamelCase ) def _snake_case ( self : Optional[Any] ): self.assertIn(__lowerCamelCase , self.tokenizer.all_special_ids ) SCREAMING_SNAKE_CASE = [RO_CODE, 884, 9019, 96, 9, 916, 86792, 36, 18743, 15596, 5, 2] SCREAMING_SNAKE_CASE = self.tokenizer.decode(__lowerCamelCase , skip_special_tokens=__lowerCamelCase ) SCREAMING_SNAKE_CASE = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=__lowerCamelCase ) self.assertEqual(__lowerCamelCase , __lowerCamelCase ) self.assertNotIn(self.tokenizer.eos_token , __lowerCamelCase ) def _snake_case ( self : List[Any] ): SCREAMING_SNAKE_CASE = ["this is gunna be a long sentence " * 20] assert isinstance(src_text[0] , __lowerCamelCase ) SCREAMING_SNAKE_CASE = 10 SCREAMING_SNAKE_CASE = self.tokenizer(__lowerCamelCase , max_length=__lowerCamelCase , truncation=__lowerCamelCase ).input_ids[0] self.assertEqual(ids[0] , __lowerCamelCase ) self.assertEqual(ids[-1] , 2 ) self.assertEqual(len(__lowerCamelCase ) , __lowerCamelCase ) def _snake_case ( self : Any ): self.assertListEqual(self.tokenizer.convert_tokens_to_ids(["<mask>", "ar_AR"] ) , [250053, 250001] ) def _snake_case ( self : List[Any] ): SCREAMING_SNAKE_CASE = tempfile.mkdtemp() SCREAMING_SNAKE_CASE = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(__lowerCamelCase ) SCREAMING_SNAKE_CASE = MBartaaTokenizer.from_pretrained(__lowerCamelCase ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , __lowerCamelCase ) @require_torch def _snake_case ( self : List[str] ): SCREAMING_SNAKE_CASE = self.tokenizer(self.src_text , text_target=self.tgt_text , padding=__lowerCamelCase , return_tensors="pt" ) SCREAMING_SNAKE_CASE = shift_tokens_right(batch["labels"] , self.tokenizer.pad_token_id ) # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 assert batch.input_ids[1][0] == EN_CODE assert batch.input_ids[1][-1] == 2 assert batch.labels[1][0] == RO_CODE assert batch.labels[1][-1] == 2 assert batch.decoder_input_ids[1][:2].tolist() == [2, RO_CODE] @require_torch def _snake_case ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=__lowerCamelCase , truncation=__lowerCamelCase , max_length=len(self.expected_src_tokens ) , return_tensors="pt" , ) SCREAMING_SNAKE_CASE = shift_tokens_right(batch["labels"] , self.tokenizer.pad_token_id ) self.assertIsInstance(__lowerCamelCase , __lowerCamelCase ) self.assertEqual((2, 14) , batch.input_ids.shape ) self.assertEqual((2, 14) , batch.attention_mask.shape ) SCREAMING_SNAKE_CASE = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , __lowerCamelCase ) self.assertEqual(2 , batch.decoder_input_ids[0, 0] ) # decoder_start_token_id # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [EN_CODE] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) def _snake_case ( self : Optional[int] ): SCREAMING_SNAKE_CASE = self.tokenizer(self.src_text , padding=__lowerCamelCase , truncation=__lowerCamelCase , max_length=3 , return_tensors="pt" ) SCREAMING_SNAKE_CASE = self.tokenizer( text_target=self.tgt_text , padding=__lowerCamelCase , truncation=__lowerCamelCase , max_length=10 , return_tensors="pt" ) SCREAMING_SNAKE_CASE = targets["input_ids"] SCREAMING_SNAKE_CASE = shift_tokens_right(__lowerCamelCase , self.tokenizer.pad_token_id ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.decoder_input_ids.shape[1] , 10 ) @require_torch def _snake_case ( self : List[Any] ): SCREAMING_SNAKE_CASE = self.tokenizer._build_translation_inputs( "A test" , return_tensors="pt" , src_lang="en_XX" , tgt_lang="ar_AR" ) self.assertEqual( nested_simplify(__lowerCamelCase ) , { # en_XX, A, test, EOS "input_ids": [[250004, 62, 3034, 2]], "attention_mask": [[1, 1, 1, 1]], # ar_AR "forced_bos_token_id": 250001, } , )
706
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, TensorType __A : str = logging.get_logger(__name__) __A : Optional[Any] = { 'openai/imagegpt-small': '', 'openai/imagegpt-medium': '', 'openai/imagegpt-large': '', } class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' lowerCamelCase__ = "imagegpt" lowerCamelCase__ = ["past_key_values"] lowerCamelCase__ = { "hidden_size": "n_embd", "max_position_embeddings": "n_positions", "num_attention_heads": "n_head", "num_hidden_layers": "n_layer", } def __init__( self : List[str] , __lowerCamelCase : Any=512 + 1 , __lowerCamelCase : str=32 * 32 , __lowerCamelCase : Any=512 , __lowerCamelCase : Optional[int]=24 , __lowerCamelCase : Tuple=8 , __lowerCamelCase : List[str]=None , __lowerCamelCase : List[Any]="quick_gelu" , __lowerCamelCase : int=0.1 , __lowerCamelCase : str=0.1 , __lowerCamelCase : Optional[int]=0.1 , __lowerCamelCase : Optional[Any]=1e-5 , __lowerCamelCase : Optional[Any]=0.02 , __lowerCamelCase : Optional[Any]=True , __lowerCamelCase : Optional[int]=True , __lowerCamelCase : List[Any]=False , __lowerCamelCase : int=False , __lowerCamelCase : int=False , **__lowerCamelCase : Union[str, Any] , ): SCREAMING_SNAKE_CASE = vocab_size SCREAMING_SNAKE_CASE = n_positions SCREAMING_SNAKE_CASE = n_embd SCREAMING_SNAKE_CASE = n_layer SCREAMING_SNAKE_CASE = n_head SCREAMING_SNAKE_CASE = n_inner SCREAMING_SNAKE_CASE = activation_function SCREAMING_SNAKE_CASE = resid_pdrop SCREAMING_SNAKE_CASE = embd_pdrop SCREAMING_SNAKE_CASE = attn_pdrop SCREAMING_SNAKE_CASE = layer_norm_epsilon SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = scale_attn_weights SCREAMING_SNAKE_CASE = use_cache SCREAMING_SNAKE_CASE = scale_attn_by_inverse_layer_idx SCREAMING_SNAKE_CASE = reorder_and_upcast_attn SCREAMING_SNAKE_CASE = tie_word_embeddings super().__init__(tie_word_embeddings=__lowerCamelCase , **__lowerCamelCase ) class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' @property def _snake_case ( self : Optional[Any] ): return OrderedDict( [ ("input_ids", {0: "batch", 1: "sequence"}), ] ) def _snake_case ( self : Optional[int] , __lowerCamelCase : "FeatureExtractionMixin" , __lowerCamelCase : int = 1 , __lowerCamelCase : int = -1 , __lowerCamelCase : bool = False , __lowerCamelCase : Optional["TensorType"] = None , __lowerCamelCase : int = 3 , __lowerCamelCase : int = 32 , __lowerCamelCase : int = 32 , ): SCREAMING_SNAKE_CASE = self._generate_dummy_images(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) SCREAMING_SNAKE_CASE = dict(preprocessor(images=__lowerCamelCase , return_tensors=__lowerCamelCase ) ) return inputs
698
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available __A : str = { 'configuration_groupvit': [ 'GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GroupViTConfig', 'GroupViTOnnxConfig', 'GroupViTTextConfig', 'GroupViTVisionConfig', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Any = [ 'GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'GroupViTModel', 'GroupViTPreTrainedModel', 'GroupViTTextModel', 'GroupViTVisionModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Union[str, Any] = [ 'TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFGroupViTModel', 'TFGroupViTPreTrainedModel', 'TFGroupViTTextModel', 'TFGroupViTVisionModel', ] if TYPE_CHECKING: from .configuration_groupvit import ( GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GroupViTConfig, GroupViTOnnxConfig, GroupViTTextConfig, GroupViTVisionConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_groupvit import ( GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, GroupViTModel, GroupViTPreTrainedModel, GroupViTTextModel, GroupViTVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_groupvit import ( TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFGroupViTModel, TFGroupViTPreTrainedModel, TFGroupViTTextModel, TFGroupViTVisionModel, ) else: import sys __A : int = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
707
from transformers import BertTokenizer, EncoderDecoderModel, SeqaSeqTrainer, SeqaSeqTrainingArguments from transformers.testing_utils import TestCasePlus, require_torch, slow from transformers.utils import is_datasets_available if is_datasets_available(): import datasets class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' @slow @require_torch def _snake_case ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE = EncoderDecoderModel.from_encoder_decoder_pretrained("prajjwal1/bert-tiny" , "prajjwal1/bert-tiny" ) SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained("bert-base-uncased" ) SCREAMING_SNAKE_CASE = bertabert.config.encoder.vocab_size SCREAMING_SNAKE_CASE = tokenizer.sep_token_id SCREAMING_SNAKE_CASE = tokenizer.cls_token_id SCREAMING_SNAKE_CASE = 128 SCREAMING_SNAKE_CASE = datasets.load_dataset("cnn_dailymail" , "3.0.0" , split="train[:1%]" ) SCREAMING_SNAKE_CASE = datasets.load_dataset("cnn_dailymail" , "3.0.0" , split="validation[:1%]" ) SCREAMING_SNAKE_CASE = train_dataset.select(range(32 ) ) SCREAMING_SNAKE_CASE = val_dataset.select(range(16 ) ) SCREAMING_SNAKE_CASE = 4 def _map_to_encoder_decoder_inputs(__lowerCamelCase : str ): # Tokenizer will automatically set [BOS] <text> [EOS] SCREAMING_SNAKE_CASE = tokenizer(batch["article"] , padding="max_length" , truncation=__lowerCamelCase , max_length=512 ) SCREAMING_SNAKE_CASE = tokenizer(batch["highlights"] , padding="max_length" , truncation=__lowerCamelCase , max_length=128 ) SCREAMING_SNAKE_CASE = inputs.input_ids SCREAMING_SNAKE_CASE = inputs.attention_mask SCREAMING_SNAKE_CASE = outputs.input_ids SCREAMING_SNAKE_CASE = outputs.input_ids.copy() SCREAMING_SNAKE_CASE = [ [-100 if token == tokenizer.pad_token_id else token for token in labels] for labels in batch["labels"] ] SCREAMING_SNAKE_CASE = outputs.attention_mask assert all(len(__lowerCamelCase ) == 512 for x in inputs.input_ids ) assert all(len(__lowerCamelCase ) == 128 for x in outputs.input_ids ) return batch def _compute_metrics(__lowerCamelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE = pred.label_ids SCREAMING_SNAKE_CASE = pred.predictions # all unnecessary tokens are removed SCREAMING_SNAKE_CASE = tokenizer.batch_decode(__lowerCamelCase , skip_special_tokens=__lowerCamelCase ) SCREAMING_SNAKE_CASE = tokenizer.batch_decode(__lowerCamelCase , skip_special_tokens=__lowerCamelCase ) SCREAMING_SNAKE_CASE = sum([int(pred_str[i] == label_str[i] ) for i in range(len(__lowerCamelCase ) )] ) / len(__lowerCamelCase ) return {"accuracy": accuracy} # map train dataset SCREAMING_SNAKE_CASE = train_dataset.map( _map_to_encoder_decoder_inputs , batched=__lowerCamelCase , batch_size=__lowerCamelCase , remove_columns=["article", "highlights"] , ) train_dataset.set_format( type="torch" , columns=["input_ids", "attention_mask", "decoder_input_ids", "decoder_attention_mask", "labels"] , ) # same for validation dataset SCREAMING_SNAKE_CASE = val_dataset.map( _map_to_encoder_decoder_inputs , batched=__lowerCamelCase , batch_size=__lowerCamelCase , remove_columns=["article", "highlights"] , ) val_dataset.set_format( type="torch" , columns=["input_ids", "attention_mask", "decoder_input_ids", "decoder_attention_mask", "labels"] , ) SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE = SeqaSeqTrainingArguments( output_dir=__lowerCamelCase , per_device_train_batch_size=__lowerCamelCase , per_device_eval_batch_size=__lowerCamelCase , predict_with_generate=__lowerCamelCase , evaluation_strategy="steps" , do_train=__lowerCamelCase , do_eval=__lowerCamelCase , warmup_steps=0 , eval_steps=2 , logging_steps=2 , ) # instantiate trainer SCREAMING_SNAKE_CASE = SeqaSeqTrainer( model=__lowerCamelCase , args=__lowerCamelCase , compute_metrics=_compute_metrics , train_dataset=__lowerCamelCase , eval_dataset=__lowerCamelCase , tokenizer=__lowerCamelCase , ) # start training trainer.train()
698
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available, is_vision_available, ) __A : Union[str, Any] = {'configuration_beit': ['BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BeitConfig', 'BeitOnnxConfig']} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Union[str, Any] = ['BeitFeatureExtractor'] __A : Dict = ['BeitImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Optional[Any] = [ 'BEIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'BeitForImageClassification', 'BeitForMaskedImageModeling', 'BeitForSemanticSegmentation', 'BeitModel', 'BeitPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Union[str, Any] = [ 'FlaxBeitForImageClassification', 'FlaxBeitForMaskedImageModeling', 'FlaxBeitModel', 'FlaxBeitPreTrainedModel', ] if TYPE_CHECKING: from .configuration_beit import BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP, BeitConfig, BeitOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_beit import BeitFeatureExtractor from .image_processing_beit import BeitImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_beit import ( BEIT_PRETRAINED_MODEL_ARCHIVE_LIST, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, BeitPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_beit import ( FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling, FlaxBeitModel, FlaxBeitPreTrainedModel, ) else: import sys __A : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
708
import inspect import unittest import warnings from math import ceil, floor from transformers import LevitConfig from transformers.file_utils import cached_property, is_torch_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_vision, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING, MODEL_MAPPING, LevitForImageClassification, LevitForImageClassificationWithTeacher, LevitModel, ) from transformers.models.levit.modeling_levit import LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import LevitImageProcessor class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' def _snake_case ( self : Optional[Any] ): SCREAMING_SNAKE_CASE = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(__lowerCamelCase , "hidden_sizes" ) ) self.parent.assertTrue(hasattr(__lowerCamelCase , "num_attention_heads" ) ) class _SCREAMING_SNAKE_CASE : '''simple docstring''' def __init__( self : str , __lowerCamelCase : Optional[int] , __lowerCamelCase : Union[str, Any]=13 , __lowerCamelCase : Optional[int]=64 , __lowerCamelCase : Dict=3 , __lowerCamelCase : Optional[int]=3 , __lowerCamelCase : int=2 , __lowerCamelCase : List[Any]=1 , __lowerCamelCase : Optional[Any]=16 , __lowerCamelCase : Tuple=[128, 256, 384] , __lowerCamelCase : int=[4, 6, 8] , __lowerCamelCase : Tuple=[2, 3, 4] , __lowerCamelCase : List[str]=[16, 16, 16] , __lowerCamelCase : int=0 , __lowerCamelCase : List[Any]=[2, 2, 2] , __lowerCamelCase : List[str]=[2, 2, 2] , __lowerCamelCase : List[str]=0.02 , __lowerCamelCase : Any=True , __lowerCamelCase : Dict=True , __lowerCamelCase : int=2 , ): SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = kernel_size SCREAMING_SNAKE_CASE = stride SCREAMING_SNAKE_CASE = padding SCREAMING_SNAKE_CASE = hidden_sizes SCREAMING_SNAKE_CASE = num_attention_heads SCREAMING_SNAKE_CASE = depths SCREAMING_SNAKE_CASE = key_dim SCREAMING_SNAKE_CASE = drop_path_rate SCREAMING_SNAKE_CASE = patch_size SCREAMING_SNAKE_CASE = attention_ratio SCREAMING_SNAKE_CASE = mlp_ratio SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = [ ["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], ] SCREAMING_SNAKE_CASE = is_training SCREAMING_SNAKE_CASE = use_labels SCREAMING_SNAKE_CASE = num_labels SCREAMING_SNAKE_CASE = initializer_range def _snake_case ( self : int ): SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE = None if self.use_labels: SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.num_labels ) SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, labels def _snake_case ( self : List[str] ): return LevitConfig( image_size=self.image_size , num_channels=self.num_channels , kernel_size=self.kernel_size , stride=self.stride , padding=self.padding , patch_size=self.patch_size , hidden_sizes=self.hidden_sizes , num_attention_heads=self.num_attention_heads , depths=self.depths , key_dim=self.key_dim , drop_path_rate=self.drop_path_rate , mlp_ratio=self.mlp_ratio , attention_ratio=self.attention_ratio , initializer_range=self.initializer_range , down_ops=self.down_ops , ) def _snake_case ( self : Optional[int] , __lowerCamelCase : List[str] , __lowerCamelCase : str , __lowerCamelCase : Any ): SCREAMING_SNAKE_CASE = LevitModel(config=__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(__lowerCamelCase ) SCREAMING_SNAKE_CASE = (self.image_size, self.image_size) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = image_size[0], image_size[1] for _ in range(4 ): SCREAMING_SNAKE_CASE = floor(((height + 2 * self.padding - self.kernel_size) / self.stride) + 1 ) SCREAMING_SNAKE_CASE = floor(((width + 2 * self.padding - self.kernel_size) / self.stride) + 1 ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, ceil(height / 4 ) * ceil(width / 4 ), self.hidden_sizes[-1]) , ) def _snake_case ( self : Tuple , __lowerCamelCase : int , __lowerCamelCase : Optional[int] , __lowerCamelCase : List[str] ): SCREAMING_SNAKE_CASE = self.num_labels SCREAMING_SNAKE_CASE = LevitForImageClassification(__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(__lowerCamelCase , labels=__lowerCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _snake_case ( self : Optional[Any] ): SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = config_and_inputs SCREAMING_SNAKE_CASE = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class _SCREAMING_SNAKE_CASE ( __snake_case , __snake_case , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = ( (LevitModel, LevitForImageClassification, LevitForImageClassificationWithTeacher) if is_torch_available() else () ) lowerCamelCase__ = ( { "feature-extraction": LevitModel, "image-classification": (LevitForImageClassification, LevitForImageClassificationWithTeacher), } if is_torch_available() else {} ) lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False def _snake_case ( self : Dict ): SCREAMING_SNAKE_CASE = LevitModelTester(self ) SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=__lowerCamelCase , has_text_modality=__lowerCamelCase , hidden_size=37 ) def _snake_case ( self : Any ): self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def _snake_case ( self : Any ): return @unittest.skip(reason="Levit does not use inputs_embeds" ) def _snake_case ( self : Union[str, Any] ): pass @unittest.skip(reason="Levit does not support input and output embeddings" ) def _snake_case ( self : Tuple ): pass @unittest.skip(reason="Levit does not output attentions" ) def _snake_case ( self : Tuple ): pass def _snake_case ( self : Dict ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(__lowerCamelCase ) SCREAMING_SNAKE_CASE = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE = ["pixel_values"] self.assertListEqual(arg_names[:1] , __lowerCamelCase ) def _snake_case ( self : List[Any] ): def check_hidden_states_output(__lowerCamelCase : List[str] , __lowerCamelCase : int , __lowerCamelCase : Optional[Any] ): SCREAMING_SNAKE_CASE = model_class(__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**self._prepare_for_class(__lowerCamelCase , __lowerCamelCase ) ) SCREAMING_SNAKE_CASE = outputs.hidden_states SCREAMING_SNAKE_CASE = len(self.model_tester.depths ) + 1 self.assertEqual(len(__lowerCamelCase ) , __lowerCamelCase ) SCREAMING_SNAKE_CASE = (self.model_tester.image_size, self.model_tester.image_size) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = image_size[0], image_size[1] for _ in range(4 ): SCREAMING_SNAKE_CASE = floor( ( (height + 2 * self.model_tester.padding - self.model_tester.kernel_size) / self.model_tester.stride ) + 1 ) SCREAMING_SNAKE_CASE = floor( ( (width + 2 * self.model_tester.padding - self.model_tester.kernel_size) / self.model_tester.stride ) + 1 ) # verify the first hidden states (first block) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [ height * width, self.model_tester.hidden_sizes[0], ] , ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = True check_hidden_states_output(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE = True check_hidden_states_output(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." ) def _snake_case ( self : int ): pass def _snake_case ( self : List[Any] , __lowerCamelCase : Tuple , __lowerCamelCase : Any , __lowerCamelCase : int=False ): SCREAMING_SNAKE_CASE = super()._prepare_for_class(__lowerCamelCase , __lowerCamelCase , return_labels=__lowerCamelCase ) if return_labels: if model_class.__name__ == "LevitForImageClassificationWithTeacher": del inputs_dict["labels"] return inputs_dict def _snake_case ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__lowerCamelCase ) def _snake_case ( self : str ): SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase ) def _snake_case ( self : List[Any] ): if not self.model_tester.is_training: return SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE = True for model_class in self.all_model_classes: # LevitForImageClassificationWithTeacher supports inference-only if ( model_class in get_values(__lowerCamelCase ) or model_class.__name__ == "LevitForImageClassificationWithTeacher" ): continue SCREAMING_SNAKE_CASE = model_class(__lowerCamelCase ) model.to(__lowerCamelCase ) model.train() SCREAMING_SNAKE_CASE = self._prepare_for_class(__lowerCamelCase , __lowerCamelCase , return_labels=__lowerCamelCase ) SCREAMING_SNAKE_CASE = model(**__lowerCamelCase ).loss loss.backward() def _snake_case ( self : Tuple ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = True for model_class in self.all_model_classes: if model_class in get_values(__lowerCamelCase ) or not model_class.supports_gradient_checkpointing: continue # LevitForImageClassificationWithTeacher supports inference-only if model_class.__name__ == "LevitForImageClassificationWithTeacher": continue SCREAMING_SNAKE_CASE = model_class(__lowerCamelCase ) model.gradient_checkpointing_enable() model.to(__lowerCamelCase ) model.train() SCREAMING_SNAKE_CASE = self._prepare_for_class(__lowerCamelCase , __lowerCamelCase , return_labels=__lowerCamelCase ) SCREAMING_SNAKE_CASE = model(**__lowerCamelCase ).loss loss.backward() def _snake_case ( self : List[str] ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE = [ {"title": "multi_label_classification", "num_labels": 2, "dtype": torch.float}, {"title": "single_label_classification", "num_labels": 1, "dtype": torch.long}, {"title": "regression", "num_labels": 1, "dtype": torch.float}, ] for model_class in self.all_model_classes: if ( model_class not in [ *get_values(__lowerCamelCase ), ] or model_class.__name__ == "LevitForImageClassificationWithTeacher" ): continue for problem_type in problem_types: with self.subTest(msg=f"Testing {model_class} with {problem_type['title']}" ): SCREAMING_SNAKE_CASE = problem_type["title"] SCREAMING_SNAKE_CASE = problem_type["num_labels"] SCREAMING_SNAKE_CASE = model_class(__lowerCamelCase ) model.to(__lowerCamelCase ) model.train() SCREAMING_SNAKE_CASE = self._prepare_for_class(__lowerCamelCase , __lowerCamelCase , return_labels=__lowerCamelCase ) if problem_type["num_labels"] > 1: SCREAMING_SNAKE_CASE = inputs["labels"].unsqueeze(1 ).repeat(1 , problem_type["num_labels"] ) SCREAMING_SNAKE_CASE = inputs["labels"].to(problem_type["dtype"] ) # This tests that we do not trigger the warning form PyTorch "Using a target size that is different # to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure # they have the same size." which is a symptom something in wrong for the regression problem. # See https://github.com/huggingface/transformers/issues/11780 with warnings.catch_warnings(record=__lowerCamelCase ) as warning_list: SCREAMING_SNAKE_CASE = model(**__lowerCamelCase ).loss for w in warning_list: if "Using a target size that is different to the input size" in str(w.message ): raise ValueError( f"Something is going wrong in the regression problem: intercepted {w.message}" ) loss.backward() @slow def _snake_case ( self : List[Any] ): for model_name in LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE = LevitModel.from_pretrained(__lowerCamelCase ) self.assertIsNotNone(__lowerCamelCase ) def __a ( ): SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' @cached_property def _snake_case ( self : Dict ): return LevitImageProcessor.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) @slow def _snake_case ( self : Optional[int] ): SCREAMING_SNAKE_CASE = LevitForImageClassificationWithTeacher.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to( __lowerCamelCase ) SCREAMING_SNAKE_CASE = self.default_image_processor SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=__lowerCamelCase , return_tensors="pt" ).to(__lowerCamelCase ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**__lowerCamelCase ) # verify the logits SCREAMING_SNAKE_CASE = torch.Size((1, 1000) ) self.assertEqual(outputs.logits.shape , __lowerCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([1.0_448, -0.3_745, -1.8_317] ).to(__lowerCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , __lowerCamelCase , atol=1e-4 ) )
698
0
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import warnings from typing import List from unittest.mock import Mock import torch from torch.utils.data import DataLoader, IterableDataset, TensorDataset from accelerate.accelerator import Accelerator from accelerate.utils.dataclasses import DistributedType class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' def __init__( self : Tuple , __lowerCamelCase : Optional[Any] ): SCREAMING_SNAKE_CASE = data def __iter__( self : str ): for element in self.data: yield element def __a ( A__ : List[str]=True ): SCREAMING_SNAKE_CASE = Accelerator(even_batches=A__ ) assert accelerator.num_processes == 2, "this script expects that two GPUs are available" return accelerator def __a ( A__ : Accelerator , A__ : int , A__ : int , A__ : bool = False ): if iterable: SCREAMING_SNAKE_CASE = DummyIterableDataset(torch.as_tensor(range(A__ ) ) ) else: SCREAMING_SNAKE_CASE = TensorDataset(torch.as_tensor(range(A__ ) ) ) SCREAMING_SNAKE_CASE = DataLoader(A__ , batch_size=A__ ) SCREAMING_SNAKE_CASE = accelerator.prepare(A__ ) return dl def __a ( A__ : Accelerator , A__ : int , A__ : int , A__ : List[int] , A__ : List[int] , ): SCREAMING_SNAKE_CASE = create_dataloader(accelerator=A__ , dataset_size=A__ , batch_size=A__ ) SCREAMING_SNAKE_CASE = [len(batch[0] ) for batch in dl] if accelerator.process_index == 0: assert batch_sizes == process_0_expected_batch_sizes elif accelerator.process_index == 1: assert batch_sizes == process_1_expected_batch_sizes def __a ( ): SCREAMING_SNAKE_CASE = create_accelerator() # without padding, we would expect a different number of batches verify_dataloader_batch_sizes( A__ , dataset_size=3 , batch_size=1 , process_0_expected_batch_sizes=[1, 1] , process_1_expected_batch_sizes=[1, 1] , ) # without padding, we would expect the same number of batches, but different sizes verify_dataloader_batch_sizes( A__ , dataset_size=7 , batch_size=2 , process_0_expected_batch_sizes=[2, 2] , process_1_expected_batch_sizes=[2, 2] , ) def __a ( ): SCREAMING_SNAKE_CASE = create_accelerator(even_batches=A__ ) verify_dataloader_batch_sizes( A__ , dataset_size=3 , batch_size=1 , process_0_expected_batch_sizes=[1, 1] , process_1_expected_batch_sizes=[1] , ) verify_dataloader_batch_sizes( A__ , dataset_size=7 , batch_size=2 , process_0_expected_batch_sizes=[2, 2] , process_1_expected_batch_sizes=[2, 1] , ) def __a ( ): SCREAMING_SNAKE_CASE = create_accelerator(even_batches=A__ ) SCREAMING_SNAKE_CASE = torch.nn.Linear(1 , 1 ) SCREAMING_SNAKE_CASE = accelerator.prepare(A__ ) SCREAMING_SNAKE_CASE = create_dataloader(A__ , dataset_size=3 , batch_size=1 ) SCREAMING_SNAKE_CASE = [] with accelerator.join_uneven_inputs([ddp_model] ): for batch_idx, batch in enumerate(A__ ): SCREAMING_SNAKE_CASE = ddp_model(batch[0].float() ) SCREAMING_SNAKE_CASE = output.sum() loss.backward() batch_idxs.append(A__ ) accelerator.wait_for_everyone() if accelerator.process_index == 0: assert batch_idxs == [0, 1] elif accelerator.process_index == 1: assert batch_idxs == [0] def __a ( A__ : List[Any] ): with warnings.catch_warnings(record=A__ ) as w: with accelerator.join_uneven_inputs([Mock()] ): pass assert issubclass(w[-1].category , A__ ) assert "only supported for multi-GPU" in str(w[-1].message ) def __a ( ): SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = create_accelerator(even_batches=A__ ) SCREAMING_SNAKE_CASE = torch.nn.Linear(1 , 1 ) SCREAMING_SNAKE_CASE = accelerator.prepare(A__ ) SCREAMING_SNAKE_CASE = create_dataloader(A__ , dataset_size=3 , batch_size=1 ) SCREAMING_SNAKE_CASE = create_dataloader(A__ , dataset_size=3 , batch_size=1 ) with accelerator.join_uneven_inputs([ddp_model] , even_batches=A__ ): SCREAMING_SNAKE_CASE = train_dl.batch_sampler.even_batches SCREAMING_SNAKE_CASE = valid_dl.batch_sampler.even_batches assert train_dl_overridden_value == overridden_even_batches assert valid_dl_overridden_value == overridden_even_batches assert train_dl.batch_sampler.even_batches == default_even_batches assert valid_dl.batch_sampler.even_batches == default_even_batches def __a ( ): SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = create_accelerator(even_batches=A__ ) SCREAMING_SNAKE_CASE = torch.nn.Linear(1 , 1 ) SCREAMING_SNAKE_CASE = accelerator.prepare(A__ ) create_dataloader(A__ , dataset_size=3 , batch_size=1 , iterable=A__ ) SCREAMING_SNAKE_CASE = create_dataloader(A__ , dataset_size=3 , batch_size=1 ) with warnings.catch_warnings(): warnings.filterwarnings("ignore" ) try: with accelerator.join_uneven_inputs([ddp_model] , even_batches=A__ ): SCREAMING_SNAKE_CASE = batch_dl.batch_sampler.even_batches except AttributeError: # ensure attribute error is not raised when processing iterable dl raise AssertionError assert batch_dl_overridden_value == overridden_even_batches assert batch_dl.batch_sampler.even_batches == default_even_batches def __a ( ): SCREAMING_SNAKE_CASE = create_accelerator() SCREAMING_SNAKE_CASE = torch.nn.Linear(1 , 1 ) SCREAMING_SNAKE_CASE = accelerator.prepare(A__ ) create_dataloader(A__ , dataset_size=3 , batch_size=1 , iterable=A__ ) with warnings.catch_warnings(record=A__ ) as w: with accelerator.join_uneven_inputs([ddp_model] , even_batches=A__ ): pass assert issubclass(w[-1].category , A__ ) assert "only supported for map-style datasets" in str(w[-1].message ) def __a ( ): SCREAMING_SNAKE_CASE = create_accelerator() accelerator.print("Test that even_batches variable ensures uniform batches across processes" ) test_default_ensures_even_batch_sizes() accelerator.print("Run tests with even_batches disabled" ) test_can_disable_even_batches() accelerator.print("Test joining uneven inputs" ) test_can_join_uneven_inputs() accelerator.print("Test overriding even_batches when joining uneven inputs" ) test_join_can_override_even_batches() accelerator.print("Test overriding even_batches for mixed dataloader types" ) test_join_can_override_for_mixed_type_dataloaders() accelerator.print("Test overriding even_batches raises a warning for iterable dataloaders" ) test_join_raises_warning_for_iterable_when_overriding_even_batches() accelerator.print("Test join with non DDP distributed raises warning" ) SCREAMING_SNAKE_CASE = accelerator.state.distributed_type SCREAMING_SNAKE_CASE = DistributedType.FSDP test_join_raises_warning_for_non_ddp_distributed(A__ ) SCREAMING_SNAKE_CASE = original_state if __name__ == "__main__": main()
709
import unittest from pathlib import Path from tempfile import NamedTemporaryFile, TemporaryDirectory from transformers import BertConfig, BertTokenizerFast, FeatureExtractionPipeline from transformers.convert_graph_to_onnx import ( convert, ensure_valid_input, generate_identified_filename, infer_shapes, quantize, ) from transformers.testing_utils import require_tf, require_tokenizers, require_torch, slow class _SCREAMING_SNAKE_CASE : '''simple docstring''' def _snake_case ( self : Tuple , __lowerCamelCase : Optional[int] , __lowerCamelCase : Optional[Any] , __lowerCamelCase : Any ): return None class _SCREAMING_SNAKE_CASE : '''simple docstring''' def _snake_case ( self : Union[str, Any] , __lowerCamelCase : Dict , __lowerCamelCase : Dict , __lowerCamelCase : Any , __lowerCamelCase : int ): return None class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = [ # (model_name, model_kwargs) ("bert-base-cased", {}), ("gpt2", {"use_cache": False}), # We don't support exporting GPT2 past keys anymore ] @require_tf @slow def _snake_case ( self : List[Any] ): for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST: self._test_export(__lowerCamelCase , "tf" , 12 , **__lowerCamelCase ) @require_torch @slow def _snake_case ( self : List[Any] ): for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST: self._test_export(__lowerCamelCase , "pt" , 12 , **__lowerCamelCase ) @require_torch @slow def _snake_case ( self : Optional[int] ): from transformers import BertModel SCREAMING_SNAKE_CASE = ["[UNK]", "[SEP]", "[CLS]", "[PAD]", "[MASK]", "some", "other", "words"] with NamedTemporaryFile(mode="w+t" ) as vocab_file: vocab_file.write("\n".join(__lowerCamelCase ) ) vocab_file.flush() SCREAMING_SNAKE_CASE = BertTokenizerFast(vocab_file.name ) with TemporaryDirectory() as bert_save_dir: SCREAMING_SNAKE_CASE = BertModel(BertConfig(vocab_size=len(__lowerCamelCase ) ) ) model.save_pretrained(__lowerCamelCase ) self._test_export(__lowerCamelCase , "pt" , 12 , __lowerCamelCase ) @require_tf @slow def _snake_case ( self : Any ): for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST: SCREAMING_SNAKE_CASE = self._test_export(__lowerCamelCase , "tf" , 12 , **__lowerCamelCase ) SCREAMING_SNAKE_CASE = quantize(Path(__lowerCamelCase ) ) # Ensure the actual quantized model is not bigger than the original one if quantized_path.stat().st_size >= Path(__lowerCamelCase ).stat().st_size: self.fail("Quantized model is bigger than initial ONNX model" ) @require_torch @slow def _snake_case ( self : Any ): for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST: SCREAMING_SNAKE_CASE = self._test_export(__lowerCamelCase , "pt" , 12 , **__lowerCamelCase ) SCREAMING_SNAKE_CASE = quantize(__lowerCamelCase ) # Ensure the actual quantized model is not bigger than the original one if quantized_path.stat().st_size >= Path(__lowerCamelCase ).stat().st_size: self.fail("Quantized model is bigger than initial ONNX model" ) def _snake_case ( self : Dict , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : Dict , __lowerCamelCase : int , __lowerCamelCase : Tuple=None , **__lowerCamelCase : List[str] ): try: # Compute path with TemporaryDirectory() as tempdir: SCREAMING_SNAKE_CASE = Path(__lowerCamelCase ).joinpath("model.onnx" ) # Remove folder if exists if path.parent.exists(): path.parent.rmdir() # Export convert(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , **__lowerCamelCase ) return path except Exception as e: self.fail(__lowerCamelCase ) @require_torch @require_tokenizers @slow def _snake_case ( self : Dict ): from transformers import BertModel SCREAMING_SNAKE_CASE = BertModel(BertConfig.from_pretrained("lysandre/tiny-bert-random" ) ) SCREAMING_SNAKE_CASE = BertTokenizerFast.from_pretrained("lysandre/tiny-bert-random" ) self._test_infer_dynamic_axis(__lowerCamelCase , __lowerCamelCase , "pt" ) @require_tf @require_tokenizers @slow def _snake_case ( self : int ): from transformers import TFBertModel SCREAMING_SNAKE_CASE = TFBertModel(BertConfig.from_pretrained("lysandre/tiny-bert-random" ) ) SCREAMING_SNAKE_CASE = BertTokenizerFast.from_pretrained("lysandre/tiny-bert-random" ) self._test_infer_dynamic_axis(__lowerCamelCase , __lowerCamelCase , "tf" ) def _snake_case ( self : int , __lowerCamelCase : Dict , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : str ): SCREAMING_SNAKE_CASE = FeatureExtractionPipeline(__lowerCamelCase , __lowerCamelCase ) SCREAMING_SNAKE_CASE = ["input_ids", "token_type_ids", "attention_mask", "output_0", "output_1"] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = infer_shapes(__lowerCamelCase , __lowerCamelCase ) # Assert all variables are present self.assertEqual(len(__lowerCamelCase ) , len(__lowerCamelCase ) ) self.assertTrue(all(var_name in shapes for var_name in variable_names ) ) self.assertSequenceEqual(variable_names[:3] , __lowerCamelCase ) self.assertSequenceEqual(variable_names[3:] , __lowerCamelCase ) # Assert inputs are {0: batch, 1: sequence} for var_name in ["input_ids", "token_type_ids", "attention_mask"]: self.assertDictEqual(shapes[var_name] , {0: "batch", 1: "sequence"} ) # Assert outputs are {0: batch, 1: sequence} and {0: batch} self.assertDictEqual(shapes["output_0"] , {0: "batch", 1: "sequence"} ) self.assertDictEqual(shapes["output_1"] , {0: "batch"} ) def _snake_case ( self : Optional[Any] ): SCREAMING_SNAKE_CASE = ["input_ids", "attention_mask", "token_type_ids"] SCREAMING_SNAKE_CASE = {"input_ids": [1, 2, 3, 4], "attention_mask": [0, 0, 0, 0], "token_type_ids": [1, 1, 1, 1]} SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = ensure_valid_input(FuncContiguousArgs() , __lowerCamelCase , __lowerCamelCase ) # Should have exactly the same number of args (all are valid) self.assertEqual(len(__lowerCamelCase ) , 3 ) # Should have exactly the same input names self.assertEqual(set(__lowerCamelCase ) , set(__lowerCamelCase ) ) # Parameter should be reordered according to their respective place in the function: # (input_ids, token_type_ids, attention_mask) self.assertEqual(__lowerCamelCase , (tokens["input_ids"], tokens["token_type_ids"], tokens["attention_mask"]) ) # Generated args are interleaved with another args (for instance parameter "past" in GPT2) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = ensure_valid_input(FuncNonContiguousArgs() , __lowerCamelCase , __lowerCamelCase ) # Should have exactly the one arg (all before the one not provided "some_other_args") self.assertEqual(len(__lowerCamelCase ) , 1 ) self.assertEqual(len(__lowerCamelCase ) , 1 ) # Should have only "input_ids" self.assertEqual(inputs_args[0] , tokens["input_ids"] ) self.assertEqual(ordered_input_names[0] , "input_ids" ) def _snake_case ( self : Optional[int] ): SCREAMING_SNAKE_CASE = generate_identified_filename(Path("/home/something/my_fake_model.onnx" ) , "-test" ) self.assertEqual("/home/something/my_fake_model-test.onnx" , generated.as_posix() )
698
0
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import KandinskyPipeline, KandinskyPriorPipeline else: from .pipeline_kandinsky import KandinskyPipeline from .pipeline_kandinsky_imgaimg import KandinskyImgaImgPipeline from .pipeline_kandinsky_inpaint import KandinskyInpaintPipeline from .pipeline_kandinsky_prior import KandinskyPriorPipeline, KandinskyPriorPipelineOutput from .text_encoder import MultilingualCLIP
710
from manim import * class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' def _snake_case ( self : List[Any] ): SCREAMING_SNAKE_CASE = Rectangle(height=0.5 , width=0.5 ) SCREAMING_SNAKE_CASE = Rectangle(height=0.46 , width=0.46 ).set_stroke(width=0 ) SCREAMING_SNAKE_CASE = [mem.copy() for i in range(6 )] SCREAMING_SNAKE_CASE = [mem.copy() for i in range(6 )] SCREAMING_SNAKE_CASE = VGroup(*__lowerCamelCase ).arrange(__lowerCamelCase , buff=0 ) SCREAMING_SNAKE_CASE = VGroup(*__lowerCamelCase ).arrange(__lowerCamelCase , buff=0 ) SCREAMING_SNAKE_CASE = VGroup(__lowerCamelCase , __lowerCamelCase ).arrange(__lowerCamelCase , buff=0 ) SCREAMING_SNAKE_CASE = Text("CPU" , font_size=24 ) SCREAMING_SNAKE_CASE = Group(__lowerCamelCase , __lowerCamelCase ).arrange(__lowerCamelCase , buff=0.5 , aligned_edge=__lowerCamelCase ) cpu.move_to([-2.5, -0.5, 0] ) self.add(__lowerCamelCase ) SCREAMING_SNAKE_CASE = [mem.copy() for i in range(4 )] SCREAMING_SNAKE_CASE = VGroup(*__lowerCamelCase ).arrange(__lowerCamelCase , buff=0 ) SCREAMING_SNAKE_CASE = Text("GPU" , font_size=24 ) SCREAMING_SNAKE_CASE = Group(__lowerCamelCase , __lowerCamelCase ).arrange(__lowerCamelCase , buff=0.5 , aligned_edge=__lowerCamelCase ) gpu.move_to([-1, -1, 0] ) self.add(__lowerCamelCase ) SCREAMING_SNAKE_CASE = [mem.copy() for i in range(6 )] SCREAMING_SNAKE_CASE = VGroup(*__lowerCamelCase ).arrange(__lowerCamelCase , buff=0 ) SCREAMING_SNAKE_CASE = Text("Model" , font_size=24 ) SCREAMING_SNAKE_CASE = Group(__lowerCamelCase , __lowerCamelCase ).arrange(__lowerCamelCase , buff=0.5 , aligned_edge=__lowerCamelCase ) model.move_to([3, -1.0, 0] ) self.add(__lowerCamelCase ) SCREAMING_SNAKE_CASE = [] for i, rect in enumerate(__lowerCamelCase ): rect.set_stroke(__lowerCamelCase ) # target = fill.copy().set_fill(YELLOW, opacity=0.7) # target.move_to(rect) # self.add(target) SCREAMING_SNAKE_CASE = Rectangle(height=0.46 / 4 , width=0.46 / 3 ).set_stroke(width=0.0 ).set_fill(__lowerCamelCase , opacity=0.7 ) if i == 0: cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT ) , buff=0.02 , direction=__lowerCamelCase ) cpu_target.set_x(cpu_target.get_x() + 0.1 ) elif i == 3: cpu_target.next_to(cpu_targs[0] , direction=__lowerCamelCase , buff=0.0 ) else: cpu_target.next_to(cpu_targs[i - 1] , direction=__lowerCamelCase , buff=0.0 ) self.add(__lowerCamelCase ) cpu_targs.append(__lowerCamelCase ) SCREAMING_SNAKE_CASE = [mem.copy() for i in range(6 )] SCREAMING_SNAKE_CASE = VGroup(*__lowerCamelCase ).arrange(__lowerCamelCase , buff=0 ) SCREAMING_SNAKE_CASE = Text("Loaded Checkpoint" , font_size=24 ) SCREAMING_SNAKE_CASE = Group(__lowerCamelCase , __lowerCamelCase ).arrange(__lowerCamelCase , aligned_edge=__lowerCamelCase , buff=0.4 ) checkpoint.move_to([3, 0.5, 0] ) SCREAMING_SNAKE_CASE = Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) SCREAMING_SNAKE_CASE = MarkupText( f"<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model" , font_size=18 , ) key_text.move_to([-5, 2.4, 0] ) self.add(__lowerCamelCase , __lowerCamelCase ) SCREAMING_SNAKE_CASE = MarkupText( f"<span fgcolor='{BLUE}'>●</span> Checkpoint" , font_size=18 , ) blue_text.next_to(__lowerCamelCase , DOWN * 2.4 , aligned_edge=key_text.get_left() ) SCREAMING_SNAKE_CASE = MarkupText( f"Next, a <i><span fgcolor=\"{BLUE}\">second</span></i> model is loaded into memory,\nwith the weights of a <span fgcolor=\"{BLUE}\">single shard</span>." , font_size=24 , ) step_a.move_to([2, 2, 0] ) self.play(Write(__lowerCamelCase ) , Write(__lowerCamelCase ) ) self.play(Write(__lowerCamelCase , run_time=1 ) , Create(__lowerCamelCase , run_time=1 ) ) SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for i, rect in enumerate(__lowerCamelCase ): SCREAMING_SNAKE_CASE = fill.copy().set_fill(__lowerCamelCase , opacity=0.7 ) target.move_to(__lowerCamelCase ) first_animations.append(GrowFromCenter(__lowerCamelCase , run_time=1 ) ) SCREAMING_SNAKE_CASE = target.copy() cpu_target.generate_target() if i < 5: cpu_target.target.move_to(cpu_left_col_base[i + 1] ) else: cpu_target.target.move_to(cpu_right_col_base[i - 5] ) second_animations.append(MoveToTarget(__lowerCamelCase , run_time=1.5 ) ) self.play(*__lowerCamelCase ) self.play(*__lowerCamelCase ) self.wait()
698
0
from __future__ import annotations from cmath import sqrt def __a ( A__ : int , A__ : int , A__ : int ): if a == 0: raise ValueError("Coefficient 'a' must not be zero." ) SCREAMING_SNAKE_CASE = b * b - 4 * a * c SCREAMING_SNAKE_CASE = (-b + sqrt(A__ )) / (2 * a) SCREAMING_SNAKE_CASE = (-b - sqrt(A__ )) / (2 * a) return ( root_a.real if not root_a.imag else root_a, root_a.real if not root_a.imag else root_a, ) def __a ( ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = quadratic_roots(a=5 , b=6 , c=1 ) print(F"The solutions are: {solutiona} and {solutiona}" ) if __name__ == "__main__": main()
711
import unittest from transformers import is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class _SCREAMING_SNAKE_CASE : '''simple docstring''' @staticmethod def _snake_case ( *__lowerCamelCase : Any , **__lowerCamelCase : Any ): pass @is_pipeline_test @require_vision class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' @require_torch def _snake_case ( self : Dict ): SCREAMING_SNAKE_CASE = pipeline( model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification" , ) SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) SCREAMING_SNAKE_CASE = image_classifier(__lowerCamelCase , candidate_labels=["a", "b", "c"] ) # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across # python and torch versions. self.assertIn( nested_simplify(__lowerCamelCase ) , [ [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}], [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "c"}, {"score": 0.333, "label": "b"}], ] , ) SCREAMING_SNAKE_CASE = image_classifier([image] * 5 , candidate_labels=["A", "B", "C"] , batch_size=2 ) self.assertEqual( nested_simplify(__lowerCamelCase ) , [ [ {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, ], [ {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, ], [ {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, ], [ {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, ], [ {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, ], ] , ) @require_tf def _snake_case ( self : int ): SCREAMING_SNAKE_CASE = pipeline( model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification" , framework="tf" ) SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) SCREAMING_SNAKE_CASE = image_classifier(__lowerCamelCase , candidate_labels=["a", "b", "c"] ) self.assertEqual( nested_simplify(__lowerCamelCase ) , [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}] , ) SCREAMING_SNAKE_CASE = image_classifier([image] * 5 , candidate_labels=["A", "B", "C"] , batch_size=2 ) self.assertEqual( nested_simplify(__lowerCamelCase ) , [ [ {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, ], [ {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, ], [ {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, ], [ {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, ], [ {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, ], ] , ) @slow @require_torch def _snake_case ( self : Tuple ): SCREAMING_SNAKE_CASE = pipeline( task="zero-shot-image-classification" , model="openai/clip-vit-base-patch32" , ) # This is an image of 2 cats with remotes and no planes SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) SCREAMING_SNAKE_CASE = image_classifier(__lowerCamelCase , candidate_labels=["cat", "plane", "remote"] ) self.assertEqual( nested_simplify(__lowerCamelCase ) , [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ] , ) SCREAMING_SNAKE_CASE = image_classifier([image] * 5 , candidate_labels=["cat", "plane", "remote"] , batch_size=2 ) self.assertEqual( nested_simplify(__lowerCamelCase ) , [ [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ] * 5 , ) @slow @require_tf def _snake_case ( self : Any ): SCREAMING_SNAKE_CASE = pipeline( task="zero-shot-image-classification" , model="openai/clip-vit-base-patch32" , framework="tf" ) # This is an image of 2 cats with remotes and no planes SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) SCREAMING_SNAKE_CASE = image_classifier(__lowerCamelCase , candidate_labels=["cat", "plane", "remote"] ) self.assertEqual( nested_simplify(__lowerCamelCase ) , [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ] , ) SCREAMING_SNAKE_CASE = image_classifier([image] * 5 , candidate_labels=["cat", "plane", "remote"] , batch_size=2 ) self.assertEqual( nested_simplify(__lowerCamelCase ) , [ [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ] * 5 , )
698
0
from ...configuration_utils import PretrainedConfig from ...utils import logging __A : Optional[Any] = logging.get_logger(__name__) __A : Optional[int] = { 'facebook/nllb-moe-54B': 'https://huggingface.co/facebook/nllb-moe-54b/resolve/main/config.json', } class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' lowerCamelCase__ = "nllb-moe" lowerCamelCase__ = ["past_key_values"] lowerCamelCase__ = {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"} def __init__( self : Dict , __lowerCamelCase : Dict=128112 , __lowerCamelCase : Optional[Any]=1024 , __lowerCamelCase : Tuple=12 , __lowerCamelCase : Optional[Any]=4096 , __lowerCamelCase : List[Any]=16 , __lowerCamelCase : Optional[int]=12 , __lowerCamelCase : Union[str, Any]=4096 , __lowerCamelCase : Optional[Any]=16 , __lowerCamelCase : str=0.05 , __lowerCamelCase : Union[str, Any]=0.05 , __lowerCamelCase : Tuple=True , __lowerCamelCase : List[str]=True , __lowerCamelCase : Optional[int]="relu" , __lowerCamelCase : int=1024 , __lowerCamelCase : Tuple=0.1 , __lowerCamelCase : int=0.1 , __lowerCamelCase : Dict=0.0 , __lowerCamelCase : str=0.02 , __lowerCamelCase : Optional[Any]=2 , __lowerCamelCase : List[Any]=True , __lowerCamelCase : Optional[Any]=False , __lowerCamelCase : Tuple="float32" , __lowerCamelCase : List[str]=False , __lowerCamelCase : Union[str, Any]=128 , __lowerCamelCase : Optional[int]=64 , __lowerCamelCase : Optional[Any]=4 , __lowerCamelCase : Union[str, Any]=4 , __lowerCamelCase : Dict=0.001 , __lowerCamelCase : Dict=0.001 , __lowerCamelCase : List[Any]="all" , __lowerCamelCase : Optional[int]=False , __lowerCamelCase : List[Any]=False , __lowerCamelCase : Union[str, Any]=1.0 , __lowerCamelCase : Tuple=0.2 , __lowerCamelCase : str=1 , __lowerCamelCase : str=0 , __lowerCamelCase : List[Any]=2 , __lowerCamelCase : Optional[Any]=False , **__lowerCamelCase : Union[str, Any] , ): SCREAMING_SNAKE_CASE = vocab_size SCREAMING_SNAKE_CASE = max_position_embeddings SCREAMING_SNAKE_CASE = d_model SCREAMING_SNAKE_CASE = encoder_ffn_dim SCREAMING_SNAKE_CASE = encoder_layers SCREAMING_SNAKE_CASE = encoder_attention_heads SCREAMING_SNAKE_CASE = decoder_ffn_dim SCREAMING_SNAKE_CASE = decoder_layers SCREAMING_SNAKE_CASE = decoder_attention_heads SCREAMING_SNAKE_CASE = dropout SCREAMING_SNAKE_CASE = attention_dropout SCREAMING_SNAKE_CASE = activation_dropout SCREAMING_SNAKE_CASE = activation_function SCREAMING_SNAKE_CASE = init_std SCREAMING_SNAKE_CASE = encoder_layerdrop SCREAMING_SNAKE_CASE = decoder_layerdrop SCREAMING_SNAKE_CASE = use_cache SCREAMING_SNAKE_CASE = encoder_layers SCREAMING_SNAKE_CASE = scale_embedding # scale factor will be sqrt(d_model) if True SCREAMING_SNAKE_CASE = router_z_loss_coef SCREAMING_SNAKE_CASE = router_aux_loss_coef SCREAMING_SNAKE_CASE = decoder_sparse_step SCREAMING_SNAKE_CASE = encoder_sparse_step SCREAMING_SNAKE_CASE = num_experts SCREAMING_SNAKE_CASE = expert_capacity SCREAMING_SNAKE_CASE = router_bias if router_dtype not in ["float32", "float16", "bfloat16"]: raise ValueError(f"`router_dtype` must be one of 'float32', 'float16' or 'bfloat16', got {router_dtype}" ) SCREAMING_SNAKE_CASE = router_dtype SCREAMING_SNAKE_CASE = router_ignore_padding_tokens SCREAMING_SNAKE_CASE = batch_prioritized_routing SCREAMING_SNAKE_CASE = second_expert_policy SCREAMING_SNAKE_CASE = normalize_router_prob_before_dropping SCREAMING_SNAKE_CASE = moe_eval_capacity_token_fraction SCREAMING_SNAKE_CASE = moe_token_dropout SCREAMING_SNAKE_CASE = output_router_logits super().__init__( pad_token_id=__lowerCamelCase , bos_token_id=__lowerCamelCase , eos_token_id=__lowerCamelCase , is_encoder_decoder=__lowerCamelCase , decoder_start_token_id=__lowerCamelCase , **__lowerCamelCase , )
712
__A : dict[str, float] = { "joule": 1.0, "kilojoule": 1_0_0_0, "megajoule": 1_0_0_0_0_0_0, "gigajoule": 1_0_0_0_0_0_0_0_0_0, "wattsecond": 1.0, "watthour": 3_6_0_0, "kilowatthour": 3_6_0_0_0_0_0, "newtonmeter": 1.0, "calorie_nutr": 4_1_8_6.8, "kilocalorie_nutr": 4_1_8_6_8_0_0.0_0, "electronvolt": 1.6_0217_6634e-19, "britishthermalunit_it": 1_0_5_5.0_5_5_8_5, "footpound": 1.355_818, } def __a ( A__ : str , A__ : str , A__ : float ): if to_type not in ENERGY_CONVERSION or from_type not in ENERGY_CONVERSION: SCREAMING_SNAKE_CASE = ( F"Incorrect 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\n" F"Valid values are: {', '.join(A__ )}" ) raise ValueError(A__ ) return value * ENERGY_CONVERSION[from_type] / ENERGY_CONVERSION[to_type] if __name__ == "__main__": import doctest doctest.testmod()
698
0
'''simple docstring''' from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_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 __A : List[Any] = logging.get_logger(__name__) def __a ( A__ : Tuple ): 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 _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' lowerCamelCase__ = ["pixel_values"] def __init__( self : str , __lowerCamelCase : bool = True , __lowerCamelCase : Dict[str, int] = None , __lowerCamelCase : PILImageResampling = PILImageResampling.BILINEAR , __lowerCamelCase : bool = True , __lowerCamelCase : Dict[str, int] = None , __lowerCamelCase : bool = True , __lowerCamelCase : Union[int, float] = 1 / 255 , __lowerCamelCase : bool = True , __lowerCamelCase : Optional[Union[float, List[float]]] = None , __lowerCamelCase : Optional[Union[float, List[float]]] = None , **__lowerCamelCase : List[str] , ): super().__init__(**__lowerCamelCase ) SCREAMING_SNAKE_CASE = size if size is not None else {"shortest_edge": 224} SCREAMING_SNAKE_CASE = get_size_dict(__lowerCamelCase , default_to_square=__lowerCamelCase ) SCREAMING_SNAKE_CASE = crop_size if crop_size is not None else {"height": 224, "width": 224} SCREAMING_SNAKE_CASE = get_size_dict(__lowerCamelCase , param_name="crop_size" ) SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = do_center_crop SCREAMING_SNAKE_CASE = crop_size SCREAMING_SNAKE_CASE = resample SCREAMING_SNAKE_CASE = do_rescale SCREAMING_SNAKE_CASE = rescale_factor SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN SCREAMING_SNAKE_CASE = image_std if image_std is not None else IMAGENET_STANDARD_STD def _snake_case ( self : List[Any] , __lowerCamelCase : np.ndarray , __lowerCamelCase : Dict[str, int] , __lowerCamelCase : PILImageResampling = PILImageResampling.BILINEAR , __lowerCamelCase : Optional[Union[str, ChannelDimension]] = None , **__lowerCamelCase : Optional[Any] , ): SCREAMING_SNAKE_CASE = get_size_dict(__lowerCamelCase , default_to_square=__lowerCamelCase ) if "shortest_edge" in size: SCREAMING_SNAKE_CASE = get_resize_output_image_size(__lowerCamelCase , size["shortest_edge"] , default_to_square=__lowerCamelCase ) elif "height" in size and "width" in size: SCREAMING_SNAKE_CASE = (size["height"], size["width"]) else: raise ValueError(f"Size must have 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}" ) return resize(__lowerCamelCase , size=__lowerCamelCase , resample=__lowerCamelCase , data_format=__lowerCamelCase , **__lowerCamelCase ) def _snake_case ( self : Any , __lowerCamelCase : np.ndarray , __lowerCamelCase : Dict[str, int] , __lowerCamelCase : Optional[Union[str, ChannelDimension]] = None , **__lowerCamelCase : str , ): SCREAMING_SNAKE_CASE = get_size_dict(__lowerCamelCase ) if "height" not in size or "width" not in size: raise ValueError(f"Size must have 'height' and 'width' as keys. Got {size.keys()}" ) return center_crop(__lowerCamelCase , size=(size["height"], size["width"]) , data_format=__lowerCamelCase , **__lowerCamelCase ) def _snake_case ( self : List[Any] , __lowerCamelCase : np.ndarray , __lowerCamelCase : Union[int, float] , __lowerCamelCase : Optional[Union[str, ChannelDimension]] = None , **__lowerCamelCase : List[str] , ): return rescale(__lowerCamelCase , scale=__lowerCamelCase , data_format=__lowerCamelCase , **__lowerCamelCase ) def _snake_case ( self : str , __lowerCamelCase : np.ndarray , __lowerCamelCase : Union[float, List[float]] , __lowerCamelCase : Union[float, List[float]] , __lowerCamelCase : Optional[Union[str, ChannelDimension]] = None , **__lowerCamelCase : Dict , ): return normalize(__lowerCamelCase , mean=__lowerCamelCase , std=__lowerCamelCase , data_format=__lowerCamelCase , **__lowerCamelCase ) def _snake_case ( self : int , __lowerCamelCase : ImageInput , __lowerCamelCase : bool = None , __lowerCamelCase : Dict[str, int] = None , __lowerCamelCase : PILImageResampling = None , __lowerCamelCase : bool = None , __lowerCamelCase : Dict[str, int] = None , __lowerCamelCase : bool = None , __lowerCamelCase : float = None , __lowerCamelCase : bool = None , __lowerCamelCase : Optional[Union[float, List[float]]] = None , __lowerCamelCase : Optional[Union[float, List[float]]] = None , __lowerCamelCase : Optional[ChannelDimension] = ChannelDimension.FIRST , ): if do_resize and size is None or resample is None: raise ValueError("Size and resample must be specified if do_resize is True." ) if do_center_crop and crop_size is None: raise ValueError("Crop size must be specified if do_center_crop is True." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("Image mean and std must be specified if do_normalize is True." ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE = to_numpy_array(__lowerCamelCase ) if do_resize: SCREAMING_SNAKE_CASE = self.resize(image=__lowerCamelCase , size=__lowerCamelCase , resample=__lowerCamelCase ) if do_center_crop: SCREAMING_SNAKE_CASE = self.center_crop(__lowerCamelCase , size=__lowerCamelCase ) if do_rescale: SCREAMING_SNAKE_CASE = self.rescale(image=__lowerCamelCase , scale=__lowerCamelCase ) if do_normalize: SCREAMING_SNAKE_CASE = self.normalize(image=__lowerCamelCase , mean=__lowerCamelCase , std=__lowerCamelCase ) SCREAMING_SNAKE_CASE = to_channel_dimension_format(__lowerCamelCase , __lowerCamelCase ) return image def _snake_case ( self : List[Any] , __lowerCamelCase : ImageInput , __lowerCamelCase : bool = None , __lowerCamelCase : Dict[str, int] = None , __lowerCamelCase : PILImageResampling = None , __lowerCamelCase : bool = None , __lowerCamelCase : Dict[str, int] = None , __lowerCamelCase : bool = None , __lowerCamelCase : float = None , __lowerCamelCase : bool = None , __lowerCamelCase : Optional[Union[float, List[float]]] = None , __lowerCamelCase : Optional[Union[float, List[float]]] = None , __lowerCamelCase : Optional[Union[str, TensorType]] = None , __lowerCamelCase : ChannelDimension = ChannelDimension.FIRST , **__lowerCamelCase : List[str] , ): SCREAMING_SNAKE_CASE = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE = do_center_crop if do_center_crop is not None else self.do_center_crop SCREAMING_SNAKE_CASE = do_rescale if do_rescale is not None else self.do_rescale SCREAMING_SNAKE_CASE = rescale_factor if rescale_factor is not None else self.rescale_factor SCREAMING_SNAKE_CASE = do_normalize if do_normalize is not None else self.do_normalize SCREAMING_SNAKE_CASE = image_mean if image_mean is not None else self.image_mean SCREAMING_SNAKE_CASE = image_std if image_std is not None else self.image_std SCREAMING_SNAKE_CASE = size if size is not None else self.size SCREAMING_SNAKE_CASE = get_size_dict(__lowerCamelCase , default_to_square=__lowerCamelCase ) SCREAMING_SNAKE_CASE = crop_size if crop_size is not None else self.crop_size SCREAMING_SNAKE_CASE = get_size_dict(__lowerCamelCase , param_name="crop_size" ) if not valid_images(__lowerCamelCase ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) SCREAMING_SNAKE_CASE = make_batched(__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [ self._preprocess_image( image=__lowerCamelCase , do_resize=__lowerCamelCase , size=__lowerCamelCase , resample=__lowerCamelCase , do_center_crop=__lowerCamelCase , crop_size=__lowerCamelCase , do_rescale=__lowerCamelCase , rescale_factor=__lowerCamelCase , do_normalize=__lowerCamelCase , image_mean=__lowerCamelCase , image_std=__lowerCamelCase , data_format=__lowerCamelCase , ) for img in video ] for video in videos ] SCREAMING_SNAKE_CASE = {"pixel_values": videos} return BatchFeature(data=__lowerCamelCase , tensor_type=__lowerCamelCase )
713
from collections import deque from .hash_table import HashTable class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' def __init__( self : Optional[int] , *__lowerCamelCase : List[Any] , **__lowerCamelCase : Optional[Any] ): super().__init__(*__lowerCamelCase , **__lowerCamelCase ) def _snake_case ( self : Any , __lowerCamelCase : Dict , __lowerCamelCase : Tuple ): SCREAMING_SNAKE_CASE = deque([] ) if self.values[key] is None else self.values[key] self.values[key].appendleft(__lowerCamelCase ) SCREAMING_SNAKE_CASE = self.values[key] def _snake_case ( self : Union[str, Any] ): return ( sum(self.charge_factor - len(__lowerCamelCase ) for slot in self.values ) / self.size_table * self.charge_factor ) def _snake_case ( self : Union[str, Any] , __lowerCamelCase : Dict , __lowerCamelCase : Tuple=None ): if not ( len(self.values[key] ) == self.charge_factor and self.values.count(__lowerCamelCase ) == 0 ): return key return super()._collision_resolution(__lowerCamelCase , __lowerCamelCase )
698
0
from typing import Dict, List, Optional, Union import numpy as np from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy __A : str = logging.get_logger(__name__) class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' def __init__( self : Union[str, Any] , __lowerCamelCase : int , __lowerCamelCase : int , __lowerCamelCase : float , **__lowerCamelCase : List[str] ): SCREAMING_SNAKE_CASE = feature_size SCREAMING_SNAKE_CASE = sampling_rate SCREAMING_SNAKE_CASE = padding_value SCREAMING_SNAKE_CASE = kwargs.pop("padding_side" , "right" ) SCREAMING_SNAKE_CASE = kwargs.pop("return_attention_mask" , __lowerCamelCase ) super().__init__(**__lowerCamelCase ) def _snake_case ( self : List[str] , __lowerCamelCase : Union[ BatchFeature, List[BatchFeature], Dict[str, BatchFeature], Dict[str, List[BatchFeature]], List[Dict[str, BatchFeature]], ] , __lowerCamelCase : Union[bool, str, PaddingStrategy] = True , __lowerCamelCase : Optional[int] = None , __lowerCamelCase : bool = False , __lowerCamelCase : Optional[int] = None , __lowerCamelCase : Optional[bool] = None , __lowerCamelCase : Optional[Union[str, TensorType]] = None , ): # If we have a list of dicts, let's convert it in a dict of lists # We do this to allow using this method as a collate_fn function in PyTorch Dataloader if isinstance(__lowerCamelCase , (list, tuple) ) and isinstance(processed_features[0] , (dict, BatchFeature) ): SCREAMING_SNAKE_CASE = { key: [example[key] for example in processed_features] for key in processed_features[0].keys() } # The model's main input name, usually `input_values`, has be passed for padding if self.model_input_names[0] not in processed_features: raise ValueError( "You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`" f" to this method that includes {self.model_input_names[0]}, but you provided" f" {list(processed_features.keys() )}" ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] SCREAMING_SNAKE_CASE = ( return_attention_mask if return_attention_mask is not None else self.return_attention_mask ) if len(__lowerCamelCase ) == 0: if return_attention_mask: SCREAMING_SNAKE_CASE = [] return processed_features # If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays # and rebuild them afterwards if no return_tensors is specified # Note that we lose the specific device the tensor may be on for PyTorch SCREAMING_SNAKE_CASE = required_input[0] if isinstance(__lowerCamelCase , (list, tuple) ): # first_element might be an empty list/tuple in some edge cases so we grab the first non empty element. SCREAMING_SNAKE_CASE = 0 while len(required_input[index] ) == 0: index += 1 if index < len(__lowerCamelCase ): SCREAMING_SNAKE_CASE = required_input[index][0] if return_tensors is None: if is_tf_tensor(__lowerCamelCase ): SCREAMING_SNAKE_CASE = "tf" elif is_torch_tensor(__lowerCamelCase ): SCREAMING_SNAKE_CASE = "pt" elif isinstance(__lowerCamelCase , (int, float, list, tuple, np.ndarray) ): SCREAMING_SNAKE_CASE = "np" else: raise ValueError( f"type of {first_element} unknown: {type(__lowerCamelCase )}. " "Should be one of a python, numpy, pytorch or tensorflow object." ) for key, value in processed_features.items(): if isinstance(value[0] , (int, float) ): SCREAMING_SNAKE_CASE = to_numpy(__lowerCamelCase ) else: SCREAMING_SNAKE_CASE = [to_numpy(__lowerCamelCase ) for v in value] # Convert padding_strategy in PaddingStrategy SCREAMING_SNAKE_CASE = self._get_padding_strategies(padding=__lowerCamelCase , max_length=__lowerCamelCase ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] SCREAMING_SNAKE_CASE = len(__lowerCamelCase ) if not all(len(__lowerCamelCase ) == batch_size for v in processed_features.values() ): raise ValueError("Some items in the output dictionary have a different batch size than others." ) SCREAMING_SNAKE_CASE = [] for i in range(__lowerCamelCase ): SCREAMING_SNAKE_CASE = {k: v[i] for k, v in processed_features.items()} # truncation SCREAMING_SNAKE_CASE = self._truncate( __lowerCamelCase , max_length=__lowerCamelCase , pad_to_multiple_of=__lowerCamelCase , truncation=__lowerCamelCase , ) truncated_inputs.append(__lowerCamelCase ) if padding_strategy == PaddingStrategy.LONGEST: # make sure that `max_length` cannot be longer than the longest truncated length SCREAMING_SNAKE_CASE = max(len(input_slice[self.model_input_names[0]] ) for input_slice in truncated_inputs ) SCREAMING_SNAKE_CASE = PaddingStrategy.MAX_LENGTH SCREAMING_SNAKE_CASE = {} for i in range(__lowerCamelCase ): # padding SCREAMING_SNAKE_CASE = self._pad( truncated_inputs[i] , max_length=__lowerCamelCase , padding_strategy=__lowerCamelCase , pad_to_multiple_of=__lowerCamelCase , return_attention_mask=__lowerCamelCase , ) for key, value in outputs.items(): if key not in batch_outputs: SCREAMING_SNAKE_CASE = [] if value.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE = value.astype(np.floataa ) batch_outputs[key].append(__lowerCamelCase ) return BatchFeature(__lowerCamelCase , tensor_type=__lowerCamelCase ) def _snake_case ( self : List[str] , __lowerCamelCase : Union[Dict[str, np.ndarray], BatchFeature] , __lowerCamelCase : Optional[int] = None , __lowerCamelCase : PaddingStrategy = PaddingStrategy.DO_NOT_PAD , __lowerCamelCase : Optional[int] = None , __lowerCamelCase : Optional[bool] = None , ): SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] if padding_strategy == PaddingStrategy.LONGEST: SCREAMING_SNAKE_CASE = len(__lowerCamelCase ) if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): SCREAMING_SNAKE_CASE = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of SCREAMING_SNAKE_CASE = padding_strategy != PaddingStrategy.DO_NOT_PAD and len(__lowerCamelCase ) < max_length if return_attention_mask and "attention_mask" not in processed_features: SCREAMING_SNAKE_CASE = np.ones(len(__lowerCamelCase ) , dtype=np.intaa ) if needs_to_be_padded: SCREAMING_SNAKE_CASE = max_length - len(__lowerCamelCase ) if self.padding_side == "right": if return_attention_mask: SCREAMING_SNAKE_CASE = np.pad( processed_features["attention_mask"] , (0, difference) ) SCREAMING_SNAKE_CASE = ((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference) SCREAMING_SNAKE_CASE = np.pad( __lowerCamelCase , __lowerCamelCase , "constant" , constant_values=self.padding_value ) elif self.padding_side == "left": if return_attention_mask: SCREAMING_SNAKE_CASE = np.pad( processed_features["attention_mask"] , (difference, 0) ) SCREAMING_SNAKE_CASE = ((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0) SCREAMING_SNAKE_CASE = np.pad( __lowerCamelCase , __lowerCamelCase , "constant" , constant_values=self.padding_value ) else: raise ValueError("Invalid padding strategy:" + str(self.padding_side ) ) return processed_features def _snake_case ( self : Optional[Any] , __lowerCamelCase : Union[Dict[str, np.ndarray], BatchFeature] , __lowerCamelCase : Optional[int] = None , __lowerCamelCase : Optional[int] = None , __lowerCamelCase : Optional[bool] = None , ): if not truncation: return processed_features elif truncation and max_length is None: raise ValueError("When setting ``truncation=True``, make sure that ``max_length`` is defined." ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] # find `max_length` that fits `pad_to_multiple_of` if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): SCREAMING_SNAKE_CASE = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of SCREAMING_SNAKE_CASE = len(__lowerCamelCase ) > max_length if needs_to_be_truncated: SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]][:max_length] if "attention_mask" in processed_features: SCREAMING_SNAKE_CASE = processed_features["attention_mask"][:max_length] return processed_features def _snake_case ( self : int , __lowerCamelCase : Optional[Any]=False , __lowerCamelCase : Any=None ): # Get padding strategy if padding is not False: if padding is True: SCREAMING_SNAKE_CASE = PaddingStrategy.LONGEST # Default to pad to the longest sequence in the batch elif not isinstance(__lowerCamelCase , __lowerCamelCase ): SCREAMING_SNAKE_CASE = PaddingStrategy(__lowerCamelCase ) elif isinstance(__lowerCamelCase , __lowerCamelCase ): SCREAMING_SNAKE_CASE = padding else: SCREAMING_SNAKE_CASE = PaddingStrategy.DO_NOT_PAD # Set max length if needed if max_length is None: if padding_strategy == PaddingStrategy.MAX_LENGTH: raise ValueError( f"When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined" ) # Test if we have a padding value if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None): raise ValueError( "Asking to pad but the feature_extractor does not have a padding value. Please select a value to use" " as `padding_value`. For example: `feature_extractor.padding_value = 0.0`." ) return padding_strategy
714
from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast from ...utils import logging __A : Optional[int] = logging.get_logger(__name__) __A : int = { 'EleutherAI/gpt-neo-1.3B': 'https://huggingface.co/EleutherAI/gpt-neo-1.3B/resolve/main/config.json', # See all GPTNeo models at https://huggingface.co/models?filter=gpt_neo } class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' lowerCamelCase__ = "gpt_neo" lowerCamelCase__ = ["past_key_values"] lowerCamelCase__ = {"num_attention_heads": "num_heads", "num_hidden_layers": "num_layers"} def __init__( self : str , __lowerCamelCase : Dict=50257 , __lowerCamelCase : Tuple=2048 , __lowerCamelCase : Optional[Any]=2048 , __lowerCamelCase : int=24 , __lowerCamelCase : int=[[["global", "local"], 12]] , __lowerCamelCase : int=16 , __lowerCamelCase : List[str]=None , __lowerCamelCase : List[Any]=256 , __lowerCamelCase : Tuple="gelu_new" , __lowerCamelCase : Optional[Any]=0.0 , __lowerCamelCase : str=0.0 , __lowerCamelCase : List[Any]=0.0 , __lowerCamelCase : Optional[int]=0.1 , __lowerCamelCase : List[Any]=1e-5 , __lowerCamelCase : Dict=0.02 , __lowerCamelCase : Dict=True , __lowerCamelCase : Dict=50256 , __lowerCamelCase : Optional[int]=50256 , **__lowerCamelCase : Dict , ): SCREAMING_SNAKE_CASE = vocab_size SCREAMING_SNAKE_CASE = max_position_embeddings SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = num_layers SCREAMING_SNAKE_CASE = num_heads SCREAMING_SNAKE_CASE = intermediate_size SCREAMING_SNAKE_CASE = window_size SCREAMING_SNAKE_CASE = activation_function SCREAMING_SNAKE_CASE = resid_dropout SCREAMING_SNAKE_CASE = embed_dropout SCREAMING_SNAKE_CASE = attention_dropout SCREAMING_SNAKE_CASE = classifier_dropout SCREAMING_SNAKE_CASE = layer_norm_epsilon SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = use_cache SCREAMING_SNAKE_CASE = bos_token_id SCREAMING_SNAKE_CASE = eos_token_id SCREAMING_SNAKE_CASE = attention_types SCREAMING_SNAKE_CASE = self.expand_attention_types_params(__lowerCamelCase ) if len(self.attention_layers ) != self.num_layers: raise ValueError( "Configuration for convolutional module is incorrect. " "It is required that `len(config.attention_layers)` == `config.num_layers` " f"but is `len(config.attention_layers) = {len(self.attention_layers )}`, " f"`config.num_layers = {self.num_layers}`. " "`config.attention_layers` is prepared using `config.attention_types`. " "Please verify the value of `config.attention_types` argument." ) super().__init__(bos_token_id=__lowerCamelCase , eos_token_id=__lowerCamelCase , **__lowerCamelCase ) @staticmethod def _snake_case ( __lowerCamelCase : Dict ): SCREAMING_SNAKE_CASE = [] for item in attention_types: for _ in range(item[1] ): attentions.extend(item[0] ) return attentions def __a ( A__ : str , A__ : List[Any] , A__ : List[str] , A__ : Union[str, Any] ): import torch SCREAMING_SNAKE_CASE = input.size() SCREAMING_SNAKE_CASE = len(A__ ) SCREAMING_SNAKE_CASE = shape[dimension] SCREAMING_SNAKE_CASE = torch.arange(0 , A__ , A__ ) SCREAMING_SNAKE_CASE = torch.div(sizedim - size , A__ , rounding_mode="floor" ) + 1 SCREAMING_SNAKE_CASE = torch.arange(A__ ) + low_indices[:min_length][:, None] SCREAMING_SNAKE_CASE = [slice(A__ )] * rank SCREAMING_SNAKE_CASE = indices SCREAMING_SNAKE_CASE = input[s] SCREAMING_SNAKE_CASE = list(range(0 , rank + 1 ) ) perm.append(perm.pop(dimension + 1 ) ) return sliced.permute(A__ ) def __a ( A__ : Union[str, Any] , A__ : Optional[int] ): import torch SCREAMING_SNAKE_CASE = torch.arange(1 , A__ ) SCREAMING_SNAKE_CASE = torch.remainder(A__ , A__ ) SCREAMING_SNAKE_CASE = remainders == 0 SCREAMING_SNAKE_CASE = candidates[divisor_indices] SCREAMING_SNAKE_CASE = torch.max(A__ ) return largest_divisor, torch.div(A__ , A__ , rounding_mode="floor" ) class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' @property def _snake_case ( self : Any ): SCREAMING_SNAKE_CASE = OrderedDict({"input_ids": {0: "batch", 1: "sequence"}} ) if self.use_past: self.fill_with_past_key_values_(__lowerCamelCase , direction="inputs" ) SCREAMING_SNAKE_CASE = {0: "batch", 1: "past_sequence + sequence"} else: SCREAMING_SNAKE_CASE = {0: "batch", 1: "sequence"} return common_inputs @property def _snake_case ( self : Optional[int] ): return self._config.num_heads def _snake_case ( self : Union[str, Any] , __lowerCamelCase : PreTrainedTokenizer , __lowerCamelCase : int = -1 , __lowerCamelCase : int = -1 , __lowerCamelCase : bool = False , __lowerCamelCase : Optional[TensorType] = None , ): SCREAMING_SNAKE_CASE = super(__lowerCamelCase , self ).generate_dummy_inputs( __lowerCamelCase , batch_size=__lowerCamelCase , seq_length=__lowerCamelCase , is_pair=__lowerCamelCase , framework=__lowerCamelCase ) # We need to order the input in the way they appears in the forward() SCREAMING_SNAKE_CASE = OrderedDict({"input_ids": common_inputs["input_ids"]} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." ) else: import torch SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = common_inputs["input_ids"].shape # Not using the same length for past_key_values SCREAMING_SNAKE_CASE = seqlen + 2 SCREAMING_SNAKE_CASE = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) SCREAMING_SNAKE_CASE = [ (torch.zeros(__lowerCamelCase ), torch.zeros(__lowerCamelCase )) for _ in range(self.num_layers ) ] SCREAMING_SNAKE_CASE = common_inputs["attention_mask"] if self.use_past: SCREAMING_SNAKE_CASE = ordered_inputs["attention_mask"].dtype SCREAMING_SNAKE_CASE = torch.cat( [ordered_inputs["attention_mask"], torch.ones(__lowerCamelCase , __lowerCamelCase , dtype=__lowerCamelCase )] , dim=1 ) return ordered_inputs @property def _snake_case ( self : Optional[int] ): return 13
698
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __A : Any = { 'configuration_roberta': ['ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP', 'RobertaConfig', 'RobertaOnnxConfig'], 'tokenization_roberta': ['RobertaTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : str = ['RobertaTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[str] = [ 'ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'RobertaForCausalLM', 'RobertaForMaskedLM', 'RobertaForMultipleChoice', 'RobertaForQuestionAnswering', 'RobertaForSequenceClassification', 'RobertaForTokenClassification', 'RobertaModel', 'RobertaPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Optional[Any] = [ 'TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFRobertaForCausalLM', 'TFRobertaForMaskedLM', 'TFRobertaForMultipleChoice', 'TFRobertaForQuestionAnswering', 'TFRobertaForSequenceClassification', 'TFRobertaForTokenClassification', 'TFRobertaMainLayer', 'TFRobertaModel', 'TFRobertaPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Union[str, Any] = [ 'FlaxRobertaForCausalLM', 'FlaxRobertaForMaskedLM', 'FlaxRobertaForMultipleChoice', 'FlaxRobertaForQuestionAnswering', 'FlaxRobertaForSequenceClassification', 'FlaxRobertaForTokenClassification', 'FlaxRobertaModel', 'FlaxRobertaPreTrainedModel', ] if TYPE_CHECKING: from .configuration_roberta import ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, RobertaConfig, RobertaOnnxConfig from .tokenization_roberta import RobertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roberta_fast import RobertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roberta import ( ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, RobertaForCausalLM, RobertaForMaskedLM, RobertaForMultipleChoice, RobertaForQuestionAnswering, RobertaForSequenceClassification, RobertaForTokenClassification, RobertaModel, RobertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roberta import ( TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFRobertaForCausalLM, TFRobertaForMaskedLM, TFRobertaForMultipleChoice, TFRobertaForQuestionAnswering, TFRobertaForSequenceClassification, TFRobertaForTokenClassification, TFRobertaMainLayer, TFRobertaModel, TFRobertaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, FlaxRobertaPreTrainedModel, ) else: import sys __A : Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
715
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available __A : Any = { 'configuration_longt5': ['LONGT5_PRETRAINED_CONFIG_ARCHIVE_MAP', 'LongT5Config', 'LongT5OnnxConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[str] = [ 'LONGT5_PRETRAINED_MODEL_ARCHIVE_LIST', 'LongT5EncoderModel', 'LongT5ForConditionalGeneration', 'LongT5Model', 'LongT5PreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[Any] = [ 'FlaxLongT5ForConditionalGeneration', 'FlaxLongT5Model', 'FlaxLongT5PreTrainedModel', ] if TYPE_CHECKING: from .configuration_longta import LONGT5_PRETRAINED_CONFIG_ARCHIVE_MAP, LongTaConfig, LongTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_longta import ( LONGT5_PRETRAINED_MODEL_ARCHIVE_LIST, LongTaEncoderModel, LongTaForConditionalGeneration, LongTaModel, LongTaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_longta import ( FlaxLongTaForConditionalGeneration, FlaxLongTaModel, FlaxLongTaPreTrainedModel, ) else: import sys __A : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
698
0
from collections.abc import Sequence def __a ( A__ : Sequence[int] | None = None ): if nums is None or not nums: raise ValueError("Input sequence should not be empty" ) SCREAMING_SNAKE_CASE = nums[0] for i in range(1 , len(A__ ) ): SCREAMING_SNAKE_CASE = nums[i] SCREAMING_SNAKE_CASE = max(A__ , ans + num , A__ ) return ans if __name__ == "__main__": import doctest doctest.testmod() # Try on a sample input from the user __A : Optional[int] = int(input('Enter number of elements : ').strip()) __A : Optional[int] = list(map(int, input('\nEnter the numbers : ').strip().split()))[:n] print(max_subsequence_sum(array))
716
import cmath import math def __a ( A__ : float , A__ : float , A__ : float , A__ : float ): SCREAMING_SNAKE_CASE = math.radians(A__ ) SCREAMING_SNAKE_CASE = math.radians(A__ ) # Convert voltage and current to rectangular form SCREAMING_SNAKE_CASE = cmath.rect(A__ , A__ ) SCREAMING_SNAKE_CASE = cmath.rect(A__ , A__ ) # Calculate apparent power return voltage_rect * current_rect if __name__ == "__main__": import doctest doctest.testmod()
698
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __A : str = { 'configuration_swinv2': ['SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Swinv2Config'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : int = [ '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 __A : int = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
717
import argparse import json import os import torch from torch import nn from transformers import NllbMoeConfig, NllbMoeModel from transformers.modeling_utils import dtype_byte_size from transformers.utils import WEIGHTS_INDEX_NAME, WEIGHTS_NAME def __a ( A__ : List[str] ): SCREAMING_SNAKE_CASE = [ "encoder.version", "decoder.version", "model.encoder.version", "model.decoder.version", "decoder.output_projection.weight", "_float_tensor", "encoder.embed_positions._float_tensor", "decoder.embed_positions._float_tensor", ] for k in ignore_keys: state_dict.pop(A__ , A__ ) def __a ( A__ : Tuple ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = emb.weight.shape SCREAMING_SNAKE_CASE = nn.Linear(A__ , A__ , bias=A__ ) SCREAMING_SNAKE_CASE = emb.weight.data return lin_layer def __a ( A__ : Tuple , A__ : Union[str, Any]=None ): SCREAMING_SNAKE_CASE = {} for old_key in state_dict.keys(): SCREAMING_SNAKE_CASE = old_key if "moe_layer.experts." in key: if expert_idx is not None: SCREAMING_SNAKE_CASE = key.replace("moe_layer.experts.0" , F"ffn.experts.expert_{expert_idx}" ) else: SCREAMING_SNAKE_CASE = key.replace("moe_layer.experts." , "ffn.experts.expert_" ) if "gate" in key: SCREAMING_SNAKE_CASE = key.replace(".moe_layer.gate.wg" , ".ffn.router.classifier" ) if "fc2" and "experts" not in key: SCREAMING_SNAKE_CASE = key.replace(".fc2." , ".ffn.fc2." ) if "fc1" and "experts" not in key: SCREAMING_SNAKE_CASE = key.replace(".fc1." , ".ffn.fc1." ) if ".encoder_attn." in key: SCREAMING_SNAKE_CASE = key.replace(".encoder_attn." , ".cross_attention." ) if "encoder_attn_layer_norm" in key: SCREAMING_SNAKE_CASE = key.replace("encoder_attn_layer_norm" , "cross_attention_layer_norm" ) if "final_layer_norm" in key: SCREAMING_SNAKE_CASE = key.replace("final_layer_norm" , "ff_layer_norm" ) SCREAMING_SNAKE_CASE = state_dict[old_key] return new_dict def __a ( A__ : List[str] , A__ : List[Any] , A__ : str , A__ : Union[str, Any] , A__ : str = WEIGHTS_NAME ): SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = 0 os.makedirs(A__ , exist_ok=A__ ) for expert in range(A__ ): SCREAMING_SNAKE_CASE = switch_checkpoint_path + F"-rank-{expert}.pt" if os.path.isfile(A__ ): SCREAMING_SNAKE_CASE = torch.load(A__ )["model"] remove_ignore_keys_(A__ ) SCREAMING_SNAKE_CASE = rename_fairseq_keys(A__ , A__ ) SCREAMING_SNAKE_CASE = os.path.join( A__ , weights_name.replace(".bin" , F"-{len(A__ )+1:05d}-of-???.bin" ) ) torch.save(A__ , A__ ) sharded_state_dicts.append(expert_state.keys() ) total_size += sum([value.numel() for key, value in expert_state.items()] ) * dtype_byte_size( expert_state[list(A__ )[0]].dtype ) # Add the last block SCREAMING_SNAKE_CASE = os.path.join(A__ , weights_name.replace(".bin" , F"-{len(A__ )+1:05d}-of-???.bin" ) ) SCREAMING_SNAKE_CASE = torch.load(switch_checkpoint_path + "-shared.pt" )["model"] remove_ignore_keys_(A__ ) SCREAMING_SNAKE_CASE = rename_fairseq_keys(A__ , A__ ) SCREAMING_SNAKE_CASE = shared_weights["decoder.embed_tokens.weight"] sharded_state_dicts.append(shared_weights.keys() ) # If we only have the shared weights (dummy model/experts saved on the same file) if len(A__ ) == 1: SCREAMING_SNAKE_CASE = os.path.join(A__ , A__ ) torch.save(A__ , A__ ) return {weights_name: sharded_state_dicts[0]}, None else: torch.save(A__ , A__ ) # Otherwise, let's build the index SCREAMING_SNAKE_CASE = {} for idx, shard in enumerate(A__ ): SCREAMING_SNAKE_CASE = weights_name.replace(".bin" , F"-{idx+1:05d}-of-{len(A__ ):05d}.bin" ) SCREAMING_SNAKE_CASE = os.path.join(A__ , weights_name.replace(".bin" , F"-{idx+1:05d}-of-???.bin" ) ) os.rename(A__ , os.path.join(A__ , A__ ) ) for key in shard: SCREAMING_SNAKE_CASE = shard_file # Add the metadata SCREAMING_SNAKE_CASE = {"total_size": total_size} SCREAMING_SNAKE_CASE = {"metadata": metadata, "weight_map": weight_map} with open(os.path.join(A__ , A__ ) , "w" , encoding="utf-8" ) as f: SCREAMING_SNAKE_CASE = json.dumps(A__ , indent=2 , sort_keys=A__ ) + "\n" f.write(A__ ) return metadata, index if __name__ == "__main__": __A : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument( '--nllb_moe_checkpoint_path', default='/home/arthur_huggingface_co/fairseq/weights/checkpoints/model_moe_54b/checkpoint_2_300000', type=str, required=False, help='Path to a directory containing a folder per layer. Follows the original Google format.', ) parser.add_argument('--dtype', default='float32', type=str, required=False, help='dtype of the saved model') parser.add_argument( '--pytorch_dump_folder_path', default='/home/arthur_huggingface_co/fairseq/weights/checkpoints/hf-converted-moe-54b', type=str, required=False, help='Path to the output pytorch model.', ) __A : Optional[int] = parser.parse_args() __A , __A : Union[str, Any] = shard_on_the_fly( args.nllb_moe_checkpoint_path, args.pytorch_dump_folder_path, 1_2_8, args.dtype, ) __A : Any = NllbMoeConfig.from_pretrained( 'facebook/nllb-200-3.3B', encoder_sparse_step=4, decoder_sparse_step=4, num_experts=1_2_8 ) config.save_pretrained(args.pytorch_dump_folder_path) __A : Any = NllbMoeModel.from_pretrained(args.pytorch_dump_folder_path) print('Done') model.save_pretrained(args.pytorch_dump_folder_path)
698
0
from collections import UserDict from typing import Union import numpy as np import requests from ..utils import ( add_end_docstrings, logging, ) from .audio_classification import ffmpeg_read from .base import PIPELINE_INIT_ARGS, Pipeline __A : Optional[Any] = logging.get_logger(__name__) @add_end_docstrings(__snake_case ) class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' def __init__( self : int , **__lowerCamelCase : Optional[int] ): super().__init__(**__lowerCamelCase ) if self.framework != "pt": raise ValueError(f"The {self.__class__} is only available in PyTorch." ) # No specific FOR_XXX available yet def __call__( self : Tuple , __lowerCamelCase : Union[np.ndarray, bytes, str] , **__lowerCamelCase : Union[str, Any] ): return super().__call__(__lowerCamelCase , **__lowerCamelCase ) def _snake_case ( self : Dict , **__lowerCamelCase : str ): SCREAMING_SNAKE_CASE = {} if "candidate_labels" in kwargs: SCREAMING_SNAKE_CASE = kwargs["candidate_labels"] if "hypothesis_template" in kwargs: SCREAMING_SNAKE_CASE = kwargs["hypothesis_template"] return preprocess_params, {}, {} def _snake_case ( self : List[str] , __lowerCamelCase : int , __lowerCamelCase : List[str]=None , __lowerCamelCase : Optional[Any]="This is a sound of {}." ): if isinstance(__lowerCamelCase , __lowerCamelCase ): if audio.startswith("http://" ) or audio.startswith("https://" ): # We need to actually check for a real protocol, otherwise it's impossible to use a local file # like http_huggingface_co.png SCREAMING_SNAKE_CASE = requests.get(__lowerCamelCase ).content else: with open(__lowerCamelCase , "rb" ) as f: SCREAMING_SNAKE_CASE = f.read() if isinstance(__lowerCamelCase , __lowerCamelCase ): SCREAMING_SNAKE_CASE = ffmpeg_read(__lowerCamelCase , self.feature_extractor.sampling_rate ) if not isinstance(__lowerCamelCase , np.ndarray ): raise ValueError("We expect a numpy ndarray as input" ) if len(audio.shape ) != 1: raise ValueError("We expect a single channel audio input for ZeroShotAudioClassificationPipeline" ) SCREAMING_SNAKE_CASE = self.feature_extractor( [audio] , sampling_rate=self.feature_extractor.sampling_rate , return_tensors="pt" ) SCREAMING_SNAKE_CASE = candidate_labels SCREAMING_SNAKE_CASE = [hypothesis_template.format(__lowerCamelCase ) for x in candidate_labels] SCREAMING_SNAKE_CASE = self.tokenizer(__lowerCamelCase , return_tensors=self.framework , padding=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [text_inputs] return inputs def _snake_case ( self : int , __lowerCamelCase : List[Any] ): SCREAMING_SNAKE_CASE = model_inputs.pop("candidate_labels" ) SCREAMING_SNAKE_CASE = model_inputs.pop("text_inputs" ) if isinstance(text_inputs[0] , __lowerCamelCase ): SCREAMING_SNAKE_CASE = text_inputs[0] else: # Batching case. SCREAMING_SNAKE_CASE = text_inputs[0][0] SCREAMING_SNAKE_CASE = self.model(**__lowerCamelCase , **__lowerCamelCase ) SCREAMING_SNAKE_CASE = { "candidate_labels": candidate_labels, "logits": outputs.logits_per_audio, } return model_outputs def _snake_case ( self : Any , __lowerCamelCase : Optional[int] ): SCREAMING_SNAKE_CASE = model_outputs.pop("candidate_labels" ) SCREAMING_SNAKE_CASE = model_outputs["logits"][0] if self.framework == "pt": SCREAMING_SNAKE_CASE = logits.softmax(dim=0 ) SCREAMING_SNAKE_CASE = probs.tolist() else: raise ValueError("`tf` framework not supported." ) SCREAMING_SNAKE_CASE = [ {"score": score, "label": candidate_label} for score, candidate_label in sorted(zip(__lowerCamelCase , __lowerCamelCase ) , key=lambda __lowerCamelCase : -x[0] ) ] return result
718
import shutil import tempfile import unittest import numpy as np import pytest from transformers.testing_utils import require_vision from transformers.utils import is_vision_available if is_vision_available(): from PIL import Image from transformers import AutoProcessor, BertTokenizer, BlipImageProcessor, BlipProcessor, PreTrainedTokenizerFast @require_vision class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' def _snake_case ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE = tempfile.mkdtemp() SCREAMING_SNAKE_CASE = BlipImageProcessor() SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained("hf-internal-testing/tiny-random-BertModel" ) SCREAMING_SNAKE_CASE = BlipProcessor(__lowerCamelCase , __lowerCamelCase ) processor.save_pretrained(self.tmpdirname ) def _snake_case ( self : Dict , **__lowerCamelCase : Any ): return AutoProcessor.from_pretrained(self.tmpdirname , **__lowerCamelCase ).tokenizer def _snake_case ( self : List[Any] , **__lowerCamelCase : Optional[Any] ): return AutoProcessor.from_pretrained(self.tmpdirname , **__lowerCamelCase ).image_processor def _snake_case ( self : Union[str, Any] ): shutil.rmtree(self.tmpdirname ) def _snake_case ( self : Tuple ): SCREAMING_SNAKE_CASE = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] SCREAMING_SNAKE_CASE = [Image.fromarray(np.moveaxis(__lowerCamelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def _snake_case ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE = BlipProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) SCREAMING_SNAKE_CASE = self.get_tokenizer(bos_token="(BOS)" , eos_token="(EOS)" ) SCREAMING_SNAKE_CASE = self.get_image_processor(do_normalize=__lowerCamelCase , padding_value=1.0 ) SCREAMING_SNAKE_CASE = BlipProcessor.from_pretrained( self.tmpdirname , bos_token="(BOS)" , eos_token="(EOS)" , do_normalize=__lowerCamelCase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __lowerCamelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __lowerCamelCase ) def _snake_case ( self : Optional[int] ): SCREAMING_SNAKE_CASE = self.get_image_processor() SCREAMING_SNAKE_CASE = self.get_tokenizer() SCREAMING_SNAKE_CASE = BlipProcessor(tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) SCREAMING_SNAKE_CASE = self.prepare_image_inputs() SCREAMING_SNAKE_CASE = image_processor(__lowerCamelCase , return_tensors="np" ) SCREAMING_SNAKE_CASE = processor(images=__lowerCamelCase , return_tensors="np" ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def _snake_case ( self : Dict ): SCREAMING_SNAKE_CASE = self.get_image_processor() SCREAMING_SNAKE_CASE = self.get_tokenizer() SCREAMING_SNAKE_CASE = BlipProcessor(tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) SCREAMING_SNAKE_CASE = "lower newer" SCREAMING_SNAKE_CASE = processor(text=__lowerCamelCase ) SCREAMING_SNAKE_CASE = tokenizer(__lowerCamelCase , return_token_type_ids=__lowerCamelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def _snake_case ( self : str ): SCREAMING_SNAKE_CASE = self.get_image_processor() SCREAMING_SNAKE_CASE = self.get_tokenizer() SCREAMING_SNAKE_CASE = BlipProcessor(tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) SCREAMING_SNAKE_CASE = "lower newer" SCREAMING_SNAKE_CASE = self.prepare_image_inputs() SCREAMING_SNAKE_CASE = processor(text=__lowerCamelCase , images=__lowerCamelCase ) self.assertListEqual(list(inputs.keys() ) , ["pixel_values", "input_ids", "attention_mask"] ) # test if it raises when no input is passed with pytest.raises(__lowerCamelCase ): processor() def _snake_case ( self : Any ): SCREAMING_SNAKE_CASE = self.get_image_processor() SCREAMING_SNAKE_CASE = self.get_tokenizer() SCREAMING_SNAKE_CASE = BlipProcessor(tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] SCREAMING_SNAKE_CASE = processor.batch_decode(__lowerCamelCase ) SCREAMING_SNAKE_CASE = tokenizer.batch_decode(__lowerCamelCase ) self.assertListEqual(__lowerCamelCase , __lowerCamelCase ) def _snake_case ( self : Dict ): SCREAMING_SNAKE_CASE = self.get_image_processor() SCREAMING_SNAKE_CASE = self.get_tokenizer() SCREAMING_SNAKE_CASE = BlipProcessor(tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) SCREAMING_SNAKE_CASE = "lower newer" SCREAMING_SNAKE_CASE = self.prepare_image_inputs() SCREAMING_SNAKE_CASE = processor(text=__lowerCamelCase , images=__lowerCamelCase ) # For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask'] self.assertListEqual(list(inputs.keys() ) , ["pixel_values", "input_ids", "attention_mask"] )
698
0
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices __A : int = logging.get_logger(__name__) __A : Optional[int] = { 'google/bit-50': 'https://huggingface.co/google/bit-50/resolve/main/config.json', } class _SCREAMING_SNAKE_CASE ( __snake_case , __snake_case ): '''simple docstring''' lowerCamelCase__ = "bit" lowerCamelCase__ = ["preactivation", "bottleneck"] lowerCamelCase__ = ["SAME", "VALID"] def __init__( self : List[str] , __lowerCamelCase : List[Any]=3 , __lowerCamelCase : Optional[Any]=64 , __lowerCamelCase : int=[256, 512, 1024, 2048] , __lowerCamelCase : int=[3, 4, 6, 3] , __lowerCamelCase : Tuple="preactivation" , __lowerCamelCase : Any="relu" , __lowerCamelCase : str=None , __lowerCamelCase : List[Any]=32 , __lowerCamelCase : Union[str, Any]=0.0 , __lowerCamelCase : List[Any]=False , __lowerCamelCase : Optional[int]=32 , __lowerCamelCase : Optional[int]=1 , __lowerCamelCase : Optional[Any]=None , __lowerCamelCase : Union[str, Any]=None , **__lowerCamelCase : Any , ): super().__init__(**__lowerCamelCase ) if layer_type not in self.layer_types: raise ValueError(f"layer_type={layer_type} is not one of {','.join(self.layer_types )}" ) if global_padding is not None: if global_padding.upper() in self.supported_padding: SCREAMING_SNAKE_CASE = global_padding.upper() else: raise ValueError(f"Padding strategy {global_padding} not supported" ) SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = embedding_size SCREAMING_SNAKE_CASE = hidden_sizes SCREAMING_SNAKE_CASE = depths SCREAMING_SNAKE_CASE = layer_type SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = global_padding SCREAMING_SNAKE_CASE = num_groups SCREAMING_SNAKE_CASE = drop_path_rate SCREAMING_SNAKE_CASE = embedding_dynamic_padding SCREAMING_SNAKE_CASE = output_stride SCREAMING_SNAKE_CASE = width_factor SCREAMING_SNAKE_CASE = ["stem"] + [f"stage{idx}" for idx in range(1 , len(__lowerCamelCase ) + 1 )] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = get_aligned_output_features_output_indices( out_features=__lowerCamelCase , out_indices=__lowerCamelCase , stage_names=self.stage_names )
719
import unittest from transformers import MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING, AutoTokenizer, is_vision_available from transformers.pipelines import pipeline from transformers.pipelines.document_question_answering import apply_tesseract from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_detectrona, require_pytesseract, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image from transformers.image_utils import load_image else: class _SCREAMING_SNAKE_CASE : '''simple docstring''' @staticmethod def _snake_case ( *__lowerCamelCase : Optional[Any] , **__lowerCamelCase : Union[str, Any] ): pass def __a ( A__ : str ): return None # This is a pinned image from a specific revision of a document question answering space, hosted by HuggingFace, # so we can expect it to be available. __A : Tuple = ( 'https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png' ) @is_pipeline_test @require_torch @require_vision class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING @require_pytesseract @require_vision def _snake_case ( self : Optional[Any] , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : Dict , __lowerCamelCase : Optional[Any] ): SCREAMING_SNAKE_CASE = pipeline( "document-question-answering" , model=__lowerCamelCase , tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = list(zip(*apply_tesseract(load_image(__lowerCamelCase ) , __lowerCamelCase , "" ) ) ) SCREAMING_SNAKE_CASE = "What is the placebo?" SCREAMING_SNAKE_CASE = [ { "image": load_image(__lowerCamelCase ), "question": question, }, { "image": image, "question": question, }, { "image": image, "question": question, "word_boxes": word_boxes, }, ] return dqa_pipeline, examples def _snake_case ( self : List[Any] , __lowerCamelCase : int , __lowerCamelCase : int ): SCREAMING_SNAKE_CASE = dqa_pipeline(__lowerCamelCase , top_k=2 ) self.assertEqual( __lowerCamelCase , [ [ {"score": ANY(__lowerCamelCase ), "answer": ANY(__lowerCamelCase ), "start": ANY(__lowerCamelCase ), "end": ANY(__lowerCamelCase )}, {"score": ANY(__lowerCamelCase ), "answer": ANY(__lowerCamelCase ), "start": ANY(__lowerCamelCase ), "end": ANY(__lowerCamelCase )}, ] ] * 3 , ) @require_torch @require_detectrona @require_pytesseract def _snake_case ( self : Optional[int] ): SCREAMING_SNAKE_CASE = pipeline("document-question-answering" , model="hf-internal-testing/tiny-random-layoutlmv2" ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = "How many cats are there?" SCREAMING_SNAKE_CASE = [ {"score": 0.0_001, "answer": "oy 2312/2019", "start": 38, "end": 39}, {"score": 0.0_001, "answer": "oy 2312/2019 DUE", "start": 38, "end": 40}, ] SCREAMING_SNAKE_CASE = dqa_pipeline(image=__lowerCamelCase , question=__lowerCamelCase , top_k=2 ) self.assertEqual(nested_simplify(__lowerCamelCase , decimals=4 ) , __lowerCamelCase ) SCREAMING_SNAKE_CASE = dqa_pipeline({"image": image, "question": question} , top_k=2 ) self.assertEqual(nested_simplify(__lowerCamelCase , decimals=4 ) , __lowerCamelCase ) # This image does not detect ANY text in it, meaning layoutlmv2 should fail. # Empty answer probably SCREAMING_SNAKE_CASE = "./tests/fixtures/tests_samples/COCO/000000039769.png" SCREAMING_SNAKE_CASE = dqa_pipeline(image=__lowerCamelCase , question=__lowerCamelCase , top_k=2 ) self.assertEqual(__lowerCamelCase , [] ) # We can optionnally pass directly the words and bounding boxes SCREAMING_SNAKE_CASE = "./tests/fixtures/tests_samples/COCO/000000039769.png" SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = dqa_pipeline(image=__lowerCamelCase , question=__lowerCamelCase , words=__lowerCamelCase , boxes=__lowerCamelCase , top_k=2 ) self.assertEqual(__lowerCamelCase , [] ) @slow @require_torch @require_detectrona @require_pytesseract def _snake_case ( self : List[str] ): SCREAMING_SNAKE_CASE = pipeline( "document-question-answering" , model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa" , revision="9977165" , ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = "What is the invoice number?" SCREAMING_SNAKE_CASE = dqa_pipeline(image=__lowerCamelCase , question=__lowerCamelCase , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.9_944, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_009, "answer": "us-001", "start": 16, "end": 16}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline({"image": image, "question": question} , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.9_944, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_009, "answer": "us-001", "start": 16, "end": 16}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline( [{"image": image, "question": question}, {"image": image, "question": question}] , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ [ {"score": 0.9_944, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_009, "answer": "us-001", "start": 16, "end": 16}, ], ] * 2 , ) @slow @require_torch @require_detectrona @require_pytesseract def _snake_case ( self : str ): SCREAMING_SNAKE_CASE = pipeline( "document-question-answering" , model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa" , revision="9977165" , max_seq_len=50 , ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = "What is the invoice number?" SCREAMING_SNAKE_CASE = dqa_pipeline(image=__lowerCamelCase , question=__lowerCamelCase , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.9_974, "answer": "1110212019", "start": 23, "end": 23}, {"score": 0.9_948, "answer": "us-001", "start": 16, "end": 16}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline({"image": image, "question": question} , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.9_974, "answer": "1110212019", "start": 23, "end": 23}, {"score": 0.9_948, "answer": "us-001", "start": 16, "end": 16}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline( [{"image": image, "question": question}, {"image": image, "question": question}] , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ [ {"score": 0.9_974, "answer": "1110212019", "start": 23, "end": 23}, {"score": 0.9_948, "answer": "us-001", "start": 16, "end": 16}, ] ] * 2 , ) @slow @require_torch @require_pytesseract @require_vision def _snake_case ( self : str ): SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained( "impira/layoutlm-document-qa" , revision="3dc6de3" , add_prefix_space=__lowerCamelCase ) SCREAMING_SNAKE_CASE = pipeline( "document-question-answering" , model="impira/layoutlm-document-qa" , tokenizer=__lowerCamelCase , revision="3dc6de3" , ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = "What is the invoice number?" SCREAMING_SNAKE_CASE = dqa_pipeline(image=__lowerCamelCase , question=__lowerCamelCase , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.4_251, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_819, "answer": "1110212019", "start": 23, "end": 23}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline({"image": image, "question": question} , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.4_251, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_819, "answer": "1110212019", "start": 23, "end": 23}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline( [{"image": image, "question": question}, {"image": image, "question": question}] , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ [ {"score": 0.4_251, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_819, "answer": "1110212019", "start": 23, "end": 23}, ] ] * 2 , ) SCREAMING_SNAKE_CASE = list(zip(*apply_tesseract(load_image(__lowerCamelCase ) , __lowerCamelCase , "" ) ) ) # This model should also work if `image` is set to None SCREAMING_SNAKE_CASE = dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question} , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.4_251, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_819, "answer": "1110212019", "start": 23, "end": 23}, ] , ) @slow @require_torch @require_pytesseract @require_vision def _snake_case ( self : Dict ): SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained( "impira/layoutlm-document-qa" , revision="3dc6de3" , add_prefix_space=__lowerCamelCase ) SCREAMING_SNAKE_CASE = pipeline( "document-question-answering" , model="impira/layoutlm-document-qa" , tokenizer=__lowerCamelCase , revision="3dc6de3" , max_seq_len=50 , ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = "What is the invoice number?" SCREAMING_SNAKE_CASE = dqa_pipeline(image=__lowerCamelCase , question=__lowerCamelCase , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.9_999, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.9_998, "answer": "us-001", "start": 16, "end": 16}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline( [{"image": image, "question": question}, {"image": image, "question": question}] , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ [ {"score": 0.9_999, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.9_998, "answer": "us-001", "start": 16, "end": 16}, ] ] * 2 , ) SCREAMING_SNAKE_CASE = list(zip(*apply_tesseract(load_image(__lowerCamelCase ) , __lowerCamelCase , "" ) ) ) # This model should also work if `image` is set to None SCREAMING_SNAKE_CASE = dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question} , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.9_999, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.9_998, "answer": "us-001", "start": 16, "end": 16}, ] , ) @slow @require_torch def _snake_case ( self : List[str] ): SCREAMING_SNAKE_CASE = pipeline( "document-question-answering" , model="naver-clova-ix/donut-base-finetuned-docvqa" , tokenizer=AutoTokenizer.from_pretrained("naver-clova-ix/donut-base-finetuned-docvqa" ) , feature_extractor="naver-clova-ix/donut-base-finetuned-docvqa" , ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = "What is the invoice number?" SCREAMING_SNAKE_CASE = dqa_pipeline(image=__lowerCamelCase , question=__lowerCamelCase , top_k=2 ) self.assertEqual(nested_simplify(__lowerCamelCase , decimals=4 ) , [{"answer": "us-001"}] ) @require_tf @unittest.skip("Document question answering not implemented in TF" ) def _snake_case ( self : List[Any] ): pass
698
0
import unittest from pathlib import Path from tempfile import NamedTemporaryFile, TemporaryDirectory from transformers import BertConfig, BertTokenizerFast, FeatureExtractionPipeline from transformers.convert_graph_to_onnx import ( convert, ensure_valid_input, generate_identified_filename, infer_shapes, quantize, ) from transformers.testing_utils import require_tf, require_tokenizers, require_torch, slow class _SCREAMING_SNAKE_CASE : '''simple docstring''' def _snake_case ( self : Tuple , __lowerCamelCase : Optional[int] , __lowerCamelCase : Optional[Any] , __lowerCamelCase : Any ): return None class _SCREAMING_SNAKE_CASE : '''simple docstring''' def _snake_case ( self : Union[str, Any] , __lowerCamelCase : Dict , __lowerCamelCase : Dict , __lowerCamelCase : Any , __lowerCamelCase : int ): return None class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = [ # (model_name, model_kwargs) ("bert-base-cased", {}), ("gpt2", {"use_cache": False}), # We don't support exporting GPT2 past keys anymore ] @require_tf @slow def _snake_case ( self : List[Any] ): for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST: self._test_export(__lowerCamelCase , "tf" , 12 , **__lowerCamelCase ) @require_torch @slow def _snake_case ( self : List[Any] ): for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST: self._test_export(__lowerCamelCase , "pt" , 12 , **__lowerCamelCase ) @require_torch @slow def _snake_case ( self : Optional[int] ): from transformers import BertModel SCREAMING_SNAKE_CASE = ["[UNK]", "[SEP]", "[CLS]", "[PAD]", "[MASK]", "some", "other", "words"] with NamedTemporaryFile(mode="w+t" ) as vocab_file: vocab_file.write("\n".join(__lowerCamelCase ) ) vocab_file.flush() SCREAMING_SNAKE_CASE = BertTokenizerFast(vocab_file.name ) with TemporaryDirectory() as bert_save_dir: SCREAMING_SNAKE_CASE = BertModel(BertConfig(vocab_size=len(__lowerCamelCase ) ) ) model.save_pretrained(__lowerCamelCase ) self._test_export(__lowerCamelCase , "pt" , 12 , __lowerCamelCase ) @require_tf @slow def _snake_case ( self : Any ): for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST: SCREAMING_SNAKE_CASE = self._test_export(__lowerCamelCase , "tf" , 12 , **__lowerCamelCase ) SCREAMING_SNAKE_CASE = quantize(Path(__lowerCamelCase ) ) # Ensure the actual quantized model is not bigger than the original one if quantized_path.stat().st_size >= Path(__lowerCamelCase ).stat().st_size: self.fail("Quantized model is bigger than initial ONNX model" ) @require_torch @slow def _snake_case ( self : Any ): for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST: SCREAMING_SNAKE_CASE = self._test_export(__lowerCamelCase , "pt" , 12 , **__lowerCamelCase ) SCREAMING_SNAKE_CASE = quantize(__lowerCamelCase ) # Ensure the actual quantized model is not bigger than the original one if quantized_path.stat().st_size >= Path(__lowerCamelCase ).stat().st_size: self.fail("Quantized model is bigger than initial ONNX model" ) def _snake_case ( self : Dict , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : Dict , __lowerCamelCase : int , __lowerCamelCase : Tuple=None , **__lowerCamelCase : List[str] ): try: # Compute path with TemporaryDirectory() as tempdir: SCREAMING_SNAKE_CASE = Path(__lowerCamelCase ).joinpath("model.onnx" ) # Remove folder if exists if path.parent.exists(): path.parent.rmdir() # Export convert(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , **__lowerCamelCase ) return path except Exception as e: self.fail(__lowerCamelCase ) @require_torch @require_tokenizers @slow def _snake_case ( self : Dict ): from transformers import BertModel SCREAMING_SNAKE_CASE = BertModel(BertConfig.from_pretrained("lysandre/tiny-bert-random" ) ) SCREAMING_SNAKE_CASE = BertTokenizerFast.from_pretrained("lysandre/tiny-bert-random" ) self._test_infer_dynamic_axis(__lowerCamelCase , __lowerCamelCase , "pt" ) @require_tf @require_tokenizers @slow def _snake_case ( self : int ): from transformers import TFBertModel SCREAMING_SNAKE_CASE = TFBertModel(BertConfig.from_pretrained("lysandre/tiny-bert-random" ) ) SCREAMING_SNAKE_CASE = BertTokenizerFast.from_pretrained("lysandre/tiny-bert-random" ) self._test_infer_dynamic_axis(__lowerCamelCase , __lowerCamelCase , "tf" ) def _snake_case ( self : int , __lowerCamelCase : Dict , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : str ): SCREAMING_SNAKE_CASE = FeatureExtractionPipeline(__lowerCamelCase , __lowerCamelCase ) SCREAMING_SNAKE_CASE = ["input_ids", "token_type_ids", "attention_mask", "output_0", "output_1"] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = infer_shapes(__lowerCamelCase , __lowerCamelCase ) # Assert all variables are present self.assertEqual(len(__lowerCamelCase ) , len(__lowerCamelCase ) ) self.assertTrue(all(var_name in shapes for var_name in variable_names ) ) self.assertSequenceEqual(variable_names[:3] , __lowerCamelCase ) self.assertSequenceEqual(variable_names[3:] , __lowerCamelCase ) # Assert inputs are {0: batch, 1: sequence} for var_name in ["input_ids", "token_type_ids", "attention_mask"]: self.assertDictEqual(shapes[var_name] , {0: "batch", 1: "sequence"} ) # Assert outputs are {0: batch, 1: sequence} and {0: batch} self.assertDictEqual(shapes["output_0"] , {0: "batch", 1: "sequence"} ) self.assertDictEqual(shapes["output_1"] , {0: "batch"} ) def _snake_case ( self : Optional[Any] ): SCREAMING_SNAKE_CASE = ["input_ids", "attention_mask", "token_type_ids"] SCREAMING_SNAKE_CASE = {"input_ids": [1, 2, 3, 4], "attention_mask": [0, 0, 0, 0], "token_type_ids": [1, 1, 1, 1]} SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = ensure_valid_input(FuncContiguousArgs() , __lowerCamelCase , __lowerCamelCase ) # Should have exactly the same number of args (all are valid) self.assertEqual(len(__lowerCamelCase ) , 3 ) # Should have exactly the same input names self.assertEqual(set(__lowerCamelCase ) , set(__lowerCamelCase ) ) # Parameter should be reordered according to their respective place in the function: # (input_ids, token_type_ids, attention_mask) self.assertEqual(__lowerCamelCase , (tokens["input_ids"], tokens["token_type_ids"], tokens["attention_mask"]) ) # Generated args are interleaved with another args (for instance parameter "past" in GPT2) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = ensure_valid_input(FuncNonContiguousArgs() , __lowerCamelCase , __lowerCamelCase ) # Should have exactly the one arg (all before the one not provided "some_other_args") self.assertEqual(len(__lowerCamelCase ) , 1 ) self.assertEqual(len(__lowerCamelCase ) , 1 ) # Should have only "input_ids" self.assertEqual(inputs_args[0] , tokens["input_ids"] ) self.assertEqual(ordered_input_names[0] , "input_ids" ) def _snake_case ( self : Optional[int] ): SCREAMING_SNAKE_CASE = generate_identified_filename(Path("/home/something/my_fake_model.onnx" ) , "-test" ) self.assertEqual("/home/something/my_fake_model-test.onnx" , generated.as_posix() )
720
# 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 torch from ..models.auto import AutoModelForSequenceClassification, AutoTokenizer from .base import PipelineTool class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' lowerCamelCase__ = "facebook/bart-large-mnli" lowerCamelCase__ = ( "This is a tool that classifies an English text using provided labels. It takes two inputs: `text`, which " "should be the text to classify, and `labels`, which should be the list of labels to use for classification. " "It returns the most likely label in the list of provided `labels` for the input text." ) lowerCamelCase__ = "text_classifier" lowerCamelCase__ = AutoTokenizer lowerCamelCase__ = AutoModelForSequenceClassification lowerCamelCase__ = ["text", ["text"]] lowerCamelCase__ = ["text"] def _snake_case ( self : Optional[Any] ): super().setup() SCREAMING_SNAKE_CASE = self.model.config SCREAMING_SNAKE_CASE = -1 for idx, label in config.idalabel.items(): if label.lower().startswith("entail" ): SCREAMING_SNAKE_CASE = int(__lowerCamelCase ) if self.entailment_id == -1: raise ValueError("Could not determine the entailment ID from the model config, please pass it at init." ) def _snake_case ( self : List[Any] , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : List[Any] ): SCREAMING_SNAKE_CASE = labels return self.pre_processor( [text] * len(__lowerCamelCase ) , [f"This example is {label}" for label in labels] , return_tensors="pt" , padding="max_length" , ) def _snake_case ( self : str , __lowerCamelCase : Optional[int] ): SCREAMING_SNAKE_CASE = outputs.logits SCREAMING_SNAKE_CASE = torch.argmax(logits[:, 2] ).item() return self._labels[label_id]
698
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) __A : Union[str, Any] = {'configuration_fnet': ['FNET_PRETRAINED_CONFIG_ARCHIVE_MAP', 'FNetConfig']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[str] = ['FNetTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Any = ['FNetTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Union[str, Any] = [ 'FNET_PRETRAINED_MODEL_ARCHIVE_LIST', 'FNetForMaskedLM', 'FNetForMultipleChoice', 'FNetForNextSentencePrediction', 'FNetForPreTraining', 'FNetForQuestionAnswering', 'FNetForSequenceClassification', 'FNetForTokenClassification', 'FNetLayer', 'FNetModel', 'FNetPreTrainedModel', ] if TYPE_CHECKING: from .configuration_fnet import FNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FNetConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_fnet import FNetTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_fnet_fast import FNetTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_fnet import ( FNET_PRETRAINED_MODEL_ARCHIVE_LIST, FNetForMaskedLM, FNetForMultipleChoice, FNetForNextSentencePrediction, FNetForPreTraining, FNetForQuestionAnswering, FNetForSequenceClassification, FNetForTokenClassification, FNetLayer, FNetModel, FNetPreTrainedModel, ) else: import sys __A : Dict = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
721
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os from accelerate.test_utils import execute_subprocess_async def __a ( A__ : str=None ): if subparsers is not None: SCREAMING_SNAKE_CASE = subparsers.add_parser("test" ) else: SCREAMING_SNAKE_CASE = argparse.ArgumentParser("Accelerate test command" ) parser.add_argument( "--config_file" , default=A__ , help=( "The path to use to store the config file. Will default to a file named default_config.yaml in the cache " "location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have " "such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed " "with 'huggingface'." ) , ) if subparsers is not None: parser.set_defaults(func=A__ ) return parser def __a ( A__ : Tuple ): SCREAMING_SNAKE_CASE = os.path.sep.join(__file__.split(os.path.sep )[:-2] + ["test_utils", "scripts", "test_script.py"] ) if args.config_file is None: SCREAMING_SNAKE_CASE = script_name else: SCREAMING_SNAKE_CASE = F"--config_file={args.config_file} {script_name}" SCREAMING_SNAKE_CASE = ["accelerate-launch"] + test_args.split() SCREAMING_SNAKE_CASE = execute_subprocess_async(A__ , env=os.environ.copy() ) if result.returncode == 0: print("Test is a success! You are ready for your distributed training!" ) def __a ( ): SCREAMING_SNAKE_CASE = test_command_parser() SCREAMING_SNAKE_CASE = parser.parse_args() test_command(A__ ) if __name__ == "__main__": main()
698
0
from ...configuration_utils import PretrainedConfig from ...utils import logging __A : int = logging.get_logger(__name__) __A : int = { 'studio-ousia/luke-base': 'https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json', 'studio-ousia/luke-large': 'https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json', } class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' lowerCamelCase__ = "luke" def __init__( self : Any , __lowerCamelCase : Union[str, Any]=50267 , __lowerCamelCase : List[Any]=500000 , __lowerCamelCase : Union[str, Any]=768 , __lowerCamelCase : Optional[Any]=256 , __lowerCamelCase : Any=12 , __lowerCamelCase : Dict=12 , __lowerCamelCase : int=3072 , __lowerCamelCase : str="gelu" , __lowerCamelCase : Any=0.1 , __lowerCamelCase : Any=0.1 , __lowerCamelCase : Union[str, Any]=512 , __lowerCamelCase : Any=2 , __lowerCamelCase : Union[str, Any]=0.02 , __lowerCamelCase : str=1e-12 , __lowerCamelCase : Optional[int]=True , __lowerCamelCase : Optional[int]=None , __lowerCamelCase : str=1 , __lowerCamelCase : Union[str, Any]=0 , __lowerCamelCase : int=2 , **__lowerCamelCase : Tuple , ): super().__init__(pad_token_id=__lowerCamelCase , bos_token_id=__lowerCamelCase , eos_token_id=__lowerCamelCase , **__lowerCamelCase ) SCREAMING_SNAKE_CASE = vocab_size SCREAMING_SNAKE_CASE = entity_vocab_size SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = entity_emb_size SCREAMING_SNAKE_CASE = num_hidden_layers SCREAMING_SNAKE_CASE = num_attention_heads SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = intermediate_size SCREAMING_SNAKE_CASE = hidden_dropout_prob SCREAMING_SNAKE_CASE = attention_probs_dropout_prob SCREAMING_SNAKE_CASE = max_position_embeddings SCREAMING_SNAKE_CASE = type_vocab_size SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = layer_norm_eps SCREAMING_SNAKE_CASE = use_entity_aware_attention SCREAMING_SNAKE_CASE = classifier_dropout
700
import json import os from functools import lru_cache from typing import TYPE_CHECKING, List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation __A : Optional[int] = logging.get_logger(__name__) __A : List[str] = { 'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_config_file': 'tokenizer_config.json', } __A : Tuple = { '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' }, } __A : Dict = {'facebook/blenderbot-3B': 1_2_8} @lru_cache() # Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode def __a ( ): SCREAMING_SNAKE_CASE = ( list(range(ord("!" ) , ord("~" ) + 1 ) ) + list(range(ord("¡" ) , ord("¬" ) + 1 ) ) + list(range(ord("®" ) , ord("ÿ" ) + 1 ) ) ) SCREAMING_SNAKE_CASE = bs[:] SCREAMING_SNAKE_CASE = 0 for b in range(2**8 ): if b not in bs: bs.append(A__ ) cs.append(2**8 + n ) n += 1 SCREAMING_SNAKE_CASE = [chr(A__ ) for n in cs] return dict(zip(A__ , A__ ) ) def __a ( A__ : Optional[int] ): SCREAMING_SNAKE_CASE = set() SCREAMING_SNAKE_CASE = word[0] for char in word[1:]: pairs.add((prev_char, char) ) SCREAMING_SNAKE_CASE = char return pairs class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' lowerCamelCase__ = VOCAB_FILES_NAMES lowerCamelCase__ = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase__ = ["input_ids", "attention_mask"] def __init__( self : int , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : str , __lowerCamelCase : List[Any]="replace" , __lowerCamelCase : Any="<s>" , __lowerCamelCase : List[Any]="</s>" , __lowerCamelCase : Dict="</s>" , __lowerCamelCase : Any="<s>" , __lowerCamelCase : Optional[Any]="<unk>" , __lowerCamelCase : str="<pad>" , __lowerCamelCase : List[Any]="<mask>" , __lowerCamelCase : List[Any]=False , **__lowerCamelCase : Tuple , ): SCREAMING_SNAKE_CASE = AddedToken(__lowerCamelCase , lstrip=__lowerCamelCase , rstrip=__lowerCamelCase ) if isinstance(__lowerCamelCase , __lowerCamelCase ) else bos_token SCREAMING_SNAKE_CASE = AddedToken(__lowerCamelCase , lstrip=__lowerCamelCase , rstrip=__lowerCamelCase ) if isinstance(__lowerCamelCase , __lowerCamelCase ) else eos_token SCREAMING_SNAKE_CASE = AddedToken(__lowerCamelCase , lstrip=__lowerCamelCase , rstrip=__lowerCamelCase ) if isinstance(__lowerCamelCase , __lowerCamelCase ) else sep_token SCREAMING_SNAKE_CASE = AddedToken(__lowerCamelCase , lstrip=__lowerCamelCase , rstrip=__lowerCamelCase ) if isinstance(__lowerCamelCase , __lowerCamelCase ) else cls_token SCREAMING_SNAKE_CASE = AddedToken(__lowerCamelCase , lstrip=__lowerCamelCase , rstrip=__lowerCamelCase ) if isinstance(__lowerCamelCase , __lowerCamelCase ) else unk_token SCREAMING_SNAKE_CASE = AddedToken(__lowerCamelCase , lstrip=__lowerCamelCase , rstrip=__lowerCamelCase ) if isinstance(__lowerCamelCase , __lowerCamelCase ) else pad_token # Mask token behave like a normal word, i.e. include the space before it SCREAMING_SNAKE_CASE = AddedToken(__lowerCamelCase , lstrip=__lowerCamelCase , rstrip=__lowerCamelCase ) if isinstance(__lowerCamelCase , __lowerCamelCase ) else mask_token super().__init__( errors=__lowerCamelCase , bos_token=__lowerCamelCase , eos_token=__lowerCamelCase , unk_token=__lowerCamelCase , sep_token=__lowerCamelCase , cls_token=__lowerCamelCase , pad_token=__lowerCamelCase , mask_token=__lowerCamelCase , add_prefix_space=__lowerCamelCase , **__lowerCamelCase , ) with open(__lowerCamelCase , encoding="utf-8" ) as vocab_handle: SCREAMING_SNAKE_CASE = json.load(__lowerCamelCase ) SCREAMING_SNAKE_CASE = {v: k for k, v in self.encoder.items()} SCREAMING_SNAKE_CASE = errors # how to handle errors in decoding SCREAMING_SNAKE_CASE = bytes_to_unicode() SCREAMING_SNAKE_CASE = {v: k for k, v in self.byte_encoder.items()} with open(__lowerCamelCase , encoding="utf-8" ) as merges_handle: SCREAMING_SNAKE_CASE = merges_handle.read().split("\n" )[1:-1] SCREAMING_SNAKE_CASE = [tuple(merge.split() ) for merge in bpe_merges] SCREAMING_SNAKE_CASE = dict(zip(__lowerCamelCase , range(len(__lowerCamelCase ) ) ) ) SCREAMING_SNAKE_CASE = {} SCREAMING_SNAKE_CASE = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions SCREAMING_SNAKE_CASE = re.compile(r"'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+" ) @property # Copied from transformers.models.roberta.tokenization_roberta.RobertaTokenizer.vocab_size with Roberta->Blenderbot, RoBERTa->Blenderbot def _snake_case ( self : Dict ): return len(self.encoder ) def _snake_case ( self : int ): return dict(self.encoder , **self.added_tokens_encoder ) def _snake_case ( self : Tuple , __lowerCamelCase : List[str] ): if token in self.cache: return self.cache[token] SCREAMING_SNAKE_CASE = tuple(__lowerCamelCase ) SCREAMING_SNAKE_CASE = get_pairs(__lowerCamelCase ) if not pairs: return token while True: SCREAMING_SNAKE_CASE = min(__lowerCamelCase , key=lambda __lowerCamelCase : self.bpe_ranks.get(__lowerCamelCase , float("inf" ) ) ) if bigram not in self.bpe_ranks: break SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = bigram SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = 0 while i < len(__lowerCamelCase ): try: SCREAMING_SNAKE_CASE = word.index(__lowerCamelCase , __lowerCamelCase ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) SCREAMING_SNAKE_CASE = j if word[i] == first and i < len(__lowerCamelCase ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 SCREAMING_SNAKE_CASE = tuple(__lowerCamelCase ) SCREAMING_SNAKE_CASE = new_word if len(__lowerCamelCase ) == 1: break else: SCREAMING_SNAKE_CASE = get_pairs(__lowerCamelCase ) SCREAMING_SNAKE_CASE = " ".join(__lowerCamelCase ) SCREAMING_SNAKE_CASE = word return word def _snake_case ( self : str , __lowerCamelCase : Dict ): SCREAMING_SNAKE_CASE = [] for token in re.findall(self.pat , __lowerCamelCase ): SCREAMING_SNAKE_CASE = "".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(__lowerCamelCase ).split(" " ) ) return bpe_tokens def _snake_case ( self : List[str] , __lowerCamelCase : str ): return self.encoder.get(__lowerCamelCase , self.encoder.get(self.unk_token ) ) def _snake_case ( self : Optional[int] , __lowerCamelCase : Tuple ): return self.decoder.get(__lowerCamelCase ) def _snake_case ( self : Tuple , __lowerCamelCase : str ): SCREAMING_SNAKE_CASE = "".join(__lowerCamelCase ) SCREAMING_SNAKE_CASE = bytearray([self.byte_decoder[c] for c in text] ).decode("utf-8" , errors=self.errors ) return text def _snake_case ( self : Union[str, Any] , __lowerCamelCase : str , __lowerCamelCase : Optional[str] = None ): if not os.path.isdir(__lowerCamelCase ): logger.error(f"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE = os.path.join( __lowerCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) SCREAMING_SNAKE_CASE = os.path.join( __lowerCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"] ) with open(__lowerCamelCase , "w" , encoding="utf-8" ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=__lowerCamelCase , ensure_ascii=__lowerCamelCase ) + "\n" ) SCREAMING_SNAKE_CASE = 0 with open(__lowerCamelCase , "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 __lowerCamelCase : 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!" ) SCREAMING_SNAKE_CASE = token_index writer.write(" ".join(__lowerCamelCase ) + "\n" ) index += 1 return vocab_file, merge_file def _snake_case ( self : Tuple , __lowerCamelCase : List[int] , __lowerCamelCase : Optional[List[int]] = None , __lowerCamelCase : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__lowerCamelCase , token_ids_a=__lowerCamelCase , already_has_special_tokens=__lowerCamelCase ) if token_ids_a is None: return [1] + ([0] * len(__lowerCamelCase )) + [1] return [1] + ([0] * len(__lowerCamelCase )) + [1, 1] + ([0] * len(__lowerCamelCase )) + [1] def _snake_case ( self : str , __lowerCamelCase : List[int] , __lowerCamelCase : Optional[List[int]] = None ): SCREAMING_SNAKE_CASE = [self.sep_token_id] SCREAMING_SNAKE_CASE = [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 : Any , __lowerCamelCase : Optional[Any] , __lowerCamelCase : Dict=False , **__lowerCamelCase : Optional[int] ): SCREAMING_SNAKE_CASE = kwargs.pop("add_prefix_space" , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(__lowerCamelCase ) > 0 and not text[0].isspace()): SCREAMING_SNAKE_CASE = " " + text return (text, kwargs) def _snake_case ( self : Dict , __lowerCamelCase : List[int] , __lowerCamelCase : Optional[List[int]] = None ): return token_ids_a + [self.eos_token_id] def _snake_case ( self : Tuple , __lowerCamelCase : "Conversation" ): SCREAMING_SNAKE_CASE = [] 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(__lowerCamelCase ) SCREAMING_SNAKE_CASE = " ".join(__lowerCamelCase ) SCREAMING_SNAKE_CASE = self.encode(__lowerCamelCase ) if len(__lowerCamelCase ) > self.model_max_length: SCREAMING_SNAKE_CASE = 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
698
0
from __future__ import annotations import math from collections.abc import Callable def __a ( A__ : Callable[[int | float], int | float] , A__ : int | float , A__ : int | float , A__ : int = 100 , ): SCREAMING_SNAKE_CASE = x_start SCREAMING_SNAKE_CASE = fnc(A__ ) SCREAMING_SNAKE_CASE = 0.0 for _ in range(A__ ): # Approximates curve as a sequence of linear lines and sums their length SCREAMING_SNAKE_CASE = (x_end - x_start) / steps + xa SCREAMING_SNAKE_CASE = fnc(A__ ) length += math.hypot(xa - xa , fxa - fxa ) # Increment step SCREAMING_SNAKE_CASE = xa SCREAMING_SNAKE_CASE = fxa return length if __name__ == "__main__": def __a ( A__ : Optional[Any] ): return math.sin(10 * x ) print('f(x) = sin(10 * x)') print('The length of the curve from x = -10 to x = 10 is:') __A : Optional[int] = 1_0 while i <= 1_0_0_0_0_0: print(f'With {i} steps: {line_length(f, -1_0, 1_0, i)}') i *= 1_0
701
from __future__ import annotations from cmath import sqrt def __a ( A__ : int , A__ : int , A__ : int ): if a == 0: raise ValueError("Coefficient 'a' must not be zero." ) SCREAMING_SNAKE_CASE = b * b - 4 * a * c SCREAMING_SNAKE_CASE = (-b + sqrt(A__ )) / (2 * a) SCREAMING_SNAKE_CASE = (-b - sqrt(A__ )) / (2 * a) return ( root_a.real if not root_a.imag else root_a, root_a.real if not root_a.imag else root_a, ) def __a ( ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = quadratic_roots(a=5 , b=6 , c=1 ) print(F"The solutions are: {solutiona} and {solutiona}" ) if __name__ == "__main__": main()
698
0
__A : dict[str, float] = { "joule": 1.0, "kilojoule": 1_0_0_0, "megajoule": 1_0_0_0_0_0_0, "gigajoule": 1_0_0_0_0_0_0_0_0_0, "wattsecond": 1.0, "watthour": 3_6_0_0, "kilowatthour": 3_6_0_0_0_0_0, "newtonmeter": 1.0, "calorie_nutr": 4_1_8_6.8, "kilocalorie_nutr": 4_1_8_6_8_0_0.0_0, "electronvolt": 1.6_0217_6634e-19, "britishthermalunit_it": 1_0_5_5.0_5_5_8_5, "footpound": 1.355_818, } def __a ( A__ : str , A__ : str , A__ : float ): if to_type not in ENERGY_CONVERSION or from_type not in ENERGY_CONVERSION: SCREAMING_SNAKE_CASE = ( F"Incorrect 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\n" F"Valid values are: {', '.join(A__ )}" ) raise ValueError(A__ ) return value * ENERGY_CONVERSION[from_type] / ENERGY_CONVERSION[to_type] if __name__ == "__main__": import doctest doctest.testmod()
702
import unittest from queue import Empty from threading import Thread from transformers import AutoTokenizer, TextIteratorStreamer, TextStreamer, is_torch_available from transformers.testing_utils import CaptureStdout, require_torch, torch_device from ..test_modeling_common import ids_tensor if is_torch_available(): import torch from transformers import AutoModelForCausalLM @require_torch class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' def _snake_case ( self : Dict ): SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2" ) SCREAMING_SNAKE_CASE = AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2" ).to(__lowerCamelCase ) SCREAMING_SNAKE_CASE = -1 SCREAMING_SNAKE_CASE = ids_tensor((1, 5) , vocab_size=model.config.vocab_size ).to(__lowerCamelCase ) SCREAMING_SNAKE_CASE = model.generate(__lowerCamelCase , max_new_tokens=10 , do_sample=__lowerCamelCase ) SCREAMING_SNAKE_CASE = tokenizer.decode(greedy_ids[0] ) with CaptureStdout() as cs: SCREAMING_SNAKE_CASE = TextStreamer(__lowerCamelCase ) model.generate(__lowerCamelCase , max_new_tokens=10 , do_sample=__lowerCamelCase , streamer=__lowerCamelCase ) # The greedy text should be printed to stdout, except for the final "\n" in the streamer SCREAMING_SNAKE_CASE = cs.out[:-1] self.assertEqual(__lowerCamelCase , __lowerCamelCase ) def _snake_case ( self : int ): SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2" ) SCREAMING_SNAKE_CASE = AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2" ).to(__lowerCamelCase ) SCREAMING_SNAKE_CASE = -1 SCREAMING_SNAKE_CASE = ids_tensor((1, 5) , vocab_size=model.config.vocab_size ).to(__lowerCamelCase ) SCREAMING_SNAKE_CASE = model.generate(__lowerCamelCase , max_new_tokens=10 , do_sample=__lowerCamelCase ) SCREAMING_SNAKE_CASE = tokenizer.decode(greedy_ids[0] ) SCREAMING_SNAKE_CASE = TextIteratorStreamer(__lowerCamelCase ) SCREAMING_SNAKE_CASE = {"input_ids": input_ids, "max_new_tokens": 10, "do_sample": False, "streamer": streamer} SCREAMING_SNAKE_CASE = Thread(target=model.generate , kwargs=__lowerCamelCase ) thread.start() SCREAMING_SNAKE_CASE = "" for new_text in streamer: streamer_text += new_text self.assertEqual(__lowerCamelCase , __lowerCamelCase ) def _snake_case ( self : List[str] ): SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2" ) SCREAMING_SNAKE_CASE = AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2" ).to(__lowerCamelCase ) SCREAMING_SNAKE_CASE = -1 SCREAMING_SNAKE_CASE = ids_tensor((1, 5) , vocab_size=model.config.vocab_size ).to(__lowerCamelCase ) SCREAMING_SNAKE_CASE = model.generate(__lowerCamelCase , max_new_tokens=10 , do_sample=__lowerCamelCase ) SCREAMING_SNAKE_CASE = greedy_ids[:, input_ids.shape[1] :] SCREAMING_SNAKE_CASE = tokenizer.decode(new_greedy_ids[0] ) with CaptureStdout() as cs: SCREAMING_SNAKE_CASE = TextStreamer(__lowerCamelCase , skip_prompt=__lowerCamelCase ) model.generate(__lowerCamelCase , max_new_tokens=10 , do_sample=__lowerCamelCase , streamer=__lowerCamelCase ) # The greedy text should be printed to stdout, except for the final "\n" in the streamer SCREAMING_SNAKE_CASE = cs.out[:-1] self.assertEqual(__lowerCamelCase , __lowerCamelCase ) def _snake_case ( self : int ): # Tests that we can pass `decode_kwargs` to the streamer to control how the tokens are decoded. Must be tested # with actual models -- the dummy models' tokenizers are not aligned with their models, and # `skip_special_tokens=True` has no effect on them SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("distilgpt2" ) SCREAMING_SNAKE_CASE = AutoModelForCausalLM.from_pretrained("distilgpt2" ).to(__lowerCamelCase ) SCREAMING_SNAKE_CASE = -1 SCREAMING_SNAKE_CASE = torch.ones((1, 5) , device=__lowerCamelCase ).long() * model.config.bos_token_id with CaptureStdout() as cs: SCREAMING_SNAKE_CASE = TextStreamer(__lowerCamelCase , skip_special_tokens=__lowerCamelCase ) model.generate(__lowerCamelCase , max_new_tokens=1 , do_sample=__lowerCamelCase , streamer=__lowerCamelCase ) # The prompt contains a special token, so the streamer should not print it. As such, the output text, when # re-tokenized, must only contain one token SCREAMING_SNAKE_CASE = cs.out[:-1] # Remove the final "\n" SCREAMING_SNAKE_CASE = tokenizer(__lowerCamelCase , return_tensors="pt" ) self.assertEqual(streamer_text_tokenized.input_ids.shape , (1, 1) ) def _snake_case ( self : Optional[int] ): SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2" ) SCREAMING_SNAKE_CASE = AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2" ).to(__lowerCamelCase ) SCREAMING_SNAKE_CASE = -1 SCREAMING_SNAKE_CASE = ids_tensor((1, 5) , vocab_size=model.config.vocab_size ).to(__lowerCamelCase ) SCREAMING_SNAKE_CASE = TextIteratorStreamer(__lowerCamelCase , timeout=0.001 ) SCREAMING_SNAKE_CASE = {"input_ids": input_ids, "max_new_tokens": 10, "do_sample": False, "streamer": streamer} SCREAMING_SNAKE_CASE = Thread(target=model.generate , kwargs=__lowerCamelCase ) thread.start() # The streamer will timeout after 0.001 seconds, so an exception will be raised with self.assertRaises(__lowerCamelCase ): SCREAMING_SNAKE_CASE = "" for new_text in streamer: streamer_text += new_text
698
0
import inspect import os import sys import unittest import accelerate from accelerate.test_utils import execute_subprocess_async, require_tpu class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' def _snake_case ( self : List[Any] ): SCREAMING_SNAKE_CASE = inspect.getfile(accelerate.test_utils ) SCREAMING_SNAKE_CASE = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["scripts", "test_script.py"] ) SCREAMING_SNAKE_CASE = os.path.sep.join(inspect.getfile(self.__class__ ).split(os.path.sep )[:-1] ) @require_tpu def _snake_case ( self : List[str] ): SCREAMING_SNAKE_CASE = f"\n {self.test_dir}/xla_spawn.py\n --num_cores 8\n {self.test_file_path}\n ".split() SCREAMING_SNAKE_CASE = [sys.executable] + distributed_args execute_subprocess_async(__lowerCamelCase , env=os.environ.copy() )
703
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) __A : Union[str, Any] = {'configuration_fnet': ['FNET_PRETRAINED_CONFIG_ARCHIVE_MAP', 'FNetConfig']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[str] = ['FNetTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Any = ['FNetTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Union[str, Any] = [ 'FNET_PRETRAINED_MODEL_ARCHIVE_LIST', 'FNetForMaskedLM', 'FNetForMultipleChoice', 'FNetForNextSentencePrediction', 'FNetForPreTraining', 'FNetForQuestionAnswering', 'FNetForSequenceClassification', 'FNetForTokenClassification', 'FNetLayer', 'FNetModel', 'FNetPreTrainedModel', ] if TYPE_CHECKING: from .configuration_fnet import FNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FNetConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_fnet import FNetTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_fnet_fast import FNetTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_fnet import ( FNET_PRETRAINED_MODEL_ARCHIVE_LIST, FNetForMaskedLM, FNetForMultipleChoice, FNetForNextSentencePrediction, FNetForPreTraining, FNetForQuestionAnswering, FNetForSequenceClassification, FNetForTokenClassification, FNetLayer, FNetModel, FNetPreTrainedModel, ) else: import sys __A : Dict = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
698
0
def __a ( A__ : int ): SCREAMING_SNAKE_CASE = n ** (1 / 3) return (val * val * val) == n if __name__ == "__main__": print(perfect_cube(2_7)) print(perfect_cube(4))
704
from collections import Counter import numpy as np from sklearn import datasets from sklearn.model_selection import train_test_split __A : Optional[Any] = datasets.load_iris() __A : Optional[Any] = np.array(data['data']) __A : Optional[int] = np.array(data['target']) __A : Union[str, Any] = data['target_names'] __A , __A , __A , __A : Optional[int] = train_test_split(X, y) def __a ( A__ : Optional[int] , A__ : Dict ): return np.linalg.norm(np.array(A__ ) - np.array(A__ ) ) def __a ( A__ : Optional[Any] , A__ : int , A__ : Dict , A__ : Optional[Any] , A__ : Dict=5 ): SCREAMING_SNAKE_CASE = zip(A__ , A__ ) # List of distances of all points from the point to be classified SCREAMING_SNAKE_CASE = [] for data_point in data: SCREAMING_SNAKE_CASE = euclidean_distance(data_point[0] , A__ ) distances.append((distance, data_point[1]) ) # Choosing 'k' points with the least distances. SCREAMING_SNAKE_CASE = [i[1] for i in sorted(A__ )[:k]] # Most commonly occurring class among them # is the class into which the point is classified SCREAMING_SNAKE_CASE = Counter(A__ ).most_common(1 )[0][0] return classes[result] if __name__ == "__main__": print(classifier(X_train, y_train, classes, [4.4, 3.1, 1.3, 1.4]))
698
0
import argparse import json from collections import OrderedDict from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import PoolFormerConfig, PoolFormerForImageClassification, PoolFormerImageProcessor from transformers.utils import logging logging.set_verbosity_info() __A : Any = logging.get_logger(__name__) def __a ( A__ : List[Any] , A__ : Tuple , A__ : Optional[int] , A__ : Optional[int] ): SCREAMING_SNAKE_CASE = original_name.split("." )[0] SCREAMING_SNAKE_CASE = key.split("." ) SCREAMING_SNAKE_CASE = int(key_list[key_list.index(A__ ) - 2] ) SCREAMING_SNAKE_CASE = int(key_list[key_list.index(A__ ) - 1] ) SCREAMING_SNAKE_CASE = orig_block_num - offset SCREAMING_SNAKE_CASE = key.replace(F"{orig_block_num}.{layer_num}.{original_name}" , F"block.{new_block_num}.{layer_num}.{new_name}" ) return key def __a ( A__ : Any ): SCREAMING_SNAKE_CASE = OrderedDict() SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = 0, 0 for key, value in state_dict.items(): if key.startswith("network" ): SCREAMING_SNAKE_CASE = key.replace("network" , "poolformer.encoder" ) if "proj" in key: # Works for the first embedding as well as the internal embedding layers if key.endswith("bias" ) and "patch_embed" not in key: patch_emb_offset += 1 SCREAMING_SNAKE_CASE = key[: key.find("proj" )] SCREAMING_SNAKE_CASE = key.replace(A__ , F"patch_embeddings.{total_embed_found}." ) SCREAMING_SNAKE_CASE = key.replace("proj" , "projection" ) if key.endswith("bias" ): total_embed_found += 1 if "patch_embeddings" in key: SCREAMING_SNAKE_CASE = "poolformer.encoder." + key if "mlp.fc1" in key: SCREAMING_SNAKE_CASE = replace_key_with_offset(A__ , A__ , "mlp.fc1" , "output.conv1" ) if "mlp.fc2" in key: SCREAMING_SNAKE_CASE = replace_key_with_offset(A__ , A__ , "mlp.fc2" , "output.conv2" ) if "norm1" in key: SCREAMING_SNAKE_CASE = replace_key_with_offset(A__ , A__ , "norm1" , "before_norm" ) if "norm2" in key: SCREAMING_SNAKE_CASE = replace_key_with_offset(A__ , A__ , "norm2" , "after_norm" ) if "layer_scale_1" in key: SCREAMING_SNAKE_CASE = replace_key_with_offset(A__ , A__ , "layer_scale_1" , "layer_scale_1" ) if "layer_scale_2" in key: SCREAMING_SNAKE_CASE = replace_key_with_offset(A__ , A__ , "layer_scale_2" , "layer_scale_2" ) if "head" in key: SCREAMING_SNAKE_CASE = key.replace("head" , "classifier" ) SCREAMING_SNAKE_CASE = value return new_state_dict def __a ( ): SCREAMING_SNAKE_CASE = "http://images.cocodataset.org/val2017/000000039769.jpg" SCREAMING_SNAKE_CASE = Image.open(requests.get(A__ , stream=A__ ).raw ) return image @torch.no_grad() def __a ( A__ : List[Any] , A__ : int , A__ : int ): SCREAMING_SNAKE_CASE = PoolFormerConfig() # set attributes based on model_name SCREAMING_SNAKE_CASE = "huggingface/label-files" SCREAMING_SNAKE_CASE = model_name[-3:] SCREAMING_SNAKE_CASE = 1000 SCREAMING_SNAKE_CASE = "imagenet-1k-id2label.json" SCREAMING_SNAKE_CASE = (1, 1000) # set config attributes SCREAMING_SNAKE_CASE = json.load(open(hf_hub_download(A__ , A__ , repo_type="dataset" ) , "r" ) ) SCREAMING_SNAKE_CASE = {int(A__ ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE = idalabel SCREAMING_SNAKE_CASE = {v: k for k, v in idalabel.items()} if size == "s12": SCREAMING_SNAKE_CASE = [2, 2, 6, 2] SCREAMING_SNAKE_CASE = [64, 128, 320, 512] SCREAMING_SNAKE_CASE = 4.0 SCREAMING_SNAKE_CASE = 0.9 elif size == "s24": SCREAMING_SNAKE_CASE = [4, 4, 12, 4] SCREAMING_SNAKE_CASE = [64, 128, 320, 512] SCREAMING_SNAKE_CASE = 4.0 SCREAMING_SNAKE_CASE = 0.9 elif size == "s36": SCREAMING_SNAKE_CASE = [6, 6, 18, 6] SCREAMING_SNAKE_CASE = [64, 128, 320, 512] SCREAMING_SNAKE_CASE = 4.0 SCREAMING_SNAKE_CASE = 1E-6 SCREAMING_SNAKE_CASE = 0.9 elif size == "m36": SCREAMING_SNAKE_CASE = [6, 6, 18, 6] SCREAMING_SNAKE_CASE = [96, 192, 384, 768] SCREAMING_SNAKE_CASE = 4.0 SCREAMING_SNAKE_CASE = 1E-6 SCREAMING_SNAKE_CASE = 0.9_5 elif size == "m48": SCREAMING_SNAKE_CASE = [8, 8, 24, 8] SCREAMING_SNAKE_CASE = [96, 192, 384, 768] SCREAMING_SNAKE_CASE = 4.0 SCREAMING_SNAKE_CASE = 1E-6 SCREAMING_SNAKE_CASE = 0.9_5 else: raise ValueError(F"Size {size} not supported" ) # load image processor SCREAMING_SNAKE_CASE = PoolFormerImageProcessor(crop_pct=A__ ) # Prepare image SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=A__ , return_tensors="pt" ).pixel_values logger.info(F"Converting model {model_name}..." ) # load original state dict SCREAMING_SNAKE_CASE = torch.load(A__ , map_location=torch.device("cpu" ) ) # rename keys SCREAMING_SNAKE_CASE = rename_keys(A__ ) # create HuggingFace model and load state dict SCREAMING_SNAKE_CASE = PoolFormerForImageClassification(A__ ) model.load_state_dict(A__ ) model.eval() # Define image processor SCREAMING_SNAKE_CASE = PoolFormerImageProcessor(crop_pct=A__ ) SCREAMING_SNAKE_CASE = image_processor(images=prepare_img() , return_tensors="pt" ).pixel_values # forward pass SCREAMING_SNAKE_CASE = model(A__ ) SCREAMING_SNAKE_CASE = outputs.logits # define expected logit slices for different models if size == "s12": SCREAMING_SNAKE_CASE = torch.tensor([-0.3_0_4_5, -0.6_7_5_8, -0.4_8_6_9] ) elif size == "s24": SCREAMING_SNAKE_CASE = torch.tensor([0.4_4_0_2, -0.1_3_7_4, -0.8_0_4_5] ) elif size == "s36": SCREAMING_SNAKE_CASE = torch.tensor([-0.6_0_8_0, -0.5_1_3_3, -0.5_8_9_8] ) elif size == "m36": SCREAMING_SNAKE_CASE = torch.tensor([0.3_9_5_2, 0.2_2_6_3, -1.2_6_6_8] ) elif size == "m48": SCREAMING_SNAKE_CASE = torch.tensor([0.1_1_6_7, -0.0_6_5_6, -0.3_4_2_3] ) else: raise ValueError(F"Size {size} not supported" ) # verify logits assert logits.shape == expected_shape assert torch.allclose(logits[0, :3] , A__ , atol=1E-2 ) # finally, save model and image processor logger.info(F"Saving PyTorch model and image processor to {pytorch_dump_folder_path}..." ) Path(A__ ).mkdir(exist_ok=A__ ) model.save_pretrained(A__ ) print(F"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(A__ ) if __name__ == "__main__": __A : Any = argparse.ArgumentParser() parser.add_argument( '--model_name', default='poolformer_s12', type=str, help='Name of the model you\'d like to convert.', ) parser.add_argument( '--checkpoint_path', default=None, type=str, help='Path to the original PyTorch checkpoint (.pth file).' ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the folder to output PyTorch model.' ) __A : Dict = parser.parse_args() convert_poolformer_checkpoint(args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path)
705
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices __A : Union[str, Any] = logging.get_logger(__name__) __A : Dict = { 'microsoft/resnet-50': 'https://huggingface.co/microsoft/resnet-50/blob/main/config.json', } class _SCREAMING_SNAKE_CASE ( __snake_case , __snake_case ): '''simple docstring''' lowerCamelCase__ = "resnet" lowerCamelCase__ = ["basic", "bottleneck"] def __init__( self : Optional[Any] , __lowerCamelCase : int=3 , __lowerCamelCase : Dict=64 , __lowerCamelCase : str=[256, 512, 1024, 2048] , __lowerCamelCase : str=[3, 4, 6, 3] , __lowerCamelCase : Optional[int]="bottleneck" , __lowerCamelCase : int="relu" , __lowerCamelCase : List[str]=False , __lowerCamelCase : Optional[int]=None , __lowerCamelCase : int=None , **__lowerCamelCase : Dict , ): super().__init__(**__lowerCamelCase ) if layer_type not in self.layer_types: raise ValueError(f"layer_type={layer_type} is not one of {','.join(self.layer_types )}" ) SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = embedding_size SCREAMING_SNAKE_CASE = hidden_sizes SCREAMING_SNAKE_CASE = depths SCREAMING_SNAKE_CASE = layer_type SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = downsample_in_first_stage SCREAMING_SNAKE_CASE = ["stem"] + [f"stage{idx}" for idx in range(1 , len(__lowerCamelCase ) + 1 )] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = get_aligned_output_features_output_indices( out_features=__lowerCamelCase , out_indices=__lowerCamelCase , stage_names=self.stage_names ) class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' lowerCamelCase__ = version.parse("1.11" ) @property def _snake_case ( self : Optional[int] ): return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def _snake_case ( self : Optional[int] ): return 1e-3
698
0
from itertools import product def __a ( A__ : int , A__ : int ): SCREAMING_SNAKE_CASE = sides_number SCREAMING_SNAKE_CASE = max_face_number * dice_number SCREAMING_SNAKE_CASE = [0] * (max_total + 1) SCREAMING_SNAKE_CASE = 1 SCREAMING_SNAKE_CASE = range(A__ , max_face_number + 1 ) for dice_numbers in product(A__ , repeat=A__ ): SCREAMING_SNAKE_CASE = sum(A__ ) totals_frequencies[total] += 1 return totals_frequencies def __a ( ): SCREAMING_SNAKE_CASE = total_frequency_distribution( sides_number=4 , dice_number=9 ) SCREAMING_SNAKE_CASE = total_frequency_distribution( sides_number=6 , dice_number=6 ) SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 9 SCREAMING_SNAKE_CASE = 4 * 9 SCREAMING_SNAKE_CASE = 6 for peter_total in range(A__ , max_peter_total + 1 ): peter_wins_count += peter_totals_frequencies[peter_total] * sum( colin_totals_frequencies[min_colin_total:peter_total] ) SCREAMING_SNAKE_CASE = (4**9) * (6**6) SCREAMING_SNAKE_CASE = peter_wins_count / total_games_number SCREAMING_SNAKE_CASE = round(A__ , ndigits=7 ) return rounded_peter_win_probability if __name__ == "__main__": print(f'{solution() = }')
706
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, TensorType __A : str = logging.get_logger(__name__) __A : Optional[Any] = { 'openai/imagegpt-small': '', 'openai/imagegpt-medium': '', 'openai/imagegpt-large': '', } class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' lowerCamelCase__ = "imagegpt" lowerCamelCase__ = ["past_key_values"] lowerCamelCase__ = { "hidden_size": "n_embd", "max_position_embeddings": "n_positions", "num_attention_heads": "n_head", "num_hidden_layers": "n_layer", } def __init__( self : List[str] , __lowerCamelCase : Any=512 + 1 , __lowerCamelCase : str=32 * 32 , __lowerCamelCase : Any=512 , __lowerCamelCase : Optional[int]=24 , __lowerCamelCase : Tuple=8 , __lowerCamelCase : List[str]=None , __lowerCamelCase : List[Any]="quick_gelu" , __lowerCamelCase : int=0.1 , __lowerCamelCase : str=0.1 , __lowerCamelCase : Optional[int]=0.1 , __lowerCamelCase : Optional[Any]=1e-5 , __lowerCamelCase : Optional[Any]=0.02 , __lowerCamelCase : Optional[Any]=True , __lowerCamelCase : Optional[int]=True , __lowerCamelCase : List[Any]=False , __lowerCamelCase : int=False , __lowerCamelCase : int=False , **__lowerCamelCase : Union[str, Any] , ): SCREAMING_SNAKE_CASE = vocab_size SCREAMING_SNAKE_CASE = n_positions SCREAMING_SNAKE_CASE = n_embd SCREAMING_SNAKE_CASE = n_layer SCREAMING_SNAKE_CASE = n_head SCREAMING_SNAKE_CASE = n_inner SCREAMING_SNAKE_CASE = activation_function SCREAMING_SNAKE_CASE = resid_pdrop SCREAMING_SNAKE_CASE = embd_pdrop SCREAMING_SNAKE_CASE = attn_pdrop SCREAMING_SNAKE_CASE = layer_norm_epsilon SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = scale_attn_weights SCREAMING_SNAKE_CASE = use_cache SCREAMING_SNAKE_CASE = scale_attn_by_inverse_layer_idx SCREAMING_SNAKE_CASE = reorder_and_upcast_attn SCREAMING_SNAKE_CASE = tie_word_embeddings super().__init__(tie_word_embeddings=__lowerCamelCase , **__lowerCamelCase ) class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' @property def _snake_case ( self : Optional[Any] ): return OrderedDict( [ ("input_ids", {0: "batch", 1: "sequence"}), ] ) def _snake_case ( self : Optional[int] , __lowerCamelCase : "FeatureExtractionMixin" , __lowerCamelCase : int = 1 , __lowerCamelCase : int = -1 , __lowerCamelCase : bool = False , __lowerCamelCase : Optional["TensorType"] = None , __lowerCamelCase : int = 3 , __lowerCamelCase : int = 32 , __lowerCamelCase : int = 32 , ): SCREAMING_SNAKE_CASE = self._generate_dummy_images(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) SCREAMING_SNAKE_CASE = dict(preprocessor(images=__lowerCamelCase , return_tensors=__lowerCamelCase ) ) return inputs
698
0
from __future__ import annotations def __a ( A__ : list[int] ): # This function is recursive SCREAMING_SNAKE_CASE = len(A__ ) # If the array contains only one element, we return it (it's the stop condition of # recursion) if array_length <= 1: return array # Else SCREAMING_SNAKE_CASE = array[0] SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = 1 SCREAMING_SNAKE_CASE = [] while not is_found and i < array_length: if array[i] < pivot: SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = [element for element in array[i:] if element >= array[i]] SCREAMING_SNAKE_CASE = longest_subsequence(A__ ) if len(A__ ) > len(A__ ): SCREAMING_SNAKE_CASE = temp_array else: i += 1 SCREAMING_SNAKE_CASE = [element for element in array[1:] if element >= pivot] SCREAMING_SNAKE_CASE = [pivot, *longest_subsequence(A__ )] if len(A__ ) > len(A__ ): return temp_array else: return longest_subseq if __name__ == "__main__": import doctest doctest.testmod()
707
from transformers import BertTokenizer, EncoderDecoderModel, SeqaSeqTrainer, SeqaSeqTrainingArguments from transformers.testing_utils import TestCasePlus, require_torch, slow from transformers.utils import is_datasets_available if is_datasets_available(): import datasets class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' @slow @require_torch def _snake_case ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE = EncoderDecoderModel.from_encoder_decoder_pretrained("prajjwal1/bert-tiny" , "prajjwal1/bert-tiny" ) SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained("bert-base-uncased" ) SCREAMING_SNAKE_CASE = bertabert.config.encoder.vocab_size SCREAMING_SNAKE_CASE = tokenizer.sep_token_id SCREAMING_SNAKE_CASE = tokenizer.cls_token_id SCREAMING_SNAKE_CASE = 128 SCREAMING_SNAKE_CASE = datasets.load_dataset("cnn_dailymail" , "3.0.0" , split="train[:1%]" ) SCREAMING_SNAKE_CASE = datasets.load_dataset("cnn_dailymail" , "3.0.0" , split="validation[:1%]" ) SCREAMING_SNAKE_CASE = train_dataset.select(range(32 ) ) SCREAMING_SNAKE_CASE = val_dataset.select(range(16 ) ) SCREAMING_SNAKE_CASE = 4 def _map_to_encoder_decoder_inputs(__lowerCamelCase : str ): # Tokenizer will automatically set [BOS] <text> [EOS] SCREAMING_SNAKE_CASE = tokenizer(batch["article"] , padding="max_length" , truncation=__lowerCamelCase , max_length=512 ) SCREAMING_SNAKE_CASE = tokenizer(batch["highlights"] , padding="max_length" , truncation=__lowerCamelCase , max_length=128 ) SCREAMING_SNAKE_CASE = inputs.input_ids SCREAMING_SNAKE_CASE = inputs.attention_mask SCREAMING_SNAKE_CASE = outputs.input_ids SCREAMING_SNAKE_CASE = outputs.input_ids.copy() SCREAMING_SNAKE_CASE = [ [-100 if token == tokenizer.pad_token_id else token for token in labels] for labels in batch["labels"] ] SCREAMING_SNAKE_CASE = outputs.attention_mask assert all(len(__lowerCamelCase ) == 512 for x in inputs.input_ids ) assert all(len(__lowerCamelCase ) == 128 for x in outputs.input_ids ) return batch def _compute_metrics(__lowerCamelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE = pred.label_ids SCREAMING_SNAKE_CASE = pred.predictions # all unnecessary tokens are removed SCREAMING_SNAKE_CASE = tokenizer.batch_decode(__lowerCamelCase , skip_special_tokens=__lowerCamelCase ) SCREAMING_SNAKE_CASE = tokenizer.batch_decode(__lowerCamelCase , skip_special_tokens=__lowerCamelCase ) SCREAMING_SNAKE_CASE = sum([int(pred_str[i] == label_str[i] ) for i in range(len(__lowerCamelCase ) )] ) / len(__lowerCamelCase ) return {"accuracy": accuracy} # map train dataset SCREAMING_SNAKE_CASE = train_dataset.map( _map_to_encoder_decoder_inputs , batched=__lowerCamelCase , batch_size=__lowerCamelCase , remove_columns=["article", "highlights"] , ) train_dataset.set_format( type="torch" , columns=["input_ids", "attention_mask", "decoder_input_ids", "decoder_attention_mask", "labels"] , ) # same for validation dataset SCREAMING_SNAKE_CASE = val_dataset.map( _map_to_encoder_decoder_inputs , batched=__lowerCamelCase , batch_size=__lowerCamelCase , remove_columns=["article", "highlights"] , ) val_dataset.set_format( type="torch" , columns=["input_ids", "attention_mask", "decoder_input_ids", "decoder_attention_mask", "labels"] , ) SCREAMING_SNAKE_CASE = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE = SeqaSeqTrainingArguments( output_dir=__lowerCamelCase , per_device_train_batch_size=__lowerCamelCase , per_device_eval_batch_size=__lowerCamelCase , predict_with_generate=__lowerCamelCase , evaluation_strategy="steps" , do_train=__lowerCamelCase , do_eval=__lowerCamelCase , warmup_steps=0 , eval_steps=2 , logging_steps=2 , ) # instantiate trainer SCREAMING_SNAKE_CASE = SeqaSeqTrainer( model=__lowerCamelCase , args=__lowerCamelCase , compute_metrics=_compute_metrics , train_dataset=__lowerCamelCase , eval_dataset=__lowerCamelCase , tokenizer=__lowerCamelCase , ) # start training trainer.train()
698
0
from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_VISION_2_SEQ_MAPPING if is_torch_available(): import torch from ..models.auto.modeling_auto import MODEL_FOR_VISION_2_SEQ_MAPPING __A : Optional[int] = logging.get_logger(__name__) @add_end_docstrings(__snake_case ) class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' def __init__( self : List[str] , *__lowerCamelCase : Any , **__lowerCamelCase : int ): super().__init__(*__lowerCamelCase , **__lowerCamelCase ) requires_backends(self , "vision" ) self.check_model_type( TF_MODEL_FOR_VISION_2_SEQ_MAPPING if self.framework == "tf" else MODEL_FOR_VISION_2_SEQ_MAPPING ) def _snake_case ( self : Optional[Any] , __lowerCamelCase : List[str]=None , __lowerCamelCase : List[Any]=None , __lowerCamelCase : Union[str, Any]=None ): SCREAMING_SNAKE_CASE = {} SCREAMING_SNAKE_CASE = {} if prompt is not None: SCREAMING_SNAKE_CASE = prompt if generate_kwargs is not None: SCREAMING_SNAKE_CASE = generate_kwargs if max_new_tokens is not None: if "generate_kwargs" not in forward_kwargs: SCREAMING_SNAKE_CASE = {} if "max_new_tokens" in forward_kwargs["generate_kwargs"]: raise ValueError( "'max_new_tokens' is defined twice, once in 'generate_kwargs' and once as a direct parameter," " please use only one" ) SCREAMING_SNAKE_CASE = max_new_tokens return preprocess_params, forward_kwargs, {} def __call__( self : Any , __lowerCamelCase : Union[str, List[str], "Image.Image", List["Image.Image"]] , **__lowerCamelCase : Any ): return super().__call__(__lowerCamelCase , **__lowerCamelCase ) def _snake_case ( self : Union[str, Any] , __lowerCamelCase : int , __lowerCamelCase : Any=None ): SCREAMING_SNAKE_CASE = load_image(__lowerCamelCase ) if prompt is not None: if not isinstance(__lowerCamelCase , __lowerCamelCase ): raise ValueError( f"Received an invalid text input, got - {type(__lowerCamelCase )} - but expected a single string. " "Note also that one single text can be provided for conditional image to text generation." ) SCREAMING_SNAKE_CASE = self.model.config.model_type if model_type == "git": SCREAMING_SNAKE_CASE = self.image_processor(images=__lowerCamelCase , return_tensors=self.framework ) SCREAMING_SNAKE_CASE = self.tokenizer(text=__lowerCamelCase , add_special_tokens=__lowerCamelCase ).input_ids SCREAMING_SNAKE_CASE = [self.tokenizer.cls_token_id] + input_ids SCREAMING_SNAKE_CASE = torch.tensor(__lowerCamelCase ).unsqueeze(0 ) model_inputs.update({"input_ids": input_ids} ) elif model_type == "pix2struct": SCREAMING_SNAKE_CASE = self.image_processor(images=__lowerCamelCase , header_text=__lowerCamelCase , return_tensors=self.framework ) elif model_type != "vision-encoder-decoder": # vision-encoder-decoder does not support conditional generation SCREAMING_SNAKE_CASE = self.image_processor(images=__lowerCamelCase , return_tensors=self.framework ) SCREAMING_SNAKE_CASE = self.tokenizer(__lowerCamelCase , return_tensors=self.framework ) model_inputs.update(__lowerCamelCase ) else: raise ValueError(f"Model type {model_type} does not support conditional text generation" ) else: SCREAMING_SNAKE_CASE = self.image_processor(images=__lowerCamelCase , return_tensors=self.framework ) if self.model.config.model_type == "git" and prompt is None: SCREAMING_SNAKE_CASE = None return model_inputs def _snake_case ( self : Union[str, Any] , __lowerCamelCase : Tuple , __lowerCamelCase : Optional[int]=None ): # Git model sets `model_inputs["input_ids"] = None` in `preprocess` (when `prompt=None`). In batch model, the # pipeline will group them into a list of `None`, which fail `_forward`. Avoid this by checking it first. if ( "input_ids" in model_inputs and isinstance(model_inputs["input_ids"] , __lowerCamelCase ) and all(x is None for x in model_inputs["input_ids"] ) ): SCREAMING_SNAKE_CASE = None if generate_kwargs is None: SCREAMING_SNAKE_CASE = {} # FIXME: We need to pop here due to a difference in how `generation.py` and `generation.tf_utils.py` # parse inputs. In the Tensorflow version, `generate` raises an error if we don't use `input_ids` whereas # the PyTorch version matches it with `self.model.main_input_name` or `self.model.encoder.main_input_name` # in the `_prepare_model_inputs` method. SCREAMING_SNAKE_CASE = model_inputs.pop(self.model.main_input_name ) SCREAMING_SNAKE_CASE = self.model.generate(__lowerCamelCase , **__lowerCamelCase , **__lowerCamelCase ) return model_outputs def _snake_case ( self : List[str] , __lowerCamelCase : Optional[Any] ): SCREAMING_SNAKE_CASE = [] for output_ids in model_outputs: SCREAMING_SNAKE_CASE = { "generated_text": self.tokenizer.decode( __lowerCamelCase , skip_special_tokens=__lowerCamelCase , ) } records.append(__lowerCamelCase ) return records
708
import inspect import unittest import warnings from math import ceil, floor from transformers import LevitConfig from transformers.file_utils import cached_property, is_torch_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_vision, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING, MODEL_MAPPING, LevitForImageClassification, LevitForImageClassificationWithTeacher, LevitModel, ) from transformers.models.levit.modeling_levit import LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import LevitImageProcessor class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' def _snake_case ( self : Optional[Any] ): SCREAMING_SNAKE_CASE = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(__lowerCamelCase , "hidden_sizes" ) ) self.parent.assertTrue(hasattr(__lowerCamelCase , "num_attention_heads" ) ) class _SCREAMING_SNAKE_CASE : '''simple docstring''' def __init__( self : str , __lowerCamelCase : Optional[int] , __lowerCamelCase : Union[str, Any]=13 , __lowerCamelCase : Optional[int]=64 , __lowerCamelCase : Dict=3 , __lowerCamelCase : Optional[int]=3 , __lowerCamelCase : int=2 , __lowerCamelCase : List[Any]=1 , __lowerCamelCase : Optional[Any]=16 , __lowerCamelCase : Tuple=[128, 256, 384] , __lowerCamelCase : int=[4, 6, 8] , __lowerCamelCase : Tuple=[2, 3, 4] , __lowerCamelCase : List[str]=[16, 16, 16] , __lowerCamelCase : int=0 , __lowerCamelCase : List[Any]=[2, 2, 2] , __lowerCamelCase : List[str]=[2, 2, 2] , __lowerCamelCase : List[str]=0.02 , __lowerCamelCase : Any=True , __lowerCamelCase : Dict=True , __lowerCamelCase : int=2 , ): SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = kernel_size SCREAMING_SNAKE_CASE = stride SCREAMING_SNAKE_CASE = padding SCREAMING_SNAKE_CASE = hidden_sizes SCREAMING_SNAKE_CASE = num_attention_heads SCREAMING_SNAKE_CASE = depths SCREAMING_SNAKE_CASE = key_dim SCREAMING_SNAKE_CASE = drop_path_rate SCREAMING_SNAKE_CASE = patch_size SCREAMING_SNAKE_CASE = attention_ratio SCREAMING_SNAKE_CASE = mlp_ratio SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = [ ["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], ] SCREAMING_SNAKE_CASE = is_training SCREAMING_SNAKE_CASE = use_labels SCREAMING_SNAKE_CASE = num_labels SCREAMING_SNAKE_CASE = initializer_range def _snake_case ( self : int ): SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE = None if self.use_labels: SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.num_labels ) SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, labels def _snake_case ( self : List[str] ): return LevitConfig( image_size=self.image_size , num_channels=self.num_channels , kernel_size=self.kernel_size , stride=self.stride , padding=self.padding , patch_size=self.patch_size , hidden_sizes=self.hidden_sizes , num_attention_heads=self.num_attention_heads , depths=self.depths , key_dim=self.key_dim , drop_path_rate=self.drop_path_rate , mlp_ratio=self.mlp_ratio , attention_ratio=self.attention_ratio , initializer_range=self.initializer_range , down_ops=self.down_ops , ) def _snake_case ( self : Optional[int] , __lowerCamelCase : List[str] , __lowerCamelCase : str , __lowerCamelCase : Any ): SCREAMING_SNAKE_CASE = LevitModel(config=__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(__lowerCamelCase ) SCREAMING_SNAKE_CASE = (self.image_size, self.image_size) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = image_size[0], image_size[1] for _ in range(4 ): SCREAMING_SNAKE_CASE = floor(((height + 2 * self.padding - self.kernel_size) / self.stride) + 1 ) SCREAMING_SNAKE_CASE = floor(((width + 2 * self.padding - self.kernel_size) / self.stride) + 1 ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, ceil(height / 4 ) * ceil(width / 4 ), self.hidden_sizes[-1]) , ) def _snake_case ( self : Tuple , __lowerCamelCase : int , __lowerCamelCase : Optional[int] , __lowerCamelCase : List[str] ): SCREAMING_SNAKE_CASE = self.num_labels SCREAMING_SNAKE_CASE = LevitForImageClassification(__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(__lowerCamelCase , labels=__lowerCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _snake_case ( self : Optional[Any] ): SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = config_and_inputs SCREAMING_SNAKE_CASE = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class _SCREAMING_SNAKE_CASE ( __snake_case , __snake_case , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = ( (LevitModel, LevitForImageClassification, LevitForImageClassificationWithTeacher) if is_torch_available() else () ) lowerCamelCase__ = ( { "feature-extraction": LevitModel, "image-classification": (LevitForImageClassification, LevitForImageClassificationWithTeacher), } if is_torch_available() else {} ) lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False def _snake_case ( self : Dict ): SCREAMING_SNAKE_CASE = LevitModelTester(self ) SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=__lowerCamelCase , has_text_modality=__lowerCamelCase , hidden_size=37 ) def _snake_case ( self : Any ): self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def _snake_case ( self : Any ): return @unittest.skip(reason="Levit does not use inputs_embeds" ) def _snake_case ( self : Union[str, Any] ): pass @unittest.skip(reason="Levit does not support input and output embeddings" ) def _snake_case ( self : Tuple ): pass @unittest.skip(reason="Levit does not output attentions" ) def _snake_case ( self : Tuple ): pass def _snake_case ( self : Dict ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(__lowerCamelCase ) SCREAMING_SNAKE_CASE = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE = ["pixel_values"] self.assertListEqual(arg_names[:1] , __lowerCamelCase ) def _snake_case ( self : List[Any] ): def check_hidden_states_output(__lowerCamelCase : List[str] , __lowerCamelCase : int , __lowerCamelCase : Optional[Any] ): SCREAMING_SNAKE_CASE = model_class(__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**self._prepare_for_class(__lowerCamelCase , __lowerCamelCase ) ) SCREAMING_SNAKE_CASE = outputs.hidden_states SCREAMING_SNAKE_CASE = len(self.model_tester.depths ) + 1 self.assertEqual(len(__lowerCamelCase ) , __lowerCamelCase ) SCREAMING_SNAKE_CASE = (self.model_tester.image_size, self.model_tester.image_size) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = image_size[0], image_size[1] for _ in range(4 ): SCREAMING_SNAKE_CASE = floor( ( (height + 2 * self.model_tester.padding - self.model_tester.kernel_size) / self.model_tester.stride ) + 1 ) SCREAMING_SNAKE_CASE = floor( ( (width + 2 * self.model_tester.padding - self.model_tester.kernel_size) / self.model_tester.stride ) + 1 ) # verify the first hidden states (first block) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [ height * width, self.model_tester.hidden_sizes[0], ] , ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = True check_hidden_states_output(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE = True check_hidden_states_output(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." ) def _snake_case ( self : int ): pass def _snake_case ( self : List[Any] , __lowerCamelCase : Tuple , __lowerCamelCase : Any , __lowerCamelCase : int=False ): SCREAMING_SNAKE_CASE = super()._prepare_for_class(__lowerCamelCase , __lowerCamelCase , return_labels=__lowerCamelCase ) if return_labels: if model_class.__name__ == "LevitForImageClassificationWithTeacher": del inputs_dict["labels"] return inputs_dict def _snake_case ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__lowerCamelCase ) def _snake_case ( self : str ): SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase ) def _snake_case ( self : List[Any] ): if not self.model_tester.is_training: return SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE = True for model_class in self.all_model_classes: # LevitForImageClassificationWithTeacher supports inference-only if ( model_class in get_values(__lowerCamelCase ) or model_class.__name__ == "LevitForImageClassificationWithTeacher" ): continue SCREAMING_SNAKE_CASE = model_class(__lowerCamelCase ) model.to(__lowerCamelCase ) model.train() SCREAMING_SNAKE_CASE = self._prepare_for_class(__lowerCamelCase , __lowerCamelCase , return_labels=__lowerCamelCase ) SCREAMING_SNAKE_CASE = model(**__lowerCamelCase ).loss loss.backward() def _snake_case ( self : Tuple ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = True for model_class in self.all_model_classes: if model_class in get_values(__lowerCamelCase ) or not model_class.supports_gradient_checkpointing: continue # LevitForImageClassificationWithTeacher supports inference-only if model_class.__name__ == "LevitForImageClassificationWithTeacher": continue SCREAMING_SNAKE_CASE = model_class(__lowerCamelCase ) model.gradient_checkpointing_enable() model.to(__lowerCamelCase ) model.train() SCREAMING_SNAKE_CASE = self._prepare_for_class(__lowerCamelCase , __lowerCamelCase , return_labels=__lowerCamelCase ) SCREAMING_SNAKE_CASE = model(**__lowerCamelCase ).loss loss.backward() def _snake_case ( self : List[str] ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE = [ {"title": "multi_label_classification", "num_labels": 2, "dtype": torch.float}, {"title": "single_label_classification", "num_labels": 1, "dtype": torch.long}, {"title": "regression", "num_labels": 1, "dtype": torch.float}, ] for model_class in self.all_model_classes: if ( model_class not in [ *get_values(__lowerCamelCase ), ] or model_class.__name__ == "LevitForImageClassificationWithTeacher" ): continue for problem_type in problem_types: with self.subTest(msg=f"Testing {model_class} with {problem_type['title']}" ): SCREAMING_SNAKE_CASE = problem_type["title"] SCREAMING_SNAKE_CASE = problem_type["num_labels"] SCREAMING_SNAKE_CASE = model_class(__lowerCamelCase ) model.to(__lowerCamelCase ) model.train() SCREAMING_SNAKE_CASE = self._prepare_for_class(__lowerCamelCase , __lowerCamelCase , return_labels=__lowerCamelCase ) if problem_type["num_labels"] > 1: SCREAMING_SNAKE_CASE = inputs["labels"].unsqueeze(1 ).repeat(1 , problem_type["num_labels"] ) SCREAMING_SNAKE_CASE = inputs["labels"].to(problem_type["dtype"] ) # This tests that we do not trigger the warning form PyTorch "Using a target size that is different # to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure # they have the same size." which is a symptom something in wrong for the regression problem. # See https://github.com/huggingface/transformers/issues/11780 with warnings.catch_warnings(record=__lowerCamelCase ) as warning_list: SCREAMING_SNAKE_CASE = model(**__lowerCamelCase ).loss for w in warning_list: if "Using a target size that is different to the input size" in str(w.message ): raise ValueError( f"Something is going wrong in the regression problem: intercepted {w.message}" ) loss.backward() @slow def _snake_case ( self : List[Any] ): for model_name in LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE = LevitModel.from_pretrained(__lowerCamelCase ) self.assertIsNotNone(__lowerCamelCase ) def __a ( ): SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' @cached_property def _snake_case ( self : Dict ): return LevitImageProcessor.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) @slow def _snake_case ( self : Optional[int] ): SCREAMING_SNAKE_CASE = LevitForImageClassificationWithTeacher.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to( __lowerCamelCase ) SCREAMING_SNAKE_CASE = self.default_image_processor SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=__lowerCamelCase , return_tensors="pt" ).to(__lowerCamelCase ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**__lowerCamelCase ) # verify the logits SCREAMING_SNAKE_CASE = torch.Size((1, 1000) ) self.assertEqual(outputs.logits.shape , __lowerCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([1.0_448, -0.3_745, -1.8_317] ).to(__lowerCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , __lowerCamelCase , atol=1e-4 ) )
698
0
import os import random import sys from . import cryptomath_module as cryptomath from . import rabin_miller __A : int = 3 def __a ( A__ : int ): print("Generating primitive root of p" ) while True: SCREAMING_SNAKE_CASE = random.randrange(3 , A__ ) if pow(A__ , 2 , A__ ) == 1: continue if pow(A__ , A__ , A__ ) == 1: continue return g def __a ( A__ : int ): print("Generating prime p..." ) SCREAMING_SNAKE_CASE = rabin_miller.generate_large_prime(A__ ) # select large prime number. SCREAMING_SNAKE_CASE = primitive_root(A__ ) # one primitive root on modulo p. SCREAMING_SNAKE_CASE = random.randrange(3 , A__ ) # private_key -> have to be greater than 2 for safety. SCREAMING_SNAKE_CASE = cryptomath.find_mod_inverse(pow(A__ , A__ , A__ ) , A__ ) SCREAMING_SNAKE_CASE = (key_size, e_a, e_a, p) SCREAMING_SNAKE_CASE = (key_size, d) return public_key, private_key def __a ( A__ : str , A__ : int ): if os.path.exists(F"{name}_pubkey.txt" ) or os.path.exists(F"{name}_privkey.txt" ): print("\nWARNING:" ) print( F"\"{name}_pubkey.txt\" or \"{name}_privkey.txt\" already exists. \n" "Use a different name or delete these files and re-run this program." ) sys.exit() SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = generate_key(A__ ) print(F"\nWriting public key to file {name}_pubkey.txt..." ) with open(F"{name}_pubkey.txt" , "w" ) as fo: fo.write(F"{public_key[0]},{public_key[1]},{public_key[2]},{public_key[3]}" ) print(F"Writing private key to file {name}_privkey.txt..." ) with open(F"{name}_privkey.txt" , "w" ) as fo: fo.write(F"{private_key[0]},{private_key[1]}" ) def __a ( ): print("Making key files..." ) make_key_files("elgamal" , 2048 ) print("Key files generation successful" ) if __name__ == "__main__": main()
709
import unittest from pathlib import Path from tempfile import NamedTemporaryFile, TemporaryDirectory from transformers import BertConfig, BertTokenizerFast, FeatureExtractionPipeline from transformers.convert_graph_to_onnx import ( convert, ensure_valid_input, generate_identified_filename, infer_shapes, quantize, ) from transformers.testing_utils import require_tf, require_tokenizers, require_torch, slow class _SCREAMING_SNAKE_CASE : '''simple docstring''' def _snake_case ( self : Tuple , __lowerCamelCase : Optional[int] , __lowerCamelCase : Optional[Any] , __lowerCamelCase : Any ): return None class _SCREAMING_SNAKE_CASE : '''simple docstring''' def _snake_case ( self : Union[str, Any] , __lowerCamelCase : Dict , __lowerCamelCase : Dict , __lowerCamelCase : Any , __lowerCamelCase : int ): return None class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = [ # (model_name, model_kwargs) ("bert-base-cased", {}), ("gpt2", {"use_cache": False}), # We don't support exporting GPT2 past keys anymore ] @require_tf @slow def _snake_case ( self : List[Any] ): for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST: self._test_export(__lowerCamelCase , "tf" , 12 , **__lowerCamelCase ) @require_torch @slow def _snake_case ( self : List[Any] ): for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST: self._test_export(__lowerCamelCase , "pt" , 12 , **__lowerCamelCase ) @require_torch @slow def _snake_case ( self : Optional[int] ): from transformers import BertModel SCREAMING_SNAKE_CASE = ["[UNK]", "[SEP]", "[CLS]", "[PAD]", "[MASK]", "some", "other", "words"] with NamedTemporaryFile(mode="w+t" ) as vocab_file: vocab_file.write("\n".join(__lowerCamelCase ) ) vocab_file.flush() SCREAMING_SNAKE_CASE = BertTokenizerFast(vocab_file.name ) with TemporaryDirectory() as bert_save_dir: SCREAMING_SNAKE_CASE = BertModel(BertConfig(vocab_size=len(__lowerCamelCase ) ) ) model.save_pretrained(__lowerCamelCase ) self._test_export(__lowerCamelCase , "pt" , 12 , __lowerCamelCase ) @require_tf @slow def _snake_case ( self : Any ): for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST: SCREAMING_SNAKE_CASE = self._test_export(__lowerCamelCase , "tf" , 12 , **__lowerCamelCase ) SCREAMING_SNAKE_CASE = quantize(Path(__lowerCamelCase ) ) # Ensure the actual quantized model is not bigger than the original one if quantized_path.stat().st_size >= Path(__lowerCamelCase ).stat().st_size: self.fail("Quantized model is bigger than initial ONNX model" ) @require_torch @slow def _snake_case ( self : Any ): for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST: SCREAMING_SNAKE_CASE = self._test_export(__lowerCamelCase , "pt" , 12 , **__lowerCamelCase ) SCREAMING_SNAKE_CASE = quantize(__lowerCamelCase ) # Ensure the actual quantized model is not bigger than the original one if quantized_path.stat().st_size >= Path(__lowerCamelCase ).stat().st_size: self.fail("Quantized model is bigger than initial ONNX model" ) def _snake_case ( self : Dict , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : Dict , __lowerCamelCase : int , __lowerCamelCase : Tuple=None , **__lowerCamelCase : List[str] ): try: # Compute path with TemporaryDirectory() as tempdir: SCREAMING_SNAKE_CASE = Path(__lowerCamelCase ).joinpath("model.onnx" ) # Remove folder if exists if path.parent.exists(): path.parent.rmdir() # Export convert(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , **__lowerCamelCase ) return path except Exception as e: self.fail(__lowerCamelCase ) @require_torch @require_tokenizers @slow def _snake_case ( self : Dict ): from transformers import BertModel SCREAMING_SNAKE_CASE = BertModel(BertConfig.from_pretrained("lysandre/tiny-bert-random" ) ) SCREAMING_SNAKE_CASE = BertTokenizerFast.from_pretrained("lysandre/tiny-bert-random" ) self._test_infer_dynamic_axis(__lowerCamelCase , __lowerCamelCase , "pt" ) @require_tf @require_tokenizers @slow def _snake_case ( self : int ): from transformers import TFBertModel SCREAMING_SNAKE_CASE = TFBertModel(BertConfig.from_pretrained("lysandre/tiny-bert-random" ) ) SCREAMING_SNAKE_CASE = BertTokenizerFast.from_pretrained("lysandre/tiny-bert-random" ) self._test_infer_dynamic_axis(__lowerCamelCase , __lowerCamelCase , "tf" ) def _snake_case ( self : int , __lowerCamelCase : Dict , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : str ): SCREAMING_SNAKE_CASE = FeatureExtractionPipeline(__lowerCamelCase , __lowerCamelCase ) SCREAMING_SNAKE_CASE = ["input_ids", "token_type_ids", "attention_mask", "output_0", "output_1"] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = infer_shapes(__lowerCamelCase , __lowerCamelCase ) # Assert all variables are present self.assertEqual(len(__lowerCamelCase ) , len(__lowerCamelCase ) ) self.assertTrue(all(var_name in shapes for var_name in variable_names ) ) self.assertSequenceEqual(variable_names[:3] , __lowerCamelCase ) self.assertSequenceEqual(variable_names[3:] , __lowerCamelCase ) # Assert inputs are {0: batch, 1: sequence} for var_name in ["input_ids", "token_type_ids", "attention_mask"]: self.assertDictEqual(shapes[var_name] , {0: "batch", 1: "sequence"} ) # Assert outputs are {0: batch, 1: sequence} and {0: batch} self.assertDictEqual(shapes["output_0"] , {0: "batch", 1: "sequence"} ) self.assertDictEqual(shapes["output_1"] , {0: "batch"} ) def _snake_case ( self : Optional[Any] ): SCREAMING_SNAKE_CASE = ["input_ids", "attention_mask", "token_type_ids"] SCREAMING_SNAKE_CASE = {"input_ids": [1, 2, 3, 4], "attention_mask": [0, 0, 0, 0], "token_type_ids": [1, 1, 1, 1]} SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = ensure_valid_input(FuncContiguousArgs() , __lowerCamelCase , __lowerCamelCase ) # Should have exactly the same number of args (all are valid) self.assertEqual(len(__lowerCamelCase ) , 3 ) # Should have exactly the same input names self.assertEqual(set(__lowerCamelCase ) , set(__lowerCamelCase ) ) # Parameter should be reordered according to their respective place in the function: # (input_ids, token_type_ids, attention_mask) self.assertEqual(__lowerCamelCase , (tokens["input_ids"], tokens["token_type_ids"], tokens["attention_mask"]) ) # Generated args are interleaved with another args (for instance parameter "past" in GPT2) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = ensure_valid_input(FuncNonContiguousArgs() , __lowerCamelCase , __lowerCamelCase ) # Should have exactly the one arg (all before the one not provided "some_other_args") self.assertEqual(len(__lowerCamelCase ) , 1 ) self.assertEqual(len(__lowerCamelCase ) , 1 ) # Should have only "input_ids" self.assertEqual(inputs_args[0] , tokens["input_ids"] ) self.assertEqual(ordered_input_names[0] , "input_ids" ) def _snake_case ( self : Optional[int] ): SCREAMING_SNAKE_CASE = generate_identified_filename(Path("/home/something/my_fake_model.onnx" ) , "-test" ) self.assertEqual("/home/something/my_fake_model-test.onnx" , generated.as_posix() )
698
0
import argparse from transformers import BigBirdConfig, BigBirdForPreTraining, BigBirdForQuestionAnswering, load_tf_weights_in_big_bird from transformers.utils import logging logging.set_verbosity_info() def __a ( A__ : List[str] , A__ : Optional[Any] , A__ : Union[str, Any] , A__ : List[Any] ): # Initialise PyTorch model SCREAMING_SNAKE_CASE = BigBirdConfig.from_json_file(A__ ) print(F"Building PyTorch model from configuration: {config}" ) if is_trivia_qa: SCREAMING_SNAKE_CASE = BigBirdForQuestionAnswering(A__ ) else: SCREAMING_SNAKE_CASE = BigBirdForPreTraining(A__ ) # Load weights from tf checkpoint load_tf_weights_in_big_bird(A__ , A__ , is_trivia_qa=A__ ) # Save pytorch-model print(F"Save PyTorch model to {pytorch_dump_path}" ) model.save_pretrained(A__ ) if __name__ == "__main__": __A : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--tf_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.' ) parser.add_argument( '--big_bird_config_file', default=None, type=str, required=True, help=( 'The config json file corresponding to the pre-trained BERT model. \n' 'This specifies the model architecture.' ), ) parser.add_argument( '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) parser.add_argument( '--is_trivia_qa', action='store_true', help='Whether to convert a model with a trivia_qa head.' ) __A : List[Any] = parser.parse_args() convert_tf_checkpoint_to_pytorch( args.tf_checkpoint_path, args.big_bird_config_file, args.pytorch_dump_path, args.is_trivia_qa )
710
from manim import * class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' def _snake_case ( self : List[Any] ): SCREAMING_SNAKE_CASE = Rectangle(height=0.5 , width=0.5 ) SCREAMING_SNAKE_CASE = Rectangle(height=0.46 , width=0.46 ).set_stroke(width=0 ) SCREAMING_SNAKE_CASE = [mem.copy() for i in range(6 )] SCREAMING_SNAKE_CASE = [mem.copy() for i in range(6 )] SCREAMING_SNAKE_CASE = VGroup(*__lowerCamelCase ).arrange(__lowerCamelCase , buff=0 ) SCREAMING_SNAKE_CASE = VGroup(*__lowerCamelCase ).arrange(__lowerCamelCase , buff=0 ) SCREAMING_SNAKE_CASE = VGroup(__lowerCamelCase , __lowerCamelCase ).arrange(__lowerCamelCase , buff=0 ) SCREAMING_SNAKE_CASE = Text("CPU" , font_size=24 ) SCREAMING_SNAKE_CASE = Group(__lowerCamelCase , __lowerCamelCase ).arrange(__lowerCamelCase , buff=0.5 , aligned_edge=__lowerCamelCase ) cpu.move_to([-2.5, -0.5, 0] ) self.add(__lowerCamelCase ) SCREAMING_SNAKE_CASE = [mem.copy() for i in range(4 )] SCREAMING_SNAKE_CASE = VGroup(*__lowerCamelCase ).arrange(__lowerCamelCase , buff=0 ) SCREAMING_SNAKE_CASE = Text("GPU" , font_size=24 ) SCREAMING_SNAKE_CASE = Group(__lowerCamelCase , __lowerCamelCase ).arrange(__lowerCamelCase , buff=0.5 , aligned_edge=__lowerCamelCase ) gpu.move_to([-1, -1, 0] ) self.add(__lowerCamelCase ) SCREAMING_SNAKE_CASE = [mem.copy() for i in range(6 )] SCREAMING_SNAKE_CASE = VGroup(*__lowerCamelCase ).arrange(__lowerCamelCase , buff=0 ) SCREAMING_SNAKE_CASE = Text("Model" , font_size=24 ) SCREAMING_SNAKE_CASE = Group(__lowerCamelCase , __lowerCamelCase ).arrange(__lowerCamelCase , buff=0.5 , aligned_edge=__lowerCamelCase ) model.move_to([3, -1.0, 0] ) self.add(__lowerCamelCase ) SCREAMING_SNAKE_CASE = [] for i, rect in enumerate(__lowerCamelCase ): rect.set_stroke(__lowerCamelCase ) # target = fill.copy().set_fill(YELLOW, opacity=0.7) # target.move_to(rect) # self.add(target) SCREAMING_SNAKE_CASE = Rectangle(height=0.46 / 4 , width=0.46 / 3 ).set_stroke(width=0.0 ).set_fill(__lowerCamelCase , opacity=0.7 ) if i == 0: cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT ) , buff=0.02 , direction=__lowerCamelCase ) cpu_target.set_x(cpu_target.get_x() + 0.1 ) elif i == 3: cpu_target.next_to(cpu_targs[0] , direction=__lowerCamelCase , buff=0.0 ) else: cpu_target.next_to(cpu_targs[i - 1] , direction=__lowerCamelCase , buff=0.0 ) self.add(__lowerCamelCase ) cpu_targs.append(__lowerCamelCase ) SCREAMING_SNAKE_CASE = [mem.copy() for i in range(6 )] SCREAMING_SNAKE_CASE = VGroup(*__lowerCamelCase ).arrange(__lowerCamelCase , buff=0 ) SCREAMING_SNAKE_CASE = Text("Loaded Checkpoint" , font_size=24 ) SCREAMING_SNAKE_CASE = Group(__lowerCamelCase , __lowerCamelCase ).arrange(__lowerCamelCase , aligned_edge=__lowerCamelCase , buff=0.4 ) checkpoint.move_to([3, 0.5, 0] ) SCREAMING_SNAKE_CASE = Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) SCREAMING_SNAKE_CASE = MarkupText( f"<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model" , font_size=18 , ) key_text.move_to([-5, 2.4, 0] ) self.add(__lowerCamelCase , __lowerCamelCase ) SCREAMING_SNAKE_CASE = MarkupText( f"<span fgcolor='{BLUE}'>●</span> Checkpoint" , font_size=18 , ) blue_text.next_to(__lowerCamelCase , DOWN * 2.4 , aligned_edge=key_text.get_left() ) SCREAMING_SNAKE_CASE = MarkupText( f"Next, a <i><span fgcolor=\"{BLUE}\">second</span></i> model is loaded into memory,\nwith the weights of a <span fgcolor=\"{BLUE}\">single shard</span>." , font_size=24 , ) step_a.move_to([2, 2, 0] ) self.play(Write(__lowerCamelCase ) , Write(__lowerCamelCase ) ) self.play(Write(__lowerCamelCase , run_time=1 ) , Create(__lowerCamelCase , run_time=1 ) ) SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for i, rect in enumerate(__lowerCamelCase ): SCREAMING_SNAKE_CASE = fill.copy().set_fill(__lowerCamelCase , opacity=0.7 ) target.move_to(__lowerCamelCase ) first_animations.append(GrowFromCenter(__lowerCamelCase , run_time=1 ) ) SCREAMING_SNAKE_CASE = target.copy() cpu_target.generate_target() if i < 5: cpu_target.target.move_to(cpu_left_col_base[i + 1] ) else: cpu_target.target.move_to(cpu_right_col_base[i - 5] ) second_animations.append(MoveToTarget(__lowerCamelCase , run_time=1.5 ) ) self.play(*__lowerCamelCase ) self.play(*__lowerCamelCase ) self.wait()
698
0
from unittest.mock import patch import pyspark from datasets.packaged_modules.spark.spark import ( Spark, SparkExamplesIterable, _generate_iterable_examples, ) from ..utils import ( require_dill_gt_0_3_2, require_not_windows, ) def __a ( A__ : Optional[Any] , A__ : Union[str, Any] ): SCREAMING_SNAKE_CASE = [] for part_id in partition_order: SCREAMING_SNAKE_CASE = df.where(F"SPARK_PARTITION_ID() = {part_id}" ).collect() for row_idx, row in enumerate(A__ ): expected_row_ids_and_row_dicts.append((F"{part_id}_{row_idx}", row.asDict()) ) return expected_row_ids_and_row_dicts @require_not_windows @require_dill_gt_0_3_2 def __a ( ): SCREAMING_SNAKE_CASE = pyspark.sql.SparkSession.builder.master("local[*]" ).appName("pyspark" ).getOrCreate() SCREAMING_SNAKE_CASE = spark.range(100 ).repartition(1 ) SCREAMING_SNAKE_CASE = Spark(A__ ) # The id ints will be converted to Pyarrow int64s, so each row will be 8 bytes. Setting a max_shard_size of 16 means # that each partition can hold 2 rows. spark_builder._repartition_df_if_needed(max_shard_size=16 ) # Given that the dataframe has 100 rows and each partition has 2 rows, we expect 50 partitions. assert spark_builder.df.rdd.getNumPartitions() == 50 @require_not_windows @require_dill_gt_0_3_2 def __a ( ): SCREAMING_SNAKE_CASE = pyspark.sql.SparkSession.builder.master("local[*]" ).appName("pyspark" ).getOrCreate() SCREAMING_SNAKE_CASE = spark.range(10 ).repartition(2 ) SCREAMING_SNAKE_CASE = [1, 0] SCREAMING_SNAKE_CASE = _generate_iterable_examples(A__ , A__ ) # Reverse the partitions. SCREAMING_SNAKE_CASE = _get_expected_row_ids_and_row_dicts_for_partition_order(A__ , A__ ) for i, (row_id, row_dict) in enumerate(generate_fn() ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def __a ( ): SCREAMING_SNAKE_CASE = pyspark.sql.SparkSession.builder.master("local[*]" ).appName("pyspark" ).getOrCreate() SCREAMING_SNAKE_CASE = spark.range(10 ).repartition(1 ) SCREAMING_SNAKE_CASE = SparkExamplesIterable(A__ ) assert it.n_shards == 1 for i, (row_id, row_dict) in enumerate(A__ ): assert row_id == F"0_{i}" assert row_dict == {"id": i} @require_not_windows @require_dill_gt_0_3_2 def __a ( ): SCREAMING_SNAKE_CASE = pyspark.sql.SparkSession.builder.master("local[*]" ).appName("pyspark" ).getOrCreate() SCREAMING_SNAKE_CASE = spark.range(30 ).repartition(3 ) # Mock the generator so that shuffle reverses the partition indices. with patch("numpy.random.Generator" ) as generator_mock: SCREAMING_SNAKE_CASE = lambda A__ : x.reverse() SCREAMING_SNAKE_CASE = _get_expected_row_ids_and_row_dicts_for_partition_order(A__ , [2, 1, 0] ) SCREAMING_SNAKE_CASE = SparkExamplesIterable(A__ ).shuffle_data_sources(A__ ) assert shuffled_it.n_shards == 3 for i, (row_id, row_dict) in enumerate(A__ ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def __a ( ): SCREAMING_SNAKE_CASE = pyspark.sql.SparkSession.builder.master("local[*]" ).appName("pyspark" ).getOrCreate() SCREAMING_SNAKE_CASE = spark.range(20 ).repartition(4 ) # Partitions 0 and 2 SCREAMING_SNAKE_CASE = SparkExamplesIterable(A__ ).shard_data_sources(worker_id=0 , num_workers=2 ) assert shard_it_a.n_shards == 2 SCREAMING_SNAKE_CASE = _get_expected_row_ids_and_row_dicts_for_partition_order(A__ , [0, 2] ) for i, (row_id, row_dict) in enumerate(A__ ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict # Partitions 1 and 3 SCREAMING_SNAKE_CASE = SparkExamplesIterable(A__ ).shard_data_sources(worker_id=1 , num_workers=2 ) assert shard_it_a.n_shards == 2 SCREAMING_SNAKE_CASE = _get_expected_row_ids_and_row_dicts_for_partition_order(A__ , [1, 3] ) for i, (row_id, row_dict) in enumerate(A__ ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def __a ( ): SCREAMING_SNAKE_CASE = pyspark.sql.SparkSession.builder.master("local[*]" ).appName("pyspark" ).getOrCreate() SCREAMING_SNAKE_CASE = spark.range(100 ).repartition(1 ) SCREAMING_SNAKE_CASE = Spark(A__ ) # Choose a small max_shard_size for maximum partitioning. spark_builder._repartition_df_if_needed(max_shard_size=1 ) # The new number of partitions should not be greater than the number of rows. assert spark_builder.df.rdd.getNumPartitions() == 100
711
import unittest from transformers import is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class _SCREAMING_SNAKE_CASE : '''simple docstring''' @staticmethod def _snake_case ( *__lowerCamelCase : Any , **__lowerCamelCase : Any ): pass @is_pipeline_test @require_vision class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' @require_torch def _snake_case ( self : Dict ): SCREAMING_SNAKE_CASE = pipeline( model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification" , ) SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) SCREAMING_SNAKE_CASE = image_classifier(__lowerCamelCase , candidate_labels=["a", "b", "c"] ) # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across # python and torch versions. self.assertIn( nested_simplify(__lowerCamelCase ) , [ [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}], [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "c"}, {"score": 0.333, "label": "b"}], ] , ) SCREAMING_SNAKE_CASE = image_classifier([image] * 5 , candidate_labels=["A", "B", "C"] , batch_size=2 ) self.assertEqual( nested_simplify(__lowerCamelCase ) , [ [ {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, ], [ {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, ], [ {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, ], [ {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, ], [ {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, ], ] , ) @require_tf def _snake_case ( self : int ): SCREAMING_SNAKE_CASE = pipeline( model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification" , framework="tf" ) SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) SCREAMING_SNAKE_CASE = image_classifier(__lowerCamelCase , candidate_labels=["a", "b", "c"] ) self.assertEqual( nested_simplify(__lowerCamelCase ) , [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}] , ) SCREAMING_SNAKE_CASE = image_classifier([image] * 5 , candidate_labels=["A", "B", "C"] , batch_size=2 ) self.assertEqual( nested_simplify(__lowerCamelCase ) , [ [ {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, ], [ {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, ], [ {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, ], [ {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, ], [ {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, {"score": 0.333, "label": ANY(__lowerCamelCase )}, ], ] , ) @slow @require_torch def _snake_case ( self : Tuple ): SCREAMING_SNAKE_CASE = pipeline( task="zero-shot-image-classification" , model="openai/clip-vit-base-patch32" , ) # This is an image of 2 cats with remotes and no planes SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) SCREAMING_SNAKE_CASE = image_classifier(__lowerCamelCase , candidate_labels=["cat", "plane", "remote"] ) self.assertEqual( nested_simplify(__lowerCamelCase ) , [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ] , ) SCREAMING_SNAKE_CASE = image_classifier([image] * 5 , candidate_labels=["cat", "plane", "remote"] , batch_size=2 ) self.assertEqual( nested_simplify(__lowerCamelCase ) , [ [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ] * 5 , ) @slow @require_tf def _snake_case ( self : Any ): SCREAMING_SNAKE_CASE = pipeline( task="zero-shot-image-classification" , model="openai/clip-vit-base-patch32" , framework="tf" ) # This is an image of 2 cats with remotes and no planes SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) SCREAMING_SNAKE_CASE = image_classifier(__lowerCamelCase , candidate_labels=["cat", "plane", "remote"] ) self.assertEqual( nested_simplify(__lowerCamelCase ) , [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ] , ) SCREAMING_SNAKE_CASE = image_classifier([image] * 5 , candidate_labels=["cat", "plane", "remote"] , batch_size=2 ) self.assertEqual( nested_simplify(__lowerCamelCase ) , [ [ {"score": 0.511, "label": "remote"}, {"score": 0.485, "label": "cat"}, {"score": 0.004, "label": "plane"}, ], ] * 5 , )
698
0
from math import factorial class _SCREAMING_SNAKE_CASE : '''simple docstring''' def __init__( self : Any , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : Dict ): SCREAMING_SNAKE_CASE = real if isinstance(__lowerCamelCase , __lowerCamelCase ): SCREAMING_SNAKE_CASE = [1] * rank else: SCREAMING_SNAKE_CASE = rank def __repr__( self : Any ): return ( f"{self.real}+" f"{'+'.join(str(__lowerCamelCase )+'E'+str(n+1 )for n,dual in enumerate(self.duals ) )}" ) def _snake_case ( self : Any ): SCREAMING_SNAKE_CASE = self.duals.copy() while cur[-1] == 0: cur.pop(-1 ) return Dual(self.real , __lowerCamelCase ) def __add__( self : List[Any] , __lowerCamelCase : str ): if not isinstance(__lowerCamelCase , __lowerCamelCase ): return Dual(self.real + other , self.duals ) SCREAMING_SNAKE_CASE = self.duals.copy() SCREAMING_SNAKE_CASE = other.duals.copy() if len(__lowerCamelCase ) > len(__lowerCamelCase ): o_dual.extend([1] * (len(__lowerCamelCase ) - len(__lowerCamelCase )) ) elif len(__lowerCamelCase ) < len(__lowerCamelCase ): s_dual.extend([1] * (len(__lowerCamelCase ) - len(__lowerCamelCase )) ) SCREAMING_SNAKE_CASE = [] for i in range(len(__lowerCamelCase ) ): new_duals.append(s_dual[i] + o_dual[i] ) return Dual(self.real + other.real , __lowerCamelCase ) lowerCamelCase__ = __add__ def __sub__( self : Dict , __lowerCamelCase : Union[str, Any] ): return self + other * -1 def __mul__( self : Tuple , __lowerCamelCase : List[Any] ): if not isinstance(__lowerCamelCase , __lowerCamelCase ): SCREAMING_SNAKE_CASE = [] for i in self.duals: new_duals.append(i * other ) return Dual(self.real * other , __lowerCamelCase ) SCREAMING_SNAKE_CASE = [0] * (len(self.duals ) + len(other.duals ) + 1) for i, item in enumerate(self.duals ): for j, jtem in enumerate(other.duals ): new_duals[i + j + 1] += item * jtem for k in range(len(self.duals ) ): new_duals[k] += self.duals[k] * other.real for index in range(len(other.duals ) ): new_duals[index] += other.duals[index] * self.real return Dual(self.real * other.real , __lowerCamelCase ) lowerCamelCase__ = __mul__ def __truediv__( self : Any , __lowerCamelCase : str ): if not isinstance(__lowerCamelCase , __lowerCamelCase ): SCREAMING_SNAKE_CASE = [] for i in self.duals: new_duals.append(i / other ) return Dual(self.real / other , __lowerCamelCase ) raise ValueError def __floordiv__( self : Optional[Any] , __lowerCamelCase : Tuple ): if not isinstance(__lowerCamelCase , __lowerCamelCase ): SCREAMING_SNAKE_CASE = [] for i in self.duals: new_duals.append(i // other ) return Dual(self.real // other , __lowerCamelCase ) raise ValueError def __pow__( self : Dict , __lowerCamelCase : List[str] ): if n < 0 or isinstance(__lowerCamelCase , __lowerCamelCase ): raise ValueError("power must be a positive integer" ) if n == 0: return 1 if n == 1: return self SCREAMING_SNAKE_CASE = self for _ in range(n - 1 ): x *= self return x def __a ( A__ : Optional[int] , A__ : Optional[Any] , A__ : List[Any] ): if not callable(A__ ): raise ValueError("differentiate() requires a function as input for func" ) if not isinstance(A__ , (float, int) ): raise ValueError("differentiate() requires a float as input for position" ) if not isinstance(A__ , A__ ): raise ValueError("differentiate() requires an int as input for order" ) SCREAMING_SNAKE_CASE = Dual(A__ , 1 ) SCREAMING_SNAKE_CASE = func(A__ ) if order == 0: return result.real return result.duals[order - 1] * factorial(A__ ) if __name__ == "__main__": import doctest doctest.testmod() def __a ( A__ : Optional[Any] ): return y**2 * y**4 print(differentiate(f, 9, 2))
712
__A : dict[str, float] = { "joule": 1.0, "kilojoule": 1_0_0_0, "megajoule": 1_0_0_0_0_0_0, "gigajoule": 1_0_0_0_0_0_0_0_0_0, "wattsecond": 1.0, "watthour": 3_6_0_0, "kilowatthour": 3_6_0_0_0_0_0, "newtonmeter": 1.0, "calorie_nutr": 4_1_8_6.8, "kilocalorie_nutr": 4_1_8_6_8_0_0.0_0, "electronvolt": 1.6_0217_6634e-19, "britishthermalunit_it": 1_0_5_5.0_5_5_8_5, "footpound": 1.355_818, } def __a ( A__ : str , A__ : str , A__ : float ): if to_type not in ENERGY_CONVERSION or from_type not in ENERGY_CONVERSION: SCREAMING_SNAKE_CASE = ( F"Incorrect 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\n" F"Valid values are: {', '.join(A__ )}" ) raise ValueError(A__ ) return value * ENERGY_CONVERSION[from_type] / ENERGY_CONVERSION[to_type] if __name__ == "__main__": import doctest doctest.testmod()
698
0
'''simple docstring''' import io import json import fsspec import pytest from datasets import Dataset, DatasetDict, Features, NamedSplit, Value from datasets.io.json import JsonDatasetReader, JsonDatasetWriter from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def __a ( A__ : str , A__ : List[str] ): assert isinstance(A__ , A__ ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("keep_in_memory" , [False, True] ) def __a ( A__ : Tuple , A__ : List[Any] , A__ : Optional[Any] ): SCREAMING_SNAKE_CASE = tmp_path / "cache" SCREAMING_SNAKE_CASE = {"col_1": "string", "col_2": "int64", "col_3": "float64"} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): SCREAMING_SNAKE_CASE = JsonDatasetReader(A__ , cache_dir=A__ , keep_in_memory=A__ ).read() _check_json_dataset(A__ , A__ ) @pytest.mark.parametrize( "features" , [ None, {"col_1": "string", "col_2": "int64", "col_3": "float64"}, {"col_1": "string", "col_2": "string", "col_3": "string"}, {"col_1": "int32", "col_2": "int32", "col_3": "int32"}, {"col_1": "float32", "col_2": "float32", "col_3": "float32"}, ] , ) def __a ( A__ : Optional[Any] , A__ : Optional[int] , A__ : int ): SCREAMING_SNAKE_CASE = tmp_path / "cache" SCREAMING_SNAKE_CASE = {"col_1": "string", "col_2": "int64", "col_3": "float64"} SCREAMING_SNAKE_CASE = features.copy() if features else default_expected_features SCREAMING_SNAKE_CASE = ( Features({feature: Value(A__ ) for feature, dtype in features.items()} ) if features is not None else None ) SCREAMING_SNAKE_CASE = JsonDatasetReader(A__ , features=A__ , cache_dir=A__ ).read() _check_json_dataset(A__ , A__ ) @pytest.mark.parametrize( "features" , [ None, {"col_3": "float64", "col_1": "string", "col_2": "int64"}, ] , ) def __a ( A__ : int , A__ : str , A__ : Optional[Any] ): SCREAMING_SNAKE_CASE = tmp_path / "cache" SCREAMING_SNAKE_CASE = {"col_3": "float64", "col_1": "string", "col_2": "int64"} SCREAMING_SNAKE_CASE = features.copy() if features else default_expected_features SCREAMING_SNAKE_CASE = ( Features({feature: Value(A__ ) for feature, dtype in features.items()} ) if features is not None else None ) SCREAMING_SNAKE_CASE = JsonDatasetReader(A__ , features=A__ , cache_dir=A__ ).read() assert isinstance(A__ , A__ ) assert dataset.num_rows == 2 assert dataset.num_columns == 3 assert dataset.column_names == ["col_3", "col_1", "col_2"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype def __a ( A__ : str , A__ : str ): # jsonl_312_path features are {"col_3": "float64", "col_1": "string", "col_2": "int64"} SCREAMING_SNAKE_CASE = {"col_2": "int64", "col_3": "float64", "col_1": "string"} SCREAMING_SNAKE_CASE = features.copy() SCREAMING_SNAKE_CASE = ( Features({feature: Value(A__ ) for feature, dtype in features.items()} ) if features is not None else None ) SCREAMING_SNAKE_CASE = tmp_path / "cache" SCREAMING_SNAKE_CASE = JsonDatasetReader(A__ , features=A__ , cache_dir=A__ ).read() assert isinstance(A__ , A__ ) assert dataset.num_rows == 2 assert dataset.num_columns == 3 assert dataset.column_names == ["col_2", "col_3", "col_1"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("split" , [None, NamedSplit("train" ), "train", "test"] ) def __a ( A__ : Any , A__ : List[str] , A__ : Union[str, Any] ): SCREAMING_SNAKE_CASE = tmp_path / "cache" SCREAMING_SNAKE_CASE = {"col_1": "string", "col_2": "int64", "col_3": "float64"} SCREAMING_SNAKE_CASE = JsonDatasetReader(A__ , cache_dir=A__ , split=A__ ).read() _check_json_dataset(A__ , A__ ) assert dataset.split == split if split else "train" @pytest.mark.parametrize("path_type" , [str, list] ) def __a ( A__ : Tuple , A__ : Optional[Any] , A__ : List[Any] ): if issubclass(A__ , A__ ): SCREAMING_SNAKE_CASE = jsonl_path elif issubclass(A__ , A__ ): SCREAMING_SNAKE_CASE = [jsonl_path] SCREAMING_SNAKE_CASE = tmp_path / "cache" SCREAMING_SNAKE_CASE = {"col_1": "string", "col_2": "int64", "col_3": "float64"} SCREAMING_SNAKE_CASE = JsonDatasetReader(A__ , cache_dir=A__ ).read() _check_json_dataset(A__ , A__ ) def __a ( A__ : str , A__ : Dict , A__ : Any=("train",) ): assert isinstance(A__ , A__ ) for split in splits: SCREAMING_SNAKE_CASE = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("keep_in_memory" , [False, True] ) def __a ( A__ : List[Any] , A__ : Dict , A__ : Dict ): SCREAMING_SNAKE_CASE = tmp_path / "cache" SCREAMING_SNAKE_CASE = {"col_1": "string", "col_2": "int64", "col_3": "float64"} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): SCREAMING_SNAKE_CASE = JsonDatasetReader({"train": jsonl_path} , cache_dir=A__ , keep_in_memory=A__ ).read() _check_json_datasetdict(A__ , A__ ) @pytest.mark.parametrize( "features" , [ None, {"col_1": "string", "col_2": "int64", "col_3": "float64"}, {"col_1": "string", "col_2": "string", "col_3": "string"}, {"col_1": "int32", "col_2": "int32", "col_3": "int32"}, {"col_1": "float32", "col_2": "float32", "col_3": "float32"}, ] , ) def __a ( A__ : int , A__ : Optional[Any] , A__ : Optional[Any] ): SCREAMING_SNAKE_CASE = tmp_path / "cache" SCREAMING_SNAKE_CASE = {"col_1": "string", "col_2": "int64", "col_3": "float64"} SCREAMING_SNAKE_CASE = features.copy() if features else default_expected_features SCREAMING_SNAKE_CASE = ( Features({feature: Value(A__ ) for feature, dtype in features.items()} ) if features is not None else None ) SCREAMING_SNAKE_CASE = JsonDatasetReader({"train": jsonl_path} , features=A__ , cache_dir=A__ ).read() _check_json_datasetdict(A__ , A__ ) @pytest.mark.parametrize("split" , [None, NamedSplit("train" ), "train", "test"] ) def __a ( A__ : Dict , A__ : Optional[Any] , A__ : Dict ): if split: SCREAMING_SNAKE_CASE = {split: jsonl_path} else: SCREAMING_SNAKE_CASE = "train" SCREAMING_SNAKE_CASE = {"train": jsonl_path, "test": jsonl_path} SCREAMING_SNAKE_CASE = tmp_path / "cache" SCREAMING_SNAKE_CASE = {"col_1": "string", "col_2": "int64", "col_3": "float64"} SCREAMING_SNAKE_CASE = JsonDatasetReader(A__ , cache_dir=A__ ).read() _check_json_datasetdict(A__ , A__ , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def __a ( A__ : Dict ): return json.load(A__ ) def __a ( A__ : Optional[int] ): return [json.loads(A__ ) for line in buffer] class _SCREAMING_SNAKE_CASE : '''simple docstring''' @pytest.mark.parametrize("lines, load_json_function" , [(True, load_json_lines), (False, load_json)] ) def _snake_case ( self : List[str] , __lowerCamelCase : List[str] , __lowerCamelCase : Dict , __lowerCamelCase : List[str] ): with io.BytesIO() as buffer: JsonDatasetWriter(__lowerCamelCase , __lowerCamelCase , lines=__lowerCamelCase ).write() buffer.seek(0 ) SCREAMING_SNAKE_CASE = load_json_function(__lowerCamelCase ) assert isinstance(__lowerCamelCase , __lowerCamelCase ) assert isinstance(exported_content[0] , __lowerCamelCase ) assert len(__lowerCamelCase ) == 10 @pytest.mark.parametrize( "orient, container, keys, len_at" , [ ("records", list, {"tokens", "labels", "answers", "id"}, None), ("split", dict, {"columns", "data"}, "data"), ("index", dict, set("0123456789" ), None), ("columns", dict, {"tokens", "labels", "answers", "id"}, "tokens"), ("values", list, None, None), ("table", dict, {"schema", "data"}, "data"), ] , ) def _snake_case ( self : Optional[Any] , __lowerCamelCase : int , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : int , __lowerCamelCase : int , __lowerCamelCase : str ): with io.BytesIO() as buffer: JsonDatasetWriter(__lowerCamelCase , __lowerCamelCase , lines=__lowerCamelCase , orient=__lowerCamelCase ).write() buffer.seek(0 ) SCREAMING_SNAKE_CASE = load_json(__lowerCamelCase ) assert isinstance(__lowerCamelCase , __lowerCamelCase ) if keys: if container is dict: assert exported_content.keys() == keys else: assert exported_content[0].keys() == keys else: assert not hasattr(__lowerCamelCase , "keys" ) and not hasattr(exported_content[0] , "keys" ) if len_at: assert len(exported_content[len_at] ) == 10 else: assert len(__lowerCamelCase ) == 10 @pytest.mark.parametrize("lines, load_json_function" , [(True, load_json_lines), (False, load_json)] ) def _snake_case ( self : Tuple , __lowerCamelCase : int , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : Optional[Any] ): with io.BytesIO() as buffer: JsonDatasetWriter(__lowerCamelCase , __lowerCamelCase , lines=__lowerCamelCase , num_proc=2 ).write() buffer.seek(0 ) SCREAMING_SNAKE_CASE = load_json_function(__lowerCamelCase ) assert isinstance(__lowerCamelCase , __lowerCamelCase ) assert isinstance(exported_content[0] , __lowerCamelCase ) assert len(__lowerCamelCase ) == 10 @pytest.mark.parametrize( "orient, container, keys, len_at" , [ ("records", list, {"tokens", "labels", "answers", "id"}, None), ("split", dict, {"columns", "data"}, "data"), ("index", dict, set("0123456789" ), None), ("columns", dict, {"tokens", "labels", "answers", "id"}, "tokens"), ("values", list, None, None), ("table", dict, {"schema", "data"}, "data"), ] , ) def _snake_case ( self : Dict , __lowerCamelCase : Dict , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : Optional[int] , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : List[str] ): with io.BytesIO() as buffer: JsonDatasetWriter(__lowerCamelCase , __lowerCamelCase , lines=__lowerCamelCase , orient=__lowerCamelCase , num_proc=2 ).write() buffer.seek(0 ) SCREAMING_SNAKE_CASE = load_json(__lowerCamelCase ) assert isinstance(__lowerCamelCase , __lowerCamelCase ) if keys: if container is dict: assert exported_content.keys() == keys else: assert exported_content[0].keys() == keys else: assert not hasattr(__lowerCamelCase , "keys" ) and not hasattr(exported_content[0] , "keys" ) if len_at: assert len(exported_content[len_at] ) == 10 else: assert len(__lowerCamelCase ) == 10 def _snake_case ( self : Any , __lowerCamelCase : List[Any] ): with pytest.raises(__lowerCamelCase ): with io.BytesIO() as buffer: JsonDatasetWriter(__lowerCamelCase , __lowerCamelCase , num_proc=0 ) @pytest.mark.parametrize("compression, extension" , [("gzip", "gz"), ("bz2", "bz2"), ("xz", "xz")] ) def _snake_case ( self : Tuple , __lowerCamelCase : Optional[int] , __lowerCamelCase : str , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : List[str] , __lowerCamelCase : Optional[int] ): SCREAMING_SNAKE_CASE = tmp_path_factory.mktemp("data" ) / f"test.json.{extension}" SCREAMING_SNAKE_CASE = str(shared_datadir / f"test_file.json.{extension}" ) JsonDatasetWriter(__lowerCamelCase , __lowerCamelCase , compression=__lowerCamelCase ).write() with fsspec.open(__lowerCamelCase , "rb" , compression="infer" ) as f: SCREAMING_SNAKE_CASE = f.read() with fsspec.open(__lowerCamelCase , "rb" , compression="infer" ) as f: SCREAMING_SNAKE_CASE = f.read() assert exported_content == original_content
713
from collections import deque from .hash_table import HashTable class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' def __init__( self : Optional[int] , *__lowerCamelCase : List[Any] , **__lowerCamelCase : Optional[Any] ): super().__init__(*__lowerCamelCase , **__lowerCamelCase ) def _snake_case ( self : Any , __lowerCamelCase : Dict , __lowerCamelCase : Tuple ): SCREAMING_SNAKE_CASE = deque([] ) if self.values[key] is None else self.values[key] self.values[key].appendleft(__lowerCamelCase ) SCREAMING_SNAKE_CASE = self.values[key] def _snake_case ( self : Union[str, Any] ): return ( sum(self.charge_factor - len(__lowerCamelCase ) for slot in self.values ) / self.size_table * self.charge_factor ) def _snake_case ( self : Union[str, Any] , __lowerCamelCase : Dict , __lowerCamelCase : Tuple=None ): if not ( len(self.values[key] ) == self.charge_factor and self.values.count(__lowerCamelCase ) == 0 ): return key return super()._collision_resolution(__lowerCamelCase , __lowerCamelCase )
698
0
from ...configuration_utils import PretrainedConfig from ...utils import logging __A : Tuple = logging.get_logger(__name__) __A : Union[str, Any] = { 'google/canine-s': 'https://huggingface.co/google/canine-s/resolve/main/config.json', # See all CANINE models at https://huggingface.co/models?filter=canine } class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' lowerCamelCase__ = "canine" def __init__( self : Optional[int] , __lowerCamelCase : Union[str, Any]=768 , __lowerCamelCase : Union[str, Any]=12 , __lowerCamelCase : int=12 , __lowerCamelCase : int=3072 , __lowerCamelCase : Any="gelu" , __lowerCamelCase : Union[str, Any]=0.1 , __lowerCamelCase : Any=0.1 , __lowerCamelCase : Optional[Any]=16384 , __lowerCamelCase : Dict=16 , __lowerCamelCase : Optional[Any]=0.02 , __lowerCamelCase : int=1e-12 , __lowerCamelCase : Tuple=0 , __lowerCamelCase : Union[str, Any]=0xE_000 , __lowerCamelCase : str=0xE_001 , __lowerCamelCase : int=4 , __lowerCamelCase : Optional[Any]=4 , __lowerCamelCase : Optional[Any]=8 , __lowerCamelCase : int=16384 , __lowerCamelCase : Union[str, Any]=128 , **__lowerCamelCase : Tuple , ): super().__init__(pad_token_id=__lowerCamelCase , bos_token_id=__lowerCamelCase , eos_token_id=__lowerCamelCase , **__lowerCamelCase ) SCREAMING_SNAKE_CASE = max_position_embeddings SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = num_hidden_layers SCREAMING_SNAKE_CASE = num_attention_heads SCREAMING_SNAKE_CASE = intermediate_size SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = hidden_dropout_prob SCREAMING_SNAKE_CASE = attention_probs_dropout_prob SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = type_vocab_size SCREAMING_SNAKE_CASE = layer_norm_eps # Character config: SCREAMING_SNAKE_CASE = downsampling_rate SCREAMING_SNAKE_CASE = upsampling_kernel_size SCREAMING_SNAKE_CASE = num_hash_functions SCREAMING_SNAKE_CASE = num_hash_buckets SCREAMING_SNAKE_CASE = local_transformer_stride
714
from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast from ...utils import logging __A : Optional[int] = logging.get_logger(__name__) __A : int = { 'EleutherAI/gpt-neo-1.3B': 'https://huggingface.co/EleutherAI/gpt-neo-1.3B/resolve/main/config.json', # See all GPTNeo models at https://huggingface.co/models?filter=gpt_neo } class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' lowerCamelCase__ = "gpt_neo" lowerCamelCase__ = ["past_key_values"] lowerCamelCase__ = {"num_attention_heads": "num_heads", "num_hidden_layers": "num_layers"} def __init__( self : str , __lowerCamelCase : Dict=50257 , __lowerCamelCase : Tuple=2048 , __lowerCamelCase : Optional[Any]=2048 , __lowerCamelCase : int=24 , __lowerCamelCase : int=[[["global", "local"], 12]] , __lowerCamelCase : int=16 , __lowerCamelCase : List[str]=None , __lowerCamelCase : List[Any]=256 , __lowerCamelCase : Tuple="gelu_new" , __lowerCamelCase : Optional[Any]=0.0 , __lowerCamelCase : str=0.0 , __lowerCamelCase : List[Any]=0.0 , __lowerCamelCase : Optional[int]=0.1 , __lowerCamelCase : List[Any]=1e-5 , __lowerCamelCase : Dict=0.02 , __lowerCamelCase : Dict=True , __lowerCamelCase : Dict=50256 , __lowerCamelCase : Optional[int]=50256 , **__lowerCamelCase : Dict , ): SCREAMING_SNAKE_CASE = vocab_size SCREAMING_SNAKE_CASE = max_position_embeddings SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = num_layers SCREAMING_SNAKE_CASE = num_heads SCREAMING_SNAKE_CASE = intermediate_size SCREAMING_SNAKE_CASE = window_size SCREAMING_SNAKE_CASE = activation_function SCREAMING_SNAKE_CASE = resid_dropout SCREAMING_SNAKE_CASE = embed_dropout SCREAMING_SNAKE_CASE = attention_dropout SCREAMING_SNAKE_CASE = classifier_dropout SCREAMING_SNAKE_CASE = layer_norm_epsilon SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = use_cache SCREAMING_SNAKE_CASE = bos_token_id SCREAMING_SNAKE_CASE = eos_token_id SCREAMING_SNAKE_CASE = attention_types SCREAMING_SNAKE_CASE = self.expand_attention_types_params(__lowerCamelCase ) if len(self.attention_layers ) != self.num_layers: raise ValueError( "Configuration for convolutional module is incorrect. " "It is required that `len(config.attention_layers)` == `config.num_layers` " f"but is `len(config.attention_layers) = {len(self.attention_layers )}`, " f"`config.num_layers = {self.num_layers}`. " "`config.attention_layers` is prepared using `config.attention_types`. " "Please verify the value of `config.attention_types` argument." ) super().__init__(bos_token_id=__lowerCamelCase , eos_token_id=__lowerCamelCase , **__lowerCamelCase ) @staticmethod def _snake_case ( __lowerCamelCase : Dict ): SCREAMING_SNAKE_CASE = [] for item in attention_types: for _ in range(item[1] ): attentions.extend(item[0] ) return attentions def __a ( A__ : str , A__ : List[Any] , A__ : List[str] , A__ : Union[str, Any] ): import torch SCREAMING_SNAKE_CASE = input.size() SCREAMING_SNAKE_CASE = len(A__ ) SCREAMING_SNAKE_CASE = shape[dimension] SCREAMING_SNAKE_CASE = torch.arange(0 , A__ , A__ ) SCREAMING_SNAKE_CASE = torch.div(sizedim - size , A__ , rounding_mode="floor" ) + 1 SCREAMING_SNAKE_CASE = torch.arange(A__ ) + low_indices[:min_length][:, None] SCREAMING_SNAKE_CASE = [slice(A__ )] * rank SCREAMING_SNAKE_CASE = indices SCREAMING_SNAKE_CASE = input[s] SCREAMING_SNAKE_CASE = list(range(0 , rank + 1 ) ) perm.append(perm.pop(dimension + 1 ) ) return sliced.permute(A__ ) def __a ( A__ : Union[str, Any] , A__ : Optional[int] ): import torch SCREAMING_SNAKE_CASE = torch.arange(1 , A__ ) SCREAMING_SNAKE_CASE = torch.remainder(A__ , A__ ) SCREAMING_SNAKE_CASE = remainders == 0 SCREAMING_SNAKE_CASE = candidates[divisor_indices] SCREAMING_SNAKE_CASE = torch.max(A__ ) return largest_divisor, torch.div(A__ , A__ , rounding_mode="floor" ) class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' @property def _snake_case ( self : Any ): SCREAMING_SNAKE_CASE = OrderedDict({"input_ids": {0: "batch", 1: "sequence"}} ) if self.use_past: self.fill_with_past_key_values_(__lowerCamelCase , direction="inputs" ) SCREAMING_SNAKE_CASE = {0: "batch", 1: "past_sequence + sequence"} else: SCREAMING_SNAKE_CASE = {0: "batch", 1: "sequence"} return common_inputs @property def _snake_case ( self : Optional[int] ): return self._config.num_heads def _snake_case ( self : Union[str, Any] , __lowerCamelCase : PreTrainedTokenizer , __lowerCamelCase : int = -1 , __lowerCamelCase : int = -1 , __lowerCamelCase : bool = False , __lowerCamelCase : Optional[TensorType] = None , ): SCREAMING_SNAKE_CASE = super(__lowerCamelCase , self ).generate_dummy_inputs( __lowerCamelCase , batch_size=__lowerCamelCase , seq_length=__lowerCamelCase , is_pair=__lowerCamelCase , framework=__lowerCamelCase ) # We need to order the input in the way they appears in the forward() SCREAMING_SNAKE_CASE = OrderedDict({"input_ids": common_inputs["input_ids"]} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." ) else: import torch SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = common_inputs["input_ids"].shape # Not using the same length for past_key_values SCREAMING_SNAKE_CASE = seqlen + 2 SCREAMING_SNAKE_CASE = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) SCREAMING_SNAKE_CASE = [ (torch.zeros(__lowerCamelCase ), torch.zeros(__lowerCamelCase )) for _ in range(self.num_layers ) ] SCREAMING_SNAKE_CASE = common_inputs["attention_mask"] if self.use_past: SCREAMING_SNAKE_CASE = ordered_inputs["attention_mask"].dtype SCREAMING_SNAKE_CASE = torch.cat( [ordered_inputs["attention_mask"], torch.ones(__lowerCamelCase , __lowerCamelCase , dtype=__lowerCamelCase )] , dim=1 ) return ordered_inputs @property def _snake_case ( self : Optional[int] ): return 13
698
0
import numpy as np class _SCREAMING_SNAKE_CASE : '''simple docstring''' def __init__( self : List[Any] ): SCREAMING_SNAKE_CASE = (0, 0) SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 def __eq__( self : List[str] , __lowerCamelCase : str ): return self.position == cell.position def _snake_case ( self : Optional[int] ): print(self.position ) class _SCREAMING_SNAKE_CASE : '''simple docstring''' def __init__( self : Union[str, Any] , __lowerCamelCase : Tuple=(5, 5) ): SCREAMING_SNAKE_CASE = np.zeros(__lowerCamelCase ) SCREAMING_SNAKE_CASE = world_size[0] SCREAMING_SNAKE_CASE = world_size[1] def _snake_case ( self : Optional[int] ): print(self.w ) def _snake_case ( self : int , __lowerCamelCase : Tuple ): SCREAMING_SNAKE_CASE = [ (-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1), ] SCREAMING_SNAKE_CASE = cell.position[0] SCREAMING_SNAKE_CASE = cell.position[1] SCREAMING_SNAKE_CASE = [] for n in neughbour_cord: SCREAMING_SNAKE_CASE = current_x + n[0] SCREAMING_SNAKE_CASE = current_y + n[1] if 0 <= x < self.world_x_limit and 0 <= y < self.world_y_limit: SCREAMING_SNAKE_CASE = Cell() SCREAMING_SNAKE_CASE = (x, y) SCREAMING_SNAKE_CASE = cell neighbours.append(__lowerCamelCase ) return neighbours def __a ( A__ : Optional[int] , A__ : Union[str, Any] , A__ : int ): SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] _open.append(A__ ) while _open: SCREAMING_SNAKE_CASE = np.argmin([n.f for n in _open] ) SCREAMING_SNAKE_CASE = _open[min_f] _closed.append(_open.pop(A__ ) ) if current == goal: break for n in world.get_neigbours(A__ ): for c in _closed: if c == n: continue SCREAMING_SNAKE_CASE = current.g + 1 SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = n.position SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = goal.position SCREAMING_SNAKE_CASE = (ya - ya) ** 2 + (xa - xa) ** 2 SCREAMING_SNAKE_CASE = n.h + n.g for c in _open: if c == n and c.f < n.f: continue _open.append(A__ ) SCREAMING_SNAKE_CASE = [] while current.parent is not None: path.append(current.position ) SCREAMING_SNAKE_CASE = current.parent path.append(current.position ) return path[::-1] if __name__ == "__main__": __A : int = Gridworld() # Start position and goal __A : List[Any] = Cell() __A : Optional[int] = (0, 0) __A : List[Any] = Cell() __A : Optional[Any] = (4, 4) print(f'path from {start.position} to {goal.position}') __A : Optional[int] = astar(world, start, goal) # Just for visual reasons. for i in s: __A : Optional[int] = 1 print(world.w)
715
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available __A : Any = { 'configuration_longt5': ['LONGT5_PRETRAINED_CONFIG_ARCHIVE_MAP', 'LongT5Config', 'LongT5OnnxConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[str] = [ 'LONGT5_PRETRAINED_MODEL_ARCHIVE_LIST', 'LongT5EncoderModel', 'LongT5ForConditionalGeneration', 'LongT5Model', 'LongT5PreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[Any] = [ 'FlaxLongT5ForConditionalGeneration', 'FlaxLongT5Model', 'FlaxLongT5PreTrainedModel', ] if TYPE_CHECKING: from .configuration_longta import LONGT5_PRETRAINED_CONFIG_ARCHIVE_MAP, LongTaConfig, LongTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_longta import ( LONGT5_PRETRAINED_MODEL_ARCHIVE_LIST, LongTaEncoderModel, LongTaForConditionalGeneration, LongTaModel, LongTaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_longta import ( FlaxLongTaForConditionalGeneration, FlaxLongTaModel, FlaxLongTaPreTrainedModel, ) else: import sys __A : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
698
0
import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' lowerCamelCase__ = ["image_processor", "tokenizer"] lowerCamelCase__ = "ViTImageProcessor" lowerCamelCase__ = ("CLIPTokenizer", "CLIPTokenizerFast") def __init__( self : str , __lowerCamelCase : int=None , __lowerCamelCase : str=None , **__lowerCamelCase : Optional[Any] ): SCREAMING_SNAKE_CASE = None if "feature_extractor" in kwargs: warnings.warn( "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`" " instead." , __lowerCamelCase , ) SCREAMING_SNAKE_CASE = kwargs.pop("feature_extractor" ) SCREAMING_SNAKE_CASE = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("You need to specify an `image_processor`." ) if tokenizer is None: raise ValueError("You need to specify a `tokenizer`." ) super().__init__(__lowerCamelCase , __lowerCamelCase ) def __call__( self : Tuple , __lowerCamelCase : List[Any]=None , __lowerCamelCase : Optional[int]=None , __lowerCamelCase : int=None , __lowerCamelCase : Optional[Any]=None , **__lowerCamelCase : int ): if text is None and visual_prompt is None and images is None: raise ValueError("You have to specify either text, visual prompt or images." ) if text is not None and visual_prompt is not None: raise ValueError("You have to specify exactly one type of prompt. Either text or visual prompt." ) if text is not None: SCREAMING_SNAKE_CASE = self.tokenizer(__lowerCamelCase , return_tensors=__lowerCamelCase , **__lowerCamelCase ) if visual_prompt is not None: SCREAMING_SNAKE_CASE = self.image_processor(__lowerCamelCase , return_tensors=__lowerCamelCase , **__lowerCamelCase ) if images is not None: SCREAMING_SNAKE_CASE = self.image_processor(__lowerCamelCase , return_tensors=__lowerCamelCase , **__lowerCamelCase ) if visual_prompt is not None and images is not None: SCREAMING_SNAKE_CASE = { "pixel_values": image_features.pixel_values, "conditional_pixel_values": prompt_features.pixel_values, } return encoding elif text is not None and images is not None: SCREAMING_SNAKE_CASE = image_features.pixel_values return encoding elif text is not None: return encoding elif visual_prompt is not None: SCREAMING_SNAKE_CASE = { "conditional_pixel_values": prompt_features.pixel_values, } return encoding else: return BatchEncoding(data=dict(**__lowerCamelCase ) , tensor_type=__lowerCamelCase ) def _snake_case ( self : List[Any] , *__lowerCamelCase : List[str] , **__lowerCamelCase : Optional[Any] ): return self.tokenizer.batch_decode(*__lowerCamelCase , **__lowerCamelCase ) def _snake_case ( self : Dict , *__lowerCamelCase : int , **__lowerCamelCase : List[Any] ): return self.tokenizer.decode(*__lowerCamelCase , **__lowerCamelCase ) @property def _snake_case ( self : str ): warnings.warn( "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead." , __lowerCamelCase , ) return self.image_processor_class @property def _snake_case ( self : Dict ): warnings.warn( "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead." , __lowerCamelCase , ) return self.image_processor
716
import cmath import math def __a ( A__ : float , A__ : float , A__ : float , A__ : float ): SCREAMING_SNAKE_CASE = math.radians(A__ ) SCREAMING_SNAKE_CASE = math.radians(A__ ) # Convert voltage and current to rectangular form SCREAMING_SNAKE_CASE = cmath.rect(A__ , A__ ) SCREAMING_SNAKE_CASE = cmath.rect(A__ , A__ ) # Calculate apparent power return voltage_rect * current_rect if __name__ == "__main__": import doctest doctest.testmod()
698
0
from manim import * class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' def _snake_case ( self : List[Any] ): SCREAMING_SNAKE_CASE = Rectangle(height=0.5 , width=0.5 ) SCREAMING_SNAKE_CASE = Rectangle(height=0.46 , width=0.46 ).set_stroke(width=0 ) SCREAMING_SNAKE_CASE = [mem.copy() for i in range(6 )] SCREAMING_SNAKE_CASE = [mem.copy() for i in range(6 )] SCREAMING_SNAKE_CASE = VGroup(*__lowerCamelCase ).arrange(__lowerCamelCase , buff=0 ) SCREAMING_SNAKE_CASE = VGroup(*__lowerCamelCase ).arrange(__lowerCamelCase , buff=0 ) SCREAMING_SNAKE_CASE = VGroup(__lowerCamelCase , __lowerCamelCase ).arrange(__lowerCamelCase , buff=0 ) SCREAMING_SNAKE_CASE = Text("CPU" , font_size=24 ) SCREAMING_SNAKE_CASE = Group(__lowerCamelCase , __lowerCamelCase ).arrange(__lowerCamelCase , buff=0.5 , aligned_edge=__lowerCamelCase ) cpu.move_to([-2.5, -0.5, 0] ) self.add(__lowerCamelCase ) SCREAMING_SNAKE_CASE = [mem.copy() for i in range(4 )] SCREAMING_SNAKE_CASE = VGroup(*__lowerCamelCase ).arrange(__lowerCamelCase , buff=0 ) SCREAMING_SNAKE_CASE = Text("GPU" , font_size=24 ) SCREAMING_SNAKE_CASE = Group(__lowerCamelCase , __lowerCamelCase ).arrange(__lowerCamelCase , buff=0.5 , aligned_edge=__lowerCamelCase ) gpu.move_to([-1, -1, 0] ) self.add(__lowerCamelCase ) SCREAMING_SNAKE_CASE = [mem.copy() for i in range(6 )] SCREAMING_SNAKE_CASE = VGroup(*__lowerCamelCase ).arrange(__lowerCamelCase , buff=0 ) SCREAMING_SNAKE_CASE = Text("Model" , font_size=24 ) SCREAMING_SNAKE_CASE = Group(__lowerCamelCase , __lowerCamelCase ).arrange(__lowerCamelCase , buff=0.5 , aligned_edge=__lowerCamelCase ) model.move_to([3, -1.0, 0] ) self.add(__lowerCamelCase ) SCREAMING_SNAKE_CASE = [] for i, rect in enumerate(__lowerCamelCase ): rect.set_stroke(__lowerCamelCase ) # target = fill.copy().set_fill(YELLOW, opacity=0.7) # target.move_to(rect) # self.add(target) SCREAMING_SNAKE_CASE = Rectangle(height=0.46 / 4 , width=0.46 / 3 ).set_stroke(width=0.0 ).set_fill(__lowerCamelCase , opacity=0.7 ) if i == 0: cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT ) , buff=0.02 , direction=__lowerCamelCase ) cpu_target.set_x(cpu_target.get_x() + 0.1 ) elif i == 3: cpu_target.next_to(cpu_targs[0] , direction=__lowerCamelCase , buff=0.0 ) else: cpu_target.next_to(cpu_targs[i - 1] , direction=__lowerCamelCase , buff=0.0 ) self.add(__lowerCamelCase ) cpu_targs.append(__lowerCamelCase ) SCREAMING_SNAKE_CASE = [mem.copy() for i in range(6 )] SCREAMING_SNAKE_CASE = VGroup(*__lowerCamelCase ).arrange(__lowerCamelCase , buff=0 ) SCREAMING_SNAKE_CASE = Text("Loaded Checkpoint" , font_size=24 ) SCREAMING_SNAKE_CASE = Group(__lowerCamelCase , __lowerCamelCase ).arrange(__lowerCamelCase , aligned_edge=__lowerCamelCase , buff=0.4 ) checkpoint.move_to([3, 0.5, 0] ) SCREAMING_SNAKE_CASE = Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) SCREAMING_SNAKE_CASE = MarkupText( f"<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model" , font_size=18 , ) key_text.move_to([-5, 2.4, 0] ) self.add(__lowerCamelCase , __lowerCamelCase ) SCREAMING_SNAKE_CASE = MarkupText( f"<span fgcolor='{BLUE}'>●</span> Checkpoint" , font_size=18 , ) blue_text.next_to(__lowerCamelCase , DOWN * 2.4 , aligned_edge=key_text.get_left() ) SCREAMING_SNAKE_CASE = MarkupText( f"Next, a <i><span fgcolor=\"{BLUE}\">second</span></i> model is loaded into memory,\nwith the weights of a <span fgcolor=\"{BLUE}\">single shard</span>." , font_size=24 , ) step_a.move_to([2, 2, 0] ) self.play(Write(__lowerCamelCase ) , Write(__lowerCamelCase ) ) self.play(Write(__lowerCamelCase , run_time=1 ) , Create(__lowerCamelCase , run_time=1 ) ) SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for i, rect in enumerate(__lowerCamelCase ): SCREAMING_SNAKE_CASE = fill.copy().set_fill(__lowerCamelCase , opacity=0.7 ) target.move_to(__lowerCamelCase ) first_animations.append(GrowFromCenter(__lowerCamelCase , run_time=1 ) ) SCREAMING_SNAKE_CASE = target.copy() cpu_target.generate_target() if i < 5: cpu_target.target.move_to(cpu_left_col_base[i + 1] ) else: cpu_target.target.move_to(cpu_right_col_base[i - 5] ) second_animations.append(MoveToTarget(__lowerCamelCase , run_time=1.5 ) ) self.play(*__lowerCamelCase ) self.play(*__lowerCamelCase ) self.wait()
717
import argparse import json import os import torch from torch import nn from transformers import NllbMoeConfig, NllbMoeModel from transformers.modeling_utils import dtype_byte_size from transformers.utils import WEIGHTS_INDEX_NAME, WEIGHTS_NAME def __a ( A__ : List[str] ): SCREAMING_SNAKE_CASE = [ "encoder.version", "decoder.version", "model.encoder.version", "model.decoder.version", "decoder.output_projection.weight", "_float_tensor", "encoder.embed_positions._float_tensor", "decoder.embed_positions._float_tensor", ] for k in ignore_keys: state_dict.pop(A__ , A__ ) def __a ( A__ : Tuple ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = emb.weight.shape SCREAMING_SNAKE_CASE = nn.Linear(A__ , A__ , bias=A__ ) SCREAMING_SNAKE_CASE = emb.weight.data return lin_layer def __a ( A__ : Tuple , A__ : Union[str, Any]=None ): SCREAMING_SNAKE_CASE = {} for old_key in state_dict.keys(): SCREAMING_SNAKE_CASE = old_key if "moe_layer.experts." in key: if expert_idx is not None: SCREAMING_SNAKE_CASE = key.replace("moe_layer.experts.0" , F"ffn.experts.expert_{expert_idx}" ) else: SCREAMING_SNAKE_CASE = key.replace("moe_layer.experts." , "ffn.experts.expert_" ) if "gate" in key: SCREAMING_SNAKE_CASE = key.replace(".moe_layer.gate.wg" , ".ffn.router.classifier" ) if "fc2" and "experts" not in key: SCREAMING_SNAKE_CASE = key.replace(".fc2." , ".ffn.fc2." ) if "fc1" and "experts" not in key: SCREAMING_SNAKE_CASE = key.replace(".fc1." , ".ffn.fc1." ) if ".encoder_attn." in key: SCREAMING_SNAKE_CASE = key.replace(".encoder_attn." , ".cross_attention." ) if "encoder_attn_layer_norm" in key: SCREAMING_SNAKE_CASE = key.replace("encoder_attn_layer_norm" , "cross_attention_layer_norm" ) if "final_layer_norm" in key: SCREAMING_SNAKE_CASE = key.replace("final_layer_norm" , "ff_layer_norm" ) SCREAMING_SNAKE_CASE = state_dict[old_key] return new_dict def __a ( A__ : List[str] , A__ : List[Any] , A__ : str , A__ : Union[str, Any] , A__ : str = WEIGHTS_NAME ): SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = 0 os.makedirs(A__ , exist_ok=A__ ) for expert in range(A__ ): SCREAMING_SNAKE_CASE = switch_checkpoint_path + F"-rank-{expert}.pt" if os.path.isfile(A__ ): SCREAMING_SNAKE_CASE = torch.load(A__ )["model"] remove_ignore_keys_(A__ ) SCREAMING_SNAKE_CASE = rename_fairseq_keys(A__ , A__ ) SCREAMING_SNAKE_CASE = os.path.join( A__ , weights_name.replace(".bin" , F"-{len(A__ )+1:05d}-of-???.bin" ) ) torch.save(A__ , A__ ) sharded_state_dicts.append(expert_state.keys() ) total_size += sum([value.numel() for key, value in expert_state.items()] ) * dtype_byte_size( expert_state[list(A__ )[0]].dtype ) # Add the last block SCREAMING_SNAKE_CASE = os.path.join(A__ , weights_name.replace(".bin" , F"-{len(A__ )+1:05d}-of-???.bin" ) ) SCREAMING_SNAKE_CASE = torch.load(switch_checkpoint_path + "-shared.pt" )["model"] remove_ignore_keys_(A__ ) SCREAMING_SNAKE_CASE = rename_fairseq_keys(A__ , A__ ) SCREAMING_SNAKE_CASE = shared_weights["decoder.embed_tokens.weight"] sharded_state_dicts.append(shared_weights.keys() ) # If we only have the shared weights (dummy model/experts saved on the same file) if len(A__ ) == 1: SCREAMING_SNAKE_CASE = os.path.join(A__ , A__ ) torch.save(A__ , A__ ) return {weights_name: sharded_state_dicts[0]}, None else: torch.save(A__ , A__ ) # Otherwise, let's build the index SCREAMING_SNAKE_CASE = {} for idx, shard in enumerate(A__ ): SCREAMING_SNAKE_CASE = weights_name.replace(".bin" , F"-{idx+1:05d}-of-{len(A__ ):05d}.bin" ) SCREAMING_SNAKE_CASE = os.path.join(A__ , weights_name.replace(".bin" , F"-{idx+1:05d}-of-???.bin" ) ) os.rename(A__ , os.path.join(A__ , A__ ) ) for key in shard: SCREAMING_SNAKE_CASE = shard_file # Add the metadata SCREAMING_SNAKE_CASE = {"total_size": total_size} SCREAMING_SNAKE_CASE = {"metadata": metadata, "weight_map": weight_map} with open(os.path.join(A__ , A__ ) , "w" , encoding="utf-8" ) as f: SCREAMING_SNAKE_CASE = json.dumps(A__ , indent=2 , sort_keys=A__ ) + "\n" f.write(A__ ) return metadata, index if __name__ == "__main__": __A : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument( '--nllb_moe_checkpoint_path', default='/home/arthur_huggingface_co/fairseq/weights/checkpoints/model_moe_54b/checkpoint_2_300000', type=str, required=False, help='Path to a directory containing a folder per layer. Follows the original Google format.', ) parser.add_argument('--dtype', default='float32', type=str, required=False, help='dtype of the saved model') parser.add_argument( '--pytorch_dump_folder_path', default='/home/arthur_huggingface_co/fairseq/weights/checkpoints/hf-converted-moe-54b', type=str, required=False, help='Path to the output pytorch model.', ) __A : Optional[int] = parser.parse_args() __A , __A : Union[str, Any] = shard_on_the_fly( args.nllb_moe_checkpoint_path, args.pytorch_dump_folder_path, 1_2_8, args.dtype, ) __A : Any = NllbMoeConfig.from_pretrained( 'facebook/nllb-200-3.3B', encoder_sparse_step=4, decoder_sparse_step=4, num_experts=1_2_8 ) config.save_pretrained(args.pytorch_dump_folder_path) __A : Any = NllbMoeModel.from_pretrained(args.pytorch_dump_folder_path) print('Done') model.save_pretrained(args.pytorch_dump_folder_path)
698
0
from dataclasses import dataclass from typing import Optional, Tuple, Union import flax import jax.numpy as jnp from jax import random from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput from .scheduling_utils_flax import FlaxSchedulerMixin @flax.struct.dataclass class _SCREAMING_SNAKE_CASE : '''simple docstring''' lowerCamelCase__ = None lowerCamelCase__ = None lowerCamelCase__ = None # sigma(t_i) @classmethod def _snake_case ( cls : Tuple ): return cls() @dataclass class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' lowerCamelCase__ = 4_2 lowerCamelCase__ = 4_2 lowerCamelCase__ = 4_2 class _SCREAMING_SNAKE_CASE ( __snake_case , __snake_case ): '''simple docstring''' @property def _snake_case ( self : List[Any] ): return True @register_to_config def __init__( self : int , __lowerCamelCase : float = 0.02 , __lowerCamelCase : float = 100 , __lowerCamelCase : float = 1.007 , __lowerCamelCase : float = 80 , __lowerCamelCase : float = 0.05 , __lowerCamelCase : float = 50 , ): pass def _snake_case ( self : Optional[int] ): return KarrasVeSchedulerState.create() def _snake_case ( self : str , __lowerCamelCase : KarrasVeSchedulerState , __lowerCamelCase : int , __lowerCamelCase : Tuple = () ): SCREAMING_SNAKE_CASE = jnp.arange(0 , __lowerCamelCase )[::-1].copy() SCREAMING_SNAKE_CASE = [ ( self.config.sigma_max**2 * (self.config.sigma_min**2 / self.config.sigma_max**2) ** (i / (num_inference_steps - 1)) ) for i in timesteps ] return state.replace( num_inference_steps=__lowerCamelCase , schedule=jnp.array(__lowerCamelCase , dtype=jnp.floataa ) , timesteps=__lowerCamelCase , ) def _snake_case ( self : Dict , __lowerCamelCase : KarrasVeSchedulerState , __lowerCamelCase : jnp.ndarray , __lowerCamelCase : float , __lowerCamelCase : random.KeyArray , ): if self.config.s_min <= sigma <= self.config.s_max: SCREAMING_SNAKE_CASE = min(self.config.s_churn / state.num_inference_steps , 2**0.5 - 1 ) else: SCREAMING_SNAKE_CASE = 0 # sample eps ~ N(0, S_noise^2 * I) SCREAMING_SNAKE_CASE = random.split(__lowerCamelCase , num=1 ) SCREAMING_SNAKE_CASE = self.config.s_noise * random.normal(key=__lowerCamelCase , shape=sample.shape ) SCREAMING_SNAKE_CASE = sigma + gamma * sigma SCREAMING_SNAKE_CASE = sample + ((sigma_hat**2 - sigma**2) ** 0.5 * eps) return sample_hat, sigma_hat def _snake_case ( self : Dict , __lowerCamelCase : KarrasVeSchedulerState , __lowerCamelCase : jnp.ndarray , __lowerCamelCase : float , __lowerCamelCase : float , __lowerCamelCase : jnp.ndarray , __lowerCamelCase : bool = True , ): SCREAMING_SNAKE_CASE = sample_hat + sigma_hat * model_output SCREAMING_SNAKE_CASE = (sample_hat - pred_original_sample) / sigma_hat SCREAMING_SNAKE_CASE = sample_hat + (sigma_prev - sigma_hat) * derivative if not return_dict: return (sample_prev, derivative, state) return FlaxKarrasVeOutput(prev_sample=__lowerCamelCase , derivative=__lowerCamelCase , state=__lowerCamelCase ) def _snake_case ( self : Optional[Any] , __lowerCamelCase : KarrasVeSchedulerState , __lowerCamelCase : jnp.ndarray , __lowerCamelCase : float , __lowerCamelCase : float , __lowerCamelCase : jnp.ndarray , __lowerCamelCase : jnp.ndarray , __lowerCamelCase : jnp.ndarray , __lowerCamelCase : bool = True , ): SCREAMING_SNAKE_CASE = sample_prev + sigma_prev * model_output SCREAMING_SNAKE_CASE = (sample_prev - pred_original_sample) / sigma_prev SCREAMING_SNAKE_CASE = sample_hat + (sigma_prev - sigma_hat) * (0.5 * derivative + 0.5 * derivative_corr) if not return_dict: return (sample_prev, derivative, state) return FlaxKarrasVeOutput(prev_sample=__lowerCamelCase , derivative=__lowerCamelCase , state=__lowerCamelCase ) def _snake_case ( self : Dict , __lowerCamelCase : KarrasVeSchedulerState , __lowerCamelCase : List[str] , __lowerCamelCase : Optional[Any] , __lowerCamelCase : List[Any] ): raise NotImplementedError()
718
import shutil import tempfile import unittest import numpy as np import pytest from transformers.testing_utils import require_vision from transformers.utils import is_vision_available if is_vision_available(): from PIL import Image from transformers import AutoProcessor, BertTokenizer, BlipImageProcessor, BlipProcessor, PreTrainedTokenizerFast @require_vision class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' def _snake_case ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE = tempfile.mkdtemp() SCREAMING_SNAKE_CASE = BlipImageProcessor() SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained("hf-internal-testing/tiny-random-BertModel" ) SCREAMING_SNAKE_CASE = BlipProcessor(__lowerCamelCase , __lowerCamelCase ) processor.save_pretrained(self.tmpdirname ) def _snake_case ( self : Dict , **__lowerCamelCase : Any ): return AutoProcessor.from_pretrained(self.tmpdirname , **__lowerCamelCase ).tokenizer def _snake_case ( self : List[Any] , **__lowerCamelCase : Optional[Any] ): return AutoProcessor.from_pretrained(self.tmpdirname , **__lowerCamelCase ).image_processor def _snake_case ( self : Union[str, Any] ): shutil.rmtree(self.tmpdirname ) def _snake_case ( self : Tuple ): SCREAMING_SNAKE_CASE = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] SCREAMING_SNAKE_CASE = [Image.fromarray(np.moveaxis(__lowerCamelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def _snake_case ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE = BlipProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) SCREAMING_SNAKE_CASE = self.get_tokenizer(bos_token="(BOS)" , eos_token="(EOS)" ) SCREAMING_SNAKE_CASE = self.get_image_processor(do_normalize=__lowerCamelCase , padding_value=1.0 ) SCREAMING_SNAKE_CASE = BlipProcessor.from_pretrained( self.tmpdirname , bos_token="(BOS)" , eos_token="(EOS)" , do_normalize=__lowerCamelCase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __lowerCamelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __lowerCamelCase ) def _snake_case ( self : Optional[int] ): SCREAMING_SNAKE_CASE = self.get_image_processor() SCREAMING_SNAKE_CASE = self.get_tokenizer() SCREAMING_SNAKE_CASE = BlipProcessor(tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) SCREAMING_SNAKE_CASE = self.prepare_image_inputs() SCREAMING_SNAKE_CASE = image_processor(__lowerCamelCase , return_tensors="np" ) SCREAMING_SNAKE_CASE = processor(images=__lowerCamelCase , return_tensors="np" ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def _snake_case ( self : Dict ): SCREAMING_SNAKE_CASE = self.get_image_processor() SCREAMING_SNAKE_CASE = self.get_tokenizer() SCREAMING_SNAKE_CASE = BlipProcessor(tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) SCREAMING_SNAKE_CASE = "lower newer" SCREAMING_SNAKE_CASE = processor(text=__lowerCamelCase ) SCREAMING_SNAKE_CASE = tokenizer(__lowerCamelCase , return_token_type_ids=__lowerCamelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def _snake_case ( self : str ): SCREAMING_SNAKE_CASE = self.get_image_processor() SCREAMING_SNAKE_CASE = self.get_tokenizer() SCREAMING_SNAKE_CASE = BlipProcessor(tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) SCREAMING_SNAKE_CASE = "lower newer" SCREAMING_SNAKE_CASE = self.prepare_image_inputs() SCREAMING_SNAKE_CASE = processor(text=__lowerCamelCase , images=__lowerCamelCase ) self.assertListEqual(list(inputs.keys() ) , ["pixel_values", "input_ids", "attention_mask"] ) # test if it raises when no input is passed with pytest.raises(__lowerCamelCase ): processor() def _snake_case ( self : Any ): SCREAMING_SNAKE_CASE = self.get_image_processor() SCREAMING_SNAKE_CASE = self.get_tokenizer() SCREAMING_SNAKE_CASE = BlipProcessor(tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] SCREAMING_SNAKE_CASE = processor.batch_decode(__lowerCamelCase ) SCREAMING_SNAKE_CASE = tokenizer.batch_decode(__lowerCamelCase ) self.assertListEqual(__lowerCamelCase , __lowerCamelCase ) def _snake_case ( self : Dict ): SCREAMING_SNAKE_CASE = self.get_image_processor() SCREAMING_SNAKE_CASE = self.get_tokenizer() SCREAMING_SNAKE_CASE = BlipProcessor(tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) SCREAMING_SNAKE_CASE = "lower newer" SCREAMING_SNAKE_CASE = self.prepare_image_inputs() SCREAMING_SNAKE_CASE = processor(text=__lowerCamelCase , images=__lowerCamelCase ) # For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask'] self.assertListEqual(list(inputs.keys() ) , ["pixel_values", "input_ids", "attention_mask"] )
698
0
import math def __a ( A__ : list , A__ : int = 0 , A__ : int = 0 ): SCREAMING_SNAKE_CASE = end or len(A__ ) for i in range(A__ , A__ ): SCREAMING_SNAKE_CASE = i SCREAMING_SNAKE_CASE = array[i] while temp_index != start and temp_index_value < array[temp_index - 1]: SCREAMING_SNAKE_CASE = array[temp_index - 1] temp_index -= 1 SCREAMING_SNAKE_CASE = temp_index_value return array def __a ( A__ : list , A__ : int , A__ : int ): # Max Heap SCREAMING_SNAKE_CASE = index SCREAMING_SNAKE_CASE = 2 * index + 1 # Left Node SCREAMING_SNAKE_CASE = 2 * index + 2 # Right Node if left_index < heap_size and array[largest] < array[left_index]: SCREAMING_SNAKE_CASE = left_index if right_index < heap_size and array[largest] < array[right_index]: SCREAMING_SNAKE_CASE = right_index if largest != index: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = array[largest], array[index] heapify(A__ , A__ , A__ ) def __a ( A__ : list ): SCREAMING_SNAKE_CASE = len(A__ ) for i in range(n // 2 , -1 , -1 ): heapify(A__ , A__ , A__ ) for i in range(n - 1 , 0 , -1 ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = array[0], array[i] heapify(A__ , 0 , A__ ) return array def __a ( A__ : list , A__ : int , A__ : int , A__ : int ): if (array[first_index] > array[middle_index]) != ( array[first_index] > array[last_index] ): return array[first_index] elif (array[middle_index] > array[first_index]) != ( array[middle_index] > array[last_index] ): return array[middle_index] else: return array[last_index] def __a ( A__ : list , A__ : int , A__ : int , A__ : int ): SCREAMING_SNAKE_CASE = low SCREAMING_SNAKE_CASE = high while True: while array[i] < pivot: i += 1 j -= 1 while pivot < array[j]: j -= 1 if i >= j: return i SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = array[j], array[i] i += 1 def __a ( A__ : list ): if len(A__ ) == 0: return array SCREAMING_SNAKE_CASE = 2 * math.ceil(math.loga(len(A__ ) ) ) SCREAMING_SNAKE_CASE = 16 return intro_sort(A__ , 0 , len(A__ ) , A__ , A__ ) def __a ( A__ : list , A__ : int , A__ : int , A__ : int , A__ : int ): while end - start > size_threshold: if max_depth == 0: return heap_sort(A__ ) max_depth -= 1 SCREAMING_SNAKE_CASE = median_of_a(A__ , A__ , start + ((end - start) // 2) + 1 , end - 1 ) SCREAMING_SNAKE_CASE = partition(A__ , A__ , A__ , A__ ) intro_sort(A__ , A__ , A__ , A__ , A__ ) SCREAMING_SNAKE_CASE = p return insertion_sort(A__ , A__ , A__ ) if __name__ == "__main__": import doctest doctest.testmod() __A : Dict = input('Enter numbers separated by a comma : ').strip() __A : Any = [float(item) for item in user_input.split(',')] print(sort(unsorted))
719
import unittest from transformers import MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING, AutoTokenizer, is_vision_available from transformers.pipelines import pipeline from transformers.pipelines.document_question_answering import apply_tesseract from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_detectrona, require_pytesseract, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image from transformers.image_utils import load_image else: class _SCREAMING_SNAKE_CASE : '''simple docstring''' @staticmethod def _snake_case ( *__lowerCamelCase : Optional[Any] , **__lowerCamelCase : Union[str, Any] ): pass def __a ( A__ : str ): return None # This is a pinned image from a specific revision of a document question answering space, hosted by HuggingFace, # so we can expect it to be available. __A : Tuple = ( 'https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png' ) @is_pipeline_test @require_torch @require_vision class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING @require_pytesseract @require_vision def _snake_case ( self : Optional[Any] , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : Dict , __lowerCamelCase : Optional[Any] ): SCREAMING_SNAKE_CASE = pipeline( "document-question-answering" , model=__lowerCamelCase , tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = list(zip(*apply_tesseract(load_image(__lowerCamelCase ) , __lowerCamelCase , "" ) ) ) SCREAMING_SNAKE_CASE = "What is the placebo?" SCREAMING_SNAKE_CASE = [ { "image": load_image(__lowerCamelCase ), "question": question, }, { "image": image, "question": question, }, { "image": image, "question": question, "word_boxes": word_boxes, }, ] return dqa_pipeline, examples def _snake_case ( self : List[Any] , __lowerCamelCase : int , __lowerCamelCase : int ): SCREAMING_SNAKE_CASE = dqa_pipeline(__lowerCamelCase , top_k=2 ) self.assertEqual( __lowerCamelCase , [ [ {"score": ANY(__lowerCamelCase ), "answer": ANY(__lowerCamelCase ), "start": ANY(__lowerCamelCase ), "end": ANY(__lowerCamelCase )}, {"score": ANY(__lowerCamelCase ), "answer": ANY(__lowerCamelCase ), "start": ANY(__lowerCamelCase ), "end": ANY(__lowerCamelCase )}, ] ] * 3 , ) @require_torch @require_detectrona @require_pytesseract def _snake_case ( self : Optional[int] ): SCREAMING_SNAKE_CASE = pipeline("document-question-answering" , model="hf-internal-testing/tiny-random-layoutlmv2" ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = "How many cats are there?" SCREAMING_SNAKE_CASE = [ {"score": 0.0_001, "answer": "oy 2312/2019", "start": 38, "end": 39}, {"score": 0.0_001, "answer": "oy 2312/2019 DUE", "start": 38, "end": 40}, ] SCREAMING_SNAKE_CASE = dqa_pipeline(image=__lowerCamelCase , question=__lowerCamelCase , top_k=2 ) self.assertEqual(nested_simplify(__lowerCamelCase , decimals=4 ) , __lowerCamelCase ) SCREAMING_SNAKE_CASE = dqa_pipeline({"image": image, "question": question} , top_k=2 ) self.assertEqual(nested_simplify(__lowerCamelCase , decimals=4 ) , __lowerCamelCase ) # This image does not detect ANY text in it, meaning layoutlmv2 should fail. # Empty answer probably SCREAMING_SNAKE_CASE = "./tests/fixtures/tests_samples/COCO/000000039769.png" SCREAMING_SNAKE_CASE = dqa_pipeline(image=__lowerCamelCase , question=__lowerCamelCase , top_k=2 ) self.assertEqual(__lowerCamelCase , [] ) # We can optionnally pass directly the words and bounding boxes SCREAMING_SNAKE_CASE = "./tests/fixtures/tests_samples/COCO/000000039769.png" SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = dqa_pipeline(image=__lowerCamelCase , question=__lowerCamelCase , words=__lowerCamelCase , boxes=__lowerCamelCase , top_k=2 ) self.assertEqual(__lowerCamelCase , [] ) @slow @require_torch @require_detectrona @require_pytesseract def _snake_case ( self : List[str] ): SCREAMING_SNAKE_CASE = pipeline( "document-question-answering" , model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa" , revision="9977165" , ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = "What is the invoice number?" SCREAMING_SNAKE_CASE = dqa_pipeline(image=__lowerCamelCase , question=__lowerCamelCase , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.9_944, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_009, "answer": "us-001", "start": 16, "end": 16}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline({"image": image, "question": question} , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.9_944, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_009, "answer": "us-001", "start": 16, "end": 16}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline( [{"image": image, "question": question}, {"image": image, "question": question}] , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ [ {"score": 0.9_944, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_009, "answer": "us-001", "start": 16, "end": 16}, ], ] * 2 , ) @slow @require_torch @require_detectrona @require_pytesseract def _snake_case ( self : str ): SCREAMING_SNAKE_CASE = pipeline( "document-question-answering" , model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa" , revision="9977165" , max_seq_len=50 , ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = "What is the invoice number?" SCREAMING_SNAKE_CASE = dqa_pipeline(image=__lowerCamelCase , question=__lowerCamelCase , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.9_974, "answer": "1110212019", "start": 23, "end": 23}, {"score": 0.9_948, "answer": "us-001", "start": 16, "end": 16}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline({"image": image, "question": question} , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.9_974, "answer": "1110212019", "start": 23, "end": 23}, {"score": 0.9_948, "answer": "us-001", "start": 16, "end": 16}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline( [{"image": image, "question": question}, {"image": image, "question": question}] , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ [ {"score": 0.9_974, "answer": "1110212019", "start": 23, "end": 23}, {"score": 0.9_948, "answer": "us-001", "start": 16, "end": 16}, ] ] * 2 , ) @slow @require_torch @require_pytesseract @require_vision def _snake_case ( self : str ): SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained( "impira/layoutlm-document-qa" , revision="3dc6de3" , add_prefix_space=__lowerCamelCase ) SCREAMING_SNAKE_CASE = pipeline( "document-question-answering" , model="impira/layoutlm-document-qa" , tokenizer=__lowerCamelCase , revision="3dc6de3" , ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = "What is the invoice number?" SCREAMING_SNAKE_CASE = dqa_pipeline(image=__lowerCamelCase , question=__lowerCamelCase , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.4_251, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_819, "answer": "1110212019", "start": 23, "end": 23}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline({"image": image, "question": question} , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.4_251, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_819, "answer": "1110212019", "start": 23, "end": 23}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline( [{"image": image, "question": question}, {"image": image, "question": question}] , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ [ {"score": 0.4_251, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_819, "answer": "1110212019", "start": 23, "end": 23}, ] ] * 2 , ) SCREAMING_SNAKE_CASE = list(zip(*apply_tesseract(load_image(__lowerCamelCase ) , __lowerCamelCase , "" ) ) ) # This model should also work if `image` is set to None SCREAMING_SNAKE_CASE = dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question} , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.4_251, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.0_819, "answer": "1110212019", "start": 23, "end": 23}, ] , ) @slow @require_torch @require_pytesseract @require_vision def _snake_case ( self : Dict ): SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained( "impira/layoutlm-document-qa" , revision="3dc6de3" , add_prefix_space=__lowerCamelCase ) SCREAMING_SNAKE_CASE = pipeline( "document-question-answering" , model="impira/layoutlm-document-qa" , tokenizer=__lowerCamelCase , revision="3dc6de3" , max_seq_len=50 , ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = "What is the invoice number?" SCREAMING_SNAKE_CASE = dqa_pipeline(image=__lowerCamelCase , question=__lowerCamelCase , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.9_999, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.9_998, "answer": "us-001", "start": 16, "end": 16}, ] , ) SCREAMING_SNAKE_CASE = dqa_pipeline( [{"image": image, "question": question}, {"image": image, "question": question}] , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ [ {"score": 0.9_999, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.9_998, "answer": "us-001", "start": 16, "end": 16}, ] ] * 2 , ) SCREAMING_SNAKE_CASE = list(zip(*apply_tesseract(load_image(__lowerCamelCase ) , __lowerCamelCase , "" ) ) ) # This model should also work if `image` is set to None SCREAMING_SNAKE_CASE = dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question} , top_k=2 ) self.assertEqual( nested_simplify(__lowerCamelCase , decimals=4 ) , [ {"score": 0.9_999, "answer": "us-001", "start": 16, "end": 16}, {"score": 0.9_998, "answer": "us-001", "start": 16, "end": 16}, ] , ) @slow @require_torch def _snake_case ( self : List[str] ): SCREAMING_SNAKE_CASE = pipeline( "document-question-answering" , model="naver-clova-ix/donut-base-finetuned-docvqa" , tokenizer=AutoTokenizer.from_pretrained("naver-clova-ix/donut-base-finetuned-docvqa" ) , feature_extractor="naver-clova-ix/donut-base-finetuned-docvqa" , ) SCREAMING_SNAKE_CASE = INVOICE_URL SCREAMING_SNAKE_CASE = "What is the invoice number?" SCREAMING_SNAKE_CASE = dqa_pipeline(image=__lowerCamelCase , question=__lowerCamelCase , top_k=2 ) self.assertEqual(nested_simplify(__lowerCamelCase , decimals=4 ) , [{"answer": "us-001"}] ) @require_tf @unittest.skip("Document question answering not implemented in TF" ) def _snake_case ( self : List[Any] ): pass
698
0
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') __A : Tuple = logging.getLogger(__name__) @dataclass class _SCREAMING_SNAKE_CASE : '''simple docstring''' lowerCamelCase__ = field( default="tab_fact" , metadata={"help": "The name of the dataset to use (via the datasets library)."} ) lowerCamelCase__ = field( default="tab_fact" , metadata={"help": "The configuration name of the dataset to use (via the datasets library)."} , ) lowerCamelCase__ = 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." ) } , ) lowerCamelCase__ = field( default=__snake_case , metadata={"help": "Overwrite the cached preprocessed datasets or not."} ) lowerCamelCase__ = field( default=__snake_case , 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." ) } , ) lowerCamelCase__ = field( default=__snake_case , metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of training examples to this " "value if set." ) } , ) lowerCamelCase__ = field( default=__snake_case , metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of evaluation examples to this " "value if set." ) } , ) lowerCamelCase__ = field( default=__snake_case , metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of prediction examples to this " "value if set." ) } , ) lowerCamelCase__ = field( default=__snake_case , metadata={"help": "A csv or a json file containing the training data."} ) lowerCamelCase__ = field( default=__snake_case , metadata={"help": "A csv or a json file containing the validation data."} ) lowerCamelCase__ = field(default=__snake_case , metadata={"help": "A csv or a json file containing the test data."} ) def _snake_case ( self : List[str] ): if self.dataset_name is not None: pass elif self.train_file is None or self.validation_file is None: raise ValueError("Need either a GLUE task, a training/validation file or a dataset name." ) else: SCREAMING_SNAKE_CASE = self.train_file.split("." )[-1] assert train_extension in ["csv", "json"], "`train_file` should be a csv or a json file." SCREAMING_SNAKE_CASE = 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 _SCREAMING_SNAKE_CASE : '''simple docstring''' lowerCamelCase__ = field( default=__snake_case , metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} ) lowerCamelCase__ = field( default=__snake_case , metadata={"help": "Pretrained config name or path if not the same as model_name"} ) lowerCamelCase__ = field( default=__snake_case , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"} ) lowerCamelCase__ = field( default=__snake_case , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) lowerCamelCase__ = field( default=__snake_case , metadata={"help": "Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."} , ) lowerCamelCase__ = field( default="main" , metadata={"help": "The specific model version to use (can be a branch name, tag name or commit id)."} , ) lowerCamelCase__ = field( default=__snake_case , metadata={ "help": ( "Will use the token generated when running `huggingface-cli login` (necessary to use this script " "with private models)." ) } , ) def __a ( ): # 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. SCREAMING_SNAKE_CASE = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(".json" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = parser.parse_args_into_dataclasses() # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , handlers=[logging.StreamHandler(sys.stdout )] , ) SCREAMING_SNAKE_CASE = training_args.get_process_log_level() logger.setLevel(A__ ) datasets.utils.logging.set_verbosity(A__ ) transformers.utils.logging.set_verbosity(A__ ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}" + F"distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}" ) logger.info(F"Training/evaluation parameters {training_args}" ) # Detecting last checkpoint. SCREAMING_SNAKE_CASE = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: SCREAMING_SNAKE_CASE = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F"Output directory ({training_args.output_dir}) already exists and is not empty. " "Use --overwrite_output_dir to overcome." ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change " "the `--output_dir` or add `--overwrite_output_dir` to train from scratch." ) # Set seed before initializing model. set_seed(training_args.seed ) # Get the datasets: you can either provide your own CSV/JSON training and evaluation files (see below) # or specify a GLUE benchmark task (the dataset will be downloaded automatically from the datasets Hub). # # For JSON files, this script will use the `question` column for the input question and `table` column for the corresponding table. # # If the CSVs/JSONs contain only one non-label column, the script does single sentence classification on this # single column. You can easily tweak this behavior (see below) # # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.dataset_name is not None: # Downloading and loading a dataset from the hub. SCREAMING_SNAKE_CASE = load_dataset( data_args.dataset_name , data_args.dataset_config_name , cache_dir=model_args.cache_dir ) else: # Loading a dataset from your local files. # CSV/JSON training and evaluation files are needed. SCREAMING_SNAKE_CASE = {"train": data_args.train_file, "validation": data_args.validation_file} # Get the test dataset: you can provide your own CSV/JSON test file (see below) # when you use `do_predict` without specifying a GLUE benchmark task. if training_args.do_predict: if data_args.test_file is not None: SCREAMING_SNAKE_CASE = data_args.train_file.split("." )[-1] SCREAMING_SNAKE_CASE = data_args.test_file.split("." )[-1] assert ( test_extension == train_extension ), "`test_file` should have the same extension (csv or json) as `train_file`." SCREAMING_SNAKE_CASE = data_args.test_file else: raise ValueError("Need either a GLUE task or a test file for `do_predict`." ) for key in data_files.keys(): logger.info(F"load a local file for {key}: {data_files[key]}" ) if data_args.train_file.endswith(".csv" ): # Loading a dataset from local csv files SCREAMING_SNAKE_CASE = load_dataset("csv" , data_files=A__ , cache_dir=model_args.cache_dir ) else: # Loading a dataset from local json files SCREAMING_SNAKE_CASE = load_dataset("json" , data_files=A__ , cache_dir=model_args.cache_dir ) # See more about loading any type of standard or custom dataset at # https://huggingface.co/docs/datasets/loading_datasets.html. # Labels SCREAMING_SNAKE_CASE = raw_datasets["train"].features["label"].names SCREAMING_SNAKE_CASE = len(A__ ) # Load pretrained model and tokenizer # # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. SCREAMING_SNAKE_CASE = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=A__ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # load tapex tokenizer SCREAMING_SNAKE_CASE = TapexTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , use_fast=model_args.use_fast_tokenizer , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , add_prefix_space=A__ , ) SCREAMING_SNAKE_CASE = BartForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool(".ckpt" in model_args.model_name_or_path ) , config=A__ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # Padding strategy if data_args.pad_to_max_length: SCREAMING_SNAKE_CASE = "max_length" else: # We will pad later, dynamically at batch creation, to the max sequence length in each batch SCREAMING_SNAKE_CASE = False # Some models have set the order of the labels to use, so let's make sure we do use it. SCREAMING_SNAKE_CASE = {"Refused": 0, "Entailed": 1} SCREAMING_SNAKE_CASE = {0: "Refused", 1: "Entailed"} if data_args.max_seq_length > tokenizer.model_max_length: logger.warning( F"The max_seq_length passed ({data_args.max_seq_length}) is larger than the maximum length for the" F"model ({tokenizer.model_max_length}). Using max_seq_length={tokenizer.model_max_length}." ) SCREAMING_SNAKE_CASE = min(data_args.max_seq_length , tokenizer.model_max_length ) def preprocess_tabfact_function(A__ : List[str] ): # Tokenize the texts def _convert_table_text_to_pandas(A__ : List[Any] ): SCREAMING_SNAKE_CASE = [_table_row.split("#" ) for _table_row in _table_text.strip("\n" ).split("\n" )] SCREAMING_SNAKE_CASE = pd.DataFrame.from_records(_table_content[1:] , columns=_table_content[0] ) return _table_pd SCREAMING_SNAKE_CASE = examples["statement"] SCREAMING_SNAKE_CASE = list(map(_convert_table_text_to_pandas , examples["table_text"] ) ) SCREAMING_SNAKE_CASE = tokenizer(A__ , A__ , padding=A__ , max_length=A__ , truncation=A__ ) SCREAMING_SNAKE_CASE = examples["label"] return result with training_args.main_process_first(desc="dataset map pre-processing" ): SCREAMING_SNAKE_CASE = raw_datasets.map( A__ , batched=A__ , load_from_cache_file=not data_args.overwrite_cache , desc="Running tokenizer on dataset" , ) if training_args.do_train: if "train" not in raw_datasets: raise ValueError("--do_train requires a train dataset" ) SCREAMING_SNAKE_CASE = raw_datasets["train"] if data_args.max_train_samples is not None: SCREAMING_SNAKE_CASE = train_dataset.select(range(data_args.max_train_samples ) ) if training_args.do_eval: if "validation" not in raw_datasets and "validation_matched" not in raw_datasets: raise ValueError("--do_eval requires a validation dataset" ) SCREAMING_SNAKE_CASE = raw_datasets["validation"] if data_args.max_eval_samples is not None: SCREAMING_SNAKE_CASE = eval_dataset.select(range(data_args.max_eval_samples ) ) if training_args.do_predict or data_args.test_file is not None: if "test" not in raw_datasets and "test_matched" not in raw_datasets: raise ValueError("--do_predict requires a test dataset" ) SCREAMING_SNAKE_CASE = raw_datasets["test"] if data_args.max_predict_samples is not None: SCREAMING_SNAKE_CASE = predict_dataset.select(range(data_args.max_predict_samples ) ) # Log a few random samples from the training set: if training_args.do_train: for index in random.sample(range(len(A__ ) ) , 3 ): logger.info(F"Sample {index} of the training set: {train_dataset[index]}." ) # You can define your custom compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a # predictions and label_ids field) and has to return a dictionary string to float. def compute_metrics(A__ : EvalPrediction ): SCREAMING_SNAKE_CASE = p.predictions[0] if isinstance(p.predictions , A__ ) else p.predictions SCREAMING_SNAKE_CASE = np.argmax(A__ , axis=1 ) return {"accuracy": (preds == p.label_ids).astype(np.floataa ).mean().item()} # Data collator will default to DataCollatorWithPadding, so we change it if we already did the padding. if data_args.pad_to_max_length: SCREAMING_SNAKE_CASE = default_data_collator elif training_args.fpaa: SCREAMING_SNAKE_CASE = DataCollatorWithPadding(A__ , pad_to_multiple_of=8 ) else: SCREAMING_SNAKE_CASE = None # Initialize our Trainer SCREAMING_SNAKE_CASE = Trainer( model=A__ , args=A__ , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , compute_metrics=A__ , tokenizer=A__ , data_collator=A__ , ) # Training if training_args.do_train: SCREAMING_SNAKE_CASE = None if training_args.resume_from_checkpoint is not None: SCREAMING_SNAKE_CASE = training_args.resume_from_checkpoint elif last_checkpoint is not None: SCREAMING_SNAKE_CASE = last_checkpoint SCREAMING_SNAKE_CASE = trainer.train(resume_from_checkpoint=A__ ) SCREAMING_SNAKE_CASE = train_result.metrics SCREAMING_SNAKE_CASE = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(A__ ) ) SCREAMING_SNAKE_CASE = min(A__ , len(A__ ) ) trainer.save_model() # Saves the tokenizer too for easy upload trainer.log_metrics("train" , A__ ) trainer.save_metrics("train" , A__ ) trainer.save_state() # Evaluation if training_args.do_eval: logger.info("*** Evaluate ***" ) SCREAMING_SNAKE_CASE = trainer.evaluate(eval_dataset=A__ ) SCREAMING_SNAKE_CASE = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(A__ ) SCREAMING_SNAKE_CASE = min(A__ , len(A__ ) ) trainer.log_metrics("eval" , A__ ) trainer.save_metrics("eval" , A__ ) if training_args.do_predict: logger.info("*** Predict ***" ) # Removing the `label` columns because it contains -1 and Trainer won't like that. SCREAMING_SNAKE_CASE = predict_dataset.remove_columns("label" ) SCREAMING_SNAKE_CASE = trainer.predict(A__ , metric_key_prefix="predict" ).predictions SCREAMING_SNAKE_CASE = np.argmax(A__ , axis=1 ) SCREAMING_SNAKE_CASE = os.path.join(training_args.output_dir , "predict_results_tabfact.txt" ) if trainer.is_world_process_zero(): with open(A__ , "w" ) as writer: logger.info("***** Predict Results *****" ) writer.write("index\tprediction\n" ) for index, item in enumerate(A__ ): SCREAMING_SNAKE_CASE = label_list[item] writer.write(F"{index}\t{item}\n" ) SCREAMING_SNAKE_CASE = {"finetuned_from": model_args.model_name_or_path, "tasks": "text-classification"} if training_args.push_to_hub: trainer.push_to_hub(**A__ ) else: trainer.create_model_card(**A__ ) def __a ( A__ : Dict ): # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
720
# 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 torch from ..models.auto import AutoModelForSequenceClassification, AutoTokenizer from .base import PipelineTool class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' lowerCamelCase__ = "facebook/bart-large-mnli" lowerCamelCase__ = ( "This is a tool that classifies an English text using provided labels. It takes two inputs: `text`, which " "should be the text to classify, and `labels`, which should be the list of labels to use for classification. " "It returns the most likely label in the list of provided `labels` for the input text." ) lowerCamelCase__ = "text_classifier" lowerCamelCase__ = AutoTokenizer lowerCamelCase__ = AutoModelForSequenceClassification lowerCamelCase__ = ["text", ["text"]] lowerCamelCase__ = ["text"] def _snake_case ( self : Optional[Any] ): super().setup() SCREAMING_SNAKE_CASE = self.model.config SCREAMING_SNAKE_CASE = -1 for idx, label in config.idalabel.items(): if label.lower().startswith("entail" ): SCREAMING_SNAKE_CASE = int(__lowerCamelCase ) if self.entailment_id == -1: raise ValueError("Could not determine the entailment ID from the model config, please pass it at init." ) def _snake_case ( self : List[Any] , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : List[Any] ): SCREAMING_SNAKE_CASE = labels return self.pre_processor( [text] * len(__lowerCamelCase ) , [f"This example is {label}" for label in labels] , return_tensors="pt" , padding="max_length" , ) def _snake_case ( self : str , __lowerCamelCase : Optional[int] ): SCREAMING_SNAKE_CASE = outputs.logits SCREAMING_SNAKE_CASE = torch.argmax(logits[:, 2] ).item() return self._labels[label_id]
698
0
import warnings from ...utils import is_sklearn_available, requires_backends if is_sklearn_available(): from scipy.stats import pearsonr, spearmanr from sklearn.metrics import fa_score, matthews_corrcoef __A : List[Any] = ( 'This metric will be removed from the library soon, metrics should be handled with the 🤗 Evaluate ' 'library. You can have a look at this example script for pointers: ' 'https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py' ) def __a ( A__ : Any , A__ : Union[str, Any] ): '''simple docstring''' warnings.warn(A__ , A__ ) requires_backends(A__ , "sklearn" ) return (preds == labels).mean() def __a ( A__ : List[str] , A__ : Tuple ): '''simple docstring''' warnings.warn(A__ , A__ ) requires_backends(A__ , "sklearn" ) SCREAMING_SNAKE_CASE = simple_accuracy(A__ , A__ ) SCREAMING_SNAKE_CASE = fa_score(y_true=A__ , y_pred=A__ ) return { "acc": acc, "f1": fa, "acc_and_f1": (acc + fa) / 2, } def __a ( A__ : List[Any] , A__ : List[str] ): '''simple docstring''' warnings.warn(A__ , A__ ) requires_backends(A__ , "sklearn" ) SCREAMING_SNAKE_CASE = pearsonr(A__ , A__ )[0] SCREAMING_SNAKE_CASE = spearmanr(A__ , A__ )[0] return { "pearson": pearson_corr, "spearmanr": spearman_corr, "corr": (pearson_corr + spearman_corr) / 2, } def __a ( A__ : Union[str, Any] , A__ : List[str] , A__ : Optional[Any] ): '''simple docstring''' warnings.warn(A__ , A__ ) requires_backends(A__ , "sklearn" ) assert len(A__ ) == len(A__ ), F"Predictions and labels have mismatched lengths {len(A__ )} and {len(A__ )}" if task_name == "cola": return {"mcc": matthews_corrcoef(A__ , A__ )} elif task_name == "sst-2": return {"acc": simple_accuracy(A__ , A__ )} elif task_name == "mrpc": return acc_and_fa(A__ , A__ ) elif task_name == "sts-b": return pearson_and_spearman(A__ , A__ ) elif task_name == "qqp": return acc_and_fa(A__ , A__ ) elif task_name == "mnli": return {"mnli/acc": simple_accuracy(A__ , A__ )} elif task_name == "mnli-mm": return {"mnli-mm/acc": simple_accuracy(A__ , A__ )} elif task_name == "qnli": return {"acc": simple_accuracy(A__ , A__ )} elif task_name == "rte": return {"acc": simple_accuracy(A__ , A__ )} elif task_name == "wnli": return {"acc": simple_accuracy(A__ , A__ )} elif task_name == "hans": return {"acc": simple_accuracy(A__ , A__ )} else: raise KeyError(A__ ) def __a ( A__ : Tuple , A__ : Union[str, Any] , A__ : Dict ): '''simple docstring''' warnings.warn(A__ , A__ ) requires_backends(A__ , "sklearn" ) if len(A__ ) != len(A__ ): raise ValueError(F"Predictions and labels have mismatched lengths {len(A__ )} and {len(A__ )}" ) if task_name == "xnli": return {"acc": simple_accuracy(A__ , A__ )} else: raise KeyError(A__ )
721
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os from accelerate.test_utils import execute_subprocess_async def __a ( A__ : str=None ): if subparsers is not None: SCREAMING_SNAKE_CASE = subparsers.add_parser("test" ) else: SCREAMING_SNAKE_CASE = argparse.ArgumentParser("Accelerate test command" ) parser.add_argument( "--config_file" , default=A__ , help=( "The path to use to store the config file. Will default to a file named default_config.yaml in the cache " "location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have " "such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed " "with 'huggingface'." ) , ) if subparsers is not None: parser.set_defaults(func=A__ ) return parser def __a ( A__ : Tuple ): SCREAMING_SNAKE_CASE = os.path.sep.join(__file__.split(os.path.sep )[:-2] + ["test_utils", "scripts", "test_script.py"] ) if args.config_file is None: SCREAMING_SNAKE_CASE = script_name else: SCREAMING_SNAKE_CASE = F"--config_file={args.config_file} {script_name}" SCREAMING_SNAKE_CASE = ["accelerate-launch"] + test_args.split() SCREAMING_SNAKE_CASE = execute_subprocess_async(A__ , env=os.environ.copy() ) if result.returncode == 0: print("Test is a success! You are ready for your distributed training!" ) def __a ( ): SCREAMING_SNAKE_CASE = test_command_parser() SCREAMING_SNAKE_CASE = parser.parse_args() test_command(A__ ) if __name__ == "__main__": main()
698
0
import datasets lowerCAmelCase__ : str = '''\ @InProceedings{conneau2018xnli, author = "Conneau, Alexis and Rinott, Ruty and Lample, Guillaume and Williams, Adina and Bowman, Samuel R. and Schwenk, Holger and Stoyanov, Veselin", title = "XNLI: Evaluating Cross-lingual Sentence Representations", booktitle = "Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing", year = "2018", publisher = "Association for Computational Linguistics", location = "Brussels, Belgium", } ''' lowerCAmelCase__ : Dict = '''\ XNLI is a subset of a few thousand examples from MNLI which has been translated into a 14 different languages (some low-ish resource). As with MNLI, the goal is to predict textual entailment (does sentence A imply/contradict/neither sentence B) and is a classification task (given two sentences, predict one of three labels). ''' lowerCAmelCase__ : int = ''' Computes XNLI score which is just simple accuracy. Args: predictions: Predicted labels. references: Ground truth labels. Returns: \'accuracy\': accuracy Examples: >>> predictions = [0, 1] >>> references = [0, 1] >>> xnli_metric = datasets.load_metric("xnli") >>> results = xnli_metric.compute(predictions=predictions, references=references) >>> print(results) {\'accuracy\': 1.0} ''' def UpperCamelCase__ ( A__ , A__ ) -> Union[str, Any]: return (preds == labels).mean() @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION ,_KWARGS_DESCRIPTION ) class __snake_case ( datasets.Metric ): def __a ( self ) -> Dict: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('int64' if self.config_name != 'sts-b' else 'float32' ), 'references': datasets.Value('int64' if self.config_name != 'sts-b' else 'float32' ), } ) , codebase_urls=[] , reference_urls=[] , format='numpy' , ) def __a ( self , __UpperCamelCase , __UpperCamelCase ) -> List[Any]: '''simple docstring''' return {"accuracy": simple_accuracy(__UpperCamelCase , __UpperCamelCase )}
699
from __future__ import annotations from collections.abc import Iterator from typing import Generic, TypeVar lowerCAmelCase__ : Optional[int] = TypeVar('''T''') class __snake_case ( Generic[T] ): def __init__( self , __UpperCamelCase ) -> Any: '''simple docstring''' snake_case__ : Optional[int] = data snake_case__ : Node[T] | None = None def __str__( self ) -> str: '''simple docstring''' return F"""{self.data}""" class __snake_case ( Generic[T] ): def __init__( self ) -> None: '''simple docstring''' snake_case__ : Node[T] | None = None def __iter__( self ) -> Iterator[T]: '''simple docstring''' snake_case__ : str = self.top while node: yield node.data snake_case__ : Dict = node.next def __str__( self ) -> str: '''simple docstring''' return "->".join([str(__UpperCamelCase ) for item in self] ) def __len__( self ) -> int: '''simple docstring''' return len(tuple(iter(self ) ) ) def __a ( self ) -> bool: '''simple docstring''' return self.top is None def __a ( self , __UpperCamelCase ) -> None: '''simple docstring''' snake_case__ : str = Node(__UpperCamelCase ) if not self.is_empty(): snake_case__ : List[str] = self.top snake_case__ : Tuple = node def __a ( self ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError('pop from empty stack' ) assert isinstance(self.top , __UpperCamelCase ) snake_case__ : List[str] = self.top snake_case__ : Union[str, Any] = self.top.next return pop_node.data def __a ( self ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError('peek from empty stack' ) assert self.top is not None return self.top.data def __a ( self ) -> None: '''simple docstring''' snake_case__ : Any = None if __name__ == "__main__": from doctest import testmod testmod()
699
1
class __snake_case : def __init__( self , __UpperCamelCase ) -> List[Any]: '''simple docstring''' snake_case__ : Optional[int] = val snake_case__ : Dict = None snake_case__ : Tuple = None def __a ( self , __UpperCamelCase ) -> Optional[Any]: '''simple docstring''' if self.val: if val < self.val: if self.left is None: snake_case__ : int = Node(__UpperCamelCase ) else: self.left.insert(__UpperCamelCase ) elif val > self.val: if self.right is None: snake_case__ : List[str] = Node(__UpperCamelCase ) else: self.right.insert(__UpperCamelCase ) else: snake_case__ : Union[str, Any] = val def UpperCamelCase__ ( A__ , A__ ) -> Any: # Recursive traversal if root: inorder(root.left , A__ ) res.append(root.val ) inorder(root.right , A__ ) def UpperCamelCase__ ( A__ ) -> Optional[Any]: # Build BST if len(A__ ) == 0: return arr snake_case__ : Optional[Any] = Node(arr[0] ) for i in range(1 , len(A__ ) ): root.insert(arr[i] ) # Traverse BST in order. snake_case__ : Dict = [] inorder(A__ , A__ ) return res if __name__ == "__main__": print(tree_sort([10, 1, 3, 2, 9, 14, 13]))
699
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCAmelCase__ : Dict = logging.get_logger(__name__) lowerCAmelCase__ : int = { '''sail/poolformer_s12''': '''https://huggingface.co/sail/poolformer_s12/resolve/main/config.json''', # See all PoolFormer models at https://huggingface.co/models?filter=poolformer } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = """poolformer""" def __init__( self , __UpperCamelCase=3 , __UpperCamelCase=16 , __UpperCamelCase=16 , __UpperCamelCase=3 , __UpperCamelCase=4.0 , __UpperCamelCase=[2, 2, 6, 2] , __UpperCamelCase=[64, 128, 320, 512] , __UpperCamelCase=[7, 3, 3, 3] , __UpperCamelCase=[4, 2, 2, 2] , __UpperCamelCase=[2, 1, 1, 1] , __UpperCamelCase=4 , __UpperCamelCase=0.0 , __UpperCamelCase="gelu" , __UpperCamelCase=True , __UpperCamelCase=1E-5 , __UpperCamelCase=0.0_2 , **__UpperCamelCase , ) -> Any: '''simple docstring''' snake_case__ : List[str] = num_channels snake_case__ : Dict = patch_size snake_case__ : Optional[int] = stride snake_case__ : str = padding snake_case__ : List[str] = pool_size snake_case__ : List[Any] = hidden_sizes snake_case__ : List[Any] = mlp_ratio snake_case__ : Union[str, Any] = depths snake_case__ : Dict = patch_sizes snake_case__ : Dict = strides snake_case__ : Dict = num_encoder_blocks snake_case__ : Union[str, Any] = drop_path_rate snake_case__ : List[str] = hidden_act snake_case__ : Optional[Any] = use_layer_scale snake_case__ : int = layer_scale_init_value snake_case__ : Dict = initializer_range super().__init__(**__UpperCamelCase ) class __snake_case ( _lowerCamelCase ): __lowerCamelCase = version.parse("""1.11""" ) @property def __a ( self ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def __a ( self ) -> float: '''simple docstring''' return 2E-3
699
1
def UpperCamelCase__ ( A__ , A__ , A__ ) -> int: if exponent == 1: return base if exponent % 2 == 0: snake_case__ : Dict = _modexpt(A__ , exponent // 2 , A__ ) % modulo_value return (x * x) % modulo_value else: return (base * _modexpt(A__ , exponent - 1 , A__ )) % modulo_value def UpperCamelCase__ ( A__ = 1777 , A__ = 1855 , A__ = 8 ) -> int: snake_case__ : Tuple = base for _ in range(1 , A__ ): snake_case__ : Any = _modexpt(A__ , A__ , 10**digits ) return result if __name__ == "__main__": print(F'''{solution() = }''')
699
import numpy as np import qiskit def UpperCamelCase__ ( A__ = 8 , A__ = None ) -> str: snake_case__ : Optional[int] = np.random.default_rng(seed=A__ ) # Roughly 25% of the qubits will contribute to the key. # So we take more than we need. snake_case__ : Tuple = 6 * key_len # Measurement basis for Alice's qubits. snake_case__ : Tuple = rng.integers(2 , size=A__ ) # The set of states Alice will prepare. snake_case__ : List[str] = rng.integers(2 , size=A__ ) # Measurement basis for Bob's qubits. snake_case__ : List[Any] = rng.integers(2 , size=A__ ) # Quantum Circuit to simulate BB84 snake_case__ : Any = qiskit.QuantumCircuit(A__ , name='BB84' ) # Alice prepares her qubits according to rules above. for index, _ in enumerate(A__ ): if alice_state[index] == 1: bbaa_circ.x(A__ ) if alice_basis[index] == 1: bbaa_circ.h(A__ ) bbaa_circ.barrier() # Bob measures the received qubits according to rules above. for index, _ in enumerate(A__ ): if bob_basis[index] == 1: bbaa_circ.h(A__ ) bbaa_circ.barrier() bbaa_circ.measure_all() # Simulate the quantum circuit. snake_case__ : List[str] = qiskit.Aer.get_backend('aer_simulator' ) # We only need to run one shot because the key is unique. # Multiple shots will produce the same key. snake_case__ : Optional[Any] = qiskit.execute(A__ , A__ , shots=1 , seed_simulator=A__ ) # Returns the result of measurement. snake_case__ : Union[str, Any] = job.result().get_counts(A__ ).most_frequent() # Extracting the generated key from the simulation results. # Only keep measurement results where Alice and Bob chose the same basis. snake_case__ : Optional[Any] = ''.join( [ result_bit for alice_basis_bit, bob_basis_bit, result_bit in zip( A__ , A__ , A__ ) if alice_basis_bit == bob_basis_bit ] ) # Get final key. Pad with 0 if too short, otherwise truncate. snake_case__ : Tuple = gen_key[:key_len] if len(A__ ) >= key_len else gen_key.ljust(A__ , '0' ) return key if __name__ == "__main__": print(F'''The generated key is : {bbaa(8, seed=0)}''') from doctest import testmod testmod()
699
1
from ....configuration_utils import PretrainedConfig from ....utils import logging lowerCAmelCase__ : List[Any] = logging.get_logger(__name__) lowerCAmelCase__ : Any = { '''Visual-Attention-Network/van-base''': ( '''https://huggingface.co/Visual-Attention-Network/van-base/blob/main/config.json''' ), } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = """van""" def __init__( self , __UpperCamelCase=224 , __UpperCamelCase=3 , __UpperCamelCase=[7, 3, 3, 3] , __UpperCamelCase=[4, 2, 2, 2] , __UpperCamelCase=[64, 128, 320, 512] , __UpperCamelCase=[3, 3, 12, 3] , __UpperCamelCase=[8, 8, 4, 4] , __UpperCamelCase="gelu" , __UpperCamelCase=0.0_2 , __UpperCamelCase=1E-6 , __UpperCamelCase=1E-2 , __UpperCamelCase=0.0 , __UpperCamelCase=0.0 , **__UpperCamelCase , ) -> Any: '''simple docstring''' super().__init__(**__UpperCamelCase ) snake_case__ : Dict = image_size snake_case__ : Any = num_channels snake_case__ : Tuple = patch_sizes snake_case__ : Dict = strides snake_case__ : Optional[Any] = hidden_sizes snake_case__ : str = depths snake_case__ : str = mlp_ratios snake_case__ : Optional[int] = hidden_act snake_case__ : Dict = initializer_range snake_case__ : List[str] = layer_norm_eps snake_case__ : Optional[Any] = layer_scale_init_value snake_case__ : Union[str, Any] = drop_path_rate snake_case__ : List[Any] = dropout_rate
699
def UpperCamelCase__ ( A__ , A__ , A__ ) -> int: if exponent == 1: return base if exponent % 2 == 0: snake_case__ : Dict = _modexpt(A__ , exponent // 2 , A__ ) % modulo_value return (x * x) % modulo_value else: return (base * _modexpt(A__ , exponent - 1 , A__ )) % modulo_value def UpperCamelCase__ ( A__ = 1777 , A__ = 1855 , A__ = 8 ) -> int: snake_case__ : Tuple = base for _ in range(1 , A__ ): snake_case__ : Any = _modexpt(A__ , A__ , 10**digits ) return result if __name__ == "__main__": print(F'''{solution() = }''')
699
1
import os from pickle import UnpicklingError from typing import Dict, Tuple import jax import jax.numpy as jnp import numpy as np from flax.serialization import from_bytes from flax.traverse_util import flatten_dict, unflatten_dict import transformers from .utils import logging lowerCAmelCase__ : List[str] = logging.get_logger(__name__) def UpperCamelCase__ ( A__ , A__ , A__ , A__=False ) -> int: try: import torch # noqa: F401 except ImportError: logger.error( 'Loading a PyTorch model in Flax, 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 if not is_sharded: snake_case__ : Tuple = os.path.abspath(A__ ) logger.info(F"""Loading PyTorch weights from {pt_path}""" ) snake_case__ : List[Any] = torch.load(A__ , map_location='cpu' ) logger.info(F"""PyTorch checkpoint contains {sum(t.numel() for t in pt_state_dict.values() ):,} parameters.""" ) snake_case__ : Tuple = convert_pytorch_state_dict_to_flax(A__ , A__ ) else: # model is sharded and pytorch_checkpoint_path already contains the list of .pt shard files snake_case__ : int = convert_pytorch_sharded_state_dict_to_flax(A__ , A__ ) return flax_state_dict def UpperCamelCase__ ( A__ , A__ , A__ , A__ , ) -> (Tuple[str], np.ndarray): def is_key_or_prefix_key_in_dict(A__ ) -> bool: return len(set(A__ ) & {key, (model_prefix,) + key} ) > 0 # layer norm snake_case__ : List[Any] = pt_tuple_key[:-1] + ('scale',) if pt_tuple_key[-1] in ["weight", "gamma"] and is_key_or_prefix_key_in_dict(A__ ): return renamed_pt_tuple_key, pt_tensor # batch norm layer mean snake_case__ : str = pt_tuple_key[:-1] + ('mean',) if pt_tuple_key[-1] == "running_mean" and not is_key_or_prefix_key_in_dict(A__ ): return renamed_pt_tuple_key, pt_tensor # batch norm layer var snake_case__ : int = pt_tuple_key[:-1] + ('var',) if pt_tuple_key[-1] == "running_var" and not is_key_or_prefix_key_in_dict(A__ ): return renamed_pt_tuple_key, pt_tensor # embedding snake_case__ : Tuple = pt_tuple_key[:-1] + ('embedding',) if pt_tuple_key[-1] == "weight" and is_key_or_prefix_key_in_dict(A__ ): return renamed_pt_tuple_key, pt_tensor # conv layer snake_case__ : Optional[Any] = pt_tuple_key[:-1] + ('kernel',) if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4 and not is_key_or_prefix_key_in_dict(A__ ): snake_case__ : List[str] = pt_tensor.transpose(2 , 3 , 1 , 0 ) return renamed_pt_tuple_key, pt_tensor # linear layer snake_case__ : Tuple = pt_tuple_key[:-1] + ('kernel',) if pt_tuple_key[-1] == "weight" and not is_key_or_prefix_key_in_dict(A__ ): snake_case__ : List[Any] = pt_tensor.T return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm weight snake_case__ : Tuple = pt_tuple_key[:-1] + ('weight',) if pt_tuple_key[-1] == "gamma": return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm bias snake_case__ : List[Any] = pt_tuple_key[:-1] + ('bias',) if pt_tuple_key[-1] == "beta": return renamed_pt_tuple_key, pt_tensor # New `weight_norm` from https://github.com/huggingface/transformers/pull/24030 snake_case__ : Optional[Any] = None if pt_tuple_key[-3::2] == ("parametrizations", "original0"): snake_case__ : int = pt_tuple_key[-2] + '_g' elif pt_tuple_key[-3::2] == ("parametrizations", "original1"): snake_case__ : List[str] = pt_tuple_key[-2] + '_v' if name is not None: snake_case__ : Union[str, Any] = pt_tuple_key[:-3] + (name,) return renamed_pt_tuple_key, pt_tensor return pt_tuple_key, pt_tensor def UpperCamelCase__ ( A__ , A__ ) -> Any: # convert pytorch tensor to numpy snake_case__ : Any = {k: v.numpy() for k, v in pt_state_dict.items()} snake_case__ : Optional[Any] = flax_model.base_model_prefix # use params dict if the model contains batch norm layers if "params" in flax_model.params: snake_case__ : Union[str, Any] = flax_model.params['params'] else: snake_case__ : Union[str, Any] = flax_model.params snake_case__ : List[str] = flatten_dict(A__ ) # add batch_stats keys,values to dict if "batch_stats" in flax_model.params: snake_case__ : Optional[int] = flatten_dict(flax_model.params['batch_stats'] ) random_flax_state_dict.update(A__ ) snake_case__ : int = {} snake_case__ : Dict = (model_prefix not in flax_model_params) and ( model_prefix in {k.split('.' )[0] for k in pt_state_dict.keys()} ) snake_case__ : Optional[int] = (model_prefix in flax_model_params) and ( model_prefix not in {k.split('.' )[0] for k in pt_state_dict.keys()} ) # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): snake_case__ : Any = tuple(pt_key.split('.' ) ) # remove base model prefix if necessary snake_case__ : List[str] = pt_tuple_key[0] == model_prefix if load_model_with_head_into_base_model and has_base_model_prefix: snake_case__ : int = pt_tuple_key[1:] # Correctly rename weight parameters snake_case__ , snake_case__ : int = rename_key_and_reshape_tensor( A__ , A__ , A__ , A__ ) # add model prefix if necessary snake_case__ : str = (model_prefix,) + flax_key in random_flax_state_dict if load_base_model_into_model_with_head and require_base_model_prefix: snake_case__ : Any = (model_prefix,) + flax_key if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( F"""PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape """ F"""{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.""" ) # add batch stats if the model contains batchnorm layers if "batch_stats" in flax_model.params: if "mean" in flax_key[-1] or "var" in flax_key[-1]: snake_case__ : Optional[int] = jnp.asarray(A__ ) continue # remove num_batches_tracked key if "num_batches_tracked" in flax_key[-1]: flax_state_dict.pop(A__ , A__ ) continue # also add unexpected weight so that warning is thrown snake_case__ : Union[str, Any] = jnp.asarray(A__ ) else: # also add unexpected weight so that warning is thrown snake_case__ : Dict = jnp.asarray(A__ ) return unflatten_dict(A__ ) def UpperCamelCase__ ( A__ , A__ ) -> Optional[Any]: import torch # Load the index snake_case__ : int = {} for shard_file in shard_filenames: # load using msgpack utils snake_case__ : Optional[int] = torch.load(A__ ) snake_case__ : int = {k: v.numpy() for k, v in pt_state_dict.items()} snake_case__ : List[str] = flax_model.base_model_prefix # use params dict if the model contains batch norm layers and then add batch_stats keys,values to dict if "batch_stats" in flax_model.params: snake_case__ : Union[str, Any] = flax_model.params['params'] snake_case__ : Any = flatten_dict(A__ ) random_flax_state_dict.update(flatten_dict(flax_model.params['batch_stats'] ) ) else: snake_case__ : Optional[int] = flax_model.params snake_case__ : str = flatten_dict(A__ ) snake_case__ : Tuple = (model_prefix not in flax_model_params) and ( model_prefix in {k.split('.' )[0] for k in pt_state_dict.keys()} ) snake_case__ : Optional[Any] = (model_prefix in flax_model_params) and ( model_prefix not in {k.split('.' )[0] for k in pt_state_dict.keys()} ) # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): snake_case__ : Optional[int] = tuple(pt_key.split('.' ) ) # remove base model prefix if necessary snake_case__ : List[Any] = pt_tuple_key[0] == model_prefix if load_model_with_head_into_base_model and has_base_model_prefix: snake_case__ : str = pt_tuple_key[1:] # Correctly rename weight parameters snake_case__ , snake_case__ : Tuple = rename_key_and_reshape_tensor( A__ , A__ , A__ , A__ ) # add model prefix if necessary snake_case__ : Optional[int] = (model_prefix,) + flax_key in random_flax_state_dict if load_base_model_into_model_with_head and require_base_model_prefix: snake_case__ : int = (model_prefix,) + flax_key if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( F"""PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape """ F"""{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.""" ) # add batch stats if the model contains batchnorm layers if "batch_stats" in flax_model.params: if "mean" in flax_key[-1]: snake_case__ : List[Any] = jnp.asarray(A__ ) continue if "var" in flax_key[-1]: snake_case__ : Optional[Any] = jnp.asarray(A__ ) continue # remove num_batches_tracked key if "num_batches_tracked" in flax_key[-1]: flax_state_dict.pop(A__ , A__ ) continue # also add unexpected weight so that warning is thrown snake_case__ : str = jnp.asarray(A__ ) else: # also add unexpected weight so that warning is thrown snake_case__ : Tuple = jnp.asarray(A__ ) return unflatten_dict(A__ ) def UpperCamelCase__ ( A__ , A__ ) -> Dict: snake_case__ : Any = os.path.abspath(A__ ) logger.info(F"""Loading Flax weights from {flax_checkpoint_path}""" ) # import correct flax class snake_case__ : Optional[int] = getattr(A__ , 'Flax' + model.__class__.__name__ ) # load flax weight dict with open(A__ , 'rb' ) as state_f: try: snake_case__ : int = from_bytes(A__ , state_f.read() ) except UnpicklingError: raise EnvironmentError(F"""Unable to convert {flax_checkpoint_path} to Flax deserializable object. """ ) return load_flax_weights_in_pytorch_model(A__ , A__ ) def UpperCamelCase__ ( A__ , A__ ) -> Optional[int]: try: import torch # noqa: F401 except ImportError: logger.error( 'Loading a 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 snake_case__ : Any = flatten_dict(jax.tree_util.tree_map(lambda A__ : x.dtype == jnp.bfloataa , A__ ) ).values() if any(A__ ): # convert all weights to fp32 if the 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.' ) snake_case__ : Optional[Any] = jax.tree_util.tree_map( lambda A__ : params.astype(np.floataa ) if params.dtype == jnp.bfloataa else params , A__ ) snake_case__ : List[Any] = flatten_dict(A__ ) snake_case__ : Any = pt_model.state_dict() snake_case__ : Optional[Any] = (pt_model.base_model_prefix in flax_state) and ( pt_model.base_model_prefix not in {k.split('.' )[0] for k in pt_model_dict.keys()} ) snake_case__ : Tuple = (pt_model.base_model_prefix not in flax_state) and ( pt_model.base_model_prefix in {k.split('.' )[0] for k in pt_model_dict.keys()} ) # keep track of unexpected & missing keys snake_case__ : List[str] = [] snake_case__ : Optional[int] = set(pt_model_dict.keys() ) for flax_key_tuple, flax_tensor in flax_state_dict.items(): snake_case__ : str = flax_key_tuple[0] == pt_model.base_model_prefix snake_case__ : Dict = '.'.join((pt_model.base_model_prefix,) + flax_key_tuple ) in pt_model_dict # adapt flax_key to prepare for loading from/to base model only if load_model_with_head_into_base_model and has_base_model_prefix: snake_case__ : str = flax_key_tuple[1:] elif load_base_model_into_model_with_head and require_base_model_prefix: snake_case__ : int = (pt_model.base_model_prefix,) + flax_key_tuple # rename flax weights to PyTorch format if flax_key_tuple[-1] == "kernel" and flax_tensor.ndim == 4 and ".".join(A__ ) not in pt_model_dict: # conv layer snake_case__ : List[Any] = flax_key_tuple[:-1] + ('weight',) snake_case__ : Optional[int] = jnp.transpose(A__ , (3, 2, 0, 1) ) elif flax_key_tuple[-1] == "kernel" and ".".join(A__ ) not in pt_model_dict: # linear layer snake_case__ : Union[str, Any] = flax_key_tuple[:-1] + ('weight',) snake_case__ : Optional[int] = flax_tensor.T elif flax_key_tuple[-1] in ["scale", "embedding"]: snake_case__ : Optional[Any] = flax_key_tuple[:-1] + ('weight',) # adding batch stats from flax batch norm to pt elif "mean" in flax_key_tuple[-1]: snake_case__ : Dict = flax_key_tuple[:-1] + ('running_mean',) elif "var" in flax_key_tuple[-1]: snake_case__ : List[Any] = flax_key_tuple[:-1] + ('running_var',) if "batch_stats" in flax_state: snake_case__ : Union[str, Any] = '.'.join(flax_key_tuple[1:] ) # Remove the params/batch_stats header else: snake_case__ : Tuple = '.'.join(A__ ) # We also need to look at `pt_model_dict` and see if there are keys requiring further transformation. snake_case__ : Tuple = {} # New `weight_norm` from https://github.com/huggingface/transformers/pull/24030 for key in pt_model_dict: snake_case__ : List[Any] = key.split('.' ) snake_case__ : Tuple = None if key_components[-3::2] == ["parametrizations", "original0"]: snake_case__ : Optional[int] = key_components[-2] + '_g' elif key_components[-3::2] == ["parametrizations", "original1"]: snake_case__ : List[Any] = key_components[-2] + '_v' if name is not None: snake_case__ : List[str] = key_components[:-3] + [name] snake_case__ : Optional[int] = '.'.join(A__ ) snake_case__ : Optional[int] = key if flax_key in special_pt_names: snake_case__ : str = special_pt_names[flax_key] 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 snake_case__ : int = np.asarray(A__ ) if not isinstance(A__ , np.ndarray ) else flax_tensor snake_case__ : Optional[Any] = torch.from_numpy(A__ ) # remove from missing keys missing_keys.remove(A__ ) else: # weight is not expected by PyTorch model unexpected_keys.append(A__ ) pt_model.load_state_dict(A__ ) # re-transform missing_keys to list snake_case__ : Union[str, Any] = list(A__ ) if len(A__ ) > 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).' ) else: logger.warning(F"""All Flax model weights were used when initializing {pt_model.__class__.__name__}.\n""" ) if len(A__ ) > 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.' ) else: logger.warning( F"""All the weights of {pt_model.__class__.__name__} were initialized from the Flax model.\n""" 'If your task is similar to the task the model of the checkpoint was trained on, ' F"""you can already use {pt_model.__class__.__name__} for predictions without further training.""" ) return pt_model
699
# tests directory-specific settings - this file is run automatically # by pytest before any tests are run import sys import warnings from os.path import abspath, dirname, join # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. lowerCAmelCase__ : Tuple = abspath(join(dirname(dirname(__file__)), '''src''')) sys.path.insert(1, git_repo_path) # silence FutureWarning warnings in tests since often we can't act on them until # they become normal warnings - i.e. the tests still need to test the current functionality warnings.simplefilter(action='''ignore''', category=FutureWarning) def UpperCamelCase__ ( A__ ) -> Optional[Any]: from diffusers.utils.testing_utils import pytest_addoption_shared pytest_addoption_shared(A__ ) def UpperCamelCase__ ( A__ ) -> Optional[Any]: from diffusers.utils.testing_utils import pytest_terminal_summary_main snake_case__ : Union[str, Any] = terminalreporter.config.getoption('--make-reports' ) if make_reports: pytest_terminal_summary_main(A__ , id=A__ )
699
1
from dataclasses import dataclass from typing import Optional import numpy as np import torch import torch.nn as nn from ..utils import BaseOutput, is_torch_version, randn_tensor from .attention_processor import SpatialNorm from .unet_ad_blocks import UNetMidBlockaD, get_down_block, get_up_block @dataclass class __snake_case ( _lowerCamelCase ): __lowerCamelCase = 42 class __snake_case ( nn.Module ): def __init__( self , __UpperCamelCase=3 , __UpperCamelCase=3 , __UpperCamelCase=("DownEncoderBlock2D",) , __UpperCamelCase=(64,) , __UpperCamelCase=2 , __UpperCamelCase=32 , __UpperCamelCase="silu" , __UpperCamelCase=True , ) -> Optional[Any]: '''simple docstring''' super().__init__() snake_case__ : List[Any] = layers_per_block snake_case__ : List[Any] = torch.nn.Convad( __UpperCamelCase , block_out_channels[0] , kernel_size=3 , stride=1 , padding=1 , ) snake_case__ : List[Any] = None snake_case__ : List[Any] = nn.ModuleList([] ) # down snake_case__ : str = block_out_channels[0] for i, down_block_type in enumerate(__UpperCamelCase ): snake_case__ : Optional[int] = output_channel snake_case__ : Optional[int] = block_out_channels[i] snake_case__ : Any = i == len(__UpperCamelCase ) - 1 snake_case__ : Optional[Any] = get_down_block( __UpperCamelCase , num_layers=self.layers_per_block , in_channels=__UpperCamelCase , out_channels=__UpperCamelCase , add_downsample=not is_final_block , resnet_eps=1E-6 , downsample_padding=0 , resnet_act_fn=__UpperCamelCase , resnet_groups=__UpperCamelCase , attention_head_dim=__UpperCamelCase , temb_channels=__UpperCamelCase , ) self.down_blocks.append(__UpperCamelCase ) # mid snake_case__ : Union[str, Any] = UNetMidBlockaD( in_channels=block_out_channels[-1] , resnet_eps=1E-6 , resnet_act_fn=__UpperCamelCase , output_scale_factor=1 , resnet_time_scale_shift='default' , attention_head_dim=block_out_channels[-1] , resnet_groups=__UpperCamelCase , temb_channels=__UpperCamelCase , ) # out snake_case__ : List[Any] = nn.GroupNorm(num_channels=block_out_channels[-1] , num_groups=__UpperCamelCase , eps=1E-6 ) snake_case__ : Optional[Any] = nn.SiLU() snake_case__ : Optional[int] = 2 * out_channels if double_z else out_channels snake_case__ : Optional[int] = nn.Convad(block_out_channels[-1] , __UpperCamelCase , 3 , padding=1 ) snake_case__ : Optional[int] = False def __a ( self , __UpperCamelCase ) -> int: '''simple docstring''' snake_case__ : Any = x snake_case__ : Optional[int] = self.conv_in(__UpperCamelCase ) if self.training and self.gradient_checkpointing: def create_custom_forward(__UpperCamelCase ): def custom_forward(*__UpperCamelCase ): return module(*__UpperCamelCase ) return custom_forward # down if is_torch_version('>=' , '1.11.0' ): for down_block in self.down_blocks: snake_case__ : Tuple = torch.utils.checkpoint.checkpoint( create_custom_forward(__UpperCamelCase ) , __UpperCamelCase , use_reentrant=__UpperCamelCase ) # middle snake_case__ : Tuple = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , __UpperCamelCase , use_reentrant=__UpperCamelCase ) else: for down_block in self.down_blocks: snake_case__ : Optional[int] = torch.utils.checkpoint.checkpoint(create_custom_forward(__UpperCamelCase ) , __UpperCamelCase ) # middle snake_case__ : List[Any] = torch.utils.checkpoint.checkpoint(create_custom_forward(self.mid_block ) , __UpperCamelCase ) else: # down for down_block in self.down_blocks: snake_case__ : Optional[Any] = down_block(__UpperCamelCase ) # middle snake_case__ : List[str] = self.mid_block(__UpperCamelCase ) # post-process snake_case__ : Any = self.conv_norm_out(__UpperCamelCase ) snake_case__ : Union[str, Any] = self.conv_act(__UpperCamelCase ) snake_case__ : int = self.conv_out(__UpperCamelCase ) return sample class __snake_case ( nn.Module ): def __init__( self , __UpperCamelCase=3 , __UpperCamelCase=3 , __UpperCamelCase=("UpDecoderBlock2D",) , __UpperCamelCase=(64,) , __UpperCamelCase=2 , __UpperCamelCase=32 , __UpperCamelCase="silu" , __UpperCamelCase="group" , ) -> Any: '''simple docstring''' super().__init__() snake_case__ : int = layers_per_block snake_case__ : List[Any] = nn.Convad( __UpperCamelCase , block_out_channels[-1] , kernel_size=3 , stride=1 , padding=1 , ) snake_case__ : List[str] = None snake_case__ : Union[str, Any] = nn.ModuleList([] ) snake_case__ : Optional[Any] = in_channels if norm_type == 'spatial' else None # mid snake_case__ : int = UNetMidBlockaD( in_channels=block_out_channels[-1] , resnet_eps=1E-6 , resnet_act_fn=__UpperCamelCase , output_scale_factor=1 , resnet_time_scale_shift='default' if norm_type == 'group' else norm_type , attention_head_dim=block_out_channels[-1] , resnet_groups=__UpperCamelCase , temb_channels=__UpperCamelCase , ) # up snake_case__ : Dict = list(reversed(__UpperCamelCase ) ) snake_case__ : List[Any] = reversed_block_out_channels[0] for i, up_block_type in enumerate(__UpperCamelCase ): snake_case__ : Optional[Any] = output_channel snake_case__ : Tuple = reversed_block_out_channels[i] snake_case__ : Union[str, Any] = i == len(__UpperCamelCase ) - 1 snake_case__ : str = get_up_block( __UpperCamelCase , num_layers=self.layers_per_block + 1 , in_channels=__UpperCamelCase , out_channels=__UpperCamelCase , prev_output_channel=__UpperCamelCase , add_upsample=not is_final_block , resnet_eps=1E-6 , resnet_act_fn=__UpperCamelCase , resnet_groups=__UpperCamelCase , attention_head_dim=__UpperCamelCase , temb_channels=__UpperCamelCase , resnet_time_scale_shift=__UpperCamelCase , ) self.up_blocks.append(__UpperCamelCase ) snake_case__ : str = output_channel # out if norm_type == "spatial": snake_case__ : str = SpatialNorm(block_out_channels[0] , __UpperCamelCase ) else: snake_case__ : Any = nn.GroupNorm(num_channels=block_out_channels[0] , num_groups=__UpperCamelCase , eps=1E-6 ) snake_case__ : Optional[Any] = nn.SiLU() snake_case__ : Dict = nn.Convad(block_out_channels[0] , __UpperCamelCase , 3 , padding=1 ) snake_case__ : Optional[Any] = False def __a ( self , __UpperCamelCase , __UpperCamelCase=None ) -> Optional[int]: '''simple docstring''' snake_case__ : Any = z snake_case__ : Union[str, Any] = self.conv_in(__UpperCamelCase ) snake_case__ : List[str] = next(iter(self.up_blocks.parameters() ) ).dtype if self.training and self.gradient_checkpointing: def create_custom_forward(__UpperCamelCase ): def custom_forward(*__UpperCamelCase ): return module(*__UpperCamelCase ) return custom_forward if is_torch_version('>=' , '1.11.0' ): # middle snake_case__ : Dict = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , __UpperCamelCase , __UpperCamelCase , use_reentrant=__UpperCamelCase ) snake_case__ : List[Any] = sample.to(__UpperCamelCase ) # up for up_block in self.up_blocks: snake_case__ : Optional[int] = torch.utils.checkpoint.checkpoint( create_custom_forward(__UpperCamelCase ) , __UpperCamelCase , __UpperCamelCase , use_reentrant=__UpperCamelCase ) else: # middle snake_case__ : Optional[Any] = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , __UpperCamelCase , __UpperCamelCase ) snake_case__ : Union[str, Any] = sample.to(__UpperCamelCase ) # up for up_block in self.up_blocks: snake_case__ : int = torch.utils.checkpoint.checkpoint(create_custom_forward(__UpperCamelCase ) , __UpperCamelCase , __UpperCamelCase ) else: # middle snake_case__ : Dict = self.mid_block(__UpperCamelCase , __UpperCamelCase ) snake_case__ : Optional[int] = sample.to(__UpperCamelCase ) # up for up_block in self.up_blocks: snake_case__ : Any = up_block(__UpperCamelCase , __UpperCamelCase ) # post-process if latent_embeds is None: snake_case__ : Tuple = self.conv_norm_out(__UpperCamelCase ) else: snake_case__ : Optional[int] = self.conv_norm_out(__UpperCamelCase , __UpperCamelCase ) snake_case__ : str = self.conv_act(__UpperCamelCase ) snake_case__ : List[str] = self.conv_out(__UpperCamelCase ) return sample class __snake_case ( nn.Module ): def __init__( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=None , __UpperCamelCase="random" , __UpperCamelCase=False , __UpperCamelCase=True ) -> int: '''simple docstring''' super().__init__() snake_case__ : List[str] = n_e snake_case__ : Optional[Any] = vq_embed_dim snake_case__ : Optional[Any] = beta snake_case__ : List[Any] = legacy snake_case__ : Any = nn.Embedding(self.n_e , self.vq_embed_dim ) self.embedding.weight.data.uniform_(-1.0 / self.n_e , 1.0 / self.n_e ) snake_case__ : Dict = remap if self.remap is not None: self.register_buffer('used' , torch.tensor(np.load(self.remap ) ) ) snake_case__ : int = self.used.shape[0] snake_case__ : Any = unknown_index # "random" or "extra" or integer if self.unknown_index == "extra": snake_case__ : List[str] = self.re_embed snake_case__ : str = self.re_embed + 1 print( F"""Remapping {self.n_e} indices to {self.re_embed} indices. """ F"""Using {self.unknown_index} for unknown indices.""" ) else: snake_case__ : int = n_e snake_case__ : Tuple = sane_index_shape def __a ( self , __UpperCamelCase ) -> int: '''simple docstring''' snake_case__ : Any = inds.shape assert len(__UpperCamelCase ) > 1 snake_case__ : Optional[Any] = inds.reshape(ishape[0] , -1 ) snake_case__ : List[Any] = self.used.to(__UpperCamelCase ) snake_case__ : List[str] = (inds[:, :, None] == used[None, None, ...]).long() snake_case__ : Union[str, Any] = match.argmax(-1 ) snake_case__ : Any = match.sum(2 ) < 1 if self.unknown_index == "random": snake_case__ : Any = torch.randint(0 , self.re_embed , size=new[unknown].shape ).to(device=new.device ) else: snake_case__ : Tuple = self.unknown_index return new.reshape(__UpperCamelCase ) def __a ( self , __UpperCamelCase ) -> Any: '''simple docstring''' snake_case__ : Dict = inds.shape assert len(__UpperCamelCase ) > 1 snake_case__ : Dict = inds.reshape(ishape[0] , -1 ) snake_case__ : Dict = self.used.to(__UpperCamelCase ) if self.re_embed > self.used.shape[0]: # extra token snake_case__ : Optional[Any] = 0 # simply set to zero snake_case__ : List[str] = torch.gather(used[None, :][inds.shape[0] * [0], :] , 1 , __UpperCamelCase ) return back.reshape(__UpperCamelCase ) def __a ( self , __UpperCamelCase ) -> List[Any]: '''simple docstring''' snake_case__ : Optional[Any] = z.permute(0 , 2 , 3 , 1 ).contiguous() snake_case__ : Optional[int] = z.view(-1 , self.vq_embed_dim ) # distances from z to embeddings e_j (z - e)^2 = z^2 + e^2 - 2 e * z snake_case__ : Tuple = torch.argmin(torch.cdist(__UpperCamelCase , self.embedding.weight ) , dim=1 ) snake_case__ : Tuple = self.embedding(__UpperCamelCase ).view(z.shape ) snake_case__ : Optional[Any] = None snake_case__ : Union[str, Any] = None # compute loss for embedding if not self.legacy: snake_case__ : Tuple = self.beta * torch.mean((z_q.detach() - z) ** 2 ) + torch.mean((z_q - z.detach()) ** 2 ) else: snake_case__ : Optional[int] = torch.mean((z_q.detach() - z) ** 2 ) + self.beta * torch.mean((z_q - z.detach()) ** 2 ) # preserve gradients snake_case__ : List[Any] = z + (z_q - z).detach() # reshape back to match original input shape snake_case__ : List[Any] = z_q.permute(0 , 3 , 1 , 2 ).contiguous() if self.remap is not None: snake_case__ : Tuple = min_encoding_indices.reshape(z.shape[0] , -1 ) # add batch axis snake_case__ : str = self.remap_to_used(__UpperCamelCase ) snake_case__ : str = min_encoding_indices.reshape(-1 , 1 ) # flatten if self.sane_index_shape: snake_case__ : List[Any] = min_encoding_indices.reshape(z_q.shape[0] , z_q.shape[2] , z_q.shape[3] ) return z_q, loss, (perplexity, min_encodings, min_encoding_indices) def __a ( self , __UpperCamelCase , __UpperCamelCase ) -> List[str]: '''simple docstring''' if self.remap is not None: snake_case__ : Tuple = indices.reshape(shape[0] , -1 ) # add batch axis snake_case__ : int = self.unmap_to_all(__UpperCamelCase ) snake_case__ : List[str] = indices.reshape(-1 ) # flatten again # get quantized latent vectors snake_case__ : List[Any] = self.embedding(__UpperCamelCase ) if shape is not None: snake_case__ : List[str] = z_q.view(__UpperCamelCase ) # reshape back to match original input shape snake_case__ : Any = z_q.permute(0 , 3 , 1 , 2 ).contiguous() return z_q class __snake_case ( _lowerCamelCase ): def __init__( self , __UpperCamelCase , __UpperCamelCase=False ) -> Tuple: '''simple docstring''' snake_case__ : Optional[int] = parameters snake_case__ , snake_case__ : str = torch.chunk(__UpperCamelCase , 2 , dim=1 ) snake_case__ : Any = torch.clamp(self.logvar , -3_0.0 , 2_0.0 ) snake_case__ : Optional[int] = deterministic snake_case__ : Dict = torch.exp(0.5 * self.logvar ) snake_case__ : List[str] = torch.exp(self.logvar ) if self.deterministic: snake_case__ : Optional[Any] = torch.zeros_like( self.mean , device=self.parameters.device , dtype=self.parameters.dtype ) def __a ( self , __UpperCamelCase = None ) -> torch.FloatTensor: '''simple docstring''' snake_case__ : Any = randn_tensor( self.mean.shape , generator=__UpperCamelCase , device=self.parameters.device , dtype=self.parameters.dtype ) snake_case__ : int = self.mean + self.std * sample return x def __a ( self , __UpperCamelCase=None ) -> Union[str, Any]: '''simple docstring''' if self.deterministic: return torch.Tensor([0.0] ) else: if other is None: return 0.5 * torch.sum(torch.pow(self.mean , 2 ) + self.var - 1.0 - self.logvar , dim=[1, 2, 3] ) else: return 0.5 * torch.sum( torch.pow(self.mean - other.mean , 2 ) / other.var + self.var / other.var - 1.0 - self.logvar + other.logvar , dim=[1, 2, 3] , ) def __a ( self , __UpperCamelCase , __UpperCamelCase=[1, 2, 3] ) -> Any: '''simple docstring''' if self.deterministic: return torch.Tensor([0.0] ) snake_case__ : List[Any] = np.log(2.0 * np.pi ) return 0.5 * torch.sum(logtwopi + self.logvar + torch.pow(sample - self.mean , 2 ) / self.var , dim=__UpperCamelCase ) def __a ( self ) -> List[Any]: '''simple docstring''' return self.mean
699
def UpperCamelCase__ ( A__ ) -> list[int]: if length <= 0 or not isinstance(A__ , A__ ): raise ValueError('Length must be a positive integer.' ) return [n * (2 * n - 1) for n in range(A__ )] if __name__ == "__main__": print(hexagonal_numbers(length=5)) print(hexagonal_numbers(length=10))
699
1
import argparse import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing the experiment tracking capability, # and builds off the `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To help focus on the differences in the code, building `DataLoaders` # was refactored into its own function. # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## lowerCAmelCase__ : List[Any] = 16 lowerCAmelCase__ : int = 32 def UpperCamelCase__ ( A__ , A__ = 16 ) -> Optional[Any]: snake_case__ : Optional[Any] = AutoTokenizer.from_pretrained('bert-base-cased' ) snake_case__ : str = load_dataset('glue' , 'mrpc' ) def tokenize_function(A__ ): # max_length=None => use the model max length (it's actually the default) snake_case__ : Optional[Any] = tokenizer(examples['sentence1'] , examples['sentence2'] , truncation=A__ , max_length=A__ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): snake_case__ : Optional[Any] = datasets.map( A__ , batched=A__ , remove_columns=['idx', 'sentence1', 'sentence2'] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library snake_case__ : int = tokenized_datasets.rename_column('label' , 'labels' ) def collate_fn(A__ ): # On TPU it's best to pad everything to the same length or training will be very slow. snake_case__ : int = 128 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": snake_case__ : Optional[int] = 16 elif accelerator.mixed_precision != "no": snake_case__ : Union[str, Any] = 8 else: snake_case__ : Any = None return tokenizer.pad( A__ , padding='longest' , max_length=A__ , pad_to_multiple_of=A__ , return_tensors='pt' , ) # Instantiate dataloaders. snake_case__ : Any = DataLoader( tokenized_datasets['train'] , shuffle=A__ , collate_fn=A__ , batch_size=A__ ) snake_case__ : Tuple = DataLoader( tokenized_datasets['validation'] , shuffle=A__ , collate_fn=A__ , batch_size=A__ ) return train_dataloader, eval_dataloader # For testing only if os.environ.get('''TESTING_MOCKED_DATALOADERS''', None) == "1": from accelerate.test_utils.training import mocked_dataloaders lowerCAmelCase__ : List[Any] = mocked_dataloaders # noqa: F811 def UpperCamelCase__ ( A__ , A__ ) -> Union[str, Any]: # For testing only if os.environ.get('TESTING_MOCKED_DATALOADERS' , A__ ) == "1": snake_case__ : List[str] = 2 # Initialize Accelerator # New Code # # We pass in "all" to `log_with` to grab all available trackers in the environment # Note: If using a custom `Tracker` class, should be passed in here such as: # >>> log_with = ["all", MyCustomTrackerClassInstance()] if args.with_tracking: snake_case__ : int = Accelerator( cpu=args.cpu , mixed_precision=args.mixed_precision , log_with='all' , project_dir=args.project_dir ) else: snake_case__ : int = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs snake_case__ : Tuple = config['lr'] snake_case__ : Any = int(config['num_epochs'] ) snake_case__ : int = int(config['seed'] ) snake_case__ : int = int(config['batch_size'] ) set_seed(A__ ) snake_case__ , snake_case__ : str = get_dataloaders(A__ , A__ ) snake_case__ : List[str] = evaluate.load('glue' , 'mrpc' ) # If the batch size is too big we use gradient accumulation snake_case__ : Optional[Any] = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: snake_case__ : Optional[int] = batch_size // MAX_GPU_BATCH_SIZE snake_case__ : Dict = MAX_GPU_BATCH_SIZE # Instantiate the model (we build the model here so that the seed also control new weights initialization) snake_case__ : Union[str, Any] = AutoModelForSequenceClassification.from_pretrained('bert-base-cased' , return_dict=A__ ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). snake_case__ : str = model.to(accelerator.device ) # Instantiate optimizer snake_case__ : Optional[int] = AdamW(params=model.parameters() , lr=A__ ) # Instantiate scheduler snake_case__ : List[str] = get_linear_schedule_with_warmup( optimizer=A__ , num_warmup_steps=100 , num_training_steps=(len(A__ ) * num_epochs) // gradient_accumulation_steps , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ : Tuple = accelerator.prepare( A__ , A__ , A__ , A__ , A__ ) # New Code # # We need to initialize the trackers we use. Overall configurations can also be stored if args.with_tracking: snake_case__ : Optional[Any] = os.path.split(A__ )[-1].split('.' )[0] accelerator.init_trackers(A__ , A__ ) # Now we train the model for epoch in range(A__ ): model.train() # New Code # # For our tracking example, we will log the total loss of each epoch if args.with_tracking: snake_case__ : Any = 0 for step, batch in enumerate(A__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) snake_case__ : Optional[int] = model(**A__ ) snake_case__ : str = outputs.loss # New Code # if args.with_tracking: total_loss += loss.detach().float() snake_case__ : Any = loss / gradient_accumulation_steps accelerator.backward(A__ ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(A__ ): # We could avoid this line since we set the accelerator with `device_placement=True` (the default). batch.to(accelerator.device ) with torch.no_grad(): snake_case__ : Tuple = model(**A__ ) snake_case__ : str = outputs.logits.argmax(dim=-1 ) snake_case__ , snake_case__ : Any = accelerator.gather_for_metrics((predictions, batch['labels']) ) metric.add_batch( predictions=A__ , references=A__ , ) snake_case__ : List[str] = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F"""epoch {epoch}:""" , A__ ) # New Code # # To actually log, we call `Accelerator.log` # The values passed can be of `str`, `int`, `float` or `dict` of `str` to `float`/`int` if args.with_tracking: accelerator.log( { 'accuracy': eval_metric['accuracy'], 'f1': eval_metric['f1'], 'train_loss': total_loss.item() / len(A__ ), 'epoch': epoch, } , step=A__ , ) # New Code # # When a run is finished, you should call `accelerator.end_training()` # to close all of the open trackers if args.with_tracking: accelerator.end_training() def UpperCamelCase__ ( ) -> List[str]: snake_case__ : str = argparse.ArgumentParser(description='Simple example of training script.' ) parser.add_argument( '--mixed_precision' , type=A__ , default=A__ , choices=['no', 'fp16', 'bf16', 'fp8'] , help='Whether to use mixed precision. Choose' 'between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.' 'and an Nvidia Ampere GPU.' , ) parser.add_argument('--cpu' , action='store_true' , help='If passed, will train on the CPU.' ) parser.add_argument( '--with_tracking' , action='store_true' , help='Whether to load in all available experiment trackers from the environment and use them for logging.' , ) parser.add_argument( '--project_dir' , type=A__ , default='logs' , help='Location on where to store experiment tracking logs` and relevent project information' , ) snake_case__ : List[Any] = parser.parse_args() snake_case__ : List[str] = {'lr': 2e-5, 'num_epochs': 3, 'seed': 42, 'batch_size': 16} training_function(A__ , A__ ) if __name__ == "__main__": main()
699
import argparse import json from collections import OrderedDict from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( ConditionalDetrConfig, ConditionalDetrForObjectDetection, ConditionalDetrForSegmentation, ConditionalDetrImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase__ : Dict = logging.get_logger(__name__) # here we list all keys to be renamed (original name on the left, our name on the right) lowerCAmelCase__ : Optional[Any] = [] for i in range(6): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append( (F'''transformer.encoder.layers.{i}.self_attn.out_proj.weight''', F'''encoder.layers.{i}.self_attn.out_proj.weight''') ) rename_keys.append( (F'''transformer.encoder.layers.{i}.self_attn.out_proj.bias''', F'''encoder.layers.{i}.self_attn.out_proj.bias''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.linear1.weight''', F'''encoder.layers.{i}.fc1.weight''')) rename_keys.append((F'''transformer.encoder.layers.{i}.linear1.bias''', F'''encoder.layers.{i}.fc1.bias''')) rename_keys.append((F'''transformer.encoder.layers.{i}.linear2.weight''', F'''encoder.layers.{i}.fc2.weight''')) rename_keys.append((F'''transformer.encoder.layers.{i}.linear2.bias''', F'''encoder.layers.{i}.fc2.bias''')) rename_keys.append( (F'''transformer.encoder.layers.{i}.norm1.weight''', F'''encoder.layers.{i}.self_attn_layer_norm.weight''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.norm1.bias''', F'''encoder.layers.{i}.self_attn_layer_norm.bias''')) rename_keys.append((F'''transformer.encoder.layers.{i}.norm2.weight''', F'''encoder.layers.{i}.final_layer_norm.weight''')) rename_keys.append((F'''transformer.encoder.layers.{i}.norm2.bias''', F'''encoder.layers.{i}.final_layer_norm.bias''')) # decoder layers: 2 times output projection, 2 feedforward neural networks and 3 layernorms rename_keys.append( (F'''transformer.decoder.layers.{i}.self_attn.out_proj.weight''', F'''decoder.layers.{i}.self_attn.out_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.self_attn.out_proj.bias''', F'''decoder.layers.{i}.self_attn.out_proj.bias''') ) rename_keys.append( ( F'''transformer.decoder.layers.{i}.cross_attn.out_proj.weight''', F'''decoder.layers.{i}.encoder_attn.out_proj.weight''', ) ) rename_keys.append( ( F'''transformer.decoder.layers.{i}.cross_attn.out_proj.bias''', F'''decoder.layers.{i}.encoder_attn.out_proj.bias''', ) ) rename_keys.append((F'''transformer.decoder.layers.{i}.linear1.weight''', F'''decoder.layers.{i}.fc1.weight''')) rename_keys.append((F'''transformer.decoder.layers.{i}.linear1.bias''', F'''decoder.layers.{i}.fc1.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.linear2.weight''', F'''decoder.layers.{i}.fc2.weight''')) rename_keys.append((F'''transformer.decoder.layers.{i}.linear2.bias''', F'''decoder.layers.{i}.fc2.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.norm1.weight''', F'''decoder.layers.{i}.self_attn_layer_norm.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.norm1.bias''', F'''decoder.layers.{i}.self_attn_layer_norm.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.norm2.weight''', F'''decoder.layers.{i}.encoder_attn_layer_norm.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.norm2.bias''', F'''decoder.layers.{i}.encoder_attn_layer_norm.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.norm3.weight''', F'''decoder.layers.{i}.final_layer_norm.weight''')) rename_keys.append((F'''transformer.decoder.layers.{i}.norm3.bias''', F'''decoder.layers.{i}.final_layer_norm.bias''')) # q, k, v projections in self/cross-attention in decoder for conditional DETR rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_qcontent_proj.weight''', F'''decoder.layers.{i}.sa_qcontent_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_kcontent_proj.weight''', F'''decoder.layers.{i}.sa_kcontent_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_qpos_proj.weight''', F'''decoder.layers.{i}.sa_qpos_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_kpos_proj.weight''', F'''decoder.layers.{i}.sa_kpos_proj.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_v_proj.weight''', F'''decoder.layers.{i}.sa_v_proj.weight''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qcontent_proj.weight''', F'''decoder.layers.{i}.ca_qcontent_proj.weight''') ) # rename_keys.append((f"transformer.decoder.layers.{i}.ca_qpos_proj.weight", f"decoder.layers.{i}.ca_qpos_proj.weight")) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_kcontent_proj.weight''', F'''decoder.layers.{i}.ca_kcontent_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_kpos_proj.weight''', F'''decoder.layers.{i}.ca_kpos_proj.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.ca_v_proj.weight''', F'''decoder.layers.{i}.ca_v_proj.weight''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qpos_sine_proj.weight''', F'''decoder.layers.{i}.ca_qpos_sine_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_qcontent_proj.bias''', F'''decoder.layers.{i}.sa_qcontent_proj.bias''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_kcontent_proj.bias''', F'''decoder.layers.{i}.sa_kcontent_proj.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_qpos_proj.bias''', F'''decoder.layers.{i}.sa_qpos_proj.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_kpos_proj.bias''', F'''decoder.layers.{i}.sa_kpos_proj.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_v_proj.bias''', F'''decoder.layers.{i}.sa_v_proj.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qcontent_proj.bias''', F'''decoder.layers.{i}.ca_qcontent_proj.bias''') ) # rename_keys.append((f"transformer.decoder.layers.{i}.ca_qpos_proj.bias", f"decoder.layers.{i}.ca_qpos_proj.bias")) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_kcontent_proj.bias''', F'''decoder.layers.{i}.ca_kcontent_proj.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.ca_kpos_proj.bias''', F'''decoder.layers.{i}.ca_kpos_proj.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.ca_v_proj.bias''', F'''decoder.layers.{i}.ca_v_proj.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qpos_sine_proj.bias''', F'''decoder.layers.{i}.ca_qpos_sine_proj.bias''') ) # convolutional projection + query embeddings + layernorm of decoder + class and bounding box heads # for conditional DETR, also convert reference point head and query scale MLP rename_keys.extend( [ ('''input_proj.weight''', '''input_projection.weight'''), ('''input_proj.bias''', '''input_projection.bias'''), ('''query_embed.weight''', '''query_position_embeddings.weight'''), ('''transformer.decoder.norm.weight''', '''decoder.layernorm.weight'''), ('''transformer.decoder.norm.bias''', '''decoder.layernorm.bias'''), ('''class_embed.weight''', '''class_labels_classifier.weight'''), ('''class_embed.bias''', '''class_labels_classifier.bias'''), ('''bbox_embed.layers.0.weight''', '''bbox_predictor.layers.0.weight'''), ('''bbox_embed.layers.0.bias''', '''bbox_predictor.layers.0.bias'''), ('''bbox_embed.layers.1.weight''', '''bbox_predictor.layers.1.weight'''), ('''bbox_embed.layers.1.bias''', '''bbox_predictor.layers.1.bias'''), ('''bbox_embed.layers.2.weight''', '''bbox_predictor.layers.2.weight'''), ('''bbox_embed.layers.2.bias''', '''bbox_predictor.layers.2.bias'''), ('''transformer.decoder.ref_point_head.layers.0.weight''', '''decoder.ref_point_head.layers.0.weight'''), ('''transformer.decoder.ref_point_head.layers.0.bias''', '''decoder.ref_point_head.layers.0.bias'''), ('''transformer.decoder.ref_point_head.layers.1.weight''', '''decoder.ref_point_head.layers.1.weight'''), ('''transformer.decoder.ref_point_head.layers.1.bias''', '''decoder.ref_point_head.layers.1.bias'''), ('''transformer.decoder.query_scale.layers.0.weight''', '''decoder.query_scale.layers.0.weight'''), ('''transformer.decoder.query_scale.layers.0.bias''', '''decoder.query_scale.layers.0.bias'''), ('''transformer.decoder.query_scale.layers.1.weight''', '''decoder.query_scale.layers.1.weight'''), ('''transformer.decoder.query_scale.layers.1.bias''', '''decoder.query_scale.layers.1.bias'''), ('''transformer.decoder.layers.0.ca_qpos_proj.weight''', '''decoder.layers.0.ca_qpos_proj.weight'''), ('''transformer.decoder.layers.0.ca_qpos_proj.bias''', '''decoder.layers.0.ca_qpos_proj.bias'''), ] ) def UpperCamelCase__ ( A__ , A__ , A__ ) -> List[str]: snake_case__ : int = state_dict.pop(A__ ) snake_case__ : Union[str, Any] = val def UpperCamelCase__ ( A__ ) -> int: snake_case__ : List[Any] = OrderedDict() for key, value in state_dict.items(): if "backbone.0.body" in key: snake_case__ : Any = key.replace('backbone.0.body' , 'backbone.conv_encoder.model' ) snake_case__ : Optional[int] = value else: snake_case__ : Optional[int] = value return new_state_dict def UpperCamelCase__ ( A__ , A__=False ) -> Optional[int]: snake_case__ : Optional[int] = '' if is_panoptic: snake_case__ : Tuple = 'conditional_detr.' # first: transformer encoder for i in range(6 ): # read in weights + bias of input projection layer (in PyTorch's MultiHeadAttention, this is a single matrix + bias) snake_case__ : int = state_dict.pop(F"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_weight""" ) snake_case__ : str = state_dict.pop(F"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict snake_case__ : Union[str, Any] = in_proj_weight[:256, :] snake_case__ : Union[str, Any] = in_proj_bias[:256] snake_case__ : Union[str, Any] = in_proj_weight[256:512, :] snake_case__ : Optional[Any] = in_proj_bias[256:512] snake_case__ : List[str] = in_proj_weight[-256:, :] snake_case__ : Tuple = in_proj_bias[-256:] def UpperCamelCase__ ( ) -> Tuple: snake_case__ : int = 'http://images.cocodataset.org/val2017/000000039769.jpg' snake_case__ : str = Image.open(requests.get(A__ , stream=A__ ).raw ) return im @torch.no_grad() def UpperCamelCase__ ( A__ , A__ ) -> str: snake_case__ : List[Any] = ConditionalDetrConfig() # set backbone and dilation attributes if "resnet101" in model_name: snake_case__ : Any = 'resnet101' if "dc5" in model_name: snake_case__ : Any = True snake_case__ : int = 'panoptic' in model_name if is_panoptic: snake_case__ : str = 250 else: snake_case__ : Union[str, Any] = 91 snake_case__ : Optional[int] = 'huggingface/label-files' snake_case__ : Optional[Any] = 'coco-detection-id2label.json' snake_case__ : str = json.load(open(hf_hub_download(A__ , A__ , repo_type='dataset' ) , 'r' ) ) snake_case__ : List[Any] = {int(A__ ): v for k, v in idalabel.items()} snake_case__ : Any = idalabel snake_case__ : int = {v: k for k, v in idalabel.items()} # load image processor snake_case__ : List[Any] = 'coco_panoptic' if is_panoptic else 'coco_detection' snake_case__ : List[Any] = ConditionalDetrImageProcessor(format=A__ ) # prepare image snake_case__ : List[str] = prepare_img() snake_case__ : Any = image_processor(images=A__ , return_tensors='pt' ) snake_case__ : Dict = encoding['pixel_values'] logger.info(F"""Converting model {model_name}...""" ) # load original model from torch hub snake_case__ : Any = torch.hub.load('DeppMeng/ConditionalDETR' , A__ , pretrained=A__ ).eval() snake_case__ : Tuple = conditional_detr.state_dict() # rename keys for src, dest in rename_keys: if is_panoptic: snake_case__ : List[Any] = 'conditional_detr.' + src rename_key(A__ , A__ , A__ ) snake_case__ : Dict = rename_backbone_keys(A__ ) # query, key and value matrices need special treatment read_in_q_k_v(A__ , is_panoptic=A__ ) # important: we need to prepend a prefix to each of the base model keys as the head models use different attributes for them snake_case__ : Optional[int] = 'conditional_detr.model.' if is_panoptic else 'model.' for key in state_dict.copy().keys(): if is_panoptic: if ( key.startswith('conditional_detr' ) and not key.startswith('class_labels_classifier' ) and not key.startswith('bbox_predictor' ) ): snake_case__ : List[Any] = state_dict.pop(A__ ) snake_case__ : Optional[int] = val elif "class_labels_classifier" in key or "bbox_predictor" in key: snake_case__ : str = state_dict.pop(A__ ) snake_case__ : List[Any] = val elif key.startswith('bbox_attention' ) or key.startswith('mask_head' ): continue else: snake_case__ : Union[str, Any] = state_dict.pop(A__ ) snake_case__ : Dict = val else: if not key.startswith('class_labels_classifier' ) and not key.startswith('bbox_predictor' ): snake_case__ : List[Any] = state_dict.pop(A__ ) snake_case__ : Optional[int] = val # finally, create HuggingFace model and load state dict snake_case__ : Union[str, Any] = ConditionalDetrForSegmentation(A__ ) if is_panoptic else ConditionalDetrForObjectDetection(A__ ) model.load_state_dict(A__ ) model.eval() model.push_to_hub(repo_id=A__ , organization='DepuMeng' , commit_message='Add model' ) # verify our conversion snake_case__ : Tuple = conditional_detr(A__ ) snake_case__ : str = model(A__ ) assert torch.allclose(outputs.logits , original_outputs['pred_logits'] , atol=1e-4 ) assert torch.allclose(outputs.pred_boxes , original_outputs['pred_boxes'] , atol=1e-4 ) if is_panoptic: assert torch.allclose(outputs.pred_masks , original_outputs['pred_masks'] , atol=1e-4 ) # Save model and image processor logger.info(F"""Saving PyTorch model and image processor to {pytorch_dump_folder_path}...""" ) Path(A__ ).mkdir(exist_ok=A__ ) model.save_pretrained(A__ ) image_processor.save_pretrained(A__ ) if __name__ == "__main__": lowerCAmelCase__ : Any = argparse.ArgumentParser() parser.add_argument( '''--model_name''', default='''conditional_detr_resnet50''', type=str, help='''Name of the CONDITIONAL_DETR model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the folder to output PyTorch model.''' ) lowerCAmelCase__ : int = parser.parse_args() convert_conditional_detr_checkpoint(args.model_name, args.pytorch_dump_folder_path)
699
1
import argparse import json from collections import OrderedDict from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( ConditionalDetrConfig, ConditionalDetrForObjectDetection, ConditionalDetrForSegmentation, ConditionalDetrImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase__ : Dict = logging.get_logger(__name__) # here we list all keys to be renamed (original name on the left, our name on the right) lowerCAmelCase__ : Optional[Any] = [] for i in range(6): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append( (F'''transformer.encoder.layers.{i}.self_attn.out_proj.weight''', F'''encoder.layers.{i}.self_attn.out_proj.weight''') ) rename_keys.append( (F'''transformer.encoder.layers.{i}.self_attn.out_proj.bias''', F'''encoder.layers.{i}.self_attn.out_proj.bias''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.linear1.weight''', F'''encoder.layers.{i}.fc1.weight''')) rename_keys.append((F'''transformer.encoder.layers.{i}.linear1.bias''', F'''encoder.layers.{i}.fc1.bias''')) rename_keys.append((F'''transformer.encoder.layers.{i}.linear2.weight''', F'''encoder.layers.{i}.fc2.weight''')) rename_keys.append((F'''transformer.encoder.layers.{i}.linear2.bias''', F'''encoder.layers.{i}.fc2.bias''')) rename_keys.append( (F'''transformer.encoder.layers.{i}.norm1.weight''', F'''encoder.layers.{i}.self_attn_layer_norm.weight''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.norm1.bias''', F'''encoder.layers.{i}.self_attn_layer_norm.bias''')) rename_keys.append((F'''transformer.encoder.layers.{i}.norm2.weight''', F'''encoder.layers.{i}.final_layer_norm.weight''')) rename_keys.append((F'''transformer.encoder.layers.{i}.norm2.bias''', F'''encoder.layers.{i}.final_layer_norm.bias''')) # decoder layers: 2 times output projection, 2 feedforward neural networks and 3 layernorms rename_keys.append( (F'''transformer.decoder.layers.{i}.self_attn.out_proj.weight''', F'''decoder.layers.{i}.self_attn.out_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.self_attn.out_proj.bias''', F'''decoder.layers.{i}.self_attn.out_proj.bias''') ) rename_keys.append( ( F'''transformer.decoder.layers.{i}.cross_attn.out_proj.weight''', F'''decoder.layers.{i}.encoder_attn.out_proj.weight''', ) ) rename_keys.append( ( F'''transformer.decoder.layers.{i}.cross_attn.out_proj.bias''', F'''decoder.layers.{i}.encoder_attn.out_proj.bias''', ) ) rename_keys.append((F'''transformer.decoder.layers.{i}.linear1.weight''', F'''decoder.layers.{i}.fc1.weight''')) rename_keys.append((F'''transformer.decoder.layers.{i}.linear1.bias''', F'''decoder.layers.{i}.fc1.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.linear2.weight''', F'''decoder.layers.{i}.fc2.weight''')) rename_keys.append((F'''transformer.decoder.layers.{i}.linear2.bias''', F'''decoder.layers.{i}.fc2.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.norm1.weight''', F'''decoder.layers.{i}.self_attn_layer_norm.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.norm1.bias''', F'''decoder.layers.{i}.self_attn_layer_norm.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.norm2.weight''', F'''decoder.layers.{i}.encoder_attn_layer_norm.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.norm2.bias''', F'''decoder.layers.{i}.encoder_attn_layer_norm.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.norm3.weight''', F'''decoder.layers.{i}.final_layer_norm.weight''')) rename_keys.append((F'''transformer.decoder.layers.{i}.norm3.bias''', F'''decoder.layers.{i}.final_layer_norm.bias''')) # q, k, v projections in self/cross-attention in decoder for conditional DETR rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_qcontent_proj.weight''', F'''decoder.layers.{i}.sa_qcontent_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_kcontent_proj.weight''', F'''decoder.layers.{i}.sa_kcontent_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_qpos_proj.weight''', F'''decoder.layers.{i}.sa_qpos_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_kpos_proj.weight''', F'''decoder.layers.{i}.sa_kpos_proj.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_v_proj.weight''', F'''decoder.layers.{i}.sa_v_proj.weight''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qcontent_proj.weight''', F'''decoder.layers.{i}.ca_qcontent_proj.weight''') ) # rename_keys.append((f"transformer.decoder.layers.{i}.ca_qpos_proj.weight", f"decoder.layers.{i}.ca_qpos_proj.weight")) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_kcontent_proj.weight''', F'''decoder.layers.{i}.ca_kcontent_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_kpos_proj.weight''', F'''decoder.layers.{i}.ca_kpos_proj.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.ca_v_proj.weight''', F'''decoder.layers.{i}.ca_v_proj.weight''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qpos_sine_proj.weight''', F'''decoder.layers.{i}.ca_qpos_sine_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_qcontent_proj.bias''', F'''decoder.layers.{i}.sa_qcontent_proj.bias''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_kcontent_proj.bias''', F'''decoder.layers.{i}.sa_kcontent_proj.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_qpos_proj.bias''', F'''decoder.layers.{i}.sa_qpos_proj.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_kpos_proj.bias''', F'''decoder.layers.{i}.sa_kpos_proj.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_v_proj.bias''', F'''decoder.layers.{i}.sa_v_proj.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qcontent_proj.bias''', F'''decoder.layers.{i}.ca_qcontent_proj.bias''') ) # rename_keys.append((f"transformer.decoder.layers.{i}.ca_qpos_proj.bias", f"decoder.layers.{i}.ca_qpos_proj.bias")) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_kcontent_proj.bias''', F'''decoder.layers.{i}.ca_kcontent_proj.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.ca_kpos_proj.bias''', F'''decoder.layers.{i}.ca_kpos_proj.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.ca_v_proj.bias''', F'''decoder.layers.{i}.ca_v_proj.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qpos_sine_proj.bias''', F'''decoder.layers.{i}.ca_qpos_sine_proj.bias''') ) # convolutional projection + query embeddings + layernorm of decoder + class and bounding box heads # for conditional DETR, also convert reference point head and query scale MLP rename_keys.extend( [ ('''input_proj.weight''', '''input_projection.weight'''), ('''input_proj.bias''', '''input_projection.bias'''), ('''query_embed.weight''', '''query_position_embeddings.weight'''), ('''transformer.decoder.norm.weight''', '''decoder.layernorm.weight'''), ('''transformer.decoder.norm.bias''', '''decoder.layernorm.bias'''), ('''class_embed.weight''', '''class_labels_classifier.weight'''), ('''class_embed.bias''', '''class_labels_classifier.bias'''), ('''bbox_embed.layers.0.weight''', '''bbox_predictor.layers.0.weight'''), ('''bbox_embed.layers.0.bias''', '''bbox_predictor.layers.0.bias'''), ('''bbox_embed.layers.1.weight''', '''bbox_predictor.layers.1.weight'''), ('''bbox_embed.layers.1.bias''', '''bbox_predictor.layers.1.bias'''), ('''bbox_embed.layers.2.weight''', '''bbox_predictor.layers.2.weight'''), ('''bbox_embed.layers.2.bias''', '''bbox_predictor.layers.2.bias'''), ('''transformer.decoder.ref_point_head.layers.0.weight''', '''decoder.ref_point_head.layers.0.weight'''), ('''transformer.decoder.ref_point_head.layers.0.bias''', '''decoder.ref_point_head.layers.0.bias'''), ('''transformer.decoder.ref_point_head.layers.1.weight''', '''decoder.ref_point_head.layers.1.weight'''), ('''transformer.decoder.ref_point_head.layers.1.bias''', '''decoder.ref_point_head.layers.1.bias'''), ('''transformer.decoder.query_scale.layers.0.weight''', '''decoder.query_scale.layers.0.weight'''), ('''transformer.decoder.query_scale.layers.0.bias''', '''decoder.query_scale.layers.0.bias'''), ('''transformer.decoder.query_scale.layers.1.weight''', '''decoder.query_scale.layers.1.weight'''), ('''transformer.decoder.query_scale.layers.1.bias''', '''decoder.query_scale.layers.1.bias'''), ('''transformer.decoder.layers.0.ca_qpos_proj.weight''', '''decoder.layers.0.ca_qpos_proj.weight'''), ('''transformer.decoder.layers.0.ca_qpos_proj.bias''', '''decoder.layers.0.ca_qpos_proj.bias'''), ] ) def UpperCamelCase__ ( A__ , A__ , A__ ) -> List[str]: snake_case__ : int = state_dict.pop(A__ ) snake_case__ : Union[str, Any] = val def UpperCamelCase__ ( A__ ) -> int: snake_case__ : List[Any] = OrderedDict() for key, value in state_dict.items(): if "backbone.0.body" in key: snake_case__ : Any = key.replace('backbone.0.body' , 'backbone.conv_encoder.model' ) snake_case__ : Optional[int] = value else: snake_case__ : Optional[int] = value return new_state_dict def UpperCamelCase__ ( A__ , A__=False ) -> Optional[int]: snake_case__ : Optional[int] = '' if is_panoptic: snake_case__ : Tuple = 'conditional_detr.' # first: transformer encoder for i in range(6 ): # read in weights + bias of input projection layer (in PyTorch's MultiHeadAttention, this is a single matrix + bias) snake_case__ : int = state_dict.pop(F"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_weight""" ) snake_case__ : str = state_dict.pop(F"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict snake_case__ : Union[str, Any] = in_proj_weight[:256, :] snake_case__ : Union[str, Any] = in_proj_bias[:256] snake_case__ : Union[str, Any] = in_proj_weight[256:512, :] snake_case__ : Optional[Any] = in_proj_bias[256:512] snake_case__ : List[str] = in_proj_weight[-256:, :] snake_case__ : Tuple = in_proj_bias[-256:] def UpperCamelCase__ ( ) -> Tuple: snake_case__ : int = 'http://images.cocodataset.org/val2017/000000039769.jpg' snake_case__ : str = Image.open(requests.get(A__ , stream=A__ ).raw ) return im @torch.no_grad() def UpperCamelCase__ ( A__ , A__ ) -> str: snake_case__ : List[Any] = ConditionalDetrConfig() # set backbone and dilation attributes if "resnet101" in model_name: snake_case__ : Any = 'resnet101' if "dc5" in model_name: snake_case__ : Any = True snake_case__ : int = 'panoptic' in model_name if is_panoptic: snake_case__ : str = 250 else: snake_case__ : Union[str, Any] = 91 snake_case__ : Optional[int] = 'huggingface/label-files' snake_case__ : Optional[Any] = 'coco-detection-id2label.json' snake_case__ : str = json.load(open(hf_hub_download(A__ , A__ , repo_type='dataset' ) , 'r' ) ) snake_case__ : List[Any] = {int(A__ ): v for k, v in idalabel.items()} snake_case__ : Any = idalabel snake_case__ : int = {v: k for k, v in idalabel.items()} # load image processor snake_case__ : List[Any] = 'coco_panoptic' if is_panoptic else 'coco_detection' snake_case__ : List[Any] = ConditionalDetrImageProcessor(format=A__ ) # prepare image snake_case__ : List[str] = prepare_img() snake_case__ : Any = image_processor(images=A__ , return_tensors='pt' ) snake_case__ : Dict = encoding['pixel_values'] logger.info(F"""Converting model {model_name}...""" ) # load original model from torch hub snake_case__ : Any = torch.hub.load('DeppMeng/ConditionalDETR' , A__ , pretrained=A__ ).eval() snake_case__ : Tuple = conditional_detr.state_dict() # rename keys for src, dest in rename_keys: if is_panoptic: snake_case__ : List[Any] = 'conditional_detr.' + src rename_key(A__ , A__ , A__ ) snake_case__ : Dict = rename_backbone_keys(A__ ) # query, key and value matrices need special treatment read_in_q_k_v(A__ , is_panoptic=A__ ) # important: we need to prepend a prefix to each of the base model keys as the head models use different attributes for them snake_case__ : Optional[int] = 'conditional_detr.model.' if is_panoptic else 'model.' for key in state_dict.copy().keys(): if is_panoptic: if ( key.startswith('conditional_detr' ) and not key.startswith('class_labels_classifier' ) and not key.startswith('bbox_predictor' ) ): snake_case__ : List[Any] = state_dict.pop(A__ ) snake_case__ : Optional[int] = val elif "class_labels_classifier" in key or "bbox_predictor" in key: snake_case__ : str = state_dict.pop(A__ ) snake_case__ : List[Any] = val elif key.startswith('bbox_attention' ) or key.startswith('mask_head' ): continue else: snake_case__ : Union[str, Any] = state_dict.pop(A__ ) snake_case__ : Dict = val else: if not key.startswith('class_labels_classifier' ) and not key.startswith('bbox_predictor' ): snake_case__ : List[Any] = state_dict.pop(A__ ) snake_case__ : Optional[int] = val # finally, create HuggingFace model and load state dict snake_case__ : Union[str, Any] = ConditionalDetrForSegmentation(A__ ) if is_panoptic else ConditionalDetrForObjectDetection(A__ ) model.load_state_dict(A__ ) model.eval() model.push_to_hub(repo_id=A__ , organization='DepuMeng' , commit_message='Add model' ) # verify our conversion snake_case__ : Tuple = conditional_detr(A__ ) snake_case__ : str = model(A__ ) assert torch.allclose(outputs.logits , original_outputs['pred_logits'] , atol=1e-4 ) assert torch.allclose(outputs.pred_boxes , original_outputs['pred_boxes'] , atol=1e-4 ) if is_panoptic: assert torch.allclose(outputs.pred_masks , original_outputs['pred_masks'] , atol=1e-4 ) # Save model and image processor logger.info(F"""Saving PyTorch model and image processor to {pytorch_dump_folder_path}...""" ) Path(A__ ).mkdir(exist_ok=A__ ) model.save_pretrained(A__ ) image_processor.save_pretrained(A__ ) if __name__ == "__main__": lowerCAmelCase__ : Any = argparse.ArgumentParser() parser.add_argument( '''--model_name''', default='''conditional_detr_resnet50''', type=str, help='''Name of the CONDITIONAL_DETR model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the folder to output PyTorch model.''' ) lowerCAmelCase__ : int = parser.parse_args() convert_conditional_detr_checkpoint(args.model_name, args.pytorch_dump_folder_path)
699
from collections import namedtuple lowerCAmelCase__ : Union[str, Any] = namedtuple('''from_to''', '''from_ to''') lowerCAmelCase__ : Tuple = { '''cubicmeter''': from_to(1, 1), '''litre''': from_to(0.0_01, 10_00), '''kilolitre''': from_to(1, 1), '''gallon''': from_to(0.0_04_54, 2_64.1_72), '''cubicyard''': from_to(0.7_64_55, 1.3_07_95), '''cubicfoot''': from_to(0.0_28, 35.31_47), '''cup''': from_to(0.0_00_23_65_88, 42_26.75), } def UpperCamelCase__ ( A__ , A__ , A__ ) -> float: if from_type not in METRIC_CONVERSION: raise ValueError( F"""Invalid 'from_type' value: {from_type!r} Supported values are:\n""" + ', '.join(A__ ) ) if to_type not in METRIC_CONVERSION: raise ValueError( F"""Invalid 'to_type' value: {to_type!r}. Supported values are:\n""" + ', '.join(A__ ) ) return value * METRIC_CONVERSION[from_type].from_ * METRIC_CONVERSION[to_type].to if __name__ == "__main__": import doctest doctest.testmod()
699
1
import requests from bsa import BeautifulSoup def UpperCamelCase__ ( A__ , A__ ) -> str: snake_case__ : Dict = BeautifulSoup(requests.get(A__ , params=A__ ).content , 'html.parser' ) snake_case__ : Any = soup.find('div' , attrs={'class': 'gs_ri'} ) snake_case__ : Optional[Any] = div.find('div' , attrs={'class': 'gs_fl'} ).find_all('a' ) return anchors[2].get_text() if __name__ == "__main__": lowerCAmelCase__ : int = { '''title''': ( '''Precisely geometry controlled microsupercapacitors for ultrahigh areal ''' '''capacitance, volumetric capacitance, and energy density''' ), '''journal''': '''Chem. Mater.''', '''volume''': 30, '''pages''': '''3979-3990''', '''year''': 20_18, '''hl''': '''en''', } print(get_citation('''https://scholar.google.com/scholar_lookup''', params=params))
699
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCAmelCase__ : Tuple = logging.get_logger(__name__) lowerCAmelCase__ : Union[str, Any] = '''▁''' lowerCAmelCase__ : List[Any] = {'''vocab_file''': '''sentencepiece.bpe.model'''} lowerCAmelCase__ : Optional[Any] = { '''vocab_file''': { '''facebook/xglm-564M''': '''https://huggingface.co/facebook/xglm-564M/resolve/main/sentencepiece.bpe.model''', } } lowerCAmelCase__ : str = { '''facebook/xglm-564M''': 20_48, } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = ["""input_ids""", """attention_mask"""] def __init__( self , __UpperCamelCase , __UpperCamelCase="<s>" , __UpperCamelCase="</s>" , __UpperCamelCase="</s>" , __UpperCamelCase="<s>" , __UpperCamelCase="<unk>" , __UpperCamelCase="<pad>" , __UpperCamelCase = None , **__UpperCamelCase , ) -> None: '''simple docstring''' snake_case__ : Any = {} if sp_model_kwargs is None else sp_model_kwargs # Compatibility with the original tokenizer snake_case__ : Tuple = 7 snake_case__ : Dict = [F"""<madeupword{i}>""" for i in range(self.num_madeup_words )] snake_case__ : Union[str, Any] = kwargs.get('additional_special_tokens' , [] ) kwargs["additional_special_tokens"] += [ word for word in madeup_words if word not in kwargs["additional_special_tokens"] ] super().__init__( bos_token=__UpperCamelCase , eos_token=__UpperCamelCase , unk_token=__UpperCamelCase , sep_token=__UpperCamelCase , cls_token=__UpperCamelCase , pad_token=__UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , **__UpperCamelCase , ) snake_case__ : Optional[int] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(__UpperCamelCase ) ) snake_case__ : Optional[Any] = 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' # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab snake_case__ : Tuple = 1 # Mimic fairseq token-to-id alignment for the first 4 token snake_case__ : Tuple = {'<s>': 0, '<pad>': 1, '</s>': 2, '<unk>': 3} snake_case__ : List[Any] = len(self.sp_model ) snake_case__ : Optional[Any] = {F"""<madeupword{i}>""": sp_size + i + self.fairseq_offset for i in range(self.num_madeup_words )} self.fairseq_tokens_to_ids.update(__UpperCamelCase ) snake_case__ : Union[str, Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def __getstate__( self ) -> List[Any]: '''simple docstring''' snake_case__ : Union[str, Any] = self.__dict__.copy() snake_case__ : Optional[Any] = None snake_case__ : Tuple = self.sp_model.serialized_model_proto() return state def __setstate__( self , __UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Union[str, Any] = d # for backward compatibility if not hasattr(self , 'sp_model_kwargs' ): snake_case__ : Any = {} snake_case__ : Dict = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.LoadFromSerializedProto(self.sp_model_proto ) def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return [self.sep_token_id] + token_ids_a snake_case__ : str = [self.sep_token_id] return sep + token_ids_a + sep + sep + token_ids_a def __a ( self , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__UpperCamelCase , token_ids_a=__UpperCamelCase , already_has_special_tokens=__UpperCamelCase ) if token_ids_a is None: return [1] + ([0] * len(__UpperCamelCase )) return [1] + ([0] * len(__UpperCamelCase )) + [1, 1] + ([0] * len(__UpperCamelCase )) def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> List[int]: '''simple docstring''' snake_case__ : int = [self.sep_token_id] if token_ids_a is None: return len(sep + token_ids_a ) * [0] return len(sep + token_ids_a + sep + sep + token_ids_a ) * [0] @property def __a ( self ) -> Tuple: '''simple docstring''' return len(self.sp_model ) + self.fairseq_offset + self.num_madeup_words def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : int = {self.convert_ids_to_tokens(__UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __a ( self , __UpperCamelCase ) -> List[str]: '''simple docstring''' return self.sp_model.encode(__UpperCamelCase , out_type=__UpperCamelCase ) def __a ( self , __UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] snake_case__ : Optional[Any] = self.sp_model.PieceToId(__UpperCamelCase ) # 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 __a ( self , __UpperCamelCase ) -> Dict: '''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 __a ( self , __UpperCamelCase ) -> int: '''simple docstring''' snake_case__ : int = ''.join(__UpperCamelCase ).replace(__UpperCamelCase , ' ' ).strip() return out_string def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(__UpperCamelCase ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return snake_case__ : List[str] = 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __UpperCamelCase ) elif not os.path.isfile(self.vocab_file ): with open(__UpperCamelCase , 'wb' ) as fi: snake_case__ : Any = self.sp_model.serialized_model_proto() fi.write(__UpperCamelCase ) return (out_vocab_file,)
699
1
import numpy as np lowerCAmelCase__ : Any = [ ['''a''', '''b''', '''c''', '''d''', '''e'''], ['''f''', '''g''', '''h''', '''i''', '''k'''], ['''l''', '''m''', '''n''', '''o''', '''p'''], ['''q''', '''r''', '''s''', '''t''', '''u'''], ['''v''', '''w''', '''x''', '''y''', '''z'''], ] class __snake_case : def __init__( self ) -> None: '''simple docstring''' snake_case__ : List[str] = np.array(__UpperCamelCase ) def __a ( self , __UpperCamelCase ) -> np.ndarray: '''simple docstring''' snake_case__ , snake_case__ : Union[str, Any] = np.where(letter == self.SQUARE ) snake_case__ : int = np.concatenate([indexa + 1, indexa + 1] ) return indexes def __a ( self , __UpperCamelCase , __UpperCamelCase ) -> str: '''simple docstring''' snake_case__ : int = self.SQUARE[indexa - 1, indexa - 1] return letter def __a ( self , __UpperCamelCase ) -> str: '''simple docstring''' snake_case__ : Any = message.lower() snake_case__ : Optional[Any] = message.replace(' ' , '' ) snake_case__ : int = message.replace('j' , 'i' ) snake_case__ : Optional[int] = np.empty((2, len(__UpperCamelCase )) ) for letter_index in range(len(__UpperCamelCase ) ): snake_case__ : Optional[int] = self.letter_to_numbers(message[letter_index] ) snake_case__ : List[str] = numbers[0] snake_case__ : Union[str, Any] = numbers[1] snake_case__ : Optional[Any] = first_step.reshape(2 * len(__UpperCamelCase ) ) snake_case__ : Dict = '' for numbers_index in range(len(__UpperCamelCase ) ): snake_case__ : Dict = int(second_step[numbers_index * 2] ) snake_case__ : str = int(second_step[(numbers_index * 2) + 1] ) snake_case__ : Dict = self.numbers_to_letter(__UpperCamelCase , __UpperCamelCase ) snake_case__ : List[str] = encoded_message + letter return encoded_message def __a ( self , __UpperCamelCase ) -> str: '''simple docstring''' snake_case__ : Dict = message.lower() message.replace(' ' , '' ) snake_case__ : str = np.empty(2 * len(__UpperCamelCase ) ) for letter_index in range(len(__UpperCamelCase ) ): snake_case__ : Tuple = self.letter_to_numbers(message[letter_index] ) snake_case__ : Any = numbers[0] snake_case__ : List[Any] = numbers[1] snake_case__ : Any = first_step.reshape((2, len(__UpperCamelCase )) ) snake_case__ : Tuple = '' for numbers_index in range(len(__UpperCamelCase ) ): snake_case__ : Union[str, Any] = int(second_step[0, numbers_index] ) snake_case__ : Any = int(second_step[1, numbers_index] ) snake_case__ : Optional[int] = self.numbers_to_letter(__UpperCamelCase , __UpperCamelCase ) snake_case__ : Tuple = decoded_message + letter return decoded_message
699
import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert_fast import BertTokenizerFast from .tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer, DPRReaderTokenizer lowerCAmelCase__ : Any = logging.get_logger(__name__) lowerCAmelCase__ : List[Any] = {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''} lowerCAmelCase__ : Any = { '''vocab_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } lowerCAmelCase__ : Any = { '''vocab_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } lowerCAmelCase__ : Tuple = { '''vocab_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json''' ), }, } lowerCAmelCase__ : Dict = { '''facebook/dpr-ctx_encoder-single-nq-base''': 5_12, '''facebook/dpr-ctx_encoder-multiset-base''': 5_12, } lowerCAmelCase__ : Union[str, Any] = { '''facebook/dpr-question_encoder-single-nq-base''': 5_12, '''facebook/dpr-question_encoder-multiset-base''': 5_12, } lowerCAmelCase__ : Optional[Any] = { '''facebook/dpr-reader-single-nq-base''': 5_12, '''facebook/dpr-reader-multiset-base''': 5_12, } lowerCAmelCase__ : Tuple = { '''facebook/dpr-ctx_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-ctx_encoder-multiset-base''': {'''do_lower_case''': True}, } lowerCAmelCase__ : Any = { '''facebook/dpr-question_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-question_encoder-multiset-base''': {'''do_lower_case''': True}, } lowerCAmelCase__ : List[str] = { '''facebook/dpr-reader-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-reader-multiset-base''': {'''do_lower_case''': True}, } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION __lowerCamelCase = DPRContextEncoderTokenizer class __snake_case ( _lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION __lowerCamelCase = DPRQuestionEncoderTokenizer lowerCAmelCase__ : Tuple = collections.namedtuple( '''DPRSpanPrediction''', ['''span_score''', '''relevance_score''', '''doc_id''', '''start_index''', '''end_index''', '''text'''] ) lowerCAmelCase__ : List[Any] = collections.namedtuple('''DPRReaderOutput''', ['''start_logits''', '''end_logits''', '''relevance_logits''']) lowerCAmelCase__ : int = r''' Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`. It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers), using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)` with the format: [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids> Args: questions (`str` or `List[str]`): The questions to be encoded. You can specify one question for many passages. In this case, the question will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in `titles` or `texts`. titles (`str` or `List[str]`): The passages titles to be encoded. This can be a string or a list of strings if there are several passages. texts (`str` or `List[str]`): The passages texts to be encoded. This can be a string or a list of strings if there are several passages. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`): Activates and controls padding. Accepts the following values: - `True` or `\'longest\'`: Pad to the longest sequence in the batch (or no padding if only a single sequence if provided). - `\'max_length\'`: Pad to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. - `False` or `\'do_not_pad\'` (default): No padding (i.e., can output a batch with sequences of different lengths). truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`): Activates and controls truncation. Accepts the following values: - `True` or `\'longest_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will truncate token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch of pairs) is provided. - `\'only_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `\'only_second\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `False` or `\'do_not_truncate\'` (default): No truncation (i.e., can output batch with sequence lengths greater than the model maximum admissible input size). max_length (`int`, *optional*): Controls the maximum length to use by one of the truncation/padding parameters. If left unset or set to `None`, this will use the predefined model maximum length if a maximum length is required by one of the truncation/padding parameters. If the model has no specific maximum input length (like XLNet) truncation/padding to a maximum length will be deactivated. return_tensors (`str` or [`~utils.TensorType`], *optional*): If set, will return tensors instead of list of python integers. Acceptable values are: - `\'tf\'`: Return TensorFlow `tf.constant` objects. - `\'pt\'`: Return PyTorch `torch.Tensor` objects. - `\'np\'`: Return Numpy `np.ndarray` objects. return_attention_mask (`bool`, *optional*): Whether or not to return the attention mask. If not set, will return the attention mask according to the specific tokenizer\'s default, defined by the `return_outputs` attribute. [What are attention masks?](../glossary#attention-mask) Return: `Dict[str, List[List[int]]]`: A dictionary with the following keys: - `input_ids`: List of token ids to be fed to a model. - `attention_mask`: List of indices specifying which tokens should be attended to by the model. ''' @add_start_docstrings(_lowerCamelCase ) class __snake_case : def __call__( self , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = False , __UpperCamelCase = False , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = None , **__UpperCamelCase , ) -> BatchEncoding: '''simple docstring''' if titles is None and texts is None: return super().__call__( __UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase , max_length=__UpperCamelCase , return_tensors=__UpperCamelCase , return_attention_mask=__UpperCamelCase , **__UpperCamelCase , ) elif titles is None or texts is None: snake_case__ : Optional[Any] = titles if texts is None else texts return super().__call__( __UpperCamelCase , __UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase , max_length=__UpperCamelCase , return_tensors=__UpperCamelCase , return_attention_mask=__UpperCamelCase , **__UpperCamelCase , ) snake_case__ : int = titles if not isinstance(__UpperCamelCase , __UpperCamelCase ) else [titles] snake_case__ : Optional[int] = texts if not isinstance(__UpperCamelCase , __UpperCamelCase ) else [texts] snake_case__ : List[Any] = len(__UpperCamelCase ) snake_case__ : str = questions if not isinstance(__UpperCamelCase , __UpperCamelCase ) else [questions] * n_passages assert len(__UpperCamelCase ) == len( __UpperCamelCase ), F"""There should be as many titles than texts but got {len(__UpperCamelCase )} titles and {len(__UpperCamelCase )} texts.""" snake_case__ : Optional[int] = super().__call__(__UpperCamelCase , __UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase )['input_ids'] snake_case__ : Optional[Any] = super().__call__(__UpperCamelCase , add_special_tokens=__UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase )['input_ids'] snake_case__ : Union[str, Any] = { 'input_ids': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(__UpperCamelCase , __UpperCamelCase ) ] } if return_attention_mask is not False: snake_case__ : List[Any] = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) snake_case__ : Union[str, Any] = attention_mask return self.pad(__UpperCamelCase , padding=__UpperCamelCase , max_length=__UpperCamelCase , return_tensors=__UpperCamelCase ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = 16 , __UpperCamelCase = 64 , __UpperCamelCase = 4 , ) -> List[DPRSpanPrediction]: '''simple docstring''' snake_case__ : Optional[Any] = reader_input['input_ids'] snake_case__ , snake_case__ , snake_case__ : Any = reader_output[:3] snake_case__ : List[str] = len(__UpperCamelCase ) snake_case__ : Tuple = sorted(range(__UpperCamelCase ) , reverse=__UpperCamelCase , key=relevance_logits.__getitem__ ) snake_case__ : List[DPRReaderOutput] = [] for doc_id in sorted_docs: snake_case__ : Tuple = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence snake_case__ : int = sequence_ids.index(self.sep_token_id , 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: snake_case__ : Union[str, Any] = sequence_ids.index(self.pad_token_id ) else: snake_case__ : str = len(__UpperCamelCase ) snake_case__ : Dict = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=__UpperCamelCase , top_spans=__UpperCamelCase , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=__UpperCamelCase , start_index=__UpperCamelCase , end_index=__UpperCamelCase , text=self.decode(sequence_ids[start_index : end_index + 1] ) , ) ) if len(__UpperCamelCase ) >= num_spans: break return nbest_spans_predictions[:num_spans] def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , ) -> List[DPRSpanPrediction]: '''simple docstring''' snake_case__ : Any = [] for start_index, start_score in enumerate(__UpperCamelCase ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) snake_case__ : str = sorted(__UpperCamelCase , key=lambda __UpperCamelCase : x[1] , reverse=__UpperCamelCase ) snake_case__ : Any = [] for (start_index, end_index), score in scores: assert start_index <= end_index, F"""Wrong span indices: [{start_index}:{end_index}]""" snake_case__ : str = end_index - start_index + 1 assert length <= max_answer_length, F"""Span is too long: {length} > {max_answer_length}""" if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(__UpperCamelCase ) == top_spans: break return chosen_span_intervals @add_end_docstrings(_lowerCamelCase ) class __snake_case ( _lowerCamelCase ,_lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = READER_PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = READER_PRETRAINED_INIT_CONFIGURATION __lowerCamelCase = ["""input_ids""", """attention_mask"""] __lowerCamelCase = DPRReaderTokenizer
699
1
from __future__ import annotations lowerCAmelCase__ : str = 1.6_021E-19 # units = C def UpperCamelCase__ ( A__ , A__ , A__ , ) -> tuple[str, float]: if (conductivity, electron_conc, mobility).count(0 ) != 1: raise ValueError('You cannot supply more or less than 2 values' ) elif conductivity < 0: raise ValueError('Conductivity cannot be negative' ) elif electron_conc < 0: raise ValueError('Electron concentration cannot be negative' ) elif mobility < 0: raise ValueError('mobility cannot be negative' ) elif conductivity == 0: return ( "conductivity", mobility * electron_conc * ELECTRON_CHARGE, ) elif electron_conc == 0: return ( "electron_conc", conductivity / (mobility * ELECTRON_CHARGE), ) else: return ( "mobility", conductivity / (electron_conc * ELECTRON_CHARGE), ) if __name__ == "__main__": import doctest doctest.testmod()
699
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, EulerAncestralDiscreteScheduler, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionInstructPixaPixPipeline, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.utils import floats_tensor, load_image, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( IMAGE_TO_IMAGE_IMAGE_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __snake_case ( _lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ,unittest.TestCase ): __lowerCamelCase = StableDiffusionInstructPixaPixPipeline __lowerCamelCase = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""height""", """width""", """cross_attention_kwargs"""} __lowerCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __lowerCamelCase = IMAGE_TO_IMAGE_IMAGE_PARAMS __lowerCamelCase = IMAGE_TO_IMAGE_IMAGE_PARAMS def __a ( self ) -> List[Any]: '''simple docstring''' torch.manual_seed(0 ) snake_case__ : Tuple = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=8 , out_channels=4 , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D') , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D') , cross_attention_dim=32 , ) snake_case__ : Any = PNDMScheduler(skip_prk_steps=__UpperCamelCase ) torch.manual_seed(0 ) snake_case__ : Dict = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] , up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] , latent_channels=4 , ) torch.manual_seed(0 ) snake_case__ : int = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) snake_case__ : Tuple = CLIPTextModel(__UpperCamelCase ) snake_case__ : Optional[int] = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) snake_case__ : Optional[int] = { 'unet': unet, 'scheduler': scheduler, 'vae': vae, 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'safety_checker': None, 'feature_extractor': None, } return components def __a ( self , __UpperCamelCase , __UpperCamelCase=0 ) -> Dict: '''simple docstring''' snake_case__ : Optional[Any] = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) snake_case__ : int = image.cpu().permute(0 , 2 , 3 , 1 )[0] snake_case__ : Union[str, Any] = Image.fromarray(np.uinta(__UpperCamelCase ) ).convert('RGB' ) if str(__UpperCamelCase ).startswith('mps' ): snake_case__ : str = torch.manual_seed(__UpperCamelCase ) else: snake_case__ : Dict = torch.Generator(device=__UpperCamelCase ).manual_seed(__UpperCamelCase ) snake_case__ : str = { 'prompt': 'A painting of a squirrel eating a burger', 'image': image, 'generator': generator, 'num_inference_steps': 2, 'guidance_scale': 6.0, 'image_guidance_scale': 1, 'output_type': 'numpy', } return inputs def __a ( self ) -> Tuple: '''simple docstring''' snake_case__ : Dict = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : Optional[int] = self.get_dummy_components() snake_case__ : str = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : Optional[int] = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Tuple = self.get_dummy_inputs(__UpperCamelCase ) snake_case__ : List[str] = sd_pipe(**__UpperCamelCase ).images snake_case__ : List[str] = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) snake_case__ : str = np.array([0.7_5_2_6, 0.3_7_5_0, 0.4_5_4_7, 0.6_1_1_7, 0.5_8_6_6, 0.5_0_1_6, 0.4_3_2_7, 0.5_6_4_2, 0.4_8_1_5] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __a ( self ) -> Any: '''simple docstring''' snake_case__ : Dict = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : Union[str, Any] = self.get_dummy_components() snake_case__ : Optional[Any] = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : List[Any] = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Union[str, Any] = self.get_dummy_inputs(__UpperCamelCase ) snake_case__ : List[str] = 'french fries' snake_case__ : Optional[Any] = sd_pipe(**__UpperCamelCase , negative_prompt=__UpperCamelCase ) snake_case__ : Union[str, Any] = output.images snake_case__ : Tuple = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) snake_case__ : Any = np.array([0.7_5_1_1, 0.3_6_4_2, 0.4_5_5_3, 0.6_2_3_6, 0.5_7_9_7, 0.5_0_1_3, 0.4_3_4_3, 0.5_6_1_1, 0.4_8_3_1] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __a ( self ) -> int: '''simple docstring''' snake_case__ : Optional[int] = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : List[str] = self.get_dummy_components() snake_case__ : str = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : str = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Dict = self.get_dummy_inputs(__UpperCamelCase ) snake_case__ : Any = [inputs['prompt']] * 2 snake_case__ : Optional[int] = np.array(inputs['image'] ).astype(np.floataa ) / 2_5_5.0 snake_case__ : Optional[int] = torch.from_numpy(__UpperCamelCase ).unsqueeze(0 ).to(__UpperCamelCase ) snake_case__ : Any = image / 2 + 0.5 snake_case__ : Optional[Any] = image.permute(0 , 3 , 1 , 2 ) snake_case__ : List[Any] = image.repeat(2 , 1 , 1 , 1 ) snake_case__ : Optional[int] = sd_pipe(**__UpperCamelCase ).images snake_case__ : Union[str, Any] = image[-1, -3:, -3:, -1] assert image.shape == (2, 32, 32, 3) snake_case__ : List[Any] = np.array([0.5_8_1_2, 0.5_7_4_8, 0.5_2_2_2, 0.5_9_0_8, 0.5_6_9_5, 0.7_1_7_4, 0.6_8_0_4, 0.5_5_2_3, 0.5_5_7_9] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Optional[int] = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : Optional[int] = self.get_dummy_components() snake_case__ : Tuple = EulerAncestralDiscreteScheduler( beta_start=0.0_0_0_8_5 , beta_end=0.0_1_2 , beta_schedule='scaled_linear' ) snake_case__ : int = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : List[str] = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : str = self.get_dummy_inputs(__UpperCamelCase ) snake_case__ : Any = sd_pipe(**__UpperCamelCase ).images snake_case__ : int = image[0, -3:, -3:, -1] snake_case__ : Tuple = [round(__UpperCamelCase , 4 ) for x in image_slice.flatten().tolist()] print(','.join([str(__UpperCamelCase ) for x in slice] ) ) assert image.shape == (1, 32, 32, 3) snake_case__ : List[Any] = np.array([0.7_4_1_7, 0.3_8_4_2, 0.4_7_3_2, 0.5_7_7_6, 0.5_8_9_1, 0.5_1_3_9, 0.4_0_5_2, 0.5_6_7_3, 0.4_9_8_6] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __a ( self ) -> int: '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) def __a ( self ) -> Any: '''simple docstring''' snake_case__ : Optional[int] = self.get_dummy_components() snake_case__ : int = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : Union[str, Any] = VaeImageProcessor(do_resize=__UpperCamelCase , do_normalize=__UpperCamelCase ) snake_case__ : Optional[int] = pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Optional[Any] = pipe(**self.get_dummy_inputs_by_type(__UpperCamelCase , input_image_type='pt' ) )[0] snake_case__ : Union[str, Any] = components['vae'] snake_case__ : str = self.get_dummy_inputs_by_type(__UpperCamelCase , input_image_type='pt' ) for image_param in self.image_latents_params: if image_param in inputs.keys(): snake_case__ : List[str] = vae.encode(inputs[image_param] ).latent_dist.mode() snake_case__ : Dict = pipe(**__UpperCamelCase )[0] snake_case__ : str = np.abs(out - out_latents_inputs ).max() self.assertLess(__UpperCamelCase , 1E-4 , 'passing latents as image input generate different result from passing image' ) @slow @require_torch_gpu class __snake_case ( unittest.TestCase ): def __a ( self ) -> List[str]: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __a ( self , __UpperCamelCase=0 ) -> Dict: '''simple docstring''' snake_case__ : Optional[Any] = torch.manual_seed(__UpperCamelCase ) snake_case__ : List[str] = load_image( 'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_pix2pix/example.jpg' ) snake_case__ : int = { 'prompt': 'turn him into a cyborg', 'image': image, 'generator': generator, 'num_inference_steps': 3, 'guidance_scale': 7.5, 'image_guidance_scale': 1.0, 'output_type': 'numpy', } return inputs def __a ( self ) -> Dict: '''simple docstring''' snake_case__ : Any = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : Tuple = self.get_inputs() snake_case__ : List[Any] = pipe(**__UpperCamelCase ).images snake_case__ : Union[str, Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) snake_case__ : Dict = np.array([0.5_9_0_2, 0.6_0_1_5, 0.6_0_2_7, 0.5_9_8_3, 0.6_0_9_2, 0.6_0_6_1, 0.5_7_6_5, 0.5_7_8_5, 0.5_5_5_5] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def __a ( self ) -> str: '''simple docstring''' snake_case__ : Any = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase ) snake_case__ : Tuple = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : Dict = self.get_inputs() snake_case__ : Dict = pipe(**__UpperCamelCase ).images snake_case__ : Union[str, Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) snake_case__ : List[Any] = np.array([0.6_5_7_8, 0.6_8_1_7, 0.6_9_7_2, 0.6_7_6_1, 0.6_8_5_6, 0.6_9_1_6, 0.6_4_2_8, 0.6_5_1_6, 0.6_3_0_1] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def __a ( self ) -> Optional[Any]: '''simple docstring''' snake_case__ : Tuple = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase ) snake_case__ : Tuple = DDIMScheduler.from_config(pipe.scheduler.config ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : Optional[int] = self.get_inputs() snake_case__ : Optional[int] = pipe(**__UpperCamelCase ).images snake_case__ : Tuple = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) snake_case__ : int = np.array([0.3_8_2_8, 0.3_8_3_4, 0.3_8_1_8, 0.3_7_9_2, 0.3_8_6_5, 0.3_7_5_2, 0.3_7_9_2, 0.3_8_4_7, 0.3_7_5_3] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : int = 0 def callback_fn(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> None: snake_case__ : List[Any] = True nonlocal number_of_steps number_of_steps += 1 if step == 1: snake_case__ : Any = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 64) snake_case__ : int = latents[0, -3:, -3:, -1] snake_case__ : List[str] = np.array([-0.2_4_6_3, -0.4_6_4_4, -0.9_7_5_6, 1.5_1_7_6, 1.4_4_1_4, 0.7_8_6_6, 0.9_8_9_7, 0.8_5_2_1, 0.7_9_8_3] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2 elif step == 2: snake_case__ : Dict = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 64) snake_case__ : Dict = latents[0, -3:, -3:, -1] snake_case__ : Optional[Any] = np.array([-0.2_6_4_4, -0.4_6_2_6, -0.9_6_5_3, 1.5_1_7_6, 1.4_5_5_1, 0.7_6_8_6, 0.9_8_0_5, 0.8_4_5_2, 0.8_1_1_5] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2 snake_case__ : str = False snake_case__ : List[Any] = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase , torch_dtype=torch.floataa ) snake_case__ : int = pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : int = self.get_inputs() pipe(**__UpperCamelCase , callback=__UpperCamelCase , callback_steps=1 ) assert callback_fn.has_been_called assert number_of_steps == 3 def __a ( self ) -> Any: '''simple docstring''' torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() snake_case__ : str = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase , torch_dtype=torch.floataa ) snake_case__ : Dict = pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() snake_case__ : str = self.get_inputs() snake_case__ : Tuple = pipe(**__UpperCamelCase ) snake_case__ : List[Any] = torch.cuda.max_memory_allocated() # make sure that less than 2.2 GB is allocated assert mem_bytes < 2.2 * 10**9 def __a ( self ) -> int: '''simple docstring''' snake_case__ : int = self.get_inputs() # resize to resolution that is divisible by 8 but not 16 or 32 snake_case__ : Tuple = inputs['image'].resize((504, 504) ) snake_case__ : str = 'timbrooks/instruct-pix2pix' snake_case__ : Tuple = StableDiffusionInstructPixaPixPipeline.from_pretrained( __UpperCamelCase , safety_checker=__UpperCamelCase , ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : str = pipe(**__UpperCamelCase ) snake_case__ : List[Any] = output.images[0] snake_case__ : List[Any] = image[255:258, 383:386, -1] assert image.shape == (504, 504, 3) snake_case__ : List[str] = np.array([0.2_7_2_6, 0.2_5_2_9, 0.2_6_6_4, 0.2_6_5_5, 0.2_6_4_1, 0.2_6_4_2, 0.2_5_9_1, 0.2_6_4_9, 0.2_5_9_0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-3
699
1
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig, OnnxSeqaSeqConfigWithPast from ...utils import logging if TYPE_CHECKING: from ...feature_extraction_utils import FeatureExtractionMixin from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import TensorType lowerCAmelCase__ : Union[str, Any] = logging.get_logger(__name__) lowerCAmelCase__ : Union[str, Any] = { '''openai/whisper-base''': '''https://huggingface.co/openai/whisper-base/resolve/main/config.json''', } # fmt: off lowerCAmelCase__ : Optional[int] = [ 1, 2, 7, 8, 9, 10, 14, 25, 26, 27, 28, 29, 31, 58, 59, 60, 61, 62, 63, 90, 91, 92, 93, 3_57, 3_66, 4_38, 5_32, 6_85, 7_05, 7_96, 9_30, 10_58, 12_20, 12_67, 12_79, 13_03, 13_43, 13_77, 13_91, 16_35, 17_82, 18_75, 21_62, 23_61, 24_88, 34_67, 40_08, 42_11, 46_00, 48_08, 52_99, 58_55, 63_29, 72_03, 96_09, 99_59, 1_05_63, 1_07_86, 1_14_20, 1_17_09, 1_19_07, 1_31_63, 1_36_97, 1_37_00, 1_48_08, 1_53_06, 1_64_10, 1_67_91, 1_79_92, 1_92_03, 1_95_10, 2_07_24, 2_23_05, 2_29_35, 2_70_07, 3_01_09, 3_04_20, 3_34_09, 3_49_49, 4_02_83, 4_04_93, 4_05_49, 4_72_82, 4_91_46, 5_02_57, 5_03_59, 5_03_60, 5_03_61 ] lowerCAmelCase__ : Union[str, Any] = [ 1, 2, 7, 8, 9, 10, 14, 25, 26, 27, 28, 29, 31, 58, 59, 60, 61, 62, 63, 90, 91, 92, 93, 3_59, 5_03, 5_22, 5_42, 8_73, 8_93, 9_02, 9_18, 9_22, 9_31, 13_50, 18_53, 19_82, 24_60, 26_27, 32_46, 32_53, 32_68, 35_36, 38_46, 39_61, 41_83, 46_67, 65_85, 66_47, 72_73, 90_61, 93_83, 1_04_28, 1_09_29, 1_19_38, 1_20_33, 1_23_31, 1_25_62, 1_37_93, 1_41_57, 1_46_35, 1_52_65, 1_56_18, 1_65_53, 1_66_04, 1_83_62, 1_89_56, 2_00_75, 2_16_75, 2_25_20, 2_61_30, 2_61_61, 2_64_35, 2_82_79, 2_94_64, 3_16_50, 3_23_02, 3_24_70, 3_68_65, 4_28_63, 4_74_25, 4_98_70, 5_02_54, 5_02_58, 5_03_60, 5_03_61, 5_03_62 ] class __snake_case ( _lowerCamelCase ): __lowerCamelCase = """whisper""" __lowerCamelCase = ["""past_key_values"""] __lowerCamelCase = {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""} def __init__( self , __UpperCamelCase=51865 , __UpperCamelCase=80 , __UpperCamelCase=6 , __UpperCamelCase=4 , __UpperCamelCase=6 , __UpperCamelCase=4 , __UpperCamelCase=1536 , __UpperCamelCase=1536 , __UpperCamelCase=0.0 , __UpperCamelCase=0.0 , __UpperCamelCase=50257 , __UpperCamelCase=True , __UpperCamelCase=True , __UpperCamelCase="gelu" , __UpperCamelCase=256 , __UpperCamelCase=0.0 , __UpperCamelCase=0.0 , __UpperCamelCase=0.0 , __UpperCamelCase=0.0_2 , __UpperCamelCase=False , __UpperCamelCase=1500 , __UpperCamelCase=448 , __UpperCamelCase=50256 , __UpperCamelCase=50256 , __UpperCamelCase=50256 , __UpperCamelCase=None , __UpperCamelCase=[220, 50256] , __UpperCamelCase=False , __UpperCamelCase=256 , __UpperCamelCase=False , __UpperCamelCase=0.0_5 , __UpperCamelCase=10 , __UpperCamelCase=2 , __UpperCamelCase=0.0 , __UpperCamelCase=10 , __UpperCamelCase=0 , __UpperCamelCase=7 , **__UpperCamelCase , ) -> int: '''simple docstring''' snake_case__ : Optional[Any] = vocab_size snake_case__ : Union[str, Any] = num_mel_bins snake_case__ : List[Any] = d_model snake_case__ : Any = encoder_layers snake_case__ : Tuple = encoder_attention_heads snake_case__ : int = decoder_layers snake_case__ : List[Any] = decoder_attention_heads snake_case__ : Tuple = decoder_ffn_dim snake_case__ : str = encoder_ffn_dim snake_case__ : Dict = dropout snake_case__ : Any = attention_dropout snake_case__ : Optional[int] = activation_dropout snake_case__ : int = activation_function snake_case__ : Any = init_std snake_case__ : Dict = encoder_layerdrop snake_case__ : Union[str, Any] = decoder_layerdrop snake_case__ : List[Any] = use_cache snake_case__ : str = encoder_layers snake_case__ : List[str] = scale_embedding # scale factor will be sqrt(d_model) if True snake_case__ : List[Any] = max_source_positions snake_case__ : str = max_target_positions # Audio Classification-specific parameters. Feel free to ignore for other classes. snake_case__ : List[str] = classifier_proj_size snake_case__ : Tuple = use_weighted_layer_sum # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 snake_case__ : Union[str, Any] = apply_spec_augment snake_case__ : str = mask_time_prob snake_case__ : Optional[Any] = mask_time_length snake_case__ : int = mask_time_min_masks snake_case__ : str = mask_feature_prob snake_case__ : Tuple = mask_feature_length snake_case__ : str = mask_feature_min_masks snake_case__ : List[Any] = median_filter_width super().__init__( pad_token_id=__UpperCamelCase , bos_token_id=__UpperCamelCase , eos_token_id=__UpperCamelCase , is_encoder_decoder=__UpperCamelCase , decoder_start_token_id=__UpperCamelCase , suppress_tokens=__UpperCamelCase , begin_suppress_tokens=__UpperCamelCase , **__UpperCamelCase , ) class __snake_case ( _lowerCamelCase ): @property def __a ( self ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' snake_case__ : List[Any] = OrderedDict( [ ('input_features', {0: 'batch', 1: 'feature_size', 2: 'encoder_sequence'}), ] ) if self.use_past: snake_case__ : Dict = {0: 'batch'} else: snake_case__ : Tuple = {0: 'batch', 1: 'decoder_sequence'} if self.use_past: self.fill_with_past_key_values_(__UpperCamelCase , direction='inputs' ) return common_inputs def __a ( self , __UpperCamelCase , __UpperCamelCase = -1 , __UpperCamelCase = -1 , __UpperCamelCase = False , __UpperCamelCase = None , __UpperCamelCase = 22050 , __UpperCamelCase = 5.0 , __UpperCamelCase = 220 , ) -> Mapping[str, Any]: '''simple docstring''' snake_case__ : Optional[Any] = OrderedDict() snake_case__ : Optional[Any] = OnnxConfig.generate_dummy_inputs( self , preprocessor=preprocessor.feature_extractor , batch_size=__UpperCamelCase , framework=__UpperCamelCase , sampling_rate=__UpperCamelCase , time_duration=__UpperCamelCase , frequency=__UpperCamelCase , ) snake_case__ : List[str] = encoder_inputs['input_features'].shape[2] snake_case__ : Optional[int] = encoder_sequence_length // 2 if self.use_past else seq_length snake_case__ : Tuple = super().generate_dummy_inputs( preprocessor.tokenizer , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) snake_case__ : int = encoder_inputs.pop('input_features' ) snake_case__ : Optional[int] = decoder_inputs.pop('decoder_input_ids' ) if "past_key_values" in decoder_inputs: snake_case__ : int = decoder_inputs.pop('past_key_values' ) return dummy_inputs @property def __a ( self ) -> float: '''simple docstring''' return 1E-3
699
from .data_collator import ( DataCollatorForLanguageModeling, DataCollatorForPermutationLanguageModeling, DataCollatorForSeqaSeq, DataCollatorForSOP, DataCollatorForTokenClassification, DataCollatorForWholeWordMask, DataCollatorWithPadding, DefaultDataCollator, default_data_collator, ) from .metrics import glue_compute_metrics, xnli_compute_metrics from .processors import ( DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor, SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels, squad_convert_examples_to_features, xnli_output_modes, xnli_processors, xnli_tasks_num_labels, )
699
1
from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_torch_available from ...utils import OptionalDependencyNotAvailable lowerCAmelCase__ : Tuple = { '''configuration_gpt_neox_japanese''': ['''GPT_NEOX_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''GPTNeoXJapaneseConfig'''], '''tokenization_gpt_neox_japanese''': ['''GPTNeoXJapaneseTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Any = [ '''GPT_NEOX_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST''', '''GPTNeoXJapaneseForCausalLM''', '''GPTNeoXJapaneseLayer''', '''GPTNeoXJapaneseModel''', '''GPTNeoXJapanesePreTrainedModel''', ] if TYPE_CHECKING: from .configuration_gpt_neox_japanese import GPT_NEOX_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoXJapaneseConfig from .tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_neox_japanese import ( GPT_NEOX_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST, GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseLayer, GPTNeoXJapaneseModel, GPTNeoXJapanesePreTrainedModel, ) else: import sys lowerCAmelCase__ : Optional[int] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
699
from dataclasses import dataclass, field from typing import Optional from transformers import AutoConfig, AutoImageProcessor, AutoTokenizer, FlaxVisionEncoderDecoderModel, HfArgumentParser @dataclass class __snake_case : __lowerCamelCase = field( metadata={"""help""": """The output directory where the model will be written."""} ,) __lowerCamelCase = field( metadata={ """help""": ( """The encoder model checkpoint for weights initialization.""" """Don't set if you want to train an encoder model from scratch.""" ) } ,) __lowerCamelCase = field( metadata={ """help""": ( """The decoder model checkpoint for weights initialization.""" """Don't set if you want to train a decoder model from scratch.""" ) } ,) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained encoder config name or path if not the same as encoder_model_name"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained decoder config name or path if not the same as decoder_model_name"""} ) def UpperCamelCase__ ( ) -> Union[str, Any]: snake_case__ : str = HfArgumentParser((ModelArguments,) ) ((snake_case__) , ) : Dict = parser.parse_args_into_dataclasses() # Load pretrained model and tokenizer # Use explicit specified encoder config if model_args.encoder_config_name: snake_case__ : List[str] = AutoConfig.from_pretrained(model_args.encoder_config_name ) # Use pretrained encoder model's config else: snake_case__ : Optional[int] = AutoConfig.from_pretrained(model_args.encoder_model_name_or_path ) # Use explicit specified decoder config if model_args.decoder_config_name: snake_case__ : Optional[Any] = AutoConfig.from_pretrained(model_args.decoder_config_name ) # Use pretrained decoder model's config else: snake_case__ : List[str] = AutoConfig.from_pretrained(model_args.decoder_model_name_or_path ) # necessary for `from_encoder_decoder_pretrained` when `decoder_config` is passed snake_case__ : Any = True snake_case__ : Dict = True snake_case__ : Tuple = FlaxVisionEncoderDecoderModel.from_encoder_decoder_pretrained( encoder_pretrained_model_name_or_path=model_args.encoder_model_name_or_path , decoder_pretrained_model_name_or_path=model_args.decoder_model_name_or_path , encoder_config=A__ , decoder_config=A__ , ) # GPT2 only has bos/eos tokens but not decoder_start/pad tokens snake_case__ : Optional[Any] = decoder_config.decoder_start_token_id snake_case__ : Tuple = decoder_config.pad_token_id if decoder_start_token_id is None: snake_case__ : Optional[Any] = decoder_config.bos_token_id if pad_token_id is None: snake_case__ : int = decoder_config.eos_token_id # This is necessary to make Flax's generate() work snake_case__ : Union[str, Any] = decoder_config.eos_token_id snake_case__ : Optional[int] = decoder_start_token_id snake_case__ : int = pad_token_id snake_case__ : Tuple = AutoImageProcessor.from_pretrained(model_args.encoder_model_name_or_path ) snake_case__ : int = AutoTokenizer.from_pretrained(model_args.decoder_model_name_or_path ) snake_case__ : Tuple = tokenizer.convert_ids_to_tokens(model.config.pad_token_id ) model.save_pretrained(model_args.output_dir ) image_processor.save_pretrained(model_args.output_dir ) tokenizer.save_pretrained(model_args.output_dir ) if __name__ == "__main__": main()
699
1
import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class __snake_case ( _lowerCamelCase ): __lowerCamelCase = ["""image_processor""", """tokenizer"""] __lowerCamelCase = """CLIPImageProcessor""" __lowerCamelCase = ("""XLMRobertaTokenizer""", """XLMRobertaTokenizerFast""") def __init__( self , __UpperCamelCase=None , __UpperCamelCase=None , **__UpperCamelCase ) -> List[str]: '''simple docstring''' snake_case__ : int = None if "feature_extractor" in kwargs: warnings.warn( 'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`' ' instead.' , __UpperCamelCase , ) snake_case__ : List[str] = kwargs.pop('feature_extractor' ) snake_case__ : Optional[Any] = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('You need to specify an `image_processor`.' ) if tokenizer is None: raise ValueError('You need to specify a `tokenizer`.' ) super().__init__(__UpperCamelCase , __UpperCamelCase ) def __call__( self , __UpperCamelCase=None , __UpperCamelCase=None , __UpperCamelCase=None , **__UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' if text is None and images is None: raise ValueError('You have to specify either text or images. Both cannot be none.' ) if text is not None: snake_case__ : str = self.tokenizer(__UpperCamelCase , return_tensors=__UpperCamelCase , **__UpperCamelCase ) if images is not None: snake_case__ : Tuple = self.image_processor(__UpperCamelCase , return_tensors=__UpperCamelCase , **__UpperCamelCase ) if text is not None and images is not None: snake_case__ : int = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**__UpperCamelCase ) , tensor_type=__UpperCamelCase ) def __a ( self , *__UpperCamelCase , **__UpperCamelCase ) -> str: '''simple docstring''' return self.tokenizer.batch_decode(*__UpperCamelCase , **__UpperCamelCase ) def __a ( self , *__UpperCamelCase , **__UpperCamelCase ) -> List[str]: '''simple docstring''' return self.tokenizer.decode(*__UpperCamelCase , **__UpperCamelCase ) @property def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Union[str, Any] = self.tokenizer.model_input_names snake_case__ : Union[str, Any] = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
699
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import datasets import numpy as np import tensorflow as tf from transformers import ( AutoConfig, AutoTokenizer, EvalPrediction, HfArgumentParser, PreTrainedTokenizer, TFAutoModelForSequenceClassification, TFTrainer, TFTrainingArguments, ) from transformers.utils import logging as hf_logging hf_logging.set_verbosity_info() hf_logging.enable_default_handler() hf_logging.enable_explicit_format() def UpperCamelCase__ ( A__ , A__ , A__ , A__ , A__ , A__ = None , ) -> Optional[int]: snake_case__ : List[str] = {} if train_file is not None: snake_case__ : Tuple = [train_file] if eval_file is not None: snake_case__ : Dict = [eval_file] if test_file is not None: snake_case__ : str = [test_file] snake_case__ : Optional[Any] = datasets.load_dataset('csv' , data_files=A__ ) snake_case__ : Any = list(ds[list(files.keys() )[0]].features.keys() ) snake_case__ : Optional[Any] = features_name.pop(A__ ) snake_case__ : Optional[Any] = list(set(ds[list(files.keys() )[0]][label_name] ) ) snake_case__ : str = {label: i for i, label in enumerate(A__ )} snake_case__ : int = tokenizer.model_input_names snake_case__ : int = {} if len(A__ ) == 1: for k in files.keys(): snake_case__ : str = ds[k].map( lambda A__ : tokenizer.batch_encode_plus( example[features_name[0]] , truncation=A__ , max_length=A__ , padding='max_length' ) , batched=A__ , ) elif len(A__ ) == 2: for k in files.keys(): snake_case__ : Optional[int] = ds[k].map( lambda A__ : tokenizer.batch_encode_plus( (example[features_name[0]], example[features_name[1]]) , truncation=A__ , max_length=A__ , padding='max_length' , ) , batched=A__ , ) def gen_train(): for ex in transformed_ds[datasets.Split.TRAIN]: snake_case__ : int = {k: v for k, v in ex.items() if k in input_names} snake_case__ : Any = labelaid[ex[label_name]] yield (d, label) def gen_val(): for ex in transformed_ds[datasets.Split.VALIDATION]: snake_case__ : int = {k: v for k, v in ex.items() if k in input_names} snake_case__ : Union[str, Any] = labelaid[ex[label_name]] yield (d, label) def gen_test(): for ex in transformed_ds[datasets.Split.TEST]: snake_case__ : Dict = {k: v for k, v in ex.items() if k in input_names} snake_case__ : List[str] = labelaid[ex[label_name]] yield (d, label) snake_case__ : Any = ( tf.data.Dataset.from_generator( A__ , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.TRAIN in transformed_ds else None ) if train_ds is not None: snake_case__ : str = train_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TRAIN] ) ) ) snake_case__ : Optional[int] = ( tf.data.Dataset.from_generator( A__ , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.VALIDATION in transformed_ds else None ) if val_ds is not None: snake_case__ : Optional[int] = val_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.VALIDATION] ) ) ) snake_case__ : List[str] = ( tf.data.Dataset.from_generator( A__ , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.TEST in transformed_ds else None ) if test_ds is not None: snake_case__ : str = test_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TEST] ) ) ) return train_ds, val_ds, test_ds, labelaid lowerCAmelCase__ : List[str] = logging.getLogger(__name__) @dataclass class __snake_case : __lowerCamelCase = field(metadata={"""help""": """Which column contains the label"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The path of the training file"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The path of the development file"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The path of the test file"""} ) __lowerCamelCase = field( default=128 ,metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } ,) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Overwrite the cached training and evaluation sets"""} ) @dataclass class __snake_case : __lowerCamelCase = field( metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """Set this flag to use fast tokenization."""} ) # If you want to tweak more attributes on your tokenizer, you should do it in a distinct script, # or just modify its tokenizer_config.json. __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} ,) def UpperCamelCase__ ( ) -> Union[str, 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. snake_case__ : int = HfArgumentParser((ModelArguments, DataTrainingArguments, TFTrainingArguments) ) snake_case__ , snake_case__ , snake_case__ : Dict = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( F"""Output directory ({training_args.output_dir}) already exists and is not empty. Use""" ' --overwrite_output_dir to overcome.' ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , level=logging.INFO , ) logger.info( F"""n_replicas: {training_args.n_replicas}, distributed training: {bool(training_args.n_replicas > 1 )}, """ F"""16-bits training: {training_args.fpaa}""" ) logger.info(F"""Training/evaluation parameters {training_args}""" ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. snake_case__ : Dict = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) snake_case__ , snake_case__ , snake_case__ , snake_case__ : Dict = get_tfds( train_file=data_args.train_file , eval_file=data_args.dev_file , test_file=data_args.test_file , tokenizer=A__ , label_column_id=data_args.label_column_id , max_seq_length=data_args.max_seq_length , ) snake_case__ : Dict = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=len(A__ ) , labelaid=A__ , idalabel={id: label for label, id in labelaid.items()} , finetuning_task='text-classification' , cache_dir=model_args.cache_dir , ) with training_args.strategy.scope(): snake_case__ : Optional[Any] = TFAutoModelForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_pt=bool('.bin' in model_args.model_name_or_path ) , config=A__ , cache_dir=model_args.cache_dir , ) def compute_metrics(A__ ) -> Dict: snake_case__ : Optional[Any] = np.argmax(p.predictions , axis=1 ) return {"acc": (preds == p.label_ids).mean()} # Initialize our Trainer snake_case__ : Any = TFTrainer( model=A__ , args=A__ , train_dataset=A__ , eval_dataset=A__ , compute_metrics=A__ , ) # Training if training_args.do_train: trainer.train() trainer.save_model() tokenizer.save_pretrained(training_args.output_dir ) # Evaluation snake_case__ : Dict = {} if training_args.do_eval: logger.info('*** Evaluate ***' ) snake_case__ : Tuple = trainer.evaluate() snake_case__ : Any = os.path.join(training_args.output_dir , 'eval_results.txt' ) with open(A__ , 'w' ) as writer: logger.info('***** Eval results *****' ) for key, value in result.items(): logger.info(F""" {key} = {value}""" ) writer.write(F"""{key} = {value}\n""" ) results.update(A__ ) return results if __name__ == "__main__": main()
699
1
from ..utils import DummyObject, requires_backends class __snake_case ( metaclass=_lowerCamelCase ): __lowerCamelCase = ["""flax""", """transformers"""] def __init__( self , *__UpperCamelCase , **__UpperCamelCase ) -> List[str]: '''simple docstring''' requires_backends(self , ['flax', 'transformers'] ) @classmethod def __a ( cls , *__UpperCamelCase , **__UpperCamelCase ) -> Any: '''simple docstring''' requires_backends(cls , ['flax', 'transformers'] ) @classmethod def __a ( cls , *__UpperCamelCase , **__UpperCamelCase ) -> int: '''simple docstring''' requires_backends(cls , ['flax', 'transformers'] ) class __snake_case ( metaclass=_lowerCamelCase ): __lowerCamelCase = ["""flax""", """transformers"""] def __init__( self , *__UpperCamelCase , **__UpperCamelCase ) -> int: '''simple docstring''' requires_backends(self , ['flax', 'transformers'] ) @classmethod def __a ( cls , *__UpperCamelCase , **__UpperCamelCase ) -> int: '''simple docstring''' requires_backends(cls , ['flax', 'transformers'] ) @classmethod def __a ( cls , *__UpperCamelCase , **__UpperCamelCase ) -> Dict: '''simple docstring''' requires_backends(cls , ['flax', 'transformers'] ) class __snake_case ( metaclass=_lowerCamelCase ): __lowerCamelCase = ["""flax""", """transformers"""] def __init__( self , *__UpperCamelCase , **__UpperCamelCase ) -> List[str]: '''simple docstring''' requires_backends(self , ['flax', 'transformers'] ) @classmethod def __a ( cls , *__UpperCamelCase , **__UpperCamelCase ) -> List[Any]: '''simple docstring''' requires_backends(cls , ['flax', 'transformers'] ) @classmethod def __a ( cls , *__UpperCamelCase , **__UpperCamelCase ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ['flax', 'transformers'] ) class __snake_case ( metaclass=_lowerCamelCase ): __lowerCamelCase = ["""flax""", """transformers"""] def __init__( self , *__UpperCamelCase , **__UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' requires_backends(self , ['flax', 'transformers'] ) @classmethod def __a ( cls , *__UpperCamelCase , **__UpperCamelCase ) -> Dict: '''simple docstring''' requires_backends(cls , ['flax', 'transformers'] ) @classmethod def __a ( cls , *__UpperCamelCase , **__UpperCamelCase ) -> str: '''simple docstring''' requires_backends(cls , ['flax', 'transformers'] )
699
from typing import List import datasets from datasets.tasks import AudioClassification from ..folder_based_builder import folder_based_builder lowerCAmelCase__ : List[Any] = datasets.utils.logging.get_logger(__name__) class __snake_case ( folder_based_builder.FolderBasedBuilderConfig ): __lowerCamelCase = None __lowerCamelCase = None class __snake_case ( folder_based_builder.FolderBasedBuilder ): __lowerCamelCase = datasets.Audio() __lowerCamelCase = """audio""" __lowerCamelCase = AudioFolderConfig __lowerCamelCase = 42 # definition at the bottom of the script __lowerCamelCase = AudioClassification(audio_column="""audio""" ,label_column="""label""" ) lowerCAmelCase__ : Tuple = [ '''.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''', ] lowerCAmelCase__ : List[Any] = AUDIO_EXTENSIONS
699
1
import pytest import requests from datasets.utils.file_utils import http_head from .utils import OfflineSimulationMode, RequestWouldHangIndefinitelyError, offline @pytest.mark.integration def UpperCamelCase__ ( ) -> Optional[int]: with offline(OfflineSimulationMode.CONNECTION_TIMES_OUT ): with pytest.raises(A__ ): requests.request('GET' , 'https://huggingface.co' ) with pytest.raises(requests.exceptions.ConnectTimeout ): requests.request('GET' , 'https://huggingface.co' , timeout=1.0 ) @pytest.mark.integration def UpperCamelCase__ ( ) -> Tuple: with offline(OfflineSimulationMode.CONNECTION_FAILS ): with pytest.raises(requests.exceptions.ConnectionError ): requests.request('GET' , 'https://huggingface.co' ) def UpperCamelCase__ ( ) -> Tuple: with offline(OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1 ): with pytest.raises(A__ ): http_head('https://huggingface.co' )
699
import random import unittest import torch from diffusers import IFInpaintingPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class __snake_case ( _lowerCamelCase ,_lowerCamelCase ,unittest.TestCase ): __lowerCamelCase = IFInpaintingPipeline __lowerCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"""width""", """height"""} __lowerCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __lowerCamelCase = PipelineTesterMixin.required_optional_params - {"""latents"""} def __a ( self ) -> Optional[Any]: '''simple docstring''' return self._get_dummy_components() def __a ( self , __UpperCamelCase , __UpperCamelCase=0 ) -> str: '''simple docstring''' if str(__UpperCamelCase ).startswith('mps' ): snake_case__ : int = torch.manual_seed(__UpperCamelCase ) else: snake_case__ : Union[str, Any] = torch.Generator(device=__UpperCamelCase ).manual_seed(__UpperCamelCase ) snake_case__ : Optional[int] = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) snake_case__ : Dict = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) snake_case__ : Optional[Any] = { 'prompt': 'A painting of a squirrel eating a burger', 'image': image, 'mask_image': mask_image, 'generator': generator, 'num_inference_steps': 2, 'output_type': 'numpy', } return inputs @unittest.skipIf( torch_device != 'cuda' or not is_xformers_available() , reason='XFormers attention is only available with CUDA and `xformers` installed' , ) def __a ( self ) -> List[Any]: '''simple docstring''' self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) def __a ( self ) -> Optional[int]: '''simple docstring''' self._test_save_load_optional_components() @unittest.skipIf(torch_device != 'cuda' , reason='float16 requires CUDA' ) def __a ( self ) -> List[str]: '''simple docstring''' super().test_save_load_floataa(expected_max_diff=1E-1 ) def __a ( self ) -> List[str]: '''simple docstring''' self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def __a ( self ) -> int: '''simple docstring''' self._test_save_load_local() def __a ( self ) -> List[str]: '''simple docstring''' self._test_inference_batch_single_identical( expected_max_diff=1E-2 , )
699
1
import math import os import sys def UpperCamelCase__ ( A__ ) -> str: snake_case__ : str = '' try: with open(A__ , 'rb' ) as binary_file: snake_case__ : Optional[Any] = binary_file.read() for dat in data: snake_case__ : Tuple = F"""{dat:08b}""" result += curr_byte return result except OSError: print('File not accessible' ) sys.exit() def UpperCamelCase__ ( A__ , A__ , A__ , A__ ) -> None: lexicon.pop(A__ ) snake_case__ : Any = last_match_id if math.loga(A__ ).is_integer(): for curr_key in lexicon: snake_case__ : int = '0' + lexicon[curr_key] snake_case__ : Optional[int] = bin(A__ )[2:] def UpperCamelCase__ ( A__ ) -> str: snake_case__ : str = {'0': '0', '1': '1'} snake_case__ , snake_case__ : Dict = '', '' snake_case__ : str = len(A__ ) for i in range(len(A__ ) ): curr_string += data_bits[i] if curr_string not in lexicon: continue snake_case__ : List[str] = lexicon[curr_string] result += last_match_id add_key_to_lexicon(A__ , A__ , A__ , A__ ) index += 1 snake_case__ : List[Any] = '' while curr_string != "" and curr_string not in lexicon: curr_string += "0" if curr_string != "": snake_case__ : str = lexicon[curr_string] result += last_match_id return result def UpperCamelCase__ ( A__ , A__ ) -> str: snake_case__ : Tuple = os.path.getsize(A__ ) snake_case__ : Optional[int] = bin(A__ )[2:] snake_case__ : str = len(A__ ) return "0" * (length_length - 1) + file_length_binary + compressed def UpperCamelCase__ ( A__ , A__ ) -> None: snake_case__ : Union[str, Any] = 8 try: with open(A__ , 'wb' ) as opened_file: snake_case__ : Tuple = [ to_write[i : i + byte_length] for i in range(0 , len(A__ ) , A__ ) ] if len(result_byte_array[-1] ) % byte_length == 0: result_byte_array.append('10000000' ) else: result_byte_array[-1] += "1" + "0" * ( byte_length - len(result_byte_array[-1] ) - 1 ) for elem in result_byte_array: opened_file.write(int(A__ , 2 ).to_bytes(1 , byteorder='big' ) ) except OSError: print('File not accessible' ) sys.exit() def UpperCamelCase__ ( A__ , A__ ) -> None: snake_case__ : List[Any] = read_file_binary(A__ ) snake_case__ : Optional[Any] = compress_data(A__ ) snake_case__ : Optional[int] = add_file_length(A__ , A__ ) write_file_binary(A__ , A__ ) if __name__ == "__main__": compress(sys.argv[1], sys.argv[2])
699
import unittest from transformers import BertGenerationTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin lowerCAmelCase__ : List[Any] = '''▁''' lowerCAmelCase__ : int = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece class __snake_case ( _lowerCamelCase ,unittest.TestCase ): __lowerCamelCase = BertGenerationTokenizer __lowerCamelCase = False __lowerCamelCase = True def __a ( self ) -> Optional[int]: '''simple docstring''' super().setUp() snake_case__ : str = BertGenerationTokenizer(__UpperCamelCase , keep_accents=__UpperCamelCase ) tokenizer.save_pretrained(self.tmpdirname ) def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : List[str] = '<s>' snake_case__ : Dict = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__UpperCamelCase ) , __UpperCamelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__UpperCamelCase ) , __UpperCamelCase ) def __a ( self ) -> List[str]: '''simple docstring''' snake_case__ : Tuple = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '<unk>' ) self.assertEqual(vocab_keys[1] , '<s>' ) self.assertEqual(vocab_keys[-1] , '<pad>' ) self.assertEqual(len(__UpperCamelCase ) , 1002 ) def __a ( self ) -> int: '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 1000 ) def __a ( self ) -> Tuple: '''simple docstring''' snake_case__ : Optional[Any] = BertGenerationTokenizer(__UpperCamelCase , keep_accents=__UpperCamelCase ) snake_case__ : int = tokenizer.tokenize('This is a test' ) self.assertListEqual(__UpperCamelCase , ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__UpperCamelCase ) , [285, 46, 10, 170, 382] , ) snake_case__ : Any = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( __UpperCamelCase , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '9', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', 'é', '.', ] , ) snake_case__ : Optional[Any] = tokenizer.convert_tokens_to_ids(__UpperCamelCase ) self.assertListEqual( __UpperCamelCase , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) snake_case__ : int = tokenizer.convert_ids_to_tokens(__UpperCamelCase ) self.assertListEqual( __UpperCamelCase , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '<unk>', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', '<unk>', '.', ] , ) @cached_property def __a ( self ) -> Dict: '''simple docstring''' return BertGenerationTokenizer.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder' ) @slow def __a ( self ) -> Any: '''simple docstring''' snake_case__ : int = 'Hello World!' snake_case__ : Union[str, Any] = [18536, 2260, 101] self.assertListEqual(__UpperCamelCase , self.big_tokenizer.encode(__UpperCamelCase ) ) @slow def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : str = ( 'This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will' ' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth' ) snake_case__ : List[Any] = [ 871, 419, 358, 946, 991, 2521, 452, 358, 1357, 387, 7751, 3536, 112, 985, 456, 126, 865, 938, 5400, 5734, 458, 1368, 467, 786, 2462, 5246, 1159, 633, 865, 4519, 457, 582, 852, 2557, 427, 916, 508, 405, 34324, 497, 391, 408, 11342, 1244, 385, 100, 938, 985, 456, 574, 362, 12597, 3200, 3129, 1172, ] self.assertListEqual(__UpperCamelCase , self.big_tokenizer.encode(__UpperCamelCase ) ) @require_torch @slow def __a ( self ) -> List[str]: '''simple docstring''' import torch from transformers import BertGenerationConfig, BertGenerationEncoder # Build sequence snake_case__ : Optional[int] = list(self.big_tokenizer.get_vocab().keys() )[:10] snake_case__ : Optional[int] = ' '.join(__UpperCamelCase ) snake_case__ : int = self.big_tokenizer.encode_plus(__UpperCamelCase , return_tensors='pt' , return_token_type_ids=__UpperCamelCase ) snake_case__ : Tuple = self.big_tokenizer.batch_encode_plus( [sequence + ' ' + sequence] , return_tensors='pt' , return_token_type_ids=__UpperCamelCase ) snake_case__ : Dict = BertGenerationConfig() snake_case__ : List[str] = BertGenerationEncoder(__UpperCamelCase ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**__UpperCamelCase ) model(**__UpperCamelCase ) @slow def __a ( self ) -> Dict: '''simple docstring''' snake_case__ : Optional[int] = {'input_ids': [[39286, 458, 36335, 2001, 456, 13073, 13266, 455, 113, 7746, 1741, 11157, 391, 13073, 13266, 455, 113, 3967, 35412, 113, 4936, 109, 3870, 2377, 113, 30084, 45720, 458, 134, 17496, 112, 503, 11672, 113, 118, 112, 5665, 13347, 38687, 112, 1496, 31389, 112, 3268, 47264, 134, 962, 112, 16377, 8035, 23130, 430, 12169, 15518, 28592, 458, 146, 41697, 109, 391, 12169, 15518, 16689, 458, 146, 41358, 109, 452, 726, 4034, 111, 763, 35412, 5082, 388, 1903, 111, 9051, 391, 2870, 48918, 1900, 1123, 550, 998, 112, 9586, 15985, 455, 391, 410, 22955, 37636, 114], [448, 17496, 419, 3663, 385, 763, 113, 27533, 2870, 3283, 13043, 1639, 24713, 523, 656, 24013, 18550, 2521, 517, 27014, 21244, 420, 1212, 1465, 391, 927, 4833, 388, 578, 11786, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [484, 2169, 7687, 21932, 18146, 726, 363, 17032, 3391, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=__UpperCamelCase , model_name='google/bert_for_seq_generation_L-24_bbc_encoder' , revision='c817d1fd1be2ffa69431227a1fe320544943d4db' , )
699
1
# Copyright 2022 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os import 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__ ( A__=None ) -> int: if subparsers is not None: snake_case__ : int = subparsers.add_parser('env' ) else: snake_case__ : Optional[int] = argparse.ArgumentParser('Accelerate env command' ) parser.add_argument( '--config_file' , default=A__ , help='The config file to use for the default values in the launching script.' ) if subparsers is not None: parser.set_defaults(func=A__ ) return parser def UpperCamelCase__ ( A__ ) -> Union[str, Any]: snake_case__ : int = torch.__version__ snake_case__ : Dict = torch.cuda.is_available() snake_case__ : Tuple = is_xpu_available() snake_case__ : Optional[Any] = is_npu_available() snake_case__ : Union[str, Any] = 'Not found' # Get the default from the config file. if args.config_file is not None or os.path.isfile(A__ ): snake_case__ : Tuple = load_config_from_file(args.config_file ).to_dict() snake_case__ : Union[str, Any] = { '`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(A__ ), 'PyTorch NPU available': str(A__ ), 'System RAM': F"""{psutil.virtual_memory().total / 1024 ** 3:.2f} GB""", } if pt_cuda_available: snake_case__ : int = 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:' ) snake_case__ : Dict = ( '\n'.join([F"""\t- {prop}: {val}""" for prop, val in accelerate_config.items()] ) if isinstance(A__ , A__ ) else F"""\t{accelerate_config}""" ) print(A__ ) snake_case__ : List[str] = accelerate_config return info def UpperCamelCase__ ( ) -> int: snake_case__ : List[Any] = env_command_parser() snake_case__ : List[Any] = parser.parse_args() env_command(A__ ) return 0 if __name__ == "__main__": raise SystemExit(main())
699
import random import torch from huggingface_hub import HfApi from diffusers import UNetaDModel lowerCAmelCase__ : List[str] = HfApi() lowerCAmelCase__ : str = {} # fmt: off lowerCAmelCase__ : int = torch.tensor([ -0.75_15, -1.68_83, 0.24_20, 0.03_00, 0.63_47, 1.34_33, -1.17_43, -3.74_67, 1.23_42, -2.24_85, 0.46_36, 0.80_76, -0.79_91, 0.39_69, 0.84_98, 0.91_89, -1.88_87, -3.35_22, 0.76_39, 0.20_40, 0.62_71, -2.71_48, -1.63_16, 3.08_39, 0.31_86, 0.27_21, -0.97_59, -1.24_61, 2.62_57, 1.35_57 ]) lowerCAmelCase__ : Dict = torch.tensor([ -2.36_39, -2.53_44, 0.00_54, -0.66_74, 1.59_90, 1.01_58, 0.31_24, -2.14_36, 1.87_95, -2.54_29, -0.15_66, -0.39_73, 1.24_90, 2.64_47, 1.22_83, -0.52_08, -2.81_54, -3.51_19, 2.38_38, 1.20_33, 1.72_01, -2.12_56, -1.45_76, 2.79_48, 2.42_04, -0.97_52, -1.25_46, 0.80_27, 3.27_58, 3.13_65 ]) lowerCAmelCase__ : Dict = torch.tensor([ -0.65_31, -0.68_91, -0.31_72, -0.53_75, -0.91_40, -0.53_67, -0.11_75, -0.78_69, -0.38_08, -0.45_13, -0.20_98, -0.00_83, 0.31_83, 0.51_40, 0.22_47, -0.13_04, -0.13_02, -0.28_02, -0.20_84, -0.20_25, -0.49_67, -0.48_73, -0.08_61, 0.69_25, 0.02_50, 0.12_90, -0.15_43, 0.63_16, 1.04_60, 1.49_43 ]) lowerCAmelCase__ : List[str] = torch.tensor([ 0.09_11, 0.11_07, 0.01_82, 0.04_35, -0.08_05, -0.06_08, 0.03_81, 0.21_72, -0.02_80, 0.13_27, -0.02_99, -0.02_55, -0.00_50, -0.11_70, -0.10_46, 0.03_09, 0.13_67, 0.17_28, -0.05_33, -0.07_48, -0.05_34, 0.16_24, 0.03_84, -0.18_05, -0.07_07, 0.06_42, 0.02_20, -0.01_34, -0.13_33, -0.15_05 ]) lowerCAmelCase__ : Union[str, Any] = torch.tensor([ 0.13_21, 0.13_37, 0.04_40, 0.06_22, -0.05_91, -0.03_70, 0.05_03, 0.21_33, -0.01_77, 0.14_15, -0.01_16, -0.01_12, 0.00_44, -0.09_80, -0.07_89, 0.03_95, 0.15_02, 0.17_85, -0.04_88, -0.05_14, -0.04_04, 0.15_39, 0.04_54, -0.15_59, -0.06_65, 0.06_59, 0.03_83, -0.00_05, -0.12_66, -0.13_86 ]) lowerCAmelCase__ : List[Any] = torch.tensor([ 0.11_54, 0.12_18, 0.03_07, 0.05_26, -0.07_11, -0.05_41, 0.03_66, 0.20_78, -0.02_67, 0.13_17, -0.02_26, -0.01_93, -0.00_14, -0.10_55, -0.09_02, 0.03_30, 0.13_91, 0.17_09, -0.05_62, -0.06_93, -0.05_60, 0.14_82, 0.03_81, -0.16_83, -0.06_81, 0.06_61, 0.03_31, -0.00_46, -0.12_68, -0.14_31 ]) lowerCAmelCase__ : Optional[Any] = torch.tensor([ 0.11_92, 0.12_40, 0.04_14, 0.06_06, -0.05_57, -0.04_12, 0.04_30, 0.20_42, -0.02_00, 0.13_85, -0.01_15, -0.01_32, 0.00_17, -0.09_65, -0.08_02, 0.03_98, 0.14_33, 0.17_47, -0.04_58, -0.05_33, -0.04_07, 0.15_45, 0.04_19, -0.15_74, -0.06_45, 0.06_26, 0.03_41, -0.00_10, -0.11_99, -0.13_90 ]) lowerCAmelCase__ : List[str] = torch.tensor([ 0.10_75, 0.10_74, 0.02_05, 0.04_31, -0.07_74, -0.06_07, 0.02_98, 0.20_42, -0.03_20, 0.12_67, -0.02_81, -0.02_50, -0.00_64, -0.10_91, -0.09_46, 0.02_90, 0.13_28, 0.16_50, -0.05_80, -0.07_38, -0.05_86, 0.14_40, 0.03_37, -0.17_46, -0.07_12, 0.06_05, 0.02_50, -0.00_99, -0.13_16, -0.14_73 ]) lowerCAmelCase__ : List[str] = torch.tensor([ -1.45_72, -2.04_81, -0.04_14, -0.60_05, 1.41_36, 0.58_48, 0.40_28, -2.73_30, 1.22_12, -2.12_28, 0.21_55, 0.40_39, 0.76_62, 2.05_35, 0.74_77, -0.32_43, -2.17_58, -2.76_48, 1.69_47, 0.70_26, 1.23_38, -1.60_78, -0.86_82, 2.28_10, 1.85_74, -0.57_18, -0.55_86, -0.01_86, 2.34_15, 2.12_51]) lowerCAmelCase__ : List[Any] = torch.tensor([ -1.36_90, -1.97_20, -0.40_90, -0.69_66, 1.46_60, 0.99_38, -0.13_85, -2.73_24, 0.77_36, -1.89_17, 0.29_23, 0.42_93, 0.16_93, 1.41_12, 1.18_87, -0.31_81, -2.21_60, -2.63_81, 1.31_70, 0.81_63, 0.92_40, -1.65_44, -0.60_99, 2.52_59, 1.64_30, -0.90_90, -0.93_92, -0.01_26, 2.42_68, 2.32_66 ]) lowerCAmelCase__ : Tuple = torch.tensor([ -1.35_25, -1.96_28, -0.39_56, -0.68_60, 1.46_64, 1.00_14, -0.12_59, -2.72_12, 0.77_72, -1.88_11, 0.29_96, 0.43_88, 0.17_04, 1.40_29, 1.17_01, -0.30_27, -2.20_53, -2.62_87, 1.33_50, 0.81_31, 0.92_74, -1.62_92, -0.60_98, 2.51_31, 1.65_05, -0.89_58, -0.92_98, -0.01_51, 2.42_57, 2.33_55 ]) lowerCAmelCase__ : List[str] = torch.tensor([ -2.05_85, -2.78_97, -0.28_50, -0.89_40, 1.90_52, 0.57_02, 0.63_45, -3.89_59, 1.59_32, -3.23_19, 0.19_74, 0.02_87, 1.75_66, 2.65_43, 0.83_87, -0.53_51, -3.27_36, -4.33_75, 2.90_29, 1.63_90, 1.46_40, -2.17_01, -1.90_13, 2.93_41, 3.49_81, -0.62_55, -1.16_44, -0.15_91, 3.70_97, 3.20_66 ]) lowerCAmelCase__ : Dict = torch.tensor([ -2.31_39, -2.55_94, -0.01_97, -0.67_85, 1.70_01, 1.16_06, 0.30_75, -2.17_40, 1.80_71, -2.56_30, -0.09_26, -0.38_11, 1.21_16, 2.62_46, 1.27_31, -0.53_98, -2.81_53, -3.61_40, 2.38_93, 1.32_62, 1.62_58, -2.18_56, -1.32_67, 2.83_95, 2.37_79, -1.06_23, -1.24_68, 0.89_59, 3.33_67, 3.22_43 ]) lowerCAmelCase__ : Dict = torch.tensor([ -2.06_28, -2.76_67, -0.20_89, -0.82_63, 2.05_39, 0.59_92, 0.64_95, -3.83_36, 1.60_25, -3.28_17, 0.17_21, -0.06_33, 1.75_16, 2.70_39, 0.81_00, -0.59_08, -3.21_13, -4.43_43, 2.92_57, 1.36_32, 1.55_62, -2.14_89, -1.98_94, 3.05_60, 3.33_96, -0.73_28, -1.04_17, 0.03_83, 3.70_93, 3.23_43 ]) lowerCAmelCase__ : Any = torch.tensor([ -1.45_74, -2.05_69, -0.04_73, -0.61_17, 1.40_18, 0.57_69, 0.41_29, -2.73_44, 1.22_41, -2.13_97, 0.20_00, 0.39_37, 0.76_16, 2.04_53, 0.73_24, -0.33_91, -2.17_46, -2.77_44, 1.69_63, 0.69_21, 1.21_87, -1.61_72, -0.88_77, 2.24_39, 1.84_71, -0.58_39, -0.56_05, -0.04_64, 2.32_50, 2.12_19 ]) # fmt: on lowerCAmelCase__ : Any = api.list_models(filter='''diffusers''') for mod in models: if "google" in mod.author or mod.modelId == "CompVis/ldm-celebahq-256": lowerCAmelCase__ : List[str] = '''/home/patrick/google_checkpoints/''' + mod.modelId.split('''/''')[-1] print(F'''Started running {mod.modelId}!!!''') if mod.modelId.startswith('''CompVis'''): lowerCAmelCase__ : int = UNetaDModel.from_pretrained(local_checkpoint, subfolder='''unet''') else: lowerCAmelCase__ : str = UNetaDModel.from_pretrained(local_checkpoint) torch.manual_seed(0) random.seed(0) lowerCAmelCase__ : Any = torch.randn(1, model.config.in_channels, model.config.sample_size, model.config.sample_size) lowerCAmelCase__ : List[str] = torch.tensor([10] * noise.shape[0]) with torch.no_grad(): lowerCAmelCase__ : int = model(noise, time_step).sample assert torch.allclose( logits[0, 0, 0, :30], results['''_'''.join('''_'''.join(mod.modelId.split('''/''')).split('''-'''))], atol=1E-3 ) print(F'''{mod.modelId} has passed successfully!!!''')
699
1
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ : Union[str, Any] = logging.get_logger(__name__) lowerCAmelCase__ : str = { '''microsoft/biogpt''': '''https://huggingface.co/microsoft/biogpt/resolve/main/config.json''', # See all BioGPT models at https://huggingface.co/models?filter=biogpt } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = """biogpt""" def __init__( self , __UpperCamelCase=42384 , __UpperCamelCase=1024 , __UpperCamelCase=24 , __UpperCamelCase=16 , __UpperCamelCase=4096 , __UpperCamelCase="gelu" , __UpperCamelCase=0.1 , __UpperCamelCase=0.1 , __UpperCamelCase=1024 , __UpperCamelCase=0.0_2 , __UpperCamelCase=1E-12 , __UpperCamelCase=True , __UpperCamelCase=True , __UpperCamelCase=0.0 , __UpperCamelCase=0.0 , __UpperCamelCase=1 , __UpperCamelCase=0 , __UpperCamelCase=2 , **__UpperCamelCase , ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Optional[int] = vocab_size snake_case__ : Dict = max_position_embeddings snake_case__ : List[Any] = hidden_size snake_case__ : List[Any] = num_hidden_layers snake_case__ : List[str] = num_attention_heads snake_case__ : Union[str, Any] = intermediate_size snake_case__ : int = hidden_act snake_case__ : Tuple = hidden_dropout_prob snake_case__ : int = attention_probs_dropout_prob snake_case__ : Any = initializer_range snake_case__ : Dict = layer_norm_eps snake_case__ : Dict = scale_embedding snake_case__ : Dict = use_cache snake_case__ : str = layerdrop snake_case__ : Dict = activation_dropout super().__init__(pad_token_id=__UpperCamelCase , bos_token_id=__UpperCamelCase , eos_token_id=__UpperCamelCase , **__UpperCamelCase )
699
import warnings from ...utils import logging from .image_processing_perceiver import PerceiverImageProcessor lowerCAmelCase__ : Dict = logging.get_logger(__name__) class __snake_case ( _lowerCamelCase ): def __init__( self , *__UpperCamelCase , **__UpperCamelCase ) -> None: '''simple docstring''' warnings.warn( 'The class PerceiverFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use PerceiverImageProcessor instead.' , __UpperCamelCase , ) super().__init__(*__UpperCamelCase , **__UpperCamelCase )
699
1
import gc import random import tempfile import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel from diffusers.pipelines.stable_diffusion_safe import StableDiffusionPipelineSafe as StableDiffusionPipeline from diffusers.utils import floats_tensor, nightly, torch_device from diffusers.utils.testing_utils import require_torch_gpu class __snake_case ( unittest.TestCase ): def __a ( self ) -> str: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() @property def __a ( self ) -> int: '''simple docstring''' snake_case__ : Optional[Any] = 1 snake_case__ : Tuple = 3 snake_case__ : Dict = (32, 32) snake_case__ : Optional[Any] = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(__UpperCamelCase ) return image @property def __a ( self ) -> Any: '''simple docstring''' torch.manual_seed(0 ) snake_case__ : List[str] = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D') , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D') , cross_attention_dim=32 , ) return model @property def __a ( self ) -> Tuple: '''simple docstring''' torch.manual_seed(0 ) snake_case__ : Tuple = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] , up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] , latent_channels=4 , ) return model @property def __a ( self ) -> str: '''simple docstring''' torch.manual_seed(0 ) snake_case__ : Any = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) return CLIPTextModel(__UpperCamelCase ) @property def __a ( self ) -> List[Any]: '''simple docstring''' def extract(*__UpperCamelCase , **__UpperCamelCase ): class __snake_case : def __init__( self ) -> Dict: '''simple docstring''' snake_case__ : Any = torch.ones([0] ) def __a ( self , __UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' self.pixel_values.to(__UpperCamelCase ) return self return Out() return extract def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : str = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : Union[str, Any] = self.dummy_cond_unet snake_case__ : Optional[Any] = DDIMScheduler( beta_start=0.0_0_0_8_5 , beta_end=0.0_1_2 , beta_schedule='scaled_linear' , clip_sample=__UpperCamelCase , set_alpha_to_one=__UpperCamelCase , ) snake_case__ : List[str] = self.dummy_vae snake_case__ : List[str] = self.dummy_text_encoder snake_case__ : Optional[int] = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) # make sure here that pndm scheduler skips prk snake_case__ : List[Any] = StableDiffusionPipeline( unet=__UpperCamelCase , scheduler=__UpperCamelCase , vae=__UpperCamelCase , text_encoder=__UpperCamelCase , tokenizer=__UpperCamelCase , safety_checker=__UpperCamelCase , feature_extractor=self.dummy_extractor , ) snake_case__ : Tuple = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Optional[int] = 'A painting of a squirrel eating a burger' snake_case__ : int = torch.Generator(device=__UpperCamelCase ).manual_seed(0 ) snake_case__ : Dict = sd_pipe([prompt] , generator=__UpperCamelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type='np' ) snake_case__ : Dict = output.images snake_case__ : Tuple = torch.Generator(device=__UpperCamelCase ).manual_seed(0 ) snake_case__ : Tuple = sd_pipe( [prompt] , generator=__UpperCamelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type='np' , return_dict=__UpperCamelCase , )[0] snake_case__ : str = image[0, -3:, -3:, -1] snake_case__ : Any = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) snake_case__ : Dict = np.array([0.5_7_5_6, 0.6_1_1_8, 0.5_0_0_5, 0.5_0_4_1, 0.5_4_7_1, 0.4_7_2_6, 0.4_9_7_6, 0.4_8_6_5, 0.4_8_6_4] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 def __a ( self ) -> Optional[Any]: '''simple docstring''' snake_case__ : Any = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : Dict = self.dummy_cond_unet snake_case__ : Union[str, Any] = PNDMScheduler(skip_prk_steps=__UpperCamelCase ) snake_case__ : Union[str, Any] = self.dummy_vae snake_case__ : Any = self.dummy_text_encoder snake_case__ : Union[str, Any] = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) # make sure here that pndm scheduler skips prk snake_case__ : str = StableDiffusionPipeline( unet=__UpperCamelCase , scheduler=__UpperCamelCase , vae=__UpperCamelCase , text_encoder=__UpperCamelCase , tokenizer=__UpperCamelCase , safety_checker=__UpperCamelCase , feature_extractor=self.dummy_extractor , ) snake_case__ : Tuple = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Optional[Any] = 'A painting of a squirrel eating a burger' snake_case__ : Optional[Any] = torch.Generator(device=__UpperCamelCase ).manual_seed(0 ) snake_case__ : Union[str, Any] = sd_pipe([prompt] , generator=__UpperCamelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type='np' ) snake_case__ : List[str] = output.images snake_case__ : List[str] = torch.Generator(device=__UpperCamelCase ).manual_seed(0 ) snake_case__ : Union[str, Any] = sd_pipe( [prompt] , generator=__UpperCamelCase , guidance_scale=6.0 , num_inference_steps=2 , output_type='np' , return_dict=__UpperCamelCase , )[0] snake_case__ : Any = image[0, -3:, -3:, -1] snake_case__ : int = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) snake_case__ : int = np.array([0.5_1_2_5, 0.5_7_1_6, 0.4_8_2_8, 0.5_0_6_0, 0.5_6_5_0, 0.4_7_6_8, 0.5_1_8_5, 0.4_8_9_5, 0.4_9_9_3] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 def __a ( self ) -> Tuple: '''simple docstring''' snake_case__ : int = StableDiffusionPipeline.from_pretrained( 'hf-internal-testing/tiny-stable-diffusion-lms-pipe' , safety_checker=__UpperCamelCase ) assert isinstance(__UpperCamelCase , __UpperCamelCase ) assert isinstance(pipe.scheduler , __UpperCamelCase ) assert pipe.safety_checker is None snake_case__ : str = pipe('example prompt' , num_inference_steps=2 ).images[0] assert image is not None # check that there's no error when saving a pipeline with one of the models being None with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(__UpperCamelCase ) snake_case__ : Dict = StableDiffusionPipeline.from_pretrained(__UpperCamelCase ) # sanity check that the pipeline still works assert pipe.safety_checker is None snake_case__ : int = pipe('example prompt' , num_inference_steps=2 ).images[0] assert image is not None @unittest.skipIf(torch_device != 'cuda' , 'This test requires a GPU' ) def __a ( self ) -> Optional[Any]: '''simple docstring''' snake_case__ : Tuple = self.dummy_cond_unet snake_case__ : Tuple = PNDMScheduler(skip_prk_steps=__UpperCamelCase ) snake_case__ : str = self.dummy_vae snake_case__ : Optional[int] = self.dummy_text_encoder snake_case__ : Union[str, Any] = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) # put models in fp16 snake_case__ : int = unet.half() snake_case__ : int = vae.half() snake_case__ : Optional[int] = bert.half() # make sure here that pndm scheduler skips prk snake_case__ : Optional[Any] = StableDiffusionPipeline( unet=__UpperCamelCase , scheduler=__UpperCamelCase , vae=__UpperCamelCase , text_encoder=__UpperCamelCase , tokenizer=__UpperCamelCase , safety_checker=__UpperCamelCase , feature_extractor=self.dummy_extractor , ) snake_case__ : Optional[Any] = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : str = 'A painting of a squirrel eating a burger' snake_case__ : Optional[int] = sd_pipe([prompt] , num_inference_steps=2 , output_type='np' ).images assert image.shape == (1, 64, 64, 3) @nightly @require_torch_gpu class __snake_case ( unittest.TestCase ): def __a ( self ) -> List[Any]: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __a ( self ) -> Any: '''simple docstring''' snake_case__ : Dict = StableDiffusionPipeline.from_pretrained('runwayml/stable-diffusion-v1-5' , safety_checker=__UpperCamelCase ) snake_case__ : Optional[Any] = LMSDiscreteScheduler.from_config(sd_pipe.scheduler.config ) snake_case__ : Tuple = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : List[str] = ( 'portrait of girl with smokey eyes makeup in abandoned hotel, grange clothes, redshift, wide high angle' ' coloured polaroid photograph with flash, kodak film, hyper real, stunning moody cinematography, with' ' anamorphic lenses, by maripol, fallen angels by wong kar - wai, style of suspiria and neon demon and' ' children from bahnhof zoo, detailed ' ) snake_case__ : Optional[Any] = 4003660346 snake_case__ : Optional[Any] = 7 # without safety guidance (sld_guidance_scale = 0) snake_case__ : Any = torch.manual_seed(__UpperCamelCase ) snake_case__ : Optional[Any] = sd_pipe( [prompt] , generator=__UpperCamelCase , guidance_scale=__UpperCamelCase , num_inference_steps=50 , output_type='np' , width=512 , height=512 , sld_guidance_scale=0 , ) snake_case__ : List[str] = output.images snake_case__ : List[Any] = image[0, -3:, -3:, -1] snake_case__ : List[Any] = [0.2_2_7_8, 0.2_2_3_1, 0.2_2_4_9, 0.2_3_3_3, 0.2_3_0_3, 0.1_8_8_5, 0.2_2_7_3, 0.2_1_4_4, 0.2_1_7_6] assert image.shape == (1, 512, 512, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 # without safety guidance (strong configuration) snake_case__ : str = torch.manual_seed(__UpperCamelCase ) snake_case__ : Dict = sd_pipe( [prompt] , generator=__UpperCamelCase , guidance_scale=__UpperCamelCase , num_inference_steps=50 , output_type='np' , width=512 , height=512 , sld_guidance_scale=2000 , sld_warmup_steps=7 , sld_threshold=0.0_2_5 , sld_momentum_scale=0.5 , sld_mom_beta=0.7 , ) snake_case__ : Tuple = output.images snake_case__ : Union[str, Any] = image[0, -3:, -3:, -1] snake_case__ : Union[str, Any] = [0.2_3_8_3, 0.2_2_7_6, 0.2_3_6, 0.2_1_9_2, 0.2_1_8_6, 0.2_0_5_3, 0.1_9_7_1, 0.1_9_0_1, 0.1_7_1_9] assert image.shape == (1, 512, 512, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __a ( self ) -> List[str]: '''simple docstring''' snake_case__ : str = StableDiffusionPipeline.from_pretrained('runwayml/stable-diffusion-v1-5' , safety_checker=__UpperCamelCase ) snake_case__ : str = LMSDiscreteScheduler.from_config(sd_pipe.scheduler.config ) snake_case__ : Union[str, Any] = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Tuple = 'padme amidala taking a bath artwork, safe for work, no nudity' snake_case__ : Union[str, Any] = 2734971755 snake_case__ : Union[str, Any] = 7 snake_case__ : Dict = torch.manual_seed(__UpperCamelCase ) snake_case__ : List[str] = sd_pipe( [prompt] , generator=__UpperCamelCase , guidance_scale=__UpperCamelCase , num_inference_steps=50 , output_type='np' , width=512 , height=512 , sld_guidance_scale=0 , ) snake_case__ : Any = output.images snake_case__ : Any = image[0, -3:, -3:, -1] snake_case__ : Tuple = [0.3_5_0_2, 0.3_6_2_2, 0.3_3_9_6, 0.3_6_4_2, 0.3_4_7_8, 0.3_3_1_8, 0.3_5, 0.3_3_4_8, 0.3_2_9_7] assert image.shape == (1, 512, 512, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 snake_case__ : str = torch.manual_seed(__UpperCamelCase ) snake_case__ : Optional[Any] = sd_pipe( [prompt] , generator=__UpperCamelCase , guidance_scale=__UpperCamelCase , num_inference_steps=50 , output_type='np' , width=512 , height=512 , sld_guidance_scale=2000 , sld_warmup_steps=7 , sld_threshold=0.0_2_5 , sld_momentum_scale=0.5 , sld_mom_beta=0.7 , ) snake_case__ : List[Any] = output.images snake_case__ : Optional[int] = image[0, -3:, -3:, -1] snake_case__ : List[str] = [0.5_5_3_1, 0.5_2_0_6, 0.4_8_9_5, 0.5_1_5_6, 0.5_1_8_2, 0.4_7_5_1, 0.4_8_0_2, 0.4_8_0_3, 0.4_4_4_3] assert image.shape == (1, 512, 512, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __a ( self ) -> Tuple: '''simple docstring''' snake_case__ : str = StableDiffusionPipeline.from_pretrained('runwayml/stable-diffusion-v1-5' ) snake_case__ : Union[str, Any] = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Union[str, Any] = ( 'the four horsewomen of the apocalypse, painting by tom of finland, gaston bussiere, craig mullins, j. c.' ' leyendecker' ) snake_case__ : int = 1044355234 snake_case__ : Any = 12 snake_case__ : int = torch.manual_seed(__UpperCamelCase ) snake_case__ : str = sd_pipe( [prompt] , generator=__UpperCamelCase , guidance_scale=__UpperCamelCase , num_inference_steps=50 , output_type='np' , width=512 , height=512 , sld_guidance_scale=0 , ) snake_case__ : List[Any] = output.images snake_case__ : Union[str, Any] = image[0, -3:, -3:, -1] snake_case__ : Union[str, Any] = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] ) assert image.shape == (1, 512, 512, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-7 snake_case__ : Union[str, Any] = torch.manual_seed(__UpperCamelCase ) snake_case__ : Dict = sd_pipe( [prompt] , generator=__UpperCamelCase , guidance_scale=__UpperCamelCase , num_inference_steps=50 , output_type='np' , width=512 , height=512 , sld_guidance_scale=2000 , sld_warmup_steps=7 , sld_threshold=0.0_2_5 , sld_momentum_scale=0.5 , sld_mom_beta=0.7 , ) snake_case__ : Dict = output.images snake_case__ : Union[str, Any] = image[0, -3:, -3:, -1] snake_case__ : Optional[int] = np.array([0.5_8_1_8, 0.6_2_8_5, 0.6_8_3_5, 0.6_0_1_9, 0.6_2_5, 0.6_7_5_4, 0.6_0_9_6, 0.6_3_3_4, 0.6_5_6_1] ) assert image.shape == (1, 512, 512, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
699
import io import itertools import json from dataclasses import dataclass from typing import Optional import pyarrow as pa import pyarrow.json as paj import datasets from datasets.table import table_cast from datasets.utils.file_utils import readline lowerCAmelCase__ : List[Any] = datasets.utils.logging.get_logger(__name__) @dataclass class __snake_case ( datasets.BuilderConfig ): __lowerCamelCase = None __lowerCamelCase = "utf-8" __lowerCamelCase = None __lowerCamelCase = None __lowerCamelCase = True # deprecated __lowerCamelCase = None # deprecated __lowerCamelCase = 10 << 20 # 10MB __lowerCamelCase = None class __snake_case ( datasets.ArrowBasedBuilder ): __lowerCamelCase = JsonConfig def __a ( self ) -> Optional[Any]: '''simple docstring''' if self.config.block_size is not None: logger.warning('The JSON loader parameter `block_size` is deprecated. Please use `chunksize` instead' ) snake_case__ : str = self.config.block_size if self.config.use_threads is not True: logger.warning( 'The JSON loader parameter `use_threads` is deprecated and doesn\'t have any effect anymore.' ) if self.config.newlines_in_values is not None: raise ValueError('The JSON loader parameter `newlines_in_values` is no longer supported' ) return datasets.DatasetInfo(features=self.config.features ) def __a ( self , __UpperCamelCase ) -> Dict: '''simple docstring''' if not self.config.data_files: raise ValueError(F"""At least one data file must be specified, but got data_files={self.config.data_files}""" ) snake_case__ : int = dl_manager.download_and_extract(self.config.data_files ) if isinstance(__UpperCamelCase , (str, list, tuple) ): snake_case__ : Any = data_files if isinstance(__UpperCamelCase , __UpperCamelCase ): snake_case__ : Optional[Any] = [files] snake_case__ : List[str] = [dl_manager.iter_files(__UpperCamelCase ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'files': files} )] snake_case__ : List[Any] = [] for split_name, files in data_files.items(): if isinstance(__UpperCamelCase , __UpperCamelCase ): snake_case__ : List[Any] = [files] snake_case__ : Any = [dl_manager.iter_files(__UpperCamelCase ) for file in files] splits.append(datasets.SplitGenerator(name=__UpperCamelCase , gen_kwargs={'files': files} ) ) return splits def __a ( self , __UpperCamelCase ) -> pa.Table: '''simple docstring''' if self.config.features is not None: # adding missing columns for column_name in set(self.config.features ) - set(pa_table.column_names ): snake_case__ : List[Any] = self.config.features.arrow_schema.field(__UpperCamelCase ).type snake_case__ : List[str] = pa_table.append_column(__UpperCamelCase , pa.array([None] * len(__UpperCamelCase ) , type=__UpperCamelCase ) ) # more expensive cast to support nested structures with keys in a different order # allows str <-> int/float or str to Audio for example snake_case__ : List[str] = table_cast(__UpperCamelCase , self.config.features.arrow_schema ) return pa_table def __a ( self , __UpperCamelCase ) -> int: '''simple docstring''' for file_idx, file in enumerate(itertools.chain.from_iterable(__UpperCamelCase ) ): # If the file is one json object and if we need to look at the list of items in one specific field if self.config.field is not None: with open(__UpperCamelCase , encoding=self.config.encoding , errors=self.config.encoding_errors ) as f: snake_case__ : Union[str, Any] = json.load(__UpperCamelCase ) # We keep only the field we are interested in snake_case__ : Tuple = dataset[self.config.field] # We accept two format: a list of dicts or a dict of lists if isinstance(__UpperCamelCase , (list, tuple) ): snake_case__ : List[Any] = set().union(*[row.keys() for row in dataset] ) snake_case__ : List[Any] = {col: [row.get(__UpperCamelCase ) for row in dataset] for col in keys} else: snake_case__ : List[Any] = dataset snake_case__ : Dict = pa.Table.from_pydict(__UpperCamelCase ) yield file_idx, self._cast_table(__UpperCamelCase ) # If the file has one json object per line else: with open(__UpperCamelCase , 'rb' ) as f: snake_case__ : Optional[int] = 0 # Use block_size equal to the chunk size divided by 32 to leverage multithreading # Set a default minimum value of 16kB if the chunk size is really small snake_case__ : Tuple = max(self.config.chunksize // 32 , 16 << 10 ) snake_case__ : Optional[Any] = ( self.config.encoding_errors if self.config.encoding_errors is not None else 'strict' ) while True: snake_case__ : Optional[int] = f.read(self.config.chunksize ) if not batch: break # Finish current line try: batch += f.readline() except (AttributeError, io.UnsupportedOperation): batch += readline(__UpperCamelCase ) # PyArrow only accepts utf-8 encoded bytes if self.config.encoding != "utf-8": snake_case__ : int = batch.decode(self.config.encoding , errors=__UpperCamelCase ).encode('utf-8' ) try: while True: try: snake_case__ : List[str] = paj.read_json( io.BytesIO(__UpperCamelCase ) , read_options=paj.ReadOptions(block_size=__UpperCamelCase ) ) break except (pa.ArrowInvalid, pa.ArrowNotImplementedError) as e: if ( isinstance(__UpperCamelCase , pa.ArrowInvalid ) and "straddling" not in str(__UpperCamelCase ) or block_size > len(__UpperCamelCase ) ): raise else: # Increase the block size in case it was too small. # The block size will be reset for the next file. logger.debug( F"""Batch of {len(__UpperCamelCase )} bytes couldn't be parsed with block_size={block_size}. Retrying with block_size={block_size * 2}.""" ) block_size *= 2 except pa.ArrowInvalid as e: try: with open( __UpperCamelCase , encoding=self.config.encoding , errors=self.config.encoding_errors ) as f: snake_case__ : Tuple = json.load(__UpperCamelCase ) except json.JSONDecodeError: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCamelCase )}: {e}""" ) raise e # If possible, parse the file as a list of json objects and exit the loop if isinstance(__UpperCamelCase , __UpperCamelCase ): # list is the only sequence type supported in JSON try: snake_case__ : str = set().union(*[row.keys() for row in dataset] ) snake_case__ : Union[str, Any] = {col: [row.get(__UpperCamelCase ) for row in dataset] for col in keys} snake_case__ : List[str] = pa.Table.from_pydict(__UpperCamelCase ) except (pa.ArrowInvalid, AttributeError) as e: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCamelCase )}: {e}""" ) raise ValueError(F"""Not able to read records in the JSON file at {file}.""" ) from None yield file_idx, self._cast_table(__UpperCamelCase ) break else: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCamelCase )}: {e}""" ) raise ValueError( F"""Not able to read records in the JSON file at {file}. """ F"""You should probably indicate the field of the JSON file containing your records. """ F"""This JSON file contain the following fields: {str(list(dataset.keys() ) )}. """ F"""Select the correct one and provide it as `field='XXX'` to the dataset loading method. """ ) from None # Uncomment for debugging (will print the Arrow table size and elements) # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}") # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows))) yield (file_idx, batch_idx), self._cast_table(__UpperCamelCase ) batch_idx += 1
699
1
from typing import Any, Dict, Optional import torch import torch.nn.functional as F from torch import nn from ..utils import maybe_allow_in_graph from .activations import get_activation from .attention_processor import Attention from .embeddings import CombinedTimestepLabelEmbeddings @maybe_allow_in_graph class __snake_case ( nn.Module ): def __init__( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=0.0 , __UpperCamelCase = None , __UpperCamelCase = "geglu" , __UpperCamelCase = None , __UpperCamelCase = False , __UpperCamelCase = False , __UpperCamelCase = False , __UpperCamelCase = False , __UpperCamelCase = True , __UpperCamelCase = "layer_norm" , __UpperCamelCase = False , ) -> Dict: '''simple docstring''' super().__init__() snake_case__ : int = only_cross_attention snake_case__ : Union[str, Any] = (num_embeds_ada_norm is not None) and norm_type == 'ada_norm_zero' snake_case__ : int = (num_embeds_ada_norm is not None) and norm_type == 'ada_norm' if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None: raise ValueError( F"""`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to""" F""" define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}.""" ) # Define 3 blocks. Each block has its own normalization layer. # 1. Self-Attn if self.use_ada_layer_norm: snake_case__ : List[str] = AdaLayerNorm(__UpperCamelCase , __UpperCamelCase ) elif self.use_ada_layer_norm_zero: snake_case__ : Optional[int] = AdaLayerNormZero(__UpperCamelCase , __UpperCamelCase ) else: snake_case__ : Tuple = nn.LayerNorm(__UpperCamelCase , elementwise_affine=__UpperCamelCase ) snake_case__ : List[str] = Attention( query_dim=__UpperCamelCase , heads=__UpperCamelCase , dim_head=__UpperCamelCase , dropout=__UpperCamelCase , bias=__UpperCamelCase , cross_attention_dim=cross_attention_dim if only_cross_attention else None , upcast_attention=__UpperCamelCase , ) # 2. Cross-Attn if cross_attention_dim is not None or double_self_attention: # We currently only use AdaLayerNormZero for self attention where there will only be one attention block. # I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during # the second cross attention block. snake_case__ : Optional[int] = ( AdaLayerNorm(__UpperCamelCase , __UpperCamelCase ) if self.use_ada_layer_norm else nn.LayerNorm(__UpperCamelCase , elementwise_affine=__UpperCamelCase ) ) snake_case__ : Any = Attention( query_dim=__UpperCamelCase , cross_attention_dim=cross_attention_dim if not double_self_attention else None , heads=__UpperCamelCase , dim_head=__UpperCamelCase , dropout=__UpperCamelCase , bias=__UpperCamelCase , upcast_attention=__UpperCamelCase , ) # is self-attn if encoder_hidden_states is none else: snake_case__ : int = None snake_case__ : Tuple = None # 3. Feed-forward snake_case__ : str = nn.LayerNorm(__UpperCamelCase , elementwise_affine=__UpperCamelCase ) snake_case__ : List[Any] = FeedForward(__UpperCamelCase , dropout=__UpperCamelCase , activation_fn=__UpperCamelCase , final_dropout=__UpperCamelCase ) # let chunk size default to None snake_case__ : Optional[int] = None snake_case__ : str = 0 def __a ( self , __UpperCamelCase , __UpperCamelCase ) -> Optional[Any]: '''simple docstring''' snake_case__ : str = chunk_size snake_case__ : List[Any] = dim def __a ( self , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = None , ) -> int: '''simple docstring''' if self.use_ada_layer_norm: snake_case__ : Optional[Any] = self.norma(__UpperCamelCase , __UpperCamelCase ) elif self.use_ada_layer_norm_zero: snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ : List[Any] = self.norma( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , hidden_dtype=hidden_states.dtype ) else: snake_case__ : Union[str, Any] = self.norma(__UpperCamelCase ) snake_case__ : List[Any] = cross_attention_kwargs if cross_attention_kwargs is not None else {} snake_case__ : str = self.attna( __UpperCamelCase , encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None , attention_mask=__UpperCamelCase , **__UpperCamelCase , ) if self.use_ada_layer_norm_zero: snake_case__ : Dict = gate_msa.unsqueeze(1 ) * attn_output snake_case__ : str = attn_output + hidden_states # 2. Cross-Attention if self.attna is not None: snake_case__ : Union[str, Any] = ( self.norma(__UpperCamelCase , __UpperCamelCase ) if self.use_ada_layer_norm else self.norma(__UpperCamelCase ) ) snake_case__ : str = self.attna( __UpperCamelCase , encoder_hidden_states=__UpperCamelCase , attention_mask=__UpperCamelCase , **__UpperCamelCase , ) snake_case__ : Any = attn_output + hidden_states # 3. Feed-forward snake_case__ : Union[str, Any] = self.norma(__UpperCamelCase ) if self.use_ada_layer_norm_zero: snake_case__ : Union[str, Any] = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None] if self._chunk_size is not None: # "feed_forward_chunk_size" can be used to save memory if norm_hidden_states.shape[self._chunk_dim] % self._chunk_size != 0: raise ValueError( F"""`hidden_states` dimension to be chunked: {norm_hidden_states.shape[self._chunk_dim]} has to be divisible by chunk size: {self._chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`.""" ) snake_case__ : int = norm_hidden_states.shape[self._chunk_dim] // self._chunk_size snake_case__ : List[str] = torch.cat( [self.ff(__UpperCamelCase ) for hid_slice in norm_hidden_states.chunk(__UpperCamelCase , dim=self._chunk_dim )] , dim=self._chunk_dim , ) else: snake_case__ : str = self.ff(__UpperCamelCase ) if self.use_ada_layer_norm_zero: snake_case__ : Optional[Any] = gate_mlp.unsqueeze(1 ) * ff_output snake_case__ : List[str] = ff_output + hidden_states return hidden_states class __snake_case ( nn.Module ): def __init__( self , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = 4 , __UpperCamelCase = 0.0 , __UpperCamelCase = "geglu" , __UpperCamelCase = False , ) -> int: '''simple docstring''' super().__init__() snake_case__ : Optional[int] = int(dim * mult ) snake_case__ : Tuple = dim_out if dim_out is not None else dim if activation_fn == "gelu": snake_case__ : Union[str, Any] = GELU(__UpperCamelCase , __UpperCamelCase ) if activation_fn == "gelu-approximate": snake_case__ : List[Any] = GELU(__UpperCamelCase , __UpperCamelCase , approximate='tanh' ) elif activation_fn == "geglu": snake_case__ : Tuple = GEGLU(__UpperCamelCase , __UpperCamelCase ) elif activation_fn == "geglu-approximate": snake_case__ : List[Any] = ApproximateGELU(__UpperCamelCase , __UpperCamelCase ) snake_case__ : Tuple = nn.ModuleList([] ) # project in self.net.append(__UpperCamelCase ) # project dropout self.net.append(nn.Dropout(__UpperCamelCase ) ) # project out self.net.append(nn.Linear(__UpperCamelCase , __UpperCamelCase ) ) # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout if final_dropout: self.net.append(nn.Dropout(__UpperCamelCase ) ) def __a ( self , __UpperCamelCase ) -> str: '''simple docstring''' for module in self.net: snake_case__ : Union[str, Any] = module(__UpperCamelCase ) return hidden_states class __snake_case ( nn.Module ): def __init__( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = "none" ) -> List[Any]: '''simple docstring''' super().__init__() snake_case__ : Optional[Any] = nn.Linear(__UpperCamelCase , __UpperCamelCase ) snake_case__ : List[Any] = approximate def __a ( self , __UpperCamelCase ) -> Optional[Any]: '''simple docstring''' if gate.device.type != "mps": return F.gelu(__UpperCamelCase , approximate=self.approximate ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) , approximate=self.approximate ).to(dtype=gate.dtype ) def __a ( self , __UpperCamelCase ) -> Optional[int]: '''simple docstring''' snake_case__ : int = self.proj(__UpperCamelCase ) snake_case__ : str = self.gelu(__UpperCamelCase ) return hidden_states class __snake_case ( nn.Module ): def __init__( self , __UpperCamelCase , __UpperCamelCase ) -> Dict: '''simple docstring''' super().__init__() snake_case__ : List[Any] = nn.Linear(__UpperCamelCase , dim_out * 2 ) def __a ( self , __UpperCamelCase ) -> Optional[Any]: '''simple docstring''' if gate.device.type != "mps": return F.gelu(__UpperCamelCase ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) ).to(dtype=gate.dtype ) def __a ( self , __UpperCamelCase ) -> Any: '''simple docstring''' snake_case__ , snake_case__ : Tuple = self.proj(__UpperCamelCase ).chunk(2 , dim=-1 ) return hidden_states * self.gelu(__UpperCamelCase ) class __snake_case ( nn.Module ): def __init__( self , __UpperCamelCase , __UpperCamelCase ) -> Any: '''simple docstring''' super().__init__() snake_case__ : str = nn.Linear(__UpperCamelCase , __UpperCamelCase ) def __a ( self , __UpperCamelCase ) -> List[Any]: '''simple docstring''' snake_case__ : List[str] = self.proj(__UpperCamelCase ) return x * torch.sigmoid(1.7_0_2 * x ) class __snake_case ( nn.Module ): def __init__( self , __UpperCamelCase , __UpperCamelCase ) -> Optional[Any]: '''simple docstring''' super().__init__() snake_case__ : List[Any] = nn.Embedding(__UpperCamelCase , __UpperCamelCase ) snake_case__ : Optional[int] = nn.SiLU() snake_case__ : Union[str, Any] = nn.Linear(__UpperCamelCase , embedding_dim * 2 ) snake_case__ : Union[str, Any] = nn.LayerNorm(__UpperCamelCase , elementwise_affine=__UpperCamelCase ) def __a ( self , __UpperCamelCase , __UpperCamelCase ) -> Dict: '''simple docstring''' snake_case__ : Optional[int] = self.linear(self.silu(self.emb(__UpperCamelCase ) ) ) snake_case__ , snake_case__ : List[str] = torch.chunk(__UpperCamelCase , 2 ) snake_case__ : List[Any] = self.norm(__UpperCamelCase ) * (1 + scale) + shift return x class __snake_case ( nn.Module ): def __init__( self , __UpperCamelCase , __UpperCamelCase ) -> List[str]: '''simple docstring''' super().__init__() snake_case__ : List[Any] = CombinedTimestepLabelEmbeddings(__UpperCamelCase , __UpperCamelCase ) snake_case__ : Optional[int] = nn.SiLU() snake_case__ : int = nn.Linear(__UpperCamelCase , 6 * embedding_dim , bias=__UpperCamelCase ) snake_case__ : Tuple = nn.LayerNorm(__UpperCamelCase , elementwise_affine=__UpperCamelCase , eps=1E-6 ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=None ) -> int: '''simple docstring''' snake_case__ : Optional[int] = self.linear(self.silu(self.emb(__UpperCamelCase , __UpperCamelCase , hidden_dtype=__UpperCamelCase ) ) ) snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ : Optional[int] = emb.chunk(6 , dim=1 ) snake_case__ : Any = self.norm(__UpperCamelCase ) * (1 + scale_msa[:, None]) + shift_msa[:, None] return x, gate_msa, shift_mlp, scale_mlp, gate_mlp class __snake_case ( nn.Module ): def __init__( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = 1E-5 ) -> Union[str, Any]: '''simple docstring''' super().__init__() snake_case__ : int = num_groups snake_case__ : Dict = eps if act_fn is None: snake_case__ : List[Any] = None else: snake_case__ : List[str] = get_activation(__UpperCamelCase ) snake_case__ : Union[str, Any] = nn.Linear(__UpperCamelCase , out_dim * 2 ) def __a ( self , __UpperCamelCase , __UpperCamelCase ) -> int: '''simple docstring''' if self.act: snake_case__ : List[str] = self.act(__UpperCamelCase ) snake_case__ : Tuple = self.linear(__UpperCamelCase ) snake_case__ : List[Any] = emb[:, :, None, None] snake_case__ , snake_case__ : List[Any] = emb.chunk(2 , dim=1 ) snake_case__ : str = F.group_norm(__UpperCamelCase , self.num_groups , eps=self.eps ) snake_case__ : Any = x * (1 + scale) + shift return x
699
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase__ : Any = {'''configuration_xglm''': ['''XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XGLMConfig''']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : str = ['''XGLMTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict = ['''XGLMTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Optional[int] = [ '''XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XGLMForCausalLM''', '''XGLMModel''', '''XGLMPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict = [ '''FlaxXGLMForCausalLM''', '''FlaxXGLMModel''', '''FlaxXGLMPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict = [ '''TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXGLMForCausalLM''', '''TFXGLMModel''', '''TFXGLMPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm import XGLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm_fast import XGLMTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, TFXGLMPreTrainedModel, ) else: import sys lowerCAmelCase__ : Tuple = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
699
1
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import datasets import numpy as np import tensorflow as tf from transformers import ( AutoConfig, AutoTokenizer, EvalPrediction, HfArgumentParser, PreTrainedTokenizer, TFAutoModelForSequenceClassification, TFTrainer, TFTrainingArguments, ) from transformers.utils import logging as hf_logging hf_logging.set_verbosity_info() hf_logging.enable_default_handler() hf_logging.enable_explicit_format() def UpperCamelCase__ ( A__ , A__ , A__ , A__ , A__ , A__ = None , ) -> Optional[int]: snake_case__ : List[str] = {} if train_file is not None: snake_case__ : Tuple = [train_file] if eval_file is not None: snake_case__ : Dict = [eval_file] if test_file is not None: snake_case__ : str = [test_file] snake_case__ : Optional[Any] = datasets.load_dataset('csv' , data_files=A__ ) snake_case__ : Any = list(ds[list(files.keys() )[0]].features.keys() ) snake_case__ : Optional[Any] = features_name.pop(A__ ) snake_case__ : Optional[Any] = list(set(ds[list(files.keys() )[0]][label_name] ) ) snake_case__ : str = {label: i for i, label in enumerate(A__ )} snake_case__ : int = tokenizer.model_input_names snake_case__ : int = {} if len(A__ ) == 1: for k in files.keys(): snake_case__ : str = ds[k].map( lambda A__ : tokenizer.batch_encode_plus( example[features_name[0]] , truncation=A__ , max_length=A__ , padding='max_length' ) , batched=A__ , ) elif len(A__ ) == 2: for k in files.keys(): snake_case__ : Optional[int] = ds[k].map( lambda A__ : tokenizer.batch_encode_plus( (example[features_name[0]], example[features_name[1]]) , truncation=A__ , max_length=A__ , padding='max_length' , ) , batched=A__ , ) def gen_train(): for ex in transformed_ds[datasets.Split.TRAIN]: snake_case__ : int = {k: v for k, v in ex.items() if k in input_names} snake_case__ : Any = labelaid[ex[label_name]] yield (d, label) def gen_val(): for ex in transformed_ds[datasets.Split.VALIDATION]: snake_case__ : int = {k: v for k, v in ex.items() if k in input_names} snake_case__ : Union[str, Any] = labelaid[ex[label_name]] yield (d, label) def gen_test(): for ex in transformed_ds[datasets.Split.TEST]: snake_case__ : Dict = {k: v for k, v in ex.items() if k in input_names} snake_case__ : List[str] = labelaid[ex[label_name]] yield (d, label) snake_case__ : Any = ( tf.data.Dataset.from_generator( A__ , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.TRAIN in transformed_ds else None ) if train_ds is not None: snake_case__ : str = train_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TRAIN] ) ) ) snake_case__ : Optional[int] = ( tf.data.Dataset.from_generator( A__ , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.VALIDATION in transformed_ds else None ) if val_ds is not None: snake_case__ : Optional[int] = val_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.VALIDATION] ) ) ) snake_case__ : List[str] = ( tf.data.Dataset.from_generator( A__ , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.TEST in transformed_ds else None ) if test_ds is not None: snake_case__ : str = test_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TEST] ) ) ) return train_ds, val_ds, test_ds, labelaid lowerCAmelCase__ : List[str] = logging.getLogger(__name__) @dataclass class __snake_case : __lowerCamelCase = field(metadata={"""help""": """Which column contains the label"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The path of the training file"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The path of the development file"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The path of the test file"""} ) __lowerCamelCase = field( default=128 ,metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } ,) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Overwrite the cached training and evaluation sets"""} ) @dataclass class __snake_case : __lowerCamelCase = field( metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """Set this flag to use fast tokenization."""} ) # If you want to tweak more attributes on your tokenizer, you should do it in a distinct script, # or just modify its tokenizer_config.json. __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} ,) def UpperCamelCase__ ( ) -> Union[str, 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. snake_case__ : int = HfArgumentParser((ModelArguments, DataTrainingArguments, TFTrainingArguments) ) snake_case__ , snake_case__ , snake_case__ : Dict = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( F"""Output directory ({training_args.output_dir}) already exists and is not empty. Use""" ' --overwrite_output_dir to overcome.' ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , level=logging.INFO , ) logger.info( F"""n_replicas: {training_args.n_replicas}, distributed training: {bool(training_args.n_replicas > 1 )}, """ F"""16-bits training: {training_args.fpaa}""" ) logger.info(F"""Training/evaluation parameters {training_args}""" ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. snake_case__ : Dict = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) snake_case__ , snake_case__ , snake_case__ , snake_case__ : Dict = get_tfds( train_file=data_args.train_file , eval_file=data_args.dev_file , test_file=data_args.test_file , tokenizer=A__ , label_column_id=data_args.label_column_id , max_seq_length=data_args.max_seq_length , ) snake_case__ : Dict = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=len(A__ ) , labelaid=A__ , idalabel={id: label for label, id in labelaid.items()} , finetuning_task='text-classification' , cache_dir=model_args.cache_dir , ) with training_args.strategy.scope(): snake_case__ : Optional[Any] = TFAutoModelForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_pt=bool('.bin' in model_args.model_name_or_path ) , config=A__ , cache_dir=model_args.cache_dir , ) def compute_metrics(A__ ) -> Dict: snake_case__ : Optional[Any] = np.argmax(p.predictions , axis=1 ) return {"acc": (preds == p.label_ids).mean()} # Initialize our Trainer snake_case__ : Any = TFTrainer( model=A__ , args=A__ , train_dataset=A__ , eval_dataset=A__ , compute_metrics=A__ , ) # Training if training_args.do_train: trainer.train() trainer.save_model() tokenizer.save_pretrained(training_args.output_dir ) # Evaluation snake_case__ : Dict = {} if training_args.do_eval: logger.info('*** Evaluate ***' ) snake_case__ : Tuple = trainer.evaluate() snake_case__ : Any = os.path.join(training_args.output_dir , 'eval_results.txt' ) with open(A__ , 'w' ) as writer: logger.info('***** Eval results *****' ) for key, value in result.items(): logger.info(F""" {key} = {value}""" ) writer.write(F"""{key} = {value}\n""" ) results.update(A__ ) return results if __name__ == "__main__": main()
699
from __future__ import annotations import random # Maximum size of the population. Bigger could be faster but is more memory expensive. lowerCAmelCase__ : Dict = 2_00 # Number of elements selected in every generation of evolution. The selection takes # place from best to worst of that generation and must be smaller than N_POPULATION. lowerCAmelCase__ : List[str] = 50 # Probability that an element of a generation can mutate, changing one of its genes. # This will guarantee that all genes will be used during evolution. lowerCAmelCase__ : List[str] = 0.4 # Just a seed to improve randomness required by the algorithm. random.seed(random.randint(0, 10_00)) def UpperCamelCase__ ( A__ , A__ ) -> tuple[str, float]: snake_case__ : Tuple = len([g for position, g in enumerate(A__ ) if g == main_target[position]] ) return (item, float(A__ )) def UpperCamelCase__ ( A__ , A__ ) -> tuple[str, str]: snake_case__ : str = random.randint(0 , len(A__ ) - 1 ) snake_case__ : int = parent_a[:random_slice] + parent_a[random_slice:] snake_case__ : Any = parent_a[:random_slice] + parent_a[random_slice:] return (child_a, child_a) def UpperCamelCase__ ( A__ , A__ ) -> str: snake_case__ : List[Any] = list(A__ ) if random.uniform(0 , 1 ) < MUTATION_PROBABILITY: snake_case__ : Optional[Any] = random.choice(A__ ) return "".join(A__ ) def UpperCamelCase__ ( A__ , A__ , A__ , ) -> list[str]: snake_case__ : Tuple = [] # Generate more children proportionally to the fitness score. snake_case__ : Optional[Any] = int(parent_a[1] * 100 ) + 1 snake_case__ : str = 10 if child_n >= 10 else child_n for _ in range(A__ ): snake_case__ : Any = population_score[random.randint(0 , A__ )][0] snake_case__ , snake_case__ : int = crossover(parent_a[0] , A__ ) # Append new string to the population list. pop.append(mutate(A__ , A__ ) ) pop.append(mutate(A__ , A__ ) ) return pop def UpperCamelCase__ ( A__ , A__ , A__ = True ) -> tuple[int, int, str]: # Verify if N_POPULATION is bigger than N_SELECTED if N_POPULATION < N_SELECTED: snake_case__ : Union[str, Any] = F"""{N_POPULATION} must be bigger than {N_SELECTED}""" raise ValueError(A__ ) # Verify that the target contains no genes besides the ones inside genes variable. snake_case__ : Tuple = sorted({c for c in target if c not in genes} ) if not_in_genes_list: snake_case__ : int = F"""{not_in_genes_list} is not in genes list, evolution cannot converge""" raise ValueError(A__ ) # Generate random starting population. snake_case__ : Union[str, Any] = [] for _ in range(A__ ): population.append(''.join([random.choice(A__ ) for i in range(len(A__ ) )] ) ) # Just some logs to know what the algorithms is doing. snake_case__ , snake_case__ : str = 0, 0 # This loop will end when we find a perfect match for our target. while True: generation += 1 total_population += len(A__ ) # Random population created. Now it's time to evaluate. # Adding a bit of concurrency can make everything faster, # # import concurrent.futures # population_score: list[tuple[str, float]] = [] # with concurrent.futures.ThreadPoolExecutor( # max_workers=NUM_WORKERS) as executor: # futures = {executor.submit(evaluate, item) for item in population} # concurrent.futures.wait(futures) # population_score = [item.result() for item in futures] # # but with a simple algorithm like this, it will probably be slower. # We just need to call evaluate for every item inside the population. snake_case__ : List[Any] = [evaluate(A__ , A__ ) for item in population] # Check if there is a matching evolution. snake_case__ : int = sorted(A__ , key=lambda A__ : x[1] , reverse=A__ ) if population_score[0][0] == target: return (generation, total_population, population_score[0][0]) # Print the best result every 10 generation. # Just to know that the algorithm is working. if debug and generation % 10 == 0: print( F"""\nGeneration: {generation}""" F"""\nTotal Population:{total_population}""" F"""\nBest score: {population_score[0][1]}""" F"""\nBest string: {population_score[0][0]}""" ) # Flush the old population, keeping some of the best evolutions. # Keeping this avoid regression of evolution. snake_case__ : Optional[int] = population[: int(N_POPULATION / 3 )] population.clear() population.extend(A__ ) # Normalize population score to be between 0 and 1. snake_case__ : str = [ (item, score / len(A__ )) for item, score in population_score ] # This is selection for i in range(A__ ): population.extend(select(population_score[int(A__ )] , A__ , A__ ) ) # Check if the population has already reached the maximum value and if so, # break the cycle. If this check is disabled, the algorithm will take # forever to compute large strings, but will also calculate small strings in # a far fewer generations. if len(A__ ) > N_POPULATION: break if __name__ == "__main__": lowerCAmelCase__ : str = ( '''This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!''' ) lowerCAmelCase__ : Optional[Any] = list( ''' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm''' '''nopqrstuvwxyz.,;!?+-*#@^\'èéòà€ù=)(&%$£/\\''' ) lowerCAmelCase__, lowerCAmelCase__, lowerCAmelCase__ : List[str] = basic(target_str, genes_list) print( F'''\nGeneration: {generation}\nTotal Population: {population}\nTarget: {target}''' )
699
1
import importlib import json import os from collections import OrderedDict from typing import Dict, Optional, Union # Build the list of all image processors from ...configuration_utils import PretrainedConfig from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code from ...image_processing_utils import ImageProcessingMixin from ...utils import CONFIG_NAME, IMAGE_PROCESSOR_NAME, get_file_from_repo, logging from .auto_factory import _LazyAutoMapping from .configuration_auto import ( CONFIG_MAPPING_NAMES, AutoConfig, model_type_to_module_name, replace_list_option_in_docstrings, ) lowerCAmelCase__ : Tuple = logging.get_logger(__name__) lowerCAmelCase__ : List[str] = OrderedDict( [ ('''align''', '''EfficientNetImageProcessor'''), ('''beit''', '''BeitImageProcessor'''), ('''bit''', '''BitImageProcessor'''), ('''blip''', '''BlipImageProcessor'''), ('''blip-2''', '''BlipImageProcessor'''), ('''bridgetower''', '''BridgeTowerImageProcessor'''), ('''chinese_clip''', '''ChineseCLIPImageProcessor'''), ('''clip''', '''CLIPImageProcessor'''), ('''clipseg''', '''ViTImageProcessor'''), ('''conditional_detr''', '''ConditionalDetrImageProcessor'''), ('''convnext''', '''ConvNextImageProcessor'''), ('''convnextv2''', '''ConvNextImageProcessor'''), ('''cvt''', '''ConvNextImageProcessor'''), ('''data2vec-vision''', '''BeitImageProcessor'''), ('''deformable_detr''', '''DeformableDetrImageProcessor'''), ('''deit''', '''DeiTImageProcessor'''), ('''deta''', '''DetaImageProcessor'''), ('''detr''', '''DetrImageProcessor'''), ('''dinat''', '''ViTImageProcessor'''), ('''donut-swin''', '''DonutImageProcessor'''), ('''dpt''', '''DPTImageProcessor'''), ('''efficientformer''', '''EfficientFormerImageProcessor'''), ('''efficientnet''', '''EfficientNetImageProcessor'''), ('''flava''', '''FlavaImageProcessor'''), ('''focalnet''', '''BitImageProcessor'''), ('''git''', '''CLIPImageProcessor'''), ('''glpn''', '''GLPNImageProcessor'''), ('''groupvit''', '''CLIPImageProcessor'''), ('''imagegpt''', '''ImageGPTImageProcessor'''), ('''instructblip''', '''BlipImageProcessor'''), ('''layoutlmv2''', '''LayoutLMv2ImageProcessor'''), ('''layoutlmv3''', '''LayoutLMv3ImageProcessor'''), ('''levit''', '''LevitImageProcessor'''), ('''mask2former''', '''Mask2FormerImageProcessor'''), ('''maskformer''', '''MaskFormerImageProcessor'''), ('''mgp-str''', '''ViTImageProcessor'''), ('''mobilenet_v1''', '''MobileNetV1ImageProcessor'''), ('''mobilenet_v2''', '''MobileNetV2ImageProcessor'''), ('''mobilevit''', '''MobileViTImageProcessor'''), ('''mobilevit''', '''MobileViTImageProcessor'''), ('''mobilevitv2''', '''MobileViTImageProcessor'''), ('''nat''', '''ViTImageProcessor'''), ('''oneformer''', '''OneFormerImageProcessor'''), ('''owlvit''', '''OwlViTImageProcessor'''), ('''perceiver''', '''PerceiverImageProcessor'''), ('''pix2struct''', '''Pix2StructImageProcessor'''), ('''poolformer''', '''PoolFormerImageProcessor'''), ('''regnet''', '''ConvNextImageProcessor'''), ('''resnet''', '''ConvNextImageProcessor'''), ('''sam''', '''SamImageProcessor'''), ('''segformer''', '''SegformerImageProcessor'''), ('''swiftformer''', '''ViTImageProcessor'''), ('''swin''', '''ViTImageProcessor'''), ('''swin2sr''', '''Swin2SRImageProcessor'''), ('''swinv2''', '''ViTImageProcessor'''), ('''table-transformer''', '''DetrImageProcessor'''), ('''timesformer''', '''VideoMAEImageProcessor'''), ('''tvlt''', '''TvltImageProcessor'''), ('''upernet''', '''SegformerImageProcessor'''), ('''van''', '''ConvNextImageProcessor'''), ('''videomae''', '''VideoMAEImageProcessor'''), ('''vilt''', '''ViltImageProcessor'''), ('''vit''', '''ViTImageProcessor'''), ('''vit_hybrid''', '''ViTHybridImageProcessor'''), ('''vit_mae''', '''ViTImageProcessor'''), ('''vit_msn''', '''ViTImageProcessor'''), ('''xclip''', '''CLIPImageProcessor'''), ('''yolos''', '''YolosImageProcessor'''), ] ) lowerCAmelCase__ : List[Any] = _LazyAutoMapping(CONFIG_MAPPING_NAMES, IMAGE_PROCESSOR_MAPPING_NAMES) def UpperCamelCase__ ( A__ ) -> int: for module_name, extractors in IMAGE_PROCESSOR_MAPPING_NAMES.items(): if class_name in extractors: snake_case__ : Tuple = model_type_to_module_name(A__ ) snake_case__ : Any = importlib.import_module(F""".{module_name}""" , 'transformers.models' ) try: return getattr(A__ , A__ ) except AttributeError: continue for _, extractor in IMAGE_PROCESSOR_MAPPING._extra_content.items(): if getattr(A__ , '__name__' , A__ ) == class_name: return extractor # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main # init and we return the proper dummy to get an appropriate error message. snake_case__ : List[str] = importlib.import_module('transformers' ) if hasattr(A__ , A__ ): return getattr(A__ , A__ ) return None def UpperCamelCase__ ( A__ , A__ = None , A__ = False , A__ = False , A__ = None , A__ = None , A__ = None , A__ = False , **A__ , ) -> List[Any]: snake_case__ : int = get_file_from_repo( A__ , A__ , cache_dir=A__ , force_download=A__ , resume_download=A__ , proxies=A__ , use_auth_token=A__ , revision=A__ , local_files_only=A__ , ) if resolved_config_file is None: logger.info( 'Could not locate the image processor configuration file, will try to use the model config instead.' ) return {} with open(A__ , encoding='utf-8' ) as reader: return json.load(A__ ) class __snake_case : def __init__( self ) -> Tuple: '''simple docstring''' raise EnvironmentError( 'AutoImageProcessor is designed to be instantiated ' 'using the `AutoImageProcessor.from_pretrained(pretrained_model_name_or_path)` method.' ) @classmethod @replace_list_option_in_docstrings(__UpperCamelCase ) def __a ( cls , __UpperCamelCase , **__UpperCamelCase ) -> Optional[Any]: '''simple docstring''' snake_case__ : List[str] = kwargs.pop('config' , __UpperCamelCase ) snake_case__ : List[Any] = kwargs.pop('trust_remote_code' , __UpperCamelCase ) snake_case__ : Union[str, Any] = True snake_case__ , snake_case__ : Any = ImageProcessingMixin.get_image_processor_dict(__UpperCamelCase , **__UpperCamelCase ) snake_case__ : str = config_dict.get('image_processor_type' , __UpperCamelCase ) snake_case__ : Any = None if "AutoImageProcessor" in config_dict.get('auto_map' , {} ): snake_case__ : Dict = config_dict['auto_map']['AutoImageProcessor'] # If we still don't have the image processor class, check if we're loading from a previous feature extractor config # and if so, infer the image processor class from there. if image_processor_class is None and image_processor_auto_map is None: snake_case__ : List[str] = config_dict.pop('feature_extractor_type' , __UpperCamelCase ) if feature_extractor_class is not None: logger.warning( 'Could not find image processor class in the image processor config or the model config. Loading' ' based on pattern matching with the model\'s feature extractor configuration.' ) snake_case__ : int = feature_extractor_class.replace('FeatureExtractor' , 'ImageProcessor' ) if "AutoFeatureExtractor" in config_dict.get('auto_map' , {} ): snake_case__ : List[str] = config_dict['auto_map']['AutoFeatureExtractor'] snake_case__ : Union[str, Any] = feature_extractor_auto_map.replace('FeatureExtractor' , 'ImageProcessor' ) logger.warning( 'Could not find image processor auto map in the image processor config or the model config.' ' Loading based on pattern matching with the model\'s feature extractor configuration.' ) # If we don't find the image processor class in the image processor config, let's try the model config. if image_processor_class is None and image_processor_auto_map is None: if not isinstance(__UpperCamelCase , __UpperCamelCase ): snake_case__ : int = AutoConfig.from_pretrained(__UpperCamelCase , **__UpperCamelCase ) # It could be in `config.image_processor_type`` snake_case__ : Dict = getattr(__UpperCamelCase , 'image_processor_type' , __UpperCamelCase ) if hasattr(__UpperCamelCase , 'auto_map' ) and "AutoImageProcessor" in config.auto_map: snake_case__ : int = config.auto_map['AutoImageProcessor'] if image_processor_class is not None: snake_case__ : Dict = image_processor_class_from_name(__UpperCamelCase ) snake_case__ : Any = image_processor_auto_map is not None snake_case__ : Union[str, Any] = image_processor_class is not None or type(__UpperCamelCase ) in IMAGE_PROCESSOR_MAPPING snake_case__ : int = resolve_trust_remote_code( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) if has_remote_code and trust_remote_code: snake_case__ : Union[str, Any] = get_class_from_dynamic_module( __UpperCamelCase , __UpperCamelCase , **__UpperCamelCase ) snake_case__ : Any = kwargs.pop('code_revision' , __UpperCamelCase ) if os.path.isdir(__UpperCamelCase ): image_processor_class.register_for_auto_class() return image_processor_class.from_dict(__UpperCamelCase , **__UpperCamelCase ) elif image_processor_class is not None: return image_processor_class.from_dict(__UpperCamelCase , **__UpperCamelCase ) # Last try: we use the IMAGE_PROCESSOR_MAPPING. elif type(__UpperCamelCase ) in IMAGE_PROCESSOR_MAPPING: snake_case__ : Tuple = IMAGE_PROCESSOR_MAPPING[type(__UpperCamelCase )] return image_processor_class.from_dict(__UpperCamelCase , **__UpperCamelCase ) raise ValueError( F"""Unrecognized image processor in {pretrained_model_name_or_path}. Should have a """ F"""`image_processor_type` key in its {IMAGE_PROCESSOR_NAME} of {CONFIG_NAME}, or one of the following """ F"""`model_type` keys in its {CONFIG_NAME}: {', '.join(c for c in IMAGE_PROCESSOR_MAPPING_NAMES.keys() )}""" ) @staticmethod def __a ( __UpperCamelCase , __UpperCamelCase ) -> str: '''simple docstring''' IMAGE_PROCESSOR_MAPPING.register(__UpperCamelCase , __UpperCamelCase )
699
from __future__ import annotations from collections.abc import Iterator from typing import Generic, TypeVar lowerCAmelCase__ : Optional[int] = TypeVar('''T''') class __snake_case ( Generic[T] ): def __init__( self , __UpperCamelCase ) -> Any: '''simple docstring''' snake_case__ : Optional[int] = data snake_case__ : Node[T] | None = None def __str__( self ) -> str: '''simple docstring''' return F"""{self.data}""" class __snake_case ( Generic[T] ): def __init__( self ) -> None: '''simple docstring''' snake_case__ : Node[T] | None = None def __iter__( self ) -> Iterator[T]: '''simple docstring''' snake_case__ : str = self.top while node: yield node.data snake_case__ : Dict = node.next def __str__( self ) -> str: '''simple docstring''' return "->".join([str(__UpperCamelCase ) for item in self] ) def __len__( self ) -> int: '''simple docstring''' return len(tuple(iter(self ) ) ) def __a ( self ) -> bool: '''simple docstring''' return self.top is None def __a ( self , __UpperCamelCase ) -> None: '''simple docstring''' snake_case__ : str = Node(__UpperCamelCase ) if not self.is_empty(): snake_case__ : List[str] = self.top snake_case__ : Tuple = node def __a ( self ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError('pop from empty stack' ) assert isinstance(self.top , __UpperCamelCase ) snake_case__ : List[str] = self.top snake_case__ : Union[str, Any] = self.top.next return pop_node.data def __a ( self ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError('peek from empty stack' ) assert self.top is not None return self.top.data def __a ( self ) -> None: '''simple docstring''' snake_case__ : Any = None if __name__ == "__main__": from doctest import testmod testmod()
699
1
import torch import torch.nn as nn from transformers.modeling_utils import ModuleUtilsMixin from transformers.models.ta.modeling_ta import TaBlock, TaConfig, TaLayerNorm from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin class __snake_case ( _lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ): @register_to_config def __init__( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = False , ) -> Union[str, Any]: '''simple docstring''' super().__init__() snake_case__ : Optional[int] = nn.Embedding(__UpperCamelCase , __UpperCamelCase ) snake_case__ : Dict = nn.Embedding(__UpperCamelCase , __UpperCamelCase ) snake_case__ : Tuple = False snake_case__ : Dict = nn.Dropout(p=__UpperCamelCase ) snake_case__ : int = TaConfig( vocab_size=__UpperCamelCase , d_model=__UpperCamelCase , num_heads=__UpperCamelCase , d_kv=__UpperCamelCase , d_ff=__UpperCamelCase , dropout_rate=__UpperCamelCase , feed_forward_proj=__UpperCamelCase , is_decoder=__UpperCamelCase , is_encoder_decoder=__UpperCamelCase , ) snake_case__ : int = nn.ModuleList() for lyr_num in range(__UpperCamelCase ): snake_case__ : Any = TaBlock(__UpperCamelCase ) self.encoders.append(__UpperCamelCase ) snake_case__ : Union[str, Any] = TaLayerNorm(__UpperCamelCase ) snake_case__ : List[Any] = nn.Dropout(p=__UpperCamelCase ) def __a ( self , __UpperCamelCase , __UpperCamelCase ) -> int: '''simple docstring''' snake_case__ : Optional[int] = self.token_embedder(__UpperCamelCase ) snake_case__ : str = encoder_input_tokens.shape[1] snake_case__ : Optional[Any] = torch.arange(__UpperCamelCase , device=encoder_input_tokens.device ) x += self.position_encoding(__UpperCamelCase ) snake_case__ : Optional[Any] = self.dropout_pre(__UpperCamelCase ) # inverted the attention mask snake_case__ : List[str] = encoder_input_tokens.size() snake_case__ : int = self.get_extended_attention_mask(__UpperCamelCase , __UpperCamelCase ) for lyr in self.encoders: snake_case__ : int = lyr(__UpperCamelCase , __UpperCamelCase )[0] snake_case__ : Union[str, Any] = self.layer_norm(__UpperCamelCase ) return self.dropout_post(__UpperCamelCase ), encoder_inputs_mask
699
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCAmelCase__ : Dict = logging.get_logger(__name__) lowerCAmelCase__ : int = { '''sail/poolformer_s12''': '''https://huggingface.co/sail/poolformer_s12/resolve/main/config.json''', # See all PoolFormer models at https://huggingface.co/models?filter=poolformer } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = """poolformer""" def __init__( self , __UpperCamelCase=3 , __UpperCamelCase=16 , __UpperCamelCase=16 , __UpperCamelCase=3 , __UpperCamelCase=4.0 , __UpperCamelCase=[2, 2, 6, 2] , __UpperCamelCase=[64, 128, 320, 512] , __UpperCamelCase=[7, 3, 3, 3] , __UpperCamelCase=[4, 2, 2, 2] , __UpperCamelCase=[2, 1, 1, 1] , __UpperCamelCase=4 , __UpperCamelCase=0.0 , __UpperCamelCase="gelu" , __UpperCamelCase=True , __UpperCamelCase=1E-5 , __UpperCamelCase=0.0_2 , **__UpperCamelCase , ) -> Any: '''simple docstring''' snake_case__ : List[str] = num_channels snake_case__ : Dict = patch_size snake_case__ : Optional[int] = stride snake_case__ : str = padding snake_case__ : List[str] = pool_size snake_case__ : List[Any] = hidden_sizes snake_case__ : List[Any] = mlp_ratio snake_case__ : Union[str, Any] = depths snake_case__ : Dict = patch_sizes snake_case__ : Dict = strides snake_case__ : Dict = num_encoder_blocks snake_case__ : Union[str, Any] = drop_path_rate snake_case__ : List[str] = hidden_act snake_case__ : Optional[Any] = use_layer_scale snake_case__ : int = layer_scale_init_value snake_case__ : Dict = initializer_range super().__init__(**__UpperCamelCase ) class __snake_case ( _lowerCamelCase ): __lowerCamelCase = version.parse("""1.11""" ) @property def __a ( self ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def __a ( self ) -> float: '''simple docstring''' return 2E-3
699
1
from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer from ...configuration_utils import PretrainedConfig from ...file_utils import TensorType, is_torch_available from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging lowerCAmelCase__ : List[Any] = logging.get_logger(__name__) lowerCAmelCase__ : Union[str, Any] = { '''facebook/blenderbot_small-90M''': '''https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/config.json''', # See all BlenderbotSmall models at https://huggingface.co/models?filter=blenderbot_small } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = """blenderbot-small""" __lowerCamelCase = ["""past_key_values"""] __lowerCamelCase = {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""} def __init__( self , __UpperCamelCase=50265 , __UpperCamelCase=512 , __UpperCamelCase=8 , __UpperCamelCase=2048 , __UpperCamelCase=16 , __UpperCamelCase=8 , __UpperCamelCase=2048 , __UpperCamelCase=16 , __UpperCamelCase=0.0 , __UpperCamelCase=0.0 , __UpperCamelCase=True , __UpperCamelCase=True , __UpperCamelCase="gelu" , __UpperCamelCase=512 , __UpperCamelCase=0.1 , __UpperCamelCase=0.0 , __UpperCamelCase=0.0 , __UpperCamelCase=0.0_2 , __UpperCamelCase=1 , __UpperCamelCase=False , __UpperCamelCase=0 , __UpperCamelCase=1 , __UpperCamelCase=2 , __UpperCamelCase=2 , **__UpperCamelCase , ) -> Dict: '''simple docstring''' snake_case__ : Tuple = vocab_size snake_case__ : Tuple = max_position_embeddings snake_case__ : int = d_model snake_case__ : Optional[Any] = encoder_ffn_dim snake_case__ : Any = encoder_layers snake_case__ : List[Any] = encoder_attention_heads snake_case__ : Union[str, Any] = decoder_ffn_dim snake_case__ : List[str] = decoder_layers snake_case__ : str = decoder_attention_heads snake_case__ : Dict = dropout snake_case__ : str = attention_dropout snake_case__ : Dict = activation_dropout snake_case__ : Optional[int] = activation_function snake_case__ : Union[str, Any] = init_std snake_case__ : str = encoder_layerdrop snake_case__ : Optional[int] = decoder_layerdrop snake_case__ : Dict = use_cache snake_case__ : List[Any] = encoder_layers snake_case__ : Any = scale_embedding # scale factor will be sqrt(d_model) if True super().__init__( pad_token_id=__UpperCamelCase , bos_token_id=__UpperCamelCase , eos_token_id=__UpperCamelCase , is_encoder_decoder=__UpperCamelCase , decoder_start_token_id=__UpperCamelCase , forced_eos_token_id=__UpperCamelCase , **__UpperCamelCase , ) class __snake_case ( _lowerCamelCase ): @property def __a ( self ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: snake_case__ : Optional[int] = OrderedDict( [ ('input_ids', {0: 'batch', 1: 'encoder_sequence'}), ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}), ] ) if self.use_past: snake_case__ : int = {0: 'batch'} snake_case__ : Optional[Any] = {0: 'batch', 1: 'past_decoder_sequence + sequence'} else: snake_case__ : Tuple = {0: 'batch', 1: 'decoder_sequence'} snake_case__ : List[Any] = {0: 'batch', 1: 'decoder_sequence'} if self.use_past: self.fill_with_past_key_values_(__UpperCamelCase , direction='inputs' ) elif self.task == "causal-lm": # TODO: figure this case out. snake_case__ : Tuple = OrderedDict( [ ('input_ids', {0: 'batch', 1: 'encoder_sequence'}), ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}), ] ) if self.use_past: snake_case__ , snake_case__ : Optional[int] = self.num_layers for i in range(__UpperCamelCase ): snake_case__ : Optional[Any] = {0: 'batch', 2: 'past_sequence + sequence'} snake_case__ : Dict = {0: 'batch', 2: 'past_sequence + sequence'} else: snake_case__ : List[str] = 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 __a ( self ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: snake_case__ : Any = super().outputs else: snake_case__ : Union[str, Any] = super(__UpperCamelCase , self ).outputs if self.use_past: snake_case__ , snake_case__ : Tuple = self.num_layers for i in range(__UpperCamelCase ): snake_case__ : Optional[Any] = {0: 'batch', 2: 'past_sequence + sequence'} snake_case__ : Dict = {0: 'batch', 2: 'past_sequence + sequence'} return common_outputs def __a ( self , __UpperCamelCase , __UpperCamelCase = -1 , __UpperCamelCase = -1 , __UpperCamelCase = False , __UpperCamelCase = None , ) -> Mapping[str, Any]: '''simple docstring''' snake_case__ : Any = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) # Generate decoder inputs snake_case__ : Optional[int] = seq_length if not self.use_past else 1 snake_case__ : Union[str, Any] = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) snake_case__ : Union[str, Any] = {F"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()} snake_case__ : str = dict(**__UpperCamelCase , **__UpperCamelCase ) if self.use_past: if not is_torch_available(): raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.' ) else: import torch snake_case__ , snake_case__ : Optional[int] = common_inputs['input_ids'].shape snake_case__ : str = common_inputs['decoder_input_ids'].shape[1] snake_case__ , snake_case__ : str = self.num_attention_heads snake_case__ : List[str] = ( batch, num_encoder_attention_heads, encoder_seq_length, self._config.hidden_size // num_encoder_attention_heads, ) snake_case__ : List[Any] = decoder_seq_length + 3 snake_case__ : List[str] = ( batch, num_decoder_attention_heads, decoder_past_length, self._config.hidden_size // num_decoder_attention_heads, ) snake_case__ : str = torch.cat( [common_inputs['decoder_attention_mask'], torch.ones(__UpperCamelCase , __UpperCamelCase )] , dim=1 ) snake_case__ : Union[str, Any] = [] # If the number of encoder and decoder layers are present in the model configuration, both are considered snake_case__ , snake_case__ : List[str] = self.num_layers snake_case__ : List[str] = min(__UpperCamelCase , __UpperCamelCase ) snake_case__ : Any = max(__UpperCamelCase , __UpperCamelCase ) - min_num_layers snake_case__ : str = 'encoder' if num_encoder_layers > num_decoder_layers else 'decoder' for _ in range(__UpperCamelCase ): common_inputs["past_key_values"].append( ( torch.zeros(__UpperCamelCase ), torch.zeros(__UpperCamelCase ), torch.zeros(__UpperCamelCase ), torch.zeros(__UpperCamelCase ), ) ) # TODO: test this. snake_case__ : Any = encoder_shape if remaining_side_name == 'encoder' else decoder_shape for _ in range(__UpperCamelCase , __UpperCamelCase ): common_inputs["past_key_values"].append((torch.zeros(__UpperCamelCase ), torch.zeros(__UpperCamelCase )) ) return common_inputs def __a ( self , __UpperCamelCase , __UpperCamelCase = -1 , __UpperCamelCase = -1 , __UpperCamelCase = False , __UpperCamelCase = None , ) -> Mapping[str, Any]: '''simple docstring''' snake_case__ : Tuple = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) if self.use_past: if not is_torch_available(): raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.' ) else: import torch snake_case__ , snake_case__ : List[Any] = common_inputs['input_ids'].shape # Not using the same length for past_key_values snake_case__ : Optional[Any] = seqlen + 2 snake_case__ , snake_case__ : Optional[Any] = self.num_layers snake_case__ , snake_case__ : Any = self.num_attention_heads snake_case__ : str = ( batch, num_encoder_attention_heads, past_key_values_length, self._config.hidden_size // num_encoder_attention_heads, ) snake_case__ : Optional[int] = common_inputs['attention_mask'].dtype snake_case__ : Optional[int] = torch.cat( [common_inputs['attention_mask'], torch.ones(__UpperCamelCase , __UpperCamelCase , dtype=__UpperCamelCase )] , dim=1 ) snake_case__ : Dict = [ (torch.zeros(__UpperCamelCase ), torch.zeros(__UpperCamelCase )) for _ in range(__UpperCamelCase ) ] return common_inputs def __a ( self , __UpperCamelCase , __UpperCamelCase = -1 , __UpperCamelCase = -1 , __UpperCamelCase = False , __UpperCamelCase = None , ) -> Mapping[str, Any]: '''simple docstring''' snake_case__ : Optional[int] = compute_effective_axis_dimension( __UpperCamelCase , 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 snake_case__ : List[Any] = tokenizer.num_special_tokens_to_add(__UpperCamelCase ) snake_case__ : Optional[int] = compute_effective_axis_dimension( __UpperCamelCase , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=__UpperCamelCase ) # Generate dummy inputs according to compute batch and sequence snake_case__ : List[Any] = [' '.join([tokenizer.unk_token] ) * seq_length] * batch_size snake_case__ : Any = dict(tokenizer(__UpperCamelCase , return_tensors=__UpperCamelCase ) ) return common_inputs def __a ( self , __UpperCamelCase , __UpperCamelCase = -1 , __UpperCamelCase = -1 , __UpperCamelCase = False , __UpperCamelCase = None , ) -> Mapping[str, Any]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: snake_case__ : Dict = self._generate_dummy_inputs_for_default_and_seqaseq_lm( __UpperCamelCase , batch_size=__UpperCamelCase , seq_length=__UpperCamelCase , is_pair=__UpperCamelCase , framework=__UpperCamelCase ) elif self.task == "causal-lm": snake_case__ : int = self._generate_dummy_inputs_for_causal_lm( __UpperCamelCase , batch_size=__UpperCamelCase , seq_length=__UpperCamelCase , is_pair=__UpperCamelCase , framework=__UpperCamelCase ) else: snake_case__ : Optional[int] = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( __UpperCamelCase , batch_size=__UpperCamelCase , seq_length=__UpperCamelCase , is_pair=__UpperCamelCase , framework=__UpperCamelCase ) return common_inputs def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[Any]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: snake_case__ : Union[str, Any] = super()._flatten_past_key_values_(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) else: snake_case__ : Any = super(__UpperCamelCase , self )._flatten_past_key_values_( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )
699
import numpy as np import qiskit def UpperCamelCase__ ( A__ = 8 , A__ = None ) -> str: snake_case__ : Optional[int] = np.random.default_rng(seed=A__ ) # Roughly 25% of the qubits will contribute to the key. # So we take more than we need. snake_case__ : Tuple = 6 * key_len # Measurement basis for Alice's qubits. snake_case__ : Tuple = rng.integers(2 , size=A__ ) # The set of states Alice will prepare. snake_case__ : List[str] = rng.integers(2 , size=A__ ) # Measurement basis for Bob's qubits. snake_case__ : List[Any] = rng.integers(2 , size=A__ ) # Quantum Circuit to simulate BB84 snake_case__ : Any = qiskit.QuantumCircuit(A__ , name='BB84' ) # Alice prepares her qubits according to rules above. for index, _ in enumerate(A__ ): if alice_state[index] == 1: bbaa_circ.x(A__ ) if alice_basis[index] == 1: bbaa_circ.h(A__ ) bbaa_circ.barrier() # Bob measures the received qubits according to rules above. for index, _ in enumerate(A__ ): if bob_basis[index] == 1: bbaa_circ.h(A__ ) bbaa_circ.barrier() bbaa_circ.measure_all() # Simulate the quantum circuit. snake_case__ : List[str] = qiskit.Aer.get_backend('aer_simulator' ) # We only need to run one shot because the key is unique. # Multiple shots will produce the same key. snake_case__ : Optional[Any] = qiskit.execute(A__ , A__ , shots=1 , seed_simulator=A__ ) # Returns the result of measurement. snake_case__ : Union[str, Any] = job.result().get_counts(A__ ).most_frequent() # Extracting the generated key from the simulation results. # Only keep measurement results where Alice and Bob chose the same basis. snake_case__ : Optional[Any] = ''.join( [ result_bit for alice_basis_bit, bob_basis_bit, result_bit in zip( A__ , A__ , A__ ) if alice_basis_bit == bob_basis_bit ] ) # Get final key. Pad with 0 if too short, otherwise truncate. snake_case__ : Tuple = gen_key[:key_len] if len(A__ ) >= key_len else gen_key.ljust(A__ , '0' ) return key if __name__ == "__main__": print(F'''The generated key is : {bbaa(8, seed=0)}''') from doctest import testmod testmod()
699
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase__ : Optional[int] = {'''configuration_fnet''': ['''FNET_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''FNetConfig''']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : List[Any] = ['''FNetTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Union[str, Any] = ['''FNetTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : List[str] = [ '''FNET_PRETRAINED_MODEL_ARCHIVE_LIST''', '''FNetForMaskedLM''', '''FNetForMultipleChoice''', '''FNetForNextSentencePrediction''', '''FNetForPreTraining''', '''FNetForQuestionAnswering''', '''FNetForSequenceClassification''', '''FNetForTokenClassification''', '''FNetLayer''', '''FNetModel''', '''FNetPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_fnet import FNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FNetConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_fnet import FNetTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_fnet_fast import FNetTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_fnet import ( FNET_PRETRAINED_MODEL_ARCHIVE_LIST, FNetForMaskedLM, FNetForMultipleChoice, FNetForNextSentencePrediction, FNetForPreTraining, FNetForQuestionAnswering, FNetForSequenceClassification, FNetForTokenClassification, FNetLayer, FNetModel, FNetPreTrainedModel, ) else: import sys lowerCAmelCase__ : int = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
699
def UpperCamelCase__ ( A__ , A__ , A__ ) -> int: if exponent == 1: return base if exponent % 2 == 0: snake_case__ : Dict = _modexpt(A__ , exponent // 2 , A__ ) % modulo_value return (x * x) % modulo_value else: return (base * _modexpt(A__ , exponent - 1 , A__ )) % modulo_value def UpperCamelCase__ ( A__ = 1777 , A__ = 1855 , A__ = 8 ) -> int: snake_case__ : Tuple = base for _ in range(1 , A__ ): snake_case__ : Any = _modexpt(A__ , A__ , 10**digits ) return result if __name__ == "__main__": print(F'''{solution() = }''')
699
1
from collections import namedtuple lowerCAmelCase__ : Union[str, Any] = namedtuple('''from_to''', '''from_ to''') lowerCAmelCase__ : Tuple = { '''cubicmeter''': from_to(1, 1), '''litre''': from_to(0.0_01, 10_00), '''kilolitre''': from_to(1, 1), '''gallon''': from_to(0.0_04_54, 2_64.1_72), '''cubicyard''': from_to(0.7_64_55, 1.3_07_95), '''cubicfoot''': from_to(0.0_28, 35.31_47), '''cup''': from_to(0.0_00_23_65_88, 42_26.75), } def UpperCamelCase__ ( A__ , A__ , A__ ) -> float: if from_type not in METRIC_CONVERSION: raise ValueError( F"""Invalid 'from_type' value: {from_type!r} Supported values are:\n""" + ', '.join(A__ ) ) if to_type not in METRIC_CONVERSION: raise ValueError( F"""Invalid 'to_type' value: {to_type!r}. Supported values are:\n""" + ', '.join(A__ ) ) return value * METRIC_CONVERSION[from_type].from_ * METRIC_CONVERSION[to_type].to if __name__ == "__main__": import doctest doctest.testmod()
699
# tests directory-specific settings - this file is run automatically # by pytest before any tests are run import sys import warnings from os.path import abspath, dirname, join # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. lowerCAmelCase__ : Tuple = abspath(join(dirname(dirname(__file__)), '''src''')) sys.path.insert(1, git_repo_path) # silence FutureWarning warnings in tests since often we can't act on them until # they become normal warnings - i.e. the tests still need to test the current functionality warnings.simplefilter(action='''ignore''', category=FutureWarning) def UpperCamelCase__ ( A__ ) -> Optional[Any]: from diffusers.utils.testing_utils import pytest_addoption_shared pytest_addoption_shared(A__ ) def UpperCamelCase__ ( A__ ) -> Optional[Any]: from diffusers.utils.testing_utils import pytest_terminal_summary_main snake_case__ : Union[str, Any] = terminalreporter.config.getoption('--make-reports' ) if make_reports: pytest_terminal_summary_main(A__ , id=A__ )
699
1
import argparse import pytorch_lightning as pl import torch from torch import nn from transformers import LongformerForQuestionAnswering, LongformerModel class __snake_case ( pl.LightningModule ): def __init__( self , __UpperCamelCase ) -> List[Any]: '''simple docstring''' super().__init__() snake_case__ : str = model snake_case__ : str = 2 snake_case__ : Optional[Any] = nn.Linear(self.model.config.hidden_size , self.num_labels ) def __a ( self ) -> Dict: '''simple docstring''' pass def UpperCamelCase__ ( A__ , A__ , A__ ) -> Optional[int]: # load longformer model from model identifier snake_case__ : str = LongformerModel.from_pretrained(A__ ) snake_case__ : Optional[int] = LightningModel(A__ ) snake_case__ : Optional[Any] = torch.load(A__ , map_location=torch.device('cpu' ) ) lightning_model.load_state_dict(ckpt['state_dict'] ) # init longformer question answering model snake_case__ : Dict = LongformerForQuestionAnswering.from_pretrained(A__ ) # transfer weights longformer_for_qa.longformer.load_state_dict(lightning_model.model.state_dict() ) longformer_for_qa.qa_outputs.load_state_dict(lightning_model.qa_outputs.state_dict() ) longformer_for_qa.eval() # save model longformer_for_qa.save_pretrained(A__ ) print(F"""Conversion successful. Model saved under {pytorch_dump_folder_path}""" ) if __name__ == "__main__": lowerCAmelCase__ : str = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--longformer_model''', default=None, type=str, required=True, help='''model identifier of longformer. Should be either `longformer-base-4096` or `longformer-large-4096`.''', ) parser.add_argument( '''--longformer_question_answering_ckpt_path''', default=None, type=str, required=True, help='''Path the official PyTorch Lightning Checkpoint.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) lowerCAmelCase__ : str = parser.parse_args() convert_longformer_qa_checkpoint_to_pytorch( args.longformer_model, args.longformer_question_answering_ckpt_path, args.pytorch_dump_folder_path )
699
def UpperCamelCase__ ( A__ ) -> list[int]: if length <= 0 or not isinstance(A__ , A__ ): raise ValueError('Length must be a positive integer.' ) return [n * (2 * n - 1) for n in range(A__ )] if __name__ == "__main__": print(hexagonal_numbers(length=5)) print(hexagonal_numbers(length=10))
699
1
import itertools from dataclasses import dataclass from typing import Optional import pandas as pd import pyarrow as pa import datasets from datasets.table import table_cast @dataclass class __snake_case ( datasets.BuilderConfig ): __lowerCamelCase = None class __snake_case ( datasets.ArrowBasedBuilder ): __lowerCamelCase = PandasConfig def __a ( self ) -> List[Any]: '''simple docstring''' return datasets.DatasetInfo(features=self.config.features ) def __a ( self , __UpperCamelCase ) -> List[str]: '''simple docstring''' if not self.config.data_files: raise ValueError(F"""At least one data file must be specified, but got data_files={self.config.data_files}""" ) snake_case__ : str = dl_manager.download_and_extract(self.config.data_files ) if isinstance(__UpperCamelCase , (str, list, tuple) ): snake_case__ : Optional[Any] = data_files if isinstance(__UpperCamelCase , __UpperCamelCase ): snake_case__ : Union[str, Any] = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive snake_case__ : Optional[int] = [dl_manager.iter_files(__UpperCamelCase ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'files': files} )] snake_case__ : Optional[Any] = [] for split_name, files in data_files.items(): if isinstance(__UpperCamelCase , __UpperCamelCase ): snake_case__ : Union[str, Any] = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive snake_case__ : Optional[Any] = [dl_manager.iter_files(__UpperCamelCase ) for file in files] splits.append(datasets.SplitGenerator(name=__UpperCamelCase , gen_kwargs={'files': files} ) ) return splits def __a ( self , __UpperCamelCase ) -> pa.Table: '''simple docstring''' if self.config.features is not None: # more expensive cast to support nested features with keys in a different order # allows str <-> int/float or str to Audio for example snake_case__ : Any = table_cast(__UpperCamelCase , self.config.features.arrow_schema ) return pa_table def __a ( self , __UpperCamelCase ) -> Optional[int]: '''simple docstring''' for i, file in enumerate(itertools.chain.from_iterable(__UpperCamelCase ) ): with open(__UpperCamelCase , 'rb' ) as f: snake_case__ : Any = pa.Table.from_pandas(pd.read_pickle(__UpperCamelCase ) ) yield i, self._cast_table(__UpperCamelCase )
699
import argparse import json from collections import OrderedDict from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( ConditionalDetrConfig, ConditionalDetrForObjectDetection, ConditionalDetrForSegmentation, ConditionalDetrImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase__ : Dict = logging.get_logger(__name__) # here we list all keys to be renamed (original name on the left, our name on the right) lowerCAmelCase__ : Optional[Any] = [] for i in range(6): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append( (F'''transformer.encoder.layers.{i}.self_attn.out_proj.weight''', F'''encoder.layers.{i}.self_attn.out_proj.weight''') ) rename_keys.append( (F'''transformer.encoder.layers.{i}.self_attn.out_proj.bias''', F'''encoder.layers.{i}.self_attn.out_proj.bias''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.linear1.weight''', F'''encoder.layers.{i}.fc1.weight''')) rename_keys.append((F'''transformer.encoder.layers.{i}.linear1.bias''', F'''encoder.layers.{i}.fc1.bias''')) rename_keys.append((F'''transformer.encoder.layers.{i}.linear2.weight''', F'''encoder.layers.{i}.fc2.weight''')) rename_keys.append((F'''transformer.encoder.layers.{i}.linear2.bias''', F'''encoder.layers.{i}.fc2.bias''')) rename_keys.append( (F'''transformer.encoder.layers.{i}.norm1.weight''', F'''encoder.layers.{i}.self_attn_layer_norm.weight''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.norm1.bias''', F'''encoder.layers.{i}.self_attn_layer_norm.bias''')) rename_keys.append((F'''transformer.encoder.layers.{i}.norm2.weight''', F'''encoder.layers.{i}.final_layer_norm.weight''')) rename_keys.append((F'''transformer.encoder.layers.{i}.norm2.bias''', F'''encoder.layers.{i}.final_layer_norm.bias''')) # decoder layers: 2 times output projection, 2 feedforward neural networks and 3 layernorms rename_keys.append( (F'''transformer.decoder.layers.{i}.self_attn.out_proj.weight''', F'''decoder.layers.{i}.self_attn.out_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.self_attn.out_proj.bias''', F'''decoder.layers.{i}.self_attn.out_proj.bias''') ) rename_keys.append( ( F'''transformer.decoder.layers.{i}.cross_attn.out_proj.weight''', F'''decoder.layers.{i}.encoder_attn.out_proj.weight''', ) ) rename_keys.append( ( F'''transformer.decoder.layers.{i}.cross_attn.out_proj.bias''', F'''decoder.layers.{i}.encoder_attn.out_proj.bias''', ) ) rename_keys.append((F'''transformer.decoder.layers.{i}.linear1.weight''', F'''decoder.layers.{i}.fc1.weight''')) rename_keys.append((F'''transformer.decoder.layers.{i}.linear1.bias''', F'''decoder.layers.{i}.fc1.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.linear2.weight''', F'''decoder.layers.{i}.fc2.weight''')) rename_keys.append((F'''transformer.decoder.layers.{i}.linear2.bias''', F'''decoder.layers.{i}.fc2.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.norm1.weight''', F'''decoder.layers.{i}.self_attn_layer_norm.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.norm1.bias''', F'''decoder.layers.{i}.self_attn_layer_norm.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.norm2.weight''', F'''decoder.layers.{i}.encoder_attn_layer_norm.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.norm2.bias''', F'''decoder.layers.{i}.encoder_attn_layer_norm.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.norm3.weight''', F'''decoder.layers.{i}.final_layer_norm.weight''')) rename_keys.append((F'''transformer.decoder.layers.{i}.norm3.bias''', F'''decoder.layers.{i}.final_layer_norm.bias''')) # q, k, v projections in self/cross-attention in decoder for conditional DETR rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_qcontent_proj.weight''', F'''decoder.layers.{i}.sa_qcontent_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_kcontent_proj.weight''', F'''decoder.layers.{i}.sa_kcontent_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_qpos_proj.weight''', F'''decoder.layers.{i}.sa_qpos_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_kpos_proj.weight''', F'''decoder.layers.{i}.sa_kpos_proj.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_v_proj.weight''', F'''decoder.layers.{i}.sa_v_proj.weight''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qcontent_proj.weight''', F'''decoder.layers.{i}.ca_qcontent_proj.weight''') ) # rename_keys.append((f"transformer.decoder.layers.{i}.ca_qpos_proj.weight", f"decoder.layers.{i}.ca_qpos_proj.weight")) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_kcontent_proj.weight''', F'''decoder.layers.{i}.ca_kcontent_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_kpos_proj.weight''', F'''decoder.layers.{i}.ca_kpos_proj.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.ca_v_proj.weight''', F'''decoder.layers.{i}.ca_v_proj.weight''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qpos_sine_proj.weight''', F'''decoder.layers.{i}.ca_qpos_sine_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_qcontent_proj.bias''', F'''decoder.layers.{i}.sa_qcontent_proj.bias''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_kcontent_proj.bias''', F'''decoder.layers.{i}.sa_kcontent_proj.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_qpos_proj.bias''', F'''decoder.layers.{i}.sa_qpos_proj.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_kpos_proj.bias''', F'''decoder.layers.{i}.sa_kpos_proj.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_v_proj.bias''', F'''decoder.layers.{i}.sa_v_proj.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qcontent_proj.bias''', F'''decoder.layers.{i}.ca_qcontent_proj.bias''') ) # rename_keys.append((f"transformer.decoder.layers.{i}.ca_qpos_proj.bias", f"decoder.layers.{i}.ca_qpos_proj.bias")) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_kcontent_proj.bias''', F'''decoder.layers.{i}.ca_kcontent_proj.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.ca_kpos_proj.bias''', F'''decoder.layers.{i}.ca_kpos_proj.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.ca_v_proj.bias''', F'''decoder.layers.{i}.ca_v_proj.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qpos_sine_proj.bias''', F'''decoder.layers.{i}.ca_qpos_sine_proj.bias''') ) # convolutional projection + query embeddings + layernorm of decoder + class and bounding box heads # for conditional DETR, also convert reference point head and query scale MLP rename_keys.extend( [ ('''input_proj.weight''', '''input_projection.weight'''), ('''input_proj.bias''', '''input_projection.bias'''), ('''query_embed.weight''', '''query_position_embeddings.weight'''), ('''transformer.decoder.norm.weight''', '''decoder.layernorm.weight'''), ('''transformer.decoder.norm.bias''', '''decoder.layernorm.bias'''), ('''class_embed.weight''', '''class_labels_classifier.weight'''), ('''class_embed.bias''', '''class_labels_classifier.bias'''), ('''bbox_embed.layers.0.weight''', '''bbox_predictor.layers.0.weight'''), ('''bbox_embed.layers.0.bias''', '''bbox_predictor.layers.0.bias'''), ('''bbox_embed.layers.1.weight''', '''bbox_predictor.layers.1.weight'''), ('''bbox_embed.layers.1.bias''', '''bbox_predictor.layers.1.bias'''), ('''bbox_embed.layers.2.weight''', '''bbox_predictor.layers.2.weight'''), ('''bbox_embed.layers.2.bias''', '''bbox_predictor.layers.2.bias'''), ('''transformer.decoder.ref_point_head.layers.0.weight''', '''decoder.ref_point_head.layers.0.weight'''), ('''transformer.decoder.ref_point_head.layers.0.bias''', '''decoder.ref_point_head.layers.0.bias'''), ('''transformer.decoder.ref_point_head.layers.1.weight''', '''decoder.ref_point_head.layers.1.weight'''), ('''transformer.decoder.ref_point_head.layers.1.bias''', '''decoder.ref_point_head.layers.1.bias'''), ('''transformer.decoder.query_scale.layers.0.weight''', '''decoder.query_scale.layers.0.weight'''), ('''transformer.decoder.query_scale.layers.0.bias''', '''decoder.query_scale.layers.0.bias'''), ('''transformer.decoder.query_scale.layers.1.weight''', '''decoder.query_scale.layers.1.weight'''), ('''transformer.decoder.query_scale.layers.1.bias''', '''decoder.query_scale.layers.1.bias'''), ('''transformer.decoder.layers.0.ca_qpos_proj.weight''', '''decoder.layers.0.ca_qpos_proj.weight'''), ('''transformer.decoder.layers.0.ca_qpos_proj.bias''', '''decoder.layers.0.ca_qpos_proj.bias'''), ] ) def UpperCamelCase__ ( A__ , A__ , A__ ) -> List[str]: snake_case__ : int = state_dict.pop(A__ ) snake_case__ : Union[str, Any] = val def UpperCamelCase__ ( A__ ) -> int: snake_case__ : List[Any] = OrderedDict() for key, value in state_dict.items(): if "backbone.0.body" in key: snake_case__ : Any = key.replace('backbone.0.body' , 'backbone.conv_encoder.model' ) snake_case__ : Optional[int] = value else: snake_case__ : Optional[int] = value return new_state_dict def UpperCamelCase__ ( A__ , A__=False ) -> Optional[int]: snake_case__ : Optional[int] = '' if is_panoptic: snake_case__ : Tuple = 'conditional_detr.' # first: transformer encoder for i in range(6 ): # read in weights + bias of input projection layer (in PyTorch's MultiHeadAttention, this is a single matrix + bias) snake_case__ : int = state_dict.pop(F"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_weight""" ) snake_case__ : str = state_dict.pop(F"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict snake_case__ : Union[str, Any] = in_proj_weight[:256, :] snake_case__ : Union[str, Any] = in_proj_bias[:256] snake_case__ : Union[str, Any] = in_proj_weight[256:512, :] snake_case__ : Optional[Any] = in_proj_bias[256:512] snake_case__ : List[str] = in_proj_weight[-256:, :] snake_case__ : Tuple = in_proj_bias[-256:] def UpperCamelCase__ ( ) -> Tuple: snake_case__ : int = 'http://images.cocodataset.org/val2017/000000039769.jpg' snake_case__ : str = Image.open(requests.get(A__ , stream=A__ ).raw ) return im @torch.no_grad() def UpperCamelCase__ ( A__ , A__ ) -> str: snake_case__ : List[Any] = ConditionalDetrConfig() # set backbone and dilation attributes if "resnet101" in model_name: snake_case__ : Any = 'resnet101' if "dc5" in model_name: snake_case__ : Any = True snake_case__ : int = 'panoptic' in model_name if is_panoptic: snake_case__ : str = 250 else: snake_case__ : Union[str, Any] = 91 snake_case__ : Optional[int] = 'huggingface/label-files' snake_case__ : Optional[Any] = 'coco-detection-id2label.json' snake_case__ : str = json.load(open(hf_hub_download(A__ , A__ , repo_type='dataset' ) , 'r' ) ) snake_case__ : List[Any] = {int(A__ ): v for k, v in idalabel.items()} snake_case__ : Any = idalabel snake_case__ : int = {v: k for k, v in idalabel.items()} # load image processor snake_case__ : List[Any] = 'coco_panoptic' if is_panoptic else 'coco_detection' snake_case__ : List[Any] = ConditionalDetrImageProcessor(format=A__ ) # prepare image snake_case__ : List[str] = prepare_img() snake_case__ : Any = image_processor(images=A__ , return_tensors='pt' ) snake_case__ : Dict = encoding['pixel_values'] logger.info(F"""Converting model {model_name}...""" ) # load original model from torch hub snake_case__ : Any = torch.hub.load('DeppMeng/ConditionalDETR' , A__ , pretrained=A__ ).eval() snake_case__ : Tuple = conditional_detr.state_dict() # rename keys for src, dest in rename_keys: if is_panoptic: snake_case__ : List[Any] = 'conditional_detr.' + src rename_key(A__ , A__ , A__ ) snake_case__ : Dict = rename_backbone_keys(A__ ) # query, key and value matrices need special treatment read_in_q_k_v(A__ , is_panoptic=A__ ) # important: we need to prepend a prefix to each of the base model keys as the head models use different attributes for them snake_case__ : Optional[int] = 'conditional_detr.model.' if is_panoptic else 'model.' for key in state_dict.copy().keys(): if is_panoptic: if ( key.startswith('conditional_detr' ) and not key.startswith('class_labels_classifier' ) and not key.startswith('bbox_predictor' ) ): snake_case__ : List[Any] = state_dict.pop(A__ ) snake_case__ : Optional[int] = val elif "class_labels_classifier" in key or "bbox_predictor" in key: snake_case__ : str = state_dict.pop(A__ ) snake_case__ : List[Any] = val elif key.startswith('bbox_attention' ) or key.startswith('mask_head' ): continue else: snake_case__ : Union[str, Any] = state_dict.pop(A__ ) snake_case__ : Dict = val else: if not key.startswith('class_labels_classifier' ) and not key.startswith('bbox_predictor' ): snake_case__ : List[Any] = state_dict.pop(A__ ) snake_case__ : Optional[int] = val # finally, create HuggingFace model and load state dict snake_case__ : Union[str, Any] = ConditionalDetrForSegmentation(A__ ) if is_panoptic else ConditionalDetrForObjectDetection(A__ ) model.load_state_dict(A__ ) model.eval() model.push_to_hub(repo_id=A__ , organization='DepuMeng' , commit_message='Add model' ) # verify our conversion snake_case__ : Tuple = conditional_detr(A__ ) snake_case__ : str = model(A__ ) assert torch.allclose(outputs.logits , original_outputs['pred_logits'] , atol=1e-4 ) assert torch.allclose(outputs.pred_boxes , original_outputs['pred_boxes'] , atol=1e-4 ) if is_panoptic: assert torch.allclose(outputs.pred_masks , original_outputs['pred_masks'] , atol=1e-4 ) # Save model and image processor logger.info(F"""Saving PyTorch model and image processor to {pytorch_dump_folder_path}...""" ) Path(A__ ).mkdir(exist_ok=A__ ) model.save_pretrained(A__ ) image_processor.save_pretrained(A__ ) if __name__ == "__main__": lowerCAmelCase__ : Any = argparse.ArgumentParser() parser.add_argument( '''--model_name''', default='''conditional_detr_resnet50''', type=str, help='''Name of the CONDITIONAL_DETR model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the folder to output PyTorch model.''' ) lowerCAmelCase__ : int = parser.parse_args() convert_conditional_detr_checkpoint(args.model_name, args.pytorch_dump_folder_path)
699
1
import torch from transformers import CamembertForMaskedLM, CamembertTokenizer def UpperCamelCase__ ( A__ , A__ , A__ , A__=5 ) -> int: # Adapted from https://github.com/pytorch/fairseq/blob/master/fairseq/models/roberta/hub_interface.py assert masked_input.count('<mask>' ) == 1 snake_case__ : str = torch.tensor(tokenizer.encode(A__ , add_special_tokens=A__ ) ).unsqueeze(0 ) # Batch size 1 snake_case__ : Dict = model(A__ )[0] # The last hidden-state is the first element of the output tuple snake_case__ : int = (input_ids.squeeze() == tokenizer.mask_token_id).nonzero().item() snake_case__ : Any = logits[0, masked_index, :] snake_case__ : str = logits.softmax(dim=0 ) snake_case__ , snake_case__ : List[str] = prob.topk(k=A__ , dim=0 ) snake_case__ : Optional[int] = ' '.join( [tokenizer.convert_ids_to_tokens(indices[i].item() ) for i in range(len(A__ ) )] ) snake_case__ : int = tokenizer.mask_token snake_case__ : Union[str, Any] = [] for index, predicted_token_bpe in enumerate(topk_predicted_token_bpe.split(' ' ) ): snake_case__ : int = predicted_token_bpe.replace('\u2581' , ' ' ) if " {0}".format(A__ ) in masked_input: topk_filled_outputs.append( ( masked_input.replace(' {0}'.format(A__ ) , A__ ), values[index].item(), predicted_token, ) ) else: topk_filled_outputs.append( ( masked_input.replace(A__ , A__ ), values[index].item(), predicted_token, ) ) return topk_filled_outputs lowerCAmelCase__ : Optional[Any] = CamembertTokenizer.from_pretrained('''camembert-base''') lowerCAmelCase__ : Optional[int] = CamembertForMaskedLM.from_pretrained('''camembert-base''') model.eval() lowerCAmelCase__ : List[str] = '''Le camembert est <mask> :)''' print(fill_mask(masked_input, model, tokenizer, topk=3))
699
from collections import namedtuple lowerCAmelCase__ : Union[str, Any] = namedtuple('''from_to''', '''from_ to''') lowerCAmelCase__ : Tuple = { '''cubicmeter''': from_to(1, 1), '''litre''': from_to(0.0_01, 10_00), '''kilolitre''': from_to(1, 1), '''gallon''': from_to(0.0_04_54, 2_64.1_72), '''cubicyard''': from_to(0.7_64_55, 1.3_07_95), '''cubicfoot''': from_to(0.0_28, 35.31_47), '''cup''': from_to(0.0_00_23_65_88, 42_26.75), } def UpperCamelCase__ ( A__ , A__ , A__ ) -> float: if from_type not in METRIC_CONVERSION: raise ValueError( F"""Invalid 'from_type' value: {from_type!r} Supported values are:\n""" + ', '.join(A__ ) ) if to_type not in METRIC_CONVERSION: raise ValueError( F"""Invalid 'to_type' value: {to_type!r}. Supported values are:\n""" + ', '.join(A__ ) ) return value * METRIC_CONVERSION[from_type].from_ * METRIC_CONVERSION[to_type].to if __name__ == "__main__": import doctest doctest.testmod()
699
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) lowerCAmelCase__ : int = { '''configuration_swiftformer''': [ '''SWIFTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''SwiftFormerConfig''', '''SwiftFormerOnnxConfig''', ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict = [ '''SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''SwiftFormerForImageClassification''', '''SwiftFormerModel''', '''SwiftFormerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_swiftformer import ( SWIFTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, SwiftFormerConfig, SwiftFormerOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swiftformer import ( SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, SwiftFormerForImageClassification, SwiftFormerModel, SwiftFormerPreTrainedModel, ) else: import sys lowerCAmelCase__ : str = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
699
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCAmelCase__ : Tuple = logging.get_logger(__name__) lowerCAmelCase__ : Union[str, Any] = '''▁''' lowerCAmelCase__ : List[Any] = {'''vocab_file''': '''sentencepiece.bpe.model'''} lowerCAmelCase__ : Optional[Any] = { '''vocab_file''': { '''facebook/xglm-564M''': '''https://huggingface.co/facebook/xglm-564M/resolve/main/sentencepiece.bpe.model''', } } lowerCAmelCase__ : str = { '''facebook/xglm-564M''': 20_48, } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = ["""input_ids""", """attention_mask"""] def __init__( self , __UpperCamelCase , __UpperCamelCase="<s>" , __UpperCamelCase="</s>" , __UpperCamelCase="</s>" , __UpperCamelCase="<s>" , __UpperCamelCase="<unk>" , __UpperCamelCase="<pad>" , __UpperCamelCase = None , **__UpperCamelCase , ) -> None: '''simple docstring''' snake_case__ : Any = {} if sp_model_kwargs is None else sp_model_kwargs # Compatibility with the original tokenizer snake_case__ : Tuple = 7 snake_case__ : Dict = [F"""<madeupword{i}>""" for i in range(self.num_madeup_words )] snake_case__ : Union[str, Any] = kwargs.get('additional_special_tokens' , [] ) kwargs["additional_special_tokens"] += [ word for word in madeup_words if word not in kwargs["additional_special_tokens"] ] super().__init__( bos_token=__UpperCamelCase , eos_token=__UpperCamelCase , unk_token=__UpperCamelCase , sep_token=__UpperCamelCase , cls_token=__UpperCamelCase , pad_token=__UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , **__UpperCamelCase , ) snake_case__ : Optional[int] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(__UpperCamelCase ) ) snake_case__ : Optional[Any] = 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' # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab snake_case__ : Tuple = 1 # Mimic fairseq token-to-id alignment for the first 4 token snake_case__ : Tuple = {'<s>': 0, '<pad>': 1, '</s>': 2, '<unk>': 3} snake_case__ : List[Any] = len(self.sp_model ) snake_case__ : Optional[Any] = {F"""<madeupword{i}>""": sp_size + i + self.fairseq_offset for i in range(self.num_madeup_words )} self.fairseq_tokens_to_ids.update(__UpperCamelCase ) snake_case__ : Union[str, Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def __getstate__( self ) -> List[Any]: '''simple docstring''' snake_case__ : Union[str, Any] = self.__dict__.copy() snake_case__ : Optional[Any] = None snake_case__ : Tuple = self.sp_model.serialized_model_proto() return state def __setstate__( self , __UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Union[str, Any] = d # for backward compatibility if not hasattr(self , 'sp_model_kwargs' ): snake_case__ : Any = {} snake_case__ : Dict = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.LoadFromSerializedProto(self.sp_model_proto ) def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return [self.sep_token_id] + token_ids_a snake_case__ : str = [self.sep_token_id] return sep + token_ids_a + sep + sep + token_ids_a def __a ( self , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__UpperCamelCase , token_ids_a=__UpperCamelCase , already_has_special_tokens=__UpperCamelCase ) if token_ids_a is None: return [1] + ([0] * len(__UpperCamelCase )) return [1] + ([0] * len(__UpperCamelCase )) + [1, 1] + ([0] * len(__UpperCamelCase )) def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> List[int]: '''simple docstring''' snake_case__ : int = [self.sep_token_id] if token_ids_a is None: return len(sep + token_ids_a ) * [0] return len(sep + token_ids_a + sep + sep + token_ids_a ) * [0] @property def __a ( self ) -> Tuple: '''simple docstring''' return len(self.sp_model ) + self.fairseq_offset + self.num_madeup_words def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : int = {self.convert_ids_to_tokens(__UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __a ( self , __UpperCamelCase ) -> List[str]: '''simple docstring''' return self.sp_model.encode(__UpperCamelCase , out_type=__UpperCamelCase ) def __a ( self , __UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] snake_case__ : Optional[Any] = self.sp_model.PieceToId(__UpperCamelCase ) # 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 __a ( self , __UpperCamelCase ) -> Dict: '''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 __a ( self , __UpperCamelCase ) -> int: '''simple docstring''' snake_case__ : int = ''.join(__UpperCamelCase ).replace(__UpperCamelCase , ' ' ).strip() return out_string def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(__UpperCamelCase ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return snake_case__ : List[str] = 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __UpperCamelCase ) elif not os.path.isfile(self.vocab_file ): with open(__UpperCamelCase , 'wb' ) as fi: snake_case__ : Any = self.sp_model.serialized_model_proto() fi.write(__UpperCamelCase ) return (out_vocab_file,)
699
1
import argparse import json from pathlib import Path import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import DeiTImageProcessor, ViTConfig, ViTForImageClassification, ViTImageProcessor, ViTModel from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase__ : Optional[Any] = logging.get_logger(__name__) def UpperCamelCase__ ( A__ , A__=False ) -> Union[str, Any]: snake_case__ : Tuple = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"""blocks.{i}.norm1.weight""", F"""vit.encoder.layer.{i}.layernorm_before.weight""") ) rename_keys.append((F"""blocks.{i}.norm1.bias""", F"""vit.encoder.layer.{i}.layernorm_before.bias""") ) rename_keys.append((F"""blocks.{i}.attn.proj.weight""", F"""vit.encoder.layer.{i}.attention.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.attn.proj.bias""", F"""vit.encoder.layer.{i}.attention.output.dense.bias""") ) rename_keys.append((F"""blocks.{i}.norm2.weight""", F"""vit.encoder.layer.{i}.layernorm_after.weight""") ) rename_keys.append((F"""blocks.{i}.norm2.bias""", F"""vit.encoder.layer.{i}.layernorm_after.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.weight""", F"""vit.encoder.layer.{i}.intermediate.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.bias""", F"""vit.encoder.layer.{i}.intermediate.dense.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.weight""", F"""vit.encoder.layer.{i}.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.bias""", F"""vit.encoder.layer.{i}.output.dense.bias""") ) # projection layer + position embeddings rename_keys.extend( [ ('cls_token', 'vit.embeddings.cls_token'), ('patch_embed.proj.weight', 'vit.embeddings.patch_embeddings.projection.weight'), ('patch_embed.proj.bias', 'vit.embeddings.patch_embeddings.projection.bias'), ('pos_embed', 'vit.embeddings.position_embeddings'), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ('norm.weight', 'layernorm.weight'), ('norm.bias', 'layernorm.bias'), ('pre_logits.fc.weight', 'pooler.dense.weight'), ('pre_logits.fc.bias', 'pooler.dense.bias'), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" snake_case__ : Any = [(pair[0], pair[1][4:]) if pair[1].startswith('vit' ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ('norm.weight', 'vit.layernorm.weight'), ('norm.bias', 'vit.layernorm.bias'), ('head.weight', 'classifier.weight'), ('head.bias', 'classifier.bias'), ] ) return rename_keys def UpperCamelCase__ ( A__ , A__ , A__=False ) -> Any: for i in range(config.num_hidden_layers ): if base_model: snake_case__ : Optional[Any] = '' else: snake_case__ : List[Any] = 'vit.' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) snake_case__ : Dict = state_dict.pop(F"""blocks.{i}.attn.qkv.weight""" ) snake_case__ : str = state_dict.pop(F"""blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict snake_case__ : Union[str, Any] = in_proj_weight[ : config.hidden_size, : ] snake_case__ : Dict = in_proj_bias[: config.hidden_size] snake_case__ : Dict = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] snake_case__ : int = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] snake_case__ : Optional[Any] = in_proj_weight[ -config.hidden_size :, : ] snake_case__ : List[Any] = in_proj_bias[-config.hidden_size :] def UpperCamelCase__ ( A__ ) -> Any: snake_case__ : str = ['head.weight', 'head.bias'] for k in ignore_keys: state_dict.pop(A__ , A__ ) def UpperCamelCase__ ( A__ , A__ , A__ ) -> Tuple: snake_case__ : Any = dct.pop(A__ ) snake_case__ : Optional[int] = val def UpperCamelCase__ ( ) -> Tuple: snake_case__ : int = 'http://images.cocodataset.org/val2017/000000039769.jpg' snake_case__ : List[Any] = Image.open(requests.get(A__ , stream=A__ ).raw ) return im @torch.no_grad() def UpperCamelCase__ ( A__ , A__ ) -> List[Any]: snake_case__ : List[Any] = ViTConfig() snake_case__ : List[Any] = False # dataset (ImageNet-21k only or also fine-tuned on ImageNet 2012), patch_size and image_size if vit_name[-5:] == "in21k": snake_case__ : List[Any] = True snake_case__ : Union[str, Any] = int(vit_name[-12:-10] ) snake_case__ : Tuple = int(vit_name[-9:-6] ) else: snake_case__ : List[str] = 1000 snake_case__ : Optional[int] = 'huggingface/label-files' snake_case__ : Dict = 'imagenet-1k-id2label.json' snake_case__ : List[Any] = json.load(open(hf_hub_download(A__ , A__ , repo_type='dataset' ) , 'r' ) ) snake_case__ : Tuple = {int(A__ ): v for k, v in idalabel.items()} snake_case__ : List[str] = idalabel snake_case__ : List[Any] = {v: k for k, v in idalabel.items()} snake_case__ : Union[str, Any] = int(vit_name[-6:-4] ) snake_case__ : Tuple = int(vit_name[-3:] ) # size of the architecture if "deit" in vit_name: if vit_name[9:].startswith('tiny' ): snake_case__ : List[Any] = 192 snake_case__ : Tuple = 768 snake_case__ : List[Any] = 12 snake_case__ : Dict = 3 elif vit_name[9:].startswith('small' ): snake_case__ : Union[str, Any] = 384 snake_case__ : Optional[int] = 1536 snake_case__ : Optional[Any] = 12 snake_case__ : Any = 6 else: pass else: if vit_name[4:].startswith('small' ): snake_case__ : Dict = 768 snake_case__ : int = 2304 snake_case__ : str = 8 snake_case__ : Optional[Any] = 8 elif vit_name[4:].startswith('base' ): pass elif vit_name[4:].startswith('large' ): snake_case__ : List[Any] = 1024 snake_case__ : Union[str, Any] = 4096 snake_case__ : Optional[Any] = 24 snake_case__ : Dict = 16 elif vit_name[4:].startswith('huge' ): snake_case__ : Union[str, Any] = 1280 snake_case__ : str = 5120 snake_case__ : List[Any] = 32 snake_case__ : Optional[int] = 16 # load original model from timm snake_case__ : List[Any] = timm.create_model(A__ , pretrained=A__ ) timm_model.eval() # load state_dict of original model, remove and rename some keys snake_case__ : Any = timm_model.state_dict() if base_model: remove_classification_head_(A__ ) snake_case__ : int = create_rename_keys(A__ , A__ ) for src, dest in rename_keys: rename_key(A__ , A__ , A__ ) read_in_q_k_v(A__ , A__ , A__ ) # load HuggingFace model if vit_name[-5:] == "in21k": snake_case__ : Optional[int] = ViTModel(A__ ).eval() else: snake_case__ : str = ViTForImageClassification(A__ ).eval() model.load_state_dict(A__ ) # Check outputs on an image, prepared by ViTImageProcessor/DeiTImageProcessor if "deit" in vit_name: snake_case__ : Tuple = DeiTImageProcessor(size=config.image_size ) else: snake_case__ : Optional[int] = ViTImageProcessor(size=config.image_size ) snake_case__ : Dict = image_processor(images=prepare_img() , return_tensors='pt' ) snake_case__ : str = encoding['pixel_values'] snake_case__ : int = model(A__ ) if base_model: snake_case__ : Optional[int] = timm_model.forward_features(A__ ) assert timm_pooled_output.shape == outputs.pooler_output.shape assert torch.allclose(A__ , outputs.pooler_output , atol=1e-3 ) else: snake_case__ : List[str] = timm_model(A__ ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(A__ , outputs.logits , atol=1e-3 ) Path(A__ ).mkdir(exist_ok=A__ ) print(F"""Saving model {vit_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(A__ ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(A__ ) if __name__ == "__main__": lowerCAmelCase__ : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--vit_name''', default='''vit_base_patch16_224''', type=str, help='''Name of the ViT timm model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) lowerCAmelCase__ : List[str] = parser.parse_args() convert_vit_checkpoint(args.vit_name, args.pytorch_dump_folder_path)
699
import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert_fast import BertTokenizerFast from .tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer, DPRReaderTokenizer lowerCAmelCase__ : Any = logging.get_logger(__name__) lowerCAmelCase__ : List[Any] = {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''} lowerCAmelCase__ : Any = { '''vocab_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } lowerCAmelCase__ : Any = { '''vocab_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } lowerCAmelCase__ : Tuple = { '''vocab_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json''' ), }, } lowerCAmelCase__ : Dict = { '''facebook/dpr-ctx_encoder-single-nq-base''': 5_12, '''facebook/dpr-ctx_encoder-multiset-base''': 5_12, } lowerCAmelCase__ : Union[str, Any] = { '''facebook/dpr-question_encoder-single-nq-base''': 5_12, '''facebook/dpr-question_encoder-multiset-base''': 5_12, } lowerCAmelCase__ : Optional[Any] = { '''facebook/dpr-reader-single-nq-base''': 5_12, '''facebook/dpr-reader-multiset-base''': 5_12, } lowerCAmelCase__ : Tuple = { '''facebook/dpr-ctx_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-ctx_encoder-multiset-base''': {'''do_lower_case''': True}, } lowerCAmelCase__ : Any = { '''facebook/dpr-question_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-question_encoder-multiset-base''': {'''do_lower_case''': True}, } lowerCAmelCase__ : List[str] = { '''facebook/dpr-reader-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-reader-multiset-base''': {'''do_lower_case''': True}, } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION __lowerCamelCase = DPRContextEncoderTokenizer class __snake_case ( _lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION __lowerCamelCase = DPRQuestionEncoderTokenizer lowerCAmelCase__ : Tuple = collections.namedtuple( '''DPRSpanPrediction''', ['''span_score''', '''relevance_score''', '''doc_id''', '''start_index''', '''end_index''', '''text'''] ) lowerCAmelCase__ : List[Any] = collections.namedtuple('''DPRReaderOutput''', ['''start_logits''', '''end_logits''', '''relevance_logits''']) lowerCAmelCase__ : int = r''' Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`. It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers), using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)` with the format: [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids> Args: questions (`str` or `List[str]`): The questions to be encoded. You can specify one question for many passages. In this case, the question will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in `titles` or `texts`. titles (`str` or `List[str]`): The passages titles to be encoded. This can be a string or a list of strings if there are several passages. texts (`str` or `List[str]`): The passages texts to be encoded. This can be a string or a list of strings if there are several passages. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`): Activates and controls padding. Accepts the following values: - `True` or `\'longest\'`: Pad to the longest sequence in the batch (or no padding if only a single sequence if provided). - `\'max_length\'`: Pad to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. - `False` or `\'do_not_pad\'` (default): No padding (i.e., can output a batch with sequences of different lengths). truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`): Activates and controls truncation. Accepts the following values: - `True` or `\'longest_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will truncate token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch of pairs) is provided. - `\'only_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `\'only_second\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `False` or `\'do_not_truncate\'` (default): No truncation (i.e., can output batch with sequence lengths greater than the model maximum admissible input size). max_length (`int`, *optional*): Controls the maximum length to use by one of the truncation/padding parameters. If left unset or set to `None`, this will use the predefined model maximum length if a maximum length is required by one of the truncation/padding parameters. If the model has no specific maximum input length (like XLNet) truncation/padding to a maximum length will be deactivated. return_tensors (`str` or [`~utils.TensorType`], *optional*): If set, will return tensors instead of list of python integers. Acceptable values are: - `\'tf\'`: Return TensorFlow `tf.constant` objects. - `\'pt\'`: Return PyTorch `torch.Tensor` objects. - `\'np\'`: Return Numpy `np.ndarray` objects. return_attention_mask (`bool`, *optional*): Whether or not to return the attention mask. If not set, will return the attention mask according to the specific tokenizer\'s default, defined by the `return_outputs` attribute. [What are attention masks?](../glossary#attention-mask) Return: `Dict[str, List[List[int]]]`: A dictionary with the following keys: - `input_ids`: List of token ids to be fed to a model. - `attention_mask`: List of indices specifying which tokens should be attended to by the model. ''' @add_start_docstrings(_lowerCamelCase ) class __snake_case : def __call__( self , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = False , __UpperCamelCase = False , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = None , **__UpperCamelCase , ) -> BatchEncoding: '''simple docstring''' if titles is None and texts is None: return super().__call__( __UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase , max_length=__UpperCamelCase , return_tensors=__UpperCamelCase , return_attention_mask=__UpperCamelCase , **__UpperCamelCase , ) elif titles is None or texts is None: snake_case__ : Optional[Any] = titles if texts is None else texts return super().__call__( __UpperCamelCase , __UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase , max_length=__UpperCamelCase , return_tensors=__UpperCamelCase , return_attention_mask=__UpperCamelCase , **__UpperCamelCase , ) snake_case__ : int = titles if not isinstance(__UpperCamelCase , __UpperCamelCase ) else [titles] snake_case__ : Optional[int] = texts if not isinstance(__UpperCamelCase , __UpperCamelCase ) else [texts] snake_case__ : List[Any] = len(__UpperCamelCase ) snake_case__ : str = questions if not isinstance(__UpperCamelCase , __UpperCamelCase ) else [questions] * n_passages assert len(__UpperCamelCase ) == len( __UpperCamelCase ), F"""There should be as many titles than texts but got {len(__UpperCamelCase )} titles and {len(__UpperCamelCase )} texts.""" snake_case__ : Optional[int] = super().__call__(__UpperCamelCase , __UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase )['input_ids'] snake_case__ : Optional[Any] = super().__call__(__UpperCamelCase , add_special_tokens=__UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase )['input_ids'] snake_case__ : Union[str, Any] = { 'input_ids': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(__UpperCamelCase , __UpperCamelCase ) ] } if return_attention_mask is not False: snake_case__ : List[Any] = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) snake_case__ : Union[str, Any] = attention_mask return self.pad(__UpperCamelCase , padding=__UpperCamelCase , max_length=__UpperCamelCase , return_tensors=__UpperCamelCase ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = 16 , __UpperCamelCase = 64 , __UpperCamelCase = 4 , ) -> List[DPRSpanPrediction]: '''simple docstring''' snake_case__ : Optional[Any] = reader_input['input_ids'] snake_case__ , snake_case__ , snake_case__ : Any = reader_output[:3] snake_case__ : List[str] = len(__UpperCamelCase ) snake_case__ : Tuple = sorted(range(__UpperCamelCase ) , reverse=__UpperCamelCase , key=relevance_logits.__getitem__ ) snake_case__ : List[DPRReaderOutput] = [] for doc_id in sorted_docs: snake_case__ : Tuple = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence snake_case__ : int = sequence_ids.index(self.sep_token_id , 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: snake_case__ : Union[str, Any] = sequence_ids.index(self.pad_token_id ) else: snake_case__ : str = len(__UpperCamelCase ) snake_case__ : Dict = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=__UpperCamelCase , top_spans=__UpperCamelCase , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=__UpperCamelCase , start_index=__UpperCamelCase , end_index=__UpperCamelCase , text=self.decode(sequence_ids[start_index : end_index + 1] ) , ) ) if len(__UpperCamelCase ) >= num_spans: break return nbest_spans_predictions[:num_spans] def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , ) -> List[DPRSpanPrediction]: '''simple docstring''' snake_case__ : Any = [] for start_index, start_score in enumerate(__UpperCamelCase ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) snake_case__ : str = sorted(__UpperCamelCase , key=lambda __UpperCamelCase : x[1] , reverse=__UpperCamelCase ) snake_case__ : Any = [] for (start_index, end_index), score in scores: assert start_index <= end_index, F"""Wrong span indices: [{start_index}:{end_index}]""" snake_case__ : str = end_index - start_index + 1 assert length <= max_answer_length, F"""Span is too long: {length} > {max_answer_length}""" if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(__UpperCamelCase ) == top_spans: break return chosen_span_intervals @add_end_docstrings(_lowerCamelCase ) class __snake_case ( _lowerCamelCase ,_lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = READER_PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = READER_PRETRAINED_INIT_CONFIGURATION __lowerCamelCase = ["""input_ids""", """attention_mask"""] __lowerCamelCase = DPRReaderTokenizer
699
1
import random def UpperCamelCase__ ( A__ , A__ ) -> tuple: snake_case__ , snake_case__ , snake_case__ : Tuple = [], [], [] for element in data: if element < pivot: less.append(A__ ) elif element > pivot: greater.append(A__ ) else: equal.append(A__ ) return less, equal, greater def UpperCamelCase__ ( A__ , A__ ) -> Optional[int]: # index = len(items) // 2 when trying to find the median # (value of index when items is sorted) # invalid input if index >= len(A__ ) or index < 0: return None snake_case__ : Union[str, Any] = items[random.randint(0 , len(A__ ) - 1 )] snake_case__ : List[str] = 0 snake_case__ , snake_case__ , snake_case__ : List[str] = _partition(A__ , A__ ) snake_case__ : str = len(A__ ) snake_case__ : Union[str, Any] = len(A__ ) # index is the pivot if m <= index < m + count: return pivot # must be in smaller elif m > index: return quick_select(A__ , A__ ) # must be in larger else: return quick_select(A__ , index - (m + count) )
699
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, EulerAncestralDiscreteScheduler, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionInstructPixaPixPipeline, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.utils import floats_tensor, load_image, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( IMAGE_TO_IMAGE_IMAGE_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __snake_case ( _lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ,unittest.TestCase ): __lowerCamelCase = StableDiffusionInstructPixaPixPipeline __lowerCamelCase = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""height""", """width""", """cross_attention_kwargs"""} __lowerCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __lowerCamelCase = IMAGE_TO_IMAGE_IMAGE_PARAMS __lowerCamelCase = IMAGE_TO_IMAGE_IMAGE_PARAMS def __a ( self ) -> List[Any]: '''simple docstring''' torch.manual_seed(0 ) snake_case__ : Tuple = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=8 , out_channels=4 , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D') , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D') , cross_attention_dim=32 , ) snake_case__ : Any = PNDMScheduler(skip_prk_steps=__UpperCamelCase ) torch.manual_seed(0 ) snake_case__ : Dict = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] , up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] , latent_channels=4 , ) torch.manual_seed(0 ) snake_case__ : int = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) snake_case__ : Tuple = CLIPTextModel(__UpperCamelCase ) snake_case__ : Optional[int] = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) snake_case__ : Optional[int] = { 'unet': unet, 'scheduler': scheduler, 'vae': vae, 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'safety_checker': None, 'feature_extractor': None, } return components def __a ( self , __UpperCamelCase , __UpperCamelCase=0 ) -> Dict: '''simple docstring''' snake_case__ : Optional[Any] = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) snake_case__ : int = image.cpu().permute(0 , 2 , 3 , 1 )[0] snake_case__ : Union[str, Any] = Image.fromarray(np.uinta(__UpperCamelCase ) ).convert('RGB' ) if str(__UpperCamelCase ).startswith('mps' ): snake_case__ : str = torch.manual_seed(__UpperCamelCase ) else: snake_case__ : Dict = torch.Generator(device=__UpperCamelCase ).manual_seed(__UpperCamelCase ) snake_case__ : str = { 'prompt': 'A painting of a squirrel eating a burger', 'image': image, 'generator': generator, 'num_inference_steps': 2, 'guidance_scale': 6.0, 'image_guidance_scale': 1, 'output_type': 'numpy', } return inputs def __a ( self ) -> Tuple: '''simple docstring''' snake_case__ : Dict = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : Optional[int] = self.get_dummy_components() snake_case__ : str = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : Optional[int] = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Tuple = self.get_dummy_inputs(__UpperCamelCase ) snake_case__ : List[str] = sd_pipe(**__UpperCamelCase ).images snake_case__ : List[str] = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) snake_case__ : str = np.array([0.7_5_2_6, 0.3_7_5_0, 0.4_5_4_7, 0.6_1_1_7, 0.5_8_6_6, 0.5_0_1_6, 0.4_3_2_7, 0.5_6_4_2, 0.4_8_1_5] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __a ( self ) -> Any: '''simple docstring''' snake_case__ : Dict = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : Union[str, Any] = self.get_dummy_components() snake_case__ : Optional[Any] = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : List[Any] = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Union[str, Any] = self.get_dummy_inputs(__UpperCamelCase ) snake_case__ : List[str] = 'french fries' snake_case__ : Optional[Any] = sd_pipe(**__UpperCamelCase , negative_prompt=__UpperCamelCase ) snake_case__ : Union[str, Any] = output.images snake_case__ : Tuple = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) snake_case__ : Any = np.array([0.7_5_1_1, 0.3_6_4_2, 0.4_5_5_3, 0.6_2_3_6, 0.5_7_9_7, 0.5_0_1_3, 0.4_3_4_3, 0.5_6_1_1, 0.4_8_3_1] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __a ( self ) -> int: '''simple docstring''' snake_case__ : Optional[int] = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : List[str] = self.get_dummy_components() snake_case__ : str = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : str = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Dict = self.get_dummy_inputs(__UpperCamelCase ) snake_case__ : Any = [inputs['prompt']] * 2 snake_case__ : Optional[int] = np.array(inputs['image'] ).astype(np.floataa ) / 2_5_5.0 snake_case__ : Optional[int] = torch.from_numpy(__UpperCamelCase ).unsqueeze(0 ).to(__UpperCamelCase ) snake_case__ : Any = image / 2 + 0.5 snake_case__ : Optional[Any] = image.permute(0 , 3 , 1 , 2 ) snake_case__ : List[Any] = image.repeat(2 , 1 , 1 , 1 ) snake_case__ : Optional[int] = sd_pipe(**__UpperCamelCase ).images snake_case__ : Union[str, Any] = image[-1, -3:, -3:, -1] assert image.shape == (2, 32, 32, 3) snake_case__ : List[Any] = np.array([0.5_8_1_2, 0.5_7_4_8, 0.5_2_2_2, 0.5_9_0_8, 0.5_6_9_5, 0.7_1_7_4, 0.6_8_0_4, 0.5_5_2_3, 0.5_5_7_9] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Optional[int] = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : Optional[int] = self.get_dummy_components() snake_case__ : Tuple = EulerAncestralDiscreteScheduler( beta_start=0.0_0_0_8_5 , beta_end=0.0_1_2 , beta_schedule='scaled_linear' ) snake_case__ : int = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : List[str] = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : str = self.get_dummy_inputs(__UpperCamelCase ) snake_case__ : Any = sd_pipe(**__UpperCamelCase ).images snake_case__ : int = image[0, -3:, -3:, -1] snake_case__ : Tuple = [round(__UpperCamelCase , 4 ) for x in image_slice.flatten().tolist()] print(','.join([str(__UpperCamelCase ) for x in slice] ) ) assert image.shape == (1, 32, 32, 3) snake_case__ : List[Any] = np.array([0.7_4_1_7, 0.3_8_4_2, 0.4_7_3_2, 0.5_7_7_6, 0.5_8_9_1, 0.5_1_3_9, 0.4_0_5_2, 0.5_6_7_3, 0.4_9_8_6] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __a ( self ) -> int: '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) def __a ( self ) -> Any: '''simple docstring''' snake_case__ : Optional[int] = self.get_dummy_components() snake_case__ : int = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : Union[str, Any] = VaeImageProcessor(do_resize=__UpperCamelCase , do_normalize=__UpperCamelCase ) snake_case__ : Optional[int] = pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Optional[Any] = pipe(**self.get_dummy_inputs_by_type(__UpperCamelCase , input_image_type='pt' ) )[0] snake_case__ : Union[str, Any] = components['vae'] snake_case__ : str = self.get_dummy_inputs_by_type(__UpperCamelCase , input_image_type='pt' ) for image_param in self.image_latents_params: if image_param in inputs.keys(): snake_case__ : List[str] = vae.encode(inputs[image_param] ).latent_dist.mode() snake_case__ : Dict = pipe(**__UpperCamelCase )[0] snake_case__ : str = np.abs(out - out_latents_inputs ).max() self.assertLess(__UpperCamelCase , 1E-4 , 'passing latents as image input generate different result from passing image' ) @slow @require_torch_gpu class __snake_case ( unittest.TestCase ): def __a ( self ) -> List[str]: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __a ( self , __UpperCamelCase=0 ) -> Dict: '''simple docstring''' snake_case__ : Optional[Any] = torch.manual_seed(__UpperCamelCase ) snake_case__ : List[str] = load_image( 'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_pix2pix/example.jpg' ) snake_case__ : int = { 'prompt': 'turn him into a cyborg', 'image': image, 'generator': generator, 'num_inference_steps': 3, 'guidance_scale': 7.5, 'image_guidance_scale': 1.0, 'output_type': 'numpy', } return inputs def __a ( self ) -> Dict: '''simple docstring''' snake_case__ : Any = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : Tuple = self.get_inputs() snake_case__ : List[Any] = pipe(**__UpperCamelCase ).images snake_case__ : Union[str, Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) snake_case__ : Dict = np.array([0.5_9_0_2, 0.6_0_1_5, 0.6_0_2_7, 0.5_9_8_3, 0.6_0_9_2, 0.6_0_6_1, 0.5_7_6_5, 0.5_7_8_5, 0.5_5_5_5] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def __a ( self ) -> str: '''simple docstring''' snake_case__ : Any = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase ) snake_case__ : Tuple = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : Dict = self.get_inputs() snake_case__ : Dict = pipe(**__UpperCamelCase ).images snake_case__ : Union[str, Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) snake_case__ : List[Any] = np.array([0.6_5_7_8, 0.6_8_1_7, 0.6_9_7_2, 0.6_7_6_1, 0.6_8_5_6, 0.6_9_1_6, 0.6_4_2_8, 0.6_5_1_6, 0.6_3_0_1] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def __a ( self ) -> Optional[Any]: '''simple docstring''' snake_case__ : Tuple = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase ) snake_case__ : Tuple = DDIMScheduler.from_config(pipe.scheduler.config ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : Optional[int] = self.get_inputs() snake_case__ : Optional[int] = pipe(**__UpperCamelCase ).images snake_case__ : Tuple = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) snake_case__ : int = np.array([0.3_8_2_8, 0.3_8_3_4, 0.3_8_1_8, 0.3_7_9_2, 0.3_8_6_5, 0.3_7_5_2, 0.3_7_9_2, 0.3_8_4_7, 0.3_7_5_3] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : int = 0 def callback_fn(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> None: snake_case__ : List[Any] = True nonlocal number_of_steps number_of_steps += 1 if step == 1: snake_case__ : Any = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 64) snake_case__ : int = latents[0, -3:, -3:, -1] snake_case__ : List[str] = np.array([-0.2_4_6_3, -0.4_6_4_4, -0.9_7_5_6, 1.5_1_7_6, 1.4_4_1_4, 0.7_8_6_6, 0.9_8_9_7, 0.8_5_2_1, 0.7_9_8_3] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2 elif step == 2: snake_case__ : Dict = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 64) snake_case__ : Dict = latents[0, -3:, -3:, -1] snake_case__ : Optional[Any] = np.array([-0.2_6_4_4, -0.4_6_2_6, -0.9_6_5_3, 1.5_1_7_6, 1.4_5_5_1, 0.7_6_8_6, 0.9_8_0_5, 0.8_4_5_2, 0.8_1_1_5] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2 snake_case__ : str = False snake_case__ : List[Any] = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase , torch_dtype=torch.floataa ) snake_case__ : int = pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : int = self.get_inputs() pipe(**__UpperCamelCase , callback=__UpperCamelCase , callback_steps=1 ) assert callback_fn.has_been_called assert number_of_steps == 3 def __a ( self ) -> Any: '''simple docstring''' torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() snake_case__ : str = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase , torch_dtype=torch.floataa ) snake_case__ : Dict = pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() snake_case__ : str = self.get_inputs() snake_case__ : Tuple = pipe(**__UpperCamelCase ) snake_case__ : List[Any] = torch.cuda.max_memory_allocated() # make sure that less than 2.2 GB is allocated assert mem_bytes < 2.2 * 10**9 def __a ( self ) -> int: '''simple docstring''' snake_case__ : int = self.get_inputs() # resize to resolution that is divisible by 8 but not 16 or 32 snake_case__ : Tuple = inputs['image'].resize((504, 504) ) snake_case__ : str = 'timbrooks/instruct-pix2pix' snake_case__ : Tuple = StableDiffusionInstructPixaPixPipeline.from_pretrained( __UpperCamelCase , safety_checker=__UpperCamelCase , ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : str = pipe(**__UpperCamelCase ) snake_case__ : List[Any] = output.images[0] snake_case__ : List[Any] = image[255:258, 383:386, -1] assert image.shape == (504, 504, 3) snake_case__ : List[str] = np.array([0.2_7_2_6, 0.2_5_2_9, 0.2_6_6_4, 0.2_6_5_5, 0.2_6_4_1, 0.2_6_4_2, 0.2_5_9_1, 0.2_6_4_9, 0.2_5_9_0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-3
699
1
from math import sqrt def UpperCamelCase__ ( A__ ) -> bool: assert isinstance(A__ , A__ ) and ( number >= 0 ), "'number' must been an int and positive" snake_case__ : List[Any] = True # 0 and 1 are none primes. if number <= 1: snake_case__ : Any = False for divisor in range(2 , int(round(sqrt(A__ ) ) ) + 1 ): # if 'number' divisible by 'divisor' then sets 'status' # of false and break up the loop. if number % divisor == 0: snake_case__ : Optional[Any] = False break # precondition assert isinstance(A__ , A__ ), "'status' must been from type bool" return status def UpperCamelCase__ ( A__ ) -> int: assert isinstance(A__ , A__ ) and (n > 2), "'N' must been an int and > 2" # beginList: contains all natural numbers from 2 up to N snake_case__ : Optional[int] = list(range(2 , n + 1 ) ) snake_case__ : Any = [] # this list will be returns. # actual sieve of erathostenes for i in range(len(A__ ) ): for j in range(i + 1 , len(A__ ) ): if (begin_list[i] != 0) and (begin_list[j] % begin_list[i] == 0): snake_case__ : Optional[int] = 0 # filters actual prime numbers. snake_case__ : Any = [x for x in begin_list if x != 0] # precondition assert isinstance(A__ , A__ ), "'ans' must been from type list" return ans def UpperCamelCase__ ( A__ ) -> Tuple: assert isinstance(A__ , A__ ) and (n > 2), "'N' must been an int and > 2" snake_case__ : Dict = [] # iterates over all numbers between 2 up to N+1 # if a number is prime then appends to list 'ans' for number in range(2 , n + 1 ): if is_prime(A__ ): ans.append(A__ ) # precondition assert isinstance(A__ , A__ ), "'ans' must been from type list" return ans def UpperCamelCase__ ( A__ ) -> List[str]: assert isinstance(A__ , A__ ) and number >= 0, "'number' must been an int and >= 0" snake_case__ : List[Any] = [] # this list will be returns of the function. # potential prime number factors. snake_case__ : Tuple = 2 snake_case__ : int = number if number == 0 or number == 1: ans.append(A__ ) # if 'number' not prime then builds the prime factorization of 'number' elif not is_prime(A__ ): while quotient != 1: if is_prime(A__ ) and (quotient % factor == 0): ans.append(A__ ) quotient /= factor else: factor += 1 else: ans.append(A__ ) # precondition assert isinstance(A__ , A__ ), "'ans' must been from type list" return ans def UpperCamelCase__ ( A__ ) -> Any: assert isinstance(A__ , A__ ) and ( number >= 0 ), "'number' bust been an int and >= 0" snake_case__ : int = 0 # prime factorization of 'number' snake_case__ : Optional[Any] = prime_factorization(A__ ) snake_case__ : str = max(A__ ) # precondition assert isinstance(A__ , A__ ), "'ans' must been from type int" return ans def UpperCamelCase__ ( A__ ) -> Union[str, Any]: assert isinstance(A__ , A__ ) and ( number >= 0 ), "'number' bust been an int and >= 0" snake_case__ : str = 0 # prime factorization of 'number' snake_case__ : str = prime_factorization(A__ ) snake_case__ : Optional[int] = min(A__ ) # precondition assert isinstance(A__ , A__ ), "'ans' must been from type int" return ans def UpperCamelCase__ ( A__ ) -> Optional[Any]: assert isinstance(A__ , A__ ), "'number' must been an int" assert isinstance(number % 2 == 0 , A__ ), "compare bust been from type bool" return number % 2 == 0 def UpperCamelCase__ ( A__ ) -> List[str]: assert isinstance(A__ , A__ ), "'number' must been an int" assert isinstance(number % 2 != 0 , A__ ), "compare bust been from type bool" return number % 2 != 0 def UpperCamelCase__ ( A__ ) -> Union[str, Any]: assert ( isinstance(A__ , A__ ) and (number > 2) and is_even(A__ ) ), "'number' must been an int, even and > 2" snake_case__ : List[str] = [] # this list will returned # creates a list of prime numbers between 2 up to 'number' snake_case__ : Dict = get_prime_numbers(A__ ) snake_case__ : str = len(A__ ) # run variable for while-loops. snake_case__ : Tuple = 0 snake_case__ : Dict = None # exit variable. for break up the loops snake_case__ : List[str] = True while i < len_pn and loop: snake_case__ : Union[str, Any] = i + 1 while j < len_pn and loop: if prime_numbers[i] + prime_numbers[j] == number: snake_case__ : str = False ans.append(prime_numbers[i] ) ans.append(prime_numbers[j] ) j += 1 i += 1 # precondition assert ( isinstance(A__ , A__ ) and (len(A__ ) == 2) and (ans[0] + ans[1] == number) and is_prime(ans[0] ) and is_prime(ans[1] ) ), "'ans' must contains two primes. And sum of elements must been eq 'number'" return ans def UpperCamelCase__ ( A__ , A__ ) -> Dict: assert ( isinstance(A__ , A__ ) and isinstance(A__ , A__ ) and (numbera >= 0) and (numbera >= 0) ), "'number1' and 'number2' must been positive integer." snake_case__ : Union[str, Any] = 0 while numbera != 0: snake_case__ : Tuple = numbera % numbera snake_case__ : Optional[Any] = numbera snake_case__ : int = rest # precondition assert isinstance(A__ , A__ ) and ( numbera >= 0 ), "'number' must been from type int and positive" return numbera def UpperCamelCase__ ( A__ , A__ ) -> Optional[int]: assert ( isinstance(A__ , A__ ) and isinstance(A__ , A__ ) and (numbera >= 1) and (numbera >= 1) ), "'number1' and 'number2' must been positive integer." snake_case__ : Optional[Any] = 1 # actual answer that will be return. # for kgV (x,1) if numbera > 1 and numbera > 1: # builds the prime factorization of 'number1' and 'number2' snake_case__ : int = prime_factorization(A__ ) snake_case__ : int = prime_factorization(A__ ) elif numbera == 1 or numbera == 1: snake_case__ : List[Any] = [] snake_case__ : List[str] = [] snake_case__ : List[str] = max(A__ , A__ ) snake_case__ : List[str] = 0 snake_case__ : Optional[Any] = 0 snake_case__ : List[str] = [] # captured numbers int both 'primeFac1' and 'primeFac2' # iterates through primeFac1 for n in prime_fac_a: if n not in done: if n in prime_fac_a: snake_case__ : List[str] = prime_fac_a.count(A__ ) snake_case__ : Tuple = prime_fac_a.count(A__ ) for _ in range(max(A__ , A__ ) ): ans *= n else: snake_case__ : Optional[Any] = prime_fac_a.count(A__ ) for _ in range(A__ ): ans *= n done.append(A__ ) # iterates through primeFac2 for n in prime_fac_a: if n not in done: snake_case__ : int = prime_fac_a.count(A__ ) for _ in range(A__ ): ans *= n done.append(A__ ) # precondition assert isinstance(A__ , A__ ) and ( ans >= 0 ), "'ans' must been from type int and positive" return ans def UpperCamelCase__ ( A__ ) -> str: assert isinstance(A__ , A__ ) and (n >= 0), "'number' must been a positive int" snake_case__ : Optional[Any] = 0 snake_case__ : Optional[int] = 2 # this variable holds the answer while index < n: index += 1 ans += 1 # counts to the next number # if ans not prime then # runs to the next prime number. while not is_prime(A__ ): ans += 1 # precondition assert isinstance(A__ , A__ ) and is_prime( A__ ), "'ans' must been a prime number and from type int" return ans def UpperCamelCase__ ( A__ , A__ ) -> Optional[Any]: assert ( is_prime(A__ ) and is_prime(A__ ) and (p_number_a < p_number_a) ), "The arguments must been prime numbers and 'pNumber1' < 'pNumber2'" snake_case__ : int = p_number_a + 1 # jump to the next number snake_case__ : Union[str, Any] = [] # this list will be returns. # if number is not prime then # fetch the next prime number. while not is_prime(A__ ): number += 1 while number < p_number_a: ans.append(A__ ) number += 1 # fetch the next prime number. while not is_prime(A__ ): number += 1 # precondition assert ( isinstance(A__ , A__ ) and ans[0] != p_number_a and ans[len(A__ ) - 1] != p_number_a ), "'ans' must been a list without the arguments" # 'ans' contains not 'pNumber1' and 'pNumber2' ! return ans def UpperCamelCase__ ( A__ ) -> Optional[Any]: assert isinstance(A__ , A__ ) and (n >= 1), "'n' must been int and >= 1" snake_case__ : Optional[Any] = [] # will be returned. for divisor in range(1 , n + 1 ): if n % divisor == 0: ans.append(A__ ) # precondition assert ans[0] == 1 and ans[len(A__ ) - 1] == n, "Error in function getDivisiors(...)" return ans def UpperCamelCase__ ( A__ ) -> Any: assert isinstance(A__ , A__ ) and ( number > 1 ), "'number' must been an int and >= 1" snake_case__ : Optional[int] = get_divisors(A__ ) # precondition assert ( isinstance(A__ , A__ ) and (divisors[0] == 1) and (divisors[len(A__ ) - 1] == number) ), "Error in help-function getDivisiors(...)" # summed all divisors up to 'number' (exclusive), hence [:-1] return sum(divisors[:-1] ) == number def UpperCamelCase__ ( A__ , A__ ) -> Tuple: assert ( isinstance(A__ , A__ ) and isinstance(A__ , A__ ) and (denominator != 0) ), "The arguments must been from type int and 'denominator' != 0" # build the greatest common divisor of numerator and denominator. snake_case__ : Optional[int] = gcd(abs(A__ ) , abs(A__ ) ) # precondition assert ( isinstance(A__ , A__ ) and (numerator % gcd_of_fraction == 0) and (denominator % gcd_of_fraction == 0) ), "Error in function gcd(...,...)" return (numerator // gcd_of_fraction, denominator // gcd_of_fraction) def UpperCamelCase__ ( A__ ) -> Tuple: assert isinstance(A__ , A__ ) and (n >= 0), "'n' must been a int and >= 0" snake_case__ : Dict = 1 # this will be return. for factor in range(1 , n + 1 ): ans *= factor return ans def UpperCamelCase__ ( A__ ) -> Dict: assert isinstance(A__ , A__ ) and (n >= 0), "'n' must been an int and >= 0" snake_case__ : List[Any] = 0 snake_case__ : Optional[Any] = 1 snake_case__ : Any = 1 # this will be return for _ in range(n - 1 ): snake_case__ : List[str] = ans ans += fiba snake_case__ : Union[str, Any] = tmp return ans
699
from .data_collator import ( DataCollatorForLanguageModeling, DataCollatorForPermutationLanguageModeling, DataCollatorForSeqaSeq, DataCollatorForSOP, DataCollatorForTokenClassification, DataCollatorForWholeWordMask, DataCollatorWithPadding, DefaultDataCollator, default_data_collator, ) from .metrics import glue_compute_metrics, xnli_compute_metrics from .processors import ( DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor, SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels, squad_convert_examples_to_features, xnli_output_modes, xnli_processors, xnli_tasks_num_labels, )
699
1
from pathlib import Path import numpy as np from PIL import Image def UpperCamelCase__ ( A__ ) -> np.ndarray: snake_case__ , snake_case__ , snake_case__ : Any = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2] return 0.2_9_8_9 * r + 0.5_8_7_0 * g + 0.1_1_4_0 * b def UpperCamelCase__ ( A__ ) -> np.ndarray: return (gray > 127) & (gray <= 255) def UpperCamelCase__ ( A__ , A__ ) -> np.ndarray: snake_case__ : Optional[Any] = np.zeros_like(A__ ) snake_case__ : Optional[Any] = np.zeros( (image.shape[0] + kernel.shape[0] - 1, image.shape[1] + kernel.shape[1] - 1) ) # Copy image to padded image snake_case__ : List[Any] = image # Iterate over image & apply kernel for x in range(image.shape[1] ): for y in range(image.shape[0] ): snake_case__ : Tuple = ( kernel * image_padded[y : y + kernel.shape[0], x : x + kernel.shape[1]] ).sum() snake_case__ : List[str] = int(summation > 0 ) return output if __name__ == "__main__": # read original image lowerCAmelCase__ : Optional[Any] = Path(__file__).resolve().parent / '''image_data''' / '''lena.jpg''' lowerCAmelCase__ : Optional[Any] = np.array(Image.open(lena_path)) # kernel to be applied lowerCAmelCase__ : Dict = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]]) lowerCAmelCase__ : Any = dilation(gray_to_binary(rgb_to_gray(lena)), structuring_element) # Save the output image lowerCAmelCase__ : int = Image.fromarray(output).convert('''RGB''') pil_img.save('''result_dilation.png''')
699
from dataclasses import dataclass, field from typing import Optional from transformers import AutoConfig, AutoImageProcessor, AutoTokenizer, FlaxVisionEncoderDecoderModel, HfArgumentParser @dataclass class __snake_case : __lowerCamelCase = field( metadata={"""help""": """The output directory where the model will be written."""} ,) __lowerCamelCase = field( metadata={ """help""": ( """The encoder model checkpoint for weights initialization.""" """Don't set if you want to train an encoder model from scratch.""" ) } ,) __lowerCamelCase = field( metadata={ """help""": ( """The decoder model checkpoint for weights initialization.""" """Don't set if you want to train a decoder model from scratch.""" ) } ,) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained encoder config name or path if not the same as encoder_model_name"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained decoder config name or path if not the same as decoder_model_name"""} ) def UpperCamelCase__ ( ) -> Union[str, Any]: snake_case__ : str = HfArgumentParser((ModelArguments,) ) ((snake_case__) , ) : Dict = parser.parse_args_into_dataclasses() # Load pretrained model and tokenizer # Use explicit specified encoder config if model_args.encoder_config_name: snake_case__ : List[str] = AutoConfig.from_pretrained(model_args.encoder_config_name ) # Use pretrained encoder model's config else: snake_case__ : Optional[int] = AutoConfig.from_pretrained(model_args.encoder_model_name_or_path ) # Use explicit specified decoder config if model_args.decoder_config_name: snake_case__ : Optional[Any] = AutoConfig.from_pretrained(model_args.decoder_config_name ) # Use pretrained decoder model's config else: snake_case__ : List[str] = AutoConfig.from_pretrained(model_args.decoder_model_name_or_path ) # necessary for `from_encoder_decoder_pretrained` when `decoder_config` is passed snake_case__ : Any = True snake_case__ : Dict = True snake_case__ : Tuple = FlaxVisionEncoderDecoderModel.from_encoder_decoder_pretrained( encoder_pretrained_model_name_or_path=model_args.encoder_model_name_or_path , decoder_pretrained_model_name_or_path=model_args.decoder_model_name_or_path , encoder_config=A__ , decoder_config=A__ , ) # GPT2 only has bos/eos tokens but not decoder_start/pad tokens snake_case__ : Optional[Any] = decoder_config.decoder_start_token_id snake_case__ : Tuple = decoder_config.pad_token_id if decoder_start_token_id is None: snake_case__ : Optional[Any] = decoder_config.bos_token_id if pad_token_id is None: snake_case__ : int = decoder_config.eos_token_id # This is necessary to make Flax's generate() work snake_case__ : Union[str, Any] = decoder_config.eos_token_id snake_case__ : Optional[int] = decoder_start_token_id snake_case__ : int = pad_token_id snake_case__ : Tuple = AutoImageProcessor.from_pretrained(model_args.encoder_model_name_or_path ) snake_case__ : int = AutoTokenizer.from_pretrained(model_args.decoder_model_name_or_path ) snake_case__ : Tuple = tokenizer.convert_ids_to_tokens(model.config.pad_token_id ) model.save_pretrained(model_args.output_dir ) image_processor.save_pretrained(model_args.output_dir ) tokenizer.save_pretrained(model_args.output_dir ) if __name__ == "__main__": main()
699
1
def UpperCamelCase__ ( A__ ) -> "list[int]": if upper_limit < 0: raise ValueError('Limit for the Catalan sequence must be ≥ 0' ) snake_case__ : List[str] = [0] * (upper_limit + 1) # Base case: C(0) = C(1) = 1 snake_case__ : int = 1 if upper_limit > 0: snake_case__ : Tuple = 1 # Recurrence relation: C(i) = sum(C(j).C(i-j-1)), from j = 0 to i for i in range(2 , upper_limit + 1 ): for j in range(A__ ): catalan_list[i] += catalan_list[j] * catalan_list[i - j - 1] return catalan_list if __name__ == "__main__": print('''\n********* Catalan Numbers Using Dynamic Programming ************\n''') print('''\n*** Enter -1 at any time to quit ***''') print('''\nEnter the upper limit (≥ 0) for the Catalan number sequence: ''', end='''''') try: while True: lowerCAmelCase__ : Any = int(input().strip()) if N < 0: print('''\n********* Goodbye!! ************''') break else: print(F'''The Catalan numbers from 0 through {N} are:''') print(catalan_numbers(N)) print('''Try another upper limit for the sequence: ''', end='''''') except (NameError, ValueError): print('''\n********* Invalid input, goodbye! ************\n''') import doctest doctest.testmod()
699
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import datasets import numpy as np import tensorflow as tf from transformers import ( AutoConfig, AutoTokenizer, EvalPrediction, HfArgumentParser, PreTrainedTokenizer, TFAutoModelForSequenceClassification, TFTrainer, TFTrainingArguments, ) from transformers.utils import logging as hf_logging hf_logging.set_verbosity_info() hf_logging.enable_default_handler() hf_logging.enable_explicit_format() def UpperCamelCase__ ( A__ , A__ , A__ , A__ , A__ , A__ = None , ) -> Optional[int]: snake_case__ : List[str] = {} if train_file is not None: snake_case__ : Tuple = [train_file] if eval_file is not None: snake_case__ : Dict = [eval_file] if test_file is not None: snake_case__ : str = [test_file] snake_case__ : Optional[Any] = datasets.load_dataset('csv' , data_files=A__ ) snake_case__ : Any = list(ds[list(files.keys() )[0]].features.keys() ) snake_case__ : Optional[Any] = features_name.pop(A__ ) snake_case__ : Optional[Any] = list(set(ds[list(files.keys() )[0]][label_name] ) ) snake_case__ : str = {label: i for i, label in enumerate(A__ )} snake_case__ : int = tokenizer.model_input_names snake_case__ : int = {} if len(A__ ) == 1: for k in files.keys(): snake_case__ : str = ds[k].map( lambda A__ : tokenizer.batch_encode_plus( example[features_name[0]] , truncation=A__ , max_length=A__ , padding='max_length' ) , batched=A__ , ) elif len(A__ ) == 2: for k in files.keys(): snake_case__ : Optional[int] = ds[k].map( lambda A__ : tokenizer.batch_encode_plus( (example[features_name[0]], example[features_name[1]]) , truncation=A__ , max_length=A__ , padding='max_length' , ) , batched=A__ , ) def gen_train(): for ex in transformed_ds[datasets.Split.TRAIN]: snake_case__ : int = {k: v for k, v in ex.items() if k in input_names} snake_case__ : Any = labelaid[ex[label_name]] yield (d, label) def gen_val(): for ex in transformed_ds[datasets.Split.VALIDATION]: snake_case__ : int = {k: v for k, v in ex.items() if k in input_names} snake_case__ : Union[str, Any] = labelaid[ex[label_name]] yield (d, label) def gen_test(): for ex in transformed_ds[datasets.Split.TEST]: snake_case__ : Dict = {k: v for k, v in ex.items() if k in input_names} snake_case__ : List[str] = labelaid[ex[label_name]] yield (d, label) snake_case__ : Any = ( tf.data.Dataset.from_generator( A__ , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.TRAIN in transformed_ds else None ) if train_ds is not None: snake_case__ : str = train_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TRAIN] ) ) ) snake_case__ : Optional[int] = ( tf.data.Dataset.from_generator( A__ , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.VALIDATION in transformed_ds else None ) if val_ds is not None: snake_case__ : Optional[int] = val_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.VALIDATION] ) ) ) snake_case__ : List[str] = ( tf.data.Dataset.from_generator( A__ , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.TEST in transformed_ds else None ) if test_ds is not None: snake_case__ : str = test_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TEST] ) ) ) return train_ds, val_ds, test_ds, labelaid lowerCAmelCase__ : List[str] = logging.getLogger(__name__) @dataclass class __snake_case : __lowerCamelCase = field(metadata={"""help""": """Which column contains the label"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The path of the training file"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The path of the development file"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The path of the test file"""} ) __lowerCamelCase = field( default=128 ,metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } ,) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Overwrite the cached training and evaluation sets"""} ) @dataclass class __snake_case : __lowerCamelCase = field( metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """Set this flag to use fast tokenization."""} ) # If you want to tweak more attributes on your tokenizer, you should do it in a distinct script, # or just modify its tokenizer_config.json. __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} ,) def UpperCamelCase__ ( ) -> Union[str, 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. snake_case__ : int = HfArgumentParser((ModelArguments, DataTrainingArguments, TFTrainingArguments) ) snake_case__ , snake_case__ , snake_case__ : Dict = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( F"""Output directory ({training_args.output_dir}) already exists and is not empty. Use""" ' --overwrite_output_dir to overcome.' ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , level=logging.INFO , ) logger.info( F"""n_replicas: {training_args.n_replicas}, distributed training: {bool(training_args.n_replicas > 1 )}, """ F"""16-bits training: {training_args.fpaa}""" ) logger.info(F"""Training/evaluation parameters {training_args}""" ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. snake_case__ : Dict = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) snake_case__ , snake_case__ , snake_case__ , snake_case__ : Dict = get_tfds( train_file=data_args.train_file , eval_file=data_args.dev_file , test_file=data_args.test_file , tokenizer=A__ , label_column_id=data_args.label_column_id , max_seq_length=data_args.max_seq_length , ) snake_case__ : Dict = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=len(A__ ) , labelaid=A__ , idalabel={id: label for label, id in labelaid.items()} , finetuning_task='text-classification' , cache_dir=model_args.cache_dir , ) with training_args.strategy.scope(): snake_case__ : Optional[Any] = TFAutoModelForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_pt=bool('.bin' in model_args.model_name_or_path ) , config=A__ , cache_dir=model_args.cache_dir , ) def compute_metrics(A__ ) -> Dict: snake_case__ : Optional[Any] = np.argmax(p.predictions , axis=1 ) return {"acc": (preds == p.label_ids).mean()} # Initialize our Trainer snake_case__ : Any = TFTrainer( model=A__ , args=A__ , train_dataset=A__ , eval_dataset=A__ , compute_metrics=A__ , ) # Training if training_args.do_train: trainer.train() trainer.save_model() tokenizer.save_pretrained(training_args.output_dir ) # Evaluation snake_case__ : Dict = {} if training_args.do_eval: logger.info('*** Evaluate ***' ) snake_case__ : Tuple = trainer.evaluate() snake_case__ : Any = os.path.join(training_args.output_dir , 'eval_results.txt' ) with open(A__ , 'w' ) as writer: logger.info('***** Eval results *****' ) for key, value in result.items(): logger.info(F""" {key} = {value}""" ) writer.write(F"""{key} = {value}\n""" ) results.update(A__ ) return results if __name__ == "__main__": main()
699
1
def UpperCamelCase__ ( A__ , A__ ) -> int: if b == 0: return 1 if (b % 2) == 0: return actual_power(A__ , int(b / 2 ) ) * actual_power(A__ , int(b / 2 ) ) else: return a * actual_power(A__ , int(b / 2 ) ) * actual_power(A__ , int(b / 2 ) ) def UpperCamelCase__ ( A__ , A__ ) -> float: if b < 0: return 1 / actual_power(A__ , A__ ) return actual_power(A__ , A__ ) if __name__ == "__main__": print(power(-2, -3))
699
from typing import List import datasets from datasets.tasks import AudioClassification from ..folder_based_builder import folder_based_builder lowerCAmelCase__ : List[Any] = datasets.utils.logging.get_logger(__name__) class __snake_case ( folder_based_builder.FolderBasedBuilderConfig ): __lowerCamelCase = None __lowerCamelCase = None class __snake_case ( folder_based_builder.FolderBasedBuilder ): __lowerCamelCase = datasets.Audio() __lowerCamelCase = """audio""" __lowerCamelCase = AudioFolderConfig __lowerCamelCase = 42 # definition at the bottom of the script __lowerCamelCase = AudioClassification(audio_column="""audio""" ,label_column="""label""" ) lowerCAmelCase__ : Tuple = [ '''.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''', ] lowerCAmelCase__ : List[Any] = AUDIO_EXTENSIONS
699
1
import os from distutils.util import strtobool def UpperCamelCase__ ( A__ , A__ ) -> Optional[int]: for e in env_keys: snake_case__ : Optional[int] = int(os.environ.get(A__ , -1 ) ) if val >= 0: return val return default def UpperCamelCase__ ( A__ , A__=False ) -> Union[str, Any]: snake_case__ : Union[str, Any] = os.environ.get(A__ , str(A__ ) ) return strtobool(A__ ) == 1 # As its name indicates `strtobool` actually returns an int... def UpperCamelCase__ ( A__ , A__="no" ) -> int: snake_case__ : List[Any] = os.environ.get(A__ , str(A__ ) ) return value
699
import random import unittest import torch from diffusers import IFInpaintingPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class __snake_case ( _lowerCamelCase ,_lowerCamelCase ,unittest.TestCase ): __lowerCamelCase = IFInpaintingPipeline __lowerCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"""width""", """height"""} __lowerCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __lowerCamelCase = PipelineTesterMixin.required_optional_params - {"""latents"""} def __a ( self ) -> Optional[Any]: '''simple docstring''' return self._get_dummy_components() def __a ( self , __UpperCamelCase , __UpperCamelCase=0 ) -> str: '''simple docstring''' if str(__UpperCamelCase ).startswith('mps' ): snake_case__ : int = torch.manual_seed(__UpperCamelCase ) else: snake_case__ : Union[str, Any] = torch.Generator(device=__UpperCamelCase ).manual_seed(__UpperCamelCase ) snake_case__ : Optional[int] = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) snake_case__ : Dict = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) snake_case__ : Optional[Any] = { 'prompt': 'A painting of a squirrel eating a burger', 'image': image, 'mask_image': mask_image, 'generator': generator, 'num_inference_steps': 2, 'output_type': 'numpy', } return inputs @unittest.skipIf( torch_device != 'cuda' or not is_xformers_available() , reason='XFormers attention is only available with CUDA and `xformers` installed' , ) def __a ( self ) -> List[Any]: '''simple docstring''' self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) def __a ( self ) -> Optional[int]: '''simple docstring''' self._test_save_load_optional_components() @unittest.skipIf(torch_device != 'cuda' , reason='float16 requires CUDA' ) def __a ( self ) -> List[str]: '''simple docstring''' super().test_save_load_floataa(expected_max_diff=1E-1 ) def __a ( self ) -> List[str]: '''simple docstring''' self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def __a ( self ) -> int: '''simple docstring''' self._test_save_load_local() def __a ( self ) -> List[str]: '''simple docstring''' self._test_inference_batch_single_identical( expected_max_diff=1E-2 , )
699
1
import argparse import math import os import torch from neural_compressor.utils.pytorch import load from PIL import Image from transformers import CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, StableDiffusionPipeline, UNetaDConditionModel def UpperCamelCase__ ( ) -> List[Any]: snake_case__ : Optional[Any] = argparse.ArgumentParser() parser.add_argument( '-m' , '--pretrained_model_name_or_path' , type=A__ , default=A__ , required=A__ , help='Path to pretrained model or model identifier from huggingface.co/models.' , ) parser.add_argument( '-c' , '--caption' , type=A__ , default='robotic cat with wings' , help='Text used to generate images.' , ) parser.add_argument( '-n' , '--images_num' , type=A__ , default=4 , help='How much images to generate.' , ) parser.add_argument( '-s' , '--seed' , type=A__ , default=42 , help='Seed for random process.' , ) parser.add_argument( '-ci' , '--cuda_id' , type=A__ , default=0 , help='cuda_id.' , ) snake_case__ : Any = parser.parse_args() return args def UpperCamelCase__ ( A__ , A__ , A__ ) -> Optional[int]: if not len(A__ ) == rows * cols: raise ValueError('The specified number of rows and columns are not correct.' ) snake_case__ , snake_case__ : List[Any] = imgs[0].size snake_case__ : Union[str, Any] = Image.new('RGB' , size=(cols * w, rows * h) ) snake_case__ , snake_case__ : Dict = grid.size for i, img in enumerate(A__ ): grid.paste(A__ , box=(i % cols * w, i // cols * h) ) return grid def UpperCamelCase__ ( A__ , A__="robotic cat with wings" , A__=7.5 , A__=50 , A__=1 , A__=42 , ) -> str: snake_case__ : Tuple = torch.Generator(pipeline.device ).manual_seed(A__ ) snake_case__ : int = pipeline( A__ , guidance_scale=A__ , num_inference_steps=A__ , generator=A__ , num_images_per_prompt=A__ , ).images snake_case__ : Dict = int(math.sqrt(A__ ) ) snake_case__ : Optional[Any] = image_grid(A__ , rows=_rows , cols=num_images_per_prompt // _rows ) return grid, images lowerCAmelCase__ : Union[str, Any] = parse_args() # Load models and create wrapper for stable diffusion lowerCAmelCase__ : Any = CLIPTokenizer.from_pretrained(args.pretrained_model_name_or_path, subfolder='''tokenizer''') lowerCAmelCase__ : Optional[Any] = CLIPTextModel.from_pretrained(args.pretrained_model_name_or_path, subfolder='''text_encoder''') lowerCAmelCase__ : Any = AutoencoderKL.from_pretrained(args.pretrained_model_name_or_path, subfolder='''vae''') lowerCAmelCase__ : str = UNetaDConditionModel.from_pretrained(args.pretrained_model_name_or_path, subfolder='''unet''') lowerCAmelCase__ : Tuple = StableDiffusionPipeline.from_pretrained( args.pretrained_model_name_or_path, text_encoder=text_encoder, vae=vae, unet=unet, tokenizer=tokenizer ) lowerCAmelCase__ : Dict = lambda images, clip_input: (images, False) if os.path.exists(os.path.join(args.pretrained_model_name_or_path, '''best_model.pt''')): lowerCAmelCase__ : Union[str, Any] = load(args.pretrained_model_name_or_path, model=unet) unet.eval() setattr(pipeline, '''unet''', unet) else: lowerCAmelCase__ : Any = unet.to(torch.device('''cuda''', args.cuda_id)) lowerCAmelCase__ : List[str] = pipeline.to(unet.device) lowerCAmelCase__, lowerCAmelCase__ : List[Any] = generate_images(pipeline, prompt=args.caption, num_images_per_prompt=args.images_num, seed=args.seed) grid.save(os.path.join(args.pretrained_model_name_or_path, '''{}.png'''.format('''_'''.join(args.caption.split())))) lowerCAmelCase__ : Tuple = os.path.join(args.pretrained_model_name_or_path, '''_'''.join(args.caption.split())) os.makedirs(dirname, exist_ok=True) for idx, image in enumerate(images): image.save(os.path.join(dirname, '''{}.png'''.format(idx + 1)))
699
import unittest from transformers import BertGenerationTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin lowerCAmelCase__ : List[Any] = '''▁''' lowerCAmelCase__ : int = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece class __snake_case ( _lowerCamelCase ,unittest.TestCase ): __lowerCamelCase = BertGenerationTokenizer __lowerCamelCase = False __lowerCamelCase = True def __a ( self ) -> Optional[int]: '''simple docstring''' super().setUp() snake_case__ : str = BertGenerationTokenizer(__UpperCamelCase , keep_accents=__UpperCamelCase ) tokenizer.save_pretrained(self.tmpdirname ) def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : List[str] = '<s>' snake_case__ : Dict = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__UpperCamelCase ) , __UpperCamelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__UpperCamelCase ) , __UpperCamelCase ) def __a ( self ) -> List[str]: '''simple docstring''' snake_case__ : Tuple = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '<unk>' ) self.assertEqual(vocab_keys[1] , '<s>' ) self.assertEqual(vocab_keys[-1] , '<pad>' ) self.assertEqual(len(__UpperCamelCase ) , 1002 ) def __a ( self ) -> int: '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 1000 ) def __a ( self ) -> Tuple: '''simple docstring''' snake_case__ : Optional[Any] = BertGenerationTokenizer(__UpperCamelCase , keep_accents=__UpperCamelCase ) snake_case__ : int = tokenizer.tokenize('This is a test' ) self.assertListEqual(__UpperCamelCase , ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__UpperCamelCase ) , [285, 46, 10, 170, 382] , ) snake_case__ : Any = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( __UpperCamelCase , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '9', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', 'é', '.', ] , ) snake_case__ : Optional[Any] = tokenizer.convert_tokens_to_ids(__UpperCamelCase ) self.assertListEqual( __UpperCamelCase , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) snake_case__ : int = tokenizer.convert_ids_to_tokens(__UpperCamelCase ) self.assertListEqual( __UpperCamelCase , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '<unk>', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', '<unk>', '.', ] , ) @cached_property def __a ( self ) -> Dict: '''simple docstring''' return BertGenerationTokenizer.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder' ) @slow def __a ( self ) -> Any: '''simple docstring''' snake_case__ : int = 'Hello World!' snake_case__ : Union[str, Any] = [18536, 2260, 101] self.assertListEqual(__UpperCamelCase , self.big_tokenizer.encode(__UpperCamelCase ) ) @slow def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : str = ( 'This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will' ' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth' ) snake_case__ : List[Any] = [ 871, 419, 358, 946, 991, 2521, 452, 358, 1357, 387, 7751, 3536, 112, 985, 456, 126, 865, 938, 5400, 5734, 458, 1368, 467, 786, 2462, 5246, 1159, 633, 865, 4519, 457, 582, 852, 2557, 427, 916, 508, 405, 34324, 497, 391, 408, 11342, 1244, 385, 100, 938, 985, 456, 574, 362, 12597, 3200, 3129, 1172, ] self.assertListEqual(__UpperCamelCase , self.big_tokenizer.encode(__UpperCamelCase ) ) @require_torch @slow def __a ( self ) -> List[str]: '''simple docstring''' import torch from transformers import BertGenerationConfig, BertGenerationEncoder # Build sequence snake_case__ : Optional[int] = list(self.big_tokenizer.get_vocab().keys() )[:10] snake_case__ : Optional[int] = ' '.join(__UpperCamelCase ) snake_case__ : int = self.big_tokenizer.encode_plus(__UpperCamelCase , return_tensors='pt' , return_token_type_ids=__UpperCamelCase ) snake_case__ : Tuple = self.big_tokenizer.batch_encode_plus( [sequence + ' ' + sequence] , return_tensors='pt' , return_token_type_ids=__UpperCamelCase ) snake_case__ : Dict = BertGenerationConfig() snake_case__ : List[str] = BertGenerationEncoder(__UpperCamelCase ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**__UpperCamelCase ) model(**__UpperCamelCase ) @slow def __a ( self ) -> Dict: '''simple docstring''' snake_case__ : Optional[int] = {'input_ids': [[39286, 458, 36335, 2001, 456, 13073, 13266, 455, 113, 7746, 1741, 11157, 391, 13073, 13266, 455, 113, 3967, 35412, 113, 4936, 109, 3870, 2377, 113, 30084, 45720, 458, 134, 17496, 112, 503, 11672, 113, 118, 112, 5665, 13347, 38687, 112, 1496, 31389, 112, 3268, 47264, 134, 962, 112, 16377, 8035, 23130, 430, 12169, 15518, 28592, 458, 146, 41697, 109, 391, 12169, 15518, 16689, 458, 146, 41358, 109, 452, 726, 4034, 111, 763, 35412, 5082, 388, 1903, 111, 9051, 391, 2870, 48918, 1900, 1123, 550, 998, 112, 9586, 15985, 455, 391, 410, 22955, 37636, 114], [448, 17496, 419, 3663, 385, 763, 113, 27533, 2870, 3283, 13043, 1639, 24713, 523, 656, 24013, 18550, 2521, 517, 27014, 21244, 420, 1212, 1465, 391, 927, 4833, 388, 578, 11786, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [484, 2169, 7687, 21932, 18146, 726, 363, 17032, 3391, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=__UpperCamelCase , model_name='google/bert_for_seq_generation_L-24_bbc_encoder' , revision='c817d1fd1be2ffa69431227a1fe320544943d4db' , )
699
1
import argparse import torch from transformers import OpenAIGPTConfig, OpenAIGPTModel, load_tf_weights_in_openai_gpt from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() def UpperCamelCase__ ( A__ , A__ , A__ ) -> str: # Construct model if openai_config_file == "": snake_case__ : Tuple = OpenAIGPTConfig() else: snake_case__ : str = OpenAIGPTConfig.from_json_file(A__ ) snake_case__ : str = OpenAIGPTModel(A__ ) # Load weights from numpy load_tf_weights_in_openai_gpt(A__ , A__ , A__ ) # Save pytorch-model snake_case__ : int = pytorch_dump_folder_path + '/' + WEIGHTS_NAME snake_case__ : Dict = pytorch_dump_folder_path + '/' + CONFIG_NAME print(F"""Save PyTorch model to {pytorch_weights_dump_path}""" ) torch.save(model.state_dict() , A__ ) print(F"""Save configuration file to {pytorch_config_dump_path}""" ) with open(A__ , 'w' , encoding='utf-8' ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": lowerCAmelCase__ : List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--openai_checkpoint_folder_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) parser.add_argument( '''--openai_config_file''', default='''''', type=str, help=( '''An optional config json file corresponding to the pre-trained OpenAI model. \n''' '''This specifies the model architecture.''' ), ) lowerCAmelCase__ : Any = parser.parse_args() convert_openai_checkpoint_to_pytorch( args.openai_checkpoint_folder_path, args.openai_config_file, args.pytorch_dump_folder_path )
699
import random import torch from huggingface_hub import HfApi from diffusers import UNetaDModel lowerCAmelCase__ : List[str] = HfApi() lowerCAmelCase__ : str = {} # fmt: off lowerCAmelCase__ : int = torch.tensor([ -0.75_15, -1.68_83, 0.24_20, 0.03_00, 0.63_47, 1.34_33, -1.17_43, -3.74_67, 1.23_42, -2.24_85, 0.46_36, 0.80_76, -0.79_91, 0.39_69, 0.84_98, 0.91_89, -1.88_87, -3.35_22, 0.76_39, 0.20_40, 0.62_71, -2.71_48, -1.63_16, 3.08_39, 0.31_86, 0.27_21, -0.97_59, -1.24_61, 2.62_57, 1.35_57 ]) lowerCAmelCase__ : Dict = torch.tensor([ -2.36_39, -2.53_44, 0.00_54, -0.66_74, 1.59_90, 1.01_58, 0.31_24, -2.14_36, 1.87_95, -2.54_29, -0.15_66, -0.39_73, 1.24_90, 2.64_47, 1.22_83, -0.52_08, -2.81_54, -3.51_19, 2.38_38, 1.20_33, 1.72_01, -2.12_56, -1.45_76, 2.79_48, 2.42_04, -0.97_52, -1.25_46, 0.80_27, 3.27_58, 3.13_65 ]) lowerCAmelCase__ : Dict = torch.tensor([ -0.65_31, -0.68_91, -0.31_72, -0.53_75, -0.91_40, -0.53_67, -0.11_75, -0.78_69, -0.38_08, -0.45_13, -0.20_98, -0.00_83, 0.31_83, 0.51_40, 0.22_47, -0.13_04, -0.13_02, -0.28_02, -0.20_84, -0.20_25, -0.49_67, -0.48_73, -0.08_61, 0.69_25, 0.02_50, 0.12_90, -0.15_43, 0.63_16, 1.04_60, 1.49_43 ]) lowerCAmelCase__ : List[str] = torch.tensor([ 0.09_11, 0.11_07, 0.01_82, 0.04_35, -0.08_05, -0.06_08, 0.03_81, 0.21_72, -0.02_80, 0.13_27, -0.02_99, -0.02_55, -0.00_50, -0.11_70, -0.10_46, 0.03_09, 0.13_67, 0.17_28, -0.05_33, -0.07_48, -0.05_34, 0.16_24, 0.03_84, -0.18_05, -0.07_07, 0.06_42, 0.02_20, -0.01_34, -0.13_33, -0.15_05 ]) lowerCAmelCase__ : Union[str, Any] = torch.tensor([ 0.13_21, 0.13_37, 0.04_40, 0.06_22, -0.05_91, -0.03_70, 0.05_03, 0.21_33, -0.01_77, 0.14_15, -0.01_16, -0.01_12, 0.00_44, -0.09_80, -0.07_89, 0.03_95, 0.15_02, 0.17_85, -0.04_88, -0.05_14, -0.04_04, 0.15_39, 0.04_54, -0.15_59, -0.06_65, 0.06_59, 0.03_83, -0.00_05, -0.12_66, -0.13_86 ]) lowerCAmelCase__ : List[Any] = torch.tensor([ 0.11_54, 0.12_18, 0.03_07, 0.05_26, -0.07_11, -0.05_41, 0.03_66, 0.20_78, -0.02_67, 0.13_17, -0.02_26, -0.01_93, -0.00_14, -0.10_55, -0.09_02, 0.03_30, 0.13_91, 0.17_09, -0.05_62, -0.06_93, -0.05_60, 0.14_82, 0.03_81, -0.16_83, -0.06_81, 0.06_61, 0.03_31, -0.00_46, -0.12_68, -0.14_31 ]) lowerCAmelCase__ : Optional[Any] = torch.tensor([ 0.11_92, 0.12_40, 0.04_14, 0.06_06, -0.05_57, -0.04_12, 0.04_30, 0.20_42, -0.02_00, 0.13_85, -0.01_15, -0.01_32, 0.00_17, -0.09_65, -0.08_02, 0.03_98, 0.14_33, 0.17_47, -0.04_58, -0.05_33, -0.04_07, 0.15_45, 0.04_19, -0.15_74, -0.06_45, 0.06_26, 0.03_41, -0.00_10, -0.11_99, -0.13_90 ]) lowerCAmelCase__ : List[str] = torch.tensor([ 0.10_75, 0.10_74, 0.02_05, 0.04_31, -0.07_74, -0.06_07, 0.02_98, 0.20_42, -0.03_20, 0.12_67, -0.02_81, -0.02_50, -0.00_64, -0.10_91, -0.09_46, 0.02_90, 0.13_28, 0.16_50, -0.05_80, -0.07_38, -0.05_86, 0.14_40, 0.03_37, -0.17_46, -0.07_12, 0.06_05, 0.02_50, -0.00_99, -0.13_16, -0.14_73 ]) lowerCAmelCase__ : List[str] = torch.tensor([ -1.45_72, -2.04_81, -0.04_14, -0.60_05, 1.41_36, 0.58_48, 0.40_28, -2.73_30, 1.22_12, -2.12_28, 0.21_55, 0.40_39, 0.76_62, 2.05_35, 0.74_77, -0.32_43, -2.17_58, -2.76_48, 1.69_47, 0.70_26, 1.23_38, -1.60_78, -0.86_82, 2.28_10, 1.85_74, -0.57_18, -0.55_86, -0.01_86, 2.34_15, 2.12_51]) lowerCAmelCase__ : List[Any] = torch.tensor([ -1.36_90, -1.97_20, -0.40_90, -0.69_66, 1.46_60, 0.99_38, -0.13_85, -2.73_24, 0.77_36, -1.89_17, 0.29_23, 0.42_93, 0.16_93, 1.41_12, 1.18_87, -0.31_81, -2.21_60, -2.63_81, 1.31_70, 0.81_63, 0.92_40, -1.65_44, -0.60_99, 2.52_59, 1.64_30, -0.90_90, -0.93_92, -0.01_26, 2.42_68, 2.32_66 ]) lowerCAmelCase__ : Tuple = torch.tensor([ -1.35_25, -1.96_28, -0.39_56, -0.68_60, 1.46_64, 1.00_14, -0.12_59, -2.72_12, 0.77_72, -1.88_11, 0.29_96, 0.43_88, 0.17_04, 1.40_29, 1.17_01, -0.30_27, -2.20_53, -2.62_87, 1.33_50, 0.81_31, 0.92_74, -1.62_92, -0.60_98, 2.51_31, 1.65_05, -0.89_58, -0.92_98, -0.01_51, 2.42_57, 2.33_55 ]) lowerCAmelCase__ : List[str] = torch.tensor([ -2.05_85, -2.78_97, -0.28_50, -0.89_40, 1.90_52, 0.57_02, 0.63_45, -3.89_59, 1.59_32, -3.23_19, 0.19_74, 0.02_87, 1.75_66, 2.65_43, 0.83_87, -0.53_51, -3.27_36, -4.33_75, 2.90_29, 1.63_90, 1.46_40, -2.17_01, -1.90_13, 2.93_41, 3.49_81, -0.62_55, -1.16_44, -0.15_91, 3.70_97, 3.20_66 ]) lowerCAmelCase__ : Dict = torch.tensor([ -2.31_39, -2.55_94, -0.01_97, -0.67_85, 1.70_01, 1.16_06, 0.30_75, -2.17_40, 1.80_71, -2.56_30, -0.09_26, -0.38_11, 1.21_16, 2.62_46, 1.27_31, -0.53_98, -2.81_53, -3.61_40, 2.38_93, 1.32_62, 1.62_58, -2.18_56, -1.32_67, 2.83_95, 2.37_79, -1.06_23, -1.24_68, 0.89_59, 3.33_67, 3.22_43 ]) lowerCAmelCase__ : Dict = torch.tensor([ -2.06_28, -2.76_67, -0.20_89, -0.82_63, 2.05_39, 0.59_92, 0.64_95, -3.83_36, 1.60_25, -3.28_17, 0.17_21, -0.06_33, 1.75_16, 2.70_39, 0.81_00, -0.59_08, -3.21_13, -4.43_43, 2.92_57, 1.36_32, 1.55_62, -2.14_89, -1.98_94, 3.05_60, 3.33_96, -0.73_28, -1.04_17, 0.03_83, 3.70_93, 3.23_43 ]) lowerCAmelCase__ : Any = torch.tensor([ -1.45_74, -2.05_69, -0.04_73, -0.61_17, 1.40_18, 0.57_69, 0.41_29, -2.73_44, 1.22_41, -2.13_97, 0.20_00, 0.39_37, 0.76_16, 2.04_53, 0.73_24, -0.33_91, -2.17_46, -2.77_44, 1.69_63, 0.69_21, 1.21_87, -1.61_72, -0.88_77, 2.24_39, 1.84_71, -0.58_39, -0.56_05, -0.04_64, 2.32_50, 2.12_19 ]) # fmt: on lowerCAmelCase__ : Any = api.list_models(filter='''diffusers''') for mod in models: if "google" in mod.author or mod.modelId == "CompVis/ldm-celebahq-256": lowerCAmelCase__ : List[str] = '''/home/patrick/google_checkpoints/''' + mod.modelId.split('''/''')[-1] print(F'''Started running {mod.modelId}!!!''') if mod.modelId.startswith('''CompVis'''): lowerCAmelCase__ : int = UNetaDModel.from_pretrained(local_checkpoint, subfolder='''unet''') else: lowerCAmelCase__ : str = UNetaDModel.from_pretrained(local_checkpoint) torch.manual_seed(0) random.seed(0) lowerCAmelCase__ : Any = torch.randn(1, model.config.in_channels, model.config.sample_size, model.config.sample_size) lowerCAmelCase__ : List[str] = torch.tensor([10] * noise.shape[0]) with torch.no_grad(): lowerCAmelCase__ : int = model(noise, time_step).sample assert torch.allclose( logits[0, 0, 0, :30], results['''_'''.join('''_'''.join(mod.modelId.split('''/''')).split('''-'''))], atol=1E-3 ) print(F'''{mod.modelId} has passed successfully!!!''')
699
1
from typing import Dict, List, Optional, Union import numpy as np from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy lowerCAmelCase__ : str = logging.get_logger(__name__) class __snake_case ( _lowerCamelCase ): def __init__( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , **__UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Optional[int] = feature_size snake_case__ : Optional[int] = sampling_rate snake_case__ : Any = padding_value snake_case__ : List[Any] = kwargs.pop('padding_side' , 'right' ) snake_case__ : List[Any] = kwargs.pop('return_attention_mask' , __UpperCamelCase ) super().__init__(**__UpperCamelCase ) def __a ( self , __UpperCamelCase , __UpperCamelCase = True , __UpperCamelCase = None , __UpperCamelCase = False , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = None , ) -> BatchFeature: '''simple docstring''' if isinstance(__UpperCamelCase , (list, tuple) ) and isinstance(processed_features[0] , (dict, BatchFeature) ): snake_case__ : Dict = { key: [example[key] for example in processed_features] for key in processed_features[0].keys() } # The model's main input name, usually `input_values`, has be passed for padding if self.model_input_names[0] not in processed_features: raise ValueError( 'You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`' F""" to this method that includes {self.model_input_names[0]}, but you provided""" F""" {list(processed_features.keys() )}""" ) snake_case__ : Any = processed_features[self.model_input_names[0]] snake_case__ : Union[str, Any] = ( return_attention_mask if return_attention_mask is not None else self.return_attention_mask ) if len(__UpperCamelCase ) == 0: if return_attention_mask: snake_case__ : Optional[int] = [] return processed_features # If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays # and rebuild them afterwards if no return_tensors is specified # Note that we lose the specific device the tensor may be on for PyTorch snake_case__ : Optional[Any] = required_input[0] if isinstance(__UpperCamelCase , (list, tuple) ): # first_element might be an empty list/tuple in some edge cases so we grab the first non empty element. snake_case__ : Optional[Any] = 0 while len(required_input[index] ) == 0: index += 1 if index < len(__UpperCamelCase ): snake_case__ : Dict = required_input[index][0] if return_tensors is None: if is_tf_tensor(__UpperCamelCase ): snake_case__ : Optional[int] = 'tf' elif is_torch_tensor(__UpperCamelCase ): snake_case__ : str = 'pt' elif isinstance(__UpperCamelCase , (int, float, list, tuple, np.ndarray) ): snake_case__ : str = 'np' else: raise ValueError( F"""type of {first_element} unknown: {type(__UpperCamelCase )}. """ 'Should be one of a python, numpy, pytorch or tensorflow object.' ) for key, value in processed_features.items(): if isinstance(value[0] , (int, float) ): snake_case__ : Union[str, Any] = to_numpy(__UpperCamelCase ) else: snake_case__ : Any = [to_numpy(__UpperCamelCase ) for v in value] # Convert padding_strategy in PaddingStrategy snake_case__ : List[str] = self._get_padding_strategies(padding=__UpperCamelCase , max_length=__UpperCamelCase ) snake_case__ : Tuple = processed_features[self.model_input_names[0]] snake_case__ : str = len(__UpperCamelCase ) if not all(len(__UpperCamelCase ) == batch_size for v in processed_features.values() ): raise ValueError('Some items in the output dictionary have a different batch size than others.' ) snake_case__ : Any = [] for i in range(__UpperCamelCase ): snake_case__ : str = {k: v[i] for k, v in processed_features.items()} # truncation snake_case__ : List[str] = self._truncate( __UpperCamelCase , max_length=__UpperCamelCase , pad_to_multiple_of=__UpperCamelCase , truncation=__UpperCamelCase , ) truncated_inputs.append(__UpperCamelCase ) if padding_strategy == PaddingStrategy.LONGEST: # make sure that `max_length` cannot be longer than the longest truncated length snake_case__ : Any = max(len(input_slice[self.model_input_names[0]] ) for input_slice in truncated_inputs ) snake_case__ : Dict = PaddingStrategy.MAX_LENGTH snake_case__ : int = {} for i in range(__UpperCamelCase ): # padding snake_case__ : Dict = self._pad( truncated_inputs[i] , max_length=__UpperCamelCase , padding_strategy=__UpperCamelCase , pad_to_multiple_of=__UpperCamelCase , return_attention_mask=__UpperCamelCase , ) for key, value in outputs.items(): if key not in batch_outputs: snake_case__ : List[Any] = [] if value.dtype is np.dtype(np.floataa ): snake_case__ : Any = value.astype(np.floataa ) batch_outputs[key].append(__UpperCamelCase ) return BatchFeature(__UpperCamelCase , tensor_type=__UpperCamelCase ) def __a ( self , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = PaddingStrategy.DO_NOT_PAD , __UpperCamelCase = None , __UpperCamelCase = None , ) -> dict: '''simple docstring''' snake_case__ : Optional[Any] = processed_features[self.model_input_names[0]] if padding_strategy == PaddingStrategy.LONGEST: snake_case__ : List[Any] = len(__UpperCamelCase ) if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): snake_case__ : Optional[Any] = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of snake_case__ : int = padding_strategy != PaddingStrategy.DO_NOT_PAD and len(__UpperCamelCase ) < max_length if return_attention_mask and "attention_mask" not in processed_features: snake_case__ : Any = np.ones(len(__UpperCamelCase ) , dtype=np.intaa ) if needs_to_be_padded: snake_case__ : Union[str, Any] = max_length - len(__UpperCamelCase ) if self.padding_side == "right": if return_attention_mask: snake_case__ : str = np.pad( processed_features['attention_mask'] , (0, difference) ) snake_case__ : Any = ((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference) snake_case__ : Optional[int] = np.pad( __UpperCamelCase , __UpperCamelCase , 'constant' , constant_values=self.padding_value ) elif self.padding_side == "left": if return_attention_mask: snake_case__ : List[str] = np.pad( processed_features['attention_mask'] , (difference, 0) ) snake_case__ : Any = ((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0) snake_case__ : str = np.pad( __UpperCamelCase , __UpperCamelCase , 'constant' , constant_values=self.padding_value ) else: raise ValueError('Invalid padding strategy:' + str(self.padding_side ) ) return processed_features def __a ( self , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = None , ) -> Optional[int]: '''simple docstring''' if not truncation: return processed_features elif truncation and max_length is None: raise ValueError('When setting ``truncation=True``, make sure that ``max_length`` is defined.' ) snake_case__ : Any = processed_features[self.model_input_names[0]] # find `max_length` that fits `pad_to_multiple_of` if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): snake_case__ : Optional[int] = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of snake_case__ : Tuple = len(__UpperCamelCase ) > max_length if needs_to_be_truncated: snake_case__ : Optional[Any] = processed_features[self.model_input_names[0]][:max_length] if "attention_mask" in processed_features: snake_case__ : Optional[int] = processed_features['attention_mask'][:max_length] return processed_features def __a ( self , __UpperCamelCase=False , __UpperCamelCase=None ) -> Dict: '''simple docstring''' if padding is not False: if padding is True: snake_case__ : Any = PaddingStrategy.LONGEST # Default to pad to the longest sequence in the batch elif not isinstance(__UpperCamelCase , __UpperCamelCase ): snake_case__ : List[Any] = PaddingStrategy(__UpperCamelCase ) elif isinstance(__UpperCamelCase , __UpperCamelCase ): snake_case__ : Union[str, Any] = padding else: snake_case__ : List[str] = PaddingStrategy.DO_NOT_PAD # Set max length if needed if max_length is None: if padding_strategy == PaddingStrategy.MAX_LENGTH: raise ValueError( F"""When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined""" ) # Test if we have a padding value if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None): raise ValueError( 'Asking to pad but the feature_extractor does not have a padding value. Please select a value to use' ' as `padding_value`. For example: `feature_extractor.padding_value = 0.0`.' ) return padding_strategy
699
import warnings from ...utils import logging from .image_processing_perceiver import PerceiverImageProcessor lowerCAmelCase__ : Dict = logging.get_logger(__name__) class __snake_case ( _lowerCamelCase ): def __init__( self , *__UpperCamelCase , **__UpperCamelCase ) -> None: '''simple docstring''' warnings.warn( 'The class PerceiverFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use PerceiverImageProcessor instead.' , __UpperCamelCase , ) super().__init__(*__UpperCamelCase , **__UpperCamelCase )
699
1
from copy import deepcopy class __snake_case : def __init__( self , __UpperCamelCase = None , __UpperCamelCase = None ) -> None: '''simple docstring''' if arr is None and size is not None: snake_case__ : List[Any] = size snake_case__ : Union[str, Any] = [0] * size elif arr is not None: self.init(__UpperCamelCase ) else: raise ValueError('Either arr or size must be specified' ) def __a ( self , __UpperCamelCase ) -> None: '''simple docstring''' snake_case__ : Optional[int] = len(__UpperCamelCase ) snake_case__ : List[Any] = deepcopy(__UpperCamelCase ) for i in range(1 , self.size ): snake_case__ : List[str] = self.next_(__UpperCamelCase ) if j < self.size: self.tree[j] += self.tree[i] def __a ( self ) -> list[int]: '''simple docstring''' snake_case__ : int = self.tree[:] for i in range(self.size - 1 , 0 , -1 ): snake_case__ : str = self.next_(__UpperCamelCase ) if j < self.size: arr[j] -= arr[i] return arr @staticmethod def __a ( __UpperCamelCase ) -> int: '''simple docstring''' return index + (index & (-index)) @staticmethod def __a ( __UpperCamelCase ) -> int: '''simple docstring''' return index - (index & (-index)) def __a ( self , __UpperCamelCase , __UpperCamelCase ) -> None: '''simple docstring''' if index == 0: self.tree[0] += value return while index < self.size: self.tree[index] += value snake_case__ : Any = self.next_(__UpperCamelCase ) def __a ( self , __UpperCamelCase , __UpperCamelCase ) -> None: '''simple docstring''' self.add(__UpperCamelCase , value - self.get(__UpperCamelCase ) ) def __a ( self , __UpperCamelCase ) -> int: '''simple docstring''' if right == 0: return 0 snake_case__ : Union[str, Any] = self.tree[0] right -= 1 # make right inclusive while right > 0: result += self.tree[right] snake_case__ : Optional[Any] = self.prev(__UpperCamelCase ) return result def __a ( self , __UpperCamelCase , __UpperCamelCase ) -> int: '''simple docstring''' return self.prefix(__UpperCamelCase ) - self.prefix(__UpperCamelCase ) def __a ( self , __UpperCamelCase ) -> int: '''simple docstring''' return self.query(__UpperCamelCase , index + 1 ) def __a ( self , __UpperCamelCase ) -> int: '''simple docstring''' value -= self.tree[0] if value < 0: return -1 snake_case__ : Optional[int] = 1 # Largest power of 2 <= size while j * 2 < self.size: j *= 2 snake_case__ : Any = 0 while j > 0: if i + j < self.size and self.tree[i + j] <= value: value -= self.tree[i + j] i += j j //= 2 return i if __name__ == "__main__": import doctest doctest.testmod()
699
import io import itertools import json from dataclasses import dataclass from typing import Optional import pyarrow as pa import pyarrow.json as paj import datasets from datasets.table import table_cast from datasets.utils.file_utils import readline lowerCAmelCase__ : List[Any] = datasets.utils.logging.get_logger(__name__) @dataclass class __snake_case ( datasets.BuilderConfig ): __lowerCamelCase = None __lowerCamelCase = "utf-8" __lowerCamelCase = None __lowerCamelCase = None __lowerCamelCase = True # deprecated __lowerCamelCase = None # deprecated __lowerCamelCase = 10 << 20 # 10MB __lowerCamelCase = None class __snake_case ( datasets.ArrowBasedBuilder ): __lowerCamelCase = JsonConfig def __a ( self ) -> Optional[Any]: '''simple docstring''' if self.config.block_size is not None: logger.warning('The JSON loader parameter `block_size` is deprecated. Please use `chunksize` instead' ) snake_case__ : str = self.config.block_size if self.config.use_threads is not True: logger.warning( 'The JSON loader parameter `use_threads` is deprecated and doesn\'t have any effect anymore.' ) if self.config.newlines_in_values is not None: raise ValueError('The JSON loader parameter `newlines_in_values` is no longer supported' ) return datasets.DatasetInfo(features=self.config.features ) def __a ( self , __UpperCamelCase ) -> Dict: '''simple docstring''' if not self.config.data_files: raise ValueError(F"""At least one data file must be specified, but got data_files={self.config.data_files}""" ) snake_case__ : int = dl_manager.download_and_extract(self.config.data_files ) if isinstance(__UpperCamelCase , (str, list, tuple) ): snake_case__ : Any = data_files if isinstance(__UpperCamelCase , __UpperCamelCase ): snake_case__ : Optional[Any] = [files] snake_case__ : List[str] = [dl_manager.iter_files(__UpperCamelCase ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'files': files} )] snake_case__ : List[Any] = [] for split_name, files in data_files.items(): if isinstance(__UpperCamelCase , __UpperCamelCase ): snake_case__ : List[Any] = [files] snake_case__ : Any = [dl_manager.iter_files(__UpperCamelCase ) for file in files] splits.append(datasets.SplitGenerator(name=__UpperCamelCase , gen_kwargs={'files': files} ) ) return splits def __a ( self , __UpperCamelCase ) -> pa.Table: '''simple docstring''' if self.config.features is not None: # adding missing columns for column_name in set(self.config.features ) - set(pa_table.column_names ): snake_case__ : List[Any] = self.config.features.arrow_schema.field(__UpperCamelCase ).type snake_case__ : List[str] = pa_table.append_column(__UpperCamelCase , pa.array([None] * len(__UpperCamelCase ) , type=__UpperCamelCase ) ) # more expensive cast to support nested structures with keys in a different order # allows str <-> int/float or str to Audio for example snake_case__ : List[str] = table_cast(__UpperCamelCase , self.config.features.arrow_schema ) return pa_table def __a ( self , __UpperCamelCase ) -> int: '''simple docstring''' for file_idx, file in enumerate(itertools.chain.from_iterable(__UpperCamelCase ) ): # If the file is one json object and if we need to look at the list of items in one specific field if self.config.field is not None: with open(__UpperCamelCase , encoding=self.config.encoding , errors=self.config.encoding_errors ) as f: snake_case__ : Union[str, Any] = json.load(__UpperCamelCase ) # We keep only the field we are interested in snake_case__ : Tuple = dataset[self.config.field] # We accept two format: a list of dicts or a dict of lists if isinstance(__UpperCamelCase , (list, tuple) ): snake_case__ : List[Any] = set().union(*[row.keys() for row in dataset] ) snake_case__ : List[Any] = {col: [row.get(__UpperCamelCase ) for row in dataset] for col in keys} else: snake_case__ : List[Any] = dataset snake_case__ : Dict = pa.Table.from_pydict(__UpperCamelCase ) yield file_idx, self._cast_table(__UpperCamelCase ) # If the file has one json object per line else: with open(__UpperCamelCase , 'rb' ) as f: snake_case__ : Optional[int] = 0 # Use block_size equal to the chunk size divided by 32 to leverage multithreading # Set a default minimum value of 16kB if the chunk size is really small snake_case__ : Tuple = max(self.config.chunksize // 32 , 16 << 10 ) snake_case__ : Optional[Any] = ( self.config.encoding_errors if self.config.encoding_errors is not None else 'strict' ) while True: snake_case__ : Optional[int] = f.read(self.config.chunksize ) if not batch: break # Finish current line try: batch += f.readline() except (AttributeError, io.UnsupportedOperation): batch += readline(__UpperCamelCase ) # PyArrow only accepts utf-8 encoded bytes if self.config.encoding != "utf-8": snake_case__ : int = batch.decode(self.config.encoding , errors=__UpperCamelCase ).encode('utf-8' ) try: while True: try: snake_case__ : List[str] = paj.read_json( io.BytesIO(__UpperCamelCase ) , read_options=paj.ReadOptions(block_size=__UpperCamelCase ) ) break except (pa.ArrowInvalid, pa.ArrowNotImplementedError) as e: if ( isinstance(__UpperCamelCase , pa.ArrowInvalid ) and "straddling" not in str(__UpperCamelCase ) or block_size > len(__UpperCamelCase ) ): raise else: # Increase the block size in case it was too small. # The block size will be reset for the next file. logger.debug( F"""Batch of {len(__UpperCamelCase )} bytes couldn't be parsed with block_size={block_size}. Retrying with block_size={block_size * 2}.""" ) block_size *= 2 except pa.ArrowInvalid as e: try: with open( __UpperCamelCase , encoding=self.config.encoding , errors=self.config.encoding_errors ) as f: snake_case__ : Tuple = json.load(__UpperCamelCase ) except json.JSONDecodeError: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCamelCase )}: {e}""" ) raise e # If possible, parse the file as a list of json objects and exit the loop if isinstance(__UpperCamelCase , __UpperCamelCase ): # list is the only sequence type supported in JSON try: snake_case__ : str = set().union(*[row.keys() for row in dataset] ) snake_case__ : Union[str, Any] = {col: [row.get(__UpperCamelCase ) for row in dataset] for col in keys} snake_case__ : List[str] = pa.Table.from_pydict(__UpperCamelCase ) except (pa.ArrowInvalid, AttributeError) as e: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCamelCase )}: {e}""" ) raise ValueError(F"""Not able to read records in the JSON file at {file}.""" ) from None yield file_idx, self._cast_table(__UpperCamelCase ) break else: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCamelCase )}: {e}""" ) raise ValueError( F"""Not able to read records in the JSON file at {file}. """ F"""You should probably indicate the field of the JSON file containing your records. """ F"""This JSON file contain the following fields: {str(list(dataset.keys() ) )}. """ F"""Select the correct one and provide it as `field='XXX'` to the dataset loading method. """ ) from None # Uncomment for debugging (will print the Arrow table size and elements) # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}") # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows))) yield (file_idx, batch_idx), self._cast_table(__UpperCamelCase ) batch_idx += 1
699
1
from .data_collator import ( DataCollatorForLanguageModeling, DataCollatorForPermutationLanguageModeling, DataCollatorForSeqaSeq, DataCollatorForSOP, DataCollatorForTokenClassification, DataCollatorForWholeWordMask, DataCollatorWithPadding, DefaultDataCollator, default_data_collator, ) from .metrics import glue_compute_metrics, xnli_compute_metrics from .processors import ( DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor, SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels, squad_convert_examples_to_features, xnli_output_modes, xnli_processors, xnli_tasks_num_labels, )
699
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase__ : Any = {'''configuration_xglm''': ['''XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XGLMConfig''']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : str = ['''XGLMTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict = ['''XGLMTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Optional[int] = [ '''XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XGLMForCausalLM''', '''XGLMModel''', '''XGLMPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict = [ '''FlaxXGLMForCausalLM''', '''FlaxXGLMModel''', '''FlaxXGLMPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict = [ '''TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXGLMForCausalLM''', '''TFXGLMModel''', '''TFXGLMPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm import XGLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm_fast import XGLMTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, TFXGLMPreTrainedModel, ) else: import sys lowerCAmelCase__ : Tuple = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
699
1
from typing import Dict from .base import GenericTensor, Pipeline class __snake_case ( _lowerCamelCase ): def __a ( self , __UpperCamelCase=None , __UpperCamelCase=None , __UpperCamelCase=None , **__UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' if tokenize_kwargs is None: snake_case__ : Optional[Any] = {} if truncation is not None: if "truncation" in tokenize_kwargs: raise ValueError( 'truncation parameter defined twice (given as keyword argument as well as in tokenize_kwargs)' ) snake_case__ : int = truncation snake_case__ : Optional[int] = tokenize_kwargs snake_case__ : Optional[int] = {} if return_tensors is not None: snake_case__ : Optional[int] = return_tensors return preprocess_params, {}, postprocess_params def __a ( self , __UpperCamelCase , **__UpperCamelCase ) -> Dict[str, GenericTensor]: '''simple docstring''' snake_case__ : Tuple = self.framework snake_case__ : Optional[int] = self.tokenizer(__UpperCamelCase , return_tensors=__UpperCamelCase , **__UpperCamelCase ) return model_inputs def __a ( self , __UpperCamelCase ) -> Optional[Any]: '''simple docstring''' snake_case__ : str = self.model(**__UpperCamelCase ) return model_outputs def __a ( self , __UpperCamelCase , __UpperCamelCase=False ) -> int: '''simple docstring''' if return_tensors: return model_outputs[0] if self.framework == "pt": return model_outputs[0].tolist() elif self.framework == "tf": return model_outputs[0].numpy().tolist() def __call__( self , *__UpperCamelCase , **__UpperCamelCase ) -> List[Any]: '''simple docstring''' return super().__call__(*__UpperCamelCase , **__UpperCamelCase )
699
from __future__ import annotations import random # Maximum size of the population. Bigger could be faster but is more memory expensive. lowerCAmelCase__ : Dict = 2_00 # Number of elements selected in every generation of evolution. The selection takes # place from best to worst of that generation and must be smaller than N_POPULATION. lowerCAmelCase__ : List[str] = 50 # Probability that an element of a generation can mutate, changing one of its genes. # This will guarantee that all genes will be used during evolution. lowerCAmelCase__ : List[str] = 0.4 # Just a seed to improve randomness required by the algorithm. random.seed(random.randint(0, 10_00)) def UpperCamelCase__ ( A__ , A__ ) -> tuple[str, float]: snake_case__ : Tuple = len([g for position, g in enumerate(A__ ) if g == main_target[position]] ) return (item, float(A__ )) def UpperCamelCase__ ( A__ , A__ ) -> tuple[str, str]: snake_case__ : str = random.randint(0 , len(A__ ) - 1 ) snake_case__ : int = parent_a[:random_slice] + parent_a[random_slice:] snake_case__ : Any = parent_a[:random_slice] + parent_a[random_slice:] return (child_a, child_a) def UpperCamelCase__ ( A__ , A__ ) -> str: snake_case__ : List[Any] = list(A__ ) if random.uniform(0 , 1 ) < MUTATION_PROBABILITY: snake_case__ : Optional[Any] = random.choice(A__ ) return "".join(A__ ) def UpperCamelCase__ ( A__ , A__ , A__ , ) -> list[str]: snake_case__ : Tuple = [] # Generate more children proportionally to the fitness score. snake_case__ : Optional[Any] = int(parent_a[1] * 100 ) + 1 snake_case__ : str = 10 if child_n >= 10 else child_n for _ in range(A__ ): snake_case__ : Any = population_score[random.randint(0 , A__ )][0] snake_case__ , snake_case__ : int = crossover(parent_a[0] , A__ ) # Append new string to the population list. pop.append(mutate(A__ , A__ ) ) pop.append(mutate(A__ , A__ ) ) return pop def UpperCamelCase__ ( A__ , A__ , A__ = True ) -> tuple[int, int, str]: # Verify if N_POPULATION is bigger than N_SELECTED if N_POPULATION < N_SELECTED: snake_case__ : Union[str, Any] = F"""{N_POPULATION} must be bigger than {N_SELECTED}""" raise ValueError(A__ ) # Verify that the target contains no genes besides the ones inside genes variable. snake_case__ : Tuple = sorted({c for c in target if c not in genes} ) if not_in_genes_list: snake_case__ : int = F"""{not_in_genes_list} is not in genes list, evolution cannot converge""" raise ValueError(A__ ) # Generate random starting population. snake_case__ : Union[str, Any] = [] for _ in range(A__ ): population.append(''.join([random.choice(A__ ) for i in range(len(A__ ) )] ) ) # Just some logs to know what the algorithms is doing. snake_case__ , snake_case__ : str = 0, 0 # This loop will end when we find a perfect match for our target. while True: generation += 1 total_population += len(A__ ) # Random population created. Now it's time to evaluate. # Adding a bit of concurrency can make everything faster, # # import concurrent.futures # population_score: list[tuple[str, float]] = [] # with concurrent.futures.ThreadPoolExecutor( # max_workers=NUM_WORKERS) as executor: # futures = {executor.submit(evaluate, item) for item in population} # concurrent.futures.wait(futures) # population_score = [item.result() for item in futures] # # but with a simple algorithm like this, it will probably be slower. # We just need to call evaluate for every item inside the population. snake_case__ : List[Any] = [evaluate(A__ , A__ ) for item in population] # Check if there is a matching evolution. snake_case__ : int = sorted(A__ , key=lambda A__ : x[1] , reverse=A__ ) if population_score[0][0] == target: return (generation, total_population, population_score[0][0]) # Print the best result every 10 generation. # Just to know that the algorithm is working. if debug and generation % 10 == 0: print( F"""\nGeneration: {generation}""" F"""\nTotal Population:{total_population}""" F"""\nBest score: {population_score[0][1]}""" F"""\nBest string: {population_score[0][0]}""" ) # Flush the old population, keeping some of the best evolutions. # Keeping this avoid regression of evolution. snake_case__ : Optional[int] = population[: int(N_POPULATION / 3 )] population.clear() population.extend(A__ ) # Normalize population score to be between 0 and 1. snake_case__ : str = [ (item, score / len(A__ )) for item, score in population_score ] # This is selection for i in range(A__ ): population.extend(select(population_score[int(A__ )] , A__ , A__ ) ) # Check if the population has already reached the maximum value and if so, # break the cycle. If this check is disabled, the algorithm will take # forever to compute large strings, but will also calculate small strings in # a far fewer generations. if len(A__ ) > N_POPULATION: break if __name__ == "__main__": lowerCAmelCase__ : str = ( '''This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!''' ) lowerCAmelCase__ : Optional[Any] = list( ''' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm''' '''nopqrstuvwxyz.,;!?+-*#@^\'èéòà€ù=)(&%$£/\\''' ) lowerCAmelCase__, lowerCAmelCase__, lowerCAmelCase__ : List[str] = basic(target_str, genes_list) print( F'''\nGeneration: {generation}\nTotal Population: {population}\nTarget: {target}''' )
699
1
from __future__ import annotations import pandas as pd def UpperCamelCase__ ( A__ , A__ , A__ ) -> list[int]: snake_case__ : Any = [0] * no_of_processes snake_case__ : Dict = [0] * no_of_processes # Copy the burst time into remaining_time[] for i in range(A__ ): snake_case__ : List[Any] = burst_time[i] snake_case__ : Optional[Any] = 0 snake_case__ : str = 0 snake_case__ : Dict = 9_9999_9999 snake_case__ : Optional[Any] = 0 snake_case__ : int = False # Process until all processes are completed while complete != no_of_processes: for j in range(A__ ): if arrival_time[j] <= increment_time and remaining_time[j] > 0: if remaining_time[j] < minm: snake_case__ : Any = remaining_time[j] snake_case__ : Union[str, Any] = j snake_case__ : str = True if not check: increment_time += 1 continue remaining_time[short] -= 1 snake_case__ : Any = remaining_time[short] if minm == 0: snake_case__ : Optional[int] = 9_9999_9999 if remaining_time[short] == 0: complete += 1 snake_case__ : List[Any] = False # Find finish time of current process snake_case__ : List[Any] = increment_time + 1 # Calculate waiting time snake_case__ : Any = finish_time - arrival_time[short] snake_case__ : Dict = finar - burst_time[short] if waiting_time[short] < 0: snake_case__ : str = 0 # Increment time increment_time += 1 return waiting_time def UpperCamelCase__ ( A__ , A__ , A__ ) -> list[int]: snake_case__ : int = [0] * no_of_processes for i in range(A__ ): snake_case__ : Any = burst_time[i] + waiting_time[i] return turn_around_time def UpperCamelCase__ ( A__ , A__ , A__ ) -> None: snake_case__ : Tuple = 0 snake_case__ : Dict = 0 for i in range(A__ ): snake_case__ : int = total_waiting_time + waiting_time[i] snake_case__ : List[str] = total_turn_around_time + turn_around_time[i] print(F"""Average waiting time = {total_waiting_time / no_of_processes:.5f}""" ) print('Average turn around time =' , total_turn_around_time / no_of_processes ) if __name__ == "__main__": print('''Enter how many process you want to analyze''') lowerCAmelCase__ : List[Any] = int(input()) lowerCAmelCase__ : List[Any] = [0] * no_of_processes lowerCAmelCase__ : str = [0] * no_of_processes lowerCAmelCase__ : Optional[int] = list(range(1, no_of_processes + 1)) for i in range(no_of_processes): print('''Enter the arrival time and burst time for process:--''' + str(i + 1)) lowerCAmelCase__, lowerCAmelCase__ : Dict = map(int, input().split()) lowerCAmelCase__ : int = calculate_waitingtime(arrival_time, burst_time, no_of_processes) lowerCAmelCase__ : Optional[Any] = burst_time lowerCAmelCase__ : str = no_of_processes lowerCAmelCase__ : List[str] = waiting_time lowerCAmelCase__ : Tuple = calculate_turnaroundtime(bt, n, wt) calculate_average_times(waiting_time, turn_around_time, no_of_processes) lowerCAmelCase__ : Optional[int] = pd.DataFrame( list(zip(processes, burst_time, arrival_time, waiting_time, turn_around_time)), columns=[ '''Process''', '''BurstTime''', '''ArrivalTime''', '''WaitingTime''', '''TurnAroundTime''', ], ) # Printing the dataFrame pd.set_option('''display.max_rows''', fcfs.shape[0] + 1) print(fcfs)
699
from __future__ import annotations from collections.abc import Iterator from typing import Generic, TypeVar lowerCAmelCase__ : Optional[int] = TypeVar('''T''') class __snake_case ( Generic[T] ): def __init__( self , __UpperCamelCase ) -> Any: '''simple docstring''' snake_case__ : Optional[int] = data snake_case__ : Node[T] | None = None def __str__( self ) -> str: '''simple docstring''' return F"""{self.data}""" class __snake_case ( Generic[T] ): def __init__( self ) -> None: '''simple docstring''' snake_case__ : Node[T] | None = None def __iter__( self ) -> Iterator[T]: '''simple docstring''' snake_case__ : str = self.top while node: yield node.data snake_case__ : Dict = node.next def __str__( self ) -> str: '''simple docstring''' return "->".join([str(__UpperCamelCase ) for item in self] ) def __len__( self ) -> int: '''simple docstring''' return len(tuple(iter(self ) ) ) def __a ( self ) -> bool: '''simple docstring''' return self.top is None def __a ( self , __UpperCamelCase ) -> None: '''simple docstring''' snake_case__ : str = Node(__UpperCamelCase ) if not self.is_empty(): snake_case__ : List[str] = self.top snake_case__ : Tuple = node def __a ( self ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError('pop from empty stack' ) assert isinstance(self.top , __UpperCamelCase ) snake_case__ : List[str] = self.top snake_case__ : Union[str, Any] = self.top.next return pop_node.data def __a ( self ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError('peek from empty stack' ) assert self.top is not None return self.top.data def __a ( self ) -> None: '''simple docstring''' snake_case__ : Any = None if __name__ == "__main__": from doctest import testmod testmod()
699
1
from math import isqrt def UpperCamelCase__ ( A__ ) -> bool: return all(number % divisor != 0 for divisor in range(2 , isqrt(A__ ) + 1 ) ) def UpperCamelCase__ ( A__ = 10**6 ) -> int: snake_case__ : List[str] = 0 snake_case__ : List[str] = 1 snake_case__ : Tuple = 7 while prime_candidate < max_prime: primes_count += is_prime(A__ ) cube_index += 1 prime_candidate += 6 * cube_index return primes_count if __name__ == "__main__": print(F'''{solution() = }''')
699
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCAmelCase__ : Dict = logging.get_logger(__name__) lowerCAmelCase__ : int = { '''sail/poolformer_s12''': '''https://huggingface.co/sail/poolformer_s12/resolve/main/config.json''', # See all PoolFormer models at https://huggingface.co/models?filter=poolformer } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = """poolformer""" def __init__( self , __UpperCamelCase=3 , __UpperCamelCase=16 , __UpperCamelCase=16 , __UpperCamelCase=3 , __UpperCamelCase=4.0 , __UpperCamelCase=[2, 2, 6, 2] , __UpperCamelCase=[64, 128, 320, 512] , __UpperCamelCase=[7, 3, 3, 3] , __UpperCamelCase=[4, 2, 2, 2] , __UpperCamelCase=[2, 1, 1, 1] , __UpperCamelCase=4 , __UpperCamelCase=0.0 , __UpperCamelCase="gelu" , __UpperCamelCase=True , __UpperCamelCase=1E-5 , __UpperCamelCase=0.0_2 , **__UpperCamelCase , ) -> Any: '''simple docstring''' snake_case__ : List[str] = num_channels snake_case__ : Dict = patch_size snake_case__ : Optional[int] = stride snake_case__ : str = padding snake_case__ : List[str] = pool_size snake_case__ : List[Any] = hidden_sizes snake_case__ : List[Any] = mlp_ratio snake_case__ : Union[str, Any] = depths snake_case__ : Dict = patch_sizes snake_case__ : Dict = strides snake_case__ : Dict = num_encoder_blocks snake_case__ : Union[str, Any] = drop_path_rate snake_case__ : List[str] = hidden_act snake_case__ : Optional[Any] = use_layer_scale snake_case__ : int = layer_scale_init_value snake_case__ : Dict = initializer_range super().__init__(**__UpperCamelCase ) class __snake_case ( _lowerCamelCase ): __lowerCamelCase = version.parse("""1.11""" ) @property def __a ( self ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def __a ( self ) -> float: '''simple docstring''' return 2E-3
699
1
class __snake_case : def __init__( self , __UpperCamelCase ) -> None: '''simple docstring''' snake_case__ : Tuple = len(__UpperCamelCase ) snake_case__ : List[Any] = [0] * len_array if len_array > 0: snake_case__ : Union[str, Any] = array[0] for i in range(1 , __UpperCamelCase ): snake_case__ : Dict = self.prefix_sum[i - 1] + array[i] def __a ( self , __UpperCamelCase , __UpperCamelCase ) -> int: '''simple docstring''' if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def __a ( self , __UpperCamelCase ) -> bool: '''simple docstring''' snake_case__ : List[str] = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(__UpperCamelCase ) return False if __name__ == "__main__": import doctest doctest.testmod()
699
import numpy as np import qiskit def UpperCamelCase__ ( A__ = 8 , A__ = None ) -> str: snake_case__ : Optional[int] = np.random.default_rng(seed=A__ ) # Roughly 25% of the qubits will contribute to the key. # So we take more than we need. snake_case__ : Tuple = 6 * key_len # Measurement basis for Alice's qubits. snake_case__ : Tuple = rng.integers(2 , size=A__ ) # The set of states Alice will prepare. snake_case__ : List[str] = rng.integers(2 , size=A__ ) # Measurement basis for Bob's qubits. snake_case__ : List[Any] = rng.integers(2 , size=A__ ) # Quantum Circuit to simulate BB84 snake_case__ : Any = qiskit.QuantumCircuit(A__ , name='BB84' ) # Alice prepares her qubits according to rules above. for index, _ in enumerate(A__ ): if alice_state[index] == 1: bbaa_circ.x(A__ ) if alice_basis[index] == 1: bbaa_circ.h(A__ ) bbaa_circ.barrier() # Bob measures the received qubits according to rules above. for index, _ in enumerate(A__ ): if bob_basis[index] == 1: bbaa_circ.h(A__ ) bbaa_circ.barrier() bbaa_circ.measure_all() # Simulate the quantum circuit. snake_case__ : List[str] = qiskit.Aer.get_backend('aer_simulator' ) # We only need to run one shot because the key is unique. # Multiple shots will produce the same key. snake_case__ : Optional[Any] = qiskit.execute(A__ , A__ , shots=1 , seed_simulator=A__ ) # Returns the result of measurement. snake_case__ : Union[str, Any] = job.result().get_counts(A__ ).most_frequent() # Extracting the generated key from the simulation results. # Only keep measurement results where Alice and Bob chose the same basis. snake_case__ : Optional[Any] = ''.join( [ result_bit for alice_basis_bit, bob_basis_bit, result_bit in zip( A__ , A__ , A__ ) if alice_basis_bit == bob_basis_bit ] ) # Get final key. Pad with 0 if too short, otherwise truncate. snake_case__ : Tuple = gen_key[:key_len] if len(A__ ) >= key_len else gen_key.ljust(A__ , '0' ) return key if __name__ == "__main__": print(F'''The generated key is : {bbaa(8, seed=0)}''') from doctest import testmod testmod()
699
1
from __future__ import annotations import unittest from transformers import XGLMConfig, XGLMTokenizer, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers.models.xglm.modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, ) @require_tf class __snake_case : __lowerCamelCase = XGLMConfig __lowerCamelCase = {} __lowerCamelCase = """gelu""" def __init__( self , __UpperCamelCase , __UpperCamelCase=14 , __UpperCamelCase=7 , __UpperCamelCase=True , __UpperCamelCase=True , __UpperCamelCase=True , __UpperCamelCase=99 , __UpperCamelCase=32 , __UpperCamelCase=2 , __UpperCamelCase=4 , __UpperCamelCase=37 , __UpperCamelCase="gelu" , __UpperCamelCase=0.1 , __UpperCamelCase=0.1 , __UpperCamelCase=512 , __UpperCamelCase=0.0_2 , ) -> List[Any]: '''simple docstring''' snake_case__ : List[str] = parent snake_case__ : List[Any] = batch_size snake_case__ : Any = seq_length snake_case__ : Union[str, Any] = is_training snake_case__ : Optional[int] = use_input_mask snake_case__ : Any = use_labels snake_case__ : List[str] = vocab_size snake_case__ : Tuple = d_model snake_case__ : List[Any] = num_hidden_layers snake_case__ : Dict = num_attention_heads snake_case__ : Dict = ffn_dim snake_case__ : Optional[Any] = activation_function snake_case__ : Any = activation_dropout snake_case__ : Tuple = attention_dropout snake_case__ : Union[str, Any] = max_position_embeddings snake_case__ : List[Any] = initializer_range snake_case__ : str = None snake_case__ : Union[str, Any] = 0 snake_case__ : str = 2 snake_case__ : Any = 1 def __a ( self ) -> Optional[int]: '''simple docstring''' return XGLMConfig.from_pretrained('facebook/xglm-564M' ) def __a ( self ) -> Any: '''simple docstring''' snake_case__ : Any = tf.clip_by_value( ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) , clip_value_min=0 , clip_value_max=3 ) snake_case__ : Optional[Any] = None if self.use_input_mask: snake_case__ : List[Any] = random_attention_mask([self.batch_size, self.seq_length] ) snake_case__ : str = self.get_config() snake_case__ : int = floats_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, input_mask, head_mask, ) def __a ( self ) -> Dict: '''simple docstring''' return XGLMConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , num_layers=self.num_hidden_layers , attention_heads=self.num_attention_heads , ffn_dim=self.ffn_dim , activation_function=self.activation_function , activation_dropout=self.activation_dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , use_cache=__UpperCamelCase , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , return_dict=__UpperCamelCase , ) def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : List[str] = self.prepare_config_and_inputs() ( ( snake_case__ ) , ( snake_case__ ) , ( snake_case__ ) , ( snake_case__ ) , ) : int = config_and_inputs snake_case__ : Any = { 'input_ids': input_ids, 'head_mask': head_mask, } return config, inputs_dict @require_tf class __snake_case ( _lowerCamelCase ,_lowerCamelCase ,unittest.TestCase ): __lowerCamelCase = (TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else () __lowerCamelCase = (TFXGLMForCausalLM,) if is_tf_available() else () __lowerCamelCase = ( {"""feature-extraction""": TFXGLMModel, """text-generation""": TFXGLMForCausalLM} if is_tf_available() else {} ) __lowerCamelCase = False __lowerCamelCase = False __lowerCamelCase = False def __a ( self ) -> int: '''simple docstring''' snake_case__ : List[Any] = TFXGLMModelTester(self ) snake_case__ : int = ConfigTester(self , config_class=__UpperCamelCase , n_embd=37 ) def __a ( self ) -> Any: '''simple docstring''' self.config_tester.run_common_tests() @slow def __a ( self ) -> List[Any]: '''simple docstring''' for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case__ : str = TFXGLMModel.from_pretrained(__UpperCamelCase ) self.assertIsNotNone(__UpperCamelCase ) @unittest.skip(reason='Currently, model embeddings are going to undergo a major refactor.' ) def __a ( self ) -> Union[str, Any]: '''simple docstring''' super().test_resize_token_embeddings() @require_tf class __snake_case ( unittest.TestCase ): @slow def __a ( self , __UpperCamelCase=True ) -> List[Any]: '''simple docstring''' snake_case__ : Union[str, Any] = TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) snake_case__ : Optional[int] = tf.convert_to_tensor([[2, 268, 9865]] , dtype=tf.intaa ) # The dog # </s> The dog is a very friendly dog. He is very affectionate and loves to play with other # fmt: off snake_case__ : str = [2, 268, 9865, 67, 11, 1988, 57252, 9865, 5, 984, 67, 1988, 213838, 1658, 53, 70446, 33, 6657, 278, 1581] # fmt: on snake_case__ : int = model.generate(__UpperCamelCase , do_sample=__UpperCamelCase , num_beams=1 ) if verify_outputs: self.assertListEqual(output_ids[0].numpy().tolist() , __UpperCamelCase ) @slow def __a ( self ) -> List[str]: '''simple docstring''' snake_case__ : List[Any] = XGLMTokenizer.from_pretrained('facebook/xglm-564M' ) snake_case__ : int = TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) tf.random.set_seed(0 ) snake_case__ : Any = tokenizer('Today is a nice day and' , return_tensors='tf' ) snake_case__ : List[str] = tokenized.input_ids # forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices) with tf.device(':/CPU:0' ): snake_case__ : Any = model.generate(__UpperCamelCase , do_sample=__UpperCamelCase , seed=[7, 0] ) snake_case__ : List[str] = tokenizer.decode(output_ids[0] , skip_special_tokens=__UpperCamelCase ) snake_case__ : Union[str, Any] = ( 'Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due' ) self.assertEqual(__UpperCamelCase , __UpperCamelCase ) @slow def __a ( self ) -> Optional[Any]: '''simple docstring''' snake_case__ : int = TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) snake_case__ : List[str] = XGLMTokenizer.from_pretrained('facebook/xglm-564M' ) snake_case__ : str = 'left' # use different length sentences to test batching snake_case__ : Tuple = [ 'This is an extremelly long sentence that only exists to test the ability of the model to cope with ' 'left-padding, such as in batched generation. The output for the sequence below should be the same ' 'regardless of whether left padding is applied or not. When', 'Hello, my dog is a little', ] snake_case__ : Optional[Any] = tokenizer(__UpperCamelCase , return_tensors='tf' , padding=__UpperCamelCase ) snake_case__ : str = inputs['input_ids'] snake_case__ : List[Any] = model.generate(input_ids=__UpperCamelCase , attention_mask=inputs['attention_mask'] , max_new_tokens=12 ) snake_case__ : Dict = tokenizer(sentences[0] , return_tensors='tf' ).input_ids snake_case__ : Tuple = model.generate(input_ids=__UpperCamelCase , max_new_tokens=12 ) snake_case__ : List[Any] = tokenizer(sentences[1] , return_tensors='tf' ).input_ids snake_case__ : Optional[Any] = model.generate(input_ids=__UpperCamelCase , max_new_tokens=12 ) snake_case__ : Optional[int] = tokenizer.batch_decode(__UpperCamelCase , skip_special_tokens=__UpperCamelCase ) snake_case__ : Tuple = tokenizer.decode(output_non_padded[0] , skip_special_tokens=__UpperCamelCase ) snake_case__ : Dict = tokenizer.decode(output_padded[0] , skip_special_tokens=__UpperCamelCase ) snake_case__ : int = [ 'This is an extremelly long sentence that only exists to test the ability of the model to cope with ' 'left-padding, such as in batched generation. The output for the sequence below should be the same ' 'regardless of whether left padding is applied or not. When left padding is applied, the sequence will be ' 'a single', 'Hello, my dog is a little bit of a shy one, but he is very friendly', ] self.assertListEqual(__UpperCamelCase , __UpperCamelCase ) self.assertListEqual(__UpperCamelCase , [non_padded_sentence, padded_sentence] )
699
def UpperCamelCase__ ( A__ , A__ , A__ ) -> int: if exponent == 1: return base if exponent % 2 == 0: snake_case__ : Dict = _modexpt(A__ , exponent // 2 , A__ ) % modulo_value return (x * x) % modulo_value else: return (base * _modexpt(A__ , exponent - 1 , A__ )) % modulo_value def UpperCamelCase__ ( A__ = 1777 , A__ = 1855 , A__ = 8 ) -> int: snake_case__ : Tuple = base for _ in range(1 , A__ ): snake_case__ : Any = _modexpt(A__ , A__ , 10**digits ) return result if __name__ == "__main__": print(F'''{solution() = }''')
699
1
# This is the module that test_patching.py uses to test patch_submodule() import os # noqa: this is just for tests import os as renamed_os # noqa: this is just for tests from os import path # noqa: this is just for tests from os import path as renamed_path # noqa: this is just for tests from os.path import join # noqa: this is just for tests from os.path import join as renamed_join # noqa: this is just for tests lowerCAmelCase__ : List[Any] = open # noqa: we just need to have a builtin inside this module to test it properly
699
# tests directory-specific settings - this file is run automatically # by pytest before any tests are run import sys import warnings from os.path import abspath, dirname, join # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. lowerCAmelCase__ : Tuple = abspath(join(dirname(dirname(__file__)), '''src''')) sys.path.insert(1, git_repo_path) # silence FutureWarning warnings in tests since often we can't act on them until # they become normal warnings - i.e. the tests still need to test the current functionality warnings.simplefilter(action='''ignore''', category=FutureWarning) def UpperCamelCase__ ( A__ ) -> Optional[Any]: from diffusers.utils.testing_utils import pytest_addoption_shared pytest_addoption_shared(A__ ) def UpperCamelCase__ ( A__ ) -> Optional[Any]: from diffusers.utils.testing_utils import pytest_terminal_summary_main snake_case__ : Union[str, Any] = terminalreporter.config.getoption('--make-reports' ) if make_reports: pytest_terminal_summary_main(A__ , id=A__ )
699
1
from __future__ import annotations import inspect import unittest import numpy as np from transformers import DeiTConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, TFDeiTModel, ) from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import DeiTImageProcessor class __snake_case : def __init__( self , __UpperCamelCase , __UpperCamelCase=13 , __UpperCamelCase=30 , __UpperCamelCase=2 , __UpperCamelCase=3 , __UpperCamelCase=True , __UpperCamelCase=True , __UpperCamelCase=32 , __UpperCamelCase=2 , __UpperCamelCase=4 , __UpperCamelCase=37 , __UpperCamelCase="gelu" , __UpperCamelCase=0.1 , __UpperCamelCase=0.1 , __UpperCamelCase=10 , __UpperCamelCase=0.0_2 , __UpperCamelCase=3 , __UpperCamelCase=None , __UpperCamelCase=2 , ) -> Optional[int]: '''simple docstring''' snake_case__ : List[str] = parent snake_case__ : Optional[Any] = batch_size snake_case__ : List[Any] = image_size snake_case__ : int = patch_size snake_case__ : str = num_channels snake_case__ : Tuple = is_training snake_case__ : Any = use_labels snake_case__ : str = hidden_size snake_case__ : List[str] = num_hidden_layers snake_case__ : Dict = num_attention_heads snake_case__ : List[str] = intermediate_size snake_case__ : List[str] = hidden_act snake_case__ : Dict = hidden_dropout_prob snake_case__ : Any = attention_probs_dropout_prob snake_case__ : Optional[Any] = type_sequence_label_size snake_case__ : str = initializer_range snake_case__ : str = scope snake_case__ : Optional[Any] = encoder_stride # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens) snake_case__ : Union[str, Any] = (image_size // patch_size) ** 2 snake_case__ : Dict = num_patches + 2 def __a ( self ) -> Optional[Any]: '''simple docstring''' snake_case__ : Optional[Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) snake_case__ : Optional[Any] = None if self.use_labels: snake_case__ : Any = ids_tensor([self.batch_size] , self.type_sequence_label_size ) snake_case__ : Dict = self.get_config() return config, pixel_values, labels def __a ( self ) -> List[str]: '''simple docstring''' return DeiTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=__UpperCamelCase , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: '''simple docstring''' snake_case__ : int = TFDeiTModel(config=__UpperCamelCase ) snake_case__ : List[Any] = model(__UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Tuple: '''simple docstring''' snake_case__ : Optional[Any] = TFDeiTForMaskedImageModeling(config=__UpperCamelCase ) snake_case__ : Union[str, Any] = model(__UpperCamelCase ) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images snake_case__ : Tuple = 1 snake_case__ : List[str] = TFDeiTForMaskedImageModeling(__UpperCamelCase ) snake_case__ : Any = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) snake_case__ : Union[str, Any] = model(__UpperCamelCase ) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Tuple: '''simple docstring''' snake_case__ : Dict = self.type_sequence_label_size snake_case__ : Optional[int] = TFDeiTForImageClassification(__UpperCamelCase ) snake_case__ : int = model(__UpperCamelCase , labels=__UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images snake_case__ : Any = 1 snake_case__ : List[str] = TFDeiTForImageClassification(__UpperCamelCase ) snake_case__ : Dict = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) snake_case__ : Optional[int] = model(__UpperCamelCase , labels=__UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : str = self.prepare_config_and_inputs() snake_case__ , snake_case__ , snake_case__ : Optional[Any] = config_and_inputs snake_case__ : str = {'pixel_values': pixel_values} return config, inputs_dict @require_tf class __snake_case ( _lowerCamelCase ,_lowerCamelCase ,unittest.TestCase ): __lowerCamelCase = ( ( TFDeiTModel, TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, ) if is_tf_available() else () ) __lowerCamelCase = ( { """feature-extraction""": TFDeiTModel, """image-classification""": (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher), } if is_tf_available() else {} ) __lowerCamelCase = False __lowerCamelCase = False __lowerCamelCase = False __lowerCamelCase = False def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : int = TFDeiTModelTester(self ) snake_case__ : Any = ConfigTester(self , config_class=__UpperCamelCase , has_text_modality=__UpperCamelCase , hidden_size=37 ) def __a ( self ) -> int: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason='DeiT does not use inputs_embeds' ) def __a ( self ) -> Tuple: '''simple docstring''' pass def __a ( self ) -> Optional[Any]: '''simple docstring''' snake_case__ , snake_case__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case__ : List[Any] = model_class(__UpperCamelCase ) self.assertIsInstance(model.get_input_embeddings() , (tf.keras.layers.Layer) ) snake_case__ : List[Any] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(__UpperCamelCase , tf.keras.layers.Dense ) ) def __a ( self ) -> str: '''simple docstring''' snake_case__ , snake_case__ : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case__ : str = model_class(__UpperCamelCase ) snake_case__ : Tuple = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic snake_case__ : Optional[int] = [*signature.parameters.keys()] snake_case__ : str = ['pixel_values'] self.assertListEqual(arg_names[:1] , __UpperCamelCase ) def __a ( self ) -> str: '''simple docstring''' snake_case__ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__UpperCamelCase ) def __a ( self ) -> str: '''simple docstring''' snake_case__ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*__UpperCamelCase ) def __a ( self ) -> List[Any]: '''simple docstring''' snake_case__ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*__UpperCamelCase ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=False ) -> Tuple: '''simple docstring''' snake_case__ : Union[str, Any] = super()._prepare_for_class(__UpperCamelCase , __UpperCamelCase , return_labels=__UpperCamelCase ) if return_labels: if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call ).parameters: del inputs_dict["labels"] return inputs_dict @slow def __a ( self ) -> Tuple: '''simple docstring''' for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case__ : Dict = TFDeiTModel.from_pretrained(__UpperCamelCase ) self.assertIsNotNone(__UpperCamelCase ) def UpperCamelCase__ ( ) -> Any: snake_case__ : Tuple = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_tf @require_vision class __snake_case ( unittest.TestCase ): @cached_property def __a ( self ) -> Optional[Any]: '''simple docstring''' return ( DeiTImageProcessor.from_pretrained('facebook/deit-base-distilled-patch16-224' ) if is_vision_available() else None ) @slow def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Optional[Any] = TFDeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-224' ) snake_case__ : Tuple = self.default_image_processor snake_case__ : List[Any] = prepare_img() snake_case__ : Any = image_processor(images=__UpperCamelCase , return_tensors='tf' ) # forward pass snake_case__ : Optional[Any] = model(**__UpperCamelCase ) # verify the logits snake_case__ : List[Any] = tf.TensorShape((1, 1000) ) self.assertEqual(outputs.logits.shape , __UpperCamelCase ) snake_case__ : Union[str, Any] = tf.constant([-1.0_2_6_6, 0.1_9_1_2, -1.2_8_6_1] ) self.assertTrue(np.allclose(outputs.logits[0, :3] , __UpperCamelCase , atol=1E-4 ) )
699
def UpperCamelCase__ ( A__ ) -> list[int]: if length <= 0 or not isinstance(A__ , A__ ): raise ValueError('Length must be a positive integer.' ) return [n * (2 * n - 1) for n in range(A__ )] if __name__ == "__main__": print(hexagonal_numbers(length=5)) print(hexagonal_numbers(length=10))
699
1
def UpperCamelCase__ ( A__ ) -> list[int]: if length <= 0 or not isinstance(A__ , A__ ): raise ValueError('Length must be a positive integer.' ) return [n * (2 * n - 1) for n in range(A__ )] if __name__ == "__main__": print(hexagonal_numbers(length=5)) print(hexagonal_numbers(length=10))
699
import argparse import json from collections import OrderedDict from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( ConditionalDetrConfig, ConditionalDetrForObjectDetection, ConditionalDetrForSegmentation, ConditionalDetrImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase__ : Dict = logging.get_logger(__name__) # here we list all keys to be renamed (original name on the left, our name on the right) lowerCAmelCase__ : Optional[Any] = [] for i in range(6): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append( (F'''transformer.encoder.layers.{i}.self_attn.out_proj.weight''', F'''encoder.layers.{i}.self_attn.out_proj.weight''') ) rename_keys.append( (F'''transformer.encoder.layers.{i}.self_attn.out_proj.bias''', F'''encoder.layers.{i}.self_attn.out_proj.bias''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.linear1.weight''', F'''encoder.layers.{i}.fc1.weight''')) rename_keys.append((F'''transformer.encoder.layers.{i}.linear1.bias''', F'''encoder.layers.{i}.fc1.bias''')) rename_keys.append((F'''transformer.encoder.layers.{i}.linear2.weight''', F'''encoder.layers.{i}.fc2.weight''')) rename_keys.append((F'''transformer.encoder.layers.{i}.linear2.bias''', F'''encoder.layers.{i}.fc2.bias''')) rename_keys.append( (F'''transformer.encoder.layers.{i}.norm1.weight''', F'''encoder.layers.{i}.self_attn_layer_norm.weight''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.norm1.bias''', F'''encoder.layers.{i}.self_attn_layer_norm.bias''')) rename_keys.append((F'''transformer.encoder.layers.{i}.norm2.weight''', F'''encoder.layers.{i}.final_layer_norm.weight''')) rename_keys.append((F'''transformer.encoder.layers.{i}.norm2.bias''', F'''encoder.layers.{i}.final_layer_norm.bias''')) # decoder layers: 2 times output projection, 2 feedforward neural networks and 3 layernorms rename_keys.append( (F'''transformer.decoder.layers.{i}.self_attn.out_proj.weight''', F'''decoder.layers.{i}.self_attn.out_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.self_attn.out_proj.bias''', F'''decoder.layers.{i}.self_attn.out_proj.bias''') ) rename_keys.append( ( F'''transformer.decoder.layers.{i}.cross_attn.out_proj.weight''', F'''decoder.layers.{i}.encoder_attn.out_proj.weight''', ) ) rename_keys.append( ( F'''transformer.decoder.layers.{i}.cross_attn.out_proj.bias''', F'''decoder.layers.{i}.encoder_attn.out_proj.bias''', ) ) rename_keys.append((F'''transformer.decoder.layers.{i}.linear1.weight''', F'''decoder.layers.{i}.fc1.weight''')) rename_keys.append((F'''transformer.decoder.layers.{i}.linear1.bias''', F'''decoder.layers.{i}.fc1.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.linear2.weight''', F'''decoder.layers.{i}.fc2.weight''')) rename_keys.append((F'''transformer.decoder.layers.{i}.linear2.bias''', F'''decoder.layers.{i}.fc2.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.norm1.weight''', F'''decoder.layers.{i}.self_attn_layer_norm.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.norm1.bias''', F'''decoder.layers.{i}.self_attn_layer_norm.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.norm2.weight''', F'''decoder.layers.{i}.encoder_attn_layer_norm.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.norm2.bias''', F'''decoder.layers.{i}.encoder_attn_layer_norm.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.norm3.weight''', F'''decoder.layers.{i}.final_layer_norm.weight''')) rename_keys.append((F'''transformer.decoder.layers.{i}.norm3.bias''', F'''decoder.layers.{i}.final_layer_norm.bias''')) # q, k, v projections in self/cross-attention in decoder for conditional DETR rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_qcontent_proj.weight''', F'''decoder.layers.{i}.sa_qcontent_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_kcontent_proj.weight''', F'''decoder.layers.{i}.sa_kcontent_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_qpos_proj.weight''', F'''decoder.layers.{i}.sa_qpos_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_kpos_proj.weight''', F'''decoder.layers.{i}.sa_kpos_proj.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_v_proj.weight''', F'''decoder.layers.{i}.sa_v_proj.weight''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qcontent_proj.weight''', F'''decoder.layers.{i}.ca_qcontent_proj.weight''') ) # rename_keys.append((f"transformer.decoder.layers.{i}.ca_qpos_proj.weight", f"decoder.layers.{i}.ca_qpos_proj.weight")) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_kcontent_proj.weight''', F'''decoder.layers.{i}.ca_kcontent_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_kpos_proj.weight''', F'''decoder.layers.{i}.ca_kpos_proj.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.ca_v_proj.weight''', F'''decoder.layers.{i}.ca_v_proj.weight''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qpos_sine_proj.weight''', F'''decoder.layers.{i}.ca_qpos_sine_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_qcontent_proj.bias''', F'''decoder.layers.{i}.sa_qcontent_proj.bias''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_kcontent_proj.bias''', F'''decoder.layers.{i}.sa_kcontent_proj.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_qpos_proj.bias''', F'''decoder.layers.{i}.sa_qpos_proj.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_kpos_proj.bias''', F'''decoder.layers.{i}.sa_kpos_proj.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_v_proj.bias''', F'''decoder.layers.{i}.sa_v_proj.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qcontent_proj.bias''', F'''decoder.layers.{i}.ca_qcontent_proj.bias''') ) # rename_keys.append((f"transformer.decoder.layers.{i}.ca_qpos_proj.bias", f"decoder.layers.{i}.ca_qpos_proj.bias")) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_kcontent_proj.bias''', F'''decoder.layers.{i}.ca_kcontent_proj.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.ca_kpos_proj.bias''', F'''decoder.layers.{i}.ca_kpos_proj.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.ca_v_proj.bias''', F'''decoder.layers.{i}.ca_v_proj.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qpos_sine_proj.bias''', F'''decoder.layers.{i}.ca_qpos_sine_proj.bias''') ) # convolutional projection + query embeddings + layernorm of decoder + class and bounding box heads # for conditional DETR, also convert reference point head and query scale MLP rename_keys.extend( [ ('''input_proj.weight''', '''input_projection.weight'''), ('''input_proj.bias''', '''input_projection.bias'''), ('''query_embed.weight''', '''query_position_embeddings.weight'''), ('''transformer.decoder.norm.weight''', '''decoder.layernorm.weight'''), ('''transformer.decoder.norm.bias''', '''decoder.layernorm.bias'''), ('''class_embed.weight''', '''class_labels_classifier.weight'''), ('''class_embed.bias''', '''class_labels_classifier.bias'''), ('''bbox_embed.layers.0.weight''', '''bbox_predictor.layers.0.weight'''), ('''bbox_embed.layers.0.bias''', '''bbox_predictor.layers.0.bias'''), ('''bbox_embed.layers.1.weight''', '''bbox_predictor.layers.1.weight'''), ('''bbox_embed.layers.1.bias''', '''bbox_predictor.layers.1.bias'''), ('''bbox_embed.layers.2.weight''', '''bbox_predictor.layers.2.weight'''), ('''bbox_embed.layers.2.bias''', '''bbox_predictor.layers.2.bias'''), ('''transformer.decoder.ref_point_head.layers.0.weight''', '''decoder.ref_point_head.layers.0.weight'''), ('''transformer.decoder.ref_point_head.layers.0.bias''', '''decoder.ref_point_head.layers.0.bias'''), ('''transformer.decoder.ref_point_head.layers.1.weight''', '''decoder.ref_point_head.layers.1.weight'''), ('''transformer.decoder.ref_point_head.layers.1.bias''', '''decoder.ref_point_head.layers.1.bias'''), ('''transformer.decoder.query_scale.layers.0.weight''', '''decoder.query_scale.layers.0.weight'''), ('''transformer.decoder.query_scale.layers.0.bias''', '''decoder.query_scale.layers.0.bias'''), ('''transformer.decoder.query_scale.layers.1.weight''', '''decoder.query_scale.layers.1.weight'''), ('''transformer.decoder.query_scale.layers.1.bias''', '''decoder.query_scale.layers.1.bias'''), ('''transformer.decoder.layers.0.ca_qpos_proj.weight''', '''decoder.layers.0.ca_qpos_proj.weight'''), ('''transformer.decoder.layers.0.ca_qpos_proj.bias''', '''decoder.layers.0.ca_qpos_proj.bias'''), ] ) def UpperCamelCase__ ( A__ , A__ , A__ ) -> List[str]: snake_case__ : int = state_dict.pop(A__ ) snake_case__ : Union[str, Any] = val def UpperCamelCase__ ( A__ ) -> int: snake_case__ : List[Any] = OrderedDict() for key, value in state_dict.items(): if "backbone.0.body" in key: snake_case__ : Any = key.replace('backbone.0.body' , 'backbone.conv_encoder.model' ) snake_case__ : Optional[int] = value else: snake_case__ : Optional[int] = value return new_state_dict def UpperCamelCase__ ( A__ , A__=False ) -> Optional[int]: snake_case__ : Optional[int] = '' if is_panoptic: snake_case__ : Tuple = 'conditional_detr.' # first: transformer encoder for i in range(6 ): # read in weights + bias of input projection layer (in PyTorch's MultiHeadAttention, this is a single matrix + bias) snake_case__ : int = state_dict.pop(F"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_weight""" ) snake_case__ : str = state_dict.pop(F"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict snake_case__ : Union[str, Any] = in_proj_weight[:256, :] snake_case__ : Union[str, Any] = in_proj_bias[:256] snake_case__ : Union[str, Any] = in_proj_weight[256:512, :] snake_case__ : Optional[Any] = in_proj_bias[256:512] snake_case__ : List[str] = in_proj_weight[-256:, :] snake_case__ : Tuple = in_proj_bias[-256:] def UpperCamelCase__ ( ) -> Tuple: snake_case__ : int = 'http://images.cocodataset.org/val2017/000000039769.jpg' snake_case__ : str = Image.open(requests.get(A__ , stream=A__ ).raw ) return im @torch.no_grad() def UpperCamelCase__ ( A__ , A__ ) -> str: snake_case__ : List[Any] = ConditionalDetrConfig() # set backbone and dilation attributes if "resnet101" in model_name: snake_case__ : Any = 'resnet101' if "dc5" in model_name: snake_case__ : Any = True snake_case__ : int = 'panoptic' in model_name if is_panoptic: snake_case__ : str = 250 else: snake_case__ : Union[str, Any] = 91 snake_case__ : Optional[int] = 'huggingface/label-files' snake_case__ : Optional[Any] = 'coco-detection-id2label.json' snake_case__ : str = json.load(open(hf_hub_download(A__ , A__ , repo_type='dataset' ) , 'r' ) ) snake_case__ : List[Any] = {int(A__ ): v for k, v in idalabel.items()} snake_case__ : Any = idalabel snake_case__ : int = {v: k for k, v in idalabel.items()} # load image processor snake_case__ : List[Any] = 'coco_panoptic' if is_panoptic else 'coco_detection' snake_case__ : List[Any] = ConditionalDetrImageProcessor(format=A__ ) # prepare image snake_case__ : List[str] = prepare_img() snake_case__ : Any = image_processor(images=A__ , return_tensors='pt' ) snake_case__ : Dict = encoding['pixel_values'] logger.info(F"""Converting model {model_name}...""" ) # load original model from torch hub snake_case__ : Any = torch.hub.load('DeppMeng/ConditionalDETR' , A__ , pretrained=A__ ).eval() snake_case__ : Tuple = conditional_detr.state_dict() # rename keys for src, dest in rename_keys: if is_panoptic: snake_case__ : List[Any] = 'conditional_detr.' + src rename_key(A__ , A__ , A__ ) snake_case__ : Dict = rename_backbone_keys(A__ ) # query, key and value matrices need special treatment read_in_q_k_v(A__ , is_panoptic=A__ ) # important: we need to prepend a prefix to each of the base model keys as the head models use different attributes for them snake_case__ : Optional[int] = 'conditional_detr.model.' if is_panoptic else 'model.' for key in state_dict.copy().keys(): if is_panoptic: if ( key.startswith('conditional_detr' ) and not key.startswith('class_labels_classifier' ) and not key.startswith('bbox_predictor' ) ): snake_case__ : List[Any] = state_dict.pop(A__ ) snake_case__ : Optional[int] = val elif "class_labels_classifier" in key or "bbox_predictor" in key: snake_case__ : str = state_dict.pop(A__ ) snake_case__ : List[Any] = val elif key.startswith('bbox_attention' ) or key.startswith('mask_head' ): continue else: snake_case__ : Union[str, Any] = state_dict.pop(A__ ) snake_case__ : Dict = val else: if not key.startswith('class_labels_classifier' ) and not key.startswith('bbox_predictor' ): snake_case__ : List[Any] = state_dict.pop(A__ ) snake_case__ : Optional[int] = val # finally, create HuggingFace model and load state dict snake_case__ : Union[str, Any] = ConditionalDetrForSegmentation(A__ ) if is_panoptic else ConditionalDetrForObjectDetection(A__ ) model.load_state_dict(A__ ) model.eval() model.push_to_hub(repo_id=A__ , organization='DepuMeng' , commit_message='Add model' ) # verify our conversion snake_case__ : Tuple = conditional_detr(A__ ) snake_case__ : str = model(A__ ) assert torch.allclose(outputs.logits , original_outputs['pred_logits'] , atol=1e-4 ) assert torch.allclose(outputs.pred_boxes , original_outputs['pred_boxes'] , atol=1e-4 ) if is_panoptic: assert torch.allclose(outputs.pred_masks , original_outputs['pred_masks'] , atol=1e-4 ) # Save model and image processor logger.info(F"""Saving PyTorch model and image processor to {pytorch_dump_folder_path}...""" ) Path(A__ ).mkdir(exist_ok=A__ ) model.save_pretrained(A__ ) image_processor.save_pretrained(A__ ) if __name__ == "__main__": lowerCAmelCase__ : Any = argparse.ArgumentParser() parser.add_argument( '''--model_name''', default='''conditional_detr_resnet50''', type=str, help='''Name of the CONDITIONAL_DETR model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the folder to output PyTorch model.''' ) lowerCAmelCase__ : int = parser.parse_args() convert_conditional_detr_checkpoint(args.model_name, args.pytorch_dump_folder_path)
699
1
import argparse import logging import os from datetime import datetime import numpy as np import torch from torch import nn from torch.utils.data import DataLoader, RandomSampler, TensorDataset from tqdm import tqdm from transformers import GPTaLMHeadModel lowerCAmelCase__ : Dict = logging.getLogger(__name__) def UpperCamelCase__ ( A__ , A__ ) -> Any: # save results if os.path.exists(A__ ): if os.path.exists(os.path.join(A__ , 'config.json' ) ) and os.path.isfile( os.path.join(A__ , 'config.json' ) ): os.remove(os.path.join(A__ , 'config.json' ) ) if os.path.exists(os.path.join(A__ , 'pytorch_model.bin' ) ) and os.path.isfile( os.path.join(A__ , 'pytorch_model.bin' ) ): os.remove(os.path.join(A__ , 'pytorch_model.bin' ) ) else: os.makedirs(A__ ) model.save_pretrained(A__ ) def UpperCamelCase__ ( A__ , A__=False ) -> Optional[Any]: snake_case__ : str = 2 if unlogit: snake_case__ : Optional[int] = torch.pow(A__ , A__ ) snake_case__ : Any = p * torch.log(A__ ) snake_case__ : Dict = 0 return -plogp.sum(dim=-1 ) def UpperCamelCase__ ( A__ ) -> str: logger.info('lv, h >\t' + '\t'.join(F"""{x + 1}""" for x in range(len(A__ ) ) ) ) for row in range(len(A__ ) ): if tensor.dtype != torch.long: logger.info(F"""layer {row + 1}:\t""" + '\t'.join(F"""{x:.5f}""" for x in tensor[row].cpu().data ) ) else: logger.info(F"""layer {row + 1}:\t""" + '\t'.join(F"""{x:d}""" for x in tensor[row].cpu().data ) ) def UpperCamelCase__ ( A__ , A__ , A__ , A__=True , A__=True , A__=None , A__=False ) -> Any: snake_case__ , snake_case__ : List[Any] = model.config.num_hidden_layers, model.config.num_attention_heads snake_case__ : Any = torch.zeros(A__ , A__ ).to(args.device ) snake_case__ : str = torch.zeros(A__ , A__ ).to(args.device ) if head_mask is None: snake_case__ : int = torch.ones(A__ , A__ ).to(args.device ) head_mask.requires_grad_(requires_grad=A__ ) # If actually pruned attention multi-head, set head mask to None to avoid shape mismatch if actually_pruned: snake_case__ : Tuple = None snake_case__ : int = 0.0 snake_case__ : Union[str, Any] = 0.0 for step, inputs in enumerate(tqdm(A__ , desc='Iteration' , disable=args.local_rank not in [-1, 0] ) ): snake_case__ : List[Any] = tuple(t.to(args.device ) for t in inputs ) ((snake_case__) , ) : Optional[Any] = inputs # Do a forward pass (not with torch.no_grad() since we need gradients for importance score - see below) snake_case__ : List[str] = model(A__ , labels=A__ , head_mask=A__ ) # (loss), lm_logits, presents, (all hidden_states), (attentions) snake_case__ , snake_case__ , snake_case__ : List[Any] = ( outputs[0], outputs[1], outputs[-1], ) # Loss and logits are the first, attention the last loss.backward() # Backpropagate to populate the gradients in the head mask total_loss += loss.detach().cpu().numpy() if compute_entropy: for layer, attn in enumerate(A__ ): snake_case__ : List[Any] = entropy(attn.detach() , A__ ) attn_entropy[layer] += masked_entropy.sum(-1 ).sum(0 ).sum(0 ).detach() if compute_importance: head_importance += head_mask.grad.abs().detach() tot_tokens += torch.ones_like(A__ ).float().detach().sum().data # Normalize attn_entropy /= tot_tokens head_importance /= tot_tokens # Layerwise importance normalization if not args.dont_normalize_importance_by_layer: snake_case__ : Dict = 2 snake_case__ : Optional[int] = torch.pow(torch.pow(A__ , A__ ).sum(-1 ) , 1 / exponent ) head_importance /= norm_by_layer.unsqueeze(-1 ) + 1e-2_0 if not args.dont_normalize_global_importance: snake_case__ : Optional[int] = (head_importance - head_importance.min()) / (head_importance.max() - head_importance.min()) # Print matrices if compute_entropy: logger.info('Attention entropies' ) print_ad_tensor(A__ ) if compute_importance: logger.info('Head importance scores' ) print_ad_tensor(A__ ) logger.info('Head ranked by importance scores' ) snake_case__ : List[str] = torch.zeros(head_importance.numel() , dtype=torch.long , device=args.device ) snake_case__ : Any = torch.arange( head_importance.numel() , device=args.device ) snake_case__ : str = head_ranks.view_as(A__ ) print_ad_tensor(A__ ) return attn_entropy, head_importance, total_loss def UpperCamelCase__ ( A__ , A__ , A__ ) -> Optional[int]: snake_case__ , snake_case__ , snake_case__ : int = compute_heads_importance(A__ , A__ , A__ , compute_entropy=A__ ) snake_case__ : List[str] = 1 / loss # instead of downsteam score use the LM loss logger.info('Pruning: original score: %f, threshold: %f' , A__ , original_score * args.masking_threshold ) snake_case__ : Union[str, Any] = torch.ones_like(A__ ) snake_case__ : Optional[int] = max(1 , int(new_head_mask.numel() * args.masking_amount ) ) snake_case__ : List[Any] = original_score while current_score >= original_score * args.masking_threshold: snake_case__ : Any = new_head_mask.clone().detach() # save current head mask # heads from least important to most - keep only not-masked heads snake_case__ : Any = float('Inf' ) snake_case__ : List[str] = head_importance.view(-1 ).sort()[1] if len(A__ ) <= num_to_mask: print('BREAK BY num_to_mask' ) break # mask heads snake_case__ : str = current_heads_to_mask[:num_to_mask] logger.info('Heads to mask: %s' , str(current_heads_to_mask.tolist() ) ) snake_case__ : List[Any] = new_head_mask.view(-1 ) snake_case__ : Optional[Any] = 0.0 snake_case__ : Optional[int] = new_head_mask.view_as(A__ ) snake_case__ : str = new_head_mask.clone().detach() print_ad_tensor(A__ ) # Compute metric and head importance again snake_case__ , snake_case__ , snake_case__ : Any = compute_heads_importance( A__ , A__ , A__ , compute_entropy=A__ , head_mask=A__ ) snake_case__ : Dict = 1 / loss logger.info( 'Masking: current score: %f, remaining heads %d (%.1f percents)' , A__ , new_head_mask.sum() , new_head_mask.sum() / new_head_mask.numel() * 100 , ) logger.info('Final head mask' ) print_ad_tensor(A__ ) np.save(os.path.join(args.output_dir , 'head_mask.npy' ) , head_mask.detach().cpu().numpy() ) return head_mask def UpperCamelCase__ ( A__ , A__ , A__ , A__ ) -> Union[str, Any]: snake_case__ : List[Any] = datetime.now() snake_case__ , snake_case__ , snake_case__ : str = compute_heads_importance( A__ , A__ , A__ , compute_entropy=A__ , compute_importance=A__ , head_mask=A__ ) snake_case__ : List[Any] = 1 / loss snake_case__ : Tuple = datetime.now() - before_time snake_case__ : List[str] = sum(p.numel() for p in model.parameters() ) snake_case__ : Any = { layer: (1 - head_mask[layer].long()).nonzero().squeeze().tolist() for layer in range(len(A__ ) ) } for k, v in heads_to_prune.items(): if isinstance(A__ , A__ ): snake_case__ : Any = [ v, ] assert sum(len(A__ ) for h in heads_to_prune.values() ) == (1 - head_mask.long()).sum().item() model.prune_heads(A__ ) snake_case__ : Dict = sum(p.numel() for p in model.parameters() ) snake_case__ : Optional[Any] = datetime.now() snake_case__ , snake_case__ , snake_case__ : Optional[Any] = compute_heads_importance( A__ , A__ , A__ , compute_entropy=A__ , compute_importance=A__ , head_mask=A__ , actually_pruned=A__ , ) snake_case__ : List[Any] = 1 / loss snake_case__ : Dict = datetime.now() - before_time logger.info( 'Pruning: original num of params: %.2e, after pruning %.2e (%.1f percents)' , A__ , A__ , pruned_num_params / original_num_params * 100 , ) logger.info('Pruning: score with masking: %f score with pruning: %f' , A__ , A__ ) logger.info('Pruning: speed ratio (original timing / new timing): %f percents' , original_time / new_time * 100 ) save_model(A__ , args.output_dir ) def UpperCamelCase__ ( ) -> Any: snake_case__ : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument( '--data_dir' , default=A__ , type=A__ , required=A__ , help='The input data dir. Should contain the .tsv files (or other data files) for the task.' , ) parser.add_argument( '--model_name_or_path' , default=A__ , type=A__ , required=A__ , help='Path to pretrained model or model identifier from huggingface.co/models' , ) parser.add_argument( '--output_dir' , default=A__ , type=A__ , required=A__ , help='The output directory where the model predictions and checkpoints will be written.' , ) # Other parameters parser.add_argument( '--config_name' , default='' , type=A__ , help='Pretrained config name or path if not the same as model_name_or_path' , ) parser.add_argument( '--tokenizer_name' , default='' , type=A__ , help='Pretrained tokenizer name or path if not the same as model_name_or_path' , ) parser.add_argument( '--cache_dir' , default=A__ , type=A__ , help='Where do you want to store the pre-trained models downloaded from s3' , ) parser.add_argument( '--data_subset' , type=A__ , default=-1 , help='If > 0: limit the data to a subset of data_subset instances.' ) parser.add_argument( '--overwrite_output_dir' , action='store_true' , help='Whether to overwrite data in output directory' ) parser.add_argument( '--overwrite_cache' , action='store_true' , help='Overwrite the cached training and evaluation sets' ) parser.add_argument( '--dont_normalize_importance_by_layer' , action='store_true' , help='Don\'t normalize importance score by layers' ) parser.add_argument( '--dont_normalize_global_importance' , action='store_true' , help='Don\'t normalize all importance scores between 0 and 1' , ) parser.add_argument( '--try_masking' , action='store_true' , help='Whether to try to mask head until a threshold of accuracy.' ) parser.add_argument( '--masking_threshold' , default=0.9 , type=A__ , help='masking threshold in term of metrics (stop masking when metric < threshold * original metric value).' , ) parser.add_argument( '--masking_amount' , default=0.1 , type=A__ , help='Amount to heads to masking at each masking step.' ) parser.add_argument('--metric_name' , default='acc' , type=A__ , help='Metric to use for head masking.' ) parser.add_argument( '--max_seq_length' , default=128 , type=A__ , help=( 'The maximum total input sequence length after WordPiece tokenization. \n' 'Sequences longer than this will be truncated, sequences shorter padded.' ) , ) parser.add_argument('--batch_size' , default=1 , type=A__ , help='Batch size.' ) parser.add_argument('--seed' , type=A__ , default=42 ) parser.add_argument('--local_rank' , type=A__ , default=-1 , help='local_rank for distributed training on gpus' ) parser.add_argument('--no_cuda' , action='store_true' , help='Whether not to use CUDA when available' ) parser.add_argument('--server_ip' , type=A__ , default='' , help='Can be used for distant debugging.' ) parser.add_argument('--server_port' , type=A__ , default='' , help='Can be used for distant debugging.' ) snake_case__ : str = parser.parse_args() if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print('Waiting for debugger attach' ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=A__ ) ptvsd.wait_for_attach() # Setup devices and distributed training if args.local_rank == -1 or args.no_cuda: snake_case__ : Any = torch.device('cuda' if torch.cuda.is_available() and not args.no_cuda else 'cpu' ) snake_case__ : int = 0 if args.no_cuda else torch.cuda.device_count() else: torch.cuda.set_device(args.local_rank ) snake_case__ : Any = torch.device('cuda' , args.local_rank ) snake_case__ : str = 1 torch.distributed.init_process_group(backend='nccl' ) # Initializes the distributed backend # Setup logging logging.basicConfig(level=logging.INFO if args.local_rank in [-1, 0] else logging.WARN ) logger.info('device: {} n_gpu: {}, distributed: {}'.format(args.device , args.n_gpu , bool(args.local_rank != -1 ) ) ) snake_case__ : int = GPTaLMHeadModel.from_pretrained(args.model_name_or_path ) # Distributed and parallel training model.to(args.device ) if args.local_rank != -1: snake_case__ : Any = nn.parallel.DistributedDataParallel( A__ , device_ids=[args.local_rank] , output_device=args.local_rank , find_unused_parameters=A__ ) elif args.n_gpu > 1: snake_case__ : Tuple = nn.DataParallel(A__ ) # Print/save training arguments os.makedirs(args.output_dir , exist_ok=A__ ) torch.save(A__ , os.path.join(args.output_dir , 'run_args.bin' ) ) logger.info('Training/evaluation parameters %s' , A__ ) # Prepare dataset snake_case__ : Union[str, Any] = np.concatenate( [ np.loadtxt(args.data_dir , dtype=np.intaa ), ] ) snake_case__ : Dict = (torch.from_numpy(A__ ),) snake_case__ : List[Any] = TensorDataset(*A__ ) snake_case__ : int = RandomSampler(A__ ) snake_case__ : Optional[int] = DataLoader(A__ , sampler=A__ , batch_size=args.batch_size ) # Compute head entropy and importance score compute_heads_importance(A__ , A__ , A__ ) # Try head masking (set heads to zero until the score goes under a threshole) # and head pruning (remove masked heads and see the effect on the network) if args.try_masking and args.masking_threshold > 0.0 and args.masking_threshold < 1.0: snake_case__ : Union[str, Any] = mask_heads(A__ , A__ , A__ ) prune_heads(A__ , A__ , A__ , A__ ) if __name__ == "__main__": main()
699
from collections import namedtuple lowerCAmelCase__ : Union[str, Any] = namedtuple('''from_to''', '''from_ to''') lowerCAmelCase__ : Tuple = { '''cubicmeter''': from_to(1, 1), '''litre''': from_to(0.0_01, 10_00), '''kilolitre''': from_to(1, 1), '''gallon''': from_to(0.0_04_54, 2_64.1_72), '''cubicyard''': from_to(0.7_64_55, 1.3_07_95), '''cubicfoot''': from_to(0.0_28, 35.31_47), '''cup''': from_to(0.0_00_23_65_88, 42_26.75), } def UpperCamelCase__ ( A__ , A__ , A__ ) -> float: if from_type not in METRIC_CONVERSION: raise ValueError( F"""Invalid 'from_type' value: {from_type!r} Supported values are:\n""" + ', '.join(A__ ) ) if to_type not in METRIC_CONVERSION: raise ValueError( F"""Invalid 'to_type' value: {to_type!r}. Supported values are:\n""" + ', '.join(A__ ) ) return value * METRIC_CONVERSION[from_type].from_ * METRIC_CONVERSION[to_type].to if __name__ == "__main__": import doctest doctest.testmod()
699
1
import argparse import json import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils.deepspeed import DummyOptim, DummyScheduler lowerCAmelCase__ : Optional[int] = 16 lowerCAmelCase__ : List[str] = 32 def UpperCamelCase__ ( A__ , A__ = 16 , A__ = "bert-base-cased" ) -> Tuple: snake_case__ : Union[str, Any] = AutoTokenizer.from_pretrained(A__ ) snake_case__ : List[Any] = load_dataset('glue' , 'mrpc' ) def tokenize_function(A__ ): # max_length=None => use the model max length (it's actually the default) snake_case__ : Union[str, Any] = tokenizer(examples['sentence1'] , examples['sentence2'] , truncation=A__ , max_length=A__ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset snake_case__ : Union[str, Any] = datasets.map( A__ , batched=A__ , remove_columns=['idx', 'sentence1', 'sentence2'] , load_from_cache_file=A__ ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library snake_case__ : Any = tokenized_datasets.rename_column('label' , 'labels' ) def collate_fn(A__ ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(A__ , padding='max_length' , max_length=128 , return_tensors='pt' ) return tokenizer.pad(A__ , padding='longest' , return_tensors='pt' ) # Instantiate dataloaders. snake_case__ : List[Any] = DataLoader( tokenized_datasets['train'] , shuffle=A__ , collate_fn=A__ , batch_size=A__ ) snake_case__ : Optional[int] = DataLoader( tokenized_datasets['validation'] , shuffle=A__ , collate_fn=A__ , batch_size=A__ ) return train_dataloader, eval_dataloader def UpperCamelCase__ ( A__ , A__ ) -> int: # Initialize accelerator snake_case__ : Optional[int] = Accelerator() # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs snake_case__ : Optional[int] = config['lr'] snake_case__ : Union[str, Any] = int(config['num_epochs'] ) snake_case__ : List[str] = int(config['seed'] ) snake_case__ : int = int(config['batch_size'] ) snake_case__ : Optional[Any] = args.model_name_or_path set_seed(A__ ) snake_case__ , snake_case__ : Any = get_dataloaders(A__ , A__ , A__ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) snake_case__ : List[str] = AutoModelForSequenceClassification.from_pretrained(A__ , return_dict=A__ ) # Instantiate optimizer snake_case__ : Optional[Any] = ( AdamW if accelerator.state.deepspeed_plugin is None or 'optimizer' not in accelerator.state.deepspeed_plugin.deepspeed_config else DummyOptim ) snake_case__ : Any = optimizer_cls(params=model.parameters() , lr=A__ ) if accelerator.state.deepspeed_plugin is not None: snake_case__ : Tuple = accelerator.state.deepspeed_plugin.deepspeed_config[ 'gradient_accumulation_steps' ] else: snake_case__ : Any = 1 snake_case__ : List[Any] = (len(A__ ) * num_epochs) // gradient_accumulation_steps # Instantiate scheduler if ( accelerator.state.deepspeed_plugin is None or "scheduler" not in accelerator.state.deepspeed_plugin.deepspeed_config ): snake_case__ : List[Any] = get_linear_schedule_with_warmup( optimizer=A__ , num_warmup_steps=0 , num_training_steps=A__ , ) else: snake_case__ : Union[str, Any] = DummyScheduler(A__ , total_num_steps=A__ , warmup_num_steps=0 ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ : int = accelerator.prepare( A__ , A__ , A__ , A__ , A__ ) # We need to keep track of how many total steps we have iterated over snake_case__ : Optional[int] = 0 # We also need to keep track of the stating epoch so files are named properly snake_case__ : List[str] = 0 # Now we train the model snake_case__ : str = evaluate.load('glue' , 'mrpc' ) snake_case__ : int = 0 snake_case__ : Optional[Any] = {} for epoch in range(A__ , A__ ): model.train() for step, batch in enumerate(A__ ): snake_case__ : Union[str, Any] = model(**A__ ) snake_case__ : Dict = outputs.loss snake_case__ : Tuple = loss / gradient_accumulation_steps accelerator.backward(A__ ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 model.eval() snake_case__ : Any = 0 for step, batch in enumerate(A__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): snake_case__ : Optional[int] = model(**A__ ) snake_case__ : Union[str, Any] = outputs.logits.argmax(dim=-1 ) # It is slightly faster to call this once, than multiple times snake_case__ , snake_case__ : int = accelerator.gather( (predictions, batch['labels']) ) # If we are in a multiprocess environment, the last batch has duplicates if accelerator.use_distributed: if step == len(A__ ) - 1: snake_case__ : Optional[int] = predictions[: len(eval_dataloader.dataset ) - samples_seen] snake_case__ : Union[str, Any] = references[: len(eval_dataloader.dataset ) - samples_seen] else: samples_seen += references.shape[0] metric.add_batch( predictions=A__ , references=A__ , ) snake_case__ : Union[str, Any] = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F"""epoch {epoch}:""" , A__ ) snake_case__ : List[Any] = eval_metric['accuracy'] if best_performance < eval_metric["accuracy"]: snake_case__ : str = eval_metric['accuracy'] if args.performance_lower_bound is not None: assert ( args.performance_lower_bound <= best_performance ), F"""Best performance metric {best_performance} is lower than the lower bound {args.performance_lower_bound}""" accelerator.wait_for_everyone() if accelerator.is_main_process: with open(os.path.join(args.output_dir , 'all_results.json' ) , 'w' ) as f: json.dump(A__ , A__ ) def UpperCamelCase__ ( ) -> str: snake_case__ : str = argparse.ArgumentParser(description='Simple example of training script tracking peak GPU memory usage.' ) parser.add_argument( '--model_name_or_path' , type=A__ , default='bert-base-cased' , help='Path to pretrained model or model identifier from huggingface.co/models.' , required=A__ , ) parser.add_argument( '--output_dir' , type=A__ , default='.' , help='Optional save directory where all checkpoint folders will be stored. Default is the current working directory.' , ) parser.add_argument( '--performance_lower_bound' , type=A__ , default=A__ , help='Optional lower bound for the performance metric. If set, the training will throw error when the performance metric drops below this value.' , ) parser.add_argument( '--num_epochs' , type=A__ , default=3 , help='Number of train epochs.' , ) snake_case__ : Union[str, Any] = parser.parse_args() snake_case__ : List[Any] = {'lr': 2e-5, 'num_epochs': args.num_epochs, 'seed': 42, 'batch_size': 16} training_function(A__ , A__ ) if __name__ == "__main__": main()
699
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCAmelCase__ : Tuple = logging.get_logger(__name__) lowerCAmelCase__ : Union[str, Any] = '''▁''' lowerCAmelCase__ : List[Any] = {'''vocab_file''': '''sentencepiece.bpe.model'''} lowerCAmelCase__ : Optional[Any] = { '''vocab_file''': { '''facebook/xglm-564M''': '''https://huggingface.co/facebook/xglm-564M/resolve/main/sentencepiece.bpe.model''', } } lowerCAmelCase__ : str = { '''facebook/xglm-564M''': 20_48, } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = ["""input_ids""", """attention_mask"""] def __init__( self , __UpperCamelCase , __UpperCamelCase="<s>" , __UpperCamelCase="</s>" , __UpperCamelCase="</s>" , __UpperCamelCase="<s>" , __UpperCamelCase="<unk>" , __UpperCamelCase="<pad>" , __UpperCamelCase = None , **__UpperCamelCase , ) -> None: '''simple docstring''' snake_case__ : Any = {} if sp_model_kwargs is None else sp_model_kwargs # Compatibility with the original tokenizer snake_case__ : Tuple = 7 snake_case__ : Dict = [F"""<madeupword{i}>""" for i in range(self.num_madeup_words )] snake_case__ : Union[str, Any] = kwargs.get('additional_special_tokens' , [] ) kwargs["additional_special_tokens"] += [ word for word in madeup_words if word not in kwargs["additional_special_tokens"] ] super().__init__( bos_token=__UpperCamelCase , eos_token=__UpperCamelCase , unk_token=__UpperCamelCase , sep_token=__UpperCamelCase , cls_token=__UpperCamelCase , pad_token=__UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , **__UpperCamelCase , ) snake_case__ : Optional[int] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(__UpperCamelCase ) ) snake_case__ : Optional[Any] = 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' # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab snake_case__ : Tuple = 1 # Mimic fairseq token-to-id alignment for the first 4 token snake_case__ : Tuple = {'<s>': 0, '<pad>': 1, '</s>': 2, '<unk>': 3} snake_case__ : List[Any] = len(self.sp_model ) snake_case__ : Optional[Any] = {F"""<madeupword{i}>""": sp_size + i + self.fairseq_offset for i in range(self.num_madeup_words )} self.fairseq_tokens_to_ids.update(__UpperCamelCase ) snake_case__ : Union[str, Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def __getstate__( self ) -> List[Any]: '''simple docstring''' snake_case__ : Union[str, Any] = self.__dict__.copy() snake_case__ : Optional[Any] = None snake_case__ : Tuple = self.sp_model.serialized_model_proto() return state def __setstate__( self , __UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Union[str, Any] = d # for backward compatibility if not hasattr(self , 'sp_model_kwargs' ): snake_case__ : Any = {} snake_case__ : Dict = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.LoadFromSerializedProto(self.sp_model_proto ) def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return [self.sep_token_id] + token_ids_a snake_case__ : str = [self.sep_token_id] return sep + token_ids_a + sep + sep + token_ids_a def __a ( self , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__UpperCamelCase , token_ids_a=__UpperCamelCase , already_has_special_tokens=__UpperCamelCase ) if token_ids_a is None: return [1] + ([0] * len(__UpperCamelCase )) return [1] + ([0] * len(__UpperCamelCase )) + [1, 1] + ([0] * len(__UpperCamelCase )) def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> List[int]: '''simple docstring''' snake_case__ : int = [self.sep_token_id] if token_ids_a is None: return len(sep + token_ids_a ) * [0] return len(sep + token_ids_a + sep + sep + token_ids_a ) * [0] @property def __a ( self ) -> Tuple: '''simple docstring''' return len(self.sp_model ) + self.fairseq_offset + self.num_madeup_words def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : int = {self.convert_ids_to_tokens(__UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __a ( self , __UpperCamelCase ) -> List[str]: '''simple docstring''' return self.sp_model.encode(__UpperCamelCase , out_type=__UpperCamelCase ) def __a ( self , __UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] snake_case__ : Optional[Any] = self.sp_model.PieceToId(__UpperCamelCase ) # 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 __a ( self , __UpperCamelCase ) -> Dict: '''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 __a ( self , __UpperCamelCase ) -> int: '''simple docstring''' snake_case__ : int = ''.join(__UpperCamelCase ).replace(__UpperCamelCase , ' ' ).strip() return out_string def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(__UpperCamelCase ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return snake_case__ : List[str] = 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __UpperCamelCase ) elif not os.path.isfile(self.vocab_file ): with open(__UpperCamelCase , 'wb' ) as fi: snake_case__ : Any = self.sp_model.serialized_model_proto() fi.write(__UpperCamelCase ) return (out_vocab_file,)
699
1